PR c++/84338 - wrong variadic sizeof.
[official-gcc.git] / gcc / cp / pt.c
bloba83b7073d20f6d79ef2209f2cdc870fc25c50b1f
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 (tree args)
1139 ++processing_template_decl;
1140 if (!any_dependent_template_arguments_p (args))
1142 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1153 else
1154 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 --processing_template_decl;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1174 static tree
1175 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1177 if (tmpl == NULL_TREE)
1178 return NULL_TREE;
1180 if (args == error_mark_node)
1181 return NULL_TREE;
1183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl) == FIELD_DECL);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args)
1189 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1191 : template_class_depth (DECL_CONTEXT (tmpl))));
1193 if (flag_checking)
1194 verify_unstripped_args (args);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl))
1199 return NULL_TREE;
1201 if (optimize_specialization_lookup_p (tmpl))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1205 arguments. */
1206 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1207 tree class_specialization
1208 = retrieve_specialization (class_template, args, 0);
1209 if (!class_specialization)
1210 return NULL_TREE;
1212 /* Find the instance of TMPL. */
1213 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1214 for (ovl_iterator iter (fns); iter; ++iter)
1216 tree fn = *iter;
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1223 return NULL_TREE;
1225 else
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1247 return NULL_TREE;
1250 /* Like retrieve_specialization, but for local declarations. */
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl, tree tmpl)
1267 tree t;
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1277 else
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1289 return 0;
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1298 bool need_template = true;
1299 int template_depth;
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1339 void f();
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1358 and DECL's are
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1372 while (current_depth < template_depth)
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1382 context = TYPE_CONTEXT (context);
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1412 else
1413 decl_type = TREE_TYPE (decl);
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1433 return compparms (decl_args_type, friend_args_type);
1435 else
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1471 return false;
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1548 else
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1595 check_specialization_namespace (tmpl);
1597 return fn;
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1602 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1613 return fn;
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1646 return spec;
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations;
1653 bool
1654 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1656 int equal;
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1673 --comparing_specializations;
1675 return equal;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1680 static hashval_t
1681 hash_tmpl_and_args (tree tmpl, tree args)
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1690 hashval_t
1691 spec_hasher::hash (spec_entry *e)
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1699 hashval_t
1700 iterative_hash_template_arg (tree arg, hashval_t val)
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1718 val = iterative_hash_object (code, val);
1720 switch (code)
1722 case ERROR_MARK:
1723 return val;
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1728 case TREE_VEC:
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1750 case OVERLOAD:
1751 for (lkp_iterator iter (arg); iter; ++iter)
1752 val = iterative_hash_template_arg (*iter, val);
1753 return val;
1755 case CONSTRUCTOR:
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1764 return val;
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1822 default:
1823 break;
1826 switch (tclass)
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1862 gcc_unreachable ();
1863 return 0;
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1872 bool
1873 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1875 spec_entry *entry;
1876 spec_entry elt;
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1891 return 0;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1897 void
1898 register_local_specialization (tree spec, tree tmpl)
1900 gcc_assert (tmpl != spec);
1901 local_specializations->put (tmpl, spec);
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1907 bool
1908 explicit_class_specialization_p (tree type)
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1911 return false;
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1918 overloads.
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1925 static void
1926 print_candidates_1 (tree fns, char **str, bool more = false)
1928 if (TREE_CODE (fns) == TREE_LIST)
1929 for (; fns; fns = TREE_CHAIN (fns))
1930 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1931 else
1932 for (lkp_iterator iter (fns); iter;)
1934 tree cand = *iter;
1935 ++iter;
1937 const char *pfx = *str;
1938 if (!pfx)
1940 if (more || iter)
1941 pfx = _("candidates are:");
1942 else
1943 pfx = _("candidate is:");
1944 *str = get_spaces (pfx);
1946 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1953 void
1954 print_candidates (tree fns)
1956 char *str = NULL;
1957 print_candidates_1 (fns, &str);
1958 free (str);
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1963 static tree
1964 get_template_for_ordering (tree list)
1966 gcc_assert (TREE_CODE (list) == TREE_LIST);
1967 tree f = TREE_VALUE (list);
1968 if (tree ti = DECL_TEMPLATE_INFO (f))
1969 return TI_TEMPLATE (ti);
1970 return f;
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1977 most constrained.
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1981 static tree
1982 most_constrained_function (tree candidates)
1984 // Try to find the best candidate in a first pass.
1985 tree champ = candidates;
1986 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1988 int winner = more_constrained (get_template_for_ordering (champ),
1989 get_template_for_ordering (c));
1990 if (winner == -1)
1991 champ = c; // The candidate is more constrained
1992 else if (winner == 0)
1993 return NULL_TREE; // Neither is more constrained
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1998 if (!more_constrained (get_template_for_ordering (champ),
1999 get_template_for_ordering (c)))
2000 return NULL_TREE;
2003 return champ;
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2031 static tree
2032 determine_specialization (tree template_id,
2033 tree decl,
2034 tree* targs_out,
2035 int need_member_template,
2036 int template_count,
2037 tmpl_spec_kind tsk)
2039 tree fns;
2040 tree targs;
2041 tree explicit_targs;
2042 tree candidates = NULL_TREE;
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates = NULL_TREE;
2050 int header_count;
2051 cp_binding_level *b;
2053 *targs_out = NULL_TREE;
2055 if (template_id == error_mark_node || decl == error_mark_node)
2056 return error_mark_node;
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count && DECL_CLASS_SCOPE_P (decl)
2062 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2064 gcc_assert (errorcount);
2065 return error_mark_node;
2068 fns = TREE_OPERAND (template_id, 0);
2069 explicit_targs = TREE_OPERAND (template_id, 1);
2071 if (fns == error_mark_node)
2072 return error_mark_node;
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns))
2076 fns = BASELINK_FUNCTIONS (fns);
2078 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2080 error ("%qD is not a function template", fns);
2081 return error_mark_node;
2083 else if (VAR_P (decl) && !variable_template_p (fns))
2085 error ("%qD is not a variable template", fns);
2086 return error_mark_node;
2089 /* Count the number of template headers specified for this
2090 specialization. */
2091 header_count = 0;
2092 for (b = current_binding_level;
2093 b->kind == sk_template_parms;
2094 b = b->level_chain)
2095 ++header_count;
2097 tree orig_fns = fns;
2099 if (variable_template_p (fns))
2101 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2102 targs = coerce_template_parms (parms, explicit_targs, fns,
2103 tf_warning_or_error,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs != error_mark_node)
2106 templates = tree_cons (targs, fns, templates);
2108 else for (lkp_iterator iter (fns); iter; ++iter)
2110 tree fn = *iter;
2112 if (TREE_CODE (fn) == TEMPLATE_DECL)
2114 tree decl_arg_types;
2115 tree fn_arg_types;
2116 tree insttype;
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2132 them.
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2139 itself).
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count && header_count != template_count + 1)
2145 continue;
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level->kind == sk_template_parms
2150 && !current_binding_level->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms))))
2154 continue;
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2158 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2165 && !same_type_p (TREE_VALUE (fn_arg_types),
2166 TREE_VALUE (decl_arg_types)))
2167 continue;
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2173 decl_arg_types
2174 = skip_artificial_parms_for (decl, decl_arg_types);
2175 fn_arg_types
2176 = skip_artificial_parms_for (fn, fn_arg_types);
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2181 match.
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2185 below. */
2186 if (tsk == tsk_template)
2188 if (compparms (fn_arg_types, decl_arg_types))
2189 candidates = tree_cons (NULL_TREE, fn, candidates);
2190 continue;
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check);
2198 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2201 if (!targs)
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2204 continue;
2206 /* Remove, from the set of candidates, all those functions
2207 whose constraints are not satisfied. */
2208 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2209 continue;
2211 // Then, try to form the new function type.
2212 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2213 if (insttype == error_mark_node)
2214 continue;
2215 fn_arg_types
2216 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2217 if (!compparms (fn_arg_types, decl_arg_types))
2218 continue;
2220 /* Save this template, and the arguments deduced. */
2221 templates = tree_cons (targs, fn, templates);
2223 else if (need_member_template)
2224 /* FN is an ordinary member function, and we need a
2225 specialization of a member template. */
2227 else if (TREE_CODE (fn) != FUNCTION_DECL)
2228 /* We can get IDENTIFIER_NODEs here in certain erroneous
2229 cases. */
2231 else if (!DECL_FUNCTION_MEMBER_P (fn))
2232 /* This is just an ordinary non-member function. Nothing can
2233 be a specialization of that. */
2235 else if (DECL_ARTIFICIAL (fn))
2236 /* Cannot specialize functions that are created implicitly. */
2238 else
2240 tree decl_arg_types;
2242 /* This is an ordinary member function. However, since
2243 we're here, we can assume its enclosing class is a
2244 template class. For example,
2246 template <typename T> struct S { void f(); };
2247 template <> void S<int>::f() {}
2249 Here, S<int>::f is a non-template, but S<int> is a
2250 template class. If FN has the same type as DECL, we
2251 might be in business. */
2253 if (!DECL_TEMPLATE_INFO (fn))
2254 /* Its enclosing class is an explicit specialization
2255 of a template class. This is not a candidate. */
2256 continue;
2258 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2259 TREE_TYPE (TREE_TYPE (fn))))
2260 /* The return types differ. */
2261 continue;
2263 /* Adjust the type of DECL in case FN is a static member. */
2264 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2265 if (DECL_STATIC_FUNCTION_P (fn)
2266 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2267 decl_arg_types = TREE_CHAIN (decl_arg_types);
2269 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2270 decl_arg_types))
2271 continue;
2273 // If the deduced arguments do not satisfy the constraints,
2274 // this is not a candidate.
2275 if (flag_concepts && !constraints_satisfied_p (fn))
2276 continue;
2278 // Add the candidate.
2279 candidates = tree_cons (NULL_TREE, fn, candidates);
2283 if (templates && TREE_CHAIN (templates))
2285 /* We have:
2287 [temp.expl.spec]
2289 It is possible for a specialization with a given function
2290 signature to be instantiated from more than one function
2291 template. In such cases, explicit specification of the
2292 template arguments must be used to uniquely identify the
2293 function template specialization being specialized.
2295 Note that here, there's no suggestion that we're supposed to
2296 determine which of the candidate templates is most
2297 specialized. However, we, also have:
2299 [temp.func.order]
2301 Partial ordering of overloaded function template
2302 declarations is used in the following contexts to select
2303 the function template to which a function template
2304 specialization refers:
2306 -- when an explicit specialization refers to a function
2307 template.
2309 So, we do use the partial ordering rules, at least for now.
2310 This extension can only serve to make invalid programs valid,
2311 so it's safe. And, there is strong anecdotal evidence that
2312 the committee intended the partial ordering rules to apply;
2313 the EDG front end has that behavior, and John Spicer claims
2314 that the committee simply forgot to delete the wording in
2315 [temp.expl.spec]. */
2316 tree tmpl = most_specialized_instantiation (templates);
2317 if (tmpl != error_mark_node)
2319 templates = tmpl;
2320 TREE_CHAIN (templates) = NULL_TREE;
2324 // Concepts allows multiple declarations of member functions
2325 // with the same signature. Like above, we need to rely on
2326 // on the partial ordering of those candidates to determine which
2327 // is the best.
2328 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2330 if (tree cand = most_constrained_function (candidates))
2332 candidates = cand;
2333 TREE_CHAIN (cand) = NULL_TREE;
2337 if (templates == NULL_TREE && candidates == NULL_TREE)
2339 error ("template-id %qD for %q+D does not match any template "
2340 "declaration", template_id, decl);
2341 if (header_count && header_count != template_count + 1)
2342 inform (input_location, "saw %d %<template<>%>, need %d for "
2343 "specializing a member function template",
2344 header_count, template_count + 1);
2345 else
2346 print_candidates (orig_fns);
2347 return error_mark_node;
2349 else if ((templates && TREE_CHAIN (templates))
2350 || (candidates && TREE_CHAIN (candidates))
2351 || (templates && candidates))
2353 error ("ambiguous template specialization %qD for %q+D",
2354 template_id, decl);
2355 candidates = chainon (candidates, templates);
2356 print_candidates (candidates);
2357 return error_mark_node;
2360 /* We have one, and exactly one, match. */
2361 if (candidates)
2363 tree fn = TREE_VALUE (candidates);
2364 *targs_out = copy_node (DECL_TI_ARGS (fn));
2366 // Propagate the candidate's constraints to the declaration.
2367 set_constraints (decl, get_constraints (fn));
2369 /* DECL is a re-declaration or partial instantiation of a template
2370 function. */
2371 if (TREE_CODE (fn) == TEMPLATE_DECL)
2372 return fn;
2373 /* It was a specialization of an ordinary member function in a
2374 template class. */
2375 return DECL_TI_TEMPLATE (fn);
2378 /* It was a specialization of a template. */
2379 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2382 *targs_out = copy_node (targs);
2383 SET_TMPL_ARGS_LEVEL (*targs_out,
2384 TMPL_ARGS_DEPTH (*targs_out),
2385 TREE_PURPOSE (templates));
2387 else
2388 *targs_out = TREE_PURPOSE (templates);
2389 return TREE_VALUE (templates);
2392 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2393 but with the default argument values filled in from those in the
2394 TMPL_TYPES. */
2396 static tree
2397 copy_default_args_to_explicit_spec_1 (tree spec_types,
2398 tree tmpl_types)
2400 tree new_spec_types;
2402 if (!spec_types)
2403 return NULL_TREE;
2405 if (spec_types == void_list_node)
2406 return void_list_node;
2408 /* Substitute into the rest of the list. */
2409 new_spec_types =
2410 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2411 TREE_CHAIN (tmpl_types));
2413 /* Add the default argument for this parameter. */
2414 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2415 TREE_VALUE (spec_types),
2416 new_spec_types);
2419 /* DECL is an explicit specialization. Replicate default arguments
2420 from the template it specializes. (That way, code like:
2422 template <class T> void f(T = 3);
2423 template <> void f(double);
2424 void g () { f (); }
2426 works, as required.) An alternative approach would be to look up
2427 the correct default arguments at the call-site, but this approach
2428 is consistent with how implicit instantiations are handled. */
2430 static void
2431 copy_default_args_to_explicit_spec (tree decl)
2433 tree tmpl;
2434 tree spec_types;
2435 tree tmpl_types;
2436 tree new_spec_types;
2437 tree old_type;
2438 tree new_type;
2439 tree t;
2440 tree object_type = NULL_TREE;
2441 tree in_charge = NULL_TREE;
2442 tree vtt = NULL_TREE;
2444 /* See if there's anything we need to do. */
2445 tmpl = DECL_TI_TEMPLATE (decl);
2446 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2447 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2448 if (TREE_PURPOSE (t))
2449 break;
2450 if (!t)
2451 return;
2453 old_type = TREE_TYPE (decl);
2454 spec_types = TYPE_ARG_TYPES (old_type);
2456 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2458 /* Remove the this pointer, but remember the object's type for
2459 CV quals. */
2460 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2461 spec_types = TREE_CHAIN (spec_types);
2462 tmpl_types = TREE_CHAIN (tmpl_types);
2464 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2466 /* DECL may contain more parameters than TMPL due to the extra
2467 in-charge parameter in constructors and destructors. */
2468 in_charge = spec_types;
2469 spec_types = TREE_CHAIN (spec_types);
2471 if (DECL_HAS_VTT_PARM_P (decl))
2473 vtt = spec_types;
2474 spec_types = TREE_CHAIN (spec_types);
2478 /* Compute the merged default arguments. */
2479 new_spec_types =
2480 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2482 /* Compute the new FUNCTION_TYPE. */
2483 if (object_type)
2485 if (vtt)
2486 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2487 TREE_VALUE (vtt),
2488 new_spec_types);
2490 if (in_charge)
2491 /* Put the in-charge parameter back. */
2492 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2493 TREE_VALUE (in_charge),
2494 new_spec_types);
2496 new_type = build_method_type_directly (object_type,
2497 TREE_TYPE (old_type),
2498 new_spec_types);
2500 else
2501 new_type = build_function_type (TREE_TYPE (old_type),
2502 new_spec_types);
2503 new_type = cp_build_type_attribute_variant (new_type,
2504 TYPE_ATTRIBUTES (old_type));
2505 new_type = build_exception_variant (new_type,
2506 TYPE_RAISES_EXCEPTIONS (old_type));
2508 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2509 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2511 TREE_TYPE (decl) = new_type;
2514 /* Return the number of template headers we expect to see for a definition
2515 or specialization of CTYPE or one of its non-template members. */
2518 num_template_headers_for_class (tree ctype)
2520 int num_templates = 0;
2522 while (ctype && CLASS_TYPE_P (ctype))
2524 /* You're supposed to have one `template <...>' for every
2525 template class, but you don't need one for a full
2526 specialization. For example:
2528 template <class T> struct S{};
2529 template <> struct S<int> { void f(); };
2530 void S<int>::f () {}
2532 is correct; there shouldn't be a `template <>' for the
2533 definition of `S<int>::f'. */
2534 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2535 /* If CTYPE does not have template information of any
2536 kind, then it is not a template, nor is it nested
2537 within a template. */
2538 break;
2539 if (explicit_class_specialization_p (ctype))
2540 break;
2541 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2542 ++num_templates;
2544 ctype = TYPE_CONTEXT (ctype);
2547 return num_templates;
2550 /* Do a simple sanity check on the template headers that precede the
2551 variable declaration DECL. */
2553 void
2554 check_template_variable (tree decl)
2556 tree ctx = CP_DECL_CONTEXT (decl);
2557 int wanted = num_template_headers_for_class (ctx);
2558 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2559 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2561 if (cxx_dialect < cxx14)
2562 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2563 "variable templates only available with "
2564 "-std=c++14 or -std=gnu++14");
2566 // Namespace-scope variable templates should have a template header.
2567 ++wanted;
2569 if (template_header_count > wanted)
2571 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2572 "too many template headers for %qD "
2573 "(should be %d)",
2574 decl, wanted);
2575 if (warned && CLASS_TYPE_P (ctx)
2576 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2577 inform (DECL_SOURCE_LOCATION (decl),
2578 "members of an explicitly specialized class are defined "
2579 "without a template header");
2583 /* An explicit specialization whose declarator-id or class-head-name is not
2584 qualified shall be declared in the nearest enclosing namespace of the
2585 template, or, if the namespace is inline (7.3.1), any namespace from its
2586 enclosing namespace set.
2588 If the name declared in the explicit instantiation is an unqualified name,
2589 the explicit instantiation shall appear in the namespace where its template
2590 is declared or, if that namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set. */
2593 void
2594 check_unqualified_spec_or_inst (tree t, location_t loc)
2596 tree tmpl = most_general_template (t);
2597 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2598 && !is_nested_namespace (current_namespace,
2599 CP_DECL_CONTEXT (tmpl), true))
2601 if (processing_specialization)
2602 permerror (loc, "explicit specialization of %qD outside its "
2603 "namespace must use a nested-name-specifier", tmpl);
2604 else if (processing_explicit_instantiation
2605 && cxx_dialect >= cxx11)
2606 /* This was allowed in C++98, so only pedwarn. */
2607 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2608 "outside its namespace must use a nested-name-"
2609 "specifier", tmpl);
2613 /* Check to see if the function just declared, as indicated in
2614 DECLARATOR, and in DECL, is a specialization of a function
2615 template. We may also discover that the declaration is an explicit
2616 instantiation at this point.
2618 Returns DECL, or an equivalent declaration that should be used
2619 instead if all goes well. Issues an error message if something is
2620 amiss. Returns error_mark_node if the error is not easily
2621 recoverable.
2623 FLAGS is a bitmask consisting of the following flags:
2625 2: The function has a definition.
2626 4: The function is a friend.
2628 The TEMPLATE_COUNT is the number of references to qualifying
2629 template classes that appeared in the name of the function. For
2630 example, in
2632 template <class T> struct S { void f(); };
2633 void S<int>::f();
2635 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2636 classes are not counted in the TEMPLATE_COUNT, so that in
2638 template <class T> struct S {};
2639 template <> struct S<int> { void f(); }
2640 template <> void S<int>::f();
2642 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2643 invalid; there should be no template <>.)
2645 If the function is a specialization, it is marked as such via
2646 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2647 is set up correctly, and it is added to the list of specializations
2648 for that template. */
2650 tree
2651 check_explicit_specialization (tree declarator,
2652 tree decl,
2653 int template_count,
2654 int flags)
2656 int have_def = flags & 2;
2657 int is_friend = flags & 4;
2658 bool is_concept = flags & 8;
2659 int specialization = 0;
2660 int explicit_instantiation = 0;
2661 int member_specialization = 0;
2662 tree ctype = DECL_CLASS_CONTEXT (decl);
2663 tree dname = DECL_NAME (decl);
2664 tmpl_spec_kind tsk;
2666 if (is_friend)
2668 if (!processing_specialization)
2669 tsk = tsk_none;
2670 else
2671 tsk = tsk_excessive_parms;
2673 else
2674 tsk = current_tmpl_spec_kind (template_count);
2676 switch (tsk)
2678 case tsk_none:
2679 if (processing_specialization && !VAR_P (decl))
2681 specialization = 1;
2682 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2684 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2686 if (is_friend)
2687 /* This could be something like:
2689 template <class T> void f(T);
2690 class S { friend void f<>(int); } */
2691 specialization = 1;
2692 else
2694 /* This case handles bogus declarations like template <>
2695 template <class T> void f<int>(); */
2697 error ("template-id %qD in declaration of primary template",
2698 declarator);
2699 return decl;
2702 break;
2704 case tsk_invalid_member_spec:
2705 /* The error has already been reported in
2706 check_specialization_scope. */
2707 return error_mark_node;
2709 case tsk_invalid_expl_inst:
2710 error ("template parameter list used in explicit instantiation");
2712 /* Fall through. */
2714 case tsk_expl_inst:
2715 if (have_def)
2716 error ("definition provided for explicit instantiation");
2718 explicit_instantiation = 1;
2719 break;
2721 case tsk_excessive_parms:
2722 case tsk_insufficient_parms:
2723 if (tsk == tsk_excessive_parms)
2724 error ("too many template parameter lists in declaration of %qD",
2725 decl);
2726 else if (template_header_count)
2727 error("too few template parameter lists in declaration of %qD", decl);
2728 else
2729 error("explicit specialization of %qD must be introduced by "
2730 "%<template <>%>", decl);
2732 /* Fall through. */
2733 case tsk_expl_spec:
2734 if (is_concept)
2735 error ("explicit specialization declared %<concept%>");
2737 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2738 /* In cases like template<> constexpr bool v = true;
2739 We'll give an error in check_template_variable. */
2740 break;
2742 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2743 if (ctype)
2744 member_specialization = 1;
2745 else
2746 specialization = 1;
2747 break;
2749 case tsk_template:
2750 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2752 /* This case handles bogus declarations like template <>
2753 template <class T> void f<int>(); */
2755 if (!uses_template_parms (declarator))
2756 error ("template-id %qD in declaration of primary template",
2757 declarator);
2758 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2760 /* Partial specialization of variable template. */
2761 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2762 specialization = 1;
2763 goto ok;
2765 else if (cxx_dialect < cxx14)
2766 error ("non-type partial specialization %qD "
2767 "is not allowed", declarator);
2768 else
2769 error ("non-class, non-variable partial specialization %qD "
2770 "is not allowed", declarator);
2771 return decl;
2772 ok:;
2775 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2776 /* This is a specialization of a member template, without
2777 specialization the containing class. Something like:
2779 template <class T> struct S {
2780 template <class U> void f (U);
2782 template <> template <class U> void S<int>::f(U) {}
2784 That's a specialization -- but of the entire template. */
2785 specialization = 1;
2786 break;
2788 default:
2789 gcc_unreachable ();
2792 if ((specialization || member_specialization)
2793 /* This doesn't apply to variable templates. */
2794 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2795 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2797 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2798 for (; t; t = TREE_CHAIN (t))
2799 if (TREE_PURPOSE (t))
2801 permerror (input_location,
2802 "default argument specified in explicit specialization");
2803 break;
2807 if (specialization || member_specialization || explicit_instantiation)
2809 tree tmpl = NULL_TREE;
2810 tree targs = NULL_TREE;
2811 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2813 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2814 if (!was_template_id)
2816 tree fns;
2818 gcc_assert (identifier_p (declarator));
2819 if (ctype)
2820 fns = dname;
2821 else
2823 /* If there is no class context, the explicit instantiation
2824 must be at namespace scope. */
2825 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2827 /* Find the namespace binding, using the declaration
2828 context. */
2829 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2830 false, true);
2831 if (fns == error_mark_node)
2832 /* If lookup fails, look for a friend declaration so we can
2833 give a better diagnostic. */
2834 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2835 /*type*/false, /*complain*/true,
2836 /*hidden*/true);
2838 if (fns == error_mark_node || !is_overloaded_fn (fns))
2840 error ("%qD is not a template function", dname);
2841 fns = error_mark_node;
2845 declarator = lookup_template_function (fns, NULL_TREE);
2848 if (declarator == error_mark_node)
2849 return error_mark_node;
2851 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2853 if (!explicit_instantiation)
2854 /* A specialization in class scope. This is invalid,
2855 but the error will already have been flagged by
2856 check_specialization_scope. */
2857 return error_mark_node;
2858 else
2860 /* It's not valid to write an explicit instantiation in
2861 class scope, e.g.:
2863 class C { template void f(); }
2865 This case is caught by the parser. However, on
2866 something like:
2868 template class C { void f(); };
2870 (which is invalid) we can get here. The error will be
2871 issued later. */
2875 return decl;
2877 else if (ctype != NULL_TREE
2878 && (identifier_p (TREE_OPERAND (declarator, 0))))
2880 // We'll match variable templates in start_decl.
2881 if (VAR_P (decl))
2882 return decl;
2884 /* Find the list of functions in ctype that have the same
2885 name as the declared function. */
2886 tree name = TREE_OPERAND (declarator, 0);
2888 if (constructor_name_p (name, ctype))
2890 if (DECL_CONSTRUCTOR_P (decl)
2891 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2892 : !CLASSTYPE_DESTRUCTOR (ctype))
2894 /* From [temp.expl.spec]:
2896 If such an explicit specialization for the member
2897 of a class template names an implicitly-declared
2898 special member function (clause _special_), the
2899 program is ill-formed.
2901 Similar language is found in [temp.explicit]. */
2902 error ("specialization of implicitly-declared special member function");
2903 return error_mark_node;
2906 name = DECL_NAME (decl);
2909 /* For a type-conversion operator, We might be looking for
2910 `operator int' which will be a specialization of
2911 `operator T'. Grab all the conversion operators, and
2912 then select from them. */
2913 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2914 ? conv_op_identifier : name);
2916 if (fns == NULL_TREE)
2918 error ("no member function %qD declared in %qT", name, ctype);
2919 return error_mark_node;
2921 else
2922 TREE_OPERAND (declarator, 0) = fns;
2925 /* Figure out what exactly is being specialized at this point.
2926 Note that for an explicit instantiation, even one for a
2927 member function, we cannot tell a priori whether the
2928 instantiation is for a member template, or just a member
2929 function of a template class. Even if a member template is
2930 being instantiated, the member template arguments may be
2931 elided if they can be deduced from the rest of the
2932 declaration. */
2933 tmpl = determine_specialization (declarator, decl,
2934 &targs,
2935 member_specialization,
2936 template_count,
2937 tsk);
2939 if (!tmpl || tmpl == error_mark_node)
2940 /* We couldn't figure out what this declaration was
2941 specializing. */
2942 return error_mark_node;
2943 else
2945 if (TREE_CODE (decl) == FUNCTION_DECL
2946 && DECL_HIDDEN_FRIEND_P (tmpl))
2948 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2949 "friend declaration %qD is not visible to "
2950 "explicit specialization", tmpl))
2951 inform (DECL_SOURCE_LOCATION (tmpl),
2952 "friend declaration here");
2954 else if (!ctype && !is_friend
2955 && CP_DECL_CONTEXT (decl) == current_namespace)
2956 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2958 tree gen_tmpl = most_general_template (tmpl);
2960 if (explicit_instantiation)
2962 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2963 is done by do_decl_instantiation later. */
2965 int arg_depth = TMPL_ARGS_DEPTH (targs);
2966 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2968 if (arg_depth > parm_depth)
2970 /* If TMPL is not the most general template (for
2971 example, if TMPL is a friend template that is
2972 injected into namespace scope), then there will
2973 be too many levels of TARGS. Remove some of them
2974 here. */
2975 int i;
2976 tree new_targs;
2978 new_targs = make_tree_vec (parm_depth);
2979 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2980 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2981 = TREE_VEC_ELT (targs, i);
2982 targs = new_targs;
2985 return instantiate_template (tmpl, targs, tf_error);
2988 /* If we thought that the DECL was a member function, but it
2989 turns out to be specializing a static member function,
2990 make DECL a static member function as well. */
2991 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2992 && DECL_STATIC_FUNCTION_P (tmpl)
2993 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2994 revert_static_member_fn (decl);
2996 /* If this is a specialization of a member template of a
2997 template class, we want to return the TEMPLATE_DECL, not
2998 the specialization of it. */
2999 if (tsk == tsk_template && !was_template_id)
3001 tree result = DECL_TEMPLATE_RESULT (tmpl);
3002 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3003 DECL_INITIAL (result) = NULL_TREE;
3004 if (have_def)
3006 tree parm;
3007 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3008 DECL_SOURCE_LOCATION (result)
3009 = DECL_SOURCE_LOCATION (decl);
3010 /* We want to use the argument list specified in the
3011 definition, not in the original declaration. */
3012 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3013 for (parm = DECL_ARGUMENTS (result); parm;
3014 parm = DECL_CHAIN (parm))
3015 DECL_CONTEXT (parm) = result;
3017 return register_specialization (tmpl, gen_tmpl, targs,
3018 is_friend, 0);
3021 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3022 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3024 if (was_template_id)
3025 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3027 /* Inherit default function arguments from the template
3028 DECL is specializing. */
3029 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3030 copy_default_args_to_explicit_spec (decl);
3032 /* This specialization has the same protection as the
3033 template it specializes. */
3034 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3035 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3037 /* 7.1.1-1 [dcl.stc]
3039 A storage-class-specifier shall not be specified in an
3040 explicit specialization...
3042 The parser rejects these, so unless action is taken here,
3043 explicit function specializations will always appear with
3044 global linkage.
3046 The action recommended by the C++ CWG in response to C++
3047 defect report 605 is to make the storage class and linkage
3048 of the explicit specialization match the templated function:
3050 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3052 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3054 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3055 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3057 /* A concept cannot be specialized. */
3058 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3060 error ("explicit specialization of function concept %qD",
3061 gen_tmpl);
3062 return error_mark_node;
3065 /* This specialization has the same linkage and visibility as
3066 the function template it specializes. */
3067 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3068 if (! TREE_PUBLIC (decl))
3070 DECL_INTERFACE_KNOWN (decl) = 1;
3071 DECL_NOT_REALLY_EXTERN (decl) = 1;
3073 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3074 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3076 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3077 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3081 /* If DECL is a friend declaration, declared using an
3082 unqualified name, the namespace associated with DECL may
3083 have been set incorrectly. For example, in:
3085 template <typename T> void f(T);
3086 namespace N {
3087 struct S { friend void f<int>(int); }
3090 we will have set the DECL_CONTEXT for the friend
3091 declaration to N, rather than to the global namespace. */
3092 if (DECL_NAMESPACE_SCOPE_P (decl))
3093 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3095 if (is_friend && !have_def)
3096 /* This is not really a declaration of a specialization.
3097 It's just the name of an instantiation. But, it's not
3098 a request for an instantiation, either. */
3099 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3100 else if (TREE_CODE (decl) == FUNCTION_DECL)
3101 /* A specialization is not necessarily COMDAT. */
3102 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3103 && DECL_DECLARED_INLINE_P (decl));
3104 else if (VAR_P (decl))
3105 DECL_COMDAT (decl) = false;
3107 /* If this is a full specialization, register it so that we can find
3108 it again. Partial specializations will be registered in
3109 process_partial_specialization. */
3110 if (!processing_template_decl)
3111 decl = register_specialization (decl, gen_tmpl, targs,
3112 is_friend, 0);
3114 /* A 'structor should already have clones. */
3115 gcc_assert (decl == error_mark_node
3116 || variable_template_p (tmpl)
3117 || !(DECL_CONSTRUCTOR_P (decl)
3118 || DECL_DESTRUCTOR_P (decl))
3119 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3123 return decl;
3126 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3127 parameters. These are represented in the same format used for
3128 DECL_TEMPLATE_PARMS. */
3131 comp_template_parms (const_tree parms1, const_tree parms2)
3133 const_tree p1;
3134 const_tree p2;
3136 if (parms1 == parms2)
3137 return 1;
3139 for (p1 = parms1, p2 = parms2;
3140 p1 != NULL_TREE && p2 != NULL_TREE;
3141 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3143 tree t1 = TREE_VALUE (p1);
3144 tree t2 = TREE_VALUE (p2);
3145 int i;
3147 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3148 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3150 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3151 return 0;
3153 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3155 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3156 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3158 /* If either of the template parameters are invalid, assume
3159 they match for the sake of error recovery. */
3160 if (error_operand_p (parm1) || error_operand_p (parm2))
3161 return 1;
3163 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3164 return 0;
3166 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3167 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3168 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3169 continue;
3170 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3171 return 0;
3175 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3176 /* One set of parameters has more parameters lists than the
3177 other. */
3178 return 0;
3180 return 1;
3183 /* Determine whether PARM is a parameter pack. */
3185 bool
3186 template_parameter_pack_p (const_tree parm)
3188 /* Determine if we have a non-type template parameter pack. */
3189 if (TREE_CODE (parm) == PARM_DECL)
3190 return (DECL_TEMPLATE_PARM_P (parm)
3191 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3192 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3193 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3195 /* If this is a list of template parameters, we could get a
3196 TYPE_DECL or a TEMPLATE_DECL. */
3197 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3198 parm = TREE_TYPE (parm);
3200 /* Otherwise it must be a type template parameter. */
3201 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3202 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3203 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3206 /* Determine if T is a function parameter pack. */
3208 bool
3209 function_parameter_pack_p (const_tree t)
3211 if (t && TREE_CODE (t) == PARM_DECL)
3212 return DECL_PACK_P (t);
3213 return false;
3216 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3217 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3219 tree
3220 get_function_template_decl (const_tree primary_func_tmpl_inst)
3222 if (! primary_func_tmpl_inst
3223 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3224 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3225 return NULL;
3227 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3230 /* Return true iff the function parameter PARAM_DECL was expanded
3231 from the function parameter pack PACK. */
3233 bool
3234 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3236 if (DECL_ARTIFICIAL (param_decl)
3237 || !function_parameter_pack_p (pack))
3238 return false;
3240 /* The parameter pack and its pack arguments have the same
3241 DECL_PARM_INDEX. */
3242 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3245 /* Determine whether ARGS describes a variadic template args list,
3246 i.e., one that is terminated by a template argument pack. */
3248 static bool
3249 template_args_variadic_p (tree args)
3251 int nargs;
3252 tree last_parm;
3254 if (args == NULL_TREE)
3255 return false;
3257 args = INNERMOST_TEMPLATE_ARGS (args);
3258 nargs = TREE_VEC_LENGTH (args);
3260 if (nargs == 0)
3261 return false;
3263 last_parm = TREE_VEC_ELT (args, nargs - 1);
3265 return ARGUMENT_PACK_P (last_parm);
3268 /* Generate a new name for the parameter pack name NAME (an
3269 IDENTIFIER_NODE) that incorporates its */
3271 static tree
3272 make_ith_pack_parameter_name (tree name, int i)
3274 /* Munge the name to include the parameter index. */
3275 #define NUMBUF_LEN 128
3276 char numbuf[NUMBUF_LEN];
3277 char* newname;
3278 int newname_len;
3280 if (name == NULL_TREE)
3281 return name;
3282 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3283 newname_len = IDENTIFIER_LENGTH (name)
3284 + strlen (numbuf) + 2;
3285 newname = (char*)alloca (newname_len);
3286 snprintf (newname, newname_len,
3287 "%s#%i", IDENTIFIER_POINTER (name), i);
3288 return get_identifier (newname);
3291 /* Return true if T is a primary function, class or alias template
3292 specialization, not including the template pattern. */
3294 bool
3295 primary_template_specialization_p (const_tree t)
3297 if (!t)
3298 return false;
3300 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3301 return (DECL_LANG_SPECIFIC (t)
3302 && DECL_USE_TEMPLATE (t)
3303 && DECL_TEMPLATE_INFO (t)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3305 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3306 return (CLASSTYPE_TEMPLATE_INFO (t)
3307 && CLASSTYPE_USE_TEMPLATE (t)
3308 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3309 else if (alias_template_specialization_p (t))
3310 return true;
3311 return false;
3314 /* Return true if PARM is a template template parameter. */
3316 bool
3317 template_template_parameter_p (const_tree parm)
3319 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3322 /* Return true iff PARM is a DECL representing a type template
3323 parameter. */
3325 bool
3326 template_type_parameter_p (const_tree parm)
3328 return (parm
3329 && (TREE_CODE (parm) == TYPE_DECL
3330 || TREE_CODE (parm) == TEMPLATE_DECL)
3331 && DECL_TEMPLATE_PARM_P (parm));
3334 /* Return the template parameters of T if T is a
3335 primary template instantiation, NULL otherwise. */
3337 tree
3338 get_primary_template_innermost_parameters (const_tree t)
3340 tree parms = NULL, template_info = NULL;
3342 if ((template_info = get_template_info (t))
3343 && primary_template_specialization_p (t))
3344 parms = INNERMOST_TEMPLATE_PARMS
3345 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3347 return parms;
3350 /* Return the template parameters of the LEVELth level from the full list
3351 of template parameters PARMS. */
3353 tree
3354 get_template_parms_at_level (tree parms, int level)
3356 tree p;
3357 if (!parms
3358 || TREE_CODE (parms) != TREE_LIST
3359 || level > TMPL_PARMS_DEPTH (parms))
3360 return NULL_TREE;
3362 for (p = parms; p; p = TREE_CHAIN (p))
3363 if (TMPL_PARMS_DEPTH (p) == level)
3364 return p;
3366 return NULL_TREE;
3369 /* Returns the template arguments of T if T is a template instantiation,
3370 NULL otherwise. */
3372 tree
3373 get_template_innermost_arguments (const_tree t)
3375 tree args = NULL, template_info = NULL;
3377 if ((template_info = get_template_info (t))
3378 && TI_ARGS (template_info))
3379 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3381 return args;
3384 /* Return the argument pack elements of T if T is a template argument pack,
3385 NULL otherwise. */
3387 tree
3388 get_template_argument_pack_elems (const_tree t)
3390 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3391 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3392 return NULL;
3394 return ARGUMENT_PACK_ARGS (t);
3397 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3398 ARGUMENT_PACK_SELECT represents. */
3400 static tree
3401 argument_pack_select_arg (tree t)
3403 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3404 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3406 /* If the selected argument is an expansion E, that most likely means we were
3407 called from gen_elem_of_pack_expansion_instantiation during the
3408 substituting of an argument pack (of which the Ith element is a pack
3409 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3410 In this case, the Ith element resulting from this substituting is going to
3411 be a pack expansion, which pattern is the pattern of E. Let's return the
3412 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3413 resulting pack expansion from it. */
3414 if (PACK_EXPANSION_P (arg))
3416 /* Make sure we aren't throwing away arg info. */
3417 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3418 arg = PACK_EXPANSION_PATTERN (arg);
3421 return arg;
3425 /* True iff FN is a function representing a built-in variadic parameter
3426 pack. */
3428 bool
3429 builtin_pack_fn_p (tree fn)
3431 if (!fn
3432 || TREE_CODE (fn) != FUNCTION_DECL
3433 || !DECL_IS_BUILTIN (fn))
3434 return false;
3436 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3437 return true;
3439 return false;
3442 /* True iff CALL is a call to a function representing a built-in variadic
3443 parameter pack. */
3445 static bool
3446 builtin_pack_call_p (tree call)
3448 if (TREE_CODE (call) != CALL_EXPR)
3449 return false;
3450 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3453 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3455 static tree
3456 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3457 tree in_decl)
3459 tree ohi = CALL_EXPR_ARG (call, 0);
3460 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3461 false/*fn*/, true/*int_cst*/);
3463 if (value_dependent_expression_p (hi))
3465 if (hi != ohi)
3467 call = copy_node (call);
3468 CALL_EXPR_ARG (call, 0) = hi;
3470 tree ex = make_pack_expansion (call, complain);
3471 tree vec = make_tree_vec (1);
3472 TREE_VEC_ELT (vec, 0) = ex;
3473 return vec;
3475 else
3477 hi = cxx_constant_value (hi);
3478 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3480 /* Calculate the largest value of len that won't make the size of the vec
3481 overflow an int. The compiler will exceed resource limits long before
3482 this, but it seems a decent place to diagnose. */
3483 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3485 if (len < 0 || len > max)
3487 if ((complain & tf_error)
3488 && hi != error_mark_node)
3489 error ("argument to __integer_pack must be between 0 and %d", max);
3490 return error_mark_node;
3493 tree vec = make_tree_vec (len);
3495 for (int i = 0; i < len; ++i)
3496 TREE_VEC_ELT (vec, i) = size_int (i);
3498 return vec;
3502 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3503 CALL. */
3505 static tree
3506 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3507 tree in_decl)
3509 if (!builtin_pack_call_p (call))
3510 return NULL_TREE;
3512 tree fn = CALL_EXPR_FN (call);
3514 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3515 return expand_integer_pack (call, args, complain, in_decl);
3517 return NULL_TREE;
3520 /* Structure used to track the progress of find_parameter_packs_r. */
3521 struct find_parameter_pack_data
3523 /* TREE_LIST that will contain all of the parameter packs found by
3524 the traversal. */
3525 tree* parameter_packs;
3527 /* Set of AST nodes that have been visited by the traversal. */
3528 hash_set<tree> *visited;
3530 /* True iff we're making a type pack expansion. */
3531 bool type_pack_expansion_p;
3534 /* Identifies all of the argument packs that occur in a template
3535 argument and appends them to the TREE_LIST inside DATA, which is a
3536 find_parameter_pack_data structure. This is a subroutine of
3537 make_pack_expansion and uses_parameter_packs. */
3538 static tree
3539 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3541 tree t = *tp;
3542 struct find_parameter_pack_data* ppd =
3543 (struct find_parameter_pack_data*)data;
3544 bool parameter_pack_p = false;
3546 /* Handle type aliases/typedefs. */
3547 if (TYPE_ALIAS_P (t))
3549 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3550 cp_walk_tree (&TI_ARGS (tinfo),
3551 &find_parameter_packs_r,
3552 ppd, ppd->visited);
3553 *walk_subtrees = 0;
3554 return NULL_TREE;
3557 /* Identify whether this is a parameter pack or not. */
3558 switch (TREE_CODE (t))
3560 case TEMPLATE_PARM_INDEX:
3561 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3562 parameter_pack_p = true;
3563 break;
3565 case TEMPLATE_TYPE_PARM:
3566 t = TYPE_MAIN_VARIANT (t);
3567 /* FALLTHRU */
3568 case TEMPLATE_TEMPLATE_PARM:
3569 /* If the placeholder appears in the decl-specifier-seq of a function
3570 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3571 is a pack expansion, the invented template parameter is a template
3572 parameter pack. */
3573 if (ppd->type_pack_expansion_p && is_auto (t))
3574 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3575 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3576 parameter_pack_p = true;
3577 break;
3579 case FIELD_DECL:
3580 case PARM_DECL:
3581 if (DECL_PACK_P (t))
3583 /* We don't want to walk into the type of a PARM_DECL,
3584 because we don't want to see the type parameter pack. */
3585 *walk_subtrees = 0;
3586 parameter_pack_p = true;
3588 break;
3590 /* Look through a lambda capture proxy to the field pack. */
3591 case VAR_DECL:
3592 if (DECL_PACK_P (t))
3594 /* We don't want to walk into the type of a variadic capture proxy,
3595 because we don't want to see the type parameter pack. */
3596 *walk_subtrees = 0;
3597 parameter_pack_p = true;
3599 else if (variable_template_specialization_p (t))
3601 cp_walk_tree (&DECL_TI_ARGS (t),
3602 find_parameter_packs_r,
3603 ppd, ppd->visited);
3604 *walk_subtrees = 0;
3606 break;
3608 case CALL_EXPR:
3609 if (builtin_pack_call_p (t))
3610 parameter_pack_p = true;
3611 break;
3613 case BASES:
3614 parameter_pack_p = true;
3615 break;
3616 default:
3617 /* Not a parameter pack. */
3618 break;
3621 if (parameter_pack_p)
3623 /* Add this parameter pack to the list. */
3624 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3627 if (TYPE_P (t))
3628 cp_walk_tree (&TYPE_CONTEXT (t),
3629 &find_parameter_packs_r, ppd, ppd->visited);
3631 /* This switch statement will return immediately if we don't find a
3632 parameter pack. */
3633 switch (TREE_CODE (t))
3635 case TEMPLATE_PARM_INDEX:
3636 return NULL_TREE;
3638 case BOUND_TEMPLATE_TEMPLATE_PARM:
3639 /* Check the template itself. */
3640 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3641 &find_parameter_packs_r, ppd, ppd->visited);
3642 /* Check the template arguments. */
3643 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3644 ppd->visited);
3645 *walk_subtrees = 0;
3646 return NULL_TREE;
3648 case TEMPLATE_TYPE_PARM:
3649 case TEMPLATE_TEMPLATE_PARM:
3650 return NULL_TREE;
3652 case PARM_DECL:
3653 return NULL_TREE;
3655 case DECL_EXPR:
3656 /* Ignore the declaration of a capture proxy for a parameter pack. */
3657 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3658 *walk_subtrees = 0;
3659 return NULL_TREE;
3661 case RECORD_TYPE:
3662 if (TYPE_PTRMEMFUNC_P (t))
3663 return NULL_TREE;
3664 /* Fall through. */
3666 case UNION_TYPE:
3667 case ENUMERAL_TYPE:
3668 if (TYPE_TEMPLATE_INFO (t))
3669 cp_walk_tree (&TYPE_TI_ARGS (t),
3670 &find_parameter_packs_r, ppd, ppd->visited);
3672 *walk_subtrees = 0;
3673 return NULL_TREE;
3675 case TEMPLATE_DECL:
3676 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3677 return NULL_TREE;
3678 gcc_fallthrough();
3680 case CONSTRUCTOR:
3681 cp_walk_tree (&TREE_TYPE (t),
3682 &find_parameter_packs_r, ppd, ppd->visited);
3683 return NULL_TREE;
3685 case TYPENAME_TYPE:
3686 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3687 ppd, ppd->visited);
3688 *walk_subtrees = 0;
3689 return NULL_TREE;
3691 case TYPE_PACK_EXPANSION:
3692 case EXPR_PACK_EXPANSION:
3693 *walk_subtrees = 0;
3694 return NULL_TREE;
3696 case INTEGER_TYPE:
3697 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3698 ppd, ppd->visited);
3699 *walk_subtrees = 0;
3700 return NULL_TREE;
3702 case IDENTIFIER_NODE:
3703 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3704 ppd->visited);
3705 *walk_subtrees = 0;
3706 return NULL_TREE;
3708 case LAMBDA_EXPR:
3710 tree fn = lambda_function (t);
3711 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3712 ppd->visited);
3713 *walk_subtrees = 0;
3714 return NULL_TREE;
3717 case DECLTYPE_TYPE:
3719 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3720 type_pack_expansion_p to false so that any placeholders
3721 within the expression don't get marked as parameter packs. */
3722 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3723 ppd->type_pack_expansion_p = false;
3724 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3725 ppd, ppd->visited);
3726 ppd->type_pack_expansion_p = type_pack_expansion_p;
3727 *walk_subtrees = 0;
3728 return NULL_TREE;
3731 default:
3732 return NULL_TREE;
3735 return NULL_TREE;
3738 /* Determines if the expression or type T uses any parameter packs. */
3739 bool
3740 uses_parameter_packs (tree t)
3742 tree parameter_packs = NULL_TREE;
3743 struct find_parameter_pack_data ppd;
3744 ppd.parameter_packs = &parameter_packs;
3745 ppd.visited = new hash_set<tree>;
3746 ppd.type_pack_expansion_p = false;
3747 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3748 delete ppd.visited;
3749 return parameter_packs != NULL_TREE;
3752 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3753 representation a base-class initializer into a parameter pack
3754 expansion. If all goes well, the resulting node will be an
3755 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3756 respectively. */
3757 tree
3758 make_pack_expansion (tree arg, tsubst_flags_t complain)
3760 tree result;
3761 tree parameter_packs = NULL_TREE;
3762 bool for_types = false;
3763 struct find_parameter_pack_data ppd;
3765 if (!arg || arg == error_mark_node)
3766 return arg;
3768 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3770 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3771 class initializer. In this case, the TREE_PURPOSE will be a
3772 _TYPE node (representing the base class expansion we're
3773 initializing) and the TREE_VALUE will be a TREE_LIST
3774 containing the initialization arguments.
3776 The resulting expansion looks somewhat different from most
3777 expansions. Rather than returning just one _EXPANSION, we
3778 return a TREE_LIST whose TREE_PURPOSE is a
3779 TYPE_PACK_EXPANSION containing the bases that will be
3780 initialized. The TREE_VALUE will be identical to the
3781 original TREE_VALUE, which is a list of arguments that will
3782 be passed to each base. We do not introduce any new pack
3783 expansion nodes into the TREE_VALUE (although it is possible
3784 that some already exist), because the TREE_PURPOSE and
3785 TREE_VALUE all need to be expanded together with the same
3786 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3787 resulting TREE_PURPOSE will mention the parameter packs in
3788 both the bases and the arguments to the bases. */
3789 tree purpose;
3790 tree value;
3791 tree parameter_packs = NULL_TREE;
3793 /* Determine which parameter packs will be used by the base
3794 class expansion. */
3795 ppd.visited = new hash_set<tree>;
3796 ppd.parameter_packs = &parameter_packs;
3797 ppd.type_pack_expansion_p = true;
3798 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3799 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3800 &ppd, ppd.visited);
3802 if (parameter_packs == NULL_TREE)
3804 if (complain & tf_error)
3805 error ("base initializer expansion %qT contains no parameter packs",
3806 arg);
3807 delete ppd.visited;
3808 return error_mark_node;
3811 if (TREE_VALUE (arg) != void_type_node)
3813 /* Collect the sets of parameter packs used in each of the
3814 initialization arguments. */
3815 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3817 /* Determine which parameter packs will be expanded in this
3818 argument. */
3819 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3820 &ppd, ppd.visited);
3824 delete ppd.visited;
3826 /* Create the pack expansion type for the base type. */
3827 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3828 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3829 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3830 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3832 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3833 they will rarely be compared to anything. */
3834 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3836 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3839 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3840 for_types = true;
3842 /* Build the PACK_EXPANSION_* node. */
3843 result = for_types
3844 ? cxx_make_type (TYPE_PACK_EXPANSION)
3845 : make_node (EXPR_PACK_EXPANSION);
3846 SET_PACK_EXPANSION_PATTERN (result, arg);
3847 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3849 /* Propagate type and const-expression information. */
3850 TREE_TYPE (result) = TREE_TYPE (arg);
3851 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3852 /* Mark this read now, since the expansion might be length 0. */
3853 mark_exp_read (arg);
3855 else
3856 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3857 they will rarely be compared to anything. */
3858 SET_TYPE_STRUCTURAL_EQUALITY (result);
3860 /* Determine which parameter packs will be expanded. */
3861 ppd.parameter_packs = &parameter_packs;
3862 ppd.visited = new hash_set<tree>;
3863 ppd.type_pack_expansion_p = TYPE_P (arg);
3864 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3865 delete ppd.visited;
3867 /* Make sure we found some parameter packs. */
3868 if (parameter_packs == NULL_TREE)
3870 if (complain & tf_error)
3872 if (TYPE_P (arg))
3873 error ("expansion pattern %qT contains no argument packs", arg);
3874 else
3875 error ("expansion pattern %qE contains no argument packs", arg);
3877 return error_mark_node;
3879 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3881 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3883 return result;
3886 /* Checks T for any "bare" parameter packs, which have not yet been
3887 expanded, and issues an error if any are found. This operation can
3888 only be done on full expressions or types (e.g., an expression
3889 statement, "if" condition, etc.), because we could have expressions like:
3891 foo(f(g(h(args)))...)
3893 where "args" is a parameter pack. check_for_bare_parameter_packs
3894 should not be called for the subexpressions args, h(args),
3895 g(h(args)), or f(g(h(args))), because we would produce erroneous
3896 error messages.
3898 Returns TRUE and emits an error if there were bare parameter packs,
3899 returns FALSE otherwise. */
3900 bool
3901 check_for_bare_parameter_packs (tree t)
3903 tree parameter_packs = NULL_TREE;
3904 struct find_parameter_pack_data ppd;
3906 if (!processing_template_decl || !t || t == error_mark_node)
3907 return false;
3909 /* A lambda might use a parameter pack from the containing context. */
3910 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3911 return false;
3913 if (TREE_CODE (t) == TYPE_DECL)
3914 t = TREE_TYPE (t);
3916 ppd.parameter_packs = &parameter_packs;
3917 ppd.visited = new hash_set<tree>;
3918 ppd.type_pack_expansion_p = false;
3919 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3920 delete ppd.visited;
3922 if (parameter_packs)
3924 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3925 error_at (loc, "parameter packs not expanded with %<...%>:");
3926 while (parameter_packs)
3928 tree pack = TREE_VALUE (parameter_packs);
3929 tree name = NULL_TREE;
3931 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3932 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3933 name = TYPE_NAME (pack);
3934 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3935 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3936 else if (TREE_CODE (pack) == CALL_EXPR)
3937 name = DECL_NAME (CALL_EXPR_FN (pack));
3938 else
3939 name = DECL_NAME (pack);
3941 if (name)
3942 inform (loc, " %qD", name);
3943 else
3944 inform (loc, " <anonymous>");
3946 parameter_packs = TREE_CHAIN (parameter_packs);
3949 return true;
3952 return false;
3955 /* Expand any parameter packs that occur in the template arguments in
3956 ARGS. */
3957 tree
3958 expand_template_argument_pack (tree args)
3960 if (args == error_mark_node)
3961 return error_mark_node;
3963 tree result_args = NULL_TREE;
3964 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3965 int num_result_args = -1;
3966 int non_default_args_count = -1;
3968 /* First, determine if we need to expand anything, and the number of
3969 slots we'll need. */
3970 for (in_arg = 0; in_arg < nargs; ++in_arg)
3972 tree arg = TREE_VEC_ELT (args, in_arg);
3973 if (arg == NULL_TREE)
3974 return args;
3975 if (ARGUMENT_PACK_P (arg))
3977 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3978 if (num_result_args < 0)
3979 num_result_args = in_arg + num_packed;
3980 else
3981 num_result_args += num_packed;
3983 else
3985 if (num_result_args >= 0)
3986 num_result_args++;
3990 /* If no expansion is necessary, we're done. */
3991 if (num_result_args < 0)
3992 return args;
3994 /* Expand arguments. */
3995 result_args = make_tree_vec (num_result_args);
3996 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3997 non_default_args_count =
3998 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3999 for (in_arg = 0; in_arg < nargs; ++in_arg)
4001 tree arg = TREE_VEC_ELT (args, in_arg);
4002 if (ARGUMENT_PACK_P (arg))
4004 tree packed = ARGUMENT_PACK_ARGS (arg);
4005 int i, num_packed = TREE_VEC_LENGTH (packed);
4006 for (i = 0; i < num_packed; ++i, ++out_arg)
4007 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4008 if (non_default_args_count > 0)
4009 non_default_args_count += num_packed - 1;
4011 else
4013 TREE_VEC_ELT (result_args, out_arg) = arg;
4014 ++out_arg;
4017 if (non_default_args_count >= 0)
4018 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4019 return result_args;
4022 /* Checks if DECL shadows a template parameter.
4024 [temp.local]: A template-parameter shall not be redeclared within its
4025 scope (including nested scopes).
4027 Emits an error and returns TRUE if the DECL shadows a parameter,
4028 returns FALSE otherwise. */
4030 bool
4031 check_template_shadow (tree decl)
4033 tree olddecl;
4035 /* If we're not in a template, we can't possibly shadow a template
4036 parameter. */
4037 if (!current_template_parms)
4038 return true;
4040 /* Figure out what we're shadowing. */
4041 decl = OVL_FIRST (decl);
4042 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4044 /* If there's no previous binding for this name, we're not shadowing
4045 anything, let alone a template parameter. */
4046 if (!olddecl)
4047 return true;
4049 /* If we're not shadowing a template parameter, we're done. Note
4050 that OLDDECL might be an OVERLOAD (or perhaps even an
4051 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4052 node. */
4053 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4054 return true;
4056 /* We check for decl != olddecl to avoid bogus errors for using a
4057 name inside a class. We check TPFI to avoid duplicate errors for
4058 inline member templates. */
4059 if (decl == olddecl
4060 || (DECL_TEMPLATE_PARM_P (decl)
4061 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4062 return true;
4064 /* Don't complain about the injected class name, as we've already
4065 complained about the class itself. */
4066 if (DECL_SELF_REFERENCE_P (decl))
4067 return false;
4069 if (DECL_TEMPLATE_PARM_P (decl))
4070 error ("declaration of template parameter %q+D shadows "
4071 "template parameter", decl);
4072 else
4073 error ("declaration of %q+#D shadows template parameter", decl);
4074 inform (DECL_SOURCE_LOCATION (olddecl),
4075 "template parameter %qD declared here", olddecl);
4076 return false;
4079 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4080 ORIG_LEVEL, DECL, and TYPE. */
4082 static tree
4083 build_template_parm_index (int index,
4084 int level,
4085 int orig_level,
4086 tree decl,
4087 tree type)
4089 tree t = make_node (TEMPLATE_PARM_INDEX);
4090 TEMPLATE_PARM_IDX (t) = index;
4091 TEMPLATE_PARM_LEVEL (t) = level;
4092 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4093 TEMPLATE_PARM_DECL (t) = decl;
4094 TREE_TYPE (t) = type;
4095 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4096 TREE_READONLY (t) = TREE_READONLY (decl);
4098 return t;
4101 /* Find the canonical type parameter for the given template type
4102 parameter. Returns the canonical type parameter, which may be TYPE
4103 if no such parameter existed. */
4105 static tree
4106 canonical_type_parameter (tree type)
4108 tree list;
4109 int idx = TEMPLATE_TYPE_IDX (type);
4110 if (!canonical_template_parms)
4111 vec_alloc (canonical_template_parms, idx + 1);
4113 if (canonical_template_parms->length () <= (unsigned) idx)
4114 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4116 list = (*canonical_template_parms)[idx];
4117 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4118 list = TREE_CHAIN (list);
4120 if (list)
4121 return TREE_VALUE (list);
4122 else
4124 (*canonical_template_parms)[idx]
4125 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4126 return type;
4130 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4131 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4132 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4133 new one is created. */
4135 static tree
4136 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4137 tsubst_flags_t complain)
4139 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4140 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4141 != TEMPLATE_PARM_LEVEL (index) - levels)
4142 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4144 tree orig_decl = TEMPLATE_PARM_DECL (index);
4145 tree decl, t;
4147 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4148 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4149 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4150 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4151 DECL_ARTIFICIAL (decl) = 1;
4152 SET_DECL_TEMPLATE_PARM_P (decl);
4154 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4155 TEMPLATE_PARM_LEVEL (index) - levels,
4156 TEMPLATE_PARM_ORIG_LEVEL (index),
4157 decl, type);
4158 TEMPLATE_PARM_DESCENDANTS (index) = t;
4159 TEMPLATE_PARM_PARAMETER_PACK (t)
4160 = TEMPLATE_PARM_PARAMETER_PACK (index);
4162 /* Template template parameters need this. */
4163 if (TREE_CODE (decl) == TEMPLATE_DECL)
4165 DECL_TEMPLATE_RESULT (decl)
4166 = build_decl (DECL_SOURCE_LOCATION (decl),
4167 TYPE_DECL, DECL_NAME (decl), type);
4168 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4169 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4170 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4174 return TEMPLATE_PARM_DESCENDANTS (index);
4177 /* Process information from new template parameter PARM and append it
4178 to the LIST being built. This new parameter is a non-type
4179 parameter iff IS_NON_TYPE is true. This new parameter is a
4180 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4181 is in PARM_LOC. */
4183 tree
4184 process_template_parm (tree list, location_t parm_loc, tree parm,
4185 bool is_non_type, bool is_parameter_pack)
4187 tree decl = 0;
4188 int idx = 0;
4190 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4191 tree defval = TREE_PURPOSE (parm);
4192 tree constr = TREE_TYPE (parm);
4194 if (list)
4196 tree p = tree_last (list);
4198 if (p && TREE_VALUE (p) != error_mark_node)
4200 p = TREE_VALUE (p);
4201 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4202 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4203 else
4204 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4207 ++idx;
4210 if (is_non_type)
4212 parm = TREE_VALUE (parm);
4214 SET_DECL_TEMPLATE_PARM_P (parm);
4216 if (TREE_TYPE (parm) != error_mark_node)
4218 /* [temp.param]
4220 The top-level cv-qualifiers on the template-parameter are
4221 ignored when determining its type. */
4222 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4223 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4224 TREE_TYPE (parm) = error_mark_node;
4225 else if (uses_parameter_packs (TREE_TYPE (parm))
4226 && !is_parameter_pack
4227 /* If we're in a nested template parameter list, the template
4228 template parameter could be a parameter pack. */
4229 && processing_template_parmlist == 1)
4231 /* This template parameter is not a parameter pack, but it
4232 should be. Complain about "bare" parameter packs. */
4233 check_for_bare_parameter_packs (TREE_TYPE (parm));
4235 /* Recover by calling this a parameter pack. */
4236 is_parameter_pack = true;
4240 /* A template parameter is not modifiable. */
4241 TREE_CONSTANT (parm) = 1;
4242 TREE_READONLY (parm) = 1;
4243 decl = build_decl (parm_loc,
4244 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4245 TREE_CONSTANT (decl) = 1;
4246 TREE_READONLY (decl) = 1;
4247 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4248 = build_template_parm_index (idx, processing_template_decl,
4249 processing_template_decl,
4250 decl, TREE_TYPE (parm));
4252 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4253 = is_parameter_pack;
4255 else
4257 tree t;
4258 parm = TREE_VALUE (TREE_VALUE (parm));
4260 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4262 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4263 /* This is for distinguishing between real templates and template
4264 template parameters */
4265 TREE_TYPE (parm) = t;
4266 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4267 decl = parm;
4269 else
4271 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4272 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4273 decl = build_decl (parm_loc,
4274 TYPE_DECL, parm, t);
4277 TYPE_NAME (t) = decl;
4278 TYPE_STUB_DECL (t) = decl;
4279 parm = decl;
4280 TEMPLATE_TYPE_PARM_INDEX (t)
4281 = build_template_parm_index (idx, processing_template_decl,
4282 processing_template_decl,
4283 decl, TREE_TYPE (parm));
4284 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4285 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4287 DECL_ARTIFICIAL (decl) = 1;
4288 SET_DECL_TEMPLATE_PARM_P (decl);
4290 /* Build requirements for the type/template parameter.
4291 This must be done after SET_DECL_TEMPLATE_PARM_P or
4292 process_template_parm could fail. */
4293 tree reqs = finish_shorthand_constraint (parm, constr);
4295 pushdecl (decl);
4297 /* Build the parameter node linking the parameter declaration,
4298 its default argument (if any), and its constraints (if any). */
4299 parm = build_tree_list (defval, parm);
4300 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4302 return chainon (list, parm);
4305 /* The end of a template parameter list has been reached. Process the
4306 tree list into a parameter vector, converting each parameter into a more
4307 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4308 as PARM_DECLs. */
4310 tree
4311 end_template_parm_list (tree parms)
4313 int nparms;
4314 tree parm, next;
4315 tree saved_parmlist = make_tree_vec (list_length (parms));
4317 /* Pop the dummy parameter level and add the real one. */
4318 current_template_parms = TREE_CHAIN (current_template_parms);
4320 current_template_parms
4321 = tree_cons (size_int (processing_template_decl),
4322 saved_parmlist, current_template_parms);
4324 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4326 next = TREE_CHAIN (parm);
4327 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4328 TREE_CHAIN (parm) = NULL_TREE;
4331 --processing_template_parmlist;
4333 return saved_parmlist;
4336 // Explicitly indicate the end of the template parameter list. We assume
4337 // that the current template parameters have been constructed and/or
4338 // managed explicitly, as when creating new template template parameters
4339 // from a shorthand constraint.
4340 void
4341 end_template_parm_list ()
4343 --processing_template_parmlist;
4346 /* end_template_decl is called after a template declaration is seen. */
4348 void
4349 end_template_decl (void)
4351 reset_specialization ();
4353 if (! processing_template_decl)
4354 return;
4356 /* This matches the pushlevel in begin_template_parm_list. */
4357 finish_scope ();
4359 --processing_template_decl;
4360 current_template_parms = TREE_CHAIN (current_template_parms);
4363 /* Takes a TREE_LIST representing a template parameter and convert it
4364 into an argument suitable to be passed to the type substitution
4365 functions. Note that If the TREE_LIST contains an error_mark
4366 node, the returned argument is error_mark_node. */
4368 tree
4369 template_parm_to_arg (tree t)
4372 if (t == NULL_TREE
4373 || TREE_CODE (t) != TREE_LIST)
4374 return t;
4376 if (error_operand_p (TREE_VALUE (t)))
4377 return error_mark_node;
4379 t = TREE_VALUE (t);
4381 if (TREE_CODE (t) == TYPE_DECL
4382 || TREE_CODE (t) == TEMPLATE_DECL)
4384 t = TREE_TYPE (t);
4386 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4388 /* Turn this argument into a TYPE_ARGUMENT_PACK
4389 with a single element, which expands T. */
4390 tree vec = make_tree_vec (1);
4391 if (CHECKING_P)
4392 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4394 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4396 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4397 SET_ARGUMENT_PACK_ARGS (t, vec);
4400 else
4402 t = DECL_INITIAL (t);
4404 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4406 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4407 with a single element, which expands T. */
4408 tree vec = make_tree_vec (1);
4409 if (CHECKING_P)
4410 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4412 t = convert_from_reference (t);
4413 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4415 t = make_node (NONTYPE_ARGUMENT_PACK);
4416 SET_ARGUMENT_PACK_ARGS (t, vec);
4418 else
4419 t = convert_from_reference (t);
4421 return t;
4424 /* Given a single level of template parameters (a TREE_VEC), return it
4425 as a set of template arguments. */
4427 static tree
4428 template_parms_level_to_args (tree parms)
4430 tree a = copy_node (parms);
4431 TREE_TYPE (a) = NULL_TREE;
4432 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4433 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4435 if (CHECKING_P)
4436 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4438 return a;
4441 /* Given a set of template parameters, return them as a set of template
4442 arguments. The template parameters are represented as a TREE_VEC, in
4443 the form documented in cp-tree.h for template arguments. */
4445 static tree
4446 template_parms_to_args (tree parms)
4448 tree header;
4449 tree args = NULL_TREE;
4450 int length = TMPL_PARMS_DEPTH (parms);
4451 int l = length;
4453 /* If there is only one level of template parameters, we do not
4454 create a TREE_VEC of TREE_VECs. Instead, we return a single
4455 TREE_VEC containing the arguments. */
4456 if (length > 1)
4457 args = make_tree_vec (length);
4459 for (header = parms; header; header = TREE_CHAIN (header))
4461 tree a = template_parms_level_to_args (TREE_VALUE (header));
4463 if (length > 1)
4464 TREE_VEC_ELT (args, --l) = a;
4465 else
4466 args = a;
4469 return args;
4472 /* Within the declaration of a template, return the currently active
4473 template parameters as an argument TREE_VEC. */
4475 static tree
4476 current_template_args (void)
4478 return template_parms_to_args (current_template_parms);
4481 /* Update the declared TYPE by doing any lookups which were thought to be
4482 dependent, but are not now that we know the SCOPE of the declarator. */
4484 tree
4485 maybe_update_decl_type (tree orig_type, tree scope)
4487 tree type = orig_type;
4489 if (type == NULL_TREE)
4490 return type;
4492 if (TREE_CODE (orig_type) == TYPE_DECL)
4493 type = TREE_TYPE (type);
4495 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4496 && dependent_type_p (type)
4497 /* Don't bother building up the args in this case. */
4498 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4500 /* tsubst in the args corresponding to the template parameters,
4501 including auto if present. Most things will be unchanged, but
4502 make_typename_type and tsubst_qualified_id will resolve
4503 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4504 tree args = current_template_args ();
4505 tree auto_node = type_uses_auto (type);
4506 tree pushed;
4507 if (auto_node)
4509 tree auto_vec = make_tree_vec (1);
4510 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4511 args = add_to_template_args (args, auto_vec);
4513 pushed = push_scope (scope);
4514 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4515 if (pushed)
4516 pop_scope (scope);
4519 if (type == error_mark_node)
4520 return orig_type;
4522 if (TREE_CODE (orig_type) == TYPE_DECL)
4524 if (same_type_p (type, TREE_TYPE (orig_type)))
4525 type = orig_type;
4526 else
4527 type = TYPE_NAME (type);
4529 return type;
4532 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4533 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4534 the new template is a member template. */
4536 tree
4537 build_template_decl (tree decl, tree parms, bool member_template_p)
4539 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4540 DECL_TEMPLATE_PARMS (tmpl) = parms;
4541 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4542 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4543 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4545 return tmpl;
4548 struct template_parm_data
4550 /* The level of the template parameters we are currently
4551 processing. */
4552 int level;
4554 /* The index of the specialization argument we are currently
4555 processing. */
4556 int current_arg;
4558 /* An array whose size is the number of template parameters. The
4559 elements are nonzero if the parameter has been used in any one
4560 of the arguments processed so far. */
4561 int* parms;
4563 /* An array whose size is the number of template arguments. The
4564 elements are nonzero if the argument makes use of template
4565 parameters of this level. */
4566 int* arg_uses_template_parms;
4569 /* Subroutine of push_template_decl used to see if each template
4570 parameter in a partial specialization is used in the explicit
4571 argument list. If T is of the LEVEL given in DATA (which is
4572 treated as a template_parm_data*), then DATA->PARMS is marked
4573 appropriately. */
4575 static int
4576 mark_template_parm (tree t, void* data)
4578 int level;
4579 int idx;
4580 struct template_parm_data* tpd = (struct template_parm_data*) data;
4582 template_parm_level_and_index (t, &level, &idx);
4584 if (level == tpd->level)
4586 tpd->parms[idx] = 1;
4587 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4590 /* In C++17 the type of a non-type argument is a deduced context. */
4591 if (cxx_dialect >= cxx17
4592 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4593 for_each_template_parm (TREE_TYPE (t),
4594 &mark_template_parm,
4595 data,
4596 NULL,
4597 /*include_nondeduced_p=*/false);
4599 /* Return zero so that for_each_template_parm will continue the
4600 traversal of the tree; we want to mark *every* template parm. */
4601 return 0;
4604 /* Process the partial specialization DECL. */
4606 static tree
4607 process_partial_specialization (tree decl)
4609 tree type = TREE_TYPE (decl);
4610 tree tinfo = get_template_info (decl);
4611 tree maintmpl = TI_TEMPLATE (tinfo);
4612 tree specargs = TI_ARGS (tinfo);
4613 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4614 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4615 tree inner_parms;
4616 tree inst;
4617 int nargs = TREE_VEC_LENGTH (inner_args);
4618 int ntparms;
4619 int i;
4620 bool did_error_intro = false;
4621 struct template_parm_data tpd;
4622 struct template_parm_data tpd2;
4624 gcc_assert (current_template_parms);
4626 /* A concept cannot be specialized. */
4627 if (flag_concepts && variable_concept_p (maintmpl))
4629 error ("specialization of variable concept %q#D", maintmpl);
4630 return error_mark_node;
4633 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4634 ntparms = TREE_VEC_LENGTH (inner_parms);
4636 /* We check that each of the template parameters given in the
4637 partial specialization is used in the argument list to the
4638 specialization. For example:
4640 template <class T> struct S;
4641 template <class T> struct S<T*>;
4643 The second declaration is OK because `T*' uses the template
4644 parameter T, whereas
4646 template <class T> struct S<int>;
4648 is no good. Even trickier is:
4650 template <class T>
4651 struct S1
4653 template <class U>
4654 struct S2;
4655 template <class U>
4656 struct S2<T>;
4659 The S2<T> declaration is actually invalid; it is a
4660 full-specialization. Of course,
4662 template <class U>
4663 struct S2<T (*)(U)>;
4665 or some such would have been OK. */
4666 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4667 tpd.parms = XALLOCAVEC (int, ntparms);
4668 memset (tpd.parms, 0, sizeof (int) * ntparms);
4670 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4671 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4672 for (i = 0; i < nargs; ++i)
4674 tpd.current_arg = i;
4675 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4676 &mark_template_parm,
4677 &tpd,
4678 NULL,
4679 /*include_nondeduced_p=*/false);
4681 for (i = 0; i < ntparms; ++i)
4682 if (tpd.parms[i] == 0)
4684 /* One of the template parms was not used in a deduced context in the
4685 specialization. */
4686 if (!did_error_intro)
4688 error ("template parameters not deducible in "
4689 "partial specialization:");
4690 did_error_intro = true;
4693 inform (input_location, " %qD",
4694 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4697 if (did_error_intro)
4698 return error_mark_node;
4700 /* [temp.class.spec]
4702 The argument list of the specialization shall not be identical to
4703 the implicit argument list of the primary template. */
4704 tree main_args
4705 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4706 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4707 && (!flag_concepts
4708 || !strictly_subsumes (current_template_constraints (),
4709 get_constraints (maintmpl))))
4711 if (!flag_concepts)
4712 error ("partial specialization %q+D does not specialize "
4713 "any template arguments", decl);
4714 else
4715 error ("partial specialization %q+D does not specialize any "
4716 "template arguments and is not more constrained than", decl);
4717 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4720 /* A partial specialization that replaces multiple parameters of the
4721 primary template with a pack expansion is less specialized for those
4722 parameters. */
4723 if (nargs < DECL_NTPARMS (maintmpl))
4725 error ("partial specialization is not more specialized than the "
4726 "primary template because it replaces multiple parameters "
4727 "with a pack expansion");
4728 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4729 /* Avoid crash in process_partial_specialization. */
4730 return decl;
4733 /* If we aren't in a dependent class, we can actually try deduction. */
4734 else if (tpd.level == 1
4735 /* FIXME we should be able to handle a partial specialization of a
4736 partial instantiation, but currently we can't (c++/41727). */
4737 && TMPL_ARGS_DEPTH (specargs) == 1
4738 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4740 if (permerror (input_location, "partial specialization %qD is not "
4741 "more specialized than", decl))
4742 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4743 maintmpl);
4746 /* [temp.class.spec]
4748 A partially specialized non-type argument expression shall not
4749 involve template parameters of the partial specialization except
4750 when the argument expression is a simple identifier.
4752 The type of a template parameter corresponding to a specialized
4753 non-type argument shall not be dependent on a parameter of the
4754 specialization.
4756 Also, we verify that pack expansions only occur at the
4757 end of the argument list. */
4758 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4759 tpd2.parms = 0;
4760 for (i = 0; i < nargs; ++i)
4762 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4763 tree arg = TREE_VEC_ELT (inner_args, i);
4764 tree packed_args = NULL_TREE;
4765 int j, len = 1;
4767 if (ARGUMENT_PACK_P (arg))
4769 /* Extract the arguments from the argument pack. We'll be
4770 iterating over these in the following loop. */
4771 packed_args = ARGUMENT_PACK_ARGS (arg);
4772 len = TREE_VEC_LENGTH (packed_args);
4775 for (j = 0; j < len; j++)
4777 if (packed_args)
4778 /* Get the Jth argument in the parameter pack. */
4779 arg = TREE_VEC_ELT (packed_args, j);
4781 if (PACK_EXPANSION_P (arg))
4783 /* Pack expansions must come at the end of the
4784 argument list. */
4785 if ((packed_args && j < len - 1)
4786 || (!packed_args && i < nargs - 1))
4788 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4789 error ("parameter pack argument %qE must be at the "
4790 "end of the template argument list", arg);
4791 else
4792 error ("parameter pack argument %qT must be at the "
4793 "end of the template argument list", arg);
4797 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4798 /* We only care about the pattern. */
4799 arg = PACK_EXPANSION_PATTERN (arg);
4801 if (/* These first two lines are the `non-type' bit. */
4802 !TYPE_P (arg)
4803 && TREE_CODE (arg) != TEMPLATE_DECL
4804 /* This next two lines are the `argument expression is not just a
4805 simple identifier' condition and also the `specialized
4806 non-type argument' bit. */
4807 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4808 && !(REFERENCE_REF_P (arg)
4809 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4811 if ((!packed_args && tpd.arg_uses_template_parms[i])
4812 || (packed_args && uses_template_parms (arg)))
4813 error ("template argument %qE involves template parameter(s)",
4814 arg);
4815 else
4817 /* Look at the corresponding template parameter,
4818 marking which template parameters its type depends
4819 upon. */
4820 tree type = TREE_TYPE (parm);
4822 if (!tpd2.parms)
4824 /* We haven't yet initialized TPD2. Do so now. */
4825 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4826 /* The number of parameters here is the number in the
4827 main template, which, as checked in the assertion
4828 above, is NARGS. */
4829 tpd2.parms = XALLOCAVEC (int, nargs);
4830 tpd2.level =
4831 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4834 /* Mark the template parameters. But this time, we're
4835 looking for the template parameters of the main
4836 template, not in the specialization. */
4837 tpd2.current_arg = i;
4838 tpd2.arg_uses_template_parms[i] = 0;
4839 memset (tpd2.parms, 0, sizeof (int) * nargs);
4840 for_each_template_parm (type,
4841 &mark_template_parm,
4842 &tpd2,
4843 NULL,
4844 /*include_nondeduced_p=*/false);
4846 if (tpd2.arg_uses_template_parms [i])
4848 /* The type depended on some template parameters.
4849 If they are fully specialized in the
4850 specialization, that's OK. */
4851 int j;
4852 int count = 0;
4853 for (j = 0; j < nargs; ++j)
4854 if (tpd2.parms[j] != 0
4855 && tpd.arg_uses_template_parms [j])
4856 ++count;
4857 if (count != 0)
4858 error_n (input_location, count,
4859 "type %qT of template argument %qE depends "
4860 "on a template parameter",
4861 "type %qT of template argument %qE depends "
4862 "on template parameters",
4863 type,
4864 arg);
4871 /* We should only get here once. */
4872 if (TREE_CODE (decl) == TYPE_DECL)
4873 gcc_assert (!COMPLETE_TYPE_P (type));
4875 // Build the template decl.
4876 tree tmpl = build_template_decl (decl, current_template_parms,
4877 DECL_MEMBER_TEMPLATE_P (maintmpl));
4878 TREE_TYPE (tmpl) = type;
4879 DECL_TEMPLATE_RESULT (tmpl) = decl;
4880 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4881 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4882 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4884 /* Give template template parms a DECL_CONTEXT of the template
4885 for which they are a parameter. */
4886 for (i = 0; i < ntparms; ++i)
4888 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4889 if (TREE_CODE (parm) == TEMPLATE_DECL)
4890 DECL_CONTEXT (parm) = tmpl;
4893 if (VAR_P (decl))
4894 /* We didn't register this in check_explicit_specialization so we could
4895 wait until the constraints were set. */
4896 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4897 else
4898 associate_classtype_constraints (type);
4900 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4901 = tree_cons (specargs, tmpl,
4902 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4903 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4905 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4906 inst = TREE_CHAIN (inst))
4908 tree instance = TREE_VALUE (inst);
4909 if (TYPE_P (instance)
4910 ? (COMPLETE_TYPE_P (instance)
4911 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4912 : DECL_TEMPLATE_INSTANTIATION (instance))
4914 tree spec = most_specialized_partial_spec (instance, tf_none);
4915 tree inst_decl = (DECL_P (instance)
4916 ? instance : TYPE_NAME (instance));
4917 if (!spec)
4918 /* OK */;
4919 else if (spec == error_mark_node)
4920 permerror (input_location,
4921 "declaration of %qD ambiguates earlier template "
4922 "instantiation for %qD", decl, inst_decl);
4923 else if (TREE_VALUE (spec) == tmpl)
4924 permerror (input_location,
4925 "partial specialization of %qD after instantiation "
4926 "of %qD", decl, inst_decl);
4930 return decl;
4933 /* PARM is a template parameter of some form; return the corresponding
4934 TEMPLATE_PARM_INDEX. */
4936 static tree
4937 get_template_parm_index (tree parm)
4939 if (TREE_CODE (parm) == PARM_DECL
4940 || TREE_CODE (parm) == CONST_DECL)
4941 parm = DECL_INITIAL (parm);
4942 else if (TREE_CODE (parm) == TYPE_DECL
4943 || TREE_CODE (parm) == TEMPLATE_DECL)
4944 parm = TREE_TYPE (parm);
4945 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4946 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4947 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4948 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4949 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4950 return parm;
4953 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4954 parameter packs used by the template parameter PARM. */
4956 static void
4957 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4959 /* A type parm can't refer to another parm. */
4960 if (TREE_CODE (parm) == TYPE_DECL)
4961 return;
4962 else if (TREE_CODE (parm) == PARM_DECL)
4964 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4965 ppd, ppd->visited);
4966 return;
4969 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4971 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4972 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4973 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4976 /* PARM is a template parameter pack. Return any parameter packs used in
4977 its type or the type of any of its template parameters. If there are
4978 any such packs, it will be instantiated into a fixed template parameter
4979 list by partial instantiation rather than be fully deduced. */
4981 tree
4982 fixed_parameter_pack_p (tree parm)
4984 /* This can only be true in a member template. */
4985 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4986 return NULL_TREE;
4987 /* This can only be true for a parameter pack. */
4988 if (!template_parameter_pack_p (parm))
4989 return NULL_TREE;
4990 /* A type parm can't refer to another parm. */
4991 if (TREE_CODE (parm) == TYPE_DECL)
4992 return NULL_TREE;
4994 tree parameter_packs = NULL_TREE;
4995 struct find_parameter_pack_data ppd;
4996 ppd.parameter_packs = &parameter_packs;
4997 ppd.visited = new hash_set<tree>;
4998 ppd.type_pack_expansion_p = false;
5000 fixed_parameter_pack_p_1 (parm, &ppd);
5002 delete ppd.visited;
5003 return parameter_packs;
5006 /* Check that a template declaration's use of default arguments and
5007 parameter packs is not invalid. Here, PARMS are the template
5008 parameters. IS_PRIMARY is true if DECL is the thing declared by
5009 a primary template. IS_PARTIAL is true if DECL is a partial
5010 specialization.
5012 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5013 function template declaration or a friend class template
5014 declaration. In the function case, 1 indicates a declaration, 2
5015 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5016 emitted for extraneous default arguments.
5018 Returns TRUE if there were no errors found, FALSE otherwise. */
5020 bool
5021 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5022 bool is_partial, int is_friend_decl)
5024 const char *msg;
5025 int last_level_to_check;
5026 tree parm_level;
5027 bool no_errors = true;
5029 /* [temp.param]
5031 A default template-argument shall not be specified in a
5032 function template declaration or a function template definition, nor
5033 in the template-parameter-list of the definition of a member of a
5034 class template. */
5036 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5037 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5038 /* You can't have a function template declaration in a local
5039 scope, nor you can you define a member of a class template in a
5040 local scope. */
5041 return true;
5043 if ((TREE_CODE (decl) == TYPE_DECL
5044 && TREE_TYPE (decl)
5045 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5046 || (TREE_CODE (decl) == FUNCTION_DECL
5047 && LAMBDA_FUNCTION_P (decl)))
5048 /* A lambda doesn't have an explicit declaration; don't complain
5049 about the parms of the enclosing class. */
5050 return true;
5052 if (current_class_type
5053 && !TYPE_BEING_DEFINED (current_class_type)
5054 && DECL_LANG_SPECIFIC (decl)
5055 && DECL_DECLARES_FUNCTION_P (decl)
5056 /* If this is either a friend defined in the scope of the class
5057 or a member function. */
5058 && (DECL_FUNCTION_MEMBER_P (decl)
5059 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5060 : DECL_FRIEND_CONTEXT (decl)
5061 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5062 : false)
5063 /* And, if it was a member function, it really was defined in
5064 the scope of the class. */
5065 && (!DECL_FUNCTION_MEMBER_P (decl)
5066 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5067 /* We already checked these parameters when the template was
5068 declared, so there's no need to do it again now. This function
5069 was defined in class scope, but we're processing its body now
5070 that the class is complete. */
5071 return true;
5073 /* Core issue 226 (C++0x only): the following only applies to class
5074 templates. */
5075 if (is_primary
5076 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5078 /* [temp.param]
5080 If a template-parameter has a default template-argument, all
5081 subsequent template-parameters shall have a default
5082 template-argument supplied. */
5083 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5085 tree inner_parms = TREE_VALUE (parm_level);
5086 int ntparms = TREE_VEC_LENGTH (inner_parms);
5087 int seen_def_arg_p = 0;
5088 int i;
5090 for (i = 0; i < ntparms; ++i)
5092 tree parm = TREE_VEC_ELT (inner_parms, i);
5094 if (parm == error_mark_node)
5095 continue;
5097 if (TREE_PURPOSE (parm))
5098 seen_def_arg_p = 1;
5099 else if (seen_def_arg_p
5100 && !template_parameter_pack_p (TREE_VALUE (parm)))
5102 error ("no default argument for %qD", TREE_VALUE (parm));
5103 /* For better subsequent error-recovery, we indicate that
5104 there should have been a default argument. */
5105 TREE_PURPOSE (parm) = error_mark_node;
5106 no_errors = false;
5108 else if (!is_partial
5109 && !is_friend_decl
5110 /* Don't complain about an enclosing partial
5111 specialization. */
5112 && parm_level == parms
5113 && TREE_CODE (decl) == TYPE_DECL
5114 && i < ntparms - 1
5115 && template_parameter_pack_p (TREE_VALUE (parm))
5116 /* A fixed parameter pack will be partially
5117 instantiated into a fixed length list. */
5118 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5120 /* A primary class template can only have one
5121 parameter pack, at the end of the template
5122 parameter list. */
5124 error ("parameter pack %q+D must be at the end of the"
5125 " template parameter list", TREE_VALUE (parm));
5127 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5128 = error_mark_node;
5129 no_errors = false;
5135 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5136 || is_partial
5137 || !is_primary
5138 || is_friend_decl)
5139 /* For an ordinary class template, default template arguments are
5140 allowed at the innermost level, e.g.:
5141 template <class T = int>
5142 struct S {};
5143 but, in a partial specialization, they're not allowed even
5144 there, as we have in [temp.class.spec]:
5146 The template parameter list of a specialization shall not
5147 contain default template argument values.
5149 So, for a partial specialization, or for a function template
5150 (in C++98/C++03), we look at all of them. */
5152 else
5153 /* But, for a primary class template that is not a partial
5154 specialization we look at all template parameters except the
5155 innermost ones. */
5156 parms = TREE_CHAIN (parms);
5158 /* Figure out what error message to issue. */
5159 if (is_friend_decl == 2)
5160 msg = G_("default template arguments may not be used in function template "
5161 "friend re-declaration");
5162 else if (is_friend_decl)
5163 msg = G_("default template arguments may not be used in template "
5164 "friend declarations");
5165 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5166 msg = G_("default template arguments may not be used in function templates "
5167 "without -std=c++11 or -std=gnu++11");
5168 else if (is_partial)
5169 msg = G_("default template arguments may not be used in "
5170 "partial specializations");
5171 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5172 msg = G_("default argument for template parameter for class enclosing %qD");
5173 else
5174 /* Per [temp.param]/9, "A default template-argument shall not be
5175 specified in the template-parameter-lists of the definition of
5176 a member of a class template that appears outside of the member's
5177 class.", thus if we aren't handling a member of a class template
5178 there is no need to examine the parameters. */
5179 return true;
5181 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5182 /* If we're inside a class definition, there's no need to
5183 examine the parameters to the class itself. On the one
5184 hand, they will be checked when the class is defined, and,
5185 on the other, default arguments are valid in things like:
5186 template <class T = double>
5187 struct S { template <class U> void f(U); };
5188 Here the default argument for `S' has no bearing on the
5189 declaration of `f'. */
5190 last_level_to_check = template_class_depth (current_class_type) + 1;
5191 else
5192 /* Check everything. */
5193 last_level_to_check = 0;
5195 for (parm_level = parms;
5196 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5197 parm_level = TREE_CHAIN (parm_level))
5199 tree inner_parms = TREE_VALUE (parm_level);
5200 int i;
5201 int ntparms;
5203 ntparms = TREE_VEC_LENGTH (inner_parms);
5204 for (i = 0; i < ntparms; ++i)
5206 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5207 continue;
5209 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5211 if (msg)
5213 no_errors = false;
5214 if (is_friend_decl == 2)
5215 return no_errors;
5217 error (msg, decl);
5218 msg = 0;
5221 /* Clear out the default argument so that we are not
5222 confused later. */
5223 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5227 /* At this point, if we're still interested in issuing messages,
5228 they must apply to classes surrounding the object declared. */
5229 if (msg)
5230 msg = G_("default argument for template parameter for class "
5231 "enclosing %qD");
5234 return no_errors;
5237 /* Worker for push_template_decl_real, called via
5238 for_each_template_parm. DATA is really an int, indicating the
5239 level of the parameters we are interested in. If T is a template
5240 parameter of that level, return nonzero. */
5242 static int
5243 template_parm_this_level_p (tree t, void* data)
5245 int this_level = *(int *)data;
5246 int level;
5248 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5249 level = TEMPLATE_PARM_LEVEL (t);
5250 else
5251 level = TEMPLATE_TYPE_LEVEL (t);
5252 return level == this_level;
5255 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5256 DATA is really an int, indicating the innermost outer level of parameters.
5257 If T is a template parameter of that level or further out, return
5258 nonzero. */
5260 static int
5261 template_parm_outer_level (tree t, void *data)
5263 int this_level = *(int *)data;
5264 int level;
5266 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5267 level = TEMPLATE_PARM_LEVEL (t);
5268 else
5269 level = TEMPLATE_TYPE_LEVEL (t);
5270 return level <= this_level;
5273 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5274 parameters given by current_template_args, or reuses a
5275 previously existing one, if appropriate. Returns the DECL, or an
5276 equivalent one, if it is replaced via a call to duplicate_decls.
5278 If IS_FRIEND is true, DECL is a friend declaration. */
5280 tree
5281 push_template_decl_real (tree decl, bool is_friend)
5283 tree tmpl;
5284 tree args;
5285 tree info;
5286 tree ctx;
5287 bool is_primary;
5288 bool is_partial;
5289 int new_template_p = 0;
5290 /* True if the template is a member template, in the sense of
5291 [temp.mem]. */
5292 bool member_template_p = false;
5294 if (decl == error_mark_node || !current_template_parms)
5295 return error_mark_node;
5297 /* See if this is a partial specialization. */
5298 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5299 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5300 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5301 || (VAR_P (decl)
5302 && DECL_LANG_SPECIFIC (decl)
5303 && DECL_TEMPLATE_SPECIALIZATION (decl)
5304 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5306 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5307 is_friend = true;
5309 if (is_friend)
5310 /* For a friend, we want the context of the friend, not
5311 the type of which it is a friend. */
5312 ctx = CP_DECL_CONTEXT (decl);
5313 else if (CP_DECL_CONTEXT (decl)
5314 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5315 /* In the case of a virtual function, we want the class in which
5316 it is defined. */
5317 ctx = CP_DECL_CONTEXT (decl);
5318 else
5319 /* Otherwise, if we're currently defining some class, the DECL
5320 is assumed to be a member of the class. */
5321 ctx = current_scope ();
5323 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5324 ctx = NULL_TREE;
5326 if (!DECL_CONTEXT (decl))
5327 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5329 /* See if this is a primary template. */
5330 if (is_friend && ctx
5331 && uses_template_parms_level (ctx, processing_template_decl))
5332 /* A friend template that specifies a class context, i.e.
5333 template <typename T> friend void A<T>::f();
5334 is not primary. */
5335 is_primary = false;
5336 else if (TREE_CODE (decl) == TYPE_DECL
5337 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5338 is_primary = false;
5339 else
5340 is_primary = template_parm_scope_p ();
5342 if (is_primary)
5344 warning (OPT_Wtemplates, "template %qD declared", decl);
5346 if (DECL_CLASS_SCOPE_P (decl))
5347 member_template_p = true;
5348 if (TREE_CODE (decl) == TYPE_DECL
5349 && anon_aggrname_p (DECL_NAME (decl)))
5351 error ("template class without a name");
5352 return error_mark_node;
5354 else if (TREE_CODE (decl) == FUNCTION_DECL)
5356 if (member_template_p)
5358 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5359 error ("member template %qD may not have virt-specifiers", decl);
5361 if (DECL_DESTRUCTOR_P (decl))
5363 /* [temp.mem]
5365 A destructor shall not be a member template. */
5366 error ("destructor %qD declared as member template", decl);
5367 return error_mark_node;
5369 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5370 && (!prototype_p (TREE_TYPE (decl))
5371 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5372 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5373 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5374 == void_list_node)))
5376 /* [basic.stc.dynamic.allocation]
5378 An allocation function can be a function
5379 template. ... Template allocation functions shall
5380 have two or more parameters. */
5381 error ("invalid template declaration of %qD", decl);
5382 return error_mark_node;
5385 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5386 && CLASS_TYPE_P (TREE_TYPE (decl)))
5388 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5389 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5390 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5392 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5393 if (TREE_CODE (t) == TYPE_DECL)
5394 t = TREE_TYPE (t);
5395 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5396 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5399 else if (TREE_CODE (decl) == TYPE_DECL
5400 && TYPE_DECL_ALIAS_P (decl))
5401 /* alias-declaration */
5402 gcc_assert (!DECL_ARTIFICIAL (decl));
5403 else if (VAR_P (decl))
5404 /* C++14 variable template. */;
5405 else
5407 error ("template declaration of %q#D", decl);
5408 return error_mark_node;
5412 /* Check to see that the rules regarding the use of default
5413 arguments are not being violated. We check args for a friend
5414 functions when we know whether it's a definition, introducing
5415 declaration or re-declaration. */
5416 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5417 check_default_tmpl_args (decl, current_template_parms,
5418 is_primary, is_partial, is_friend);
5420 /* Ensure that there are no parameter packs in the type of this
5421 declaration that have not been expanded. */
5422 if (TREE_CODE (decl) == FUNCTION_DECL)
5424 /* Check each of the arguments individually to see if there are
5425 any bare parameter packs. */
5426 tree type = TREE_TYPE (decl);
5427 tree arg = DECL_ARGUMENTS (decl);
5428 tree argtype = TYPE_ARG_TYPES (type);
5430 while (arg && argtype)
5432 if (!DECL_PACK_P (arg)
5433 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5435 /* This is a PARM_DECL that contains unexpanded parameter
5436 packs. We have already complained about this in the
5437 check_for_bare_parameter_packs call, so just replace
5438 these types with ERROR_MARK_NODE. */
5439 TREE_TYPE (arg) = error_mark_node;
5440 TREE_VALUE (argtype) = error_mark_node;
5443 arg = DECL_CHAIN (arg);
5444 argtype = TREE_CHAIN (argtype);
5447 /* Check for bare parameter packs in the return type and the
5448 exception specifiers. */
5449 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5450 /* Errors were already issued, set return type to int
5451 as the frontend doesn't expect error_mark_node as
5452 the return type. */
5453 TREE_TYPE (type) = integer_type_node;
5454 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5455 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5457 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5458 && TYPE_DECL_ALIAS_P (decl))
5459 ? DECL_ORIGINAL_TYPE (decl)
5460 : TREE_TYPE (decl)))
5462 TREE_TYPE (decl) = error_mark_node;
5463 return error_mark_node;
5466 if (is_partial)
5467 return process_partial_specialization (decl);
5469 args = current_template_args ();
5471 if (!ctx
5472 || TREE_CODE (ctx) == FUNCTION_DECL
5473 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5474 || (TREE_CODE (decl) == TYPE_DECL
5475 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5476 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5478 if (DECL_LANG_SPECIFIC (decl)
5479 && DECL_TEMPLATE_INFO (decl)
5480 && DECL_TI_TEMPLATE (decl))
5481 tmpl = DECL_TI_TEMPLATE (decl);
5482 /* If DECL is a TYPE_DECL for a class-template, then there won't
5483 be DECL_LANG_SPECIFIC. The information equivalent to
5484 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5485 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5486 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5487 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5489 /* Since a template declaration already existed for this
5490 class-type, we must be redeclaring it here. Make sure
5491 that the redeclaration is valid. */
5492 redeclare_class_template (TREE_TYPE (decl),
5493 current_template_parms,
5494 current_template_constraints ());
5495 /* We don't need to create a new TEMPLATE_DECL; just use the
5496 one we already had. */
5497 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5499 else
5501 tmpl = build_template_decl (decl, current_template_parms,
5502 member_template_p);
5503 new_template_p = 1;
5505 if (DECL_LANG_SPECIFIC (decl)
5506 && DECL_TEMPLATE_SPECIALIZATION (decl))
5508 /* A specialization of a member template of a template
5509 class. */
5510 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5511 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5512 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5516 else
5518 tree a, t, current, parms;
5519 int i;
5520 tree tinfo = get_template_info (decl);
5522 if (!tinfo)
5524 error ("template definition of non-template %q#D", decl);
5525 return error_mark_node;
5528 tmpl = TI_TEMPLATE (tinfo);
5530 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5531 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5532 && DECL_TEMPLATE_SPECIALIZATION (decl)
5533 && DECL_MEMBER_TEMPLATE_P (tmpl))
5535 tree new_tmpl;
5537 /* The declaration is a specialization of a member
5538 template, declared outside the class. Therefore, the
5539 innermost template arguments will be NULL, so we
5540 replace them with the arguments determined by the
5541 earlier call to check_explicit_specialization. */
5542 args = DECL_TI_ARGS (decl);
5544 new_tmpl
5545 = build_template_decl (decl, current_template_parms,
5546 member_template_p);
5547 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5548 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5549 DECL_TI_TEMPLATE (decl) = new_tmpl;
5550 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5551 DECL_TEMPLATE_INFO (new_tmpl)
5552 = build_template_info (tmpl, args);
5554 register_specialization (new_tmpl,
5555 most_general_template (tmpl),
5556 args,
5557 is_friend, 0);
5558 return decl;
5561 /* Make sure the template headers we got make sense. */
5563 parms = DECL_TEMPLATE_PARMS (tmpl);
5564 i = TMPL_PARMS_DEPTH (parms);
5565 if (TMPL_ARGS_DEPTH (args) != i)
5567 error ("expected %d levels of template parms for %q#D, got %d",
5568 i, decl, TMPL_ARGS_DEPTH (args));
5569 DECL_INTERFACE_KNOWN (decl) = 1;
5570 return error_mark_node;
5572 else
5573 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5575 a = TMPL_ARGS_LEVEL (args, i);
5576 t = INNERMOST_TEMPLATE_PARMS (parms);
5578 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5580 if (current == decl)
5581 error ("got %d template parameters for %q#D",
5582 TREE_VEC_LENGTH (a), decl);
5583 else
5584 error ("got %d template parameters for %q#T",
5585 TREE_VEC_LENGTH (a), current);
5586 error (" but %d required", TREE_VEC_LENGTH (t));
5587 /* Avoid crash in import_export_decl. */
5588 DECL_INTERFACE_KNOWN (decl) = 1;
5589 return error_mark_node;
5592 if (current == decl)
5593 current = ctx;
5594 else if (current == NULL_TREE)
5595 /* Can happen in erroneous input. */
5596 break;
5597 else
5598 current = get_containing_scope (current);
5601 /* Check that the parms are used in the appropriate qualifying scopes
5602 in the declarator. */
5603 if (!comp_template_args
5604 (TI_ARGS (tinfo),
5605 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5607 error ("template arguments to %qD do not match original "
5608 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5609 if (!uses_template_parms (TI_ARGS (tinfo)))
5610 inform (input_location, "use %<template<>%> for"
5611 " an explicit specialization");
5612 /* Avoid crash in import_export_decl. */
5613 DECL_INTERFACE_KNOWN (decl) = 1;
5614 return error_mark_node;
5618 DECL_TEMPLATE_RESULT (tmpl) = decl;
5619 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5621 /* Push template declarations for global functions and types. Note
5622 that we do not try to push a global template friend declared in a
5623 template class; such a thing may well depend on the template
5624 parameters of the class. */
5625 if (new_template_p && !ctx
5626 && !(is_friend && template_class_depth (current_class_type) > 0))
5628 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5629 if (tmpl == error_mark_node)
5630 return error_mark_node;
5632 /* Hide template friend classes that haven't been declared yet. */
5633 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5635 DECL_ANTICIPATED (tmpl) = 1;
5636 DECL_FRIEND_P (tmpl) = 1;
5640 if (is_primary)
5642 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5644 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5646 /* Give template template parms a DECL_CONTEXT of the template
5647 for which they are a parameter. */
5648 parms = INNERMOST_TEMPLATE_PARMS (parms);
5649 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5651 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5652 if (TREE_CODE (parm) == TEMPLATE_DECL)
5653 DECL_CONTEXT (parm) = tmpl;
5656 if (TREE_CODE (decl) == TYPE_DECL
5657 && TYPE_DECL_ALIAS_P (decl)
5658 && complex_alias_template_p (tmpl))
5659 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5662 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5663 back to its most general template. If TMPL is a specialization,
5664 ARGS may only have the innermost set of arguments. Add the missing
5665 argument levels if necessary. */
5666 if (DECL_TEMPLATE_INFO (tmpl))
5667 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5669 info = build_template_info (tmpl, args);
5671 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5672 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5673 else
5675 if (is_primary)
5676 retrofit_lang_decl (decl);
5677 if (DECL_LANG_SPECIFIC (decl))
5678 DECL_TEMPLATE_INFO (decl) = info;
5681 if (flag_implicit_templates
5682 && !is_friend
5683 && TREE_PUBLIC (decl)
5684 && VAR_OR_FUNCTION_DECL_P (decl))
5685 /* Set DECL_COMDAT on template instantiations; if we force
5686 them to be emitted by explicit instantiation or -frepo,
5687 mark_needed will tell cgraph to do the right thing. */
5688 DECL_COMDAT (decl) = true;
5690 return DECL_TEMPLATE_RESULT (tmpl);
5693 tree
5694 push_template_decl (tree decl)
5696 return push_template_decl_real (decl, false);
5699 /* FN is an inheriting constructor that inherits from the constructor
5700 template INHERITED; turn FN into a constructor template with a matching
5701 template header. */
5703 tree
5704 add_inherited_template_parms (tree fn, tree inherited)
5706 tree inner_parms
5707 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5708 inner_parms = copy_node (inner_parms);
5709 tree parms
5710 = tree_cons (size_int (processing_template_decl + 1),
5711 inner_parms, current_template_parms);
5712 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5713 tree args = template_parms_to_args (parms);
5714 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5715 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5716 DECL_TEMPLATE_RESULT (tmpl) = fn;
5717 DECL_ARTIFICIAL (tmpl) = true;
5718 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5719 return tmpl;
5722 /* Called when a class template TYPE is redeclared with the indicated
5723 template PARMS, e.g.:
5725 template <class T> struct S;
5726 template <class T> struct S {}; */
5728 bool
5729 redeclare_class_template (tree type, tree parms, tree cons)
5731 tree tmpl;
5732 tree tmpl_parms;
5733 int i;
5735 if (!TYPE_TEMPLATE_INFO (type))
5737 error ("%qT is not a template type", type);
5738 return false;
5741 tmpl = TYPE_TI_TEMPLATE (type);
5742 if (!PRIMARY_TEMPLATE_P (tmpl))
5743 /* The type is nested in some template class. Nothing to worry
5744 about here; there are no new template parameters for the nested
5745 type. */
5746 return true;
5748 if (!parms)
5750 error ("template specifiers not specified in declaration of %qD",
5751 tmpl);
5752 return false;
5755 parms = INNERMOST_TEMPLATE_PARMS (parms);
5756 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5758 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5760 error_n (input_location, TREE_VEC_LENGTH (parms),
5761 "redeclared with %d template parameter",
5762 "redeclared with %d template parameters",
5763 TREE_VEC_LENGTH (parms));
5764 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5765 "previous declaration %qD used %d template parameter",
5766 "previous declaration %qD used %d template parameters",
5767 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5768 return false;
5771 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5773 tree tmpl_parm;
5774 tree parm;
5775 tree tmpl_default;
5776 tree parm_default;
5778 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5779 || TREE_VEC_ELT (parms, i) == error_mark_node)
5780 continue;
5782 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5783 if (error_operand_p (tmpl_parm))
5784 return false;
5786 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5787 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5788 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5790 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5791 TEMPLATE_DECL. */
5792 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5793 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5794 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5795 || (TREE_CODE (tmpl_parm) != PARM_DECL
5796 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5797 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5798 || (TREE_CODE (tmpl_parm) == PARM_DECL
5799 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5800 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5802 error ("template parameter %q+#D", tmpl_parm);
5803 error ("redeclared here as %q#D", parm);
5804 return false;
5807 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5809 /* We have in [temp.param]:
5811 A template-parameter may not be given default arguments
5812 by two different declarations in the same scope. */
5813 error_at (input_location, "redefinition of default argument for %q#D", parm);
5814 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5815 "original definition appeared here");
5816 return false;
5819 if (parm_default != NULL_TREE)
5820 /* Update the previous template parameters (which are the ones
5821 that will really count) with the new default value. */
5822 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5823 else if (tmpl_default != NULL_TREE)
5824 /* Update the new parameters, too; they'll be used as the
5825 parameters for any members. */
5826 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5828 /* Give each template template parm in this redeclaration a
5829 DECL_CONTEXT of the template for which they are a parameter. */
5830 if (TREE_CODE (parm) == TEMPLATE_DECL)
5832 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5833 DECL_CONTEXT (parm) = tmpl;
5836 if (TREE_CODE (parm) == TYPE_DECL)
5837 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5840 // Cannot redeclare a class template with a different set of constraints.
5841 if (!equivalent_constraints (get_constraints (tmpl), cons))
5843 error_at (input_location, "redeclaration %q#D with different "
5844 "constraints", tmpl);
5845 inform (DECL_SOURCE_LOCATION (tmpl),
5846 "original declaration appeared here");
5849 return true;
5852 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5853 to be used when the caller has already checked
5854 (processing_template_decl
5855 && !instantiation_dependent_expression_p (expr)
5856 && potential_constant_expression (expr))
5857 and cleared processing_template_decl. */
5859 tree
5860 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5862 return tsubst_copy_and_build (expr,
5863 /*args=*/NULL_TREE,
5864 complain,
5865 /*in_decl=*/NULL_TREE,
5866 /*function_p=*/false,
5867 /*integral_constant_expression_p=*/true);
5870 /* Simplify EXPR if it is a non-dependent expression. Returns the
5871 (possibly simplified) expression. */
5873 tree
5874 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5876 if (expr == NULL_TREE)
5877 return NULL_TREE;
5879 /* If we're in a template, but EXPR isn't value dependent, simplify
5880 it. We're supposed to treat:
5882 template <typename T> void f(T[1 + 1]);
5883 template <typename T> void f(T[2]);
5885 as two declarations of the same function, for example. */
5886 if (processing_template_decl
5887 && is_nondependent_constant_expression (expr))
5889 processing_template_decl_sentinel s;
5890 expr = instantiate_non_dependent_expr_internal (expr, complain);
5892 return expr;
5895 tree
5896 instantiate_non_dependent_expr (tree expr)
5898 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5901 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5902 an uninstantiated expression. */
5904 tree
5905 instantiate_non_dependent_or_null (tree expr)
5907 if (expr == NULL_TREE)
5908 return NULL_TREE;
5909 if (processing_template_decl)
5911 if (!is_nondependent_constant_expression (expr))
5912 expr = NULL_TREE;
5913 else
5915 processing_template_decl_sentinel s;
5916 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5919 return expr;
5922 /* True iff T is a specialization of a variable template. */
5924 bool
5925 variable_template_specialization_p (tree t)
5927 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5928 return false;
5929 tree tmpl = DECL_TI_TEMPLATE (t);
5930 return variable_template_p (tmpl);
5933 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5934 template declaration, or a TYPE_DECL for an alias declaration. */
5936 bool
5937 alias_type_or_template_p (tree t)
5939 if (t == NULL_TREE)
5940 return false;
5941 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5942 || (TYPE_P (t)
5943 && TYPE_NAME (t)
5944 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5945 || DECL_ALIAS_TEMPLATE_P (t));
5948 /* Return TRUE iff T is a specialization of an alias template. */
5950 bool
5951 alias_template_specialization_p (const_tree t)
5953 /* It's an alias template specialization if it's an alias and its
5954 TYPE_NAME is a specialization of a primary template. */
5955 if (TYPE_ALIAS_P (t))
5956 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5957 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5959 return false;
5962 /* An alias template is complex from a SFINAE perspective if a template-id
5963 using that alias can be ill-formed when the expansion is not, as with
5964 the void_t template. We determine this by checking whether the
5965 expansion for the alias template uses all its template parameters. */
5967 struct uses_all_template_parms_data
5969 int level;
5970 bool *seen;
5973 static int
5974 uses_all_template_parms_r (tree t, void *data_)
5976 struct uses_all_template_parms_data &data
5977 = *(struct uses_all_template_parms_data*)data_;
5978 tree idx = get_template_parm_index (t);
5980 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5981 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5982 return 0;
5985 static bool
5986 complex_alias_template_p (const_tree tmpl)
5988 struct uses_all_template_parms_data data;
5989 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5990 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5991 data.level = TMPL_PARMS_DEPTH (parms);
5992 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5993 data.seen = XALLOCAVEC (bool, len);
5994 for (int i = 0; i < len; ++i)
5995 data.seen[i] = false;
5997 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5998 for (int i = 0; i < len; ++i)
5999 if (!data.seen[i])
6000 return true;
6001 return false;
6004 /* Return TRUE iff T is a specialization of a complex alias template with
6005 dependent template-arguments. */
6007 bool
6008 dependent_alias_template_spec_p (const_tree t)
6010 if (!alias_template_specialization_p (t))
6011 return false;
6013 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6014 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6015 return false;
6017 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6018 if (!any_dependent_template_arguments_p (args))
6019 return false;
6021 return true;
6024 /* Return the number of innermost template parameters in TMPL. */
6026 static int
6027 num_innermost_template_parms (tree tmpl)
6029 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6030 return TREE_VEC_LENGTH (parms);
6033 /* Return either TMPL or another template that it is equivalent to under DR
6034 1286: An alias that just changes the name of a template is equivalent to
6035 the other template. */
6037 static tree
6038 get_underlying_template (tree tmpl)
6040 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6041 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6043 /* Determine if the alias is equivalent to an underlying template. */
6044 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6045 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6046 if (!tinfo)
6047 break;
6049 tree underlying = TI_TEMPLATE (tinfo);
6050 if (!PRIMARY_TEMPLATE_P (underlying)
6051 || (num_innermost_template_parms (tmpl)
6052 != num_innermost_template_parms (underlying)))
6053 break;
6055 tree alias_args = INNERMOST_TEMPLATE_ARGS
6056 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6057 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6058 break;
6060 /* Alias is equivalent. Strip it and repeat. */
6061 tmpl = underlying;
6064 return tmpl;
6067 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6068 must be a reference-to-function or a pointer-to-function type, as specified
6069 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6070 and check that the resulting function has external linkage. */
6072 static tree
6073 convert_nontype_argument_function (tree type, tree expr,
6074 tsubst_flags_t complain)
6076 tree fns = expr;
6077 tree fn, fn_no_ptr;
6078 linkage_kind linkage;
6080 fn = instantiate_type (type, fns, tf_none);
6081 if (fn == error_mark_node)
6082 return error_mark_node;
6084 if (value_dependent_expression_p (fn))
6085 goto accept;
6087 fn_no_ptr = strip_fnptr_conv (fn);
6088 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6089 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6090 if (BASELINK_P (fn_no_ptr))
6091 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6093 /* [temp.arg.nontype]/1
6095 A template-argument for a non-type, non-template template-parameter
6096 shall be one of:
6097 [...]
6098 -- the address of an object or function with external [C++11: or
6099 internal] linkage. */
6101 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6103 if (complain & tf_error)
6105 error ("%qE is not a valid template argument for type %qT",
6106 expr, type);
6107 if (TYPE_PTR_P (type))
6108 inform (input_location, "it must be the address of a function "
6109 "with external linkage");
6110 else
6111 inform (input_location, "it must be the name of a function with "
6112 "external linkage");
6114 return NULL_TREE;
6117 linkage = decl_linkage (fn_no_ptr);
6118 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6120 if (complain & tf_error)
6122 if (cxx_dialect >= cxx11)
6123 error ("%qE is not a valid template argument for type %qT "
6124 "because %qD has no linkage",
6125 expr, type, fn_no_ptr);
6126 else
6127 error ("%qE is not a valid template argument for type %qT "
6128 "because %qD does not have external linkage",
6129 expr, type, fn_no_ptr);
6131 return NULL_TREE;
6134 accept:
6135 if (TREE_CODE (type) == REFERENCE_TYPE)
6136 fn = build_address (fn);
6137 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6138 fn = build_nop (type, fn);
6140 return fn;
6143 /* Subroutine of convert_nontype_argument.
6144 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6145 Emit an error otherwise. */
6147 static bool
6148 check_valid_ptrmem_cst_expr (tree type, tree expr,
6149 tsubst_flags_t complain)
6151 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6152 tree orig_expr = expr;
6153 STRIP_NOPS (expr);
6154 if (null_ptr_cst_p (expr))
6155 return true;
6156 if (TREE_CODE (expr) == PTRMEM_CST
6157 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6158 PTRMEM_CST_CLASS (expr)))
6159 return true;
6160 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6161 return true;
6162 if (processing_template_decl
6163 && TREE_CODE (expr) == ADDR_EXPR
6164 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6165 return true;
6166 if (complain & tf_error)
6168 error_at (loc, "%qE is not a valid template argument for type %qT",
6169 orig_expr, type);
6170 if (TREE_CODE (expr) != PTRMEM_CST)
6171 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6172 else
6173 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6175 return false;
6178 /* Returns TRUE iff the address of OP is value-dependent.
6180 14.6.2.4 [temp.dep.temp]:
6181 A non-integral non-type template-argument is dependent if its type is
6182 dependent or it has either of the following forms
6183 qualified-id
6184 & qualified-id
6185 and contains a nested-name-specifier which specifies a class-name that
6186 names a dependent type.
6188 We generalize this to just say that the address of a member of a
6189 dependent class is value-dependent; the above doesn't cover the
6190 address of a static data member named with an unqualified-id. */
6192 static bool
6193 has_value_dependent_address (tree op)
6195 /* We could use get_inner_reference here, but there's no need;
6196 this is only relevant for template non-type arguments, which
6197 can only be expressed as &id-expression. */
6198 if (DECL_P (op))
6200 tree ctx = CP_DECL_CONTEXT (op);
6201 if (TYPE_P (ctx) && dependent_type_p (ctx))
6202 return true;
6205 return false;
6208 /* The next set of functions are used for providing helpful explanatory
6209 diagnostics for failed overload resolution. Their messages should be
6210 indented by two spaces for consistency with the messages in
6211 call.c */
6213 static int
6214 unify_success (bool /*explain_p*/)
6216 return 0;
6219 /* Other failure functions should call this one, to provide a single function
6220 for setting a breakpoint on. */
6222 static int
6223 unify_invalid (bool /*explain_p*/)
6225 return 1;
6228 static int
6229 unify_parameter_deduction_failure (bool explain_p, tree parm)
6231 if (explain_p)
6232 inform (input_location,
6233 " couldn't deduce template parameter %qD", parm);
6234 return unify_invalid (explain_p);
6237 static int
6238 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6240 if (explain_p)
6241 inform (input_location,
6242 " types %qT and %qT have incompatible cv-qualifiers",
6243 parm, arg);
6244 return unify_invalid (explain_p);
6247 static int
6248 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6250 if (explain_p)
6251 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6252 return unify_invalid (explain_p);
6255 static int
6256 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6258 if (explain_p)
6259 inform (input_location,
6260 " template parameter %qD is not a parameter pack, but "
6261 "argument %qD is",
6262 parm, arg);
6263 return unify_invalid (explain_p);
6266 static int
6267 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6269 if (explain_p)
6270 inform (input_location,
6271 " template argument %qE does not match "
6272 "pointer-to-member constant %qE",
6273 arg, parm);
6274 return unify_invalid (explain_p);
6277 static int
6278 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6280 if (explain_p)
6281 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6282 return unify_invalid (explain_p);
6285 static int
6286 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6288 if (explain_p)
6289 inform (input_location,
6290 " inconsistent parameter pack deduction with %qT and %qT",
6291 old_arg, new_arg);
6292 return unify_invalid (explain_p);
6295 static int
6296 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6298 if (explain_p)
6300 if (TYPE_P (parm))
6301 inform (input_location,
6302 " deduced conflicting types for parameter %qT (%qT and %qT)",
6303 parm, first, second);
6304 else
6305 inform (input_location,
6306 " deduced conflicting values for non-type parameter "
6307 "%qE (%qE and %qE)", parm, first, second);
6309 return unify_invalid (explain_p);
6312 static int
6313 unify_vla_arg (bool explain_p, tree arg)
6315 if (explain_p)
6316 inform (input_location,
6317 " variable-sized array type %qT is not "
6318 "a valid template argument",
6319 arg);
6320 return unify_invalid (explain_p);
6323 static int
6324 unify_method_type_error (bool explain_p, tree arg)
6326 if (explain_p)
6327 inform (input_location,
6328 " member function type %qT is not a valid template argument",
6329 arg);
6330 return unify_invalid (explain_p);
6333 static int
6334 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6336 if (explain_p)
6338 if (least_p)
6339 inform_n (input_location, wanted,
6340 " candidate expects at least %d argument, %d provided",
6341 " candidate expects at least %d arguments, %d provided",
6342 wanted, have);
6343 else
6344 inform_n (input_location, wanted,
6345 " candidate expects %d argument, %d provided",
6346 " candidate expects %d arguments, %d provided",
6347 wanted, have);
6349 return unify_invalid (explain_p);
6352 static int
6353 unify_too_many_arguments (bool explain_p, int have, int wanted)
6355 return unify_arity (explain_p, have, wanted);
6358 static int
6359 unify_too_few_arguments (bool explain_p, int have, int wanted,
6360 bool least_p = false)
6362 return unify_arity (explain_p, have, wanted, least_p);
6365 static int
6366 unify_arg_conversion (bool explain_p, tree to_type,
6367 tree from_type, tree arg)
6369 if (explain_p)
6370 inform (EXPR_LOC_OR_LOC (arg, input_location),
6371 " cannot convert %qE (type %qT) to type %qT",
6372 arg, from_type, to_type);
6373 return unify_invalid (explain_p);
6376 static int
6377 unify_no_common_base (bool explain_p, enum template_base_result r,
6378 tree parm, tree arg)
6380 if (explain_p)
6381 switch (r)
6383 case tbr_ambiguous_baseclass:
6384 inform (input_location, " %qT is an ambiguous base class of %qT",
6385 parm, arg);
6386 break;
6387 default:
6388 inform (input_location, " %qT is not derived from %qT", arg, parm);
6389 break;
6391 return unify_invalid (explain_p);
6394 static int
6395 unify_inconsistent_template_template_parameters (bool explain_p)
6397 if (explain_p)
6398 inform (input_location,
6399 " template parameters of a template template argument are "
6400 "inconsistent with other deduced template arguments");
6401 return unify_invalid (explain_p);
6404 static int
6405 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6407 if (explain_p)
6408 inform (input_location,
6409 " can't deduce a template for %qT from non-template type %qT",
6410 parm, arg);
6411 return unify_invalid (explain_p);
6414 static int
6415 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6417 if (explain_p)
6418 inform (input_location,
6419 " template argument %qE does not match %qE", arg, parm);
6420 return unify_invalid (explain_p);
6423 /* Attempt to convert the non-type template parameter EXPR to the
6424 indicated TYPE. If the conversion is successful, return the
6425 converted value. If the conversion is unsuccessful, return
6426 NULL_TREE if we issued an error message, or error_mark_node if we
6427 did not. We issue error messages for out-and-out bad template
6428 parameters, but not simply because the conversion failed, since we
6429 might be just trying to do argument deduction. Both TYPE and EXPR
6430 must be non-dependent.
6432 The conversion follows the special rules described in
6433 [temp.arg.nontype], and it is much more strict than an implicit
6434 conversion.
6436 This function is called twice for each template argument (see
6437 lookup_template_class for a more accurate description of this
6438 problem). This means that we need to handle expressions which
6439 are not valid in a C++ source, but can be created from the
6440 first call (for instance, casts to perform conversions). These
6441 hacks can go away after we fix the double coercion problem. */
6443 static tree
6444 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6446 tree expr_type;
6447 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6448 tree orig_expr = expr;
6450 /* Detect immediately string literals as invalid non-type argument.
6451 This special-case is not needed for correctness (we would easily
6452 catch this later), but only to provide better diagnostic for this
6453 common user mistake. As suggested by DR 100, we do not mention
6454 linkage issues in the diagnostic as this is not the point. */
6455 /* FIXME we're making this OK. */
6456 if (TREE_CODE (expr) == STRING_CST)
6458 if (complain & tf_error)
6459 error ("%qE is not a valid template argument for type %qT "
6460 "because string literals can never be used in this context",
6461 expr, type);
6462 return NULL_TREE;
6465 /* Add the ADDR_EXPR now for the benefit of
6466 value_dependent_expression_p. */
6467 if (TYPE_PTROBV_P (type)
6468 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6470 expr = decay_conversion (expr, complain);
6471 if (expr == error_mark_node)
6472 return error_mark_node;
6475 /* If we are in a template, EXPR may be non-dependent, but still
6476 have a syntactic, rather than semantic, form. For example, EXPR
6477 might be a SCOPE_REF, rather than the VAR_DECL to which the
6478 SCOPE_REF refers. Preserving the qualifying scope is necessary
6479 so that access checking can be performed when the template is
6480 instantiated -- but here we need the resolved form so that we can
6481 convert the argument. */
6482 bool non_dep = false;
6483 if (TYPE_REF_OBJ_P (type)
6484 && has_value_dependent_address (expr))
6485 /* If we want the address and it's value-dependent, don't fold. */;
6486 else if (processing_template_decl
6487 && is_nondependent_constant_expression (expr))
6488 non_dep = true;
6489 if (error_operand_p (expr))
6490 return error_mark_node;
6491 expr_type = TREE_TYPE (expr);
6493 /* If the argument is non-dependent, perform any conversions in
6494 non-dependent context as well. */
6495 processing_template_decl_sentinel s (non_dep);
6496 if (non_dep)
6497 expr = instantiate_non_dependent_expr_internal (expr, complain);
6499 if (value_dependent_expression_p (expr))
6500 expr = canonicalize_expr_argument (expr, complain);
6502 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6503 to a non-type argument of "nullptr". */
6504 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6505 expr = fold_simple (convert (type, expr));
6507 /* In C++11, integral or enumeration non-type template arguments can be
6508 arbitrary constant expressions. Pointer and pointer to
6509 member arguments can be general constant expressions that evaluate
6510 to a null value, but otherwise still need to be of a specific form. */
6511 if (cxx_dialect >= cxx11)
6513 if (TREE_CODE (expr) == PTRMEM_CST)
6514 /* A PTRMEM_CST is already constant, and a valid template
6515 argument for a parameter of pointer to member type, we just want
6516 to leave it in that form rather than lower it to a
6517 CONSTRUCTOR. */;
6518 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6519 || cxx_dialect >= cxx17)
6521 /* C++17: A template-argument for a non-type template-parameter shall
6522 be a converted constant expression (8.20) of the type of the
6523 template-parameter. */
6524 expr = build_converted_constant_expr (type, expr, complain);
6525 if (expr == error_mark_node)
6526 return error_mark_node;
6527 expr = maybe_constant_value (expr);
6528 expr = convert_from_reference (expr);
6530 else if (TYPE_PTR_OR_PTRMEM_P (type))
6532 tree folded = maybe_constant_value (expr);
6533 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6534 : null_member_pointer_value_p (folded))
6535 expr = folded;
6539 if (TREE_CODE (type) == REFERENCE_TYPE)
6540 expr = mark_lvalue_use (expr);
6541 else
6542 expr = mark_rvalue_use (expr);
6544 /* HACK: Due to double coercion, we can get a
6545 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6546 which is the tree that we built on the first call (see
6547 below when coercing to reference to object or to reference to
6548 function). We just strip everything and get to the arg.
6549 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6550 for examples. */
6551 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6553 tree probe_type, probe = expr;
6554 if (REFERENCE_REF_P (probe))
6555 probe = TREE_OPERAND (probe, 0);
6556 probe_type = TREE_TYPE (probe);
6557 if (TREE_CODE (probe) == NOP_EXPR)
6559 /* ??? Maybe we could use convert_from_reference here, but we
6560 would need to relax its constraints because the NOP_EXPR
6561 could actually change the type to something more cv-qualified,
6562 and this is not folded by convert_from_reference. */
6563 tree addr = TREE_OPERAND (probe, 0);
6564 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6565 && TREE_CODE (addr) == ADDR_EXPR
6566 && TYPE_PTR_P (TREE_TYPE (addr))
6567 && (same_type_ignoring_top_level_qualifiers_p
6568 (TREE_TYPE (probe_type),
6569 TREE_TYPE (TREE_TYPE (addr)))))
6571 expr = TREE_OPERAND (addr, 0);
6572 expr_type = TREE_TYPE (probe_type);
6577 /* [temp.arg.nontype]/5, bullet 1
6579 For a non-type template-parameter of integral or enumeration type,
6580 integral promotions (_conv.prom_) and integral conversions
6581 (_conv.integral_) are applied. */
6582 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6584 if (cxx_dialect < cxx11)
6586 tree t = build_converted_constant_expr (type, expr, complain);
6587 t = maybe_constant_value (t);
6588 if (t != error_mark_node)
6589 expr = t;
6592 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6593 return error_mark_node;
6595 /* Notice that there are constant expressions like '4 % 0' which
6596 do not fold into integer constants. */
6597 if (TREE_CODE (expr) != INTEGER_CST
6598 && !value_dependent_expression_p (expr))
6600 if (complain & tf_error)
6602 int errs = errorcount, warns = warningcount + werrorcount;
6603 if (!require_potential_constant_expression (expr))
6604 expr = error_mark_node;
6605 else
6606 expr = cxx_constant_value (expr);
6607 if (errorcount > errs || warningcount + werrorcount > warns)
6608 inform (loc, "in template argument for type %qT ", type);
6609 if (expr == error_mark_node)
6610 return NULL_TREE;
6611 /* else cxx_constant_value complained but gave us
6612 a real constant, so go ahead. */
6613 if (TREE_CODE (expr) != INTEGER_CST)
6615 /* Some assemble time constant expressions like
6616 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6617 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6618 as we can emit them into .rodata initializers of
6619 variables, yet they can't fold into an INTEGER_CST at
6620 compile time. Refuse them here. */
6621 gcc_checking_assert (reduced_constant_expression_p (expr));
6622 error_at (loc, "template argument %qE for type %qT not "
6623 "a constant integer", expr, type);
6624 return NULL_TREE;
6627 else
6628 return NULL_TREE;
6631 /* Avoid typedef problems. */
6632 if (TREE_TYPE (expr) != type)
6633 expr = fold_convert (type, expr);
6635 /* [temp.arg.nontype]/5, bullet 2
6637 For a non-type template-parameter of type pointer to object,
6638 qualification conversions (_conv.qual_) and the array-to-pointer
6639 conversion (_conv.array_) are applied. */
6640 else if (TYPE_PTROBV_P (type))
6642 tree decayed = expr;
6644 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6645 decay_conversion or an explicit cast. If it's a problematic cast,
6646 we'll complain about it below. */
6647 if (TREE_CODE (expr) == NOP_EXPR)
6649 tree probe = expr;
6650 STRIP_NOPS (probe);
6651 if (TREE_CODE (probe) == ADDR_EXPR
6652 && TYPE_PTR_P (TREE_TYPE (probe)))
6654 expr = probe;
6655 expr_type = TREE_TYPE (expr);
6659 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6661 A template-argument for a non-type, non-template template-parameter
6662 shall be one of: [...]
6664 -- the name of a non-type template-parameter;
6665 -- the address of an object or function with external linkage, [...]
6666 expressed as "& id-expression" where the & is optional if the name
6667 refers to a function or array, or if the corresponding
6668 template-parameter is a reference.
6670 Here, we do not care about functions, as they are invalid anyway
6671 for a parameter of type pointer-to-object. */
6673 if (value_dependent_expression_p (expr))
6674 /* Non-type template parameters are OK. */
6676 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6677 /* Null pointer values are OK in C++11. */;
6678 else if (TREE_CODE (expr) != ADDR_EXPR)
6680 if (VAR_P (expr))
6682 if (complain & tf_error)
6683 error ("%qD is not a valid template argument "
6684 "because %qD is a variable, not the address of "
6685 "a variable", orig_expr, expr);
6686 return NULL_TREE;
6688 if (POINTER_TYPE_P (expr_type))
6690 if (complain & tf_error)
6691 error ("%qE is not a valid template argument for %qT "
6692 "because it is not the address of a variable",
6693 orig_expr, type);
6694 return NULL_TREE;
6696 /* Other values, like integer constants, might be valid
6697 non-type arguments of some other type. */
6698 return error_mark_node;
6700 else
6702 tree decl = TREE_OPERAND (expr, 0);
6704 if (!VAR_P (decl))
6706 if (complain & tf_error)
6707 error ("%qE is not a valid template argument of type %qT "
6708 "because %qE is not a variable", orig_expr, type, decl);
6709 return NULL_TREE;
6711 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6713 if (complain & tf_error)
6714 error ("%qE is not a valid template argument of type %qT "
6715 "because %qD does not have external linkage",
6716 orig_expr, type, decl);
6717 return NULL_TREE;
6719 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6720 && decl_linkage (decl) == lk_none)
6722 if (complain & tf_error)
6723 error ("%qE is not a valid template argument of type %qT "
6724 "because %qD has no linkage", orig_expr, type, decl);
6725 return NULL_TREE;
6727 /* C++17: For a non-type template-parameter of reference or pointer
6728 type, the value of the constant expression shall not refer to (or
6729 for a pointer type, shall not be the address of):
6730 * a subobject (4.5),
6731 * a temporary object (15.2),
6732 * a string literal (5.13.5),
6733 * the result of a typeid expression (8.2.8), or
6734 * a predefined __func__ variable (11.4.1). */
6735 else if (DECL_ARTIFICIAL (decl))
6737 if (complain & tf_error)
6738 error ("the address of %qD is not a valid template argument",
6739 decl);
6740 return NULL_TREE;
6742 else if (!same_type_ignoring_top_level_qualifiers_p
6743 (strip_array_types (TREE_TYPE (type)),
6744 strip_array_types (TREE_TYPE (decl))))
6746 if (complain & tf_error)
6747 error ("the address of the %qT subobject of %qD is not a "
6748 "valid template argument", TREE_TYPE (type), decl);
6749 return NULL_TREE;
6751 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6753 if (complain & tf_error)
6754 error ("the address of %qD is not a valid template argument "
6755 "because it does not have static storage duration",
6756 decl);
6757 return NULL_TREE;
6761 expr = decayed;
6763 expr = perform_qualification_conversions (type, expr);
6764 if (expr == error_mark_node)
6765 return error_mark_node;
6767 /* [temp.arg.nontype]/5, bullet 3
6769 For a non-type template-parameter of type reference to object, no
6770 conversions apply. The type referred to by the reference may be more
6771 cv-qualified than the (otherwise identical) type of the
6772 template-argument. The template-parameter is bound directly to the
6773 template-argument, which must be an lvalue. */
6774 else if (TYPE_REF_OBJ_P (type))
6776 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6777 expr_type))
6778 return error_mark_node;
6780 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6782 if (complain & tf_error)
6783 error ("%qE is not a valid template argument for type %qT "
6784 "because of conflicts in cv-qualification", expr, type);
6785 return NULL_TREE;
6788 if (!lvalue_p (expr))
6790 if (complain & tf_error)
6791 error ("%qE is not a valid template argument for type %qT "
6792 "because it is not an lvalue", expr, type);
6793 return NULL_TREE;
6796 /* [temp.arg.nontype]/1
6798 A template-argument for a non-type, non-template template-parameter
6799 shall be one of: [...]
6801 -- the address of an object or function with external linkage. */
6802 if (INDIRECT_REF_P (expr)
6803 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6805 expr = TREE_OPERAND (expr, 0);
6806 if (DECL_P (expr))
6808 if (complain & tf_error)
6809 error ("%q#D is not a valid template argument for type %qT "
6810 "because a reference variable does not have a constant "
6811 "address", expr, type);
6812 return NULL_TREE;
6816 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6817 && value_dependent_expression_p (expr))
6818 /* OK, dependent reference. We don't want to ask whether a DECL is
6819 itself value-dependent, since what we want here is its address. */;
6820 else
6822 if (!DECL_P (expr))
6824 if (complain & tf_error)
6825 error ("%qE is not a valid template argument for type %qT "
6826 "because it is not an object with linkage",
6827 expr, type);
6828 return NULL_TREE;
6831 /* DR 1155 allows internal linkage in C++11 and up. */
6832 linkage_kind linkage = decl_linkage (expr);
6833 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6835 if (complain & tf_error)
6836 error ("%qE is not a valid template argument for type %qT "
6837 "because object %qD does not have linkage",
6838 expr, type, expr);
6839 return NULL_TREE;
6842 expr = build_address (expr);
6845 if (!same_type_p (type, TREE_TYPE (expr)))
6846 expr = build_nop (type, expr);
6848 /* [temp.arg.nontype]/5, bullet 4
6850 For a non-type template-parameter of type pointer to function, only
6851 the function-to-pointer conversion (_conv.func_) is applied. If the
6852 template-argument represents a set of overloaded functions (or a
6853 pointer to such), the matching function is selected from the set
6854 (_over.over_). */
6855 else if (TYPE_PTRFN_P (type))
6857 /* If the argument is a template-id, we might not have enough
6858 context information to decay the pointer. */
6859 if (!type_unknown_p (expr_type))
6861 expr = decay_conversion (expr, complain);
6862 if (expr == error_mark_node)
6863 return error_mark_node;
6866 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6867 /* Null pointer values are OK in C++11. */
6868 return perform_qualification_conversions (type, expr);
6870 expr = convert_nontype_argument_function (type, expr, complain);
6871 if (!expr || expr == error_mark_node)
6872 return expr;
6874 /* [temp.arg.nontype]/5, bullet 5
6876 For a non-type template-parameter of type reference to function, no
6877 conversions apply. If the template-argument represents a set of
6878 overloaded functions, the matching function is selected from the set
6879 (_over.over_). */
6880 else if (TYPE_REFFN_P (type))
6882 if (TREE_CODE (expr) == ADDR_EXPR)
6884 if (complain & tf_error)
6886 error ("%qE is not a valid template argument for type %qT "
6887 "because it is a pointer", expr, type);
6888 inform (input_location, "try using %qE instead",
6889 TREE_OPERAND (expr, 0));
6891 return NULL_TREE;
6894 expr = convert_nontype_argument_function (type, expr, complain);
6895 if (!expr || expr == error_mark_node)
6896 return expr;
6898 /* [temp.arg.nontype]/5, bullet 6
6900 For a non-type template-parameter of type pointer to member function,
6901 no conversions apply. If the template-argument represents a set of
6902 overloaded member functions, the matching member function is selected
6903 from the set (_over.over_). */
6904 else if (TYPE_PTRMEMFUNC_P (type))
6906 expr = instantiate_type (type, expr, tf_none);
6907 if (expr == error_mark_node)
6908 return error_mark_node;
6910 /* [temp.arg.nontype] bullet 1 says the pointer to member
6911 expression must be a pointer-to-member constant. */
6912 if (!value_dependent_expression_p (expr)
6913 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6914 return NULL_TREE;
6916 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6917 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6918 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6919 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6921 /* [temp.arg.nontype]/5, bullet 7
6923 For a non-type template-parameter of type pointer to data member,
6924 qualification conversions (_conv.qual_) are applied. */
6925 else if (TYPE_PTRDATAMEM_P (type))
6927 /* [temp.arg.nontype] bullet 1 says the pointer to member
6928 expression must be a pointer-to-member constant. */
6929 if (!value_dependent_expression_p (expr)
6930 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6931 return NULL_TREE;
6933 expr = perform_qualification_conversions (type, expr);
6934 if (expr == error_mark_node)
6935 return expr;
6937 else if (NULLPTR_TYPE_P (type))
6939 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6941 if (complain & tf_error)
6942 error ("%qE is not a valid template argument for type %qT "
6943 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6944 return NULL_TREE;
6946 return expr;
6948 /* A template non-type parameter must be one of the above. */
6949 else
6950 gcc_unreachable ();
6952 /* Sanity check: did we actually convert the argument to the
6953 right type? */
6954 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6955 (type, TREE_TYPE (expr)));
6956 return convert_from_reference (expr);
6959 /* Subroutine of coerce_template_template_parms, which returns 1 if
6960 PARM_PARM and ARG_PARM match using the rule for the template
6961 parameters of template template parameters. Both PARM and ARG are
6962 template parameters; the rest of the arguments are the same as for
6963 coerce_template_template_parms.
6965 static int
6966 coerce_template_template_parm (tree parm,
6967 tree arg,
6968 tsubst_flags_t complain,
6969 tree in_decl,
6970 tree outer_args)
6972 if (arg == NULL_TREE || error_operand_p (arg)
6973 || parm == NULL_TREE || error_operand_p (parm))
6974 return 0;
6976 if (TREE_CODE (arg) != TREE_CODE (parm))
6977 return 0;
6979 switch (TREE_CODE (parm))
6981 case TEMPLATE_DECL:
6982 /* We encounter instantiations of templates like
6983 template <template <template <class> class> class TT>
6984 class C; */
6986 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6987 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6989 if (!coerce_template_template_parms
6990 (parmparm, argparm, complain, in_decl, outer_args))
6991 return 0;
6993 /* Fall through. */
6995 case TYPE_DECL:
6996 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6997 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6998 /* Argument is a parameter pack but parameter is not. */
6999 return 0;
7000 break;
7002 case PARM_DECL:
7003 /* The tsubst call is used to handle cases such as
7005 template <int> class C {};
7006 template <class T, template <T> class TT> class D {};
7007 D<int, C> d;
7009 i.e. the parameter list of TT depends on earlier parameters. */
7010 if (!uses_template_parms (TREE_TYPE (arg)))
7012 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7013 if (!uses_template_parms (t)
7014 && !same_type_p (t, TREE_TYPE (arg)))
7015 return 0;
7018 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7019 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7020 /* Argument is a parameter pack but parameter is not. */
7021 return 0;
7023 break;
7025 default:
7026 gcc_unreachable ();
7029 return 1;
7032 /* Coerce template argument list ARGLIST for use with template
7033 template-parameter TEMPL. */
7035 static tree
7036 coerce_template_args_for_ttp (tree templ, tree arglist,
7037 tsubst_flags_t complain)
7039 /* Consider an example where a template template parameter declared as
7041 template <class T, class U = std::allocator<T> > class TT
7043 The template parameter level of T and U are one level larger than
7044 of TT. To proper process the default argument of U, say when an
7045 instantiation `TT<int>' is seen, we need to build the full
7046 arguments containing {int} as the innermost level. Outer levels,
7047 available when not appearing as default template argument, can be
7048 obtained from the arguments of the enclosing template.
7050 Suppose that TT is later substituted with std::vector. The above
7051 instantiation is `TT<int, std::allocator<T> >' with TT at
7052 level 1, and T at level 2, while the template arguments at level 1
7053 becomes {std::vector} and the inner level 2 is {int}. */
7055 tree outer = DECL_CONTEXT (templ);
7056 if (outer)
7058 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7059 /* We want arguments for the partial specialization, not arguments for
7060 the primary template. */
7061 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7062 else
7063 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7065 else if (current_template_parms)
7067 /* This is an argument of the current template, so we haven't set
7068 DECL_CONTEXT yet. */
7069 tree relevant_template_parms;
7071 /* Parameter levels that are greater than the level of the given
7072 template template parm are irrelevant. */
7073 relevant_template_parms = current_template_parms;
7074 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7075 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7076 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7078 outer = template_parms_to_args (relevant_template_parms);
7081 if (outer)
7082 arglist = add_to_template_args (outer, arglist);
7084 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7085 return coerce_template_parms (parmlist, arglist, templ,
7086 complain,
7087 /*require_all_args=*/true,
7088 /*use_default_args=*/true);
7091 /* A cache of template template parameters with match-all default
7092 arguments. */
7093 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7094 static void
7095 store_defaulted_ttp (tree v, tree t)
7097 if (!defaulted_ttp_cache)
7098 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7099 defaulted_ttp_cache->put (v, t);
7101 static tree
7102 lookup_defaulted_ttp (tree v)
7104 if (defaulted_ttp_cache)
7105 if (tree *p = defaulted_ttp_cache->get (v))
7106 return *p;
7107 return NULL_TREE;
7110 /* T is a bound template template-parameter. Copy its arguments into default
7111 arguments of the template template-parameter's template parameters. */
7113 static tree
7114 add_defaults_to_ttp (tree otmpl)
7116 if (tree c = lookup_defaulted_ttp (otmpl))
7117 return c;
7119 tree ntmpl = copy_node (otmpl);
7121 tree ntype = copy_node (TREE_TYPE (otmpl));
7122 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7123 TYPE_MAIN_VARIANT (ntype) = ntype;
7124 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7125 TYPE_NAME (ntype) = ntmpl;
7126 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7128 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7129 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7130 TEMPLATE_PARM_DECL (idx) = ntmpl;
7131 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7133 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7134 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7135 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7136 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7137 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7139 tree o = TREE_VEC_ELT (vec, i);
7140 if (!template_parameter_pack_p (TREE_VALUE (o)))
7142 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7143 TREE_PURPOSE (n) = any_targ_node;
7147 store_defaulted_ttp (otmpl, ntmpl);
7148 return ntmpl;
7151 /* ARG is a bound potential template template-argument, and PARGS is a list
7152 of arguments for the corresponding template template-parameter. Adjust
7153 PARGS as appropriate for application to ARG's template, and if ARG is a
7154 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7155 arguments to the template template parameter. */
7157 static tree
7158 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7160 ++processing_template_decl;
7161 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7162 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7164 /* When comparing two template template-parameters in partial ordering,
7165 rewrite the one currently being used as an argument to have default
7166 arguments for all parameters. */
7167 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7168 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7169 if (pargs != error_mark_node)
7170 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7171 TYPE_TI_ARGS (arg));
7173 else
7175 tree aparms
7176 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7177 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7178 /*require_all*/true,
7179 /*use_default*/true);
7181 --processing_template_decl;
7182 return pargs;
7185 /* Subroutine of unify for the case when PARM is a
7186 BOUND_TEMPLATE_TEMPLATE_PARM. */
7188 static int
7189 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7190 bool explain_p)
7192 tree parmvec = TYPE_TI_ARGS (parm);
7193 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7195 /* The template template parm might be variadic and the argument
7196 not, so flatten both argument lists. */
7197 parmvec = expand_template_argument_pack (parmvec);
7198 argvec = expand_template_argument_pack (argvec);
7200 if (flag_new_ttp)
7202 /* In keeping with P0522R0, adjust P's template arguments
7203 to apply to A's template; then flatten it again. */
7204 tree nparmvec = parmvec;
7205 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7206 nparmvec = expand_template_argument_pack (nparmvec);
7208 if (unify (tparms, targs, nparmvec, argvec,
7209 UNIFY_ALLOW_NONE, explain_p))
7210 return 1;
7212 /* If the P0522 adjustment eliminated a pack expansion, deduce
7213 empty packs. */
7214 if (flag_new_ttp
7215 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7216 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7217 DEDUCE_EXACT, /*sub*/true, explain_p))
7218 return 1;
7220 else
7222 /* Deduce arguments T, i from TT<T> or TT<i>.
7223 We check each element of PARMVEC and ARGVEC individually
7224 rather than the whole TREE_VEC since they can have
7225 different number of elements, which is allowed under N2555. */
7227 int len = TREE_VEC_LENGTH (parmvec);
7229 /* Check if the parameters end in a pack, making them
7230 variadic. */
7231 int parm_variadic_p = 0;
7232 if (len > 0
7233 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7234 parm_variadic_p = 1;
7236 for (int i = 0; i < len - parm_variadic_p; ++i)
7237 /* If the template argument list of P contains a pack
7238 expansion that is not the last template argument, the
7239 entire template argument list is a non-deduced
7240 context. */
7241 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7242 return unify_success (explain_p);
7244 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7245 return unify_too_few_arguments (explain_p,
7246 TREE_VEC_LENGTH (argvec), len);
7248 for (int i = 0; i < len - parm_variadic_p; ++i)
7249 if (unify (tparms, targs,
7250 TREE_VEC_ELT (parmvec, i),
7251 TREE_VEC_ELT (argvec, i),
7252 UNIFY_ALLOW_NONE, explain_p))
7253 return 1;
7255 if (parm_variadic_p
7256 && unify_pack_expansion (tparms, targs,
7257 parmvec, argvec,
7258 DEDUCE_EXACT,
7259 /*subr=*/true, explain_p))
7260 return 1;
7263 return 0;
7266 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7267 template template parameters. Both PARM_PARMS and ARG_PARMS are
7268 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7269 or PARM_DECL.
7271 Consider the example:
7272 template <class T> class A;
7273 template<template <class U> class TT> class B;
7275 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7276 the parameters to A, and OUTER_ARGS contains A. */
7278 static int
7279 coerce_template_template_parms (tree parm_parms,
7280 tree arg_parms,
7281 tsubst_flags_t complain,
7282 tree in_decl,
7283 tree outer_args)
7285 int nparms, nargs, i;
7286 tree parm, arg;
7287 int variadic_p = 0;
7289 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7290 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7292 nparms = TREE_VEC_LENGTH (parm_parms);
7293 nargs = TREE_VEC_LENGTH (arg_parms);
7295 if (flag_new_ttp)
7297 /* P0522R0: A template template-parameter P is at least as specialized as
7298 a template template-argument A if, given the following rewrite to two
7299 function templates, the function template corresponding to P is at
7300 least as specialized as the function template corresponding to A
7301 according to the partial ordering rules for function templates
7302 ([temp.func.order]). Given an invented class template X with the
7303 template parameter list of A (including default arguments):
7305 * Each of the two function templates has the same template parameters,
7306 respectively, as P or A.
7308 * Each function template has a single function parameter whose type is
7309 a specialization of X with template arguments corresponding to the
7310 template parameters from the respective function template where, for
7311 each template parameter PP in the template parameter list of the
7312 function template, a corresponding template argument AA is formed. If
7313 PP declares a parameter pack, then AA is the pack expansion
7314 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7316 If the rewrite produces an invalid type, then P is not at least as
7317 specialized as A. */
7319 /* So coerce P's args to apply to A's parms, and then deduce between A's
7320 args and the converted args. If that succeeds, A is at least as
7321 specialized as P, so they match.*/
7322 tree pargs = template_parms_level_to_args (parm_parms);
7323 ++processing_template_decl;
7324 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7325 /*require_all*/true, /*use_default*/true);
7326 --processing_template_decl;
7327 if (pargs != error_mark_node)
7329 tree targs = make_tree_vec (nargs);
7330 tree aargs = template_parms_level_to_args (arg_parms);
7331 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7332 /*explain*/false))
7333 return 1;
7337 /* Determine whether we have a parameter pack at the end of the
7338 template template parameter's template parameter list. */
7339 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7341 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7343 if (error_operand_p (parm))
7344 return 0;
7346 switch (TREE_CODE (parm))
7348 case TEMPLATE_DECL:
7349 case TYPE_DECL:
7350 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7351 variadic_p = 1;
7352 break;
7354 case PARM_DECL:
7355 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7356 variadic_p = 1;
7357 break;
7359 default:
7360 gcc_unreachable ();
7364 if (nargs != nparms
7365 && !(variadic_p && nargs >= nparms - 1))
7366 return 0;
7368 /* Check all of the template parameters except the parameter pack at
7369 the end (if any). */
7370 for (i = 0; i < nparms - variadic_p; ++i)
7372 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7373 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7374 continue;
7376 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7377 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7379 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7380 outer_args))
7381 return 0;
7385 if (variadic_p)
7387 /* Check each of the template parameters in the template
7388 argument against the template parameter pack at the end of
7389 the template template parameter. */
7390 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7391 return 0;
7393 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7395 for (; i < nargs; ++i)
7397 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7398 continue;
7400 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7402 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7403 outer_args))
7404 return 0;
7408 return 1;
7411 /* Verifies that the deduced template arguments (in TARGS) for the
7412 template template parameters (in TPARMS) represent valid bindings,
7413 by comparing the template parameter list of each template argument
7414 to the template parameter list of its corresponding template
7415 template parameter, in accordance with DR150. This
7416 routine can only be called after all template arguments have been
7417 deduced. It will return TRUE if all of the template template
7418 parameter bindings are okay, FALSE otherwise. */
7419 bool
7420 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7422 int i, ntparms = TREE_VEC_LENGTH (tparms);
7423 bool ret = true;
7425 /* We're dealing with template parms in this process. */
7426 ++processing_template_decl;
7428 targs = INNERMOST_TEMPLATE_ARGS (targs);
7430 for (i = 0; i < ntparms; ++i)
7432 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7433 tree targ = TREE_VEC_ELT (targs, i);
7435 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7437 tree packed_args = NULL_TREE;
7438 int idx, len = 1;
7440 if (ARGUMENT_PACK_P (targ))
7442 /* Look inside the argument pack. */
7443 packed_args = ARGUMENT_PACK_ARGS (targ);
7444 len = TREE_VEC_LENGTH (packed_args);
7447 for (idx = 0; idx < len; ++idx)
7449 tree targ_parms = NULL_TREE;
7451 if (packed_args)
7452 /* Extract the next argument from the argument
7453 pack. */
7454 targ = TREE_VEC_ELT (packed_args, idx);
7456 if (PACK_EXPANSION_P (targ))
7457 /* Look at the pattern of the pack expansion. */
7458 targ = PACK_EXPANSION_PATTERN (targ);
7460 /* Extract the template parameters from the template
7461 argument. */
7462 if (TREE_CODE (targ) == TEMPLATE_DECL)
7463 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7464 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7465 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7467 /* Verify that we can coerce the template template
7468 parameters from the template argument to the template
7469 parameter. This requires an exact match. */
7470 if (targ_parms
7471 && !coerce_template_template_parms
7472 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7473 targ_parms,
7474 tf_none,
7475 tparm,
7476 targs))
7478 ret = false;
7479 goto out;
7485 out:
7487 --processing_template_decl;
7488 return ret;
7491 /* Since type attributes aren't mangled, we need to strip them from
7492 template type arguments. */
7494 static tree
7495 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7497 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7498 return arg;
7499 bool removed_attributes = false;
7500 tree canon = strip_typedefs (arg, &removed_attributes);
7501 if (removed_attributes
7502 && (complain & tf_warning))
7503 warning (OPT_Wignored_attributes,
7504 "ignoring attributes on template argument %qT", arg);
7505 return canon;
7508 /* And from inside dependent non-type arguments like sizeof(Type). */
7510 static tree
7511 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7513 if (!arg || arg == error_mark_node)
7514 return arg;
7515 bool removed_attributes = false;
7516 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7517 if (removed_attributes
7518 && (complain & tf_warning))
7519 warning (OPT_Wignored_attributes,
7520 "ignoring attributes in template argument %qE", arg);
7521 return canon;
7524 // A template declaration can be substituted for a constrained
7525 // template template parameter only when the argument is more
7526 // constrained than the parameter.
7527 static bool
7528 is_compatible_template_arg (tree parm, tree arg)
7530 tree parm_cons = get_constraints (parm);
7532 /* For now, allow constrained template template arguments
7533 and unconstrained template template parameters. */
7534 if (parm_cons == NULL_TREE)
7535 return true;
7537 tree arg_cons = get_constraints (arg);
7539 // If the template parameter is constrained, we need to rewrite its
7540 // constraints in terms of the ARG's template parameters. This ensures
7541 // that all of the template parameter types will have the same depth.
7543 // Note that this is only valid when coerce_template_template_parm is
7544 // true for the innermost template parameters of PARM and ARG. In other
7545 // words, because coercion is successful, this conversion will be valid.
7546 if (parm_cons)
7548 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7549 parm_cons = tsubst_constraint_info (parm_cons,
7550 INNERMOST_TEMPLATE_ARGS (args),
7551 tf_none, NULL_TREE);
7552 if (parm_cons == error_mark_node)
7553 return false;
7556 return subsumes (parm_cons, arg_cons);
7559 // Convert a placeholder argument into a binding to the original
7560 // parameter. The original parameter is saved as the TREE_TYPE of
7561 // ARG.
7562 static inline tree
7563 convert_wildcard_argument (tree parm, tree arg)
7565 TREE_TYPE (arg) = parm;
7566 return arg;
7569 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7570 because one of them is dependent. But we need to represent the
7571 conversion for the benefit of cp_tree_equal. */
7573 static tree
7574 maybe_convert_nontype_argument (tree type, tree arg)
7576 /* Auto parms get no conversion. */
7577 if (type_uses_auto (type))
7578 return arg;
7579 /* We don't need or want to add this conversion now if we're going to use the
7580 argument for deduction. */
7581 if (value_dependent_expression_p (arg))
7582 return arg;
7584 type = cv_unqualified (type);
7585 tree argtype = TREE_TYPE (arg);
7586 if (same_type_p (type, argtype))
7587 return arg;
7589 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7590 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7591 return arg;
7594 /* Convert the indicated template ARG as necessary to match the
7595 indicated template PARM. Returns the converted ARG, or
7596 error_mark_node if the conversion was unsuccessful. Error and
7597 warning messages are issued under control of COMPLAIN. This
7598 conversion is for the Ith parameter in the parameter list. ARGS is
7599 the full set of template arguments deduced so far. */
7601 static tree
7602 convert_template_argument (tree parm,
7603 tree arg,
7604 tree args,
7605 tsubst_flags_t complain,
7606 int i,
7607 tree in_decl)
7609 tree orig_arg;
7610 tree val;
7611 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7613 if (parm == error_mark_node)
7614 return error_mark_node;
7616 /* Trivially convert placeholders. */
7617 if (TREE_CODE (arg) == WILDCARD_DECL)
7618 return convert_wildcard_argument (parm, arg);
7620 if (arg == any_targ_node)
7621 return arg;
7623 if (TREE_CODE (arg) == TREE_LIST
7624 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7626 /* The template argument was the name of some
7627 member function. That's usually
7628 invalid, but static members are OK. In any
7629 case, grab the underlying fields/functions
7630 and issue an error later if required. */
7631 orig_arg = TREE_VALUE (arg);
7632 TREE_TYPE (arg) = unknown_type_node;
7635 orig_arg = arg;
7637 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7638 requires_type = (TREE_CODE (parm) == TYPE_DECL
7639 || requires_tmpl_type);
7641 /* When determining whether an argument pack expansion is a template,
7642 look at the pattern. */
7643 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7644 arg = PACK_EXPANSION_PATTERN (arg);
7646 /* Deal with an injected-class-name used as a template template arg. */
7647 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7649 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7650 if (TREE_CODE (t) == TEMPLATE_DECL)
7652 if (cxx_dialect >= cxx11)
7653 /* OK under DR 1004. */;
7654 else if (complain & tf_warning_or_error)
7655 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7656 " used as template template argument", TYPE_NAME (arg));
7657 else if (flag_pedantic_errors)
7658 t = arg;
7660 arg = t;
7664 is_tmpl_type =
7665 ((TREE_CODE (arg) == TEMPLATE_DECL
7666 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7667 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7668 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7669 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7671 if (is_tmpl_type
7672 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7673 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7674 arg = TYPE_STUB_DECL (arg);
7676 is_type = TYPE_P (arg) || is_tmpl_type;
7678 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7679 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7681 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7683 if (complain & tf_error)
7684 error ("invalid use of destructor %qE as a type", orig_arg);
7685 return error_mark_node;
7688 permerror (input_location,
7689 "to refer to a type member of a template parameter, "
7690 "use %<typename %E%>", orig_arg);
7692 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7693 TREE_OPERAND (arg, 1),
7694 typename_type,
7695 complain);
7696 arg = orig_arg;
7697 is_type = 1;
7699 if (is_type != requires_type)
7701 if (in_decl)
7703 if (complain & tf_error)
7705 error ("type/value mismatch at argument %d in template "
7706 "parameter list for %qD",
7707 i + 1, in_decl);
7708 if (is_type)
7709 inform (input_location,
7710 " expected a constant of type %qT, got %qT",
7711 TREE_TYPE (parm),
7712 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7713 else if (requires_tmpl_type)
7714 inform (input_location,
7715 " expected a class template, got %qE", orig_arg);
7716 else
7717 inform (input_location,
7718 " expected a type, got %qE", orig_arg);
7721 return error_mark_node;
7723 if (is_tmpl_type ^ requires_tmpl_type)
7725 if (in_decl && (complain & tf_error))
7727 error ("type/value mismatch at argument %d in template "
7728 "parameter list for %qD",
7729 i + 1, in_decl);
7730 if (is_tmpl_type)
7731 inform (input_location,
7732 " expected a type, got %qT", DECL_NAME (arg));
7733 else
7734 inform (input_location,
7735 " expected a class template, got %qT", orig_arg);
7737 return error_mark_node;
7740 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7741 /* We already did the appropriate conversion when packing args. */
7742 val = orig_arg;
7743 else if (is_type)
7745 if (requires_tmpl_type)
7747 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7748 /* The number of argument required is not known yet.
7749 Just accept it for now. */
7750 val = orig_arg;
7751 else
7753 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7754 tree argparm;
7756 /* Strip alias templates that are equivalent to another
7757 template. */
7758 arg = get_underlying_template (arg);
7759 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7761 if (coerce_template_template_parms (parmparm, argparm,
7762 complain, in_decl,
7763 args))
7765 val = arg;
7767 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7768 TEMPLATE_DECL. */
7769 if (val != error_mark_node)
7771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7772 val = TREE_TYPE (val);
7773 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7774 val = make_pack_expansion (val, complain);
7777 else
7779 if (in_decl && (complain & tf_error))
7781 error ("type/value mismatch at argument %d in "
7782 "template parameter list for %qD",
7783 i + 1, in_decl);
7784 inform (input_location,
7785 " expected a template of type %qD, got %qT",
7786 parm, orig_arg);
7789 val = error_mark_node;
7792 // Check that the constraints are compatible before allowing the
7793 // substitution.
7794 if (val != error_mark_node)
7795 if (!is_compatible_template_arg (parm, arg))
7797 if (in_decl && (complain & tf_error))
7799 error ("constraint mismatch at argument %d in "
7800 "template parameter list for %qD",
7801 i + 1, in_decl);
7802 inform (input_location, " expected %qD but got %qD",
7803 parm, arg);
7805 val = error_mark_node;
7809 else
7810 val = orig_arg;
7811 /* We only form one instance of each template specialization.
7812 Therefore, if we use a non-canonical variant (i.e., a
7813 typedef), any future messages referring to the type will use
7814 the typedef, which is confusing if those future uses do not
7815 themselves also use the typedef. */
7816 if (TYPE_P (val))
7817 val = canonicalize_type_argument (val, complain);
7819 else
7821 tree t = TREE_TYPE (parm);
7823 if (tree a = type_uses_auto (t))
7825 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7826 if (t == error_mark_node)
7827 return error_mark_node;
7829 else
7830 t = tsubst (t, args, complain, in_decl);
7832 if (invalid_nontype_parm_type_p (t, complain))
7833 return error_mark_node;
7835 if (!type_dependent_expression_p (orig_arg)
7836 && !uses_template_parms (t))
7837 /* We used to call digest_init here. However, digest_init
7838 will report errors, which we don't want when complain
7839 is zero. More importantly, digest_init will try too
7840 hard to convert things: for example, `0' should not be
7841 converted to pointer type at this point according to
7842 the standard. Accepting this is not merely an
7843 extension, since deciding whether or not these
7844 conversions can occur is part of determining which
7845 function template to call, or whether a given explicit
7846 argument specification is valid. */
7847 val = convert_nontype_argument (t, orig_arg, complain);
7848 else
7850 val = canonicalize_expr_argument (orig_arg, complain);
7851 val = maybe_convert_nontype_argument (t, val);
7855 if (val == NULL_TREE)
7856 val = error_mark_node;
7857 else if (val == error_mark_node && (complain & tf_error))
7858 error ("could not convert template argument %qE from %qT to %qT",
7859 orig_arg, TREE_TYPE (orig_arg), t);
7861 if (INDIRECT_REF_P (val))
7863 /* Reject template arguments that are references to built-in
7864 functions with no library fallbacks. */
7865 const_tree inner = TREE_OPERAND (val, 0);
7866 const_tree innertype = TREE_TYPE (inner);
7867 if (innertype
7868 && TREE_CODE (innertype) == REFERENCE_TYPE
7869 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7870 && TREE_OPERAND_LENGTH (inner) > 0
7871 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7872 return error_mark_node;
7875 if (TREE_CODE (val) == SCOPE_REF)
7877 /* Strip typedefs from the SCOPE_REF. */
7878 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7879 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7880 complain);
7881 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7882 QUALIFIED_NAME_IS_TEMPLATE (val));
7886 return val;
7889 /* Coerces the remaining template arguments in INNER_ARGS (from
7890 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7891 Returns the coerced argument pack. PARM_IDX is the position of this
7892 parameter in the template parameter list. ARGS is the original
7893 template argument list. */
7894 static tree
7895 coerce_template_parameter_pack (tree parms,
7896 int parm_idx,
7897 tree args,
7898 tree inner_args,
7899 int arg_idx,
7900 tree new_args,
7901 int* lost,
7902 tree in_decl,
7903 tsubst_flags_t complain)
7905 tree parm = TREE_VEC_ELT (parms, parm_idx);
7906 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7907 tree packed_args;
7908 tree argument_pack;
7909 tree packed_parms = NULL_TREE;
7911 if (arg_idx > nargs)
7912 arg_idx = nargs;
7914 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7916 /* When the template parameter is a non-type template parameter pack
7917 or template template parameter pack whose type or template
7918 parameters use parameter packs, we know exactly how many arguments
7919 we are looking for. Build a vector of the instantiated decls for
7920 these template parameters in PACKED_PARMS. */
7921 /* We can't use make_pack_expansion here because it would interpret a
7922 _DECL as a use rather than a declaration. */
7923 tree decl = TREE_VALUE (parm);
7924 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7925 SET_PACK_EXPANSION_PATTERN (exp, decl);
7926 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7927 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7929 TREE_VEC_LENGTH (args)--;
7930 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7931 TREE_VEC_LENGTH (args)++;
7933 if (packed_parms == error_mark_node)
7934 return error_mark_node;
7936 /* If we're doing a partial instantiation of a member template,
7937 verify that all of the types used for the non-type
7938 template parameter pack are, in fact, valid for non-type
7939 template parameters. */
7940 if (arg_idx < nargs
7941 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7943 int j, len = TREE_VEC_LENGTH (packed_parms);
7944 for (j = 0; j < len; ++j)
7946 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7947 if (invalid_nontype_parm_type_p (t, complain))
7948 return error_mark_node;
7950 /* We don't know how many args we have yet, just
7951 use the unconverted ones for now. */
7952 return NULL_TREE;
7955 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7957 /* Check if we have a placeholder pack, which indicates we're
7958 in the context of a introduction list. In that case we want
7959 to match this pack to the single placeholder. */
7960 else if (arg_idx < nargs
7961 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7962 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7964 nargs = arg_idx + 1;
7965 packed_args = make_tree_vec (1);
7967 else
7968 packed_args = make_tree_vec (nargs - arg_idx);
7970 /* Convert the remaining arguments, which will be a part of the
7971 parameter pack "parm". */
7972 int first_pack_arg = arg_idx;
7973 for (; arg_idx < nargs; ++arg_idx)
7975 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7976 tree actual_parm = TREE_VALUE (parm);
7977 int pack_idx = arg_idx - first_pack_arg;
7979 if (packed_parms)
7981 /* Once we've packed as many args as we have types, stop. */
7982 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7983 break;
7984 else if (PACK_EXPANSION_P (arg))
7985 /* We don't know how many args we have yet, just
7986 use the unconverted ones for now. */
7987 return NULL_TREE;
7988 else
7989 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7992 if (arg == error_mark_node)
7994 if (complain & tf_error)
7995 error ("template argument %d is invalid", arg_idx + 1);
7997 else
7998 arg = convert_template_argument (actual_parm,
7999 arg, new_args, complain, parm_idx,
8000 in_decl);
8001 if (arg == error_mark_node)
8002 (*lost)++;
8003 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8006 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8007 && TREE_VEC_LENGTH (packed_args) > 0)
8009 if (complain & tf_error)
8010 error ("wrong number of template arguments (%d, should be %d)",
8011 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8012 return error_mark_node;
8015 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8016 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8017 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8018 else
8020 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8021 TREE_CONSTANT (argument_pack) = 1;
8024 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8025 if (CHECKING_P)
8026 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8027 TREE_VEC_LENGTH (packed_args));
8028 return argument_pack;
8031 /* Returns the number of pack expansions in the template argument vector
8032 ARGS. */
8034 static int
8035 pack_expansion_args_count (tree args)
8037 int i;
8038 int count = 0;
8039 if (args)
8040 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8042 tree elt = TREE_VEC_ELT (args, i);
8043 if (elt && PACK_EXPANSION_P (elt))
8044 ++count;
8046 return count;
8049 /* Convert all template arguments to their appropriate types, and
8050 return a vector containing the innermost resulting template
8051 arguments. If any error occurs, return error_mark_node. Error and
8052 warning messages are issued under control of COMPLAIN.
8054 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8055 for arguments not specified in ARGS. Otherwise, if
8056 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8057 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8058 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8059 ARGS. */
8061 static tree
8062 coerce_template_parms (tree parms,
8063 tree args,
8064 tree in_decl,
8065 tsubst_flags_t complain,
8066 bool require_all_args,
8067 bool use_default_args)
8069 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8070 tree orig_inner_args;
8071 tree inner_args;
8072 tree new_args;
8073 tree new_inner_args;
8074 int saved_unevaluated_operand;
8075 int saved_inhibit_evaluation_warnings;
8077 /* When used as a boolean value, indicates whether this is a
8078 variadic template parameter list. Since it's an int, we can also
8079 subtract it from nparms to get the number of non-variadic
8080 parameters. */
8081 int variadic_p = 0;
8082 int variadic_args_p = 0;
8083 int post_variadic_parms = 0;
8085 /* Likewise for parameters with default arguments. */
8086 int default_p = 0;
8088 if (args == error_mark_node)
8089 return error_mark_node;
8091 nparms = TREE_VEC_LENGTH (parms);
8093 /* Determine if there are any parameter packs or default arguments. */
8094 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8096 tree parm = TREE_VEC_ELT (parms, parm_idx);
8097 if (variadic_p)
8098 ++post_variadic_parms;
8099 if (template_parameter_pack_p (TREE_VALUE (parm)))
8100 ++variadic_p;
8101 if (TREE_PURPOSE (parm))
8102 ++default_p;
8105 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8106 /* If there are no parameters that follow a parameter pack, we need to
8107 expand any argument packs so that we can deduce a parameter pack from
8108 some non-packed args followed by an argument pack, as in variadic85.C.
8109 If there are such parameters, we need to leave argument packs intact
8110 so the arguments are assigned properly. This can happen when dealing
8111 with a nested class inside a partial specialization of a class
8112 template, as in variadic92.C, or when deducing a template parameter pack
8113 from a sub-declarator, as in variadic114.C. */
8114 if (!post_variadic_parms)
8115 inner_args = expand_template_argument_pack (inner_args);
8117 /* Count any pack expansion args. */
8118 variadic_args_p = pack_expansion_args_count (inner_args);
8120 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8121 if ((nargs - variadic_args_p > nparms && !variadic_p)
8122 || (nargs < nparms - variadic_p
8123 && require_all_args
8124 && !variadic_args_p
8125 && (!use_default_args
8126 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8127 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8129 if (complain & tf_error)
8131 if (variadic_p || default_p)
8133 nparms -= variadic_p + default_p;
8134 error ("wrong number of template arguments "
8135 "(%d, should be at least %d)", nargs, nparms);
8137 else
8138 error ("wrong number of template arguments "
8139 "(%d, should be %d)", nargs, nparms);
8141 if (in_decl)
8142 inform (DECL_SOURCE_LOCATION (in_decl),
8143 "provided for %qD", in_decl);
8146 return error_mark_node;
8148 /* We can't pass a pack expansion to a non-pack parameter of an alias
8149 template (DR 1430). */
8150 else if (in_decl
8151 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8152 || concept_template_p (in_decl))
8153 && variadic_args_p
8154 && nargs - variadic_args_p < nparms - variadic_p)
8156 if (complain & tf_error)
8158 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8160 tree arg = TREE_VEC_ELT (inner_args, i);
8161 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8163 if (PACK_EXPANSION_P (arg)
8164 && !template_parameter_pack_p (parm))
8166 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8167 error_at (location_of (arg),
8168 "pack expansion argument for non-pack parameter "
8169 "%qD of alias template %qD", parm, in_decl);
8170 else
8171 error_at (location_of (arg),
8172 "pack expansion argument for non-pack parameter "
8173 "%qD of concept %qD", parm, in_decl);
8174 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8175 goto found;
8178 gcc_unreachable ();
8179 found:;
8181 return error_mark_node;
8184 /* We need to evaluate the template arguments, even though this
8185 template-id may be nested within a "sizeof". */
8186 saved_unevaluated_operand = cp_unevaluated_operand;
8187 cp_unevaluated_operand = 0;
8188 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8189 c_inhibit_evaluation_warnings = 0;
8190 new_inner_args = make_tree_vec (nparms);
8191 new_args = add_outermost_template_args (args, new_inner_args);
8192 int pack_adjust = 0;
8193 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8195 tree arg;
8196 tree parm;
8198 /* Get the Ith template parameter. */
8199 parm = TREE_VEC_ELT (parms, parm_idx);
8201 if (parm == error_mark_node)
8203 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8204 continue;
8207 /* Calculate the next argument. */
8208 if (arg_idx < nargs)
8209 arg = TREE_VEC_ELT (inner_args, arg_idx);
8210 else
8211 arg = NULL_TREE;
8213 if (template_parameter_pack_p (TREE_VALUE (parm))
8214 && !(arg && ARGUMENT_PACK_P (arg)))
8216 /* Some arguments will be placed in the
8217 template parameter pack PARM. */
8218 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8219 inner_args, arg_idx,
8220 new_args, &lost,
8221 in_decl, complain);
8223 if (arg == NULL_TREE)
8225 /* We don't know how many args we have yet, just use the
8226 unconverted (and still packed) ones for now. */
8227 new_inner_args = orig_inner_args;
8228 arg_idx = nargs;
8229 break;
8232 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8234 /* Store this argument. */
8235 if (arg == error_mark_node)
8237 lost++;
8238 /* We are done with all of the arguments. */
8239 arg_idx = nargs;
8241 else
8243 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8244 arg_idx += pack_adjust;
8247 continue;
8249 else if (arg)
8251 if (PACK_EXPANSION_P (arg))
8253 /* "If every valid specialization of a variadic template
8254 requires an empty template parameter pack, the template is
8255 ill-formed, no diagnostic required." So check that the
8256 pattern works with this parameter. */
8257 tree pattern = PACK_EXPANSION_PATTERN (arg);
8258 tree conv = convert_template_argument (TREE_VALUE (parm),
8259 pattern, new_args,
8260 complain, parm_idx,
8261 in_decl);
8262 if (conv == error_mark_node)
8264 if (complain & tf_error)
8265 inform (input_location, "so any instantiation with a "
8266 "non-empty parameter pack would be ill-formed");
8267 ++lost;
8269 else if (TYPE_P (conv) && !TYPE_P (pattern))
8270 /* Recover from missing typename. */
8271 TREE_VEC_ELT (inner_args, arg_idx)
8272 = make_pack_expansion (conv, complain);
8274 /* We don't know how many args we have yet, just
8275 use the unconverted ones for now. */
8276 new_inner_args = inner_args;
8277 arg_idx = nargs;
8278 break;
8281 else if (require_all_args)
8283 /* There must be a default arg in this case. */
8284 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8285 complain, in_decl);
8286 /* The position of the first default template argument,
8287 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8288 Record that. */
8289 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8290 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8291 arg_idx - pack_adjust);
8293 else
8294 break;
8296 if (arg == error_mark_node)
8298 if (complain & tf_error)
8299 error ("template argument %d is invalid", arg_idx + 1);
8301 else if (!arg)
8302 /* This only occurs if there was an error in the template
8303 parameter list itself (which we would already have
8304 reported) that we are trying to recover from, e.g., a class
8305 template with a parameter list such as
8306 template<typename..., typename>. */
8307 ++lost;
8308 else
8309 arg = convert_template_argument (TREE_VALUE (parm),
8310 arg, new_args, complain,
8311 parm_idx, in_decl);
8313 if (arg == error_mark_node)
8314 lost++;
8315 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8317 cp_unevaluated_operand = saved_unevaluated_operand;
8318 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8320 if (variadic_p && arg_idx < nargs)
8322 if (complain & tf_error)
8324 error ("wrong number of template arguments "
8325 "(%d, should be %d)", nargs, arg_idx);
8326 if (in_decl)
8327 error ("provided for %q+D", in_decl);
8329 return error_mark_node;
8332 if (lost)
8333 return error_mark_node;
8335 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8336 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8337 TREE_VEC_LENGTH (new_inner_args));
8339 return new_inner_args;
8342 /* Convert all template arguments to their appropriate types, and
8343 return a vector containing the innermost resulting template
8344 arguments. If any error occurs, return error_mark_node. Error and
8345 warning messages are not issued.
8347 Note that no function argument deduction is performed, and default
8348 arguments are used to fill in unspecified arguments. */
8349 tree
8350 coerce_template_parms (tree parms, tree args, tree in_decl)
8352 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8355 /* Convert all template arguments to their appropriate type, and
8356 instantiate default arguments as needed. This returns a vector
8357 containing the innermost resulting template arguments, or
8358 error_mark_node if unsuccessful. */
8359 tree
8360 coerce_template_parms (tree parms, tree args, tree in_decl,
8361 tsubst_flags_t complain)
8363 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8366 /* Like coerce_template_parms. If PARMS represents all template
8367 parameters levels, this function returns a vector of vectors
8368 representing all the resulting argument levels. Note that in this
8369 case, only the innermost arguments are coerced because the
8370 outermost ones are supposed to have been coerced already.
8372 Otherwise, if PARMS represents only (the innermost) vector of
8373 parameters, this function returns a vector containing just the
8374 innermost resulting arguments. */
8376 static tree
8377 coerce_innermost_template_parms (tree parms,
8378 tree args,
8379 tree in_decl,
8380 tsubst_flags_t complain,
8381 bool require_all_args,
8382 bool use_default_args)
8384 int parms_depth = TMPL_PARMS_DEPTH (parms);
8385 int args_depth = TMPL_ARGS_DEPTH (args);
8386 tree coerced_args;
8388 if (parms_depth > 1)
8390 coerced_args = make_tree_vec (parms_depth);
8391 tree level;
8392 int cur_depth;
8394 for (level = parms, cur_depth = parms_depth;
8395 parms_depth > 0 && level != NULL_TREE;
8396 level = TREE_CHAIN (level), --cur_depth)
8398 tree l;
8399 if (cur_depth == args_depth)
8400 l = coerce_template_parms (TREE_VALUE (level),
8401 args, in_decl, complain,
8402 require_all_args,
8403 use_default_args);
8404 else
8405 l = TMPL_ARGS_LEVEL (args, cur_depth);
8407 if (l == error_mark_node)
8408 return error_mark_node;
8410 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8413 else
8414 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8415 args, in_decl, complain,
8416 require_all_args,
8417 use_default_args);
8418 return coerced_args;
8421 /* Returns 1 if template args OT and NT are equivalent. */
8424 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8426 if (nt == ot)
8427 return 1;
8428 if (nt == NULL_TREE || ot == NULL_TREE)
8429 return false;
8430 if (nt == any_targ_node || ot == any_targ_node)
8431 return true;
8433 if (TREE_CODE (nt) == TREE_VEC)
8434 /* For member templates */
8435 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8436 else if (PACK_EXPANSION_P (ot))
8437 return (PACK_EXPANSION_P (nt)
8438 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8439 PACK_EXPANSION_PATTERN (nt))
8440 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8441 PACK_EXPANSION_EXTRA_ARGS (nt)));
8442 else if (ARGUMENT_PACK_P (ot))
8444 int i, len;
8445 tree opack, npack;
8447 if (!ARGUMENT_PACK_P (nt))
8448 return 0;
8450 opack = ARGUMENT_PACK_ARGS (ot);
8451 npack = ARGUMENT_PACK_ARGS (nt);
8452 len = TREE_VEC_LENGTH (opack);
8453 if (TREE_VEC_LENGTH (npack) != len)
8454 return 0;
8455 for (i = 0; i < len; ++i)
8456 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8457 TREE_VEC_ELT (npack, i)))
8458 return 0;
8459 return 1;
8461 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8462 gcc_unreachable ();
8463 else if (TYPE_P (nt))
8465 if (!TYPE_P (ot))
8466 return false;
8467 /* Don't treat an alias template specialization with dependent
8468 arguments as equivalent to its underlying type when used as a
8469 template argument; we need them to be distinct so that we
8470 substitute into the specialization arguments at instantiation
8471 time. And aliases can't be equivalent without being ==, so
8472 we don't need to look any deeper.
8474 During partial ordering, however, we need to treat them normally so
8475 that we can order uses of the same alias with different
8476 cv-qualification (79960). */
8477 if (!partial_order
8478 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8479 return false;
8480 else
8481 return same_type_p (ot, nt);
8483 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8484 return 0;
8485 else
8487 /* Try to treat a template non-type argument that has been converted
8488 to the parameter type as equivalent to one that hasn't yet. */
8489 for (enum tree_code code1 = TREE_CODE (ot);
8490 CONVERT_EXPR_CODE_P (code1)
8491 || code1 == NON_LVALUE_EXPR;
8492 code1 = TREE_CODE (ot))
8493 ot = TREE_OPERAND (ot, 0);
8494 for (enum tree_code code2 = TREE_CODE (nt);
8495 CONVERT_EXPR_CODE_P (code2)
8496 || code2 == NON_LVALUE_EXPR;
8497 code2 = TREE_CODE (nt))
8498 nt = TREE_OPERAND (nt, 0);
8500 return cp_tree_equal (ot, nt);
8504 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8505 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8506 NEWARG_PTR with the offending arguments if they are non-NULL. */
8509 comp_template_args (tree oldargs, tree newargs,
8510 tree *oldarg_ptr, tree *newarg_ptr,
8511 bool partial_order)
8513 int i;
8515 if (oldargs == newargs)
8516 return 1;
8518 if (!oldargs || !newargs)
8519 return 0;
8521 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8522 return 0;
8524 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8526 tree nt = TREE_VEC_ELT (newargs, i);
8527 tree ot = TREE_VEC_ELT (oldargs, i);
8529 if (! template_args_equal (ot, nt, partial_order))
8531 if (oldarg_ptr != NULL)
8532 *oldarg_ptr = ot;
8533 if (newarg_ptr != NULL)
8534 *newarg_ptr = nt;
8535 return 0;
8538 return 1;
8541 inline bool
8542 comp_template_args_porder (tree oargs, tree nargs)
8544 return comp_template_args (oargs, nargs, NULL, NULL, true);
8547 static void
8548 add_pending_template (tree d)
8550 tree ti = (TYPE_P (d)
8551 ? CLASSTYPE_TEMPLATE_INFO (d)
8552 : DECL_TEMPLATE_INFO (d));
8553 struct pending_template *pt;
8554 int level;
8556 if (TI_PENDING_TEMPLATE_FLAG (ti))
8557 return;
8559 /* We are called both from instantiate_decl, where we've already had a
8560 tinst_level pushed, and instantiate_template, where we haven't.
8561 Compensate. */
8562 level = !current_tinst_level || current_tinst_level->decl != d;
8564 if (level)
8565 push_tinst_level (d);
8567 pt = ggc_alloc<pending_template> ();
8568 pt->next = NULL;
8569 pt->tinst = current_tinst_level;
8570 if (last_pending_template)
8571 last_pending_template->next = pt;
8572 else
8573 pending_templates = pt;
8575 last_pending_template = pt;
8577 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8579 if (level)
8580 pop_tinst_level ();
8584 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8585 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8586 documentation for TEMPLATE_ID_EXPR. */
8588 tree
8589 lookup_template_function (tree fns, tree arglist)
8591 tree type;
8593 if (fns == error_mark_node || arglist == error_mark_node)
8594 return error_mark_node;
8596 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8598 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8600 error ("%q#D is not a function template", fns);
8601 return error_mark_node;
8604 if (BASELINK_P (fns))
8606 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8607 unknown_type_node,
8608 BASELINK_FUNCTIONS (fns),
8609 arglist);
8610 return fns;
8613 type = TREE_TYPE (fns);
8614 if (TREE_CODE (fns) == OVERLOAD || !type)
8615 type = unknown_type_node;
8617 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8620 /* Within the scope of a template class S<T>, the name S gets bound
8621 (in build_self_reference) to a TYPE_DECL for the class, not a
8622 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8623 or one of its enclosing classes, and that type is a template,
8624 return the associated TEMPLATE_DECL. Otherwise, the original
8625 DECL is returned.
8627 Also handle the case when DECL is a TREE_LIST of ambiguous
8628 injected-class-names from different bases. */
8630 tree
8631 maybe_get_template_decl_from_type_decl (tree decl)
8633 if (decl == NULL_TREE)
8634 return decl;
8636 /* DR 176: A lookup that finds an injected-class-name (10.2
8637 [class.member.lookup]) can result in an ambiguity in certain cases
8638 (for example, if it is found in more than one base class). If all of
8639 the injected-class-names that are found refer to specializations of
8640 the same class template, and if the name is followed by a
8641 template-argument-list, the reference refers to the class template
8642 itself and not a specialization thereof, and is not ambiguous. */
8643 if (TREE_CODE (decl) == TREE_LIST)
8645 tree t, tmpl = NULL_TREE;
8646 for (t = decl; t; t = TREE_CHAIN (t))
8648 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8649 if (!tmpl)
8650 tmpl = elt;
8651 else if (tmpl != elt)
8652 break;
8654 if (tmpl && t == NULL_TREE)
8655 return tmpl;
8656 else
8657 return decl;
8660 return (decl != NULL_TREE
8661 && DECL_SELF_REFERENCE_P (decl)
8662 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8663 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8666 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8667 parameters, find the desired type.
8669 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8671 IN_DECL, if non-NULL, is the template declaration we are trying to
8672 instantiate.
8674 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8675 the class we are looking up.
8677 Issue error and warning messages under control of COMPLAIN.
8679 If the template class is really a local class in a template
8680 function, then the FUNCTION_CONTEXT is the function in which it is
8681 being instantiated.
8683 ??? Note that this function is currently called *twice* for each
8684 template-id: the first time from the parser, while creating the
8685 incomplete type (finish_template_type), and the second type during the
8686 real instantiation (instantiate_template_class). This is surely something
8687 that we want to avoid. It also causes some problems with argument
8688 coercion (see convert_nontype_argument for more information on this). */
8690 static tree
8691 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8692 int entering_scope, tsubst_flags_t complain)
8694 tree templ = NULL_TREE, parmlist;
8695 tree t;
8696 spec_entry **slot;
8697 spec_entry *entry;
8698 spec_entry elt;
8699 hashval_t hash;
8701 if (identifier_p (d1))
8703 tree value = innermost_non_namespace_value (d1);
8704 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8705 templ = value;
8706 else
8708 if (context)
8709 push_decl_namespace (context);
8710 templ = lookup_name (d1);
8711 templ = maybe_get_template_decl_from_type_decl (templ);
8712 if (context)
8713 pop_decl_namespace ();
8715 if (templ)
8716 context = DECL_CONTEXT (templ);
8718 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8720 tree type = TREE_TYPE (d1);
8722 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8723 an implicit typename for the second A. Deal with it. */
8724 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8725 type = TREE_TYPE (type);
8727 if (CLASSTYPE_TEMPLATE_INFO (type))
8729 templ = CLASSTYPE_TI_TEMPLATE (type);
8730 d1 = DECL_NAME (templ);
8733 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8734 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8736 templ = TYPE_TI_TEMPLATE (d1);
8737 d1 = DECL_NAME (templ);
8739 else if (DECL_TYPE_TEMPLATE_P (d1))
8741 templ = d1;
8742 d1 = DECL_NAME (templ);
8743 context = DECL_CONTEXT (templ);
8745 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8747 templ = d1;
8748 d1 = DECL_NAME (templ);
8751 /* Issue an error message if we didn't find a template. */
8752 if (! templ)
8754 if (complain & tf_error)
8755 error ("%qT is not a template", d1);
8756 return error_mark_node;
8759 if (TREE_CODE (templ) != TEMPLATE_DECL
8760 /* Make sure it's a user visible template, if it was named by
8761 the user. */
8762 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8763 && !PRIMARY_TEMPLATE_P (templ)))
8765 if (complain & tf_error)
8767 error ("non-template type %qT used as a template", d1);
8768 if (in_decl)
8769 error ("for template declaration %q+D", in_decl);
8771 return error_mark_node;
8774 complain &= ~tf_user;
8776 /* An alias that just changes the name of a template is equivalent to the
8777 other template, so if any of the arguments are pack expansions, strip
8778 the alias to avoid problems with a pack expansion passed to a non-pack
8779 alias template parameter (DR 1430). */
8780 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8781 templ = get_underlying_template (templ);
8783 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8785 tree parm;
8786 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8787 if (arglist2 == error_mark_node
8788 || (!uses_template_parms (arglist2)
8789 && check_instantiated_args (templ, arglist2, complain)))
8790 return error_mark_node;
8792 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8793 return parm;
8795 else
8797 tree template_type = TREE_TYPE (templ);
8798 tree gen_tmpl;
8799 tree type_decl;
8800 tree found = NULL_TREE;
8801 int arg_depth;
8802 int parm_depth;
8803 int is_dependent_type;
8804 int use_partial_inst_tmpl = false;
8806 if (template_type == error_mark_node)
8807 /* An error occurred while building the template TEMPL, and a
8808 diagnostic has most certainly been emitted for that
8809 already. Let's propagate that error. */
8810 return error_mark_node;
8812 gen_tmpl = most_general_template (templ);
8813 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8814 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8815 arg_depth = TMPL_ARGS_DEPTH (arglist);
8817 if (arg_depth == 1 && parm_depth > 1)
8819 /* We've been given an incomplete set of template arguments.
8820 For example, given:
8822 template <class T> struct S1 {
8823 template <class U> struct S2 {};
8824 template <class U> struct S2<U*> {};
8827 we will be called with an ARGLIST of `U*', but the
8828 TEMPLATE will be `template <class T> template
8829 <class U> struct S1<T>::S2'. We must fill in the missing
8830 arguments. */
8831 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8832 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8833 arg_depth = TMPL_ARGS_DEPTH (arglist);
8836 /* Now we should have enough arguments. */
8837 gcc_assert (parm_depth == arg_depth);
8839 /* From here on, we're only interested in the most general
8840 template. */
8842 /* Calculate the BOUND_ARGS. These will be the args that are
8843 actually tsubst'd into the definition to create the
8844 instantiation. */
8845 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8846 complain,
8847 /*require_all_args=*/true,
8848 /*use_default_args=*/true);
8850 if (arglist == error_mark_node)
8851 /* We were unable to bind the arguments. */
8852 return error_mark_node;
8854 /* In the scope of a template class, explicit references to the
8855 template class refer to the type of the template, not any
8856 instantiation of it. For example, in:
8858 template <class T> class C { void f(C<T>); }
8860 the `C<T>' is just the same as `C'. Outside of the
8861 class, however, such a reference is an instantiation. */
8862 if (entering_scope
8863 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8864 || currently_open_class (template_type))
8866 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8868 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8869 return template_type;
8872 /* If we already have this specialization, return it. */
8873 elt.tmpl = gen_tmpl;
8874 elt.args = arglist;
8875 elt.spec = NULL_TREE;
8876 hash = spec_hasher::hash (&elt);
8877 entry = type_specializations->find_with_hash (&elt, hash);
8879 if (entry)
8880 return entry->spec;
8882 /* If the the template's constraints are not satisfied,
8883 then we cannot form a valid type.
8885 Note that the check is deferred until after the hash
8886 lookup. This prevents redundant checks on previously
8887 instantiated specializations. */
8888 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8890 if (complain & tf_error)
8892 error ("template constraint failure");
8893 diagnose_constraints (input_location, gen_tmpl, arglist);
8895 return error_mark_node;
8898 is_dependent_type = uses_template_parms (arglist);
8900 /* If the deduced arguments are invalid, then the binding
8901 failed. */
8902 if (!is_dependent_type
8903 && check_instantiated_args (gen_tmpl,
8904 INNERMOST_TEMPLATE_ARGS (arglist),
8905 complain))
8906 return error_mark_node;
8908 if (!is_dependent_type
8909 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8910 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8911 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8913 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8914 DECL_NAME (gen_tmpl),
8915 /*tag_scope=*/ts_global);
8916 return found;
8919 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8920 complain, in_decl);
8921 if (context == error_mark_node)
8922 return error_mark_node;
8924 if (!context)
8925 context = global_namespace;
8927 /* Create the type. */
8928 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8930 /* The user referred to a specialization of an alias
8931 template represented by GEN_TMPL.
8933 [temp.alias]/2 says:
8935 When a template-id refers to the specialization of an
8936 alias template, it is equivalent to the associated
8937 type obtained by substitution of its
8938 template-arguments for the template-parameters in the
8939 type-id of the alias template. */
8941 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8942 /* Note that the call above (by indirectly calling
8943 register_specialization in tsubst_decl) registers the
8944 TYPE_DECL representing the specialization of the alias
8945 template. So next time someone substitutes ARGLIST for
8946 the template parms into the alias template (GEN_TMPL),
8947 she'll get that TYPE_DECL back. */
8949 if (t == error_mark_node)
8950 return t;
8952 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8954 if (!is_dependent_type)
8956 set_current_access_from_decl (TYPE_NAME (template_type));
8957 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8958 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8959 arglist, complain, in_decl),
8960 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8961 arglist, complain, in_decl),
8962 SCOPED_ENUM_P (template_type), NULL);
8964 if (t == error_mark_node)
8965 return t;
8967 else
8969 /* We don't want to call start_enum for this type, since
8970 the values for the enumeration constants may involve
8971 template parameters. And, no one should be interested
8972 in the enumeration constants for such a type. */
8973 t = cxx_make_type (ENUMERAL_TYPE);
8974 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8976 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8977 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8978 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8980 else if (CLASS_TYPE_P (template_type))
8982 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8983 instantiated here. */
8984 gcc_assert (!LAMBDA_TYPE_P (template_type));
8986 t = make_class_type (TREE_CODE (template_type));
8987 CLASSTYPE_DECLARED_CLASS (t)
8988 = CLASSTYPE_DECLARED_CLASS (template_type);
8989 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8991 /* A local class. Make sure the decl gets registered properly. */
8992 if (context == current_function_decl)
8993 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8995 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8996 /* This instantiation is another name for the primary
8997 template type. Set the TYPE_CANONICAL field
8998 appropriately. */
8999 TYPE_CANONICAL (t) = template_type;
9000 else if (any_template_arguments_need_structural_equality_p (arglist))
9001 /* Some of the template arguments require structural
9002 equality testing, so this template class requires
9003 structural equality testing. */
9004 SET_TYPE_STRUCTURAL_EQUALITY (t);
9006 else
9007 gcc_unreachable ();
9009 /* If we called start_enum or pushtag above, this information
9010 will already be set up. */
9011 if (!TYPE_NAME (t))
9013 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9015 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9016 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9017 DECL_SOURCE_LOCATION (type_decl)
9018 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9020 else
9021 type_decl = TYPE_NAME (t);
9023 if (CLASS_TYPE_P (template_type))
9025 TREE_PRIVATE (type_decl)
9026 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9027 TREE_PROTECTED (type_decl)
9028 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9029 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9031 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9032 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9036 if (OVERLOAD_TYPE_P (t)
9037 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9039 static const char *tags[] = {"abi_tag", "may_alias"};
9041 for (unsigned ix = 0; ix != 2; ix++)
9043 tree attributes
9044 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9046 if (attributes)
9047 TYPE_ATTRIBUTES (t)
9048 = tree_cons (TREE_PURPOSE (attributes),
9049 TREE_VALUE (attributes),
9050 TYPE_ATTRIBUTES (t));
9054 /* Let's consider the explicit specialization of a member
9055 of a class template specialization that is implicitly instantiated,
9056 e.g.:
9057 template<class T>
9058 struct S
9060 template<class U> struct M {}; //#0
9063 template<>
9064 template<>
9065 struct S<int>::M<char> //#1
9067 int i;
9069 [temp.expl.spec]/4 says this is valid.
9071 In this case, when we write:
9072 S<int>::M<char> m;
9074 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9075 the one of #0.
9077 When we encounter #1, we want to store the partial instantiation
9078 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9080 For all cases other than this "explicit specialization of member of a
9081 class template", we just want to store the most general template into
9082 the CLASSTYPE_TI_TEMPLATE of M.
9084 This case of "explicit specialization of member of a class template"
9085 only happens when:
9086 1/ the enclosing class is an instantiation of, and therefore not
9087 the same as, the context of the most general template, and
9088 2/ we aren't looking at the partial instantiation itself, i.e.
9089 the innermost arguments are not the same as the innermost parms of
9090 the most general template.
9092 So it's only when 1/ and 2/ happens that we want to use the partial
9093 instantiation of the member template in lieu of its most general
9094 template. */
9096 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9097 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9098 /* the enclosing class must be an instantiation... */
9099 && CLASS_TYPE_P (context)
9100 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9102 TREE_VEC_LENGTH (arglist)--;
9103 ++processing_template_decl;
9104 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9105 tree partial_inst_args =
9106 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9107 arglist, complain, NULL_TREE);
9108 --processing_template_decl;
9109 TREE_VEC_LENGTH (arglist)++;
9110 if (partial_inst_args == error_mark_node)
9111 return error_mark_node;
9112 use_partial_inst_tmpl =
9113 /*...and we must not be looking at the partial instantiation
9114 itself. */
9115 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9116 partial_inst_args);
9119 if (!use_partial_inst_tmpl)
9120 /* This case is easy; there are no member templates involved. */
9121 found = gen_tmpl;
9122 else
9124 /* This is a full instantiation of a member template. Find
9125 the partial instantiation of which this is an instance. */
9127 /* Temporarily reduce by one the number of levels in the ARGLIST
9128 so as to avoid comparing the last set of arguments. */
9129 TREE_VEC_LENGTH (arglist)--;
9130 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9131 TREE_VEC_LENGTH (arglist)++;
9132 /* FOUND is either a proper class type, or an alias
9133 template specialization. In the later case, it's a
9134 TYPE_DECL, resulting from the substituting of arguments
9135 for parameters in the TYPE_DECL of the alias template
9136 done earlier. So be careful while getting the template
9137 of FOUND. */
9138 found = (TREE_CODE (found) == TEMPLATE_DECL
9139 ? found
9140 : (TREE_CODE (found) == TYPE_DECL
9141 ? DECL_TI_TEMPLATE (found)
9142 : CLASSTYPE_TI_TEMPLATE (found)));
9145 // Build template info for the new specialization.
9146 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9148 elt.spec = t;
9149 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9150 entry = ggc_alloc<spec_entry> ();
9151 *entry = elt;
9152 *slot = entry;
9154 /* Note this use of the partial instantiation so we can check it
9155 later in maybe_process_partial_specialization. */
9156 DECL_TEMPLATE_INSTANTIATIONS (found)
9157 = tree_cons (arglist, t,
9158 DECL_TEMPLATE_INSTANTIATIONS (found));
9160 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9161 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9162 /* Now that the type has been registered on the instantiations
9163 list, we set up the enumerators. Because the enumeration
9164 constants may involve the enumeration type itself, we make
9165 sure to register the type first, and then create the
9166 constants. That way, doing tsubst_expr for the enumeration
9167 constants won't result in recursive calls here; we'll find
9168 the instantiation and exit above. */
9169 tsubst_enum (template_type, t, arglist);
9171 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9172 /* If the type makes use of template parameters, the
9173 code that generates debugging information will crash. */
9174 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9176 /* Possibly limit visibility based on template args. */
9177 TREE_PUBLIC (type_decl) = 1;
9178 determine_visibility (type_decl);
9180 inherit_targ_abi_tags (t);
9182 return t;
9186 /* Wrapper for lookup_template_class_1. */
9188 tree
9189 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9190 int entering_scope, tsubst_flags_t complain)
9192 tree ret;
9193 timevar_push (TV_TEMPLATE_INST);
9194 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9195 entering_scope, complain);
9196 timevar_pop (TV_TEMPLATE_INST);
9197 return ret;
9200 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9202 tree
9203 lookup_template_variable (tree templ, tree arglist)
9205 /* The type of the expression is NULL_TREE since the template-id could refer
9206 to an explicit or partial specialization. */
9207 tree type = NULL_TREE;
9208 if (flag_concepts && variable_concept_p (templ))
9209 /* Except that concepts are always bool. */
9210 type = boolean_type_node;
9211 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9214 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9216 tree
9217 finish_template_variable (tree var, tsubst_flags_t complain)
9219 tree templ = TREE_OPERAND (var, 0);
9220 tree arglist = TREE_OPERAND (var, 1);
9222 /* We never want to return a VAR_DECL for a variable concept, since they
9223 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9224 bool concept_p = flag_concepts && variable_concept_p (templ);
9225 if (concept_p && processing_template_decl)
9226 return var;
9228 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9229 arglist = add_outermost_template_args (tmpl_args, arglist);
9231 templ = most_general_template (templ);
9232 tree parms = DECL_TEMPLATE_PARMS (templ);
9233 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9234 /*req_all*/true,
9235 /*use_default*/true);
9237 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9239 if (complain & tf_error)
9241 error ("use of invalid variable template %qE", var);
9242 diagnose_constraints (location_of (var), templ, arglist);
9244 return error_mark_node;
9247 /* If a template-id refers to a specialization of a variable
9248 concept, then the expression is true if and only if the
9249 concept's constraints are satisfied by the given template
9250 arguments.
9252 NOTE: This is an extension of Concepts Lite TS that
9253 allows constraints to be used in expressions. */
9254 if (concept_p)
9256 tree decl = DECL_TEMPLATE_RESULT (templ);
9257 return evaluate_variable_concept (decl, arglist);
9260 return instantiate_template (templ, arglist, complain);
9263 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9264 TARGS template args, and instantiate it if it's not dependent. */
9266 tree
9267 lookup_and_finish_template_variable (tree templ, tree targs,
9268 tsubst_flags_t complain)
9270 templ = lookup_template_variable (templ, targs);
9271 if (!any_dependent_template_arguments_p (targs))
9273 templ = finish_template_variable (templ, complain);
9274 mark_used (templ);
9277 return convert_from_reference (templ);
9281 struct pair_fn_data
9283 tree_fn_t fn;
9284 tree_fn_t any_fn;
9285 void *data;
9286 /* True when we should also visit template parameters that occur in
9287 non-deduced contexts. */
9288 bool include_nondeduced_p;
9289 hash_set<tree> *visited;
9292 /* Called from for_each_template_parm via walk_tree. */
9294 static tree
9295 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9297 tree t = *tp;
9298 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9299 tree_fn_t fn = pfd->fn;
9300 void *data = pfd->data;
9301 tree result = NULL_TREE;
9303 #define WALK_SUBTREE(NODE) \
9304 do \
9306 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9307 pfd->include_nondeduced_p, \
9308 pfd->any_fn); \
9309 if (result) goto out; \
9311 while (0)
9313 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9314 return t;
9316 if (TYPE_P (t)
9317 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9318 WALK_SUBTREE (TYPE_CONTEXT (t));
9320 switch (TREE_CODE (t))
9322 case RECORD_TYPE:
9323 if (TYPE_PTRMEMFUNC_P (t))
9324 break;
9325 /* Fall through. */
9327 case UNION_TYPE:
9328 case ENUMERAL_TYPE:
9329 if (!TYPE_TEMPLATE_INFO (t))
9330 *walk_subtrees = 0;
9331 else
9332 WALK_SUBTREE (TYPE_TI_ARGS (t));
9333 break;
9335 case INTEGER_TYPE:
9336 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9337 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9338 break;
9340 case METHOD_TYPE:
9341 /* Since we're not going to walk subtrees, we have to do this
9342 explicitly here. */
9343 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9344 /* Fall through. */
9346 case FUNCTION_TYPE:
9347 /* Check the return type. */
9348 WALK_SUBTREE (TREE_TYPE (t));
9350 /* Check the parameter types. Since default arguments are not
9351 instantiated until they are needed, the TYPE_ARG_TYPES may
9352 contain expressions that involve template parameters. But,
9353 no-one should be looking at them yet. And, once they're
9354 instantiated, they don't contain template parameters, so
9355 there's no point in looking at them then, either. */
9357 tree parm;
9359 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9360 WALK_SUBTREE (TREE_VALUE (parm));
9362 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9363 want walk_tree walking into them itself. */
9364 *walk_subtrees = 0;
9367 if (flag_noexcept_type)
9369 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9370 if (spec)
9371 WALK_SUBTREE (TREE_PURPOSE (spec));
9373 break;
9375 case TYPEOF_TYPE:
9376 case UNDERLYING_TYPE:
9377 if (pfd->include_nondeduced_p
9378 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9379 pfd->visited,
9380 pfd->include_nondeduced_p,
9381 pfd->any_fn))
9382 return error_mark_node;
9383 break;
9385 case FUNCTION_DECL:
9386 case VAR_DECL:
9387 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9388 WALK_SUBTREE (DECL_TI_ARGS (t));
9389 /* Fall through. */
9391 case PARM_DECL:
9392 case CONST_DECL:
9393 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9394 WALK_SUBTREE (DECL_INITIAL (t));
9395 if (DECL_CONTEXT (t)
9396 && pfd->include_nondeduced_p)
9397 WALK_SUBTREE (DECL_CONTEXT (t));
9398 break;
9400 case BOUND_TEMPLATE_TEMPLATE_PARM:
9401 /* Record template parameters such as `T' inside `TT<T>'. */
9402 WALK_SUBTREE (TYPE_TI_ARGS (t));
9403 /* Fall through. */
9405 case TEMPLATE_TEMPLATE_PARM:
9406 case TEMPLATE_TYPE_PARM:
9407 case TEMPLATE_PARM_INDEX:
9408 if (fn && (*fn)(t, data))
9409 return t;
9410 else if (!fn)
9411 return t;
9412 break;
9414 case TEMPLATE_DECL:
9415 /* A template template parameter is encountered. */
9416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9417 WALK_SUBTREE (TREE_TYPE (t));
9419 /* Already substituted template template parameter */
9420 *walk_subtrees = 0;
9421 break;
9423 case TYPENAME_TYPE:
9424 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9425 partial instantiation. */
9426 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9427 break;
9429 case CONSTRUCTOR:
9430 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9431 && pfd->include_nondeduced_p)
9432 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9433 break;
9435 case INDIRECT_REF:
9436 case COMPONENT_REF:
9437 /* If there's no type, then this thing must be some expression
9438 involving template parameters. */
9439 if (!fn && !TREE_TYPE (t))
9440 return error_mark_node;
9441 break;
9443 case MODOP_EXPR:
9444 case CAST_EXPR:
9445 case IMPLICIT_CONV_EXPR:
9446 case REINTERPRET_CAST_EXPR:
9447 case CONST_CAST_EXPR:
9448 case STATIC_CAST_EXPR:
9449 case DYNAMIC_CAST_EXPR:
9450 case ARROW_EXPR:
9451 case DOTSTAR_EXPR:
9452 case TYPEID_EXPR:
9453 case PSEUDO_DTOR_EXPR:
9454 if (!fn)
9455 return error_mark_node;
9456 break;
9458 default:
9459 break;
9462 #undef WALK_SUBTREE
9464 /* We didn't find any template parameters we liked. */
9465 out:
9466 return result;
9469 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9470 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9471 call FN with the parameter and the DATA.
9472 If FN returns nonzero, the iteration is terminated, and
9473 for_each_template_parm returns 1. Otherwise, the iteration
9474 continues. If FN never returns a nonzero value, the value
9475 returned by for_each_template_parm is 0. If FN is NULL, it is
9476 considered to be the function which always returns 1.
9478 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9479 parameters that occur in non-deduced contexts. When false, only
9480 visits those template parameters that can be deduced. */
9482 static tree
9483 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9484 hash_set<tree> *visited,
9485 bool include_nondeduced_p,
9486 tree_fn_t any_fn)
9488 struct pair_fn_data pfd;
9489 tree result;
9491 /* Set up. */
9492 pfd.fn = fn;
9493 pfd.any_fn = any_fn;
9494 pfd.data = data;
9495 pfd.include_nondeduced_p = include_nondeduced_p;
9497 /* Walk the tree. (Conceptually, we would like to walk without
9498 duplicates, but for_each_template_parm_r recursively calls
9499 for_each_template_parm, so we would need to reorganize a fair
9500 bit to use walk_tree_without_duplicates, so we keep our own
9501 visited list.) */
9502 if (visited)
9503 pfd.visited = visited;
9504 else
9505 pfd.visited = new hash_set<tree>;
9506 result = cp_walk_tree (&t,
9507 for_each_template_parm_r,
9508 &pfd,
9509 pfd.visited);
9511 /* Clean up. */
9512 if (!visited)
9514 delete pfd.visited;
9515 pfd.visited = 0;
9518 return result;
9521 /* Returns true if T depends on any template parameter. */
9524 uses_template_parms (tree t)
9526 if (t == NULL_TREE)
9527 return false;
9529 bool dependent_p;
9530 int saved_processing_template_decl;
9532 saved_processing_template_decl = processing_template_decl;
9533 if (!saved_processing_template_decl)
9534 processing_template_decl = 1;
9535 if (TYPE_P (t))
9536 dependent_p = dependent_type_p (t);
9537 else if (TREE_CODE (t) == TREE_VEC)
9538 dependent_p = any_dependent_template_arguments_p (t);
9539 else if (TREE_CODE (t) == TREE_LIST)
9540 dependent_p = (uses_template_parms (TREE_VALUE (t))
9541 || uses_template_parms (TREE_CHAIN (t)));
9542 else if (TREE_CODE (t) == TYPE_DECL)
9543 dependent_p = dependent_type_p (TREE_TYPE (t));
9544 else if (DECL_P (t)
9545 || EXPR_P (t)
9546 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9547 || TREE_CODE (t) == OVERLOAD
9548 || BASELINK_P (t)
9549 || identifier_p (t)
9550 || TREE_CODE (t) == TRAIT_EXPR
9551 || TREE_CODE (t) == CONSTRUCTOR
9552 || CONSTANT_CLASS_P (t))
9553 dependent_p = (type_dependent_expression_p (t)
9554 || value_dependent_expression_p (t));
9555 else
9557 gcc_assert (t == error_mark_node);
9558 dependent_p = false;
9561 processing_template_decl = saved_processing_template_decl;
9563 return dependent_p;
9566 /* Returns true iff current_function_decl is an incompletely instantiated
9567 template. Useful instead of processing_template_decl because the latter
9568 is set to 0 during instantiate_non_dependent_expr. */
9570 bool
9571 in_template_function (void)
9573 tree fn = current_function_decl;
9574 bool ret;
9575 ++processing_template_decl;
9576 ret = (fn && DECL_LANG_SPECIFIC (fn)
9577 && DECL_TEMPLATE_INFO (fn)
9578 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9579 --processing_template_decl;
9580 return ret;
9583 /* Returns true if T depends on any template parameter with level LEVEL. */
9585 bool
9586 uses_template_parms_level (tree t, int level)
9588 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9589 /*include_nondeduced_p=*/true);
9592 /* Returns true if the signature of DECL depends on any template parameter from
9593 its enclosing class. */
9595 bool
9596 uses_outer_template_parms (tree decl)
9598 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9599 if (depth == 0)
9600 return false;
9601 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9602 &depth, NULL, /*include_nondeduced_p=*/true))
9603 return true;
9604 if (PRIMARY_TEMPLATE_P (decl)
9605 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9606 (DECL_TEMPLATE_PARMS (decl)),
9607 template_parm_outer_level,
9608 &depth, NULL, /*include_nondeduced_p=*/true))
9609 return true;
9610 tree ci = get_constraints (decl);
9611 if (ci)
9612 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9613 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9614 &depth, NULL, /*nondeduced*/true))
9615 return true;
9616 return false;
9619 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9620 ill-formed translation unit, i.e. a variable or function that isn't
9621 usable in a constant expression. */
9623 static inline bool
9624 neglectable_inst_p (tree d)
9626 return (DECL_P (d)
9627 && !undeduced_auto_decl (d)
9628 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9629 : decl_maybe_constant_var_p (d)));
9632 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9633 neglectable and instantiated from within an erroneous instantiation. */
9635 static bool
9636 limit_bad_template_recursion (tree decl)
9638 struct tinst_level *lev = current_tinst_level;
9639 int errs = errorcount + sorrycount;
9640 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9641 return false;
9643 for (; lev; lev = lev->next)
9644 if (neglectable_inst_p (lev->decl))
9645 break;
9647 return (lev && errs > lev->errors);
9650 static int tinst_depth;
9651 extern int max_tinst_depth;
9652 int depth_reached;
9654 static GTY(()) struct tinst_level *last_error_tinst_level;
9656 /* We're starting to instantiate D; record the template instantiation context
9657 for diagnostics and to restore it later. */
9659 bool
9660 push_tinst_level (tree d)
9662 return push_tinst_level_loc (d, input_location);
9665 /* We're starting to instantiate D; record the template instantiation context
9666 at LOC for diagnostics and to restore it later. */
9668 bool
9669 push_tinst_level_loc (tree d, location_t loc)
9671 struct tinst_level *new_level;
9673 if (tinst_depth >= max_tinst_depth)
9675 /* Tell error.c not to try to instantiate any templates. */
9676 at_eof = 2;
9677 fatal_error (input_location,
9678 "template instantiation depth exceeds maximum of %d"
9679 " (use -ftemplate-depth= to increase the maximum)",
9680 max_tinst_depth);
9681 return false;
9684 /* If the current instantiation caused problems, don't let it instantiate
9685 anything else. Do allow deduction substitution and decls usable in
9686 constant expressions. */
9687 if (limit_bad_template_recursion (d))
9688 return false;
9690 /* When not -quiet, dump template instantiations other than functions, since
9691 announce_function will take care of those. */
9692 if (!quiet_flag
9693 && TREE_CODE (d) != TREE_LIST
9694 && TREE_CODE (d) != FUNCTION_DECL)
9695 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9697 new_level = ggc_alloc<tinst_level> ();
9698 new_level->decl = d;
9699 new_level->locus = loc;
9700 new_level->errors = errorcount+sorrycount;
9701 new_level->in_system_header_p = in_system_header_at (input_location);
9702 new_level->next = current_tinst_level;
9703 current_tinst_level = new_level;
9705 ++tinst_depth;
9706 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9707 depth_reached = tinst_depth;
9709 return true;
9712 /* We're done instantiating this template; return to the instantiation
9713 context. */
9715 void
9716 pop_tinst_level (void)
9718 /* Restore the filename and line number stashed away when we started
9719 this instantiation. */
9720 input_location = current_tinst_level->locus;
9721 current_tinst_level = current_tinst_level->next;
9722 --tinst_depth;
9725 /* We're instantiating a deferred template; restore the template
9726 instantiation context in which the instantiation was requested, which
9727 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9729 static tree
9730 reopen_tinst_level (struct tinst_level *level)
9732 struct tinst_level *t;
9734 tinst_depth = 0;
9735 for (t = level; t; t = t->next)
9736 ++tinst_depth;
9738 current_tinst_level = level;
9739 pop_tinst_level ();
9740 if (current_tinst_level)
9741 current_tinst_level->errors = errorcount+sorrycount;
9742 return level->decl;
9745 /* Returns the TINST_LEVEL which gives the original instantiation
9746 context. */
9748 struct tinst_level *
9749 outermost_tinst_level (void)
9751 struct tinst_level *level = current_tinst_level;
9752 if (level)
9753 while (level->next)
9754 level = level->next;
9755 return level;
9758 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9759 vector of template arguments, as for tsubst.
9761 Returns an appropriate tsubst'd friend declaration. */
9763 static tree
9764 tsubst_friend_function (tree decl, tree args)
9766 tree new_friend;
9768 if (TREE_CODE (decl) == FUNCTION_DECL
9769 && DECL_TEMPLATE_INSTANTIATION (decl)
9770 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9771 /* This was a friend declared with an explicit template
9772 argument list, e.g.:
9774 friend void f<>(T);
9776 to indicate that f was a template instantiation, not a new
9777 function declaration. Now, we have to figure out what
9778 instantiation of what template. */
9780 tree template_id, arglist, fns;
9781 tree new_args;
9782 tree tmpl;
9783 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9785 /* Friend functions are looked up in the containing namespace scope.
9786 We must enter that scope, to avoid finding member functions of the
9787 current class with same name. */
9788 push_nested_namespace (ns);
9789 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9790 tf_warning_or_error, NULL_TREE,
9791 /*integral_constant_expression_p=*/false);
9792 pop_nested_namespace (ns);
9793 arglist = tsubst (DECL_TI_ARGS (decl), args,
9794 tf_warning_or_error, NULL_TREE);
9795 template_id = lookup_template_function (fns, arglist);
9797 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9798 tmpl = determine_specialization (template_id, new_friend,
9799 &new_args,
9800 /*need_member_template=*/0,
9801 TREE_VEC_LENGTH (args),
9802 tsk_none);
9803 return instantiate_template (tmpl, new_args, tf_error);
9806 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9808 /* The NEW_FRIEND will look like an instantiation, to the
9809 compiler, but is not an instantiation from the point of view of
9810 the language. For example, we might have had:
9812 template <class T> struct S {
9813 template <class U> friend void f(T, U);
9816 Then, in S<int>, template <class U> void f(int, U) is not an
9817 instantiation of anything. */
9818 if (new_friend == error_mark_node)
9819 return error_mark_node;
9821 DECL_USE_TEMPLATE (new_friend) = 0;
9822 if (TREE_CODE (decl) == TEMPLATE_DECL)
9824 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9825 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9826 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9829 /* The mangled name for the NEW_FRIEND is incorrect. The function
9830 is not a template instantiation and should not be mangled like
9831 one. Therefore, we forget the mangling here; we'll recompute it
9832 later if we need it. */
9833 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9835 SET_DECL_RTL (new_friend, NULL);
9836 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9839 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9841 tree old_decl;
9842 tree new_friend_template_info;
9843 tree new_friend_result_template_info;
9844 tree ns;
9845 int new_friend_is_defn;
9847 /* We must save some information from NEW_FRIEND before calling
9848 duplicate decls since that function will free NEW_FRIEND if
9849 possible. */
9850 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9851 new_friend_is_defn =
9852 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9853 (template_for_substitution (new_friend)))
9854 != NULL_TREE);
9855 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9857 /* This declaration is a `primary' template. */
9858 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9860 new_friend_result_template_info
9861 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9863 else
9864 new_friend_result_template_info = NULL_TREE;
9866 /* Inside pushdecl_namespace_level, we will push into the
9867 current namespace. However, the friend function should go
9868 into the namespace of the template. */
9869 ns = decl_namespace_context (new_friend);
9870 push_nested_namespace (ns);
9871 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9872 pop_nested_namespace (ns);
9874 if (old_decl == error_mark_node)
9875 return error_mark_node;
9877 if (old_decl != new_friend)
9879 /* This new friend declaration matched an existing
9880 declaration. For example, given:
9882 template <class T> void f(T);
9883 template <class U> class C {
9884 template <class T> friend void f(T) {}
9887 the friend declaration actually provides the definition
9888 of `f', once C has been instantiated for some type. So,
9889 old_decl will be the out-of-class template declaration,
9890 while new_friend is the in-class definition.
9892 But, if `f' was called before this point, the
9893 instantiation of `f' will have DECL_TI_ARGS corresponding
9894 to `T' but not to `U', references to which might appear
9895 in the definition of `f'. Previously, the most general
9896 template for an instantiation of `f' was the out-of-class
9897 version; now it is the in-class version. Therefore, we
9898 run through all specialization of `f', adding to their
9899 DECL_TI_ARGS appropriately. In particular, they need a
9900 new set of outer arguments, corresponding to the
9901 arguments for this class instantiation.
9903 The same situation can arise with something like this:
9905 friend void f(int);
9906 template <class T> class C {
9907 friend void f(T) {}
9910 when `C<int>' is instantiated. Now, `f(int)' is defined
9911 in the class. */
9913 if (!new_friend_is_defn)
9914 /* On the other hand, if the in-class declaration does
9915 *not* provide a definition, then we don't want to alter
9916 existing definitions. We can just leave everything
9917 alone. */
9919 else
9921 tree new_template = TI_TEMPLATE (new_friend_template_info);
9922 tree new_args = TI_ARGS (new_friend_template_info);
9924 /* Overwrite whatever template info was there before, if
9925 any, with the new template information pertaining to
9926 the declaration. */
9927 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9929 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9931 /* We should have called reregister_specialization in
9932 duplicate_decls. */
9933 gcc_assert (retrieve_specialization (new_template,
9934 new_args, 0)
9935 == old_decl);
9937 /* Instantiate it if the global has already been used. */
9938 if (DECL_ODR_USED (old_decl))
9939 instantiate_decl (old_decl, /*defer_ok=*/true,
9940 /*expl_inst_class_mem_p=*/false);
9942 else
9944 tree t;
9946 /* Indicate that the old function template is a partial
9947 instantiation. */
9948 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9949 = new_friend_result_template_info;
9951 gcc_assert (new_template
9952 == most_general_template (new_template));
9953 gcc_assert (new_template != old_decl);
9955 /* Reassign any specializations already in the hash table
9956 to the new more general template, and add the
9957 additional template args. */
9958 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9959 t != NULL_TREE;
9960 t = TREE_CHAIN (t))
9962 tree spec = TREE_VALUE (t);
9963 spec_entry elt;
9965 elt.tmpl = old_decl;
9966 elt.args = DECL_TI_ARGS (spec);
9967 elt.spec = NULL_TREE;
9969 decl_specializations->remove_elt (&elt);
9971 DECL_TI_ARGS (spec)
9972 = add_outermost_template_args (new_args,
9973 DECL_TI_ARGS (spec));
9975 register_specialization
9976 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9979 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9983 /* The information from NEW_FRIEND has been merged into OLD_DECL
9984 by duplicate_decls. */
9985 new_friend = old_decl;
9988 else
9990 tree context = DECL_CONTEXT (new_friend);
9991 bool dependent_p;
9993 /* In the code
9994 template <class T> class C {
9995 template <class U> friend void C1<U>::f (); // case 1
9996 friend void C2<T>::f (); // case 2
9998 we only need to make sure CONTEXT is a complete type for
9999 case 2. To distinguish between the two cases, we note that
10000 CONTEXT of case 1 remains dependent type after tsubst while
10001 this isn't true for case 2. */
10002 ++processing_template_decl;
10003 dependent_p = dependent_type_p (context);
10004 --processing_template_decl;
10006 if (!dependent_p
10007 && !complete_type_or_else (context, NULL_TREE))
10008 return error_mark_node;
10010 if (COMPLETE_TYPE_P (context))
10012 tree fn = new_friend;
10013 /* do_friend adds the TEMPLATE_DECL for any member friend
10014 template even if it isn't a member template, i.e.
10015 template <class T> friend A<T>::f();
10016 Look through it in that case. */
10017 if (TREE_CODE (fn) == TEMPLATE_DECL
10018 && !PRIMARY_TEMPLATE_P (fn))
10019 fn = DECL_TEMPLATE_RESULT (fn);
10020 /* Check to see that the declaration is really present, and,
10021 possibly obtain an improved declaration. */
10022 fn = check_classfn (context, fn, NULL_TREE);
10024 if (fn)
10025 new_friend = fn;
10029 return new_friend;
10032 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10033 template arguments, as for tsubst.
10035 Returns an appropriate tsubst'd friend type or error_mark_node on
10036 failure. */
10038 static tree
10039 tsubst_friend_class (tree friend_tmpl, tree args)
10041 tree tmpl;
10043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10045 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10046 return TREE_TYPE (tmpl);
10049 tree context = CP_DECL_CONTEXT (friend_tmpl);
10050 if (TREE_CODE (context) == NAMESPACE_DECL)
10051 push_nested_namespace (context);
10052 else
10053 push_nested_class (context);
10055 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10056 /*non_class=*/false, /*block_p=*/false,
10057 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10059 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10061 /* The friend template has already been declared. Just
10062 check to see that the declarations match, and install any new
10063 default parameters. We must tsubst the default parameters,
10064 of course. We only need the innermost template parameters
10065 because that is all that redeclare_class_template will look
10066 at. */
10067 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10068 > TMPL_ARGS_DEPTH (args))
10070 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10071 args, tf_warning_or_error);
10072 location_t saved_input_location = input_location;
10073 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10074 tree cons = get_constraints (tmpl);
10075 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10076 input_location = saved_input_location;
10079 else
10081 /* The friend template has not already been declared. In this
10082 case, the instantiation of the template class will cause the
10083 injection of this template into the namespace scope. */
10084 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10086 if (tmpl != error_mark_node)
10088 /* The new TMPL is not an instantiation of anything, so we
10089 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10090 for the new type because that is supposed to be the
10091 corresponding template decl, i.e., TMPL. */
10092 DECL_USE_TEMPLATE (tmpl) = 0;
10093 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10094 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10095 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10096 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10098 /* It is hidden. */
10099 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10100 DECL_ANTICIPATED (tmpl)
10101 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10103 /* Inject this template into the enclosing namspace scope. */
10104 tmpl = pushdecl_namespace_level (tmpl, true);
10108 if (TREE_CODE (context) == NAMESPACE_DECL)
10109 pop_nested_namespace (context);
10110 else
10111 pop_nested_class ();
10113 return TREE_TYPE (tmpl);
10116 /* Returns zero if TYPE cannot be completed later due to circularity.
10117 Otherwise returns one. */
10119 static int
10120 can_complete_type_without_circularity (tree type)
10122 if (type == NULL_TREE || type == error_mark_node)
10123 return 0;
10124 else if (COMPLETE_TYPE_P (type))
10125 return 1;
10126 else if (TREE_CODE (type) == ARRAY_TYPE)
10127 return can_complete_type_without_circularity (TREE_TYPE (type));
10128 else if (CLASS_TYPE_P (type)
10129 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10130 return 0;
10131 else
10132 return 1;
10135 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10136 tsubst_flags_t, tree);
10138 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10139 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10141 static tree
10142 tsubst_attribute (tree t, tree *decl_p, tree args,
10143 tsubst_flags_t complain, tree in_decl)
10145 gcc_assert (ATTR_IS_DEPENDENT (t));
10147 tree val = TREE_VALUE (t);
10148 if (val == NULL_TREE)
10149 /* Nothing to do. */;
10150 else if ((flag_openmp || flag_openmp_simd)
10151 && is_attribute_p ("omp declare simd",
10152 get_attribute_name (t)))
10154 tree clauses = TREE_VALUE (val);
10155 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10156 complain, in_decl);
10157 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10158 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10159 tree parms = DECL_ARGUMENTS (*decl_p);
10160 clauses
10161 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10162 if (clauses)
10163 val = build_tree_list (NULL_TREE, clauses);
10164 else
10165 val = NULL_TREE;
10167 /* If the first attribute argument is an identifier, don't
10168 pass it through tsubst. Attributes like mode, format,
10169 cleanup and several target specific attributes expect it
10170 unmodified. */
10171 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10173 tree chain
10174 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10175 /*integral_constant_expression_p=*/false);
10176 if (chain != TREE_CHAIN (val))
10177 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10179 else if (PACK_EXPANSION_P (val))
10181 /* An attribute pack expansion. */
10182 tree purp = TREE_PURPOSE (t);
10183 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10184 if (pack == error_mark_node)
10185 return error_mark_node;
10186 int len = TREE_VEC_LENGTH (pack);
10187 tree list = NULL_TREE;
10188 tree *q = &list;
10189 for (int i = 0; i < len; ++i)
10191 tree elt = TREE_VEC_ELT (pack, i);
10192 *q = build_tree_list (purp, elt);
10193 q = &TREE_CHAIN (*q);
10195 return list;
10197 else
10198 val = tsubst_expr (val, args, complain, in_decl,
10199 /*integral_constant_expression_p=*/false);
10201 if (val != TREE_VALUE (t))
10202 return build_tree_list (TREE_PURPOSE (t), val);
10203 return t;
10206 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10207 unchanged or a new TREE_LIST chain. */
10209 static tree
10210 tsubst_attributes (tree attributes, tree args,
10211 tsubst_flags_t complain, tree in_decl)
10213 tree last_dep = NULL_TREE;
10215 for (tree t = attributes; t; t = TREE_CHAIN (t))
10216 if (ATTR_IS_DEPENDENT (t))
10218 last_dep = t;
10219 attributes = copy_list (attributes);
10220 break;
10223 if (last_dep)
10224 for (tree *p = &attributes; *p; )
10226 tree t = *p;
10227 if (ATTR_IS_DEPENDENT (t))
10229 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10230 if (subst != t)
10232 *p = subst;
10234 p = &TREE_CHAIN (*p);
10235 while (*p);
10236 *p = TREE_CHAIN (t);
10237 continue;
10240 p = &TREE_CHAIN (*p);
10243 return attributes;
10246 /* Apply any attributes which had to be deferred until instantiation
10247 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10248 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10250 static void
10251 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10252 tree args, tsubst_flags_t complain, tree in_decl)
10254 tree last_dep = NULL_TREE;
10255 tree t;
10256 tree *p;
10258 if (attributes == NULL_TREE)
10259 return;
10261 if (DECL_P (*decl_p))
10263 if (TREE_TYPE (*decl_p) == error_mark_node)
10264 return;
10265 p = &DECL_ATTRIBUTES (*decl_p);
10266 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10267 to our attributes parameter. */
10268 gcc_assert (*p == attributes);
10270 else
10272 p = &TYPE_ATTRIBUTES (*decl_p);
10273 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10274 lookup_template_class_1, and should be preserved. */
10275 gcc_assert (*p != attributes);
10276 while (*p)
10277 p = &TREE_CHAIN (*p);
10280 for (t = attributes; t; t = TREE_CHAIN (t))
10281 if (ATTR_IS_DEPENDENT (t))
10283 last_dep = t;
10284 attributes = copy_list (attributes);
10285 break;
10288 *p = attributes;
10289 if (last_dep)
10291 tree late_attrs = NULL_TREE;
10292 tree *q = &late_attrs;
10294 for (; *p; )
10296 t = *p;
10297 if (ATTR_IS_DEPENDENT (t))
10299 *p = TREE_CHAIN (t);
10300 TREE_CHAIN (t) = NULL_TREE;
10301 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10303 q = &TREE_CHAIN (*q);
10304 while (*q);
10306 else
10307 p = &TREE_CHAIN (t);
10310 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10314 /* Perform (or defer) access check for typedefs that were referenced
10315 from within the template TMPL code.
10316 This is a subroutine of instantiate_decl and instantiate_class_template.
10317 TMPL is the template to consider and TARGS is the list of arguments of
10318 that template. */
10320 static void
10321 perform_typedefs_access_check (tree tmpl, tree targs)
10323 location_t saved_location;
10324 unsigned i;
10325 qualified_typedef_usage_t *iter;
10327 if (!tmpl
10328 || (!CLASS_TYPE_P (tmpl)
10329 && TREE_CODE (tmpl) != FUNCTION_DECL))
10330 return;
10332 saved_location = input_location;
10333 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10335 tree type_decl = iter->typedef_decl;
10336 tree type_scope = iter->context;
10338 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10339 continue;
10341 if (uses_template_parms (type_decl))
10342 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10343 if (uses_template_parms (type_scope))
10344 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10346 /* Make access check error messages point to the location
10347 of the use of the typedef. */
10348 input_location = iter->locus;
10349 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10350 type_decl, type_decl,
10351 tf_warning_or_error);
10353 input_location = saved_location;
10356 static tree
10357 instantiate_class_template_1 (tree type)
10359 tree templ, args, pattern, t, member;
10360 tree typedecl;
10361 tree pbinfo;
10362 tree base_list;
10363 unsigned int saved_maximum_field_alignment;
10364 tree fn_context;
10366 if (type == error_mark_node)
10367 return error_mark_node;
10369 if (COMPLETE_OR_OPEN_TYPE_P (type)
10370 || uses_template_parms (type))
10371 return type;
10373 /* Figure out which template is being instantiated. */
10374 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10375 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10377 /* Mark the type as in the process of being defined. */
10378 TYPE_BEING_DEFINED (type) = 1;
10380 /* Determine what specialization of the original template to
10381 instantiate. */
10382 t = most_specialized_partial_spec (type, tf_warning_or_error);
10383 if (t == error_mark_node)
10384 return error_mark_node;
10385 else if (t)
10387 /* This TYPE is actually an instantiation of a partial
10388 specialization. We replace the innermost set of ARGS with
10389 the arguments appropriate for substitution. For example,
10390 given:
10392 template <class T> struct S {};
10393 template <class T> struct S<T*> {};
10395 and supposing that we are instantiating S<int*>, ARGS will
10396 presently be {int*} -- but we need {int}. */
10397 pattern = TREE_TYPE (t);
10398 args = TREE_PURPOSE (t);
10400 else
10402 pattern = TREE_TYPE (templ);
10403 args = CLASSTYPE_TI_ARGS (type);
10406 /* If the template we're instantiating is incomplete, then clearly
10407 there's nothing we can do. */
10408 if (!COMPLETE_TYPE_P (pattern))
10410 /* We can try again later. */
10411 TYPE_BEING_DEFINED (type) = 0;
10412 return type;
10415 /* If we've recursively instantiated too many templates, stop. */
10416 if (! push_tinst_level (type))
10417 return type;
10419 /* We may be in the middle of deferred access check. Disable
10420 it now. */
10421 push_deferring_access_checks (dk_no_deferred);
10423 int saved_unevaluated_operand = cp_unevaluated_operand;
10424 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10426 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10427 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10428 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10429 fn_context = error_mark_node;
10430 if (!fn_context)
10431 push_to_top_level ();
10432 else
10434 cp_unevaluated_operand = 0;
10435 c_inhibit_evaluation_warnings = 0;
10437 /* Use #pragma pack from the template context. */
10438 saved_maximum_field_alignment = maximum_field_alignment;
10439 maximum_field_alignment = TYPE_PRECISION (pattern);
10441 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10443 /* Set the input location to the most specialized template definition.
10444 This is needed if tsubsting causes an error. */
10445 typedecl = TYPE_MAIN_DECL (pattern);
10446 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10447 DECL_SOURCE_LOCATION (typedecl);
10449 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10450 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10451 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10452 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10453 if (ANON_AGGR_TYPE_P (pattern))
10454 SET_ANON_AGGR_TYPE_P (type);
10455 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10457 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10458 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10459 /* Adjust visibility for template arguments. */
10460 determine_visibility (TYPE_MAIN_DECL (type));
10462 if (CLASS_TYPE_P (type))
10463 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10465 pbinfo = TYPE_BINFO (pattern);
10467 /* We should never instantiate a nested class before its enclosing
10468 class; we need to look up the nested class by name before we can
10469 instantiate it, and that lookup should instantiate the enclosing
10470 class. */
10471 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10472 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10474 base_list = NULL_TREE;
10475 if (BINFO_N_BASE_BINFOS (pbinfo))
10477 tree pbase_binfo;
10478 tree pushed_scope;
10479 int i;
10481 /* We must enter the scope containing the type, as that is where
10482 the accessibility of types named in dependent bases are
10483 looked up from. */
10484 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10486 /* Substitute into each of the bases to determine the actual
10487 basetypes. */
10488 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10490 tree base;
10491 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10492 tree expanded_bases = NULL_TREE;
10493 int idx, len = 1;
10495 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10497 expanded_bases =
10498 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10499 args, tf_error, NULL_TREE);
10500 if (expanded_bases == error_mark_node)
10501 continue;
10503 len = TREE_VEC_LENGTH (expanded_bases);
10506 for (idx = 0; idx < len; idx++)
10508 if (expanded_bases)
10509 /* Extract the already-expanded base class. */
10510 base = TREE_VEC_ELT (expanded_bases, idx);
10511 else
10512 /* Substitute to figure out the base class. */
10513 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10514 NULL_TREE);
10516 if (base == error_mark_node)
10517 continue;
10519 base_list = tree_cons (access, base, base_list);
10520 if (BINFO_VIRTUAL_P (pbase_binfo))
10521 TREE_TYPE (base_list) = integer_type_node;
10525 /* The list is now in reverse order; correct that. */
10526 base_list = nreverse (base_list);
10528 if (pushed_scope)
10529 pop_scope (pushed_scope);
10531 /* Now call xref_basetypes to set up all the base-class
10532 information. */
10533 xref_basetypes (type, base_list);
10535 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10536 (int) ATTR_FLAG_TYPE_IN_PLACE,
10537 args, tf_error, NULL_TREE);
10538 fixup_attribute_variants (type);
10540 /* Now that our base classes are set up, enter the scope of the
10541 class, so that name lookups into base classes, etc. will work
10542 correctly. This is precisely analogous to what we do in
10543 begin_class_definition when defining an ordinary non-template
10544 class, except we also need to push the enclosing classes. */
10545 push_nested_class (type);
10547 /* Now members are processed in the order of declaration. */
10548 for (member = CLASSTYPE_DECL_LIST (pattern);
10549 member; member = TREE_CHAIN (member))
10551 tree t = TREE_VALUE (member);
10553 if (TREE_PURPOSE (member))
10555 if (TYPE_P (t))
10557 if (LAMBDA_TYPE_P (t))
10558 /* A closure type for a lambda in an NSDMI or default argument.
10559 Ignore it; it will be regenerated when needed. */
10560 continue;
10562 /* Build new CLASSTYPE_NESTED_UTDS. */
10564 tree newtag;
10565 bool class_template_p;
10567 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10568 && TYPE_LANG_SPECIFIC (t)
10569 && CLASSTYPE_IS_TEMPLATE (t));
10570 /* If the member is a class template, then -- even after
10571 substitution -- there may be dependent types in the
10572 template argument list for the class. We increment
10573 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10574 that function will assume that no types are dependent
10575 when outside of a template. */
10576 if (class_template_p)
10577 ++processing_template_decl;
10578 newtag = tsubst (t, args, tf_error, NULL_TREE);
10579 if (class_template_p)
10580 --processing_template_decl;
10581 if (newtag == error_mark_node)
10582 continue;
10584 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10586 tree name = TYPE_IDENTIFIER (t);
10588 if (class_template_p)
10589 /* Unfortunately, lookup_template_class sets
10590 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10591 instantiation (i.e., for the type of a member
10592 template class nested within a template class.)
10593 This behavior is required for
10594 maybe_process_partial_specialization to work
10595 correctly, but is not accurate in this case;
10596 the TAG is not an instantiation of anything.
10597 (The corresponding TEMPLATE_DECL is an
10598 instantiation, but the TYPE is not.) */
10599 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10601 /* Now, we call pushtag to put this NEWTAG into the scope of
10602 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10603 pushtag calling push_template_decl. We don't have to do
10604 this for enums because it will already have been done in
10605 tsubst_enum. */
10606 if (name)
10607 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10608 pushtag (name, newtag, /*tag_scope=*/ts_current);
10611 else if (DECL_DECLARES_FUNCTION_P (t))
10613 tree r;
10615 if (TREE_CODE (t) == TEMPLATE_DECL)
10616 ++processing_template_decl;
10617 r = tsubst (t, args, tf_error, NULL_TREE);
10618 if (TREE_CODE (t) == TEMPLATE_DECL)
10619 --processing_template_decl;
10620 set_current_access_from_decl (r);
10621 finish_member_declaration (r);
10622 /* Instantiate members marked with attribute used. */
10623 if (r != error_mark_node && DECL_PRESERVE_P (r))
10624 mark_used (r);
10625 if (TREE_CODE (r) == FUNCTION_DECL
10626 && DECL_OMP_DECLARE_REDUCTION_P (r))
10627 cp_check_omp_declare_reduction (r);
10629 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10630 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10631 /* A closure type for a lambda in an NSDMI or default argument.
10632 Ignore it; it will be regenerated when needed. */;
10633 else
10635 /* Build new TYPE_FIELDS. */
10636 if (TREE_CODE (t) == STATIC_ASSERT)
10638 tree condition;
10640 ++c_inhibit_evaluation_warnings;
10641 condition =
10642 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10643 tf_warning_or_error, NULL_TREE,
10644 /*integral_constant_expression_p=*/true);
10645 --c_inhibit_evaluation_warnings;
10647 finish_static_assert (condition,
10648 STATIC_ASSERT_MESSAGE (t),
10649 STATIC_ASSERT_SOURCE_LOCATION (t),
10650 /*member_p=*/true);
10652 else if (TREE_CODE (t) != CONST_DECL)
10654 tree r;
10655 tree vec = NULL_TREE;
10656 int len = 1;
10658 /* The file and line for this declaration, to
10659 assist in error message reporting. Since we
10660 called push_tinst_level above, we don't need to
10661 restore these. */
10662 input_location = DECL_SOURCE_LOCATION (t);
10664 if (TREE_CODE (t) == TEMPLATE_DECL)
10665 ++processing_template_decl;
10666 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10667 if (TREE_CODE (t) == TEMPLATE_DECL)
10668 --processing_template_decl;
10670 if (TREE_CODE (r) == TREE_VEC)
10672 /* A capture pack became multiple fields. */
10673 vec = r;
10674 len = TREE_VEC_LENGTH (vec);
10677 for (int i = 0; i < len; ++i)
10679 if (vec)
10680 r = TREE_VEC_ELT (vec, i);
10681 if (VAR_P (r))
10683 /* In [temp.inst]:
10685 [t]he initialization (and any associated
10686 side-effects) of a static data member does
10687 not occur unless the static data member is
10688 itself used in a way that requires the
10689 definition of the static data member to
10690 exist.
10692 Therefore, we do not substitute into the
10693 initialized for the static data member here. */
10694 finish_static_data_member_decl
10696 /*init=*/NULL_TREE,
10697 /*init_const_expr_p=*/false,
10698 /*asmspec_tree=*/NULL_TREE,
10699 /*flags=*/0);
10700 /* Instantiate members marked with attribute used. */
10701 if (r != error_mark_node && DECL_PRESERVE_P (r))
10702 mark_used (r);
10704 else if (TREE_CODE (r) == FIELD_DECL)
10706 /* Determine whether R has a valid type and can be
10707 completed later. If R is invalid, then its type
10708 is replaced by error_mark_node. */
10709 tree rtype = TREE_TYPE (r);
10710 if (can_complete_type_without_circularity (rtype))
10711 complete_type (rtype);
10713 if (!complete_or_array_type_p (rtype))
10715 /* If R's type couldn't be completed and
10716 it isn't a flexible array member (whose
10717 type is incomplete by definition) give
10718 an error. */
10719 cxx_incomplete_type_error (r, rtype);
10720 TREE_TYPE (r) = error_mark_node;
10724 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10725 such a thing will already have been added to the field
10726 list by tsubst_enum in finish_member_declaration in the
10727 CLASSTYPE_NESTED_UTDS case above. */
10728 if (!(TREE_CODE (r) == TYPE_DECL
10729 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10730 && DECL_ARTIFICIAL (r)))
10732 set_current_access_from_decl (r);
10733 finish_member_declaration (r);
10739 else
10741 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10742 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10744 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10746 tree friend_type = t;
10747 bool adjust_processing_template_decl = false;
10749 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10751 /* template <class T> friend class C; */
10752 friend_type = tsubst_friend_class (friend_type, args);
10753 adjust_processing_template_decl = true;
10755 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10757 /* template <class T> friend class C::D; */
10758 friend_type = tsubst (friend_type, args,
10759 tf_warning_or_error, NULL_TREE);
10760 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10761 friend_type = TREE_TYPE (friend_type);
10762 adjust_processing_template_decl = true;
10764 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10765 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10767 /* This could be either
10769 friend class T::C;
10771 when dependent_type_p is false or
10773 template <class U> friend class T::C;
10775 otherwise. */
10776 /* Bump processing_template_decl in case this is something like
10777 template <class T> friend struct A<T>::B. */
10778 ++processing_template_decl;
10779 friend_type = tsubst (friend_type, args,
10780 tf_warning_or_error, NULL_TREE);
10781 if (dependent_type_p (friend_type))
10782 adjust_processing_template_decl = true;
10783 --processing_template_decl;
10785 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10786 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10787 && TYPE_HIDDEN_P (friend_type))
10789 /* friend class C;
10791 where C hasn't been declared yet. Let's lookup name
10792 from namespace scope directly, bypassing any name that
10793 come from dependent base class. */
10794 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10796 /* The call to xref_tag_from_type does injection for friend
10797 classes. */
10798 push_nested_namespace (ns);
10799 friend_type =
10800 xref_tag_from_type (friend_type, NULL_TREE,
10801 /*tag_scope=*/ts_current);
10802 pop_nested_namespace (ns);
10804 else if (uses_template_parms (friend_type))
10805 /* friend class C<T>; */
10806 friend_type = tsubst (friend_type, args,
10807 tf_warning_or_error, NULL_TREE);
10808 /* Otherwise it's
10810 friend class C;
10812 where C is already declared or
10814 friend class C<int>;
10816 We don't have to do anything in these cases. */
10818 if (adjust_processing_template_decl)
10819 /* Trick make_friend_class into realizing that the friend
10820 we're adding is a template, not an ordinary class. It's
10821 important that we use make_friend_class since it will
10822 perform some error-checking and output cross-reference
10823 information. */
10824 ++processing_template_decl;
10826 if (friend_type != error_mark_node)
10827 make_friend_class (type, friend_type, /*complain=*/false);
10829 if (adjust_processing_template_decl)
10830 --processing_template_decl;
10832 else
10834 /* Build new DECL_FRIENDLIST. */
10835 tree r;
10837 /* The file and line for this declaration, to
10838 assist in error message reporting. Since we
10839 called push_tinst_level above, we don't need to
10840 restore these. */
10841 input_location = DECL_SOURCE_LOCATION (t);
10843 if (TREE_CODE (t) == TEMPLATE_DECL)
10845 ++processing_template_decl;
10846 push_deferring_access_checks (dk_no_check);
10849 r = tsubst_friend_function (t, args);
10850 add_friend (type, r, /*complain=*/false);
10851 if (TREE_CODE (t) == TEMPLATE_DECL)
10853 pop_deferring_access_checks ();
10854 --processing_template_decl;
10860 if (fn_context)
10862 /* Restore these before substituting into the lambda capture
10863 initializers. */
10864 cp_unevaluated_operand = saved_unevaluated_operand;
10865 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10868 /* Set the file and line number information to whatever is given for
10869 the class itself. This puts error messages involving generated
10870 implicit functions at a predictable point, and the same point
10871 that would be used for non-template classes. */
10872 input_location = DECL_SOURCE_LOCATION (typedecl);
10874 unreverse_member_declarations (type);
10875 finish_struct_1 (type);
10876 TYPE_BEING_DEFINED (type) = 0;
10878 /* We don't instantiate default arguments for member functions. 14.7.1:
10880 The implicit instantiation of a class template specialization causes
10881 the implicit instantiation of the declarations, but not of the
10882 definitions or default arguments, of the class member functions,
10883 member classes, static data members and member templates.... */
10885 /* Some typedefs referenced from within the template code need to be access
10886 checked at template instantiation time, i.e now. These types were
10887 added to the template at parsing time. Let's get those and perform
10888 the access checks then. */
10889 perform_typedefs_access_check (pattern, args);
10890 perform_deferred_access_checks (tf_warning_or_error);
10891 pop_nested_class ();
10892 maximum_field_alignment = saved_maximum_field_alignment;
10893 if (!fn_context)
10894 pop_from_top_level ();
10895 pop_deferring_access_checks ();
10896 pop_tinst_level ();
10898 /* The vtable for a template class can be emitted in any translation
10899 unit in which the class is instantiated. When there is no key
10900 method, however, finish_struct_1 will already have added TYPE to
10901 the keyed_classes. */
10902 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10903 vec_safe_push (keyed_classes, type);
10905 return type;
10908 /* Wrapper for instantiate_class_template_1. */
10910 tree
10911 instantiate_class_template (tree type)
10913 tree ret;
10914 timevar_push (TV_TEMPLATE_INST);
10915 ret = instantiate_class_template_1 (type);
10916 timevar_pop (TV_TEMPLATE_INST);
10917 return ret;
10920 static tree
10921 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10923 tree r;
10925 if (!t)
10926 r = t;
10927 else if (TYPE_P (t))
10928 r = tsubst (t, args, complain, in_decl);
10929 else
10931 if (!(complain & tf_warning))
10932 ++c_inhibit_evaluation_warnings;
10933 r = tsubst_expr (t, args, complain, in_decl,
10934 /*integral_constant_expression_p=*/true);
10935 if (!(complain & tf_warning))
10936 --c_inhibit_evaluation_warnings;
10938 return r;
10941 /* Given a function parameter pack TMPL_PARM and some function parameters
10942 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10943 and set *SPEC_P to point at the next point in the list. */
10945 tree
10946 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10948 /* Collect all of the extra "packed" parameters into an
10949 argument pack. */
10950 tree parmvec;
10951 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10952 tree spec_parm = *spec_p;
10953 int i, len;
10955 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10956 if (tmpl_parm
10957 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10958 break;
10960 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10961 parmvec = make_tree_vec (len);
10962 spec_parm = *spec_p;
10963 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10965 tree elt = spec_parm;
10966 if (DECL_PACK_P (elt))
10967 elt = make_pack_expansion (elt);
10968 TREE_VEC_ELT (parmvec, i) = elt;
10971 /* Build the argument packs. */
10972 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10973 *spec_p = spec_parm;
10975 return argpack;
10978 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10979 NONTYPE_ARGUMENT_PACK. */
10981 static tree
10982 make_fnparm_pack (tree spec_parm)
10984 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10987 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10988 pack expansion with no extra args, 2 if it has extra args, or 0
10989 if it is not a pack expansion. */
10991 static int
10992 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10994 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10995 if (i >= TREE_VEC_LENGTH (vec))
10996 return 0;
10997 tree elt = TREE_VEC_ELT (vec, i);
10998 if (DECL_P (elt))
10999 /* A decl pack is itself an expansion. */
11000 elt = TREE_TYPE (elt);
11001 if (!PACK_EXPANSION_P (elt))
11002 return 0;
11003 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11004 return 2;
11005 return 1;
11009 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11011 static tree
11012 make_argument_pack_select (tree arg_pack, unsigned index)
11014 tree aps = make_node (ARGUMENT_PACK_SELECT);
11016 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11017 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11019 return aps;
11022 /* This is a subroutine of tsubst_pack_expansion.
11024 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11025 mechanism to store the (non complete list of) arguments of the
11026 substitution and return a non substituted pack expansion, in order
11027 to wait for when we have enough arguments to really perform the
11028 substitution. */
11030 static bool
11031 use_pack_expansion_extra_args_p (tree parm_packs,
11032 int arg_pack_len,
11033 bool has_empty_arg)
11035 /* If one pack has an expansion and another pack has a normal
11036 argument or if one pack has an empty argument and an another
11037 one hasn't then tsubst_pack_expansion cannot perform the
11038 substitution and need to fall back on the
11039 PACK_EXPANSION_EXTRA mechanism. */
11040 if (parm_packs == NULL_TREE)
11041 return false;
11042 else if (has_empty_arg)
11043 return true;
11045 bool has_expansion_arg = false;
11046 for (int i = 0 ; i < arg_pack_len; ++i)
11048 bool has_non_expansion_arg = false;
11049 for (tree parm_pack = parm_packs;
11050 parm_pack;
11051 parm_pack = TREE_CHAIN (parm_pack))
11053 tree arg = TREE_VALUE (parm_pack);
11055 int exp = argument_pack_element_is_expansion_p (arg, i);
11056 if (exp == 2)
11057 /* We can't substitute a pack expansion with extra args into
11058 our pattern. */
11059 return true;
11060 else if (exp)
11061 has_expansion_arg = true;
11062 else
11063 has_non_expansion_arg = true;
11066 if (has_expansion_arg && has_non_expansion_arg)
11067 return true;
11069 return false;
11072 /* [temp.variadic]/6 says that:
11074 The instantiation of a pack expansion [...]
11075 produces a list E1,E2, ..., En, where N is the number of elements
11076 in the pack expansion parameters.
11078 This subroutine of tsubst_pack_expansion produces one of these Ei.
11080 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11081 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11082 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11083 INDEX is the index 'i' of the element Ei to produce. ARGS,
11084 COMPLAIN, and IN_DECL are the same parameters as for the
11085 tsubst_pack_expansion function.
11087 The function returns the resulting Ei upon successful completion,
11088 or error_mark_node.
11090 Note that this function possibly modifies the ARGS parameter, so
11091 it's the responsibility of the caller to restore it. */
11093 static tree
11094 gen_elem_of_pack_expansion_instantiation (tree pattern,
11095 tree parm_packs,
11096 unsigned index,
11097 tree args /* This parm gets
11098 modified. */,
11099 tsubst_flags_t complain,
11100 tree in_decl)
11102 tree t;
11103 bool ith_elem_is_expansion = false;
11105 /* For each parameter pack, change the substitution of the parameter
11106 pack to the ith argument in its argument pack, then expand the
11107 pattern. */
11108 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11110 tree parm = TREE_PURPOSE (pack);
11111 tree arg_pack = TREE_VALUE (pack);
11112 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11114 ith_elem_is_expansion |=
11115 argument_pack_element_is_expansion_p (arg_pack, index);
11117 /* Select the Ith argument from the pack. */
11118 if (TREE_CODE (parm) == PARM_DECL
11119 || VAR_P (parm)
11120 || TREE_CODE (parm) == FIELD_DECL)
11122 if (index == 0)
11124 aps = make_argument_pack_select (arg_pack, index);
11125 if (!mark_used (parm, complain) && !(complain & tf_error))
11126 return error_mark_node;
11127 register_local_specialization (aps, parm);
11129 else
11130 aps = retrieve_local_specialization (parm);
11132 else
11134 int idx, level;
11135 template_parm_level_and_index (parm, &level, &idx);
11137 if (index == 0)
11139 aps = make_argument_pack_select (arg_pack, index);
11140 /* Update the corresponding argument. */
11141 TMPL_ARG (args, level, idx) = aps;
11143 else
11144 /* Re-use the ARGUMENT_PACK_SELECT. */
11145 aps = TMPL_ARG (args, level, idx);
11147 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11150 /* Substitute into the PATTERN with the (possibly altered)
11151 arguments. */
11152 if (pattern == in_decl)
11153 /* Expanding a fixed parameter pack from
11154 coerce_template_parameter_pack. */
11155 t = tsubst_decl (pattern, args, complain);
11156 else if (pattern == error_mark_node)
11157 t = error_mark_node;
11158 else if (constraint_p (pattern))
11160 if (processing_template_decl)
11161 t = tsubst_constraint (pattern, args, complain, in_decl);
11162 else
11163 t = (constraints_satisfied_p (pattern, args)
11164 ? boolean_true_node : boolean_false_node);
11166 else if (!TYPE_P (pattern))
11167 t = tsubst_expr (pattern, args, complain, in_decl,
11168 /*integral_constant_expression_p=*/false);
11169 else
11170 t = tsubst (pattern, args, complain, in_decl);
11172 /* If the Ith argument pack element is a pack expansion, then
11173 the Ith element resulting from the substituting is going to
11174 be a pack expansion as well. */
11175 if (ith_elem_is_expansion)
11176 t = make_pack_expansion (t, complain);
11178 return t;
11181 /* When the unexpanded parameter pack in a fold expression expands to an empty
11182 sequence, the value of the expression is as follows; the program is
11183 ill-formed if the operator is not listed in this table.
11185 && true
11186 || false
11187 , void() */
11189 tree
11190 expand_empty_fold (tree t, tsubst_flags_t complain)
11192 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11193 if (!FOLD_EXPR_MODIFY_P (t))
11194 switch (code)
11196 case TRUTH_ANDIF_EXPR:
11197 return boolean_true_node;
11198 case TRUTH_ORIF_EXPR:
11199 return boolean_false_node;
11200 case COMPOUND_EXPR:
11201 return void_node;
11202 default:
11203 break;
11206 if (complain & tf_error)
11207 error_at (location_of (t),
11208 "fold of empty expansion over %O", code);
11209 return error_mark_node;
11212 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11213 form an expression that combines the two terms using the
11214 operator of T. */
11216 static tree
11217 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11219 tree op = FOLD_EXPR_OP (t);
11220 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11222 // Handle compound assignment operators.
11223 if (FOLD_EXPR_MODIFY_P (t))
11224 return build_x_modify_expr (input_location, left, code, right, complain);
11226 switch (code)
11228 case COMPOUND_EXPR:
11229 return build_x_compound_expr (input_location, left, right, complain);
11230 case DOTSTAR_EXPR:
11231 return build_m_component_ref (left, right, complain);
11232 default:
11233 return build_x_binary_op (input_location, code,
11234 left, TREE_CODE (left),
11235 right, TREE_CODE (right),
11236 /*overload=*/NULL,
11237 complain);
11241 /* Substitute ARGS into the pack of a fold expression T. */
11243 static inline tree
11244 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11246 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11249 /* Substitute ARGS into the pack of a fold expression T. */
11251 static inline tree
11252 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11254 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11257 /* Expand a PACK of arguments into a grouped as left fold.
11258 Given a pack containing elements A0, A1, ..., An and an
11259 operator @, this builds the expression:
11261 ((A0 @ A1) @ A2) ... @ An
11263 Note that PACK must not be empty.
11265 The operator is defined by the original fold expression T. */
11267 static tree
11268 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11270 tree left = TREE_VEC_ELT (pack, 0);
11271 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11273 tree right = TREE_VEC_ELT (pack, i);
11274 left = fold_expression (t, left, right, complain);
11276 return left;
11279 /* Substitute into a unary left fold expression. */
11281 static tree
11282 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11283 tree in_decl)
11285 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11286 if (pack == error_mark_node)
11287 return error_mark_node;
11288 if (PACK_EXPANSION_P (pack))
11290 tree r = copy_node (t);
11291 FOLD_EXPR_PACK (r) = pack;
11292 return r;
11294 if (TREE_VEC_LENGTH (pack) == 0)
11295 return expand_empty_fold (t, complain);
11296 else
11297 return expand_left_fold (t, pack, complain);
11300 /* Substitute into a binary left fold expression.
11302 Do ths by building a single (non-empty) vector of argumnts and
11303 building the expression from those elements. */
11305 static tree
11306 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11307 tree in_decl)
11309 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11310 if (pack == error_mark_node)
11311 return error_mark_node;
11312 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11313 if (init == error_mark_node)
11314 return error_mark_node;
11316 if (PACK_EXPANSION_P (pack))
11318 tree r = copy_node (t);
11319 FOLD_EXPR_PACK (r) = pack;
11320 FOLD_EXPR_INIT (r) = init;
11321 return r;
11324 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11325 TREE_VEC_ELT (vec, 0) = init;
11326 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11327 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11329 return expand_left_fold (t, vec, complain);
11332 /* Expand a PACK of arguments into a grouped as right fold.
11333 Given a pack containing elementns A0, A1, ..., and an
11334 operator @, this builds the expression:
11336 A0@ ... (An-2 @ (An-1 @ An))
11338 Note that PACK must not be empty.
11340 The operator is defined by the original fold expression T. */
11342 tree
11343 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11345 // Build the expression.
11346 int n = TREE_VEC_LENGTH (pack);
11347 tree right = TREE_VEC_ELT (pack, n - 1);
11348 for (--n; n != 0; --n)
11350 tree left = TREE_VEC_ELT (pack, n - 1);
11351 right = fold_expression (t, left, right, complain);
11353 return right;
11356 /* Substitute into a unary right fold expression. */
11358 static tree
11359 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11360 tree in_decl)
11362 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11363 if (pack == error_mark_node)
11364 return error_mark_node;
11365 if (PACK_EXPANSION_P (pack))
11367 tree r = copy_node (t);
11368 FOLD_EXPR_PACK (r) = pack;
11369 return r;
11371 if (TREE_VEC_LENGTH (pack) == 0)
11372 return expand_empty_fold (t, complain);
11373 else
11374 return expand_right_fold (t, pack, complain);
11377 /* Substitute into a binary right fold expression.
11379 Do ths by building a single (non-empty) vector of arguments and
11380 building the expression from those elements. */
11382 static tree
11383 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11384 tree in_decl)
11386 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11387 if (pack == error_mark_node)
11388 return error_mark_node;
11389 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11390 if (init == error_mark_node)
11391 return error_mark_node;
11393 if (PACK_EXPANSION_P (pack))
11395 tree r = copy_node (t);
11396 FOLD_EXPR_PACK (r) = pack;
11397 FOLD_EXPR_INIT (r) = init;
11398 return r;
11401 int n = TREE_VEC_LENGTH (pack);
11402 tree vec = make_tree_vec (n + 1);
11403 for (int i = 0; i < n; ++i)
11404 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11405 TREE_VEC_ELT (vec, n) = init;
11407 return expand_right_fold (t, vec, complain);
11410 /* Walk through the pattern of a pack expansion, adding everything in
11411 local_specializations to a list. */
11413 static tree
11414 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data)
11416 tree *extra = reinterpret_cast<tree*>(data);
11417 if (tree spec = retrieve_local_specialization (*tp))
11419 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11421 /* Pull out the actual PARM_DECL for the partial instantiation. */
11422 tree args = ARGUMENT_PACK_ARGS (spec);
11423 gcc_assert (TREE_VEC_LENGTH (args) == 1);
11424 tree arg = TREE_VEC_ELT (args, 0);
11425 spec = PACK_EXPANSION_PATTERN (arg);
11427 *extra = tree_cons (*tp, spec, *extra);
11429 return NULL_TREE;
11431 static tree
11432 extract_local_specs (tree pattern)
11434 tree extra = NULL_TREE;
11435 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &extra);
11436 return extra;
11439 /* Substitute ARGS into T, which is an pack expansion
11440 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11441 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11442 (if only a partial substitution could be performed) or
11443 ERROR_MARK_NODE if there was an error. */
11444 tree
11445 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11446 tree in_decl)
11448 tree pattern;
11449 tree pack, packs = NULL_TREE;
11450 bool unsubstituted_packs = false;
11451 bool unsubstituted_fn_pack = false;
11452 int i, len = -1;
11453 tree result;
11454 hash_map<tree, tree> *saved_local_specializations = NULL;
11455 bool need_local_specializations = false;
11456 int levels;
11458 gcc_assert (PACK_EXPANSION_P (t));
11459 pattern = PACK_EXPANSION_PATTERN (t);
11461 /* Add in any args remembered from an earlier partial instantiation. */
11462 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11463 if (extra && TREE_CODE (extra) == TREE_LIST)
11465 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11467 /* The partial instantiation involved local declarations collected in
11468 extract_local_specs; map from the general template to our local
11469 context. */
11470 tree gen = TREE_PURPOSE (elt);
11471 tree partial = TREE_VALUE (elt);
11472 tree inst = retrieve_local_specialization (partial);
11473 register_local_specialization (inst, gen);
11475 gcc_assert (!TREE_PURPOSE (extra));
11476 extra = TREE_VALUE (extra);
11478 args = add_to_template_args (extra, args);
11480 levels = TMPL_ARGS_DEPTH (args);
11482 /* Determine the argument packs that will instantiate the parameter
11483 packs used in the expansion expression. While we're at it,
11484 compute the number of arguments to be expanded and make sure it
11485 is consistent. */
11486 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11487 pack = TREE_CHAIN (pack))
11489 tree parm_pack = TREE_VALUE (pack);
11490 tree arg_pack = NULL_TREE;
11491 tree orig_arg = NULL_TREE;
11492 int level = 0;
11494 if (TREE_CODE (parm_pack) == BASES)
11496 gcc_assert (parm_pack == pattern);
11497 if (BASES_DIRECT (parm_pack))
11498 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11499 args, complain, in_decl, false));
11500 else
11501 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11502 args, complain, in_decl, false));
11504 else if (builtin_pack_call_p (parm_pack))
11506 /* ??? Support use in other patterns. */
11507 gcc_assert (parm_pack == pattern);
11508 return expand_builtin_pack_call (parm_pack, args,
11509 complain, in_decl);
11511 else if (TREE_CODE (parm_pack) == PARM_DECL)
11513 /* We know we have correct local_specializations if this
11514 expansion is at function scope, or if we're dealing with a
11515 local parameter in a requires expression; for the latter,
11516 tsubst_requires_expr set it up appropriately. */
11517 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11518 arg_pack = retrieve_local_specialization (parm_pack);
11519 else
11520 /* We can't rely on local_specializations for a parameter
11521 name used later in a function declaration (such as in a
11522 late-specified return type). Even if it exists, it might
11523 have the wrong value for a recursive call. */
11524 need_local_specializations = true;
11526 if (!arg_pack)
11528 /* This parameter pack was used in an unevaluated context. Just
11529 make a dummy decl, since it's only used for its type. */
11530 arg_pack = tsubst_decl (parm_pack, args, complain);
11531 if (arg_pack && DECL_PACK_P (arg_pack))
11532 /* Partial instantiation of the parm_pack, we can't build
11533 up an argument pack yet. */
11534 arg_pack = NULL_TREE;
11535 else
11536 arg_pack = make_fnparm_pack (arg_pack);
11538 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11539 /* This argument pack isn't fully instantiated yet. We set this
11540 flag rather than clear arg_pack because we do want to do the
11541 optimization below, and we don't want to substitute directly
11542 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11543 where it isn't expected). */
11544 unsubstituted_fn_pack = true;
11546 else if (is_normal_capture_proxy (parm_pack))
11548 arg_pack = retrieve_local_specialization (parm_pack);
11549 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11550 unsubstituted_fn_pack = true;
11552 else
11554 int idx;
11555 template_parm_level_and_index (parm_pack, &level, &idx);
11557 if (level <= levels)
11558 arg_pack = TMPL_ARG (args, level, idx);
11561 orig_arg = arg_pack;
11562 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11563 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11565 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11566 /* This can only happen if we forget to expand an argument
11567 pack somewhere else. Just return an error, silently. */
11569 result = make_tree_vec (1);
11570 TREE_VEC_ELT (result, 0) = error_mark_node;
11571 return result;
11574 if (arg_pack)
11576 int my_len =
11577 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11579 /* Don't bother trying to do a partial substitution with
11580 incomplete packs; we'll try again after deduction. */
11581 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11582 return t;
11584 if (len < 0)
11585 len = my_len;
11586 else if (len != my_len
11587 && !unsubstituted_fn_pack)
11589 if (!(complain & tf_error))
11590 /* Fail quietly. */;
11591 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11592 error ("mismatched argument pack lengths while expanding %qT",
11593 pattern);
11594 else
11595 error ("mismatched argument pack lengths while expanding %qE",
11596 pattern);
11597 return error_mark_node;
11600 /* Keep track of the parameter packs and their corresponding
11601 argument packs. */
11602 packs = tree_cons (parm_pack, arg_pack, packs);
11603 TREE_TYPE (packs) = orig_arg;
11605 else
11607 /* We can't substitute for this parameter pack. We use a flag as
11608 well as the missing_level counter because function parameter
11609 packs don't have a level. */
11610 gcc_assert (processing_template_decl);
11611 unsubstituted_packs = true;
11615 /* If the expansion is just T..., return the matching argument pack, unless
11616 we need to call convert_from_reference on all the elements. This is an
11617 important optimization; see c++/68422. */
11618 if (!unsubstituted_packs
11619 && TREE_PURPOSE (packs) == pattern)
11621 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11623 /* If the argument pack is a single pack expansion, pull it out. */
11624 if (TREE_VEC_LENGTH (args) == 1
11625 && pack_expansion_args_count (args))
11626 return TREE_VEC_ELT (args, 0);
11628 /* Types need no adjustment, nor does sizeof..., and if we still have
11629 some pack expansion args we won't do anything yet. */
11630 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11631 || PACK_EXPANSION_SIZEOF_P (t)
11632 || pack_expansion_args_count (args))
11633 return args;
11634 /* Also optimize expression pack expansions if we can tell that the
11635 elements won't have reference type. */
11636 tree type = TREE_TYPE (pattern);
11637 if (type && TREE_CODE (type) != REFERENCE_TYPE
11638 && !PACK_EXPANSION_P (type)
11639 && !WILDCARD_TYPE_P (type))
11640 return args;
11641 /* Otherwise use the normal path so we get convert_from_reference. */
11644 /* We cannot expand this expansion expression, because we don't have
11645 all of the argument packs we need. */
11646 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11648 /* We got some full packs, but we can't substitute them in until we
11649 have values for all the packs. So remember these until then. */
11651 t = make_pack_expansion (pattern, complain);
11652 tree extra = args;
11653 if (local_specializations)
11654 if (tree locals = extract_local_specs (pattern))
11655 extra = tree_cons (NULL_TREE, extra, locals);
11656 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11657 return t;
11659 else if (unsubstituted_packs)
11661 /* There were no real arguments, we're just replacing a parameter
11662 pack with another version of itself. Substitute into the
11663 pattern and return a PACK_EXPANSION_*. The caller will need to
11664 deal with that. */
11665 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11666 t = tsubst_expr (pattern, args, complain, in_decl,
11667 /*integral_constant_expression_p=*/false);
11668 else
11669 t = tsubst (pattern, args, complain, in_decl);
11670 t = make_pack_expansion (t, complain);
11671 return t;
11674 gcc_assert (len >= 0);
11676 if (need_local_specializations)
11678 /* We're in a late-specified return type, so create our own local
11679 specializations map; the current map is either NULL or (in the
11680 case of recursive unification) might have bindings that we don't
11681 want to use or alter. */
11682 saved_local_specializations = local_specializations;
11683 local_specializations = new hash_map<tree, tree>;
11686 /* For each argument in each argument pack, substitute into the
11687 pattern. */
11688 result = make_tree_vec (len);
11689 tree elem_args = copy_template_args (args);
11690 for (i = 0; i < len; ++i)
11692 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11694 elem_args, complain,
11695 in_decl);
11696 TREE_VEC_ELT (result, i) = t;
11697 if (t == error_mark_node)
11699 result = error_mark_node;
11700 break;
11704 /* Update ARGS to restore the substitution from parameter packs to
11705 their argument packs. */
11706 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11708 tree parm = TREE_PURPOSE (pack);
11710 if (TREE_CODE (parm) == PARM_DECL
11711 || VAR_P (parm)
11712 || TREE_CODE (parm) == FIELD_DECL)
11713 register_local_specialization (TREE_TYPE (pack), parm);
11714 else
11716 int idx, level;
11718 if (TREE_VALUE (pack) == NULL_TREE)
11719 continue;
11721 template_parm_level_and_index (parm, &level, &idx);
11723 /* Update the corresponding argument. */
11724 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11725 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11726 TREE_TYPE (pack);
11727 else
11728 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11732 if (need_local_specializations)
11734 delete local_specializations;
11735 local_specializations = saved_local_specializations;
11738 /* If the dependent pack arguments were such that we end up with only a
11739 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11740 if (len == 1 && TREE_CODE (result) == TREE_VEC
11741 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11742 return TREE_VEC_ELT (result, 0);
11744 return result;
11747 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11748 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11749 parameter packs; all parms generated from a function parameter pack will
11750 have the same DECL_PARM_INDEX. */
11752 tree
11753 get_pattern_parm (tree parm, tree tmpl)
11755 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11756 tree patparm;
11758 if (DECL_ARTIFICIAL (parm))
11760 for (patparm = DECL_ARGUMENTS (pattern);
11761 patparm; patparm = DECL_CHAIN (patparm))
11762 if (DECL_ARTIFICIAL (patparm)
11763 && DECL_NAME (parm) == DECL_NAME (patparm))
11764 break;
11766 else
11768 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11769 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11770 gcc_assert (DECL_PARM_INDEX (patparm)
11771 == DECL_PARM_INDEX (parm));
11774 return patparm;
11777 /* Make an argument pack out of the TREE_VEC VEC. */
11779 static tree
11780 make_argument_pack (tree vec)
11782 tree pack;
11783 tree elt = TREE_VEC_ELT (vec, 0);
11784 if (TYPE_P (elt))
11785 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11786 else
11788 pack = make_node (NONTYPE_ARGUMENT_PACK);
11789 TREE_CONSTANT (pack) = 1;
11791 SET_ARGUMENT_PACK_ARGS (pack, vec);
11792 return pack;
11795 /* Return an exact copy of template args T that can be modified
11796 independently. */
11798 static tree
11799 copy_template_args (tree t)
11801 if (t == error_mark_node)
11802 return t;
11804 int len = TREE_VEC_LENGTH (t);
11805 tree new_vec = make_tree_vec (len);
11807 for (int i = 0; i < len; ++i)
11809 tree elt = TREE_VEC_ELT (t, i);
11810 if (elt && TREE_CODE (elt) == TREE_VEC)
11811 elt = copy_template_args (elt);
11812 TREE_VEC_ELT (new_vec, i) = elt;
11815 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11816 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11818 return new_vec;
11821 /* Substitute ARGS into the vector or list of template arguments T. */
11823 static tree
11824 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11826 tree orig_t = t;
11827 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11828 tree *elts;
11830 if (t == error_mark_node)
11831 return error_mark_node;
11833 len = TREE_VEC_LENGTH (t);
11834 elts = XALLOCAVEC (tree, len);
11836 for (i = 0; i < len; i++)
11838 tree orig_arg = TREE_VEC_ELT (t, i);
11839 tree new_arg;
11841 if (TREE_CODE (orig_arg) == TREE_VEC)
11842 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11843 else if (PACK_EXPANSION_P (orig_arg))
11845 /* Substitute into an expansion expression. */
11846 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11848 if (TREE_CODE (new_arg) == TREE_VEC)
11849 /* Add to the expanded length adjustment the number of
11850 expanded arguments. We subtract one from this
11851 measurement, because the argument pack expression
11852 itself is already counted as 1 in
11853 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11854 the argument pack is empty. */
11855 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11857 else if (ARGUMENT_PACK_P (orig_arg))
11859 /* Substitute into each of the arguments. */
11860 new_arg = TYPE_P (orig_arg)
11861 ? cxx_make_type (TREE_CODE (orig_arg))
11862 : make_node (TREE_CODE (orig_arg));
11864 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11865 args, complain, in_decl);
11866 if (pack_args == error_mark_node)
11867 new_arg = error_mark_node;
11868 else
11869 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11871 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11872 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11874 else
11875 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11877 if (new_arg == error_mark_node)
11878 return error_mark_node;
11880 elts[i] = new_arg;
11881 if (new_arg != orig_arg)
11882 need_new = 1;
11885 if (!need_new)
11886 return t;
11888 /* Make space for the expanded arguments coming from template
11889 argument packs. */
11890 t = make_tree_vec (len + expanded_len_adjust);
11891 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11892 arguments for a member template.
11893 In that case each TREE_VEC in ORIG_T represents a level of template
11894 arguments, and ORIG_T won't carry any non defaulted argument count.
11895 It will rather be the nested TREE_VECs that will carry one.
11896 In other words, ORIG_T carries a non defaulted argument count only
11897 if it doesn't contain any nested TREE_VEC. */
11898 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11900 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11901 count += expanded_len_adjust;
11902 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11904 for (i = 0, out = 0; i < len; i++)
11906 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11907 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11908 && TREE_CODE (elts[i]) == TREE_VEC)
11910 int idx;
11912 /* Now expand the template argument pack "in place". */
11913 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11914 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11916 else
11918 TREE_VEC_ELT (t, out) = elts[i];
11919 out++;
11923 return t;
11926 /* Substitute ARGS into one level PARMS of template parameters. */
11928 static tree
11929 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11931 if (parms == error_mark_node)
11932 return error_mark_node;
11934 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11936 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11938 tree tuple = TREE_VEC_ELT (parms, i);
11940 if (tuple == error_mark_node)
11941 continue;
11943 TREE_VEC_ELT (new_vec, i) =
11944 tsubst_template_parm (tuple, args, complain);
11947 return new_vec;
11950 /* Return the result of substituting ARGS into the template parameters
11951 given by PARMS. If there are m levels of ARGS and m + n levels of
11952 PARMS, then the result will contain n levels of PARMS. For
11953 example, if PARMS is `template <class T> template <class U>
11954 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11955 result will be `template <int*, double, class V>'. */
11957 static tree
11958 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11960 tree r = NULL_TREE;
11961 tree* new_parms;
11963 /* When substituting into a template, we must set
11964 PROCESSING_TEMPLATE_DECL as the template parameters may be
11965 dependent if they are based on one-another, and the dependency
11966 predicates are short-circuit outside of templates. */
11967 ++processing_template_decl;
11969 for (new_parms = &r;
11970 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11971 new_parms = &(TREE_CHAIN (*new_parms)),
11972 parms = TREE_CHAIN (parms))
11974 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11975 args, complain);
11976 *new_parms =
11977 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11978 - TMPL_ARGS_DEPTH (args)),
11979 new_vec, NULL_TREE);
11982 --processing_template_decl;
11984 return r;
11987 /* Return the result of substituting ARGS into one template parameter
11988 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11989 parameter and which TREE_PURPOSE is the default argument of the
11990 template parameter. */
11992 static tree
11993 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11995 tree default_value, parm_decl;
11997 if (args == NULL_TREE
11998 || t == NULL_TREE
11999 || t == error_mark_node)
12000 return t;
12002 gcc_assert (TREE_CODE (t) == TREE_LIST);
12004 default_value = TREE_PURPOSE (t);
12005 parm_decl = TREE_VALUE (t);
12007 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12008 if (TREE_CODE (parm_decl) == PARM_DECL
12009 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12010 parm_decl = error_mark_node;
12011 default_value = tsubst_template_arg (default_value, args,
12012 complain, NULL_TREE);
12014 return build_tree_list (default_value, parm_decl);
12017 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12018 type T. If T is not an aggregate or enumeration type, it is
12019 handled as if by tsubst. IN_DECL is as for tsubst. If
12020 ENTERING_SCOPE is nonzero, T is the context for a template which
12021 we are presently tsubst'ing. Return the substituted value. */
12023 static tree
12024 tsubst_aggr_type (tree t,
12025 tree args,
12026 tsubst_flags_t complain,
12027 tree in_decl,
12028 int entering_scope)
12030 if (t == NULL_TREE)
12031 return NULL_TREE;
12033 switch (TREE_CODE (t))
12035 case RECORD_TYPE:
12036 if (TYPE_PTRMEMFUNC_P (t))
12037 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12039 /* Fall through. */
12040 case ENUMERAL_TYPE:
12041 case UNION_TYPE:
12042 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12044 tree argvec;
12045 tree context;
12046 tree r;
12047 int saved_unevaluated_operand;
12048 int saved_inhibit_evaluation_warnings;
12050 /* In "sizeof(X<I>)" we need to evaluate "I". */
12051 saved_unevaluated_operand = cp_unevaluated_operand;
12052 cp_unevaluated_operand = 0;
12053 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12054 c_inhibit_evaluation_warnings = 0;
12056 /* First, determine the context for the type we are looking
12057 up. */
12058 context = TYPE_CONTEXT (t);
12059 if (context && TYPE_P (context))
12061 context = tsubst_aggr_type (context, args, complain,
12062 in_decl, /*entering_scope=*/1);
12063 /* If context is a nested class inside a class template,
12064 it may still need to be instantiated (c++/33959). */
12065 context = complete_type (context);
12068 /* Then, figure out what arguments are appropriate for the
12069 type we are trying to find. For example, given:
12071 template <class T> struct S;
12072 template <class T, class U> void f(T, U) { S<U> su; }
12074 and supposing that we are instantiating f<int, double>,
12075 then our ARGS will be {int, double}, but, when looking up
12076 S we only want {double}. */
12077 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12078 complain, in_decl);
12079 if (argvec == error_mark_node)
12080 r = error_mark_node;
12081 else
12083 r = lookup_template_class (t, argvec, in_decl, context,
12084 entering_scope, complain);
12085 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12088 cp_unevaluated_operand = saved_unevaluated_operand;
12089 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12091 return r;
12093 else
12094 /* This is not a template type, so there's nothing to do. */
12095 return t;
12097 default:
12098 return tsubst (t, args, complain, in_decl);
12102 static GTY((cache)) tree_cache_map *defarg_inst;
12104 /* Substitute into the default argument ARG (a default argument for
12105 FN), which has the indicated TYPE. */
12107 tree
12108 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12109 tsubst_flags_t complain)
12111 tree saved_class_ptr = NULL_TREE;
12112 tree saved_class_ref = NULL_TREE;
12113 int errs = errorcount + sorrycount;
12115 /* This can happen in invalid code. */
12116 if (TREE_CODE (arg) == DEFAULT_ARG)
12117 return arg;
12119 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12120 parm = chain_index (parmnum, parm);
12121 tree parmtype = TREE_TYPE (parm);
12122 if (DECL_BY_REFERENCE (parm))
12123 parmtype = TREE_TYPE (parmtype);
12124 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12126 tree *slot;
12127 if (defarg_inst && (slot = defarg_inst->get (parm)))
12128 return *slot;
12130 /* This default argument came from a template. Instantiate the
12131 default argument here, not in tsubst. In the case of
12132 something like:
12134 template <class T>
12135 struct S {
12136 static T t();
12137 void f(T = t());
12140 we must be careful to do name lookup in the scope of S<T>,
12141 rather than in the current class. */
12142 push_access_scope (fn);
12143 /* The "this" pointer is not valid in a default argument. */
12144 if (cfun)
12146 saved_class_ptr = current_class_ptr;
12147 cp_function_chain->x_current_class_ptr = NULL_TREE;
12148 saved_class_ref = current_class_ref;
12149 cp_function_chain->x_current_class_ref = NULL_TREE;
12152 start_lambda_scope (parm);
12154 push_deferring_access_checks(dk_no_deferred);
12155 /* The default argument expression may cause implicitly defined
12156 member functions to be synthesized, which will result in garbage
12157 collection. We must treat this situation as if we were within
12158 the body of function so as to avoid collecting live data on the
12159 stack. */
12160 ++function_depth;
12161 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12162 complain, NULL_TREE,
12163 /*integral_constant_expression_p=*/false);
12164 --function_depth;
12165 pop_deferring_access_checks();
12167 finish_lambda_scope ();
12169 /* Restore the "this" pointer. */
12170 if (cfun)
12172 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12173 cp_function_chain->x_current_class_ref = saved_class_ref;
12176 if (errorcount+sorrycount > errs
12177 && (complain & tf_warning_or_error))
12178 inform (input_location,
12179 " when instantiating default argument for call to %qD", fn);
12181 /* Make sure the default argument is reasonable. */
12182 arg = check_default_argument (type, arg, complain);
12184 pop_access_scope (fn);
12186 if (arg != error_mark_node && !cp_unevaluated_operand)
12188 if (!defarg_inst)
12189 defarg_inst = tree_cache_map::create_ggc (37);
12190 defarg_inst->put (parm, arg);
12193 return arg;
12196 /* Substitute into all the default arguments for FN. */
12198 static void
12199 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12201 tree arg;
12202 tree tmpl_args;
12204 tmpl_args = DECL_TI_ARGS (fn);
12206 /* If this function is not yet instantiated, we certainly don't need
12207 its default arguments. */
12208 if (uses_template_parms (tmpl_args))
12209 return;
12210 /* Don't do this again for clones. */
12211 if (DECL_CLONED_FUNCTION_P (fn))
12212 return;
12214 int i = 0;
12215 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12216 arg;
12217 arg = TREE_CHAIN (arg), ++i)
12218 if (TREE_PURPOSE (arg))
12219 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12220 TREE_VALUE (arg),
12221 TREE_PURPOSE (arg),
12222 complain);
12225 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12227 static tree
12228 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12229 tree lambda_fntype)
12231 tree gen_tmpl, argvec;
12232 hashval_t hash = 0;
12233 tree in_decl = t;
12235 /* Nobody should be tsubst'ing into non-template functions. */
12236 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12238 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12240 /* If T is not dependent, just return it. */
12241 if (!uses_template_parms (DECL_TI_ARGS (t)))
12242 return t;
12244 /* Calculate the most general template of which R is a
12245 specialization. */
12246 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12248 /* We're substituting a lambda function under tsubst_lambda_expr but not
12249 directly from it; find the matching function we're already inside.
12250 But don't do this if T is a generic lambda with a single level of
12251 template parms, as in that case we're doing a normal instantiation. */
12252 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12253 && (!generic_lambda_fn_p (t)
12254 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12255 return enclosing_instantiation_of (t);
12257 /* Calculate the complete set of arguments used to
12258 specialize R. */
12259 argvec = tsubst_template_args (DECL_TI_ARGS
12260 (DECL_TEMPLATE_RESULT
12261 (DECL_TI_TEMPLATE (t))),
12262 args, complain, in_decl);
12263 if (argvec == error_mark_node)
12264 return error_mark_node;
12266 /* Check to see if we already have this specialization. */
12267 if (!lambda_fntype)
12269 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12270 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12271 return spec;
12274 /* We can see more levels of arguments than parameters if
12275 there was a specialization of a member template, like
12276 this:
12278 template <class T> struct S { template <class U> void f(); }
12279 template <> template <class U> void S<int>::f(U);
12281 Here, we'll be substituting into the specialization,
12282 because that's where we can find the code we actually
12283 want to generate, but we'll have enough arguments for
12284 the most general template.
12286 We also deal with the peculiar case:
12288 template <class T> struct S {
12289 template <class U> friend void f();
12291 template <class U> void f() {}
12292 template S<int>;
12293 template void f<double>();
12295 Here, the ARGS for the instantiation of will be {int,
12296 double}. But, we only need as many ARGS as there are
12297 levels of template parameters in CODE_PATTERN. We are
12298 careful not to get fooled into reducing the ARGS in
12299 situations like:
12301 template <class T> struct S { template <class U> void f(U); }
12302 template <class T> template <> void S<T>::f(int) {}
12304 which we can spot because the pattern will be a
12305 specialization in this case. */
12306 int args_depth = TMPL_ARGS_DEPTH (args);
12307 int parms_depth =
12308 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12310 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12311 args = get_innermost_template_args (args, parms_depth);
12313 else
12315 /* This special case arises when we have something like this:
12317 template <class T> struct S {
12318 friend void f<int>(int, double);
12321 Here, the DECL_TI_TEMPLATE for the friend declaration
12322 will be an IDENTIFIER_NODE. We are being called from
12323 tsubst_friend_function, and we want only to create a
12324 new decl (R) with appropriate types so that we can call
12325 determine_specialization. */
12326 gen_tmpl = NULL_TREE;
12327 argvec = NULL_TREE;
12330 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12331 : NULL_TREE);
12332 tree ctx = closure ? closure : DECL_CONTEXT (t);
12333 bool member = ctx && TYPE_P (ctx);
12335 if (member && !closure)
12336 ctx = tsubst_aggr_type (ctx, args,
12337 complain, t, /*entering_scope=*/1);
12339 tree type = (lambda_fntype ? lambda_fntype
12340 : tsubst (TREE_TYPE (t), args,
12341 complain | tf_fndecl_type, in_decl));
12342 if (type == error_mark_node)
12343 return error_mark_node;
12345 /* If we hit excessive deduction depth, the type is bogus even if
12346 it isn't error_mark_node, so don't build a decl. */
12347 if (excessive_deduction_depth)
12348 return error_mark_node;
12350 /* We do NOT check for matching decls pushed separately at this
12351 point, as they may not represent instantiations of this
12352 template, and in any case are considered separate under the
12353 discrete model. */
12354 tree r = copy_decl (t);
12355 DECL_USE_TEMPLATE (r) = 0;
12356 TREE_TYPE (r) = type;
12357 /* Clear out the mangled name and RTL for the instantiation. */
12358 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12359 SET_DECL_RTL (r, NULL);
12360 /* Leave DECL_INITIAL set on deleted instantiations. */
12361 if (!DECL_DELETED_FN (r))
12362 DECL_INITIAL (r) = NULL_TREE;
12363 DECL_CONTEXT (r) = ctx;
12365 /* OpenMP UDRs have the only argument a reference to the declared
12366 type. We want to diagnose if the declared type is a reference,
12367 which is invalid, but as references to references are usually
12368 quietly merged, diagnose it here. */
12369 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12371 tree argtype
12372 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12373 argtype = tsubst (argtype, args, complain, in_decl);
12374 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12375 error_at (DECL_SOURCE_LOCATION (t),
12376 "reference type %qT in "
12377 "%<#pragma omp declare reduction%>", argtype);
12378 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12379 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12380 argtype);
12383 if (member && DECL_CONV_FN_P (r))
12384 /* Type-conversion operator. Reconstruct the name, in
12385 case it's the name of one of the template's parameters. */
12386 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12388 tree parms = DECL_ARGUMENTS (t);
12389 if (closure)
12390 parms = DECL_CHAIN (parms);
12391 parms = tsubst (parms, args, complain, t);
12392 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12393 DECL_CONTEXT (parm) = r;
12394 if (closure)
12396 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12397 DECL_CHAIN (tparm) = parms;
12398 parms = tparm;
12400 DECL_ARGUMENTS (r) = parms;
12401 DECL_RESULT (r) = NULL_TREE;
12403 TREE_STATIC (r) = 0;
12404 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12405 DECL_EXTERNAL (r) = 1;
12406 /* If this is an instantiation of a function with internal
12407 linkage, we already know what object file linkage will be
12408 assigned to the instantiation. */
12409 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12410 DECL_DEFER_OUTPUT (r) = 0;
12411 DECL_CHAIN (r) = NULL_TREE;
12412 DECL_PENDING_INLINE_INFO (r) = 0;
12413 DECL_PENDING_INLINE_P (r) = 0;
12414 DECL_SAVED_TREE (r) = NULL_TREE;
12415 DECL_STRUCT_FUNCTION (r) = NULL;
12416 TREE_USED (r) = 0;
12417 /* We'll re-clone as appropriate in instantiate_template. */
12418 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12420 /* If we aren't complaining now, return on error before we register
12421 the specialization so that we'll complain eventually. */
12422 if ((complain & tf_error) == 0
12423 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12424 && !grok_op_properties (r, /*complain=*/false))
12425 return error_mark_node;
12427 /* When instantiating a constrained member, substitute
12428 into the constraints to create a new constraint. */
12429 if (tree ci = get_constraints (t))
12430 if (member)
12432 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12433 set_constraints (r, ci);
12436 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12437 this in the special friend case mentioned above where
12438 GEN_TMPL is NULL. */
12439 if (gen_tmpl && !closure)
12441 DECL_TEMPLATE_INFO (r)
12442 = build_template_info (gen_tmpl, argvec);
12443 SET_DECL_IMPLICIT_INSTANTIATION (r);
12445 tree new_r
12446 = register_specialization (r, gen_tmpl, argvec, false, hash);
12447 if (new_r != r)
12448 /* We instantiated this while substituting into
12449 the type earlier (template/friend54.C). */
12450 return new_r;
12452 /* We're not supposed to instantiate default arguments
12453 until they are called, for a template. But, for a
12454 declaration like:
12456 template <class T> void f ()
12457 { extern void g(int i = T()); }
12459 we should do the substitution when the template is
12460 instantiated. We handle the member function case in
12461 instantiate_class_template since the default arguments
12462 might refer to other members of the class. */
12463 if (!member
12464 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12465 && !uses_template_parms (argvec))
12466 tsubst_default_arguments (r, complain);
12468 else
12469 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12471 /* Copy the list of befriending classes. */
12472 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12473 *friends;
12474 friends = &TREE_CHAIN (*friends))
12476 *friends = copy_node (*friends);
12477 TREE_VALUE (*friends)
12478 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12481 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12483 maybe_retrofit_in_chrg (r);
12484 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12485 return error_mark_node;
12486 /* If this is an instantiation of a member template, clone it.
12487 If it isn't, that'll be handled by
12488 clone_constructors_and_destructors. */
12489 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12490 clone_function_decl (r, /*update_methods=*/false);
12492 else if ((complain & tf_error) != 0
12493 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12494 && !grok_op_properties (r, /*complain=*/true))
12495 return error_mark_node;
12497 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12498 SET_DECL_FRIEND_CONTEXT (r,
12499 tsubst (DECL_FRIEND_CONTEXT (t),
12500 args, complain, in_decl));
12502 /* Possibly limit visibility based on template args. */
12503 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12504 if (DECL_VISIBILITY_SPECIFIED (t))
12506 DECL_VISIBILITY_SPECIFIED (r) = 0;
12507 DECL_ATTRIBUTES (r)
12508 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12510 determine_visibility (r);
12511 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12512 && !processing_template_decl)
12513 defaulted_late_check (r);
12515 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12516 args, complain, in_decl);
12517 return r;
12520 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12522 static tree
12523 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12524 tree lambda_fntype)
12526 /* We can get here when processing a member function template,
12527 member class template, or template template parameter. */
12528 tree decl = DECL_TEMPLATE_RESULT (t);
12529 tree in_decl = t;
12530 tree spec;
12531 tree tmpl_args;
12532 tree full_args;
12533 tree r;
12534 hashval_t hash = 0;
12536 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12538 /* Template template parameter is treated here. */
12539 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12540 if (new_type == error_mark_node)
12541 r = error_mark_node;
12542 /* If we get a real template back, return it. This can happen in
12543 the context of most_specialized_partial_spec. */
12544 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12545 r = new_type;
12546 else
12547 /* The new TEMPLATE_DECL was built in
12548 reduce_template_parm_level. */
12549 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12550 return r;
12553 if (!lambda_fntype)
12555 /* We might already have an instance of this template.
12556 The ARGS are for the surrounding class type, so the
12557 full args contain the tsubst'd args for the context,
12558 plus the innermost args from the template decl. */
12559 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12560 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12561 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12562 /* Because this is a template, the arguments will still be
12563 dependent, even after substitution. If
12564 PROCESSING_TEMPLATE_DECL is not set, the dependency
12565 predicates will short-circuit. */
12566 ++processing_template_decl;
12567 full_args = tsubst_template_args (tmpl_args, args,
12568 complain, in_decl);
12569 --processing_template_decl;
12570 if (full_args == error_mark_node)
12571 return error_mark_node;
12573 /* If this is a default template template argument,
12574 tsubst might not have changed anything. */
12575 if (full_args == tmpl_args)
12576 return t;
12578 hash = hash_tmpl_and_args (t, full_args);
12579 spec = retrieve_specialization (t, full_args, hash);
12580 if (spec != NULL_TREE)
12581 return spec;
12584 /* Make a new template decl. It will be similar to the
12585 original, but will record the current template arguments.
12586 We also create a new function declaration, which is just
12587 like the old one, but points to this new template, rather
12588 than the old one. */
12589 r = copy_decl (t);
12590 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12591 DECL_CHAIN (r) = NULL_TREE;
12593 // Build new template info linking to the original template decl.
12594 if (!lambda_fntype)
12596 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12597 SET_DECL_IMPLICIT_INSTANTIATION (r);
12599 else
12600 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12602 /* The template parameters for this new template are all the
12603 template parameters for the old template, except the
12604 outermost level of parameters. */
12605 DECL_TEMPLATE_PARMS (r)
12606 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12607 complain);
12609 if (TREE_CODE (decl) == TYPE_DECL
12610 && !TYPE_DECL_ALIAS_P (decl))
12612 tree new_type;
12613 ++processing_template_decl;
12614 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12615 --processing_template_decl;
12616 if (new_type == error_mark_node)
12617 return error_mark_node;
12619 TREE_TYPE (r) = new_type;
12620 /* For a partial specialization, we need to keep pointing to
12621 the primary template. */
12622 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12623 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12624 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12625 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12626 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12628 else
12630 tree new_decl;
12631 ++processing_template_decl;
12632 if (TREE_CODE (decl) == FUNCTION_DECL)
12633 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12634 else
12635 new_decl = tsubst (decl, args, complain, in_decl);
12636 --processing_template_decl;
12637 if (new_decl == error_mark_node)
12638 return error_mark_node;
12640 DECL_TEMPLATE_RESULT (r) = new_decl;
12641 TREE_TYPE (r) = TREE_TYPE (new_decl);
12642 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12643 if (lambda_fntype)
12645 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12646 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12648 else
12650 DECL_TI_TEMPLATE (new_decl) = r;
12651 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12655 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12656 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12658 if (PRIMARY_TEMPLATE_P (t))
12659 DECL_PRIMARY_TEMPLATE (r) = r;
12661 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12662 && !lambda_fntype)
12663 /* Record this non-type partial instantiation. */
12664 register_specialization (r, t,
12665 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12666 false, hash);
12668 return r;
12671 /* True if FN is the op() for a lambda in an uninstantiated template. */
12673 bool
12674 lambda_fn_in_template_p (tree fn)
12676 if (!fn || !LAMBDA_FUNCTION_P (fn))
12677 return false;
12678 tree closure = DECL_CONTEXT (fn);
12679 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12682 /* We're instantiating a variable from template function TCTX. Return the
12683 corresponding current enclosing scope. This gets complicated because lambda
12684 functions in templates are regenerated rather than instantiated, but generic
12685 lambda functions are subsequently instantiated. */
12687 static tree
12688 enclosing_instantiation_of (tree otctx)
12690 tree tctx = otctx;
12691 tree fn = current_function_decl;
12692 int lambda_count = 0;
12694 for (; tctx && lambda_fn_in_template_p (tctx);
12695 tctx = decl_function_context (tctx))
12696 ++lambda_count;
12697 for (; fn; fn = decl_function_context (fn))
12699 tree ofn = fn;
12700 int flambda_count = 0;
12701 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12702 fn = decl_function_context (fn))
12703 ++flambda_count;
12704 if (DECL_TEMPLATE_INFO (fn)
12705 ? most_general_template (fn) != most_general_template (tctx)
12706 : fn != tctx)
12707 continue;
12708 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12709 || DECL_CONV_FN_P (ofn));
12710 return ofn;
12712 gcc_unreachable ();
12715 /* Substitute the ARGS into the T, which is a _DECL. Return the
12716 result of the substitution. Issue error and warning messages under
12717 control of COMPLAIN. */
12719 static tree
12720 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12722 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12723 location_t saved_loc;
12724 tree r = NULL_TREE;
12725 tree in_decl = t;
12726 hashval_t hash = 0;
12728 /* Set the filename and linenumber to improve error-reporting. */
12729 saved_loc = input_location;
12730 input_location = DECL_SOURCE_LOCATION (t);
12732 switch (TREE_CODE (t))
12734 case TEMPLATE_DECL:
12735 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12736 break;
12738 case FUNCTION_DECL:
12739 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12740 break;
12742 case PARM_DECL:
12744 tree type = NULL_TREE;
12745 int i, len = 1;
12746 tree expanded_types = NULL_TREE;
12747 tree prev_r = NULL_TREE;
12748 tree first_r = NULL_TREE;
12750 if (DECL_PACK_P (t))
12752 /* If there is a local specialization that isn't a
12753 parameter pack, it means that we're doing a "simple"
12754 substitution from inside tsubst_pack_expansion. Just
12755 return the local specialization (which will be a single
12756 parm). */
12757 tree spec = retrieve_local_specialization (t);
12758 if (spec
12759 && TREE_CODE (spec) == PARM_DECL
12760 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12761 RETURN (spec);
12763 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12764 the parameters in this function parameter pack. */
12765 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12766 complain, in_decl);
12767 if (TREE_CODE (expanded_types) == TREE_VEC)
12769 len = TREE_VEC_LENGTH (expanded_types);
12771 /* Zero-length parameter packs are boring. Just substitute
12772 into the chain. */
12773 if (len == 0)
12774 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12775 TREE_CHAIN (t)));
12777 else
12779 /* All we did was update the type. Make a note of that. */
12780 type = expanded_types;
12781 expanded_types = NULL_TREE;
12785 /* Loop through all of the parameters we'll build. When T is
12786 a function parameter pack, LEN is the number of expanded
12787 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12788 r = NULL_TREE;
12789 for (i = 0; i < len; ++i)
12791 prev_r = r;
12792 r = copy_node (t);
12793 if (DECL_TEMPLATE_PARM_P (t))
12794 SET_DECL_TEMPLATE_PARM_P (r);
12796 if (expanded_types)
12797 /* We're on the Ith parameter of the function parameter
12798 pack. */
12800 /* Get the Ith type. */
12801 type = TREE_VEC_ELT (expanded_types, i);
12803 /* Rename the parameter to include the index. */
12804 DECL_NAME (r)
12805 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12807 else if (!type)
12808 /* We're dealing with a normal parameter. */
12809 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12811 type = type_decays_to (type);
12812 TREE_TYPE (r) = type;
12813 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12815 if (DECL_INITIAL (r))
12817 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12818 DECL_INITIAL (r) = TREE_TYPE (r);
12819 else
12820 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12821 complain, in_decl);
12824 DECL_CONTEXT (r) = NULL_TREE;
12826 if (!DECL_TEMPLATE_PARM_P (r))
12827 DECL_ARG_TYPE (r) = type_passed_as (type);
12829 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12830 args, complain, in_decl);
12832 /* Keep track of the first new parameter we
12833 generate. That's what will be returned to the
12834 caller. */
12835 if (!first_r)
12836 first_r = r;
12838 /* Build a proper chain of parameters when substituting
12839 into a function parameter pack. */
12840 if (prev_r)
12841 DECL_CHAIN (prev_r) = r;
12844 /* If cp_unevaluated_operand is set, we're just looking for a
12845 single dummy parameter, so don't keep going. */
12846 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12847 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12848 complain, DECL_CHAIN (t));
12850 /* FIRST_R contains the start of the chain we've built. */
12851 r = first_r;
12853 break;
12855 case FIELD_DECL:
12857 tree type = NULL_TREE;
12858 tree vec = NULL_TREE;
12859 tree expanded_types = NULL_TREE;
12860 int len = 1;
12862 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12864 /* This field is a lambda capture pack. Return a TREE_VEC of
12865 the expanded fields to instantiate_class_template_1. */
12866 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12867 complain, in_decl);
12868 if (TREE_CODE (expanded_types) == TREE_VEC)
12870 len = TREE_VEC_LENGTH (expanded_types);
12871 vec = make_tree_vec (len);
12873 else
12875 /* All we did was update the type. Make a note of that. */
12876 type = expanded_types;
12877 expanded_types = NULL_TREE;
12881 for (int i = 0; i < len; ++i)
12883 r = copy_decl (t);
12884 if (expanded_types)
12886 type = TREE_VEC_ELT (expanded_types, i);
12887 DECL_NAME (r)
12888 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12890 else if (!type)
12891 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12893 if (type == error_mark_node)
12894 RETURN (error_mark_node);
12895 TREE_TYPE (r) = type;
12896 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12898 if (DECL_C_BIT_FIELD (r))
12899 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12900 number of bits. */
12901 DECL_BIT_FIELD_REPRESENTATIVE (r)
12902 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12903 complain, in_decl,
12904 /*integral_constant_expression_p=*/true);
12905 if (DECL_INITIAL (t))
12907 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12908 NSDMI in perform_member_init. Still set DECL_INITIAL
12909 so that we know there is one. */
12910 DECL_INITIAL (r) = void_node;
12911 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12912 retrofit_lang_decl (r);
12913 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12915 /* We don't have to set DECL_CONTEXT here; it is set by
12916 finish_member_declaration. */
12917 DECL_CHAIN (r) = NULL_TREE;
12919 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12920 args, complain, in_decl);
12922 if (vec)
12923 TREE_VEC_ELT (vec, i) = r;
12926 if (vec)
12927 r = vec;
12929 break;
12931 case USING_DECL:
12932 /* We reach here only for member using decls. We also need to check
12933 uses_template_parms because DECL_DEPENDENT_P is not set for a
12934 using-declaration that designates a member of the current
12935 instantiation (c++/53549). */
12936 if (DECL_DEPENDENT_P (t)
12937 || uses_template_parms (USING_DECL_SCOPE (t)))
12939 tree scope = USING_DECL_SCOPE (t);
12940 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12941 if (PACK_EXPANSION_P (scope))
12943 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12944 int len = TREE_VEC_LENGTH (vec);
12945 r = make_tree_vec (len);
12946 for (int i = 0; i < len; ++i)
12948 tree escope = TREE_VEC_ELT (vec, i);
12949 tree elt = do_class_using_decl (escope, name);
12950 if (!elt)
12952 r = error_mark_node;
12953 break;
12955 else
12957 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12958 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12960 TREE_VEC_ELT (r, i) = elt;
12963 else
12965 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12966 complain, in_decl);
12967 r = do_class_using_decl (inst_scope, name);
12968 if (!r)
12969 r = error_mark_node;
12970 else
12972 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12973 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12977 else
12979 r = copy_node (t);
12980 DECL_CHAIN (r) = NULL_TREE;
12982 break;
12984 case TYPE_DECL:
12985 case VAR_DECL:
12987 tree argvec = NULL_TREE;
12988 tree gen_tmpl = NULL_TREE;
12989 tree spec;
12990 tree tmpl = NULL_TREE;
12991 tree ctx;
12992 tree type = NULL_TREE;
12993 bool local_p;
12995 if (TREE_TYPE (t) == error_mark_node)
12996 RETURN (error_mark_node);
12998 if (TREE_CODE (t) == TYPE_DECL
12999 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13001 /* If this is the canonical decl, we don't have to
13002 mess with instantiations, and often we can't (for
13003 typename, template type parms and such). Note that
13004 TYPE_NAME is not correct for the above test if
13005 we've copied the type for a typedef. */
13006 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13007 if (type == error_mark_node)
13008 RETURN (error_mark_node);
13009 r = TYPE_NAME (type);
13010 break;
13013 /* Check to see if we already have the specialization we
13014 need. */
13015 spec = NULL_TREE;
13016 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13018 /* T is a static data member or namespace-scope entity.
13019 We have to substitute into namespace-scope variables
13020 (not just variable templates) because of cases like:
13022 template <class T> void f() { extern T t; }
13024 where the entity referenced is not known until
13025 instantiation time. */
13026 local_p = false;
13027 ctx = DECL_CONTEXT (t);
13028 if (DECL_CLASS_SCOPE_P (t))
13030 ctx = tsubst_aggr_type (ctx, args,
13031 complain,
13032 in_decl, /*entering_scope=*/1);
13033 /* If CTX is unchanged, then T is in fact the
13034 specialization we want. That situation occurs when
13035 referencing a static data member within in its own
13036 class. We can use pointer equality, rather than
13037 same_type_p, because DECL_CONTEXT is always
13038 canonical... */
13039 if (ctx == DECL_CONTEXT (t)
13040 /* ... unless T is a member template; in which
13041 case our caller can be willing to create a
13042 specialization of that template represented
13043 by T. */
13044 && !(DECL_TI_TEMPLATE (t)
13045 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13046 spec = t;
13049 if (!spec)
13051 tmpl = DECL_TI_TEMPLATE (t);
13052 gen_tmpl = most_general_template (tmpl);
13053 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13054 if (argvec != error_mark_node)
13055 argvec = (coerce_innermost_template_parms
13056 (DECL_TEMPLATE_PARMS (gen_tmpl),
13057 argvec, t, complain,
13058 /*all*/true, /*defarg*/true));
13059 if (argvec == error_mark_node)
13060 RETURN (error_mark_node);
13061 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13062 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13065 else
13067 /* A local variable. */
13068 local_p = true;
13069 /* Subsequent calls to pushdecl will fill this in. */
13070 ctx = NULL_TREE;
13071 /* Unless this is a reference to a static variable from an
13072 enclosing function, in which case we need to fill it in now. */
13073 if (TREE_STATIC (t))
13075 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13076 if (fn != current_function_decl)
13077 ctx = fn;
13079 spec = retrieve_local_specialization (t);
13081 /* If we already have the specialization we need, there is
13082 nothing more to do. */
13083 if (spec)
13085 r = spec;
13086 break;
13089 /* Create a new node for the specialization we need. */
13090 r = copy_decl (t);
13091 if (type == NULL_TREE)
13093 if (is_typedef_decl (t))
13094 type = DECL_ORIGINAL_TYPE (t);
13095 else
13096 type = TREE_TYPE (t);
13097 if (VAR_P (t)
13098 && VAR_HAD_UNKNOWN_BOUND (t)
13099 && type != error_mark_node)
13100 type = strip_array_domain (type);
13101 tree sub_args = args;
13102 if (tree auto_node = type_uses_auto (type))
13104 /* Mask off any template args past the variable's context so we
13105 don't replace the auto with an unrelated argument. */
13106 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13107 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13108 if (extra > 0)
13109 /* This should never happen with the new lambda instantiation
13110 model, but keep the handling just in case. */
13111 gcc_assert (!CHECKING_P),
13112 sub_args = strip_innermost_template_args (args, extra);
13114 type = tsubst (type, sub_args, complain, in_decl);
13116 if (VAR_P (r))
13118 /* Even if the original location is out of scope, the
13119 newly substituted one is not. */
13120 DECL_DEAD_FOR_LOCAL (r) = 0;
13121 DECL_INITIALIZED_P (r) = 0;
13122 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13123 if (type == error_mark_node)
13124 RETURN (error_mark_node);
13125 if (TREE_CODE (type) == FUNCTION_TYPE)
13127 /* It may seem that this case cannot occur, since:
13129 typedef void f();
13130 void g() { f x; }
13132 declares a function, not a variable. However:
13134 typedef void f();
13135 template <typename T> void g() { T t; }
13136 template void g<f>();
13138 is an attempt to declare a variable with function
13139 type. */
13140 error ("variable %qD has function type",
13141 /* R is not yet sufficiently initialized, so we
13142 just use its name. */
13143 DECL_NAME (r));
13144 RETURN (error_mark_node);
13146 type = complete_type (type);
13147 /* Wait until cp_finish_decl to set this again, to handle
13148 circular dependency (template/instantiate6.C). */
13149 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13150 type = check_var_type (DECL_NAME (r), type);
13152 if (DECL_HAS_VALUE_EXPR_P (t))
13154 tree ve = DECL_VALUE_EXPR (t);
13155 ve = tsubst_expr (ve, args, complain, in_decl,
13156 /*constant_expression_p=*/false);
13157 if (REFERENCE_REF_P (ve))
13159 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13160 ve = TREE_OPERAND (ve, 0);
13162 SET_DECL_VALUE_EXPR (r, ve);
13164 if (CP_DECL_THREAD_LOCAL_P (r)
13165 && !processing_template_decl)
13166 set_decl_tls_model (r, decl_default_tls_model (r));
13168 else if (DECL_SELF_REFERENCE_P (t))
13169 SET_DECL_SELF_REFERENCE_P (r);
13170 TREE_TYPE (r) = type;
13171 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13172 DECL_CONTEXT (r) = ctx;
13173 /* Clear out the mangled name and RTL for the instantiation. */
13174 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13175 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13176 SET_DECL_RTL (r, NULL);
13177 /* The initializer must not be expanded until it is required;
13178 see [temp.inst]. */
13179 DECL_INITIAL (r) = NULL_TREE;
13180 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13181 if (VAR_P (r))
13183 if (DECL_LANG_SPECIFIC (r))
13184 SET_DECL_DEPENDENT_INIT_P (r, false);
13186 SET_DECL_MODE (r, VOIDmode);
13188 /* Possibly limit visibility based on template args. */
13189 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13190 if (DECL_VISIBILITY_SPECIFIED (t))
13192 DECL_VISIBILITY_SPECIFIED (r) = 0;
13193 DECL_ATTRIBUTES (r)
13194 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13196 determine_visibility (r);
13199 if (!local_p)
13201 /* A static data member declaration is always marked
13202 external when it is declared in-class, even if an
13203 initializer is present. We mimic the non-template
13204 processing here. */
13205 DECL_EXTERNAL (r) = 1;
13206 if (DECL_NAMESPACE_SCOPE_P (t))
13207 DECL_NOT_REALLY_EXTERN (r) = 1;
13209 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13210 SET_DECL_IMPLICIT_INSTANTIATION (r);
13211 register_specialization (r, gen_tmpl, argvec, false, hash);
13213 else
13215 if (DECL_LANG_SPECIFIC (r))
13216 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13217 if (!cp_unevaluated_operand)
13218 register_local_specialization (r, t);
13221 DECL_CHAIN (r) = NULL_TREE;
13223 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13224 /*flags=*/0,
13225 args, complain, in_decl);
13227 /* Preserve a typedef that names a type. */
13228 if (is_typedef_decl (r) && type != error_mark_node)
13230 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13231 set_underlying_type (r);
13232 if (TYPE_DECL_ALIAS_P (r))
13233 /* An alias template specialization can be dependent
13234 even if its underlying type is not. */
13235 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13238 layout_decl (r, 0);
13240 break;
13242 default:
13243 gcc_unreachable ();
13245 #undef RETURN
13247 out:
13248 /* Restore the file and line information. */
13249 input_location = saved_loc;
13251 return r;
13254 /* Substitute into the ARG_TYPES of a function type.
13255 If END is a TREE_CHAIN, leave it and any following types
13256 un-substituted. */
13258 static tree
13259 tsubst_arg_types (tree arg_types,
13260 tree args,
13261 tree end,
13262 tsubst_flags_t complain,
13263 tree in_decl)
13265 tree remaining_arg_types;
13266 tree type = NULL_TREE;
13267 int i = 1;
13268 tree expanded_args = NULL_TREE;
13269 tree default_arg;
13271 if (!arg_types || arg_types == void_list_node || arg_types == end)
13272 return arg_types;
13274 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13275 args, end, complain, in_decl);
13276 if (remaining_arg_types == error_mark_node)
13277 return error_mark_node;
13279 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13281 /* For a pack expansion, perform substitution on the
13282 entire expression. Later on, we'll handle the arguments
13283 one-by-one. */
13284 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13285 args, complain, in_decl);
13287 if (TREE_CODE (expanded_args) == TREE_VEC)
13288 /* So that we'll spin through the parameters, one by one. */
13289 i = TREE_VEC_LENGTH (expanded_args);
13290 else
13292 /* We only partially substituted into the parameter
13293 pack. Our type is TYPE_PACK_EXPANSION. */
13294 type = expanded_args;
13295 expanded_args = NULL_TREE;
13299 while (i > 0) {
13300 --i;
13302 if (expanded_args)
13303 type = TREE_VEC_ELT (expanded_args, i);
13304 else if (!type)
13305 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13307 if (type == error_mark_node)
13308 return error_mark_node;
13309 if (VOID_TYPE_P (type))
13311 if (complain & tf_error)
13313 error ("invalid parameter type %qT", type);
13314 if (in_decl)
13315 error ("in declaration %q+D", in_decl);
13317 return error_mark_node;
13319 /* DR 657. */
13320 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13321 return error_mark_node;
13323 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13324 top-level qualifiers as required. */
13325 type = cv_unqualified (type_decays_to (type));
13327 /* We do not substitute into default arguments here. The standard
13328 mandates that they be instantiated only when needed, which is
13329 done in build_over_call. */
13330 default_arg = TREE_PURPOSE (arg_types);
13332 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13333 since the new op() won't have any associated template arguments for us
13334 to refer to later. */
13335 if (lambda_fn_in_template_p (in_decl))
13336 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13337 false/*fn*/, false/*constexpr*/);
13339 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13341 /* We've instantiated a template before its default arguments
13342 have been parsed. This can happen for a nested template
13343 class, and is not an error unless we require the default
13344 argument in a call of this function. */
13345 remaining_arg_types =
13346 tree_cons (default_arg, type, remaining_arg_types);
13347 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13349 else
13350 remaining_arg_types =
13351 hash_tree_cons (default_arg, type, remaining_arg_types);
13354 return remaining_arg_types;
13357 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13358 *not* handle the exception-specification for FNTYPE, because the
13359 initial substitution of explicitly provided template parameters
13360 during argument deduction forbids substitution into the
13361 exception-specification:
13363 [temp.deduct]
13365 All references in the function type of the function template to the
13366 corresponding template parameters are replaced by the specified tem-
13367 plate argument values. If a substitution in a template parameter or
13368 in the function type of the function template results in an invalid
13369 type, type deduction fails. [Note: The equivalent substitution in
13370 exception specifications is done only when the function is instanti-
13371 ated, at which point a program is ill-formed if the substitution
13372 results in an invalid type.] */
13374 static tree
13375 tsubst_function_type (tree t,
13376 tree args,
13377 tsubst_flags_t complain,
13378 tree in_decl)
13380 tree return_type;
13381 tree arg_types = NULL_TREE;
13382 tree fntype;
13384 /* The TYPE_CONTEXT is not used for function/method types. */
13385 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13387 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13388 failure. */
13389 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13391 if (late_return_type_p)
13393 /* Substitute the argument types. */
13394 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13395 complain, in_decl);
13396 if (arg_types == error_mark_node)
13397 return error_mark_node;
13399 tree save_ccp = current_class_ptr;
13400 tree save_ccr = current_class_ref;
13401 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13402 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13403 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13404 if (do_inject)
13406 /* DR 1207: 'this' is in scope in the trailing return type. */
13407 inject_this_parameter (this_type, cp_type_quals (this_type));
13410 /* Substitute the return type. */
13411 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13413 if (do_inject)
13415 current_class_ptr = save_ccp;
13416 current_class_ref = save_ccr;
13419 else
13420 /* Substitute the return type. */
13421 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13423 if (return_type == error_mark_node)
13424 return error_mark_node;
13425 /* DR 486 clarifies that creation of a function type with an
13426 invalid return type is a deduction failure. */
13427 if (TREE_CODE (return_type) == ARRAY_TYPE
13428 || TREE_CODE (return_type) == FUNCTION_TYPE)
13430 if (complain & tf_error)
13432 if (TREE_CODE (return_type) == ARRAY_TYPE)
13433 error ("function returning an array");
13434 else
13435 error ("function returning a function");
13437 return error_mark_node;
13439 /* And DR 657. */
13440 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13441 return error_mark_node;
13443 if (!late_return_type_p)
13445 /* Substitute the argument types. */
13446 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13447 complain, in_decl);
13448 if (arg_types == error_mark_node)
13449 return error_mark_node;
13452 /* Construct a new type node and return it. */
13453 if (TREE_CODE (t) == FUNCTION_TYPE)
13455 fntype = build_function_type (return_type, arg_types);
13456 fntype = apply_memfn_quals (fntype,
13457 type_memfn_quals (t),
13458 type_memfn_rqual (t));
13460 else
13462 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13463 /* Don't pick up extra function qualifiers from the basetype. */
13464 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13465 if (! MAYBE_CLASS_TYPE_P (r))
13467 /* [temp.deduct]
13469 Type deduction may fail for any of the following
13470 reasons:
13472 -- Attempting to create "pointer to member of T" when T
13473 is not a class type. */
13474 if (complain & tf_error)
13475 error ("creating pointer to member function of non-class type %qT",
13477 return error_mark_node;
13480 fntype = build_method_type_directly (r, return_type,
13481 TREE_CHAIN (arg_types));
13482 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13484 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13486 if (late_return_type_p)
13487 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13489 return fntype;
13492 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13493 ARGS into that specification, and return the substituted
13494 specification. If there is no specification, return NULL_TREE. */
13496 static tree
13497 tsubst_exception_specification (tree fntype,
13498 tree args,
13499 tsubst_flags_t complain,
13500 tree in_decl,
13501 bool defer_ok)
13503 tree specs;
13504 tree new_specs;
13506 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13507 new_specs = NULL_TREE;
13508 if (specs && TREE_PURPOSE (specs))
13510 /* A noexcept-specifier. */
13511 tree expr = TREE_PURPOSE (specs);
13512 if (TREE_CODE (expr) == INTEGER_CST)
13513 new_specs = expr;
13514 else if (defer_ok)
13516 /* Defer instantiation of noexcept-specifiers to avoid
13517 excessive instantiations (c++/49107). */
13518 new_specs = make_node (DEFERRED_NOEXCEPT);
13519 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13521 /* We already partially instantiated this member template,
13522 so combine the new args with the old. */
13523 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13524 = DEFERRED_NOEXCEPT_PATTERN (expr);
13525 DEFERRED_NOEXCEPT_ARGS (new_specs)
13526 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13528 else
13530 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13531 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13534 else
13535 new_specs = tsubst_copy_and_build
13536 (expr, args, complain, in_decl, /*function_p=*/false,
13537 /*integral_constant_expression_p=*/true);
13538 new_specs = build_noexcept_spec (new_specs, complain);
13540 else if (specs)
13542 if (! TREE_VALUE (specs))
13543 new_specs = specs;
13544 else
13545 while (specs)
13547 tree spec;
13548 int i, len = 1;
13549 tree expanded_specs = NULL_TREE;
13551 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13553 /* Expand the pack expansion type. */
13554 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13555 args, complain,
13556 in_decl);
13558 if (expanded_specs == error_mark_node)
13559 return error_mark_node;
13560 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13561 len = TREE_VEC_LENGTH (expanded_specs);
13562 else
13564 /* We're substituting into a member template, so
13565 we got a TYPE_PACK_EXPANSION back. Add that
13566 expansion and move on. */
13567 gcc_assert (TREE_CODE (expanded_specs)
13568 == TYPE_PACK_EXPANSION);
13569 new_specs = add_exception_specifier (new_specs,
13570 expanded_specs,
13571 complain);
13572 specs = TREE_CHAIN (specs);
13573 continue;
13577 for (i = 0; i < len; ++i)
13579 if (expanded_specs)
13580 spec = TREE_VEC_ELT (expanded_specs, i);
13581 else
13582 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13583 if (spec == error_mark_node)
13584 return spec;
13585 new_specs = add_exception_specifier (new_specs, spec,
13586 complain);
13589 specs = TREE_CHAIN (specs);
13592 return new_specs;
13595 /* Take the tree structure T and replace template parameters used
13596 therein with the argument vector ARGS. IN_DECL is an associated
13597 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13598 Issue error and warning messages under control of COMPLAIN. Note
13599 that we must be relatively non-tolerant of extensions here, in
13600 order to preserve conformance; if we allow substitutions that
13601 should not be allowed, we may allow argument deductions that should
13602 not succeed, and therefore report ambiguous overload situations
13603 where there are none. In theory, we could allow the substitution,
13604 but indicate that it should have failed, and allow our caller to
13605 make sure that the right thing happens, but we don't try to do this
13606 yet.
13608 This function is used for dealing with types, decls and the like;
13609 for expressions, use tsubst_expr or tsubst_copy. */
13611 tree
13612 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13614 enum tree_code code;
13615 tree type, r = NULL_TREE;
13617 if (t == NULL_TREE || t == error_mark_node
13618 || t == integer_type_node
13619 || t == void_type_node
13620 || t == char_type_node
13621 || t == unknown_type_node
13622 || TREE_CODE (t) == NAMESPACE_DECL
13623 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13624 return t;
13626 if (DECL_P (t))
13627 return tsubst_decl (t, args, complain);
13629 if (args == NULL_TREE)
13630 return t;
13632 code = TREE_CODE (t);
13634 if (code == IDENTIFIER_NODE)
13635 type = IDENTIFIER_TYPE_VALUE (t);
13636 else
13637 type = TREE_TYPE (t);
13639 gcc_assert (type != unknown_type_node);
13641 /* Reuse typedefs. We need to do this to handle dependent attributes,
13642 such as attribute aligned. */
13643 if (TYPE_P (t)
13644 && typedef_variant_p (t))
13646 tree decl = TYPE_NAME (t);
13648 if (alias_template_specialization_p (t))
13650 /* DECL represents an alias template and we want to
13651 instantiate it. */
13652 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13653 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13654 r = instantiate_alias_template (tmpl, gen_args, complain);
13656 else if (DECL_CLASS_SCOPE_P (decl)
13657 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13658 && uses_template_parms (DECL_CONTEXT (decl)))
13660 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13661 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13662 r = retrieve_specialization (tmpl, gen_args, 0);
13664 else if (DECL_FUNCTION_SCOPE_P (decl)
13665 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13666 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13667 r = retrieve_local_specialization (decl);
13668 else
13669 /* The typedef is from a non-template context. */
13670 return t;
13672 if (r)
13674 r = TREE_TYPE (r);
13675 r = cp_build_qualified_type_real
13676 (r, cp_type_quals (t) | cp_type_quals (r),
13677 complain | tf_ignore_bad_quals);
13678 return r;
13680 else
13682 /* We don't have an instantiation yet, so drop the typedef. */
13683 int quals = cp_type_quals (t);
13684 t = DECL_ORIGINAL_TYPE (decl);
13685 t = cp_build_qualified_type_real (t, quals,
13686 complain | tf_ignore_bad_quals);
13690 bool fndecl_type = (complain & tf_fndecl_type);
13691 complain &= ~tf_fndecl_type;
13693 if (type
13694 && code != TYPENAME_TYPE
13695 && code != TEMPLATE_TYPE_PARM
13696 && code != TEMPLATE_PARM_INDEX
13697 && code != IDENTIFIER_NODE
13698 && code != FUNCTION_TYPE
13699 && code != METHOD_TYPE)
13700 type = tsubst (type, args, complain, in_decl);
13701 if (type == error_mark_node)
13702 return error_mark_node;
13704 switch (code)
13706 case RECORD_TYPE:
13707 case UNION_TYPE:
13708 case ENUMERAL_TYPE:
13709 return tsubst_aggr_type (t, args, complain, in_decl,
13710 /*entering_scope=*/0);
13712 case ERROR_MARK:
13713 case IDENTIFIER_NODE:
13714 case VOID_TYPE:
13715 case REAL_TYPE:
13716 case COMPLEX_TYPE:
13717 case VECTOR_TYPE:
13718 case BOOLEAN_TYPE:
13719 case NULLPTR_TYPE:
13720 case LANG_TYPE:
13721 return t;
13723 case INTEGER_TYPE:
13724 if (t == integer_type_node)
13725 return t;
13727 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13728 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13729 return t;
13732 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13734 max = tsubst_expr (omax, args, complain, in_decl,
13735 /*integral_constant_expression_p=*/false);
13737 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13738 needed. */
13739 if (TREE_CODE (max) == NOP_EXPR
13740 && TREE_SIDE_EFFECTS (omax)
13741 && !TREE_TYPE (max))
13742 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13744 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13745 with TREE_SIDE_EFFECTS that indicates this is not an integral
13746 constant expression. */
13747 if (processing_template_decl
13748 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13750 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13751 TREE_SIDE_EFFECTS (max) = 1;
13754 return compute_array_index_type (NULL_TREE, max, complain);
13757 case TEMPLATE_TYPE_PARM:
13758 case TEMPLATE_TEMPLATE_PARM:
13759 case BOUND_TEMPLATE_TEMPLATE_PARM:
13760 case TEMPLATE_PARM_INDEX:
13762 int idx;
13763 int level;
13764 int levels;
13765 tree arg = NULL_TREE;
13767 /* Early in template argument deduction substitution, we don't
13768 want to reduce the level of 'auto', or it will be confused
13769 with a normal template parm in subsequent deduction. */
13770 if (is_auto (t) && (complain & tf_partial))
13771 return t;
13773 r = NULL_TREE;
13775 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13776 template_parm_level_and_index (t, &level, &idx);
13778 levels = TMPL_ARGS_DEPTH (args);
13779 if (level <= levels
13780 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13782 arg = TMPL_ARG (args, level, idx);
13784 /* See through ARGUMENT_PACK_SELECT arguments. */
13785 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13786 arg = argument_pack_select_arg (arg);
13789 if (arg == error_mark_node)
13790 return error_mark_node;
13791 else if (arg != NULL_TREE)
13793 if (ARGUMENT_PACK_P (arg))
13794 /* If ARG is an argument pack, we don't actually want to
13795 perform a substitution here, because substitutions
13796 for argument packs are only done
13797 element-by-element. We can get to this point when
13798 substituting the type of a non-type template
13799 parameter pack, when that type actually contains
13800 template parameter packs from an outer template, e.g.,
13802 template<typename... Types> struct A {
13803 template<Types... Values> struct B { };
13804 }; */
13805 return t;
13807 if (code == TEMPLATE_TYPE_PARM)
13809 int quals;
13810 gcc_assert (TYPE_P (arg));
13812 quals = cp_type_quals (arg) | cp_type_quals (t);
13814 return cp_build_qualified_type_real
13815 (arg, quals, complain | tf_ignore_bad_quals);
13817 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13819 /* We are processing a type constructed from a
13820 template template parameter. */
13821 tree argvec = tsubst (TYPE_TI_ARGS (t),
13822 args, complain, in_decl);
13823 if (argvec == error_mark_node)
13824 return error_mark_node;
13826 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13827 || TREE_CODE (arg) == TEMPLATE_DECL
13828 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13830 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13831 /* Consider this code:
13833 template <template <class> class Template>
13834 struct Internal {
13835 template <class Arg> using Bind = Template<Arg>;
13838 template <template <class> class Template, class Arg>
13839 using Instantiate = Template<Arg>; //#0
13841 template <template <class> class Template,
13842 class Argument>
13843 using Bind =
13844 Instantiate<Internal<Template>::template Bind,
13845 Argument>; //#1
13847 When #1 is parsed, the
13848 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13849 parameter `Template' in #0 matches the
13850 UNBOUND_CLASS_TEMPLATE representing the argument
13851 `Internal<Template>::template Bind'; We then want
13852 to assemble the type `Bind<Argument>' that can't
13853 be fully created right now, because
13854 `Internal<Template>' not being complete, the Bind
13855 template cannot be looked up in that context. So
13856 we need to "store" `Bind<Argument>' for later
13857 when the context of Bind becomes complete. Let's
13858 store that in a TYPENAME_TYPE. */
13859 return make_typename_type (TYPE_CONTEXT (arg),
13860 build_nt (TEMPLATE_ID_EXPR,
13861 TYPE_IDENTIFIER (arg),
13862 argvec),
13863 typename_type,
13864 complain);
13866 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13867 are resolving nested-types in the signature of a
13868 member function templates. Otherwise ARG is a
13869 TEMPLATE_DECL and is the real template to be
13870 instantiated. */
13871 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13872 arg = TYPE_NAME (arg);
13874 r = lookup_template_class (arg,
13875 argvec, in_decl,
13876 DECL_CONTEXT (arg),
13877 /*entering_scope=*/0,
13878 complain);
13879 return cp_build_qualified_type_real
13880 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13882 else if (code == TEMPLATE_TEMPLATE_PARM)
13883 return arg;
13884 else
13885 /* TEMPLATE_PARM_INDEX. */
13886 return convert_from_reference (unshare_expr (arg));
13889 if (level == 1)
13890 /* This can happen during the attempted tsubst'ing in
13891 unify. This means that we don't yet have any information
13892 about the template parameter in question. */
13893 return t;
13895 /* If we get here, we must have been looking at a parm for a
13896 more deeply nested template. Make a new version of this
13897 template parameter, but with a lower level. */
13898 switch (code)
13900 case TEMPLATE_TYPE_PARM:
13901 case TEMPLATE_TEMPLATE_PARM:
13902 case BOUND_TEMPLATE_TEMPLATE_PARM:
13903 if (cp_type_quals (t))
13905 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13906 r = cp_build_qualified_type_real
13907 (r, cp_type_quals (t),
13908 complain | (code == TEMPLATE_TYPE_PARM
13909 ? tf_ignore_bad_quals : 0));
13911 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13912 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13913 && (r = (TEMPLATE_PARM_DESCENDANTS
13914 (TEMPLATE_TYPE_PARM_INDEX (t))))
13915 && (r = TREE_TYPE (r))
13916 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13917 /* Break infinite recursion when substituting the constraints
13918 of a constrained placeholder. */;
13919 else
13921 r = copy_type (t);
13922 TEMPLATE_TYPE_PARM_INDEX (r)
13923 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13924 r, levels, args, complain);
13925 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13926 TYPE_MAIN_VARIANT (r) = r;
13927 TYPE_POINTER_TO (r) = NULL_TREE;
13928 TYPE_REFERENCE_TO (r) = NULL_TREE;
13930 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13932 /* Propagate constraints on placeholders. */
13933 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13934 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13935 = tsubst_constraint (constr, args, complain, in_decl);
13936 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13939 pl = tsubst (pl, args, complain, in_decl);
13940 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13944 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13945 /* We have reduced the level of the template
13946 template parameter, but not the levels of its
13947 template parameters, so canonical_type_parameter
13948 will not be able to find the canonical template
13949 template parameter for this level. Thus, we
13950 require structural equality checking to compare
13951 TEMPLATE_TEMPLATE_PARMs. */
13952 SET_TYPE_STRUCTURAL_EQUALITY (r);
13953 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13954 SET_TYPE_STRUCTURAL_EQUALITY (r);
13955 else
13956 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13958 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13960 tree tinfo = TYPE_TEMPLATE_INFO (t);
13961 /* We might need to substitute into the types of non-type
13962 template parameters. */
13963 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13964 complain, in_decl);
13965 if (tmpl == error_mark_node)
13966 return error_mark_node;
13967 tree argvec = tsubst (TI_ARGS (tinfo), args,
13968 complain, in_decl);
13969 if (argvec == error_mark_node)
13970 return error_mark_node;
13972 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13973 = build_template_info (tmpl, argvec);
13976 break;
13978 case TEMPLATE_PARM_INDEX:
13979 /* OK, now substitute the type of the non-type parameter. We
13980 couldn't do it earlier because it might be an auto parameter,
13981 and we wouldn't need to if we had an argument. */
13982 type = tsubst (type, args, complain, in_decl);
13983 if (type == error_mark_node)
13984 return error_mark_node;
13985 r = reduce_template_parm_level (t, type, levels, args, complain);
13986 break;
13988 default:
13989 gcc_unreachable ();
13992 return r;
13995 case TREE_LIST:
13997 tree purpose, value, chain;
13999 if (t == void_list_node)
14000 return t;
14002 purpose = TREE_PURPOSE (t);
14003 if (purpose)
14005 purpose = tsubst (purpose, args, complain, in_decl);
14006 if (purpose == error_mark_node)
14007 return error_mark_node;
14009 value = TREE_VALUE (t);
14010 if (value)
14012 value = tsubst (value, args, complain, in_decl);
14013 if (value == error_mark_node)
14014 return error_mark_node;
14016 chain = TREE_CHAIN (t);
14017 if (chain && chain != void_type_node)
14019 chain = tsubst (chain, args, complain, in_decl);
14020 if (chain == error_mark_node)
14021 return error_mark_node;
14023 if (purpose == TREE_PURPOSE (t)
14024 && value == TREE_VALUE (t)
14025 && chain == TREE_CHAIN (t))
14026 return t;
14027 return hash_tree_cons (purpose, value, chain);
14030 case TREE_BINFO:
14031 /* We should never be tsubsting a binfo. */
14032 gcc_unreachable ();
14034 case TREE_VEC:
14035 /* A vector of template arguments. */
14036 gcc_assert (!type);
14037 return tsubst_template_args (t, args, complain, in_decl);
14039 case POINTER_TYPE:
14040 case REFERENCE_TYPE:
14042 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14043 return t;
14045 /* [temp.deduct]
14047 Type deduction may fail for any of the following
14048 reasons:
14050 -- Attempting to create a pointer to reference type.
14051 -- Attempting to create a reference to a reference type or
14052 a reference to void.
14054 Core issue 106 says that creating a reference to a reference
14055 during instantiation is no longer a cause for failure. We
14056 only enforce this check in strict C++98 mode. */
14057 if ((TREE_CODE (type) == REFERENCE_TYPE
14058 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14059 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14061 static location_t last_loc;
14063 /* We keep track of the last time we issued this error
14064 message to avoid spewing a ton of messages during a
14065 single bad template instantiation. */
14066 if (complain & tf_error
14067 && last_loc != input_location)
14069 if (VOID_TYPE_P (type))
14070 error ("forming reference to void");
14071 else if (code == POINTER_TYPE)
14072 error ("forming pointer to reference type %qT", type);
14073 else
14074 error ("forming reference to reference type %qT", type);
14075 last_loc = input_location;
14078 return error_mark_node;
14080 else if (TREE_CODE (type) == FUNCTION_TYPE
14081 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14082 || type_memfn_rqual (type) != REF_QUAL_NONE))
14084 if (complain & tf_error)
14086 if (code == POINTER_TYPE)
14087 error ("forming pointer to qualified function type %qT",
14088 type);
14089 else
14090 error ("forming reference to qualified function type %qT",
14091 type);
14093 return error_mark_node;
14095 else if (code == POINTER_TYPE)
14097 r = build_pointer_type (type);
14098 if (TREE_CODE (type) == METHOD_TYPE)
14099 r = build_ptrmemfunc_type (r);
14101 else if (TREE_CODE (type) == REFERENCE_TYPE)
14102 /* In C++0x, during template argument substitution, when there is an
14103 attempt to create a reference to a reference type, reference
14104 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14106 "If a template-argument for a template-parameter T names a type
14107 that is a reference to a type A, an attempt to create the type
14108 'lvalue reference to cv T' creates the type 'lvalue reference to
14109 A,' while an attempt to create the type type rvalue reference to
14110 cv T' creates the type T"
14112 r = cp_build_reference_type
14113 (TREE_TYPE (type),
14114 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14115 else
14116 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14117 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14119 if (r != error_mark_node)
14120 /* Will this ever be needed for TYPE_..._TO values? */
14121 layout_type (r);
14123 return r;
14125 case OFFSET_TYPE:
14127 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14128 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14130 /* [temp.deduct]
14132 Type deduction may fail for any of the following
14133 reasons:
14135 -- Attempting to create "pointer to member of T" when T
14136 is not a class type. */
14137 if (complain & tf_error)
14138 error ("creating pointer to member of non-class type %qT", r);
14139 return error_mark_node;
14141 if (TREE_CODE (type) == REFERENCE_TYPE)
14143 if (complain & tf_error)
14144 error ("creating pointer to member reference type %qT", type);
14145 return error_mark_node;
14147 if (VOID_TYPE_P (type))
14149 if (complain & tf_error)
14150 error ("creating pointer to member of type void");
14151 return error_mark_node;
14153 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14154 if (TREE_CODE (type) == FUNCTION_TYPE)
14156 /* The type of the implicit object parameter gets its
14157 cv-qualifiers from the FUNCTION_TYPE. */
14158 tree memptr;
14159 tree method_type
14160 = build_memfn_type (type, r, type_memfn_quals (type),
14161 type_memfn_rqual (type));
14162 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14163 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14164 complain);
14166 else
14167 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14168 cp_type_quals (t),
14169 complain);
14171 case FUNCTION_TYPE:
14172 case METHOD_TYPE:
14174 tree fntype;
14175 tree specs;
14176 fntype = tsubst_function_type (t, args, complain, in_decl);
14177 if (fntype == error_mark_node)
14178 return error_mark_node;
14180 /* Substitute the exception specification. */
14181 specs = tsubst_exception_specification (t, args, complain, in_decl,
14182 /*defer_ok*/fndecl_type);
14183 if (specs == error_mark_node)
14184 return error_mark_node;
14185 if (specs)
14186 fntype = build_exception_variant (fntype, specs);
14187 return fntype;
14189 case ARRAY_TYPE:
14191 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14192 if (domain == error_mark_node)
14193 return error_mark_node;
14195 /* As an optimization, we avoid regenerating the array type if
14196 it will obviously be the same as T. */
14197 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14198 return t;
14200 /* These checks should match the ones in create_array_type_for_decl.
14202 [temp.deduct]
14204 The deduction may fail for any of the following reasons:
14206 -- Attempting to create an array with an element type that
14207 is void, a function type, or a reference type, or [DR337]
14208 an abstract class type. */
14209 if (VOID_TYPE_P (type)
14210 || TREE_CODE (type) == FUNCTION_TYPE
14211 || (TREE_CODE (type) == ARRAY_TYPE
14212 && TYPE_DOMAIN (type) == NULL_TREE)
14213 || TREE_CODE (type) == REFERENCE_TYPE)
14215 if (complain & tf_error)
14216 error ("creating array of %qT", type);
14217 return error_mark_node;
14220 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14221 return error_mark_node;
14223 r = build_cplus_array_type (type, domain);
14225 if (TYPE_USER_ALIGN (t))
14227 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14228 TYPE_USER_ALIGN (r) = 1;
14231 return r;
14234 case TYPENAME_TYPE:
14236 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14237 in_decl, /*entering_scope=*/1);
14238 if (ctx == error_mark_node)
14239 return error_mark_node;
14241 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14242 complain, in_decl);
14243 if (f == error_mark_node)
14244 return error_mark_node;
14246 if (!MAYBE_CLASS_TYPE_P (ctx))
14248 if (complain & tf_error)
14249 error ("%qT is not a class, struct, or union type", ctx);
14250 return error_mark_node;
14252 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14254 /* Normally, make_typename_type does not require that the CTX
14255 have complete type in order to allow things like:
14257 template <class T> struct S { typename S<T>::X Y; };
14259 But, such constructs have already been resolved by this
14260 point, so here CTX really should have complete type, unless
14261 it's a partial instantiation. */
14262 ctx = complete_type (ctx);
14263 if (!COMPLETE_TYPE_P (ctx))
14265 if (complain & tf_error)
14266 cxx_incomplete_type_error (NULL_TREE, ctx);
14267 return error_mark_node;
14271 f = make_typename_type (ctx, f, typename_type,
14272 complain | tf_keep_type_decl);
14273 if (f == error_mark_node)
14274 return f;
14275 if (TREE_CODE (f) == TYPE_DECL)
14277 complain |= tf_ignore_bad_quals;
14278 f = TREE_TYPE (f);
14281 if (TREE_CODE (f) != TYPENAME_TYPE)
14283 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14285 if (complain & tf_error)
14286 error ("%qT resolves to %qT, which is not an enumeration type",
14287 t, f);
14288 else
14289 return error_mark_node;
14291 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14293 if (complain & tf_error)
14294 error ("%qT resolves to %qT, which is is not a class type",
14295 t, f);
14296 else
14297 return error_mark_node;
14301 return cp_build_qualified_type_real
14302 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14305 case UNBOUND_CLASS_TEMPLATE:
14307 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14308 in_decl, /*entering_scope=*/1);
14309 tree name = TYPE_IDENTIFIER (t);
14310 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14312 if (ctx == error_mark_node || name == error_mark_node)
14313 return error_mark_node;
14315 if (parm_list)
14316 parm_list = tsubst_template_parms (parm_list, args, complain);
14317 return make_unbound_class_template (ctx, name, parm_list, complain);
14320 case TYPEOF_TYPE:
14322 tree type;
14324 ++cp_unevaluated_operand;
14325 ++c_inhibit_evaluation_warnings;
14327 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14328 complain, in_decl,
14329 /*integral_constant_expression_p=*/false);
14331 --cp_unevaluated_operand;
14332 --c_inhibit_evaluation_warnings;
14334 type = finish_typeof (type);
14335 return cp_build_qualified_type_real (type,
14336 cp_type_quals (t)
14337 | cp_type_quals (type),
14338 complain);
14341 case DECLTYPE_TYPE:
14343 tree type;
14345 ++cp_unevaluated_operand;
14346 ++c_inhibit_evaluation_warnings;
14348 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14349 complain|tf_decltype, in_decl,
14350 /*function_p*/false,
14351 /*integral_constant_expression*/false);
14353 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14355 if (type == NULL_TREE)
14357 if (complain & tf_error)
14358 error ("empty initializer in lambda init-capture");
14359 type = error_mark_node;
14361 else if (TREE_CODE (type) == TREE_LIST)
14362 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14365 --cp_unevaluated_operand;
14366 --c_inhibit_evaluation_warnings;
14368 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14369 type = lambda_capture_field_type (type,
14370 DECLTYPE_FOR_INIT_CAPTURE (t),
14371 DECLTYPE_FOR_REF_CAPTURE (t));
14372 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14373 type = lambda_proxy_type (type);
14374 else
14376 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14377 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14378 && EXPR_P (type))
14379 /* In a template ~id could be either a complement expression
14380 or an unqualified-id naming a destructor; if instantiating
14381 it produces an expression, it's not an id-expression or
14382 member access. */
14383 id = false;
14384 type = finish_decltype_type (type, id, complain);
14386 return cp_build_qualified_type_real (type,
14387 cp_type_quals (t)
14388 | cp_type_quals (type),
14389 complain | tf_ignore_bad_quals);
14392 case UNDERLYING_TYPE:
14394 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14395 complain, in_decl);
14396 return finish_underlying_type (type);
14399 case TYPE_ARGUMENT_PACK:
14400 case NONTYPE_ARGUMENT_PACK:
14402 tree r;
14404 if (code == NONTYPE_ARGUMENT_PACK)
14405 r = make_node (code);
14406 else
14407 r = cxx_make_type (code);
14409 tree pack_args = ARGUMENT_PACK_ARGS (t);
14410 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14411 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14413 return r;
14416 case VOID_CST:
14417 case INTEGER_CST:
14418 case REAL_CST:
14419 case STRING_CST:
14420 case PLUS_EXPR:
14421 case MINUS_EXPR:
14422 case NEGATE_EXPR:
14423 case NOP_EXPR:
14424 case INDIRECT_REF:
14425 case ADDR_EXPR:
14426 case CALL_EXPR:
14427 case ARRAY_REF:
14428 case SCOPE_REF:
14429 /* We should use one of the expression tsubsts for these codes. */
14430 gcc_unreachable ();
14432 default:
14433 sorry ("use of %qs in template", get_tree_code_name (code));
14434 return error_mark_node;
14438 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14439 expression on the left-hand side of the "." or "->" operator. We
14440 only do the lookup if we had a dependent BASELINK. Otherwise we
14441 adjust it onto the instantiated heirarchy. */
14443 static tree
14444 tsubst_baselink (tree baselink, tree object_type,
14445 tree args, tsubst_flags_t complain, tree in_decl)
14447 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14448 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14449 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14451 tree optype = BASELINK_OPTYPE (baselink);
14452 optype = tsubst (optype, args, complain, in_decl);
14454 tree template_args = NULL_TREE;
14455 bool template_id_p = false;
14456 tree fns = BASELINK_FUNCTIONS (baselink);
14457 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14459 template_id_p = true;
14460 template_args = TREE_OPERAND (fns, 1);
14461 fns = TREE_OPERAND (fns, 0);
14462 if (template_args)
14463 template_args = tsubst_template_args (template_args, args,
14464 complain, in_decl);
14467 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14468 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14469 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14471 if (dependent_p)
14473 tree name = OVL_NAME (fns);
14474 if (IDENTIFIER_CONV_OP_P (name))
14475 name = make_conv_op_name (optype);
14477 if (name == complete_dtor_identifier)
14478 /* Treat as-if non-dependent below. */
14479 dependent_p = false;
14481 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14482 if (!baselink)
14484 if ((complain & tf_error)
14485 && constructor_name_p (name, qualifying_scope))
14486 error ("cannot call constructor %<%T::%D%> directly",
14487 qualifying_scope, name);
14488 return error_mark_node;
14491 if (BASELINK_P (baselink))
14492 fns = BASELINK_FUNCTIONS (baselink);
14494 else
14495 /* We're going to overwrite pieces below, make a duplicate. */
14496 baselink = copy_node (baselink);
14498 /* If lookup found a single function, mark it as used at this point.
14499 (If lookup found multiple functions the one selected later by
14500 overload resolution will be marked as used at that point.) */
14501 if (!template_id_p && !really_overloaded_fn (fns)
14502 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14503 return error_mark_node;
14505 if (BASELINK_P (baselink))
14507 /* Add back the template arguments, if present. */
14508 if (template_id_p)
14509 BASELINK_FUNCTIONS (baselink)
14510 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14512 /* Update the conversion operator type. */
14513 BASELINK_OPTYPE (baselink) = optype;
14516 if (!object_type)
14517 object_type = current_class_type;
14519 if (qualified_p || !dependent_p)
14521 baselink = adjust_result_of_qualified_name_lookup (baselink,
14522 qualifying_scope,
14523 object_type);
14524 if (!qualified_p)
14525 /* We need to call adjust_result_of_qualified_name_lookup in case the
14526 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14527 so that we still get virtual function binding. */
14528 BASELINK_QUALIFIED_P (baselink) = false;
14531 return baselink;
14534 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14535 true if the qualified-id will be a postfix-expression in-and-of
14536 itself; false if more of the postfix-expression follows the
14537 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14538 of "&". */
14540 static tree
14541 tsubst_qualified_id (tree qualified_id, tree args,
14542 tsubst_flags_t complain, tree in_decl,
14543 bool done, bool address_p)
14545 tree expr;
14546 tree scope;
14547 tree name;
14548 bool is_template;
14549 tree template_args;
14550 location_t loc = UNKNOWN_LOCATION;
14552 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14554 /* Figure out what name to look up. */
14555 name = TREE_OPERAND (qualified_id, 1);
14556 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14558 is_template = true;
14559 loc = EXPR_LOCATION (name);
14560 template_args = TREE_OPERAND (name, 1);
14561 if (template_args)
14562 template_args = tsubst_template_args (template_args, args,
14563 complain, in_decl);
14564 if (template_args == error_mark_node)
14565 return error_mark_node;
14566 name = TREE_OPERAND (name, 0);
14568 else
14570 is_template = false;
14571 template_args = NULL_TREE;
14574 /* Substitute into the qualifying scope. When there are no ARGS, we
14575 are just trying to simplify a non-dependent expression. In that
14576 case the qualifying scope may be dependent, and, in any case,
14577 substituting will not help. */
14578 scope = TREE_OPERAND (qualified_id, 0);
14579 if (args)
14581 scope = tsubst (scope, args, complain, in_decl);
14582 expr = tsubst_copy (name, args, complain, in_decl);
14584 else
14585 expr = name;
14587 if (dependent_scope_p (scope))
14589 if (is_template)
14590 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14591 tree r = build_qualified_name (NULL_TREE, scope, expr,
14592 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14593 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14594 return r;
14597 if (!BASELINK_P (name) && !DECL_P (expr))
14599 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14601 /* A BIT_NOT_EXPR is used to represent a destructor. */
14602 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14604 error ("qualifying type %qT does not match destructor name ~%qT",
14605 scope, TREE_OPERAND (expr, 0));
14606 expr = error_mark_node;
14608 else
14609 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14610 /*is_type_p=*/0, false);
14612 else
14613 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14614 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14615 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14617 if (complain & tf_error)
14619 error ("dependent-name %qE is parsed as a non-type, but "
14620 "instantiation yields a type", qualified_id);
14621 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14623 return error_mark_node;
14627 if (DECL_P (expr))
14629 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14630 scope);
14631 /* Remember that there was a reference to this entity. */
14632 if (!mark_used (expr, complain) && !(complain & tf_error))
14633 return error_mark_node;
14636 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14638 if (complain & tf_error)
14639 qualified_name_lookup_error (scope,
14640 TREE_OPERAND (qualified_id, 1),
14641 expr, input_location);
14642 return error_mark_node;
14645 if (is_template)
14647 if (variable_template_p (expr))
14648 expr = lookup_and_finish_template_variable (expr, template_args,
14649 complain);
14650 else
14651 expr = lookup_template_function (expr, template_args);
14654 if (expr == error_mark_node && complain & tf_error)
14655 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14656 expr, input_location);
14657 else if (TYPE_P (scope))
14659 expr = (adjust_result_of_qualified_name_lookup
14660 (expr, scope, current_nonlambda_class_type ()));
14661 expr = (finish_qualified_id_expr
14662 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14663 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14664 /*template_arg_p=*/false, complain));
14667 /* Expressions do not generally have reference type. */
14668 if (TREE_CODE (expr) != SCOPE_REF
14669 /* However, if we're about to form a pointer-to-member, we just
14670 want the referenced member referenced. */
14671 && TREE_CODE (expr) != OFFSET_REF)
14672 expr = convert_from_reference (expr);
14674 if (REF_PARENTHESIZED_P (qualified_id))
14675 expr = force_paren_expr (expr);
14677 return expr;
14680 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14681 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14682 for tsubst. */
14684 static tree
14685 tsubst_init (tree init, tree decl, tree args,
14686 tsubst_flags_t complain, tree in_decl)
14688 if (!init)
14689 return NULL_TREE;
14691 init = tsubst_expr (init, args, complain, in_decl, false);
14693 if (!init && TREE_TYPE (decl) != error_mark_node)
14695 /* If we had an initializer but it
14696 instantiated to nothing,
14697 value-initialize the object. This will
14698 only occur when the initializer was a
14699 pack expansion where the parameter packs
14700 used in that expansion were of length
14701 zero. */
14702 init = build_value_init (TREE_TYPE (decl),
14703 complain);
14704 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14705 init = get_target_expr_sfinae (init, complain);
14706 if (TREE_CODE (init) == TARGET_EXPR)
14707 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14710 return init;
14713 /* Like tsubst, but deals with expressions. This function just replaces
14714 template parms; to finish processing the resultant expression, use
14715 tsubst_copy_and_build or tsubst_expr. */
14717 static tree
14718 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14720 enum tree_code code;
14721 tree r;
14723 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14724 return t;
14726 code = TREE_CODE (t);
14728 switch (code)
14730 case PARM_DECL:
14731 r = retrieve_local_specialization (t);
14733 if (r == NULL_TREE)
14735 /* We get here for a use of 'this' in an NSDMI. */
14736 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14737 return current_class_ptr;
14739 /* This can happen for a parameter name used later in a function
14740 declaration (such as in a late-specified return type). Just
14741 make a dummy decl, since it's only used for its type. */
14742 gcc_assert (cp_unevaluated_operand != 0);
14743 r = tsubst_decl (t, args, complain);
14744 /* Give it the template pattern as its context; its true context
14745 hasn't been instantiated yet and this is good enough for
14746 mangling. */
14747 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14750 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14751 r = argument_pack_select_arg (r);
14752 if (!mark_used (r, complain) && !(complain & tf_error))
14753 return error_mark_node;
14754 return r;
14756 case CONST_DECL:
14758 tree enum_type;
14759 tree v;
14761 if (DECL_TEMPLATE_PARM_P (t))
14762 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14763 /* There is no need to substitute into namespace-scope
14764 enumerators. */
14765 if (DECL_NAMESPACE_SCOPE_P (t))
14766 return t;
14767 /* If ARGS is NULL, then T is known to be non-dependent. */
14768 if (args == NULL_TREE)
14769 return scalar_constant_value (t);
14771 /* Unfortunately, we cannot just call lookup_name here.
14772 Consider:
14774 template <int I> int f() {
14775 enum E { a = I };
14776 struct S { void g() { E e = a; } };
14779 When we instantiate f<7>::S::g(), say, lookup_name is not
14780 clever enough to find f<7>::a. */
14781 enum_type
14782 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14783 /*entering_scope=*/0);
14785 for (v = TYPE_VALUES (enum_type);
14786 v != NULL_TREE;
14787 v = TREE_CHAIN (v))
14788 if (TREE_PURPOSE (v) == DECL_NAME (t))
14789 return TREE_VALUE (v);
14791 /* We didn't find the name. That should never happen; if
14792 name-lookup found it during preliminary parsing, we
14793 should find it again here during instantiation. */
14794 gcc_unreachable ();
14796 return t;
14798 case FIELD_DECL:
14799 if (DECL_CONTEXT (t))
14801 tree ctx;
14803 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14804 /*entering_scope=*/1);
14805 if (ctx != DECL_CONTEXT (t))
14807 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14808 if (!r)
14810 if (complain & tf_error)
14811 error ("using invalid field %qD", t);
14812 return error_mark_node;
14814 return r;
14818 return t;
14820 case VAR_DECL:
14821 case FUNCTION_DECL:
14822 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14823 r = tsubst (t, args, complain, in_decl);
14824 else if (local_variable_p (t)
14825 && uses_template_parms (DECL_CONTEXT (t)))
14827 r = retrieve_local_specialization (t);
14828 if (r == NULL_TREE)
14830 /* First try name lookup to find the instantiation. */
14831 r = lookup_name (DECL_NAME (t));
14832 if (r && !is_capture_proxy (r))
14834 /* Make sure that the one we found is the one we want. */
14835 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14836 if (ctx != DECL_CONTEXT (r))
14837 r = NULL_TREE;
14840 if (r)
14841 /* OK */;
14842 else
14844 /* This can happen for a variable used in a
14845 late-specified return type of a local lambda, or for a
14846 local static or constant. Building a new VAR_DECL
14847 should be OK in all those cases. */
14848 r = tsubst_decl (t, args, complain);
14849 if (local_specializations)
14850 /* Avoid infinite recursion (79640). */
14851 register_local_specialization (r, t);
14852 if (decl_maybe_constant_var_p (r))
14854 /* We can't call cp_finish_decl, so handle the
14855 initializer by hand. */
14856 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14857 complain, in_decl);
14858 if (!processing_template_decl)
14859 init = maybe_constant_init (init);
14860 if (processing_template_decl
14861 ? potential_constant_expression (init)
14862 : reduced_constant_expression_p (init))
14863 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14864 = TREE_CONSTANT (r) = true;
14865 DECL_INITIAL (r) = init;
14866 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14867 TREE_TYPE (r)
14868 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14869 complain, adc_variable_type);
14871 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14872 || decl_constant_var_p (r)
14873 || errorcount || sorrycount);
14874 if (!processing_template_decl
14875 && !TREE_STATIC (r))
14876 r = process_outer_var_ref (r, complain);
14878 /* Remember this for subsequent uses. */
14879 if (local_specializations)
14880 register_local_specialization (r, t);
14882 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14883 r = argument_pack_select_arg (r);
14885 else
14886 r = t;
14887 if (!mark_used (r, complain))
14888 return error_mark_node;
14889 return r;
14891 case NAMESPACE_DECL:
14892 return t;
14894 case OVERLOAD:
14895 /* An OVERLOAD will always be a non-dependent overload set; an
14896 overload set from function scope will just be represented with an
14897 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14898 gcc_assert (!uses_template_parms (t));
14899 /* We must have marked any lookups as persistent. */
14900 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14901 return t;
14903 case BASELINK:
14904 return tsubst_baselink (t, current_nonlambda_class_type (),
14905 args, complain, in_decl);
14907 case TEMPLATE_DECL:
14908 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14909 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14910 args, complain, in_decl);
14911 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14912 return tsubst (t, args, complain, in_decl);
14913 else if (DECL_CLASS_SCOPE_P (t)
14914 && uses_template_parms (DECL_CONTEXT (t)))
14916 /* Template template argument like the following example need
14917 special treatment:
14919 template <template <class> class TT> struct C {};
14920 template <class T> struct D {
14921 template <class U> struct E {};
14922 C<E> c; // #1
14924 D<int> d; // #2
14926 We are processing the template argument `E' in #1 for
14927 the template instantiation #2. Originally, `E' is a
14928 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14929 have to substitute this with one having context `D<int>'. */
14931 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14932 if (dependent_scope_p (context))
14934 /* When rewriting a constructor into a deduction guide, a
14935 non-dependent name can become dependent, so memtmpl<args>
14936 becomes context::template memtmpl<args>. */
14937 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14938 return build_qualified_name (type, context, DECL_NAME (t),
14939 /*template*/true);
14941 return lookup_field (context, DECL_NAME(t), 0, false);
14943 else
14944 /* Ordinary template template argument. */
14945 return t;
14947 case NON_LVALUE_EXPR:
14948 case VIEW_CONVERT_EXPR:
14950 /* Handle location wrappers by substituting the wrapped node
14951 first, *then* reusing the resulting type. Doing the type
14952 first ensures that we handle template parameters and
14953 parameter pack expansions. */
14954 gcc_assert (location_wrapper_p (t));
14955 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14956 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
14959 case CAST_EXPR:
14960 case REINTERPRET_CAST_EXPR:
14961 case CONST_CAST_EXPR:
14962 case STATIC_CAST_EXPR:
14963 case DYNAMIC_CAST_EXPR:
14964 case IMPLICIT_CONV_EXPR:
14965 case CONVERT_EXPR:
14966 case NOP_EXPR:
14968 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14969 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14970 return build1 (code, type, op0);
14973 case SIZEOF_EXPR:
14974 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14975 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14977 tree expanded, op = TREE_OPERAND (t, 0);
14978 int len = 0;
14980 if (SIZEOF_EXPR_TYPE_P (t))
14981 op = TREE_TYPE (op);
14983 ++cp_unevaluated_operand;
14984 ++c_inhibit_evaluation_warnings;
14985 /* We only want to compute the number of arguments. */
14986 if (PACK_EXPANSION_P (op))
14987 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14988 else
14989 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14990 args, complain, in_decl);
14991 --cp_unevaluated_operand;
14992 --c_inhibit_evaluation_warnings;
14994 if (TREE_CODE (expanded) == TREE_VEC)
14996 len = TREE_VEC_LENGTH (expanded);
14997 /* Set TREE_USED for the benefit of -Wunused. */
14998 for (int i = 0; i < len; i++)
14999 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15000 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15003 if (expanded == error_mark_node)
15004 return error_mark_node;
15005 else if (PACK_EXPANSION_P (expanded)
15006 || (TREE_CODE (expanded) == TREE_VEC
15007 && pack_expansion_args_count (expanded)))
15010 if (PACK_EXPANSION_P (expanded))
15011 /* OK. */;
15012 else if (TREE_VEC_LENGTH (expanded) == 1)
15013 expanded = TREE_VEC_ELT (expanded, 0);
15014 else
15015 expanded = make_argument_pack (expanded);
15017 if (TYPE_P (expanded))
15018 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15019 complain & tf_error);
15020 else
15021 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15022 complain & tf_error);
15024 else
15025 return build_int_cst (size_type_node, len);
15027 if (SIZEOF_EXPR_TYPE_P (t))
15029 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15030 args, complain, in_decl);
15031 r = build1 (NOP_EXPR, r, error_mark_node);
15032 r = build1 (SIZEOF_EXPR,
15033 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15034 SIZEOF_EXPR_TYPE_P (r) = 1;
15035 return r;
15037 /* Fall through */
15039 case INDIRECT_REF:
15040 case NEGATE_EXPR:
15041 case TRUTH_NOT_EXPR:
15042 case BIT_NOT_EXPR:
15043 case ADDR_EXPR:
15044 case UNARY_PLUS_EXPR: /* Unary + */
15045 case ALIGNOF_EXPR:
15046 case AT_ENCODE_EXPR:
15047 case ARROW_EXPR:
15048 case THROW_EXPR:
15049 case TYPEID_EXPR:
15050 case REALPART_EXPR:
15051 case IMAGPART_EXPR:
15052 case PAREN_EXPR:
15054 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15055 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15056 return build1 (code, type, op0);
15059 case COMPONENT_REF:
15061 tree object;
15062 tree name;
15064 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15065 name = TREE_OPERAND (t, 1);
15066 if (TREE_CODE (name) == BIT_NOT_EXPR)
15068 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15069 complain, in_decl);
15070 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15072 else if (TREE_CODE (name) == SCOPE_REF
15073 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15075 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15076 complain, in_decl);
15077 name = TREE_OPERAND (name, 1);
15078 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15079 complain, in_decl);
15080 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15081 name = build_qualified_name (/*type=*/NULL_TREE,
15082 base, name,
15083 /*template_p=*/false);
15085 else if (BASELINK_P (name))
15086 name = tsubst_baselink (name,
15087 non_reference (TREE_TYPE (object)),
15088 args, complain,
15089 in_decl);
15090 else
15091 name = tsubst_copy (name, args, complain, in_decl);
15092 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15095 case PLUS_EXPR:
15096 case MINUS_EXPR:
15097 case MULT_EXPR:
15098 case TRUNC_DIV_EXPR:
15099 case CEIL_DIV_EXPR:
15100 case FLOOR_DIV_EXPR:
15101 case ROUND_DIV_EXPR:
15102 case EXACT_DIV_EXPR:
15103 case BIT_AND_EXPR:
15104 case BIT_IOR_EXPR:
15105 case BIT_XOR_EXPR:
15106 case TRUNC_MOD_EXPR:
15107 case FLOOR_MOD_EXPR:
15108 case TRUTH_ANDIF_EXPR:
15109 case TRUTH_ORIF_EXPR:
15110 case TRUTH_AND_EXPR:
15111 case TRUTH_OR_EXPR:
15112 case RSHIFT_EXPR:
15113 case LSHIFT_EXPR:
15114 case RROTATE_EXPR:
15115 case LROTATE_EXPR:
15116 case EQ_EXPR:
15117 case NE_EXPR:
15118 case MAX_EXPR:
15119 case MIN_EXPR:
15120 case LE_EXPR:
15121 case GE_EXPR:
15122 case LT_EXPR:
15123 case GT_EXPR:
15124 case COMPOUND_EXPR:
15125 case DOTSTAR_EXPR:
15126 case MEMBER_REF:
15127 case PREDECREMENT_EXPR:
15128 case PREINCREMENT_EXPR:
15129 case POSTDECREMENT_EXPR:
15130 case POSTINCREMENT_EXPR:
15132 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15133 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15134 return build_nt (code, op0, op1);
15137 case SCOPE_REF:
15139 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15140 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15141 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15142 QUALIFIED_NAME_IS_TEMPLATE (t));
15145 case ARRAY_REF:
15147 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15148 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15149 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15152 case CALL_EXPR:
15154 int n = VL_EXP_OPERAND_LENGTH (t);
15155 tree result = build_vl_exp (CALL_EXPR, n);
15156 int i;
15157 for (i = 0; i < n; i++)
15158 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15159 complain, in_decl);
15160 return result;
15163 case COND_EXPR:
15164 case MODOP_EXPR:
15165 case PSEUDO_DTOR_EXPR:
15166 case VEC_PERM_EXPR:
15168 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15169 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15170 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15171 r = build_nt (code, op0, op1, op2);
15172 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15173 return r;
15176 case NEW_EXPR:
15178 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15179 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15180 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15181 r = build_nt (code, op0, op1, op2);
15182 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15183 return r;
15186 case DELETE_EXPR:
15188 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15189 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15190 r = build_nt (code, op0, op1);
15191 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15192 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15193 return r;
15196 case TEMPLATE_ID_EXPR:
15198 /* Substituted template arguments */
15199 tree fn = TREE_OPERAND (t, 0);
15200 tree targs = TREE_OPERAND (t, 1);
15202 fn = tsubst_copy (fn, args, complain, in_decl);
15203 if (targs)
15204 targs = tsubst_template_args (targs, args, complain, in_decl);
15206 return lookup_template_function (fn, targs);
15209 case TREE_LIST:
15211 tree purpose, value, chain;
15213 if (t == void_list_node)
15214 return t;
15216 purpose = TREE_PURPOSE (t);
15217 if (purpose)
15218 purpose = tsubst_copy (purpose, args, complain, in_decl);
15219 value = TREE_VALUE (t);
15220 if (value)
15221 value = tsubst_copy (value, args, complain, in_decl);
15222 chain = TREE_CHAIN (t);
15223 if (chain && chain != void_type_node)
15224 chain = tsubst_copy (chain, args, complain, in_decl);
15225 if (purpose == TREE_PURPOSE (t)
15226 && value == TREE_VALUE (t)
15227 && chain == TREE_CHAIN (t))
15228 return t;
15229 return tree_cons (purpose, value, chain);
15232 case RECORD_TYPE:
15233 case UNION_TYPE:
15234 case ENUMERAL_TYPE:
15235 case INTEGER_TYPE:
15236 case TEMPLATE_TYPE_PARM:
15237 case TEMPLATE_TEMPLATE_PARM:
15238 case BOUND_TEMPLATE_TEMPLATE_PARM:
15239 case TEMPLATE_PARM_INDEX:
15240 case POINTER_TYPE:
15241 case REFERENCE_TYPE:
15242 case OFFSET_TYPE:
15243 case FUNCTION_TYPE:
15244 case METHOD_TYPE:
15245 case ARRAY_TYPE:
15246 case TYPENAME_TYPE:
15247 case UNBOUND_CLASS_TEMPLATE:
15248 case TYPEOF_TYPE:
15249 case DECLTYPE_TYPE:
15250 case TYPE_DECL:
15251 return tsubst (t, args, complain, in_decl);
15253 case USING_DECL:
15254 t = DECL_NAME (t);
15255 /* Fall through. */
15256 case IDENTIFIER_NODE:
15257 if (IDENTIFIER_CONV_OP_P (t))
15259 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15260 return make_conv_op_name (new_type);
15262 else
15263 return t;
15265 case CONSTRUCTOR:
15266 /* This is handled by tsubst_copy_and_build. */
15267 gcc_unreachable ();
15269 case VA_ARG_EXPR:
15271 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15272 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15273 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15276 case CLEANUP_POINT_EXPR:
15277 /* We shouldn't have built any of these during initial template
15278 generation. Instead, they should be built during instantiation
15279 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15280 gcc_unreachable ();
15282 case OFFSET_REF:
15284 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15285 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15286 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15287 r = build2 (code, type, op0, op1);
15288 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15289 if (!mark_used (TREE_OPERAND (r, 1), complain)
15290 && !(complain & tf_error))
15291 return error_mark_node;
15292 return r;
15295 case EXPR_PACK_EXPANSION:
15296 error ("invalid use of pack expansion expression");
15297 return error_mark_node;
15299 case NONTYPE_ARGUMENT_PACK:
15300 error ("use %<...%> to expand argument pack");
15301 return error_mark_node;
15303 case VOID_CST:
15304 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15305 return t;
15307 case INTEGER_CST:
15308 case REAL_CST:
15309 case STRING_CST:
15310 case COMPLEX_CST:
15312 /* Instantiate any typedefs in the type. */
15313 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15314 r = fold_convert (type, t);
15315 gcc_assert (TREE_CODE (r) == code);
15316 return r;
15319 case PTRMEM_CST:
15320 /* These can sometimes show up in a partial instantiation, but never
15321 involve template parms. */
15322 gcc_assert (!uses_template_parms (t));
15323 return t;
15325 case UNARY_LEFT_FOLD_EXPR:
15326 return tsubst_unary_left_fold (t, args, complain, in_decl);
15327 case UNARY_RIGHT_FOLD_EXPR:
15328 return tsubst_unary_right_fold (t, args, complain, in_decl);
15329 case BINARY_LEFT_FOLD_EXPR:
15330 return tsubst_binary_left_fold (t, args, complain, in_decl);
15331 case BINARY_RIGHT_FOLD_EXPR:
15332 return tsubst_binary_right_fold (t, args, complain, in_decl);
15333 case PREDICT_EXPR:
15334 return t;
15336 case DEBUG_BEGIN_STMT:
15337 /* ??? There's no point in copying it for now, but maybe some
15338 day it will contain more information, such as a pointer back
15339 to the containing function, inlined copy or so. */
15340 return t;
15342 default:
15343 /* We shouldn't get here, but keep going if !flag_checking. */
15344 if (flag_checking)
15345 gcc_unreachable ();
15346 return t;
15350 /* Helper function for tsubst_omp_clauses, used for instantiation of
15351 OMP_CLAUSE_DECL of clauses. */
15353 static tree
15354 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15355 tree in_decl)
15357 if (decl == NULL_TREE)
15358 return NULL_TREE;
15360 /* Handle an OpenMP array section represented as a TREE_LIST (or
15361 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15362 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15363 TREE_LIST. We can handle it exactly the same as an array section
15364 (purpose, value, and a chain), even though the nomenclature
15365 (low_bound, length, etc) is different. */
15366 if (TREE_CODE (decl) == TREE_LIST)
15368 tree low_bound
15369 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15370 /*integral_constant_expression_p=*/false);
15371 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15372 /*integral_constant_expression_p=*/false);
15373 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15374 in_decl);
15375 if (TREE_PURPOSE (decl) == low_bound
15376 && TREE_VALUE (decl) == length
15377 && TREE_CHAIN (decl) == chain)
15378 return decl;
15379 tree ret = tree_cons (low_bound, length, chain);
15380 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15381 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15382 return ret;
15384 tree ret = tsubst_expr (decl, args, complain, in_decl,
15385 /*integral_constant_expression_p=*/false);
15386 /* Undo convert_from_reference tsubst_expr could have called. */
15387 if (decl
15388 && REFERENCE_REF_P (ret)
15389 && !REFERENCE_REF_P (decl))
15390 ret = TREE_OPERAND (ret, 0);
15391 return ret;
15394 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15396 static tree
15397 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15398 tree args, tsubst_flags_t complain, tree in_decl)
15400 tree new_clauses = NULL_TREE, nc, oc;
15401 tree linear_no_step = NULL_TREE;
15403 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15405 nc = copy_node (oc);
15406 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15407 new_clauses = nc;
15409 switch (OMP_CLAUSE_CODE (nc))
15411 case OMP_CLAUSE_LASTPRIVATE:
15412 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15414 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15415 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15416 in_decl, /*integral_constant_expression_p=*/false);
15417 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15418 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15420 /* FALLTHRU */
15421 case OMP_CLAUSE_PRIVATE:
15422 case OMP_CLAUSE_SHARED:
15423 case OMP_CLAUSE_FIRSTPRIVATE:
15424 case OMP_CLAUSE_COPYIN:
15425 case OMP_CLAUSE_COPYPRIVATE:
15426 case OMP_CLAUSE_UNIFORM:
15427 case OMP_CLAUSE_DEPEND:
15428 case OMP_CLAUSE_FROM:
15429 case OMP_CLAUSE_TO:
15430 case OMP_CLAUSE_MAP:
15431 case OMP_CLAUSE_USE_DEVICE_PTR:
15432 case OMP_CLAUSE_IS_DEVICE_PTR:
15433 OMP_CLAUSE_DECL (nc)
15434 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15435 in_decl);
15436 break;
15437 case OMP_CLAUSE_TILE:
15438 case OMP_CLAUSE_IF:
15439 case OMP_CLAUSE_NUM_THREADS:
15440 case OMP_CLAUSE_SCHEDULE:
15441 case OMP_CLAUSE_COLLAPSE:
15442 case OMP_CLAUSE_FINAL:
15443 case OMP_CLAUSE_DEVICE:
15444 case OMP_CLAUSE_DIST_SCHEDULE:
15445 case OMP_CLAUSE_NUM_TEAMS:
15446 case OMP_CLAUSE_THREAD_LIMIT:
15447 case OMP_CLAUSE_SAFELEN:
15448 case OMP_CLAUSE_SIMDLEN:
15449 case OMP_CLAUSE_NUM_TASKS:
15450 case OMP_CLAUSE_GRAINSIZE:
15451 case OMP_CLAUSE_PRIORITY:
15452 case OMP_CLAUSE_ORDERED:
15453 case OMP_CLAUSE_HINT:
15454 case OMP_CLAUSE_NUM_GANGS:
15455 case OMP_CLAUSE_NUM_WORKERS:
15456 case OMP_CLAUSE_VECTOR_LENGTH:
15457 case OMP_CLAUSE_WORKER:
15458 case OMP_CLAUSE_VECTOR:
15459 case OMP_CLAUSE_ASYNC:
15460 case OMP_CLAUSE_WAIT:
15461 OMP_CLAUSE_OPERAND (nc, 0)
15462 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15463 in_decl, /*integral_constant_expression_p=*/false);
15464 break;
15465 case OMP_CLAUSE_REDUCTION:
15466 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15468 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15469 if (TREE_CODE (placeholder) == SCOPE_REF)
15471 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15472 complain, in_decl);
15473 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15474 = build_qualified_name (NULL_TREE, scope,
15475 TREE_OPERAND (placeholder, 1),
15476 false);
15478 else
15479 gcc_assert (identifier_p (placeholder));
15481 OMP_CLAUSE_DECL (nc)
15482 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15483 in_decl);
15484 break;
15485 case OMP_CLAUSE_GANG:
15486 case OMP_CLAUSE_ALIGNED:
15487 OMP_CLAUSE_DECL (nc)
15488 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15489 in_decl);
15490 OMP_CLAUSE_OPERAND (nc, 1)
15491 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15492 in_decl, /*integral_constant_expression_p=*/false);
15493 break;
15494 case OMP_CLAUSE_LINEAR:
15495 OMP_CLAUSE_DECL (nc)
15496 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15497 in_decl);
15498 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15500 gcc_assert (!linear_no_step);
15501 linear_no_step = nc;
15503 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15504 OMP_CLAUSE_LINEAR_STEP (nc)
15505 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15506 complain, in_decl);
15507 else
15508 OMP_CLAUSE_LINEAR_STEP (nc)
15509 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15510 in_decl,
15511 /*integral_constant_expression_p=*/false);
15512 break;
15513 case OMP_CLAUSE_NOWAIT:
15514 case OMP_CLAUSE_DEFAULT:
15515 case OMP_CLAUSE_UNTIED:
15516 case OMP_CLAUSE_MERGEABLE:
15517 case OMP_CLAUSE_INBRANCH:
15518 case OMP_CLAUSE_NOTINBRANCH:
15519 case OMP_CLAUSE_PROC_BIND:
15520 case OMP_CLAUSE_FOR:
15521 case OMP_CLAUSE_PARALLEL:
15522 case OMP_CLAUSE_SECTIONS:
15523 case OMP_CLAUSE_TASKGROUP:
15524 case OMP_CLAUSE_NOGROUP:
15525 case OMP_CLAUSE_THREADS:
15526 case OMP_CLAUSE_SIMD:
15527 case OMP_CLAUSE_DEFAULTMAP:
15528 case OMP_CLAUSE_INDEPENDENT:
15529 case OMP_CLAUSE_AUTO:
15530 case OMP_CLAUSE_SEQ:
15531 break;
15532 default:
15533 gcc_unreachable ();
15535 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15536 switch (OMP_CLAUSE_CODE (nc))
15538 case OMP_CLAUSE_SHARED:
15539 case OMP_CLAUSE_PRIVATE:
15540 case OMP_CLAUSE_FIRSTPRIVATE:
15541 case OMP_CLAUSE_LASTPRIVATE:
15542 case OMP_CLAUSE_COPYPRIVATE:
15543 case OMP_CLAUSE_LINEAR:
15544 case OMP_CLAUSE_REDUCTION:
15545 case OMP_CLAUSE_USE_DEVICE_PTR:
15546 case OMP_CLAUSE_IS_DEVICE_PTR:
15547 /* tsubst_expr on SCOPE_REF results in returning
15548 finish_non_static_data_member result. Undo that here. */
15549 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15550 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15551 == IDENTIFIER_NODE))
15553 tree t = OMP_CLAUSE_DECL (nc);
15554 tree v = t;
15555 while (v)
15556 switch (TREE_CODE (v))
15558 case COMPONENT_REF:
15559 case MEM_REF:
15560 case INDIRECT_REF:
15561 CASE_CONVERT:
15562 case POINTER_PLUS_EXPR:
15563 v = TREE_OPERAND (v, 0);
15564 continue;
15565 case PARM_DECL:
15566 if (DECL_CONTEXT (v) == current_function_decl
15567 && DECL_ARTIFICIAL (v)
15568 && DECL_NAME (v) == this_identifier)
15569 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15570 /* FALLTHRU */
15571 default:
15572 v = NULL_TREE;
15573 break;
15576 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15577 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15578 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15579 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15580 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15582 tree decl = OMP_CLAUSE_DECL (nc);
15583 if (VAR_P (decl))
15585 retrofit_lang_decl (decl);
15586 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15589 break;
15590 default:
15591 break;
15595 new_clauses = nreverse (new_clauses);
15596 if (ort != C_ORT_OMP_DECLARE_SIMD)
15598 new_clauses = finish_omp_clauses (new_clauses, ort);
15599 if (linear_no_step)
15600 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15601 if (nc == linear_no_step)
15603 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15604 break;
15607 return new_clauses;
15610 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15612 static tree
15613 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15614 tree in_decl)
15616 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15618 tree purpose, value, chain;
15620 if (t == NULL)
15621 return t;
15623 if (TREE_CODE (t) != TREE_LIST)
15624 return tsubst_copy_and_build (t, args, complain, in_decl,
15625 /*function_p=*/false,
15626 /*integral_constant_expression_p=*/false);
15628 if (t == void_list_node)
15629 return t;
15631 purpose = TREE_PURPOSE (t);
15632 if (purpose)
15633 purpose = RECUR (purpose);
15634 value = TREE_VALUE (t);
15635 if (value)
15637 if (TREE_CODE (value) != LABEL_DECL)
15638 value = RECUR (value);
15639 else
15641 value = lookup_label (DECL_NAME (value));
15642 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15643 TREE_USED (value) = 1;
15646 chain = TREE_CHAIN (t);
15647 if (chain && chain != void_type_node)
15648 chain = RECUR (chain);
15649 return tree_cons (purpose, value, chain);
15650 #undef RECUR
15653 /* Used to temporarily communicate the list of #pragma omp parallel
15654 clauses to #pragma omp for instantiation if they are combined
15655 together. */
15657 static tree *omp_parallel_combined_clauses;
15659 /* Substitute one OMP_FOR iterator. */
15661 static void
15662 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15663 tree initv, tree condv, tree incrv, tree *clauses,
15664 tree args, tsubst_flags_t complain, tree in_decl,
15665 bool integral_constant_expression_p)
15667 #define RECUR(NODE) \
15668 tsubst_expr ((NODE), args, complain, in_decl, \
15669 integral_constant_expression_p)
15670 tree decl, init, cond, incr;
15672 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15673 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15675 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15677 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15678 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15681 decl = TREE_OPERAND (init, 0);
15682 init = TREE_OPERAND (init, 1);
15683 tree decl_expr = NULL_TREE;
15684 if (init && TREE_CODE (init) == DECL_EXPR)
15686 /* We need to jump through some hoops to handle declarations in the
15687 init-statement, since we might need to handle auto deduction,
15688 but we need to keep control of initialization. */
15689 decl_expr = init;
15690 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15691 decl = tsubst_decl (decl, args, complain);
15693 else
15695 if (TREE_CODE (decl) == SCOPE_REF)
15697 decl = RECUR (decl);
15698 if (TREE_CODE (decl) == COMPONENT_REF)
15700 tree v = decl;
15701 while (v)
15702 switch (TREE_CODE (v))
15704 case COMPONENT_REF:
15705 case MEM_REF:
15706 case INDIRECT_REF:
15707 CASE_CONVERT:
15708 case POINTER_PLUS_EXPR:
15709 v = TREE_OPERAND (v, 0);
15710 continue;
15711 case PARM_DECL:
15712 if (DECL_CONTEXT (v) == current_function_decl
15713 && DECL_ARTIFICIAL (v)
15714 && DECL_NAME (v) == this_identifier)
15716 decl = TREE_OPERAND (decl, 1);
15717 decl = omp_privatize_field (decl, false);
15719 /* FALLTHRU */
15720 default:
15721 v = NULL_TREE;
15722 break;
15726 else
15727 decl = RECUR (decl);
15729 init = RECUR (init);
15731 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15732 if (auto_node && init)
15733 TREE_TYPE (decl)
15734 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15736 gcc_assert (!type_dependent_expression_p (decl));
15738 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15740 if (decl_expr)
15742 /* Declare the variable, but don't let that initialize it. */
15743 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15744 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15745 RECUR (decl_expr);
15746 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15749 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15750 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15751 if (TREE_CODE (incr) == MODIFY_EXPR)
15753 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15754 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15755 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15756 NOP_EXPR, rhs, complain);
15758 else
15759 incr = RECUR (incr);
15760 TREE_VEC_ELT (declv, i) = decl;
15761 TREE_VEC_ELT (initv, i) = init;
15762 TREE_VEC_ELT (condv, i) = cond;
15763 TREE_VEC_ELT (incrv, i) = incr;
15764 return;
15767 if (decl_expr)
15769 /* Declare and initialize the variable. */
15770 RECUR (decl_expr);
15771 init = NULL_TREE;
15773 else if (init)
15775 tree *pc;
15776 int j;
15777 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15779 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15781 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15782 && OMP_CLAUSE_DECL (*pc) == decl)
15783 break;
15784 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15785 && OMP_CLAUSE_DECL (*pc) == decl)
15787 if (j)
15788 break;
15789 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15790 tree c = *pc;
15791 *pc = OMP_CLAUSE_CHAIN (c);
15792 OMP_CLAUSE_CHAIN (c) = *clauses;
15793 *clauses = c;
15795 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15796 && OMP_CLAUSE_DECL (*pc) == decl)
15798 error ("iteration variable %qD should not be firstprivate",
15799 decl);
15800 *pc = OMP_CLAUSE_CHAIN (*pc);
15802 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15803 && OMP_CLAUSE_DECL (*pc) == decl)
15805 error ("iteration variable %qD should not be reduction",
15806 decl);
15807 *pc = OMP_CLAUSE_CHAIN (*pc);
15809 else
15810 pc = &OMP_CLAUSE_CHAIN (*pc);
15812 if (*pc)
15813 break;
15815 if (*pc == NULL_TREE)
15817 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15818 OMP_CLAUSE_DECL (c) = decl;
15819 c = finish_omp_clauses (c, C_ORT_OMP);
15820 if (c)
15822 OMP_CLAUSE_CHAIN (c) = *clauses;
15823 *clauses = c;
15827 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15828 if (COMPARISON_CLASS_P (cond))
15830 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15831 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15832 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15834 else
15835 cond = RECUR (cond);
15836 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15837 switch (TREE_CODE (incr))
15839 case PREINCREMENT_EXPR:
15840 case PREDECREMENT_EXPR:
15841 case POSTINCREMENT_EXPR:
15842 case POSTDECREMENT_EXPR:
15843 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15844 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15845 break;
15846 case MODIFY_EXPR:
15847 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15848 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15850 tree rhs = TREE_OPERAND (incr, 1);
15851 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15852 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15853 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15854 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15855 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15856 rhs0, rhs1));
15858 else
15859 incr = RECUR (incr);
15860 break;
15861 case MODOP_EXPR:
15862 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15863 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15865 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15866 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15867 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15868 TREE_TYPE (decl), lhs,
15869 RECUR (TREE_OPERAND (incr, 2))));
15871 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15872 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15873 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15875 tree rhs = TREE_OPERAND (incr, 2);
15876 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15877 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15878 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15879 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15880 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15881 rhs0, rhs1));
15883 else
15884 incr = RECUR (incr);
15885 break;
15886 default:
15887 incr = RECUR (incr);
15888 break;
15891 TREE_VEC_ELT (declv, i) = decl;
15892 TREE_VEC_ELT (initv, i) = init;
15893 TREE_VEC_ELT (condv, i) = cond;
15894 TREE_VEC_ELT (incrv, i) = incr;
15895 #undef RECUR
15898 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15899 of OMP_TARGET's body. */
15901 static tree
15902 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15904 *walk_subtrees = 0;
15905 switch (TREE_CODE (*tp))
15907 case OMP_TEAMS:
15908 return *tp;
15909 case BIND_EXPR:
15910 case STATEMENT_LIST:
15911 *walk_subtrees = 1;
15912 break;
15913 default:
15914 break;
15916 return NULL_TREE;
15919 /* Helper function for tsubst_expr. For decomposition declaration
15920 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15921 also the corresponding decls representing the identifiers
15922 of the decomposition declaration. Return DECL if successful
15923 or error_mark_node otherwise, set *FIRST to the first decl
15924 in the list chained through DECL_CHAIN and *CNT to the number
15925 of such decls. */
15927 static tree
15928 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15929 tsubst_flags_t complain, tree in_decl, tree *first,
15930 unsigned int *cnt)
15932 tree decl2, decl3, prev = decl;
15933 *cnt = 0;
15934 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15935 for (decl2 = DECL_CHAIN (pattern_decl);
15936 decl2
15937 && VAR_P (decl2)
15938 && DECL_DECOMPOSITION_P (decl2)
15939 && DECL_NAME (decl2);
15940 decl2 = DECL_CHAIN (decl2))
15942 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15944 gcc_assert (errorcount);
15945 return error_mark_node;
15947 (*cnt)++;
15948 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15949 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15950 tree v = DECL_VALUE_EXPR (decl2);
15951 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15952 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15953 decl3 = tsubst (decl2, args, complain, in_decl);
15954 SET_DECL_VALUE_EXPR (decl2, v);
15955 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15956 if (VAR_P (decl3))
15957 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15958 maybe_push_decl (decl3);
15959 if (error_operand_p (decl3))
15960 decl = error_mark_node;
15961 else if (decl != error_mark_node
15962 && DECL_CHAIN (decl3) != prev)
15964 gcc_assert (errorcount);
15965 decl = error_mark_node;
15967 else
15968 prev = decl3;
15970 *first = prev;
15971 return decl;
15974 /* Like tsubst_copy for expressions, etc. but also does semantic
15975 processing. */
15977 tree
15978 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15979 bool integral_constant_expression_p)
15981 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15982 #define RECUR(NODE) \
15983 tsubst_expr ((NODE), args, complain, in_decl, \
15984 integral_constant_expression_p)
15986 tree stmt, tmp;
15987 tree r;
15988 location_t loc;
15990 if (t == NULL_TREE || t == error_mark_node)
15991 return t;
15993 loc = input_location;
15994 if (EXPR_HAS_LOCATION (t))
15995 input_location = EXPR_LOCATION (t);
15996 if (STATEMENT_CODE_P (TREE_CODE (t)))
15997 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15999 switch (TREE_CODE (t))
16001 case STATEMENT_LIST:
16003 tree_stmt_iterator i;
16004 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16005 RECUR (tsi_stmt (i));
16006 break;
16009 case CTOR_INITIALIZER:
16010 finish_mem_initializers (tsubst_initializer_list
16011 (TREE_OPERAND (t, 0), args));
16012 break;
16014 case RETURN_EXPR:
16015 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16016 break;
16018 case EXPR_STMT:
16019 tmp = RECUR (EXPR_STMT_EXPR (t));
16020 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16021 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16022 else
16023 finish_expr_stmt (tmp);
16024 break;
16026 case USING_STMT:
16027 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16028 /*attribs=*/NULL_TREE);
16029 break;
16031 case DECL_EXPR:
16033 tree decl, pattern_decl;
16034 tree init;
16036 pattern_decl = decl = DECL_EXPR_DECL (t);
16037 if (TREE_CODE (decl) == LABEL_DECL)
16038 finish_label_decl (DECL_NAME (decl));
16039 else if (TREE_CODE (decl) == USING_DECL)
16041 tree scope = USING_DECL_SCOPE (decl);
16042 tree name = DECL_NAME (decl);
16044 scope = tsubst (scope, args, complain, in_decl);
16045 decl = lookup_qualified_name (scope, name,
16046 /*is_type_p=*/false,
16047 /*complain=*/false);
16048 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16049 qualified_name_lookup_error (scope, name, decl, input_location);
16050 else
16051 finish_local_using_decl (decl, scope, name);
16053 else if (is_capture_proxy (decl)
16054 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16056 /* We're in tsubst_lambda_expr, we've already inserted a new
16057 capture proxy, so look it up and register it. */
16058 tree inst;
16059 if (DECL_PACK_P (decl))
16061 inst = (retrieve_local_specialization
16062 (DECL_CAPTURED_VARIABLE (decl)));
16063 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16065 else
16067 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16068 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16069 gcc_assert (inst != decl && is_capture_proxy (inst));
16071 register_local_specialization (inst, decl);
16072 break;
16074 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16075 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16076 /* Don't copy the old closure; we'll create a new one in
16077 tsubst_lambda_expr. */
16078 break;
16079 else
16081 init = DECL_INITIAL (decl);
16082 decl = tsubst (decl, args, complain, in_decl);
16083 if (decl != error_mark_node)
16085 /* By marking the declaration as instantiated, we avoid
16086 trying to instantiate it. Since instantiate_decl can't
16087 handle local variables, and since we've already done
16088 all that needs to be done, that's the right thing to
16089 do. */
16090 if (VAR_P (decl))
16091 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16092 if (VAR_P (decl)
16093 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16094 /* Anonymous aggregates are a special case. */
16095 finish_anon_union (decl);
16096 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16098 DECL_CONTEXT (decl) = current_function_decl;
16099 if (DECL_NAME (decl) == this_identifier)
16101 tree lam = DECL_CONTEXT (current_function_decl);
16102 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16103 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16105 insert_capture_proxy (decl);
16107 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16108 /* We already did a pushtag. */;
16109 else if (TREE_CODE (decl) == FUNCTION_DECL
16110 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16111 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16113 DECL_CONTEXT (decl) = NULL_TREE;
16114 pushdecl (decl);
16115 DECL_CONTEXT (decl) = current_function_decl;
16116 cp_check_omp_declare_reduction (decl);
16118 else
16120 int const_init = false;
16121 maybe_push_decl (decl);
16122 if (VAR_P (decl)
16123 && DECL_PRETTY_FUNCTION_P (decl))
16125 /* For __PRETTY_FUNCTION__ we have to adjust the
16126 initializer. */
16127 const char *const name
16128 = cxx_printable_name (current_function_decl, 2);
16129 init = cp_fname_init (name, &TREE_TYPE (decl));
16131 else
16132 init = tsubst_init (init, decl, args, complain, in_decl);
16134 if (VAR_P (decl))
16135 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16136 (pattern_decl));
16137 if (VAR_P (decl)
16138 && DECL_DECOMPOSITION_P (decl)
16139 && TREE_TYPE (pattern_decl) != error_mark_node)
16141 unsigned int cnt;
16142 tree first;
16143 tree ndecl
16144 = tsubst_decomp_names (decl, pattern_decl, args,
16145 complain, in_decl, &first, &cnt);
16146 if (ndecl != error_mark_node)
16147 cp_maybe_mangle_decomp (ndecl, first, cnt);
16148 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16149 if (ndecl != error_mark_node)
16150 cp_finish_decomp (ndecl, first, cnt);
16152 else
16153 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16158 break;
16161 case FOR_STMT:
16162 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16163 RECUR (FOR_INIT_STMT (t));
16164 finish_init_stmt (stmt);
16165 tmp = RECUR (FOR_COND (t));
16166 finish_for_cond (tmp, stmt, false, 0);
16167 tmp = RECUR (FOR_EXPR (t));
16168 finish_for_expr (tmp, stmt);
16170 bool prev = note_iteration_stmt_body_start ();
16171 RECUR (FOR_BODY (t));
16172 note_iteration_stmt_body_end (prev);
16174 finish_for_stmt (stmt);
16175 break;
16177 case RANGE_FOR_STMT:
16179 /* Construct another range_for, if this is not a final
16180 substitution (for inside inside a generic lambda of a
16181 template). Otherwise convert to a regular for. */
16182 tree decl, expr;
16183 stmt = (processing_template_decl
16184 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16185 : begin_for_stmt (NULL_TREE, NULL_TREE));
16186 decl = RANGE_FOR_DECL (t);
16187 decl = tsubst (decl, args, complain, in_decl);
16188 maybe_push_decl (decl);
16189 expr = RECUR (RANGE_FOR_EXPR (t));
16191 tree decomp_first = NULL_TREE;
16192 unsigned decomp_cnt = 0;
16193 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16194 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16195 complain, in_decl,
16196 &decomp_first, &decomp_cnt);
16198 if (processing_template_decl)
16200 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16201 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16202 finish_range_for_decl (stmt, decl, expr);
16204 else
16206 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16207 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16208 stmt = cp_convert_range_for (stmt, decl, expr,
16209 decomp_first, decomp_cnt,
16210 RANGE_FOR_IVDEP (t), unroll);
16213 bool prev = note_iteration_stmt_body_start ();
16214 RECUR (RANGE_FOR_BODY (t));
16215 note_iteration_stmt_body_end (prev);
16216 finish_for_stmt (stmt);
16218 break;
16220 case WHILE_STMT:
16221 stmt = begin_while_stmt ();
16222 tmp = RECUR (WHILE_COND (t));
16223 finish_while_stmt_cond (tmp, stmt, false, 0);
16225 bool prev = note_iteration_stmt_body_start ();
16226 RECUR (WHILE_BODY (t));
16227 note_iteration_stmt_body_end (prev);
16229 finish_while_stmt (stmt);
16230 break;
16232 case DO_STMT:
16233 stmt = begin_do_stmt ();
16235 bool prev = note_iteration_stmt_body_start ();
16236 RECUR (DO_BODY (t));
16237 note_iteration_stmt_body_end (prev);
16239 finish_do_body (stmt);
16240 tmp = RECUR (DO_COND (t));
16241 finish_do_stmt (tmp, stmt, false, 0);
16242 break;
16244 case IF_STMT:
16245 stmt = begin_if_stmt ();
16246 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16247 tmp = RECUR (IF_COND (t));
16248 tmp = finish_if_stmt_cond (tmp, stmt);
16249 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16250 /* Don't instantiate the THEN_CLAUSE. */;
16251 else
16253 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16254 if (inhibit)
16255 ++c_inhibit_evaluation_warnings;
16256 RECUR (THEN_CLAUSE (t));
16257 if (inhibit)
16258 --c_inhibit_evaluation_warnings;
16260 finish_then_clause (stmt);
16262 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16263 /* Don't instantiate the ELSE_CLAUSE. */;
16264 else if (ELSE_CLAUSE (t))
16266 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16267 begin_else_clause (stmt);
16268 if (inhibit)
16269 ++c_inhibit_evaluation_warnings;
16270 RECUR (ELSE_CLAUSE (t));
16271 if (inhibit)
16272 --c_inhibit_evaluation_warnings;
16273 finish_else_clause (stmt);
16276 finish_if_stmt (stmt);
16277 break;
16279 case BIND_EXPR:
16280 if (BIND_EXPR_BODY_BLOCK (t))
16281 stmt = begin_function_body ();
16282 else
16283 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16284 ? BCS_TRY_BLOCK : 0);
16286 RECUR (BIND_EXPR_BODY (t));
16288 if (BIND_EXPR_BODY_BLOCK (t))
16289 finish_function_body (stmt);
16290 else
16291 finish_compound_stmt (stmt);
16292 break;
16294 case BREAK_STMT:
16295 finish_break_stmt ();
16296 break;
16298 case CONTINUE_STMT:
16299 finish_continue_stmt ();
16300 break;
16302 case SWITCH_STMT:
16303 stmt = begin_switch_stmt ();
16304 tmp = RECUR (SWITCH_STMT_COND (t));
16305 finish_switch_cond (tmp, stmt);
16306 RECUR (SWITCH_STMT_BODY (t));
16307 finish_switch_stmt (stmt);
16308 break;
16310 case CASE_LABEL_EXPR:
16312 tree low = RECUR (CASE_LOW (t));
16313 tree high = RECUR (CASE_HIGH (t));
16314 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16315 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16316 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16317 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16319 break;
16321 case LABEL_EXPR:
16323 tree decl = LABEL_EXPR_LABEL (t);
16324 tree label;
16326 label = finish_label_stmt (DECL_NAME (decl));
16327 if (TREE_CODE (label) == LABEL_DECL)
16328 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16329 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16330 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16332 break;
16334 case GOTO_EXPR:
16335 tmp = GOTO_DESTINATION (t);
16336 if (TREE_CODE (tmp) != LABEL_DECL)
16337 /* Computed goto's must be tsubst'd into. On the other hand,
16338 non-computed gotos must not be; the identifier in question
16339 will have no binding. */
16340 tmp = RECUR (tmp);
16341 else
16342 tmp = DECL_NAME (tmp);
16343 finish_goto_stmt (tmp);
16344 break;
16346 case ASM_EXPR:
16348 tree string = RECUR (ASM_STRING (t));
16349 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16350 complain, in_decl);
16351 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16352 complain, in_decl);
16353 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16354 complain, in_decl);
16355 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16356 complain, in_decl);
16357 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16358 clobbers, labels);
16359 tree asm_expr = tmp;
16360 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16361 asm_expr = TREE_OPERAND (asm_expr, 0);
16362 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16364 break;
16366 case TRY_BLOCK:
16367 if (CLEANUP_P (t))
16369 stmt = begin_try_block ();
16370 RECUR (TRY_STMTS (t));
16371 finish_cleanup_try_block (stmt);
16372 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16374 else
16376 tree compound_stmt = NULL_TREE;
16378 if (FN_TRY_BLOCK_P (t))
16379 stmt = begin_function_try_block (&compound_stmt);
16380 else
16381 stmt = begin_try_block ();
16383 RECUR (TRY_STMTS (t));
16385 if (FN_TRY_BLOCK_P (t))
16386 finish_function_try_block (stmt);
16387 else
16388 finish_try_block (stmt);
16390 RECUR (TRY_HANDLERS (t));
16391 if (FN_TRY_BLOCK_P (t))
16392 finish_function_handler_sequence (stmt, compound_stmt);
16393 else
16394 finish_handler_sequence (stmt);
16396 break;
16398 case HANDLER:
16400 tree decl = HANDLER_PARMS (t);
16402 if (decl)
16404 decl = tsubst (decl, args, complain, in_decl);
16405 /* Prevent instantiate_decl from trying to instantiate
16406 this variable. We've already done all that needs to be
16407 done. */
16408 if (decl != error_mark_node)
16409 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16411 stmt = begin_handler ();
16412 finish_handler_parms (decl, stmt);
16413 RECUR (HANDLER_BODY (t));
16414 finish_handler (stmt);
16416 break;
16418 case TAG_DEFN:
16419 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16420 if (CLASS_TYPE_P (tmp))
16422 /* Local classes are not independent templates; they are
16423 instantiated along with their containing function. And this
16424 way we don't have to deal with pushing out of one local class
16425 to instantiate a member of another local class. */
16426 /* Closures are handled by the LAMBDA_EXPR. */
16427 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16428 complete_type (tmp);
16429 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16430 if ((VAR_P (fld)
16431 || (TREE_CODE (fld) == FUNCTION_DECL
16432 && !DECL_ARTIFICIAL (fld)))
16433 && DECL_TEMPLATE_INSTANTIATION (fld))
16434 instantiate_decl (fld, /*defer_ok=*/false,
16435 /*expl_inst_class=*/false);
16437 break;
16439 case STATIC_ASSERT:
16441 tree condition;
16443 ++c_inhibit_evaluation_warnings;
16444 condition =
16445 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16446 args,
16447 complain, in_decl,
16448 /*integral_constant_expression_p=*/true);
16449 --c_inhibit_evaluation_warnings;
16451 finish_static_assert (condition,
16452 STATIC_ASSERT_MESSAGE (t),
16453 STATIC_ASSERT_SOURCE_LOCATION (t),
16454 /*member_p=*/false);
16456 break;
16458 case OACC_KERNELS:
16459 case OACC_PARALLEL:
16460 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16461 in_decl);
16462 stmt = begin_omp_parallel ();
16463 RECUR (OMP_BODY (t));
16464 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16465 break;
16467 case OMP_PARALLEL:
16468 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16469 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16470 complain, in_decl);
16471 if (OMP_PARALLEL_COMBINED (t))
16472 omp_parallel_combined_clauses = &tmp;
16473 stmt = begin_omp_parallel ();
16474 RECUR (OMP_PARALLEL_BODY (t));
16475 gcc_assert (omp_parallel_combined_clauses == NULL);
16476 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16477 = OMP_PARALLEL_COMBINED (t);
16478 pop_omp_privatization_clauses (r);
16479 break;
16481 case OMP_TASK:
16482 r = push_omp_privatization_clauses (false);
16483 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16484 complain, in_decl);
16485 stmt = begin_omp_task ();
16486 RECUR (OMP_TASK_BODY (t));
16487 finish_omp_task (tmp, stmt);
16488 pop_omp_privatization_clauses (r);
16489 break;
16491 case OMP_FOR:
16492 case OMP_SIMD:
16493 case OMP_DISTRIBUTE:
16494 case OMP_TASKLOOP:
16495 case OACC_LOOP:
16497 tree clauses, body, pre_body;
16498 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16499 tree orig_declv = NULL_TREE;
16500 tree incrv = NULL_TREE;
16501 enum c_omp_region_type ort = C_ORT_OMP;
16502 int i;
16504 if (TREE_CODE (t) == OACC_LOOP)
16505 ort = C_ORT_ACC;
16507 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16508 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16509 in_decl);
16510 if (OMP_FOR_INIT (t) != NULL_TREE)
16512 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16513 if (OMP_FOR_ORIG_DECLS (t))
16514 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16515 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16516 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16517 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16520 stmt = begin_omp_structured_block ();
16522 pre_body = push_stmt_list ();
16523 RECUR (OMP_FOR_PRE_BODY (t));
16524 pre_body = pop_stmt_list (pre_body);
16526 if (OMP_FOR_INIT (t) != NULL_TREE)
16527 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16528 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16529 incrv, &clauses, args, complain, in_decl,
16530 integral_constant_expression_p);
16531 omp_parallel_combined_clauses = NULL;
16533 body = push_stmt_list ();
16534 RECUR (OMP_FOR_BODY (t));
16535 body = pop_stmt_list (body);
16537 if (OMP_FOR_INIT (t) != NULL_TREE)
16538 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16539 orig_declv, initv, condv, incrv, body, pre_body,
16540 NULL, clauses);
16541 else
16543 t = make_node (TREE_CODE (t));
16544 TREE_TYPE (t) = void_type_node;
16545 OMP_FOR_BODY (t) = body;
16546 OMP_FOR_PRE_BODY (t) = pre_body;
16547 OMP_FOR_CLAUSES (t) = clauses;
16548 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16549 add_stmt (t);
16552 add_stmt (finish_omp_structured_block (stmt));
16553 pop_omp_privatization_clauses (r);
16555 break;
16557 case OMP_SECTIONS:
16558 omp_parallel_combined_clauses = NULL;
16559 /* FALLTHRU */
16560 case OMP_SINGLE:
16561 case OMP_TEAMS:
16562 case OMP_CRITICAL:
16563 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16564 && OMP_TEAMS_COMBINED (t));
16565 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16566 in_decl);
16567 stmt = push_stmt_list ();
16568 RECUR (OMP_BODY (t));
16569 stmt = pop_stmt_list (stmt);
16571 t = copy_node (t);
16572 OMP_BODY (t) = stmt;
16573 OMP_CLAUSES (t) = tmp;
16574 add_stmt (t);
16575 pop_omp_privatization_clauses (r);
16576 break;
16578 case OACC_DATA:
16579 case OMP_TARGET_DATA:
16580 case OMP_TARGET:
16581 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16582 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16583 in_decl);
16584 keep_next_level (true);
16585 stmt = begin_omp_structured_block ();
16587 RECUR (OMP_BODY (t));
16588 stmt = finish_omp_structured_block (stmt);
16590 t = copy_node (t);
16591 OMP_BODY (t) = stmt;
16592 OMP_CLAUSES (t) = tmp;
16593 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16595 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16596 if (teams)
16598 /* For combined target teams, ensure the num_teams and
16599 thread_limit clause expressions are evaluated on the host,
16600 before entering the target construct. */
16601 tree c;
16602 for (c = OMP_TEAMS_CLAUSES (teams);
16603 c; c = OMP_CLAUSE_CHAIN (c))
16604 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16605 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16606 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16608 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16609 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16610 if (expr == error_mark_node)
16611 continue;
16612 tmp = TARGET_EXPR_SLOT (expr);
16613 add_stmt (expr);
16614 OMP_CLAUSE_OPERAND (c, 0) = expr;
16615 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16616 OMP_CLAUSE_FIRSTPRIVATE);
16617 OMP_CLAUSE_DECL (tc) = tmp;
16618 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16619 OMP_TARGET_CLAUSES (t) = tc;
16623 add_stmt (t);
16624 break;
16626 case OACC_DECLARE:
16627 t = copy_node (t);
16628 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16629 complain, in_decl);
16630 OACC_DECLARE_CLAUSES (t) = tmp;
16631 add_stmt (t);
16632 break;
16634 case OMP_TARGET_UPDATE:
16635 case OMP_TARGET_ENTER_DATA:
16636 case OMP_TARGET_EXIT_DATA:
16637 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16638 complain, in_decl);
16639 t = copy_node (t);
16640 OMP_STANDALONE_CLAUSES (t) = tmp;
16641 add_stmt (t);
16642 break;
16644 case OACC_ENTER_DATA:
16645 case OACC_EXIT_DATA:
16646 case OACC_UPDATE:
16647 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16648 complain, in_decl);
16649 t = copy_node (t);
16650 OMP_STANDALONE_CLAUSES (t) = tmp;
16651 add_stmt (t);
16652 break;
16654 case OMP_ORDERED:
16655 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16656 complain, in_decl);
16657 stmt = push_stmt_list ();
16658 RECUR (OMP_BODY (t));
16659 stmt = pop_stmt_list (stmt);
16661 t = copy_node (t);
16662 OMP_BODY (t) = stmt;
16663 OMP_ORDERED_CLAUSES (t) = tmp;
16664 add_stmt (t);
16665 break;
16667 case OMP_SECTION:
16668 case OMP_MASTER:
16669 case OMP_TASKGROUP:
16670 stmt = push_stmt_list ();
16671 RECUR (OMP_BODY (t));
16672 stmt = pop_stmt_list (stmt);
16674 t = copy_node (t);
16675 OMP_BODY (t) = stmt;
16676 add_stmt (t);
16677 break;
16679 case OMP_ATOMIC:
16680 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16681 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16683 tree op1 = TREE_OPERAND (t, 1);
16684 tree rhs1 = NULL_TREE;
16685 tree lhs, rhs;
16686 if (TREE_CODE (op1) == COMPOUND_EXPR)
16688 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16689 op1 = TREE_OPERAND (op1, 1);
16691 lhs = RECUR (TREE_OPERAND (op1, 0));
16692 rhs = RECUR (TREE_OPERAND (op1, 1));
16693 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16694 NULL_TREE, NULL_TREE, rhs1,
16695 OMP_ATOMIC_SEQ_CST (t));
16697 else
16699 tree op1 = TREE_OPERAND (t, 1);
16700 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16701 tree rhs1 = NULL_TREE;
16702 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16703 enum tree_code opcode = NOP_EXPR;
16704 if (code == OMP_ATOMIC_READ)
16706 v = RECUR (TREE_OPERAND (op1, 0));
16707 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16709 else if (code == OMP_ATOMIC_CAPTURE_OLD
16710 || code == OMP_ATOMIC_CAPTURE_NEW)
16712 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16713 v = RECUR (TREE_OPERAND (op1, 0));
16714 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16715 if (TREE_CODE (op11) == COMPOUND_EXPR)
16717 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16718 op11 = TREE_OPERAND (op11, 1);
16720 lhs = RECUR (TREE_OPERAND (op11, 0));
16721 rhs = RECUR (TREE_OPERAND (op11, 1));
16722 opcode = TREE_CODE (op11);
16723 if (opcode == MODIFY_EXPR)
16724 opcode = NOP_EXPR;
16726 else
16728 code = OMP_ATOMIC;
16729 lhs = RECUR (TREE_OPERAND (op1, 0));
16730 rhs = RECUR (TREE_OPERAND (op1, 1));
16732 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16733 OMP_ATOMIC_SEQ_CST (t));
16735 break;
16737 case TRANSACTION_EXPR:
16739 int flags = 0;
16740 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16741 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16743 if (TRANSACTION_EXPR_IS_STMT (t))
16745 tree body = TRANSACTION_EXPR_BODY (t);
16746 tree noex = NULL_TREE;
16747 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16749 noex = MUST_NOT_THROW_COND (body);
16750 if (noex == NULL_TREE)
16751 noex = boolean_true_node;
16752 body = TREE_OPERAND (body, 0);
16754 stmt = begin_transaction_stmt (input_location, NULL, flags);
16755 RECUR (body);
16756 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16758 else
16760 stmt = build_transaction_expr (EXPR_LOCATION (t),
16761 RECUR (TRANSACTION_EXPR_BODY (t)),
16762 flags, NULL_TREE);
16763 RETURN (stmt);
16766 break;
16768 case MUST_NOT_THROW_EXPR:
16770 tree op0 = RECUR (TREE_OPERAND (t, 0));
16771 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16772 RETURN (build_must_not_throw_expr (op0, cond));
16775 case EXPR_PACK_EXPANSION:
16776 error ("invalid use of pack expansion expression");
16777 RETURN (error_mark_node);
16779 case NONTYPE_ARGUMENT_PACK:
16780 error ("use %<...%> to expand argument pack");
16781 RETURN (error_mark_node);
16783 case COMPOUND_EXPR:
16784 tmp = RECUR (TREE_OPERAND (t, 0));
16785 if (tmp == NULL_TREE)
16786 /* If the first operand was a statement, we're done with it. */
16787 RETURN (RECUR (TREE_OPERAND (t, 1)));
16788 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16789 RECUR (TREE_OPERAND (t, 1)),
16790 complain));
16792 case ANNOTATE_EXPR:
16793 tmp = RECUR (TREE_OPERAND (t, 0));
16794 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16795 TREE_TYPE (tmp), tmp,
16796 RECUR (TREE_OPERAND (t, 1)),
16797 RECUR (TREE_OPERAND (t, 2))));
16799 default:
16800 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16802 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16803 /*function_p=*/false,
16804 integral_constant_expression_p));
16807 RETURN (NULL_TREE);
16808 out:
16809 input_location = loc;
16810 return r;
16811 #undef RECUR
16812 #undef RETURN
16815 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16816 function. For description of the body see comment above
16817 cp_parser_omp_declare_reduction_exprs. */
16819 static void
16820 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16822 if (t == NULL_TREE || t == error_mark_node)
16823 return;
16825 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16827 tree_stmt_iterator tsi;
16828 int i;
16829 tree stmts[7];
16830 memset (stmts, 0, sizeof stmts);
16831 for (i = 0, tsi = tsi_start (t);
16832 i < 7 && !tsi_end_p (tsi);
16833 i++, tsi_next (&tsi))
16834 stmts[i] = tsi_stmt (tsi);
16835 gcc_assert (tsi_end_p (tsi));
16837 if (i >= 3)
16839 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16840 && TREE_CODE (stmts[1]) == DECL_EXPR);
16841 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16842 args, complain, in_decl);
16843 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16844 args, complain, in_decl);
16845 DECL_CONTEXT (omp_out) = current_function_decl;
16846 DECL_CONTEXT (omp_in) = current_function_decl;
16847 keep_next_level (true);
16848 tree block = begin_omp_structured_block ();
16849 tsubst_expr (stmts[2], args, complain, in_decl, false);
16850 block = finish_omp_structured_block (block);
16851 block = maybe_cleanup_point_expr_void (block);
16852 add_decl_expr (omp_out);
16853 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16854 TREE_NO_WARNING (omp_out) = 1;
16855 add_decl_expr (omp_in);
16856 finish_expr_stmt (block);
16858 if (i >= 6)
16860 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16861 && TREE_CODE (stmts[4]) == DECL_EXPR);
16862 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16863 args, complain, in_decl);
16864 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16865 args, complain, in_decl);
16866 DECL_CONTEXT (omp_priv) = current_function_decl;
16867 DECL_CONTEXT (omp_orig) = current_function_decl;
16868 keep_next_level (true);
16869 tree block = begin_omp_structured_block ();
16870 tsubst_expr (stmts[5], args, complain, in_decl, false);
16871 block = finish_omp_structured_block (block);
16872 block = maybe_cleanup_point_expr_void (block);
16873 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16874 add_decl_expr (omp_priv);
16875 add_decl_expr (omp_orig);
16876 finish_expr_stmt (block);
16877 if (i == 7)
16878 add_decl_expr (omp_orig);
16882 /* T is a postfix-expression that is not being used in a function
16883 call. Return the substituted version of T. */
16885 static tree
16886 tsubst_non_call_postfix_expression (tree t, tree args,
16887 tsubst_flags_t complain,
16888 tree in_decl)
16890 if (TREE_CODE (t) == SCOPE_REF)
16891 t = tsubst_qualified_id (t, args, complain, in_decl,
16892 /*done=*/false, /*address_p=*/false);
16893 else
16894 t = tsubst_copy_and_build (t, args, complain, in_decl,
16895 /*function_p=*/false,
16896 /*integral_constant_expression_p=*/false);
16898 return t;
16901 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16902 instantiation context. Instantiating a pack expansion containing a lambda
16903 might result in multiple lambdas all based on the same lambda in the
16904 template. */
16906 tree
16907 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16909 tree oldfn = lambda_function (t);
16910 in_decl = oldfn;
16912 tree r = build_lambda_expr ();
16914 LAMBDA_EXPR_LOCATION (r)
16915 = LAMBDA_EXPR_LOCATION (t);
16916 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16917 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16918 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16920 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16921 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16922 else
16923 record_lambda_scope (r);
16925 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16926 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16928 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16929 cap = TREE_CHAIN (cap))
16931 tree field = TREE_PURPOSE (cap);
16932 if (PACK_EXPANSION_P (field))
16933 field = PACK_EXPANSION_PATTERN (field);
16934 field = tsubst_decl (field, args, complain);
16936 if (field == error_mark_node)
16937 return error_mark_node;
16939 tree init = TREE_VALUE (cap);
16940 if (PACK_EXPANSION_P (init))
16941 init = tsubst_pack_expansion (init, args, complain, in_decl);
16942 else
16943 init = tsubst_copy_and_build (init, args, complain, in_decl,
16944 /*fn*/false, /*constexpr*/false);
16946 if (TREE_CODE (field) == TREE_VEC)
16948 int len = TREE_VEC_LENGTH (field);
16949 gcc_assert (TREE_CODE (init) == TREE_VEC
16950 && TREE_VEC_LENGTH (init) == len);
16951 for (int i = 0; i < len; ++i)
16952 LAMBDA_EXPR_CAPTURE_LIST (r)
16953 = tree_cons (TREE_VEC_ELT (field, i),
16954 TREE_VEC_ELT (init, i),
16955 LAMBDA_EXPR_CAPTURE_LIST (r));
16957 else
16959 LAMBDA_EXPR_CAPTURE_LIST (r)
16960 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16962 if (id_equal (DECL_NAME (field), "__this"))
16963 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16967 tree type = begin_lambda_type (r);
16969 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16970 determine_visibility (TYPE_NAME (type));
16972 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16974 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16975 ? DECL_TI_TEMPLATE (oldfn)
16976 : NULL_TREE);
16978 tree fntype = static_fn_type (oldfn);
16979 if (oldtmpl)
16980 ++processing_template_decl;
16981 fntype = tsubst (fntype, args, complain, in_decl);
16982 if (oldtmpl)
16983 --processing_template_decl;
16985 if (fntype == error_mark_node)
16986 r = error_mark_node;
16987 else
16989 /* Fix the type of 'this'. */
16990 fntype = build_memfn_type (fntype, type,
16991 type_memfn_quals (fntype),
16992 type_memfn_rqual (fntype));
16993 tree fn, tmpl;
16994 if (oldtmpl)
16996 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16997 fn = DECL_TEMPLATE_RESULT (tmpl);
16998 finish_member_declaration (tmpl);
17000 else
17002 tmpl = NULL_TREE;
17003 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17004 finish_member_declaration (fn);
17007 /* Let finish_function set this. */
17008 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17010 bool nested = cfun;
17011 if (nested)
17012 push_function_context ();
17014 local_specialization_stack s (lss_copy);
17016 tree body = start_lambda_function (fn, r);
17018 register_parameter_specializations (oldfn, fn);
17020 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17021 /*constexpr*/false);
17023 finish_lambda_function (body);
17025 if (nested)
17026 pop_function_context ();
17028 /* The capture list was built up in reverse order; fix that now. */
17029 LAMBDA_EXPR_CAPTURE_LIST (r)
17030 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17032 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17034 maybe_add_lambda_conv_op (type);
17037 finish_struct (type, /*attr*/NULL_TREE);
17039 insert_pending_capture_proxies ();
17041 return r;
17044 /* Like tsubst but deals with expressions and performs semantic
17045 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17047 tree
17048 tsubst_copy_and_build (tree t,
17049 tree args,
17050 tsubst_flags_t complain,
17051 tree in_decl,
17052 bool function_p,
17053 bool integral_constant_expression_p)
17055 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17056 #define RECUR(NODE) \
17057 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17058 /*function_p=*/false, \
17059 integral_constant_expression_p)
17061 tree retval, op1;
17062 location_t loc;
17064 if (t == NULL_TREE || t == error_mark_node)
17065 return t;
17067 loc = input_location;
17068 if (EXPR_HAS_LOCATION (t))
17069 input_location = EXPR_LOCATION (t);
17071 /* N3276 decltype magic only applies to calls at the top level or on the
17072 right side of a comma. */
17073 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17074 complain &= ~tf_decltype;
17076 switch (TREE_CODE (t))
17078 case USING_DECL:
17079 t = DECL_NAME (t);
17080 /* Fall through. */
17081 case IDENTIFIER_NODE:
17083 tree decl;
17084 cp_id_kind idk;
17085 bool non_integral_constant_expression_p;
17086 const char *error_msg;
17088 if (IDENTIFIER_CONV_OP_P (t))
17090 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17091 t = make_conv_op_name (new_type);
17094 /* Look up the name. */
17095 decl = lookup_name (t);
17097 /* By convention, expressions use ERROR_MARK_NODE to indicate
17098 failure, not NULL_TREE. */
17099 if (decl == NULL_TREE)
17100 decl = error_mark_node;
17102 decl = finish_id_expression (t, decl, NULL_TREE,
17103 &idk,
17104 integral_constant_expression_p,
17105 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17106 &non_integral_constant_expression_p,
17107 /*template_p=*/false,
17108 /*done=*/true,
17109 /*address_p=*/false,
17110 /*template_arg_p=*/false,
17111 &error_msg,
17112 input_location);
17113 if (error_msg)
17114 error (error_msg);
17115 if (!function_p && identifier_p (decl))
17117 if (complain & tf_error)
17118 unqualified_name_lookup_error (decl);
17119 decl = error_mark_node;
17121 RETURN (decl);
17124 case TEMPLATE_ID_EXPR:
17126 tree object;
17127 tree templ = RECUR (TREE_OPERAND (t, 0));
17128 tree targs = TREE_OPERAND (t, 1);
17130 if (targs)
17131 targs = tsubst_template_args (targs, args, complain, in_decl);
17132 if (targs == error_mark_node)
17133 return error_mark_node;
17135 if (TREE_CODE (templ) == SCOPE_REF)
17137 tree name = TREE_OPERAND (templ, 1);
17138 tree tid = lookup_template_function (name, targs);
17139 TREE_OPERAND (templ, 1) = tid;
17140 return templ;
17143 if (variable_template_p (templ))
17144 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17146 if (TREE_CODE (templ) == COMPONENT_REF)
17148 object = TREE_OPERAND (templ, 0);
17149 templ = TREE_OPERAND (templ, 1);
17151 else
17152 object = NULL_TREE;
17153 templ = lookup_template_function (templ, targs);
17155 if (object)
17156 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17157 object, templ, NULL_TREE));
17158 else
17159 RETURN (baselink_for_fns (templ));
17162 case INDIRECT_REF:
17164 tree r = RECUR (TREE_OPERAND (t, 0));
17166 if (REFERENCE_REF_P (t))
17168 /* A type conversion to reference type will be enclosed in
17169 such an indirect ref, but the substitution of the cast
17170 will have also added such an indirect ref. */
17171 r = convert_from_reference (r);
17173 else
17174 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17175 complain|decltype_flag);
17177 if (REF_PARENTHESIZED_P (t))
17178 r = force_paren_expr (r);
17180 RETURN (r);
17183 case NOP_EXPR:
17185 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17186 tree op0 = RECUR (TREE_OPERAND (t, 0));
17187 RETURN (build_nop (type, op0));
17190 case IMPLICIT_CONV_EXPR:
17192 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17193 tree expr = RECUR (TREE_OPERAND (t, 0));
17194 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17196 retval = copy_node (t);
17197 TREE_TYPE (retval) = type;
17198 TREE_OPERAND (retval, 0) = expr;
17199 RETURN (retval);
17201 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17202 /* We'll pass this to convert_nontype_argument again, we don't need
17203 to actually perform any conversion here. */
17204 RETURN (expr);
17205 int flags = LOOKUP_IMPLICIT;
17206 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17207 flags = LOOKUP_NORMAL;
17208 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17209 flags));
17212 case CONVERT_EXPR:
17214 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17215 tree op0 = RECUR (TREE_OPERAND (t, 0));
17216 RETURN (build1 (CONVERT_EXPR, type, op0));
17219 case CAST_EXPR:
17220 case REINTERPRET_CAST_EXPR:
17221 case CONST_CAST_EXPR:
17222 case DYNAMIC_CAST_EXPR:
17223 case STATIC_CAST_EXPR:
17225 tree type;
17226 tree op, r = NULL_TREE;
17228 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17229 if (integral_constant_expression_p
17230 && !cast_valid_in_integral_constant_expression_p (type))
17232 if (complain & tf_error)
17233 error ("a cast to a type other than an integral or "
17234 "enumeration type cannot appear in a constant-expression");
17235 RETURN (error_mark_node);
17238 op = RECUR (TREE_OPERAND (t, 0));
17240 warning_sentinel s(warn_useless_cast);
17241 warning_sentinel s2(warn_ignored_qualifiers);
17242 switch (TREE_CODE (t))
17244 case CAST_EXPR:
17245 r = build_functional_cast (type, op, complain);
17246 break;
17247 case REINTERPRET_CAST_EXPR:
17248 r = build_reinterpret_cast (type, op, complain);
17249 break;
17250 case CONST_CAST_EXPR:
17251 r = build_const_cast (type, op, complain);
17252 break;
17253 case DYNAMIC_CAST_EXPR:
17254 r = build_dynamic_cast (type, op, complain);
17255 break;
17256 case STATIC_CAST_EXPR:
17257 r = build_static_cast (type, op, complain);
17258 break;
17259 default:
17260 gcc_unreachable ();
17263 RETURN (r);
17266 case POSTDECREMENT_EXPR:
17267 case POSTINCREMENT_EXPR:
17268 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17269 args, complain, in_decl);
17270 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17271 complain|decltype_flag));
17273 case PREDECREMENT_EXPR:
17274 case PREINCREMENT_EXPR:
17275 case NEGATE_EXPR:
17276 case BIT_NOT_EXPR:
17277 case ABS_EXPR:
17278 case TRUTH_NOT_EXPR:
17279 case UNARY_PLUS_EXPR: /* Unary + */
17280 case REALPART_EXPR:
17281 case IMAGPART_EXPR:
17282 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17283 RECUR (TREE_OPERAND (t, 0)),
17284 complain|decltype_flag));
17286 case FIX_TRUNC_EXPR:
17287 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17288 false, complain));
17290 case ADDR_EXPR:
17291 op1 = TREE_OPERAND (t, 0);
17292 if (TREE_CODE (op1) == LABEL_DECL)
17293 RETURN (finish_label_address_expr (DECL_NAME (op1),
17294 EXPR_LOCATION (op1)));
17295 if (TREE_CODE (op1) == SCOPE_REF)
17296 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17297 /*done=*/true, /*address_p=*/true);
17298 else
17299 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17300 in_decl);
17301 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17302 complain|decltype_flag));
17304 case PLUS_EXPR:
17305 case MINUS_EXPR:
17306 case MULT_EXPR:
17307 case TRUNC_DIV_EXPR:
17308 case CEIL_DIV_EXPR:
17309 case FLOOR_DIV_EXPR:
17310 case ROUND_DIV_EXPR:
17311 case EXACT_DIV_EXPR:
17312 case BIT_AND_EXPR:
17313 case BIT_IOR_EXPR:
17314 case BIT_XOR_EXPR:
17315 case TRUNC_MOD_EXPR:
17316 case FLOOR_MOD_EXPR:
17317 case TRUTH_ANDIF_EXPR:
17318 case TRUTH_ORIF_EXPR:
17319 case TRUTH_AND_EXPR:
17320 case TRUTH_OR_EXPR:
17321 case RSHIFT_EXPR:
17322 case LSHIFT_EXPR:
17323 case RROTATE_EXPR:
17324 case LROTATE_EXPR:
17325 case EQ_EXPR:
17326 case NE_EXPR:
17327 case MAX_EXPR:
17328 case MIN_EXPR:
17329 case LE_EXPR:
17330 case GE_EXPR:
17331 case LT_EXPR:
17332 case GT_EXPR:
17333 case MEMBER_REF:
17334 case DOTSTAR_EXPR:
17336 warning_sentinel s1(warn_type_limits);
17337 warning_sentinel s2(warn_div_by_zero);
17338 warning_sentinel s3(warn_logical_op);
17339 warning_sentinel s4(warn_tautological_compare);
17340 tree op0 = RECUR (TREE_OPERAND (t, 0));
17341 tree op1 = RECUR (TREE_OPERAND (t, 1));
17342 tree r = build_x_binary_op
17343 (input_location, TREE_CODE (t),
17344 op0,
17345 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17346 ? ERROR_MARK
17347 : TREE_CODE (TREE_OPERAND (t, 0))),
17348 op1,
17349 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17350 ? ERROR_MARK
17351 : TREE_CODE (TREE_OPERAND (t, 1))),
17352 /*overload=*/NULL,
17353 complain|decltype_flag);
17354 if (EXPR_P (r) && TREE_NO_WARNING (t))
17355 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17357 RETURN (r);
17360 case POINTER_PLUS_EXPR:
17362 tree op0 = RECUR (TREE_OPERAND (t, 0));
17363 tree op1 = RECUR (TREE_OPERAND (t, 1));
17364 return fold_build_pointer_plus (op0, op1);
17367 case SCOPE_REF:
17368 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17369 /*address_p=*/false));
17370 case ARRAY_REF:
17371 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17372 args, complain, in_decl);
17373 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17374 RECUR (TREE_OPERAND (t, 1)),
17375 complain|decltype_flag));
17377 case SIZEOF_EXPR:
17378 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17379 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17380 RETURN (tsubst_copy (t, args, complain, in_decl));
17381 /* Fall through */
17383 case ALIGNOF_EXPR:
17385 tree r;
17387 op1 = TREE_OPERAND (t, 0);
17388 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17389 op1 = TREE_TYPE (op1);
17390 if (!args)
17392 /* When there are no ARGS, we are trying to evaluate a
17393 non-dependent expression from the parser. Trying to do
17394 the substitutions may not work. */
17395 if (!TYPE_P (op1))
17396 op1 = TREE_TYPE (op1);
17398 else
17400 ++cp_unevaluated_operand;
17401 ++c_inhibit_evaluation_warnings;
17402 if (TYPE_P (op1))
17403 op1 = tsubst (op1, args, complain, in_decl);
17404 else
17405 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17406 /*function_p=*/false,
17407 /*integral_constant_expression_p=*/
17408 false);
17409 --cp_unevaluated_operand;
17410 --c_inhibit_evaluation_warnings;
17412 if (TYPE_P (op1))
17413 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17414 complain & tf_error);
17415 else
17416 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17417 complain & tf_error);
17418 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17420 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17422 if (!processing_template_decl && TYPE_P (op1))
17424 r = build_min (SIZEOF_EXPR, size_type_node,
17425 build1 (NOP_EXPR, op1, error_mark_node));
17426 SIZEOF_EXPR_TYPE_P (r) = 1;
17428 else
17429 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17430 TREE_SIDE_EFFECTS (r) = 0;
17431 TREE_READONLY (r) = 1;
17433 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17435 RETURN (r);
17438 case AT_ENCODE_EXPR:
17440 op1 = TREE_OPERAND (t, 0);
17441 ++cp_unevaluated_operand;
17442 ++c_inhibit_evaluation_warnings;
17443 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17444 /*function_p=*/false,
17445 /*integral_constant_expression_p=*/false);
17446 --cp_unevaluated_operand;
17447 --c_inhibit_evaluation_warnings;
17448 RETURN (objc_build_encode_expr (op1));
17451 case NOEXCEPT_EXPR:
17452 op1 = TREE_OPERAND (t, 0);
17453 ++cp_unevaluated_operand;
17454 ++c_inhibit_evaluation_warnings;
17455 ++cp_noexcept_operand;
17456 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17457 /*function_p=*/false,
17458 /*integral_constant_expression_p=*/false);
17459 --cp_unevaluated_operand;
17460 --c_inhibit_evaluation_warnings;
17461 --cp_noexcept_operand;
17462 RETURN (finish_noexcept_expr (op1, complain));
17464 case MODOP_EXPR:
17466 warning_sentinel s(warn_div_by_zero);
17467 tree lhs = RECUR (TREE_OPERAND (t, 0));
17468 tree rhs = RECUR (TREE_OPERAND (t, 2));
17469 tree r = build_x_modify_expr
17470 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17471 complain|decltype_flag);
17472 /* TREE_NO_WARNING must be set if either the expression was
17473 parenthesized or it uses an operator such as >>= rather
17474 than plain assignment. In the former case, it was already
17475 set and must be copied. In the latter case,
17476 build_x_modify_expr sets it and it must not be reset
17477 here. */
17478 if (TREE_NO_WARNING (t))
17479 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17481 RETURN (r);
17484 case ARROW_EXPR:
17485 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17486 args, complain, in_decl);
17487 /* Remember that there was a reference to this entity. */
17488 if (DECL_P (op1)
17489 && !mark_used (op1, complain) && !(complain & tf_error))
17490 RETURN (error_mark_node);
17491 RETURN (build_x_arrow (input_location, op1, complain));
17493 case NEW_EXPR:
17495 tree placement = RECUR (TREE_OPERAND (t, 0));
17496 tree init = RECUR (TREE_OPERAND (t, 3));
17497 vec<tree, va_gc> *placement_vec;
17498 vec<tree, va_gc> *init_vec;
17499 tree ret;
17501 if (placement == NULL_TREE)
17502 placement_vec = NULL;
17503 else
17505 placement_vec = make_tree_vector ();
17506 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17507 vec_safe_push (placement_vec, TREE_VALUE (placement));
17510 /* If there was an initializer in the original tree, but it
17511 instantiated to an empty list, then we should pass a
17512 non-NULL empty vector to tell build_new that it was an
17513 empty initializer() rather than no initializer. This can
17514 only happen when the initializer is a pack expansion whose
17515 parameter packs are of length zero. */
17516 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17517 init_vec = NULL;
17518 else
17520 init_vec = make_tree_vector ();
17521 if (init == void_node)
17522 gcc_assert (init_vec != NULL);
17523 else
17525 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17526 vec_safe_push (init_vec, TREE_VALUE (init));
17530 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17531 tree op2 = RECUR (TREE_OPERAND (t, 2));
17532 ret = build_new (&placement_vec, op1, op2, &init_vec,
17533 NEW_EXPR_USE_GLOBAL (t),
17534 complain);
17536 if (placement_vec != NULL)
17537 release_tree_vector (placement_vec);
17538 if (init_vec != NULL)
17539 release_tree_vector (init_vec);
17541 RETURN (ret);
17544 case DELETE_EXPR:
17546 tree op0 = RECUR (TREE_OPERAND (t, 0));
17547 tree op1 = RECUR (TREE_OPERAND (t, 1));
17548 RETURN (delete_sanity (op0, op1,
17549 DELETE_EXPR_USE_VEC (t),
17550 DELETE_EXPR_USE_GLOBAL (t),
17551 complain));
17554 case COMPOUND_EXPR:
17556 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17557 complain & ~tf_decltype, in_decl,
17558 /*function_p=*/false,
17559 integral_constant_expression_p);
17560 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17561 op0,
17562 RECUR (TREE_OPERAND (t, 1)),
17563 complain|decltype_flag));
17566 case CALL_EXPR:
17568 tree function;
17569 vec<tree, va_gc> *call_args;
17570 unsigned int nargs, i;
17571 bool qualified_p;
17572 bool koenig_p;
17573 tree ret;
17575 function = CALL_EXPR_FN (t);
17576 /* Internal function with no arguments. */
17577 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17578 RETURN (t);
17580 /* When we parsed the expression, we determined whether or
17581 not Koenig lookup should be performed. */
17582 koenig_p = KOENIG_LOOKUP_P (t);
17583 if (function == NULL_TREE)
17585 koenig_p = false;
17586 qualified_p = false;
17588 else if (TREE_CODE (function) == SCOPE_REF)
17590 qualified_p = true;
17591 function = tsubst_qualified_id (function, args, complain, in_decl,
17592 /*done=*/false,
17593 /*address_p=*/false);
17595 else if (koenig_p && identifier_p (function))
17597 /* Do nothing; calling tsubst_copy_and_build on an identifier
17598 would incorrectly perform unqualified lookup again.
17600 Note that we can also have an IDENTIFIER_NODE if the earlier
17601 unqualified lookup found a member function; in that case
17602 koenig_p will be false and we do want to do the lookup
17603 again to find the instantiated member function.
17605 FIXME but doing that causes c++/15272, so we need to stop
17606 using IDENTIFIER_NODE in that situation. */
17607 qualified_p = false;
17609 else
17611 if (TREE_CODE (function) == COMPONENT_REF)
17613 tree op = TREE_OPERAND (function, 1);
17615 qualified_p = (TREE_CODE (op) == SCOPE_REF
17616 || (BASELINK_P (op)
17617 && BASELINK_QUALIFIED_P (op)));
17619 else
17620 qualified_p = false;
17622 if (TREE_CODE (function) == ADDR_EXPR
17623 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17624 /* Avoid error about taking the address of a constructor. */
17625 function = TREE_OPERAND (function, 0);
17627 function = tsubst_copy_and_build (function, args, complain,
17628 in_decl,
17629 !qualified_p,
17630 integral_constant_expression_p);
17632 if (BASELINK_P (function))
17633 qualified_p = true;
17636 nargs = call_expr_nargs (t);
17637 call_args = make_tree_vector ();
17638 for (i = 0; i < nargs; ++i)
17640 tree arg = CALL_EXPR_ARG (t, i);
17642 if (!PACK_EXPANSION_P (arg))
17643 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17644 else
17646 /* Expand the pack expansion and push each entry onto
17647 CALL_ARGS. */
17648 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17649 if (TREE_CODE (arg) == TREE_VEC)
17651 unsigned int len, j;
17653 len = TREE_VEC_LENGTH (arg);
17654 for (j = 0; j < len; ++j)
17656 tree value = TREE_VEC_ELT (arg, j);
17657 if (value != NULL_TREE)
17658 value = convert_from_reference (value);
17659 vec_safe_push (call_args, value);
17662 else
17664 /* A partial substitution. Add one entry. */
17665 vec_safe_push (call_args, arg);
17670 /* We do not perform argument-dependent lookup if normal
17671 lookup finds a non-function, in accordance with the
17672 expected resolution of DR 218. */
17673 if (koenig_p
17674 && ((is_overloaded_fn (function)
17675 /* If lookup found a member function, the Koenig lookup is
17676 not appropriate, even if an unqualified-name was used
17677 to denote the function. */
17678 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17679 || identifier_p (function))
17680 /* Only do this when substitution turns a dependent call
17681 into a non-dependent call. */
17682 && type_dependent_expression_p_push (t)
17683 && !any_type_dependent_arguments_p (call_args))
17684 function = perform_koenig_lookup (function, call_args, tf_none);
17686 if (function != NULL_TREE
17687 && identifier_p (function)
17688 && !any_type_dependent_arguments_p (call_args))
17690 if (koenig_p && (complain & tf_warning_or_error))
17692 /* For backwards compatibility and good diagnostics, try
17693 the unqualified lookup again if we aren't in SFINAE
17694 context. */
17695 tree unq = (tsubst_copy_and_build
17696 (function, args, complain, in_decl, true,
17697 integral_constant_expression_p));
17698 if (unq == error_mark_node)
17700 release_tree_vector (call_args);
17701 RETURN (error_mark_node);
17704 if (unq != function)
17706 /* In a lambda fn, we have to be careful to not
17707 introduce new this captures. Legacy code can't
17708 be using lambdas anyway, so it's ok to be
17709 stricter. */
17710 bool in_lambda = (current_class_type
17711 && LAMBDA_TYPE_P (current_class_type));
17712 char const *const msg
17713 = G_("%qD was not declared in this scope, "
17714 "and no declarations were found by "
17715 "argument-dependent lookup at the point "
17716 "of instantiation");
17718 bool diag = true;
17719 if (in_lambda)
17720 error_at (EXPR_LOC_OR_LOC (t, input_location),
17721 msg, function);
17722 else
17723 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17724 msg, function);
17725 if (diag)
17727 tree fn = unq;
17729 if (INDIRECT_REF_P (fn))
17730 fn = TREE_OPERAND (fn, 0);
17731 if (is_overloaded_fn (fn))
17732 fn = get_first_fn (fn);
17734 if (!DECL_P (fn))
17735 /* Can't say anything more. */;
17736 else if (DECL_CLASS_SCOPE_P (fn))
17738 location_t loc = EXPR_LOC_OR_LOC (t,
17739 input_location);
17740 inform (loc,
17741 "declarations in dependent base %qT are "
17742 "not found by unqualified lookup",
17743 DECL_CLASS_CONTEXT (fn));
17744 if (current_class_ptr)
17745 inform (loc,
17746 "use %<this->%D%> instead", function);
17747 else
17748 inform (loc,
17749 "use %<%T::%D%> instead",
17750 current_class_name, function);
17752 else
17753 inform (DECL_SOURCE_LOCATION (fn),
17754 "%qD declared here, later in the "
17755 "translation unit", fn);
17756 if (in_lambda)
17758 release_tree_vector (call_args);
17759 RETURN (error_mark_node);
17763 function = unq;
17766 if (identifier_p (function))
17768 if (complain & tf_error)
17769 unqualified_name_lookup_error (function);
17770 release_tree_vector (call_args);
17771 RETURN (error_mark_node);
17775 /* Remember that there was a reference to this entity. */
17776 if (function != NULL_TREE
17777 && DECL_P (function)
17778 && !mark_used (function, complain) && !(complain & tf_error))
17780 release_tree_vector (call_args);
17781 RETURN (error_mark_node);
17784 /* Put back tf_decltype for the actual call. */
17785 complain |= decltype_flag;
17787 if (function == NULL_TREE)
17788 switch (CALL_EXPR_IFN (t))
17790 case IFN_LAUNDER:
17791 gcc_assert (nargs == 1);
17792 if (vec_safe_length (call_args) != 1)
17794 error_at (EXPR_LOC_OR_LOC (t, input_location),
17795 "wrong number of arguments to "
17796 "%<__builtin_launder%>");
17797 ret = error_mark_node;
17799 else
17800 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17801 input_location),
17802 (*call_args)[0], complain);
17803 break;
17805 default:
17806 /* Unsupported internal function with arguments. */
17807 gcc_unreachable ();
17809 else if (TREE_CODE (function) == OFFSET_REF)
17810 ret = build_offset_ref_call_from_tree (function, &call_args,
17811 complain);
17812 else if (TREE_CODE (function) == COMPONENT_REF)
17814 tree instance = TREE_OPERAND (function, 0);
17815 tree fn = TREE_OPERAND (function, 1);
17817 if (processing_template_decl
17818 && (type_dependent_expression_p (instance)
17819 || (!BASELINK_P (fn)
17820 && TREE_CODE (fn) != FIELD_DECL)
17821 || type_dependent_expression_p (fn)
17822 || any_type_dependent_arguments_p (call_args)))
17823 ret = build_min_nt_call_vec (function, call_args);
17824 else if (!BASELINK_P (fn))
17825 ret = finish_call_expr (function, &call_args,
17826 /*disallow_virtual=*/false,
17827 /*koenig_p=*/false,
17828 complain);
17829 else
17830 ret = (build_new_method_call
17831 (instance, fn,
17832 &call_args, NULL_TREE,
17833 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17834 /*fn_p=*/NULL,
17835 complain));
17837 else
17838 ret = finish_call_expr (function, &call_args,
17839 /*disallow_virtual=*/qualified_p,
17840 koenig_p,
17841 complain);
17843 release_tree_vector (call_args);
17845 if (ret != error_mark_node)
17847 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17848 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17849 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17850 bool thk = CALL_FROM_THUNK_P (t);
17851 if (op || ord || rev || thk)
17853 function = extract_call_expr (ret);
17854 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17855 CALL_EXPR_ORDERED_ARGS (function) = ord;
17856 CALL_EXPR_REVERSE_ARGS (function) = rev;
17857 if (thk)
17859 if (TREE_CODE (function) == CALL_EXPR)
17860 CALL_FROM_THUNK_P (function) = true;
17861 else
17862 AGGR_INIT_FROM_THUNK_P (function) = true;
17863 /* The thunk location is not interesting. */
17864 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17869 RETURN (ret);
17872 case COND_EXPR:
17874 tree cond = RECUR (TREE_OPERAND (t, 0));
17875 tree folded_cond = fold_non_dependent_expr (cond);
17876 tree exp1, exp2;
17878 if (TREE_CODE (folded_cond) == INTEGER_CST)
17880 if (integer_zerop (folded_cond))
17882 ++c_inhibit_evaluation_warnings;
17883 exp1 = RECUR (TREE_OPERAND (t, 1));
17884 --c_inhibit_evaluation_warnings;
17885 exp2 = RECUR (TREE_OPERAND (t, 2));
17887 else
17889 exp1 = RECUR (TREE_OPERAND (t, 1));
17890 ++c_inhibit_evaluation_warnings;
17891 exp2 = RECUR (TREE_OPERAND (t, 2));
17892 --c_inhibit_evaluation_warnings;
17894 cond = folded_cond;
17896 else
17898 exp1 = RECUR (TREE_OPERAND (t, 1));
17899 exp2 = RECUR (TREE_OPERAND (t, 2));
17902 warning_sentinel s(warn_duplicated_branches);
17903 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17904 cond, exp1, exp2, complain));
17907 case PSEUDO_DTOR_EXPR:
17909 tree op0 = RECUR (TREE_OPERAND (t, 0));
17910 tree op1 = RECUR (TREE_OPERAND (t, 1));
17911 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17912 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17913 input_location));
17916 case TREE_LIST:
17918 tree purpose, value, chain;
17920 if (t == void_list_node)
17921 RETURN (t);
17923 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17924 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17926 /* We have pack expansions, so expand those and
17927 create a new list out of it. */
17928 tree purposevec = NULL_TREE;
17929 tree valuevec = NULL_TREE;
17930 tree chain;
17931 int i, len = -1;
17933 /* Expand the argument expressions. */
17934 if (TREE_PURPOSE (t))
17935 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17936 complain, in_decl);
17937 if (TREE_VALUE (t))
17938 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17939 complain, in_decl);
17941 /* Build the rest of the list. */
17942 chain = TREE_CHAIN (t);
17943 if (chain && chain != void_type_node)
17944 chain = RECUR (chain);
17946 /* Determine the number of arguments. */
17947 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17949 len = TREE_VEC_LENGTH (purposevec);
17950 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17952 else if (TREE_CODE (valuevec) == TREE_VEC)
17953 len = TREE_VEC_LENGTH (valuevec);
17954 else
17956 /* Since we only performed a partial substitution into
17957 the argument pack, we only RETURN (a single list
17958 node. */
17959 if (purposevec == TREE_PURPOSE (t)
17960 && valuevec == TREE_VALUE (t)
17961 && chain == TREE_CHAIN (t))
17962 RETURN (t);
17964 RETURN (tree_cons (purposevec, valuevec, chain));
17967 /* Convert the argument vectors into a TREE_LIST */
17968 i = len;
17969 while (i > 0)
17971 /* Grab the Ith values. */
17972 i--;
17973 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17974 : NULL_TREE;
17975 value
17976 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17977 : NULL_TREE;
17979 /* Build the list (backwards). */
17980 chain = tree_cons (purpose, value, chain);
17983 RETURN (chain);
17986 purpose = TREE_PURPOSE (t);
17987 if (purpose)
17988 purpose = RECUR (purpose);
17989 value = TREE_VALUE (t);
17990 if (value)
17991 value = RECUR (value);
17992 chain = TREE_CHAIN (t);
17993 if (chain && chain != void_type_node)
17994 chain = RECUR (chain);
17995 if (purpose == TREE_PURPOSE (t)
17996 && value == TREE_VALUE (t)
17997 && chain == TREE_CHAIN (t))
17998 RETURN (t);
17999 RETURN (tree_cons (purpose, value, chain));
18002 case COMPONENT_REF:
18004 tree object;
18005 tree object_type;
18006 tree member;
18007 tree r;
18009 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18010 args, complain, in_decl);
18011 /* Remember that there was a reference to this entity. */
18012 if (DECL_P (object)
18013 && !mark_used (object, complain) && !(complain & tf_error))
18014 RETURN (error_mark_node);
18015 object_type = TREE_TYPE (object);
18017 member = TREE_OPERAND (t, 1);
18018 if (BASELINK_P (member))
18019 member = tsubst_baselink (member,
18020 non_reference (TREE_TYPE (object)),
18021 args, complain, in_decl);
18022 else
18023 member = tsubst_copy (member, args, complain, in_decl);
18024 if (member == error_mark_node)
18025 RETURN (error_mark_node);
18027 if (TREE_CODE (member) == FIELD_DECL)
18029 r = finish_non_static_data_member (member, object, NULL_TREE);
18030 if (TREE_CODE (r) == COMPONENT_REF)
18031 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18032 RETURN (r);
18034 else if (type_dependent_expression_p (object))
18035 /* We can't do much here. */;
18036 else if (!CLASS_TYPE_P (object_type))
18038 if (scalarish_type_p (object_type))
18040 tree s = NULL_TREE;
18041 tree dtor = member;
18043 if (TREE_CODE (dtor) == SCOPE_REF)
18045 s = TREE_OPERAND (dtor, 0);
18046 dtor = TREE_OPERAND (dtor, 1);
18048 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18050 dtor = TREE_OPERAND (dtor, 0);
18051 if (TYPE_P (dtor))
18052 RETURN (finish_pseudo_destructor_expr
18053 (object, s, dtor, input_location));
18057 else if (TREE_CODE (member) == SCOPE_REF
18058 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18060 /* Lookup the template functions now that we know what the
18061 scope is. */
18062 tree scope = TREE_OPERAND (member, 0);
18063 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18064 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18065 member = lookup_qualified_name (scope, tmpl,
18066 /*is_type_p=*/false,
18067 /*complain=*/false);
18068 if (BASELINK_P (member))
18070 BASELINK_FUNCTIONS (member)
18071 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18072 args);
18073 member = (adjust_result_of_qualified_name_lookup
18074 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18075 object_type));
18077 else
18079 qualified_name_lookup_error (scope, tmpl, member,
18080 input_location);
18081 RETURN (error_mark_node);
18084 else if (TREE_CODE (member) == SCOPE_REF
18085 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18086 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18088 if (complain & tf_error)
18090 if (TYPE_P (TREE_OPERAND (member, 0)))
18091 error ("%qT is not a class or namespace",
18092 TREE_OPERAND (member, 0));
18093 else
18094 error ("%qD is not a class or namespace",
18095 TREE_OPERAND (member, 0));
18097 RETURN (error_mark_node);
18100 r = finish_class_member_access_expr (object, member,
18101 /*template_p=*/false,
18102 complain);
18103 if (TREE_CODE (r) == COMPONENT_REF)
18104 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18105 RETURN (r);
18108 case THROW_EXPR:
18109 RETURN (build_throw
18110 (RECUR (TREE_OPERAND (t, 0))));
18112 case CONSTRUCTOR:
18114 vec<constructor_elt, va_gc> *n;
18115 constructor_elt *ce;
18116 unsigned HOST_WIDE_INT idx;
18117 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18118 bool process_index_p;
18119 int newlen;
18120 bool need_copy_p = false;
18121 tree r;
18123 if (type == error_mark_node)
18124 RETURN (error_mark_node);
18126 /* We do not want to process the index of aggregate
18127 initializers as they are identifier nodes which will be
18128 looked up by digest_init. */
18129 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18131 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18132 newlen = vec_safe_length (n);
18133 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18135 if (ce->index && process_index_p
18136 /* An identifier index is looked up in the type
18137 being initialized, not the current scope. */
18138 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18139 ce->index = RECUR (ce->index);
18141 if (PACK_EXPANSION_P (ce->value))
18143 /* Substitute into the pack expansion. */
18144 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18145 in_decl);
18147 if (ce->value == error_mark_node
18148 || PACK_EXPANSION_P (ce->value))
18150 else if (TREE_VEC_LENGTH (ce->value) == 1)
18151 /* Just move the argument into place. */
18152 ce->value = TREE_VEC_ELT (ce->value, 0);
18153 else
18155 /* Update the length of the final CONSTRUCTOR
18156 arguments vector, and note that we will need to
18157 copy.*/
18158 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18159 need_copy_p = true;
18162 else
18163 ce->value = RECUR (ce->value);
18166 if (need_copy_p)
18168 vec<constructor_elt, va_gc> *old_n = n;
18170 vec_alloc (n, newlen);
18171 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18173 if (TREE_CODE (ce->value) == TREE_VEC)
18175 int i, len = TREE_VEC_LENGTH (ce->value);
18176 for (i = 0; i < len; ++i)
18177 CONSTRUCTOR_APPEND_ELT (n, 0,
18178 TREE_VEC_ELT (ce->value, i));
18180 else
18181 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18185 r = build_constructor (init_list_type_node, n);
18186 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18188 if (TREE_HAS_CONSTRUCTOR (t))
18190 fcl_t cl = fcl_functional;
18191 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18192 cl = fcl_c99;
18193 RETURN (finish_compound_literal (type, r, complain, cl));
18196 TREE_TYPE (r) = type;
18197 RETURN (r);
18200 case TYPEID_EXPR:
18202 tree operand_0 = TREE_OPERAND (t, 0);
18203 if (TYPE_P (operand_0))
18205 operand_0 = tsubst (operand_0, args, complain, in_decl);
18206 RETURN (get_typeid (operand_0, complain));
18208 else
18210 operand_0 = RECUR (operand_0);
18211 RETURN (build_typeid (operand_0, complain));
18215 case VAR_DECL:
18216 if (!args)
18217 RETURN (t);
18218 /* Fall through */
18220 case PARM_DECL:
18222 tree r = tsubst_copy (t, args, complain, in_decl);
18223 /* ??? We're doing a subset of finish_id_expression here. */
18224 if (VAR_P (r)
18225 && !processing_template_decl
18226 && !cp_unevaluated_operand
18227 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18228 && CP_DECL_THREAD_LOCAL_P (r))
18230 if (tree wrap = get_tls_wrapper_fn (r))
18231 /* Replace an evaluated use of the thread_local variable with
18232 a call to its wrapper. */
18233 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18235 else if (outer_automatic_var_p (r))
18236 r = process_outer_var_ref (r, complain);
18238 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18239 /* If the original type was a reference, we'll be wrapped in
18240 the appropriate INDIRECT_REF. */
18241 r = convert_from_reference (r);
18242 RETURN (r);
18245 case VA_ARG_EXPR:
18247 tree op0 = RECUR (TREE_OPERAND (t, 0));
18248 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18249 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18252 case OFFSETOF_EXPR:
18254 tree object_ptr
18255 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18256 in_decl, /*function_p=*/false,
18257 /*integral_constant_expression_p=*/false);
18258 RETURN (finish_offsetof (object_ptr,
18259 RECUR (TREE_OPERAND (t, 0)),
18260 EXPR_LOCATION (t)));
18263 case ADDRESSOF_EXPR:
18264 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18265 RECUR (TREE_OPERAND (t, 0)), complain));
18267 case TRAIT_EXPR:
18269 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18270 complain, in_decl);
18272 tree type2 = TRAIT_EXPR_TYPE2 (t);
18273 if (type2 && TREE_CODE (type2) == TREE_LIST)
18274 type2 = RECUR (type2);
18275 else if (type2)
18276 type2 = tsubst (type2, args, complain, in_decl);
18278 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18281 case STMT_EXPR:
18283 tree old_stmt_expr = cur_stmt_expr;
18284 tree stmt_expr = begin_stmt_expr ();
18286 cur_stmt_expr = stmt_expr;
18287 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18288 integral_constant_expression_p);
18289 stmt_expr = finish_stmt_expr (stmt_expr, false);
18290 cur_stmt_expr = old_stmt_expr;
18292 /* If the resulting list of expression statement is empty,
18293 fold it further into void_node. */
18294 if (empty_expr_stmt_p (stmt_expr))
18295 stmt_expr = void_node;
18297 RETURN (stmt_expr);
18300 case LAMBDA_EXPR:
18302 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18304 RETURN (build_lambda_object (r));
18307 case TARGET_EXPR:
18308 /* We can get here for a constant initializer of non-dependent type.
18309 FIXME stop folding in cp_parser_initializer_clause. */
18311 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18312 complain);
18313 RETURN (r);
18316 case TRANSACTION_EXPR:
18317 RETURN (tsubst_expr(t, args, complain, in_decl,
18318 integral_constant_expression_p));
18320 case PAREN_EXPR:
18321 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18323 case VEC_PERM_EXPR:
18325 tree op0 = RECUR (TREE_OPERAND (t, 0));
18326 tree op1 = RECUR (TREE_OPERAND (t, 1));
18327 tree op2 = RECUR (TREE_OPERAND (t, 2));
18328 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18329 complain));
18332 case REQUIRES_EXPR:
18333 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18335 case NON_LVALUE_EXPR:
18336 case VIEW_CONVERT_EXPR:
18337 /* We should only see these for location wrapper nodes, or within
18338 instantiate_non_dependent_expr (when args is NULL_TREE). */
18339 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18340 if (location_wrapper_p (t))
18341 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18342 EXPR_LOCATION (t)));
18343 /* fallthrough. */
18345 default:
18346 /* Handle Objective-C++ constructs, if appropriate. */
18348 tree subst
18349 = objcp_tsubst_copy_and_build (t, args, complain,
18350 in_decl, /*function_p=*/false);
18351 if (subst)
18352 RETURN (subst);
18354 RETURN (tsubst_copy (t, args, complain, in_decl));
18357 #undef RECUR
18358 #undef RETURN
18359 out:
18360 input_location = loc;
18361 return retval;
18364 /* Verify that the instantiated ARGS are valid. For type arguments,
18365 make sure that the type's linkage is ok. For non-type arguments,
18366 make sure they are constants if they are integral or enumerations.
18367 Emit an error under control of COMPLAIN, and return TRUE on error. */
18369 static bool
18370 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18372 if (dependent_template_arg_p (t))
18373 return false;
18374 if (ARGUMENT_PACK_P (t))
18376 tree vec = ARGUMENT_PACK_ARGS (t);
18377 int len = TREE_VEC_LENGTH (vec);
18378 bool result = false;
18379 int i;
18381 for (i = 0; i < len; ++i)
18382 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18383 result = true;
18384 return result;
18386 else if (TYPE_P (t))
18388 /* [basic.link]: A name with no linkage (notably, the name
18389 of a class or enumeration declared in a local scope)
18390 shall not be used to declare an entity with linkage.
18391 This implies that names with no linkage cannot be used as
18392 template arguments
18394 DR 757 relaxes this restriction for C++0x. */
18395 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18396 : no_linkage_check (t, /*relaxed_p=*/false));
18398 if (nt)
18400 /* DR 488 makes use of a type with no linkage cause
18401 type deduction to fail. */
18402 if (complain & tf_error)
18404 if (TYPE_UNNAMED_P (nt))
18405 error ("%qT is/uses unnamed type", t);
18406 else
18407 error ("template argument for %qD uses local type %qT",
18408 tmpl, t);
18410 return true;
18412 /* In order to avoid all sorts of complications, we do not
18413 allow variably-modified types as template arguments. */
18414 else if (variably_modified_type_p (t, NULL_TREE))
18416 if (complain & tf_error)
18417 error ("%qT is a variably modified type", t);
18418 return true;
18421 /* Class template and alias template arguments should be OK. */
18422 else if (DECL_TYPE_TEMPLATE_P (t))
18424 /* A non-type argument of integral or enumerated type must be a
18425 constant. */
18426 else if (TREE_TYPE (t)
18427 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18428 && !REFERENCE_REF_P (t)
18429 && !TREE_CONSTANT (t))
18431 if (complain & tf_error)
18432 error ("integral expression %qE is not constant", t);
18433 return true;
18435 return false;
18438 static bool
18439 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18441 int ix, len = DECL_NTPARMS (tmpl);
18442 bool result = false;
18444 for (ix = 0; ix != len; ix++)
18446 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18447 result = true;
18449 if (result && (complain & tf_error))
18450 error (" trying to instantiate %qD", tmpl);
18451 return result;
18454 /* We're out of SFINAE context now, so generate diagnostics for the access
18455 errors we saw earlier when instantiating D from TMPL and ARGS. */
18457 static void
18458 recheck_decl_substitution (tree d, tree tmpl, tree args)
18460 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18461 tree type = TREE_TYPE (pattern);
18462 location_t loc = input_location;
18464 push_access_scope (d);
18465 push_deferring_access_checks (dk_no_deferred);
18466 input_location = DECL_SOURCE_LOCATION (pattern);
18467 tsubst (type, args, tf_warning_or_error, d);
18468 input_location = loc;
18469 pop_deferring_access_checks ();
18470 pop_access_scope (d);
18473 /* Instantiate the indicated variable, function, or alias template TMPL with
18474 the template arguments in TARG_PTR. */
18476 static tree
18477 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18479 tree targ_ptr = orig_args;
18480 tree fndecl;
18481 tree gen_tmpl;
18482 tree spec;
18483 bool access_ok = true;
18485 if (tmpl == error_mark_node)
18486 return error_mark_node;
18488 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18490 /* If this function is a clone, handle it specially. */
18491 if (DECL_CLONED_FUNCTION_P (tmpl))
18493 tree spec;
18494 tree clone;
18496 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18497 DECL_CLONED_FUNCTION. */
18498 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18499 targ_ptr, complain);
18500 if (spec == error_mark_node)
18501 return error_mark_node;
18503 /* Look for the clone. */
18504 FOR_EACH_CLONE (clone, spec)
18505 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18506 return clone;
18507 /* We should always have found the clone by now. */
18508 gcc_unreachable ();
18509 return NULL_TREE;
18512 if (targ_ptr == error_mark_node)
18513 return error_mark_node;
18515 /* Check to see if we already have this specialization. */
18516 gen_tmpl = most_general_template (tmpl);
18517 if (TMPL_ARGS_DEPTH (targ_ptr)
18518 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18519 /* targ_ptr only has the innermost template args, so add the outer ones
18520 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18521 the case of a non-dependent call within a template definition). */
18522 targ_ptr = (add_outermost_template_args
18523 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18524 targ_ptr));
18526 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18527 but it doesn't seem to be on the hot path. */
18528 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18530 gcc_assert (tmpl == gen_tmpl
18531 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18532 == spec)
18533 || fndecl == NULL_TREE);
18535 if (spec != NULL_TREE)
18537 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18539 if (complain & tf_error)
18540 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18541 return error_mark_node;
18543 return spec;
18546 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18547 complain))
18548 return error_mark_node;
18550 /* We are building a FUNCTION_DECL, during which the access of its
18551 parameters and return types have to be checked. However this
18552 FUNCTION_DECL which is the desired context for access checking
18553 is not built yet. We solve this chicken-and-egg problem by
18554 deferring all checks until we have the FUNCTION_DECL. */
18555 push_deferring_access_checks (dk_deferred);
18557 /* Instantiation of the function happens in the context of the function
18558 template, not the context of the overload resolution we're doing. */
18559 push_to_top_level ();
18560 /* If there are dependent arguments, e.g. because we're doing partial
18561 ordering, make sure processing_template_decl stays set. */
18562 if (uses_template_parms (targ_ptr))
18563 ++processing_template_decl;
18564 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18566 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18567 complain, gen_tmpl, true);
18568 push_nested_class (ctx);
18571 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18573 fndecl = NULL_TREE;
18574 if (VAR_P (pattern))
18576 /* We need to determine if we're using a partial or explicit
18577 specialization now, because the type of the variable could be
18578 different. */
18579 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18580 tree elt = most_specialized_partial_spec (tid, complain);
18581 if (elt == error_mark_node)
18582 pattern = error_mark_node;
18583 else if (elt)
18585 tree partial_tmpl = TREE_VALUE (elt);
18586 tree partial_args = TREE_PURPOSE (elt);
18587 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18588 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18592 /* Substitute template parameters to obtain the specialization. */
18593 if (fndecl == NULL_TREE)
18594 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18595 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18596 pop_nested_class ();
18597 pop_from_top_level ();
18599 if (fndecl == error_mark_node)
18601 pop_deferring_access_checks ();
18602 return error_mark_node;
18605 /* The DECL_TI_TEMPLATE should always be the immediate parent
18606 template, not the most general template. */
18607 DECL_TI_TEMPLATE (fndecl) = tmpl;
18608 DECL_TI_ARGS (fndecl) = targ_ptr;
18610 /* Now we know the specialization, compute access previously
18611 deferred. Do no access control for inheriting constructors,
18612 as we already checked access for the inherited constructor. */
18613 if (!(flag_new_inheriting_ctors
18614 && DECL_INHERITED_CTOR (fndecl)))
18616 push_access_scope (fndecl);
18617 if (!perform_deferred_access_checks (complain))
18618 access_ok = false;
18619 pop_access_scope (fndecl);
18621 pop_deferring_access_checks ();
18623 /* If we've just instantiated the main entry point for a function,
18624 instantiate all the alternate entry points as well. We do this
18625 by cloning the instantiation of the main entry point, not by
18626 instantiating the template clones. */
18627 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18628 clone_function_decl (fndecl, /*update_methods=*/false);
18630 if (!access_ok)
18632 if (!(complain & tf_error))
18634 /* Remember to reinstantiate when we're out of SFINAE so the user
18635 can see the errors. */
18636 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18638 return error_mark_node;
18640 return fndecl;
18643 /* Wrapper for instantiate_template_1. */
18645 tree
18646 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18648 tree ret;
18649 timevar_push (TV_TEMPLATE_INST);
18650 ret = instantiate_template_1 (tmpl, orig_args, complain);
18651 timevar_pop (TV_TEMPLATE_INST);
18652 return ret;
18655 /* Instantiate the alias template TMPL with ARGS. Also push a template
18656 instantiation level, which instantiate_template doesn't do because
18657 functions and variables have sufficient context established by the
18658 callers. */
18660 static tree
18661 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18663 struct pending_template *old_last_pend = last_pending_template;
18664 struct tinst_level *old_error_tinst = last_error_tinst_level;
18665 if (tmpl == error_mark_node || args == error_mark_node)
18666 return error_mark_node;
18667 tree tinst = build_tree_list (tmpl, args);
18668 if (!push_tinst_level (tinst))
18670 ggc_free (tinst);
18671 return error_mark_node;
18674 args =
18675 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18676 args, tmpl, complain,
18677 /*require_all_args=*/true,
18678 /*use_default_args=*/true);
18680 tree r = instantiate_template (tmpl, args, complain);
18681 pop_tinst_level ();
18682 /* We can't free this if a pending_template entry or last_error_tinst_level
18683 is pointing at it. */
18684 if (last_pending_template == old_last_pend
18685 && last_error_tinst_level == old_error_tinst)
18686 ggc_free (tinst);
18688 return r;
18691 /* PARM is a template parameter pack for FN. Returns true iff
18692 PARM is used in a deducible way in the argument list of FN. */
18694 static bool
18695 pack_deducible_p (tree parm, tree fn)
18697 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18698 for (; t; t = TREE_CHAIN (t))
18700 tree type = TREE_VALUE (t);
18701 tree packs;
18702 if (!PACK_EXPANSION_P (type))
18703 continue;
18704 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18705 packs; packs = TREE_CHAIN (packs))
18706 if (template_args_equal (TREE_VALUE (packs), parm))
18708 /* The template parameter pack is used in a function parameter
18709 pack. If this is the end of the parameter list, the
18710 template parameter pack is deducible. */
18711 if (TREE_CHAIN (t) == void_list_node)
18712 return true;
18713 else
18714 /* Otherwise, not. Well, it could be deduced from
18715 a non-pack parameter, but doing so would end up with
18716 a deduction mismatch, so don't bother. */
18717 return false;
18720 /* The template parameter pack isn't used in any function parameter
18721 packs, but it might be used deeper, e.g. tuple<Args...>. */
18722 return true;
18725 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18726 NARGS elements of the arguments that are being used when calling
18727 it. TARGS is a vector into which the deduced template arguments
18728 are placed.
18730 Returns either a FUNCTION_DECL for the matching specialization of FN or
18731 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18732 true, diagnostics will be printed to explain why it failed.
18734 If FN is a conversion operator, or we are trying to produce a specific
18735 specialization, RETURN_TYPE is the return type desired.
18737 The EXPLICIT_TARGS are explicit template arguments provided via a
18738 template-id.
18740 The parameter STRICT is one of:
18742 DEDUCE_CALL:
18743 We are deducing arguments for a function call, as in
18744 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18745 deducing arguments for a call to the result of a conversion
18746 function template, as in [over.call.object].
18748 DEDUCE_CONV:
18749 We are deducing arguments for a conversion function, as in
18750 [temp.deduct.conv].
18752 DEDUCE_EXACT:
18753 We are deducing arguments when doing an explicit instantiation
18754 as in [temp.explicit], when determining an explicit specialization
18755 as in [temp.expl.spec], or when taking the address of a function
18756 template, as in [temp.deduct.funcaddr]. */
18758 tree
18759 fn_type_unification (tree fn,
18760 tree explicit_targs,
18761 tree targs,
18762 const tree *args,
18763 unsigned int nargs,
18764 tree return_type,
18765 unification_kind_t strict,
18766 int flags,
18767 bool explain_p,
18768 bool decltype_p)
18770 tree parms;
18771 tree fntype;
18772 tree decl = NULL_TREE;
18773 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18774 bool ok;
18775 static int deduction_depth;
18776 struct pending_template *old_last_pend = last_pending_template;
18777 struct tinst_level *old_error_tinst = last_error_tinst_level;
18779 tree orig_fn = fn;
18780 if (flag_new_inheriting_ctors)
18781 fn = strip_inheriting_ctors (fn);
18783 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18784 tree tinst;
18785 tree r = error_mark_node;
18787 tree full_targs = targs;
18788 if (TMPL_ARGS_DEPTH (targs)
18789 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18790 full_targs = (add_outermost_template_args
18791 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18792 targs));
18794 if (decltype_p)
18795 complain |= tf_decltype;
18797 /* In C++0x, it's possible to have a function template whose type depends
18798 on itself recursively. This is most obvious with decltype, but can also
18799 occur with enumeration scope (c++/48969). So we need to catch infinite
18800 recursion and reject the substitution at deduction time; this function
18801 will return error_mark_node for any repeated substitution.
18803 This also catches excessive recursion such as when f<N> depends on
18804 f<N-1> across all integers, and returns error_mark_node for all the
18805 substitutions back up to the initial one.
18807 This is, of course, not reentrant. */
18808 if (excessive_deduction_depth)
18809 return error_mark_node;
18810 tinst = build_tree_list (fn, NULL_TREE);
18811 ++deduction_depth;
18813 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18815 fntype = TREE_TYPE (fn);
18816 if (explicit_targs)
18818 /* [temp.deduct]
18820 The specified template arguments must match the template
18821 parameters in kind (i.e., type, nontype, template), and there
18822 must not be more arguments than there are parameters;
18823 otherwise type deduction fails.
18825 Nontype arguments must match the types of the corresponding
18826 nontype template parameters, or must be convertible to the
18827 types of the corresponding nontype parameters as specified in
18828 _temp.arg.nontype_, otherwise type deduction fails.
18830 All references in the function type of the function template
18831 to the corresponding template parameters are replaced by the
18832 specified template argument values. If a substitution in a
18833 template parameter or in the function type of the function
18834 template results in an invalid type, type deduction fails. */
18835 int i, len = TREE_VEC_LENGTH (tparms);
18836 location_t loc = input_location;
18837 bool incomplete = false;
18839 if (explicit_targs == error_mark_node)
18840 goto fail;
18842 if (TMPL_ARGS_DEPTH (explicit_targs)
18843 < TMPL_ARGS_DEPTH (full_targs))
18844 explicit_targs = add_outermost_template_args (full_targs,
18845 explicit_targs);
18847 /* Adjust any explicit template arguments before entering the
18848 substitution context. */
18849 explicit_targs
18850 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18851 complain,
18852 /*require_all_args=*/false,
18853 /*use_default_args=*/false));
18854 if (explicit_targs == error_mark_node)
18855 goto fail;
18857 /* Substitute the explicit args into the function type. This is
18858 necessary so that, for instance, explicitly declared function
18859 arguments can match null pointed constants. If we were given
18860 an incomplete set of explicit args, we must not do semantic
18861 processing during substitution as we could create partial
18862 instantiations. */
18863 for (i = 0; i < len; i++)
18865 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18866 bool parameter_pack = false;
18867 tree targ = TREE_VEC_ELT (explicit_targs, i);
18869 /* Dig out the actual parm. */
18870 if (TREE_CODE (parm) == TYPE_DECL
18871 || TREE_CODE (parm) == TEMPLATE_DECL)
18873 parm = TREE_TYPE (parm);
18874 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18876 else if (TREE_CODE (parm) == PARM_DECL)
18878 parm = DECL_INITIAL (parm);
18879 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18882 if (!parameter_pack && targ == NULL_TREE)
18883 /* No explicit argument for this template parameter. */
18884 incomplete = true;
18886 if (parameter_pack && pack_deducible_p (parm, fn))
18888 /* Mark the argument pack as "incomplete". We could
18889 still deduce more arguments during unification.
18890 We remove this mark in type_unification_real. */
18891 if (targ)
18893 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18894 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18895 = ARGUMENT_PACK_ARGS (targ);
18898 /* We have some incomplete argument packs. */
18899 incomplete = true;
18903 TREE_VALUE (tinst) = explicit_targs;
18904 if (!push_tinst_level (tinst))
18906 excessive_deduction_depth = true;
18907 goto fail;
18909 processing_template_decl += incomplete;
18910 input_location = DECL_SOURCE_LOCATION (fn);
18911 /* Ignore any access checks; we'll see them again in
18912 instantiate_template and they might have the wrong
18913 access path at this point. */
18914 push_deferring_access_checks (dk_deferred);
18915 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18916 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18917 pop_deferring_access_checks ();
18918 input_location = loc;
18919 processing_template_decl -= incomplete;
18920 pop_tinst_level ();
18922 if (fntype == error_mark_node)
18923 goto fail;
18925 /* Place the explicitly specified arguments in TARGS. */
18926 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18927 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18928 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18931 /* Never do unification on the 'this' parameter. */
18932 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18934 if (return_type && strict == DEDUCE_CALL)
18936 /* We're deducing for a call to the result of a template conversion
18937 function. The parms we really want are in return_type. */
18938 if (POINTER_TYPE_P (return_type))
18939 return_type = TREE_TYPE (return_type);
18940 parms = TYPE_ARG_TYPES (return_type);
18942 else if (return_type)
18944 tree *new_args;
18946 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18947 new_args = XALLOCAVEC (tree, nargs + 1);
18948 new_args[0] = return_type;
18949 memcpy (new_args + 1, args, nargs * sizeof (tree));
18950 args = new_args;
18951 ++nargs;
18954 /* We allow incomplete unification without an error message here
18955 because the standard doesn't seem to explicitly prohibit it. Our
18956 callers must be ready to deal with unification failures in any
18957 event. */
18959 TREE_VALUE (tinst) = targs;
18960 /* If we aren't explaining yet, push tinst context so we can see where
18961 any errors (e.g. from class instantiations triggered by instantiation
18962 of default template arguments) come from. If we are explaining, this
18963 context is redundant. */
18964 if (!explain_p && !push_tinst_level (tinst))
18966 excessive_deduction_depth = true;
18967 goto fail;
18970 /* type_unification_real will pass back any access checks from default
18971 template argument substitution. */
18972 vec<deferred_access_check, va_gc> *checks;
18973 checks = NULL;
18975 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18976 full_targs, parms, args, nargs, /*subr=*/0,
18977 strict, flags, &checks, explain_p);
18978 if (!explain_p)
18979 pop_tinst_level ();
18980 if (!ok)
18981 goto fail;
18983 /* Now that we have bindings for all of the template arguments,
18984 ensure that the arguments deduced for the template template
18985 parameters have compatible template parameter lists. We cannot
18986 check this property before we have deduced all template
18987 arguments, because the template parameter types of a template
18988 template parameter might depend on prior template parameters
18989 deduced after the template template parameter. The following
18990 ill-formed example illustrates this issue:
18992 template<typename T, template<T> class C> void f(C<5>, T);
18994 template<int N> struct X {};
18996 void g() {
18997 f(X<5>(), 5l); // error: template argument deduction fails
19000 The template parameter list of 'C' depends on the template type
19001 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19002 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19003 time that we deduce 'C'. */
19004 if (!template_template_parm_bindings_ok_p
19005 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19007 unify_inconsistent_template_template_parameters (explain_p);
19008 goto fail;
19011 /* All is well so far. Now, check:
19013 [temp.deduct]
19015 When all template arguments have been deduced, all uses of
19016 template parameters in nondeduced contexts are replaced with
19017 the corresponding deduced argument values. If the
19018 substitution results in an invalid type, as described above,
19019 type deduction fails. */
19020 TREE_VALUE (tinst) = targs;
19021 if (!push_tinst_level (tinst))
19023 excessive_deduction_depth = true;
19024 goto fail;
19027 /* Also collect access checks from the instantiation. */
19028 reopen_deferring_access_checks (checks);
19030 decl = instantiate_template (fn, targs, complain);
19032 checks = get_deferred_access_checks ();
19033 pop_deferring_access_checks ();
19035 pop_tinst_level ();
19037 if (decl == error_mark_node)
19038 goto fail;
19040 /* Now perform any access checks encountered during substitution. */
19041 push_access_scope (decl);
19042 ok = perform_access_checks (checks, complain);
19043 pop_access_scope (decl);
19044 if (!ok)
19045 goto fail;
19047 /* If we're looking for an exact match, check that what we got
19048 is indeed an exact match. It might not be if some template
19049 parameters are used in non-deduced contexts. But don't check
19050 for an exact match if we have dependent template arguments;
19051 in that case we're doing partial ordering, and we already know
19052 that we have two candidates that will provide the actual type. */
19053 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19055 tree substed = TREE_TYPE (decl);
19056 unsigned int i;
19058 tree sarg
19059 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19060 if (return_type)
19061 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19062 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19063 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19065 unify_type_mismatch (explain_p, args[i],
19066 TREE_VALUE (sarg));
19067 goto fail;
19071 /* After doing deduction with the inherited constructor, actually return an
19072 instantiation of the inheriting constructor. */
19073 if (orig_fn != fn)
19074 decl = instantiate_template (orig_fn, targs, complain);
19076 r = decl;
19078 fail:
19079 --deduction_depth;
19080 if (excessive_deduction_depth)
19082 if (deduction_depth == 0)
19083 /* Reset once we're all the way out. */
19084 excessive_deduction_depth = false;
19087 /* We can't free this if a pending_template entry or last_error_tinst_level
19088 is pointing at it. */
19089 if (last_pending_template == old_last_pend
19090 && last_error_tinst_level == old_error_tinst)
19091 ggc_free (tinst);
19093 return r;
19096 /* Adjust types before performing type deduction, as described in
19097 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19098 sections are symmetric. PARM is the type of a function parameter
19099 or the return type of the conversion function. ARG is the type of
19100 the argument passed to the call, or the type of the value
19101 initialized with the result of the conversion function.
19102 ARG_EXPR is the original argument expression, which may be null. */
19104 static int
19105 maybe_adjust_types_for_deduction (unification_kind_t strict,
19106 tree* parm,
19107 tree* arg,
19108 tree arg_expr)
19110 int result = 0;
19112 switch (strict)
19114 case DEDUCE_CALL:
19115 break;
19117 case DEDUCE_CONV:
19118 /* Swap PARM and ARG throughout the remainder of this
19119 function; the handling is precisely symmetric since PARM
19120 will initialize ARG rather than vice versa. */
19121 std::swap (parm, arg);
19122 break;
19124 case DEDUCE_EXACT:
19125 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19126 too, but here handle it by stripping the reference from PARM
19127 rather than by adding it to ARG. */
19128 if (TREE_CODE (*parm) == REFERENCE_TYPE
19129 && TYPE_REF_IS_RVALUE (*parm)
19130 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19131 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19132 && TREE_CODE (*arg) == REFERENCE_TYPE
19133 && !TYPE_REF_IS_RVALUE (*arg))
19134 *parm = TREE_TYPE (*parm);
19135 /* Nothing else to do in this case. */
19136 return 0;
19138 default:
19139 gcc_unreachable ();
19142 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19144 /* [temp.deduct.call]
19146 If P is not a reference type:
19148 --If A is an array type, the pointer type produced by the
19149 array-to-pointer standard conversion (_conv.array_) is
19150 used in place of A for type deduction; otherwise,
19152 --If A is a function type, the pointer type produced by
19153 the function-to-pointer standard conversion
19154 (_conv.func_) is used in place of A for type deduction;
19155 otherwise,
19157 --If A is a cv-qualified type, the top level
19158 cv-qualifiers of A's type are ignored for type
19159 deduction. */
19160 if (TREE_CODE (*arg) == ARRAY_TYPE)
19161 *arg = build_pointer_type (TREE_TYPE (*arg));
19162 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19163 *arg = build_pointer_type (*arg);
19164 else
19165 *arg = TYPE_MAIN_VARIANT (*arg);
19168 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19169 reference to a cv-unqualified template parameter that does not represent a
19170 template parameter of a class template (during class template argument
19171 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19172 an lvalue, the type "lvalue reference to A" is used in place of A for type
19173 deduction. */
19174 if (TREE_CODE (*parm) == REFERENCE_TYPE
19175 && TYPE_REF_IS_RVALUE (*parm)
19176 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19177 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19178 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19179 && (arg_expr ? lvalue_p (arg_expr)
19180 /* try_one_overload doesn't provide an arg_expr, but
19181 functions are always lvalues. */
19182 : TREE_CODE (*arg) == FUNCTION_TYPE))
19183 *arg = build_reference_type (*arg);
19185 /* [temp.deduct.call]
19187 If P is a cv-qualified type, the top level cv-qualifiers
19188 of P's type are ignored for type deduction. If P is a
19189 reference type, the type referred to by P is used for
19190 type deduction. */
19191 *parm = TYPE_MAIN_VARIANT (*parm);
19192 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19194 *parm = TREE_TYPE (*parm);
19195 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19198 /* DR 322. For conversion deduction, remove a reference type on parm
19199 too (which has been swapped into ARG). */
19200 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19201 *arg = TREE_TYPE (*arg);
19203 return result;
19206 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19207 template which does contain any deducible template parameters; check if
19208 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19209 unify_one_argument. */
19211 static int
19212 check_non_deducible_conversion (tree parm, tree arg, int strict,
19213 int flags, bool explain_p)
19215 tree type;
19217 if (!TYPE_P (arg))
19218 type = TREE_TYPE (arg);
19219 else
19220 type = arg;
19222 if (same_type_p (parm, type))
19223 return unify_success (explain_p);
19225 if (strict == DEDUCE_CONV)
19227 if (can_convert_arg (type, parm, NULL_TREE, flags,
19228 explain_p ? tf_warning_or_error : tf_none))
19229 return unify_success (explain_p);
19231 else if (strict != DEDUCE_EXACT)
19233 if (can_convert_arg (parm, type,
19234 TYPE_P (arg) ? NULL_TREE : arg,
19235 flags, explain_p ? tf_warning_or_error : tf_none))
19236 return unify_success (explain_p);
19239 if (strict == DEDUCE_EXACT)
19240 return unify_type_mismatch (explain_p, parm, arg);
19241 else
19242 return unify_arg_conversion (explain_p, parm, type, arg);
19245 static bool uses_deducible_template_parms (tree type);
19247 /* Returns true iff the expression EXPR is one from which a template
19248 argument can be deduced. In other words, if it's an undecorated
19249 use of a template non-type parameter. */
19251 static bool
19252 deducible_expression (tree expr)
19254 /* Strip implicit conversions. */
19255 while (CONVERT_EXPR_P (expr))
19256 expr = TREE_OPERAND (expr, 0);
19257 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19260 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19261 deducible way; that is, if it has a max value of <PARM> - 1. */
19263 static bool
19264 deducible_array_bound (tree domain)
19266 if (domain == NULL_TREE)
19267 return false;
19269 tree max = TYPE_MAX_VALUE (domain);
19270 if (TREE_CODE (max) != MINUS_EXPR)
19271 return false;
19273 return deducible_expression (TREE_OPERAND (max, 0));
19276 /* Returns true iff the template arguments ARGS use a template parameter
19277 in a deducible way. */
19279 static bool
19280 deducible_template_args (tree args)
19282 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19284 bool deducible;
19285 tree elt = TREE_VEC_ELT (args, i);
19286 if (ARGUMENT_PACK_P (elt))
19287 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19288 else
19290 if (PACK_EXPANSION_P (elt))
19291 elt = PACK_EXPANSION_PATTERN (elt);
19292 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19293 deducible = true;
19294 else if (TYPE_P (elt))
19295 deducible = uses_deducible_template_parms (elt);
19296 else
19297 deducible = deducible_expression (elt);
19299 if (deducible)
19300 return true;
19302 return false;
19305 /* Returns true iff TYPE contains any deducible references to template
19306 parameters, as per 14.8.2.5. */
19308 static bool
19309 uses_deducible_template_parms (tree type)
19311 if (PACK_EXPANSION_P (type))
19312 type = PACK_EXPANSION_PATTERN (type);
19314 /* T
19315 cv-list T
19316 TT<T>
19317 TT<i>
19318 TT<> */
19319 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19320 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19321 return true;
19323 /* T*
19325 T&& */
19326 if (POINTER_TYPE_P (type))
19327 return uses_deducible_template_parms (TREE_TYPE (type));
19329 /* T[integer-constant ]
19330 type [i] */
19331 if (TREE_CODE (type) == ARRAY_TYPE)
19332 return (uses_deducible_template_parms (TREE_TYPE (type))
19333 || deducible_array_bound (TYPE_DOMAIN (type)));
19335 /* T type ::*
19336 type T::*
19337 T T::*
19338 T (type ::*)()
19339 type (T::*)()
19340 type (type ::*)(T)
19341 type (T::*)(T)
19342 T (type ::*)(T)
19343 T (T::*)()
19344 T (T::*)(T) */
19345 if (TYPE_PTRMEM_P (type))
19346 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19347 || (uses_deducible_template_parms
19348 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19350 /* template-name <T> (where template-name refers to a class template)
19351 template-name <i> (where template-name refers to a class template) */
19352 if (CLASS_TYPE_P (type)
19353 && CLASSTYPE_TEMPLATE_INFO (type)
19354 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19355 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19356 (CLASSTYPE_TI_ARGS (type)));
19358 /* type (T)
19360 T(T) */
19361 if (TREE_CODE (type) == FUNCTION_TYPE
19362 || TREE_CODE (type) == METHOD_TYPE)
19364 if (uses_deducible_template_parms (TREE_TYPE (type)))
19365 return true;
19366 tree parm = TYPE_ARG_TYPES (type);
19367 if (TREE_CODE (type) == METHOD_TYPE)
19368 parm = TREE_CHAIN (parm);
19369 for (; parm; parm = TREE_CHAIN (parm))
19370 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19371 return true;
19374 return false;
19377 /* Subroutine of type_unification_real and unify_pack_expansion to
19378 handle unification of a single P/A pair. Parameters are as
19379 for those functions. */
19381 static int
19382 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19383 int subr, unification_kind_t strict,
19384 bool explain_p)
19386 tree arg_expr = NULL_TREE;
19387 int arg_strict;
19389 if (arg == error_mark_node || parm == error_mark_node)
19390 return unify_invalid (explain_p);
19391 if (arg == unknown_type_node)
19392 /* We can't deduce anything from this, but we might get all the
19393 template args from other function args. */
19394 return unify_success (explain_p);
19396 /* Implicit conversions (Clause 4) will be performed on a function
19397 argument to convert it to the type of the corresponding function
19398 parameter if the parameter type contains no template-parameters that
19399 participate in template argument deduction. */
19400 if (strict != DEDUCE_EXACT
19401 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19402 /* For function parameters with no deducible template parameters,
19403 just return. We'll check non-dependent conversions later. */
19404 return unify_success (explain_p);
19406 switch (strict)
19408 case DEDUCE_CALL:
19409 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19410 | UNIFY_ALLOW_MORE_CV_QUAL
19411 | UNIFY_ALLOW_DERIVED);
19412 break;
19414 case DEDUCE_CONV:
19415 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19416 break;
19418 case DEDUCE_EXACT:
19419 arg_strict = UNIFY_ALLOW_NONE;
19420 break;
19422 default:
19423 gcc_unreachable ();
19426 /* We only do these transformations if this is the top-level
19427 parameter_type_list in a call or declaration matching; in other
19428 situations (nested function declarators, template argument lists) we
19429 won't be comparing a type to an expression, and we don't do any type
19430 adjustments. */
19431 if (!subr)
19433 if (!TYPE_P (arg))
19435 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19436 if (type_unknown_p (arg))
19438 /* [temp.deduct.type] A template-argument can be
19439 deduced from a pointer to function or pointer
19440 to member function argument if the set of
19441 overloaded functions does not contain function
19442 templates and at most one of a set of
19443 overloaded functions provides a unique
19444 match. */
19445 resolve_overloaded_unification (tparms, targs, parm,
19446 arg, strict,
19447 arg_strict, explain_p);
19448 /* If a unique match was not found, this is a
19449 non-deduced context, so we still succeed. */
19450 return unify_success (explain_p);
19453 arg_expr = arg;
19454 arg = unlowered_expr_type (arg);
19455 if (arg == error_mark_node)
19456 return unify_invalid (explain_p);
19459 arg_strict |=
19460 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19462 else
19463 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19464 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19465 return unify_template_argument_mismatch (explain_p, parm, arg);
19467 /* For deduction from an init-list we need the actual list. */
19468 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19469 arg = arg_expr;
19470 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19473 /* for_each_template_parm callback that always returns 0. */
19475 static int
19476 zero_r (tree, void *)
19478 return 0;
19481 /* for_each_template_parm any_fn callback to handle deduction of a template
19482 type argument from the type of an array bound. */
19484 static int
19485 array_deduction_r (tree t, void *data)
19487 tree_pair_p d = (tree_pair_p)data;
19488 tree &tparms = d->purpose;
19489 tree &targs = d->value;
19491 if (TREE_CODE (t) == ARRAY_TYPE)
19492 if (tree dom = TYPE_DOMAIN (t))
19493 if (tree max = TYPE_MAX_VALUE (dom))
19495 if (TREE_CODE (max) == MINUS_EXPR)
19496 max = TREE_OPERAND (max, 0);
19497 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19498 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19499 UNIFY_ALLOW_NONE, /*explain*/false);
19502 /* Keep walking. */
19503 return 0;
19506 /* Try to deduce any not-yet-deduced template type arguments from the type of
19507 an array bound. This is handled separately from unify because 14.8.2.5 says
19508 "The type of a type parameter is only deduced from an array bound if it is
19509 not otherwise deduced." */
19511 static void
19512 try_array_deduction (tree tparms, tree targs, tree parm)
19514 tree_pair_s data = { tparms, targs };
19515 hash_set<tree> visited;
19516 for_each_template_parm (parm, zero_r, &data, &visited,
19517 /*nondeduced*/false, array_deduction_r);
19520 /* Most parms like fn_type_unification.
19522 If SUBR is 1, we're being called recursively (to unify the
19523 arguments of a function or method parameter of a function
19524 template).
19526 CHECKS is a pointer to a vector of access checks encountered while
19527 substituting default template arguments. */
19529 static int
19530 type_unification_real (tree tparms,
19531 tree full_targs,
19532 tree xparms,
19533 const tree *xargs,
19534 unsigned int xnargs,
19535 int subr,
19536 unification_kind_t strict,
19537 int flags,
19538 vec<deferred_access_check, va_gc> **checks,
19539 bool explain_p)
19541 tree parm, arg;
19542 int i;
19543 int ntparms = TREE_VEC_LENGTH (tparms);
19544 int saw_undeduced = 0;
19545 tree parms;
19546 const tree *args;
19547 unsigned int nargs;
19548 unsigned int ia;
19550 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19551 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19552 gcc_assert (ntparms > 0);
19554 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19556 /* Reset the number of non-defaulted template arguments contained
19557 in TARGS. */
19558 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19560 again:
19561 parms = xparms;
19562 args = xargs;
19563 nargs = xnargs;
19565 ia = 0;
19566 while (parms && parms != void_list_node
19567 && ia < nargs)
19569 parm = TREE_VALUE (parms);
19571 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19572 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19573 /* For a function parameter pack that occurs at the end of the
19574 parameter-declaration-list, the type A of each remaining
19575 argument of the call is compared with the type P of the
19576 declarator-id of the function parameter pack. */
19577 break;
19579 parms = TREE_CHAIN (parms);
19581 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19582 /* For a function parameter pack that does not occur at the
19583 end of the parameter-declaration-list, the type of the
19584 parameter pack is a non-deduced context. */
19585 continue;
19587 arg = args[ia];
19588 ++ia;
19590 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19591 explain_p))
19592 return 1;
19595 if (parms
19596 && parms != void_list_node
19597 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19599 /* Unify the remaining arguments with the pack expansion type. */
19600 tree argvec;
19601 tree parmvec = make_tree_vec (1);
19603 /* Allocate a TREE_VEC and copy in all of the arguments */
19604 argvec = make_tree_vec (nargs - ia);
19605 for (i = 0; ia < nargs; ++ia, ++i)
19606 TREE_VEC_ELT (argvec, i) = args[ia];
19608 /* Copy the parameter into parmvec. */
19609 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19610 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19611 /*subr=*/subr, explain_p))
19612 return 1;
19614 /* Advance to the end of the list of parameters. */
19615 parms = TREE_CHAIN (parms);
19618 /* Fail if we've reached the end of the parm list, and more args
19619 are present, and the parm list isn't variadic. */
19620 if (ia < nargs && parms == void_list_node)
19621 return unify_too_many_arguments (explain_p, nargs, ia);
19622 /* Fail if parms are left and they don't have default values and
19623 they aren't all deduced as empty packs (c++/57397). This is
19624 consistent with sufficient_parms_p. */
19625 if (parms && parms != void_list_node
19626 && TREE_PURPOSE (parms) == NULL_TREE)
19628 unsigned int count = nargs;
19629 tree p = parms;
19630 bool type_pack_p;
19633 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19634 if (!type_pack_p)
19635 count++;
19636 p = TREE_CHAIN (p);
19638 while (p && p != void_list_node);
19639 if (count != nargs)
19640 return unify_too_few_arguments (explain_p, ia, count,
19641 type_pack_p);
19644 if (!subr)
19646 tsubst_flags_t complain = (explain_p
19647 ? tf_warning_or_error
19648 : tf_none);
19649 bool tried_array_deduction = (cxx_dialect < cxx17);
19651 for (i = 0; i < ntparms; i++)
19653 tree targ = TREE_VEC_ELT (targs, i);
19654 tree tparm = TREE_VEC_ELT (tparms, i);
19656 /* Clear the "incomplete" flags on all argument packs now so that
19657 substituting them into later default arguments works. */
19658 if (targ && ARGUMENT_PACK_P (targ))
19660 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19661 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19664 if (targ || tparm == error_mark_node)
19665 continue;
19666 tparm = TREE_VALUE (tparm);
19668 if (TREE_CODE (tparm) == TYPE_DECL
19669 && !tried_array_deduction)
19671 try_array_deduction (tparms, targs, xparms);
19672 tried_array_deduction = true;
19673 if (TREE_VEC_ELT (targs, i))
19674 continue;
19677 /* If this is an undeduced nontype parameter that depends on
19678 a type parameter, try another pass; its type may have been
19679 deduced from a later argument than the one from which
19680 this parameter can be deduced. */
19681 if (TREE_CODE (tparm) == PARM_DECL
19682 && uses_template_parms (TREE_TYPE (tparm))
19683 && saw_undeduced < 2)
19685 saw_undeduced = 1;
19686 continue;
19689 /* Core issue #226 (C++0x) [temp.deduct]:
19691 If a template argument has not been deduced, its
19692 default template argument, if any, is used.
19694 When we are in C++98 mode, TREE_PURPOSE will either
19695 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19696 to explicitly check cxx_dialect here. */
19697 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19698 /* OK, there is a default argument. Wait until after the
19699 conversion check to do substitution. */
19700 continue;
19702 /* If the type parameter is a parameter pack, then it will
19703 be deduced to an empty parameter pack. */
19704 if (template_parameter_pack_p (tparm))
19706 tree arg;
19708 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19710 arg = make_node (NONTYPE_ARGUMENT_PACK);
19711 TREE_CONSTANT (arg) = 1;
19713 else
19714 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19716 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19718 TREE_VEC_ELT (targs, i) = arg;
19719 continue;
19722 return unify_parameter_deduction_failure (explain_p, tparm);
19725 /* DR 1391: All parameters have args, now check non-dependent parms for
19726 convertibility. */
19727 if (saw_undeduced < 2)
19728 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19729 parms && parms != void_list_node && ia < nargs; )
19731 parm = TREE_VALUE (parms);
19733 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19734 && (!TREE_CHAIN (parms)
19735 || TREE_CHAIN (parms) == void_list_node))
19736 /* For a function parameter pack that occurs at the end of the
19737 parameter-declaration-list, the type A of each remaining
19738 argument of the call is compared with the type P of the
19739 declarator-id of the function parameter pack. */
19740 break;
19742 parms = TREE_CHAIN (parms);
19744 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19745 /* For a function parameter pack that does not occur at the
19746 end of the parameter-declaration-list, the type of the
19747 parameter pack is a non-deduced context. */
19748 continue;
19750 arg = args[ia];
19751 ++ia;
19753 if (uses_template_parms (parm))
19754 continue;
19755 if (check_non_deducible_conversion (parm, arg, strict, flags,
19756 explain_p))
19757 return 1;
19760 /* Now substitute into the default template arguments. */
19761 for (i = 0; i < ntparms; i++)
19763 tree targ = TREE_VEC_ELT (targs, i);
19764 tree tparm = TREE_VEC_ELT (tparms, i);
19766 if (targ || tparm == error_mark_node)
19767 continue;
19768 tree parm = TREE_VALUE (tparm);
19770 if (TREE_CODE (parm) == PARM_DECL
19771 && uses_template_parms (TREE_TYPE (parm))
19772 && saw_undeduced < 2)
19773 continue;
19775 tree arg = TREE_PURPOSE (tparm);
19776 reopen_deferring_access_checks (*checks);
19777 location_t save_loc = input_location;
19778 if (DECL_P (parm))
19779 input_location = DECL_SOURCE_LOCATION (parm);
19780 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19781 if (!uses_template_parms (arg))
19782 arg = convert_template_argument (parm, arg, full_targs, complain,
19783 i, NULL_TREE);
19784 else if (saw_undeduced < 2)
19785 arg = NULL_TREE;
19786 else
19787 arg = error_mark_node;
19788 input_location = save_loc;
19789 *checks = get_deferred_access_checks ();
19790 pop_deferring_access_checks ();
19791 if (arg == error_mark_node)
19792 return 1;
19793 else if (arg)
19795 TREE_VEC_ELT (targs, i) = arg;
19796 /* The position of the first default template argument,
19797 is also the number of non-defaulted arguments in TARGS.
19798 Record that. */
19799 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19804 if (saw_undeduced++ == 1)
19805 goto again;
19808 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19809 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19811 return unify_success (explain_p);
19814 /* Subroutine of type_unification_real. Args are like the variables
19815 at the call site. ARG is an overloaded function (or template-id);
19816 we try deducing template args from each of the overloads, and if
19817 only one succeeds, we go with that. Modifies TARGS and returns
19818 true on success. */
19820 static bool
19821 resolve_overloaded_unification (tree tparms,
19822 tree targs,
19823 tree parm,
19824 tree arg,
19825 unification_kind_t strict,
19826 int sub_strict,
19827 bool explain_p)
19829 tree tempargs = copy_node (targs);
19830 int good = 0;
19831 tree goodfn = NULL_TREE;
19832 bool addr_p;
19834 if (TREE_CODE (arg) == ADDR_EXPR)
19836 arg = TREE_OPERAND (arg, 0);
19837 addr_p = true;
19839 else
19840 addr_p = false;
19842 if (TREE_CODE (arg) == COMPONENT_REF)
19843 /* Handle `&x' where `x' is some static or non-static member
19844 function name. */
19845 arg = TREE_OPERAND (arg, 1);
19847 if (TREE_CODE (arg) == OFFSET_REF)
19848 arg = TREE_OPERAND (arg, 1);
19850 /* Strip baselink information. */
19851 if (BASELINK_P (arg))
19852 arg = BASELINK_FUNCTIONS (arg);
19854 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19856 /* If we got some explicit template args, we need to plug them into
19857 the affected templates before we try to unify, in case the
19858 explicit args will completely resolve the templates in question. */
19860 int ok = 0;
19861 tree expl_subargs = TREE_OPERAND (arg, 1);
19862 arg = TREE_OPERAND (arg, 0);
19864 for (lkp_iterator iter (arg); iter; ++iter)
19866 tree fn = *iter;
19867 tree subargs, elem;
19869 if (TREE_CODE (fn) != TEMPLATE_DECL)
19870 continue;
19872 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19873 expl_subargs, NULL_TREE, tf_none,
19874 /*require_all_args=*/true,
19875 /*use_default_args=*/true);
19876 if (subargs != error_mark_node
19877 && !any_dependent_template_arguments_p (subargs))
19879 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19880 if (try_one_overload (tparms, targs, tempargs, parm,
19881 elem, strict, sub_strict, addr_p, explain_p)
19882 && (!goodfn || !same_type_p (goodfn, elem)))
19884 goodfn = elem;
19885 ++good;
19888 else if (subargs)
19889 ++ok;
19891 /* If no templates (or more than one) are fully resolved by the
19892 explicit arguments, this template-id is a non-deduced context; it
19893 could still be OK if we deduce all template arguments for the
19894 enclosing call through other arguments. */
19895 if (good != 1)
19896 good = ok;
19898 else if (TREE_CODE (arg) != OVERLOAD
19899 && TREE_CODE (arg) != FUNCTION_DECL)
19900 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19901 -- but the deduction does not succeed because the expression is
19902 not just the function on its own. */
19903 return false;
19904 else
19905 for (lkp_iterator iter (arg); iter; ++iter)
19907 tree fn = *iter;
19908 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19909 strict, sub_strict, addr_p, explain_p)
19910 && (!goodfn || !decls_match (goodfn, fn)))
19912 goodfn = fn;
19913 ++good;
19917 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19918 to function or pointer to member function argument if the set of
19919 overloaded functions does not contain function templates and at most
19920 one of a set of overloaded functions provides a unique match.
19922 So if we found multiple possibilities, we return success but don't
19923 deduce anything. */
19925 if (good == 1)
19927 int i = TREE_VEC_LENGTH (targs);
19928 for (; i--; )
19929 if (TREE_VEC_ELT (tempargs, i))
19931 tree old = TREE_VEC_ELT (targs, i);
19932 tree new_ = TREE_VEC_ELT (tempargs, i);
19933 if (new_ && old && ARGUMENT_PACK_P (old)
19934 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19935 /* Don't forget explicit template arguments in a pack. */
19936 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19937 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19938 TREE_VEC_ELT (targs, i) = new_;
19941 if (good)
19942 return true;
19944 return false;
19947 /* Core DR 115: In contexts where deduction is done and fails, or in
19948 contexts where deduction is not done, if a template argument list is
19949 specified and it, along with any default template arguments, identifies
19950 a single function template specialization, then the template-id is an
19951 lvalue for the function template specialization. */
19953 tree
19954 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19956 tree expr, offset, baselink;
19957 bool addr;
19959 if (!type_unknown_p (orig_expr))
19960 return orig_expr;
19962 expr = orig_expr;
19963 addr = false;
19964 offset = NULL_TREE;
19965 baselink = NULL_TREE;
19967 if (TREE_CODE (expr) == ADDR_EXPR)
19969 expr = TREE_OPERAND (expr, 0);
19970 addr = true;
19972 if (TREE_CODE (expr) == OFFSET_REF)
19974 offset = expr;
19975 expr = TREE_OPERAND (expr, 1);
19977 if (BASELINK_P (expr))
19979 baselink = expr;
19980 expr = BASELINK_FUNCTIONS (expr);
19983 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19985 int good = 0;
19986 tree goodfn = NULL_TREE;
19988 /* If we got some explicit template args, we need to plug them into
19989 the affected templates before we try to unify, in case the
19990 explicit args will completely resolve the templates in question. */
19992 tree expl_subargs = TREE_OPERAND (expr, 1);
19993 tree arg = TREE_OPERAND (expr, 0);
19994 tree badfn = NULL_TREE;
19995 tree badargs = NULL_TREE;
19997 for (lkp_iterator iter (arg); iter; ++iter)
19999 tree fn = *iter;
20000 tree subargs, elem;
20002 if (TREE_CODE (fn) != TEMPLATE_DECL)
20003 continue;
20005 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20006 expl_subargs, NULL_TREE, tf_none,
20007 /*require_all_args=*/true,
20008 /*use_default_args=*/true);
20009 if (subargs != error_mark_node
20010 && !any_dependent_template_arguments_p (subargs))
20012 elem = instantiate_template (fn, subargs, tf_none);
20013 if (elem == error_mark_node)
20015 badfn = fn;
20016 badargs = subargs;
20018 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20020 goodfn = elem;
20021 ++good;
20025 if (good == 1)
20027 mark_used (goodfn);
20028 expr = goodfn;
20029 if (baselink)
20030 expr = build_baselink (BASELINK_BINFO (baselink),
20031 BASELINK_ACCESS_BINFO (baselink),
20032 expr, BASELINK_OPTYPE (baselink));
20033 if (offset)
20035 tree base
20036 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20037 expr = build_offset_ref (base, expr, addr, complain);
20039 if (addr)
20040 expr = cp_build_addr_expr (expr, complain);
20041 return expr;
20043 else if (good == 0 && badargs && (complain & tf_error))
20044 /* There were no good options and at least one bad one, so let the
20045 user know what the problem is. */
20046 instantiate_template (badfn, badargs, complain);
20048 return orig_expr;
20051 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20052 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20053 different overloads deduce different arguments for a given parm.
20054 ADDR_P is true if the expression for which deduction is being
20055 performed was of the form "& fn" rather than simply "fn".
20057 Returns 1 on success. */
20059 static int
20060 try_one_overload (tree tparms,
20061 tree orig_targs,
20062 tree targs,
20063 tree parm,
20064 tree arg,
20065 unification_kind_t strict,
20066 int sub_strict,
20067 bool addr_p,
20068 bool explain_p)
20070 int nargs;
20071 tree tempargs;
20072 int i;
20074 if (arg == error_mark_node)
20075 return 0;
20077 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20078 to function or pointer to member function argument if the set of
20079 overloaded functions does not contain function templates and at most
20080 one of a set of overloaded functions provides a unique match.
20082 So if this is a template, just return success. */
20084 if (uses_template_parms (arg))
20085 return 1;
20087 if (TREE_CODE (arg) == METHOD_TYPE)
20088 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20089 else if (addr_p)
20090 arg = build_pointer_type (arg);
20092 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20094 /* We don't copy orig_targs for this because if we have already deduced
20095 some template args from previous args, unify would complain when we
20096 try to deduce a template parameter for the same argument, even though
20097 there isn't really a conflict. */
20098 nargs = TREE_VEC_LENGTH (targs);
20099 tempargs = make_tree_vec (nargs);
20101 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20102 return 0;
20104 /* First make sure we didn't deduce anything that conflicts with
20105 explicitly specified args. */
20106 for (i = nargs; i--; )
20108 tree elt = TREE_VEC_ELT (tempargs, i);
20109 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20111 if (!elt)
20112 /*NOP*/;
20113 else if (uses_template_parms (elt))
20114 /* Since we're unifying against ourselves, we will fill in
20115 template args used in the function parm list with our own
20116 template parms. Discard them. */
20117 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20118 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20120 /* Check that the argument at each index of the deduced argument pack
20121 is equivalent to the corresponding explicitly specified argument.
20122 We may have deduced more arguments than were explicitly specified,
20123 and that's OK. */
20125 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20126 that's wrong if we deduce the same argument pack from multiple
20127 function arguments: it's only incomplete the first time. */
20129 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20130 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20132 if (TREE_VEC_LENGTH (deduced_pack)
20133 < TREE_VEC_LENGTH (explicit_pack))
20134 return 0;
20136 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20137 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20138 TREE_VEC_ELT (deduced_pack, j)))
20139 return 0;
20141 else if (oldelt && !template_args_equal (oldelt, elt))
20142 return 0;
20145 for (i = nargs; i--; )
20147 tree elt = TREE_VEC_ELT (tempargs, i);
20149 if (elt)
20150 TREE_VEC_ELT (targs, i) = elt;
20153 return 1;
20156 /* PARM is a template class (perhaps with unbound template
20157 parameters). ARG is a fully instantiated type. If ARG can be
20158 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20159 TARGS are as for unify. */
20161 static tree
20162 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20163 bool explain_p)
20165 tree copy_of_targs;
20167 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20168 return NULL_TREE;
20169 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20170 /* Matches anything. */;
20171 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20172 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20173 return NULL_TREE;
20175 /* We need to make a new template argument vector for the call to
20176 unify. If we used TARGS, we'd clutter it up with the result of
20177 the attempted unification, even if this class didn't work out.
20178 We also don't want to commit ourselves to all the unifications
20179 we've already done, since unification is supposed to be done on
20180 an argument-by-argument basis. In other words, consider the
20181 following pathological case:
20183 template <int I, int J, int K>
20184 struct S {};
20186 template <int I, int J>
20187 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20189 template <int I, int J, int K>
20190 void f(S<I, J, K>, S<I, I, I>);
20192 void g() {
20193 S<0, 0, 0> s0;
20194 S<0, 1, 2> s2;
20196 f(s0, s2);
20199 Now, by the time we consider the unification involving `s2', we
20200 already know that we must have `f<0, 0, 0>'. But, even though
20201 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20202 because there are two ways to unify base classes of S<0, 1, 2>
20203 with S<I, I, I>. If we kept the already deduced knowledge, we
20204 would reject the possibility I=1. */
20205 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20207 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20209 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20210 return NULL_TREE;
20211 return arg;
20214 /* If unification failed, we're done. */
20215 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20216 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20217 return NULL_TREE;
20219 return arg;
20222 /* Given a template type PARM and a class type ARG, find the unique
20223 base type in ARG that is an instance of PARM. We do not examine
20224 ARG itself; only its base-classes. If there is not exactly one
20225 appropriate base class, return NULL_TREE. PARM may be the type of
20226 a partial specialization, as well as a plain template type. Used
20227 by unify. */
20229 static enum template_base_result
20230 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20231 bool explain_p, tree *result)
20233 tree rval = NULL_TREE;
20234 tree binfo;
20236 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20238 binfo = TYPE_BINFO (complete_type (arg));
20239 if (!binfo)
20241 /* The type could not be completed. */
20242 *result = NULL_TREE;
20243 return tbr_incomplete_type;
20246 /* Walk in inheritance graph order. The search order is not
20247 important, and this avoids multiple walks of virtual bases. */
20248 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20250 tree r = try_class_unification (tparms, targs, parm,
20251 BINFO_TYPE (binfo), explain_p);
20253 if (r)
20255 /* If there is more than one satisfactory baseclass, then:
20257 [temp.deduct.call]
20259 If they yield more than one possible deduced A, the type
20260 deduction fails.
20262 applies. */
20263 if (rval && !same_type_p (r, rval))
20265 *result = NULL_TREE;
20266 return tbr_ambiguous_baseclass;
20269 rval = r;
20273 *result = rval;
20274 return tbr_success;
20277 /* Returns the level of DECL, which declares a template parameter. */
20279 static int
20280 template_decl_level (tree decl)
20282 switch (TREE_CODE (decl))
20284 case TYPE_DECL:
20285 case TEMPLATE_DECL:
20286 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20288 case PARM_DECL:
20289 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20291 default:
20292 gcc_unreachable ();
20294 return 0;
20297 /* Decide whether ARG can be unified with PARM, considering only the
20298 cv-qualifiers of each type, given STRICT as documented for unify.
20299 Returns nonzero iff the unification is OK on that basis. */
20301 static int
20302 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20304 int arg_quals = cp_type_quals (arg);
20305 int parm_quals = cp_type_quals (parm);
20307 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20308 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20310 /* Although a CVR qualifier is ignored when being applied to a
20311 substituted template parameter ([8.3.2]/1 for example), that
20312 does not allow us to unify "const T" with "int&" because both
20313 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20314 It is ok when we're allowing additional CV qualifiers
20315 at the outer level [14.8.2.1]/3,1st bullet. */
20316 if ((TREE_CODE (arg) == REFERENCE_TYPE
20317 || TREE_CODE (arg) == FUNCTION_TYPE
20318 || TREE_CODE (arg) == METHOD_TYPE)
20319 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20320 return 0;
20322 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20323 && (parm_quals & TYPE_QUAL_RESTRICT))
20324 return 0;
20327 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20328 && (arg_quals & parm_quals) != parm_quals)
20329 return 0;
20331 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20332 && (parm_quals & arg_quals) != arg_quals)
20333 return 0;
20335 return 1;
20338 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20339 void
20340 template_parm_level_and_index (tree parm, int* level, int* index)
20342 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20343 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20344 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20346 *index = TEMPLATE_TYPE_IDX (parm);
20347 *level = TEMPLATE_TYPE_LEVEL (parm);
20349 else
20351 *index = TEMPLATE_PARM_IDX (parm);
20352 *level = TEMPLATE_PARM_LEVEL (parm);
20356 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20357 do { \
20358 if (unify (TP, TA, P, A, S, EP)) \
20359 return 1; \
20360 } while (0)
20362 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20363 expansion at the end of PACKED_PARMS. Returns 0 if the type
20364 deduction succeeds, 1 otherwise. STRICT is the same as in
20365 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20366 function call argument list. We'll need to adjust the arguments to make them
20367 types. SUBR tells us if this is from a recursive call to
20368 type_unification_real, or for comparing two template argument
20369 lists. */
20371 static int
20372 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20373 tree packed_args, unification_kind_t strict,
20374 bool subr, bool explain_p)
20376 tree parm
20377 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20378 tree pattern = PACK_EXPANSION_PATTERN (parm);
20379 tree pack, packs = NULL_TREE;
20380 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20382 /* Add in any args remembered from an earlier partial instantiation. */
20383 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20384 int levels = TMPL_ARGS_DEPTH (targs);
20386 packed_args = expand_template_argument_pack (packed_args);
20388 int len = TREE_VEC_LENGTH (packed_args);
20390 /* Determine the parameter packs we will be deducing from the
20391 pattern, and record their current deductions. */
20392 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20393 pack; pack = TREE_CHAIN (pack))
20395 tree parm_pack = TREE_VALUE (pack);
20396 int idx, level;
20398 /* Determine the index and level of this parameter pack. */
20399 template_parm_level_and_index (parm_pack, &level, &idx);
20400 if (level < levels)
20401 continue;
20403 /* Keep track of the parameter packs and their corresponding
20404 argument packs. */
20405 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20406 TREE_TYPE (packs) = make_tree_vec (len - start);
20409 /* Loop through all of the arguments that have not yet been
20410 unified and unify each with the pattern. */
20411 for (i = start; i < len; i++)
20413 tree parm;
20414 bool any_explicit = false;
20415 tree arg = TREE_VEC_ELT (packed_args, i);
20417 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20418 or the element of its argument pack at the current index if
20419 this argument was explicitly specified. */
20420 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20422 int idx, level;
20423 tree arg, pargs;
20424 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20426 arg = NULL_TREE;
20427 if (TREE_VALUE (pack)
20428 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20429 && (i - start < TREE_VEC_LENGTH (pargs)))
20431 any_explicit = true;
20432 arg = TREE_VEC_ELT (pargs, i - start);
20434 TMPL_ARG (targs, level, idx) = arg;
20437 /* If we had explicit template arguments, substitute them into the
20438 pattern before deduction. */
20439 if (any_explicit)
20441 /* Some arguments might still be unspecified or dependent. */
20442 bool dependent;
20443 ++processing_template_decl;
20444 dependent = any_dependent_template_arguments_p (targs);
20445 if (!dependent)
20446 --processing_template_decl;
20447 parm = tsubst (pattern, targs,
20448 explain_p ? tf_warning_or_error : tf_none,
20449 NULL_TREE);
20450 if (dependent)
20451 --processing_template_decl;
20452 if (parm == error_mark_node)
20453 return 1;
20455 else
20456 parm = pattern;
20458 /* Unify the pattern with the current argument. */
20459 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20460 explain_p))
20461 return 1;
20463 /* For each parameter pack, collect the deduced value. */
20464 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20466 int idx, level;
20467 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20469 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20470 TMPL_ARG (targs, level, idx);
20474 /* Verify that the results of unification with the parameter packs
20475 produce results consistent with what we've seen before, and make
20476 the deduced argument packs available. */
20477 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20479 tree old_pack = TREE_VALUE (pack);
20480 tree new_args = TREE_TYPE (pack);
20481 int i, len = TREE_VEC_LENGTH (new_args);
20482 int idx, level;
20483 bool nondeduced_p = false;
20485 /* By default keep the original deduced argument pack.
20486 If necessary, more specific code is going to update the
20487 resulting deduced argument later down in this function. */
20488 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20489 TMPL_ARG (targs, level, idx) = old_pack;
20491 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20492 actually deduce anything. */
20493 for (i = 0; i < len && !nondeduced_p; ++i)
20494 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20495 nondeduced_p = true;
20496 if (nondeduced_p)
20497 continue;
20499 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20501 /* If we had fewer function args than explicit template args,
20502 just use the explicits. */
20503 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20504 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20505 if (len < explicit_len)
20506 new_args = explicit_args;
20509 if (!old_pack)
20511 tree result;
20512 /* Build the deduced *_ARGUMENT_PACK. */
20513 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20515 result = make_node (NONTYPE_ARGUMENT_PACK);
20516 TREE_CONSTANT (result) = 1;
20518 else
20519 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20521 SET_ARGUMENT_PACK_ARGS (result, new_args);
20523 /* Note the deduced argument packs for this parameter
20524 pack. */
20525 TMPL_ARG (targs, level, idx) = result;
20527 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20528 && (ARGUMENT_PACK_ARGS (old_pack)
20529 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20531 /* We only had the explicitly-provided arguments before, but
20532 now we have a complete set of arguments. */
20533 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20535 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20536 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20537 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20539 else
20541 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20542 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20544 if (!comp_template_args (old_args, new_args,
20545 &bad_old_arg, &bad_new_arg))
20546 /* Inconsistent unification of this parameter pack. */
20547 return unify_parameter_pack_inconsistent (explain_p,
20548 bad_old_arg,
20549 bad_new_arg);
20553 return unify_success (explain_p);
20556 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20557 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20558 parameters and return value are as for unify. */
20560 static int
20561 unify_array_domain (tree tparms, tree targs,
20562 tree parm_dom, tree arg_dom,
20563 bool explain_p)
20565 tree parm_max;
20566 tree arg_max;
20567 bool parm_cst;
20568 bool arg_cst;
20570 /* Our representation of array types uses "N - 1" as the
20571 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20572 not an integer constant. We cannot unify arbitrarily
20573 complex expressions, so we eliminate the MINUS_EXPRs
20574 here. */
20575 parm_max = TYPE_MAX_VALUE (parm_dom);
20576 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20577 if (!parm_cst)
20579 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20580 parm_max = TREE_OPERAND (parm_max, 0);
20582 arg_max = TYPE_MAX_VALUE (arg_dom);
20583 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20584 if (!arg_cst)
20586 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20587 trying to unify the type of a variable with the type
20588 of a template parameter. For example:
20590 template <unsigned int N>
20591 void f (char (&) [N]);
20592 int g();
20593 void h(int i) {
20594 char a[g(i)];
20595 f(a);
20598 Here, the type of the ARG will be "int [g(i)]", and
20599 may be a SAVE_EXPR, etc. */
20600 if (TREE_CODE (arg_max) != MINUS_EXPR)
20601 return unify_vla_arg (explain_p, arg_dom);
20602 arg_max = TREE_OPERAND (arg_max, 0);
20605 /* If only one of the bounds used a MINUS_EXPR, compensate
20606 by adding one to the other bound. */
20607 if (parm_cst && !arg_cst)
20608 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20609 integer_type_node,
20610 parm_max,
20611 integer_one_node);
20612 else if (arg_cst && !parm_cst)
20613 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20614 integer_type_node,
20615 arg_max,
20616 integer_one_node);
20618 return unify (tparms, targs, parm_max, arg_max,
20619 UNIFY_ALLOW_INTEGER, explain_p);
20622 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20624 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20626 static pa_kind_t
20627 pa_kind (tree t)
20629 if (PACK_EXPANSION_P (t))
20630 t = PACK_EXPANSION_PATTERN (t);
20631 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20632 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20633 || DECL_TYPE_TEMPLATE_P (t))
20634 return pa_tmpl;
20635 else if (TYPE_P (t))
20636 return pa_type;
20637 else
20638 return pa_expr;
20641 /* Deduce the value of template parameters. TPARMS is the (innermost)
20642 set of template parameters to a template. TARGS is the bindings
20643 for those template parameters, as determined thus far; TARGS may
20644 include template arguments for outer levels of template parameters
20645 as well. PARM is a parameter to a template function, or a
20646 subcomponent of that parameter; ARG is the corresponding argument.
20647 This function attempts to match PARM with ARG in a manner
20648 consistent with the existing assignments in TARGS. If more values
20649 are deduced, then TARGS is updated.
20651 Returns 0 if the type deduction succeeds, 1 otherwise. The
20652 parameter STRICT is a bitwise or of the following flags:
20654 UNIFY_ALLOW_NONE:
20655 Require an exact match between PARM and ARG.
20656 UNIFY_ALLOW_MORE_CV_QUAL:
20657 Allow the deduced ARG to be more cv-qualified (by qualification
20658 conversion) than ARG.
20659 UNIFY_ALLOW_LESS_CV_QUAL:
20660 Allow the deduced ARG to be less cv-qualified than ARG.
20661 UNIFY_ALLOW_DERIVED:
20662 Allow the deduced ARG to be a template base class of ARG,
20663 or a pointer to a template base class of the type pointed to by
20664 ARG.
20665 UNIFY_ALLOW_INTEGER:
20666 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20667 case for more information.
20668 UNIFY_ALLOW_OUTER_LEVEL:
20669 This is the outermost level of a deduction. Used to determine validity
20670 of qualification conversions. A valid qualification conversion must
20671 have const qualified pointers leading up to the inner type which
20672 requires additional CV quals, except at the outer level, where const
20673 is not required [conv.qual]. It would be normal to set this flag in
20674 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20675 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20676 This is the outermost level of a deduction, and PARM can be more CV
20677 qualified at this point.
20678 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20679 This is the outermost level of a deduction, and PARM can be less CV
20680 qualified at this point. */
20682 static int
20683 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20684 bool explain_p)
20686 int idx;
20687 tree targ;
20688 tree tparm;
20689 int strict_in = strict;
20690 tsubst_flags_t complain = (explain_p
20691 ? tf_warning_or_error
20692 : tf_none);
20694 /* I don't think this will do the right thing with respect to types.
20695 But the only case I've seen it in so far has been array bounds, where
20696 signedness is the only information lost, and I think that will be
20697 okay. */
20698 while (CONVERT_EXPR_P (parm))
20699 parm = TREE_OPERAND (parm, 0);
20701 if (arg == error_mark_node)
20702 return unify_invalid (explain_p);
20703 if (arg == unknown_type_node
20704 || arg == init_list_type_node)
20705 /* We can't deduce anything from this, but we might get all the
20706 template args from other function args. */
20707 return unify_success (explain_p);
20709 if (parm == any_targ_node || arg == any_targ_node)
20710 return unify_success (explain_p);
20712 /* If PARM uses template parameters, then we can't bail out here,
20713 even if ARG == PARM, since we won't record unifications for the
20714 template parameters. We might need them if we're trying to
20715 figure out which of two things is more specialized. */
20716 if (arg == parm && !uses_template_parms (parm))
20717 return unify_success (explain_p);
20719 /* Handle init lists early, so the rest of the function can assume
20720 we're dealing with a type. */
20721 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20723 tree elt, elttype;
20724 unsigned i;
20725 tree orig_parm = parm;
20727 /* Replace T with std::initializer_list<T> for deduction. */
20728 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20729 && flag_deduce_init_list)
20730 parm = listify (parm);
20732 if (!is_std_init_list (parm)
20733 && TREE_CODE (parm) != ARRAY_TYPE)
20734 /* We can only deduce from an initializer list argument if the
20735 parameter is std::initializer_list or an array; otherwise this
20736 is a non-deduced context. */
20737 return unify_success (explain_p);
20739 if (TREE_CODE (parm) == ARRAY_TYPE)
20740 elttype = TREE_TYPE (parm);
20741 else
20743 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20744 /* Deduction is defined in terms of a single type, so just punt
20745 on the (bizarre) std::initializer_list<T...>. */
20746 if (PACK_EXPANSION_P (elttype))
20747 return unify_success (explain_p);
20750 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20752 int elt_strict = strict;
20754 if (elt == error_mark_node)
20755 return unify_invalid (explain_p);
20757 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20759 tree type = TREE_TYPE (elt);
20760 if (type == error_mark_node)
20761 return unify_invalid (explain_p);
20762 /* It should only be possible to get here for a call. */
20763 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20764 elt_strict |= maybe_adjust_types_for_deduction
20765 (DEDUCE_CALL, &elttype, &type, elt);
20766 elt = type;
20769 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20770 explain_p);
20773 if (TREE_CODE (parm) == ARRAY_TYPE
20774 && deducible_array_bound (TYPE_DOMAIN (parm)))
20776 /* Also deduce from the length of the initializer list. */
20777 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20778 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20779 if (idx == error_mark_node)
20780 return unify_invalid (explain_p);
20781 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20782 idx, explain_p);
20785 /* If the std::initializer_list<T> deduction worked, replace the
20786 deduced A with std::initializer_list<A>. */
20787 if (orig_parm != parm)
20789 idx = TEMPLATE_TYPE_IDX (orig_parm);
20790 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20791 targ = listify (targ);
20792 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20794 return unify_success (explain_p);
20797 /* If parm and arg aren't the same kind of thing (template, type, or
20798 expression), fail early. */
20799 if (pa_kind (parm) != pa_kind (arg))
20800 return unify_invalid (explain_p);
20802 /* Immediately reject some pairs that won't unify because of
20803 cv-qualification mismatches. */
20804 if (TREE_CODE (arg) == TREE_CODE (parm)
20805 && TYPE_P (arg)
20806 /* It is the elements of the array which hold the cv quals of an array
20807 type, and the elements might be template type parms. We'll check
20808 when we recurse. */
20809 && TREE_CODE (arg) != ARRAY_TYPE
20810 /* We check the cv-qualifiers when unifying with template type
20811 parameters below. We want to allow ARG `const T' to unify with
20812 PARM `T' for example, when computing which of two templates
20813 is more specialized, for example. */
20814 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20815 && !check_cv_quals_for_unify (strict_in, arg, parm))
20816 return unify_cv_qual_mismatch (explain_p, parm, arg);
20818 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20819 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20820 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20821 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20822 strict &= ~UNIFY_ALLOW_DERIVED;
20823 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20824 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20826 switch (TREE_CODE (parm))
20828 case TYPENAME_TYPE:
20829 case SCOPE_REF:
20830 case UNBOUND_CLASS_TEMPLATE:
20831 /* In a type which contains a nested-name-specifier, template
20832 argument values cannot be deduced for template parameters used
20833 within the nested-name-specifier. */
20834 return unify_success (explain_p);
20836 case TEMPLATE_TYPE_PARM:
20837 case TEMPLATE_TEMPLATE_PARM:
20838 case BOUND_TEMPLATE_TEMPLATE_PARM:
20839 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20840 if (error_operand_p (tparm))
20841 return unify_invalid (explain_p);
20843 if (TEMPLATE_TYPE_LEVEL (parm)
20844 != template_decl_level (tparm))
20845 /* The PARM is not one we're trying to unify. Just check
20846 to see if it matches ARG. */
20848 if (TREE_CODE (arg) == TREE_CODE (parm)
20849 && (is_auto (parm) ? is_auto (arg)
20850 : same_type_p (parm, arg)))
20851 return unify_success (explain_p);
20852 else
20853 return unify_type_mismatch (explain_p, parm, arg);
20855 idx = TEMPLATE_TYPE_IDX (parm);
20856 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20857 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20858 if (error_operand_p (tparm))
20859 return unify_invalid (explain_p);
20861 /* Check for mixed types and values. */
20862 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20863 && TREE_CODE (tparm) != TYPE_DECL)
20864 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20865 && TREE_CODE (tparm) != TEMPLATE_DECL))
20866 gcc_unreachable ();
20868 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20870 if ((strict_in & UNIFY_ALLOW_DERIVED)
20871 && CLASS_TYPE_P (arg))
20873 /* First try to match ARG directly. */
20874 tree t = try_class_unification (tparms, targs, parm, arg,
20875 explain_p);
20876 if (!t)
20878 /* Otherwise, look for a suitable base of ARG, as below. */
20879 enum template_base_result r;
20880 r = get_template_base (tparms, targs, parm, arg,
20881 explain_p, &t);
20882 if (!t)
20883 return unify_no_common_base (explain_p, r, parm, arg);
20884 arg = t;
20887 /* ARG must be constructed from a template class or a template
20888 template parameter. */
20889 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20890 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20891 return unify_template_deduction_failure (explain_p, parm, arg);
20893 /* Deduce arguments T, i from TT<T> or TT<i>. */
20894 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20895 return 1;
20897 arg = TYPE_TI_TEMPLATE (arg);
20899 /* Fall through to deduce template name. */
20902 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20903 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20905 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20907 /* Simple cases: Value already set, does match or doesn't. */
20908 if (targ != NULL_TREE && template_args_equal (targ, arg))
20909 return unify_success (explain_p);
20910 else if (targ)
20911 return unify_inconsistency (explain_p, parm, targ, arg);
20913 else
20915 /* If PARM is `const T' and ARG is only `int', we don't have
20916 a match unless we are allowing additional qualification.
20917 If ARG is `const int' and PARM is just `T' that's OK;
20918 that binds `const int' to `T'. */
20919 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20920 arg, parm))
20921 return unify_cv_qual_mismatch (explain_p, parm, arg);
20923 /* Consider the case where ARG is `const volatile int' and
20924 PARM is `const T'. Then, T should be `volatile int'. */
20925 arg = cp_build_qualified_type_real
20926 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20927 if (arg == error_mark_node)
20928 return unify_invalid (explain_p);
20930 /* Simple cases: Value already set, does match or doesn't. */
20931 if (targ != NULL_TREE && same_type_p (targ, arg))
20932 return unify_success (explain_p);
20933 else if (targ)
20934 return unify_inconsistency (explain_p, parm, targ, arg);
20936 /* Make sure that ARG is not a variable-sized array. (Note
20937 that were talking about variable-sized arrays (like
20938 `int[n]'), rather than arrays of unknown size (like
20939 `int[]').) We'll get very confused by such a type since
20940 the bound of the array is not constant, and therefore
20941 not mangleable. Besides, such types are not allowed in
20942 ISO C++, so we can do as we please here. We do allow
20943 them for 'auto' deduction, since that isn't ABI-exposed. */
20944 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20945 return unify_vla_arg (explain_p, arg);
20947 /* Strip typedefs as in convert_template_argument. */
20948 arg = canonicalize_type_argument (arg, tf_none);
20951 /* If ARG is a parameter pack or an expansion, we cannot unify
20952 against it unless PARM is also a parameter pack. */
20953 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20954 && !template_parameter_pack_p (parm))
20955 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20957 /* If the argument deduction results is a METHOD_TYPE,
20958 then there is a problem.
20959 METHOD_TYPE doesn't map to any real C++ type the result of
20960 the deduction can not be of that type. */
20961 if (TREE_CODE (arg) == METHOD_TYPE)
20962 return unify_method_type_error (explain_p, arg);
20964 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20965 return unify_success (explain_p);
20967 case TEMPLATE_PARM_INDEX:
20968 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20969 if (error_operand_p (tparm))
20970 return unify_invalid (explain_p);
20972 if (TEMPLATE_PARM_LEVEL (parm)
20973 != template_decl_level (tparm))
20975 /* The PARM is not one we're trying to unify. Just check
20976 to see if it matches ARG. */
20977 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20978 && cp_tree_equal (parm, arg));
20979 if (result)
20980 unify_expression_unequal (explain_p, parm, arg);
20981 return result;
20984 idx = TEMPLATE_PARM_IDX (parm);
20985 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20987 if (targ)
20989 if ((strict & UNIFY_ALLOW_INTEGER)
20990 && TREE_TYPE (targ) && TREE_TYPE (arg)
20991 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20992 /* We're deducing from an array bound, the type doesn't matter. */
20993 arg = fold_convert (TREE_TYPE (targ), arg);
20994 int x = !cp_tree_equal (targ, arg);
20995 if (x)
20996 unify_inconsistency (explain_p, parm, targ, arg);
20997 return x;
21000 /* [temp.deduct.type] If, in the declaration of a function template
21001 with a non-type template-parameter, the non-type
21002 template-parameter is used in an expression in the function
21003 parameter-list and, if the corresponding template-argument is
21004 deduced, the template-argument type shall match the type of the
21005 template-parameter exactly, except that a template-argument
21006 deduced from an array bound may be of any integral type.
21007 The non-type parameter might use already deduced type parameters. */
21008 ++processing_template_decl;
21009 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21010 --processing_template_decl;
21011 if (tree a = type_uses_auto (tparm))
21013 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21014 if (tparm == error_mark_node)
21015 return 1;
21018 if (!TREE_TYPE (arg))
21019 /* Template-parameter dependent expression. Just accept it for now.
21020 It will later be processed in convert_template_argument. */
21022 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21023 non_reference (tparm)))
21024 /* OK */;
21025 else if ((strict & UNIFY_ALLOW_INTEGER)
21026 && CP_INTEGRAL_TYPE_P (tparm))
21027 /* Convert the ARG to the type of PARM; the deduced non-type
21028 template argument must exactly match the types of the
21029 corresponding parameter. */
21030 arg = fold (build_nop (tparm, arg));
21031 else if (uses_template_parms (tparm))
21033 /* We haven't deduced the type of this parameter yet. */
21034 if (cxx_dialect >= cxx17
21035 /* We deduce from array bounds in try_array_deduction. */
21036 && !(strict & UNIFY_ALLOW_INTEGER))
21038 /* Deduce it from the non-type argument. */
21039 tree atype = TREE_TYPE (arg);
21040 RECUR_AND_CHECK_FAILURE (tparms, targs,
21041 tparm, atype,
21042 UNIFY_ALLOW_NONE, explain_p);
21044 else
21045 /* Try again later. */
21046 return unify_success (explain_p);
21048 else
21049 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21051 /* If ARG is a parameter pack or an expansion, we cannot unify
21052 against it unless PARM is also a parameter pack. */
21053 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21054 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21055 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21058 bool removed_attr = false;
21059 arg = strip_typedefs_expr (arg, &removed_attr);
21061 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21062 return unify_success (explain_p);
21064 case PTRMEM_CST:
21066 /* A pointer-to-member constant can be unified only with
21067 another constant. */
21068 if (TREE_CODE (arg) != PTRMEM_CST)
21069 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21071 /* Just unify the class member. It would be useless (and possibly
21072 wrong, depending on the strict flags) to unify also
21073 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21074 arg refer to the same variable, even if through different
21075 classes. For instance:
21077 struct A { int x; };
21078 struct B : A { };
21080 Unification of &A::x and &B::x must succeed. */
21081 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21082 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21085 case POINTER_TYPE:
21087 if (!TYPE_PTR_P (arg))
21088 return unify_type_mismatch (explain_p, parm, arg);
21090 /* [temp.deduct.call]
21092 A can be another pointer or pointer to member type that can
21093 be converted to the deduced A via a qualification
21094 conversion (_conv.qual_).
21096 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21097 This will allow for additional cv-qualification of the
21098 pointed-to types if appropriate. */
21100 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21101 /* The derived-to-base conversion only persists through one
21102 level of pointers. */
21103 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21105 return unify (tparms, targs, TREE_TYPE (parm),
21106 TREE_TYPE (arg), strict, explain_p);
21109 case REFERENCE_TYPE:
21110 if (TREE_CODE (arg) != REFERENCE_TYPE)
21111 return unify_type_mismatch (explain_p, parm, arg);
21112 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21113 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21115 case ARRAY_TYPE:
21116 if (TREE_CODE (arg) != ARRAY_TYPE)
21117 return unify_type_mismatch (explain_p, parm, arg);
21118 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21119 != (TYPE_DOMAIN (arg) == NULL_TREE))
21120 return unify_type_mismatch (explain_p, parm, arg);
21121 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21122 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21123 if (TYPE_DOMAIN (parm) != NULL_TREE)
21124 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21125 TYPE_DOMAIN (arg), explain_p);
21126 return unify_success (explain_p);
21128 case REAL_TYPE:
21129 case COMPLEX_TYPE:
21130 case VECTOR_TYPE:
21131 case INTEGER_TYPE:
21132 case BOOLEAN_TYPE:
21133 case ENUMERAL_TYPE:
21134 case VOID_TYPE:
21135 case NULLPTR_TYPE:
21136 if (TREE_CODE (arg) != TREE_CODE (parm))
21137 return unify_type_mismatch (explain_p, parm, arg);
21139 /* We have already checked cv-qualification at the top of the
21140 function. */
21141 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21142 return unify_type_mismatch (explain_p, parm, arg);
21144 /* As far as unification is concerned, this wins. Later checks
21145 will invalidate it if necessary. */
21146 return unify_success (explain_p);
21148 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21149 /* Type INTEGER_CST can come from ordinary constant template args. */
21150 case INTEGER_CST:
21151 while (CONVERT_EXPR_P (arg))
21152 arg = TREE_OPERAND (arg, 0);
21154 if (TREE_CODE (arg) != INTEGER_CST)
21155 return unify_template_argument_mismatch (explain_p, parm, arg);
21156 return (tree_int_cst_equal (parm, arg)
21157 ? unify_success (explain_p)
21158 : unify_template_argument_mismatch (explain_p, parm, arg));
21160 case TREE_VEC:
21162 int i, len, argslen;
21163 int parm_variadic_p = 0;
21165 if (TREE_CODE (arg) != TREE_VEC)
21166 return unify_template_argument_mismatch (explain_p, parm, arg);
21168 len = TREE_VEC_LENGTH (parm);
21169 argslen = TREE_VEC_LENGTH (arg);
21171 /* Check for pack expansions in the parameters. */
21172 for (i = 0; i < len; ++i)
21174 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21176 if (i == len - 1)
21177 /* We can unify against something with a trailing
21178 parameter pack. */
21179 parm_variadic_p = 1;
21180 else
21181 /* [temp.deduct.type]/9: If the template argument list of
21182 P contains a pack expansion that is not the last
21183 template argument, the entire template argument list
21184 is a non-deduced context. */
21185 return unify_success (explain_p);
21189 /* If we don't have enough arguments to satisfy the parameters
21190 (not counting the pack expression at the end), or we have
21191 too many arguments for a parameter list that doesn't end in
21192 a pack expression, we can't unify. */
21193 if (parm_variadic_p
21194 ? argslen < len - parm_variadic_p
21195 : argslen != len)
21196 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21198 /* Unify all of the parameters that precede the (optional)
21199 pack expression. */
21200 for (i = 0; i < len - parm_variadic_p; ++i)
21202 RECUR_AND_CHECK_FAILURE (tparms, targs,
21203 TREE_VEC_ELT (parm, i),
21204 TREE_VEC_ELT (arg, i),
21205 UNIFY_ALLOW_NONE, explain_p);
21207 if (parm_variadic_p)
21208 return unify_pack_expansion (tparms, targs, parm, arg,
21209 DEDUCE_EXACT,
21210 /*subr=*/true, explain_p);
21211 return unify_success (explain_p);
21214 case RECORD_TYPE:
21215 case UNION_TYPE:
21216 if (TREE_CODE (arg) != TREE_CODE (parm))
21217 return unify_type_mismatch (explain_p, parm, arg);
21219 if (TYPE_PTRMEMFUNC_P (parm))
21221 if (!TYPE_PTRMEMFUNC_P (arg))
21222 return unify_type_mismatch (explain_p, parm, arg);
21224 return unify (tparms, targs,
21225 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21226 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21227 strict, explain_p);
21229 else if (TYPE_PTRMEMFUNC_P (arg))
21230 return unify_type_mismatch (explain_p, parm, arg);
21232 if (CLASSTYPE_TEMPLATE_INFO (parm))
21234 tree t = NULL_TREE;
21236 if (strict_in & UNIFY_ALLOW_DERIVED)
21238 /* First, we try to unify the PARM and ARG directly. */
21239 t = try_class_unification (tparms, targs,
21240 parm, arg, explain_p);
21242 if (!t)
21244 /* Fallback to the special case allowed in
21245 [temp.deduct.call]:
21247 If P is a class, and P has the form
21248 template-id, then A can be a derived class of
21249 the deduced A. Likewise, if P is a pointer to
21250 a class of the form template-id, A can be a
21251 pointer to a derived class pointed to by the
21252 deduced A. */
21253 enum template_base_result r;
21254 r = get_template_base (tparms, targs, parm, arg,
21255 explain_p, &t);
21257 if (!t)
21259 /* Don't give the derived diagnostic if we're
21260 already dealing with the same template. */
21261 bool same_template
21262 = (CLASSTYPE_TEMPLATE_INFO (arg)
21263 && (CLASSTYPE_TI_TEMPLATE (parm)
21264 == CLASSTYPE_TI_TEMPLATE (arg)));
21265 return unify_no_common_base (explain_p && !same_template,
21266 r, parm, arg);
21270 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21271 && (CLASSTYPE_TI_TEMPLATE (parm)
21272 == CLASSTYPE_TI_TEMPLATE (arg)))
21273 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21274 Then, we should unify `int' and `U'. */
21275 t = arg;
21276 else
21277 /* There's no chance of unification succeeding. */
21278 return unify_type_mismatch (explain_p, parm, arg);
21280 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21281 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21283 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21284 return unify_type_mismatch (explain_p, parm, arg);
21285 return unify_success (explain_p);
21287 case METHOD_TYPE:
21288 case FUNCTION_TYPE:
21290 unsigned int nargs;
21291 tree *args;
21292 tree a;
21293 unsigned int i;
21295 if (TREE_CODE (arg) != TREE_CODE (parm))
21296 return unify_type_mismatch (explain_p, parm, arg);
21298 /* CV qualifications for methods can never be deduced, they must
21299 match exactly. We need to check them explicitly here,
21300 because type_unification_real treats them as any other
21301 cv-qualified parameter. */
21302 if (TREE_CODE (parm) == METHOD_TYPE
21303 && (!check_cv_quals_for_unify
21304 (UNIFY_ALLOW_NONE,
21305 class_of_this_parm (arg),
21306 class_of_this_parm (parm))))
21307 return unify_cv_qual_mismatch (explain_p, parm, arg);
21308 if (TREE_CODE (arg) == FUNCTION_TYPE
21309 && type_memfn_quals (parm) != type_memfn_quals (arg))
21310 return unify_cv_qual_mismatch (explain_p, parm, arg);
21311 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21312 return unify_type_mismatch (explain_p, parm, arg);
21314 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21315 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21317 nargs = list_length (TYPE_ARG_TYPES (arg));
21318 args = XALLOCAVEC (tree, nargs);
21319 for (a = TYPE_ARG_TYPES (arg), i = 0;
21320 a != NULL_TREE && a != void_list_node;
21321 a = TREE_CHAIN (a), ++i)
21322 args[i] = TREE_VALUE (a);
21323 nargs = i;
21325 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21326 args, nargs, 1, DEDUCE_EXACT,
21327 LOOKUP_NORMAL, NULL, explain_p))
21328 return 1;
21330 if (flag_noexcept_type)
21332 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21333 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21334 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21335 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21336 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21337 && uses_template_parms (TREE_PURPOSE (pspec)))
21338 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21339 TREE_PURPOSE (aspec),
21340 UNIFY_ALLOW_NONE, explain_p);
21341 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21342 return unify_type_mismatch (explain_p, parm, arg);
21345 return 0;
21348 case OFFSET_TYPE:
21349 /* Unify a pointer to member with a pointer to member function, which
21350 deduces the type of the member as a function type. */
21351 if (TYPE_PTRMEMFUNC_P (arg))
21353 /* Check top-level cv qualifiers */
21354 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21355 return unify_cv_qual_mismatch (explain_p, parm, arg);
21357 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21358 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21359 UNIFY_ALLOW_NONE, explain_p);
21361 /* Determine the type of the function we are unifying against. */
21362 tree fntype = static_fn_type (arg);
21364 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21367 if (TREE_CODE (arg) != OFFSET_TYPE)
21368 return unify_type_mismatch (explain_p, parm, arg);
21369 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21370 TYPE_OFFSET_BASETYPE (arg),
21371 UNIFY_ALLOW_NONE, explain_p);
21372 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21373 strict, explain_p);
21375 case CONST_DECL:
21376 if (DECL_TEMPLATE_PARM_P (parm))
21377 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21378 if (arg != scalar_constant_value (parm))
21379 return unify_template_argument_mismatch (explain_p, parm, arg);
21380 return unify_success (explain_p);
21382 case FIELD_DECL:
21383 case TEMPLATE_DECL:
21384 /* Matched cases are handled by the ARG == PARM test above. */
21385 return unify_template_argument_mismatch (explain_p, parm, arg);
21387 case VAR_DECL:
21388 /* We might get a variable as a non-type template argument in parm if the
21389 corresponding parameter is type-dependent. Make any necessary
21390 adjustments based on whether arg is a reference. */
21391 if (CONSTANT_CLASS_P (arg))
21392 parm = fold_non_dependent_expr (parm);
21393 else if (REFERENCE_REF_P (arg))
21395 tree sub = TREE_OPERAND (arg, 0);
21396 STRIP_NOPS (sub);
21397 if (TREE_CODE (sub) == ADDR_EXPR)
21398 arg = TREE_OPERAND (sub, 0);
21400 /* Now use the normal expression code to check whether they match. */
21401 goto expr;
21403 case TYPE_ARGUMENT_PACK:
21404 case NONTYPE_ARGUMENT_PACK:
21405 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21406 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21408 case TYPEOF_TYPE:
21409 case DECLTYPE_TYPE:
21410 case UNDERLYING_TYPE:
21411 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21412 or UNDERLYING_TYPE nodes. */
21413 return unify_success (explain_p);
21415 case ERROR_MARK:
21416 /* Unification fails if we hit an error node. */
21417 return unify_invalid (explain_p);
21419 case INDIRECT_REF:
21420 if (REFERENCE_REF_P (parm))
21422 bool pexp = PACK_EXPANSION_P (arg);
21423 if (pexp)
21424 arg = PACK_EXPANSION_PATTERN (arg);
21425 if (REFERENCE_REF_P (arg))
21426 arg = TREE_OPERAND (arg, 0);
21427 if (pexp)
21428 arg = make_pack_expansion (arg, complain);
21429 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21430 strict, explain_p);
21432 /* FALLTHRU */
21434 default:
21435 /* An unresolved overload is a nondeduced context. */
21436 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21437 return unify_success (explain_p);
21438 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21439 expr:
21440 /* We must be looking at an expression. This can happen with
21441 something like:
21443 template <int I>
21444 void foo(S<I>, S<I + 2>);
21446 This is a "nondeduced context":
21448 [deduct.type]
21450 The nondeduced contexts are:
21452 --A type that is a template-id in which one or more of
21453 the template-arguments is an expression that references
21454 a template-parameter.
21456 In these cases, we assume deduction succeeded, but don't
21457 actually infer any unifications. */
21459 if (!uses_template_parms (parm)
21460 && !template_args_equal (parm, arg))
21461 return unify_expression_unequal (explain_p, parm, arg);
21462 else
21463 return unify_success (explain_p);
21466 #undef RECUR_AND_CHECK_FAILURE
21468 /* Note that DECL can be defined in this translation unit, if
21469 required. */
21471 static void
21472 mark_definable (tree decl)
21474 tree clone;
21475 DECL_NOT_REALLY_EXTERN (decl) = 1;
21476 FOR_EACH_CLONE (clone, decl)
21477 DECL_NOT_REALLY_EXTERN (clone) = 1;
21480 /* Called if RESULT is explicitly instantiated, or is a member of an
21481 explicitly instantiated class. */
21483 void
21484 mark_decl_instantiated (tree result, int extern_p)
21486 SET_DECL_EXPLICIT_INSTANTIATION (result);
21488 /* If this entity has already been written out, it's too late to
21489 make any modifications. */
21490 if (TREE_ASM_WRITTEN (result))
21491 return;
21493 /* For anonymous namespace we don't need to do anything. */
21494 if (decl_anon_ns_mem_p (result))
21496 gcc_assert (!TREE_PUBLIC (result));
21497 return;
21500 if (TREE_CODE (result) != FUNCTION_DECL)
21501 /* The TREE_PUBLIC flag for function declarations will have been
21502 set correctly by tsubst. */
21503 TREE_PUBLIC (result) = 1;
21505 /* This might have been set by an earlier implicit instantiation. */
21506 DECL_COMDAT (result) = 0;
21508 if (extern_p)
21509 DECL_NOT_REALLY_EXTERN (result) = 0;
21510 else
21512 mark_definable (result);
21513 mark_needed (result);
21514 /* Always make artificials weak. */
21515 if (DECL_ARTIFICIAL (result) && flag_weak)
21516 comdat_linkage (result);
21517 /* For WIN32 we also want to put explicit instantiations in
21518 linkonce sections. */
21519 else if (TREE_PUBLIC (result))
21520 maybe_make_one_only (result);
21523 /* If EXTERN_P, then this function will not be emitted -- unless
21524 followed by an explicit instantiation, at which point its linkage
21525 will be adjusted. If !EXTERN_P, then this function will be
21526 emitted here. In neither circumstance do we want
21527 import_export_decl to adjust the linkage. */
21528 DECL_INTERFACE_KNOWN (result) = 1;
21531 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21532 important template arguments. If any are missing, we check whether
21533 they're important by using error_mark_node for substituting into any
21534 args that were used for partial ordering (the ones between ARGS and END)
21535 and seeing if it bubbles up. */
21537 static bool
21538 check_undeduced_parms (tree targs, tree args, tree end)
21540 bool found = false;
21541 int i;
21542 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21543 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21545 found = true;
21546 TREE_VEC_ELT (targs, i) = error_mark_node;
21548 if (found)
21550 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21551 if (substed == error_mark_node)
21552 return true;
21554 return false;
21557 /* Given two function templates PAT1 and PAT2, return:
21559 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21560 -1 if PAT2 is more specialized than PAT1.
21561 0 if neither is more specialized.
21563 LEN indicates the number of parameters we should consider
21564 (defaulted parameters should not be considered).
21566 The 1998 std underspecified function template partial ordering, and
21567 DR214 addresses the issue. We take pairs of arguments, one from
21568 each of the templates, and deduce them against each other. One of
21569 the templates will be more specialized if all the *other*
21570 template's arguments deduce against its arguments and at least one
21571 of its arguments *does* *not* deduce against the other template's
21572 corresponding argument. Deduction is done as for class templates.
21573 The arguments used in deduction have reference and top level cv
21574 qualifiers removed. Iff both arguments were originally reference
21575 types *and* deduction succeeds in both directions, an lvalue reference
21576 wins against an rvalue reference and otherwise the template
21577 with the more cv-qualified argument wins for that pairing (if
21578 neither is more cv-qualified, they both are equal). Unlike regular
21579 deduction, after all the arguments have been deduced in this way,
21580 we do *not* verify the deduced template argument values can be
21581 substituted into non-deduced contexts.
21583 The logic can be a bit confusing here, because we look at deduce1 and
21584 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21585 can find template arguments for pat1 to make arg1 look like arg2, that
21586 means that arg2 is at least as specialized as arg1. */
21589 more_specialized_fn (tree pat1, tree pat2, int len)
21591 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21592 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21593 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21594 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21595 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21596 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21597 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21598 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21599 tree origs1, origs2;
21600 bool lose1 = false;
21601 bool lose2 = false;
21603 /* Remove the this parameter from non-static member functions. If
21604 one is a non-static member function and the other is not a static
21605 member function, remove the first parameter from that function
21606 also. This situation occurs for operator functions where we
21607 locate both a member function (with this pointer) and non-member
21608 operator (with explicit first operand). */
21609 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21611 len--; /* LEN is the number of significant arguments for DECL1 */
21612 args1 = TREE_CHAIN (args1);
21613 if (!DECL_STATIC_FUNCTION_P (decl2))
21614 args2 = TREE_CHAIN (args2);
21616 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21618 args2 = TREE_CHAIN (args2);
21619 if (!DECL_STATIC_FUNCTION_P (decl1))
21621 len--;
21622 args1 = TREE_CHAIN (args1);
21626 /* If only one is a conversion operator, they are unordered. */
21627 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21628 return 0;
21630 /* Consider the return type for a conversion function */
21631 if (DECL_CONV_FN_P (decl1))
21633 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21634 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21635 len++;
21638 processing_template_decl++;
21640 origs1 = args1;
21641 origs2 = args2;
21643 while (len--
21644 /* Stop when an ellipsis is seen. */
21645 && args1 != NULL_TREE && args2 != NULL_TREE)
21647 tree arg1 = TREE_VALUE (args1);
21648 tree arg2 = TREE_VALUE (args2);
21649 int deduce1, deduce2;
21650 int quals1 = -1;
21651 int quals2 = -1;
21652 int ref1 = 0;
21653 int ref2 = 0;
21655 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21656 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21658 /* When both arguments are pack expansions, we need only
21659 unify the patterns themselves. */
21660 arg1 = PACK_EXPANSION_PATTERN (arg1);
21661 arg2 = PACK_EXPANSION_PATTERN (arg2);
21663 /* This is the last comparison we need to do. */
21664 len = 0;
21667 /* DR 1847: If a particular P contains no template-parameters that
21668 participate in template argument deduction, that P is not used to
21669 determine the ordering. */
21670 if (!uses_deducible_template_parms (arg1)
21671 && !uses_deducible_template_parms (arg2))
21672 goto next;
21674 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21676 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21677 arg1 = TREE_TYPE (arg1);
21678 quals1 = cp_type_quals (arg1);
21681 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21683 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21684 arg2 = TREE_TYPE (arg2);
21685 quals2 = cp_type_quals (arg2);
21688 arg1 = TYPE_MAIN_VARIANT (arg1);
21689 arg2 = TYPE_MAIN_VARIANT (arg2);
21691 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21693 int i, len2 = remaining_arguments (args2);
21694 tree parmvec = make_tree_vec (1);
21695 tree argvec = make_tree_vec (len2);
21696 tree ta = args2;
21698 /* Setup the parameter vector, which contains only ARG1. */
21699 TREE_VEC_ELT (parmvec, 0) = arg1;
21701 /* Setup the argument vector, which contains the remaining
21702 arguments. */
21703 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21704 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21706 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21707 argvec, DEDUCE_EXACT,
21708 /*subr=*/true, /*explain_p=*/false)
21709 == 0);
21711 /* We cannot deduce in the other direction, because ARG1 is
21712 a pack expansion but ARG2 is not. */
21713 deduce2 = 0;
21715 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21717 int i, len1 = remaining_arguments (args1);
21718 tree parmvec = make_tree_vec (1);
21719 tree argvec = make_tree_vec (len1);
21720 tree ta = args1;
21722 /* Setup the parameter vector, which contains only ARG1. */
21723 TREE_VEC_ELT (parmvec, 0) = arg2;
21725 /* Setup the argument vector, which contains the remaining
21726 arguments. */
21727 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21728 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21730 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21731 argvec, DEDUCE_EXACT,
21732 /*subr=*/true, /*explain_p=*/false)
21733 == 0);
21735 /* We cannot deduce in the other direction, because ARG2 is
21736 a pack expansion but ARG1 is not.*/
21737 deduce1 = 0;
21740 else
21742 /* The normal case, where neither argument is a pack
21743 expansion. */
21744 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21745 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21746 == 0);
21747 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21748 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21749 == 0);
21752 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21753 arg2, then arg2 is not as specialized as arg1. */
21754 if (!deduce1)
21755 lose2 = true;
21756 if (!deduce2)
21757 lose1 = true;
21759 /* "If, for a given type, deduction succeeds in both directions
21760 (i.e., the types are identical after the transformations above)
21761 and both P and A were reference types (before being replaced with
21762 the type referred to above):
21763 - if the type from the argument template was an lvalue reference and
21764 the type from the parameter template was not, the argument type is
21765 considered to be more specialized than the other; otherwise,
21766 - if the type from the argument template is more cv-qualified
21767 than the type from the parameter template (as described above),
21768 the argument type is considered to be more specialized than the other;
21769 otherwise,
21770 - neither type is more specialized than the other." */
21772 if (deduce1 && deduce2)
21774 if (ref1 && ref2 && ref1 != ref2)
21776 if (ref1 > ref2)
21777 lose1 = true;
21778 else
21779 lose2 = true;
21781 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21783 if ((quals1 & quals2) == quals2)
21784 lose2 = true;
21785 if ((quals1 & quals2) == quals1)
21786 lose1 = true;
21790 if (lose1 && lose2)
21791 /* We've failed to deduce something in either direction.
21792 These must be unordered. */
21793 break;
21795 next:
21797 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21798 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21799 /* We have already processed all of the arguments in our
21800 handing of the pack expansion type. */
21801 len = 0;
21803 args1 = TREE_CHAIN (args1);
21804 args2 = TREE_CHAIN (args2);
21807 /* "In most cases, all template parameters must have values in order for
21808 deduction to succeed, but for partial ordering purposes a template
21809 parameter may remain without a value provided it is not used in the
21810 types being used for partial ordering."
21812 Thus, if we are missing any of the targs1 we need to substitute into
21813 origs1, then pat2 is not as specialized as pat1. This can happen when
21814 there is a nondeduced context. */
21815 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21816 lose2 = true;
21817 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21818 lose1 = true;
21820 processing_template_decl--;
21822 /* If both deductions succeed, the partial ordering selects the more
21823 constrained template. */
21824 if (!lose1 && !lose2)
21826 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21827 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21828 lose1 = !subsumes_constraints (c1, c2);
21829 lose2 = !subsumes_constraints (c2, c1);
21832 /* All things being equal, if the next argument is a pack expansion
21833 for one function but not for the other, prefer the
21834 non-variadic function. FIXME this is bogus; see c++/41958. */
21835 if (lose1 == lose2
21836 && args1 && TREE_VALUE (args1)
21837 && args2 && TREE_VALUE (args2))
21839 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21840 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21843 if (lose1 == lose2)
21844 return 0;
21845 else if (!lose1)
21846 return 1;
21847 else
21848 return -1;
21851 /* Determine which of two partial specializations of TMPL is more
21852 specialized.
21854 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21855 to the first partial specialization. The TREE_PURPOSE is the
21856 innermost set of template parameters for the partial
21857 specialization. PAT2 is similar, but for the second template.
21859 Return 1 if the first partial specialization is more specialized;
21860 -1 if the second is more specialized; 0 if neither is more
21861 specialized.
21863 See [temp.class.order] for information about determining which of
21864 two templates is more specialized. */
21866 static int
21867 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21869 tree targs;
21870 int winner = 0;
21871 bool any_deductions = false;
21873 tree tmpl1 = TREE_VALUE (pat1);
21874 tree tmpl2 = TREE_VALUE (pat2);
21875 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21876 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21878 /* Just like what happens for functions, if we are ordering between
21879 different template specializations, we may encounter dependent
21880 types in the arguments, and we need our dependency check functions
21881 to behave correctly. */
21882 ++processing_template_decl;
21883 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21884 if (targs)
21886 --winner;
21887 any_deductions = true;
21890 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21891 if (targs)
21893 ++winner;
21894 any_deductions = true;
21896 --processing_template_decl;
21898 /* If both deductions succeed, the partial ordering selects the more
21899 constrained template. */
21900 if (!winner && any_deductions)
21901 return more_constrained (tmpl1, tmpl2);
21903 /* In the case of a tie where at least one of the templates
21904 has a parameter pack at the end, the template with the most
21905 non-packed parameters wins. */
21906 if (winner == 0
21907 && any_deductions
21908 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21909 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21911 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21912 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21913 int len1 = TREE_VEC_LENGTH (args1);
21914 int len2 = TREE_VEC_LENGTH (args2);
21916 /* We don't count the pack expansion at the end. */
21917 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21918 --len1;
21919 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21920 --len2;
21922 if (len1 > len2)
21923 return 1;
21924 else if (len1 < len2)
21925 return -1;
21928 return winner;
21931 /* Return the template arguments that will produce the function signature
21932 DECL from the function template FN, with the explicit template
21933 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21934 also match. Return NULL_TREE if no satisfactory arguments could be
21935 found. */
21937 static tree
21938 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21940 int ntparms = DECL_NTPARMS (fn);
21941 tree targs = make_tree_vec (ntparms);
21942 tree decl_type = TREE_TYPE (decl);
21943 tree decl_arg_types;
21944 tree *args;
21945 unsigned int nargs, ix;
21946 tree arg;
21948 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21950 /* Never do unification on the 'this' parameter. */
21951 decl_arg_types = skip_artificial_parms_for (decl,
21952 TYPE_ARG_TYPES (decl_type));
21954 nargs = list_length (decl_arg_types);
21955 args = XALLOCAVEC (tree, nargs);
21956 for (arg = decl_arg_types, ix = 0;
21957 arg != NULL_TREE && arg != void_list_node;
21958 arg = TREE_CHAIN (arg), ++ix)
21959 args[ix] = TREE_VALUE (arg);
21961 if (fn_type_unification (fn, explicit_args, targs,
21962 args, ix,
21963 (check_rettype || DECL_CONV_FN_P (fn)
21964 ? TREE_TYPE (decl_type) : NULL_TREE),
21965 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21966 /*decltype*/false)
21967 == error_mark_node)
21968 return NULL_TREE;
21970 return targs;
21973 /* Return the innermost template arguments that, when applied to a partial
21974 specialization SPEC_TMPL of TMPL, yield the ARGS.
21976 For example, suppose we have:
21978 template <class T, class U> struct S {};
21979 template <class T> struct S<T*, int> {};
21981 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21982 partial specialization and the ARGS will be {double*, int}. The resulting
21983 vector will be {double}, indicating that `T' is bound to `double'. */
21985 static tree
21986 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21988 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21989 tree spec_args
21990 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21991 int i, ntparms = TREE_VEC_LENGTH (tparms);
21992 tree deduced_args;
21993 tree innermost_deduced_args;
21995 innermost_deduced_args = make_tree_vec (ntparms);
21996 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21998 deduced_args = copy_node (args);
21999 SET_TMPL_ARGS_LEVEL (deduced_args,
22000 TMPL_ARGS_DEPTH (deduced_args),
22001 innermost_deduced_args);
22003 else
22004 deduced_args = innermost_deduced_args;
22006 bool tried_array_deduction = (cxx_dialect < cxx17);
22007 again:
22008 if (unify (tparms, deduced_args,
22009 INNERMOST_TEMPLATE_ARGS (spec_args),
22010 INNERMOST_TEMPLATE_ARGS (args),
22011 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22012 return NULL_TREE;
22014 for (i = 0; i < ntparms; ++i)
22015 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22017 if (!tried_array_deduction)
22019 try_array_deduction (tparms, innermost_deduced_args,
22020 INNERMOST_TEMPLATE_ARGS (spec_args));
22021 tried_array_deduction = true;
22022 if (TREE_VEC_ELT (innermost_deduced_args, i))
22023 goto again;
22025 return NULL_TREE;
22028 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22029 if (!push_tinst_level (tinst))
22031 excessive_deduction_depth = true;
22032 return NULL_TREE;
22035 /* Verify that nondeduced template arguments agree with the type
22036 obtained from argument deduction.
22038 For example:
22040 struct A { typedef int X; };
22041 template <class T, class U> struct C {};
22042 template <class T> struct C<T, typename T::X> {};
22044 Then with the instantiation `C<A, int>', we can deduce that
22045 `T' is `A' but unify () does not check whether `typename T::X'
22046 is `int'. */
22047 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22049 if (spec_args != error_mark_node)
22050 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22051 INNERMOST_TEMPLATE_ARGS (spec_args),
22052 tmpl, tf_none, false, false);
22054 pop_tinst_level ();
22056 if (spec_args == error_mark_node
22057 /* We only need to check the innermost arguments; the other
22058 arguments will always agree. */
22059 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22060 INNERMOST_TEMPLATE_ARGS (args)))
22061 return NULL_TREE;
22063 /* Now that we have bindings for all of the template arguments,
22064 ensure that the arguments deduced for the template template
22065 parameters have compatible template parameter lists. See the use
22066 of template_template_parm_bindings_ok_p in fn_type_unification
22067 for more information. */
22068 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22069 return NULL_TREE;
22071 return deduced_args;
22074 // Compare two function templates T1 and T2 by deducing bindings
22075 // from one against the other. If both deductions succeed, compare
22076 // constraints to see which is more constrained.
22077 static int
22078 more_specialized_inst (tree t1, tree t2)
22080 int fate = 0;
22081 int count = 0;
22083 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22085 --fate;
22086 ++count;
22089 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22091 ++fate;
22092 ++count;
22095 // If both deductions succeed, then one may be more constrained.
22096 if (count == 2 && fate == 0)
22097 fate = more_constrained (t1, t2);
22099 return fate;
22102 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22103 Return the TREE_LIST node with the most specialized template, if
22104 any. If there is no most specialized template, the error_mark_node
22105 is returned.
22107 Note that this function does not look at, or modify, the
22108 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22109 returned is one of the elements of INSTANTIATIONS, callers may
22110 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22111 and retrieve it from the value returned. */
22113 tree
22114 most_specialized_instantiation (tree templates)
22116 tree fn, champ;
22118 ++processing_template_decl;
22120 champ = templates;
22121 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22123 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22124 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22125 if (fate == -1)
22126 champ = fn;
22127 else if (!fate)
22129 /* Equally specialized, move to next function. If there
22130 is no next function, nothing's most specialized. */
22131 fn = TREE_CHAIN (fn);
22132 champ = fn;
22133 if (!fn)
22134 break;
22138 if (champ)
22139 /* Now verify that champ is better than everything earlier in the
22140 instantiation list. */
22141 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22142 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22144 champ = NULL_TREE;
22145 break;
22149 processing_template_decl--;
22151 if (!champ)
22152 return error_mark_node;
22154 return champ;
22157 /* If DECL is a specialization of some template, return the most
22158 general such template. Otherwise, returns NULL_TREE.
22160 For example, given:
22162 template <class T> struct S { template <class U> void f(U); };
22164 if TMPL is `template <class U> void S<int>::f(U)' this will return
22165 the full template. This function will not trace past partial
22166 specializations, however. For example, given in addition:
22168 template <class T> struct S<T*> { template <class U> void f(U); };
22170 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22171 `template <class T> template <class U> S<T*>::f(U)'. */
22173 tree
22174 most_general_template (tree decl)
22176 if (TREE_CODE (decl) != TEMPLATE_DECL)
22178 if (tree tinfo = get_template_info (decl))
22179 decl = TI_TEMPLATE (tinfo);
22180 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22181 template friend, or a FIELD_DECL for a capture pack. */
22182 if (TREE_CODE (decl) != TEMPLATE_DECL)
22183 return NULL_TREE;
22186 /* Look for more and more general templates. */
22187 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22189 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22190 (See cp-tree.h for details.) */
22191 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22192 break;
22194 if (CLASS_TYPE_P (TREE_TYPE (decl))
22195 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22196 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22197 break;
22199 /* Stop if we run into an explicitly specialized class template. */
22200 if (!DECL_NAMESPACE_SCOPE_P (decl)
22201 && DECL_CONTEXT (decl)
22202 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22203 break;
22205 decl = DECL_TI_TEMPLATE (decl);
22208 return decl;
22211 /* Return the most specialized of the template partial specializations
22212 which can produce TARGET, a specialization of some class or variable
22213 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22214 a TEMPLATE_DECL node corresponding to the partial specialization, while
22215 the TREE_PURPOSE is the set of template arguments that must be
22216 substituted into the template pattern in order to generate TARGET.
22218 If the choice of partial specialization is ambiguous, a diagnostic
22219 is issued, and the error_mark_node is returned. If there are no
22220 partial specializations matching TARGET, then NULL_TREE is
22221 returned, indicating that the primary template should be used. */
22223 static tree
22224 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22226 tree list = NULL_TREE;
22227 tree t;
22228 tree champ;
22229 int fate;
22230 bool ambiguous_p;
22231 tree outer_args = NULL_TREE;
22232 tree tmpl, args;
22234 if (TYPE_P (target))
22236 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22237 tmpl = TI_TEMPLATE (tinfo);
22238 args = TI_ARGS (tinfo);
22240 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22242 tmpl = TREE_OPERAND (target, 0);
22243 args = TREE_OPERAND (target, 1);
22245 else if (VAR_P (target))
22247 tree tinfo = DECL_TEMPLATE_INFO (target);
22248 tmpl = TI_TEMPLATE (tinfo);
22249 args = TI_ARGS (tinfo);
22251 else
22252 gcc_unreachable ();
22254 tree main_tmpl = most_general_template (tmpl);
22256 /* For determining which partial specialization to use, only the
22257 innermost args are interesting. */
22258 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22260 outer_args = strip_innermost_template_args (args, 1);
22261 args = INNERMOST_TEMPLATE_ARGS (args);
22264 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22266 tree spec_args;
22267 tree spec_tmpl = TREE_VALUE (t);
22269 if (outer_args)
22271 /* Substitute in the template args from the enclosing class. */
22272 ++processing_template_decl;
22273 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22274 --processing_template_decl;
22277 if (spec_tmpl == error_mark_node)
22278 return error_mark_node;
22280 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22281 if (spec_args)
22283 if (outer_args)
22284 spec_args = add_to_template_args (outer_args, spec_args);
22286 /* Keep the candidate only if the constraints are satisfied,
22287 or if we're not compiling with concepts. */
22288 if (!flag_concepts
22289 || constraints_satisfied_p (spec_tmpl, spec_args))
22291 list = tree_cons (spec_args, TREE_VALUE (t), list);
22292 TREE_TYPE (list) = TREE_TYPE (t);
22297 if (! list)
22298 return NULL_TREE;
22300 ambiguous_p = false;
22301 t = list;
22302 champ = t;
22303 t = TREE_CHAIN (t);
22304 for (; t; t = TREE_CHAIN (t))
22306 fate = more_specialized_partial_spec (tmpl, champ, t);
22307 if (fate == 1)
22309 else
22311 if (fate == 0)
22313 t = TREE_CHAIN (t);
22314 if (! t)
22316 ambiguous_p = true;
22317 break;
22320 champ = t;
22324 if (!ambiguous_p)
22325 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22327 fate = more_specialized_partial_spec (tmpl, champ, t);
22328 if (fate != 1)
22330 ambiguous_p = true;
22331 break;
22335 if (ambiguous_p)
22337 const char *str;
22338 char *spaces = NULL;
22339 if (!(complain & tf_error))
22340 return error_mark_node;
22341 if (TYPE_P (target))
22342 error ("ambiguous template instantiation for %q#T", target);
22343 else
22344 error ("ambiguous template instantiation for %q#D", target);
22345 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22346 for (t = list; t; t = TREE_CHAIN (t))
22348 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22349 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22350 "%s %#qS", spaces ? spaces : str, subst);
22351 spaces = spaces ? spaces : get_spaces (str);
22353 free (spaces);
22354 return error_mark_node;
22357 return champ;
22360 /* Explicitly instantiate DECL. */
22362 void
22363 do_decl_instantiation (tree decl, tree storage)
22365 tree result = NULL_TREE;
22366 int extern_p = 0;
22368 if (!decl || decl == error_mark_node)
22369 /* An error occurred, for which grokdeclarator has already issued
22370 an appropriate message. */
22371 return;
22372 else if (! DECL_LANG_SPECIFIC (decl))
22374 error ("explicit instantiation of non-template %q#D", decl);
22375 return;
22378 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22379 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22381 if (VAR_P (decl) && !var_templ)
22383 /* There is an asymmetry here in the way VAR_DECLs and
22384 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22385 the latter, the DECL we get back will be marked as a
22386 template instantiation, and the appropriate
22387 DECL_TEMPLATE_INFO will be set up. This does not happen for
22388 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22389 should handle VAR_DECLs as it currently handles
22390 FUNCTION_DECLs. */
22391 if (!DECL_CLASS_SCOPE_P (decl))
22393 error ("%qD is not a static data member of a class template", decl);
22394 return;
22396 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22397 if (!result || !VAR_P (result))
22399 error ("no matching template for %qD found", decl);
22400 return;
22402 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22404 error ("type %qT for explicit instantiation %qD does not match "
22405 "declared type %qT", TREE_TYPE (result), decl,
22406 TREE_TYPE (decl));
22407 return;
22410 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22412 error ("explicit instantiation of %q#D", decl);
22413 return;
22415 else
22416 result = decl;
22418 /* Check for various error cases. Note that if the explicit
22419 instantiation is valid the RESULT will currently be marked as an
22420 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22421 until we get here. */
22423 if (DECL_TEMPLATE_SPECIALIZATION (result))
22425 /* DR 259 [temp.spec].
22427 Both an explicit instantiation and a declaration of an explicit
22428 specialization shall not appear in a program unless the explicit
22429 instantiation follows a declaration of the explicit specialization.
22431 For a given set of template parameters, if an explicit
22432 instantiation of a template appears after a declaration of an
22433 explicit specialization for that template, the explicit
22434 instantiation has no effect. */
22435 return;
22437 else if (DECL_EXPLICIT_INSTANTIATION (result))
22439 /* [temp.spec]
22441 No program shall explicitly instantiate any template more
22442 than once.
22444 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22445 the first instantiation was `extern' and the second is not,
22446 and EXTERN_P for the opposite case. */
22447 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22448 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22449 /* If an "extern" explicit instantiation follows an ordinary
22450 explicit instantiation, the template is instantiated. */
22451 if (extern_p)
22452 return;
22454 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22456 error ("no matching template for %qD found", result);
22457 return;
22459 else if (!DECL_TEMPLATE_INFO (result))
22461 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22462 return;
22465 if (storage == NULL_TREE)
22467 else if (storage == ridpointers[(int) RID_EXTERN])
22469 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22470 pedwarn (input_location, OPT_Wpedantic,
22471 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22472 "instantiations");
22473 extern_p = 1;
22475 else
22476 error ("storage class %qD applied to template instantiation", storage);
22478 check_explicit_instantiation_namespace (result);
22479 mark_decl_instantiated (result, extern_p);
22480 if (! extern_p)
22481 instantiate_decl (result, /*defer_ok=*/true,
22482 /*expl_inst_class_mem_p=*/false);
22485 static void
22486 mark_class_instantiated (tree t, int extern_p)
22488 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22489 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22490 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22491 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22492 if (! extern_p)
22494 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22495 rest_of_type_compilation (t, 1);
22499 /* Called from do_type_instantiation through binding_table_foreach to
22500 do recursive instantiation for the type bound in ENTRY. */
22501 static void
22502 bt_instantiate_type_proc (binding_entry entry, void *data)
22504 tree storage = *(tree *) data;
22506 if (MAYBE_CLASS_TYPE_P (entry->type)
22507 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22508 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22511 /* Perform an explicit instantiation of template class T. STORAGE, if
22512 non-null, is the RID for extern, inline or static. COMPLAIN is
22513 nonzero if this is called from the parser, zero if called recursively,
22514 since the standard is unclear (as detailed below). */
22516 void
22517 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22519 int extern_p = 0;
22520 int nomem_p = 0;
22521 int static_p = 0;
22522 int previous_instantiation_extern_p = 0;
22524 if (TREE_CODE (t) == TYPE_DECL)
22525 t = TREE_TYPE (t);
22527 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22529 tree tmpl =
22530 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22531 if (tmpl)
22532 error ("explicit instantiation of non-class template %qD", tmpl);
22533 else
22534 error ("explicit instantiation of non-template type %qT", t);
22535 return;
22538 complete_type (t);
22540 if (!COMPLETE_TYPE_P (t))
22542 if (complain & tf_error)
22543 error ("explicit instantiation of %q#T before definition of template",
22545 return;
22548 if (storage != NULL_TREE)
22550 if (!in_system_header_at (input_location))
22552 if (storage == ridpointers[(int) RID_EXTERN])
22554 if (cxx_dialect == cxx98)
22555 pedwarn (input_location, OPT_Wpedantic,
22556 "ISO C++ 1998 forbids the use of %<extern%> on "
22557 "explicit instantiations");
22559 else
22560 pedwarn (input_location, OPT_Wpedantic,
22561 "ISO C++ forbids the use of %qE"
22562 " on explicit instantiations", storage);
22565 if (storage == ridpointers[(int) RID_INLINE])
22566 nomem_p = 1;
22567 else if (storage == ridpointers[(int) RID_EXTERN])
22568 extern_p = 1;
22569 else if (storage == ridpointers[(int) RID_STATIC])
22570 static_p = 1;
22571 else
22573 error ("storage class %qD applied to template instantiation",
22574 storage);
22575 extern_p = 0;
22579 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22581 /* DR 259 [temp.spec].
22583 Both an explicit instantiation and a declaration of an explicit
22584 specialization shall not appear in a program unless the explicit
22585 instantiation follows a declaration of the explicit specialization.
22587 For a given set of template parameters, if an explicit
22588 instantiation of a template appears after a declaration of an
22589 explicit specialization for that template, the explicit
22590 instantiation has no effect. */
22591 return;
22593 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22595 /* [temp.spec]
22597 No program shall explicitly instantiate any template more
22598 than once.
22600 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22601 instantiation was `extern'. If EXTERN_P then the second is.
22602 These cases are OK. */
22603 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22605 if (!previous_instantiation_extern_p && !extern_p
22606 && (complain & tf_error))
22607 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22609 /* If we've already instantiated the template, just return now. */
22610 if (!CLASSTYPE_INTERFACE_ONLY (t))
22611 return;
22614 check_explicit_instantiation_namespace (TYPE_NAME (t));
22615 mark_class_instantiated (t, extern_p);
22617 if (nomem_p)
22618 return;
22620 /* In contrast to implicit instantiation, where only the
22621 declarations, and not the definitions, of members are
22622 instantiated, we have here:
22624 [temp.explicit]
22626 The explicit instantiation of a class template specialization
22627 implies the instantiation of all of its members not
22628 previously explicitly specialized in the translation unit
22629 containing the explicit instantiation.
22631 Of course, we can't instantiate member template classes, since we
22632 don't have any arguments for them. Note that the standard is
22633 unclear on whether the instantiation of the members are
22634 *explicit* instantiations or not. However, the most natural
22635 interpretation is that it should be an explicit
22636 instantiation. */
22637 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22638 if ((VAR_P (fld)
22639 || (TREE_CODE (fld) == FUNCTION_DECL
22640 && !static_p
22641 && user_provided_p (fld)))
22642 && DECL_TEMPLATE_INSTANTIATION (fld))
22644 mark_decl_instantiated (fld, extern_p);
22645 if (! extern_p)
22646 instantiate_decl (fld, /*defer_ok=*/true,
22647 /*expl_inst_class_mem_p=*/true);
22650 if (CLASSTYPE_NESTED_UTDS (t))
22651 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22652 bt_instantiate_type_proc, &storage);
22655 /* Given a function DECL, which is a specialization of TMPL, modify
22656 DECL to be a re-instantiation of TMPL with the same template
22657 arguments. TMPL should be the template into which tsubst'ing
22658 should occur for DECL, not the most general template.
22660 One reason for doing this is a scenario like this:
22662 template <class T>
22663 void f(const T&, int i);
22665 void g() { f(3, 7); }
22667 template <class T>
22668 void f(const T& t, const int i) { }
22670 Note that when the template is first instantiated, with
22671 instantiate_template, the resulting DECL will have no name for the
22672 first parameter, and the wrong type for the second. So, when we go
22673 to instantiate the DECL, we regenerate it. */
22675 static void
22676 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22678 /* The arguments used to instantiate DECL, from the most general
22679 template. */
22680 tree code_pattern;
22682 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22684 /* Make sure that we can see identifiers, and compute access
22685 correctly. */
22686 push_access_scope (decl);
22688 if (TREE_CODE (decl) == FUNCTION_DECL)
22690 tree decl_parm;
22691 tree pattern_parm;
22692 tree specs;
22693 int args_depth;
22694 int parms_depth;
22696 args_depth = TMPL_ARGS_DEPTH (args);
22697 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22698 if (args_depth > parms_depth)
22699 args = get_innermost_template_args (args, parms_depth);
22701 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22702 args, tf_error, NULL_TREE,
22703 /*defer_ok*/false);
22704 if (specs && specs != error_mark_node)
22705 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22706 specs);
22708 /* Merge parameter declarations. */
22709 decl_parm = skip_artificial_parms_for (decl,
22710 DECL_ARGUMENTS (decl));
22711 pattern_parm
22712 = skip_artificial_parms_for (code_pattern,
22713 DECL_ARGUMENTS (code_pattern));
22714 while (decl_parm && !DECL_PACK_P (pattern_parm))
22716 tree parm_type;
22717 tree attributes;
22719 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22720 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22721 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22722 NULL_TREE);
22723 parm_type = type_decays_to (parm_type);
22724 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22725 TREE_TYPE (decl_parm) = parm_type;
22726 attributes = DECL_ATTRIBUTES (pattern_parm);
22727 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22729 DECL_ATTRIBUTES (decl_parm) = attributes;
22730 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22732 decl_parm = DECL_CHAIN (decl_parm);
22733 pattern_parm = DECL_CHAIN (pattern_parm);
22735 /* Merge any parameters that match with the function parameter
22736 pack. */
22737 if (pattern_parm && DECL_PACK_P (pattern_parm))
22739 int i, len;
22740 tree expanded_types;
22741 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22742 the parameters in this function parameter pack. */
22743 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22744 args, tf_error, NULL_TREE);
22745 len = TREE_VEC_LENGTH (expanded_types);
22746 for (i = 0; i < len; i++)
22748 tree parm_type;
22749 tree attributes;
22751 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22752 /* Rename the parameter to include the index. */
22753 DECL_NAME (decl_parm) =
22754 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22755 parm_type = TREE_VEC_ELT (expanded_types, i);
22756 parm_type = type_decays_to (parm_type);
22757 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22758 TREE_TYPE (decl_parm) = parm_type;
22759 attributes = DECL_ATTRIBUTES (pattern_parm);
22760 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22762 DECL_ATTRIBUTES (decl_parm) = attributes;
22763 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22765 decl_parm = DECL_CHAIN (decl_parm);
22768 /* Merge additional specifiers from the CODE_PATTERN. */
22769 if (DECL_DECLARED_INLINE_P (code_pattern)
22770 && !DECL_DECLARED_INLINE_P (decl))
22771 DECL_DECLARED_INLINE_P (decl) = 1;
22773 else if (VAR_P (decl))
22775 start_lambda_scope (decl);
22776 DECL_INITIAL (decl) =
22777 tsubst_expr (DECL_INITIAL (code_pattern), args,
22778 tf_error, DECL_TI_TEMPLATE (decl),
22779 /*integral_constant_expression_p=*/false);
22780 finish_lambda_scope ();
22781 if (VAR_HAD_UNKNOWN_BOUND (decl))
22782 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22783 tf_error, DECL_TI_TEMPLATE (decl));
22785 else
22786 gcc_unreachable ();
22788 pop_access_scope (decl);
22791 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22792 substituted to get DECL. */
22794 tree
22795 template_for_substitution (tree decl)
22797 tree tmpl = DECL_TI_TEMPLATE (decl);
22799 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22800 for the instantiation. This is not always the most general
22801 template. Consider, for example:
22803 template <class T>
22804 struct S { template <class U> void f();
22805 template <> void f<int>(); };
22807 and an instantiation of S<double>::f<int>. We want TD to be the
22808 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22809 while (/* An instantiation cannot have a definition, so we need a
22810 more general template. */
22811 DECL_TEMPLATE_INSTANTIATION (tmpl)
22812 /* We must also deal with friend templates. Given:
22814 template <class T> struct S {
22815 template <class U> friend void f() {};
22818 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22819 so far as the language is concerned, but that's still
22820 where we get the pattern for the instantiation from. On
22821 other hand, if the definition comes outside the class, say:
22823 template <class T> struct S {
22824 template <class U> friend void f();
22826 template <class U> friend void f() {}
22828 we don't need to look any further. That's what the check for
22829 DECL_INITIAL is for. */
22830 || (TREE_CODE (decl) == FUNCTION_DECL
22831 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22832 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22834 /* The present template, TD, should not be a definition. If it
22835 were a definition, we should be using it! Note that we
22836 cannot restructure the loop to just keep going until we find
22837 a template with a definition, since that might go too far if
22838 a specialization was declared, but not defined. */
22840 /* Fetch the more general template. */
22841 tmpl = DECL_TI_TEMPLATE (tmpl);
22844 return tmpl;
22847 /* Returns true if we need to instantiate this template instance even if we
22848 know we aren't going to emit it. */
22850 bool
22851 always_instantiate_p (tree decl)
22853 /* We always instantiate inline functions so that we can inline them. An
22854 explicit instantiation declaration prohibits implicit instantiation of
22855 non-inline functions. With high levels of optimization, we would
22856 normally inline non-inline functions -- but we're not allowed to do
22857 that for "extern template" functions. Therefore, we check
22858 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22859 return ((TREE_CODE (decl) == FUNCTION_DECL
22860 && (DECL_DECLARED_INLINE_P (decl)
22861 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22862 /* And we need to instantiate static data members so that
22863 their initializers are available in integral constant
22864 expressions. */
22865 || (VAR_P (decl)
22866 && decl_maybe_constant_var_p (decl)));
22869 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22870 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22871 error, true otherwise. */
22873 bool
22874 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22876 tree fntype, spec, noex, clone;
22878 /* Don't instantiate a noexcept-specification from template context. */
22879 if (processing_template_decl)
22880 return true;
22882 if (DECL_CLONED_FUNCTION_P (fn))
22883 fn = DECL_CLONED_FUNCTION (fn);
22884 fntype = TREE_TYPE (fn);
22885 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22887 if (!spec || !TREE_PURPOSE (spec))
22888 return true;
22890 noex = TREE_PURPOSE (spec);
22892 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22894 static hash_set<tree>* fns = new hash_set<tree>;
22895 bool added = false;
22896 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22897 spec = get_defaulted_eh_spec (fn, complain);
22898 else if (!(added = !fns->add (fn)))
22900 /* If hash_set::add returns true, the element was already there. */
22901 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22902 DECL_SOURCE_LOCATION (fn));
22903 error_at (loc,
22904 "exception specification of %qD depends on itself",
22905 fn);
22906 spec = noexcept_false_spec;
22908 else if (push_tinst_level (fn))
22910 push_access_scope (fn);
22911 push_deferring_access_checks (dk_no_deferred);
22912 input_location = DECL_SOURCE_LOCATION (fn);
22913 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22914 DEFERRED_NOEXCEPT_ARGS (noex),
22915 tf_warning_or_error, fn,
22916 /*function_p=*/false,
22917 /*integral_constant_expression_p=*/true);
22918 pop_deferring_access_checks ();
22919 pop_access_scope (fn);
22920 pop_tinst_level ();
22921 spec = build_noexcept_spec (noex, tf_warning_or_error);
22922 if (spec == error_mark_node)
22923 spec = noexcept_false_spec;
22925 else
22926 spec = noexcept_false_spec;
22928 if (added)
22929 fns->remove (fn);
22931 if (spec == error_mark_node)
22932 return false;
22934 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22937 FOR_EACH_CLONE (clone, fn)
22939 if (TREE_TYPE (clone) == fntype)
22940 TREE_TYPE (clone) = TREE_TYPE (fn);
22941 else
22942 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22945 return true;
22948 /* We're starting to process the function INST, an instantiation of PATTERN;
22949 add their parameters to local_specializations. */
22951 static void
22952 register_parameter_specializations (tree pattern, tree inst)
22954 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22955 tree spec_parm = DECL_ARGUMENTS (inst);
22956 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22958 register_local_specialization (spec_parm, tmpl_parm);
22959 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22960 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22962 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22964 if (!DECL_PACK_P (tmpl_parm))
22966 register_local_specialization (spec_parm, tmpl_parm);
22967 spec_parm = DECL_CHAIN (spec_parm);
22969 else
22971 /* Register the (value) argument pack as a specialization of
22972 TMPL_PARM, then move on. */
22973 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22974 register_local_specialization (argpack, tmpl_parm);
22977 gcc_assert (!spec_parm);
22980 /* Produce the definition of D, a _DECL generated from a template. If
22981 DEFER_OK is true, then we don't have to actually do the
22982 instantiation now; we just have to do it sometime. Normally it is
22983 an error if this is an explicit instantiation but D is undefined.
22984 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22985 instantiated class template. */
22987 tree
22988 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22990 tree tmpl = DECL_TI_TEMPLATE (d);
22991 tree gen_args;
22992 tree args;
22993 tree td;
22994 tree code_pattern;
22995 tree spec;
22996 tree gen_tmpl;
22997 bool pattern_defined;
22998 location_t saved_loc = input_location;
22999 int saved_unevaluated_operand = cp_unevaluated_operand;
23000 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23001 bool external_p;
23002 bool deleted_p;
23004 /* This function should only be used to instantiate templates for
23005 functions and static member variables. */
23006 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23008 /* A concept is never instantiated. */
23009 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23011 /* Variables are never deferred; if instantiation is required, they
23012 are instantiated right away. That allows for better code in the
23013 case that an expression refers to the value of the variable --
23014 if the variable has a constant value the referring expression can
23015 take advantage of that fact. */
23016 if (VAR_P (d))
23017 defer_ok = false;
23019 /* Don't instantiate cloned functions. Instead, instantiate the
23020 functions they cloned. */
23021 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23022 d = DECL_CLONED_FUNCTION (d);
23024 if (DECL_TEMPLATE_INSTANTIATED (d)
23025 || (TREE_CODE (d) == FUNCTION_DECL
23026 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23027 || DECL_TEMPLATE_SPECIALIZATION (d))
23028 /* D has already been instantiated or explicitly specialized, so
23029 there's nothing for us to do here.
23031 It might seem reasonable to check whether or not D is an explicit
23032 instantiation, and, if so, stop here. But when an explicit
23033 instantiation is deferred until the end of the compilation,
23034 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23035 the instantiation. */
23036 return d;
23038 /* Check to see whether we know that this template will be
23039 instantiated in some other file, as with "extern template"
23040 extension. */
23041 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23043 /* In general, we do not instantiate such templates. */
23044 if (external_p && !always_instantiate_p (d))
23045 return d;
23047 gen_tmpl = most_general_template (tmpl);
23048 gen_args = DECL_TI_ARGS (d);
23050 if (tmpl != gen_tmpl)
23051 /* We should already have the extra args. */
23052 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23053 == TMPL_ARGS_DEPTH (gen_args));
23054 /* And what's in the hash table should match D. */
23055 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23056 || spec == NULL_TREE);
23058 /* This needs to happen before any tsubsting. */
23059 if (! push_tinst_level (d))
23060 return d;
23062 timevar_push (TV_TEMPLATE_INST);
23064 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23065 for the instantiation. */
23066 td = template_for_substitution (d);
23067 args = gen_args;
23069 if (VAR_P (d))
23071 /* Look up an explicit specialization, if any. */
23072 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23073 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23074 if (elt && elt != error_mark_node)
23076 td = TREE_VALUE (elt);
23077 args = TREE_PURPOSE (elt);
23081 code_pattern = DECL_TEMPLATE_RESULT (td);
23083 /* We should never be trying to instantiate a member of a class
23084 template or partial specialization. */
23085 gcc_assert (d != code_pattern);
23087 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23088 || DECL_TEMPLATE_SPECIALIZATION (td))
23089 /* In the case of a friend template whose definition is provided
23090 outside the class, we may have too many arguments. Drop the
23091 ones we don't need. The same is true for specializations. */
23092 args = get_innermost_template_args
23093 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23095 if (TREE_CODE (d) == FUNCTION_DECL)
23097 deleted_p = DECL_DELETED_FN (code_pattern);
23098 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23099 && DECL_INITIAL (code_pattern) != error_mark_node)
23100 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23101 || deleted_p);
23103 else
23105 deleted_p = false;
23106 if (DECL_CLASS_SCOPE_P (code_pattern))
23107 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23108 || DECL_INLINE_VAR_P (code_pattern));
23109 else
23110 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23113 /* We may be in the middle of deferred access check. Disable it now. */
23114 push_deferring_access_checks (dk_no_deferred);
23116 /* Unless an explicit instantiation directive has already determined
23117 the linkage of D, remember that a definition is available for
23118 this entity. */
23119 if (pattern_defined
23120 && !DECL_INTERFACE_KNOWN (d)
23121 && !DECL_NOT_REALLY_EXTERN (d))
23122 mark_definable (d);
23124 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23125 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23126 input_location = DECL_SOURCE_LOCATION (d);
23128 /* If D is a member of an explicitly instantiated class template,
23129 and no definition is available, treat it like an implicit
23130 instantiation. */
23131 if (!pattern_defined && expl_inst_class_mem_p
23132 && DECL_EXPLICIT_INSTANTIATION (d))
23134 /* Leave linkage flags alone on instantiations with anonymous
23135 visibility. */
23136 if (TREE_PUBLIC (d))
23138 DECL_NOT_REALLY_EXTERN (d) = 0;
23139 DECL_INTERFACE_KNOWN (d) = 0;
23141 SET_DECL_IMPLICIT_INSTANTIATION (d);
23144 /* Defer all other templates, unless we have been explicitly
23145 forbidden from doing so. */
23146 if (/* If there is no definition, we cannot instantiate the
23147 template. */
23148 ! pattern_defined
23149 /* If it's OK to postpone instantiation, do so. */
23150 || defer_ok
23151 /* If this is a static data member that will be defined
23152 elsewhere, we don't want to instantiate the entire data
23153 member, but we do want to instantiate the initializer so that
23154 we can substitute that elsewhere. */
23155 || (external_p && VAR_P (d))
23156 /* Handle here a deleted function too, avoid generating
23157 its body (c++/61080). */
23158 || deleted_p)
23160 /* The definition of the static data member is now required so
23161 we must substitute the initializer. */
23162 if (VAR_P (d)
23163 && !DECL_INITIAL (d)
23164 && DECL_INITIAL (code_pattern))
23166 tree ns;
23167 tree init;
23168 bool const_init = false;
23169 bool enter_context = DECL_CLASS_SCOPE_P (d);
23171 ns = decl_namespace_context (d);
23172 push_nested_namespace (ns);
23173 if (enter_context)
23174 push_nested_class (DECL_CONTEXT (d));
23175 init = tsubst_expr (DECL_INITIAL (code_pattern),
23176 args,
23177 tf_warning_or_error, NULL_TREE,
23178 /*integral_constant_expression_p=*/false);
23179 /* If instantiating the initializer involved instantiating this
23180 again, don't call cp_finish_decl twice. */
23181 if (!DECL_INITIAL (d))
23183 /* Make sure the initializer is still constant, in case of
23184 circular dependency (template/instantiate6.C). */
23185 const_init
23186 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23187 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23188 /*asmspec_tree=*/NULL_TREE,
23189 LOOKUP_ONLYCONVERTING);
23191 if (enter_context)
23192 pop_nested_class ();
23193 pop_nested_namespace (ns);
23196 /* We restore the source position here because it's used by
23197 add_pending_template. */
23198 input_location = saved_loc;
23200 if (at_eof && !pattern_defined
23201 && DECL_EXPLICIT_INSTANTIATION (d)
23202 && DECL_NOT_REALLY_EXTERN (d))
23203 /* [temp.explicit]
23205 The definition of a non-exported function template, a
23206 non-exported member function template, or a non-exported
23207 member function or static data member of a class template
23208 shall be present in every translation unit in which it is
23209 explicitly instantiated. */
23210 permerror (input_location, "explicit instantiation of %qD "
23211 "but no definition available", d);
23213 /* If we're in unevaluated context, we just wanted to get the
23214 constant value; this isn't an odr use, so don't queue
23215 a full instantiation. */
23216 if (cp_unevaluated_operand != 0)
23217 goto out;
23218 /* ??? Historically, we have instantiated inline functions, even
23219 when marked as "extern template". */
23220 if (!(external_p && VAR_P (d)))
23221 add_pending_template (d);
23222 goto out;
23224 /* Tell the repository that D is available in this translation unit
23225 -- and see if it is supposed to be instantiated here. */
23226 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23228 /* In a PCH file, despite the fact that the repository hasn't
23229 requested instantiation in the PCH it is still possible that
23230 an instantiation will be required in a file that includes the
23231 PCH. */
23232 if (pch_file)
23233 add_pending_template (d);
23234 /* Instantiate inline functions so that the inliner can do its
23235 job, even though we'll not be emitting a copy of this
23236 function. */
23237 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23238 goto out;
23241 bool push_to_top, nested;
23242 tree fn_context;
23243 fn_context = decl_function_context (d);
23244 nested = current_function_decl != NULL_TREE;
23245 push_to_top = !(nested && fn_context == current_function_decl);
23247 vec<tree> omp_privatization_save;
23248 if (nested)
23249 save_omp_privatization_clauses (omp_privatization_save);
23251 if (push_to_top)
23252 push_to_top_level ();
23253 else
23255 push_function_context ();
23256 cp_unevaluated_operand = 0;
23257 c_inhibit_evaluation_warnings = 0;
23260 /* Mark D as instantiated so that recursive calls to
23261 instantiate_decl do not try to instantiate it again. */
23262 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23264 /* Regenerate the declaration in case the template has been modified
23265 by a subsequent redeclaration. */
23266 regenerate_decl_from_template (d, td, args);
23268 /* We already set the file and line above. Reset them now in case
23269 they changed as a result of calling regenerate_decl_from_template. */
23270 input_location = DECL_SOURCE_LOCATION (d);
23272 if (VAR_P (d))
23274 tree init;
23275 bool const_init = false;
23277 /* Clear out DECL_RTL; whatever was there before may not be right
23278 since we've reset the type of the declaration. */
23279 SET_DECL_RTL (d, NULL);
23280 DECL_IN_AGGR_P (d) = 0;
23282 /* The initializer is placed in DECL_INITIAL by
23283 regenerate_decl_from_template so we don't need to
23284 push/pop_access_scope again here. Pull it out so that
23285 cp_finish_decl can process it. */
23286 init = DECL_INITIAL (d);
23287 DECL_INITIAL (d) = NULL_TREE;
23288 DECL_INITIALIZED_P (d) = 0;
23290 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23291 initializer. That function will defer actual emission until
23292 we have a chance to determine linkage. */
23293 DECL_EXTERNAL (d) = 0;
23295 /* Enter the scope of D so that access-checking works correctly. */
23296 bool enter_context = DECL_CLASS_SCOPE_P (d);
23297 if (enter_context)
23298 push_nested_class (DECL_CONTEXT (d));
23300 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23301 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23303 if (enter_context)
23304 pop_nested_class ();
23306 if (variable_template_p (gen_tmpl))
23307 note_variable_template_instantiation (d);
23309 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23310 synthesize_method (d);
23311 else if (TREE_CODE (d) == FUNCTION_DECL)
23313 /* Set up the list of local specializations. */
23314 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23315 tree block = NULL_TREE;
23317 /* Set up context. */
23318 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23319 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23320 block = push_stmt_list ();
23321 else
23322 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23324 /* Some typedefs referenced from within the template code need to be
23325 access checked at template instantiation time, i.e now. These
23326 types were added to the template at parsing time. Let's get those
23327 and perform the access checks then. */
23328 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23329 args);
23331 /* Create substitution entries for the parameters. */
23332 register_parameter_specializations (code_pattern, d);
23334 /* Substitute into the body of the function. */
23335 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23336 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23337 tf_warning_or_error, tmpl);
23338 else
23340 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23341 tf_warning_or_error, tmpl,
23342 /*integral_constant_expression_p=*/false);
23344 /* Set the current input_location to the end of the function
23345 so that finish_function knows where we are. */
23346 input_location
23347 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23349 /* Remember if we saw an infinite loop in the template. */
23350 current_function_infinite_loop
23351 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23354 /* Finish the function. */
23355 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23356 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23357 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23358 else
23360 d = finish_function (/*inline_p=*/false);
23361 expand_or_defer_fn (d);
23364 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23365 cp_check_omp_declare_reduction (d);
23368 /* We're not deferring instantiation any more. */
23369 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23371 if (push_to_top)
23372 pop_from_top_level ();
23373 else
23374 pop_function_context ();
23376 if (nested)
23377 restore_omp_privatization_clauses (omp_privatization_save);
23379 out:
23380 pop_deferring_access_checks ();
23381 timevar_pop (TV_TEMPLATE_INST);
23382 pop_tinst_level ();
23383 input_location = saved_loc;
23384 cp_unevaluated_operand = saved_unevaluated_operand;
23385 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23387 return d;
23390 /* Run through the list of templates that we wish we could
23391 instantiate, and instantiate any we can. RETRIES is the
23392 number of times we retry pending template instantiation. */
23394 void
23395 instantiate_pending_templates (int retries)
23397 int reconsider;
23398 location_t saved_loc = input_location;
23400 /* Instantiating templates may trigger vtable generation. This in turn
23401 may require further template instantiations. We place a limit here
23402 to avoid infinite loop. */
23403 if (pending_templates && retries >= max_tinst_depth)
23405 tree decl = pending_templates->tinst->decl;
23407 fatal_error (input_location,
23408 "template instantiation depth exceeds maximum of %d"
23409 " instantiating %q+D, possibly from virtual table generation"
23410 " (use -ftemplate-depth= to increase the maximum)",
23411 max_tinst_depth, decl);
23412 if (TREE_CODE (decl) == FUNCTION_DECL)
23413 /* Pretend that we defined it. */
23414 DECL_INITIAL (decl) = error_mark_node;
23415 return;
23420 struct pending_template **t = &pending_templates;
23421 struct pending_template *last = NULL;
23422 reconsider = 0;
23423 while (*t)
23425 tree instantiation = reopen_tinst_level ((*t)->tinst);
23426 bool complete = false;
23428 if (TYPE_P (instantiation))
23430 if (!COMPLETE_TYPE_P (instantiation))
23432 instantiate_class_template (instantiation);
23433 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23434 for (tree fld = TYPE_FIELDS (instantiation);
23435 fld; fld = TREE_CHAIN (fld))
23436 if ((VAR_P (fld)
23437 || (TREE_CODE (fld) == FUNCTION_DECL
23438 && !DECL_ARTIFICIAL (fld)))
23439 && DECL_TEMPLATE_INSTANTIATION (fld))
23440 instantiate_decl (fld,
23441 /*defer_ok=*/false,
23442 /*expl_inst_class_mem_p=*/false);
23444 if (COMPLETE_TYPE_P (instantiation))
23445 reconsider = 1;
23448 complete = COMPLETE_TYPE_P (instantiation);
23450 else
23452 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23453 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23455 instantiation
23456 = instantiate_decl (instantiation,
23457 /*defer_ok=*/false,
23458 /*expl_inst_class_mem_p=*/false);
23459 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23460 reconsider = 1;
23463 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23464 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23467 if (complete)
23468 /* If INSTANTIATION has been instantiated, then we don't
23469 need to consider it again in the future. */
23470 *t = (*t)->next;
23471 else
23473 last = *t;
23474 t = &(*t)->next;
23476 tinst_depth = 0;
23477 current_tinst_level = NULL;
23479 last_pending_template = last;
23481 while (reconsider);
23483 input_location = saved_loc;
23486 /* Substitute ARGVEC into T, which is a list of initializers for
23487 either base class or a non-static data member. The TREE_PURPOSEs
23488 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23489 instantiate_decl. */
23491 static tree
23492 tsubst_initializer_list (tree t, tree argvec)
23494 tree inits = NULL_TREE;
23496 for (; t; t = TREE_CHAIN (t))
23498 tree decl;
23499 tree init;
23500 tree expanded_bases = NULL_TREE;
23501 tree expanded_arguments = NULL_TREE;
23502 int i, len = 1;
23504 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23506 tree expr;
23507 tree arg;
23509 /* Expand the base class expansion type into separate base
23510 classes. */
23511 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23512 tf_warning_or_error,
23513 NULL_TREE);
23514 if (expanded_bases == error_mark_node)
23515 continue;
23517 /* We'll be building separate TREE_LISTs of arguments for
23518 each base. */
23519 len = TREE_VEC_LENGTH (expanded_bases);
23520 expanded_arguments = make_tree_vec (len);
23521 for (i = 0; i < len; i++)
23522 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23524 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23525 expand each argument in the TREE_VALUE of t. */
23526 expr = make_node (EXPR_PACK_EXPANSION);
23527 PACK_EXPANSION_LOCAL_P (expr) = true;
23528 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23529 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23531 if (TREE_VALUE (t) == void_type_node)
23532 /* VOID_TYPE_NODE is used to indicate
23533 value-initialization. */
23535 for (i = 0; i < len; i++)
23536 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23538 else
23540 /* Substitute parameter packs into each argument in the
23541 TREE_LIST. */
23542 in_base_initializer = 1;
23543 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23545 tree expanded_exprs;
23547 /* Expand the argument. */
23548 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23549 expanded_exprs
23550 = tsubst_pack_expansion (expr, argvec,
23551 tf_warning_or_error,
23552 NULL_TREE);
23553 if (expanded_exprs == error_mark_node)
23554 continue;
23556 /* Prepend each of the expanded expressions to the
23557 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23558 for (i = 0; i < len; i++)
23560 TREE_VEC_ELT (expanded_arguments, i) =
23561 tree_cons (NULL_TREE,
23562 TREE_VEC_ELT (expanded_exprs, i),
23563 TREE_VEC_ELT (expanded_arguments, i));
23566 in_base_initializer = 0;
23568 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23569 since we built them backwards. */
23570 for (i = 0; i < len; i++)
23572 TREE_VEC_ELT (expanded_arguments, i) =
23573 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23578 for (i = 0; i < len; ++i)
23580 if (expanded_bases)
23582 decl = TREE_VEC_ELT (expanded_bases, i);
23583 decl = expand_member_init (decl);
23584 init = TREE_VEC_ELT (expanded_arguments, i);
23586 else
23588 tree tmp;
23589 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23590 tf_warning_or_error, NULL_TREE);
23592 decl = expand_member_init (decl);
23593 if (decl && !DECL_P (decl))
23594 in_base_initializer = 1;
23596 init = TREE_VALUE (t);
23597 tmp = init;
23598 if (init != void_type_node)
23599 init = tsubst_expr (init, argvec,
23600 tf_warning_or_error, NULL_TREE,
23601 /*integral_constant_expression_p=*/false);
23602 if (init == NULL_TREE && tmp != NULL_TREE)
23603 /* If we had an initializer but it instantiated to nothing,
23604 value-initialize the object. This will only occur when
23605 the initializer was a pack expansion where the parameter
23606 packs used in that expansion were of length zero. */
23607 init = void_type_node;
23608 in_base_initializer = 0;
23611 if (decl)
23613 init = build_tree_list (decl, init);
23614 TREE_CHAIN (init) = inits;
23615 inits = init;
23619 return inits;
23622 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23624 static void
23625 set_current_access_from_decl (tree decl)
23627 if (TREE_PRIVATE (decl))
23628 current_access_specifier = access_private_node;
23629 else if (TREE_PROTECTED (decl))
23630 current_access_specifier = access_protected_node;
23631 else
23632 current_access_specifier = access_public_node;
23635 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23636 is the instantiation (which should have been created with
23637 start_enum) and ARGS are the template arguments to use. */
23639 static void
23640 tsubst_enum (tree tag, tree newtag, tree args)
23642 tree e;
23644 if (SCOPED_ENUM_P (newtag))
23645 begin_scope (sk_scoped_enum, newtag);
23647 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23649 tree value;
23650 tree decl;
23652 decl = TREE_VALUE (e);
23653 /* Note that in a template enum, the TREE_VALUE is the
23654 CONST_DECL, not the corresponding INTEGER_CST. */
23655 value = tsubst_expr (DECL_INITIAL (decl),
23656 args, tf_warning_or_error, NULL_TREE,
23657 /*integral_constant_expression_p=*/true);
23659 /* Give this enumeration constant the correct access. */
23660 set_current_access_from_decl (decl);
23662 /* Actually build the enumerator itself. Here we're assuming that
23663 enumerators can't have dependent attributes. */
23664 build_enumerator (DECL_NAME (decl), value, newtag,
23665 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23668 if (SCOPED_ENUM_P (newtag))
23669 finish_scope ();
23671 finish_enum_value_list (newtag);
23672 finish_enum (newtag);
23674 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23675 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23678 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23679 its type -- but without substituting the innermost set of template
23680 arguments. So, innermost set of template parameters will appear in
23681 the type. */
23683 tree
23684 get_mostly_instantiated_function_type (tree decl)
23686 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23687 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23690 /* Return truthvalue if we're processing a template different from
23691 the last one involved in diagnostics. */
23692 bool
23693 problematic_instantiation_changed (void)
23695 return current_tinst_level != last_error_tinst_level;
23698 /* Remember current template involved in diagnostics. */
23699 void
23700 record_last_problematic_instantiation (void)
23702 last_error_tinst_level = current_tinst_level;
23705 struct tinst_level *
23706 current_instantiation (void)
23708 return current_tinst_level;
23711 /* Return TRUE if current_function_decl is being instantiated, false
23712 otherwise. */
23714 bool
23715 instantiating_current_function_p (void)
23717 return (current_instantiation ()
23718 && current_instantiation ()->decl == current_function_decl);
23721 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23722 type. Return false for ok, true for disallowed. Issue error and
23723 inform messages under control of COMPLAIN. */
23725 static bool
23726 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23728 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23729 return false;
23730 else if (POINTER_TYPE_P (type))
23731 return false;
23732 else if (TYPE_PTRMEM_P (type))
23733 return false;
23734 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23735 return false;
23736 else if (TREE_CODE (type) == TYPENAME_TYPE)
23737 return false;
23738 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23739 return false;
23740 else if (TREE_CODE (type) == NULLPTR_TYPE)
23741 return false;
23742 /* A bound template template parm could later be instantiated to have a valid
23743 nontype parm type via an alias template. */
23744 else if (cxx_dialect >= cxx11
23745 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23746 return false;
23748 if (complain & tf_error)
23750 if (type == error_mark_node)
23751 inform (input_location, "invalid template non-type parameter");
23752 else
23753 error ("%q#T is not a valid type for a template non-type parameter",
23754 type);
23756 return true;
23759 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23760 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23762 static bool
23763 dependent_type_p_r (tree type)
23765 tree scope;
23767 /* [temp.dep.type]
23769 A type is dependent if it is:
23771 -- a template parameter. Template template parameters are types
23772 for us (since TYPE_P holds true for them) so we handle
23773 them here. */
23774 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23775 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23776 return true;
23777 /* -- a qualified-id with a nested-name-specifier which contains a
23778 class-name that names a dependent type or whose unqualified-id
23779 names a dependent type. */
23780 if (TREE_CODE (type) == TYPENAME_TYPE)
23781 return true;
23783 /* An alias template specialization can be dependent even if the
23784 resulting type is not. */
23785 if (dependent_alias_template_spec_p (type))
23786 return true;
23788 /* -- a cv-qualified type where the cv-unqualified type is
23789 dependent.
23790 No code is necessary for this bullet; the code below handles
23791 cv-qualified types, and we don't want to strip aliases with
23792 TYPE_MAIN_VARIANT because of DR 1558. */
23793 /* -- a compound type constructed from any dependent type. */
23794 if (TYPE_PTRMEM_P (type))
23795 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23796 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23797 (type)));
23798 else if (TYPE_PTR_P (type)
23799 || TREE_CODE (type) == REFERENCE_TYPE)
23800 return dependent_type_p (TREE_TYPE (type));
23801 else if (TREE_CODE (type) == FUNCTION_TYPE
23802 || TREE_CODE (type) == METHOD_TYPE)
23804 tree arg_type;
23806 if (dependent_type_p (TREE_TYPE (type)))
23807 return true;
23808 for (arg_type = TYPE_ARG_TYPES (type);
23809 arg_type;
23810 arg_type = TREE_CHAIN (arg_type))
23811 if (dependent_type_p (TREE_VALUE (arg_type)))
23812 return true;
23813 if (cxx_dialect >= cxx17)
23814 /* A value-dependent noexcept-specifier makes the type dependent. */
23815 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23816 if (tree noex = TREE_PURPOSE (spec))
23817 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23818 affect overload resolution and treating it as dependent breaks
23819 things. */
23820 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23821 && value_dependent_expression_p (noex))
23822 return true;
23823 return false;
23825 /* -- an array type constructed from any dependent type or whose
23826 size is specified by a constant expression that is
23827 value-dependent.
23829 We checked for type- and value-dependence of the bounds in
23830 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23831 if (TREE_CODE (type) == ARRAY_TYPE)
23833 if (TYPE_DOMAIN (type)
23834 && dependent_type_p (TYPE_DOMAIN (type)))
23835 return true;
23836 return dependent_type_p (TREE_TYPE (type));
23839 /* -- a template-id in which either the template name is a template
23840 parameter ... */
23841 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23842 return true;
23843 /* ... or any of the template arguments is a dependent type or
23844 an expression that is type-dependent or value-dependent. */
23845 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23846 && (any_dependent_template_arguments_p
23847 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23848 return true;
23850 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23851 dependent; if the argument of the `typeof' expression is not
23852 type-dependent, then it should already been have resolved. */
23853 if (TREE_CODE (type) == TYPEOF_TYPE
23854 || TREE_CODE (type) == DECLTYPE_TYPE
23855 || TREE_CODE (type) == UNDERLYING_TYPE)
23856 return true;
23858 /* A template argument pack is dependent if any of its packed
23859 arguments are. */
23860 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23862 tree args = ARGUMENT_PACK_ARGS (type);
23863 int i, len = TREE_VEC_LENGTH (args);
23864 for (i = 0; i < len; ++i)
23865 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23866 return true;
23869 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23870 be template parameters. */
23871 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23872 return true;
23874 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23875 return true;
23877 /* The standard does not specifically mention types that are local
23878 to template functions or local classes, but they should be
23879 considered dependent too. For example:
23881 template <int I> void f() {
23882 enum E { a = I };
23883 S<sizeof (E)> s;
23886 The size of `E' cannot be known until the value of `I' has been
23887 determined. Therefore, `E' must be considered dependent. */
23888 scope = TYPE_CONTEXT (type);
23889 if (scope && TYPE_P (scope))
23890 return dependent_type_p (scope);
23891 /* Don't use type_dependent_expression_p here, as it can lead
23892 to infinite recursion trying to determine whether a lambda
23893 nested in a lambda is dependent (c++/47687). */
23894 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23895 && DECL_LANG_SPECIFIC (scope)
23896 && DECL_TEMPLATE_INFO (scope)
23897 && (any_dependent_template_arguments_p
23898 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23899 return true;
23901 /* Other types are non-dependent. */
23902 return false;
23905 /* Returns TRUE if TYPE is dependent, in the sense of
23906 [temp.dep.type]. Note that a NULL type is considered dependent. */
23908 bool
23909 dependent_type_p (tree type)
23911 /* If there are no template parameters in scope, then there can't be
23912 any dependent types. */
23913 if (!processing_template_decl)
23915 /* If we are not processing a template, then nobody should be
23916 providing us with a dependent type. */
23917 gcc_assert (type);
23918 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23919 return false;
23922 /* If the type is NULL, we have not computed a type for the entity
23923 in question; in that case, the type is dependent. */
23924 if (!type)
23925 return true;
23927 /* Erroneous types can be considered non-dependent. */
23928 if (type == error_mark_node)
23929 return false;
23931 /* Getting here with global_type_node means we improperly called this
23932 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23933 gcc_checking_assert (type != global_type_node);
23935 /* If we have not already computed the appropriate value for TYPE,
23936 do so now. */
23937 if (!TYPE_DEPENDENT_P_VALID (type))
23939 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23940 TYPE_DEPENDENT_P_VALID (type) = 1;
23943 return TYPE_DEPENDENT_P (type);
23946 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23947 lookup. In other words, a dependent type that is not the current
23948 instantiation. */
23950 bool
23951 dependent_scope_p (tree scope)
23953 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23954 && !currently_open_class (scope));
23957 /* T is a SCOPE_REF. Return whether it represents a non-static member of
23958 an unknown base of 'this' (and is therefore instantiation-dependent). */
23960 static bool
23961 unknown_base_ref_p (tree t)
23963 if (!current_class_ptr)
23964 return false;
23966 tree mem = TREE_OPERAND (t, 1);
23967 if (shared_member_p (mem))
23968 return false;
23970 tree cur = current_nonlambda_class_type ();
23971 if (!any_dependent_bases_p (cur))
23972 return false;
23974 tree ctx = TREE_OPERAND (t, 0);
23975 if (DERIVED_FROM_P (ctx, cur))
23976 return false;
23978 return true;
23981 /* T is a SCOPE_REF; return whether we need to consider it
23982 instantiation-dependent so that we can check access at instantiation
23983 time even though we know which member it resolves to. */
23985 static bool
23986 instantiation_dependent_scope_ref_p (tree t)
23988 if (DECL_P (TREE_OPERAND (t, 1))
23989 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23990 && !unknown_base_ref_p (t)
23991 && accessible_in_template_p (TREE_OPERAND (t, 0),
23992 TREE_OPERAND (t, 1)))
23993 return false;
23994 else
23995 return true;
23998 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23999 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24000 expression. */
24002 /* Note that this predicate is not appropriate for general expressions;
24003 only constant expressions (that satisfy potential_constant_expression)
24004 can be tested for value dependence. */
24006 bool
24007 value_dependent_expression_p (tree expression)
24009 if (!processing_template_decl || expression == NULL_TREE)
24010 return false;
24012 /* A type-dependent expression is also value-dependent. */
24013 if (type_dependent_expression_p (expression))
24014 return true;
24016 switch (TREE_CODE (expression))
24018 case BASELINK:
24019 /* A dependent member function of the current instantiation. */
24020 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24022 case FUNCTION_DECL:
24023 /* A dependent member function of the current instantiation. */
24024 if (DECL_CLASS_SCOPE_P (expression)
24025 && dependent_type_p (DECL_CONTEXT (expression)))
24026 return true;
24027 break;
24029 case IDENTIFIER_NODE:
24030 /* A name that has not been looked up -- must be dependent. */
24031 return true;
24033 case TEMPLATE_PARM_INDEX:
24034 /* A non-type template parm. */
24035 return true;
24037 case CONST_DECL:
24038 /* A non-type template parm. */
24039 if (DECL_TEMPLATE_PARM_P (expression))
24040 return true;
24041 return value_dependent_expression_p (DECL_INITIAL (expression));
24043 case VAR_DECL:
24044 /* A constant with literal type and is initialized
24045 with an expression that is value-dependent. */
24046 if (DECL_DEPENDENT_INIT_P (expression)
24047 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24048 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24049 return true;
24050 if (DECL_HAS_VALUE_EXPR_P (expression))
24052 tree value_expr = DECL_VALUE_EXPR (expression);
24053 if (value_dependent_expression_p (value_expr))
24054 return true;
24056 return false;
24058 case DYNAMIC_CAST_EXPR:
24059 case STATIC_CAST_EXPR:
24060 case CONST_CAST_EXPR:
24061 case REINTERPRET_CAST_EXPR:
24062 case CAST_EXPR:
24063 case IMPLICIT_CONV_EXPR:
24064 /* These expressions are value-dependent if the type to which
24065 the cast occurs is dependent or the expression being casted
24066 is value-dependent. */
24068 tree type = TREE_TYPE (expression);
24070 if (dependent_type_p (type))
24071 return true;
24073 /* A functional cast has a list of operands. */
24074 expression = TREE_OPERAND (expression, 0);
24075 if (!expression)
24077 /* If there are no operands, it must be an expression such
24078 as "int()". This should not happen for aggregate types
24079 because it would form non-constant expressions. */
24080 gcc_assert (cxx_dialect >= cxx11
24081 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24083 return false;
24086 if (TREE_CODE (expression) == TREE_LIST)
24087 return any_value_dependent_elements_p (expression);
24089 return value_dependent_expression_p (expression);
24092 case SIZEOF_EXPR:
24093 if (SIZEOF_EXPR_TYPE_P (expression))
24094 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24095 /* FALLTHRU */
24096 case ALIGNOF_EXPR:
24097 case TYPEID_EXPR:
24098 /* A `sizeof' expression is value-dependent if the operand is
24099 type-dependent or is a pack expansion. */
24100 expression = TREE_OPERAND (expression, 0);
24101 if (PACK_EXPANSION_P (expression))
24102 return true;
24103 else if (TYPE_P (expression))
24104 return dependent_type_p (expression);
24105 return instantiation_dependent_uneval_expression_p (expression);
24107 case AT_ENCODE_EXPR:
24108 /* An 'encode' expression is value-dependent if the operand is
24109 type-dependent. */
24110 expression = TREE_OPERAND (expression, 0);
24111 return dependent_type_p (expression);
24113 case NOEXCEPT_EXPR:
24114 expression = TREE_OPERAND (expression, 0);
24115 return instantiation_dependent_uneval_expression_p (expression);
24117 case SCOPE_REF:
24118 /* All instantiation-dependent expressions should also be considered
24119 value-dependent. */
24120 return instantiation_dependent_scope_ref_p (expression);
24122 case COMPONENT_REF:
24123 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24124 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24126 case NONTYPE_ARGUMENT_PACK:
24127 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24128 is value-dependent. */
24130 tree values = ARGUMENT_PACK_ARGS (expression);
24131 int i, len = TREE_VEC_LENGTH (values);
24133 for (i = 0; i < len; ++i)
24134 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24135 return true;
24137 return false;
24140 case TRAIT_EXPR:
24142 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24144 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24145 return true;
24147 if (!type2)
24148 return false;
24150 if (TREE_CODE (type2) != TREE_LIST)
24151 return dependent_type_p (type2);
24153 for (; type2; type2 = TREE_CHAIN (type2))
24154 if (dependent_type_p (TREE_VALUE (type2)))
24155 return true;
24157 return false;
24160 case MODOP_EXPR:
24161 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24162 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24164 case ARRAY_REF:
24165 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24166 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24168 case ADDR_EXPR:
24170 tree op = TREE_OPERAND (expression, 0);
24171 return (value_dependent_expression_p (op)
24172 || has_value_dependent_address (op));
24175 case REQUIRES_EXPR:
24176 /* Treat all requires-expressions as value-dependent so
24177 we don't try to fold them. */
24178 return true;
24180 case TYPE_REQ:
24181 return dependent_type_p (TREE_OPERAND (expression, 0));
24183 case CALL_EXPR:
24185 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24186 return true;
24187 tree fn = get_callee_fndecl (expression);
24188 int i, nargs;
24189 nargs = call_expr_nargs (expression);
24190 for (i = 0; i < nargs; ++i)
24192 tree op = CALL_EXPR_ARG (expression, i);
24193 /* In a call to a constexpr member function, look through the
24194 implicit ADDR_EXPR on the object argument so that it doesn't
24195 cause the call to be considered value-dependent. We also
24196 look through it in potential_constant_expression. */
24197 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24198 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24199 && TREE_CODE (op) == ADDR_EXPR)
24200 op = TREE_OPERAND (op, 0);
24201 if (value_dependent_expression_p (op))
24202 return true;
24204 return false;
24207 case TEMPLATE_ID_EXPR:
24208 return variable_concept_p (TREE_OPERAND (expression, 0));
24210 case CONSTRUCTOR:
24212 unsigned ix;
24213 tree val;
24214 if (dependent_type_p (TREE_TYPE (expression)))
24215 return true;
24216 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24217 if (value_dependent_expression_p (val))
24218 return true;
24219 return false;
24222 case STMT_EXPR:
24223 /* Treat a GNU statement expression as dependent to avoid crashing
24224 under instantiate_non_dependent_expr; it can't be constant. */
24225 return true;
24227 default:
24228 /* A constant expression is value-dependent if any subexpression is
24229 value-dependent. */
24230 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24232 case tcc_reference:
24233 case tcc_unary:
24234 case tcc_comparison:
24235 case tcc_binary:
24236 case tcc_expression:
24237 case tcc_vl_exp:
24239 int i, len = cp_tree_operand_length (expression);
24241 for (i = 0; i < len; i++)
24243 tree t = TREE_OPERAND (expression, i);
24245 /* In some cases, some of the operands may be missing.
24246 (For example, in the case of PREDECREMENT_EXPR, the
24247 amount to increment by may be missing.) That doesn't
24248 make the expression dependent. */
24249 if (t && value_dependent_expression_p (t))
24250 return true;
24253 break;
24254 default:
24255 break;
24257 break;
24260 /* The expression is not value-dependent. */
24261 return false;
24264 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24265 [temp.dep.expr]. Note that an expression with no type is
24266 considered dependent. Other parts of the compiler arrange for an
24267 expression with type-dependent subexpressions to have no type, so
24268 this function doesn't have to be fully recursive. */
24270 bool
24271 type_dependent_expression_p (tree expression)
24273 if (!processing_template_decl)
24274 return false;
24276 if (expression == NULL_TREE || expression == error_mark_node)
24277 return false;
24279 STRIP_ANY_LOCATION_WRAPPER (expression);
24281 /* An unresolved name is always dependent. */
24282 if (identifier_p (expression)
24283 || TREE_CODE (expression) == USING_DECL
24284 || TREE_CODE (expression) == WILDCARD_DECL)
24285 return true;
24287 /* A fold expression is type-dependent. */
24288 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24289 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24290 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24291 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24292 return true;
24294 /* Some expression forms are never type-dependent. */
24295 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24296 || TREE_CODE (expression) == SIZEOF_EXPR
24297 || TREE_CODE (expression) == ALIGNOF_EXPR
24298 || TREE_CODE (expression) == AT_ENCODE_EXPR
24299 || TREE_CODE (expression) == NOEXCEPT_EXPR
24300 || TREE_CODE (expression) == TRAIT_EXPR
24301 || TREE_CODE (expression) == TYPEID_EXPR
24302 || TREE_CODE (expression) == DELETE_EXPR
24303 || TREE_CODE (expression) == VEC_DELETE_EXPR
24304 || TREE_CODE (expression) == THROW_EXPR
24305 || TREE_CODE (expression) == REQUIRES_EXPR)
24306 return false;
24308 /* The types of these expressions depends only on the type to which
24309 the cast occurs. */
24310 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24311 || TREE_CODE (expression) == STATIC_CAST_EXPR
24312 || TREE_CODE (expression) == CONST_CAST_EXPR
24313 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24314 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24315 || TREE_CODE (expression) == CAST_EXPR)
24316 return dependent_type_p (TREE_TYPE (expression));
24318 /* The types of these expressions depends only on the type created
24319 by the expression. */
24320 if (TREE_CODE (expression) == NEW_EXPR
24321 || TREE_CODE (expression) == VEC_NEW_EXPR)
24323 /* For NEW_EXPR tree nodes created inside a template, either
24324 the object type itself or a TREE_LIST may appear as the
24325 operand 1. */
24326 tree type = TREE_OPERAND (expression, 1);
24327 if (TREE_CODE (type) == TREE_LIST)
24328 /* This is an array type. We need to check array dimensions
24329 as well. */
24330 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24331 || value_dependent_expression_p
24332 (TREE_OPERAND (TREE_VALUE (type), 1));
24333 else
24334 return dependent_type_p (type);
24337 if (TREE_CODE (expression) == SCOPE_REF)
24339 tree scope = TREE_OPERAND (expression, 0);
24340 tree name = TREE_OPERAND (expression, 1);
24342 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24343 contains an identifier associated by name lookup with one or more
24344 declarations declared with a dependent type, or...a
24345 nested-name-specifier or qualified-id that names a member of an
24346 unknown specialization. */
24347 return (type_dependent_expression_p (name)
24348 || dependent_scope_p (scope));
24351 if (TREE_CODE (expression) == TEMPLATE_DECL
24352 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24353 return uses_outer_template_parms (expression);
24355 if (TREE_CODE (expression) == STMT_EXPR)
24356 expression = stmt_expr_value_expr (expression);
24358 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24360 tree elt;
24361 unsigned i;
24363 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24365 if (type_dependent_expression_p (elt))
24366 return true;
24368 return false;
24371 /* A static data member of the current instantiation with incomplete
24372 array type is type-dependent, as the definition and specializations
24373 can have different bounds. */
24374 if (VAR_P (expression)
24375 && DECL_CLASS_SCOPE_P (expression)
24376 && dependent_type_p (DECL_CONTEXT (expression))
24377 && VAR_HAD_UNKNOWN_BOUND (expression))
24378 return true;
24380 /* An array of unknown bound depending on a variadic parameter, eg:
24382 template<typename... Args>
24383 void foo (Args... args)
24385 int arr[] = { args... };
24388 template<int... vals>
24389 void bar ()
24391 int arr[] = { vals... };
24394 If the array has no length and has an initializer, it must be that
24395 we couldn't determine its length in cp_complete_array_type because
24396 it is dependent. */
24397 if (VAR_P (expression)
24398 && TREE_TYPE (expression) != NULL_TREE
24399 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24400 && !TYPE_DOMAIN (TREE_TYPE (expression))
24401 && DECL_INITIAL (expression))
24402 return true;
24404 /* A function or variable template-id is type-dependent if it has any
24405 dependent template arguments. */
24406 if (VAR_OR_FUNCTION_DECL_P (expression)
24407 && DECL_LANG_SPECIFIC (expression)
24408 && DECL_TEMPLATE_INFO (expression))
24410 /* Consider the innermost template arguments, since those are the ones
24411 that come from the template-id; the template arguments for the
24412 enclosing class do not make it type-dependent unless they are used in
24413 the type of the decl. */
24414 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24415 && (any_dependent_template_arguments_p
24416 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24417 return true;
24420 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24421 type-dependent. Checking this is important for functions with auto return
24422 type, which looks like a dependent type. */
24423 if (TREE_CODE (expression) == FUNCTION_DECL
24424 && !(DECL_CLASS_SCOPE_P (expression)
24425 && dependent_type_p (DECL_CONTEXT (expression)))
24426 && !(DECL_FRIEND_P (expression)
24427 && (!DECL_FRIEND_CONTEXT (expression)
24428 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24429 && !DECL_LOCAL_FUNCTION_P (expression))
24431 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24432 || undeduced_auto_decl (expression));
24433 return false;
24436 /* Always dependent, on the number of arguments if nothing else. */
24437 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24438 return true;
24440 if (TREE_TYPE (expression) == unknown_type_node)
24442 if (TREE_CODE (expression) == ADDR_EXPR)
24443 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24444 if (TREE_CODE (expression) == COMPONENT_REF
24445 || TREE_CODE (expression) == OFFSET_REF)
24447 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24448 return true;
24449 expression = TREE_OPERAND (expression, 1);
24450 if (identifier_p (expression))
24451 return false;
24453 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24454 if (TREE_CODE (expression) == SCOPE_REF)
24455 return false;
24457 if (BASELINK_P (expression))
24459 if (BASELINK_OPTYPE (expression)
24460 && dependent_type_p (BASELINK_OPTYPE (expression)))
24461 return true;
24462 expression = BASELINK_FUNCTIONS (expression);
24465 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24467 if (any_dependent_template_arguments_p
24468 (TREE_OPERAND (expression, 1)))
24469 return true;
24470 expression = TREE_OPERAND (expression, 0);
24471 if (identifier_p (expression))
24472 return true;
24475 gcc_assert (TREE_CODE (expression) == OVERLOAD
24476 || TREE_CODE (expression) == FUNCTION_DECL);
24478 for (lkp_iterator iter (expression); iter; ++iter)
24479 if (type_dependent_expression_p (*iter))
24480 return true;
24482 return false;
24485 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24487 /* Dependent type attributes might not have made it from the decl to
24488 the type yet. */
24489 if (DECL_P (expression)
24490 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24491 return true;
24493 return (dependent_type_p (TREE_TYPE (expression)));
24496 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24497 type-dependent if the expression refers to a member of the current
24498 instantiation and the type of the referenced member is dependent, or the
24499 class member access expression refers to a member of an unknown
24500 specialization.
24502 This function returns true if the OBJECT in such a class member access
24503 expression is of an unknown specialization. */
24505 bool
24506 type_dependent_object_expression_p (tree object)
24508 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24509 dependent. */
24510 if (TREE_CODE (object) == IDENTIFIER_NODE)
24511 return true;
24512 tree scope = TREE_TYPE (object);
24513 return (!scope || dependent_scope_p (scope));
24516 /* walk_tree callback function for instantiation_dependent_expression_p,
24517 below. Returns non-zero if a dependent subexpression is found. */
24519 static tree
24520 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24521 void * /*data*/)
24523 if (TYPE_P (*tp))
24525 /* We don't have to worry about decltype currently because decltype
24526 of an instantiation-dependent expr is a dependent type. This
24527 might change depending on the resolution of DR 1172. */
24528 *walk_subtrees = false;
24529 return NULL_TREE;
24531 enum tree_code code = TREE_CODE (*tp);
24532 switch (code)
24534 /* Don't treat an argument list as dependent just because it has no
24535 TREE_TYPE. */
24536 case TREE_LIST:
24537 case TREE_VEC:
24538 return NULL_TREE;
24540 case TEMPLATE_PARM_INDEX:
24541 return *tp;
24543 /* Handle expressions with type operands. */
24544 case SIZEOF_EXPR:
24545 case ALIGNOF_EXPR:
24546 case TYPEID_EXPR:
24547 case AT_ENCODE_EXPR:
24549 tree op = TREE_OPERAND (*tp, 0);
24550 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24551 op = TREE_TYPE (op);
24552 if (TYPE_P (op))
24554 if (dependent_type_p (op))
24555 return *tp;
24556 else
24558 *walk_subtrees = false;
24559 return NULL_TREE;
24562 break;
24565 case COMPONENT_REF:
24566 if (identifier_p (TREE_OPERAND (*tp, 1)))
24567 /* In a template, finish_class_member_access_expr creates a
24568 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24569 type-dependent, so that we can check access control at
24570 instantiation time (PR 42277). See also Core issue 1273. */
24571 return *tp;
24572 break;
24574 case SCOPE_REF:
24575 if (instantiation_dependent_scope_ref_p (*tp))
24576 return *tp;
24577 else
24578 break;
24580 /* Treat statement-expressions as dependent. */
24581 case BIND_EXPR:
24582 return *tp;
24584 /* Treat requires-expressions as dependent. */
24585 case REQUIRES_EXPR:
24586 return *tp;
24588 case CALL_EXPR:
24589 /* Treat calls to function concepts as dependent. */
24590 if (function_concept_check_p (*tp))
24591 return *tp;
24592 break;
24594 case TEMPLATE_ID_EXPR:
24595 /* And variable concepts. */
24596 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24597 return *tp;
24598 break;
24600 default:
24601 break;
24604 if (type_dependent_expression_p (*tp))
24605 return *tp;
24606 else
24607 return NULL_TREE;
24610 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24611 sense defined by the ABI:
24613 "An expression is instantiation-dependent if it is type-dependent
24614 or value-dependent, or it has a subexpression that is type-dependent
24615 or value-dependent."
24617 Except don't actually check value-dependence for unevaluated expressions,
24618 because in sizeof(i) we don't care about the value of i. Checking
24619 type-dependence will in turn check value-dependence of array bounds/template
24620 arguments as needed. */
24622 bool
24623 instantiation_dependent_uneval_expression_p (tree expression)
24625 tree result;
24627 if (!processing_template_decl)
24628 return false;
24630 if (expression == error_mark_node)
24631 return false;
24633 result = cp_walk_tree_without_duplicates (&expression,
24634 instantiation_dependent_r, NULL);
24635 return result != NULL_TREE;
24638 /* As above, but also check value-dependence of the expression as a whole. */
24640 bool
24641 instantiation_dependent_expression_p (tree expression)
24643 return (instantiation_dependent_uneval_expression_p (expression)
24644 || value_dependent_expression_p (expression));
24647 /* Like type_dependent_expression_p, but it also works while not processing
24648 a template definition, i.e. during substitution or mangling. */
24650 bool
24651 type_dependent_expression_p_push (tree expr)
24653 bool b;
24654 ++processing_template_decl;
24655 b = type_dependent_expression_p (expr);
24656 --processing_template_decl;
24657 return b;
24660 /* Returns TRUE if ARGS contains a type-dependent expression. */
24662 bool
24663 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24665 unsigned int i;
24666 tree arg;
24668 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24670 if (type_dependent_expression_p (arg))
24671 return true;
24673 return false;
24676 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24677 expressions) contains any type-dependent expressions. */
24679 bool
24680 any_type_dependent_elements_p (const_tree list)
24682 for (; list; list = TREE_CHAIN (list))
24683 if (type_dependent_expression_p (TREE_VALUE (list)))
24684 return true;
24686 return false;
24689 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24690 expressions) contains any value-dependent expressions. */
24692 bool
24693 any_value_dependent_elements_p (const_tree list)
24695 for (; list; list = TREE_CHAIN (list))
24696 if (value_dependent_expression_p (TREE_VALUE (list)))
24697 return true;
24699 return false;
24702 /* Returns TRUE if the ARG (a template argument) is dependent. */
24704 bool
24705 dependent_template_arg_p (tree arg)
24707 if (!processing_template_decl)
24708 return false;
24710 /* Assume a template argument that was wrongly written by the user
24711 is dependent. This is consistent with what
24712 any_dependent_template_arguments_p [that calls this function]
24713 does. */
24714 if (!arg || arg == error_mark_node)
24715 return true;
24717 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24718 arg = argument_pack_select_arg (arg);
24720 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24721 return true;
24722 if (TREE_CODE (arg) == TEMPLATE_DECL)
24724 if (DECL_TEMPLATE_PARM_P (arg))
24725 return true;
24726 /* A member template of a dependent class is not necessarily
24727 type-dependent, but it is a dependent template argument because it
24728 will be a member of an unknown specialization to that template. */
24729 tree scope = CP_DECL_CONTEXT (arg);
24730 return TYPE_P (scope) && dependent_type_p (scope);
24732 else if (ARGUMENT_PACK_P (arg))
24734 tree args = ARGUMENT_PACK_ARGS (arg);
24735 int i, len = TREE_VEC_LENGTH (args);
24736 for (i = 0; i < len; ++i)
24738 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24739 return true;
24742 return false;
24744 else if (TYPE_P (arg))
24745 return dependent_type_p (arg);
24746 else
24747 return (type_dependent_expression_p (arg)
24748 || value_dependent_expression_p (arg));
24751 /* Returns true if ARGS (a collection of template arguments) contains
24752 any types that require structural equality testing. */
24754 bool
24755 any_template_arguments_need_structural_equality_p (tree args)
24757 int i;
24758 int j;
24760 if (!args)
24761 return false;
24762 if (args == error_mark_node)
24763 return true;
24765 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24767 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24768 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24770 tree arg = TREE_VEC_ELT (level, j);
24771 tree packed_args = NULL_TREE;
24772 int k, len = 1;
24774 if (ARGUMENT_PACK_P (arg))
24776 /* Look inside the argument pack. */
24777 packed_args = ARGUMENT_PACK_ARGS (arg);
24778 len = TREE_VEC_LENGTH (packed_args);
24781 for (k = 0; k < len; ++k)
24783 if (packed_args)
24784 arg = TREE_VEC_ELT (packed_args, k);
24786 if (error_operand_p (arg))
24787 return true;
24788 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24789 continue;
24790 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24791 return true;
24792 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24793 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24794 return true;
24799 return false;
24802 /* Returns true if ARGS (a collection of template arguments) contains
24803 any dependent arguments. */
24805 bool
24806 any_dependent_template_arguments_p (const_tree args)
24808 int i;
24809 int j;
24811 if (!args)
24812 return false;
24813 if (args == error_mark_node)
24814 return true;
24816 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24818 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24819 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24820 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24821 return true;
24824 return false;
24827 /* Returns TRUE if the template TMPL is type-dependent. */
24829 bool
24830 dependent_template_p (tree tmpl)
24832 if (TREE_CODE (tmpl) == OVERLOAD)
24834 for (lkp_iterator iter (tmpl); iter; ++iter)
24835 if (dependent_template_p (*iter))
24836 return true;
24837 return false;
24840 /* Template template parameters are dependent. */
24841 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24842 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24843 return true;
24844 /* So are names that have not been looked up. */
24845 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24846 return true;
24847 return false;
24850 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24852 bool
24853 dependent_template_id_p (tree tmpl, tree args)
24855 return (dependent_template_p (tmpl)
24856 || any_dependent_template_arguments_p (args));
24859 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24860 are dependent. */
24862 bool
24863 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24865 int i;
24867 if (!processing_template_decl)
24868 return false;
24870 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24872 tree decl = TREE_VEC_ELT (declv, i);
24873 tree init = TREE_VEC_ELT (initv, i);
24874 tree cond = TREE_VEC_ELT (condv, i);
24875 tree incr = TREE_VEC_ELT (incrv, i);
24877 if (type_dependent_expression_p (decl)
24878 || TREE_CODE (decl) == SCOPE_REF)
24879 return true;
24881 if (init && type_dependent_expression_p (init))
24882 return true;
24884 if (type_dependent_expression_p (cond))
24885 return true;
24887 if (COMPARISON_CLASS_P (cond)
24888 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24889 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24890 return true;
24892 if (TREE_CODE (incr) == MODOP_EXPR)
24894 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24895 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24896 return true;
24898 else if (type_dependent_expression_p (incr))
24899 return true;
24900 else if (TREE_CODE (incr) == MODIFY_EXPR)
24902 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24903 return true;
24904 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24906 tree t = TREE_OPERAND (incr, 1);
24907 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24908 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24909 return true;
24914 return false;
24917 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24918 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24919 no such TYPE can be found. Note that this function peers inside
24920 uninstantiated templates and therefore should be used only in
24921 extremely limited situations. ONLY_CURRENT_P restricts this
24922 peering to the currently open classes hierarchy (which is required
24923 when comparing types). */
24925 tree
24926 resolve_typename_type (tree type, bool only_current_p)
24928 tree scope;
24929 tree name;
24930 tree decl;
24931 int quals;
24932 tree pushed_scope;
24933 tree result;
24935 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24937 scope = TYPE_CONTEXT (type);
24938 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24939 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24940 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24941 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24942 identifier of the TYPENAME_TYPE anymore.
24943 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24944 TYPENAME_TYPE instead, we avoid messing up with a possible
24945 typedef variant case. */
24946 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24948 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24949 it first before we can figure out what NAME refers to. */
24950 if (TREE_CODE (scope) == TYPENAME_TYPE)
24952 if (TYPENAME_IS_RESOLVING_P (scope))
24953 /* Given a class template A with a dependent base with nested type C,
24954 typedef typename A::C::C C will land us here, as trying to resolve
24955 the initial A::C leads to the local C typedef, which leads back to
24956 A::C::C. So we break the recursion now. */
24957 return type;
24958 else
24959 scope = resolve_typename_type (scope, only_current_p);
24961 /* If we don't know what SCOPE refers to, then we cannot resolve the
24962 TYPENAME_TYPE. */
24963 if (!CLASS_TYPE_P (scope))
24964 return type;
24965 /* If this is a typedef, we don't want to look inside (c++/11987). */
24966 if (typedef_variant_p (type))
24967 return type;
24968 /* If SCOPE isn't the template itself, it will not have a valid
24969 TYPE_FIELDS list. */
24970 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24971 /* scope is either the template itself or a compatible instantiation
24972 like X<T>, so look up the name in the original template. */
24973 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24974 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24975 gcc_checking_assert (uses_template_parms (scope));
24976 /* If scope has no fields, it can't be a current instantiation. Check this
24977 before currently_open_class to avoid infinite recursion (71515). */
24978 if (!TYPE_FIELDS (scope))
24979 return type;
24980 /* If the SCOPE is not the current instantiation, there's no reason
24981 to look inside it. */
24982 if (only_current_p && !currently_open_class (scope))
24983 return type;
24984 /* Enter the SCOPE so that name lookup will be resolved as if we
24985 were in the class definition. In particular, SCOPE will no
24986 longer be considered a dependent type. */
24987 pushed_scope = push_scope (scope);
24988 /* Look up the declaration. */
24989 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24990 tf_warning_or_error);
24992 result = NULL_TREE;
24994 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24995 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24996 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24997 if (!decl)
24998 /*nop*/;
24999 else if (identifier_p (fullname)
25000 && TREE_CODE (decl) == TYPE_DECL)
25002 result = TREE_TYPE (decl);
25003 if (result == error_mark_node)
25004 result = NULL_TREE;
25006 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25007 && DECL_CLASS_TEMPLATE_P (decl))
25009 /* Obtain the template and the arguments. */
25010 tree tmpl = TREE_OPERAND (fullname, 0);
25011 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25013 /* We get here with a plain identifier because a previous tentative
25014 parse of the nested-name-specifier as part of a ptr-operator saw
25015 ::template X<A>. The use of ::template is necessary in a
25016 ptr-operator, but wrong in a declarator-id.
25018 [temp.names]: In a qualified-id of a declarator-id, the keyword
25019 template shall not appear at the top level. */
25020 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25021 "keyword %<template%> not allowed in declarator-id");
25022 tmpl = decl;
25024 tree args = TREE_OPERAND (fullname, 1);
25025 /* Instantiate the template. */
25026 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25027 /*entering_scope=*/true,
25028 tf_error | tf_user);
25029 if (result == error_mark_node)
25030 result = NULL_TREE;
25033 /* Leave the SCOPE. */
25034 if (pushed_scope)
25035 pop_scope (pushed_scope);
25037 /* If we failed to resolve it, return the original typename. */
25038 if (!result)
25039 return type;
25041 /* If lookup found a typename type, resolve that too. */
25042 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25044 /* Ill-formed programs can cause infinite recursion here, so we
25045 must catch that. */
25046 TYPENAME_IS_RESOLVING_P (result) = 1;
25047 result = resolve_typename_type (result, only_current_p);
25048 TYPENAME_IS_RESOLVING_P (result) = 0;
25051 /* Qualify the resulting type. */
25052 quals = cp_type_quals (type);
25053 if (quals)
25054 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25056 return result;
25059 /* EXPR is an expression which is not type-dependent. Return a proxy
25060 for EXPR that can be used to compute the types of larger
25061 expressions containing EXPR. */
25063 tree
25064 build_non_dependent_expr (tree expr)
25066 tree orig_expr = expr;
25067 tree inner_expr;
25069 /* When checking, try to get a constant value for all non-dependent
25070 expressions in order to expose bugs in *_dependent_expression_p
25071 and constexpr. This can affect code generation, see PR70704, so
25072 only do this for -fchecking=2. */
25073 if (flag_checking > 1
25074 && cxx_dialect >= cxx11
25075 /* Don't do this during nsdmi parsing as it can lead to
25076 unexpected recursive instantiations. */
25077 && !parsing_nsdmi ()
25078 /* Don't do this during concept expansion either and for
25079 the same reason. */
25080 && !expanding_concept ())
25081 fold_non_dependent_expr (expr);
25083 STRIP_ANY_LOCATION_WRAPPER (expr);
25085 /* Preserve OVERLOADs; the functions must be available to resolve
25086 types. */
25087 inner_expr = expr;
25088 if (TREE_CODE (inner_expr) == STMT_EXPR)
25089 inner_expr = stmt_expr_value_expr (inner_expr);
25090 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25091 inner_expr = TREE_OPERAND (inner_expr, 0);
25092 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25093 inner_expr = TREE_OPERAND (inner_expr, 1);
25094 if (is_overloaded_fn (inner_expr)
25095 || TREE_CODE (inner_expr) == OFFSET_REF)
25096 return orig_expr;
25097 /* There is no need to return a proxy for a variable. */
25098 if (VAR_P (expr))
25099 return orig_expr;
25100 /* Preserve string constants; conversions from string constants to
25101 "char *" are allowed, even though normally a "const char *"
25102 cannot be used to initialize a "char *". */
25103 if (TREE_CODE (expr) == STRING_CST)
25104 return orig_expr;
25105 /* Preserve void and arithmetic constants, as an optimization -- there is no
25106 reason to create a new node. */
25107 if (TREE_CODE (expr) == VOID_CST
25108 || TREE_CODE (expr) == INTEGER_CST
25109 || TREE_CODE (expr) == REAL_CST)
25110 return orig_expr;
25111 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25112 There is at least one place where we want to know that a
25113 particular expression is a throw-expression: when checking a ?:
25114 expression, there are special rules if the second or third
25115 argument is a throw-expression. */
25116 if (TREE_CODE (expr) == THROW_EXPR)
25117 return orig_expr;
25119 /* Don't wrap an initializer list, we need to be able to look inside. */
25120 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25121 return orig_expr;
25123 /* Don't wrap a dummy object, we need to be able to test for it. */
25124 if (is_dummy_object (expr))
25125 return orig_expr;
25127 if (TREE_CODE (expr) == COND_EXPR)
25128 return build3 (COND_EXPR,
25129 TREE_TYPE (expr),
25130 TREE_OPERAND (expr, 0),
25131 (TREE_OPERAND (expr, 1)
25132 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25133 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25134 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25135 if (TREE_CODE (expr) == COMPOUND_EXPR
25136 && !COMPOUND_EXPR_OVERLOADED (expr))
25137 return build2 (COMPOUND_EXPR,
25138 TREE_TYPE (expr),
25139 TREE_OPERAND (expr, 0),
25140 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25142 /* If the type is unknown, it can't really be non-dependent */
25143 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25145 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25146 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25149 /* ARGS is a vector of expressions as arguments to a function call.
25150 Replace the arguments with equivalent non-dependent expressions.
25151 This modifies ARGS in place. */
25153 void
25154 make_args_non_dependent (vec<tree, va_gc> *args)
25156 unsigned int ix;
25157 tree arg;
25159 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25161 tree newarg = build_non_dependent_expr (arg);
25162 if (newarg != arg)
25163 (*args)[ix] = newarg;
25167 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25168 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25169 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25171 static tree
25172 make_auto_1 (tree name, bool set_canonical)
25174 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25175 TYPE_NAME (au) = build_decl (input_location,
25176 TYPE_DECL, name, au);
25177 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25178 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25179 (0, processing_template_decl + 1, processing_template_decl + 1,
25180 TYPE_NAME (au), NULL_TREE);
25181 if (set_canonical)
25182 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25183 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25184 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25186 return au;
25189 tree
25190 make_decltype_auto (void)
25192 return make_auto_1 (decltype_auto_identifier, true);
25195 tree
25196 make_auto (void)
25198 return make_auto_1 (auto_identifier, true);
25201 /* Return a C++17 deduction placeholder for class template TMPL. */
25203 tree
25204 make_template_placeholder (tree tmpl)
25206 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25207 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25208 return t;
25211 /* True iff T is a C++17 class template deduction placeholder. */
25213 bool
25214 template_placeholder_p (tree t)
25216 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25219 /* Make a "constrained auto" type-specifier. This is an
25220 auto type with constraints that must be associated after
25221 deduction. The constraint is formed from the given
25222 CONC and its optional sequence of arguments, which are
25223 non-null if written as partial-concept-id. */
25225 tree
25226 make_constrained_auto (tree con, tree args)
25228 tree type = make_auto_1 (auto_identifier, false);
25230 /* Build the constraint. */
25231 tree tmpl = DECL_TI_TEMPLATE (con);
25232 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25233 expr = build_concept_check (expr, type, args);
25235 tree constr = normalize_expression (expr);
25236 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25238 /* Our canonical type depends on the constraint. */
25239 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25241 /* Attach the constraint to the type declaration. */
25242 tree decl = TYPE_NAME (type);
25243 return decl;
25246 /* Given type ARG, return std::initializer_list<ARG>. */
25248 static tree
25249 listify (tree arg)
25251 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25253 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25255 gcc_rich_location richloc (input_location);
25256 maybe_add_include_fixit (&richloc, "<initializer_list>");
25257 error_at (&richloc,
25258 "deducing from brace-enclosed initializer list"
25259 " requires %<#include <initializer_list>%>");
25261 return error_mark_node;
25263 tree argvec = make_tree_vec (1);
25264 TREE_VEC_ELT (argvec, 0) = arg;
25266 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25267 NULL_TREE, 0, tf_warning_or_error);
25270 /* Replace auto in TYPE with std::initializer_list<auto>. */
25272 static tree
25273 listify_autos (tree type, tree auto_node)
25275 tree init_auto = listify (auto_node);
25276 tree argvec = make_tree_vec (1);
25277 TREE_VEC_ELT (argvec, 0) = init_auto;
25278 if (processing_template_decl)
25279 argvec = add_to_template_args (current_template_args (), argvec);
25280 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25283 /* Hash traits for hashing possibly constrained 'auto'
25284 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25286 struct auto_hash : default_hash_traits<tree>
25288 static inline hashval_t hash (tree);
25289 static inline bool equal (tree, tree);
25292 /* Hash the 'auto' T. */
25294 inline hashval_t
25295 auto_hash::hash (tree t)
25297 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25298 /* Matching constrained-type-specifiers denote the same template
25299 parameter, so hash the constraint. */
25300 return hash_placeholder_constraint (c);
25301 else
25302 /* But unconstrained autos are all separate, so just hash the pointer. */
25303 return iterative_hash_object (t, 0);
25306 /* Compare two 'auto's. */
25308 inline bool
25309 auto_hash::equal (tree t1, tree t2)
25311 if (t1 == t2)
25312 return true;
25314 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25315 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25317 /* Two unconstrained autos are distinct. */
25318 if (!c1 || !c2)
25319 return false;
25321 return equivalent_placeholder_constraints (c1, c2);
25324 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25325 constrained) auto, add it to the vector. */
25327 static int
25328 extract_autos_r (tree t, void *data)
25330 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25331 if (is_auto (t))
25333 /* All the autos were built with index 0; fix that up now. */
25334 tree *p = hash.find_slot (t, INSERT);
25335 unsigned idx;
25336 if (*p)
25337 /* If this is a repeated constrained-type-specifier, use the index we
25338 chose before. */
25339 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25340 else
25342 /* Otherwise this is new, so use the current count. */
25343 *p = t;
25344 idx = hash.elements () - 1;
25346 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25349 /* Always keep walking. */
25350 return 0;
25353 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25354 says they can appear anywhere in the type. */
25356 static tree
25357 extract_autos (tree type)
25359 hash_set<tree> visited;
25360 hash_table<auto_hash> hash (2);
25362 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25364 tree tree_vec = make_tree_vec (hash.elements());
25365 for (hash_table<auto_hash>::iterator iter = hash.begin();
25366 iter != hash.end(); ++iter)
25368 tree elt = *iter;
25369 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25370 TREE_VEC_ELT (tree_vec, i)
25371 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25374 return tree_vec;
25377 /* The stem for deduction guide names. */
25378 const char *const dguide_base = "__dguide_";
25380 /* Return the name for a deduction guide for class template TMPL. */
25382 tree
25383 dguide_name (tree tmpl)
25385 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25386 tree tname = TYPE_IDENTIFIER (type);
25387 char *buf = (char *) alloca (1 + strlen (dguide_base)
25388 + IDENTIFIER_LENGTH (tname));
25389 memcpy (buf, dguide_base, strlen (dguide_base));
25390 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25391 IDENTIFIER_LENGTH (tname) + 1);
25392 tree dname = get_identifier (buf);
25393 TREE_TYPE (dname) = type;
25394 return dname;
25397 /* True if NAME is the name of a deduction guide. */
25399 bool
25400 dguide_name_p (tree name)
25402 return (TREE_TYPE (name)
25403 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25404 strlen (dguide_base)));
25407 /* True if FN is a deduction guide. */
25409 bool
25410 deduction_guide_p (const_tree fn)
25412 if (DECL_P (fn))
25413 if (tree name = DECL_NAME (fn))
25414 return dguide_name_p (name);
25415 return false;
25418 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25420 bool
25421 copy_guide_p (const_tree fn)
25423 gcc_assert (deduction_guide_p (fn));
25424 if (!DECL_ARTIFICIAL (fn))
25425 return false;
25426 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25427 return (TREE_CHAIN (parms) == void_list_node
25428 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25431 /* True if FN is a guide generated from a constructor template. */
25433 bool
25434 template_guide_p (const_tree fn)
25436 gcc_assert (deduction_guide_p (fn));
25437 if (!DECL_ARTIFICIAL (fn))
25438 return false;
25439 tree tmpl = DECL_TI_TEMPLATE (fn);
25440 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25441 return PRIMARY_TEMPLATE_P (org);
25442 return false;
25445 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25446 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25447 template parameter types. Note that the handling of template template
25448 parameters relies on current_template_parms being set appropriately for the
25449 new template. */
25451 static tree
25452 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25453 tree tsubst_args, tsubst_flags_t complain)
25455 tree oldidx = get_template_parm_index (olddecl);
25457 tree newtype;
25458 if (TREE_CODE (olddecl) == TYPE_DECL
25459 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25461 tree oldtype = TREE_TYPE (olddecl);
25462 newtype = cxx_make_type (TREE_CODE (oldtype));
25463 TYPE_MAIN_VARIANT (newtype) = newtype;
25464 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25465 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25466 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25468 else
25469 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25470 complain, NULL_TREE);
25472 tree newdecl
25473 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25474 DECL_NAME (olddecl), newtype);
25475 SET_DECL_TEMPLATE_PARM_P (newdecl);
25477 tree newidx;
25478 if (TREE_CODE (olddecl) == TYPE_DECL
25479 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25481 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25482 = build_template_parm_index (index, level, level,
25483 newdecl, newtype);
25484 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25485 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25486 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25487 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25489 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25491 DECL_TEMPLATE_RESULT (newdecl)
25492 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25493 DECL_NAME (olddecl), newtype);
25494 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25495 // First create a copy (ttargs) of tsubst_args with an
25496 // additional level for the template template parameter's own
25497 // template parameters (ttparms).
25498 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25499 (DECL_TEMPLATE_PARMS (olddecl)));
25500 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25501 tree ttargs = make_tree_vec (depth + 1);
25502 for (int i = 0; i < depth; ++i)
25503 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25504 TREE_VEC_ELT (ttargs, depth)
25505 = template_parms_level_to_args (ttparms);
25506 // Substitute ttargs into ttparms to fix references to
25507 // other template parameters.
25508 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25509 complain);
25510 // Now substitute again with args based on tparms, to reduce
25511 // the level of the ttparms.
25512 ttargs = current_template_args ();
25513 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25514 complain);
25515 // Finally, tack the adjusted parms onto tparms.
25516 ttparms = tree_cons (size_int (depth), ttparms,
25517 current_template_parms);
25518 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25521 else
25523 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25524 tree newconst
25525 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25526 TREE_CODE (oldconst),
25527 DECL_NAME (oldconst), newtype);
25528 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25529 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25530 SET_DECL_TEMPLATE_PARM_P (newconst);
25531 newidx = build_template_parm_index (index, level, level,
25532 newconst, newtype);
25533 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25534 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25535 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25538 return newdecl;
25541 /* Returns a C++17 class deduction guide template based on the constructor
25542 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25543 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25545 static tree
25546 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25548 tree type, tparms, targs, fparms, fargs, ci;
25549 bool memtmpl = false;
25550 bool explicit_p;
25551 location_t loc;
25552 tree fn_tmpl = NULL_TREE;
25554 if (TYPE_P (ctor))
25556 type = ctor;
25557 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25558 if (copy_p)
25560 type = TREE_TYPE (type);
25561 fparms = tree_cons (NULL_TREE, type, void_list_node);
25563 else
25564 fparms = void_list_node;
25566 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25567 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25568 targs = CLASSTYPE_TI_ARGS (type);
25569 ci = NULL_TREE;
25570 fargs = NULL_TREE;
25571 loc = DECL_SOURCE_LOCATION (ctmpl);
25572 explicit_p = false;
25574 else
25576 ++processing_template_decl;
25578 fn_tmpl
25579 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25580 : DECL_TI_TEMPLATE (ctor));
25581 if (outer_args)
25582 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25583 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25585 type = DECL_CONTEXT (ctor);
25587 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25588 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25589 fully specialized args for the enclosing class. Strip those off, as
25590 the deduction guide won't have those template parameters. */
25591 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25592 TMPL_PARMS_DEPTH (tparms));
25593 /* Discard the 'this' parameter. */
25594 fparms = FUNCTION_ARG_CHAIN (ctor);
25595 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25596 ci = get_constraints (ctor);
25597 loc = DECL_SOURCE_LOCATION (ctor);
25598 explicit_p = DECL_NONCONVERTING_P (ctor);
25600 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25602 memtmpl = true;
25604 /* For a member template constructor, we need to flatten the two
25605 template parameter lists into one, and then adjust the function
25606 signature accordingly. This gets...complicated. */
25607 tree save_parms = current_template_parms;
25609 /* For a member template we should have two levels of parms/args, one
25610 for the class and one for the constructor. We stripped
25611 specialized args for further enclosing classes above. */
25612 const int depth = 2;
25613 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25615 /* Template args for translating references to the two-level template
25616 parameters into references to the one-level template parameters we
25617 are creating. */
25618 tree tsubst_args = copy_node (targs);
25619 TMPL_ARGS_LEVEL (tsubst_args, depth)
25620 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25622 /* Template parms for the constructor template. */
25623 tree ftparms = TREE_VALUE (tparms);
25624 unsigned flen = TREE_VEC_LENGTH (ftparms);
25625 /* Template parms for the class template. */
25626 tparms = TREE_CHAIN (tparms);
25627 tree ctparms = TREE_VALUE (tparms);
25628 unsigned clen = TREE_VEC_LENGTH (ctparms);
25629 /* Template parms for the deduction guide start as a copy of the
25630 template parms for the class. We set current_template_parms for
25631 lookup_template_class_1. */
25632 current_template_parms = tparms = copy_node (tparms);
25633 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25634 for (unsigned i = 0; i < clen; ++i)
25635 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25637 /* Now we need to rewrite the constructor parms to append them to the
25638 class parms. */
25639 for (unsigned i = 0; i < flen; ++i)
25641 unsigned index = i + clen;
25642 unsigned level = 1;
25643 tree oldelt = TREE_VEC_ELT (ftparms, i);
25644 tree olddecl = TREE_VALUE (oldelt);
25645 tree newdecl = rewrite_template_parm (olddecl, index, level,
25646 tsubst_args, complain);
25647 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25648 tsubst_args, complain, ctor);
25649 tree list = build_tree_list (newdef, newdecl);
25650 TEMPLATE_PARM_CONSTRAINTS (list)
25651 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25652 tsubst_args, complain, ctor);
25653 TREE_VEC_ELT (new_vec, index) = list;
25654 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25657 /* Now we have a final set of template parms to substitute into the
25658 function signature. */
25659 targs = template_parms_to_args (tparms);
25660 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25661 complain, ctor);
25662 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25663 if (ci)
25664 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25666 current_template_parms = save_parms;
25668 --processing_template_decl;
25671 if (!memtmpl)
25673 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25674 tparms = copy_node (tparms);
25675 INNERMOST_TEMPLATE_PARMS (tparms)
25676 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25679 tree fntype = build_function_type (type, fparms);
25680 tree ded_fn = build_lang_decl_loc (loc,
25681 FUNCTION_DECL,
25682 dguide_name (type), fntype);
25683 DECL_ARGUMENTS (ded_fn) = fargs;
25684 DECL_ARTIFICIAL (ded_fn) = true;
25685 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25686 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25687 DECL_ARTIFICIAL (ded_tmpl) = true;
25688 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25689 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25690 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25691 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25692 if (DECL_P (ctor))
25693 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25694 if (ci)
25695 set_constraints (ded_tmpl, ci);
25697 return ded_tmpl;
25700 /* Deduce template arguments for the class template placeholder PTYPE for
25701 template TMPL based on the initializer INIT, and return the resulting
25702 type. */
25704 static tree
25705 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25706 tsubst_flags_t complain)
25708 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25710 /* We should have handled this in the caller. */
25711 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25712 return ptype;
25713 if (complain & tf_error)
25714 error ("non-class template %qT used without template arguments", tmpl);
25715 return error_mark_node;
25718 tree type = TREE_TYPE (tmpl);
25720 bool try_list_ctor = false;
25722 vec<tree,va_gc> *args;
25723 if (init == NULL_TREE
25724 || TREE_CODE (init) == TREE_LIST)
25725 args = make_tree_vector_from_list (init);
25726 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25728 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25729 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25731 /* As an exception, the first phase in 16.3.1.7 (considering the
25732 initializer list as a single argument) is omitted if the
25733 initializer list consists of a single expression of type cv U,
25734 where U is a specialization of C or a class derived from a
25735 specialization of C. */
25736 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25737 tree etype = TREE_TYPE (elt);
25739 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25740 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25741 int err = unify (tparms, targs, type, etype,
25742 UNIFY_ALLOW_DERIVED, /*explain*/false);
25743 if (err == 0)
25744 try_list_ctor = false;
25745 ggc_free (targs);
25747 if (try_list_ctor || is_std_init_list (type))
25748 args = make_tree_vector_single (init);
25749 else
25750 args = make_tree_vector_from_ctor (init);
25752 else
25753 args = make_tree_vector_single (init);
25755 tree dname = dguide_name (tmpl);
25756 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25757 /*type*/false, /*complain*/false,
25758 /*hidden*/false);
25759 bool elided = false;
25760 if (cands == error_mark_node)
25761 cands = NULL_TREE;
25763 /* Prune explicit deduction guides in copy-initialization context. */
25764 if (flags & LOOKUP_ONLYCONVERTING)
25766 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25767 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25768 elided = true;
25770 if (elided)
25772 /* Found a nonconverting guide, prune the candidates. */
25773 tree pruned = NULL_TREE;
25774 for (lkp_iterator iter (cands); iter; ++iter)
25775 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25776 pruned = lookup_add (*iter, pruned);
25778 cands = pruned;
25782 tree outer_args = NULL_TREE;
25783 if (DECL_CLASS_SCOPE_P (tmpl)
25784 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25786 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25787 type = TREE_TYPE (most_general_template (tmpl));
25790 bool saw_ctor = false;
25791 // FIXME cache artificial deduction guides
25792 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25794 tree guide = build_deduction_guide (*iter, outer_args, complain);
25795 if ((flags & LOOKUP_ONLYCONVERTING)
25796 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25797 elided = true;
25798 else
25799 cands = lookup_add (guide, cands);
25801 saw_ctor = true;
25804 tree call = error_mark_node;
25806 /* If this is list-initialization and the class has a list constructor, first
25807 try deducing from the list as a single argument, as [over.match.list]. */
25808 tree list_cands = NULL_TREE;
25809 if (try_list_ctor && cands)
25810 for (lkp_iterator iter (cands); iter; ++iter)
25812 tree dg = *iter;
25813 if (is_list_ctor (dg))
25814 list_cands = lookup_add (dg, list_cands);
25816 if (list_cands)
25818 ++cp_unevaluated_operand;
25819 call = build_new_function_call (list_cands, &args, tf_decltype);
25820 --cp_unevaluated_operand;
25822 if (call == error_mark_node)
25824 /* That didn't work, now try treating the list as a sequence of
25825 arguments. */
25826 release_tree_vector (args);
25827 args = make_tree_vector_from_ctor (init);
25831 /* Maybe generate an implicit deduction guide. */
25832 if (call == error_mark_node && args->length () < 2)
25834 tree gtype = NULL_TREE;
25836 if (args->length () == 1)
25837 /* Generate a copy guide. */
25838 gtype = build_reference_type (type);
25839 else if (!saw_ctor)
25840 /* Generate a default guide. */
25841 gtype = type;
25843 if (gtype)
25845 tree guide = build_deduction_guide (gtype, outer_args, complain);
25846 cands = lookup_add (guide, cands);
25850 if (elided && !cands)
25852 error ("cannot deduce template arguments for copy-initialization"
25853 " of %qT, as it has no non-explicit deduction guides or "
25854 "user-declared constructors", type);
25855 return error_mark_node;
25857 else if (!cands && call == error_mark_node)
25859 error ("cannot deduce template arguments of %qT, as it has no viable "
25860 "deduction guides", type);
25861 return error_mark_node;
25864 if (call == error_mark_node)
25866 ++cp_unevaluated_operand;
25867 call = build_new_function_call (cands, &args, tf_decltype);
25868 --cp_unevaluated_operand;
25871 if (call == error_mark_node && (complain & tf_warning_or_error))
25873 error ("class template argument deduction failed:");
25875 ++cp_unevaluated_operand;
25876 call = build_new_function_call (cands, &args, complain | tf_decltype);
25877 --cp_unevaluated_operand;
25879 if (elided)
25880 inform (input_location, "explicit deduction guides not considered "
25881 "for copy-initialization");
25884 release_tree_vector (args);
25886 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25889 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25890 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25892 tree
25893 do_auto_deduction (tree type, tree init, tree auto_node)
25895 return do_auto_deduction (type, init, auto_node,
25896 tf_warning_or_error,
25897 adc_unspecified);
25900 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25901 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25902 The CONTEXT determines the context in which auto deduction is performed
25903 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25904 OUTER_TARGS are used during template argument deduction
25905 (context == adc_unify) to properly substitute the result, and is ignored
25906 in other contexts.
25908 For partial-concept-ids, extra args may be appended to the list of deduced
25909 template arguments prior to determining constraint satisfaction. */
25911 tree
25912 do_auto_deduction (tree type, tree init, tree auto_node,
25913 tsubst_flags_t complain, auto_deduction_context context,
25914 tree outer_targs, int flags)
25916 tree targs;
25918 if (init == error_mark_node)
25919 return error_mark_node;
25921 if (init && type_dependent_expression_p (init)
25922 && context != adc_unify)
25923 /* Defining a subset of type-dependent expressions that we can deduce
25924 from ahead of time isn't worth the trouble. */
25925 return type;
25927 /* Similarly, we can't deduce from another undeduced decl. */
25928 if (init && undeduced_auto_decl (init))
25929 return type;
25931 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25932 /* C++17 class template argument deduction. */
25933 return do_class_deduction (type, tmpl, init, flags, complain);
25935 if (TREE_TYPE (init) == NULL_TREE)
25936 /* Nothing we can do with this, even in deduction context. */
25937 return type;
25939 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25940 with either a new invented type template parameter U or, if the
25941 initializer is a braced-init-list (8.5.4), with
25942 std::initializer_list<U>. */
25943 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25945 if (!DIRECT_LIST_INIT_P (init))
25946 type = listify_autos (type, auto_node);
25947 else if (CONSTRUCTOR_NELTS (init) == 1)
25948 init = CONSTRUCTOR_ELT (init, 0)->value;
25949 else
25951 if (complain & tf_warning_or_error)
25953 if (permerror (input_location, "direct-list-initialization of "
25954 "%<auto%> requires exactly one element"))
25955 inform (input_location,
25956 "for deduction to %<std::initializer_list%>, use copy-"
25957 "list-initialization (i.e. add %<=%> before the %<{%>)");
25959 type = listify_autos (type, auto_node);
25963 if (type == error_mark_node)
25964 return error_mark_node;
25966 init = resolve_nondeduced_context (init, complain);
25968 if (context == adc_decomp_type
25969 && auto_node == type
25970 && init != error_mark_node
25971 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25972 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25973 and initializer has array type, deduce cv-qualified array type. */
25974 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25975 complain);
25976 else if (AUTO_IS_DECLTYPE (auto_node))
25978 bool id = (DECL_P (init)
25979 || ((TREE_CODE (init) == COMPONENT_REF
25980 || TREE_CODE (init) == SCOPE_REF)
25981 && !REF_PARENTHESIZED_P (init)));
25982 targs = make_tree_vec (1);
25983 TREE_VEC_ELT (targs, 0)
25984 = finish_decltype_type (init, id, tf_warning_or_error);
25985 if (type != auto_node)
25987 if (complain & tf_error)
25988 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25989 return error_mark_node;
25992 else
25994 tree parms = build_tree_list (NULL_TREE, type);
25995 tree tparms;
25997 if (flag_concepts)
25998 tparms = extract_autos (type);
25999 else
26001 tparms = make_tree_vec (1);
26002 TREE_VEC_ELT (tparms, 0)
26003 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26006 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26007 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26008 DEDUCE_CALL, LOOKUP_NORMAL,
26009 NULL, /*explain_p=*/false);
26010 if (val > 0)
26012 if (processing_template_decl)
26013 /* Try again at instantiation time. */
26014 return type;
26015 if (type && type != error_mark_node
26016 && (complain & tf_error))
26017 /* If type is error_mark_node a diagnostic must have been
26018 emitted by now. Also, having a mention to '<type error>'
26019 in the diagnostic is not really useful to the user. */
26021 if (cfun && auto_node == current_function_auto_return_pattern
26022 && LAMBDA_FUNCTION_P (current_function_decl))
26023 error ("unable to deduce lambda return type from %qE", init);
26024 else
26025 error ("unable to deduce %qT from %qE", type, init);
26026 type_unification_real (tparms, targs, parms, &init, 1, 0,
26027 DEDUCE_CALL, LOOKUP_NORMAL,
26028 NULL, /*explain_p=*/true);
26030 return error_mark_node;
26034 /* Check any placeholder constraints against the deduced type. */
26035 if (flag_concepts && !processing_template_decl)
26036 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26038 /* Use the deduced type to check the associated constraints. If we
26039 have a partial-concept-id, rebuild the argument list so that
26040 we check using the extra arguments. */
26041 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26042 tree cargs = CHECK_CONSTR_ARGS (constr);
26043 if (TREE_VEC_LENGTH (cargs) > 1)
26045 cargs = copy_node (cargs);
26046 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26048 else
26049 cargs = targs;
26050 if (!constraints_satisfied_p (constr, cargs))
26052 if (complain & tf_warning_or_error)
26054 switch (context)
26056 case adc_unspecified:
26057 case adc_unify:
26058 error("placeholder constraints not satisfied");
26059 break;
26060 case adc_variable_type:
26061 case adc_decomp_type:
26062 error ("deduced initializer does not satisfy "
26063 "placeholder constraints");
26064 break;
26065 case adc_return_type:
26066 error ("deduced return type does not satisfy "
26067 "placeholder constraints");
26068 break;
26069 case adc_requirement:
26070 error ("deduced expression type does not satisfy "
26071 "placeholder constraints");
26072 break;
26074 diagnose_constraints (input_location, constr, targs);
26076 return error_mark_node;
26080 if (processing_template_decl && context != adc_unify)
26081 outer_targs = current_template_args ();
26082 targs = add_to_template_args (outer_targs, targs);
26083 return tsubst (type, targs, complain, NULL_TREE);
26086 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26087 result. */
26089 tree
26090 splice_late_return_type (tree type, tree late_return_type)
26092 if (is_auto (type))
26094 if (late_return_type)
26095 return late_return_type;
26097 tree idx = get_template_parm_index (type);
26098 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26099 /* In an abbreviated function template we didn't know we were dealing
26100 with a function template when we saw the auto return type, so update
26101 it to have the correct level. */
26102 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26104 return type;
26107 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26108 'decltype(auto)' or a deduced class template. */
26110 bool
26111 is_auto (const_tree type)
26113 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26114 && (TYPE_IDENTIFIER (type) == auto_identifier
26115 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26116 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26117 return true;
26118 else
26119 return false;
26122 /* for_each_template_parm callback for type_uses_auto. */
26125 is_auto_r (tree tp, void */*data*/)
26127 return is_auto (tp);
26130 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26131 a use of `auto'. Returns NULL_TREE otherwise. */
26133 tree
26134 type_uses_auto (tree type)
26136 if (type == NULL_TREE)
26137 return NULL_TREE;
26138 else if (flag_concepts)
26140 /* The Concepts TS allows multiple autos in one type-specifier; just
26141 return the first one we find, do_auto_deduction will collect all of
26142 them. */
26143 if (uses_template_parms (type))
26144 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26145 /*visited*/NULL, /*nondeduced*/true);
26146 else
26147 return NULL_TREE;
26149 else
26150 return find_type_usage (type, is_auto);
26153 /* For a given template T, return the vector of typedefs referenced
26154 in T for which access check is needed at T instantiation time.
26155 T is either a FUNCTION_DECL or a RECORD_TYPE.
26156 Those typedefs were added to T by the function
26157 append_type_to_template_for_access_check. */
26159 vec<qualified_typedef_usage_t, va_gc> *
26160 get_types_needing_access_check (tree t)
26162 tree ti;
26163 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26165 if (!t || t == error_mark_node)
26166 return NULL;
26168 if (!(ti = get_template_info (t)))
26169 return NULL;
26171 if (CLASS_TYPE_P (t)
26172 || TREE_CODE (t) == FUNCTION_DECL)
26174 if (!TI_TEMPLATE (ti))
26175 return NULL;
26177 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26180 return result;
26183 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26184 tied to T. That list of typedefs will be access checked at
26185 T instantiation time.
26186 T is either a FUNCTION_DECL or a RECORD_TYPE.
26187 TYPE_DECL is a TYPE_DECL node representing a typedef.
26188 SCOPE is the scope through which TYPE_DECL is accessed.
26189 LOCATION is the location of the usage point of TYPE_DECL.
26191 This function is a subroutine of
26192 append_type_to_template_for_access_check. */
26194 static void
26195 append_type_to_template_for_access_check_1 (tree t,
26196 tree type_decl,
26197 tree scope,
26198 location_t location)
26200 qualified_typedef_usage_t typedef_usage;
26201 tree ti;
26203 if (!t || t == error_mark_node)
26204 return;
26206 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26207 || CLASS_TYPE_P (t))
26208 && type_decl
26209 && TREE_CODE (type_decl) == TYPE_DECL
26210 && scope);
26212 if (!(ti = get_template_info (t)))
26213 return;
26215 gcc_assert (TI_TEMPLATE (ti));
26217 typedef_usage.typedef_decl = type_decl;
26218 typedef_usage.context = scope;
26219 typedef_usage.locus = location;
26221 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26224 /* Append TYPE_DECL to the template TEMPL.
26225 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26226 At TEMPL instanciation time, TYPE_DECL will be checked to see
26227 if it can be accessed through SCOPE.
26228 LOCATION is the location of the usage point of TYPE_DECL.
26230 e.g. consider the following code snippet:
26232 class C
26234 typedef int myint;
26237 template<class U> struct S
26239 C::myint mi; // <-- usage point of the typedef C::myint
26242 S<char> s;
26244 At S<char> instantiation time, we need to check the access of C::myint
26245 In other words, we need to check the access of the myint typedef through
26246 the C scope. For that purpose, this function will add the myint typedef
26247 and the scope C through which its being accessed to a list of typedefs
26248 tied to the template S. That list will be walked at template instantiation
26249 time and access check performed on each typedefs it contains.
26250 Note that this particular code snippet should yield an error because
26251 myint is private to C. */
26253 void
26254 append_type_to_template_for_access_check (tree templ,
26255 tree type_decl,
26256 tree scope,
26257 location_t location)
26259 qualified_typedef_usage_t *iter;
26260 unsigned i;
26262 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26264 /* Make sure we don't append the type to the template twice. */
26265 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26266 if (iter->typedef_decl == type_decl && scope == iter->context)
26267 return;
26269 append_type_to_template_for_access_check_1 (templ, type_decl,
26270 scope, location);
26273 /* Convert the generic type parameters in PARM that match the types given in the
26274 range [START_IDX, END_IDX) from the current_template_parms into generic type
26275 packs. */
26277 tree
26278 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26280 tree current = current_template_parms;
26281 int depth = TMPL_PARMS_DEPTH (current);
26282 current = INNERMOST_TEMPLATE_PARMS (current);
26283 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26285 for (int i = 0; i < start_idx; ++i)
26286 TREE_VEC_ELT (replacement, i)
26287 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26289 for (int i = start_idx; i < end_idx; ++i)
26291 /* Create a distinct parameter pack type from the current parm and add it
26292 to the replacement args to tsubst below into the generic function
26293 parameter. */
26295 tree o = TREE_TYPE (TREE_VALUE
26296 (TREE_VEC_ELT (current, i)));
26297 tree t = copy_type (o);
26298 TEMPLATE_TYPE_PARM_INDEX (t)
26299 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26300 o, 0, 0, tf_none);
26301 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26302 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26303 TYPE_MAIN_VARIANT (t) = t;
26304 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26305 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26306 TREE_VEC_ELT (replacement, i) = t;
26307 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26310 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26311 TREE_VEC_ELT (replacement, i)
26312 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26314 /* If there are more levels then build up the replacement with the outer
26315 template parms. */
26316 if (depth > 1)
26317 replacement = add_to_template_args (template_parms_to_args
26318 (TREE_CHAIN (current_template_parms)),
26319 replacement);
26321 return tsubst (parm, replacement, tf_none, NULL_TREE);
26324 /* Entries in the decl_constraint hash table. */
26325 struct GTY((for_user)) constr_entry
26327 tree decl;
26328 tree ci;
26331 /* Hashing function and equality for constraint entries. */
26332 struct constr_hasher : ggc_ptr_hash<constr_entry>
26334 static hashval_t hash (constr_entry *e)
26336 return (hashval_t)DECL_UID (e->decl);
26339 static bool equal (constr_entry *e1, constr_entry *e2)
26341 return e1->decl == e2->decl;
26345 /* A mapping from declarations to constraint information. Note that
26346 both templates and their underlying declarations are mapped to the
26347 same constraint information.
26349 FIXME: This is defined in pt.c because garbage collection
26350 code is not being generated for constraint.cc. */
26352 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26354 /* Returns the template constraints of declaration T. If T is not
26355 constrained, return NULL_TREE. Note that T must be non-null. */
26357 tree
26358 get_constraints (tree t)
26360 if (!flag_concepts)
26361 return NULL_TREE;
26363 gcc_assert (DECL_P (t));
26364 if (TREE_CODE (t) == TEMPLATE_DECL)
26365 t = DECL_TEMPLATE_RESULT (t);
26366 constr_entry elt = { t, NULL_TREE };
26367 constr_entry* found = decl_constraints->find (&elt);
26368 if (found)
26369 return found->ci;
26370 else
26371 return NULL_TREE;
26374 /* Associate the given constraint information CI with the declaration
26375 T. If T is a template, then the constraints are associated with
26376 its underlying declaration. Don't build associations if CI is
26377 NULL_TREE. */
26379 void
26380 set_constraints (tree t, tree ci)
26382 if (!ci)
26383 return;
26384 gcc_assert (t && flag_concepts);
26385 if (TREE_CODE (t) == TEMPLATE_DECL)
26386 t = DECL_TEMPLATE_RESULT (t);
26387 gcc_assert (!get_constraints (t));
26388 constr_entry elt = {t, ci};
26389 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26390 constr_entry* entry = ggc_alloc<constr_entry> ();
26391 *entry = elt;
26392 *slot = entry;
26395 /* Remove the associated constraints of the declaration T. */
26397 void
26398 remove_constraints (tree t)
26400 gcc_assert (DECL_P (t));
26401 if (TREE_CODE (t) == TEMPLATE_DECL)
26402 t = DECL_TEMPLATE_RESULT (t);
26404 constr_entry elt = {t, NULL_TREE};
26405 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26406 if (slot)
26407 decl_constraints->clear_slot (slot);
26410 /* Memoized satisfaction results for declarations. This
26411 maps the pair (constraint_info, arguments) to the result computed
26412 by constraints_satisfied_p. */
26414 struct GTY((for_user)) constraint_sat_entry
26416 tree ci;
26417 tree args;
26418 tree result;
26421 /* Hashing function and equality for constraint entries. */
26423 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26425 static hashval_t hash (constraint_sat_entry *e)
26427 hashval_t val = iterative_hash_object(e->ci, 0);
26428 return iterative_hash_template_arg (e->args, val);
26431 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26433 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26437 /* Memoized satisfaction results for concept checks. */
26439 struct GTY((for_user)) concept_spec_entry
26441 tree tmpl;
26442 tree args;
26443 tree result;
26446 /* Hashing function and equality for constraint entries. */
26448 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26450 static hashval_t hash (concept_spec_entry *e)
26452 return hash_tmpl_and_args (e->tmpl, e->args);
26455 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26457 ++comparing_specializations;
26458 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26459 --comparing_specializations;
26460 return eq;
26464 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26465 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26467 /* Search for a memoized satisfaction result. Returns one of the
26468 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26470 tree
26471 lookup_constraint_satisfaction (tree ci, tree args)
26473 constraint_sat_entry elt = { ci, args, NULL_TREE };
26474 constraint_sat_entry* found = constraint_memos->find (&elt);
26475 if (found)
26476 return found->result;
26477 else
26478 return NULL_TREE;
26481 /* Memoize the result of a satisfication test. Returns the saved result. */
26483 tree
26484 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26486 constraint_sat_entry elt = {ci, args, result};
26487 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26488 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26489 *entry = elt;
26490 *slot = entry;
26491 return result;
26494 /* Search for a memoized satisfaction result for a concept. */
26496 tree
26497 lookup_concept_satisfaction (tree tmpl, tree args)
26499 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26500 concept_spec_entry* found = concept_memos->find (&elt);
26501 if (found)
26502 return found->result;
26503 else
26504 return NULL_TREE;
26507 /* Memoize the result of a concept check. Returns the saved result. */
26509 tree
26510 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26512 concept_spec_entry elt = {tmpl, args, result};
26513 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26514 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26515 *entry = elt;
26516 *slot = entry;
26517 return result;
26520 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26522 /* Returns a prior concept specialization. This returns the substituted
26523 and normalized constraints defined by the concept. */
26525 tree
26526 get_concept_expansion (tree tmpl, tree args)
26528 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26529 concept_spec_entry* found = concept_expansions->find (&elt);
26530 if (found)
26531 return found->result;
26532 else
26533 return NULL_TREE;
26536 /* Save a concept expansion for later. */
26538 tree
26539 save_concept_expansion (tree tmpl, tree args, tree def)
26541 concept_spec_entry elt = {tmpl, args, def};
26542 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26543 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26544 *entry = elt;
26545 *slot = entry;
26546 return def;
26549 static hashval_t
26550 hash_subsumption_args (tree t1, tree t2)
26552 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26553 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26554 int val = 0;
26555 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26556 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26557 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26558 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26559 return val;
26562 /* Compare the constraints of two subsumption entries. The LEFT1 and
26563 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26564 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26566 static bool
26567 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26569 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26570 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26571 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26572 CHECK_CONSTR_ARGS (right1)))
26573 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26574 CHECK_CONSTR_ARGS (right2));
26575 return false;
26578 /* Key/value pair for learning and memoizing subsumption results. This
26579 associates a pair of check constraints (including arguments) with
26580 a boolean value indicating the result. */
26582 struct GTY((for_user)) subsumption_entry
26584 tree t1;
26585 tree t2;
26586 bool result;
26589 /* Hashing function and equality for constraint entries. */
26591 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26593 static hashval_t hash (subsumption_entry *e)
26595 return hash_subsumption_args (e->t1, e->t2);
26598 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26600 ++comparing_specializations;
26601 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26602 --comparing_specializations;
26603 return eq;
26607 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26609 /* Search for a previously cached subsumption result. */
26611 bool*
26612 lookup_subsumption_result (tree t1, tree t2)
26614 subsumption_entry elt = { t1, t2, false };
26615 subsumption_entry* found = subsumption_table->find (&elt);
26616 if (found)
26617 return &found->result;
26618 else
26619 return 0;
26622 /* Save a subsumption result. */
26624 bool
26625 save_subsumption_result (tree t1, tree t2, bool result)
26627 subsumption_entry elt = {t1, t2, result};
26628 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26629 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26630 *entry = elt;
26631 *slot = entry;
26632 return result;
26635 /* Set up the hash table for constraint association. */
26637 void
26638 init_constraint_processing (void)
26640 if (!flag_concepts)
26641 return;
26643 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26644 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26645 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26646 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26647 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26650 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26651 0..N-1. */
26653 void
26654 declare_integer_pack (void)
26656 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26657 build_function_type_list (integer_type_node,
26658 integer_type_node,
26659 NULL_TREE),
26660 NULL_TREE, ECF_CONST);
26661 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26662 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26665 /* Set up the hash tables for template instantiations. */
26667 void
26668 init_template_processing (void)
26670 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26671 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26673 if (cxx_dialect >= cxx11)
26674 declare_integer_pack ();
26677 /* Print stats about the template hash tables for -fstats. */
26679 void
26680 print_template_statistics (void)
26682 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26683 "%f collisions\n", (long) decl_specializations->size (),
26684 (long) decl_specializations->elements (),
26685 decl_specializations->collisions ());
26686 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26687 "%f collisions\n", (long) type_specializations->size (),
26688 (long) type_specializations->elements (),
26689 type_specializations->collisions ());
26692 #if CHECKING_P
26694 namespace selftest {
26696 /* Verify that build_non_dependent_expr () works, for various expressions,
26697 and that location wrappers don't affect the results. */
26699 static void
26700 test_build_non_dependent_expr ()
26702 location_t loc = BUILTINS_LOCATION;
26704 /* Verify constants, without and with location wrappers. */
26705 tree int_cst = build_int_cst (integer_type_node, 42);
26706 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26708 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26709 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26710 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26712 tree string_lit = build_string (4, "foo");
26713 TREE_TYPE (string_lit) = char_array_type_node;
26714 string_lit = fix_string_type (string_lit);
26715 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26717 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26718 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26719 ASSERT_EQ (wrapped_string_lit,
26720 build_non_dependent_expr (wrapped_string_lit));
26723 /* Verify that type_dependent_expression_p () works correctly, even
26724 in the presence of location wrapper nodes. */
26726 static void
26727 test_type_dependent_expression_p ()
26729 location_t loc = BUILTINS_LOCATION;
26731 tree name = get_identifier ("foo");
26733 /* If no templates are involved, nothing is type-dependent. */
26734 gcc_assert (!processing_template_decl);
26735 ASSERT_FALSE (type_dependent_expression_p (name));
26737 ++processing_template_decl;
26739 /* Within a template, an unresolved name is always type-dependent. */
26740 ASSERT_TRUE (type_dependent_expression_p (name));
26742 /* Ensure it copes with NULL_TREE and errors. */
26743 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26744 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26746 /* A USING_DECL in a template should be type-dependent, even if wrapped
26747 with a location wrapper (PR c++/83799). */
26748 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26749 TREE_TYPE (using_decl) = integer_type_node;
26750 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26751 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26752 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26753 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26755 --processing_template_decl;
26758 /* Run all of the selftests within this file. */
26760 void
26761 cp_pt_c_tests ()
26763 test_build_non_dependent_expr ();
26764 test_type_dependent_expression_p ();
26767 } // namespace selftest
26769 #endif /* #if CHECKING_P */
26771 #include "gt-cp-pt.h"