PR c++/82514 - ICE with local class in generic lambda.
[official-gcc.git] / gcc / cp / pt.c
blobde8ad94200a81061997a7376a2506a06cb5e2965
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 /* True iff FN is a function representing a built-in variadic parameter
3398 pack. */
3400 bool
3401 builtin_pack_fn_p (tree fn)
3403 if (!fn
3404 || TREE_CODE (fn) != FUNCTION_DECL
3405 || !DECL_IS_BUILTIN (fn))
3406 return false;
3408 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3409 return true;
3411 return false;
3414 /* True iff CALL is a call to a function representing a built-in variadic
3415 parameter pack. */
3417 static bool
3418 builtin_pack_call_p (tree call)
3420 if (TREE_CODE (call) != CALL_EXPR)
3421 return false;
3422 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3425 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3427 static tree
3428 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3429 tree in_decl)
3431 tree ohi = CALL_EXPR_ARG (call, 0);
3432 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3433 false/*fn*/, true/*int_cst*/);
3435 if (value_dependent_expression_p (hi))
3437 if (hi != ohi)
3439 call = copy_node (call);
3440 CALL_EXPR_ARG (call, 0) = hi;
3442 tree ex = make_pack_expansion (call, complain);
3443 tree vec = make_tree_vec (1);
3444 TREE_VEC_ELT (vec, 0) = ex;
3445 return vec;
3447 else
3449 hi = cxx_constant_value (hi);
3450 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3452 /* Calculate the largest value of len that won't make the size of the vec
3453 overflow an int. The compiler will exceed resource limits long before
3454 this, but it seems a decent place to diagnose. */
3455 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3457 if (len < 0 || len > max)
3459 if ((complain & tf_error)
3460 && hi != error_mark_node)
3461 error ("argument to __integer_pack must be between 0 and %d", max);
3462 return error_mark_node;
3465 tree vec = make_tree_vec (len);
3467 for (int i = 0; i < len; ++i)
3468 TREE_VEC_ELT (vec, i) = size_int (i);
3470 return vec;
3474 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3475 CALL. */
3477 static tree
3478 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3479 tree in_decl)
3481 if (!builtin_pack_call_p (call))
3482 return NULL_TREE;
3484 tree fn = CALL_EXPR_FN (call);
3486 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3487 return expand_integer_pack (call, args, complain, in_decl);
3489 return NULL_TREE;
3492 /* Structure used to track the progress of find_parameter_packs_r. */
3493 struct find_parameter_pack_data
3495 /* TREE_LIST that will contain all of the parameter packs found by
3496 the traversal. */
3497 tree* parameter_packs;
3499 /* Set of AST nodes that have been visited by the traversal. */
3500 hash_set<tree> *visited;
3502 /* True iff we're making a type pack expansion. */
3503 bool type_pack_expansion_p;
3506 /* Identifies all of the argument packs that occur in a template
3507 argument and appends them to the TREE_LIST inside DATA, which is a
3508 find_parameter_pack_data structure. This is a subroutine of
3509 make_pack_expansion and uses_parameter_packs. */
3510 static tree
3511 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3513 tree t = *tp;
3514 struct find_parameter_pack_data* ppd =
3515 (struct find_parameter_pack_data*)data;
3516 bool parameter_pack_p = false;
3518 /* Handle type aliases/typedefs. */
3519 if (TYPE_ALIAS_P (t))
3521 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3522 cp_walk_tree (&TI_ARGS (tinfo),
3523 &find_parameter_packs_r,
3524 ppd, ppd->visited);
3525 *walk_subtrees = 0;
3526 return NULL_TREE;
3529 /* Identify whether this is a parameter pack or not. */
3530 switch (TREE_CODE (t))
3532 case TEMPLATE_PARM_INDEX:
3533 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3534 parameter_pack_p = true;
3535 break;
3537 case TEMPLATE_TYPE_PARM:
3538 t = TYPE_MAIN_VARIANT (t);
3539 /* FALLTHRU */
3540 case TEMPLATE_TEMPLATE_PARM:
3541 /* If the placeholder appears in the decl-specifier-seq of a function
3542 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3543 is a pack expansion, the invented template parameter is a template
3544 parameter pack. */
3545 if (ppd->type_pack_expansion_p && is_auto (t))
3546 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3547 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3548 parameter_pack_p = true;
3549 break;
3551 case FIELD_DECL:
3552 case PARM_DECL:
3553 if (DECL_PACK_P (t))
3555 /* We don't want to walk into the type of a PARM_DECL,
3556 because we don't want to see the type parameter pack. */
3557 *walk_subtrees = 0;
3558 parameter_pack_p = true;
3560 break;
3562 /* Look through a lambda capture proxy to the field pack. */
3563 case VAR_DECL:
3564 if (DECL_HAS_VALUE_EXPR_P (t))
3566 tree v = DECL_VALUE_EXPR (t);
3567 cp_walk_tree (&v,
3568 &find_parameter_packs_r,
3569 ppd, ppd->visited);
3570 *walk_subtrees = 0;
3572 else if (variable_template_specialization_p (t))
3574 cp_walk_tree (&DECL_TI_ARGS (t),
3575 find_parameter_packs_r,
3576 ppd, ppd->visited);
3577 *walk_subtrees = 0;
3579 break;
3581 case CALL_EXPR:
3582 if (builtin_pack_call_p (t))
3583 parameter_pack_p = true;
3584 break;
3586 case BASES:
3587 parameter_pack_p = true;
3588 break;
3589 default:
3590 /* Not a parameter pack. */
3591 break;
3594 if (parameter_pack_p)
3596 /* Add this parameter pack to the list. */
3597 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3600 if (TYPE_P (t))
3601 cp_walk_tree (&TYPE_CONTEXT (t),
3602 &find_parameter_packs_r, ppd, ppd->visited);
3604 /* This switch statement will return immediately if we don't find a
3605 parameter pack. */
3606 switch (TREE_CODE (t))
3608 case TEMPLATE_PARM_INDEX:
3609 return NULL_TREE;
3611 case BOUND_TEMPLATE_TEMPLATE_PARM:
3612 /* Check the template itself. */
3613 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3614 &find_parameter_packs_r, ppd, ppd->visited);
3615 /* Check the template arguments. */
3616 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3617 ppd->visited);
3618 *walk_subtrees = 0;
3619 return NULL_TREE;
3621 case TEMPLATE_TYPE_PARM:
3622 case TEMPLATE_TEMPLATE_PARM:
3623 return NULL_TREE;
3625 case PARM_DECL:
3626 return NULL_TREE;
3628 case DECL_EXPR:
3629 /* Ignore the declaration of a capture proxy for a parameter pack. */
3630 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3631 *walk_subtrees = 0;
3632 return NULL_TREE;
3634 case RECORD_TYPE:
3635 if (TYPE_PTRMEMFUNC_P (t))
3636 return NULL_TREE;
3637 /* Fall through. */
3639 case UNION_TYPE:
3640 case ENUMERAL_TYPE:
3641 if (TYPE_TEMPLATE_INFO (t))
3642 cp_walk_tree (&TYPE_TI_ARGS (t),
3643 &find_parameter_packs_r, ppd, ppd->visited);
3645 *walk_subtrees = 0;
3646 return NULL_TREE;
3648 case TEMPLATE_DECL:
3649 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3650 return NULL_TREE;
3651 gcc_fallthrough();
3653 case CONSTRUCTOR:
3654 cp_walk_tree (&TREE_TYPE (t),
3655 &find_parameter_packs_r, ppd, ppd->visited);
3656 return NULL_TREE;
3658 case TYPENAME_TYPE:
3659 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3660 ppd, ppd->visited);
3661 *walk_subtrees = 0;
3662 return NULL_TREE;
3664 case TYPE_PACK_EXPANSION:
3665 case EXPR_PACK_EXPANSION:
3666 *walk_subtrees = 0;
3667 return NULL_TREE;
3669 case INTEGER_TYPE:
3670 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3671 ppd, ppd->visited);
3672 *walk_subtrees = 0;
3673 return NULL_TREE;
3675 case IDENTIFIER_NODE:
3676 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3677 ppd->visited);
3678 *walk_subtrees = 0;
3679 return NULL_TREE;
3681 case LAMBDA_EXPR:
3683 tree fn = lambda_function (t);
3684 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3685 ppd->visited);
3686 *walk_subtrees = 0;
3687 return NULL_TREE;
3690 case DECLTYPE_TYPE:
3692 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3693 type_pack_expansion_p to false so that any placeholders
3694 within the expression don't get marked as parameter packs. */
3695 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3696 ppd->type_pack_expansion_p = false;
3697 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3698 ppd, ppd->visited);
3699 ppd->type_pack_expansion_p = type_pack_expansion_p;
3700 *walk_subtrees = 0;
3701 return NULL_TREE;
3704 default:
3705 return NULL_TREE;
3708 return NULL_TREE;
3711 /* Determines if the expression or type T uses any parameter packs. */
3712 bool
3713 uses_parameter_packs (tree t)
3715 tree parameter_packs = NULL_TREE;
3716 struct find_parameter_pack_data ppd;
3717 ppd.parameter_packs = &parameter_packs;
3718 ppd.visited = new hash_set<tree>;
3719 ppd.type_pack_expansion_p = false;
3720 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3721 delete ppd.visited;
3722 return parameter_packs != NULL_TREE;
3725 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3726 representation a base-class initializer into a parameter pack
3727 expansion. If all goes well, the resulting node will be an
3728 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3729 respectively. */
3730 tree
3731 make_pack_expansion (tree arg, tsubst_flags_t complain)
3733 tree result;
3734 tree parameter_packs = NULL_TREE;
3735 bool for_types = false;
3736 struct find_parameter_pack_data ppd;
3738 if (!arg || arg == error_mark_node)
3739 return arg;
3741 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3743 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3744 class initializer. In this case, the TREE_PURPOSE will be a
3745 _TYPE node (representing the base class expansion we're
3746 initializing) and the TREE_VALUE will be a TREE_LIST
3747 containing the initialization arguments.
3749 The resulting expansion looks somewhat different from most
3750 expansions. Rather than returning just one _EXPANSION, we
3751 return a TREE_LIST whose TREE_PURPOSE is a
3752 TYPE_PACK_EXPANSION containing the bases that will be
3753 initialized. The TREE_VALUE will be identical to the
3754 original TREE_VALUE, which is a list of arguments that will
3755 be passed to each base. We do not introduce any new pack
3756 expansion nodes into the TREE_VALUE (although it is possible
3757 that some already exist), because the TREE_PURPOSE and
3758 TREE_VALUE all need to be expanded together with the same
3759 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3760 resulting TREE_PURPOSE will mention the parameter packs in
3761 both the bases and the arguments to the bases. */
3762 tree purpose;
3763 tree value;
3764 tree parameter_packs = NULL_TREE;
3766 /* Determine which parameter packs will be used by the base
3767 class expansion. */
3768 ppd.visited = new hash_set<tree>;
3769 ppd.parameter_packs = &parameter_packs;
3770 ppd.type_pack_expansion_p = true;
3771 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3772 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3773 &ppd, ppd.visited);
3775 if (parameter_packs == NULL_TREE)
3777 if (complain & tf_error)
3778 error ("base initializer expansion %qT contains no parameter packs",
3779 arg);
3780 delete ppd.visited;
3781 return error_mark_node;
3784 if (TREE_VALUE (arg) != void_type_node)
3786 /* Collect the sets of parameter packs used in each of the
3787 initialization arguments. */
3788 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3790 /* Determine which parameter packs will be expanded in this
3791 argument. */
3792 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3793 &ppd, ppd.visited);
3797 delete ppd.visited;
3799 /* Create the pack expansion type for the base type. */
3800 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3801 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3802 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3803 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3805 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3806 they will rarely be compared to anything. */
3807 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3809 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3812 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3813 for_types = true;
3815 /* Build the PACK_EXPANSION_* node. */
3816 result = for_types
3817 ? cxx_make_type (TYPE_PACK_EXPANSION)
3818 : make_node (EXPR_PACK_EXPANSION);
3819 SET_PACK_EXPANSION_PATTERN (result, arg);
3820 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3822 /* Propagate type and const-expression information. */
3823 TREE_TYPE (result) = TREE_TYPE (arg);
3824 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3825 /* Mark this read now, since the expansion might be length 0. */
3826 mark_exp_read (arg);
3828 else
3829 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3830 they will rarely be compared to anything. */
3831 SET_TYPE_STRUCTURAL_EQUALITY (result);
3833 /* Determine which parameter packs will be expanded. */
3834 ppd.parameter_packs = &parameter_packs;
3835 ppd.visited = new hash_set<tree>;
3836 ppd.type_pack_expansion_p = TYPE_P (arg);
3837 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3838 delete ppd.visited;
3840 /* Make sure we found some parameter packs. */
3841 if (parameter_packs == NULL_TREE)
3843 if (complain & tf_error)
3845 if (TYPE_P (arg))
3846 error ("expansion pattern %qT contains no argument packs", arg);
3847 else
3848 error ("expansion pattern %qE contains no argument packs", arg);
3850 return error_mark_node;
3852 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3854 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3856 return result;
3859 /* Checks T for any "bare" parameter packs, which have not yet been
3860 expanded, and issues an error if any are found. This operation can
3861 only be done on full expressions or types (e.g., an expression
3862 statement, "if" condition, etc.), because we could have expressions like:
3864 foo(f(g(h(args)))...)
3866 where "args" is a parameter pack. check_for_bare_parameter_packs
3867 should not be called for the subexpressions args, h(args),
3868 g(h(args)), or f(g(h(args))), because we would produce erroneous
3869 error messages.
3871 Returns TRUE and emits an error if there were bare parameter packs,
3872 returns FALSE otherwise. */
3873 bool
3874 check_for_bare_parameter_packs (tree t)
3876 tree parameter_packs = NULL_TREE;
3877 struct find_parameter_pack_data ppd;
3879 if (!processing_template_decl || !t || t == error_mark_node)
3880 return false;
3882 /* A lambda might use a parameter pack from the containing context. */
3883 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3884 return false;
3886 if (TREE_CODE (t) == TYPE_DECL)
3887 t = TREE_TYPE (t);
3889 ppd.parameter_packs = &parameter_packs;
3890 ppd.visited = new hash_set<tree>;
3891 ppd.type_pack_expansion_p = false;
3892 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3893 delete ppd.visited;
3895 if (parameter_packs)
3897 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3898 error_at (loc, "parameter packs not expanded with %<...%>:");
3899 while (parameter_packs)
3901 tree pack = TREE_VALUE (parameter_packs);
3902 tree name = NULL_TREE;
3904 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3905 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3906 name = TYPE_NAME (pack);
3907 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3908 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3909 else if (TREE_CODE (pack) == CALL_EXPR)
3910 name = DECL_NAME (CALL_EXPR_FN (pack));
3911 else
3912 name = DECL_NAME (pack);
3914 if (name)
3915 inform (loc, " %qD", name);
3916 else
3917 inform (loc, " <anonymous>");
3919 parameter_packs = TREE_CHAIN (parameter_packs);
3922 return true;
3925 return false;
3928 /* Expand any parameter packs that occur in the template arguments in
3929 ARGS. */
3930 tree
3931 expand_template_argument_pack (tree args)
3933 if (args == error_mark_node)
3934 return error_mark_node;
3936 tree result_args = NULL_TREE;
3937 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3938 int num_result_args = -1;
3939 int non_default_args_count = -1;
3941 /* First, determine if we need to expand anything, and the number of
3942 slots we'll need. */
3943 for (in_arg = 0; in_arg < nargs; ++in_arg)
3945 tree arg = TREE_VEC_ELT (args, in_arg);
3946 if (arg == NULL_TREE)
3947 return args;
3948 if (ARGUMENT_PACK_P (arg))
3950 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3951 if (num_result_args < 0)
3952 num_result_args = in_arg + num_packed;
3953 else
3954 num_result_args += num_packed;
3956 else
3958 if (num_result_args >= 0)
3959 num_result_args++;
3963 /* If no expansion is necessary, we're done. */
3964 if (num_result_args < 0)
3965 return args;
3967 /* Expand arguments. */
3968 result_args = make_tree_vec (num_result_args);
3969 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3970 non_default_args_count =
3971 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3972 for (in_arg = 0; in_arg < nargs; ++in_arg)
3974 tree arg = TREE_VEC_ELT (args, in_arg);
3975 if (ARGUMENT_PACK_P (arg))
3977 tree packed = ARGUMENT_PACK_ARGS (arg);
3978 int i, num_packed = TREE_VEC_LENGTH (packed);
3979 for (i = 0; i < num_packed; ++i, ++out_arg)
3980 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3981 if (non_default_args_count > 0)
3982 non_default_args_count += num_packed - 1;
3984 else
3986 TREE_VEC_ELT (result_args, out_arg) = arg;
3987 ++out_arg;
3990 if (non_default_args_count >= 0)
3991 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3992 return result_args;
3995 /* Checks if DECL shadows a template parameter.
3997 [temp.local]: A template-parameter shall not be redeclared within its
3998 scope (including nested scopes).
4000 Emits an error and returns TRUE if the DECL shadows a parameter,
4001 returns FALSE otherwise. */
4003 bool
4004 check_template_shadow (tree decl)
4006 tree olddecl;
4008 /* If we're not in a template, we can't possibly shadow a template
4009 parameter. */
4010 if (!current_template_parms)
4011 return true;
4013 /* Figure out what we're shadowing. */
4014 decl = OVL_FIRST (decl);
4015 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4017 /* If there's no previous binding for this name, we're not shadowing
4018 anything, let alone a template parameter. */
4019 if (!olddecl)
4020 return true;
4022 /* If we're not shadowing a template parameter, we're done. Note
4023 that OLDDECL might be an OVERLOAD (or perhaps even an
4024 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4025 node. */
4026 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4027 return true;
4029 /* We check for decl != olddecl to avoid bogus errors for using a
4030 name inside a class. We check TPFI to avoid duplicate errors for
4031 inline member templates. */
4032 if (decl == olddecl
4033 || (DECL_TEMPLATE_PARM_P (decl)
4034 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4035 return true;
4037 /* Don't complain about the injected class name, as we've already
4038 complained about the class itself. */
4039 if (DECL_SELF_REFERENCE_P (decl))
4040 return false;
4042 if (DECL_TEMPLATE_PARM_P (decl))
4043 error ("declaration of template parameter %q+D shadows "
4044 "template parameter", decl);
4045 else
4046 error ("declaration of %q+#D shadows template parameter", decl);
4047 inform (DECL_SOURCE_LOCATION (olddecl),
4048 "template parameter %qD declared here", olddecl);
4049 return false;
4052 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4053 ORIG_LEVEL, DECL, and TYPE. */
4055 static tree
4056 build_template_parm_index (int index,
4057 int level,
4058 int orig_level,
4059 tree decl,
4060 tree type)
4062 tree t = make_node (TEMPLATE_PARM_INDEX);
4063 TEMPLATE_PARM_IDX (t) = index;
4064 TEMPLATE_PARM_LEVEL (t) = level;
4065 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4066 TEMPLATE_PARM_DECL (t) = decl;
4067 TREE_TYPE (t) = type;
4068 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4069 TREE_READONLY (t) = TREE_READONLY (decl);
4071 return t;
4074 /* Find the canonical type parameter for the given template type
4075 parameter. Returns the canonical type parameter, which may be TYPE
4076 if no such parameter existed. */
4078 static tree
4079 canonical_type_parameter (tree type)
4081 tree list;
4082 int idx = TEMPLATE_TYPE_IDX (type);
4083 if (!canonical_template_parms)
4084 vec_alloc (canonical_template_parms, idx + 1);
4086 if (canonical_template_parms->length () <= (unsigned) idx)
4087 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4089 list = (*canonical_template_parms)[idx];
4090 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4091 list = TREE_CHAIN (list);
4093 if (list)
4094 return TREE_VALUE (list);
4095 else
4097 (*canonical_template_parms)[idx]
4098 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4099 return type;
4103 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4104 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4105 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4106 new one is created. */
4108 static tree
4109 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4110 tsubst_flags_t complain)
4112 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4113 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4114 != TEMPLATE_PARM_LEVEL (index) - levels)
4115 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4117 tree orig_decl = TEMPLATE_PARM_DECL (index);
4118 tree decl, t;
4120 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4121 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4122 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4123 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4124 DECL_ARTIFICIAL (decl) = 1;
4125 SET_DECL_TEMPLATE_PARM_P (decl);
4127 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4128 TEMPLATE_PARM_LEVEL (index) - levels,
4129 TEMPLATE_PARM_ORIG_LEVEL (index),
4130 decl, type);
4131 TEMPLATE_PARM_DESCENDANTS (index) = t;
4132 TEMPLATE_PARM_PARAMETER_PACK (t)
4133 = TEMPLATE_PARM_PARAMETER_PACK (index);
4135 /* Template template parameters need this. */
4136 if (TREE_CODE (decl) == TEMPLATE_DECL)
4138 DECL_TEMPLATE_RESULT (decl)
4139 = build_decl (DECL_SOURCE_LOCATION (decl),
4140 TYPE_DECL, DECL_NAME (decl), type);
4141 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4142 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4143 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4147 return TEMPLATE_PARM_DESCENDANTS (index);
4150 /* Process information from new template parameter PARM and append it
4151 to the LIST being built. This new parameter is a non-type
4152 parameter iff IS_NON_TYPE is true. This new parameter is a
4153 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4154 is in PARM_LOC. */
4156 tree
4157 process_template_parm (tree list, location_t parm_loc, tree parm,
4158 bool is_non_type, bool is_parameter_pack)
4160 tree decl = 0;
4161 int idx = 0;
4163 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4164 tree defval = TREE_PURPOSE (parm);
4165 tree constr = TREE_TYPE (parm);
4167 if (list)
4169 tree p = tree_last (list);
4171 if (p && TREE_VALUE (p) != error_mark_node)
4173 p = TREE_VALUE (p);
4174 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4175 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4176 else
4177 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4180 ++idx;
4183 if (is_non_type)
4185 parm = TREE_VALUE (parm);
4187 SET_DECL_TEMPLATE_PARM_P (parm);
4189 if (TREE_TYPE (parm) != error_mark_node)
4191 /* [temp.param]
4193 The top-level cv-qualifiers on the template-parameter are
4194 ignored when determining its type. */
4195 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4196 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4197 TREE_TYPE (parm) = error_mark_node;
4198 else if (uses_parameter_packs (TREE_TYPE (parm))
4199 && !is_parameter_pack
4200 /* If we're in a nested template parameter list, the template
4201 template parameter could be a parameter pack. */
4202 && processing_template_parmlist == 1)
4204 /* This template parameter is not a parameter pack, but it
4205 should be. Complain about "bare" parameter packs. */
4206 check_for_bare_parameter_packs (TREE_TYPE (parm));
4208 /* Recover by calling this a parameter pack. */
4209 is_parameter_pack = true;
4213 /* A template parameter is not modifiable. */
4214 TREE_CONSTANT (parm) = 1;
4215 TREE_READONLY (parm) = 1;
4216 decl = build_decl (parm_loc,
4217 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4218 TREE_CONSTANT (decl) = 1;
4219 TREE_READONLY (decl) = 1;
4220 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4221 = build_template_parm_index (idx, processing_template_decl,
4222 processing_template_decl,
4223 decl, TREE_TYPE (parm));
4225 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4226 = is_parameter_pack;
4228 else
4230 tree t;
4231 parm = TREE_VALUE (TREE_VALUE (parm));
4233 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4235 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4236 /* This is for distinguishing between real templates and template
4237 template parameters */
4238 TREE_TYPE (parm) = t;
4239 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4240 decl = parm;
4242 else
4244 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4245 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4246 decl = build_decl (parm_loc,
4247 TYPE_DECL, parm, t);
4250 TYPE_NAME (t) = decl;
4251 TYPE_STUB_DECL (t) = decl;
4252 parm = decl;
4253 TEMPLATE_TYPE_PARM_INDEX (t)
4254 = build_template_parm_index (idx, processing_template_decl,
4255 processing_template_decl,
4256 decl, TREE_TYPE (parm));
4257 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4258 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4260 DECL_ARTIFICIAL (decl) = 1;
4261 SET_DECL_TEMPLATE_PARM_P (decl);
4263 /* Build requirements for the type/template parameter.
4264 This must be done after SET_DECL_TEMPLATE_PARM_P or
4265 process_template_parm could fail. */
4266 tree reqs = finish_shorthand_constraint (parm, constr);
4268 pushdecl (decl);
4270 /* Build the parameter node linking the parameter declaration,
4271 its default argument (if any), and its constraints (if any). */
4272 parm = build_tree_list (defval, parm);
4273 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4275 return chainon (list, parm);
4278 /* The end of a template parameter list has been reached. Process the
4279 tree list into a parameter vector, converting each parameter into a more
4280 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4281 as PARM_DECLs. */
4283 tree
4284 end_template_parm_list (tree parms)
4286 int nparms;
4287 tree parm, next;
4288 tree saved_parmlist = make_tree_vec (list_length (parms));
4290 /* Pop the dummy parameter level and add the real one. */
4291 current_template_parms = TREE_CHAIN (current_template_parms);
4293 current_template_parms
4294 = tree_cons (size_int (processing_template_decl),
4295 saved_parmlist, current_template_parms);
4297 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4299 next = TREE_CHAIN (parm);
4300 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4301 TREE_CHAIN (parm) = NULL_TREE;
4304 --processing_template_parmlist;
4306 return saved_parmlist;
4309 // Explicitly indicate the end of the template parameter list. We assume
4310 // that the current template parameters have been constructed and/or
4311 // managed explicitly, as when creating new template template parameters
4312 // from a shorthand constraint.
4313 void
4314 end_template_parm_list ()
4316 --processing_template_parmlist;
4319 /* end_template_decl is called after a template declaration is seen. */
4321 void
4322 end_template_decl (void)
4324 reset_specialization ();
4326 if (! processing_template_decl)
4327 return;
4329 /* This matches the pushlevel in begin_template_parm_list. */
4330 finish_scope ();
4332 --processing_template_decl;
4333 current_template_parms = TREE_CHAIN (current_template_parms);
4336 /* Takes a TREE_LIST representing a template parameter and convert it
4337 into an argument suitable to be passed to the type substitution
4338 functions. Note that If the TREE_LIST contains an error_mark
4339 node, the returned argument is error_mark_node. */
4341 tree
4342 template_parm_to_arg (tree t)
4345 if (t == NULL_TREE
4346 || TREE_CODE (t) != TREE_LIST)
4347 return t;
4349 if (error_operand_p (TREE_VALUE (t)))
4350 return error_mark_node;
4352 t = TREE_VALUE (t);
4354 if (TREE_CODE (t) == TYPE_DECL
4355 || TREE_CODE (t) == TEMPLATE_DECL)
4357 t = TREE_TYPE (t);
4359 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4361 /* Turn this argument into a TYPE_ARGUMENT_PACK
4362 with a single element, which expands T. */
4363 tree vec = make_tree_vec (1);
4364 if (CHECKING_P)
4365 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4367 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4369 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4370 SET_ARGUMENT_PACK_ARGS (t, vec);
4373 else
4375 t = DECL_INITIAL (t);
4377 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4379 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4380 with a single element, which expands T. */
4381 tree vec = make_tree_vec (1);
4382 if (CHECKING_P)
4383 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4385 t = convert_from_reference (t);
4386 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4388 t = make_node (NONTYPE_ARGUMENT_PACK);
4389 SET_ARGUMENT_PACK_ARGS (t, vec);
4391 else
4392 t = convert_from_reference (t);
4394 return t;
4397 /* Given a single level of template parameters (a TREE_VEC), return it
4398 as a set of template arguments. */
4400 static tree
4401 template_parms_level_to_args (tree parms)
4403 tree a = copy_node (parms);
4404 TREE_TYPE (a) = NULL_TREE;
4405 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4406 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4408 if (CHECKING_P)
4409 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4411 return a;
4414 /* Given a set of template parameters, return them as a set of template
4415 arguments. The template parameters are represented as a TREE_VEC, in
4416 the form documented in cp-tree.h for template arguments. */
4418 static tree
4419 template_parms_to_args (tree parms)
4421 tree header;
4422 tree args = NULL_TREE;
4423 int length = TMPL_PARMS_DEPTH (parms);
4424 int l = length;
4426 /* If there is only one level of template parameters, we do not
4427 create a TREE_VEC of TREE_VECs. Instead, we return a single
4428 TREE_VEC containing the arguments. */
4429 if (length > 1)
4430 args = make_tree_vec (length);
4432 for (header = parms; header; header = TREE_CHAIN (header))
4434 tree a = template_parms_level_to_args (TREE_VALUE (header));
4436 if (length > 1)
4437 TREE_VEC_ELT (args, --l) = a;
4438 else
4439 args = a;
4442 return args;
4445 /* Within the declaration of a template, return the currently active
4446 template parameters as an argument TREE_VEC. */
4448 static tree
4449 current_template_args (void)
4451 return template_parms_to_args (current_template_parms);
4454 /* Update the declared TYPE by doing any lookups which were thought to be
4455 dependent, but are not now that we know the SCOPE of the declarator. */
4457 tree
4458 maybe_update_decl_type (tree orig_type, tree scope)
4460 tree type = orig_type;
4462 if (type == NULL_TREE)
4463 return type;
4465 if (TREE_CODE (orig_type) == TYPE_DECL)
4466 type = TREE_TYPE (type);
4468 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4469 && dependent_type_p (type)
4470 /* Don't bother building up the args in this case. */
4471 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4473 /* tsubst in the args corresponding to the template parameters,
4474 including auto if present. Most things will be unchanged, but
4475 make_typename_type and tsubst_qualified_id will resolve
4476 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4477 tree args = current_template_args ();
4478 tree auto_node = type_uses_auto (type);
4479 tree pushed;
4480 if (auto_node)
4482 tree auto_vec = make_tree_vec (1);
4483 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4484 args = add_to_template_args (args, auto_vec);
4486 pushed = push_scope (scope);
4487 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4488 if (pushed)
4489 pop_scope (scope);
4492 if (type == error_mark_node)
4493 return orig_type;
4495 if (TREE_CODE (orig_type) == TYPE_DECL)
4497 if (same_type_p (type, TREE_TYPE (orig_type)))
4498 type = orig_type;
4499 else
4500 type = TYPE_NAME (type);
4502 return type;
4505 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4506 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4507 the new template is a member template. */
4509 tree
4510 build_template_decl (tree decl, tree parms, bool member_template_p)
4512 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4513 DECL_TEMPLATE_PARMS (tmpl) = parms;
4514 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4515 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4516 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4518 return tmpl;
4521 struct template_parm_data
4523 /* The level of the template parameters we are currently
4524 processing. */
4525 int level;
4527 /* The index of the specialization argument we are currently
4528 processing. */
4529 int current_arg;
4531 /* An array whose size is the number of template parameters. The
4532 elements are nonzero if the parameter has been used in any one
4533 of the arguments processed so far. */
4534 int* parms;
4536 /* An array whose size is the number of template arguments. The
4537 elements are nonzero if the argument makes use of template
4538 parameters of this level. */
4539 int* arg_uses_template_parms;
4542 /* Subroutine of push_template_decl used to see if each template
4543 parameter in a partial specialization is used in the explicit
4544 argument list. If T is of the LEVEL given in DATA (which is
4545 treated as a template_parm_data*), then DATA->PARMS is marked
4546 appropriately. */
4548 static int
4549 mark_template_parm (tree t, void* data)
4551 int level;
4552 int idx;
4553 struct template_parm_data* tpd = (struct template_parm_data*) data;
4555 template_parm_level_and_index (t, &level, &idx);
4557 if (level == tpd->level)
4559 tpd->parms[idx] = 1;
4560 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4563 /* In C++17 the type of a non-type argument is a deduced context. */
4564 if (cxx_dialect >= cxx17
4565 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4566 for_each_template_parm (TREE_TYPE (t),
4567 &mark_template_parm,
4568 data,
4569 NULL,
4570 /*include_nondeduced_p=*/false);
4572 /* Return zero so that for_each_template_parm will continue the
4573 traversal of the tree; we want to mark *every* template parm. */
4574 return 0;
4577 /* Process the partial specialization DECL. */
4579 static tree
4580 process_partial_specialization (tree decl)
4582 tree type = TREE_TYPE (decl);
4583 tree tinfo = get_template_info (decl);
4584 tree maintmpl = TI_TEMPLATE (tinfo);
4585 tree specargs = TI_ARGS (tinfo);
4586 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4587 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4588 tree inner_parms;
4589 tree inst;
4590 int nargs = TREE_VEC_LENGTH (inner_args);
4591 int ntparms;
4592 int i;
4593 bool did_error_intro = false;
4594 struct template_parm_data tpd;
4595 struct template_parm_data tpd2;
4597 gcc_assert (current_template_parms);
4599 /* A concept cannot be specialized. */
4600 if (flag_concepts && variable_concept_p (maintmpl))
4602 error ("specialization of variable concept %q#D", maintmpl);
4603 return error_mark_node;
4606 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4607 ntparms = TREE_VEC_LENGTH (inner_parms);
4609 /* We check that each of the template parameters given in the
4610 partial specialization is used in the argument list to the
4611 specialization. For example:
4613 template <class T> struct S;
4614 template <class T> struct S<T*>;
4616 The second declaration is OK because `T*' uses the template
4617 parameter T, whereas
4619 template <class T> struct S<int>;
4621 is no good. Even trickier is:
4623 template <class T>
4624 struct S1
4626 template <class U>
4627 struct S2;
4628 template <class U>
4629 struct S2<T>;
4632 The S2<T> declaration is actually invalid; it is a
4633 full-specialization. Of course,
4635 template <class U>
4636 struct S2<T (*)(U)>;
4638 or some such would have been OK. */
4639 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4640 tpd.parms = XALLOCAVEC (int, ntparms);
4641 memset (tpd.parms, 0, sizeof (int) * ntparms);
4643 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4644 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4645 for (i = 0; i < nargs; ++i)
4647 tpd.current_arg = i;
4648 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4649 &mark_template_parm,
4650 &tpd,
4651 NULL,
4652 /*include_nondeduced_p=*/false);
4654 for (i = 0; i < ntparms; ++i)
4655 if (tpd.parms[i] == 0)
4657 /* One of the template parms was not used in a deduced context in the
4658 specialization. */
4659 if (!did_error_intro)
4661 error ("template parameters not deducible in "
4662 "partial specialization:");
4663 did_error_intro = true;
4666 inform (input_location, " %qD",
4667 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4670 if (did_error_intro)
4671 return error_mark_node;
4673 /* [temp.class.spec]
4675 The argument list of the specialization shall not be identical to
4676 the implicit argument list of the primary template. */
4677 tree main_args
4678 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4679 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4680 && (!flag_concepts
4681 || !strictly_subsumes (current_template_constraints (),
4682 get_constraints (maintmpl))))
4684 if (!flag_concepts)
4685 error ("partial specialization %q+D does not specialize "
4686 "any template arguments", decl);
4687 else
4688 error ("partial specialization %q+D does not specialize any "
4689 "template arguments and is not more constrained than", decl);
4690 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4693 /* A partial specialization that replaces multiple parameters of the
4694 primary template with a pack expansion is less specialized for those
4695 parameters. */
4696 if (nargs < DECL_NTPARMS (maintmpl))
4698 error ("partial specialization is not more specialized than the "
4699 "primary template because it replaces multiple parameters "
4700 "with a pack expansion");
4701 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4702 /* Avoid crash in process_partial_specialization. */
4703 return decl;
4706 /* If we aren't in a dependent class, we can actually try deduction. */
4707 else if (tpd.level == 1
4708 /* FIXME we should be able to handle a partial specialization of a
4709 partial instantiation, but currently we can't (c++/41727). */
4710 && TMPL_ARGS_DEPTH (specargs) == 1
4711 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4713 if (permerror (input_location, "partial specialization %qD is not "
4714 "more specialized than", decl))
4715 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4716 maintmpl);
4719 /* [temp.class.spec]
4721 A partially specialized non-type argument expression shall not
4722 involve template parameters of the partial specialization except
4723 when the argument expression is a simple identifier.
4725 The type of a template parameter corresponding to a specialized
4726 non-type argument shall not be dependent on a parameter of the
4727 specialization.
4729 Also, we verify that pack expansions only occur at the
4730 end of the argument list. */
4731 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4732 tpd2.parms = 0;
4733 for (i = 0; i < nargs; ++i)
4735 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4736 tree arg = TREE_VEC_ELT (inner_args, i);
4737 tree packed_args = NULL_TREE;
4738 int j, len = 1;
4740 if (ARGUMENT_PACK_P (arg))
4742 /* Extract the arguments from the argument pack. We'll be
4743 iterating over these in the following loop. */
4744 packed_args = ARGUMENT_PACK_ARGS (arg);
4745 len = TREE_VEC_LENGTH (packed_args);
4748 for (j = 0; j < len; j++)
4750 if (packed_args)
4751 /* Get the Jth argument in the parameter pack. */
4752 arg = TREE_VEC_ELT (packed_args, j);
4754 if (PACK_EXPANSION_P (arg))
4756 /* Pack expansions must come at the end of the
4757 argument list. */
4758 if ((packed_args && j < len - 1)
4759 || (!packed_args && i < nargs - 1))
4761 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4762 error ("parameter pack argument %qE must be at the "
4763 "end of the template argument list", arg);
4764 else
4765 error ("parameter pack argument %qT must be at the "
4766 "end of the template argument list", arg);
4770 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4771 /* We only care about the pattern. */
4772 arg = PACK_EXPANSION_PATTERN (arg);
4774 if (/* These first two lines are the `non-type' bit. */
4775 !TYPE_P (arg)
4776 && TREE_CODE (arg) != TEMPLATE_DECL
4777 /* This next two lines are the `argument expression is not just a
4778 simple identifier' condition and also the `specialized
4779 non-type argument' bit. */
4780 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4781 && !(REFERENCE_REF_P (arg)
4782 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4784 if ((!packed_args && tpd.arg_uses_template_parms[i])
4785 || (packed_args && uses_template_parms (arg)))
4786 error ("template argument %qE involves template parameter(s)",
4787 arg);
4788 else
4790 /* Look at the corresponding template parameter,
4791 marking which template parameters its type depends
4792 upon. */
4793 tree type = TREE_TYPE (parm);
4795 if (!tpd2.parms)
4797 /* We haven't yet initialized TPD2. Do so now. */
4798 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4799 /* The number of parameters here is the number in the
4800 main template, which, as checked in the assertion
4801 above, is NARGS. */
4802 tpd2.parms = XALLOCAVEC (int, nargs);
4803 tpd2.level =
4804 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4807 /* Mark the template parameters. But this time, we're
4808 looking for the template parameters of the main
4809 template, not in the specialization. */
4810 tpd2.current_arg = i;
4811 tpd2.arg_uses_template_parms[i] = 0;
4812 memset (tpd2.parms, 0, sizeof (int) * nargs);
4813 for_each_template_parm (type,
4814 &mark_template_parm,
4815 &tpd2,
4816 NULL,
4817 /*include_nondeduced_p=*/false);
4819 if (tpd2.arg_uses_template_parms [i])
4821 /* The type depended on some template parameters.
4822 If they are fully specialized in the
4823 specialization, that's OK. */
4824 int j;
4825 int count = 0;
4826 for (j = 0; j < nargs; ++j)
4827 if (tpd2.parms[j] != 0
4828 && tpd.arg_uses_template_parms [j])
4829 ++count;
4830 if (count != 0)
4831 error_n (input_location, count,
4832 "type %qT of template argument %qE depends "
4833 "on a template parameter",
4834 "type %qT of template argument %qE depends "
4835 "on template parameters",
4836 type,
4837 arg);
4844 /* We should only get here once. */
4845 if (TREE_CODE (decl) == TYPE_DECL)
4846 gcc_assert (!COMPLETE_TYPE_P (type));
4848 // Build the template decl.
4849 tree tmpl = build_template_decl (decl, current_template_parms,
4850 DECL_MEMBER_TEMPLATE_P (maintmpl));
4851 TREE_TYPE (tmpl) = type;
4852 DECL_TEMPLATE_RESULT (tmpl) = decl;
4853 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4854 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4855 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4857 /* Give template template parms a DECL_CONTEXT of the template
4858 for which they are a parameter. */
4859 for (i = 0; i < ntparms; ++i)
4861 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4862 if (TREE_CODE (parm) == TEMPLATE_DECL)
4863 DECL_CONTEXT (parm) = tmpl;
4866 if (VAR_P (decl))
4867 /* We didn't register this in check_explicit_specialization so we could
4868 wait until the constraints were set. */
4869 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4870 else
4871 associate_classtype_constraints (type);
4873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4874 = tree_cons (specargs, tmpl,
4875 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4876 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4878 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4879 inst = TREE_CHAIN (inst))
4881 tree instance = TREE_VALUE (inst);
4882 if (TYPE_P (instance)
4883 ? (COMPLETE_TYPE_P (instance)
4884 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4885 : DECL_TEMPLATE_INSTANTIATION (instance))
4887 tree spec = most_specialized_partial_spec (instance, tf_none);
4888 tree inst_decl = (DECL_P (instance)
4889 ? instance : TYPE_NAME (instance));
4890 if (!spec)
4891 /* OK */;
4892 else if (spec == error_mark_node)
4893 permerror (input_location,
4894 "declaration of %qD ambiguates earlier template "
4895 "instantiation for %qD", decl, inst_decl);
4896 else if (TREE_VALUE (spec) == tmpl)
4897 permerror (input_location,
4898 "partial specialization of %qD after instantiation "
4899 "of %qD", decl, inst_decl);
4903 return decl;
4906 /* PARM is a template parameter of some form; return the corresponding
4907 TEMPLATE_PARM_INDEX. */
4909 static tree
4910 get_template_parm_index (tree parm)
4912 if (TREE_CODE (parm) == PARM_DECL
4913 || TREE_CODE (parm) == CONST_DECL)
4914 parm = DECL_INITIAL (parm);
4915 else if (TREE_CODE (parm) == TYPE_DECL
4916 || TREE_CODE (parm) == TEMPLATE_DECL)
4917 parm = TREE_TYPE (parm);
4918 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4919 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4920 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4921 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4922 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4923 return parm;
4926 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4927 parameter packs used by the template parameter PARM. */
4929 static void
4930 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4932 /* A type parm can't refer to another parm. */
4933 if (TREE_CODE (parm) == TYPE_DECL)
4934 return;
4935 else if (TREE_CODE (parm) == PARM_DECL)
4937 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4938 ppd, ppd->visited);
4939 return;
4942 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4944 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4945 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4946 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4949 /* PARM is a template parameter pack. Return any parameter packs used in
4950 its type or the type of any of its template parameters. If there are
4951 any such packs, it will be instantiated into a fixed template parameter
4952 list by partial instantiation rather than be fully deduced. */
4954 tree
4955 fixed_parameter_pack_p (tree parm)
4957 /* This can only be true in a member template. */
4958 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4959 return NULL_TREE;
4960 /* This can only be true for a parameter pack. */
4961 if (!template_parameter_pack_p (parm))
4962 return NULL_TREE;
4963 /* A type parm can't refer to another parm. */
4964 if (TREE_CODE (parm) == TYPE_DECL)
4965 return NULL_TREE;
4967 tree parameter_packs = NULL_TREE;
4968 struct find_parameter_pack_data ppd;
4969 ppd.parameter_packs = &parameter_packs;
4970 ppd.visited = new hash_set<tree>;
4971 ppd.type_pack_expansion_p = false;
4973 fixed_parameter_pack_p_1 (parm, &ppd);
4975 delete ppd.visited;
4976 return parameter_packs;
4979 /* Check that a template declaration's use of default arguments and
4980 parameter packs is not invalid. Here, PARMS are the template
4981 parameters. IS_PRIMARY is true if DECL is the thing declared by
4982 a primary template. IS_PARTIAL is true if DECL is a partial
4983 specialization.
4985 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
4986 function template declaration or a friend class template
4987 declaration. In the function case, 1 indicates a declaration, 2
4988 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4989 emitted for extraneous default arguments.
4991 Returns TRUE if there were no errors found, FALSE otherwise. */
4993 bool
4994 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4995 bool is_partial, int is_friend_decl)
4997 const char *msg;
4998 int last_level_to_check;
4999 tree parm_level;
5000 bool no_errors = true;
5002 /* [temp.param]
5004 A default template-argument shall not be specified in a
5005 function template declaration or a function template definition, nor
5006 in the template-parameter-list of the definition of a member of a
5007 class template. */
5009 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5010 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5011 /* You can't have a function template declaration in a local
5012 scope, nor you can you define a member of a class template in a
5013 local scope. */
5014 return true;
5016 if ((TREE_CODE (decl) == TYPE_DECL
5017 && TREE_TYPE (decl)
5018 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5019 || (TREE_CODE (decl) == FUNCTION_DECL
5020 && LAMBDA_FUNCTION_P (decl)))
5021 /* A lambda doesn't have an explicit declaration; don't complain
5022 about the parms of the enclosing class. */
5023 return true;
5025 if (current_class_type
5026 && !TYPE_BEING_DEFINED (current_class_type)
5027 && DECL_LANG_SPECIFIC (decl)
5028 && DECL_DECLARES_FUNCTION_P (decl)
5029 /* If this is either a friend defined in the scope of the class
5030 or a member function. */
5031 && (DECL_FUNCTION_MEMBER_P (decl)
5032 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5033 : DECL_FRIEND_CONTEXT (decl)
5034 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5035 : false)
5036 /* And, if it was a member function, it really was defined in
5037 the scope of the class. */
5038 && (!DECL_FUNCTION_MEMBER_P (decl)
5039 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5040 /* We already checked these parameters when the template was
5041 declared, so there's no need to do it again now. This function
5042 was defined in class scope, but we're processing its body now
5043 that the class is complete. */
5044 return true;
5046 /* Core issue 226 (C++0x only): the following only applies to class
5047 templates. */
5048 if (is_primary
5049 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5051 /* [temp.param]
5053 If a template-parameter has a default template-argument, all
5054 subsequent template-parameters shall have a default
5055 template-argument supplied. */
5056 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5058 tree inner_parms = TREE_VALUE (parm_level);
5059 int ntparms = TREE_VEC_LENGTH (inner_parms);
5060 int seen_def_arg_p = 0;
5061 int i;
5063 for (i = 0; i < ntparms; ++i)
5065 tree parm = TREE_VEC_ELT (inner_parms, i);
5067 if (parm == error_mark_node)
5068 continue;
5070 if (TREE_PURPOSE (parm))
5071 seen_def_arg_p = 1;
5072 else if (seen_def_arg_p
5073 && !template_parameter_pack_p (TREE_VALUE (parm)))
5075 error ("no default argument for %qD", TREE_VALUE (parm));
5076 /* For better subsequent error-recovery, we indicate that
5077 there should have been a default argument. */
5078 TREE_PURPOSE (parm) = error_mark_node;
5079 no_errors = false;
5081 else if (!is_partial
5082 && !is_friend_decl
5083 /* Don't complain about an enclosing partial
5084 specialization. */
5085 && parm_level == parms
5086 && TREE_CODE (decl) == TYPE_DECL
5087 && i < ntparms - 1
5088 && template_parameter_pack_p (TREE_VALUE (parm))
5089 /* A fixed parameter pack will be partially
5090 instantiated into a fixed length list. */
5091 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5093 /* A primary class template can only have one
5094 parameter pack, at the end of the template
5095 parameter list. */
5097 error ("parameter pack %q+D must be at the end of the"
5098 " template parameter list", TREE_VALUE (parm));
5100 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5101 = error_mark_node;
5102 no_errors = false;
5108 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5109 || is_partial
5110 || !is_primary
5111 || is_friend_decl)
5112 /* For an ordinary class template, default template arguments are
5113 allowed at the innermost level, e.g.:
5114 template <class T = int>
5115 struct S {};
5116 but, in a partial specialization, they're not allowed even
5117 there, as we have in [temp.class.spec]:
5119 The template parameter list of a specialization shall not
5120 contain default template argument values.
5122 So, for a partial specialization, or for a function template
5123 (in C++98/C++03), we look at all of them. */
5125 else
5126 /* But, for a primary class template that is not a partial
5127 specialization we look at all template parameters except the
5128 innermost ones. */
5129 parms = TREE_CHAIN (parms);
5131 /* Figure out what error message to issue. */
5132 if (is_friend_decl == 2)
5133 msg = G_("default template arguments may not be used in function template "
5134 "friend re-declaration");
5135 else if (is_friend_decl)
5136 msg = G_("default template arguments may not be used in template "
5137 "friend declarations");
5138 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5139 msg = G_("default template arguments may not be used in function templates "
5140 "without -std=c++11 or -std=gnu++11");
5141 else if (is_partial)
5142 msg = G_("default template arguments may not be used in "
5143 "partial specializations");
5144 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5145 msg = G_("default argument for template parameter for class enclosing %qD");
5146 else
5147 /* Per [temp.param]/9, "A default template-argument shall not be
5148 specified in the template-parameter-lists of the definition of
5149 a member of a class template that appears outside of the member's
5150 class.", thus if we aren't handling a member of a class template
5151 there is no need to examine the parameters. */
5152 return true;
5154 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5155 /* If we're inside a class definition, there's no need to
5156 examine the parameters to the class itself. On the one
5157 hand, they will be checked when the class is defined, and,
5158 on the other, default arguments are valid in things like:
5159 template <class T = double>
5160 struct S { template <class U> void f(U); };
5161 Here the default argument for `S' has no bearing on the
5162 declaration of `f'. */
5163 last_level_to_check = template_class_depth (current_class_type) + 1;
5164 else
5165 /* Check everything. */
5166 last_level_to_check = 0;
5168 for (parm_level = parms;
5169 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5170 parm_level = TREE_CHAIN (parm_level))
5172 tree inner_parms = TREE_VALUE (parm_level);
5173 int i;
5174 int ntparms;
5176 ntparms = TREE_VEC_LENGTH (inner_parms);
5177 for (i = 0; i < ntparms; ++i)
5179 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5180 continue;
5182 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5184 if (msg)
5186 no_errors = false;
5187 if (is_friend_decl == 2)
5188 return no_errors;
5190 error (msg, decl);
5191 msg = 0;
5194 /* Clear out the default argument so that we are not
5195 confused later. */
5196 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5200 /* At this point, if we're still interested in issuing messages,
5201 they must apply to classes surrounding the object declared. */
5202 if (msg)
5203 msg = G_("default argument for template parameter for class "
5204 "enclosing %qD");
5207 return no_errors;
5210 /* Worker for push_template_decl_real, called via
5211 for_each_template_parm. DATA is really an int, indicating the
5212 level of the parameters we are interested in. If T is a template
5213 parameter of that level, return nonzero. */
5215 static int
5216 template_parm_this_level_p (tree t, void* data)
5218 int this_level = *(int *)data;
5219 int level;
5221 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5222 level = TEMPLATE_PARM_LEVEL (t);
5223 else
5224 level = TEMPLATE_TYPE_LEVEL (t);
5225 return level == this_level;
5228 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5229 DATA is really an int, indicating the innermost outer level of parameters.
5230 If T is a template parameter of that level or further out, return
5231 nonzero. */
5233 static int
5234 template_parm_outer_level (tree t, void *data)
5236 int this_level = *(int *)data;
5237 int level;
5239 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5240 level = TEMPLATE_PARM_LEVEL (t);
5241 else
5242 level = TEMPLATE_TYPE_LEVEL (t);
5243 return level <= this_level;
5246 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5247 parameters given by current_template_args, or reuses a
5248 previously existing one, if appropriate. Returns the DECL, or an
5249 equivalent one, if it is replaced via a call to duplicate_decls.
5251 If IS_FRIEND is true, DECL is a friend declaration. */
5253 tree
5254 push_template_decl_real (tree decl, bool is_friend)
5256 tree tmpl;
5257 tree args;
5258 tree info;
5259 tree ctx;
5260 bool is_primary;
5261 bool is_partial;
5262 int new_template_p = 0;
5263 /* True if the template is a member template, in the sense of
5264 [temp.mem]. */
5265 bool member_template_p = false;
5267 if (decl == error_mark_node || !current_template_parms)
5268 return error_mark_node;
5270 /* See if this is a partial specialization. */
5271 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5272 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5273 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5274 || (VAR_P (decl)
5275 && DECL_LANG_SPECIFIC (decl)
5276 && DECL_TEMPLATE_SPECIALIZATION (decl)
5277 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5279 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5280 is_friend = true;
5282 if (is_friend)
5283 /* For a friend, we want the context of the friend, not
5284 the type of which it is a friend. */
5285 ctx = CP_DECL_CONTEXT (decl);
5286 else if (CP_DECL_CONTEXT (decl)
5287 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5288 /* In the case of a virtual function, we want the class in which
5289 it is defined. */
5290 ctx = CP_DECL_CONTEXT (decl);
5291 else
5292 /* Otherwise, if we're currently defining some class, the DECL
5293 is assumed to be a member of the class. */
5294 ctx = current_scope ();
5296 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5297 ctx = NULL_TREE;
5299 if (!DECL_CONTEXT (decl))
5300 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5302 /* See if this is a primary template. */
5303 if (is_friend && ctx
5304 && uses_template_parms_level (ctx, processing_template_decl))
5305 /* A friend template that specifies a class context, i.e.
5306 template <typename T> friend void A<T>::f();
5307 is not primary. */
5308 is_primary = false;
5309 else if (TREE_CODE (decl) == TYPE_DECL
5310 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5311 is_primary = false;
5312 else
5313 is_primary = template_parm_scope_p ();
5315 if (is_primary)
5317 warning (OPT_Wtemplates, "template %qD declared", decl);
5319 if (DECL_CLASS_SCOPE_P (decl))
5320 member_template_p = true;
5321 if (TREE_CODE (decl) == TYPE_DECL
5322 && anon_aggrname_p (DECL_NAME (decl)))
5324 error ("template class without a name");
5325 return error_mark_node;
5327 else if (TREE_CODE (decl) == FUNCTION_DECL)
5329 if (member_template_p)
5331 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5332 error ("member template %qD may not have virt-specifiers", decl);
5334 if (DECL_DESTRUCTOR_P (decl))
5336 /* [temp.mem]
5338 A destructor shall not be a member template. */
5339 error ("destructor %qD declared as member template", decl);
5340 return error_mark_node;
5342 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5343 && (!prototype_p (TREE_TYPE (decl))
5344 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5345 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5346 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5347 == void_list_node)))
5349 /* [basic.stc.dynamic.allocation]
5351 An allocation function can be a function
5352 template. ... Template allocation functions shall
5353 have two or more parameters. */
5354 error ("invalid template declaration of %qD", decl);
5355 return error_mark_node;
5358 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5359 && CLASS_TYPE_P (TREE_TYPE (decl)))
5361 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5362 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5363 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5365 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5366 if (TREE_CODE (t) == TYPE_DECL)
5367 t = TREE_TYPE (t);
5368 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5369 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5372 else if (TREE_CODE (decl) == TYPE_DECL
5373 && TYPE_DECL_ALIAS_P (decl))
5374 /* alias-declaration */
5375 gcc_assert (!DECL_ARTIFICIAL (decl));
5376 else if (VAR_P (decl))
5377 /* C++14 variable template. */;
5378 else
5380 error ("template declaration of %q#D", decl);
5381 return error_mark_node;
5385 /* Check to see that the rules regarding the use of default
5386 arguments are not being violated. We check args for a friend
5387 functions when we know whether it's a definition, introducing
5388 declaration or re-declaration. */
5389 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5390 check_default_tmpl_args (decl, current_template_parms,
5391 is_primary, is_partial, is_friend);
5393 /* Ensure that there are no parameter packs in the type of this
5394 declaration that have not been expanded. */
5395 if (TREE_CODE (decl) == FUNCTION_DECL)
5397 /* Check each of the arguments individually to see if there are
5398 any bare parameter packs. */
5399 tree type = TREE_TYPE (decl);
5400 tree arg = DECL_ARGUMENTS (decl);
5401 tree argtype = TYPE_ARG_TYPES (type);
5403 while (arg && argtype)
5405 if (!DECL_PACK_P (arg)
5406 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5408 /* This is a PARM_DECL that contains unexpanded parameter
5409 packs. We have already complained about this in the
5410 check_for_bare_parameter_packs call, so just replace
5411 these types with ERROR_MARK_NODE. */
5412 TREE_TYPE (arg) = error_mark_node;
5413 TREE_VALUE (argtype) = error_mark_node;
5416 arg = DECL_CHAIN (arg);
5417 argtype = TREE_CHAIN (argtype);
5420 /* Check for bare parameter packs in the return type and the
5421 exception specifiers. */
5422 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5423 /* Errors were already issued, set return type to int
5424 as the frontend doesn't expect error_mark_node as
5425 the return type. */
5426 TREE_TYPE (type) = integer_type_node;
5427 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5428 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5430 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5431 && TYPE_DECL_ALIAS_P (decl))
5432 ? DECL_ORIGINAL_TYPE (decl)
5433 : TREE_TYPE (decl)))
5435 TREE_TYPE (decl) = error_mark_node;
5436 return error_mark_node;
5439 if (is_partial)
5440 return process_partial_specialization (decl);
5442 args = current_template_args ();
5444 if (!ctx
5445 || TREE_CODE (ctx) == FUNCTION_DECL
5446 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5447 || (TREE_CODE (decl) == TYPE_DECL
5448 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5449 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5451 if (DECL_LANG_SPECIFIC (decl)
5452 && DECL_TEMPLATE_INFO (decl)
5453 && DECL_TI_TEMPLATE (decl))
5454 tmpl = DECL_TI_TEMPLATE (decl);
5455 /* If DECL is a TYPE_DECL for a class-template, then there won't
5456 be DECL_LANG_SPECIFIC. The information equivalent to
5457 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5458 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5459 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5460 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5462 /* Since a template declaration already existed for this
5463 class-type, we must be redeclaring it here. Make sure
5464 that the redeclaration is valid. */
5465 redeclare_class_template (TREE_TYPE (decl),
5466 current_template_parms,
5467 current_template_constraints ());
5468 /* We don't need to create a new TEMPLATE_DECL; just use the
5469 one we already had. */
5470 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5472 else
5474 tmpl = build_template_decl (decl, current_template_parms,
5475 member_template_p);
5476 new_template_p = 1;
5478 if (DECL_LANG_SPECIFIC (decl)
5479 && DECL_TEMPLATE_SPECIALIZATION (decl))
5481 /* A specialization of a member template of a template
5482 class. */
5483 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5484 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5485 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5489 else
5491 tree a, t, current, parms;
5492 int i;
5493 tree tinfo = get_template_info (decl);
5495 if (!tinfo)
5497 error ("template definition of non-template %q#D", decl);
5498 return error_mark_node;
5501 tmpl = TI_TEMPLATE (tinfo);
5503 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5504 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5505 && DECL_TEMPLATE_SPECIALIZATION (decl)
5506 && DECL_MEMBER_TEMPLATE_P (tmpl))
5508 tree new_tmpl;
5510 /* The declaration is a specialization of a member
5511 template, declared outside the class. Therefore, the
5512 innermost template arguments will be NULL, so we
5513 replace them with the arguments determined by the
5514 earlier call to check_explicit_specialization. */
5515 args = DECL_TI_ARGS (decl);
5517 new_tmpl
5518 = build_template_decl (decl, current_template_parms,
5519 member_template_p);
5520 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5521 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5522 DECL_TI_TEMPLATE (decl) = new_tmpl;
5523 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5524 DECL_TEMPLATE_INFO (new_tmpl)
5525 = build_template_info (tmpl, args);
5527 register_specialization (new_tmpl,
5528 most_general_template (tmpl),
5529 args,
5530 is_friend, 0);
5531 return decl;
5534 /* Make sure the template headers we got make sense. */
5536 parms = DECL_TEMPLATE_PARMS (tmpl);
5537 i = TMPL_PARMS_DEPTH (parms);
5538 if (TMPL_ARGS_DEPTH (args) != i)
5540 error ("expected %d levels of template parms for %q#D, got %d",
5541 i, decl, TMPL_ARGS_DEPTH (args));
5542 DECL_INTERFACE_KNOWN (decl) = 1;
5543 return error_mark_node;
5545 else
5546 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5548 a = TMPL_ARGS_LEVEL (args, i);
5549 t = INNERMOST_TEMPLATE_PARMS (parms);
5551 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5553 if (current == decl)
5554 error ("got %d template parameters for %q#D",
5555 TREE_VEC_LENGTH (a), decl);
5556 else
5557 error ("got %d template parameters for %q#T",
5558 TREE_VEC_LENGTH (a), current);
5559 error (" but %d required", TREE_VEC_LENGTH (t));
5560 /* Avoid crash in import_export_decl. */
5561 DECL_INTERFACE_KNOWN (decl) = 1;
5562 return error_mark_node;
5565 if (current == decl)
5566 current = ctx;
5567 else if (current == NULL_TREE)
5568 /* Can happen in erroneous input. */
5569 break;
5570 else
5571 current = get_containing_scope (current);
5574 /* Check that the parms are used in the appropriate qualifying scopes
5575 in the declarator. */
5576 if (!comp_template_args
5577 (TI_ARGS (tinfo),
5578 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5580 error ("template arguments to %qD do not match original "
5581 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5582 if (!uses_template_parms (TI_ARGS (tinfo)))
5583 inform (input_location, "use %<template<>%> for"
5584 " an explicit specialization");
5585 /* Avoid crash in import_export_decl. */
5586 DECL_INTERFACE_KNOWN (decl) = 1;
5587 return error_mark_node;
5591 DECL_TEMPLATE_RESULT (tmpl) = decl;
5592 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5594 /* Push template declarations for global functions and types. Note
5595 that we do not try to push a global template friend declared in a
5596 template class; such a thing may well depend on the template
5597 parameters of the class. */
5598 if (new_template_p && !ctx
5599 && !(is_friend && template_class_depth (current_class_type) > 0))
5601 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5602 if (tmpl == error_mark_node)
5603 return error_mark_node;
5605 /* Hide template friend classes that haven't been declared yet. */
5606 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5608 DECL_ANTICIPATED (tmpl) = 1;
5609 DECL_FRIEND_P (tmpl) = 1;
5613 if (is_primary)
5615 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5617 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5619 /* Give template template parms a DECL_CONTEXT of the template
5620 for which they are a parameter. */
5621 parms = INNERMOST_TEMPLATE_PARMS (parms);
5622 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5624 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5625 if (TREE_CODE (parm) == TEMPLATE_DECL)
5626 DECL_CONTEXT (parm) = tmpl;
5629 if (TREE_CODE (decl) == TYPE_DECL
5630 && TYPE_DECL_ALIAS_P (decl)
5631 && complex_alias_template_p (tmpl))
5632 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5635 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5636 back to its most general template. If TMPL is a specialization,
5637 ARGS may only have the innermost set of arguments. Add the missing
5638 argument levels if necessary. */
5639 if (DECL_TEMPLATE_INFO (tmpl))
5640 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5642 info = build_template_info (tmpl, args);
5644 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5645 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5646 else
5648 if (is_primary)
5649 retrofit_lang_decl (decl);
5650 if (DECL_LANG_SPECIFIC (decl))
5651 DECL_TEMPLATE_INFO (decl) = info;
5654 if (flag_implicit_templates
5655 && !is_friend
5656 && TREE_PUBLIC (decl)
5657 && VAR_OR_FUNCTION_DECL_P (decl))
5658 /* Set DECL_COMDAT on template instantiations; if we force
5659 them to be emitted by explicit instantiation or -frepo,
5660 mark_needed will tell cgraph to do the right thing. */
5661 DECL_COMDAT (decl) = true;
5663 return DECL_TEMPLATE_RESULT (tmpl);
5666 tree
5667 push_template_decl (tree decl)
5669 return push_template_decl_real (decl, false);
5672 /* FN is an inheriting constructor that inherits from the constructor
5673 template INHERITED; turn FN into a constructor template with a matching
5674 template header. */
5676 tree
5677 add_inherited_template_parms (tree fn, tree inherited)
5679 tree inner_parms
5680 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5681 inner_parms = copy_node (inner_parms);
5682 tree parms
5683 = tree_cons (size_int (processing_template_decl + 1),
5684 inner_parms, current_template_parms);
5685 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5686 tree args = template_parms_to_args (parms);
5687 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5688 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5689 DECL_TEMPLATE_RESULT (tmpl) = fn;
5690 DECL_ARTIFICIAL (tmpl) = true;
5691 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5692 return tmpl;
5695 /* Called when a class template TYPE is redeclared with the indicated
5696 template PARMS, e.g.:
5698 template <class T> struct S;
5699 template <class T> struct S {}; */
5701 bool
5702 redeclare_class_template (tree type, tree parms, tree cons)
5704 tree tmpl;
5705 tree tmpl_parms;
5706 int i;
5708 if (!TYPE_TEMPLATE_INFO (type))
5710 error ("%qT is not a template type", type);
5711 return false;
5714 tmpl = TYPE_TI_TEMPLATE (type);
5715 if (!PRIMARY_TEMPLATE_P (tmpl))
5716 /* The type is nested in some template class. Nothing to worry
5717 about here; there are no new template parameters for the nested
5718 type. */
5719 return true;
5721 if (!parms)
5723 error ("template specifiers not specified in declaration of %qD",
5724 tmpl);
5725 return false;
5728 parms = INNERMOST_TEMPLATE_PARMS (parms);
5729 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5731 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5733 error_n (input_location, TREE_VEC_LENGTH (parms),
5734 "redeclared with %d template parameter",
5735 "redeclared with %d template parameters",
5736 TREE_VEC_LENGTH (parms));
5737 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5738 "previous declaration %qD used %d template parameter",
5739 "previous declaration %qD used %d template parameters",
5740 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5741 return false;
5744 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5746 tree tmpl_parm;
5747 tree parm;
5748 tree tmpl_default;
5749 tree parm_default;
5751 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5752 || TREE_VEC_ELT (parms, i) == error_mark_node)
5753 continue;
5755 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5756 if (error_operand_p (tmpl_parm))
5757 return false;
5759 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5760 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5761 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5763 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5764 TEMPLATE_DECL. */
5765 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5766 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5767 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5768 || (TREE_CODE (tmpl_parm) != PARM_DECL
5769 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5770 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5771 || (TREE_CODE (tmpl_parm) == PARM_DECL
5772 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5773 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5775 error ("template parameter %q+#D", tmpl_parm);
5776 error ("redeclared here as %q#D", parm);
5777 return false;
5780 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5782 /* We have in [temp.param]:
5784 A template-parameter may not be given default arguments
5785 by two different declarations in the same scope. */
5786 error_at (input_location, "redefinition of default argument for %q#D", parm);
5787 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5788 "original definition appeared here");
5789 return false;
5792 if (parm_default != NULL_TREE)
5793 /* Update the previous template parameters (which are the ones
5794 that will really count) with the new default value. */
5795 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5796 else if (tmpl_default != NULL_TREE)
5797 /* Update the new parameters, too; they'll be used as the
5798 parameters for any members. */
5799 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5801 /* Give each template template parm in this redeclaration a
5802 DECL_CONTEXT of the template for which they are a parameter. */
5803 if (TREE_CODE (parm) == TEMPLATE_DECL)
5805 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5806 DECL_CONTEXT (parm) = tmpl;
5809 if (TREE_CODE (parm) == TYPE_DECL)
5810 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5813 // Cannot redeclare a class template with a different set of constraints.
5814 if (!equivalent_constraints (get_constraints (tmpl), cons))
5816 error_at (input_location, "redeclaration %q#D with different "
5817 "constraints", tmpl);
5818 inform (DECL_SOURCE_LOCATION (tmpl),
5819 "original declaration appeared here");
5822 return true;
5825 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5826 to be used when the caller has already checked
5827 (processing_template_decl
5828 && !instantiation_dependent_expression_p (expr)
5829 && potential_constant_expression (expr))
5830 and cleared processing_template_decl. */
5832 tree
5833 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5835 return tsubst_copy_and_build (expr,
5836 /*args=*/NULL_TREE,
5837 complain,
5838 /*in_decl=*/NULL_TREE,
5839 /*function_p=*/false,
5840 /*integral_constant_expression_p=*/true);
5843 /* Simplify EXPR if it is a non-dependent expression. Returns the
5844 (possibly simplified) expression. */
5846 tree
5847 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5849 if (expr == NULL_TREE)
5850 return NULL_TREE;
5852 /* If we're in a template, but EXPR isn't value dependent, simplify
5853 it. We're supposed to treat:
5855 template <typename T> void f(T[1 + 1]);
5856 template <typename T> void f(T[2]);
5858 as two declarations of the same function, for example. */
5859 if (processing_template_decl
5860 && is_nondependent_constant_expression (expr))
5862 processing_template_decl_sentinel s;
5863 expr = instantiate_non_dependent_expr_internal (expr, complain);
5865 return expr;
5868 tree
5869 instantiate_non_dependent_expr (tree expr)
5871 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5874 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5875 an uninstantiated expression. */
5877 tree
5878 instantiate_non_dependent_or_null (tree expr)
5880 if (expr == NULL_TREE)
5881 return NULL_TREE;
5882 if (processing_template_decl)
5884 if (!is_nondependent_constant_expression (expr))
5885 expr = NULL_TREE;
5886 else
5888 processing_template_decl_sentinel s;
5889 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5892 return expr;
5895 /* True iff T is a specialization of a variable template. */
5897 bool
5898 variable_template_specialization_p (tree t)
5900 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5901 return false;
5902 tree tmpl = DECL_TI_TEMPLATE (t);
5903 return variable_template_p (tmpl);
5906 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5907 template declaration, or a TYPE_DECL for an alias declaration. */
5909 bool
5910 alias_type_or_template_p (tree t)
5912 if (t == NULL_TREE)
5913 return false;
5914 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5915 || (TYPE_P (t)
5916 && TYPE_NAME (t)
5917 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5918 || DECL_ALIAS_TEMPLATE_P (t));
5921 /* Return TRUE iff T is a specialization of an alias template. */
5923 bool
5924 alias_template_specialization_p (const_tree t)
5926 /* It's an alias template specialization if it's an alias and its
5927 TYPE_NAME is a specialization of a primary template. */
5928 if (TYPE_ALIAS_P (t))
5929 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5930 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5932 return false;
5935 /* An alias template is complex from a SFINAE perspective if a template-id
5936 using that alias can be ill-formed when the expansion is not, as with
5937 the void_t template. We determine this by checking whether the
5938 expansion for the alias template uses all its template parameters. */
5940 struct uses_all_template_parms_data
5942 int level;
5943 bool *seen;
5946 static int
5947 uses_all_template_parms_r (tree t, void *data_)
5949 struct uses_all_template_parms_data &data
5950 = *(struct uses_all_template_parms_data*)data_;
5951 tree idx = get_template_parm_index (t);
5953 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5954 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5955 return 0;
5958 static bool
5959 complex_alias_template_p (const_tree tmpl)
5961 struct uses_all_template_parms_data data;
5962 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5963 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5964 data.level = TMPL_PARMS_DEPTH (parms);
5965 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5966 data.seen = XALLOCAVEC (bool, len);
5967 for (int i = 0; i < len; ++i)
5968 data.seen[i] = false;
5970 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5971 for (int i = 0; i < len; ++i)
5972 if (!data.seen[i])
5973 return true;
5974 return false;
5977 /* Return TRUE iff T is a specialization of a complex alias template with
5978 dependent template-arguments. */
5980 bool
5981 dependent_alias_template_spec_p (const_tree t)
5983 if (!alias_template_specialization_p (t))
5984 return false;
5986 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5987 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5988 return false;
5990 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5991 if (!any_dependent_template_arguments_p (args))
5992 return false;
5994 return true;
5997 /* Return the number of innermost template parameters in TMPL. */
5999 static int
6000 num_innermost_template_parms (tree tmpl)
6002 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6003 return TREE_VEC_LENGTH (parms);
6006 /* Return either TMPL or another template that it is equivalent to under DR
6007 1286: An alias that just changes the name of a template is equivalent to
6008 the other template. */
6010 static tree
6011 get_underlying_template (tree tmpl)
6013 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6014 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6016 /* Determine if the alias is equivalent to an underlying template. */
6017 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6018 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6019 if (!tinfo)
6020 break;
6022 tree underlying = TI_TEMPLATE (tinfo);
6023 if (!PRIMARY_TEMPLATE_P (underlying)
6024 || (num_innermost_template_parms (tmpl)
6025 != num_innermost_template_parms (underlying)))
6026 break;
6028 tree alias_args = INNERMOST_TEMPLATE_ARGS
6029 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6030 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6031 break;
6033 /* Alias is equivalent. Strip it and repeat. */
6034 tmpl = underlying;
6037 return tmpl;
6040 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6041 must be a reference-to-function or a pointer-to-function type, as specified
6042 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6043 and check that the resulting function has external linkage. */
6045 static tree
6046 convert_nontype_argument_function (tree type, tree expr,
6047 tsubst_flags_t complain)
6049 tree fns = expr;
6050 tree fn, fn_no_ptr;
6051 linkage_kind linkage;
6053 fn = instantiate_type (type, fns, tf_none);
6054 if (fn == error_mark_node)
6055 return error_mark_node;
6057 if (value_dependent_expression_p (fn))
6058 goto accept;
6060 fn_no_ptr = strip_fnptr_conv (fn);
6061 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6062 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6063 if (BASELINK_P (fn_no_ptr))
6064 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6066 /* [temp.arg.nontype]/1
6068 A template-argument for a non-type, non-template template-parameter
6069 shall be one of:
6070 [...]
6071 -- the address of an object or function with external [C++11: or
6072 internal] linkage. */
6074 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6076 if (complain & tf_error)
6078 error ("%qE is not a valid template argument for type %qT",
6079 expr, type);
6080 if (TYPE_PTR_P (type))
6081 inform (input_location, "it must be the address of a function "
6082 "with external linkage");
6083 else
6084 inform (input_location, "it must be the name of a function with "
6085 "external linkage");
6087 return NULL_TREE;
6090 linkage = decl_linkage (fn_no_ptr);
6091 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6093 if (complain & tf_error)
6095 if (cxx_dialect >= cxx11)
6096 error ("%qE is not a valid template argument for type %qT "
6097 "because %qD has no linkage",
6098 expr, type, fn_no_ptr);
6099 else
6100 error ("%qE is not a valid template argument for type %qT "
6101 "because %qD does not have external linkage",
6102 expr, type, fn_no_ptr);
6104 return NULL_TREE;
6107 accept:
6108 if (TREE_CODE (type) == REFERENCE_TYPE)
6109 fn = build_address (fn);
6110 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6111 fn = build_nop (type, fn);
6113 return fn;
6116 /* Subroutine of convert_nontype_argument.
6117 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6118 Emit an error otherwise. */
6120 static bool
6121 check_valid_ptrmem_cst_expr (tree type, tree expr,
6122 tsubst_flags_t complain)
6124 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6125 tree orig_expr = expr;
6126 STRIP_NOPS (expr);
6127 if (null_ptr_cst_p (expr))
6128 return true;
6129 if (TREE_CODE (expr) == PTRMEM_CST
6130 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6131 PTRMEM_CST_CLASS (expr)))
6132 return true;
6133 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6134 return true;
6135 if (processing_template_decl
6136 && TREE_CODE (expr) == ADDR_EXPR
6137 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6138 return true;
6139 if (complain & tf_error)
6141 error_at (loc, "%qE is not a valid template argument for type %qT",
6142 orig_expr, type);
6143 if (TREE_CODE (expr) != PTRMEM_CST)
6144 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6145 else
6146 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6148 return false;
6151 /* Returns TRUE iff the address of OP is value-dependent.
6153 14.6.2.4 [temp.dep.temp]:
6154 A non-integral non-type template-argument is dependent if its type is
6155 dependent or it has either of the following forms
6156 qualified-id
6157 & qualified-id
6158 and contains a nested-name-specifier which specifies a class-name that
6159 names a dependent type.
6161 We generalize this to just say that the address of a member of a
6162 dependent class is value-dependent; the above doesn't cover the
6163 address of a static data member named with an unqualified-id. */
6165 static bool
6166 has_value_dependent_address (tree op)
6168 /* We could use get_inner_reference here, but there's no need;
6169 this is only relevant for template non-type arguments, which
6170 can only be expressed as &id-expression. */
6171 if (DECL_P (op))
6173 tree ctx = CP_DECL_CONTEXT (op);
6174 if (TYPE_P (ctx) && dependent_type_p (ctx))
6175 return true;
6178 return false;
6181 /* The next set of functions are used for providing helpful explanatory
6182 diagnostics for failed overload resolution. Their messages should be
6183 indented by two spaces for consistency with the messages in
6184 call.c */
6186 static int
6187 unify_success (bool /*explain_p*/)
6189 return 0;
6192 /* Other failure functions should call this one, to provide a single function
6193 for setting a breakpoint on. */
6195 static int
6196 unify_invalid (bool /*explain_p*/)
6198 return 1;
6201 static int
6202 unify_parameter_deduction_failure (bool explain_p, tree parm)
6204 if (explain_p)
6205 inform (input_location,
6206 " couldn't deduce template parameter %qD", parm);
6207 return unify_invalid (explain_p);
6210 static int
6211 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6213 if (explain_p)
6214 inform (input_location,
6215 " types %qT and %qT have incompatible cv-qualifiers",
6216 parm, arg);
6217 return unify_invalid (explain_p);
6220 static int
6221 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6223 if (explain_p)
6224 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6225 return unify_invalid (explain_p);
6228 static int
6229 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6231 if (explain_p)
6232 inform (input_location,
6233 " template parameter %qD is not a parameter pack, but "
6234 "argument %qD is",
6235 parm, arg);
6236 return unify_invalid (explain_p);
6239 static int
6240 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6242 if (explain_p)
6243 inform (input_location,
6244 " template argument %qE does not match "
6245 "pointer-to-member constant %qE",
6246 arg, parm);
6247 return unify_invalid (explain_p);
6250 static int
6251 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6253 if (explain_p)
6254 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6255 return unify_invalid (explain_p);
6258 static int
6259 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6261 if (explain_p)
6262 inform (input_location,
6263 " inconsistent parameter pack deduction with %qT and %qT",
6264 old_arg, new_arg);
6265 return unify_invalid (explain_p);
6268 static int
6269 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6271 if (explain_p)
6273 if (TYPE_P (parm))
6274 inform (input_location,
6275 " deduced conflicting types for parameter %qT (%qT and %qT)",
6276 parm, first, second);
6277 else
6278 inform (input_location,
6279 " deduced conflicting values for non-type parameter "
6280 "%qE (%qE and %qE)", parm, first, second);
6282 return unify_invalid (explain_p);
6285 static int
6286 unify_vla_arg (bool explain_p, tree arg)
6288 if (explain_p)
6289 inform (input_location,
6290 " variable-sized array type %qT is not "
6291 "a valid template argument",
6292 arg);
6293 return unify_invalid (explain_p);
6296 static int
6297 unify_method_type_error (bool explain_p, tree arg)
6299 if (explain_p)
6300 inform (input_location,
6301 " member function type %qT is not a valid template argument",
6302 arg);
6303 return unify_invalid (explain_p);
6306 static int
6307 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6309 if (explain_p)
6311 if (least_p)
6312 inform_n (input_location, wanted,
6313 " candidate expects at least %d argument, %d provided",
6314 " candidate expects at least %d arguments, %d provided",
6315 wanted, have);
6316 else
6317 inform_n (input_location, wanted,
6318 " candidate expects %d argument, %d provided",
6319 " candidate expects %d arguments, %d provided",
6320 wanted, have);
6322 return unify_invalid (explain_p);
6325 static int
6326 unify_too_many_arguments (bool explain_p, int have, int wanted)
6328 return unify_arity (explain_p, have, wanted);
6331 static int
6332 unify_too_few_arguments (bool explain_p, int have, int wanted,
6333 bool least_p = false)
6335 return unify_arity (explain_p, have, wanted, least_p);
6338 static int
6339 unify_arg_conversion (bool explain_p, tree to_type,
6340 tree from_type, tree arg)
6342 if (explain_p)
6343 inform (EXPR_LOC_OR_LOC (arg, input_location),
6344 " cannot convert %qE (type %qT) to type %qT",
6345 arg, from_type, to_type);
6346 return unify_invalid (explain_p);
6349 static int
6350 unify_no_common_base (bool explain_p, enum template_base_result r,
6351 tree parm, tree arg)
6353 if (explain_p)
6354 switch (r)
6356 case tbr_ambiguous_baseclass:
6357 inform (input_location, " %qT is an ambiguous base class of %qT",
6358 parm, arg);
6359 break;
6360 default:
6361 inform (input_location, " %qT is not derived from %qT", arg, parm);
6362 break;
6364 return unify_invalid (explain_p);
6367 static int
6368 unify_inconsistent_template_template_parameters (bool explain_p)
6370 if (explain_p)
6371 inform (input_location,
6372 " template parameters of a template template argument are "
6373 "inconsistent with other deduced template arguments");
6374 return unify_invalid (explain_p);
6377 static int
6378 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6380 if (explain_p)
6381 inform (input_location,
6382 " can't deduce a template for %qT from non-template type %qT",
6383 parm, arg);
6384 return unify_invalid (explain_p);
6387 static int
6388 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6390 if (explain_p)
6391 inform (input_location,
6392 " template argument %qE does not match %qE", arg, parm);
6393 return unify_invalid (explain_p);
6396 /* Attempt to convert the non-type template parameter EXPR to the
6397 indicated TYPE. If the conversion is successful, return the
6398 converted value. If the conversion is unsuccessful, return
6399 NULL_TREE if we issued an error message, or error_mark_node if we
6400 did not. We issue error messages for out-and-out bad template
6401 parameters, but not simply because the conversion failed, since we
6402 might be just trying to do argument deduction. Both TYPE and EXPR
6403 must be non-dependent.
6405 The conversion follows the special rules described in
6406 [temp.arg.nontype], and it is much more strict than an implicit
6407 conversion.
6409 This function is called twice for each template argument (see
6410 lookup_template_class for a more accurate description of this
6411 problem). This means that we need to handle expressions which
6412 are not valid in a C++ source, but can be created from the
6413 first call (for instance, casts to perform conversions). These
6414 hacks can go away after we fix the double coercion problem. */
6416 static tree
6417 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6419 tree expr_type;
6420 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6421 tree orig_expr = expr;
6423 /* Detect immediately string literals as invalid non-type argument.
6424 This special-case is not needed for correctness (we would easily
6425 catch this later), but only to provide better diagnostic for this
6426 common user mistake. As suggested by DR 100, we do not mention
6427 linkage issues in the diagnostic as this is not the point. */
6428 /* FIXME we're making this OK. */
6429 if (TREE_CODE (expr) == STRING_CST)
6431 if (complain & tf_error)
6432 error ("%qE is not a valid template argument for type %qT "
6433 "because string literals can never be used in this context",
6434 expr, type);
6435 return NULL_TREE;
6438 /* Add the ADDR_EXPR now for the benefit of
6439 value_dependent_expression_p. */
6440 if (TYPE_PTROBV_P (type)
6441 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6443 expr = decay_conversion (expr, complain);
6444 if (expr == error_mark_node)
6445 return error_mark_node;
6448 /* If we are in a template, EXPR may be non-dependent, but still
6449 have a syntactic, rather than semantic, form. For example, EXPR
6450 might be a SCOPE_REF, rather than the VAR_DECL to which the
6451 SCOPE_REF refers. Preserving the qualifying scope is necessary
6452 so that access checking can be performed when the template is
6453 instantiated -- but here we need the resolved form so that we can
6454 convert the argument. */
6455 bool non_dep = false;
6456 if (TYPE_REF_OBJ_P (type)
6457 && has_value_dependent_address (expr))
6458 /* If we want the address and it's value-dependent, don't fold. */;
6459 else if (processing_template_decl
6460 && is_nondependent_constant_expression (expr))
6461 non_dep = true;
6462 if (error_operand_p (expr))
6463 return error_mark_node;
6464 expr_type = TREE_TYPE (expr);
6466 /* If the argument is non-dependent, perform any conversions in
6467 non-dependent context as well. */
6468 processing_template_decl_sentinel s (non_dep);
6469 if (non_dep)
6470 expr = instantiate_non_dependent_expr_internal (expr, complain);
6472 if (value_dependent_expression_p (expr))
6473 expr = canonicalize_expr_argument (expr, complain);
6475 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6476 to a non-type argument of "nullptr". */
6477 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6478 expr = fold_simple (convert (type, expr));
6480 /* In C++11, integral or enumeration non-type template arguments can be
6481 arbitrary constant expressions. Pointer and pointer to
6482 member arguments can be general constant expressions that evaluate
6483 to a null value, but otherwise still need to be of a specific form. */
6484 if (cxx_dialect >= cxx11)
6486 if (TREE_CODE (expr) == PTRMEM_CST)
6487 /* A PTRMEM_CST is already constant, and a valid template
6488 argument for a parameter of pointer to member type, we just want
6489 to leave it in that form rather than lower it to a
6490 CONSTRUCTOR. */;
6491 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6492 || cxx_dialect >= cxx17)
6494 /* C++17: A template-argument for a non-type template-parameter shall
6495 be a converted constant expression (8.20) of the type of the
6496 template-parameter. */
6497 expr = build_converted_constant_expr (type, expr, complain);
6498 if (expr == error_mark_node)
6499 return error_mark_node;
6500 expr = maybe_constant_value (expr);
6501 expr = convert_from_reference (expr);
6503 else if (TYPE_PTR_OR_PTRMEM_P (type))
6505 tree folded = maybe_constant_value (expr);
6506 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6507 : null_member_pointer_value_p (folded))
6508 expr = folded;
6512 if (TREE_CODE (type) == REFERENCE_TYPE)
6513 expr = mark_lvalue_use (expr);
6514 else
6515 expr = mark_rvalue_use (expr);
6517 /* HACK: Due to double coercion, we can get a
6518 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6519 which is the tree that we built on the first call (see
6520 below when coercing to reference to object or to reference to
6521 function). We just strip everything and get to the arg.
6522 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6523 for examples. */
6524 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6526 tree probe_type, probe = expr;
6527 if (REFERENCE_REF_P (probe))
6528 probe = TREE_OPERAND (probe, 0);
6529 probe_type = TREE_TYPE (probe);
6530 if (TREE_CODE (probe) == NOP_EXPR)
6532 /* ??? Maybe we could use convert_from_reference here, but we
6533 would need to relax its constraints because the NOP_EXPR
6534 could actually change the type to something more cv-qualified,
6535 and this is not folded by convert_from_reference. */
6536 tree addr = TREE_OPERAND (probe, 0);
6537 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6538 && TREE_CODE (addr) == ADDR_EXPR
6539 && TYPE_PTR_P (TREE_TYPE (addr))
6540 && (same_type_ignoring_top_level_qualifiers_p
6541 (TREE_TYPE (probe_type),
6542 TREE_TYPE (TREE_TYPE (addr)))))
6544 expr = TREE_OPERAND (addr, 0);
6545 expr_type = TREE_TYPE (probe_type);
6550 /* [temp.arg.nontype]/5, bullet 1
6552 For a non-type template-parameter of integral or enumeration type,
6553 integral promotions (_conv.prom_) and integral conversions
6554 (_conv.integral_) are applied. */
6555 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6557 if (cxx_dialect < cxx11)
6559 tree t = build_converted_constant_expr (type, expr, complain);
6560 t = maybe_constant_value (t);
6561 if (t != error_mark_node)
6562 expr = t;
6565 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6566 return error_mark_node;
6568 /* Notice that there are constant expressions like '4 % 0' which
6569 do not fold into integer constants. */
6570 if (TREE_CODE (expr) != INTEGER_CST
6571 && !value_dependent_expression_p (expr))
6573 if (complain & tf_error)
6575 int errs = errorcount, warns = warningcount + werrorcount;
6576 if (!require_potential_constant_expression (expr))
6577 expr = error_mark_node;
6578 else
6579 expr = cxx_constant_value (expr);
6580 if (errorcount > errs || warningcount + werrorcount > warns)
6581 inform (loc, "in template argument for type %qT ", type);
6582 if (expr == error_mark_node)
6583 return NULL_TREE;
6584 /* else cxx_constant_value complained but gave us
6585 a real constant, so go ahead. */
6586 if (TREE_CODE (expr) != INTEGER_CST)
6588 /* Some assemble time constant expressions like
6589 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6590 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6591 as we can emit them into .rodata initializers of
6592 variables, yet they can't fold into an INTEGER_CST at
6593 compile time. Refuse them here. */
6594 gcc_checking_assert (reduced_constant_expression_p (expr));
6595 error_at (loc, "template argument %qE for type %qT not "
6596 "a constant integer", expr, type);
6597 return NULL_TREE;
6600 else
6601 return NULL_TREE;
6604 /* Avoid typedef problems. */
6605 if (TREE_TYPE (expr) != type)
6606 expr = fold_convert (type, expr);
6608 /* [temp.arg.nontype]/5, bullet 2
6610 For a non-type template-parameter of type pointer to object,
6611 qualification conversions (_conv.qual_) and the array-to-pointer
6612 conversion (_conv.array_) are applied. */
6613 else if (TYPE_PTROBV_P (type))
6615 tree decayed = expr;
6617 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6618 decay_conversion or an explicit cast. If it's a problematic cast,
6619 we'll complain about it below. */
6620 if (TREE_CODE (expr) == NOP_EXPR)
6622 tree probe = expr;
6623 STRIP_NOPS (probe);
6624 if (TREE_CODE (probe) == ADDR_EXPR
6625 && TYPE_PTR_P (TREE_TYPE (probe)))
6627 expr = probe;
6628 expr_type = TREE_TYPE (expr);
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr) != ADDR_EXPR)
6653 if (VAR_P (expr))
6655 if (complain & tf_error)
6656 error ("%qD is not a valid template argument "
6657 "because %qD is a variable, not the address of "
6658 "a variable", orig_expr, expr);
6659 return NULL_TREE;
6661 if (POINTER_TYPE_P (expr_type))
6663 if (complain & tf_error)
6664 error ("%qE is not a valid template argument for %qT "
6665 "because it is not the address of a variable",
6666 orig_expr, type);
6667 return NULL_TREE;
6669 /* Other values, like integer constants, might be valid
6670 non-type arguments of some other type. */
6671 return error_mark_node;
6673 else
6675 tree decl = TREE_OPERAND (expr, 0);
6677 if (!VAR_P (decl))
6679 if (complain & tf_error)
6680 error ("%qE is not a valid template argument of type %qT "
6681 "because %qE is not a variable", orig_expr, type, decl);
6682 return NULL_TREE;
6684 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6686 if (complain & tf_error)
6687 error ("%qE is not a valid template argument of type %qT "
6688 "because %qD does not have external linkage",
6689 orig_expr, type, decl);
6690 return NULL_TREE;
6692 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6693 && decl_linkage (decl) == lk_none)
6695 if (complain & tf_error)
6696 error ("%qE is not a valid template argument of type %qT "
6697 "because %qD has no linkage", orig_expr, type, decl);
6698 return NULL_TREE;
6700 /* C++17: For a non-type template-parameter of reference or pointer
6701 type, the value of the constant expression shall not refer to (or
6702 for a pointer type, shall not be the address of):
6703 * a subobject (4.5),
6704 * a temporary object (15.2),
6705 * a string literal (5.13.5),
6706 * the result of a typeid expression (8.2.8), or
6707 * a predefined __func__ variable (11.4.1). */
6708 else if (DECL_ARTIFICIAL (decl))
6710 if (complain & tf_error)
6711 error ("the address of %qD is not a valid template argument",
6712 decl);
6713 return NULL_TREE;
6715 else if (!same_type_ignoring_top_level_qualifiers_p
6716 (strip_array_types (TREE_TYPE (type)),
6717 strip_array_types (TREE_TYPE (decl))))
6719 if (complain & tf_error)
6720 error ("the address of the %qT subobject of %qD is not a "
6721 "valid template argument", TREE_TYPE (type), decl);
6722 return NULL_TREE;
6724 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6726 if (complain & tf_error)
6727 error ("the address of %qD is not a valid template argument "
6728 "because it does not have static storage duration",
6729 decl);
6730 return NULL_TREE;
6734 expr = decayed;
6736 expr = perform_qualification_conversions (type, expr);
6737 if (expr == error_mark_node)
6738 return error_mark_node;
6740 /* [temp.arg.nontype]/5, bullet 3
6742 For a non-type template-parameter of type reference to object, no
6743 conversions apply. The type referred to by the reference may be more
6744 cv-qualified than the (otherwise identical) type of the
6745 template-argument. The template-parameter is bound directly to the
6746 template-argument, which must be an lvalue. */
6747 else if (TYPE_REF_OBJ_P (type))
6749 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6750 expr_type))
6751 return error_mark_node;
6753 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6755 if (complain & tf_error)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because of conflicts in cv-qualification", expr, type);
6758 return NULL_TREE;
6761 if (!lvalue_p (expr))
6763 if (complain & tf_error)
6764 error ("%qE is not a valid template argument for type %qT "
6765 "because it is not an lvalue", expr, type);
6766 return NULL_TREE;
6769 /* [temp.arg.nontype]/1
6771 A template-argument for a non-type, non-template template-parameter
6772 shall be one of: [...]
6774 -- the address of an object or function with external linkage. */
6775 if (INDIRECT_REF_P (expr)
6776 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6778 expr = TREE_OPERAND (expr, 0);
6779 if (DECL_P (expr))
6781 if (complain & tf_error)
6782 error ("%q#D is not a valid template argument for type %qT "
6783 "because a reference variable does not have a constant "
6784 "address", expr, type);
6785 return NULL_TREE;
6789 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6790 && value_dependent_expression_p (expr))
6791 /* OK, dependent reference. We don't want to ask whether a DECL is
6792 itself value-dependent, since what we want here is its address. */;
6793 else
6795 if (!DECL_P (expr))
6797 if (complain & tf_error)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because it is not an object with linkage",
6800 expr, type);
6801 return NULL_TREE;
6804 /* DR 1155 allows internal linkage in C++11 and up. */
6805 linkage_kind linkage = decl_linkage (expr);
6806 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6808 if (complain & tf_error)
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because object %qD does not have linkage",
6811 expr, type, expr);
6812 return NULL_TREE;
6815 expr = build_address (expr);
6818 if (!same_type_p (type, TREE_TYPE (expr)))
6819 expr = build_nop (type, expr);
6821 /* [temp.arg.nontype]/5, bullet 4
6823 For a non-type template-parameter of type pointer to function, only
6824 the function-to-pointer conversion (_conv.func_) is applied. If the
6825 template-argument represents a set of overloaded functions (or a
6826 pointer to such), the matching function is selected from the set
6827 (_over.over_). */
6828 else if (TYPE_PTRFN_P (type))
6830 /* If the argument is a template-id, we might not have enough
6831 context information to decay the pointer. */
6832 if (!type_unknown_p (expr_type))
6834 expr = decay_conversion (expr, complain);
6835 if (expr == error_mark_node)
6836 return error_mark_node;
6839 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6840 /* Null pointer values are OK in C++11. */
6841 return perform_qualification_conversions (type, expr);
6843 expr = convert_nontype_argument_function (type, expr, complain);
6844 if (!expr || expr == error_mark_node)
6845 return expr;
6847 /* [temp.arg.nontype]/5, bullet 5
6849 For a non-type template-parameter of type reference to function, no
6850 conversions apply. If the template-argument represents a set of
6851 overloaded functions, the matching function is selected from the set
6852 (_over.over_). */
6853 else if (TYPE_REFFN_P (type))
6855 if (TREE_CODE (expr) == ADDR_EXPR)
6857 if (complain & tf_error)
6859 error ("%qE is not a valid template argument for type %qT "
6860 "because it is a pointer", expr, type);
6861 inform (input_location, "try using %qE instead",
6862 TREE_OPERAND (expr, 0));
6864 return NULL_TREE;
6867 expr = convert_nontype_argument_function (type, expr, complain);
6868 if (!expr || expr == error_mark_node)
6869 return expr;
6871 /* [temp.arg.nontype]/5, bullet 6
6873 For a non-type template-parameter of type pointer to member function,
6874 no conversions apply. If the template-argument represents a set of
6875 overloaded member functions, the matching member function is selected
6876 from the set (_over.over_). */
6877 else if (TYPE_PTRMEMFUNC_P (type))
6879 expr = instantiate_type (type, expr, tf_none);
6880 if (expr == error_mark_node)
6881 return error_mark_node;
6883 /* [temp.arg.nontype] bullet 1 says the pointer to member
6884 expression must be a pointer-to-member constant. */
6885 if (!value_dependent_expression_p (expr)
6886 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6887 return NULL_TREE;
6889 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6890 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6891 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6892 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6894 /* [temp.arg.nontype]/5, bullet 7
6896 For a non-type template-parameter of type pointer to data member,
6897 qualification conversions (_conv.qual_) are applied. */
6898 else if (TYPE_PTRDATAMEM_P (type))
6900 /* [temp.arg.nontype] bullet 1 says the pointer to member
6901 expression must be a pointer-to-member constant. */
6902 if (!value_dependent_expression_p (expr)
6903 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6904 return NULL_TREE;
6906 expr = perform_qualification_conversions (type, expr);
6907 if (expr == error_mark_node)
6908 return expr;
6910 else if (NULLPTR_TYPE_P (type))
6912 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6914 if (complain & tf_error)
6915 error ("%qE is not a valid template argument for type %qT "
6916 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6917 return NULL_TREE;
6919 return expr;
6921 /* A template non-type parameter must be one of the above. */
6922 else
6923 gcc_unreachable ();
6925 /* Sanity check: did we actually convert the argument to the
6926 right type? */
6927 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6928 (type, TREE_TYPE (expr)));
6929 return convert_from_reference (expr);
6932 /* Subroutine of coerce_template_template_parms, which returns 1 if
6933 PARM_PARM and ARG_PARM match using the rule for the template
6934 parameters of template template parameters. Both PARM and ARG are
6935 template parameters; the rest of the arguments are the same as for
6936 coerce_template_template_parms.
6938 static int
6939 coerce_template_template_parm (tree parm,
6940 tree arg,
6941 tsubst_flags_t complain,
6942 tree in_decl,
6943 tree outer_args)
6945 if (arg == NULL_TREE || error_operand_p (arg)
6946 || parm == NULL_TREE || error_operand_p (parm))
6947 return 0;
6949 if (TREE_CODE (arg) != TREE_CODE (parm))
6950 return 0;
6952 switch (TREE_CODE (parm))
6954 case TEMPLATE_DECL:
6955 /* We encounter instantiations of templates like
6956 template <template <template <class> class> class TT>
6957 class C; */
6959 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6960 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6962 if (!coerce_template_template_parms
6963 (parmparm, argparm, complain, in_decl, outer_args))
6964 return 0;
6966 /* Fall through. */
6968 case TYPE_DECL:
6969 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6970 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6971 /* Argument is a parameter pack but parameter is not. */
6972 return 0;
6973 break;
6975 case PARM_DECL:
6976 /* The tsubst call is used to handle cases such as
6978 template <int> class C {};
6979 template <class T, template <T> class TT> class D {};
6980 D<int, C> d;
6982 i.e. the parameter list of TT depends on earlier parameters. */
6983 if (!uses_template_parms (TREE_TYPE (arg)))
6985 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6986 if (!uses_template_parms (t)
6987 && !same_type_p (t, TREE_TYPE (arg)))
6988 return 0;
6991 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6992 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6993 /* Argument is a parameter pack but parameter is not. */
6994 return 0;
6996 break;
6998 default:
6999 gcc_unreachable ();
7002 return 1;
7005 /* Coerce template argument list ARGLIST for use with template
7006 template-parameter TEMPL. */
7008 static tree
7009 coerce_template_args_for_ttp (tree templ, tree arglist,
7010 tsubst_flags_t complain)
7012 /* Consider an example where a template template parameter declared as
7014 template <class T, class U = std::allocator<T> > class TT
7016 The template parameter level of T and U are one level larger than
7017 of TT. To proper process the default argument of U, say when an
7018 instantiation `TT<int>' is seen, we need to build the full
7019 arguments containing {int} as the innermost level. Outer levels,
7020 available when not appearing as default template argument, can be
7021 obtained from the arguments of the enclosing template.
7023 Suppose that TT is later substituted with std::vector. The above
7024 instantiation is `TT<int, std::allocator<T> >' with TT at
7025 level 1, and T at level 2, while the template arguments at level 1
7026 becomes {std::vector} and the inner level 2 is {int}. */
7028 tree outer = DECL_CONTEXT (templ);
7029 if (outer)
7031 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7032 /* We want arguments for the partial specialization, not arguments for
7033 the primary template. */
7034 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7035 else
7036 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7038 else if (current_template_parms)
7040 /* This is an argument of the current template, so we haven't set
7041 DECL_CONTEXT yet. */
7042 tree relevant_template_parms;
7044 /* Parameter levels that are greater than the level of the given
7045 template template parm are irrelevant. */
7046 relevant_template_parms = current_template_parms;
7047 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7048 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7049 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7051 outer = template_parms_to_args (relevant_template_parms);
7054 if (outer)
7055 arglist = add_to_template_args (outer, arglist);
7057 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7058 return coerce_template_parms (parmlist, arglist, templ,
7059 complain,
7060 /*require_all_args=*/true,
7061 /*use_default_args=*/true);
7064 /* A cache of template template parameters with match-all default
7065 arguments. */
7066 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7067 static void
7068 store_defaulted_ttp (tree v, tree t)
7070 if (!defaulted_ttp_cache)
7071 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7072 defaulted_ttp_cache->put (v, t);
7074 static tree
7075 lookup_defaulted_ttp (tree v)
7077 if (defaulted_ttp_cache)
7078 if (tree *p = defaulted_ttp_cache->get (v))
7079 return *p;
7080 return NULL_TREE;
7083 /* T is a bound template template-parameter. Copy its arguments into default
7084 arguments of the template template-parameter's template parameters. */
7086 static tree
7087 add_defaults_to_ttp (tree otmpl)
7089 if (tree c = lookup_defaulted_ttp (otmpl))
7090 return c;
7092 tree ntmpl = copy_node (otmpl);
7094 tree ntype = copy_node (TREE_TYPE (otmpl));
7095 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7096 TYPE_MAIN_VARIANT (ntype) = ntype;
7097 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7098 TYPE_NAME (ntype) = ntmpl;
7099 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7101 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7102 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7103 TEMPLATE_PARM_DECL (idx) = ntmpl;
7104 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7106 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7107 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7108 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7109 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7110 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7112 tree o = TREE_VEC_ELT (vec, i);
7113 if (!template_parameter_pack_p (TREE_VALUE (o)))
7115 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7116 TREE_PURPOSE (n) = any_targ_node;
7120 store_defaulted_ttp (otmpl, ntmpl);
7121 return ntmpl;
7124 /* ARG is a bound potential template template-argument, and PARGS is a list
7125 of arguments for the corresponding template template-parameter. Adjust
7126 PARGS as appropriate for application to ARG's template, and if ARG is a
7127 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7128 arguments to the template template parameter. */
7130 static tree
7131 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7133 ++processing_template_decl;
7134 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7137 /* When comparing two template template-parameters in partial ordering,
7138 rewrite the one currently being used as an argument to have default
7139 arguments for all parameters. */
7140 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7141 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7142 if (pargs != error_mark_node)
7143 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7144 TYPE_TI_ARGS (arg));
7146 else
7148 tree aparms
7149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7150 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7151 /*require_all*/true,
7152 /*use_default*/true);
7154 --processing_template_decl;
7155 return pargs;
7158 /* Subroutine of unify for the case when PARM is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM. */
7161 static int
7162 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7163 bool explain_p)
7165 tree parmvec = TYPE_TI_ARGS (parm);
7166 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7168 /* The template template parm might be variadic and the argument
7169 not, so flatten both argument lists. */
7170 parmvec = expand_template_argument_pack (parmvec);
7171 argvec = expand_template_argument_pack (argvec);
7173 if (flag_new_ttp)
7175 /* In keeping with P0522R0, adjust P's template arguments
7176 to apply to A's template; then flatten it again. */
7177 tree nparmvec = parmvec;
7178 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7179 nparmvec = expand_template_argument_pack (nparmvec);
7181 if (unify (tparms, targs, nparmvec, argvec,
7182 UNIFY_ALLOW_NONE, explain_p))
7183 return 1;
7185 /* If the P0522 adjustment eliminated a pack expansion, deduce
7186 empty packs. */
7187 if (flag_new_ttp
7188 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7189 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7190 DEDUCE_EXACT, /*sub*/true, explain_p))
7191 return 1;
7193 else
7195 /* Deduce arguments T, i from TT<T> or TT<i>.
7196 We check each element of PARMVEC and ARGVEC individually
7197 rather than the whole TREE_VEC since they can have
7198 different number of elements, which is allowed under N2555. */
7200 int len = TREE_VEC_LENGTH (parmvec);
7202 /* Check if the parameters end in a pack, making them
7203 variadic. */
7204 int parm_variadic_p = 0;
7205 if (len > 0
7206 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7207 parm_variadic_p = 1;
7209 for (int i = 0; i < len - parm_variadic_p; ++i)
7210 /* If the template argument list of P contains a pack
7211 expansion that is not the last template argument, the
7212 entire template argument list is a non-deduced
7213 context. */
7214 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7215 return unify_success (explain_p);
7217 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7218 return unify_too_few_arguments (explain_p,
7219 TREE_VEC_LENGTH (argvec), len);
7221 for (int i = 0; i < len - parm_variadic_p; ++i)
7222 if (unify (tparms, targs,
7223 TREE_VEC_ELT (parmvec, i),
7224 TREE_VEC_ELT (argvec, i),
7225 UNIFY_ALLOW_NONE, explain_p))
7226 return 1;
7228 if (parm_variadic_p
7229 && unify_pack_expansion (tparms, targs,
7230 parmvec, argvec,
7231 DEDUCE_EXACT,
7232 /*subr=*/true, explain_p))
7233 return 1;
7236 return 0;
7239 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7240 template template parameters. Both PARM_PARMS and ARG_PARMS are
7241 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7242 or PARM_DECL.
7244 Consider the example:
7245 template <class T> class A;
7246 template<template <class U> class TT> class B;
7248 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7249 the parameters to A, and OUTER_ARGS contains A. */
7251 static int
7252 coerce_template_template_parms (tree parm_parms,
7253 tree arg_parms,
7254 tsubst_flags_t complain,
7255 tree in_decl,
7256 tree outer_args)
7258 int nparms, nargs, i;
7259 tree parm, arg;
7260 int variadic_p = 0;
7262 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7263 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7265 nparms = TREE_VEC_LENGTH (parm_parms);
7266 nargs = TREE_VEC_LENGTH (arg_parms);
7268 if (flag_new_ttp)
7270 /* P0522R0: A template template-parameter P is at least as specialized as
7271 a template template-argument A if, given the following rewrite to two
7272 function templates, the function template corresponding to P is at
7273 least as specialized as the function template corresponding to A
7274 according to the partial ordering rules for function templates
7275 ([temp.func.order]). Given an invented class template X with the
7276 template parameter list of A (including default arguments):
7278 * Each of the two function templates has the same template parameters,
7279 respectively, as P or A.
7281 * Each function template has a single function parameter whose type is
7282 a specialization of X with template arguments corresponding to the
7283 template parameters from the respective function template where, for
7284 each template parameter PP in the template parameter list of the
7285 function template, a corresponding template argument AA is formed. If
7286 PP declares a parameter pack, then AA is the pack expansion
7287 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7289 If the rewrite produces an invalid type, then P is not at least as
7290 specialized as A. */
7292 /* So coerce P's args to apply to A's parms, and then deduce between A's
7293 args and the converted args. If that succeeds, A is at least as
7294 specialized as P, so they match.*/
7295 tree pargs = template_parms_level_to_args (parm_parms);
7296 ++processing_template_decl;
7297 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7298 /*require_all*/true, /*use_default*/true);
7299 --processing_template_decl;
7300 if (pargs != error_mark_node)
7302 tree targs = make_tree_vec (nargs);
7303 tree aargs = template_parms_level_to_args (arg_parms);
7304 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7305 /*explain*/false))
7306 return 1;
7310 /* Determine whether we have a parameter pack at the end of the
7311 template template parameter's template parameter list. */
7312 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7314 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7316 if (error_operand_p (parm))
7317 return 0;
7319 switch (TREE_CODE (parm))
7321 case TEMPLATE_DECL:
7322 case TYPE_DECL:
7323 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7324 variadic_p = 1;
7325 break;
7327 case PARM_DECL:
7328 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7329 variadic_p = 1;
7330 break;
7332 default:
7333 gcc_unreachable ();
7337 if (nargs != nparms
7338 && !(variadic_p && nargs >= nparms - 1))
7339 return 0;
7341 /* Check all of the template parameters except the parameter pack at
7342 the end (if any). */
7343 for (i = 0; i < nparms - variadic_p; ++i)
7345 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7346 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7347 continue;
7349 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7350 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7352 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7353 outer_args))
7354 return 0;
7358 if (variadic_p)
7360 /* Check each of the template parameters in the template
7361 argument against the template parameter pack at the end of
7362 the template template parameter. */
7363 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7364 return 0;
7366 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7368 for (; i < nargs; ++i)
7370 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7371 continue;
7373 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7375 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7376 outer_args))
7377 return 0;
7381 return 1;
7384 /* Verifies that the deduced template arguments (in TARGS) for the
7385 template template parameters (in TPARMS) represent valid bindings,
7386 by comparing the template parameter list of each template argument
7387 to the template parameter list of its corresponding template
7388 template parameter, in accordance with DR150. This
7389 routine can only be called after all template arguments have been
7390 deduced. It will return TRUE if all of the template template
7391 parameter bindings are okay, FALSE otherwise. */
7392 bool
7393 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7395 int i, ntparms = TREE_VEC_LENGTH (tparms);
7396 bool ret = true;
7398 /* We're dealing with template parms in this process. */
7399 ++processing_template_decl;
7401 targs = INNERMOST_TEMPLATE_ARGS (targs);
7403 for (i = 0; i < ntparms; ++i)
7405 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7406 tree targ = TREE_VEC_ELT (targs, i);
7408 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7410 tree packed_args = NULL_TREE;
7411 int idx, len = 1;
7413 if (ARGUMENT_PACK_P (targ))
7415 /* Look inside the argument pack. */
7416 packed_args = ARGUMENT_PACK_ARGS (targ);
7417 len = TREE_VEC_LENGTH (packed_args);
7420 for (idx = 0; idx < len; ++idx)
7422 tree targ_parms = NULL_TREE;
7424 if (packed_args)
7425 /* Extract the next argument from the argument
7426 pack. */
7427 targ = TREE_VEC_ELT (packed_args, idx);
7429 if (PACK_EXPANSION_P (targ))
7430 /* Look at the pattern of the pack expansion. */
7431 targ = PACK_EXPANSION_PATTERN (targ);
7433 /* Extract the template parameters from the template
7434 argument. */
7435 if (TREE_CODE (targ) == TEMPLATE_DECL)
7436 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7437 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7438 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7440 /* Verify that we can coerce the template template
7441 parameters from the template argument to the template
7442 parameter. This requires an exact match. */
7443 if (targ_parms
7444 && !coerce_template_template_parms
7445 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7446 targ_parms,
7447 tf_none,
7448 tparm,
7449 targs))
7451 ret = false;
7452 goto out;
7458 out:
7460 --processing_template_decl;
7461 return ret;
7464 /* Since type attributes aren't mangled, we need to strip them from
7465 template type arguments. */
7467 static tree
7468 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7470 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7471 return arg;
7472 bool removed_attributes = false;
7473 tree canon = strip_typedefs (arg, &removed_attributes);
7474 if (removed_attributes
7475 && (complain & tf_warning))
7476 warning (OPT_Wignored_attributes,
7477 "ignoring attributes on template argument %qT", arg);
7478 return canon;
7481 /* And from inside dependent non-type arguments like sizeof(Type). */
7483 static tree
7484 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7486 if (!arg || arg == error_mark_node)
7487 return arg;
7488 bool removed_attributes = false;
7489 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7490 if (removed_attributes
7491 && (complain & tf_warning))
7492 warning (OPT_Wignored_attributes,
7493 "ignoring attributes in template argument %qE", arg);
7494 return canon;
7497 // A template declaration can be substituted for a constrained
7498 // template template parameter only when the argument is more
7499 // constrained than the parameter.
7500 static bool
7501 is_compatible_template_arg (tree parm, tree arg)
7503 tree parm_cons = get_constraints (parm);
7505 /* For now, allow constrained template template arguments
7506 and unconstrained template template parameters. */
7507 if (parm_cons == NULL_TREE)
7508 return true;
7510 tree arg_cons = get_constraints (arg);
7512 // If the template parameter is constrained, we need to rewrite its
7513 // constraints in terms of the ARG's template parameters. This ensures
7514 // that all of the template parameter types will have the same depth.
7516 // Note that this is only valid when coerce_template_template_parm is
7517 // true for the innermost template parameters of PARM and ARG. In other
7518 // words, because coercion is successful, this conversion will be valid.
7519 if (parm_cons)
7521 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7522 parm_cons = tsubst_constraint_info (parm_cons,
7523 INNERMOST_TEMPLATE_ARGS (args),
7524 tf_none, NULL_TREE);
7525 if (parm_cons == error_mark_node)
7526 return false;
7529 return subsumes (parm_cons, arg_cons);
7532 // Convert a placeholder argument into a binding to the original
7533 // parameter. The original parameter is saved as the TREE_TYPE of
7534 // ARG.
7535 static inline tree
7536 convert_wildcard_argument (tree parm, tree arg)
7538 TREE_TYPE (arg) = parm;
7539 return arg;
7542 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7543 because one of them is dependent. But we need to represent the
7544 conversion for the benefit of cp_tree_equal. */
7546 static tree
7547 maybe_convert_nontype_argument (tree type, tree arg)
7549 /* Auto parms get no conversion. */
7550 if (type_uses_auto (type))
7551 return arg;
7552 /* We don't need or want to add this conversion now if we're going to use the
7553 argument for deduction. */
7554 if (value_dependent_expression_p (arg))
7555 return arg;
7557 type = cv_unqualified (type);
7558 tree argtype = TREE_TYPE (arg);
7559 if (same_type_p (type, argtype))
7560 return arg;
7562 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7563 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7564 return arg;
7567 /* Convert the indicated template ARG as necessary to match the
7568 indicated template PARM. Returns the converted ARG, or
7569 error_mark_node if the conversion was unsuccessful. Error and
7570 warning messages are issued under control of COMPLAIN. This
7571 conversion is for the Ith parameter in the parameter list. ARGS is
7572 the full set of template arguments deduced so far. */
7574 static tree
7575 convert_template_argument (tree parm,
7576 tree arg,
7577 tree args,
7578 tsubst_flags_t complain,
7579 int i,
7580 tree in_decl)
7582 tree orig_arg;
7583 tree val;
7584 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7586 if (parm == error_mark_node)
7587 return error_mark_node;
7589 /* Trivially convert placeholders. */
7590 if (TREE_CODE (arg) == WILDCARD_DECL)
7591 return convert_wildcard_argument (parm, arg);
7593 if (arg == any_targ_node)
7594 return arg;
7596 if (TREE_CODE (arg) == TREE_LIST
7597 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7599 /* The template argument was the name of some
7600 member function. That's usually
7601 invalid, but static members are OK. In any
7602 case, grab the underlying fields/functions
7603 and issue an error later if required. */
7604 orig_arg = TREE_VALUE (arg);
7605 TREE_TYPE (arg) = unknown_type_node;
7608 orig_arg = arg;
7610 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7611 requires_type = (TREE_CODE (parm) == TYPE_DECL
7612 || requires_tmpl_type);
7614 /* When determining whether an argument pack expansion is a template,
7615 look at the pattern. */
7616 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7617 arg = PACK_EXPANSION_PATTERN (arg);
7619 /* Deal with an injected-class-name used as a template template arg. */
7620 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7622 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7623 if (TREE_CODE (t) == TEMPLATE_DECL)
7625 if (cxx_dialect >= cxx11)
7626 /* OK under DR 1004. */;
7627 else if (complain & tf_warning_or_error)
7628 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7629 " used as template template argument", TYPE_NAME (arg));
7630 else if (flag_pedantic_errors)
7631 t = arg;
7633 arg = t;
7637 is_tmpl_type =
7638 ((TREE_CODE (arg) == TEMPLATE_DECL
7639 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7640 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7641 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7642 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7644 if (is_tmpl_type
7645 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7646 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7647 arg = TYPE_STUB_DECL (arg);
7649 is_type = TYPE_P (arg) || is_tmpl_type;
7651 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7652 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7654 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7656 if (complain & tf_error)
7657 error ("invalid use of destructor %qE as a type", orig_arg);
7658 return error_mark_node;
7661 permerror (input_location,
7662 "to refer to a type member of a template parameter, "
7663 "use %<typename %E%>", orig_arg);
7665 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7666 TREE_OPERAND (arg, 1),
7667 typename_type,
7668 complain);
7669 arg = orig_arg;
7670 is_type = 1;
7672 if (is_type != requires_type)
7674 if (in_decl)
7676 if (complain & tf_error)
7678 error ("type/value mismatch at argument %d in template "
7679 "parameter list for %qD",
7680 i + 1, in_decl);
7681 if (is_type)
7682 inform (input_location,
7683 " expected a constant of type %qT, got %qT",
7684 TREE_TYPE (parm),
7685 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7686 else if (requires_tmpl_type)
7687 inform (input_location,
7688 " expected a class template, got %qE", orig_arg);
7689 else
7690 inform (input_location,
7691 " expected a type, got %qE", orig_arg);
7694 return error_mark_node;
7696 if (is_tmpl_type ^ requires_tmpl_type)
7698 if (in_decl && (complain & tf_error))
7700 error ("type/value mismatch at argument %d in template "
7701 "parameter list for %qD",
7702 i + 1, in_decl);
7703 if (is_tmpl_type)
7704 inform (input_location,
7705 " expected a type, got %qT", DECL_NAME (arg));
7706 else
7707 inform (input_location,
7708 " expected a class template, got %qT", orig_arg);
7710 return error_mark_node;
7713 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7714 /* We already did the appropriate conversion when packing args. */
7715 val = orig_arg;
7716 else if (is_type)
7718 if (requires_tmpl_type)
7720 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7721 /* The number of argument required is not known yet.
7722 Just accept it for now. */
7723 val = orig_arg;
7724 else
7726 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7727 tree argparm;
7729 /* Strip alias templates that are equivalent to another
7730 template. */
7731 arg = get_underlying_template (arg);
7732 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7734 if (coerce_template_template_parms (parmparm, argparm,
7735 complain, in_decl,
7736 args))
7738 val = arg;
7740 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7741 TEMPLATE_DECL. */
7742 if (val != error_mark_node)
7744 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7745 val = TREE_TYPE (val);
7746 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7747 val = make_pack_expansion (val, complain);
7750 else
7752 if (in_decl && (complain & tf_error))
7754 error ("type/value mismatch at argument %d in "
7755 "template parameter list for %qD",
7756 i + 1, in_decl);
7757 inform (input_location,
7758 " expected a template of type %qD, got %qT",
7759 parm, orig_arg);
7762 val = error_mark_node;
7765 // Check that the constraints are compatible before allowing the
7766 // substitution.
7767 if (val != error_mark_node)
7768 if (!is_compatible_template_arg (parm, arg))
7770 if (in_decl && (complain & tf_error))
7772 error ("constraint mismatch at argument %d in "
7773 "template parameter list for %qD",
7774 i + 1, in_decl);
7775 inform (input_location, " expected %qD but got %qD",
7776 parm, arg);
7778 val = error_mark_node;
7782 else
7783 val = orig_arg;
7784 /* We only form one instance of each template specialization.
7785 Therefore, if we use a non-canonical variant (i.e., a
7786 typedef), any future messages referring to the type will use
7787 the typedef, which is confusing if those future uses do not
7788 themselves also use the typedef. */
7789 if (TYPE_P (val))
7790 val = canonicalize_type_argument (val, complain);
7792 else
7794 tree t = TREE_TYPE (parm);
7796 if (tree a = type_uses_auto (t))
7798 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7799 if (t == error_mark_node)
7800 return error_mark_node;
7802 else
7803 t = tsubst (t, args, complain, in_decl);
7805 if (invalid_nontype_parm_type_p (t, complain))
7806 return error_mark_node;
7808 if (!type_dependent_expression_p (orig_arg)
7809 && !uses_template_parms (t))
7810 /* We used to call digest_init here. However, digest_init
7811 will report errors, which we don't want when complain
7812 is zero. More importantly, digest_init will try too
7813 hard to convert things: for example, `0' should not be
7814 converted to pointer type at this point according to
7815 the standard. Accepting this is not merely an
7816 extension, since deciding whether or not these
7817 conversions can occur is part of determining which
7818 function template to call, or whether a given explicit
7819 argument specification is valid. */
7820 val = convert_nontype_argument (t, orig_arg, complain);
7821 else
7823 val = canonicalize_expr_argument (orig_arg, complain);
7824 val = maybe_convert_nontype_argument (t, val);
7828 if (val == NULL_TREE)
7829 val = error_mark_node;
7830 else if (val == error_mark_node && (complain & tf_error))
7831 error ("could not convert template argument %qE from %qT to %qT",
7832 orig_arg, TREE_TYPE (orig_arg), t);
7834 if (INDIRECT_REF_P (val))
7836 /* Reject template arguments that are references to built-in
7837 functions with no library fallbacks. */
7838 const_tree inner = TREE_OPERAND (val, 0);
7839 const_tree innertype = TREE_TYPE (inner);
7840 if (innertype
7841 && TREE_CODE (innertype) == REFERENCE_TYPE
7842 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7843 && TREE_OPERAND_LENGTH (inner) > 0
7844 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7845 return error_mark_node;
7848 if (TREE_CODE (val) == SCOPE_REF)
7850 /* Strip typedefs from the SCOPE_REF. */
7851 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7852 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7853 complain);
7854 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7855 QUALIFIED_NAME_IS_TEMPLATE (val));
7859 return val;
7862 /* Coerces the remaining template arguments in INNER_ARGS (from
7863 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7864 Returns the coerced argument pack. PARM_IDX is the position of this
7865 parameter in the template parameter list. ARGS is the original
7866 template argument list. */
7867 static tree
7868 coerce_template_parameter_pack (tree parms,
7869 int parm_idx,
7870 tree args,
7871 tree inner_args,
7872 int arg_idx,
7873 tree new_args,
7874 int* lost,
7875 tree in_decl,
7876 tsubst_flags_t complain)
7878 tree parm = TREE_VEC_ELT (parms, parm_idx);
7879 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7880 tree packed_args;
7881 tree argument_pack;
7882 tree packed_parms = NULL_TREE;
7884 if (arg_idx > nargs)
7885 arg_idx = nargs;
7887 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7889 /* When the template parameter is a non-type template parameter pack
7890 or template template parameter pack whose type or template
7891 parameters use parameter packs, we know exactly how many arguments
7892 we are looking for. Build a vector of the instantiated decls for
7893 these template parameters in PACKED_PARMS. */
7894 /* We can't use make_pack_expansion here because it would interpret a
7895 _DECL as a use rather than a declaration. */
7896 tree decl = TREE_VALUE (parm);
7897 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7898 SET_PACK_EXPANSION_PATTERN (exp, decl);
7899 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7900 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7902 TREE_VEC_LENGTH (args)--;
7903 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7904 TREE_VEC_LENGTH (args)++;
7906 if (packed_parms == error_mark_node)
7907 return error_mark_node;
7909 /* If we're doing a partial instantiation of a member template,
7910 verify that all of the types used for the non-type
7911 template parameter pack are, in fact, valid for non-type
7912 template parameters. */
7913 if (arg_idx < nargs
7914 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7916 int j, len = TREE_VEC_LENGTH (packed_parms);
7917 for (j = 0; j < len; ++j)
7919 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7920 if (invalid_nontype_parm_type_p (t, complain))
7921 return error_mark_node;
7923 /* We don't know how many args we have yet, just
7924 use the unconverted ones for now. */
7925 return NULL_TREE;
7928 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7930 /* Check if we have a placeholder pack, which indicates we're
7931 in the context of a introduction list. In that case we want
7932 to match this pack to the single placeholder. */
7933 else if (arg_idx < nargs
7934 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7935 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7937 nargs = arg_idx + 1;
7938 packed_args = make_tree_vec (1);
7940 else
7941 packed_args = make_tree_vec (nargs - arg_idx);
7943 /* Convert the remaining arguments, which will be a part of the
7944 parameter pack "parm". */
7945 int first_pack_arg = arg_idx;
7946 for (; arg_idx < nargs; ++arg_idx)
7948 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7949 tree actual_parm = TREE_VALUE (parm);
7950 int pack_idx = arg_idx - first_pack_arg;
7952 if (packed_parms)
7954 /* Once we've packed as many args as we have types, stop. */
7955 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7956 break;
7957 else if (PACK_EXPANSION_P (arg))
7958 /* We don't know how many args we have yet, just
7959 use the unconverted ones for now. */
7960 return NULL_TREE;
7961 else
7962 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7965 if (arg == error_mark_node)
7967 if (complain & tf_error)
7968 error ("template argument %d is invalid", arg_idx + 1);
7970 else
7971 arg = convert_template_argument (actual_parm,
7972 arg, new_args, complain, parm_idx,
7973 in_decl);
7974 if (arg == error_mark_node)
7975 (*lost)++;
7976 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7979 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7980 && TREE_VEC_LENGTH (packed_args) > 0)
7982 if (complain & tf_error)
7983 error ("wrong number of template arguments (%d, should be %d)",
7984 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7985 return error_mark_node;
7988 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7989 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7990 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7991 else
7993 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7994 TREE_CONSTANT (argument_pack) = 1;
7997 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7998 if (CHECKING_P)
7999 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8000 TREE_VEC_LENGTH (packed_args));
8001 return argument_pack;
8004 /* Returns the number of pack expansions in the template argument vector
8005 ARGS. */
8007 static int
8008 pack_expansion_args_count (tree args)
8010 int i;
8011 int count = 0;
8012 if (args)
8013 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8015 tree elt = TREE_VEC_ELT (args, i);
8016 if (elt && PACK_EXPANSION_P (elt))
8017 ++count;
8019 return count;
8022 /* Convert all template arguments to their appropriate types, and
8023 return a vector containing the innermost resulting template
8024 arguments. If any error occurs, return error_mark_node. Error and
8025 warning messages are issued under control of COMPLAIN.
8027 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8028 for arguments not specified in ARGS. Otherwise, if
8029 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8030 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8031 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8032 ARGS. */
8034 static tree
8035 coerce_template_parms (tree parms,
8036 tree args,
8037 tree in_decl,
8038 tsubst_flags_t complain,
8039 bool require_all_args,
8040 bool use_default_args)
8042 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8043 tree orig_inner_args;
8044 tree inner_args;
8045 tree new_args;
8046 tree new_inner_args;
8047 int saved_unevaluated_operand;
8048 int saved_inhibit_evaluation_warnings;
8050 /* When used as a boolean value, indicates whether this is a
8051 variadic template parameter list. Since it's an int, we can also
8052 subtract it from nparms to get the number of non-variadic
8053 parameters. */
8054 int variadic_p = 0;
8055 int variadic_args_p = 0;
8056 int post_variadic_parms = 0;
8058 /* Likewise for parameters with default arguments. */
8059 int default_p = 0;
8061 if (args == error_mark_node)
8062 return error_mark_node;
8064 nparms = TREE_VEC_LENGTH (parms);
8066 /* Determine if there are any parameter packs or default arguments. */
8067 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8069 tree parm = TREE_VEC_ELT (parms, parm_idx);
8070 if (variadic_p)
8071 ++post_variadic_parms;
8072 if (template_parameter_pack_p (TREE_VALUE (parm)))
8073 ++variadic_p;
8074 if (TREE_PURPOSE (parm))
8075 ++default_p;
8078 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8079 /* If there are no parameters that follow a parameter pack, we need to
8080 expand any argument packs so that we can deduce a parameter pack from
8081 some non-packed args followed by an argument pack, as in variadic85.C.
8082 If there are such parameters, we need to leave argument packs intact
8083 so the arguments are assigned properly. This can happen when dealing
8084 with a nested class inside a partial specialization of a class
8085 template, as in variadic92.C, or when deducing a template parameter pack
8086 from a sub-declarator, as in variadic114.C. */
8087 if (!post_variadic_parms)
8088 inner_args = expand_template_argument_pack (inner_args);
8090 /* Count any pack expansion args. */
8091 variadic_args_p = pack_expansion_args_count (inner_args);
8093 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8094 if ((nargs - variadic_args_p > nparms && !variadic_p)
8095 || (nargs < nparms - variadic_p
8096 && require_all_args
8097 && !variadic_args_p
8098 && (!use_default_args
8099 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8100 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8102 if (complain & tf_error)
8104 if (variadic_p || default_p)
8106 nparms -= variadic_p + default_p;
8107 error ("wrong number of template arguments "
8108 "(%d, should be at least %d)", nargs, nparms);
8110 else
8111 error ("wrong number of template arguments "
8112 "(%d, should be %d)", nargs, nparms);
8114 if (in_decl)
8115 inform (DECL_SOURCE_LOCATION (in_decl),
8116 "provided for %qD", in_decl);
8119 return error_mark_node;
8121 /* We can't pass a pack expansion to a non-pack parameter of an alias
8122 template (DR 1430). */
8123 else if (in_decl
8124 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8125 || concept_template_p (in_decl))
8126 && variadic_args_p
8127 && nargs - variadic_args_p < nparms - variadic_p)
8129 if (complain & tf_error)
8131 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8133 tree arg = TREE_VEC_ELT (inner_args, i);
8134 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8136 if (PACK_EXPANSION_P (arg)
8137 && !template_parameter_pack_p (parm))
8139 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8140 error_at (location_of (arg),
8141 "pack expansion argument for non-pack parameter "
8142 "%qD of alias template %qD", parm, in_decl);
8143 else
8144 error_at (location_of (arg),
8145 "pack expansion argument for non-pack parameter "
8146 "%qD of concept %qD", parm, in_decl);
8147 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8148 goto found;
8151 gcc_unreachable ();
8152 found:;
8154 return error_mark_node;
8157 /* We need to evaluate the template arguments, even though this
8158 template-id may be nested within a "sizeof". */
8159 saved_unevaluated_operand = cp_unevaluated_operand;
8160 cp_unevaluated_operand = 0;
8161 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8162 c_inhibit_evaluation_warnings = 0;
8163 new_inner_args = make_tree_vec (nparms);
8164 new_args = add_outermost_template_args (args, new_inner_args);
8165 int pack_adjust = 0;
8166 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8168 tree arg;
8169 tree parm;
8171 /* Get the Ith template parameter. */
8172 parm = TREE_VEC_ELT (parms, parm_idx);
8174 if (parm == error_mark_node)
8176 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8177 continue;
8180 /* Calculate the next argument. */
8181 if (arg_idx < nargs)
8182 arg = TREE_VEC_ELT (inner_args, arg_idx);
8183 else
8184 arg = NULL_TREE;
8186 if (template_parameter_pack_p (TREE_VALUE (parm))
8187 && !(arg && ARGUMENT_PACK_P (arg)))
8189 /* Some arguments will be placed in the
8190 template parameter pack PARM. */
8191 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8192 inner_args, arg_idx,
8193 new_args, &lost,
8194 in_decl, complain);
8196 if (arg == NULL_TREE)
8198 /* We don't know how many args we have yet, just use the
8199 unconverted (and still packed) ones for now. */
8200 new_inner_args = orig_inner_args;
8201 arg_idx = nargs;
8202 break;
8205 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8207 /* Store this argument. */
8208 if (arg == error_mark_node)
8210 lost++;
8211 /* We are done with all of the arguments. */
8212 arg_idx = nargs;
8214 else
8216 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8217 arg_idx += pack_adjust;
8220 continue;
8222 else if (arg)
8224 if (PACK_EXPANSION_P (arg))
8226 /* "If every valid specialization of a variadic template
8227 requires an empty template parameter pack, the template is
8228 ill-formed, no diagnostic required." So check that the
8229 pattern works with this parameter. */
8230 tree pattern = PACK_EXPANSION_PATTERN (arg);
8231 tree conv = convert_template_argument (TREE_VALUE (parm),
8232 pattern, new_args,
8233 complain, parm_idx,
8234 in_decl);
8235 if (conv == error_mark_node)
8237 if (complain & tf_error)
8238 inform (input_location, "so any instantiation with a "
8239 "non-empty parameter pack would be ill-formed");
8240 ++lost;
8242 else if (TYPE_P (conv) && !TYPE_P (pattern))
8243 /* Recover from missing typename. */
8244 TREE_VEC_ELT (inner_args, arg_idx)
8245 = make_pack_expansion (conv, complain);
8247 /* We don't know how many args we have yet, just
8248 use the unconverted ones for now. */
8249 new_inner_args = inner_args;
8250 arg_idx = nargs;
8251 break;
8254 else if (require_all_args)
8256 /* There must be a default arg in this case. */
8257 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8258 complain, in_decl);
8259 /* The position of the first default template argument,
8260 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8261 Record that. */
8262 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8264 arg_idx - pack_adjust);
8266 else
8267 break;
8269 if (arg == error_mark_node)
8271 if (complain & tf_error)
8272 error ("template argument %d is invalid", arg_idx + 1);
8274 else if (!arg)
8275 /* This only occurs if there was an error in the template
8276 parameter list itself (which we would already have
8277 reported) that we are trying to recover from, e.g., a class
8278 template with a parameter list such as
8279 template<typename..., typename>. */
8280 ++lost;
8281 else
8282 arg = convert_template_argument (TREE_VALUE (parm),
8283 arg, new_args, complain,
8284 parm_idx, in_decl);
8286 if (arg == error_mark_node)
8287 lost++;
8288 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8290 cp_unevaluated_operand = saved_unevaluated_operand;
8291 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8293 if (variadic_p && arg_idx < nargs)
8295 if (complain & tf_error)
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs, arg_idx);
8299 if (in_decl)
8300 error ("provided for %q+D", in_decl);
8302 return error_mark_node;
8305 if (lost)
8306 return error_mark_node;
8308 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8310 TREE_VEC_LENGTH (new_inner_args));
8312 return new_inner_args;
8315 /* Convert all template arguments to their appropriate types, and
8316 return a vector containing the innermost resulting template
8317 arguments. If any error occurs, return error_mark_node. Error and
8318 warning messages are not issued.
8320 Note that no function argument deduction is performed, and default
8321 arguments are used to fill in unspecified arguments. */
8322 tree
8323 coerce_template_parms (tree parms, tree args, tree in_decl)
8325 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8328 /* Convert all template arguments to their appropriate type, and
8329 instantiate default arguments as needed. This returns a vector
8330 containing the innermost resulting template arguments, or
8331 error_mark_node if unsuccessful. */
8332 tree
8333 coerce_template_parms (tree parms, tree args, tree in_decl,
8334 tsubst_flags_t complain)
8336 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8339 /* Like coerce_template_parms. If PARMS represents all template
8340 parameters levels, this function returns a vector of vectors
8341 representing all the resulting argument levels. Note that in this
8342 case, only the innermost arguments are coerced because the
8343 outermost ones are supposed to have been coerced already.
8345 Otherwise, if PARMS represents only (the innermost) vector of
8346 parameters, this function returns a vector containing just the
8347 innermost resulting arguments. */
8349 static tree
8350 coerce_innermost_template_parms (tree parms,
8351 tree args,
8352 tree in_decl,
8353 tsubst_flags_t complain,
8354 bool require_all_args,
8355 bool use_default_args)
8357 int parms_depth = TMPL_PARMS_DEPTH (parms);
8358 int args_depth = TMPL_ARGS_DEPTH (args);
8359 tree coerced_args;
8361 if (parms_depth > 1)
8363 coerced_args = make_tree_vec (parms_depth);
8364 tree level;
8365 int cur_depth;
8367 for (level = parms, cur_depth = parms_depth;
8368 parms_depth > 0 && level != NULL_TREE;
8369 level = TREE_CHAIN (level), --cur_depth)
8371 tree l;
8372 if (cur_depth == args_depth)
8373 l = coerce_template_parms (TREE_VALUE (level),
8374 args, in_decl, complain,
8375 require_all_args,
8376 use_default_args);
8377 else
8378 l = TMPL_ARGS_LEVEL (args, cur_depth);
8380 if (l == error_mark_node)
8381 return error_mark_node;
8383 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8386 else
8387 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8388 args, in_decl, complain,
8389 require_all_args,
8390 use_default_args);
8391 return coerced_args;
8394 /* Returns 1 if template args OT and NT are equivalent. */
8397 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8399 if (nt == ot)
8400 return 1;
8401 if (nt == NULL_TREE || ot == NULL_TREE)
8402 return false;
8403 if (nt == any_targ_node || ot == any_targ_node)
8404 return true;
8406 if (TREE_CODE (nt) == TREE_VEC)
8407 /* For member templates */
8408 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8409 else if (PACK_EXPANSION_P (ot))
8410 return (PACK_EXPANSION_P (nt)
8411 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8412 PACK_EXPANSION_PATTERN (nt))
8413 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8414 PACK_EXPANSION_EXTRA_ARGS (nt)));
8415 else if (ARGUMENT_PACK_P (ot))
8417 int i, len;
8418 tree opack, npack;
8420 if (!ARGUMENT_PACK_P (nt))
8421 return 0;
8423 opack = ARGUMENT_PACK_ARGS (ot);
8424 npack = ARGUMENT_PACK_ARGS (nt);
8425 len = TREE_VEC_LENGTH (opack);
8426 if (TREE_VEC_LENGTH (npack) != len)
8427 return 0;
8428 for (i = 0; i < len; ++i)
8429 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8430 TREE_VEC_ELT (npack, i)))
8431 return 0;
8432 return 1;
8434 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8435 gcc_unreachable ();
8436 else if (TYPE_P (nt))
8438 if (!TYPE_P (ot))
8439 return false;
8440 /* Don't treat an alias template specialization with dependent
8441 arguments as equivalent to its underlying type when used as a
8442 template argument; we need them to be distinct so that we
8443 substitute into the specialization arguments at instantiation
8444 time. And aliases can't be equivalent without being ==, so
8445 we don't need to look any deeper.
8447 During partial ordering, however, we need to treat them normally so
8448 that we can order uses of the same alias with different
8449 cv-qualification (79960). */
8450 if (!partial_order
8451 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8452 return false;
8453 else
8454 return same_type_p (ot, nt);
8456 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8457 return 0;
8458 else
8460 /* Try to treat a template non-type argument that has been converted
8461 to the parameter type as equivalent to one that hasn't yet. */
8462 for (enum tree_code code1 = TREE_CODE (ot);
8463 CONVERT_EXPR_CODE_P (code1)
8464 || code1 == NON_LVALUE_EXPR;
8465 code1 = TREE_CODE (ot))
8466 ot = TREE_OPERAND (ot, 0);
8467 for (enum tree_code code2 = TREE_CODE (nt);
8468 CONVERT_EXPR_CODE_P (code2)
8469 || code2 == NON_LVALUE_EXPR;
8470 code2 = TREE_CODE (nt))
8471 nt = TREE_OPERAND (nt, 0);
8473 return cp_tree_equal (ot, nt);
8477 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8478 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8479 NEWARG_PTR with the offending arguments if they are non-NULL. */
8482 comp_template_args (tree oldargs, tree newargs,
8483 tree *oldarg_ptr, tree *newarg_ptr,
8484 bool partial_order)
8486 int i;
8488 if (oldargs == newargs)
8489 return 1;
8491 if (!oldargs || !newargs)
8492 return 0;
8494 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8495 return 0;
8497 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8499 tree nt = TREE_VEC_ELT (newargs, i);
8500 tree ot = TREE_VEC_ELT (oldargs, i);
8502 if (! template_args_equal (ot, nt, partial_order))
8504 if (oldarg_ptr != NULL)
8505 *oldarg_ptr = ot;
8506 if (newarg_ptr != NULL)
8507 *newarg_ptr = nt;
8508 return 0;
8511 return 1;
8514 inline bool
8515 comp_template_args_porder (tree oargs, tree nargs)
8517 return comp_template_args (oargs, nargs, NULL, NULL, true);
8520 static void
8521 add_pending_template (tree d)
8523 tree ti = (TYPE_P (d)
8524 ? CLASSTYPE_TEMPLATE_INFO (d)
8525 : DECL_TEMPLATE_INFO (d));
8526 struct pending_template *pt;
8527 int level;
8529 if (TI_PENDING_TEMPLATE_FLAG (ti))
8530 return;
8532 /* We are called both from instantiate_decl, where we've already had a
8533 tinst_level pushed, and instantiate_template, where we haven't.
8534 Compensate. */
8535 level = !current_tinst_level || current_tinst_level->decl != d;
8537 if (level)
8538 push_tinst_level (d);
8540 pt = ggc_alloc<pending_template> ();
8541 pt->next = NULL;
8542 pt->tinst = current_tinst_level;
8543 if (last_pending_template)
8544 last_pending_template->next = pt;
8545 else
8546 pending_templates = pt;
8548 last_pending_template = pt;
8550 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8552 if (level)
8553 pop_tinst_level ();
8557 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8558 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8559 documentation for TEMPLATE_ID_EXPR. */
8561 tree
8562 lookup_template_function (tree fns, tree arglist)
8564 tree type;
8566 if (fns == error_mark_node || arglist == error_mark_node)
8567 return error_mark_node;
8569 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8571 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8573 error ("%q#D is not a function template", fns);
8574 return error_mark_node;
8577 if (BASELINK_P (fns))
8579 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8580 unknown_type_node,
8581 BASELINK_FUNCTIONS (fns),
8582 arglist);
8583 return fns;
8586 type = TREE_TYPE (fns);
8587 if (TREE_CODE (fns) == OVERLOAD || !type)
8588 type = unknown_type_node;
8590 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8593 /* Within the scope of a template class S<T>, the name S gets bound
8594 (in build_self_reference) to a TYPE_DECL for the class, not a
8595 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8596 or one of its enclosing classes, and that type is a template,
8597 return the associated TEMPLATE_DECL. Otherwise, the original
8598 DECL is returned.
8600 Also handle the case when DECL is a TREE_LIST of ambiguous
8601 injected-class-names from different bases. */
8603 tree
8604 maybe_get_template_decl_from_type_decl (tree decl)
8606 if (decl == NULL_TREE)
8607 return decl;
8609 /* DR 176: A lookup that finds an injected-class-name (10.2
8610 [class.member.lookup]) can result in an ambiguity in certain cases
8611 (for example, if it is found in more than one base class). If all of
8612 the injected-class-names that are found refer to specializations of
8613 the same class template, and if the name is followed by a
8614 template-argument-list, the reference refers to the class template
8615 itself and not a specialization thereof, and is not ambiguous. */
8616 if (TREE_CODE (decl) == TREE_LIST)
8618 tree t, tmpl = NULL_TREE;
8619 for (t = decl; t; t = TREE_CHAIN (t))
8621 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8622 if (!tmpl)
8623 tmpl = elt;
8624 else if (tmpl != elt)
8625 break;
8627 if (tmpl && t == NULL_TREE)
8628 return tmpl;
8629 else
8630 return decl;
8633 return (decl != NULL_TREE
8634 && DECL_SELF_REFERENCE_P (decl)
8635 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8636 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8639 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8640 parameters, find the desired type.
8642 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8644 IN_DECL, if non-NULL, is the template declaration we are trying to
8645 instantiate.
8647 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8648 the class we are looking up.
8650 Issue error and warning messages under control of COMPLAIN.
8652 If the template class is really a local class in a template
8653 function, then the FUNCTION_CONTEXT is the function in which it is
8654 being instantiated.
8656 ??? Note that this function is currently called *twice* for each
8657 template-id: the first time from the parser, while creating the
8658 incomplete type (finish_template_type), and the second type during the
8659 real instantiation (instantiate_template_class). This is surely something
8660 that we want to avoid. It also causes some problems with argument
8661 coercion (see convert_nontype_argument for more information on this). */
8663 static tree
8664 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8665 int entering_scope, tsubst_flags_t complain)
8667 tree templ = NULL_TREE, parmlist;
8668 tree t;
8669 spec_entry **slot;
8670 spec_entry *entry;
8671 spec_entry elt;
8672 hashval_t hash;
8674 if (identifier_p (d1))
8676 tree value = innermost_non_namespace_value (d1);
8677 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8678 templ = value;
8679 else
8681 if (context)
8682 push_decl_namespace (context);
8683 templ = lookup_name (d1);
8684 templ = maybe_get_template_decl_from_type_decl (templ);
8685 if (context)
8686 pop_decl_namespace ();
8688 if (templ)
8689 context = DECL_CONTEXT (templ);
8691 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8693 tree type = TREE_TYPE (d1);
8695 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8696 an implicit typename for the second A. Deal with it. */
8697 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8698 type = TREE_TYPE (type);
8700 if (CLASSTYPE_TEMPLATE_INFO (type))
8702 templ = CLASSTYPE_TI_TEMPLATE (type);
8703 d1 = DECL_NAME (templ);
8706 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8707 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8709 templ = TYPE_TI_TEMPLATE (d1);
8710 d1 = DECL_NAME (templ);
8712 else if (DECL_TYPE_TEMPLATE_P (d1))
8714 templ = d1;
8715 d1 = DECL_NAME (templ);
8716 context = DECL_CONTEXT (templ);
8718 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8720 templ = d1;
8721 d1 = DECL_NAME (templ);
8724 /* Issue an error message if we didn't find a template. */
8725 if (! templ)
8727 if (complain & tf_error)
8728 error ("%qT is not a template", d1);
8729 return error_mark_node;
8732 if (TREE_CODE (templ) != TEMPLATE_DECL
8733 /* Make sure it's a user visible template, if it was named by
8734 the user. */
8735 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8736 && !PRIMARY_TEMPLATE_P (templ)))
8738 if (complain & tf_error)
8740 error ("non-template type %qT used as a template", d1);
8741 if (in_decl)
8742 error ("for template declaration %q+D", in_decl);
8744 return error_mark_node;
8747 complain &= ~tf_user;
8749 /* An alias that just changes the name of a template is equivalent to the
8750 other template, so if any of the arguments are pack expansions, strip
8751 the alias to avoid problems with a pack expansion passed to a non-pack
8752 alias template parameter (DR 1430). */
8753 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8754 templ = get_underlying_template (templ);
8756 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8758 tree parm;
8759 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8760 if (arglist2 == error_mark_node
8761 || (!uses_template_parms (arglist2)
8762 && check_instantiated_args (templ, arglist2, complain)))
8763 return error_mark_node;
8765 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8766 return parm;
8768 else
8770 tree template_type = TREE_TYPE (templ);
8771 tree gen_tmpl;
8772 tree type_decl;
8773 tree found = NULL_TREE;
8774 int arg_depth;
8775 int parm_depth;
8776 int is_dependent_type;
8777 int use_partial_inst_tmpl = false;
8779 if (template_type == error_mark_node)
8780 /* An error occurred while building the template TEMPL, and a
8781 diagnostic has most certainly been emitted for that
8782 already. Let's propagate that error. */
8783 return error_mark_node;
8785 gen_tmpl = most_general_template (templ);
8786 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8787 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8788 arg_depth = TMPL_ARGS_DEPTH (arglist);
8790 if (arg_depth == 1 && parm_depth > 1)
8792 /* We've been given an incomplete set of template arguments.
8793 For example, given:
8795 template <class T> struct S1 {
8796 template <class U> struct S2 {};
8797 template <class U> struct S2<U*> {};
8800 we will be called with an ARGLIST of `U*', but the
8801 TEMPLATE will be `template <class T> template
8802 <class U> struct S1<T>::S2'. We must fill in the missing
8803 arguments. */
8804 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8805 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8806 arg_depth = TMPL_ARGS_DEPTH (arglist);
8809 /* Now we should have enough arguments. */
8810 gcc_assert (parm_depth == arg_depth);
8812 /* From here on, we're only interested in the most general
8813 template. */
8815 /* Calculate the BOUND_ARGS. These will be the args that are
8816 actually tsubst'd into the definition to create the
8817 instantiation. */
8818 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8819 complain,
8820 /*require_all_args=*/true,
8821 /*use_default_args=*/true);
8823 if (arglist == error_mark_node)
8824 /* We were unable to bind the arguments. */
8825 return error_mark_node;
8827 /* In the scope of a template class, explicit references to the
8828 template class refer to the type of the template, not any
8829 instantiation of it. For example, in:
8831 template <class T> class C { void f(C<T>); }
8833 the `C<T>' is just the same as `C'. Outside of the
8834 class, however, such a reference is an instantiation. */
8835 if (entering_scope
8836 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8837 || currently_open_class (template_type))
8839 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8841 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8842 return template_type;
8845 /* If we already have this specialization, return it. */
8846 elt.tmpl = gen_tmpl;
8847 elt.args = arglist;
8848 elt.spec = NULL_TREE;
8849 hash = spec_hasher::hash (&elt);
8850 entry = type_specializations->find_with_hash (&elt, hash);
8852 if (entry)
8853 return entry->spec;
8855 /* If the the template's constraints are not satisfied,
8856 then we cannot form a valid type.
8858 Note that the check is deferred until after the hash
8859 lookup. This prevents redundant checks on previously
8860 instantiated specializations. */
8861 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8863 if (complain & tf_error)
8865 error ("template constraint failure");
8866 diagnose_constraints (input_location, gen_tmpl, arglist);
8868 return error_mark_node;
8871 is_dependent_type = uses_template_parms (arglist);
8873 /* If the deduced arguments are invalid, then the binding
8874 failed. */
8875 if (!is_dependent_type
8876 && check_instantiated_args (gen_tmpl,
8877 INNERMOST_TEMPLATE_ARGS (arglist),
8878 complain))
8879 return error_mark_node;
8881 if (!is_dependent_type
8882 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8883 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8884 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8886 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8887 DECL_NAME (gen_tmpl),
8888 /*tag_scope=*/ts_global);
8889 return found;
8892 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8893 complain, in_decl);
8894 if (context == error_mark_node)
8895 return error_mark_node;
8897 if (!context)
8898 context = global_namespace;
8900 /* Create the type. */
8901 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8903 /* The user referred to a specialization of an alias
8904 template represented by GEN_TMPL.
8906 [temp.alias]/2 says:
8908 When a template-id refers to the specialization of an
8909 alias template, it is equivalent to the associated
8910 type obtained by substitution of its
8911 template-arguments for the template-parameters in the
8912 type-id of the alias template. */
8914 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8915 /* Note that the call above (by indirectly calling
8916 register_specialization in tsubst_decl) registers the
8917 TYPE_DECL representing the specialization of the alias
8918 template. So next time someone substitutes ARGLIST for
8919 the template parms into the alias template (GEN_TMPL),
8920 she'll get that TYPE_DECL back. */
8922 if (t == error_mark_node)
8923 return t;
8925 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8927 if (!is_dependent_type)
8929 set_current_access_from_decl (TYPE_NAME (template_type));
8930 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8931 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8932 arglist, complain, in_decl),
8933 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8934 arglist, complain, in_decl),
8935 SCOPED_ENUM_P (template_type), NULL);
8937 if (t == error_mark_node)
8938 return t;
8940 else
8942 /* We don't want to call start_enum for this type, since
8943 the values for the enumeration constants may involve
8944 template parameters. And, no one should be interested
8945 in the enumeration constants for such a type. */
8946 t = cxx_make_type (ENUMERAL_TYPE);
8947 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8949 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8950 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8951 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8953 else if (CLASS_TYPE_P (template_type))
8955 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8956 instantiated here. */
8957 gcc_assert (!LAMBDA_TYPE_P (template_type));
8959 t = make_class_type (TREE_CODE (template_type));
8960 CLASSTYPE_DECLARED_CLASS (t)
8961 = CLASSTYPE_DECLARED_CLASS (template_type);
8962 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8964 /* A local class. Make sure the decl gets registered properly. */
8965 if (context == current_function_decl)
8966 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8968 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8969 /* This instantiation is another name for the primary
8970 template type. Set the TYPE_CANONICAL field
8971 appropriately. */
8972 TYPE_CANONICAL (t) = template_type;
8973 else if (any_template_arguments_need_structural_equality_p (arglist))
8974 /* Some of the template arguments require structural
8975 equality testing, so this template class requires
8976 structural equality testing. */
8977 SET_TYPE_STRUCTURAL_EQUALITY (t);
8979 else
8980 gcc_unreachable ();
8982 /* If we called start_enum or pushtag above, this information
8983 will already be set up. */
8984 if (!TYPE_NAME (t))
8986 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8988 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8989 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8990 DECL_SOURCE_LOCATION (type_decl)
8991 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8993 else
8994 type_decl = TYPE_NAME (t);
8996 if (CLASS_TYPE_P (template_type))
8998 TREE_PRIVATE (type_decl)
8999 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9000 TREE_PROTECTED (type_decl)
9001 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9002 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9004 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9005 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9009 if (OVERLOAD_TYPE_P (t)
9010 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9012 static const char *tags[] = {"abi_tag", "may_alias"};
9014 for (unsigned ix = 0; ix != 2; ix++)
9016 tree attributes
9017 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9019 if (attributes)
9020 TYPE_ATTRIBUTES (t)
9021 = tree_cons (TREE_PURPOSE (attributes),
9022 TREE_VALUE (attributes),
9023 TYPE_ATTRIBUTES (t));
9027 /* Let's consider the explicit specialization of a member
9028 of a class template specialization that is implicitly instantiated,
9029 e.g.:
9030 template<class T>
9031 struct S
9033 template<class U> struct M {}; //#0
9036 template<>
9037 template<>
9038 struct S<int>::M<char> //#1
9040 int i;
9042 [temp.expl.spec]/4 says this is valid.
9044 In this case, when we write:
9045 S<int>::M<char> m;
9047 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9048 the one of #0.
9050 When we encounter #1, we want to store the partial instantiation
9051 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9053 For all cases other than this "explicit specialization of member of a
9054 class template", we just want to store the most general template into
9055 the CLASSTYPE_TI_TEMPLATE of M.
9057 This case of "explicit specialization of member of a class template"
9058 only happens when:
9059 1/ the enclosing class is an instantiation of, and therefore not
9060 the same as, the context of the most general template, and
9061 2/ we aren't looking at the partial instantiation itself, i.e.
9062 the innermost arguments are not the same as the innermost parms of
9063 the most general template.
9065 So it's only when 1/ and 2/ happens that we want to use the partial
9066 instantiation of the member template in lieu of its most general
9067 template. */
9069 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9070 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9071 /* the enclosing class must be an instantiation... */
9072 && CLASS_TYPE_P (context)
9073 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9075 TREE_VEC_LENGTH (arglist)--;
9076 ++processing_template_decl;
9077 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9078 tree partial_inst_args =
9079 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9080 arglist, complain, NULL_TREE);
9081 --processing_template_decl;
9082 TREE_VEC_LENGTH (arglist)++;
9083 if (partial_inst_args == error_mark_node)
9084 return error_mark_node;
9085 use_partial_inst_tmpl =
9086 /*...and we must not be looking at the partial instantiation
9087 itself. */
9088 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9089 partial_inst_args);
9092 if (!use_partial_inst_tmpl)
9093 /* This case is easy; there are no member templates involved. */
9094 found = gen_tmpl;
9095 else
9097 /* This is a full instantiation of a member template. Find
9098 the partial instantiation of which this is an instance. */
9100 /* Temporarily reduce by one the number of levels in the ARGLIST
9101 so as to avoid comparing the last set of arguments. */
9102 TREE_VEC_LENGTH (arglist)--;
9103 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9104 TREE_VEC_LENGTH (arglist)++;
9105 /* FOUND is either a proper class type, or an alias
9106 template specialization. In the later case, it's a
9107 TYPE_DECL, resulting from the substituting of arguments
9108 for parameters in the TYPE_DECL of the alias template
9109 done earlier. So be careful while getting the template
9110 of FOUND. */
9111 found = (TREE_CODE (found) == TEMPLATE_DECL
9112 ? found
9113 : (TREE_CODE (found) == TYPE_DECL
9114 ? DECL_TI_TEMPLATE (found)
9115 : CLASSTYPE_TI_TEMPLATE (found)));
9118 // Build template info for the new specialization.
9119 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9121 elt.spec = t;
9122 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9123 entry = ggc_alloc<spec_entry> ();
9124 *entry = elt;
9125 *slot = entry;
9127 /* Note this use of the partial instantiation so we can check it
9128 later in maybe_process_partial_specialization. */
9129 DECL_TEMPLATE_INSTANTIATIONS (found)
9130 = tree_cons (arglist, t,
9131 DECL_TEMPLATE_INSTANTIATIONS (found));
9133 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9134 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9135 /* Now that the type has been registered on the instantiations
9136 list, we set up the enumerators. Because the enumeration
9137 constants may involve the enumeration type itself, we make
9138 sure to register the type first, and then create the
9139 constants. That way, doing tsubst_expr for the enumeration
9140 constants won't result in recursive calls here; we'll find
9141 the instantiation and exit above. */
9142 tsubst_enum (template_type, t, arglist);
9144 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9145 /* If the type makes use of template parameters, the
9146 code that generates debugging information will crash. */
9147 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9149 /* Possibly limit visibility based on template args. */
9150 TREE_PUBLIC (type_decl) = 1;
9151 determine_visibility (type_decl);
9153 inherit_targ_abi_tags (t);
9155 return t;
9159 /* Wrapper for lookup_template_class_1. */
9161 tree
9162 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9163 int entering_scope, tsubst_flags_t complain)
9165 tree ret;
9166 timevar_push (TV_TEMPLATE_INST);
9167 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9168 entering_scope, complain);
9169 timevar_pop (TV_TEMPLATE_INST);
9170 return ret;
9173 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9175 tree
9176 lookup_template_variable (tree templ, tree arglist)
9178 /* The type of the expression is NULL_TREE since the template-id could refer
9179 to an explicit or partial specialization. */
9180 tree type = NULL_TREE;
9181 if (flag_concepts && variable_concept_p (templ))
9182 /* Except that concepts are always bool. */
9183 type = boolean_type_node;
9184 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9187 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9189 tree
9190 finish_template_variable (tree var, tsubst_flags_t complain)
9192 tree templ = TREE_OPERAND (var, 0);
9193 tree arglist = TREE_OPERAND (var, 1);
9195 /* We never want to return a VAR_DECL for a variable concept, since they
9196 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9197 bool concept_p = flag_concepts && variable_concept_p (templ);
9198 if (concept_p && processing_template_decl)
9199 return var;
9201 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9202 arglist = add_outermost_template_args (tmpl_args, arglist);
9204 templ = most_general_template (templ);
9205 tree parms = DECL_TEMPLATE_PARMS (templ);
9206 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9207 /*req_all*/true,
9208 /*use_default*/true);
9210 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9212 if (complain & tf_error)
9214 error ("use of invalid variable template %qE", var);
9215 diagnose_constraints (location_of (var), templ, arglist);
9217 return error_mark_node;
9220 /* If a template-id refers to a specialization of a variable
9221 concept, then the expression is true if and only if the
9222 concept's constraints are satisfied by the given template
9223 arguments.
9225 NOTE: This is an extension of Concepts Lite TS that
9226 allows constraints to be used in expressions. */
9227 if (concept_p)
9229 tree decl = DECL_TEMPLATE_RESULT (templ);
9230 return evaluate_variable_concept (decl, arglist);
9233 return instantiate_template (templ, arglist, complain);
9236 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9237 TARGS template args, and instantiate it if it's not dependent. */
9239 tree
9240 lookup_and_finish_template_variable (tree templ, tree targs,
9241 tsubst_flags_t complain)
9243 templ = lookup_template_variable (templ, targs);
9244 if (!any_dependent_template_arguments_p (targs))
9246 templ = finish_template_variable (templ, complain);
9247 mark_used (templ);
9250 return convert_from_reference (templ);
9254 struct pair_fn_data
9256 tree_fn_t fn;
9257 tree_fn_t any_fn;
9258 void *data;
9259 /* True when we should also visit template parameters that occur in
9260 non-deduced contexts. */
9261 bool include_nondeduced_p;
9262 hash_set<tree> *visited;
9265 /* Called from for_each_template_parm via walk_tree. */
9267 static tree
9268 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9270 tree t = *tp;
9271 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9272 tree_fn_t fn = pfd->fn;
9273 void *data = pfd->data;
9274 tree result = NULL_TREE;
9276 #define WALK_SUBTREE(NODE) \
9277 do \
9279 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9280 pfd->include_nondeduced_p, \
9281 pfd->any_fn); \
9282 if (result) goto out; \
9284 while (0)
9286 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9287 return t;
9289 if (TYPE_P (t)
9290 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9291 WALK_SUBTREE (TYPE_CONTEXT (t));
9293 switch (TREE_CODE (t))
9295 case RECORD_TYPE:
9296 if (TYPE_PTRMEMFUNC_P (t))
9297 break;
9298 /* Fall through. */
9300 case UNION_TYPE:
9301 case ENUMERAL_TYPE:
9302 if (!TYPE_TEMPLATE_INFO (t))
9303 *walk_subtrees = 0;
9304 else
9305 WALK_SUBTREE (TYPE_TI_ARGS (t));
9306 break;
9308 case INTEGER_TYPE:
9309 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9310 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9311 break;
9313 case METHOD_TYPE:
9314 /* Since we're not going to walk subtrees, we have to do this
9315 explicitly here. */
9316 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9317 /* Fall through. */
9319 case FUNCTION_TYPE:
9320 /* Check the return type. */
9321 WALK_SUBTREE (TREE_TYPE (t));
9323 /* Check the parameter types. Since default arguments are not
9324 instantiated until they are needed, the TYPE_ARG_TYPES may
9325 contain expressions that involve template parameters. But,
9326 no-one should be looking at them yet. And, once they're
9327 instantiated, they don't contain template parameters, so
9328 there's no point in looking at them then, either. */
9330 tree parm;
9332 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9333 WALK_SUBTREE (TREE_VALUE (parm));
9335 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9336 want walk_tree walking into them itself. */
9337 *walk_subtrees = 0;
9340 if (flag_noexcept_type)
9342 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9343 if (spec)
9344 WALK_SUBTREE (TREE_PURPOSE (spec));
9346 break;
9348 case TYPEOF_TYPE:
9349 case UNDERLYING_TYPE:
9350 if (pfd->include_nondeduced_p
9351 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9352 pfd->visited,
9353 pfd->include_nondeduced_p,
9354 pfd->any_fn))
9355 return error_mark_node;
9356 break;
9358 case FUNCTION_DECL:
9359 case VAR_DECL:
9360 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9361 WALK_SUBTREE (DECL_TI_ARGS (t));
9362 /* Fall through. */
9364 case PARM_DECL:
9365 case CONST_DECL:
9366 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9367 WALK_SUBTREE (DECL_INITIAL (t));
9368 if (DECL_CONTEXT (t)
9369 && pfd->include_nondeduced_p)
9370 WALK_SUBTREE (DECL_CONTEXT (t));
9371 break;
9373 case BOUND_TEMPLATE_TEMPLATE_PARM:
9374 /* Record template parameters such as `T' inside `TT<T>'. */
9375 WALK_SUBTREE (TYPE_TI_ARGS (t));
9376 /* Fall through. */
9378 case TEMPLATE_TEMPLATE_PARM:
9379 case TEMPLATE_TYPE_PARM:
9380 case TEMPLATE_PARM_INDEX:
9381 if (fn && (*fn)(t, data))
9382 return t;
9383 else if (!fn)
9384 return t;
9385 break;
9387 case TEMPLATE_DECL:
9388 /* A template template parameter is encountered. */
9389 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9390 WALK_SUBTREE (TREE_TYPE (t));
9392 /* Already substituted template template parameter */
9393 *walk_subtrees = 0;
9394 break;
9396 case TYPENAME_TYPE:
9397 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9398 partial instantiation. */
9399 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9400 break;
9402 case CONSTRUCTOR:
9403 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9404 && pfd->include_nondeduced_p)
9405 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9406 break;
9408 case INDIRECT_REF:
9409 case COMPONENT_REF:
9410 /* If there's no type, then this thing must be some expression
9411 involving template parameters. */
9412 if (!fn && !TREE_TYPE (t))
9413 return error_mark_node;
9414 break;
9416 case MODOP_EXPR:
9417 case CAST_EXPR:
9418 case IMPLICIT_CONV_EXPR:
9419 case REINTERPRET_CAST_EXPR:
9420 case CONST_CAST_EXPR:
9421 case STATIC_CAST_EXPR:
9422 case DYNAMIC_CAST_EXPR:
9423 case ARROW_EXPR:
9424 case DOTSTAR_EXPR:
9425 case TYPEID_EXPR:
9426 case PSEUDO_DTOR_EXPR:
9427 if (!fn)
9428 return error_mark_node;
9429 break;
9431 default:
9432 break;
9435 #undef WALK_SUBTREE
9437 /* We didn't find any template parameters we liked. */
9438 out:
9439 return result;
9442 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9443 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9444 call FN with the parameter and the DATA.
9445 If FN returns nonzero, the iteration is terminated, and
9446 for_each_template_parm returns 1. Otherwise, the iteration
9447 continues. If FN never returns a nonzero value, the value
9448 returned by for_each_template_parm is 0. If FN is NULL, it is
9449 considered to be the function which always returns 1.
9451 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9452 parameters that occur in non-deduced contexts. When false, only
9453 visits those template parameters that can be deduced. */
9455 static tree
9456 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9457 hash_set<tree> *visited,
9458 bool include_nondeduced_p,
9459 tree_fn_t any_fn)
9461 struct pair_fn_data pfd;
9462 tree result;
9464 /* Set up. */
9465 pfd.fn = fn;
9466 pfd.any_fn = any_fn;
9467 pfd.data = data;
9468 pfd.include_nondeduced_p = include_nondeduced_p;
9470 /* Walk the tree. (Conceptually, we would like to walk without
9471 duplicates, but for_each_template_parm_r recursively calls
9472 for_each_template_parm, so we would need to reorganize a fair
9473 bit to use walk_tree_without_duplicates, so we keep our own
9474 visited list.) */
9475 if (visited)
9476 pfd.visited = visited;
9477 else
9478 pfd.visited = new hash_set<tree>;
9479 result = cp_walk_tree (&t,
9480 for_each_template_parm_r,
9481 &pfd,
9482 pfd.visited);
9484 /* Clean up. */
9485 if (!visited)
9487 delete pfd.visited;
9488 pfd.visited = 0;
9491 return result;
9494 /* Returns true if T depends on any template parameter. */
9497 uses_template_parms (tree t)
9499 if (t == NULL_TREE)
9500 return false;
9502 bool dependent_p;
9503 int saved_processing_template_decl;
9505 saved_processing_template_decl = processing_template_decl;
9506 if (!saved_processing_template_decl)
9507 processing_template_decl = 1;
9508 if (TYPE_P (t))
9509 dependent_p = dependent_type_p (t);
9510 else if (TREE_CODE (t) == TREE_VEC)
9511 dependent_p = any_dependent_template_arguments_p (t);
9512 else if (TREE_CODE (t) == TREE_LIST)
9513 dependent_p = (uses_template_parms (TREE_VALUE (t))
9514 || uses_template_parms (TREE_CHAIN (t)));
9515 else if (TREE_CODE (t) == TYPE_DECL)
9516 dependent_p = dependent_type_p (TREE_TYPE (t));
9517 else if (DECL_P (t)
9518 || EXPR_P (t)
9519 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9520 || TREE_CODE (t) == OVERLOAD
9521 || BASELINK_P (t)
9522 || identifier_p (t)
9523 || TREE_CODE (t) == TRAIT_EXPR
9524 || TREE_CODE (t) == CONSTRUCTOR
9525 || CONSTANT_CLASS_P (t))
9526 dependent_p = (type_dependent_expression_p (t)
9527 || value_dependent_expression_p (t));
9528 else
9530 gcc_assert (t == error_mark_node);
9531 dependent_p = false;
9534 processing_template_decl = saved_processing_template_decl;
9536 return dependent_p;
9539 /* Returns true iff current_function_decl is an incompletely instantiated
9540 template. Useful instead of processing_template_decl because the latter
9541 is set to 0 during instantiate_non_dependent_expr. */
9543 bool
9544 in_template_function (void)
9546 tree fn = current_function_decl;
9547 bool ret;
9548 ++processing_template_decl;
9549 ret = (fn && DECL_LANG_SPECIFIC (fn)
9550 && DECL_TEMPLATE_INFO (fn)
9551 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9552 --processing_template_decl;
9553 return ret;
9556 /* Returns true if T depends on any template parameter with level LEVEL. */
9558 bool
9559 uses_template_parms_level (tree t, int level)
9561 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9562 /*include_nondeduced_p=*/true);
9565 /* Returns true if the signature of DECL depends on any template parameter from
9566 its enclosing class. */
9568 bool
9569 uses_outer_template_parms (tree decl)
9571 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9572 if (depth == 0)
9573 return false;
9574 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9575 &depth, NULL, /*include_nondeduced_p=*/true))
9576 return true;
9577 if (PRIMARY_TEMPLATE_P (decl)
9578 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9579 (DECL_TEMPLATE_PARMS (decl)),
9580 template_parm_outer_level,
9581 &depth, NULL, /*include_nondeduced_p=*/true))
9582 return true;
9583 tree ci = get_constraints (decl);
9584 if (ci)
9585 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9586 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9587 &depth, NULL, /*nondeduced*/true))
9588 return true;
9589 return false;
9592 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9593 ill-formed translation unit, i.e. a variable or function that isn't
9594 usable in a constant expression. */
9596 static inline bool
9597 neglectable_inst_p (tree d)
9599 return (DECL_P (d)
9600 && !undeduced_auto_decl (d)
9601 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9602 : decl_maybe_constant_var_p (d)));
9605 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9606 neglectable and instantiated from within an erroneous instantiation. */
9608 static bool
9609 limit_bad_template_recursion (tree decl)
9611 struct tinst_level *lev = current_tinst_level;
9612 int errs = errorcount + sorrycount;
9613 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9614 return false;
9616 for (; lev; lev = lev->next)
9617 if (neglectable_inst_p (lev->decl))
9618 break;
9620 return (lev && errs > lev->errors);
9623 static int tinst_depth;
9624 extern int max_tinst_depth;
9625 int depth_reached;
9627 static GTY(()) struct tinst_level *last_error_tinst_level;
9629 /* We're starting to instantiate D; record the template instantiation context
9630 for diagnostics and to restore it later. */
9632 bool
9633 push_tinst_level (tree d)
9635 return push_tinst_level_loc (d, input_location);
9638 /* We're starting to instantiate D; record the template instantiation context
9639 at LOC for diagnostics and to restore it later. */
9641 bool
9642 push_tinst_level_loc (tree d, location_t loc)
9644 struct tinst_level *new_level;
9646 if (tinst_depth >= max_tinst_depth)
9648 /* Tell error.c not to try to instantiate any templates. */
9649 at_eof = 2;
9650 fatal_error (input_location,
9651 "template instantiation depth exceeds maximum of %d"
9652 " (use -ftemplate-depth= to increase the maximum)",
9653 max_tinst_depth);
9654 return false;
9657 /* If the current instantiation caused problems, don't let it instantiate
9658 anything else. Do allow deduction substitution and decls usable in
9659 constant expressions. */
9660 if (limit_bad_template_recursion (d))
9661 return false;
9663 /* When not -quiet, dump template instantiations other than functions, since
9664 announce_function will take care of those. */
9665 if (!quiet_flag
9666 && TREE_CODE (d) != TREE_LIST
9667 && TREE_CODE (d) != FUNCTION_DECL)
9668 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9670 new_level = ggc_alloc<tinst_level> ();
9671 new_level->decl = d;
9672 new_level->locus = loc;
9673 new_level->errors = errorcount+sorrycount;
9674 new_level->in_system_header_p = in_system_header_at (input_location);
9675 new_level->next = current_tinst_level;
9676 current_tinst_level = new_level;
9678 ++tinst_depth;
9679 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9680 depth_reached = tinst_depth;
9682 return true;
9685 /* We're done instantiating this template; return to the instantiation
9686 context. */
9688 void
9689 pop_tinst_level (void)
9691 /* Restore the filename and line number stashed away when we started
9692 this instantiation. */
9693 input_location = current_tinst_level->locus;
9694 current_tinst_level = current_tinst_level->next;
9695 --tinst_depth;
9698 /* We're instantiating a deferred template; restore the template
9699 instantiation context in which the instantiation was requested, which
9700 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9702 static tree
9703 reopen_tinst_level (struct tinst_level *level)
9705 struct tinst_level *t;
9707 tinst_depth = 0;
9708 for (t = level; t; t = t->next)
9709 ++tinst_depth;
9711 current_tinst_level = level;
9712 pop_tinst_level ();
9713 if (current_tinst_level)
9714 current_tinst_level->errors = errorcount+sorrycount;
9715 return level->decl;
9718 /* Returns the TINST_LEVEL which gives the original instantiation
9719 context. */
9721 struct tinst_level *
9722 outermost_tinst_level (void)
9724 struct tinst_level *level = current_tinst_level;
9725 if (level)
9726 while (level->next)
9727 level = level->next;
9728 return level;
9731 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9732 vector of template arguments, as for tsubst.
9734 Returns an appropriate tsubst'd friend declaration. */
9736 static tree
9737 tsubst_friend_function (tree decl, tree args)
9739 tree new_friend;
9741 if (TREE_CODE (decl) == FUNCTION_DECL
9742 && DECL_TEMPLATE_INSTANTIATION (decl)
9743 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9744 /* This was a friend declared with an explicit template
9745 argument list, e.g.:
9747 friend void f<>(T);
9749 to indicate that f was a template instantiation, not a new
9750 function declaration. Now, we have to figure out what
9751 instantiation of what template. */
9753 tree template_id, arglist, fns;
9754 tree new_args;
9755 tree tmpl;
9756 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9758 /* Friend functions are looked up in the containing namespace scope.
9759 We must enter that scope, to avoid finding member functions of the
9760 current class with same name. */
9761 push_nested_namespace (ns);
9762 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9763 tf_warning_or_error, NULL_TREE,
9764 /*integral_constant_expression_p=*/false);
9765 pop_nested_namespace (ns);
9766 arglist = tsubst (DECL_TI_ARGS (decl), args,
9767 tf_warning_or_error, NULL_TREE);
9768 template_id = lookup_template_function (fns, arglist);
9770 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9771 tmpl = determine_specialization (template_id, new_friend,
9772 &new_args,
9773 /*need_member_template=*/0,
9774 TREE_VEC_LENGTH (args),
9775 tsk_none);
9776 return instantiate_template (tmpl, new_args, tf_error);
9779 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9781 /* The NEW_FRIEND will look like an instantiation, to the
9782 compiler, but is not an instantiation from the point of view of
9783 the language. For example, we might have had:
9785 template <class T> struct S {
9786 template <class U> friend void f(T, U);
9789 Then, in S<int>, template <class U> void f(int, U) is not an
9790 instantiation of anything. */
9791 if (new_friend == error_mark_node)
9792 return error_mark_node;
9794 DECL_USE_TEMPLATE (new_friend) = 0;
9795 if (TREE_CODE (decl) == TEMPLATE_DECL)
9797 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9798 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9799 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9802 /* The mangled name for the NEW_FRIEND is incorrect. The function
9803 is not a template instantiation and should not be mangled like
9804 one. Therefore, we forget the mangling here; we'll recompute it
9805 later if we need it. */
9806 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9808 SET_DECL_RTL (new_friend, NULL);
9809 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9812 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9814 tree old_decl;
9815 tree new_friend_template_info;
9816 tree new_friend_result_template_info;
9817 tree ns;
9818 int new_friend_is_defn;
9820 /* We must save some information from NEW_FRIEND before calling
9821 duplicate decls since that function will free NEW_FRIEND if
9822 possible. */
9823 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9824 new_friend_is_defn =
9825 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9826 (template_for_substitution (new_friend)))
9827 != NULL_TREE);
9828 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9830 /* This declaration is a `primary' template. */
9831 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9833 new_friend_result_template_info
9834 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9836 else
9837 new_friend_result_template_info = NULL_TREE;
9839 /* Inside pushdecl_namespace_level, we will push into the
9840 current namespace. However, the friend function should go
9841 into the namespace of the template. */
9842 ns = decl_namespace_context (new_friend);
9843 push_nested_namespace (ns);
9844 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9845 pop_nested_namespace (ns);
9847 if (old_decl == error_mark_node)
9848 return error_mark_node;
9850 if (old_decl != new_friend)
9852 /* This new friend declaration matched an existing
9853 declaration. For example, given:
9855 template <class T> void f(T);
9856 template <class U> class C {
9857 template <class T> friend void f(T) {}
9860 the friend declaration actually provides the definition
9861 of `f', once C has been instantiated for some type. So,
9862 old_decl will be the out-of-class template declaration,
9863 while new_friend is the in-class definition.
9865 But, if `f' was called before this point, the
9866 instantiation of `f' will have DECL_TI_ARGS corresponding
9867 to `T' but not to `U', references to which might appear
9868 in the definition of `f'. Previously, the most general
9869 template for an instantiation of `f' was the out-of-class
9870 version; now it is the in-class version. Therefore, we
9871 run through all specialization of `f', adding to their
9872 DECL_TI_ARGS appropriately. In particular, they need a
9873 new set of outer arguments, corresponding to the
9874 arguments for this class instantiation.
9876 The same situation can arise with something like this:
9878 friend void f(int);
9879 template <class T> class C {
9880 friend void f(T) {}
9883 when `C<int>' is instantiated. Now, `f(int)' is defined
9884 in the class. */
9886 if (!new_friend_is_defn)
9887 /* On the other hand, if the in-class declaration does
9888 *not* provide a definition, then we don't want to alter
9889 existing definitions. We can just leave everything
9890 alone. */
9892 else
9894 tree new_template = TI_TEMPLATE (new_friend_template_info);
9895 tree new_args = TI_ARGS (new_friend_template_info);
9897 /* Overwrite whatever template info was there before, if
9898 any, with the new template information pertaining to
9899 the declaration. */
9900 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9902 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9904 /* We should have called reregister_specialization in
9905 duplicate_decls. */
9906 gcc_assert (retrieve_specialization (new_template,
9907 new_args, 0)
9908 == old_decl);
9910 /* Instantiate it if the global has already been used. */
9911 if (DECL_ODR_USED (old_decl))
9912 instantiate_decl (old_decl, /*defer_ok=*/true,
9913 /*expl_inst_class_mem_p=*/false);
9915 else
9917 tree t;
9919 /* Indicate that the old function template is a partial
9920 instantiation. */
9921 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9922 = new_friend_result_template_info;
9924 gcc_assert (new_template
9925 == most_general_template (new_template));
9926 gcc_assert (new_template != old_decl);
9928 /* Reassign any specializations already in the hash table
9929 to the new more general template, and add the
9930 additional template args. */
9931 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9932 t != NULL_TREE;
9933 t = TREE_CHAIN (t))
9935 tree spec = TREE_VALUE (t);
9936 spec_entry elt;
9938 elt.tmpl = old_decl;
9939 elt.args = DECL_TI_ARGS (spec);
9940 elt.spec = NULL_TREE;
9942 decl_specializations->remove_elt (&elt);
9944 DECL_TI_ARGS (spec)
9945 = add_outermost_template_args (new_args,
9946 DECL_TI_ARGS (spec));
9948 register_specialization
9949 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9952 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9956 /* The information from NEW_FRIEND has been merged into OLD_DECL
9957 by duplicate_decls. */
9958 new_friend = old_decl;
9961 else
9963 tree context = DECL_CONTEXT (new_friend);
9964 bool dependent_p;
9966 /* In the code
9967 template <class T> class C {
9968 template <class U> friend void C1<U>::f (); // case 1
9969 friend void C2<T>::f (); // case 2
9971 we only need to make sure CONTEXT is a complete type for
9972 case 2. To distinguish between the two cases, we note that
9973 CONTEXT of case 1 remains dependent type after tsubst while
9974 this isn't true for case 2. */
9975 ++processing_template_decl;
9976 dependent_p = dependent_type_p (context);
9977 --processing_template_decl;
9979 if (!dependent_p
9980 && !complete_type_or_else (context, NULL_TREE))
9981 return error_mark_node;
9983 if (COMPLETE_TYPE_P (context))
9985 tree fn = new_friend;
9986 /* do_friend adds the TEMPLATE_DECL for any member friend
9987 template even if it isn't a member template, i.e.
9988 template <class T> friend A<T>::f();
9989 Look through it in that case. */
9990 if (TREE_CODE (fn) == TEMPLATE_DECL
9991 && !PRIMARY_TEMPLATE_P (fn))
9992 fn = DECL_TEMPLATE_RESULT (fn);
9993 /* Check to see that the declaration is really present, and,
9994 possibly obtain an improved declaration. */
9995 fn = check_classfn (context, fn, NULL_TREE);
9997 if (fn)
9998 new_friend = fn;
10002 return new_friend;
10005 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10006 template arguments, as for tsubst.
10008 Returns an appropriate tsubst'd friend type or error_mark_node on
10009 failure. */
10011 static tree
10012 tsubst_friend_class (tree friend_tmpl, tree args)
10014 tree tmpl;
10016 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10018 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10019 return TREE_TYPE (tmpl);
10022 tree context = CP_DECL_CONTEXT (friend_tmpl);
10023 if (TREE_CODE (context) == NAMESPACE_DECL)
10024 push_nested_namespace (context);
10025 else
10026 push_nested_class (context);
10028 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10029 /*non_class=*/false, /*block_p=*/false,
10030 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10032 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10034 /* The friend template has already been declared. Just
10035 check to see that the declarations match, and install any new
10036 default parameters. We must tsubst the default parameters,
10037 of course. We only need the innermost template parameters
10038 because that is all that redeclare_class_template will look
10039 at. */
10040 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10041 > TMPL_ARGS_DEPTH (args))
10043 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10044 args, tf_warning_or_error);
10045 location_t saved_input_location = input_location;
10046 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10047 tree cons = get_constraints (tmpl);
10048 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10049 input_location = saved_input_location;
10052 else
10054 /* The friend template has not already been declared. In this
10055 case, the instantiation of the template class will cause the
10056 injection of this template into the namespace scope. */
10057 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10059 if (tmpl != error_mark_node)
10061 /* The new TMPL is not an instantiation of anything, so we
10062 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10063 for the new type because that is supposed to be the
10064 corresponding template decl, i.e., TMPL. */
10065 DECL_USE_TEMPLATE (tmpl) = 0;
10066 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10067 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10068 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10069 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10071 /* It is hidden. */
10072 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10073 DECL_ANTICIPATED (tmpl)
10074 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10076 /* Inject this template into the enclosing namspace scope. */
10077 tmpl = pushdecl_namespace_level (tmpl, true);
10081 if (TREE_CODE (context) == NAMESPACE_DECL)
10082 pop_nested_namespace (context);
10083 else
10084 pop_nested_class ();
10086 return TREE_TYPE (tmpl);
10089 /* Returns zero if TYPE cannot be completed later due to circularity.
10090 Otherwise returns one. */
10092 static int
10093 can_complete_type_without_circularity (tree type)
10095 if (type == NULL_TREE || type == error_mark_node)
10096 return 0;
10097 else if (COMPLETE_TYPE_P (type))
10098 return 1;
10099 else if (TREE_CODE (type) == ARRAY_TYPE)
10100 return can_complete_type_without_circularity (TREE_TYPE (type));
10101 else if (CLASS_TYPE_P (type)
10102 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10103 return 0;
10104 else
10105 return 1;
10108 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10109 tsubst_flags_t, tree);
10111 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10112 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10114 static tree
10115 tsubst_attribute (tree t, tree *decl_p, tree args,
10116 tsubst_flags_t complain, tree in_decl)
10118 gcc_assert (ATTR_IS_DEPENDENT (t));
10120 tree val = TREE_VALUE (t);
10121 if (val == NULL_TREE)
10122 /* Nothing to do. */;
10123 else if ((flag_openmp || flag_openmp_simd)
10124 && is_attribute_p ("omp declare simd",
10125 get_attribute_name (t)))
10127 tree clauses = TREE_VALUE (val);
10128 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10129 complain, in_decl);
10130 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10131 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10132 tree parms = DECL_ARGUMENTS (*decl_p);
10133 clauses
10134 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10135 if (clauses)
10136 val = build_tree_list (NULL_TREE, clauses);
10137 else
10138 val = NULL_TREE;
10140 /* If the first attribute argument is an identifier, don't
10141 pass it through tsubst. Attributes like mode, format,
10142 cleanup and several target specific attributes expect it
10143 unmodified. */
10144 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10146 tree chain
10147 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10148 /*integral_constant_expression_p=*/false);
10149 if (chain != TREE_CHAIN (val))
10150 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10152 else if (PACK_EXPANSION_P (val))
10154 /* An attribute pack expansion. */
10155 tree purp = TREE_PURPOSE (t);
10156 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10157 if (pack == error_mark_node)
10158 return error_mark_node;
10159 int len = TREE_VEC_LENGTH (pack);
10160 tree list = NULL_TREE;
10161 tree *q = &list;
10162 for (int i = 0; i < len; ++i)
10164 tree elt = TREE_VEC_ELT (pack, i);
10165 *q = build_tree_list (purp, elt);
10166 q = &TREE_CHAIN (*q);
10168 return list;
10170 else
10171 val = tsubst_expr (val, args, complain, in_decl,
10172 /*integral_constant_expression_p=*/false);
10174 if (val != TREE_VALUE (t))
10175 return build_tree_list (TREE_PURPOSE (t), val);
10176 return t;
10179 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10180 unchanged or a new TREE_LIST chain. */
10182 static tree
10183 tsubst_attributes (tree attributes, tree args,
10184 tsubst_flags_t complain, tree in_decl)
10186 tree last_dep = NULL_TREE;
10188 for (tree t = attributes; t; t = TREE_CHAIN (t))
10189 if (ATTR_IS_DEPENDENT (t))
10191 last_dep = t;
10192 attributes = copy_list (attributes);
10193 break;
10196 if (last_dep)
10197 for (tree *p = &attributes; *p; )
10199 tree t = *p;
10200 if (ATTR_IS_DEPENDENT (t))
10202 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10203 if (subst != t)
10205 *p = subst;
10207 p = &TREE_CHAIN (*p);
10208 while (*p);
10209 *p = TREE_CHAIN (t);
10210 continue;
10213 p = &TREE_CHAIN (*p);
10216 return attributes;
10219 /* Apply any attributes which had to be deferred until instantiation
10220 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10221 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10223 static void
10224 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10225 tree args, tsubst_flags_t complain, tree in_decl)
10227 tree last_dep = NULL_TREE;
10228 tree t;
10229 tree *p;
10231 if (attributes == NULL_TREE)
10232 return;
10234 if (DECL_P (*decl_p))
10236 if (TREE_TYPE (*decl_p) == error_mark_node)
10237 return;
10238 p = &DECL_ATTRIBUTES (*decl_p);
10239 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10240 to our attributes parameter. */
10241 gcc_assert (*p == attributes);
10243 else
10245 p = &TYPE_ATTRIBUTES (*decl_p);
10246 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10247 lookup_template_class_1, and should be preserved. */
10248 gcc_assert (*p != attributes);
10249 while (*p)
10250 p = &TREE_CHAIN (*p);
10253 for (t = attributes; t; t = TREE_CHAIN (t))
10254 if (ATTR_IS_DEPENDENT (t))
10256 last_dep = t;
10257 attributes = copy_list (attributes);
10258 break;
10261 *p = attributes;
10262 if (last_dep)
10264 tree late_attrs = NULL_TREE;
10265 tree *q = &late_attrs;
10267 for (; *p; )
10269 t = *p;
10270 if (ATTR_IS_DEPENDENT (t))
10272 *p = TREE_CHAIN (t);
10273 TREE_CHAIN (t) = NULL_TREE;
10274 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10276 q = &TREE_CHAIN (*q);
10277 while (*q);
10279 else
10280 p = &TREE_CHAIN (t);
10283 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10287 /* Perform (or defer) access check for typedefs that were referenced
10288 from within the template TMPL code.
10289 This is a subroutine of instantiate_decl and instantiate_class_template.
10290 TMPL is the template to consider and TARGS is the list of arguments of
10291 that template. */
10293 static void
10294 perform_typedefs_access_check (tree tmpl, tree targs)
10296 location_t saved_location;
10297 unsigned i;
10298 qualified_typedef_usage_t *iter;
10300 if (!tmpl
10301 || (!CLASS_TYPE_P (tmpl)
10302 && TREE_CODE (tmpl) != FUNCTION_DECL))
10303 return;
10305 saved_location = input_location;
10306 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10308 tree type_decl = iter->typedef_decl;
10309 tree type_scope = iter->context;
10311 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10312 continue;
10314 if (uses_template_parms (type_decl))
10315 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10316 if (uses_template_parms (type_scope))
10317 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10319 /* Make access check error messages point to the location
10320 of the use of the typedef. */
10321 input_location = iter->locus;
10322 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10323 type_decl, type_decl,
10324 tf_warning_or_error);
10326 input_location = saved_location;
10329 static tree
10330 instantiate_class_template_1 (tree type)
10332 tree templ, args, pattern, t, member;
10333 tree typedecl;
10334 tree pbinfo;
10335 tree base_list;
10336 unsigned int saved_maximum_field_alignment;
10337 tree fn_context;
10339 if (type == error_mark_node)
10340 return error_mark_node;
10342 if (COMPLETE_OR_OPEN_TYPE_P (type)
10343 || uses_template_parms (type))
10344 return type;
10346 /* Figure out which template is being instantiated. */
10347 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10348 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10350 /* Determine what specialization of the original template to
10351 instantiate. */
10352 t = most_specialized_partial_spec (type, tf_warning_or_error);
10353 if (t == error_mark_node)
10355 TYPE_BEING_DEFINED (type) = 1;
10356 return error_mark_node;
10358 else if (t)
10360 /* This TYPE is actually an instantiation of a partial
10361 specialization. We replace the innermost set of ARGS with
10362 the arguments appropriate for substitution. For example,
10363 given:
10365 template <class T> struct S {};
10366 template <class T> struct S<T*> {};
10368 and supposing that we are instantiating S<int*>, ARGS will
10369 presently be {int*} -- but we need {int}. */
10370 pattern = TREE_TYPE (t);
10371 args = TREE_PURPOSE (t);
10373 else
10375 pattern = TREE_TYPE (templ);
10376 args = CLASSTYPE_TI_ARGS (type);
10379 /* If the template we're instantiating is incomplete, then clearly
10380 there's nothing we can do. */
10381 if (!COMPLETE_TYPE_P (pattern))
10382 return type;
10384 /* If we've recursively instantiated too many templates, stop. */
10385 if (! push_tinst_level (type))
10386 return type;
10388 /* Now we're really doing the instantiation. Mark the type as in
10389 the process of being defined. */
10390 TYPE_BEING_DEFINED (type) = 1;
10392 /* We may be in the middle of deferred access check. Disable
10393 it now. */
10394 push_deferring_access_checks (dk_no_deferred);
10396 int saved_unevaluated_operand = cp_unevaluated_operand;
10397 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10399 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10400 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10401 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10402 fn_context = error_mark_node;
10403 if (!fn_context)
10404 push_to_top_level ();
10405 else
10407 cp_unevaluated_operand = 0;
10408 c_inhibit_evaluation_warnings = 0;
10410 /* Use #pragma pack from the template context. */
10411 saved_maximum_field_alignment = maximum_field_alignment;
10412 maximum_field_alignment = TYPE_PRECISION (pattern);
10414 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10416 /* Set the input location to the most specialized template definition.
10417 This is needed if tsubsting causes an error. */
10418 typedecl = TYPE_MAIN_DECL (pattern);
10419 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10420 DECL_SOURCE_LOCATION (typedecl);
10422 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10423 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10424 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10425 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10426 if (ANON_AGGR_TYPE_P (pattern))
10427 SET_ANON_AGGR_TYPE_P (type);
10428 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10430 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10431 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10432 /* Adjust visibility for template arguments. */
10433 determine_visibility (TYPE_MAIN_DECL (type));
10435 if (CLASS_TYPE_P (type))
10436 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10438 pbinfo = TYPE_BINFO (pattern);
10440 /* We should never instantiate a nested class before its enclosing
10441 class; we need to look up the nested class by name before we can
10442 instantiate it, and that lookup should instantiate the enclosing
10443 class. */
10444 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10445 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10447 base_list = NULL_TREE;
10448 if (BINFO_N_BASE_BINFOS (pbinfo))
10450 tree pbase_binfo;
10451 tree pushed_scope;
10452 int i;
10454 /* We must enter the scope containing the type, as that is where
10455 the accessibility of types named in dependent bases are
10456 looked up from. */
10457 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10459 /* Substitute into each of the bases to determine the actual
10460 basetypes. */
10461 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10463 tree base;
10464 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10465 tree expanded_bases = NULL_TREE;
10466 int idx, len = 1;
10468 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10470 expanded_bases =
10471 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10472 args, tf_error, NULL_TREE);
10473 if (expanded_bases == error_mark_node)
10474 continue;
10476 len = TREE_VEC_LENGTH (expanded_bases);
10479 for (idx = 0; idx < len; idx++)
10481 if (expanded_bases)
10482 /* Extract the already-expanded base class. */
10483 base = TREE_VEC_ELT (expanded_bases, idx);
10484 else
10485 /* Substitute to figure out the base class. */
10486 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10487 NULL_TREE);
10489 if (base == error_mark_node)
10490 continue;
10492 base_list = tree_cons (access, base, base_list);
10493 if (BINFO_VIRTUAL_P (pbase_binfo))
10494 TREE_TYPE (base_list) = integer_type_node;
10498 /* The list is now in reverse order; correct that. */
10499 base_list = nreverse (base_list);
10501 if (pushed_scope)
10502 pop_scope (pushed_scope);
10504 /* Now call xref_basetypes to set up all the base-class
10505 information. */
10506 xref_basetypes (type, base_list);
10508 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10509 (int) ATTR_FLAG_TYPE_IN_PLACE,
10510 args, tf_error, NULL_TREE);
10511 fixup_attribute_variants (type);
10513 /* Now that our base classes are set up, enter the scope of the
10514 class, so that name lookups into base classes, etc. will work
10515 correctly. This is precisely analogous to what we do in
10516 begin_class_definition when defining an ordinary non-template
10517 class, except we also need to push the enclosing classes. */
10518 push_nested_class (type);
10520 /* Now members are processed in the order of declaration. */
10521 for (member = CLASSTYPE_DECL_LIST (pattern);
10522 member; member = TREE_CHAIN (member))
10524 tree t = TREE_VALUE (member);
10526 if (TREE_PURPOSE (member))
10528 if (TYPE_P (t))
10530 /* Build new CLASSTYPE_NESTED_UTDS. */
10532 tree newtag;
10533 bool class_template_p;
10535 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10536 && TYPE_LANG_SPECIFIC (t)
10537 && CLASSTYPE_IS_TEMPLATE (t));
10538 /* If the member is a class template, then -- even after
10539 substitution -- there may be dependent types in the
10540 template argument list for the class. We increment
10541 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10542 that function will assume that no types are dependent
10543 when outside of a template. */
10544 if (class_template_p)
10545 ++processing_template_decl;
10546 newtag = tsubst (t, args, tf_error, NULL_TREE);
10547 if (class_template_p)
10548 --processing_template_decl;
10549 if (newtag == error_mark_node)
10550 continue;
10552 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10554 tree name = TYPE_IDENTIFIER (t);
10556 if (class_template_p)
10557 /* Unfortunately, lookup_template_class sets
10558 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10559 instantiation (i.e., for the type of a member
10560 template class nested within a template class.)
10561 This behavior is required for
10562 maybe_process_partial_specialization to work
10563 correctly, but is not accurate in this case;
10564 the TAG is not an instantiation of anything.
10565 (The corresponding TEMPLATE_DECL is an
10566 instantiation, but the TYPE is not.) */
10567 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10569 /* Now, we call pushtag to put this NEWTAG into the scope of
10570 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10571 pushtag calling push_template_decl. We don't have to do
10572 this for enums because it will already have been done in
10573 tsubst_enum. */
10574 if (name)
10575 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10576 pushtag (name, newtag, /*tag_scope=*/ts_current);
10579 else if (DECL_DECLARES_FUNCTION_P (t))
10581 tree r;
10583 if (TREE_CODE (t) == TEMPLATE_DECL)
10584 ++processing_template_decl;
10585 r = tsubst (t, args, tf_error, NULL_TREE);
10586 if (TREE_CODE (t) == TEMPLATE_DECL)
10587 --processing_template_decl;
10588 set_current_access_from_decl (r);
10589 finish_member_declaration (r);
10590 /* Instantiate members marked with attribute used. */
10591 if (r != error_mark_node && DECL_PRESERVE_P (r))
10592 mark_used (r);
10593 if (TREE_CODE (r) == FUNCTION_DECL
10594 && DECL_OMP_DECLARE_REDUCTION_P (r))
10595 cp_check_omp_declare_reduction (r);
10597 else if (DECL_CLASS_TEMPLATE_P (t)
10598 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10599 /* A closure type for a lambda in a default argument for a
10600 member template. Ignore it; it will be instantiated with
10601 the default argument. */;
10602 else
10604 /* Build new TYPE_FIELDS. */
10605 if (TREE_CODE (t) == STATIC_ASSERT)
10607 tree condition;
10609 ++c_inhibit_evaluation_warnings;
10610 condition =
10611 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10612 tf_warning_or_error, NULL_TREE,
10613 /*integral_constant_expression_p=*/true);
10614 --c_inhibit_evaluation_warnings;
10616 finish_static_assert (condition,
10617 STATIC_ASSERT_MESSAGE (t),
10618 STATIC_ASSERT_SOURCE_LOCATION (t),
10619 /*member_p=*/true);
10621 else if (TREE_CODE (t) != CONST_DECL)
10623 tree r;
10624 tree vec = NULL_TREE;
10625 int len = 1;
10627 /* The file and line for this declaration, to
10628 assist in error message reporting. Since we
10629 called push_tinst_level above, we don't need to
10630 restore these. */
10631 input_location = DECL_SOURCE_LOCATION (t);
10633 if (TREE_CODE (t) == TEMPLATE_DECL)
10634 ++processing_template_decl;
10635 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10636 if (TREE_CODE (t) == TEMPLATE_DECL)
10637 --processing_template_decl;
10639 if (TREE_CODE (r) == TREE_VEC)
10641 /* A capture pack became multiple fields. */
10642 vec = r;
10643 len = TREE_VEC_LENGTH (vec);
10646 for (int i = 0; i < len; ++i)
10648 if (vec)
10649 r = TREE_VEC_ELT (vec, i);
10650 if (VAR_P (r))
10652 /* In [temp.inst]:
10654 [t]he initialization (and any associated
10655 side-effects) of a static data member does
10656 not occur unless the static data member is
10657 itself used in a way that requires the
10658 definition of the static data member to
10659 exist.
10661 Therefore, we do not substitute into the
10662 initialized for the static data member here. */
10663 finish_static_data_member_decl
10665 /*init=*/NULL_TREE,
10666 /*init_const_expr_p=*/false,
10667 /*asmspec_tree=*/NULL_TREE,
10668 /*flags=*/0);
10669 /* Instantiate members marked with attribute used. */
10670 if (r != error_mark_node && DECL_PRESERVE_P (r))
10671 mark_used (r);
10673 else if (TREE_CODE (r) == FIELD_DECL)
10675 /* Determine whether R has a valid type and can be
10676 completed later. If R is invalid, then its type
10677 is replaced by error_mark_node. */
10678 tree rtype = TREE_TYPE (r);
10679 if (can_complete_type_without_circularity (rtype))
10680 complete_type (rtype);
10682 if (!complete_or_array_type_p (rtype))
10684 /* If R's type couldn't be completed and
10685 it isn't a flexible array member (whose
10686 type is incomplete by definition) give
10687 an error. */
10688 cxx_incomplete_type_error (r, rtype);
10689 TREE_TYPE (r) = error_mark_node;
10693 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10694 such a thing will already have been added to the field
10695 list by tsubst_enum in finish_member_declaration in the
10696 CLASSTYPE_NESTED_UTDS case above. */
10697 if (!(TREE_CODE (r) == TYPE_DECL
10698 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10699 && DECL_ARTIFICIAL (r)))
10701 set_current_access_from_decl (r);
10702 finish_member_declaration (r);
10708 else
10710 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10711 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10713 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10715 tree friend_type = t;
10716 bool adjust_processing_template_decl = false;
10718 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10720 /* template <class T> friend class C; */
10721 friend_type = tsubst_friend_class (friend_type, args);
10722 adjust_processing_template_decl = true;
10724 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10726 /* template <class T> friend class C::D; */
10727 friend_type = tsubst (friend_type, args,
10728 tf_warning_or_error, NULL_TREE);
10729 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10730 friend_type = TREE_TYPE (friend_type);
10731 adjust_processing_template_decl = true;
10733 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10734 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10736 /* This could be either
10738 friend class T::C;
10740 when dependent_type_p is false or
10742 template <class U> friend class T::C;
10744 otherwise. */
10745 /* Bump processing_template_decl in case this is something like
10746 template <class T> friend struct A<T>::B. */
10747 ++processing_template_decl;
10748 friend_type = tsubst (friend_type, args,
10749 tf_warning_or_error, NULL_TREE);
10750 if (dependent_type_p (friend_type))
10751 adjust_processing_template_decl = true;
10752 --processing_template_decl;
10754 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10755 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10756 && TYPE_HIDDEN_P (friend_type))
10758 /* friend class C;
10760 where C hasn't been declared yet. Let's lookup name
10761 from namespace scope directly, bypassing any name that
10762 come from dependent base class. */
10763 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10765 /* The call to xref_tag_from_type does injection for friend
10766 classes. */
10767 push_nested_namespace (ns);
10768 friend_type =
10769 xref_tag_from_type (friend_type, NULL_TREE,
10770 /*tag_scope=*/ts_current);
10771 pop_nested_namespace (ns);
10773 else if (uses_template_parms (friend_type))
10774 /* friend class C<T>; */
10775 friend_type = tsubst (friend_type, args,
10776 tf_warning_or_error, NULL_TREE);
10777 /* Otherwise it's
10779 friend class C;
10781 where C is already declared or
10783 friend class C<int>;
10785 We don't have to do anything in these cases. */
10787 if (adjust_processing_template_decl)
10788 /* Trick make_friend_class into realizing that the friend
10789 we're adding is a template, not an ordinary class. It's
10790 important that we use make_friend_class since it will
10791 perform some error-checking and output cross-reference
10792 information. */
10793 ++processing_template_decl;
10795 if (friend_type != error_mark_node)
10796 make_friend_class (type, friend_type, /*complain=*/false);
10798 if (adjust_processing_template_decl)
10799 --processing_template_decl;
10801 else
10803 /* Build new DECL_FRIENDLIST. */
10804 tree r;
10806 /* The file and line for this declaration, to
10807 assist in error message reporting. Since we
10808 called push_tinst_level above, we don't need to
10809 restore these. */
10810 input_location = DECL_SOURCE_LOCATION (t);
10812 if (TREE_CODE (t) == TEMPLATE_DECL)
10814 ++processing_template_decl;
10815 push_deferring_access_checks (dk_no_check);
10818 r = tsubst_friend_function (t, args);
10819 add_friend (type, r, /*complain=*/false);
10820 if (TREE_CODE (t) == TEMPLATE_DECL)
10822 pop_deferring_access_checks ();
10823 --processing_template_decl;
10829 if (fn_context)
10831 /* Restore these before substituting into the lambda capture
10832 initializers. */
10833 cp_unevaluated_operand = saved_unevaluated_operand;
10834 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10837 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10839 tree decl = lambda_function (type);
10840 if (decl)
10842 if (cxx_dialect >= cxx17)
10843 CLASSTYPE_LITERAL_P (type) = true;
10845 if (!DECL_TEMPLATE_INFO (decl)
10846 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10848 /* Set function_depth to avoid garbage collection. */
10849 ++function_depth;
10850 instantiate_decl (decl, /*defer_ok=*/false, false);
10851 --function_depth;
10854 /* We need to instantiate the capture list from the template
10855 after we've instantiated the closure members, but before we
10856 consider adding the conversion op. Also keep any captures
10857 that may have been added during instantiation of the op(). */
10858 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10859 tree tmpl_cap
10860 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10861 args, tf_warning_or_error, NULL_TREE,
10862 false, false);
10864 LAMBDA_EXPR_CAPTURE_LIST (expr)
10865 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10867 maybe_add_lambda_conv_op (type);
10869 else
10870 gcc_assert (errorcount);
10873 /* Set the file and line number information to whatever is given for
10874 the class itself. This puts error messages involving generated
10875 implicit functions at a predictable point, and the same point
10876 that would be used for non-template classes. */
10877 input_location = DECL_SOURCE_LOCATION (typedecl);
10879 unreverse_member_declarations (type);
10880 finish_struct_1 (type);
10881 TYPE_BEING_DEFINED (type) = 0;
10883 /* We don't instantiate default arguments for member functions. 14.7.1:
10885 The implicit instantiation of a class template specialization causes
10886 the implicit instantiation of the declarations, but not of the
10887 definitions or default arguments, of the class member functions,
10888 member classes, static data members and member templates.... */
10890 /* Some typedefs referenced from within the template code need to be access
10891 checked at template instantiation time, i.e now. These types were
10892 added to the template at parsing time. Let's get those and perform
10893 the access checks then. */
10894 perform_typedefs_access_check (pattern, args);
10895 perform_deferred_access_checks (tf_warning_or_error);
10896 pop_nested_class ();
10897 maximum_field_alignment = saved_maximum_field_alignment;
10898 if (!fn_context)
10899 pop_from_top_level ();
10900 pop_deferring_access_checks ();
10901 pop_tinst_level ();
10903 /* The vtable for a template class can be emitted in any translation
10904 unit in which the class is instantiated. When there is no key
10905 method, however, finish_struct_1 will already have added TYPE to
10906 the keyed_classes. */
10907 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10908 vec_safe_push (keyed_classes, type);
10910 return type;
10913 /* Wrapper for instantiate_class_template_1. */
10915 tree
10916 instantiate_class_template (tree type)
10918 tree ret;
10919 timevar_push (TV_TEMPLATE_INST);
10920 ret = instantiate_class_template_1 (type);
10921 timevar_pop (TV_TEMPLATE_INST);
10922 return ret;
10925 static tree
10926 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10928 tree r;
10930 if (!t)
10931 r = t;
10932 else if (TYPE_P (t))
10933 r = tsubst (t, args, complain, in_decl);
10934 else
10936 if (!(complain & tf_warning))
10937 ++c_inhibit_evaluation_warnings;
10938 r = tsubst_expr (t, args, complain, in_decl,
10939 /*integral_constant_expression_p=*/true);
10940 if (!(complain & tf_warning))
10941 --c_inhibit_evaluation_warnings;
10943 return r;
10946 /* Given a function parameter pack TMPL_PARM and some function parameters
10947 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10948 and set *SPEC_P to point at the next point in the list. */
10950 tree
10951 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10953 /* Collect all of the extra "packed" parameters into an
10954 argument pack. */
10955 tree parmvec;
10956 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10957 tree spec_parm = *spec_p;
10958 int i, len;
10960 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10961 if (tmpl_parm
10962 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10963 break;
10965 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10966 parmvec = make_tree_vec (len);
10967 spec_parm = *spec_p;
10968 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10970 tree elt = spec_parm;
10971 if (DECL_PACK_P (elt))
10972 elt = make_pack_expansion (elt);
10973 TREE_VEC_ELT (parmvec, i) = elt;
10976 /* Build the argument packs. */
10977 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10978 *spec_p = spec_parm;
10980 return argpack;
10983 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10984 NONTYPE_ARGUMENT_PACK. */
10986 static tree
10987 make_fnparm_pack (tree spec_parm)
10989 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10992 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10993 pack expansion with no extra args, 2 if it has extra args, or 0
10994 if it is not a pack expansion. */
10996 static int
10997 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10999 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11000 if (i >= TREE_VEC_LENGTH (vec))
11001 return 0;
11002 tree elt = TREE_VEC_ELT (vec, i);
11003 if (DECL_P (elt))
11004 /* A decl pack is itself an expansion. */
11005 elt = TREE_TYPE (elt);
11006 if (!PACK_EXPANSION_P (elt))
11007 return 0;
11008 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11009 return 2;
11010 return 1;
11014 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11016 static tree
11017 make_argument_pack_select (tree arg_pack, unsigned index)
11019 tree aps = make_node (ARGUMENT_PACK_SELECT);
11021 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11022 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11024 return aps;
11027 /* This is a subroutine of tsubst_pack_expansion.
11029 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11030 mechanism to store the (non complete list of) arguments of the
11031 substitution and return a non substituted pack expansion, in order
11032 to wait for when we have enough arguments to really perform the
11033 substitution. */
11035 static bool
11036 use_pack_expansion_extra_args_p (tree parm_packs,
11037 int arg_pack_len,
11038 bool has_empty_arg)
11040 /* If one pack has an expansion and another pack has a normal
11041 argument or if one pack has an empty argument and an another
11042 one hasn't then tsubst_pack_expansion cannot perform the
11043 substitution and need to fall back on the
11044 PACK_EXPANSION_EXTRA mechanism. */
11045 if (parm_packs == NULL_TREE)
11046 return false;
11047 else if (has_empty_arg)
11048 return true;
11050 bool has_expansion_arg = false;
11051 for (int i = 0 ; i < arg_pack_len; ++i)
11053 bool has_non_expansion_arg = false;
11054 for (tree parm_pack = parm_packs;
11055 parm_pack;
11056 parm_pack = TREE_CHAIN (parm_pack))
11058 tree arg = TREE_VALUE (parm_pack);
11060 int exp = argument_pack_element_is_expansion_p (arg, i);
11061 if (exp == 2)
11062 /* We can't substitute a pack expansion with extra args into
11063 our pattern. */
11064 return true;
11065 else if (exp)
11066 has_expansion_arg = true;
11067 else
11068 has_non_expansion_arg = true;
11071 if (has_expansion_arg && has_non_expansion_arg)
11072 return true;
11074 return false;
11077 /* [temp.variadic]/6 says that:
11079 The instantiation of a pack expansion [...]
11080 produces a list E1,E2, ..., En, where N is the number of elements
11081 in the pack expansion parameters.
11083 This subroutine of tsubst_pack_expansion produces one of these Ei.
11085 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11086 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11087 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11088 INDEX is the index 'i' of the element Ei to produce. ARGS,
11089 COMPLAIN, and IN_DECL are the same parameters as for the
11090 tsubst_pack_expansion function.
11092 The function returns the resulting Ei upon successful completion,
11093 or error_mark_node.
11095 Note that this function possibly modifies the ARGS parameter, so
11096 it's the responsibility of the caller to restore it. */
11098 static tree
11099 gen_elem_of_pack_expansion_instantiation (tree pattern,
11100 tree parm_packs,
11101 unsigned index,
11102 tree args /* This parm gets
11103 modified. */,
11104 tsubst_flags_t complain,
11105 tree in_decl)
11107 tree t;
11108 bool ith_elem_is_expansion = false;
11110 /* For each parameter pack, change the substitution of the parameter
11111 pack to the ith argument in its argument pack, then expand the
11112 pattern. */
11113 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11115 tree parm = TREE_PURPOSE (pack);
11116 tree arg_pack = TREE_VALUE (pack);
11117 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11119 ith_elem_is_expansion |=
11120 argument_pack_element_is_expansion_p (arg_pack, index);
11122 /* Select the Ith argument from the pack. */
11123 if (TREE_CODE (parm) == PARM_DECL
11124 || TREE_CODE (parm) == FIELD_DECL)
11126 if (index == 0)
11128 aps = make_argument_pack_select (arg_pack, index);
11129 if (!mark_used (parm, complain) && !(complain & tf_error))
11130 return error_mark_node;
11131 register_local_specialization (aps, parm);
11133 else
11134 aps = retrieve_local_specialization (parm);
11136 else
11138 int idx, level;
11139 template_parm_level_and_index (parm, &level, &idx);
11141 if (index == 0)
11143 aps = make_argument_pack_select (arg_pack, index);
11144 /* Update the corresponding argument. */
11145 TMPL_ARG (args, level, idx) = aps;
11147 else
11148 /* Re-use the ARGUMENT_PACK_SELECT. */
11149 aps = TMPL_ARG (args, level, idx);
11151 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11154 /* Substitute into the PATTERN with the (possibly altered)
11155 arguments. */
11156 if (pattern == in_decl)
11157 /* Expanding a fixed parameter pack from
11158 coerce_template_parameter_pack. */
11159 t = tsubst_decl (pattern, args, complain);
11160 else if (pattern == error_mark_node)
11161 t = error_mark_node;
11162 else if (constraint_p (pattern))
11164 if (processing_template_decl)
11165 t = tsubst_constraint (pattern, args, complain, in_decl);
11166 else
11167 t = (constraints_satisfied_p (pattern, args)
11168 ? boolean_true_node : boolean_false_node);
11170 else if (!TYPE_P (pattern))
11171 t = tsubst_expr (pattern, args, complain, in_decl,
11172 /*integral_constant_expression_p=*/false);
11173 else
11174 t = tsubst (pattern, args, complain, in_decl);
11176 /* If the Ith argument pack element is a pack expansion, then
11177 the Ith element resulting from the substituting is going to
11178 be a pack expansion as well. */
11179 if (ith_elem_is_expansion)
11180 t = make_pack_expansion (t, complain);
11182 return t;
11185 /* When the unexpanded parameter pack in a fold expression expands to an empty
11186 sequence, the value of the expression is as follows; the program is
11187 ill-formed if the operator is not listed in this table.
11189 && true
11190 || false
11191 , void() */
11193 tree
11194 expand_empty_fold (tree t, tsubst_flags_t complain)
11196 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11197 if (!FOLD_EXPR_MODIFY_P (t))
11198 switch (code)
11200 case TRUTH_ANDIF_EXPR:
11201 return boolean_true_node;
11202 case TRUTH_ORIF_EXPR:
11203 return boolean_false_node;
11204 case COMPOUND_EXPR:
11205 return void_node;
11206 default:
11207 break;
11210 if (complain & tf_error)
11211 error_at (location_of (t),
11212 "fold of empty expansion over %O", code);
11213 return error_mark_node;
11216 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11217 form an expression that combines the two terms using the
11218 operator of T. */
11220 static tree
11221 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11223 tree op = FOLD_EXPR_OP (t);
11224 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11226 // Handle compound assignment operators.
11227 if (FOLD_EXPR_MODIFY_P (t))
11228 return build_x_modify_expr (input_location, left, code, right, complain);
11230 switch (code)
11232 case COMPOUND_EXPR:
11233 return build_x_compound_expr (input_location, left, right, complain);
11234 case DOTSTAR_EXPR:
11235 return build_m_component_ref (left, right, complain);
11236 default:
11237 return build_x_binary_op (input_location, code,
11238 left, TREE_CODE (left),
11239 right, TREE_CODE (right),
11240 /*overload=*/NULL,
11241 complain);
11245 /* Substitute ARGS into the pack of a fold expression T. */
11247 static inline tree
11248 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11250 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11253 /* Substitute ARGS into the pack of a fold expression T. */
11255 static inline tree
11256 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11258 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11261 /* Expand a PACK of arguments into a grouped as left fold.
11262 Given a pack containing elements A0, A1, ..., An and an
11263 operator @, this builds the expression:
11265 ((A0 @ A1) @ A2) ... @ An
11267 Note that PACK must not be empty.
11269 The operator is defined by the original fold expression T. */
11271 static tree
11272 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11274 tree left = TREE_VEC_ELT (pack, 0);
11275 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11277 tree right = TREE_VEC_ELT (pack, i);
11278 left = fold_expression (t, left, right, complain);
11280 return left;
11283 /* Substitute into a unary left fold expression. */
11285 static tree
11286 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11287 tree in_decl)
11289 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11290 if (pack == error_mark_node)
11291 return error_mark_node;
11292 if (PACK_EXPANSION_P (pack))
11294 tree r = copy_node (t);
11295 FOLD_EXPR_PACK (r) = pack;
11296 return r;
11298 if (TREE_VEC_LENGTH (pack) == 0)
11299 return expand_empty_fold (t, complain);
11300 else
11301 return expand_left_fold (t, pack, complain);
11304 /* Substitute into a binary left fold expression.
11306 Do ths by building a single (non-empty) vector of argumnts and
11307 building the expression from those elements. */
11309 static tree
11310 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11311 tree in_decl)
11313 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11314 if (pack == error_mark_node)
11315 return error_mark_node;
11316 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11317 if (init == error_mark_node)
11318 return error_mark_node;
11320 if (PACK_EXPANSION_P (pack))
11322 tree r = copy_node (t);
11323 FOLD_EXPR_PACK (r) = pack;
11324 FOLD_EXPR_INIT (r) = init;
11325 return r;
11328 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11329 TREE_VEC_ELT (vec, 0) = init;
11330 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11331 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11333 return expand_left_fold (t, vec, complain);
11336 /* Expand a PACK of arguments into a grouped as right fold.
11337 Given a pack containing elementns A0, A1, ..., and an
11338 operator @, this builds the expression:
11340 A0@ ... (An-2 @ (An-1 @ An))
11342 Note that PACK must not be empty.
11344 The operator is defined by the original fold expression T. */
11346 tree
11347 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11349 // Build the expression.
11350 int n = TREE_VEC_LENGTH (pack);
11351 tree right = TREE_VEC_ELT (pack, n - 1);
11352 for (--n; n != 0; --n)
11354 tree left = TREE_VEC_ELT (pack, n - 1);
11355 right = fold_expression (t, left, right, complain);
11357 return right;
11360 /* Substitute into a unary right fold expression. */
11362 static tree
11363 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11364 tree in_decl)
11366 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11367 if (pack == error_mark_node)
11368 return error_mark_node;
11369 if (PACK_EXPANSION_P (pack))
11371 tree r = copy_node (t);
11372 FOLD_EXPR_PACK (r) = pack;
11373 return r;
11375 if (TREE_VEC_LENGTH (pack) == 0)
11376 return expand_empty_fold (t, complain);
11377 else
11378 return expand_right_fold (t, pack, complain);
11381 /* Substitute into a binary right fold expression.
11383 Do ths by building a single (non-empty) vector of arguments and
11384 building the expression from those elements. */
11386 static tree
11387 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11388 tree in_decl)
11390 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11391 if (pack == error_mark_node)
11392 return error_mark_node;
11393 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11394 if (init == error_mark_node)
11395 return error_mark_node;
11397 if (PACK_EXPANSION_P (pack))
11399 tree r = copy_node (t);
11400 FOLD_EXPR_PACK (r) = pack;
11401 FOLD_EXPR_INIT (r) = init;
11402 return r;
11405 int n = TREE_VEC_LENGTH (pack);
11406 tree vec = make_tree_vec (n + 1);
11407 for (int i = 0; i < n; ++i)
11408 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11409 TREE_VEC_ELT (vec, n) = init;
11411 return expand_right_fold (t, vec, complain);
11415 /* Substitute ARGS into T, which is an pack expansion
11416 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11417 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11418 (if only a partial substitution could be performed) or
11419 ERROR_MARK_NODE if there was an error. */
11420 tree
11421 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11422 tree in_decl)
11424 tree pattern;
11425 tree pack, packs = NULL_TREE;
11426 bool unsubstituted_packs = false;
11427 bool unsubstituted_fn_pack = false;
11428 int i, len = -1;
11429 tree result;
11430 hash_map<tree, tree> *saved_local_specializations = NULL;
11431 bool need_local_specializations = false;
11432 int levels;
11434 gcc_assert (PACK_EXPANSION_P (t));
11435 pattern = PACK_EXPANSION_PATTERN (t);
11437 /* Add in any args remembered from an earlier partial instantiation. */
11438 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11440 levels = TMPL_ARGS_DEPTH (args);
11442 /* Determine the argument packs that will instantiate the parameter
11443 packs used in the expansion expression. While we're at it,
11444 compute the number of arguments to be expanded and make sure it
11445 is consistent. */
11446 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11447 pack = TREE_CHAIN (pack))
11449 tree parm_pack = TREE_VALUE (pack);
11450 tree arg_pack = NULL_TREE;
11451 tree orig_arg = NULL_TREE;
11452 int level = 0;
11454 if (TREE_CODE (parm_pack) == BASES)
11456 gcc_assert (parm_pack == pattern);
11457 if (BASES_DIRECT (parm_pack))
11458 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11459 args, complain, in_decl, false));
11460 else
11461 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11462 args, complain, in_decl, false));
11464 else if (builtin_pack_call_p (parm_pack))
11466 /* ??? Support use in other patterns. */
11467 gcc_assert (parm_pack == pattern);
11468 return expand_builtin_pack_call (parm_pack, args,
11469 complain, in_decl);
11471 else if (TREE_CODE (parm_pack) == PARM_DECL)
11473 /* We know we have correct local_specializations if this
11474 expansion is at function scope, or if we're dealing with a
11475 local parameter in a requires expression; for the latter,
11476 tsubst_requires_expr set it up appropriately. */
11477 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11478 arg_pack = retrieve_local_specialization (parm_pack);
11479 else
11480 /* We can't rely on local_specializations for a parameter
11481 name used later in a function declaration (such as in a
11482 late-specified return type). Even if it exists, it might
11483 have the wrong value for a recursive call. */
11484 need_local_specializations = true;
11486 if (!arg_pack)
11488 /* This parameter pack was used in an unevaluated context. Just
11489 make a dummy decl, since it's only used for its type. */
11490 arg_pack = tsubst_decl (parm_pack, args, complain);
11491 if (arg_pack && DECL_PACK_P (arg_pack))
11492 /* Partial instantiation of the parm_pack, we can't build
11493 up an argument pack yet. */
11494 arg_pack = NULL_TREE;
11495 else
11496 arg_pack = make_fnparm_pack (arg_pack);
11498 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11499 /* This argument pack isn't fully instantiated yet. We set this
11500 flag rather than clear arg_pack because we do want to do the
11501 optimization below, and we don't want to substitute directly
11502 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11503 where it isn't expected). */
11504 unsubstituted_fn_pack = true;
11506 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11507 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11508 else
11510 int idx;
11511 template_parm_level_and_index (parm_pack, &level, &idx);
11513 if (level <= levels)
11514 arg_pack = TMPL_ARG (args, level, idx);
11517 orig_arg = arg_pack;
11518 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11519 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11521 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11522 /* This can only happen if we forget to expand an argument
11523 pack somewhere else. Just return an error, silently. */
11525 result = make_tree_vec (1);
11526 TREE_VEC_ELT (result, 0) = error_mark_node;
11527 return result;
11530 if (arg_pack)
11532 int my_len =
11533 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11535 /* Don't bother trying to do a partial substitution with
11536 incomplete packs; we'll try again after deduction. */
11537 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11538 return t;
11540 if (len < 0)
11541 len = my_len;
11542 else if (len != my_len
11543 && !unsubstituted_fn_pack)
11545 if (!(complain & tf_error))
11546 /* Fail quietly. */;
11547 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11548 error ("mismatched argument pack lengths while expanding %qT",
11549 pattern);
11550 else
11551 error ("mismatched argument pack lengths while expanding %qE",
11552 pattern);
11553 return error_mark_node;
11556 /* Keep track of the parameter packs and their corresponding
11557 argument packs. */
11558 packs = tree_cons (parm_pack, arg_pack, packs);
11559 TREE_TYPE (packs) = orig_arg;
11561 else
11563 /* We can't substitute for this parameter pack. We use a flag as
11564 well as the missing_level counter because function parameter
11565 packs don't have a level. */
11566 gcc_assert (processing_template_decl);
11567 unsubstituted_packs = true;
11571 /* If the expansion is just T..., return the matching argument pack, unless
11572 we need to call convert_from_reference on all the elements. This is an
11573 important optimization; see c++/68422. */
11574 if (!unsubstituted_packs
11575 && TREE_PURPOSE (packs) == pattern)
11577 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11578 /* Types need no adjustment, nor does sizeof..., and if we still have
11579 some pack expansion args we won't do anything yet. */
11580 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11581 || PACK_EXPANSION_SIZEOF_P (t)
11582 || pack_expansion_args_count (args))
11583 return args;
11584 /* Also optimize expression pack expansions if we can tell that the
11585 elements won't have reference type. */
11586 tree type = TREE_TYPE (pattern);
11587 if (type && TREE_CODE (type) != REFERENCE_TYPE
11588 && !PACK_EXPANSION_P (type)
11589 && !WILDCARD_TYPE_P (type))
11590 return args;
11591 /* Otherwise use the normal path so we get convert_from_reference. */
11594 /* We cannot expand this expansion expression, because we don't have
11595 all of the argument packs we need. */
11596 if (use_pack_expansion_extra_args_p (packs, len, (unsubstituted_packs
11597 || unsubstituted_fn_pack)))
11599 /* We got some full packs, but we can't substitute them in until we
11600 have values for all the packs. So remember these until then. */
11602 t = make_pack_expansion (pattern, complain);
11603 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11604 return t;
11606 else if (unsubstituted_packs)
11608 /* There were no real arguments, we're just replacing a parameter
11609 pack with another version of itself. Substitute into the
11610 pattern and return a PACK_EXPANSION_*. The caller will need to
11611 deal with that. */
11612 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11613 t = tsubst_expr (pattern, args, complain, in_decl,
11614 /*integral_constant_expression_p=*/false);
11615 else
11616 t = tsubst (pattern, args, complain, in_decl);
11617 t = make_pack_expansion (t, complain);
11618 return t;
11621 gcc_assert (len >= 0);
11623 if (need_local_specializations)
11625 /* We're in a late-specified return type, so create our own local
11626 specializations map; the current map is either NULL or (in the
11627 case of recursive unification) might have bindings that we don't
11628 want to use or alter. */
11629 saved_local_specializations = local_specializations;
11630 local_specializations = new hash_map<tree, tree>;
11633 /* For each argument in each argument pack, substitute into the
11634 pattern. */
11635 result = make_tree_vec (len);
11636 tree elem_args = copy_template_args (args);
11637 for (i = 0; i < len; ++i)
11639 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11641 elem_args, complain,
11642 in_decl);
11643 TREE_VEC_ELT (result, i) = t;
11644 if (t == error_mark_node)
11646 result = error_mark_node;
11647 break;
11651 /* Update ARGS to restore the substitution from parameter packs to
11652 their argument packs. */
11653 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11655 tree parm = TREE_PURPOSE (pack);
11657 if (TREE_CODE (parm) == PARM_DECL
11658 || TREE_CODE (parm) == FIELD_DECL)
11659 register_local_specialization (TREE_TYPE (pack), parm);
11660 else
11662 int idx, level;
11664 if (TREE_VALUE (pack) == NULL_TREE)
11665 continue;
11667 template_parm_level_and_index (parm, &level, &idx);
11669 /* Update the corresponding argument. */
11670 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11671 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11672 TREE_TYPE (pack);
11673 else
11674 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11678 if (need_local_specializations)
11680 delete local_specializations;
11681 local_specializations = saved_local_specializations;
11684 /* If the dependent pack arguments were such that we end up with only a
11685 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11686 if (len == 1 && TREE_CODE (result) == TREE_VEC
11687 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11688 return TREE_VEC_ELT (result, 0);
11690 return result;
11693 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11694 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11695 parameter packs; all parms generated from a function parameter pack will
11696 have the same DECL_PARM_INDEX. */
11698 tree
11699 get_pattern_parm (tree parm, tree tmpl)
11701 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11702 tree patparm;
11704 if (DECL_ARTIFICIAL (parm))
11706 for (patparm = DECL_ARGUMENTS (pattern);
11707 patparm; patparm = DECL_CHAIN (patparm))
11708 if (DECL_ARTIFICIAL (patparm)
11709 && DECL_NAME (parm) == DECL_NAME (patparm))
11710 break;
11712 else
11714 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11715 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11716 gcc_assert (DECL_PARM_INDEX (patparm)
11717 == DECL_PARM_INDEX (parm));
11720 return patparm;
11723 /* Make an argument pack out of the TREE_VEC VEC. */
11725 static tree
11726 make_argument_pack (tree vec)
11728 tree pack;
11729 tree elt = TREE_VEC_ELT (vec, 0);
11730 if (TYPE_P (elt))
11731 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11732 else
11734 pack = make_node (NONTYPE_ARGUMENT_PACK);
11735 TREE_CONSTANT (pack) = 1;
11737 SET_ARGUMENT_PACK_ARGS (pack, vec);
11738 return pack;
11741 /* Return an exact copy of template args T that can be modified
11742 independently. */
11744 static tree
11745 copy_template_args (tree t)
11747 if (t == error_mark_node)
11748 return t;
11750 int len = TREE_VEC_LENGTH (t);
11751 tree new_vec = make_tree_vec (len);
11753 for (int i = 0; i < len; ++i)
11755 tree elt = TREE_VEC_ELT (t, i);
11756 if (elt && TREE_CODE (elt) == TREE_VEC)
11757 elt = copy_template_args (elt);
11758 TREE_VEC_ELT (new_vec, i) = elt;
11761 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11762 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11764 return new_vec;
11767 /* Substitute ARGS into the vector or list of template arguments T. */
11769 static tree
11770 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11772 tree orig_t = t;
11773 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11774 tree *elts;
11776 if (t == error_mark_node)
11777 return error_mark_node;
11779 len = TREE_VEC_LENGTH (t);
11780 elts = XALLOCAVEC (tree, len);
11782 for (i = 0; i < len; i++)
11784 tree orig_arg = TREE_VEC_ELT (t, i);
11785 tree new_arg;
11787 if (TREE_CODE (orig_arg) == TREE_VEC)
11788 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11789 else if (PACK_EXPANSION_P (orig_arg))
11791 /* Substitute into an expansion expression. */
11792 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11794 if (TREE_CODE (new_arg) == TREE_VEC)
11795 /* Add to the expanded length adjustment the number of
11796 expanded arguments. We subtract one from this
11797 measurement, because the argument pack expression
11798 itself is already counted as 1 in
11799 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11800 the argument pack is empty. */
11801 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11803 else if (ARGUMENT_PACK_P (orig_arg))
11805 /* Substitute into each of the arguments. */
11806 new_arg = TYPE_P (orig_arg)
11807 ? cxx_make_type (TREE_CODE (orig_arg))
11808 : make_node (TREE_CODE (orig_arg));
11810 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11811 args, complain, in_decl);
11812 if (pack_args == error_mark_node)
11813 new_arg = error_mark_node;
11814 else
11815 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11817 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11818 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11820 else
11821 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11823 if (new_arg == error_mark_node)
11824 return error_mark_node;
11826 elts[i] = new_arg;
11827 if (new_arg != orig_arg)
11828 need_new = 1;
11831 if (!need_new)
11832 return t;
11834 /* Make space for the expanded arguments coming from template
11835 argument packs. */
11836 t = make_tree_vec (len + expanded_len_adjust);
11837 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11838 arguments for a member template.
11839 In that case each TREE_VEC in ORIG_T represents a level of template
11840 arguments, and ORIG_T won't carry any non defaulted argument count.
11841 It will rather be the nested TREE_VECs that will carry one.
11842 In other words, ORIG_T carries a non defaulted argument count only
11843 if it doesn't contain any nested TREE_VEC. */
11844 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11846 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11847 count += expanded_len_adjust;
11848 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11850 for (i = 0, out = 0; i < len; i++)
11852 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11853 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11854 && TREE_CODE (elts[i]) == TREE_VEC)
11856 int idx;
11858 /* Now expand the template argument pack "in place". */
11859 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11860 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11862 else
11864 TREE_VEC_ELT (t, out) = elts[i];
11865 out++;
11869 return t;
11872 /* Substitute ARGS into one level PARMS of template parameters. */
11874 static tree
11875 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11877 if (parms == error_mark_node)
11878 return error_mark_node;
11880 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11882 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11884 tree tuple = TREE_VEC_ELT (parms, i);
11886 if (tuple == error_mark_node)
11887 continue;
11889 TREE_VEC_ELT (new_vec, i) =
11890 tsubst_template_parm (tuple, args, complain);
11893 return new_vec;
11896 /* Return the result of substituting ARGS into the template parameters
11897 given by PARMS. If there are m levels of ARGS and m + n levels of
11898 PARMS, then the result will contain n levels of PARMS. For
11899 example, if PARMS is `template <class T> template <class U>
11900 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11901 result will be `template <int*, double, class V>'. */
11903 static tree
11904 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11906 tree r = NULL_TREE;
11907 tree* new_parms;
11909 /* When substituting into a template, we must set
11910 PROCESSING_TEMPLATE_DECL as the template parameters may be
11911 dependent if they are based on one-another, and the dependency
11912 predicates are short-circuit outside of templates. */
11913 ++processing_template_decl;
11915 for (new_parms = &r;
11916 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11917 new_parms = &(TREE_CHAIN (*new_parms)),
11918 parms = TREE_CHAIN (parms))
11920 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11921 args, complain);
11922 *new_parms =
11923 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11924 - TMPL_ARGS_DEPTH (args)),
11925 new_vec, NULL_TREE);
11928 --processing_template_decl;
11930 return r;
11933 /* Return the result of substituting ARGS into one template parameter
11934 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11935 parameter and which TREE_PURPOSE is the default argument of the
11936 template parameter. */
11938 static tree
11939 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11941 tree default_value, parm_decl;
11943 if (args == NULL_TREE
11944 || t == NULL_TREE
11945 || t == error_mark_node)
11946 return t;
11948 gcc_assert (TREE_CODE (t) == TREE_LIST);
11950 default_value = TREE_PURPOSE (t);
11951 parm_decl = TREE_VALUE (t);
11953 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11954 if (TREE_CODE (parm_decl) == PARM_DECL
11955 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11956 parm_decl = error_mark_node;
11957 default_value = tsubst_template_arg (default_value, args,
11958 complain, NULL_TREE);
11960 return build_tree_list (default_value, parm_decl);
11963 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11964 type T. If T is not an aggregate or enumeration type, it is
11965 handled as if by tsubst. IN_DECL is as for tsubst. If
11966 ENTERING_SCOPE is nonzero, T is the context for a template which
11967 we are presently tsubst'ing. Return the substituted value. */
11969 static tree
11970 tsubst_aggr_type (tree t,
11971 tree args,
11972 tsubst_flags_t complain,
11973 tree in_decl,
11974 int entering_scope)
11976 if (t == NULL_TREE)
11977 return NULL_TREE;
11979 switch (TREE_CODE (t))
11981 case RECORD_TYPE:
11982 if (TYPE_PTRMEMFUNC_P (t))
11983 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11985 /* Fall through. */
11986 case ENUMERAL_TYPE:
11987 case UNION_TYPE:
11988 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11990 tree argvec;
11991 tree context;
11992 tree r;
11993 int saved_unevaluated_operand;
11994 int saved_inhibit_evaluation_warnings;
11996 /* In "sizeof(X<I>)" we need to evaluate "I". */
11997 saved_unevaluated_operand = cp_unevaluated_operand;
11998 cp_unevaluated_operand = 0;
11999 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12000 c_inhibit_evaluation_warnings = 0;
12002 /* First, determine the context for the type we are looking
12003 up. */
12004 context = TYPE_CONTEXT (t);
12005 if (context && TYPE_P (context))
12007 context = tsubst_aggr_type (context, args, complain,
12008 in_decl, /*entering_scope=*/1);
12009 /* If context is a nested class inside a class template,
12010 it may still need to be instantiated (c++/33959). */
12011 context = complete_type (context);
12014 /* Then, figure out what arguments are appropriate for the
12015 type we are trying to find. For example, given:
12017 template <class T> struct S;
12018 template <class T, class U> void f(T, U) { S<U> su; }
12020 and supposing that we are instantiating f<int, double>,
12021 then our ARGS will be {int, double}, but, when looking up
12022 S we only want {double}. */
12023 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12024 complain, in_decl);
12025 if (argvec == error_mark_node)
12026 r = error_mark_node;
12027 else
12029 r = lookup_template_class (t, argvec, in_decl, context,
12030 entering_scope, complain);
12031 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12034 cp_unevaluated_operand = saved_unevaluated_operand;
12035 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12037 return r;
12039 else
12040 /* This is not a template type, so there's nothing to do. */
12041 return t;
12043 default:
12044 return tsubst (t, args, complain, in_decl);
12048 static GTY((cache)) tree_cache_map *defarg_inst;
12050 /* Substitute into the default argument ARG (a default argument for
12051 FN), which has the indicated TYPE. */
12053 tree
12054 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12055 tsubst_flags_t complain)
12057 tree saved_class_ptr = NULL_TREE;
12058 tree saved_class_ref = NULL_TREE;
12059 int errs = errorcount + sorrycount;
12061 /* This can happen in invalid code. */
12062 if (TREE_CODE (arg) == DEFAULT_ARG)
12063 return arg;
12065 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12066 parm = chain_index (parmnum, parm);
12067 tree parmtype = TREE_TYPE (parm);
12068 if (DECL_BY_REFERENCE (parm))
12069 parmtype = TREE_TYPE (parmtype);
12070 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12072 tree *slot;
12073 if (defarg_inst && (slot = defarg_inst->get (parm)))
12074 return *slot;
12076 /* This default argument came from a template. Instantiate the
12077 default argument here, not in tsubst. In the case of
12078 something like:
12080 template <class T>
12081 struct S {
12082 static T t();
12083 void f(T = t());
12086 we must be careful to do name lookup in the scope of S<T>,
12087 rather than in the current class. */
12088 push_access_scope (fn);
12089 /* The "this" pointer is not valid in a default argument. */
12090 if (cfun)
12092 saved_class_ptr = current_class_ptr;
12093 cp_function_chain->x_current_class_ptr = NULL_TREE;
12094 saved_class_ref = current_class_ref;
12095 cp_function_chain->x_current_class_ref = NULL_TREE;
12098 start_lambda_scope (parm);
12100 push_deferring_access_checks(dk_no_deferred);
12101 /* The default argument expression may cause implicitly defined
12102 member functions to be synthesized, which will result in garbage
12103 collection. We must treat this situation as if we were within
12104 the body of function so as to avoid collecting live data on the
12105 stack. */
12106 ++function_depth;
12107 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12108 complain, NULL_TREE,
12109 /*integral_constant_expression_p=*/false);
12110 --function_depth;
12111 pop_deferring_access_checks();
12113 finish_lambda_scope ();
12115 /* Restore the "this" pointer. */
12116 if (cfun)
12118 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12119 cp_function_chain->x_current_class_ref = saved_class_ref;
12122 if (errorcount+sorrycount > errs
12123 && (complain & tf_warning_or_error))
12124 inform (input_location,
12125 " when instantiating default argument for call to %qD", fn);
12127 /* Make sure the default argument is reasonable. */
12128 arg = check_default_argument (type, arg, complain);
12130 pop_access_scope (fn);
12132 if (arg != error_mark_node && !cp_unevaluated_operand)
12134 if (!defarg_inst)
12135 defarg_inst = tree_cache_map::create_ggc (37);
12136 defarg_inst->put (parm, arg);
12139 return arg;
12142 /* Substitute into all the default arguments for FN. */
12144 static void
12145 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12147 tree arg;
12148 tree tmpl_args;
12150 tmpl_args = DECL_TI_ARGS (fn);
12152 /* If this function is not yet instantiated, we certainly don't need
12153 its default arguments. */
12154 if (uses_template_parms (tmpl_args))
12155 return;
12156 /* Don't do this again for clones. */
12157 if (DECL_CLONED_FUNCTION_P (fn))
12158 return;
12160 int i = 0;
12161 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12162 arg;
12163 arg = TREE_CHAIN (arg), ++i)
12164 if (TREE_PURPOSE (arg))
12165 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12166 TREE_VALUE (arg),
12167 TREE_PURPOSE (arg),
12168 complain);
12171 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12173 static tree
12174 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12175 tree lambda_fntype)
12177 tree gen_tmpl, argvec;
12178 hashval_t hash = 0;
12179 tree in_decl = t;
12181 /* Nobody should be tsubst'ing into non-template functions. */
12182 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12184 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12186 /* If T is not dependent, just return it. */
12187 if (!uses_template_parms (DECL_TI_ARGS (t)))
12188 return t;
12190 /* Calculate the most general template of which R is a
12191 specialization. */
12192 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12194 /* We're substituting a lambda function under tsubst_lambda_expr but not
12195 directly from it; find the matching function we're already inside.
12196 But don't do this if T is a generic lambda with a single level of
12197 template parms, as in that case we're doing a normal instantiation. */
12198 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12199 && (!generic_lambda_fn_p (t)
12200 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12201 return enclosing_instantiation_of (t);
12203 /* Calculate the complete set of arguments used to
12204 specialize R. */
12205 argvec = tsubst_template_args (DECL_TI_ARGS
12206 (DECL_TEMPLATE_RESULT
12207 (DECL_TI_TEMPLATE (t))),
12208 args, complain, in_decl);
12209 if (argvec == error_mark_node)
12210 return error_mark_node;
12212 /* Check to see if we already have this specialization. */
12213 if (!lambda_fntype)
12215 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12216 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12217 return spec;
12220 /* We can see more levels of arguments than parameters if
12221 there was a specialization of a member template, like
12222 this:
12224 template <class T> struct S { template <class U> void f(); }
12225 template <> template <class U> void S<int>::f(U);
12227 Here, we'll be substituting into the specialization,
12228 because that's where we can find the code we actually
12229 want to generate, but we'll have enough arguments for
12230 the most general template.
12232 We also deal with the peculiar case:
12234 template <class T> struct S {
12235 template <class U> friend void f();
12237 template <class U> void f() {}
12238 template S<int>;
12239 template void f<double>();
12241 Here, the ARGS for the instantiation of will be {int,
12242 double}. But, we only need as many ARGS as there are
12243 levels of template parameters in CODE_PATTERN. We are
12244 careful not to get fooled into reducing the ARGS in
12245 situations like:
12247 template <class T> struct S { template <class U> void f(U); }
12248 template <class T> template <> void S<T>::f(int) {}
12250 which we can spot because the pattern will be a
12251 specialization in this case. */
12252 int args_depth = TMPL_ARGS_DEPTH (args);
12253 int parms_depth =
12254 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12256 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12257 args = get_innermost_template_args (args, parms_depth);
12259 else
12261 /* This special case arises when we have something like this:
12263 template <class T> struct S {
12264 friend void f<int>(int, double);
12267 Here, the DECL_TI_TEMPLATE for the friend declaration
12268 will be an IDENTIFIER_NODE. We are being called from
12269 tsubst_friend_function, and we want only to create a
12270 new decl (R) with appropriate types so that we can call
12271 determine_specialization. */
12272 gen_tmpl = NULL_TREE;
12273 argvec = NULL_TREE;
12276 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12277 : NULL_TREE);
12278 tree ctx = closure ? closure : DECL_CONTEXT (t);
12279 bool member = ctx && TYPE_P (ctx);
12281 if (member && !closure)
12282 ctx = tsubst_aggr_type (ctx, args,
12283 complain, t, /*entering_scope=*/1);
12285 tree type = (lambda_fntype ? lambda_fntype
12286 : tsubst (TREE_TYPE (t), args,
12287 complain | tf_fndecl_type, in_decl));
12288 if (type == error_mark_node)
12289 return error_mark_node;
12291 /* If we hit excessive deduction depth, the type is bogus even if
12292 it isn't error_mark_node, so don't build a decl. */
12293 if (excessive_deduction_depth)
12294 return error_mark_node;
12296 /* We do NOT check for matching decls pushed separately at this
12297 point, as they may not represent instantiations of this
12298 template, and in any case are considered separate under the
12299 discrete model. */
12300 tree r = copy_decl (t);
12301 DECL_USE_TEMPLATE (r) = 0;
12302 TREE_TYPE (r) = type;
12303 /* Clear out the mangled name and RTL for the instantiation. */
12304 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12305 SET_DECL_RTL (r, NULL);
12306 /* Leave DECL_INITIAL set on deleted instantiations. */
12307 if (!DECL_DELETED_FN (r))
12308 DECL_INITIAL (r) = NULL_TREE;
12309 DECL_CONTEXT (r) = ctx;
12311 /* OpenMP UDRs have the only argument a reference to the declared
12312 type. We want to diagnose if the declared type is a reference,
12313 which is invalid, but as references to references are usually
12314 quietly merged, diagnose it here. */
12315 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12317 tree argtype
12318 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12319 argtype = tsubst (argtype, args, complain, in_decl);
12320 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12321 error_at (DECL_SOURCE_LOCATION (t),
12322 "reference type %qT in "
12323 "%<#pragma omp declare reduction%>", argtype);
12324 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12325 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12326 argtype);
12329 if (member && DECL_CONV_FN_P (r))
12330 /* Type-conversion operator. Reconstruct the name, in
12331 case it's the name of one of the template's parameters. */
12332 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12334 tree parms = DECL_ARGUMENTS (t);
12335 if (closure)
12336 parms = DECL_CHAIN (parms);
12337 parms = tsubst (parms, args, complain, t);
12338 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12339 DECL_CONTEXT (parm) = r;
12340 if (closure)
12342 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12343 DECL_CHAIN (tparm) = parms;
12344 parms = tparm;
12346 DECL_ARGUMENTS (r) = parms;
12347 DECL_RESULT (r) = NULL_TREE;
12349 TREE_STATIC (r) = 0;
12350 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12351 DECL_EXTERNAL (r) = 1;
12352 /* If this is an instantiation of a function with internal
12353 linkage, we already know what object file linkage will be
12354 assigned to the instantiation. */
12355 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12356 DECL_DEFER_OUTPUT (r) = 0;
12357 DECL_CHAIN (r) = NULL_TREE;
12358 DECL_PENDING_INLINE_INFO (r) = 0;
12359 DECL_PENDING_INLINE_P (r) = 0;
12360 DECL_SAVED_TREE (r) = NULL_TREE;
12361 DECL_STRUCT_FUNCTION (r) = NULL;
12362 TREE_USED (r) = 0;
12363 /* We'll re-clone as appropriate in instantiate_template. */
12364 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12366 /* If we aren't complaining now, return on error before we register
12367 the specialization so that we'll complain eventually. */
12368 if ((complain & tf_error) == 0
12369 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12370 && !grok_op_properties (r, /*complain=*/false))
12371 return error_mark_node;
12373 /* When instantiating a constrained member, substitute
12374 into the constraints to create a new constraint. */
12375 if (tree ci = get_constraints (t))
12376 if (member)
12378 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12379 set_constraints (r, ci);
12382 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12383 this in the special friend case mentioned above where
12384 GEN_TMPL is NULL. */
12385 if (gen_tmpl && !closure)
12387 DECL_TEMPLATE_INFO (r)
12388 = build_template_info (gen_tmpl, argvec);
12389 SET_DECL_IMPLICIT_INSTANTIATION (r);
12391 tree new_r
12392 = register_specialization (r, gen_tmpl, argvec, false, hash);
12393 if (new_r != r)
12394 /* We instantiated this while substituting into
12395 the type earlier (template/friend54.C). */
12396 return new_r;
12398 /* We're not supposed to instantiate default arguments
12399 until they are called, for a template. But, for a
12400 declaration like:
12402 template <class T> void f ()
12403 { extern void g(int i = T()); }
12405 we should do the substitution when the template is
12406 instantiated. We handle the member function case in
12407 instantiate_class_template since the default arguments
12408 might refer to other members of the class. */
12409 if (!member
12410 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12411 && !uses_template_parms (argvec))
12412 tsubst_default_arguments (r, complain);
12414 else
12415 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12417 /* Copy the list of befriending classes. */
12418 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12419 *friends;
12420 friends = &TREE_CHAIN (*friends))
12422 *friends = copy_node (*friends);
12423 TREE_VALUE (*friends)
12424 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12427 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12429 maybe_retrofit_in_chrg (r);
12430 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12431 return error_mark_node;
12432 /* If this is an instantiation of a member template, clone it.
12433 If it isn't, that'll be handled by
12434 clone_constructors_and_destructors. */
12435 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12436 clone_function_decl (r, /*update_methods=*/false);
12438 else if ((complain & tf_error) != 0
12439 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12440 && !grok_op_properties (r, /*complain=*/true))
12441 return error_mark_node;
12443 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12444 SET_DECL_FRIEND_CONTEXT (r,
12445 tsubst (DECL_FRIEND_CONTEXT (t),
12446 args, complain, in_decl));
12448 /* Possibly limit visibility based on template args. */
12449 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12450 if (DECL_VISIBILITY_SPECIFIED (t))
12452 DECL_VISIBILITY_SPECIFIED (r) = 0;
12453 DECL_ATTRIBUTES (r)
12454 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12456 determine_visibility (r);
12457 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12458 && !processing_template_decl)
12459 defaulted_late_check (r);
12461 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12462 args, complain, in_decl);
12463 return r;
12466 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12468 static tree
12469 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12470 tree lambda_fntype)
12472 /* We can get here when processing a member function template,
12473 member class template, or template template parameter. */
12474 tree decl = DECL_TEMPLATE_RESULT (t);
12475 tree in_decl = t;
12476 tree spec;
12477 tree tmpl_args;
12478 tree full_args;
12479 tree r;
12480 hashval_t hash = 0;
12482 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12484 /* Template template parameter is treated here. */
12485 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12486 if (new_type == error_mark_node)
12487 r = error_mark_node;
12488 /* If we get a real template back, return it. This can happen in
12489 the context of most_specialized_partial_spec. */
12490 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12491 r = new_type;
12492 else
12493 /* The new TEMPLATE_DECL was built in
12494 reduce_template_parm_level. */
12495 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12496 return r;
12499 if (!lambda_fntype)
12501 /* We might already have an instance of this template.
12502 The ARGS are for the surrounding class type, so the
12503 full args contain the tsubst'd args for the context,
12504 plus the innermost args from the template decl. */
12505 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12506 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12507 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12508 /* Because this is a template, the arguments will still be
12509 dependent, even after substitution. If
12510 PROCESSING_TEMPLATE_DECL is not set, the dependency
12511 predicates will short-circuit. */
12512 ++processing_template_decl;
12513 full_args = tsubst_template_args (tmpl_args, args,
12514 complain, in_decl);
12515 --processing_template_decl;
12516 if (full_args == error_mark_node)
12517 return error_mark_node;
12519 /* If this is a default template template argument,
12520 tsubst might not have changed anything. */
12521 if (full_args == tmpl_args)
12522 return t;
12524 hash = hash_tmpl_and_args (t, full_args);
12525 spec = retrieve_specialization (t, full_args, hash);
12526 if (spec != NULL_TREE)
12527 return spec;
12530 /* Make a new template decl. It will be similar to the
12531 original, but will record the current template arguments.
12532 We also create a new function declaration, which is just
12533 like the old one, but points to this new template, rather
12534 than the old one. */
12535 r = copy_decl (t);
12536 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12537 DECL_CHAIN (r) = NULL_TREE;
12539 // Build new template info linking to the original template decl.
12540 if (!lambda_fntype)
12542 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12543 SET_DECL_IMPLICIT_INSTANTIATION (r);
12545 else
12546 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12548 /* The template parameters for this new template are all the
12549 template parameters for the old template, except the
12550 outermost level of parameters. */
12551 DECL_TEMPLATE_PARMS (r)
12552 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12553 complain);
12555 if (TREE_CODE (decl) == TYPE_DECL
12556 && !TYPE_DECL_ALIAS_P (decl))
12558 tree new_type;
12559 ++processing_template_decl;
12560 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12561 --processing_template_decl;
12562 if (new_type == error_mark_node)
12563 return error_mark_node;
12565 TREE_TYPE (r) = new_type;
12566 /* For a partial specialization, we need to keep pointing to
12567 the primary template. */
12568 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12569 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12570 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12571 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12572 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12574 else
12576 tree new_decl;
12577 ++processing_template_decl;
12578 if (TREE_CODE (decl) == FUNCTION_DECL)
12579 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12580 else
12581 new_decl = tsubst (decl, args, complain, in_decl);
12582 --processing_template_decl;
12583 if (new_decl == error_mark_node)
12584 return error_mark_node;
12586 DECL_TEMPLATE_RESULT (r) = new_decl;
12587 TREE_TYPE (r) = TREE_TYPE (new_decl);
12588 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12589 if (lambda_fntype)
12591 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12592 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12594 else
12596 DECL_TI_TEMPLATE (new_decl) = r;
12597 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12601 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12602 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12604 if (PRIMARY_TEMPLATE_P (t))
12605 DECL_PRIMARY_TEMPLATE (r) = r;
12607 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12608 && !lambda_fntype)
12609 /* Record this non-type partial instantiation. */
12610 register_specialization (r, t,
12611 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12612 false, hash);
12614 return r;
12617 /* True if FN is the op() for a lambda in an uninstantiated template. */
12619 bool
12620 lambda_fn_in_template_p (tree fn)
12622 if (!fn || !LAMBDA_FUNCTION_P (fn))
12623 return false;
12624 tree closure = DECL_CONTEXT (fn);
12625 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12628 /* We're instantiating a variable from template function TCTX. Return the
12629 corresponding current enclosing scope. This gets complicated because lambda
12630 functions in templates are regenerated rather than instantiated, but generic
12631 lambda functions are subsequently instantiated. */
12633 static tree
12634 enclosing_instantiation_of (tree otctx)
12636 tree tctx = otctx;
12637 tree fn = current_function_decl;
12638 int lambda_count = 0;
12640 for (; tctx && lambda_fn_in_template_p (tctx);
12641 tctx = decl_function_context (tctx))
12642 ++lambda_count;
12643 for (; fn; fn = decl_function_context (fn))
12645 tree ofn = fn;
12646 int flambda_count = 0;
12647 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12648 fn = decl_function_context (fn))
12649 ++flambda_count;
12650 if (DECL_TEMPLATE_INFO (fn)
12651 ? most_general_template (fn) != most_general_template (tctx)
12652 : fn != tctx)
12653 continue;
12654 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12655 || DECL_CONV_FN_P (ofn));
12656 return ofn;
12658 gcc_unreachable ();
12661 /* Substitute the ARGS into the T, which is a _DECL. Return the
12662 result of the substitution. Issue error and warning messages under
12663 control of COMPLAIN. */
12665 static tree
12666 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12668 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12669 location_t saved_loc;
12670 tree r = NULL_TREE;
12671 tree in_decl = t;
12672 hashval_t hash = 0;
12674 /* Set the filename and linenumber to improve error-reporting. */
12675 saved_loc = input_location;
12676 input_location = DECL_SOURCE_LOCATION (t);
12678 switch (TREE_CODE (t))
12680 case TEMPLATE_DECL:
12681 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12682 break;
12684 case FUNCTION_DECL:
12685 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12686 break;
12688 case PARM_DECL:
12690 tree type = NULL_TREE;
12691 int i, len = 1;
12692 tree expanded_types = NULL_TREE;
12693 tree prev_r = NULL_TREE;
12694 tree first_r = NULL_TREE;
12696 if (DECL_PACK_P (t))
12698 /* If there is a local specialization that isn't a
12699 parameter pack, it means that we're doing a "simple"
12700 substitution from inside tsubst_pack_expansion. Just
12701 return the local specialization (which will be a single
12702 parm). */
12703 tree spec = retrieve_local_specialization (t);
12704 if (spec
12705 && TREE_CODE (spec) == PARM_DECL
12706 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12707 RETURN (spec);
12709 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12710 the parameters in this function parameter pack. */
12711 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12712 complain, in_decl);
12713 if (TREE_CODE (expanded_types) == TREE_VEC)
12715 len = TREE_VEC_LENGTH (expanded_types);
12717 /* Zero-length parameter packs are boring. Just substitute
12718 into the chain. */
12719 if (len == 0)
12720 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12721 TREE_CHAIN (t)));
12723 else
12725 /* All we did was update the type. Make a note of that. */
12726 type = expanded_types;
12727 expanded_types = NULL_TREE;
12731 /* Loop through all of the parameters we'll build. When T is
12732 a function parameter pack, LEN is the number of expanded
12733 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12734 r = NULL_TREE;
12735 for (i = 0; i < len; ++i)
12737 prev_r = r;
12738 r = copy_node (t);
12739 if (DECL_TEMPLATE_PARM_P (t))
12740 SET_DECL_TEMPLATE_PARM_P (r);
12742 if (expanded_types)
12743 /* We're on the Ith parameter of the function parameter
12744 pack. */
12746 /* Get the Ith type. */
12747 type = TREE_VEC_ELT (expanded_types, i);
12749 /* Rename the parameter to include the index. */
12750 DECL_NAME (r)
12751 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12753 else if (!type)
12754 /* We're dealing with a normal parameter. */
12755 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12757 type = type_decays_to (type);
12758 TREE_TYPE (r) = type;
12759 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12761 if (DECL_INITIAL (r))
12763 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12764 DECL_INITIAL (r) = TREE_TYPE (r);
12765 else
12766 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12767 complain, in_decl);
12770 DECL_CONTEXT (r) = NULL_TREE;
12772 if (!DECL_TEMPLATE_PARM_P (r))
12773 DECL_ARG_TYPE (r) = type_passed_as (type);
12775 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12776 args, complain, in_decl);
12778 /* Keep track of the first new parameter we
12779 generate. That's what will be returned to the
12780 caller. */
12781 if (!first_r)
12782 first_r = r;
12784 /* Build a proper chain of parameters when substituting
12785 into a function parameter pack. */
12786 if (prev_r)
12787 DECL_CHAIN (prev_r) = r;
12790 /* If cp_unevaluated_operand is set, we're just looking for a
12791 single dummy parameter, so don't keep going. */
12792 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12793 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12794 complain, DECL_CHAIN (t));
12796 /* FIRST_R contains the start of the chain we've built. */
12797 r = first_r;
12799 break;
12801 case FIELD_DECL:
12803 tree type = NULL_TREE;
12804 tree vec = NULL_TREE;
12805 tree expanded_types = NULL_TREE;
12806 int len = 1;
12808 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12810 /* This field is a lambda capture pack. Return a TREE_VEC of
12811 the expanded fields to instantiate_class_template_1 and
12812 store them in the specializations hash table as a
12813 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12814 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12815 complain, in_decl);
12816 if (TREE_CODE (expanded_types) == TREE_VEC)
12818 len = TREE_VEC_LENGTH (expanded_types);
12819 vec = make_tree_vec (len);
12821 else
12823 /* All we did was update the type. Make a note of that. */
12824 type = expanded_types;
12825 expanded_types = NULL_TREE;
12829 for (int i = 0; i < len; ++i)
12831 r = copy_decl (t);
12832 if (expanded_types)
12834 type = TREE_VEC_ELT (expanded_types, i);
12835 DECL_NAME (r)
12836 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12838 else if (!type)
12839 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12841 if (type == error_mark_node)
12842 RETURN (error_mark_node);
12843 TREE_TYPE (r) = type;
12844 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12846 if (DECL_C_BIT_FIELD (r))
12847 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12848 number of bits. */
12849 DECL_BIT_FIELD_REPRESENTATIVE (r)
12850 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12851 complain, in_decl,
12852 /*integral_constant_expression_p=*/true);
12853 if (DECL_INITIAL (t))
12855 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12856 NSDMI in perform_member_init. Still set DECL_INITIAL
12857 so that we know there is one. */
12858 DECL_INITIAL (r) = void_node;
12859 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12860 retrofit_lang_decl (r);
12861 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12863 /* We don't have to set DECL_CONTEXT here; it is set by
12864 finish_member_declaration. */
12865 DECL_CHAIN (r) = NULL_TREE;
12867 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12868 args, complain, in_decl);
12870 if (vec)
12871 TREE_VEC_ELT (vec, i) = r;
12874 if (vec)
12876 r = vec;
12877 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12878 SET_ARGUMENT_PACK_ARGS (pack, vec);
12879 register_specialization (pack, t, args, false, 0);
12882 break;
12884 case USING_DECL:
12885 /* We reach here only for member using decls. We also need to check
12886 uses_template_parms because DECL_DEPENDENT_P is not set for a
12887 using-declaration that designates a member of the current
12888 instantiation (c++/53549). */
12889 if (DECL_DEPENDENT_P (t)
12890 || uses_template_parms (USING_DECL_SCOPE (t)))
12892 tree scope = USING_DECL_SCOPE (t);
12893 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12894 if (PACK_EXPANSION_P (scope))
12896 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12897 int len = TREE_VEC_LENGTH (vec);
12898 r = make_tree_vec (len);
12899 for (int i = 0; i < len; ++i)
12901 tree escope = TREE_VEC_ELT (vec, i);
12902 tree elt = do_class_using_decl (escope, name);
12903 if (!elt)
12905 r = error_mark_node;
12906 break;
12908 else
12910 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12911 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12913 TREE_VEC_ELT (r, i) = elt;
12916 else
12918 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12919 complain, in_decl);
12920 r = do_class_using_decl (inst_scope, name);
12921 if (!r)
12922 r = error_mark_node;
12923 else
12925 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12926 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12930 else
12932 r = copy_node (t);
12933 DECL_CHAIN (r) = NULL_TREE;
12935 break;
12937 case TYPE_DECL:
12938 case VAR_DECL:
12940 tree argvec = NULL_TREE;
12941 tree gen_tmpl = NULL_TREE;
12942 tree spec;
12943 tree tmpl = NULL_TREE;
12944 tree ctx;
12945 tree type = NULL_TREE;
12946 bool local_p;
12948 if (TREE_TYPE (t) == error_mark_node)
12949 RETURN (error_mark_node);
12951 if (TREE_CODE (t) == TYPE_DECL
12952 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12954 /* If this is the canonical decl, we don't have to
12955 mess with instantiations, and often we can't (for
12956 typename, template type parms and such). Note that
12957 TYPE_NAME is not correct for the above test if
12958 we've copied the type for a typedef. */
12959 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12960 if (type == error_mark_node)
12961 RETURN (error_mark_node);
12962 r = TYPE_NAME (type);
12963 break;
12966 /* Check to see if we already have the specialization we
12967 need. */
12968 spec = NULL_TREE;
12969 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12971 /* T is a static data member or namespace-scope entity.
12972 We have to substitute into namespace-scope variables
12973 (not just variable templates) because of cases like:
12975 template <class T> void f() { extern T t; }
12977 where the entity referenced is not known until
12978 instantiation time. */
12979 local_p = false;
12980 ctx = DECL_CONTEXT (t);
12981 if (DECL_CLASS_SCOPE_P (t))
12983 ctx = tsubst_aggr_type (ctx, args,
12984 complain,
12985 in_decl, /*entering_scope=*/1);
12986 /* If CTX is unchanged, then T is in fact the
12987 specialization we want. That situation occurs when
12988 referencing a static data member within in its own
12989 class. We can use pointer equality, rather than
12990 same_type_p, because DECL_CONTEXT is always
12991 canonical... */
12992 if (ctx == DECL_CONTEXT (t)
12993 /* ... unless T is a member template; in which
12994 case our caller can be willing to create a
12995 specialization of that template represented
12996 by T. */
12997 && !(DECL_TI_TEMPLATE (t)
12998 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12999 spec = t;
13002 if (!spec)
13004 tmpl = DECL_TI_TEMPLATE (t);
13005 gen_tmpl = most_general_template (tmpl);
13006 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13007 if (argvec != error_mark_node)
13008 argvec = (coerce_innermost_template_parms
13009 (DECL_TEMPLATE_PARMS (gen_tmpl),
13010 argvec, t, complain,
13011 /*all*/true, /*defarg*/true));
13012 if (argvec == error_mark_node)
13013 RETURN (error_mark_node);
13014 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13015 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13018 else
13020 /* A local variable. */
13021 local_p = true;
13022 /* Subsequent calls to pushdecl will fill this in. */
13023 ctx = NULL_TREE;
13024 /* Unless this is a reference to a static variable from an
13025 enclosing function, in which case we need to fill it in now. */
13026 if (TREE_STATIC (t))
13028 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13029 if (fn != current_function_decl)
13030 ctx = fn;
13032 spec = retrieve_local_specialization (t);
13034 /* If we already have the specialization we need, there is
13035 nothing more to do. */
13036 if (spec)
13038 r = spec;
13039 break;
13042 /* Create a new node for the specialization we need. */
13043 r = copy_decl (t);
13044 if (type == NULL_TREE)
13046 if (is_typedef_decl (t))
13047 type = DECL_ORIGINAL_TYPE (t);
13048 else
13049 type = TREE_TYPE (t);
13050 if (VAR_P (t)
13051 && VAR_HAD_UNKNOWN_BOUND (t)
13052 && type != error_mark_node)
13053 type = strip_array_domain (type);
13054 tree sub_args = args;
13055 if (tree auto_node = type_uses_auto (type))
13057 /* Mask off any template args past the variable's context so we
13058 don't replace the auto with an unrelated argument. */
13059 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13060 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13061 if (extra > 0)
13062 /* This should never happen with the new lambda instantiation
13063 model, but keep the handling just in case. */
13064 gcc_assert (!CHECKING_P),
13065 sub_args = strip_innermost_template_args (args, extra);
13067 type = tsubst (type, sub_args, complain, in_decl);
13069 if (VAR_P (r))
13071 /* Even if the original location is out of scope, the
13072 newly substituted one is not. */
13073 DECL_DEAD_FOR_LOCAL (r) = 0;
13074 DECL_INITIALIZED_P (r) = 0;
13075 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13076 if (type == error_mark_node)
13077 RETURN (error_mark_node);
13078 if (TREE_CODE (type) == FUNCTION_TYPE)
13080 /* It may seem that this case cannot occur, since:
13082 typedef void f();
13083 void g() { f x; }
13085 declares a function, not a variable. However:
13087 typedef void f();
13088 template <typename T> void g() { T t; }
13089 template void g<f>();
13091 is an attempt to declare a variable with function
13092 type. */
13093 error ("variable %qD has function type",
13094 /* R is not yet sufficiently initialized, so we
13095 just use its name. */
13096 DECL_NAME (r));
13097 RETURN (error_mark_node);
13099 type = complete_type (type);
13100 /* Wait until cp_finish_decl to set this again, to handle
13101 circular dependency (template/instantiate6.C). */
13102 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13103 type = check_var_type (DECL_NAME (r), type);
13105 if (DECL_HAS_VALUE_EXPR_P (t))
13107 tree ve = DECL_VALUE_EXPR (t);
13108 ve = tsubst_expr (ve, args, complain, in_decl,
13109 /*constant_expression_p=*/false);
13110 if (REFERENCE_REF_P (ve))
13112 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13113 ve = TREE_OPERAND (ve, 0);
13115 SET_DECL_VALUE_EXPR (r, ve);
13117 if (CP_DECL_THREAD_LOCAL_P (r)
13118 && !processing_template_decl)
13119 set_decl_tls_model (r, decl_default_tls_model (r));
13121 else if (DECL_SELF_REFERENCE_P (t))
13122 SET_DECL_SELF_REFERENCE_P (r);
13123 TREE_TYPE (r) = type;
13124 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13125 DECL_CONTEXT (r) = ctx;
13126 /* Clear out the mangled name and RTL for the instantiation. */
13127 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13128 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13129 SET_DECL_RTL (r, NULL);
13130 /* The initializer must not be expanded until it is required;
13131 see [temp.inst]. */
13132 DECL_INITIAL (r) = NULL_TREE;
13133 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13134 if (VAR_P (r))
13136 if (DECL_LANG_SPECIFIC (r))
13137 SET_DECL_DEPENDENT_INIT_P (r, false);
13139 SET_DECL_MODE (r, VOIDmode);
13141 /* Possibly limit visibility based on template args. */
13142 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13143 if (DECL_VISIBILITY_SPECIFIED (t))
13145 DECL_VISIBILITY_SPECIFIED (r) = 0;
13146 DECL_ATTRIBUTES (r)
13147 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13149 determine_visibility (r);
13152 if (!local_p)
13154 /* A static data member declaration is always marked
13155 external when it is declared in-class, even if an
13156 initializer is present. We mimic the non-template
13157 processing here. */
13158 DECL_EXTERNAL (r) = 1;
13159 if (DECL_NAMESPACE_SCOPE_P (t))
13160 DECL_NOT_REALLY_EXTERN (r) = 1;
13162 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13163 SET_DECL_IMPLICIT_INSTANTIATION (r);
13164 register_specialization (r, gen_tmpl, argvec, false, hash);
13166 else
13168 if (DECL_LANG_SPECIFIC (r))
13169 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13170 if (!cp_unevaluated_operand)
13171 register_local_specialization (r, t);
13174 DECL_CHAIN (r) = NULL_TREE;
13176 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13177 /*flags=*/0,
13178 args, complain, in_decl);
13180 /* Preserve a typedef that names a type. */
13181 if (is_typedef_decl (r) && type != error_mark_node)
13183 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13184 set_underlying_type (r);
13185 if (TYPE_DECL_ALIAS_P (r))
13186 /* An alias template specialization can be dependent
13187 even if its underlying type is not. */
13188 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13191 layout_decl (r, 0);
13193 break;
13195 default:
13196 gcc_unreachable ();
13198 #undef RETURN
13200 out:
13201 /* Restore the file and line information. */
13202 input_location = saved_loc;
13204 return r;
13207 /* Substitute into the ARG_TYPES of a function type.
13208 If END is a TREE_CHAIN, leave it and any following types
13209 un-substituted. */
13211 static tree
13212 tsubst_arg_types (tree arg_types,
13213 tree args,
13214 tree end,
13215 tsubst_flags_t complain,
13216 tree in_decl)
13218 tree remaining_arg_types;
13219 tree type = NULL_TREE;
13220 int i = 1;
13221 tree expanded_args = NULL_TREE;
13222 tree default_arg;
13224 if (!arg_types || arg_types == void_list_node || arg_types == end)
13225 return arg_types;
13227 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13228 args, end, complain, in_decl);
13229 if (remaining_arg_types == error_mark_node)
13230 return error_mark_node;
13232 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13234 /* For a pack expansion, perform substitution on the
13235 entire expression. Later on, we'll handle the arguments
13236 one-by-one. */
13237 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13238 args, complain, in_decl);
13240 if (TREE_CODE (expanded_args) == TREE_VEC)
13241 /* So that we'll spin through the parameters, one by one. */
13242 i = TREE_VEC_LENGTH (expanded_args);
13243 else
13245 /* We only partially substituted into the parameter
13246 pack. Our type is TYPE_PACK_EXPANSION. */
13247 type = expanded_args;
13248 expanded_args = NULL_TREE;
13252 while (i > 0) {
13253 --i;
13255 if (expanded_args)
13256 type = TREE_VEC_ELT (expanded_args, i);
13257 else if (!type)
13258 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13260 if (type == error_mark_node)
13261 return error_mark_node;
13262 if (VOID_TYPE_P (type))
13264 if (complain & tf_error)
13266 error ("invalid parameter type %qT", type);
13267 if (in_decl)
13268 error ("in declaration %q+D", in_decl);
13270 return error_mark_node;
13272 /* DR 657. */
13273 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13274 return error_mark_node;
13276 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13277 top-level qualifiers as required. */
13278 type = cv_unqualified (type_decays_to (type));
13280 /* We do not substitute into default arguments here. The standard
13281 mandates that they be instantiated only when needed, which is
13282 done in build_over_call. */
13283 default_arg = TREE_PURPOSE (arg_types);
13285 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13286 since the new op() won't have any associated template arguments for us
13287 to refer to later. */
13288 if (lambda_fn_in_template_p (in_decl))
13289 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13290 false/*fn*/, false/*constexpr*/);
13292 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13294 /* We've instantiated a template before its default arguments
13295 have been parsed. This can happen for a nested template
13296 class, and is not an error unless we require the default
13297 argument in a call of this function. */
13298 remaining_arg_types =
13299 tree_cons (default_arg, type, remaining_arg_types);
13300 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13302 else
13303 remaining_arg_types =
13304 hash_tree_cons (default_arg, type, remaining_arg_types);
13307 return remaining_arg_types;
13310 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13311 *not* handle the exception-specification for FNTYPE, because the
13312 initial substitution of explicitly provided template parameters
13313 during argument deduction forbids substitution into the
13314 exception-specification:
13316 [temp.deduct]
13318 All references in the function type of the function template to the
13319 corresponding template parameters are replaced by the specified tem-
13320 plate argument values. If a substitution in a template parameter or
13321 in the function type of the function template results in an invalid
13322 type, type deduction fails. [Note: The equivalent substitution in
13323 exception specifications is done only when the function is instanti-
13324 ated, at which point a program is ill-formed if the substitution
13325 results in an invalid type.] */
13327 static tree
13328 tsubst_function_type (tree t,
13329 tree args,
13330 tsubst_flags_t complain,
13331 tree in_decl)
13333 tree return_type;
13334 tree arg_types = NULL_TREE;
13335 tree fntype;
13337 /* The TYPE_CONTEXT is not used for function/method types. */
13338 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13340 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13341 failure. */
13342 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13344 if (late_return_type_p)
13346 /* Substitute the argument types. */
13347 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13348 complain, in_decl);
13349 if (arg_types == error_mark_node)
13350 return error_mark_node;
13352 tree save_ccp = current_class_ptr;
13353 tree save_ccr = current_class_ref;
13354 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13355 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13356 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13357 if (do_inject)
13359 /* DR 1207: 'this' is in scope in the trailing return type. */
13360 inject_this_parameter (this_type, cp_type_quals (this_type));
13363 /* Substitute the return type. */
13364 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13366 if (do_inject)
13368 current_class_ptr = save_ccp;
13369 current_class_ref = save_ccr;
13372 else
13373 /* Substitute the return type. */
13374 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13376 if (return_type == error_mark_node)
13377 return error_mark_node;
13378 /* DR 486 clarifies that creation of a function type with an
13379 invalid return type is a deduction failure. */
13380 if (TREE_CODE (return_type) == ARRAY_TYPE
13381 || TREE_CODE (return_type) == FUNCTION_TYPE)
13383 if (complain & tf_error)
13385 if (TREE_CODE (return_type) == ARRAY_TYPE)
13386 error ("function returning an array");
13387 else
13388 error ("function returning a function");
13390 return error_mark_node;
13392 /* And DR 657. */
13393 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13394 return error_mark_node;
13396 if (!late_return_type_p)
13398 /* Substitute the argument types. */
13399 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13400 complain, in_decl);
13401 if (arg_types == error_mark_node)
13402 return error_mark_node;
13405 /* Construct a new type node and return it. */
13406 if (TREE_CODE (t) == FUNCTION_TYPE)
13408 fntype = build_function_type (return_type, arg_types);
13409 fntype = apply_memfn_quals (fntype,
13410 type_memfn_quals (t),
13411 type_memfn_rqual (t));
13413 else
13415 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13416 /* Don't pick up extra function qualifiers from the basetype. */
13417 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13418 if (! MAYBE_CLASS_TYPE_P (r))
13420 /* [temp.deduct]
13422 Type deduction may fail for any of the following
13423 reasons:
13425 -- Attempting to create "pointer to member of T" when T
13426 is not a class type. */
13427 if (complain & tf_error)
13428 error ("creating pointer to member function of non-class type %qT",
13430 return error_mark_node;
13433 fntype = build_method_type_directly (r, return_type,
13434 TREE_CHAIN (arg_types));
13435 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13437 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13439 if (late_return_type_p)
13440 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13442 return fntype;
13445 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13446 ARGS into that specification, and return the substituted
13447 specification. If there is no specification, return NULL_TREE. */
13449 static tree
13450 tsubst_exception_specification (tree fntype,
13451 tree args,
13452 tsubst_flags_t complain,
13453 tree in_decl,
13454 bool defer_ok)
13456 tree specs;
13457 tree new_specs;
13459 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13460 new_specs = NULL_TREE;
13461 if (specs && TREE_PURPOSE (specs))
13463 /* A noexcept-specifier. */
13464 tree expr = TREE_PURPOSE (specs);
13465 if (TREE_CODE (expr) == INTEGER_CST)
13466 new_specs = expr;
13467 else if (defer_ok)
13469 /* Defer instantiation of noexcept-specifiers to avoid
13470 excessive instantiations (c++/49107). */
13471 new_specs = make_node (DEFERRED_NOEXCEPT);
13472 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13474 /* We already partially instantiated this member template,
13475 so combine the new args with the old. */
13476 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13477 = DEFERRED_NOEXCEPT_PATTERN (expr);
13478 DEFERRED_NOEXCEPT_ARGS (new_specs)
13479 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13481 else
13483 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13484 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13487 else
13488 new_specs = tsubst_copy_and_build
13489 (expr, args, complain, in_decl, /*function_p=*/false,
13490 /*integral_constant_expression_p=*/true);
13491 new_specs = build_noexcept_spec (new_specs, complain);
13493 else if (specs)
13495 if (! TREE_VALUE (specs))
13496 new_specs = specs;
13497 else
13498 while (specs)
13500 tree spec;
13501 int i, len = 1;
13502 tree expanded_specs = NULL_TREE;
13504 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13506 /* Expand the pack expansion type. */
13507 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13508 args, complain,
13509 in_decl);
13511 if (expanded_specs == error_mark_node)
13512 return error_mark_node;
13513 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13514 len = TREE_VEC_LENGTH (expanded_specs);
13515 else
13517 /* We're substituting into a member template, so
13518 we got a TYPE_PACK_EXPANSION back. Add that
13519 expansion and move on. */
13520 gcc_assert (TREE_CODE (expanded_specs)
13521 == TYPE_PACK_EXPANSION);
13522 new_specs = add_exception_specifier (new_specs,
13523 expanded_specs,
13524 complain);
13525 specs = TREE_CHAIN (specs);
13526 continue;
13530 for (i = 0; i < len; ++i)
13532 if (expanded_specs)
13533 spec = TREE_VEC_ELT (expanded_specs, i);
13534 else
13535 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13536 if (spec == error_mark_node)
13537 return spec;
13538 new_specs = add_exception_specifier (new_specs, spec,
13539 complain);
13542 specs = TREE_CHAIN (specs);
13545 return new_specs;
13548 /* Take the tree structure T and replace template parameters used
13549 therein with the argument vector ARGS. IN_DECL is an associated
13550 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13551 Issue error and warning messages under control of COMPLAIN. Note
13552 that we must be relatively non-tolerant of extensions here, in
13553 order to preserve conformance; if we allow substitutions that
13554 should not be allowed, we may allow argument deductions that should
13555 not succeed, and therefore report ambiguous overload situations
13556 where there are none. In theory, we could allow the substitution,
13557 but indicate that it should have failed, and allow our caller to
13558 make sure that the right thing happens, but we don't try to do this
13559 yet.
13561 This function is used for dealing with types, decls and the like;
13562 for expressions, use tsubst_expr or tsubst_copy. */
13564 tree
13565 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13567 enum tree_code code;
13568 tree type, r = NULL_TREE;
13570 if (t == NULL_TREE || t == error_mark_node
13571 || t == integer_type_node
13572 || t == void_type_node
13573 || t == char_type_node
13574 || t == unknown_type_node
13575 || TREE_CODE (t) == NAMESPACE_DECL
13576 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13577 return t;
13579 if (DECL_P (t))
13580 return tsubst_decl (t, args, complain);
13582 if (args == NULL_TREE)
13583 return t;
13585 code = TREE_CODE (t);
13587 if (code == IDENTIFIER_NODE)
13588 type = IDENTIFIER_TYPE_VALUE (t);
13589 else
13590 type = TREE_TYPE (t);
13592 gcc_assert (type != unknown_type_node);
13594 /* Reuse typedefs. We need to do this to handle dependent attributes,
13595 such as attribute aligned. */
13596 if (TYPE_P (t)
13597 && typedef_variant_p (t))
13599 tree decl = TYPE_NAME (t);
13601 if (alias_template_specialization_p (t))
13603 /* DECL represents an alias template and we want to
13604 instantiate it. */
13605 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13606 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13607 r = instantiate_alias_template (tmpl, gen_args, complain);
13609 else if (DECL_CLASS_SCOPE_P (decl)
13610 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13611 && uses_template_parms (DECL_CONTEXT (decl)))
13613 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13614 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13615 r = retrieve_specialization (tmpl, gen_args, 0);
13617 else if (DECL_FUNCTION_SCOPE_P (decl)
13618 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13619 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13620 r = retrieve_local_specialization (decl);
13621 else
13622 /* The typedef is from a non-template context. */
13623 return t;
13625 if (r)
13627 r = TREE_TYPE (r);
13628 r = cp_build_qualified_type_real
13629 (r, cp_type_quals (t) | cp_type_quals (r),
13630 complain | tf_ignore_bad_quals);
13631 return r;
13633 else
13635 /* We don't have an instantiation yet, so drop the typedef. */
13636 int quals = cp_type_quals (t);
13637 t = DECL_ORIGINAL_TYPE (decl);
13638 t = cp_build_qualified_type_real (t, quals,
13639 complain | tf_ignore_bad_quals);
13643 bool fndecl_type = (complain & tf_fndecl_type);
13644 complain &= ~tf_fndecl_type;
13646 if (type
13647 && code != TYPENAME_TYPE
13648 && code != TEMPLATE_TYPE_PARM
13649 && code != TEMPLATE_PARM_INDEX
13650 && code != IDENTIFIER_NODE
13651 && code != FUNCTION_TYPE
13652 && code != METHOD_TYPE)
13653 type = tsubst (type, args, complain, in_decl);
13654 if (type == error_mark_node)
13655 return error_mark_node;
13657 switch (code)
13659 case RECORD_TYPE:
13660 case UNION_TYPE:
13661 case ENUMERAL_TYPE:
13662 return tsubst_aggr_type (t, args, complain, in_decl,
13663 /*entering_scope=*/0);
13665 case ERROR_MARK:
13666 case IDENTIFIER_NODE:
13667 case VOID_TYPE:
13668 case REAL_TYPE:
13669 case COMPLEX_TYPE:
13670 case VECTOR_TYPE:
13671 case BOOLEAN_TYPE:
13672 case NULLPTR_TYPE:
13673 case LANG_TYPE:
13674 return t;
13676 case INTEGER_TYPE:
13677 if (t == integer_type_node)
13678 return t;
13680 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13681 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13682 return t;
13685 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13687 max = tsubst_expr (omax, args, complain, in_decl,
13688 /*integral_constant_expression_p=*/false);
13690 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13691 needed. */
13692 if (TREE_CODE (max) == NOP_EXPR
13693 && TREE_SIDE_EFFECTS (omax)
13694 && !TREE_TYPE (max))
13695 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13697 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13698 with TREE_SIDE_EFFECTS that indicates this is not an integral
13699 constant expression. */
13700 if (processing_template_decl
13701 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13703 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13704 TREE_SIDE_EFFECTS (max) = 1;
13707 return compute_array_index_type (NULL_TREE, max, complain);
13710 case TEMPLATE_TYPE_PARM:
13711 case TEMPLATE_TEMPLATE_PARM:
13712 case BOUND_TEMPLATE_TEMPLATE_PARM:
13713 case TEMPLATE_PARM_INDEX:
13715 int idx;
13716 int level;
13717 int levels;
13718 tree arg = NULL_TREE;
13720 /* Early in template argument deduction substitution, we don't
13721 want to reduce the level of 'auto', or it will be confused
13722 with a normal template parm in subsequent deduction. */
13723 if (is_auto (t) && (complain & tf_partial))
13724 return t;
13726 r = NULL_TREE;
13728 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13729 template_parm_level_and_index (t, &level, &idx);
13731 levels = TMPL_ARGS_DEPTH (args);
13732 if (level <= levels
13733 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13735 arg = TMPL_ARG (args, level, idx);
13737 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13739 /* See through ARGUMENT_PACK_SELECT arguments. */
13740 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13741 /* If the selected argument is an expansion E, that most
13742 likely means we were called from
13743 gen_elem_of_pack_expansion_instantiation during the
13744 substituting of pack an argument pack (which Ith
13745 element is a pack expansion, where I is
13746 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13747 In this case, the Ith element resulting from this
13748 substituting is going to be a pack expansion, which
13749 pattern is the pattern of E. Let's return the
13750 pattern of E, and
13751 gen_elem_of_pack_expansion_instantiation will
13752 build the resulting pack expansion from it. */
13753 if (PACK_EXPANSION_P (arg))
13755 /* Make sure we aren't throwing away arg info. */
13756 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13757 arg = PACK_EXPANSION_PATTERN (arg);
13762 if (arg == error_mark_node)
13763 return error_mark_node;
13764 else if (arg != NULL_TREE)
13766 if (ARGUMENT_PACK_P (arg))
13767 /* If ARG is an argument pack, we don't actually want to
13768 perform a substitution here, because substitutions
13769 for argument packs are only done
13770 element-by-element. We can get to this point when
13771 substituting the type of a non-type template
13772 parameter pack, when that type actually contains
13773 template parameter packs from an outer template, e.g.,
13775 template<typename... Types> struct A {
13776 template<Types... Values> struct B { };
13777 }; */
13778 return t;
13780 if (code == TEMPLATE_TYPE_PARM)
13782 int quals;
13783 gcc_assert (TYPE_P (arg));
13785 quals = cp_type_quals (arg) | cp_type_quals (t);
13787 return cp_build_qualified_type_real
13788 (arg, quals, complain | tf_ignore_bad_quals);
13790 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13792 /* We are processing a type constructed from a
13793 template template parameter. */
13794 tree argvec = tsubst (TYPE_TI_ARGS (t),
13795 args, complain, in_decl);
13796 if (argvec == error_mark_node)
13797 return error_mark_node;
13799 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13800 || TREE_CODE (arg) == TEMPLATE_DECL
13801 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13803 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13804 /* Consider this code:
13806 template <template <class> class Template>
13807 struct Internal {
13808 template <class Arg> using Bind = Template<Arg>;
13811 template <template <class> class Template, class Arg>
13812 using Instantiate = Template<Arg>; //#0
13814 template <template <class> class Template,
13815 class Argument>
13816 using Bind =
13817 Instantiate<Internal<Template>::template Bind,
13818 Argument>; //#1
13820 When #1 is parsed, the
13821 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13822 parameter `Template' in #0 matches the
13823 UNBOUND_CLASS_TEMPLATE representing the argument
13824 `Internal<Template>::template Bind'; We then want
13825 to assemble the type `Bind<Argument>' that can't
13826 be fully created right now, because
13827 `Internal<Template>' not being complete, the Bind
13828 template cannot be looked up in that context. So
13829 we need to "store" `Bind<Argument>' for later
13830 when the context of Bind becomes complete. Let's
13831 store that in a TYPENAME_TYPE. */
13832 return make_typename_type (TYPE_CONTEXT (arg),
13833 build_nt (TEMPLATE_ID_EXPR,
13834 TYPE_IDENTIFIER (arg),
13835 argvec),
13836 typename_type,
13837 complain);
13839 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13840 are resolving nested-types in the signature of a
13841 member function templates. Otherwise ARG is a
13842 TEMPLATE_DECL and is the real template to be
13843 instantiated. */
13844 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13845 arg = TYPE_NAME (arg);
13847 r = lookup_template_class (arg,
13848 argvec, in_decl,
13849 DECL_CONTEXT (arg),
13850 /*entering_scope=*/0,
13851 complain);
13852 return cp_build_qualified_type_real
13853 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13855 else if (code == TEMPLATE_TEMPLATE_PARM)
13856 return arg;
13857 else
13858 /* TEMPLATE_PARM_INDEX. */
13859 return convert_from_reference (unshare_expr (arg));
13862 if (level == 1)
13863 /* This can happen during the attempted tsubst'ing in
13864 unify. This means that we don't yet have any information
13865 about the template parameter in question. */
13866 return t;
13868 /* If we get here, we must have been looking at a parm for a
13869 more deeply nested template. Make a new version of this
13870 template parameter, but with a lower level. */
13871 switch (code)
13873 case TEMPLATE_TYPE_PARM:
13874 case TEMPLATE_TEMPLATE_PARM:
13875 case BOUND_TEMPLATE_TEMPLATE_PARM:
13876 if (cp_type_quals (t))
13878 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13879 r = cp_build_qualified_type_real
13880 (r, cp_type_quals (t),
13881 complain | (code == TEMPLATE_TYPE_PARM
13882 ? tf_ignore_bad_quals : 0));
13884 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13885 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13886 && (r = (TEMPLATE_PARM_DESCENDANTS
13887 (TEMPLATE_TYPE_PARM_INDEX (t))))
13888 && (r = TREE_TYPE (r))
13889 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13890 /* Break infinite recursion when substituting the constraints
13891 of a constrained placeholder. */;
13892 else
13894 r = copy_type (t);
13895 TEMPLATE_TYPE_PARM_INDEX (r)
13896 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13897 r, levels, args, complain);
13898 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13899 TYPE_MAIN_VARIANT (r) = r;
13900 TYPE_POINTER_TO (r) = NULL_TREE;
13901 TYPE_REFERENCE_TO (r) = NULL_TREE;
13903 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13905 /* Propagate constraints on placeholders. */
13906 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13907 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13908 = tsubst_constraint (constr, args, complain, in_decl);
13909 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13911 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13912 pl = tsubst (pl, args, complain, in_decl);
13913 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13917 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13918 /* We have reduced the level of the template
13919 template parameter, but not the levels of its
13920 template parameters, so canonical_type_parameter
13921 will not be able to find the canonical template
13922 template parameter for this level. Thus, we
13923 require structural equality checking to compare
13924 TEMPLATE_TEMPLATE_PARMs. */
13925 SET_TYPE_STRUCTURAL_EQUALITY (r);
13926 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13927 SET_TYPE_STRUCTURAL_EQUALITY (r);
13928 else
13929 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13931 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13933 tree tinfo = TYPE_TEMPLATE_INFO (t);
13934 /* We might need to substitute into the types of non-type
13935 template parameters. */
13936 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13937 complain, in_decl);
13938 if (tmpl == error_mark_node)
13939 return error_mark_node;
13940 tree argvec = tsubst (TI_ARGS (tinfo), args,
13941 complain, in_decl);
13942 if (argvec == error_mark_node)
13943 return error_mark_node;
13945 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13946 = build_template_info (tmpl, argvec);
13949 break;
13951 case TEMPLATE_PARM_INDEX:
13952 /* OK, now substitute the type of the non-type parameter. We
13953 couldn't do it earlier because it might be an auto parameter,
13954 and we wouldn't need to if we had an argument. */
13955 type = tsubst (type, args, complain, in_decl);
13956 if (type == error_mark_node)
13957 return error_mark_node;
13958 r = reduce_template_parm_level (t, type, levels, args, complain);
13959 break;
13961 default:
13962 gcc_unreachable ();
13965 return r;
13968 case TREE_LIST:
13970 tree purpose, value, chain;
13972 if (t == void_list_node)
13973 return t;
13975 purpose = TREE_PURPOSE (t);
13976 if (purpose)
13978 purpose = tsubst (purpose, args, complain, in_decl);
13979 if (purpose == error_mark_node)
13980 return error_mark_node;
13982 value = TREE_VALUE (t);
13983 if (value)
13985 value = tsubst (value, args, complain, in_decl);
13986 if (value == error_mark_node)
13987 return error_mark_node;
13989 chain = TREE_CHAIN (t);
13990 if (chain && chain != void_type_node)
13992 chain = tsubst (chain, args, complain, in_decl);
13993 if (chain == error_mark_node)
13994 return error_mark_node;
13996 if (purpose == TREE_PURPOSE (t)
13997 && value == TREE_VALUE (t)
13998 && chain == TREE_CHAIN (t))
13999 return t;
14000 return hash_tree_cons (purpose, value, chain);
14003 case TREE_BINFO:
14004 /* We should never be tsubsting a binfo. */
14005 gcc_unreachable ();
14007 case TREE_VEC:
14008 /* A vector of template arguments. */
14009 gcc_assert (!type);
14010 return tsubst_template_args (t, args, complain, in_decl);
14012 case POINTER_TYPE:
14013 case REFERENCE_TYPE:
14015 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14016 return t;
14018 /* [temp.deduct]
14020 Type deduction may fail for any of the following
14021 reasons:
14023 -- Attempting to create a pointer to reference type.
14024 -- Attempting to create a reference to a reference type or
14025 a reference to void.
14027 Core issue 106 says that creating a reference to a reference
14028 during instantiation is no longer a cause for failure. We
14029 only enforce this check in strict C++98 mode. */
14030 if ((TREE_CODE (type) == REFERENCE_TYPE
14031 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14032 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14034 static location_t last_loc;
14036 /* We keep track of the last time we issued this error
14037 message to avoid spewing a ton of messages during a
14038 single bad template instantiation. */
14039 if (complain & tf_error
14040 && last_loc != input_location)
14042 if (VOID_TYPE_P (type))
14043 error ("forming reference to void");
14044 else if (code == POINTER_TYPE)
14045 error ("forming pointer to reference type %qT", type);
14046 else
14047 error ("forming reference to reference type %qT", type);
14048 last_loc = input_location;
14051 return error_mark_node;
14053 else if (TREE_CODE (type) == FUNCTION_TYPE
14054 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14055 || type_memfn_rqual (type) != REF_QUAL_NONE))
14057 if (complain & tf_error)
14059 if (code == POINTER_TYPE)
14060 error ("forming pointer to qualified function type %qT",
14061 type);
14062 else
14063 error ("forming reference to qualified function type %qT",
14064 type);
14066 return error_mark_node;
14068 else if (code == POINTER_TYPE)
14070 r = build_pointer_type (type);
14071 if (TREE_CODE (type) == METHOD_TYPE)
14072 r = build_ptrmemfunc_type (r);
14074 else if (TREE_CODE (type) == REFERENCE_TYPE)
14075 /* In C++0x, during template argument substitution, when there is an
14076 attempt to create a reference to a reference type, reference
14077 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14079 "If a template-argument for a template-parameter T names a type
14080 that is a reference to a type A, an attempt to create the type
14081 'lvalue reference to cv T' creates the type 'lvalue reference to
14082 A,' while an attempt to create the type type rvalue reference to
14083 cv T' creates the type T"
14085 r = cp_build_reference_type
14086 (TREE_TYPE (type),
14087 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14088 else
14089 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14090 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14092 if (r != error_mark_node)
14093 /* Will this ever be needed for TYPE_..._TO values? */
14094 layout_type (r);
14096 return r;
14098 case OFFSET_TYPE:
14100 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14101 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14103 /* [temp.deduct]
14105 Type deduction may fail for any of the following
14106 reasons:
14108 -- Attempting to create "pointer to member of T" when T
14109 is not a class type. */
14110 if (complain & tf_error)
14111 error ("creating pointer to member of non-class type %qT", r);
14112 return error_mark_node;
14114 if (TREE_CODE (type) == REFERENCE_TYPE)
14116 if (complain & tf_error)
14117 error ("creating pointer to member reference type %qT", type);
14118 return error_mark_node;
14120 if (VOID_TYPE_P (type))
14122 if (complain & tf_error)
14123 error ("creating pointer to member of type void");
14124 return error_mark_node;
14126 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14127 if (TREE_CODE (type) == FUNCTION_TYPE)
14129 /* The type of the implicit object parameter gets its
14130 cv-qualifiers from the FUNCTION_TYPE. */
14131 tree memptr;
14132 tree method_type
14133 = build_memfn_type (type, r, type_memfn_quals (type),
14134 type_memfn_rqual (type));
14135 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14136 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14137 complain);
14139 else
14140 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14141 cp_type_quals (t),
14142 complain);
14144 case FUNCTION_TYPE:
14145 case METHOD_TYPE:
14147 tree fntype;
14148 tree specs;
14149 fntype = tsubst_function_type (t, args, complain, in_decl);
14150 if (fntype == error_mark_node)
14151 return error_mark_node;
14153 /* Substitute the exception specification. */
14154 specs = tsubst_exception_specification (t, args, complain, in_decl,
14155 /*defer_ok*/fndecl_type);
14156 if (specs == error_mark_node)
14157 return error_mark_node;
14158 if (specs)
14159 fntype = build_exception_variant (fntype, specs);
14160 return fntype;
14162 case ARRAY_TYPE:
14164 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14165 if (domain == error_mark_node)
14166 return error_mark_node;
14168 /* As an optimization, we avoid regenerating the array type if
14169 it will obviously be the same as T. */
14170 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14171 return t;
14173 /* These checks should match the ones in create_array_type_for_decl.
14175 [temp.deduct]
14177 The deduction may fail for any of the following reasons:
14179 -- Attempting to create an array with an element type that
14180 is void, a function type, or a reference type, or [DR337]
14181 an abstract class type. */
14182 if (VOID_TYPE_P (type)
14183 || TREE_CODE (type) == FUNCTION_TYPE
14184 || (TREE_CODE (type) == ARRAY_TYPE
14185 && TYPE_DOMAIN (type) == NULL_TREE)
14186 || TREE_CODE (type) == REFERENCE_TYPE)
14188 if (complain & tf_error)
14189 error ("creating array of %qT", type);
14190 return error_mark_node;
14193 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14194 return error_mark_node;
14196 r = build_cplus_array_type (type, domain);
14198 if (TYPE_USER_ALIGN (t))
14200 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14201 TYPE_USER_ALIGN (r) = 1;
14204 return r;
14207 case TYPENAME_TYPE:
14209 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14210 in_decl, /*entering_scope=*/1);
14211 if (ctx == error_mark_node)
14212 return error_mark_node;
14214 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14215 complain, in_decl);
14216 if (f == error_mark_node)
14217 return error_mark_node;
14219 if (!MAYBE_CLASS_TYPE_P (ctx))
14221 if (complain & tf_error)
14222 error ("%qT is not a class, struct, or union type", ctx);
14223 return error_mark_node;
14225 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14227 /* Normally, make_typename_type does not require that the CTX
14228 have complete type in order to allow things like:
14230 template <class T> struct S { typename S<T>::X Y; };
14232 But, such constructs have already been resolved by this
14233 point, so here CTX really should have complete type, unless
14234 it's a partial instantiation. */
14235 ctx = complete_type (ctx);
14236 if (!COMPLETE_TYPE_P (ctx))
14238 if (complain & tf_error)
14239 cxx_incomplete_type_error (NULL_TREE, ctx);
14240 return error_mark_node;
14244 f = make_typename_type (ctx, f, typename_type,
14245 complain | tf_keep_type_decl);
14246 if (f == error_mark_node)
14247 return f;
14248 if (TREE_CODE (f) == TYPE_DECL)
14250 complain |= tf_ignore_bad_quals;
14251 f = TREE_TYPE (f);
14254 if (TREE_CODE (f) != TYPENAME_TYPE)
14256 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14258 if (complain & tf_error)
14259 error ("%qT resolves to %qT, which is not an enumeration type",
14260 t, f);
14261 else
14262 return error_mark_node;
14264 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14266 if (complain & tf_error)
14267 error ("%qT resolves to %qT, which is is not a class type",
14268 t, f);
14269 else
14270 return error_mark_node;
14274 return cp_build_qualified_type_real
14275 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14278 case UNBOUND_CLASS_TEMPLATE:
14280 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14281 in_decl, /*entering_scope=*/1);
14282 tree name = TYPE_IDENTIFIER (t);
14283 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14285 if (ctx == error_mark_node || name == error_mark_node)
14286 return error_mark_node;
14288 if (parm_list)
14289 parm_list = tsubst_template_parms (parm_list, args, complain);
14290 return make_unbound_class_template (ctx, name, parm_list, complain);
14293 case TYPEOF_TYPE:
14295 tree type;
14297 ++cp_unevaluated_operand;
14298 ++c_inhibit_evaluation_warnings;
14300 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14301 complain, in_decl,
14302 /*integral_constant_expression_p=*/false);
14304 --cp_unevaluated_operand;
14305 --c_inhibit_evaluation_warnings;
14307 type = finish_typeof (type);
14308 return cp_build_qualified_type_real (type,
14309 cp_type_quals (t)
14310 | cp_type_quals (type),
14311 complain);
14314 case DECLTYPE_TYPE:
14316 tree type;
14318 ++cp_unevaluated_operand;
14319 ++c_inhibit_evaluation_warnings;
14321 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14322 complain|tf_decltype, in_decl,
14323 /*function_p*/false,
14324 /*integral_constant_expression*/false);
14326 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14328 if (type == NULL_TREE)
14330 if (complain & tf_error)
14331 error ("empty initializer in lambda init-capture");
14332 type = error_mark_node;
14334 else if (TREE_CODE (type) == TREE_LIST)
14335 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14338 --cp_unevaluated_operand;
14339 --c_inhibit_evaluation_warnings;
14341 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14342 type = lambda_capture_field_type (type,
14343 DECLTYPE_FOR_INIT_CAPTURE (t),
14344 DECLTYPE_FOR_REF_CAPTURE (t));
14345 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14346 type = lambda_proxy_type (type);
14347 else
14349 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14350 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14351 && EXPR_P (type))
14352 /* In a template ~id could be either a complement expression
14353 or an unqualified-id naming a destructor; if instantiating
14354 it produces an expression, it's not an id-expression or
14355 member access. */
14356 id = false;
14357 type = finish_decltype_type (type, id, complain);
14359 return cp_build_qualified_type_real (type,
14360 cp_type_quals (t)
14361 | cp_type_quals (type),
14362 complain | tf_ignore_bad_quals);
14365 case UNDERLYING_TYPE:
14367 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14368 complain, in_decl);
14369 return finish_underlying_type (type);
14372 case TYPE_ARGUMENT_PACK:
14373 case NONTYPE_ARGUMENT_PACK:
14375 tree r;
14377 if (code == NONTYPE_ARGUMENT_PACK)
14378 r = make_node (code);
14379 else
14380 r = cxx_make_type (code);
14382 tree pack_args = ARGUMENT_PACK_ARGS (t);
14383 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14384 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14386 return r;
14389 case VOID_CST:
14390 case INTEGER_CST:
14391 case REAL_CST:
14392 case STRING_CST:
14393 case PLUS_EXPR:
14394 case MINUS_EXPR:
14395 case NEGATE_EXPR:
14396 case NOP_EXPR:
14397 case INDIRECT_REF:
14398 case ADDR_EXPR:
14399 case CALL_EXPR:
14400 case ARRAY_REF:
14401 case SCOPE_REF:
14402 /* We should use one of the expression tsubsts for these codes. */
14403 gcc_unreachable ();
14405 default:
14406 sorry ("use of %qs in template", get_tree_code_name (code));
14407 return error_mark_node;
14411 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14412 expression on the left-hand side of the "." or "->" operator. We
14413 only do the lookup if we had a dependent BASELINK. Otherwise we
14414 adjust it onto the instantiated heirarchy. */
14416 static tree
14417 tsubst_baselink (tree baselink, tree object_type,
14418 tree args, tsubst_flags_t complain, tree in_decl)
14420 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14421 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14422 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14424 tree optype = BASELINK_OPTYPE (baselink);
14425 optype = tsubst (optype, args, complain, in_decl);
14427 tree template_args = NULL_TREE;
14428 bool template_id_p = false;
14429 tree fns = BASELINK_FUNCTIONS (baselink);
14430 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14432 template_id_p = true;
14433 template_args = TREE_OPERAND (fns, 1);
14434 fns = TREE_OPERAND (fns, 0);
14435 if (template_args)
14436 template_args = tsubst_template_args (template_args, args,
14437 complain, in_decl);
14440 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14441 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14442 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14444 if (dependent_p)
14446 tree name = OVL_NAME (fns);
14447 if (IDENTIFIER_CONV_OP_P (name))
14448 name = make_conv_op_name (optype);
14450 if (name == complete_dtor_identifier)
14451 /* Treat as-if non-dependent below. */
14452 dependent_p = false;
14454 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14455 if (!baselink)
14457 if ((complain & tf_error)
14458 && constructor_name_p (name, qualifying_scope))
14459 error ("cannot call constructor %<%T::%D%> directly",
14460 qualifying_scope, name);
14461 return error_mark_node;
14464 if (BASELINK_P (baselink))
14465 fns = BASELINK_FUNCTIONS (baselink);
14467 else
14468 /* We're going to overwrite pieces below, make a duplicate. */
14469 baselink = copy_node (baselink);
14471 /* If lookup found a single function, mark it as used at this point.
14472 (If lookup found multiple functions the one selected later by
14473 overload resolution will be marked as used at that point.) */
14474 if (!template_id_p && !really_overloaded_fn (fns)
14475 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14476 return error_mark_node;
14478 if (BASELINK_P (baselink))
14480 /* Add back the template arguments, if present. */
14481 if (template_id_p)
14482 BASELINK_FUNCTIONS (baselink)
14483 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14485 /* Update the conversion operator type. */
14486 BASELINK_OPTYPE (baselink) = optype;
14489 if (!object_type)
14490 object_type = current_class_type;
14492 if (qualified_p || !dependent_p)
14494 baselink = adjust_result_of_qualified_name_lookup (baselink,
14495 qualifying_scope,
14496 object_type);
14497 if (!qualified_p)
14498 /* We need to call adjust_result_of_qualified_name_lookup in case the
14499 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14500 so that we still get virtual function binding. */
14501 BASELINK_QUALIFIED_P (baselink) = false;
14504 return baselink;
14507 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14508 true if the qualified-id will be a postfix-expression in-and-of
14509 itself; false if more of the postfix-expression follows the
14510 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14511 of "&". */
14513 static tree
14514 tsubst_qualified_id (tree qualified_id, tree args,
14515 tsubst_flags_t complain, tree in_decl,
14516 bool done, bool address_p)
14518 tree expr;
14519 tree scope;
14520 tree name;
14521 bool is_template;
14522 tree template_args;
14523 location_t loc = UNKNOWN_LOCATION;
14525 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14527 /* Figure out what name to look up. */
14528 name = TREE_OPERAND (qualified_id, 1);
14529 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14531 is_template = true;
14532 loc = EXPR_LOCATION (name);
14533 template_args = TREE_OPERAND (name, 1);
14534 if (template_args)
14535 template_args = tsubst_template_args (template_args, args,
14536 complain, in_decl);
14537 if (template_args == error_mark_node)
14538 return error_mark_node;
14539 name = TREE_OPERAND (name, 0);
14541 else
14543 is_template = false;
14544 template_args = NULL_TREE;
14547 /* Substitute into the qualifying scope. When there are no ARGS, we
14548 are just trying to simplify a non-dependent expression. In that
14549 case the qualifying scope may be dependent, and, in any case,
14550 substituting will not help. */
14551 scope = TREE_OPERAND (qualified_id, 0);
14552 if (args)
14554 scope = tsubst (scope, args, complain, in_decl);
14555 expr = tsubst_copy (name, args, complain, in_decl);
14557 else
14558 expr = name;
14560 if (dependent_scope_p (scope))
14562 if (is_template)
14563 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14564 tree r = build_qualified_name (NULL_TREE, scope, expr,
14565 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14566 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14567 return r;
14570 if (!BASELINK_P (name) && !DECL_P (expr))
14572 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14574 /* A BIT_NOT_EXPR is used to represent a destructor. */
14575 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14577 error ("qualifying type %qT does not match destructor name ~%qT",
14578 scope, TREE_OPERAND (expr, 0));
14579 expr = error_mark_node;
14581 else
14582 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14583 /*is_type_p=*/0, false);
14585 else
14586 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14587 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14588 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14590 if (complain & tf_error)
14592 error ("dependent-name %qE is parsed as a non-type, but "
14593 "instantiation yields a type", qualified_id);
14594 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14596 return error_mark_node;
14600 if (DECL_P (expr))
14602 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14603 scope);
14604 /* Remember that there was a reference to this entity. */
14605 if (!mark_used (expr, complain) && !(complain & tf_error))
14606 return error_mark_node;
14609 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14611 if (complain & tf_error)
14612 qualified_name_lookup_error (scope,
14613 TREE_OPERAND (qualified_id, 1),
14614 expr, input_location);
14615 return error_mark_node;
14618 if (is_template)
14620 if (variable_template_p (expr))
14621 expr = lookup_and_finish_template_variable (expr, template_args,
14622 complain);
14623 else
14624 expr = lookup_template_function (expr, template_args);
14627 if (expr == error_mark_node && complain & tf_error)
14628 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14629 expr, input_location);
14630 else if (TYPE_P (scope))
14632 expr = (adjust_result_of_qualified_name_lookup
14633 (expr, scope, current_nonlambda_class_type ()));
14634 expr = (finish_qualified_id_expr
14635 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14636 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14637 /*template_arg_p=*/false, complain));
14640 /* Expressions do not generally have reference type. */
14641 if (TREE_CODE (expr) != SCOPE_REF
14642 /* However, if we're about to form a pointer-to-member, we just
14643 want the referenced member referenced. */
14644 && TREE_CODE (expr) != OFFSET_REF)
14645 expr = convert_from_reference (expr);
14647 if (REF_PARENTHESIZED_P (qualified_id))
14648 expr = force_paren_expr (expr);
14650 return expr;
14653 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14654 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14655 for tsubst. */
14657 static tree
14658 tsubst_init (tree init, tree decl, tree args,
14659 tsubst_flags_t complain, tree in_decl)
14661 if (!init)
14662 return NULL_TREE;
14664 init = tsubst_expr (init, args, complain, in_decl, false);
14666 if (!init && TREE_TYPE (decl) != error_mark_node)
14668 /* If we had an initializer but it
14669 instantiated to nothing,
14670 value-initialize the object. This will
14671 only occur when the initializer was a
14672 pack expansion where the parameter packs
14673 used in that expansion were of length
14674 zero. */
14675 init = build_value_init (TREE_TYPE (decl),
14676 complain);
14677 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14678 init = get_target_expr_sfinae (init, complain);
14679 if (TREE_CODE (init) == TARGET_EXPR)
14680 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14683 return init;
14686 /* Like tsubst, but deals with expressions. This function just replaces
14687 template parms; to finish processing the resultant expression, use
14688 tsubst_copy_and_build or tsubst_expr. */
14690 static tree
14691 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14693 enum tree_code code;
14694 tree r;
14696 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14697 return t;
14699 code = TREE_CODE (t);
14701 switch (code)
14703 case PARM_DECL:
14704 r = retrieve_local_specialization (t);
14706 if (r == NULL_TREE)
14708 /* We get here for a use of 'this' in an NSDMI. */
14709 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14710 return current_class_ptr;
14712 /* This can happen for a parameter name used later in a function
14713 declaration (such as in a late-specified return type). Just
14714 make a dummy decl, since it's only used for its type. */
14715 gcc_assert (cp_unevaluated_operand != 0);
14716 r = tsubst_decl (t, args, complain);
14717 /* Give it the template pattern as its context; its true context
14718 hasn't been instantiated yet and this is good enough for
14719 mangling. */
14720 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14723 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14724 r = ARGUMENT_PACK_SELECT_ARG (r);
14725 if (!mark_used (r, complain) && !(complain & tf_error))
14726 return error_mark_node;
14727 return r;
14729 case CONST_DECL:
14731 tree enum_type;
14732 tree v;
14734 if (DECL_TEMPLATE_PARM_P (t))
14735 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14736 /* There is no need to substitute into namespace-scope
14737 enumerators. */
14738 if (DECL_NAMESPACE_SCOPE_P (t))
14739 return t;
14740 /* If ARGS is NULL, then T is known to be non-dependent. */
14741 if (args == NULL_TREE)
14742 return scalar_constant_value (t);
14744 /* Unfortunately, we cannot just call lookup_name here.
14745 Consider:
14747 template <int I> int f() {
14748 enum E { a = I };
14749 struct S { void g() { E e = a; } };
14752 When we instantiate f<7>::S::g(), say, lookup_name is not
14753 clever enough to find f<7>::a. */
14754 enum_type
14755 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14756 /*entering_scope=*/0);
14758 for (v = TYPE_VALUES (enum_type);
14759 v != NULL_TREE;
14760 v = TREE_CHAIN (v))
14761 if (TREE_PURPOSE (v) == DECL_NAME (t))
14762 return TREE_VALUE (v);
14764 /* We didn't find the name. That should never happen; if
14765 name-lookup found it during preliminary parsing, we
14766 should find it again here during instantiation. */
14767 gcc_unreachable ();
14769 return t;
14771 case FIELD_DECL:
14772 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14774 /* Check for a local specialization set up by
14775 tsubst_pack_expansion. */
14776 if (tree r = retrieve_local_specialization (t))
14778 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14779 r = ARGUMENT_PACK_SELECT_ARG (r);
14780 return r;
14783 /* When retrieving a capture pack from a generic lambda, remove the
14784 lambda call op's own template argument list from ARGS. Only the
14785 template arguments active for the closure type should be used to
14786 retrieve the pack specialization. */
14787 if (LAMBDA_FUNCTION_P (current_function_decl)
14788 && (template_class_depth (DECL_CONTEXT (t))
14789 != TMPL_ARGS_DEPTH (args)))
14790 args = strip_innermost_template_args (args, 1);
14792 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14793 tsubst_decl put in the hash table. */
14794 return retrieve_specialization (t, args, 0);
14797 if (DECL_CONTEXT (t))
14799 tree ctx;
14801 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14802 /*entering_scope=*/1);
14803 if (ctx != DECL_CONTEXT (t))
14805 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14806 if (!r)
14808 if (complain & tf_error)
14809 error ("using invalid field %qD", t);
14810 return error_mark_node;
14812 return r;
14816 return t;
14818 case VAR_DECL:
14819 case FUNCTION_DECL:
14820 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14821 r = tsubst (t, args, complain, in_decl);
14822 else if (local_variable_p (t)
14823 && uses_template_parms (DECL_CONTEXT (t)))
14825 r = retrieve_local_specialization (t);
14826 if (r == NULL_TREE)
14828 /* First try name lookup to find the instantiation. */
14829 r = lookup_name (DECL_NAME (t));
14830 if (r && !is_capture_proxy (r))
14832 /* Make sure that the one we found is the one we want. */
14833 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14834 if (ctx != DECL_CONTEXT (r))
14835 r = NULL_TREE;
14838 if (r)
14839 /* OK */;
14840 else
14842 /* This can happen for a variable used in a
14843 late-specified return type of a local lambda, or for a
14844 local static or constant. Building a new VAR_DECL
14845 should be OK in all those cases. */
14846 r = tsubst_decl (t, args, complain);
14847 if (local_specializations)
14848 /* Avoid infinite recursion (79640). */
14849 register_local_specialization (r, t);
14850 if (decl_maybe_constant_var_p (r))
14852 /* We can't call cp_finish_decl, so handle the
14853 initializer by hand. */
14854 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14855 complain, in_decl);
14856 if (!processing_template_decl)
14857 init = maybe_constant_init (init);
14858 if (processing_template_decl
14859 ? potential_constant_expression (init)
14860 : reduced_constant_expression_p (init))
14861 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14862 = TREE_CONSTANT (r) = true;
14863 DECL_INITIAL (r) = init;
14864 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14865 TREE_TYPE (r)
14866 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14867 complain, adc_variable_type);
14869 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14870 || decl_constant_var_p (r)
14871 || errorcount || sorrycount);
14872 if (!processing_template_decl
14873 && !TREE_STATIC (r))
14874 r = process_outer_var_ref (r, complain);
14876 /* Remember this for subsequent uses. */
14877 if (local_specializations)
14878 register_local_specialization (r, t);
14881 else
14882 r = t;
14883 if (!mark_used (r, complain))
14884 return error_mark_node;
14885 return r;
14887 case NAMESPACE_DECL:
14888 return t;
14890 case OVERLOAD:
14891 /* An OVERLOAD will always be a non-dependent overload set; an
14892 overload set from function scope will just be represented with an
14893 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14894 gcc_assert (!uses_template_parms (t));
14895 /* We must have marked any lookups as persistent. */
14896 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14897 return t;
14899 case BASELINK:
14900 return tsubst_baselink (t, current_nonlambda_class_type (),
14901 args, complain, in_decl);
14903 case TEMPLATE_DECL:
14904 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14905 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14906 args, complain, in_decl);
14907 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14908 return tsubst (t, args, complain, in_decl);
14909 else if (DECL_CLASS_SCOPE_P (t)
14910 && uses_template_parms (DECL_CONTEXT (t)))
14912 /* Template template argument like the following example need
14913 special treatment:
14915 template <template <class> class TT> struct C {};
14916 template <class T> struct D {
14917 template <class U> struct E {};
14918 C<E> c; // #1
14920 D<int> d; // #2
14922 We are processing the template argument `E' in #1 for
14923 the template instantiation #2. Originally, `E' is a
14924 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14925 have to substitute this with one having context `D<int>'. */
14927 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14928 if (dependent_scope_p (context))
14930 /* When rewriting a constructor into a deduction guide, a
14931 non-dependent name can become dependent, so memtmpl<args>
14932 becomes context::template memtmpl<args>. */
14933 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14934 return build_qualified_name (type, context, DECL_NAME (t),
14935 /*template*/true);
14937 return lookup_field (context, DECL_NAME(t), 0, false);
14939 else
14940 /* Ordinary template template argument. */
14941 return t;
14943 case NON_LVALUE_EXPR:
14944 case VIEW_CONVERT_EXPR:
14946 /* Handle location wrappers by substituting the wrapped node
14947 first, *then* reusing the resulting type. Doing the type
14948 first ensures that we handle template parameters and
14949 parameter pack expansions. */
14950 gcc_assert (location_wrapper_p (t));
14951 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14952 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
14955 case CAST_EXPR:
14956 case REINTERPRET_CAST_EXPR:
14957 case CONST_CAST_EXPR:
14958 case STATIC_CAST_EXPR:
14959 case DYNAMIC_CAST_EXPR:
14960 case IMPLICIT_CONV_EXPR:
14961 case CONVERT_EXPR:
14962 case NOP_EXPR:
14964 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14965 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14966 return build1 (code, type, op0);
14969 case SIZEOF_EXPR:
14970 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14971 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14973 tree expanded, op = TREE_OPERAND (t, 0);
14974 int len = 0;
14976 if (SIZEOF_EXPR_TYPE_P (t))
14977 op = TREE_TYPE (op);
14979 ++cp_unevaluated_operand;
14980 ++c_inhibit_evaluation_warnings;
14981 /* We only want to compute the number of arguments. */
14982 if (PACK_EXPANSION_P (op))
14983 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14984 else
14985 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14986 args, complain, in_decl);
14987 --cp_unevaluated_operand;
14988 --c_inhibit_evaluation_warnings;
14990 if (TREE_CODE (expanded) == TREE_VEC)
14992 len = TREE_VEC_LENGTH (expanded);
14993 /* Set TREE_USED for the benefit of -Wunused. */
14994 for (int i = 0; i < len; i++)
14995 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14996 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14999 if (expanded == error_mark_node)
15000 return error_mark_node;
15001 else if (PACK_EXPANSION_P (expanded)
15002 || (TREE_CODE (expanded) == TREE_VEC
15003 && pack_expansion_args_count (expanded)))
15006 if (PACK_EXPANSION_P (expanded))
15007 /* OK. */;
15008 else if (TREE_VEC_LENGTH (expanded) == 1)
15009 expanded = TREE_VEC_ELT (expanded, 0);
15010 else
15011 expanded = make_argument_pack (expanded);
15013 if (TYPE_P (expanded))
15014 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15015 complain & tf_error);
15016 else
15017 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15018 complain & tf_error);
15020 else
15021 return build_int_cst (size_type_node, len);
15023 if (SIZEOF_EXPR_TYPE_P (t))
15025 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15026 args, complain, in_decl);
15027 r = build1 (NOP_EXPR, r, error_mark_node);
15028 r = build1 (SIZEOF_EXPR,
15029 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15030 SIZEOF_EXPR_TYPE_P (r) = 1;
15031 return r;
15033 /* Fall through */
15035 case INDIRECT_REF:
15036 case NEGATE_EXPR:
15037 case TRUTH_NOT_EXPR:
15038 case BIT_NOT_EXPR:
15039 case ADDR_EXPR:
15040 case UNARY_PLUS_EXPR: /* Unary + */
15041 case ALIGNOF_EXPR:
15042 case AT_ENCODE_EXPR:
15043 case ARROW_EXPR:
15044 case THROW_EXPR:
15045 case TYPEID_EXPR:
15046 case REALPART_EXPR:
15047 case IMAGPART_EXPR:
15048 case PAREN_EXPR:
15050 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15051 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15052 return build1 (code, type, op0);
15055 case COMPONENT_REF:
15057 tree object;
15058 tree name;
15060 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15061 name = TREE_OPERAND (t, 1);
15062 if (TREE_CODE (name) == BIT_NOT_EXPR)
15064 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15065 complain, in_decl);
15066 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15068 else if (TREE_CODE (name) == SCOPE_REF
15069 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15071 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15072 complain, in_decl);
15073 name = TREE_OPERAND (name, 1);
15074 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15075 complain, in_decl);
15076 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15077 name = build_qualified_name (/*type=*/NULL_TREE,
15078 base, name,
15079 /*template_p=*/false);
15081 else if (BASELINK_P (name))
15082 name = tsubst_baselink (name,
15083 non_reference (TREE_TYPE (object)),
15084 args, complain,
15085 in_decl);
15086 else
15087 name = tsubst_copy (name, args, complain, in_decl);
15088 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15091 case PLUS_EXPR:
15092 case MINUS_EXPR:
15093 case MULT_EXPR:
15094 case TRUNC_DIV_EXPR:
15095 case CEIL_DIV_EXPR:
15096 case FLOOR_DIV_EXPR:
15097 case ROUND_DIV_EXPR:
15098 case EXACT_DIV_EXPR:
15099 case BIT_AND_EXPR:
15100 case BIT_IOR_EXPR:
15101 case BIT_XOR_EXPR:
15102 case TRUNC_MOD_EXPR:
15103 case FLOOR_MOD_EXPR:
15104 case TRUTH_ANDIF_EXPR:
15105 case TRUTH_ORIF_EXPR:
15106 case TRUTH_AND_EXPR:
15107 case TRUTH_OR_EXPR:
15108 case RSHIFT_EXPR:
15109 case LSHIFT_EXPR:
15110 case RROTATE_EXPR:
15111 case LROTATE_EXPR:
15112 case EQ_EXPR:
15113 case NE_EXPR:
15114 case MAX_EXPR:
15115 case MIN_EXPR:
15116 case LE_EXPR:
15117 case GE_EXPR:
15118 case LT_EXPR:
15119 case GT_EXPR:
15120 case COMPOUND_EXPR:
15121 case DOTSTAR_EXPR:
15122 case MEMBER_REF:
15123 case PREDECREMENT_EXPR:
15124 case PREINCREMENT_EXPR:
15125 case POSTDECREMENT_EXPR:
15126 case POSTINCREMENT_EXPR:
15128 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15129 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15130 return build_nt (code, op0, op1);
15133 case SCOPE_REF:
15135 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15136 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15137 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15138 QUALIFIED_NAME_IS_TEMPLATE (t));
15141 case ARRAY_REF:
15143 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15144 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15145 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15148 case CALL_EXPR:
15150 int n = VL_EXP_OPERAND_LENGTH (t);
15151 tree result = build_vl_exp (CALL_EXPR, n);
15152 int i;
15153 for (i = 0; i < n; i++)
15154 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15155 complain, in_decl);
15156 return result;
15159 case COND_EXPR:
15160 case MODOP_EXPR:
15161 case PSEUDO_DTOR_EXPR:
15162 case VEC_PERM_EXPR:
15164 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15165 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15166 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15167 r = build_nt (code, op0, op1, op2);
15168 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15169 return r;
15172 case NEW_EXPR:
15174 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15175 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15176 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15177 r = build_nt (code, op0, op1, op2);
15178 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15179 return r;
15182 case DELETE_EXPR:
15184 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15185 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15186 r = build_nt (code, op0, op1);
15187 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15188 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15189 return r;
15192 case TEMPLATE_ID_EXPR:
15194 /* Substituted template arguments */
15195 tree fn = TREE_OPERAND (t, 0);
15196 tree targs = TREE_OPERAND (t, 1);
15198 fn = tsubst_copy (fn, args, complain, in_decl);
15199 if (targs)
15200 targs = tsubst_template_args (targs, args, complain, in_decl);
15202 return lookup_template_function (fn, targs);
15205 case TREE_LIST:
15207 tree purpose, value, chain;
15209 if (t == void_list_node)
15210 return t;
15212 purpose = TREE_PURPOSE (t);
15213 if (purpose)
15214 purpose = tsubst_copy (purpose, args, complain, in_decl);
15215 value = TREE_VALUE (t);
15216 if (value)
15217 value = tsubst_copy (value, args, complain, in_decl);
15218 chain = TREE_CHAIN (t);
15219 if (chain && chain != void_type_node)
15220 chain = tsubst_copy (chain, args, complain, in_decl);
15221 if (purpose == TREE_PURPOSE (t)
15222 && value == TREE_VALUE (t)
15223 && chain == TREE_CHAIN (t))
15224 return t;
15225 return tree_cons (purpose, value, chain);
15228 case RECORD_TYPE:
15229 case UNION_TYPE:
15230 case ENUMERAL_TYPE:
15231 case INTEGER_TYPE:
15232 case TEMPLATE_TYPE_PARM:
15233 case TEMPLATE_TEMPLATE_PARM:
15234 case BOUND_TEMPLATE_TEMPLATE_PARM:
15235 case TEMPLATE_PARM_INDEX:
15236 case POINTER_TYPE:
15237 case REFERENCE_TYPE:
15238 case OFFSET_TYPE:
15239 case FUNCTION_TYPE:
15240 case METHOD_TYPE:
15241 case ARRAY_TYPE:
15242 case TYPENAME_TYPE:
15243 case UNBOUND_CLASS_TEMPLATE:
15244 case TYPEOF_TYPE:
15245 case DECLTYPE_TYPE:
15246 case TYPE_DECL:
15247 return tsubst (t, args, complain, in_decl);
15249 case USING_DECL:
15250 t = DECL_NAME (t);
15251 /* Fall through. */
15252 case IDENTIFIER_NODE:
15253 if (IDENTIFIER_CONV_OP_P (t))
15255 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15256 return make_conv_op_name (new_type);
15258 else
15259 return t;
15261 case CONSTRUCTOR:
15262 /* This is handled by tsubst_copy_and_build. */
15263 gcc_unreachable ();
15265 case VA_ARG_EXPR:
15267 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15268 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15269 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15272 case CLEANUP_POINT_EXPR:
15273 /* We shouldn't have built any of these during initial template
15274 generation. Instead, they should be built during instantiation
15275 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15276 gcc_unreachable ();
15278 case OFFSET_REF:
15280 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15281 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15282 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15283 r = build2 (code, type, op0, op1);
15284 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15285 if (!mark_used (TREE_OPERAND (r, 1), complain)
15286 && !(complain & tf_error))
15287 return error_mark_node;
15288 return r;
15291 case EXPR_PACK_EXPANSION:
15292 error ("invalid use of pack expansion expression");
15293 return error_mark_node;
15295 case NONTYPE_ARGUMENT_PACK:
15296 error ("use %<...%> to expand argument pack");
15297 return error_mark_node;
15299 case VOID_CST:
15300 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15301 return t;
15303 case INTEGER_CST:
15304 case REAL_CST:
15305 case STRING_CST:
15306 case COMPLEX_CST:
15308 /* Instantiate any typedefs in the type. */
15309 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15310 r = fold_convert (type, t);
15311 gcc_assert (TREE_CODE (r) == code);
15312 return r;
15315 case PTRMEM_CST:
15316 /* These can sometimes show up in a partial instantiation, but never
15317 involve template parms. */
15318 gcc_assert (!uses_template_parms (t));
15319 return t;
15321 case UNARY_LEFT_FOLD_EXPR:
15322 return tsubst_unary_left_fold (t, args, complain, in_decl);
15323 case UNARY_RIGHT_FOLD_EXPR:
15324 return tsubst_unary_right_fold (t, args, complain, in_decl);
15325 case BINARY_LEFT_FOLD_EXPR:
15326 return tsubst_binary_left_fold (t, args, complain, in_decl);
15327 case BINARY_RIGHT_FOLD_EXPR:
15328 return tsubst_binary_right_fold (t, args, complain, in_decl);
15329 case PREDICT_EXPR:
15330 return t;
15332 case DEBUG_BEGIN_STMT:
15333 /* ??? There's no point in copying it for now, but maybe some
15334 day it will contain more information, such as a pointer back
15335 to the containing function, inlined copy or so. */
15336 return t;
15338 default:
15339 /* We shouldn't get here, but keep going if !flag_checking. */
15340 if (flag_checking)
15341 gcc_unreachable ();
15342 return t;
15346 /* Helper function for tsubst_omp_clauses, used for instantiation of
15347 OMP_CLAUSE_DECL of clauses. */
15349 static tree
15350 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15351 tree in_decl)
15353 if (decl == NULL_TREE)
15354 return NULL_TREE;
15356 /* Handle an OpenMP array section represented as a TREE_LIST (or
15357 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15358 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15359 TREE_LIST. We can handle it exactly the same as an array section
15360 (purpose, value, and a chain), even though the nomenclature
15361 (low_bound, length, etc) is different. */
15362 if (TREE_CODE (decl) == TREE_LIST)
15364 tree low_bound
15365 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15366 /*integral_constant_expression_p=*/false);
15367 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15368 /*integral_constant_expression_p=*/false);
15369 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15370 in_decl);
15371 if (TREE_PURPOSE (decl) == low_bound
15372 && TREE_VALUE (decl) == length
15373 && TREE_CHAIN (decl) == chain)
15374 return decl;
15375 tree ret = tree_cons (low_bound, length, chain);
15376 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15377 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15378 return ret;
15380 tree ret = tsubst_expr (decl, args, complain, in_decl,
15381 /*integral_constant_expression_p=*/false);
15382 /* Undo convert_from_reference tsubst_expr could have called. */
15383 if (decl
15384 && REFERENCE_REF_P (ret)
15385 && !REFERENCE_REF_P (decl))
15386 ret = TREE_OPERAND (ret, 0);
15387 return ret;
15390 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15392 static tree
15393 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15394 tree args, tsubst_flags_t complain, tree in_decl)
15396 tree new_clauses = NULL_TREE, nc, oc;
15397 tree linear_no_step = NULL_TREE;
15399 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15401 nc = copy_node (oc);
15402 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15403 new_clauses = nc;
15405 switch (OMP_CLAUSE_CODE (nc))
15407 case OMP_CLAUSE_LASTPRIVATE:
15408 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15410 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15411 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15412 in_decl, /*integral_constant_expression_p=*/false);
15413 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15414 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15416 /* FALLTHRU */
15417 case OMP_CLAUSE_PRIVATE:
15418 case OMP_CLAUSE_SHARED:
15419 case OMP_CLAUSE_FIRSTPRIVATE:
15420 case OMP_CLAUSE_COPYIN:
15421 case OMP_CLAUSE_COPYPRIVATE:
15422 case OMP_CLAUSE_UNIFORM:
15423 case OMP_CLAUSE_DEPEND:
15424 case OMP_CLAUSE_FROM:
15425 case OMP_CLAUSE_TO:
15426 case OMP_CLAUSE_MAP:
15427 case OMP_CLAUSE_USE_DEVICE_PTR:
15428 case OMP_CLAUSE_IS_DEVICE_PTR:
15429 OMP_CLAUSE_DECL (nc)
15430 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15431 in_decl);
15432 break;
15433 case OMP_CLAUSE_TILE:
15434 case OMP_CLAUSE_IF:
15435 case OMP_CLAUSE_NUM_THREADS:
15436 case OMP_CLAUSE_SCHEDULE:
15437 case OMP_CLAUSE_COLLAPSE:
15438 case OMP_CLAUSE_FINAL:
15439 case OMP_CLAUSE_DEVICE:
15440 case OMP_CLAUSE_DIST_SCHEDULE:
15441 case OMP_CLAUSE_NUM_TEAMS:
15442 case OMP_CLAUSE_THREAD_LIMIT:
15443 case OMP_CLAUSE_SAFELEN:
15444 case OMP_CLAUSE_SIMDLEN:
15445 case OMP_CLAUSE_NUM_TASKS:
15446 case OMP_CLAUSE_GRAINSIZE:
15447 case OMP_CLAUSE_PRIORITY:
15448 case OMP_CLAUSE_ORDERED:
15449 case OMP_CLAUSE_HINT:
15450 case OMP_CLAUSE_NUM_GANGS:
15451 case OMP_CLAUSE_NUM_WORKERS:
15452 case OMP_CLAUSE_VECTOR_LENGTH:
15453 case OMP_CLAUSE_WORKER:
15454 case OMP_CLAUSE_VECTOR:
15455 case OMP_CLAUSE_ASYNC:
15456 case OMP_CLAUSE_WAIT:
15457 OMP_CLAUSE_OPERAND (nc, 0)
15458 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15459 in_decl, /*integral_constant_expression_p=*/false);
15460 break;
15461 case OMP_CLAUSE_REDUCTION:
15462 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15464 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15465 if (TREE_CODE (placeholder) == SCOPE_REF)
15467 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15468 complain, in_decl);
15469 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15470 = build_qualified_name (NULL_TREE, scope,
15471 TREE_OPERAND (placeholder, 1),
15472 false);
15474 else
15475 gcc_assert (identifier_p (placeholder));
15477 OMP_CLAUSE_DECL (nc)
15478 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15479 in_decl);
15480 break;
15481 case OMP_CLAUSE_GANG:
15482 case OMP_CLAUSE_ALIGNED:
15483 OMP_CLAUSE_DECL (nc)
15484 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15485 in_decl);
15486 OMP_CLAUSE_OPERAND (nc, 1)
15487 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15488 in_decl, /*integral_constant_expression_p=*/false);
15489 break;
15490 case OMP_CLAUSE_LINEAR:
15491 OMP_CLAUSE_DECL (nc)
15492 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15493 in_decl);
15494 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15496 gcc_assert (!linear_no_step);
15497 linear_no_step = nc;
15499 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15500 OMP_CLAUSE_LINEAR_STEP (nc)
15501 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15502 complain, in_decl);
15503 else
15504 OMP_CLAUSE_LINEAR_STEP (nc)
15505 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15506 in_decl,
15507 /*integral_constant_expression_p=*/false);
15508 break;
15509 case OMP_CLAUSE_NOWAIT:
15510 case OMP_CLAUSE_DEFAULT:
15511 case OMP_CLAUSE_UNTIED:
15512 case OMP_CLAUSE_MERGEABLE:
15513 case OMP_CLAUSE_INBRANCH:
15514 case OMP_CLAUSE_NOTINBRANCH:
15515 case OMP_CLAUSE_PROC_BIND:
15516 case OMP_CLAUSE_FOR:
15517 case OMP_CLAUSE_PARALLEL:
15518 case OMP_CLAUSE_SECTIONS:
15519 case OMP_CLAUSE_TASKGROUP:
15520 case OMP_CLAUSE_NOGROUP:
15521 case OMP_CLAUSE_THREADS:
15522 case OMP_CLAUSE_SIMD:
15523 case OMP_CLAUSE_DEFAULTMAP:
15524 case OMP_CLAUSE_INDEPENDENT:
15525 case OMP_CLAUSE_AUTO:
15526 case OMP_CLAUSE_SEQ:
15527 break;
15528 default:
15529 gcc_unreachable ();
15531 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15532 switch (OMP_CLAUSE_CODE (nc))
15534 case OMP_CLAUSE_SHARED:
15535 case OMP_CLAUSE_PRIVATE:
15536 case OMP_CLAUSE_FIRSTPRIVATE:
15537 case OMP_CLAUSE_LASTPRIVATE:
15538 case OMP_CLAUSE_COPYPRIVATE:
15539 case OMP_CLAUSE_LINEAR:
15540 case OMP_CLAUSE_REDUCTION:
15541 case OMP_CLAUSE_USE_DEVICE_PTR:
15542 case OMP_CLAUSE_IS_DEVICE_PTR:
15543 /* tsubst_expr on SCOPE_REF results in returning
15544 finish_non_static_data_member result. Undo that here. */
15545 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15546 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15547 == IDENTIFIER_NODE))
15549 tree t = OMP_CLAUSE_DECL (nc);
15550 tree v = t;
15551 while (v)
15552 switch (TREE_CODE (v))
15554 case COMPONENT_REF:
15555 case MEM_REF:
15556 case INDIRECT_REF:
15557 CASE_CONVERT:
15558 case POINTER_PLUS_EXPR:
15559 v = TREE_OPERAND (v, 0);
15560 continue;
15561 case PARM_DECL:
15562 if (DECL_CONTEXT (v) == current_function_decl
15563 && DECL_ARTIFICIAL (v)
15564 && DECL_NAME (v) == this_identifier)
15565 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15566 /* FALLTHRU */
15567 default:
15568 v = NULL_TREE;
15569 break;
15572 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15573 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15574 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15575 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15576 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15578 tree decl = OMP_CLAUSE_DECL (nc);
15579 if (VAR_P (decl))
15581 retrofit_lang_decl (decl);
15582 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15585 break;
15586 default:
15587 break;
15591 new_clauses = nreverse (new_clauses);
15592 if (ort != C_ORT_OMP_DECLARE_SIMD)
15594 new_clauses = finish_omp_clauses (new_clauses, ort);
15595 if (linear_no_step)
15596 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15597 if (nc == linear_no_step)
15599 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15600 break;
15603 return new_clauses;
15606 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15608 static tree
15609 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15610 tree in_decl)
15612 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15614 tree purpose, value, chain;
15616 if (t == NULL)
15617 return t;
15619 if (TREE_CODE (t) != TREE_LIST)
15620 return tsubst_copy_and_build (t, args, complain, in_decl,
15621 /*function_p=*/false,
15622 /*integral_constant_expression_p=*/false);
15624 if (t == void_list_node)
15625 return t;
15627 purpose = TREE_PURPOSE (t);
15628 if (purpose)
15629 purpose = RECUR (purpose);
15630 value = TREE_VALUE (t);
15631 if (value)
15633 if (TREE_CODE (value) != LABEL_DECL)
15634 value = RECUR (value);
15635 else
15637 value = lookup_label (DECL_NAME (value));
15638 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15639 TREE_USED (value) = 1;
15642 chain = TREE_CHAIN (t);
15643 if (chain && chain != void_type_node)
15644 chain = RECUR (chain);
15645 return tree_cons (purpose, value, chain);
15646 #undef RECUR
15649 /* Used to temporarily communicate the list of #pragma omp parallel
15650 clauses to #pragma omp for instantiation if they are combined
15651 together. */
15653 static tree *omp_parallel_combined_clauses;
15655 /* Substitute one OMP_FOR iterator. */
15657 static void
15658 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15659 tree initv, tree condv, tree incrv, tree *clauses,
15660 tree args, tsubst_flags_t complain, tree in_decl,
15661 bool integral_constant_expression_p)
15663 #define RECUR(NODE) \
15664 tsubst_expr ((NODE), args, complain, in_decl, \
15665 integral_constant_expression_p)
15666 tree decl, init, cond, incr;
15668 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15669 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15671 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15673 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15674 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15677 decl = TREE_OPERAND (init, 0);
15678 init = TREE_OPERAND (init, 1);
15679 tree decl_expr = NULL_TREE;
15680 if (init && TREE_CODE (init) == DECL_EXPR)
15682 /* We need to jump through some hoops to handle declarations in the
15683 init-statement, since we might need to handle auto deduction,
15684 but we need to keep control of initialization. */
15685 decl_expr = init;
15686 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15687 decl = tsubst_decl (decl, args, complain);
15689 else
15691 if (TREE_CODE (decl) == SCOPE_REF)
15693 decl = RECUR (decl);
15694 if (TREE_CODE (decl) == COMPONENT_REF)
15696 tree v = decl;
15697 while (v)
15698 switch (TREE_CODE (v))
15700 case COMPONENT_REF:
15701 case MEM_REF:
15702 case INDIRECT_REF:
15703 CASE_CONVERT:
15704 case POINTER_PLUS_EXPR:
15705 v = TREE_OPERAND (v, 0);
15706 continue;
15707 case PARM_DECL:
15708 if (DECL_CONTEXT (v) == current_function_decl
15709 && DECL_ARTIFICIAL (v)
15710 && DECL_NAME (v) == this_identifier)
15712 decl = TREE_OPERAND (decl, 1);
15713 decl = omp_privatize_field (decl, false);
15715 /* FALLTHRU */
15716 default:
15717 v = NULL_TREE;
15718 break;
15722 else
15723 decl = RECUR (decl);
15725 init = RECUR (init);
15727 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15728 if (auto_node && init)
15729 TREE_TYPE (decl)
15730 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15732 gcc_assert (!type_dependent_expression_p (decl));
15734 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15736 if (decl_expr)
15738 /* Declare the variable, but don't let that initialize it. */
15739 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15740 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15741 RECUR (decl_expr);
15742 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15745 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15746 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15747 if (TREE_CODE (incr) == MODIFY_EXPR)
15749 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15750 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15751 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15752 NOP_EXPR, rhs, complain);
15754 else
15755 incr = RECUR (incr);
15756 TREE_VEC_ELT (declv, i) = decl;
15757 TREE_VEC_ELT (initv, i) = init;
15758 TREE_VEC_ELT (condv, i) = cond;
15759 TREE_VEC_ELT (incrv, i) = incr;
15760 return;
15763 if (decl_expr)
15765 /* Declare and initialize the variable. */
15766 RECUR (decl_expr);
15767 init = NULL_TREE;
15769 else if (init)
15771 tree *pc;
15772 int j;
15773 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15775 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15777 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15778 && OMP_CLAUSE_DECL (*pc) == decl)
15779 break;
15780 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15781 && OMP_CLAUSE_DECL (*pc) == decl)
15783 if (j)
15784 break;
15785 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15786 tree c = *pc;
15787 *pc = OMP_CLAUSE_CHAIN (c);
15788 OMP_CLAUSE_CHAIN (c) = *clauses;
15789 *clauses = c;
15791 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15792 && OMP_CLAUSE_DECL (*pc) == decl)
15794 error ("iteration variable %qD should not be firstprivate",
15795 decl);
15796 *pc = OMP_CLAUSE_CHAIN (*pc);
15798 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15799 && OMP_CLAUSE_DECL (*pc) == decl)
15801 error ("iteration variable %qD should not be reduction",
15802 decl);
15803 *pc = OMP_CLAUSE_CHAIN (*pc);
15805 else
15806 pc = &OMP_CLAUSE_CHAIN (*pc);
15808 if (*pc)
15809 break;
15811 if (*pc == NULL_TREE)
15813 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15814 OMP_CLAUSE_DECL (c) = decl;
15815 c = finish_omp_clauses (c, C_ORT_OMP);
15816 if (c)
15818 OMP_CLAUSE_CHAIN (c) = *clauses;
15819 *clauses = c;
15823 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15824 if (COMPARISON_CLASS_P (cond))
15826 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15827 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15828 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15830 else
15831 cond = RECUR (cond);
15832 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15833 switch (TREE_CODE (incr))
15835 case PREINCREMENT_EXPR:
15836 case PREDECREMENT_EXPR:
15837 case POSTINCREMENT_EXPR:
15838 case POSTDECREMENT_EXPR:
15839 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15840 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15841 break;
15842 case MODIFY_EXPR:
15843 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15844 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15846 tree rhs = TREE_OPERAND (incr, 1);
15847 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15848 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15849 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15850 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15851 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15852 rhs0, rhs1));
15854 else
15855 incr = RECUR (incr);
15856 break;
15857 case MODOP_EXPR:
15858 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15859 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15861 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15862 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15863 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15864 TREE_TYPE (decl), lhs,
15865 RECUR (TREE_OPERAND (incr, 2))));
15867 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15868 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15869 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15871 tree rhs = TREE_OPERAND (incr, 2);
15872 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15873 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15874 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15875 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15876 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15877 rhs0, rhs1));
15879 else
15880 incr = RECUR (incr);
15881 break;
15882 default:
15883 incr = RECUR (incr);
15884 break;
15887 TREE_VEC_ELT (declv, i) = decl;
15888 TREE_VEC_ELT (initv, i) = init;
15889 TREE_VEC_ELT (condv, i) = cond;
15890 TREE_VEC_ELT (incrv, i) = incr;
15891 #undef RECUR
15894 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15895 of OMP_TARGET's body. */
15897 static tree
15898 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15900 *walk_subtrees = 0;
15901 switch (TREE_CODE (*tp))
15903 case OMP_TEAMS:
15904 return *tp;
15905 case BIND_EXPR:
15906 case STATEMENT_LIST:
15907 *walk_subtrees = 1;
15908 break;
15909 default:
15910 break;
15912 return NULL_TREE;
15915 /* Helper function for tsubst_expr. For decomposition declaration
15916 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15917 also the corresponding decls representing the identifiers
15918 of the decomposition declaration. Return DECL if successful
15919 or error_mark_node otherwise, set *FIRST to the first decl
15920 in the list chained through DECL_CHAIN and *CNT to the number
15921 of such decls. */
15923 static tree
15924 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15925 tsubst_flags_t complain, tree in_decl, tree *first,
15926 unsigned int *cnt)
15928 tree decl2, decl3, prev = decl;
15929 *cnt = 0;
15930 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15931 for (decl2 = DECL_CHAIN (pattern_decl);
15932 decl2
15933 && VAR_P (decl2)
15934 && DECL_DECOMPOSITION_P (decl2)
15935 && DECL_NAME (decl2);
15936 decl2 = DECL_CHAIN (decl2))
15938 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15940 gcc_assert (errorcount);
15941 return error_mark_node;
15943 (*cnt)++;
15944 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15945 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15946 tree v = DECL_VALUE_EXPR (decl2);
15947 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15948 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15949 decl3 = tsubst (decl2, args, complain, in_decl);
15950 SET_DECL_VALUE_EXPR (decl2, v);
15951 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15952 if (VAR_P (decl3))
15953 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15954 maybe_push_decl (decl3);
15955 if (error_operand_p (decl3))
15956 decl = error_mark_node;
15957 else if (decl != error_mark_node
15958 && DECL_CHAIN (decl3) != prev)
15960 gcc_assert (errorcount);
15961 decl = error_mark_node;
15963 else
15964 prev = decl3;
15966 *first = prev;
15967 return decl;
15970 /* Like tsubst_copy for expressions, etc. but also does semantic
15971 processing. */
15973 tree
15974 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15975 bool integral_constant_expression_p)
15977 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15978 #define RECUR(NODE) \
15979 tsubst_expr ((NODE), args, complain, in_decl, \
15980 integral_constant_expression_p)
15982 tree stmt, tmp;
15983 tree r;
15984 location_t loc;
15986 if (t == NULL_TREE || t == error_mark_node)
15987 return t;
15989 loc = input_location;
15990 if (EXPR_HAS_LOCATION (t))
15991 input_location = EXPR_LOCATION (t);
15992 if (STATEMENT_CODE_P (TREE_CODE (t)))
15993 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15995 switch (TREE_CODE (t))
15997 case STATEMENT_LIST:
15999 tree_stmt_iterator i;
16000 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16001 RECUR (tsi_stmt (i));
16002 break;
16005 case CTOR_INITIALIZER:
16006 finish_mem_initializers (tsubst_initializer_list
16007 (TREE_OPERAND (t, 0), args));
16008 break;
16010 case RETURN_EXPR:
16011 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16012 break;
16014 case EXPR_STMT:
16015 tmp = RECUR (EXPR_STMT_EXPR (t));
16016 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16017 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16018 else
16019 finish_expr_stmt (tmp);
16020 break;
16022 case USING_STMT:
16023 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16024 /*attribs=*/NULL_TREE);
16025 break;
16027 case DECL_EXPR:
16029 tree decl, pattern_decl;
16030 tree init;
16032 pattern_decl = decl = DECL_EXPR_DECL (t);
16033 if (TREE_CODE (decl) == LABEL_DECL)
16034 finish_label_decl (DECL_NAME (decl));
16035 else if (TREE_CODE (decl) == USING_DECL)
16037 tree scope = USING_DECL_SCOPE (decl);
16038 tree name = DECL_NAME (decl);
16040 scope = tsubst (scope, args, complain, in_decl);
16041 decl = lookup_qualified_name (scope, name,
16042 /*is_type_p=*/false,
16043 /*complain=*/false);
16044 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16045 qualified_name_lookup_error (scope, name, decl, input_location);
16046 else
16047 finish_local_using_decl (decl, scope, name);
16049 else if (DECL_PACK_P (decl))
16051 /* Don't build up decls for a variadic capture proxy, we'll
16052 instantiate the elements directly as needed. */
16053 break;
16055 else if (is_capture_proxy (decl)
16056 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16058 /* We're in tsubst_lambda_expr, we've already inserted a new
16059 capture proxy, so look it up and register it. */
16060 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16061 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16062 gcc_assert (inst != decl && is_capture_proxy (inst));
16063 register_local_specialization (inst, decl);
16064 break;
16066 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16067 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16068 /* Don't copy the old closure; we'll create a new one in
16069 tsubst_lambda_expr. */
16070 break;
16071 else
16073 init = DECL_INITIAL (decl);
16074 decl = tsubst (decl, args, complain, in_decl);
16075 if (decl != error_mark_node)
16077 /* By marking the declaration as instantiated, we avoid
16078 trying to instantiate it. Since instantiate_decl can't
16079 handle local variables, and since we've already done
16080 all that needs to be done, that's the right thing to
16081 do. */
16082 if (VAR_P (decl))
16083 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16084 if (VAR_P (decl)
16085 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16086 /* Anonymous aggregates are a special case. */
16087 finish_anon_union (decl);
16088 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16090 DECL_CONTEXT (decl) = current_function_decl;
16091 if (DECL_NAME (decl) == this_identifier)
16093 tree lam = DECL_CONTEXT (current_function_decl);
16094 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16095 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16097 insert_capture_proxy (decl);
16099 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16100 /* We already did a pushtag. */;
16101 else if (TREE_CODE (decl) == FUNCTION_DECL
16102 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16103 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16105 DECL_CONTEXT (decl) = NULL_TREE;
16106 pushdecl (decl);
16107 DECL_CONTEXT (decl) = current_function_decl;
16108 cp_check_omp_declare_reduction (decl);
16110 else
16112 int const_init = false;
16113 maybe_push_decl (decl);
16114 if (VAR_P (decl)
16115 && DECL_PRETTY_FUNCTION_P (decl))
16117 /* For __PRETTY_FUNCTION__ we have to adjust the
16118 initializer. */
16119 const char *const name
16120 = cxx_printable_name (current_function_decl, 2);
16121 init = cp_fname_init (name, &TREE_TYPE (decl));
16123 else
16124 init = tsubst_init (init, decl, args, complain, in_decl);
16126 if (VAR_P (decl))
16127 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16128 (pattern_decl));
16129 if (VAR_P (decl)
16130 && DECL_DECOMPOSITION_P (decl)
16131 && TREE_TYPE (pattern_decl) != error_mark_node)
16133 unsigned int cnt;
16134 tree first;
16135 tree ndecl
16136 = tsubst_decomp_names (decl, pattern_decl, args,
16137 complain, in_decl, &first, &cnt);
16138 if (ndecl != error_mark_node)
16139 cp_maybe_mangle_decomp (ndecl, first, cnt);
16140 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16141 if (ndecl != error_mark_node)
16142 cp_finish_decomp (ndecl, first, cnt);
16144 else
16145 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16150 break;
16153 case FOR_STMT:
16154 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16155 RECUR (FOR_INIT_STMT (t));
16156 finish_init_stmt (stmt);
16157 tmp = RECUR (FOR_COND (t));
16158 finish_for_cond (tmp, stmt, false, 0);
16159 tmp = RECUR (FOR_EXPR (t));
16160 finish_for_expr (tmp, stmt);
16162 bool prev = note_iteration_stmt_body_start ();
16163 RECUR (FOR_BODY (t));
16164 note_iteration_stmt_body_end (prev);
16166 finish_for_stmt (stmt);
16167 break;
16169 case RANGE_FOR_STMT:
16171 /* Construct another range_for, if this is not a final
16172 substitution (for inside inside a generic lambda of a
16173 template). Otherwise convert to a regular for. */
16174 tree decl, expr;
16175 stmt = (processing_template_decl
16176 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16177 : begin_for_stmt (NULL_TREE, NULL_TREE));
16178 decl = RANGE_FOR_DECL (t);
16179 decl = tsubst (decl, args, complain, in_decl);
16180 maybe_push_decl (decl);
16181 expr = RECUR (RANGE_FOR_EXPR (t));
16183 tree decomp_first = NULL_TREE;
16184 unsigned decomp_cnt = 0;
16185 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16186 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16187 complain, in_decl,
16188 &decomp_first, &decomp_cnt);
16190 if (processing_template_decl)
16192 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16193 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16194 finish_range_for_decl (stmt, decl, expr);
16196 else
16198 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16199 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16200 stmt = cp_convert_range_for (stmt, decl, expr,
16201 decomp_first, decomp_cnt,
16202 RANGE_FOR_IVDEP (t), unroll);
16205 bool prev = note_iteration_stmt_body_start ();
16206 RECUR (RANGE_FOR_BODY (t));
16207 note_iteration_stmt_body_end (prev);
16208 finish_for_stmt (stmt);
16210 break;
16212 case WHILE_STMT:
16213 stmt = begin_while_stmt ();
16214 tmp = RECUR (WHILE_COND (t));
16215 finish_while_stmt_cond (tmp, stmt, false, 0);
16217 bool prev = note_iteration_stmt_body_start ();
16218 RECUR (WHILE_BODY (t));
16219 note_iteration_stmt_body_end (prev);
16221 finish_while_stmt (stmt);
16222 break;
16224 case DO_STMT:
16225 stmt = begin_do_stmt ();
16227 bool prev = note_iteration_stmt_body_start ();
16228 RECUR (DO_BODY (t));
16229 note_iteration_stmt_body_end (prev);
16231 finish_do_body (stmt);
16232 tmp = RECUR (DO_COND (t));
16233 finish_do_stmt (tmp, stmt, false, 0);
16234 break;
16236 case IF_STMT:
16237 stmt = begin_if_stmt ();
16238 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16239 tmp = RECUR (IF_COND (t));
16240 tmp = finish_if_stmt_cond (tmp, stmt);
16241 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16242 /* Don't instantiate the THEN_CLAUSE. */;
16243 else
16245 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16246 if (inhibit)
16247 ++c_inhibit_evaluation_warnings;
16248 RECUR (THEN_CLAUSE (t));
16249 if (inhibit)
16250 --c_inhibit_evaluation_warnings;
16252 finish_then_clause (stmt);
16254 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16255 /* Don't instantiate the ELSE_CLAUSE. */;
16256 else if (ELSE_CLAUSE (t))
16258 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16259 begin_else_clause (stmt);
16260 if (inhibit)
16261 ++c_inhibit_evaluation_warnings;
16262 RECUR (ELSE_CLAUSE (t));
16263 if (inhibit)
16264 --c_inhibit_evaluation_warnings;
16265 finish_else_clause (stmt);
16268 finish_if_stmt (stmt);
16269 break;
16271 case BIND_EXPR:
16272 if (BIND_EXPR_BODY_BLOCK (t))
16273 stmt = begin_function_body ();
16274 else
16275 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16276 ? BCS_TRY_BLOCK : 0);
16278 RECUR (BIND_EXPR_BODY (t));
16280 if (BIND_EXPR_BODY_BLOCK (t))
16281 finish_function_body (stmt);
16282 else
16283 finish_compound_stmt (stmt);
16284 break;
16286 case BREAK_STMT:
16287 finish_break_stmt ();
16288 break;
16290 case CONTINUE_STMT:
16291 finish_continue_stmt ();
16292 break;
16294 case SWITCH_STMT:
16295 stmt = begin_switch_stmt ();
16296 tmp = RECUR (SWITCH_STMT_COND (t));
16297 finish_switch_cond (tmp, stmt);
16298 RECUR (SWITCH_STMT_BODY (t));
16299 finish_switch_stmt (stmt);
16300 break;
16302 case CASE_LABEL_EXPR:
16304 tree low = RECUR (CASE_LOW (t));
16305 tree high = RECUR (CASE_HIGH (t));
16306 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16307 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16308 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16309 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16311 break;
16313 case LABEL_EXPR:
16315 tree decl = LABEL_EXPR_LABEL (t);
16316 tree label;
16318 label = finish_label_stmt (DECL_NAME (decl));
16319 if (TREE_CODE (label) == LABEL_DECL)
16320 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16321 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16322 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16324 break;
16326 case GOTO_EXPR:
16327 tmp = GOTO_DESTINATION (t);
16328 if (TREE_CODE (tmp) != LABEL_DECL)
16329 /* Computed goto's must be tsubst'd into. On the other hand,
16330 non-computed gotos must not be; the identifier in question
16331 will have no binding. */
16332 tmp = RECUR (tmp);
16333 else
16334 tmp = DECL_NAME (tmp);
16335 finish_goto_stmt (tmp);
16336 break;
16338 case ASM_EXPR:
16340 tree string = RECUR (ASM_STRING (t));
16341 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16342 complain, in_decl);
16343 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16344 complain, in_decl);
16345 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16346 complain, in_decl);
16347 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16348 complain, in_decl);
16349 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16350 clobbers, labels);
16351 tree asm_expr = tmp;
16352 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16353 asm_expr = TREE_OPERAND (asm_expr, 0);
16354 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16356 break;
16358 case TRY_BLOCK:
16359 if (CLEANUP_P (t))
16361 stmt = begin_try_block ();
16362 RECUR (TRY_STMTS (t));
16363 finish_cleanup_try_block (stmt);
16364 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16366 else
16368 tree compound_stmt = NULL_TREE;
16370 if (FN_TRY_BLOCK_P (t))
16371 stmt = begin_function_try_block (&compound_stmt);
16372 else
16373 stmt = begin_try_block ();
16375 RECUR (TRY_STMTS (t));
16377 if (FN_TRY_BLOCK_P (t))
16378 finish_function_try_block (stmt);
16379 else
16380 finish_try_block (stmt);
16382 RECUR (TRY_HANDLERS (t));
16383 if (FN_TRY_BLOCK_P (t))
16384 finish_function_handler_sequence (stmt, compound_stmt);
16385 else
16386 finish_handler_sequence (stmt);
16388 break;
16390 case HANDLER:
16392 tree decl = HANDLER_PARMS (t);
16394 if (decl)
16396 decl = tsubst (decl, args, complain, in_decl);
16397 /* Prevent instantiate_decl from trying to instantiate
16398 this variable. We've already done all that needs to be
16399 done. */
16400 if (decl != error_mark_node)
16401 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16403 stmt = begin_handler ();
16404 finish_handler_parms (decl, stmt);
16405 RECUR (HANDLER_BODY (t));
16406 finish_handler (stmt);
16408 break;
16410 case TAG_DEFN:
16411 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16412 if (CLASS_TYPE_P (tmp))
16414 /* Local classes are not independent templates; they are
16415 instantiated along with their containing function. And this
16416 way we don't have to deal with pushing out of one local class
16417 to instantiate a member of another local class. */
16418 /* Closures are handled by the LAMBDA_EXPR. */
16419 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16420 complete_type (tmp);
16421 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16422 if ((VAR_P (fld)
16423 || (TREE_CODE (fld) == FUNCTION_DECL
16424 && !DECL_ARTIFICIAL (fld)))
16425 && DECL_TEMPLATE_INSTANTIATION (fld))
16426 instantiate_decl (fld, /*defer_ok=*/false,
16427 /*expl_inst_class=*/false);
16429 break;
16431 case STATIC_ASSERT:
16433 tree condition;
16435 ++c_inhibit_evaluation_warnings;
16436 condition =
16437 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16438 args,
16439 complain, in_decl,
16440 /*integral_constant_expression_p=*/true);
16441 --c_inhibit_evaluation_warnings;
16443 finish_static_assert (condition,
16444 STATIC_ASSERT_MESSAGE (t),
16445 STATIC_ASSERT_SOURCE_LOCATION (t),
16446 /*member_p=*/false);
16448 break;
16450 case OACC_KERNELS:
16451 case OACC_PARALLEL:
16452 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16453 in_decl);
16454 stmt = begin_omp_parallel ();
16455 RECUR (OMP_BODY (t));
16456 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16457 break;
16459 case OMP_PARALLEL:
16460 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16461 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16462 complain, in_decl);
16463 if (OMP_PARALLEL_COMBINED (t))
16464 omp_parallel_combined_clauses = &tmp;
16465 stmt = begin_omp_parallel ();
16466 RECUR (OMP_PARALLEL_BODY (t));
16467 gcc_assert (omp_parallel_combined_clauses == NULL);
16468 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16469 = OMP_PARALLEL_COMBINED (t);
16470 pop_omp_privatization_clauses (r);
16471 break;
16473 case OMP_TASK:
16474 r = push_omp_privatization_clauses (false);
16475 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16476 complain, in_decl);
16477 stmt = begin_omp_task ();
16478 RECUR (OMP_TASK_BODY (t));
16479 finish_omp_task (tmp, stmt);
16480 pop_omp_privatization_clauses (r);
16481 break;
16483 case OMP_FOR:
16484 case OMP_SIMD:
16485 case OMP_DISTRIBUTE:
16486 case OMP_TASKLOOP:
16487 case OACC_LOOP:
16489 tree clauses, body, pre_body;
16490 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16491 tree orig_declv = NULL_TREE;
16492 tree incrv = NULL_TREE;
16493 enum c_omp_region_type ort = C_ORT_OMP;
16494 int i;
16496 if (TREE_CODE (t) == OACC_LOOP)
16497 ort = C_ORT_ACC;
16499 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16500 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16501 in_decl);
16502 if (OMP_FOR_INIT (t) != NULL_TREE)
16504 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16505 if (OMP_FOR_ORIG_DECLS (t))
16506 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16507 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16508 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16509 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16512 stmt = begin_omp_structured_block ();
16514 pre_body = push_stmt_list ();
16515 RECUR (OMP_FOR_PRE_BODY (t));
16516 pre_body = pop_stmt_list (pre_body);
16518 if (OMP_FOR_INIT (t) != NULL_TREE)
16519 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16520 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16521 incrv, &clauses, args, complain, in_decl,
16522 integral_constant_expression_p);
16523 omp_parallel_combined_clauses = NULL;
16525 body = push_stmt_list ();
16526 RECUR (OMP_FOR_BODY (t));
16527 body = pop_stmt_list (body);
16529 if (OMP_FOR_INIT (t) != NULL_TREE)
16530 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16531 orig_declv, initv, condv, incrv, body, pre_body,
16532 NULL, clauses);
16533 else
16535 t = make_node (TREE_CODE (t));
16536 TREE_TYPE (t) = void_type_node;
16537 OMP_FOR_BODY (t) = body;
16538 OMP_FOR_PRE_BODY (t) = pre_body;
16539 OMP_FOR_CLAUSES (t) = clauses;
16540 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16541 add_stmt (t);
16544 add_stmt (finish_omp_structured_block (stmt));
16545 pop_omp_privatization_clauses (r);
16547 break;
16549 case OMP_SECTIONS:
16550 omp_parallel_combined_clauses = NULL;
16551 /* FALLTHRU */
16552 case OMP_SINGLE:
16553 case OMP_TEAMS:
16554 case OMP_CRITICAL:
16555 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16556 && OMP_TEAMS_COMBINED (t));
16557 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16558 in_decl);
16559 stmt = push_stmt_list ();
16560 RECUR (OMP_BODY (t));
16561 stmt = pop_stmt_list (stmt);
16563 t = copy_node (t);
16564 OMP_BODY (t) = stmt;
16565 OMP_CLAUSES (t) = tmp;
16566 add_stmt (t);
16567 pop_omp_privatization_clauses (r);
16568 break;
16570 case OACC_DATA:
16571 case OMP_TARGET_DATA:
16572 case OMP_TARGET:
16573 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16574 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16575 in_decl);
16576 keep_next_level (true);
16577 stmt = begin_omp_structured_block ();
16579 RECUR (OMP_BODY (t));
16580 stmt = finish_omp_structured_block (stmt);
16582 t = copy_node (t);
16583 OMP_BODY (t) = stmt;
16584 OMP_CLAUSES (t) = tmp;
16585 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16587 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16588 if (teams)
16590 /* For combined target teams, ensure the num_teams and
16591 thread_limit clause expressions are evaluated on the host,
16592 before entering the target construct. */
16593 tree c;
16594 for (c = OMP_TEAMS_CLAUSES (teams);
16595 c; c = OMP_CLAUSE_CHAIN (c))
16596 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16597 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16598 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16600 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16601 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16602 if (expr == error_mark_node)
16603 continue;
16604 tmp = TARGET_EXPR_SLOT (expr);
16605 add_stmt (expr);
16606 OMP_CLAUSE_OPERAND (c, 0) = expr;
16607 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16608 OMP_CLAUSE_FIRSTPRIVATE);
16609 OMP_CLAUSE_DECL (tc) = tmp;
16610 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16611 OMP_TARGET_CLAUSES (t) = tc;
16615 add_stmt (t);
16616 break;
16618 case OACC_DECLARE:
16619 t = copy_node (t);
16620 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16621 complain, in_decl);
16622 OACC_DECLARE_CLAUSES (t) = tmp;
16623 add_stmt (t);
16624 break;
16626 case OMP_TARGET_UPDATE:
16627 case OMP_TARGET_ENTER_DATA:
16628 case OMP_TARGET_EXIT_DATA:
16629 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16630 complain, in_decl);
16631 t = copy_node (t);
16632 OMP_STANDALONE_CLAUSES (t) = tmp;
16633 add_stmt (t);
16634 break;
16636 case OACC_ENTER_DATA:
16637 case OACC_EXIT_DATA:
16638 case OACC_UPDATE:
16639 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16640 complain, in_decl);
16641 t = copy_node (t);
16642 OMP_STANDALONE_CLAUSES (t) = tmp;
16643 add_stmt (t);
16644 break;
16646 case OMP_ORDERED:
16647 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16648 complain, in_decl);
16649 stmt = push_stmt_list ();
16650 RECUR (OMP_BODY (t));
16651 stmt = pop_stmt_list (stmt);
16653 t = copy_node (t);
16654 OMP_BODY (t) = stmt;
16655 OMP_ORDERED_CLAUSES (t) = tmp;
16656 add_stmt (t);
16657 break;
16659 case OMP_SECTION:
16660 case OMP_MASTER:
16661 case OMP_TASKGROUP:
16662 stmt = push_stmt_list ();
16663 RECUR (OMP_BODY (t));
16664 stmt = pop_stmt_list (stmt);
16666 t = copy_node (t);
16667 OMP_BODY (t) = stmt;
16668 add_stmt (t);
16669 break;
16671 case OMP_ATOMIC:
16672 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16673 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16675 tree op1 = TREE_OPERAND (t, 1);
16676 tree rhs1 = NULL_TREE;
16677 tree lhs, rhs;
16678 if (TREE_CODE (op1) == COMPOUND_EXPR)
16680 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16681 op1 = TREE_OPERAND (op1, 1);
16683 lhs = RECUR (TREE_OPERAND (op1, 0));
16684 rhs = RECUR (TREE_OPERAND (op1, 1));
16685 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16686 NULL_TREE, NULL_TREE, rhs1,
16687 OMP_ATOMIC_SEQ_CST (t));
16689 else
16691 tree op1 = TREE_OPERAND (t, 1);
16692 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16693 tree rhs1 = NULL_TREE;
16694 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16695 enum tree_code opcode = NOP_EXPR;
16696 if (code == OMP_ATOMIC_READ)
16698 v = RECUR (TREE_OPERAND (op1, 0));
16699 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16701 else if (code == OMP_ATOMIC_CAPTURE_OLD
16702 || code == OMP_ATOMIC_CAPTURE_NEW)
16704 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16705 v = RECUR (TREE_OPERAND (op1, 0));
16706 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16707 if (TREE_CODE (op11) == COMPOUND_EXPR)
16709 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16710 op11 = TREE_OPERAND (op11, 1);
16712 lhs = RECUR (TREE_OPERAND (op11, 0));
16713 rhs = RECUR (TREE_OPERAND (op11, 1));
16714 opcode = TREE_CODE (op11);
16715 if (opcode == MODIFY_EXPR)
16716 opcode = NOP_EXPR;
16718 else
16720 code = OMP_ATOMIC;
16721 lhs = RECUR (TREE_OPERAND (op1, 0));
16722 rhs = RECUR (TREE_OPERAND (op1, 1));
16724 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16725 OMP_ATOMIC_SEQ_CST (t));
16727 break;
16729 case TRANSACTION_EXPR:
16731 int flags = 0;
16732 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16733 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16735 if (TRANSACTION_EXPR_IS_STMT (t))
16737 tree body = TRANSACTION_EXPR_BODY (t);
16738 tree noex = NULL_TREE;
16739 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16741 noex = MUST_NOT_THROW_COND (body);
16742 if (noex == NULL_TREE)
16743 noex = boolean_true_node;
16744 body = TREE_OPERAND (body, 0);
16746 stmt = begin_transaction_stmt (input_location, NULL, flags);
16747 RECUR (body);
16748 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16750 else
16752 stmt = build_transaction_expr (EXPR_LOCATION (t),
16753 RECUR (TRANSACTION_EXPR_BODY (t)),
16754 flags, NULL_TREE);
16755 RETURN (stmt);
16758 break;
16760 case MUST_NOT_THROW_EXPR:
16762 tree op0 = RECUR (TREE_OPERAND (t, 0));
16763 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16764 RETURN (build_must_not_throw_expr (op0, cond));
16767 case EXPR_PACK_EXPANSION:
16768 error ("invalid use of pack expansion expression");
16769 RETURN (error_mark_node);
16771 case NONTYPE_ARGUMENT_PACK:
16772 error ("use %<...%> to expand argument pack");
16773 RETURN (error_mark_node);
16775 case COMPOUND_EXPR:
16776 tmp = RECUR (TREE_OPERAND (t, 0));
16777 if (tmp == NULL_TREE)
16778 /* If the first operand was a statement, we're done with it. */
16779 RETURN (RECUR (TREE_OPERAND (t, 1)));
16780 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16781 RECUR (TREE_OPERAND (t, 1)),
16782 complain));
16784 case ANNOTATE_EXPR:
16785 tmp = RECUR (TREE_OPERAND (t, 0));
16786 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16787 TREE_TYPE (tmp), tmp,
16788 RECUR (TREE_OPERAND (t, 1)),
16789 RECUR (TREE_OPERAND (t, 2))));
16791 default:
16792 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16794 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16795 /*function_p=*/false,
16796 integral_constant_expression_p));
16799 RETURN (NULL_TREE);
16800 out:
16801 input_location = loc;
16802 return r;
16803 #undef RECUR
16804 #undef RETURN
16807 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16808 function. For description of the body see comment above
16809 cp_parser_omp_declare_reduction_exprs. */
16811 static void
16812 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16814 if (t == NULL_TREE || t == error_mark_node)
16815 return;
16817 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16819 tree_stmt_iterator tsi;
16820 int i;
16821 tree stmts[7];
16822 memset (stmts, 0, sizeof stmts);
16823 for (i = 0, tsi = tsi_start (t);
16824 i < 7 && !tsi_end_p (tsi);
16825 i++, tsi_next (&tsi))
16826 stmts[i] = tsi_stmt (tsi);
16827 gcc_assert (tsi_end_p (tsi));
16829 if (i >= 3)
16831 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16832 && TREE_CODE (stmts[1]) == DECL_EXPR);
16833 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16834 args, complain, in_decl);
16835 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16836 args, complain, in_decl);
16837 DECL_CONTEXT (omp_out) = current_function_decl;
16838 DECL_CONTEXT (omp_in) = current_function_decl;
16839 keep_next_level (true);
16840 tree block = begin_omp_structured_block ();
16841 tsubst_expr (stmts[2], args, complain, in_decl, false);
16842 block = finish_omp_structured_block (block);
16843 block = maybe_cleanup_point_expr_void (block);
16844 add_decl_expr (omp_out);
16845 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16846 TREE_NO_WARNING (omp_out) = 1;
16847 add_decl_expr (omp_in);
16848 finish_expr_stmt (block);
16850 if (i >= 6)
16852 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16853 && TREE_CODE (stmts[4]) == DECL_EXPR);
16854 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16855 args, complain, in_decl);
16856 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16857 args, complain, in_decl);
16858 DECL_CONTEXT (omp_priv) = current_function_decl;
16859 DECL_CONTEXT (omp_orig) = current_function_decl;
16860 keep_next_level (true);
16861 tree block = begin_omp_structured_block ();
16862 tsubst_expr (stmts[5], args, complain, in_decl, false);
16863 block = finish_omp_structured_block (block);
16864 block = maybe_cleanup_point_expr_void (block);
16865 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16866 add_decl_expr (omp_priv);
16867 add_decl_expr (omp_orig);
16868 finish_expr_stmt (block);
16869 if (i == 7)
16870 add_decl_expr (omp_orig);
16874 /* T is a postfix-expression that is not being used in a function
16875 call. Return the substituted version of T. */
16877 static tree
16878 tsubst_non_call_postfix_expression (tree t, tree args,
16879 tsubst_flags_t complain,
16880 tree in_decl)
16882 if (TREE_CODE (t) == SCOPE_REF)
16883 t = tsubst_qualified_id (t, args, complain, in_decl,
16884 /*done=*/false, /*address_p=*/false);
16885 else
16886 t = tsubst_copy_and_build (t, args, complain, in_decl,
16887 /*function_p=*/false,
16888 /*integral_constant_expression_p=*/false);
16890 return t;
16893 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16894 instantiation context. Instantiating a pack expansion containing a lambda
16895 might result in multiple lambdas all based on the same lambda in the
16896 template. */
16898 tree
16899 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16901 tree oldfn = lambda_function (t);
16902 in_decl = oldfn;
16904 tree r = build_lambda_expr ();
16906 LAMBDA_EXPR_LOCATION (r)
16907 = LAMBDA_EXPR_LOCATION (t);
16908 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16909 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16910 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16912 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16913 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16914 else
16915 record_lambda_scope (r);
16917 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16918 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16920 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16921 cap = TREE_CHAIN (cap))
16923 tree field = TREE_PURPOSE (cap);
16924 if (PACK_EXPANSION_P (field))
16925 field = PACK_EXPANSION_PATTERN (field);
16926 field = tsubst_decl (field, args, complain);
16928 if (field == error_mark_node)
16929 return error_mark_node;
16931 tree init = TREE_VALUE (cap);
16932 if (PACK_EXPANSION_P (init))
16933 init = tsubst_pack_expansion (init, args, complain, in_decl);
16934 else
16935 init = tsubst_copy_and_build (init, args, complain, in_decl,
16936 /*fn*/false, /*constexpr*/false);
16938 if (TREE_CODE (field) == TREE_VEC)
16940 int len = TREE_VEC_LENGTH (field);
16941 gcc_assert (TREE_CODE (init) == TREE_VEC
16942 && TREE_VEC_LENGTH (init) == len);
16943 for (int i = 0; i < len; ++i)
16944 LAMBDA_EXPR_CAPTURE_LIST (r)
16945 = tree_cons (TREE_VEC_ELT (field, i),
16946 TREE_VEC_ELT (init, i),
16947 LAMBDA_EXPR_CAPTURE_LIST (r));
16949 else
16951 LAMBDA_EXPR_CAPTURE_LIST (r)
16952 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16954 if (id_equal (DECL_NAME (field), "__this"))
16955 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16959 tree type = begin_lambda_type (r);
16961 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16962 determine_visibility (TYPE_NAME (type));
16964 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16966 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16967 ? DECL_TI_TEMPLATE (oldfn)
16968 : NULL_TREE);
16970 tree fntype = static_fn_type (oldfn);
16971 if (oldtmpl)
16972 ++processing_template_decl;
16973 fntype = tsubst (fntype, args, complain, in_decl);
16974 if (oldtmpl)
16975 --processing_template_decl;
16977 if (fntype == error_mark_node)
16978 r = error_mark_node;
16979 else
16981 /* Fix the type of 'this'. */
16982 fntype = build_memfn_type (fntype, type,
16983 type_memfn_quals (fntype),
16984 type_memfn_rqual (fntype));
16985 tree fn, tmpl;
16986 if (oldtmpl)
16988 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16989 fn = DECL_TEMPLATE_RESULT (tmpl);
16990 finish_member_declaration (tmpl);
16992 else
16994 tmpl = NULL_TREE;
16995 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16996 finish_member_declaration (fn);
16999 /* Let finish_function set this. */
17000 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17002 bool nested = cfun;
17003 if (nested)
17004 push_function_context ();
17006 local_specialization_stack s (lss_copy);
17008 tree body = start_lambda_function (fn, r);
17010 register_parameter_specializations (oldfn, fn);
17012 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17013 /*constexpr*/false);
17015 finish_lambda_function (body);
17017 if (nested)
17018 pop_function_context ();
17020 /* The capture list was built up in reverse order; fix that now. */
17021 LAMBDA_EXPR_CAPTURE_LIST (r)
17022 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17024 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17026 maybe_add_lambda_conv_op (type);
17029 finish_struct (type, /*attr*/NULL_TREE);
17031 insert_pending_capture_proxies ();
17033 return r;
17036 /* Like tsubst but deals with expressions and performs semantic
17037 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17039 tree
17040 tsubst_copy_and_build (tree t,
17041 tree args,
17042 tsubst_flags_t complain,
17043 tree in_decl,
17044 bool function_p,
17045 bool integral_constant_expression_p)
17047 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17048 #define RECUR(NODE) \
17049 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17050 /*function_p=*/false, \
17051 integral_constant_expression_p)
17053 tree retval, op1;
17054 location_t loc;
17056 if (t == NULL_TREE || t == error_mark_node)
17057 return t;
17059 loc = input_location;
17060 if (EXPR_HAS_LOCATION (t))
17061 input_location = EXPR_LOCATION (t);
17063 /* N3276 decltype magic only applies to calls at the top level or on the
17064 right side of a comma. */
17065 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17066 complain &= ~tf_decltype;
17068 switch (TREE_CODE (t))
17070 case USING_DECL:
17071 t = DECL_NAME (t);
17072 /* Fall through. */
17073 case IDENTIFIER_NODE:
17075 tree decl;
17076 cp_id_kind idk;
17077 bool non_integral_constant_expression_p;
17078 const char *error_msg;
17080 if (IDENTIFIER_CONV_OP_P (t))
17082 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17083 t = make_conv_op_name (new_type);
17086 /* Look up the name. */
17087 decl = lookup_name (t);
17089 /* By convention, expressions use ERROR_MARK_NODE to indicate
17090 failure, not NULL_TREE. */
17091 if (decl == NULL_TREE)
17092 decl = error_mark_node;
17094 decl = finish_id_expression (t, decl, NULL_TREE,
17095 &idk,
17096 integral_constant_expression_p,
17097 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17098 &non_integral_constant_expression_p,
17099 /*template_p=*/false,
17100 /*done=*/true,
17101 /*address_p=*/false,
17102 /*template_arg_p=*/false,
17103 &error_msg,
17104 input_location);
17105 if (error_msg)
17106 error (error_msg);
17107 if (!function_p && identifier_p (decl))
17109 if (complain & tf_error)
17110 unqualified_name_lookup_error (decl);
17111 decl = error_mark_node;
17113 RETURN (decl);
17116 case TEMPLATE_ID_EXPR:
17118 tree object;
17119 tree templ = RECUR (TREE_OPERAND (t, 0));
17120 tree targs = TREE_OPERAND (t, 1);
17122 if (targs)
17123 targs = tsubst_template_args (targs, args, complain, in_decl);
17124 if (targs == error_mark_node)
17125 return error_mark_node;
17127 if (TREE_CODE (templ) == SCOPE_REF)
17129 tree name = TREE_OPERAND (templ, 1);
17130 tree tid = lookup_template_function (name, targs);
17131 TREE_OPERAND (templ, 1) = tid;
17132 return templ;
17135 if (variable_template_p (templ))
17136 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17138 if (TREE_CODE (templ) == COMPONENT_REF)
17140 object = TREE_OPERAND (templ, 0);
17141 templ = TREE_OPERAND (templ, 1);
17143 else
17144 object = NULL_TREE;
17145 templ = lookup_template_function (templ, targs);
17147 if (object)
17148 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17149 object, templ, NULL_TREE));
17150 else
17151 RETURN (baselink_for_fns (templ));
17154 case INDIRECT_REF:
17156 tree r = RECUR (TREE_OPERAND (t, 0));
17158 if (REFERENCE_REF_P (t))
17160 /* A type conversion to reference type will be enclosed in
17161 such an indirect ref, but the substitution of the cast
17162 will have also added such an indirect ref. */
17163 r = convert_from_reference (r);
17165 else
17166 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17167 complain|decltype_flag);
17169 if (TREE_CODE (r) == INDIRECT_REF)
17170 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17172 RETURN (r);
17175 case NOP_EXPR:
17177 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17178 tree op0 = RECUR (TREE_OPERAND (t, 0));
17179 RETURN (build_nop (type, op0));
17182 case IMPLICIT_CONV_EXPR:
17184 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17185 tree expr = RECUR (TREE_OPERAND (t, 0));
17186 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17188 retval = copy_node (t);
17189 TREE_TYPE (retval) = type;
17190 TREE_OPERAND (retval, 0) = expr;
17191 RETURN (retval);
17193 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17194 /* We'll pass this to convert_nontype_argument again, we don't need
17195 to actually perform any conversion here. */
17196 RETURN (expr);
17197 int flags = LOOKUP_IMPLICIT;
17198 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17199 flags = LOOKUP_NORMAL;
17200 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17201 flags));
17204 case CONVERT_EXPR:
17206 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17207 tree op0 = RECUR (TREE_OPERAND (t, 0));
17208 RETURN (build1 (CONVERT_EXPR, type, op0));
17211 case CAST_EXPR:
17212 case REINTERPRET_CAST_EXPR:
17213 case CONST_CAST_EXPR:
17214 case DYNAMIC_CAST_EXPR:
17215 case STATIC_CAST_EXPR:
17217 tree type;
17218 tree op, r = NULL_TREE;
17220 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17221 if (integral_constant_expression_p
17222 && !cast_valid_in_integral_constant_expression_p (type))
17224 if (complain & tf_error)
17225 error ("a cast to a type other than an integral or "
17226 "enumeration type cannot appear in a constant-expression");
17227 RETURN (error_mark_node);
17230 op = RECUR (TREE_OPERAND (t, 0));
17232 warning_sentinel s(warn_useless_cast);
17233 warning_sentinel s2(warn_ignored_qualifiers);
17234 switch (TREE_CODE (t))
17236 case CAST_EXPR:
17237 r = build_functional_cast (type, op, complain);
17238 break;
17239 case REINTERPRET_CAST_EXPR:
17240 r = build_reinterpret_cast (type, op, complain);
17241 break;
17242 case CONST_CAST_EXPR:
17243 r = build_const_cast (type, op, complain);
17244 break;
17245 case DYNAMIC_CAST_EXPR:
17246 r = build_dynamic_cast (type, op, complain);
17247 break;
17248 case STATIC_CAST_EXPR:
17249 r = build_static_cast (type, op, complain);
17250 break;
17251 default:
17252 gcc_unreachable ();
17255 RETURN (r);
17258 case POSTDECREMENT_EXPR:
17259 case POSTINCREMENT_EXPR:
17260 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17261 args, complain, in_decl);
17262 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17263 complain|decltype_flag));
17265 case PREDECREMENT_EXPR:
17266 case PREINCREMENT_EXPR:
17267 case NEGATE_EXPR:
17268 case BIT_NOT_EXPR:
17269 case ABS_EXPR:
17270 case TRUTH_NOT_EXPR:
17271 case UNARY_PLUS_EXPR: /* Unary + */
17272 case REALPART_EXPR:
17273 case IMAGPART_EXPR:
17274 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17275 RECUR (TREE_OPERAND (t, 0)),
17276 complain|decltype_flag));
17278 case FIX_TRUNC_EXPR:
17279 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17280 false, complain));
17282 case ADDR_EXPR:
17283 op1 = TREE_OPERAND (t, 0);
17284 if (TREE_CODE (op1) == LABEL_DECL)
17285 RETURN (finish_label_address_expr (DECL_NAME (op1),
17286 EXPR_LOCATION (op1)));
17287 if (TREE_CODE (op1) == SCOPE_REF)
17288 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17289 /*done=*/true, /*address_p=*/true);
17290 else
17291 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17292 in_decl);
17293 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17294 complain|decltype_flag));
17296 case PLUS_EXPR:
17297 case MINUS_EXPR:
17298 case MULT_EXPR:
17299 case TRUNC_DIV_EXPR:
17300 case CEIL_DIV_EXPR:
17301 case FLOOR_DIV_EXPR:
17302 case ROUND_DIV_EXPR:
17303 case EXACT_DIV_EXPR:
17304 case BIT_AND_EXPR:
17305 case BIT_IOR_EXPR:
17306 case BIT_XOR_EXPR:
17307 case TRUNC_MOD_EXPR:
17308 case FLOOR_MOD_EXPR:
17309 case TRUTH_ANDIF_EXPR:
17310 case TRUTH_ORIF_EXPR:
17311 case TRUTH_AND_EXPR:
17312 case TRUTH_OR_EXPR:
17313 case RSHIFT_EXPR:
17314 case LSHIFT_EXPR:
17315 case RROTATE_EXPR:
17316 case LROTATE_EXPR:
17317 case EQ_EXPR:
17318 case NE_EXPR:
17319 case MAX_EXPR:
17320 case MIN_EXPR:
17321 case LE_EXPR:
17322 case GE_EXPR:
17323 case LT_EXPR:
17324 case GT_EXPR:
17325 case MEMBER_REF:
17326 case DOTSTAR_EXPR:
17328 warning_sentinel s1(warn_type_limits);
17329 warning_sentinel s2(warn_div_by_zero);
17330 warning_sentinel s3(warn_logical_op);
17331 warning_sentinel s4(warn_tautological_compare);
17332 tree op0 = RECUR (TREE_OPERAND (t, 0));
17333 tree op1 = RECUR (TREE_OPERAND (t, 1));
17334 tree r = build_x_binary_op
17335 (input_location, TREE_CODE (t),
17336 op0,
17337 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17338 ? ERROR_MARK
17339 : TREE_CODE (TREE_OPERAND (t, 0))),
17340 op1,
17341 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17342 ? ERROR_MARK
17343 : TREE_CODE (TREE_OPERAND (t, 1))),
17344 /*overload=*/NULL,
17345 complain|decltype_flag);
17346 if (EXPR_P (r) && TREE_NO_WARNING (t))
17347 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17349 RETURN (r);
17352 case POINTER_PLUS_EXPR:
17354 tree op0 = RECUR (TREE_OPERAND (t, 0));
17355 tree op1 = RECUR (TREE_OPERAND (t, 1));
17356 return fold_build_pointer_plus (op0, op1);
17359 case SCOPE_REF:
17360 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17361 /*address_p=*/false));
17362 case ARRAY_REF:
17363 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17364 args, complain, in_decl);
17365 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17366 RECUR (TREE_OPERAND (t, 1)),
17367 complain|decltype_flag));
17369 case SIZEOF_EXPR:
17370 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17371 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17372 RETURN (tsubst_copy (t, args, complain, in_decl));
17373 /* Fall through */
17375 case ALIGNOF_EXPR:
17377 tree r;
17379 op1 = TREE_OPERAND (t, 0);
17380 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17381 op1 = TREE_TYPE (op1);
17382 if (!args)
17384 /* When there are no ARGS, we are trying to evaluate a
17385 non-dependent expression from the parser. Trying to do
17386 the substitutions may not work. */
17387 if (!TYPE_P (op1))
17388 op1 = TREE_TYPE (op1);
17390 else
17392 ++cp_unevaluated_operand;
17393 ++c_inhibit_evaluation_warnings;
17394 if (TYPE_P (op1))
17395 op1 = tsubst (op1, args, complain, in_decl);
17396 else
17397 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17398 /*function_p=*/false,
17399 /*integral_constant_expression_p=*/
17400 false);
17401 --cp_unevaluated_operand;
17402 --c_inhibit_evaluation_warnings;
17404 if (TYPE_P (op1))
17405 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17406 complain & tf_error);
17407 else
17408 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17409 complain & tf_error);
17410 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17412 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17414 if (!processing_template_decl && TYPE_P (op1))
17416 r = build_min (SIZEOF_EXPR, size_type_node,
17417 build1 (NOP_EXPR, op1, error_mark_node));
17418 SIZEOF_EXPR_TYPE_P (r) = 1;
17420 else
17421 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17422 TREE_SIDE_EFFECTS (r) = 0;
17423 TREE_READONLY (r) = 1;
17425 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17427 RETURN (r);
17430 case AT_ENCODE_EXPR:
17432 op1 = TREE_OPERAND (t, 0);
17433 ++cp_unevaluated_operand;
17434 ++c_inhibit_evaluation_warnings;
17435 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17436 /*function_p=*/false,
17437 /*integral_constant_expression_p=*/false);
17438 --cp_unevaluated_operand;
17439 --c_inhibit_evaluation_warnings;
17440 RETURN (objc_build_encode_expr (op1));
17443 case NOEXCEPT_EXPR:
17444 op1 = TREE_OPERAND (t, 0);
17445 ++cp_unevaluated_operand;
17446 ++c_inhibit_evaluation_warnings;
17447 ++cp_noexcept_operand;
17448 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17449 /*function_p=*/false,
17450 /*integral_constant_expression_p=*/false);
17451 --cp_unevaluated_operand;
17452 --c_inhibit_evaluation_warnings;
17453 --cp_noexcept_operand;
17454 RETURN (finish_noexcept_expr (op1, complain));
17456 case MODOP_EXPR:
17458 warning_sentinel s(warn_div_by_zero);
17459 tree lhs = RECUR (TREE_OPERAND (t, 0));
17460 tree rhs = RECUR (TREE_OPERAND (t, 2));
17461 tree r = build_x_modify_expr
17462 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17463 complain|decltype_flag);
17464 /* TREE_NO_WARNING must be set if either the expression was
17465 parenthesized or it uses an operator such as >>= rather
17466 than plain assignment. In the former case, it was already
17467 set and must be copied. In the latter case,
17468 build_x_modify_expr sets it and it must not be reset
17469 here. */
17470 if (TREE_NO_WARNING (t))
17471 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17473 RETURN (r);
17476 case ARROW_EXPR:
17477 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17478 args, complain, in_decl);
17479 /* Remember that there was a reference to this entity. */
17480 if (DECL_P (op1)
17481 && !mark_used (op1, complain) && !(complain & tf_error))
17482 RETURN (error_mark_node);
17483 RETURN (build_x_arrow (input_location, op1, complain));
17485 case NEW_EXPR:
17487 tree placement = RECUR (TREE_OPERAND (t, 0));
17488 tree init = RECUR (TREE_OPERAND (t, 3));
17489 vec<tree, va_gc> *placement_vec;
17490 vec<tree, va_gc> *init_vec;
17491 tree ret;
17493 if (placement == NULL_TREE)
17494 placement_vec = NULL;
17495 else
17497 placement_vec = make_tree_vector ();
17498 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17499 vec_safe_push (placement_vec, TREE_VALUE (placement));
17502 /* If there was an initializer in the original tree, but it
17503 instantiated to an empty list, then we should pass a
17504 non-NULL empty vector to tell build_new that it was an
17505 empty initializer() rather than no initializer. This can
17506 only happen when the initializer is a pack expansion whose
17507 parameter packs are of length zero. */
17508 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17509 init_vec = NULL;
17510 else
17512 init_vec = make_tree_vector ();
17513 if (init == void_node)
17514 gcc_assert (init_vec != NULL);
17515 else
17517 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17518 vec_safe_push (init_vec, TREE_VALUE (init));
17522 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17523 tree op2 = RECUR (TREE_OPERAND (t, 2));
17524 ret = build_new (&placement_vec, op1, op2, &init_vec,
17525 NEW_EXPR_USE_GLOBAL (t),
17526 complain);
17528 if (placement_vec != NULL)
17529 release_tree_vector (placement_vec);
17530 if (init_vec != NULL)
17531 release_tree_vector (init_vec);
17533 RETURN (ret);
17536 case DELETE_EXPR:
17538 tree op0 = RECUR (TREE_OPERAND (t, 0));
17539 tree op1 = RECUR (TREE_OPERAND (t, 1));
17540 RETURN (delete_sanity (op0, op1,
17541 DELETE_EXPR_USE_VEC (t),
17542 DELETE_EXPR_USE_GLOBAL (t),
17543 complain));
17546 case COMPOUND_EXPR:
17548 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17549 complain & ~tf_decltype, in_decl,
17550 /*function_p=*/false,
17551 integral_constant_expression_p);
17552 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17553 op0,
17554 RECUR (TREE_OPERAND (t, 1)),
17555 complain|decltype_flag));
17558 case CALL_EXPR:
17560 tree function;
17561 vec<tree, va_gc> *call_args;
17562 unsigned int nargs, i;
17563 bool qualified_p;
17564 bool koenig_p;
17565 tree ret;
17567 function = CALL_EXPR_FN (t);
17568 /* Internal function with no arguments. */
17569 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17570 RETURN (t);
17572 /* When we parsed the expression, we determined whether or
17573 not Koenig lookup should be performed. */
17574 koenig_p = KOENIG_LOOKUP_P (t);
17575 if (function == NULL_TREE)
17577 koenig_p = false;
17578 qualified_p = false;
17580 else if (TREE_CODE (function) == SCOPE_REF)
17582 qualified_p = true;
17583 function = tsubst_qualified_id (function, args, complain, in_decl,
17584 /*done=*/false,
17585 /*address_p=*/false);
17587 else if (koenig_p && identifier_p (function))
17589 /* Do nothing; calling tsubst_copy_and_build on an identifier
17590 would incorrectly perform unqualified lookup again.
17592 Note that we can also have an IDENTIFIER_NODE if the earlier
17593 unqualified lookup found a member function; in that case
17594 koenig_p will be false and we do want to do the lookup
17595 again to find the instantiated member function.
17597 FIXME but doing that causes c++/15272, so we need to stop
17598 using IDENTIFIER_NODE in that situation. */
17599 qualified_p = false;
17601 else
17603 if (TREE_CODE (function) == COMPONENT_REF)
17605 tree op = TREE_OPERAND (function, 1);
17607 qualified_p = (TREE_CODE (op) == SCOPE_REF
17608 || (BASELINK_P (op)
17609 && BASELINK_QUALIFIED_P (op)));
17611 else
17612 qualified_p = false;
17614 if (TREE_CODE (function) == ADDR_EXPR
17615 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17616 /* Avoid error about taking the address of a constructor. */
17617 function = TREE_OPERAND (function, 0);
17619 function = tsubst_copy_and_build (function, args, complain,
17620 in_decl,
17621 !qualified_p,
17622 integral_constant_expression_p);
17624 if (BASELINK_P (function))
17625 qualified_p = true;
17628 nargs = call_expr_nargs (t);
17629 call_args = make_tree_vector ();
17630 for (i = 0; i < nargs; ++i)
17632 tree arg = CALL_EXPR_ARG (t, i);
17634 if (!PACK_EXPANSION_P (arg))
17635 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17636 else
17638 /* Expand the pack expansion and push each entry onto
17639 CALL_ARGS. */
17640 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17641 if (TREE_CODE (arg) == TREE_VEC)
17643 unsigned int len, j;
17645 len = TREE_VEC_LENGTH (arg);
17646 for (j = 0; j < len; ++j)
17648 tree value = TREE_VEC_ELT (arg, j);
17649 if (value != NULL_TREE)
17650 value = convert_from_reference (value);
17651 vec_safe_push (call_args, value);
17654 else
17656 /* A partial substitution. Add one entry. */
17657 vec_safe_push (call_args, arg);
17662 /* We do not perform argument-dependent lookup if normal
17663 lookup finds a non-function, in accordance with the
17664 expected resolution of DR 218. */
17665 if (koenig_p
17666 && ((is_overloaded_fn (function)
17667 /* If lookup found a member function, the Koenig lookup is
17668 not appropriate, even if an unqualified-name was used
17669 to denote the function. */
17670 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17671 || identifier_p (function))
17672 /* Only do this when substitution turns a dependent call
17673 into a non-dependent call. */
17674 && type_dependent_expression_p_push (t)
17675 && !any_type_dependent_arguments_p (call_args))
17676 function = perform_koenig_lookup (function, call_args, tf_none);
17678 if (function != NULL_TREE
17679 && identifier_p (function)
17680 && !any_type_dependent_arguments_p (call_args))
17682 if (koenig_p && (complain & tf_warning_or_error))
17684 /* For backwards compatibility and good diagnostics, try
17685 the unqualified lookup again if we aren't in SFINAE
17686 context. */
17687 tree unq = (tsubst_copy_and_build
17688 (function, args, complain, in_decl, true,
17689 integral_constant_expression_p));
17690 if (unq == error_mark_node)
17692 release_tree_vector (call_args);
17693 RETURN (error_mark_node);
17696 if (unq != function)
17698 /* In a lambda fn, we have to be careful to not
17699 introduce new this captures. Legacy code can't
17700 be using lambdas anyway, so it's ok to be
17701 stricter. */
17702 bool in_lambda = (current_class_type
17703 && LAMBDA_TYPE_P (current_class_type));
17704 char const *const msg
17705 = G_("%qD was not declared in this scope, "
17706 "and no declarations were found by "
17707 "argument-dependent lookup at the point "
17708 "of instantiation");
17710 bool diag = true;
17711 if (in_lambda)
17712 error_at (EXPR_LOC_OR_LOC (t, input_location),
17713 msg, function);
17714 else
17715 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17716 msg, function);
17717 if (diag)
17719 tree fn = unq;
17721 if (INDIRECT_REF_P (fn))
17722 fn = TREE_OPERAND (fn, 0);
17723 if (is_overloaded_fn (fn))
17724 fn = get_first_fn (fn);
17726 if (!DECL_P (fn))
17727 /* Can't say anything more. */;
17728 else if (DECL_CLASS_SCOPE_P (fn))
17730 location_t loc = EXPR_LOC_OR_LOC (t,
17731 input_location);
17732 inform (loc,
17733 "declarations in dependent base %qT are "
17734 "not found by unqualified lookup",
17735 DECL_CLASS_CONTEXT (fn));
17736 if (current_class_ptr)
17737 inform (loc,
17738 "use %<this->%D%> instead", function);
17739 else
17740 inform (loc,
17741 "use %<%T::%D%> instead",
17742 current_class_name, function);
17744 else
17745 inform (DECL_SOURCE_LOCATION (fn),
17746 "%qD declared here, later in the "
17747 "translation unit", fn);
17748 if (in_lambda)
17750 release_tree_vector (call_args);
17751 RETURN (error_mark_node);
17755 function = unq;
17758 if (identifier_p (function))
17760 if (complain & tf_error)
17761 unqualified_name_lookup_error (function);
17762 release_tree_vector (call_args);
17763 RETURN (error_mark_node);
17767 /* Remember that there was a reference to this entity. */
17768 if (function != NULL_TREE
17769 && DECL_P (function)
17770 && !mark_used (function, complain) && !(complain & tf_error))
17772 release_tree_vector (call_args);
17773 RETURN (error_mark_node);
17776 /* Put back tf_decltype for the actual call. */
17777 complain |= decltype_flag;
17779 if (function == NULL_TREE)
17780 switch (CALL_EXPR_IFN (t))
17782 case IFN_LAUNDER:
17783 gcc_assert (nargs == 1);
17784 if (vec_safe_length (call_args) != 1)
17786 error_at (EXPR_LOC_OR_LOC (t, input_location),
17787 "wrong number of arguments to "
17788 "%<__builtin_launder%>");
17789 ret = error_mark_node;
17791 else
17792 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17793 input_location),
17794 (*call_args)[0], complain);
17795 break;
17797 default:
17798 /* Unsupported internal function with arguments. */
17799 gcc_unreachable ();
17801 else if (TREE_CODE (function) == OFFSET_REF)
17802 ret = build_offset_ref_call_from_tree (function, &call_args,
17803 complain);
17804 else if (TREE_CODE (function) == COMPONENT_REF)
17806 tree instance = TREE_OPERAND (function, 0);
17807 tree fn = TREE_OPERAND (function, 1);
17809 if (processing_template_decl
17810 && (type_dependent_expression_p (instance)
17811 || (!BASELINK_P (fn)
17812 && TREE_CODE (fn) != FIELD_DECL)
17813 || type_dependent_expression_p (fn)
17814 || any_type_dependent_arguments_p (call_args)))
17815 ret = build_min_nt_call_vec (function, call_args);
17816 else if (!BASELINK_P (fn))
17817 ret = finish_call_expr (function, &call_args,
17818 /*disallow_virtual=*/false,
17819 /*koenig_p=*/false,
17820 complain);
17821 else
17822 ret = (build_new_method_call
17823 (instance, fn,
17824 &call_args, NULL_TREE,
17825 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17826 /*fn_p=*/NULL,
17827 complain));
17829 else
17830 ret = finish_call_expr (function, &call_args,
17831 /*disallow_virtual=*/qualified_p,
17832 koenig_p,
17833 complain);
17835 release_tree_vector (call_args);
17837 if (ret != error_mark_node)
17839 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17840 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17841 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17842 bool thk = CALL_FROM_THUNK_P (t);
17843 if (op || ord || rev || thk)
17845 function = extract_call_expr (ret);
17846 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17847 CALL_EXPR_ORDERED_ARGS (function) = ord;
17848 CALL_EXPR_REVERSE_ARGS (function) = rev;
17849 if (thk)
17851 if (TREE_CODE (function) == CALL_EXPR)
17852 CALL_FROM_THUNK_P (function) = true;
17853 else
17854 AGGR_INIT_FROM_THUNK_P (function) = true;
17855 /* The thunk location is not interesting. */
17856 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17861 RETURN (ret);
17864 case COND_EXPR:
17866 tree cond = RECUR (TREE_OPERAND (t, 0));
17867 tree folded_cond = fold_non_dependent_expr (cond);
17868 tree exp1, exp2;
17870 if (TREE_CODE (folded_cond) == INTEGER_CST)
17872 if (integer_zerop (folded_cond))
17874 ++c_inhibit_evaluation_warnings;
17875 exp1 = RECUR (TREE_OPERAND (t, 1));
17876 --c_inhibit_evaluation_warnings;
17877 exp2 = RECUR (TREE_OPERAND (t, 2));
17879 else
17881 exp1 = RECUR (TREE_OPERAND (t, 1));
17882 ++c_inhibit_evaluation_warnings;
17883 exp2 = RECUR (TREE_OPERAND (t, 2));
17884 --c_inhibit_evaluation_warnings;
17886 cond = folded_cond;
17888 else
17890 exp1 = RECUR (TREE_OPERAND (t, 1));
17891 exp2 = RECUR (TREE_OPERAND (t, 2));
17894 warning_sentinel s(warn_duplicated_branches);
17895 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17896 cond, exp1, exp2, complain));
17899 case PSEUDO_DTOR_EXPR:
17901 tree op0 = RECUR (TREE_OPERAND (t, 0));
17902 tree op1 = RECUR (TREE_OPERAND (t, 1));
17903 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17904 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17905 input_location));
17908 case TREE_LIST:
17910 tree purpose, value, chain;
17912 if (t == void_list_node)
17913 RETURN (t);
17915 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17916 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17918 /* We have pack expansions, so expand those and
17919 create a new list out of it. */
17920 tree purposevec = NULL_TREE;
17921 tree valuevec = NULL_TREE;
17922 tree chain;
17923 int i, len = -1;
17925 /* Expand the argument expressions. */
17926 if (TREE_PURPOSE (t))
17927 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17928 complain, in_decl);
17929 if (TREE_VALUE (t))
17930 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17931 complain, in_decl);
17933 /* Build the rest of the list. */
17934 chain = TREE_CHAIN (t);
17935 if (chain && chain != void_type_node)
17936 chain = RECUR (chain);
17938 /* Determine the number of arguments. */
17939 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17941 len = TREE_VEC_LENGTH (purposevec);
17942 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17944 else if (TREE_CODE (valuevec) == TREE_VEC)
17945 len = TREE_VEC_LENGTH (valuevec);
17946 else
17948 /* Since we only performed a partial substitution into
17949 the argument pack, we only RETURN (a single list
17950 node. */
17951 if (purposevec == TREE_PURPOSE (t)
17952 && valuevec == TREE_VALUE (t)
17953 && chain == TREE_CHAIN (t))
17954 RETURN (t);
17956 RETURN (tree_cons (purposevec, valuevec, chain));
17959 /* Convert the argument vectors into a TREE_LIST */
17960 i = len;
17961 while (i > 0)
17963 /* Grab the Ith values. */
17964 i--;
17965 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17966 : NULL_TREE;
17967 value
17968 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17969 : NULL_TREE;
17971 /* Build the list (backwards). */
17972 chain = tree_cons (purpose, value, chain);
17975 RETURN (chain);
17978 purpose = TREE_PURPOSE (t);
17979 if (purpose)
17980 purpose = RECUR (purpose);
17981 value = TREE_VALUE (t);
17982 if (value)
17983 value = RECUR (value);
17984 chain = TREE_CHAIN (t);
17985 if (chain && chain != void_type_node)
17986 chain = RECUR (chain);
17987 if (purpose == TREE_PURPOSE (t)
17988 && value == TREE_VALUE (t)
17989 && chain == TREE_CHAIN (t))
17990 RETURN (t);
17991 RETURN (tree_cons (purpose, value, chain));
17994 case COMPONENT_REF:
17996 tree object;
17997 tree object_type;
17998 tree member;
17999 tree r;
18001 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18002 args, complain, in_decl);
18003 /* Remember that there was a reference to this entity. */
18004 if (DECL_P (object)
18005 && !mark_used (object, complain) && !(complain & tf_error))
18006 RETURN (error_mark_node);
18007 object_type = TREE_TYPE (object);
18009 member = TREE_OPERAND (t, 1);
18010 if (BASELINK_P (member))
18011 member = tsubst_baselink (member,
18012 non_reference (TREE_TYPE (object)),
18013 args, complain, in_decl);
18014 else
18015 member = tsubst_copy (member, args, complain, in_decl);
18016 if (member == error_mark_node)
18017 RETURN (error_mark_node);
18019 if (TREE_CODE (member) == FIELD_DECL)
18021 r = finish_non_static_data_member (member, object, NULL_TREE);
18022 if (TREE_CODE (r) == COMPONENT_REF)
18023 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18024 RETURN (r);
18026 else if (type_dependent_expression_p (object))
18027 /* We can't do much here. */;
18028 else if (!CLASS_TYPE_P (object_type))
18030 if (scalarish_type_p (object_type))
18032 tree s = NULL_TREE;
18033 tree dtor = member;
18035 if (TREE_CODE (dtor) == SCOPE_REF)
18037 s = TREE_OPERAND (dtor, 0);
18038 dtor = TREE_OPERAND (dtor, 1);
18040 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18042 dtor = TREE_OPERAND (dtor, 0);
18043 if (TYPE_P (dtor))
18044 RETURN (finish_pseudo_destructor_expr
18045 (object, s, dtor, input_location));
18049 else if (TREE_CODE (member) == SCOPE_REF
18050 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18052 /* Lookup the template functions now that we know what the
18053 scope is. */
18054 tree scope = TREE_OPERAND (member, 0);
18055 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18056 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18057 member = lookup_qualified_name (scope, tmpl,
18058 /*is_type_p=*/false,
18059 /*complain=*/false);
18060 if (BASELINK_P (member))
18062 BASELINK_FUNCTIONS (member)
18063 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18064 args);
18065 member = (adjust_result_of_qualified_name_lookup
18066 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18067 object_type));
18069 else
18071 qualified_name_lookup_error (scope, tmpl, member,
18072 input_location);
18073 RETURN (error_mark_node);
18076 else if (TREE_CODE (member) == SCOPE_REF
18077 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18078 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18080 if (complain & tf_error)
18082 if (TYPE_P (TREE_OPERAND (member, 0)))
18083 error ("%qT is not a class or namespace",
18084 TREE_OPERAND (member, 0));
18085 else
18086 error ("%qD is not a class or namespace",
18087 TREE_OPERAND (member, 0));
18089 RETURN (error_mark_node);
18092 r = finish_class_member_access_expr (object, member,
18093 /*template_p=*/false,
18094 complain);
18095 if (TREE_CODE (r) == COMPONENT_REF)
18096 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18097 RETURN (r);
18100 case THROW_EXPR:
18101 RETURN (build_throw
18102 (RECUR (TREE_OPERAND (t, 0))));
18104 case CONSTRUCTOR:
18106 vec<constructor_elt, va_gc> *n;
18107 constructor_elt *ce;
18108 unsigned HOST_WIDE_INT idx;
18109 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18110 bool process_index_p;
18111 int newlen;
18112 bool need_copy_p = false;
18113 tree r;
18115 if (type == error_mark_node)
18116 RETURN (error_mark_node);
18118 /* We do not want to process the index of aggregate
18119 initializers as they are identifier nodes which will be
18120 looked up by digest_init. */
18121 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18123 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18124 newlen = vec_safe_length (n);
18125 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18127 if (ce->index && process_index_p
18128 /* An identifier index is looked up in the type
18129 being initialized, not the current scope. */
18130 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18131 ce->index = RECUR (ce->index);
18133 if (PACK_EXPANSION_P (ce->value))
18135 /* Substitute into the pack expansion. */
18136 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18137 in_decl);
18139 if (ce->value == error_mark_node
18140 || PACK_EXPANSION_P (ce->value))
18142 else if (TREE_VEC_LENGTH (ce->value) == 1)
18143 /* Just move the argument into place. */
18144 ce->value = TREE_VEC_ELT (ce->value, 0);
18145 else
18147 /* Update the length of the final CONSTRUCTOR
18148 arguments vector, and note that we will need to
18149 copy.*/
18150 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18151 need_copy_p = true;
18154 else
18155 ce->value = RECUR (ce->value);
18158 if (need_copy_p)
18160 vec<constructor_elt, va_gc> *old_n = n;
18162 vec_alloc (n, newlen);
18163 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18165 if (TREE_CODE (ce->value) == TREE_VEC)
18167 int i, len = TREE_VEC_LENGTH (ce->value);
18168 for (i = 0; i < len; ++i)
18169 CONSTRUCTOR_APPEND_ELT (n, 0,
18170 TREE_VEC_ELT (ce->value, i));
18172 else
18173 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18177 r = build_constructor (init_list_type_node, n);
18178 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18180 if (TREE_HAS_CONSTRUCTOR (t))
18182 fcl_t cl = fcl_functional;
18183 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18184 cl = fcl_c99;
18185 RETURN (finish_compound_literal (type, r, complain, cl));
18188 TREE_TYPE (r) = type;
18189 RETURN (r);
18192 case TYPEID_EXPR:
18194 tree operand_0 = TREE_OPERAND (t, 0);
18195 if (TYPE_P (operand_0))
18197 operand_0 = tsubst (operand_0, args, complain, in_decl);
18198 RETURN (get_typeid (operand_0, complain));
18200 else
18202 operand_0 = RECUR (operand_0);
18203 RETURN (build_typeid (operand_0, complain));
18207 case VAR_DECL:
18208 if (!args)
18209 RETURN (t);
18210 else if (DECL_PACK_P (t))
18212 /* We don't build decls for an instantiation of a
18213 variadic capture proxy, we instantiate the elements
18214 when needed. */
18215 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18216 return RECUR (DECL_VALUE_EXPR (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 we need to consider it
23958 instantiation-dependent so that we can check access at instantiation
23959 time even though we know which member it resolves to. */
23961 static bool
23962 instantiation_dependent_scope_ref_p (tree t)
23964 if (DECL_P (TREE_OPERAND (t, 1))
23965 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23966 /* A dependent base could make a member inaccessible in the current
23967 class. */
23968 && !any_dependent_bases_p ()
23969 && accessible_in_template_p (TREE_OPERAND (t, 0),
23970 TREE_OPERAND (t, 1)))
23971 return false;
23972 else
23973 return true;
23976 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23977 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23978 expression. */
23980 /* Note that this predicate is not appropriate for general expressions;
23981 only constant expressions (that satisfy potential_constant_expression)
23982 can be tested for value dependence. */
23984 bool
23985 value_dependent_expression_p (tree expression)
23987 if (!processing_template_decl || expression == NULL_TREE)
23988 return false;
23990 /* A type-dependent expression is also value-dependent. */
23991 if (type_dependent_expression_p (expression))
23992 return true;
23994 switch (TREE_CODE (expression))
23996 case BASELINK:
23997 /* A dependent member function of the current instantiation. */
23998 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24000 case FUNCTION_DECL:
24001 /* A dependent member function of the current instantiation. */
24002 if (DECL_CLASS_SCOPE_P (expression)
24003 && dependent_type_p (DECL_CONTEXT (expression)))
24004 return true;
24005 break;
24007 case IDENTIFIER_NODE:
24008 /* A name that has not been looked up -- must be dependent. */
24009 return true;
24011 case TEMPLATE_PARM_INDEX:
24012 /* A non-type template parm. */
24013 return true;
24015 case CONST_DECL:
24016 /* A non-type template parm. */
24017 if (DECL_TEMPLATE_PARM_P (expression))
24018 return true;
24019 return value_dependent_expression_p (DECL_INITIAL (expression));
24021 case VAR_DECL:
24022 /* A constant with literal type and is initialized
24023 with an expression that is value-dependent. */
24024 if (DECL_DEPENDENT_INIT_P (expression)
24025 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24026 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24027 return true;
24028 if (DECL_HAS_VALUE_EXPR_P (expression))
24030 tree value_expr = DECL_VALUE_EXPR (expression);
24031 if (value_dependent_expression_p (value_expr))
24032 return true;
24034 return false;
24036 case DYNAMIC_CAST_EXPR:
24037 case STATIC_CAST_EXPR:
24038 case CONST_CAST_EXPR:
24039 case REINTERPRET_CAST_EXPR:
24040 case CAST_EXPR:
24041 case IMPLICIT_CONV_EXPR:
24042 /* These expressions are value-dependent if the type to which
24043 the cast occurs is dependent or the expression being casted
24044 is value-dependent. */
24046 tree type = TREE_TYPE (expression);
24048 if (dependent_type_p (type))
24049 return true;
24051 /* A functional cast has a list of operands. */
24052 expression = TREE_OPERAND (expression, 0);
24053 if (!expression)
24055 /* If there are no operands, it must be an expression such
24056 as "int()". This should not happen for aggregate types
24057 because it would form non-constant expressions. */
24058 gcc_assert (cxx_dialect >= cxx11
24059 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24061 return false;
24064 if (TREE_CODE (expression) == TREE_LIST)
24065 return any_value_dependent_elements_p (expression);
24067 return value_dependent_expression_p (expression);
24070 case SIZEOF_EXPR:
24071 if (SIZEOF_EXPR_TYPE_P (expression))
24072 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24073 /* FALLTHRU */
24074 case ALIGNOF_EXPR:
24075 case TYPEID_EXPR:
24076 /* A `sizeof' expression is value-dependent if the operand is
24077 type-dependent or is a pack expansion. */
24078 expression = TREE_OPERAND (expression, 0);
24079 if (PACK_EXPANSION_P (expression))
24080 return true;
24081 else if (TYPE_P (expression))
24082 return dependent_type_p (expression);
24083 return instantiation_dependent_uneval_expression_p (expression);
24085 case AT_ENCODE_EXPR:
24086 /* An 'encode' expression is value-dependent if the operand is
24087 type-dependent. */
24088 expression = TREE_OPERAND (expression, 0);
24089 return dependent_type_p (expression);
24091 case NOEXCEPT_EXPR:
24092 expression = TREE_OPERAND (expression, 0);
24093 return instantiation_dependent_uneval_expression_p (expression);
24095 case SCOPE_REF:
24096 /* All instantiation-dependent expressions should also be considered
24097 value-dependent. */
24098 return instantiation_dependent_scope_ref_p (expression);
24100 case COMPONENT_REF:
24101 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24102 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24104 case NONTYPE_ARGUMENT_PACK:
24105 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24106 is value-dependent. */
24108 tree values = ARGUMENT_PACK_ARGS (expression);
24109 int i, len = TREE_VEC_LENGTH (values);
24111 for (i = 0; i < len; ++i)
24112 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24113 return true;
24115 return false;
24118 case TRAIT_EXPR:
24120 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24122 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24123 return true;
24125 if (!type2)
24126 return false;
24128 if (TREE_CODE (type2) != TREE_LIST)
24129 return dependent_type_p (type2);
24131 for (; type2; type2 = TREE_CHAIN (type2))
24132 if (dependent_type_p (TREE_VALUE (type2)))
24133 return true;
24135 return false;
24138 case MODOP_EXPR:
24139 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24140 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24142 case ARRAY_REF:
24143 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24144 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24146 case ADDR_EXPR:
24148 tree op = TREE_OPERAND (expression, 0);
24149 return (value_dependent_expression_p (op)
24150 || has_value_dependent_address (op));
24153 case REQUIRES_EXPR:
24154 /* Treat all requires-expressions as value-dependent so
24155 we don't try to fold them. */
24156 return true;
24158 case TYPE_REQ:
24159 return dependent_type_p (TREE_OPERAND (expression, 0));
24161 case CALL_EXPR:
24163 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24164 return true;
24165 tree fn = get_callee_fndecl (expression);
24166 int i, nargs;
24167 nargs = call_expr_nargs (expression);
24168 for (i = 0; i < nargs; ++i)
24170 tree op = CALL_EXPR_ARG (expression, i);
24171 /* In a call to a constexpr member function, look through the
24172 implicit ADDR_EXPR on the object argument so that it doesn't
24173 cause the call to be considered value-dependent. We also
24174 look through it in potential_constant_expression. */
24175 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24176 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24177 && TREE_CODE (op) == ADDR_EXPR)
24178 op = TREE_OPERAND (op, 0);
24179 if (value_dependent_expression_p (op))
24180 return true;
24182 return false;
24185 case TEMPLATE_ID_EXPR:
24186 return variable_concept_p (TREE_OPERAND (expression, 0));
24188 case CONSTRUCTOR:
24190 unsigned ix;
24191 tree val;
24192 if (dependent_type_p (TREE_TYPE (expression)))
24193 return true;
24194 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24195 if (value_dependent_expression_p (val))
24196 return true;
24197 return false;
24200 case STMT_EXPR:
24201 /* Treat a GNU statement expression as dependent to avoid crashing
24202 under instantiate_non_dependent_expr; it can't be constant. */
24203 return true;
24205 default:
24206 /* A constant expression is value-dependent if any subexpression is
24207 value-dependent. */
24208 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24210 case tcc_reference:
24211 case tcc_unary:
24212 case tcc_comparison:
24213 case tcc_binary:
24214 case tcc_expression:
24215 case tcc_vl_exp:
24217 int i, len = cp_tree_operand_length (expression);
24219 for (i = 0; i < len; i++)
24221 tree t = TREE_OPERAND (expression, i);
24223 /* In some cases, some of the operands may be missing.
24224 (For example, in the case of PREDECREMENT_EXPR, the
24225 amount to increment by may be missing.) That doesn't
24226 make the expression dependent. */
24227 if (t && value_dependent_expression_p (t))
24228 return true;
24231 break;
24232 default:
24233 break;
24235 break;
24238 /* The expression is not value-dependent. */
24239 return false;
24242 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24243 [temp.dep.expr]. Note that an expression with no type is
24244 considered dependent. Other parts of the compiler arrange for an
24245 expression with type-dependent subexpressions to have no type, so
24246 this function doesn't have to be fully recursive. */
24248 bool
24249 type_dependent_expression_p (tree expression)
24251 if (!processing_template_decl)
24252 return false;
24254 if (expression == NULL_TREE || expression == error_mark_node)
24255 return false;
24257 STRIP_ANY_LOCATION_WRAPPER (expression);
24259 /* An unresolved name is always dependent. */
24260 if (identifier_p (expression)
24261 || TREE_CODE (expression) == USING_DECL
24262 || TREE_CODE (expression) == WILDCARD_DECL)
24263 return true;
24265 /* A fold expression is type-dependent. */
24266 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24267 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24268 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24269 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24270 return true;
24272 /* Some expression forms are never type-dependent. */
24273 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24274 || TREE_CODE (expression) == SIZEOF_EXPR
24275 || TREE_CODE (expression) == ALIGNOF_EXPR
24276 || TREE_CODE (expression) == AT_ENCODE_EXPR
24277 || TREE_CODE (expression) == NOEXCEPT_EXPR
24278 || TREE_CODE (expression) == TRAIT_EXPR
24279 || TREE_CODE (expression) == TYPEID_EXPR
24280 || TREE_CODE (expression) == DELETE_EXPR
24281 || TREE_CODE (expression) == VEC_DELETE_EXPR
24282 || TREE_CODE (expression) == THROW_EXPR
24283 || TREE_CODE (expression) == REQUIRES_EXPR)
24284 return false;
24286 /* The types of these expressions depends only on the type to which
24287 the cast occurs. */
24288 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24289 || TREE_CODE (expression) == STATIC_CAST_EXPR
24290 || TREE_CODE (expression) == CONST_CAST_EXPR
24291 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24292 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24293 || TREE_CODE (expression) == CAST_EXPR)
24294 return dependent_type_p (TREE_TYPE (expression));
24296 /* The types of these expressions depends only on the type created
24297 by the expression. */
24298 if (TREE_CODE (expression) == NEW_EXPR
24299 || TREE_CODE (expression) == VEC_NEW_EXPR)
24301 /* For NEW_EXPR tree nodes created inside a template, either
24302 the object type itself or a TREE_LIST may appear as the
24303 operand 1. */
24304 tree type = TREE_OPERAND (expression, 1);
24305 if (TREE_CODE (type) == TREE_LIST)
24306 /* This is an array type. We need to check array dimensions
24307 as well. */
24308 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24309 || value_dependent_expression_p
24310 (TREE_OPERAND (TREE_VALUE (type), 1));
24311 else
24312 return dependent_type_p (type);
24315 if (TREE_CODE (expression) == SCOPE_REF)
24317 tree scope = TREE_OPERAND (expression, 0);
24318 tree name = TREE_OPERAND (expression, 1);
24320 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24321 contains an identifier associated by name lookup with one or more
24322 declarations declared with a dependent type, or...a
24323 nested-name-specifier or qualified-id that names a member of an
24324 unknown specialization. */
24325 return (type_dependent_expression_p (name)
24326 || dependent_scope_p (scope));
24329 if (TREE_CODE (expression) == TEMPLATE_DECL
24330 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24331 return uses_outer_template_parms (expression);
24333 if (TREE_CODE (expression) == STMT_EXPR)
24334 expression = stmt_expr_value_expr (expression);
24336 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24338 tree elt;
24339 unsigned i;
24341 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24343 if (type_dependent_expression_p (elt))
24344 return true;
24346 return false;
24349 /* A static data member of the current instantiation with incomplete
24350 array type is type-dependent, as the definition and specializations
24351 can have different bounds. */
24352 if (VAR_P (expression)
24353 && DECL_CLASS_SCOPE_P (expression)
24354 && dependent_type_p (DECL_CONTEXT (expression))
24355 && VAR_HAD_UNKNOWN_BOUND (expression))
24356 return true;
24358 /* An array of unknown bound depending on a variadic parameter, eg:
24360 template<typename... Args>
24361 void foo (Args... args)
24363 int arr[] = { args... };
24366 template<int... vals>
24367 void bar ()
24369 int arr[] = { vals... };
24372 If the array has no length and has an initializer, it must be that
24373 we couldn't determine its length in cp_complete_array_type because
24374 it is dependent. */
24375 if (VAR_P (expression)
24376 && TREE_TYPE (expression) != NULL_TREE
24377 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24378 && !TYPE_DOMAIN (TREE_TYPE (expression))
24379 && DECL_INITIAL (expression))
24380 return true;
24382 /* A function or variable template-id is type-dependent if it has any
24383 dependent template arguments. */
24384 if (VAR_OR_FUNCTION_DECL_P (expression)
24385 && DECL_LANG_SPECIFIC (expression)
24386 && DECL_TEMPLATE_INFO (expression))
24388 /* Consider the innermost template arguments, since those are the ones
24389 that come from the template-id; the template arguments for the
24390 enclosing class do not make it type-dependent unless they are used in
24391 the type of the decl. */
24392 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24393 && (any_dependent_template_arguments_p
24394 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24395 return true;
24398 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24399 type-dependent. Checking this is important for functions with auto return
24400 type, which looks like a dependent type. */
24401 if (TREE_CODE (expression) == FUNCTION_DECL
24402 && !(DECL_CLASS_SCOPE_P (expression)
24403 && dependent_type_p (DECL_CONTEXT (expression)))
24404 && !(DECL_FRIEND_P (expression)
24405 && (!DECL_FRIEND_CONTEXT (expression)
24406 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24407 && !DECL_LOCAL_FUNCTION_P (expression))
24409 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24410 || undeduced_auto_decl (expression));
24411 return false;
24414 /* Always dependent, on the number of arguments if nothing else. */
24415 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24416 return true;
24418 if (TREE_TYPE (expression) == unknown_type_node)
24420 if (TREE_CODE (expression) == ADDR_EXPR)
24421 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24422 if (TREE_CODE (expression) == COMPONENT_REF
24423 || TREE_CODE (expression) == OFFSET_REF)
24425 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24426 return true;
24427 expression = TREE_OPERAND (expression, 1);
24428 if (identifier_p (expression))
24429 return false;
24431 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24432 if (TREE_CODE (expression) == SCOPE_REF)
24433 return false;
24435 if (BASELINK_P (expression))
24437 if (BASELINK_OPTYPE (expression)
24438 && dependent_type_p (BASELINK_OPTYPE (expression)))
24439 return true;
24440 expression = BASELINK_FUNCTIONS (expression);
24443 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24445 if (any_dependent_template_arguments_p
24446 (TREE_OPERAND (expression, 1)))
24447 return true;
24448 expression = TREE_OPERAND (expression, 0);
24449 if (identifier_p (expression))
24450 return true;
24453 gcc_assert (TREE_CODE (expression) == OVERLOAD
24454 || TREE_CODE (expression) == FUNCTION_DECL);
24456 for (lkp_iterator iter (expression); iter; ++iter)
24457 if (type_dependent_expression_p (*iter))
24458 return true;
24460 return false;
24463 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24465 /* Dependent type attributes might not have made it from the decl to
24466 the type yet. */
24467 if (DECL_P (expression)
24468 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24469 return true;
24471 return (dependent_type_p (TREE_TYPE (expression)));
24474 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24475 type-dependent if the expression refers to a member of the current
24476 instantiation and the type of the referenced member is dependent, or the
24477 class member access expression refers to a member of an unknown
24478 specialization.
24480 This function returns true if the OBJECT in such a class member access
24481 expression is of an unknown specialization. */
24483 bool
24484 type_dependent_object_expression_p (tree object)
24486 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24487 dependent. */
24488 if (TREE_CODE (object) == IDENTIFIER_NODE)
24489 return true;
24490 tree scope = TREE_TYPE (object);
24491 return (!scope || dependent_scope_p (scope));
24494 /* walk_tree callback function for instantiation_dependent_expression_p,
24495 below. Returns non-zero if a dependent subexpression is found. */
24497 static tree
24498 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24499 void * /*data*/)
24501 if (TYPE_P (*tp))
24503 /* We don't have to worry about decltype currently because decltype
24504 of an instantiation-dependent expr is a dependent type. This
24505 might change depending on the resolution of DR 1172. */
24506 *walk_subtrees = false;
24507 return NULL_TREE;
24509 enum tree_code code = TREE_CODE (*tp);
24510 switch (code)
24512 /* Don't treat an argument list as dependent just because it has no
24513 TREE_TYPE. */
24514 case TREE_LIST:
24515 case TREE_VEC:
24516 return NULL_TREE;
24518 case TEMPLATE_PARM_INDEX:
24519 return *tp;
24521 /* Handle expressions with type operands. */
24522 case SIZEOF_EXPR:
24523 case ALIGNOF_EXPR:
24524 case TYPEID_EXPR:
24525 case AT_ENCODE_EXPR:
24527 tree op = TREE_OPERAND (*tp, 0);
24528 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24529 op = TREE_TYPE (op);
24530 if (TYPE_P (op))
24532 if (dependent_type_p (op))
24533 return *tp;
24534 else
24536 *walk_subtrees = false;
24537 return NULL_TREE;
24540 break;
24543 case COMPONENT_REF:
24544 if (identifier_p (TREE_OPERAND (*tp, 1)))
24545 /* In a template, finish_class_member_access_expr creates a
24546 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24547 type-dependent, so that we can check access control at
24548 instantiation time (PR 42277). See also Core issue 1273. */
24549 return *tp;
24550 break;
24552 case SCOPE_REF:
24553 if (instantiation_dependent_scope_ref_p (*tp))
24554 return *tp;
24555 else
24556 break;
24558 /* Treat statement-expressions as dependent. */
24559 case BIND_EXPR:
24560 return *tp;
24562 /* Treat requires-expressions as dependent. */
24563 case REQUIRES_EXPR:
24564 return *tp;
24566 case CALL_EXPR:
24567 /* Treat calls to function concepts as dependent. */
24568 if (function_concept_check_p (*tp))
24569 return *tp;
24570 break;
24572 case TEMPLATE_ID_EXPR:
24573 /* And variable concepts. */
24574 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24575 return *tp;
24576 break;
24578 default:
24579 break;
24582 if (type_dependent_expression_p (*tp))
24583 return *tp;
24584 else
24585 return NULL_TREE;
24588 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24589 sense defined by the ABI:
24591 "An expression is instantiation-dependent if it is type-dependent
24592 or value-dependent, or it has a subexpression that is type-dependent
24593 or value-dependent."
24595 Except don't actually check value-dependence for unevaluated expressions,
24596 because in sizeof(i) we don't care about the value of i. Checking
24597 type-dependence will in turn check value-dependence of array bounds/template
24598 arguments as needed. */
24600 bool
24601 instantiation_dependent_uneval_expression_p (tree expression)
24603 tree result;
24605 if (!processing_template_decl)
24606 return false;
24608 if (expression == error_mark_node)
24609 return false;
24611 result = cp_walk_tree_without_duplicates (&expression,
24612 instantiation_dependent_r, NULL);
24613 return result != NULL_TREE;
24616 /* As above, but also check value-dependence of the expression as a whole. */
24618 bool
24619 instantiation_dependent_expression_p (tree expression)
24621 return (instantiation_dependent_uneval_expression_p (expression)
24622 || value_dependent_expression_p (expression));
24625 /* Like type_dependent_expression_p, but it also works while not processing
24626 a template definition, i.e. during substitution or mangling. */
24628 bool
24629 type_dependent_expression_p_push (tree expr)
24631 bool b;
24632 ++processing_template_decl;
24633 b = type_dependent_expression_p (expr);
24634 --processing_template_decl;
24635 return b;
24638 /* Returns TRUE if ARGS contains a type-dependent expression. */
24640 bool
24641 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24643 unsigned int i;
24644 tree arg;
24646 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24648 if (type_dependent_expression_p (arg))
24649 return true;
24651 return false;
24654 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24655 expressions) contains any type-dependent expressions. */
24657 bool
24658 any_type_dependent_elements_p (const_tree list)
24660 for (; list; list = TREE_CHAIN (list))
24661 if (type_dependent_expression_p (TREE_VALUE (list)))
24662 return true;
24664 return false;
24667 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24668 expressions) contains any value-dependent expressions. */
24670 bool
24671 any_value_dependent_elements_p (const_tree list)
24673 for (; list; list = TREE_CHAIN (list))
24674 if (value_dependent_expression_p (TREE_VALUE (list)))
24675 return true;
24677 return false;
24680 /* Returns TRUE if the ARG (a template argument) is dependent. */
24682 bool
24683 dependent_template_arg_p (tree arg)
24685 if (!processing_template_decl)
24686 return false;
24688 /* Assume a template argument that was wrongly written by the user
24689 is dependent. This is consistent with what
24690 any_dependent_template_arguments_p [that calls this function]
24691 does. */
24692 if (!arg || arg == error_mark_node)
24693 return true;
24695 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24696 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24698 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24699 return true;
24700 if (TREE_CODE (arg) == TEMPLATE_DECL)
24702 if (DECL_TEMPLATE_PARM_P (arg))
24703 return true;
24704 /* A member template of a dependent class is not necessarily
24705 type-dependent, but it is a dependent template argument because it
24706 will be a member of an unknown specialization to that template. */
24707 tree scope = CP_DECL_CONTEXT (arg);
24708 return TYPE_P (scope) && dependent_type_p (scope);
24710 else if (ARGUMENT_PACK_P (arg))
24712 tree args = ARGUMENT_PACK_ARGS (arg);
24713 int i, len = TREE_VEC_LENGTH (args);
24714 for (i = 0; i < len; ++i)
24716 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24717 return true;
24720 return false;
24722 else if (TYPE_P (arg))
24723 return dependent_type_p (arg);
24724 else
24725 return (type_dependent_expression_p (arg)
24726 || value_dependent_expression_p (arg));
24729 /* Returns true if ARGS (a collection of template arguments) contains
24730 any types that require structural equality testing. */
24732 bool
24733 any_template_arguments_need_structural_equality_p (tree args)
24735 int i;
24736 int j;
24738 if (!args)
24739 return false;
24740 if (args == error_mark_node)
24741 return true;
24743 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24745 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24746 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24748 tree arg = TREE_VEC_ELT (level, j);
24749 tree packed_args = NULL_TREE;
24750 int k, len = 1;
24752 if (ARGUMENT_PACK_P (arg))
24754 /* Look inside the argument pack. */
24755 packed_args = ARGUMENT_PACK_ARGS (arg);
24756 len = TREE_VEC_LENGTH (packed_args);
24759 for (k = 0; k < len; ++k)
24761 if (packed_args)
24762 arg = TREE_VEC_ELT (packed_args, k);
24764 if (error_operand_p (arg))
24765 return true;
24766 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24767 continue;
24768 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24769 return true;
24770 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24771 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24772 return true;
24777 return false;
24780 /* Returns true if ARGS (a collection of template arguments) contains
24781 any dependent arguments. */
24783 bool
24784 any_dependent_template_arguments_p (const_tree args)
24786 int i;
24787 int j;
24789 if (!args)
24790 return false;
24791 if (args == error_mark_node)
24792 return true;
24794 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24796 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24797 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24798 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24799 return true;
24802 return false;
24805 /* Returns TRUE if the template TMPL is type-dependent. */
24807 bool
24808 dependent_template_p (tree tmpl)
24810 if (TREE_CODE (tmpl) == OVERLOAD)
24812 for (lkp_iterator iter (tmpl); iter; ++iter)
24813 if (dependent_template_p (*iter))
24814 return true;
24815 return false;
24818 /* Template template parameters are dependent. */
24819 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24820 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24821 return true;
24822 /* So are names that have not been looked up. */
24823 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24824 return true;
24825 return false;
24828 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24830 bool
24831 dependent_template_id_p (tree tmpl, tree args)
24833 return (dependent_template_p (tmpl)
24834 || any_dependent_template_arguments_p (args));
24837 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24838 are dependent. */
24840 bool
24841 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24843 int i;
24845 if (!processing_template_decl)
24846 return false;
24848 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24850 tree decl = TREE_VEC_ELT (declv, i);
24851 tree init = TREE_VEC_ELT (initv, i);
24852 tree cond = TREE_VEC_ELT (condv, i);
24853 tree incr = TREE_VEC_ELT (incrv, i);
24855 if (type_dependent_expression_p (decl)
24856 || TREE_CODE (decl) == SCOPE_REF)
24857 return true;
24859 if (init && type_dependent_expression_p (init))
24860 return true;
24862 if (type_dependent_expression_p (cond))
24863 return true;
24865 if (COMPARISON_CLASS_P (cond)
24866 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24867 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24868 return true;
24870 if (TREE_CODE (incr) == MODOP_EXPR)
24872 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24873 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24874 return true;
24876 else if (type_dependent_expression_p (incr))
24877 return true;
24878 else if (TREE_CODE (incr) == MODIFY_EXPR)
24880 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24881 return true;
24882 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24884 tree t = TREE_OPERAND (incr, 1);
24885 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24886 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24887 return true;
24892 return false;
24895 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24896 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24897 no such TYPE can be found. Note that this function peers inside
24898 uninstantiated templates and therefore should be used only in
24899 extremely limited situations. ONLY_CURRENT_P restricts this
24900 peering to the currently open classes hierarchy (which is required
24901 when comparing types). */
24903 tree
24904 resolve_typename_type (tree type, bool only_current_p)
24906 tree scope;
24907 tree name;
24908 tree decl;
24909 int quals;
24910 tree pushed_scope;
24911 tree result;
24913 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24915 scope = TYPE_CONTEXT (type);
24916 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24917 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24918 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24919 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24920 identifier of the TYPENAME_TYPE anymore.
24921 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24922 TYPENAME_TYPE instead, we avoid messing up with a possible
24923 typedef variant case. */
24924 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24926 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24927 it first before we can figure out what NAME refers to. */
24928 if (TREE_CODE (scope) == TYPENAME_TYPE)
24930 if (TYPENAME_IS_RESOLVING_P (scope))
24931 /* Given a class template A with a dependent base with nested type C,
24932 typedef typename A::C::C C will land us here, as trying to resolve
24933 the initial A::C leads to the local C typedef, which leads back to
24934 A::C::C. So we break the recursion now. */
24935 return type;
24936 else
24937 scope = resolve_typename_type (scope, only_current_p);
24939 /* If we don't know what SCOPE refers to, then we cannot resolve the
24940 TYPENAME_TYPE. */
24941 if (!CLASS_TYPE_P (scope))
24942 return type;
24943 /* If this is a typedef, we don't want to look inside (c++/11987). */
24944 if (typedef_variant_p (type))
24945 return type;
24946 /* If SCOPE isn't the template itself, it will not have a valid
24947 TYPE_FIELDS list. */
24948 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24949 /* scope is either the template itself or a compatible instantiation
24950 like X<T>, so look up the name in the original template. */
24951 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24952 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24953 gcc_checking_assert (uses_template_parms (scope));
24954 /* If scope has no fields, it can't be a current instantiation. Check this
24955 before currently_open_class to avoid infinite recursion (71515). */
24956 if (!TYPE_FIELDS (scope))
24957 return type;
24958 /* If the SCOPE is not the current instantiation, there's no reason
24959 to look inside it. */
24960 if (only_current_p && !currently_open_class (scope))
24961 return type;
24962 /* Enter the SCOPE so that name lookup will be resolved as if we
24963 were in the class definition. In particular, SCOPE will no
24964 longer be considered a dependent type. */
24965 pushed_scope = push_scope (scope);
24966 /* Look up the declaration. */
24967 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24968 tf_warning_or_error);
24970 result = NULL_TREE;
24972 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24973 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24974 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24975 if (!decl)
24976 /*nop*/;
24977 else if (identifier_p (fullname)
24978 && TREE_CODE (decl) == TYPE_DECL)
24980 result = TREE_TYPE (decl);
24981 if (result == error_mark_node)
24982 result = NULL_TREE;
24984 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24985 && DECL_CLASS_TEMPLATE_P (decl))
24987 /* Obtain the template and the arguments. */
24988 tree tmpl = TREE_OPERAND (fullname, 0);
24989 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24991 /* We get here with a plain identifier because a previous tentative
24992 parse of the nested-name-specifier as part of a ptr-operator saw
24993 ::template X<A>. The use of ::template is necessary in a
24994 ptr-operator, but wrong in a declarator-id.
24996 [temp.names]: In a qualified-id of a declarator-id, the keyword
24997 template shall not appear at the top level. */
24998 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24999 "keyword %<template%> not allowed in declarator-id");
25000 tmpl = decl;
25002 tree args = TREE_OPERAND (fullname, 1);
25003 /* Instantiate the template. */
25004 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25005 /*entering_scope=*/true,
25006 tf_error | tf_user);
25007 if (result == error_mark_node)
25008 result = NULL_TREE;
25011 /* Leave the SCOPE. */
25012 if (pushed_scope)
25013 pop_scope (pushed_scope);
25015 /* If we failed to resolve it, return the original typename. */
25016 if (!result)
25017 return type;
25019 /* If lookup found a typename type, resolve that too. */
25020 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25022 /* Ill-formed programs can cause infinite recursion here, so we
25023 must catch that. */
25024 TYPENAME_IS_RESOLVING_P (result) = 1;
25025 result = resolve_typename_type (result, only_current_p);
25026 TYPENAME_IS_RESOLVING_P (result) = 0;
25029 /* Qualify the resulting type. */
25030 quals = cp_type_quals (type);
25031 if (quals)
25032 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25034 return result;
25037 /* EXPR is an expression which is not type-dependent. Return a proxy
25038 for EXPR that can be used to compute the types of larger
25039 expressions containing EXPR. */
25041 tree
25042 build_non_dependent_expr (tree expr)
25044 tree orig_expr = expr;
25045 tree inner_expr;
25047 /* When checking, try to get a constant value for all non-dependent
25048 expressions in order to expose bugs in *_dependent_expression_p
25049 and constexpr. This can affect code generation, see PR70704, so
25050 only do this for -fchecking=2. */
25051 if (flag_checking > 1
25052 && cxx_dialect >= cxx11
25053 /* Don't do this during nsdmi parsing as it can lead to
25054 unexpected recursive instantiations. */
25055 && !parsing_nsdmi ()
25056 /* Don't do this during concept expansion either and for
25057 the same reason. */
25058 && !expanding_concept ())
25059 fold_non_dependent_expr (expr);
25061 STRIP_ANY_LOCATION_WRAPPER (expr);
25063 /* Preserve OVERLOADs; the functions must be available to resolve
25064 types. */
25065 inner_expr = expr;
25066 if (TREE_CODE (inner_expr) == STMT_EXPR)
25067 inner_expr = stmt_expr_value_expr (inner_expr);
25068 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25069 inner_expr = TREE_OPERAND (inner_expr, 0);
25070 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25071 inner_expr = TREE_OPERAND (inner_expr, 1);
25072 if (is_overloaded_fn (inner_expr)
25073 || TREE_CODE (inner_expr) == OFFSET_REF)
25074 return orig_expr;
25075 /* There is no need to return a proxy for a variable. */
25076 if (VAR_P (expr))
25077 return orig_expr;
25078 /* Preserve string constants; conversions from string constants to
25079 "char *" are allowed, even though normally a "const char *"
25080 cannot be used to initialize a "char *". */
25081 if (TREE_CODE (expr) == STRING_CST)
25082 return orig_expr;
25083 /* Preserve void and arithmetic constants, as an optimization -- there is no
25084 reason to create a new node. */
25085 if (TREE_CODE (expr) == VOID_CST
25086 || TREE_CODE (expr) == INTEGER_CST
25087 || TREE_CODE (expr) == REAL_CST)
25088 return orig_expr;
25089 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25090 There is at least one place where we want to know that a
25091 particular expression is a throw-expression: when checking a ?:
25092 expression, there are special rules if the second or third
25093 argument is a throw-expression. */
25094 if (TREE_CODE (expr) == THROW_EXPR)
25095 return orig_expr;
25097 /* Don't wrap an initializer list, we need to be able to look inside. */
25098 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25099 return orig_expr;
25101 /* Don't wrap a dummy object, we need to be able to test for it. */
25102 if (is_dummy_object (expr))
25103 return orig_expr;
25105 if (TREE_CODE (expr) == COND_EXPR)
25106 return build3 (COND_EXPR,
25107 TREE_TYPE (expr),
25108 TREE_OPERAND (expr, 0),
25109 (TREE_OPERAND (expr, 1)
25110 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25111 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25112 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25113 if (TREE_CODE (expr) == COMPOUND_EXPR
25114 && !COMPOUND_EXPR_OVERLOADED (expr))
25115 return build2 (COMPOUND_EXPR,
25116 TREE_TYPE (expr),
25117 TREE_OPERAND (expr, 0),
25118 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25120 /* If the type is unknown, it can't really be non-dependent */
25121 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25123 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25124 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25127 /* ARGS is a vector of expressions as arguments to a function call.
25128 Replace the arguments with equivalent non-dependent expressions.
25129 This modifies ARGS in place. */
25131 void
25132 make_args_non_dependent (vec<tree, va_gc> *args)
25134 unsigned int ix;
25135 tree arg;
25137 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25139 tree newarg = build_non_dependent_expr (arg);
25140 if (newarg != arg)
25141 (*args)[ix] = newarg;
25145 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25146 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25147 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25149 static tree
25150 make_auto_1 (tree name, bool set_canonical)
25152 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25153 TYPE_NAME (au) = build_decl (input_location,
25154 TYPE_DECL, name, au);
25155 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25156 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25157 (0, processing_template_decl + 1, processing_template_decl + 1,
25158 TYPE_NAME (au), NULL_TREE);
25159 if (set_canonical)
25160 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25161 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25162 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25164 return au;
25167 tree
25168 make_decltype_auto (void)
25170 return make_auto_1 (decltype_auto_identifier, true);
25173 tree
25174 make_auto (void)
25176 return make_auto_1 (auto_identifier, true);
25179 /* Return a C++17 deduction placeholder for class template TMPL. */
25181 tree
25182 make_template_placeholder (tree tmpl)
25184 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25185 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25186 return t;
25189 /* True iff T is a C++17 class template deduction placeholder. */
25191 bool
25192 template_placeholder_p (tree t)
25194 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25197 /* Make a "constrained auto" type-specifier. This is an
25198 auto type with constraints that must be associated after
25199 deduction. The constraint is formed from the given
25200 CONC and its optional sequence of arguments, which are
25201 non-null if written as partial-concept-id. */
25203 tree
25204 make_constrained_auto (tree con, tree args)
25206 tree type = make_auto_1 (auto_identifier, false);
25208 /* Build the constraint. */
25209 tree tmpl = DECL_TI_TEMPLATE (con);
25210 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25211 expr = build_concept_check (expr, type, args);
25213 tree constr = normalize_expression (expr);
25214 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25216 /* Our canonical type depends on the constraint. */
25217 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25219 /* Attach the constraint to the type declaration. */
25220 tree decl = TYPE_NAME (type);
25221 return decl;
25224 /* Given type ARG, return std::initializer_list<ARG>. */
25226 static tree
25227 listify (tree arg)
25229 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25231 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25233 gcc_rich_location richloc (input_location);
25234 maybe_add_include_fixit (&richloc, "<initializer_list>");
25235 error_at (&richloc,
25236 "deducing from brace-enclosed initializer list"
25237 " requires %<#include <initializer_list>%>");
25239 return error_mark_node;
25241 tree argvec = make_tree_vec (1);
25242 TREE_VEC_ELT (argvec, 0) = arg;
25244 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25245 NULL_TREE, 0, tf_warning_or_error);
25248 /* Replace auto in TYPE with std::initializer_list<auto>. */
25250 static tree
25251 listify_autos (tree type, tree auto_node)
25253 tree init_auto = listify (auto_node);
25254 tree argvec = make_tree_vec (1);
25255 TREE_VEC_ELT (argvec, 0) = init_auto;
25256 if (processing_template_decl)
25257 argvec = add_to_template_args (current_template_args (), argvec);
25258 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25261 /* Hash traits for hashing possibly constrained 'auto'
25262 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25264 struct auto_hash : default_hash_traits<tree>
25266 static inline hashval_t hash (tree);
25267 static inline bool equal (tree, tree);
25270 /* Hash the 'auto' T. */
25272 inline hashval_t
25273 auto_hash::hash (tree t)
25275 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25276 /* Matching constrained-type-specifiers denote the same template
25277 parameter, so hash the constraint. */
25278 return hash_placeholder_constraint (c);
25279 else
25280 /* But unconstrained autos are all separate, so just hash the pointer. */
25281 return iterative_hash_object (t, 0);
25284 /* Compare two 'auto's. */
25286 inline bool
25287 auto_hash::equal (tree t1, tree t2)
25289 if (t1 == t2)
25290 return true;
25292 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25293 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25295 /* Two unconstrained autos are distinct. */
25296 if (!c1 || !c2)
25297 return false;
25299 return equivalent_placeholder_constraints (c1, c2);
25302 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25303 constrained) auto, add it to the vector. */
25305 static int
25306 extract_autos_r (tree t, void *data)
25308 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25309 if (is_auto (t))
25311 /* All the autos were built with index 0; fix that up now. */
25312 tree *p = hash.find_slot (t, INSERT);
25313 unsigned idx;
25314 if (*p)
25315 /* If this is a repeated constrained-type-specifier, use the index we
25316 chose before. */
25317 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25318 else
25320 /* Otherwise this is new, so use the current count. */
25321 *p = t;
25322 idx = hash.elements () - 1;
25324 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25327 /* Always keep walking. */
25328 return 0;
25331 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25332 says they can appear anywhere in the type. */
25334 static tree
25335 extract_autos (tree type)
25337 hash_set<tree> visited;
25338 hash_table<auto_hash> hash (2);
25340 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25342 tree tree_vec = make_tree_vec (hash.elements());
25343 for (hash_table<auto_hash>::iterator iter = hash.begin();
25344 iter != hash.end(); ++iter)
25346 tree elt = *iter;
25347 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25348 TREE_VEC_ELT (tree_vec, i)
25349 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25352 return tree_vec;
25355 /* The stem for deduction guide names. */
25356 const char *const dguide_base = "__dguide_";
25358 /* Return the name for a deduction guide for class template TMPL. */
25360 tree
25361 dguide_name (tree tmpl)
25363 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25364 tree tname = TYPE_IDENTIFIER (type);
25365 char *buf = (char *) alloca (1 + strlen (dguide_base)
25366 + IDENTIFIER_LENGTH (tname));
25367 memcpy (buf, dguide_base, strlen (dguide_base));
25368 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25369 IDENTIFIER_LENGTH (tname) + 1);
25370 tree dname = get_identifier (buf);
25371 TREE_TYPE (dname) = type;
25372 return dname;
25375 /* True if NAME is the name of a deduction guide. */
25377 bool
25378 dguide_name_p (tree name)
25380 return (TREE_TYPE (name)
25381 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25382 strlen (dguide_base)));
25385 /* True if FN is a deduction guide. */
25387 bool
25388 deduction_guide_p (const_tree fn)
25390 if (DECL_P (fn))
25391 if (tree name = DECL_NAME (fn))
25392 return dguide_name_p (name);
25393 return false;
25396 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25398 bool
25399 copy_guide_p (const_tree fn)
25401 gcc_assert (deduction_guide_p (fn));
25402 if (!DECL_ARTIFICIAL (fn))
25403 return false;
25404 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25405 return (TREE_CHAIN (parms) == void_list_node
25406 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25409 /* True if FN is a guide generated from a constructor template. */
25411 bool
25412 template_guide_p (const_tree fn)
25414 gcc_assert (deduction_guide_p (fn));
25415 if (!DECL_ARTIFICIAL (fn))
25416 return false;
25417 tree tmpl = DECL_TI_TEMPLATE (fn);
25418 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25419 return PRIMARY_TEMPLATE_P (org);
25420 return false;
25423 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25424 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25425 template parameter types. Note that the handling of template template
25426 parameters relies on current_template_parms being set appropriately for the
25427 new template. */
25429 static tree
25430 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25431 tree tsubst_args, tsubst_flags_t complain)
25433 tree oldidx = get_template_parm_index (olddecl);
25435 tree newtype;
25436 if (TREE_CODE (olddecl) == TYPE_DECL
25437 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25439 tree oldtype = TREE_TYPE (olddecl);
25440 newtype = cxx_make_type (TREE_CODE (oldtype));
25441 TYPE_MAIN_VARIANT (newtype) = newtype;
25442 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25443 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25444 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25446 else
25447 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25448 complain, NULL_TREE);
25450 tree newdecl
25451 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25452 DECL_NAME (olddecl), newtype);
25453 SET_DECL_TEMPLATE_PARM_P (newdecl);
25455 tree newidx;
25456 if (TREE_CODE (olddecl) == TYPE_DECL
25457 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25459 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25460 = build_template_parm_index (index, level, level,
25461 newdecl, newtype);
25462 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25463 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25464 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25465 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25467 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25469 DECL_TEMPLATE_RESULT (newdecl)
25470 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25471 DECL_NAME (olddecl), newtype);
25472 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25473 // First create a copy (ttargs) of tsubst_args with an
25474 // additional level for the template template parameter's own
25475 // template parameters (ttparms).
25476 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25477 (DECL_TEMPLATE_PARMS (olddecl)));
25478 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25479 tree ttargs = make_tree_vec (depth + 1);
25480 for (int i = 0; i < depth; ++i)
25481 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25482 TREE_VEC_ELT (ttargs, depth)
25483 = template_parms_level_to_args (ttparms);
25484 // Substitute ttargs into ttparms to fix references to
25485 // other template parameters.
25486 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25487 complain);
25488 // Now substitute again with args based on tparms, to reduce
25489 // the level of the ttparms.
25490 ttargs = current_template_args ();
25491 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25492 complain);
25493 // Finally, tack the adjusted parms onto tparms.
25494 ttparms = tree_cons (size_int (depth), ttparms,
25495 current_template_parms);
25496 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25499 else
25501 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25502 tree newconst
25503 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25504 TREE_CODE (oldconst),
25505 DECL_NAME (oldconst), newtype);
25506 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25507 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25508 SET_DECL_TEMPLATE_PARM_P (newconst);
25509 newidx = build_template_parm_index (index, level, level,
25510 newconst, newtype);
25511 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25512 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25513 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25516 return newdecl;
25519 /* Returns a C++17 class deduction guide template based on the constructor
25520 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25521 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25523 static tree
25524 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25526 tree type, tparms, targs, fparms, fargs, ci;
25527 bool memtmpl = false;
25528 bool explicit_p;
25529 location_t loc;
25530 tree fn_tmpl = NULL_TREE;
25532 if (TYPE_P (ctor))
25534 type = ctor;
25535 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25536 if (copy_p)
25538 type = TREE_TYPE (type);
25539 fparms = tree_cons (NULL_TREE, type, void_list_node);
25541 else
25542 fparms = void_list_node;
25544 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25545 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25546 targs = CLASSTYPE_TI_ARGS (type);
25547 ci = NULL_TREE;
25548 fargs = NULL_TREE;
25549 loc = DECL_SOURCE_LOCATION (ctmpl);
25550 explicit_p = false;
25552 else
25554 ++processing_template_decl;
25556 fn_tmpl
25557 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25558 : DECL_TI_TEMPLATE (ctor));
25559 if (outer_args)
25560 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25561 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25563 type = DECL_CONTEXT (ctor);
25565 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25566 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25567 fully specialized args for the enclosing class. Strip those off, as
25568 the deduction guide won't have those template parameters. */
25569 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25570 TMPL_PARMS_DEPTH (tparms));
25571 /* Discard the 'this' parameter. */
25572 fparms = FUNCTION_ARG_CHAIN (ctor);
25573 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25574 ci = get_constraints (ctor);
25575 loc = DECL_SOURCE_LOCATION (ctor);
25576 explicit_p = DECL_NONCONVERTING_P (ctor);
25578 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25580 memtmpl = true;
25582 /* For a member template constructor, we need to flatten the two
25583 template parameter lists into one, and then adjust the function
25584 signature accordingly. This gets...complicated. */
25585 tree save_parms = current_template_parms;
25587 /* For a member template we should have two levels of parms/args, one
25588 for the class and one for the constructor. We stripped
25589 specialized args for further enclosing classes above. */
25590 const int depth = 2;
25591 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25593 /* Template args for translating references to the two-level template
25594 parameters into references to the one-level template parameters we
25595 are creating. */
25596 tree tsubst_args = copy_node (targs);
25597 TMPL_ARGS_LEVEL (tsubst_args, depth)
25598 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25600 /* Template parms for the constructor template. */
25601 tree ftparms = TREE_VALUE (tparms);
25602 unsigned flen = TREE_VEC_LENGTH (ftparms);
25603 /* Template parms for the class template. */
25604 tparms = TREE_CHAIN (tparms);
25605 tree ctparms = TREE_VALUE (tparms);
25606 unsigned clen = TREE_VEC_LENGTH (ctparms);
25607 /* Template parms for the deduction guide start as a copy of the
25608 template parms for the class. We set current_template_parms for
25609 lookup_template_class_1. */
25610 current_template_parms = tparms = copy_node (tparms);
25611 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25612 for (unsigned i = 0; i < clen; ++i)
25613 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25615 /* Now we need to rewrite the constructor parms to append them to the
25616 class parms. */
25617 for (unsigned i = 0; i < flen; ++i)
25619 unsigned index = i + clen;
25620 unsigned level = 1;
25621 tree oldelt = TREE_VEC_ELT (ftparms, i);
25622 tree olddecl = TREE_VALUE (oldelt);
25623 tree newdecl = rewrite_template_parm (olddecl, index, level,
25624 tsubst_args, complain);
25625 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25626 tsubst_args, complain, ctor);
25627 tree list = build_tree_list (newdef, newdecl);
25628 TEMPLATE_PARM_CONSTRAINTS (list)
25629 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25630 tsubst_args, complain, ctor);
25631 TREE_VEC_ELT (new_vec, index) = list;
25632 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25635 /* Now we have a final set of template parms to substitute into the
25636 function signature. */
25637 targs = template_parms_to_args (tparms);
25638 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25639 complain, ctor);
25640 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25641 if (ci)
25642 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25644 current_template_parms = save_parms;
25646 --processing_template_decl;
25649 if (!memtmpl)
25651 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25652 tparms = copy_node (tparms);
25653 INNERMOST_TEMPLATE_PARMS (tparms)
25654 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25657 tree fntype = build_function_type (type, fparms);
25658 tree ded_fn = build_lang_decl_loc (loc,
25659 FUNCTION_DECL,
25660 dguide_name (type), fntype);
25661 DECL_ARGUMENTS (ded_fn) = fargs;
25662 DECL_ARTIFICIAL (ded_fn) = true;
25663 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25664 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25665 DECL_ARTIFICIAL (ded_tmpl) = true;
25666 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25667 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25668 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25669 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25670 if (DECL_P (ctor))
25671 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25672 if (ci)
25673 set_constraints (ded_tmpl, ci);
25675 return ded_tmpl;
25678 /* Deduce template arguments for the class template placeholder PTYPE for
25679 template TMPL based on the initializer INIT, and return the resulting
25680 type. */
25682 static tree
25683 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25684 tsubst_flags_t complain)
25686 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25688 /* We should have handled this in the caller. */
25689 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25690 return ptype;
25691 if (complain & tf_error)
25692 error ("non-class template %qT used without template arguments", tmpl);
25693 return error_mark_node;
25696 tree type = TREE_TYPE (tmpl);
25698 bool try_list_ctor = false;
25700 vec<tree,va_gc> *args;
25701 if (init == NULL_TREE
25702 || TREE_CODE (init) == TREE_LIST)
25703 args = make_tree_vector_from_list (init);
25704 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25706 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25707 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25709 /* As an exception, the first phase in 16.3.1.7 (considering the
25710 initializer list as a single argument) is omitted if the
25711 initializer list consists of a single expression of type cv U,
25712 where U is a specialization of C or a class derived from a
25713 specialization of C. */
25714 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25715 tree etype = TREE_TYPE (elt);
25717 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25718 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25719 int err = unify (tparms, targs, type, etype,
25720 UNIFY_ALLOW_DERIVED, /*explain*/false);
25721 if (err == 0)
25722 try_list_ctor = false;
25723 ggc_free (targs);
25725 if (try_list_ctor || is_std_init_list (type))
25726 args = make_tree_vector_single (init);
25727 else
25728 args = make_tree_vector_from_ctor (init);
25730 else
25731 args = make_tree_vector_single (init);
25733 tree dname = dguide_name (tmpl);
25734 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25735 /*type*/false, /*complain*/false,
25736 /*hidden*/false);
25737 bool elided = false;
25738 if (cands == error_mark_node)
25739 cands = NULL_TREE;
25741 /* Prune explicit deduction guides in copy-initialization context. */
25742 if (flags & LOOKUP_ONLYCONVERTING)
25744 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25745 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25746 elided = true;
25748 if (elided)
25750 /* Found a nonconverting guide, prune the candidates. */
25751 tree pruned = NULL_TREE;
25752 for (lkp_iterator iter (cands); iter; ++iter)
25753 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25754 pruned = lookup_add (*iter, pruned);
25756 cands = pruned;
25760 tree outer_args = NULL_TREE;
25761 if (DECL_CLASS_SCOPE_P (tmpl)
25762 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25764 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25765 type = TREE_TYPE (most_general_template (tmpl));
25768 bool saw_ctor = false;
25769 // FIXME cache artificial deduction guides
25770 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25772 tree guide = build_deduction_guide (*iter, outer_args, complain);
25773 if ((flags & LOOKUP_ONLYCONVERTING)
25774 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25775 elided = true;
25776 else
25777 cands = lookup_add (guide, cands);
25779 saw_ctor = true;
25782 tree call = error_mark_node;
25784 /* If this is list-initialization and the class has a list constructor, first
25785 try deducing from the list as a single argument, as [over.match.list]. */
25786 tree list_cands = NULL_TREE;
25787 if (try_list_ctor && cands)
25788 for (lkp_iterator iter (cands); iter; ++iter)
25790 tree dg = *iter;
25791 if (is_list_ctor (dg))
25792 list_cands = lookup_add (dg, list_cands);
25794 if (list_cands)
25796 ++cp_unevaluated_operand;
25797 call = build_new_function_call (list_cands, &args, tf_decltype);
25798 --cp_unevaluated_operand;
25800 if (call == error_mark_node)
25802 /* That didn't work, now try treating the list as a sequence of
25803 arguments. */
25804 release_tree_vector (args);
25805 args = make_tree_vector_from_ctor (init);
25809 /* Maybe generate an implicit deduction guide. */
25810 if (call == error_mark_node && args->length () < 2)
25812 tree gtype = NULL_TREE;
25814 if (args->length () == 1)
25815 /* Generate a copy guide. */
25816 gtype = build_reference_type (type);
25817 else if (!saw_ctor)
25818 /* Generate a default guide. */
25819 gtype = type;
25821 if (gtype)
25823 tree guide = build_deduction_guide (gtype, outer_args, complain);
25824 cands = lookup_add (guide, cands);
25828 if (elided && !cands)
25830 error ("cannot deduce template arguments for copy-initialization"
25831 " of %qT, as it has no non-explicit deduction guides or "
25832 "user-declared constructors", type);
25833 return error_mark_node;
25835 else if (!cands && call == error_mark_node)
25837 error ("cannot deduce template arguments of %qT, as it has no viable "
25838 "deduction guides", type);
25839 return error_mark_node;
25842 if (call == error_mark_node)
25844 ++cp_unevaluated_operand;
25845 call = build_new_function_call (cands, &args, tf_decltype);
25846 --cp_unevaluated_operand;
25849 if (call == error_mark_node && (complain & tf_warning_or_error))
25851 error ("class template argument deduction failed:");
25853 ++cp_unevaluated_operand;
25854 call = build_new_function_call (cands, &args, complain | tf_decltype);
25855 --cp_unevaluated_operand;
25857 if (elided)
25858 inform (input_location, "explicit deduction guides not considered "
25859 "for copy-initialization");
25862 release_tree_vector (args);
25864 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25867 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25868 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25870 tree
25871 do_auto_deduction (tree type, tree init, tree auto_node)
25873 return do_auto_deduction (type, init, auto_node,
25874 tf_warning_or_error,
25875 adc_unspecified);
25878 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25879 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25880 The CONTEXT determines the context in which auto deduction is performed
25881 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25882 OUTER_TARGS are used during template argument deduction
25883 (context == adc_unify) to properly substitute the result, and is ignored
25884 in other contexts.
25886 For partial-concept-ids, extra args may be appended to the list of deduced
25887 template arguments prior to determining constraint satisfaction. */
25889 tree
25890 do_auto_deduction (tree type, tree init, tree auto_node,
25891 tsubst_flags_t complain, auto_deduction_context context,
25892 tree outer_targs, int flags)
25894 tree targs;
25896 if (init == error_mark_node)
25897 return error_mark_node;
25899 if (init && type_dependent_expression_p (init)
25900 && context != adc_unify)
25901 /* Defining a subset of type-dependent expressions that we can deduce
25902 from ahead of time isn't worth the trouble. */
25903 return type;
25905 /* Similarly, we can't deduce from another undeduced decl. */
25906 if (init && undeduced_auto_decl (init))
25907 return type;
25909 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25910 /* C++17 class template argument deduction. */
25911 return do_class_deduction (type, tmpl, init, flags, complain);
25913 if (TREE_TYPE (init) == NULL_TREE)
25914 /* Nothing we can do with this, even in deduction context. */
25915 return type;
25917 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25918 with either a new invented type template parameter U or, if the
25919 initializer is a braced-init-list (8.5.4), with
25920 std::initializer_list<U>. */
25921 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25923 if (!DIRECT_LIST_INIT_P (init))
25924 type = listify_autos (type, auto_node);
25925 else if (CONSTRUCTOR_NELTS (init) == 1)
25926 init = CONSTRUCTOR_ELT (init, 0)->value;
25927 else
25929 if (complain & tf_warning_or_error)
25931 if (permerror (input_location, "direct-list-initialization of "
25932 "%<auto%> requires exactly one element"))
25933 inform (input_location,
25934 "for deduction to %<std::initializer_list%>, use copy-"
25935 "list-initialization (i.e. add %<=%> before the %<{%>)");
25937 type = listify_autos (type, auto_node);
25941 if (type == error_mark_node)
25942 return error_mark_node;
25944 init = resolve_nondeduced_context (init, complain);
25946 if (context == adc_decomp_type
25947 && auto_node == type
25948 && init != error_mark_node
25949 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25950 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25951 and initializer has array type, deduce cv-qualified array type. */
25952 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25953 complain);
25954 else if (AUTO_IS_DECLTYPE (auto_node))
25956 bool id = (DECL_P (init)
25957 || ((TREE_CODE (init) == COMPONENT_REF
25958 || TREE_CODE (init) == SCOPE_REF)
25959 && !REF_PARENTHESIZED_P (init)));
25960 targs = make_tree_vec (1);
25961 TREE_VEC_ELT (targs, 0)
25962 = finish_decltype_type (init, id, tf_warning_or_error);
25963 if (type != auto_node)
25965 if (complain & tf_error)
25966 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25967 return error_mark_node;
25970 else
25972 tree parms = build_tree_list (NULL_TREE, type);
25973 tree tparms;
25975 if (flag_concepts)
25976 tparms = extract_autos (type);
25977 else
25979 tparms = make_tree_vec (1);
25980 TREE_VEC_ELT (tparms, 0)
25981 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25984 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25985 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25986 DEDUCE_CALL, LOOKUP_NORMAL,
25987 NULL, /*explain_p=*/false);
25988 if (val > 0)
25990 if (processing_template_decl)
25991 /* Try again at instantiation time. */
25992 return type;
25993 if (type && type != error_mark_node
25994 && (complain & tf_error))
25995 /* If type is error_mark_node a diagnostic must have been
25996 emitted by now. Also, having a mention to '<type error>'
25997 in the diagnostic is not really useful to the user. */
25999 if (cfun && auto_node == current_function_auto_return_pattern
26000 && LAMBDA_FUNCTION_P (current_function_decl))
26001 error ("unable to deduce lambda return type from %qE", init);
26002 else
26003 error ("unable to deduce %qT from %qE", type, init);
26004 type_unification_real (tparms, targs, parms, &init, 1, 0,
26005 DEDUCE_CALL, LOOKUP_NORMAL,
26006 NULL, /*explain_p=*/true);
26008 return error_mark_node;
26012 /* Check any placeholder constraints against the deduced type. */
26013 if (flag_concepts && !processing_template_decl)
26014 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26016 /* Use the deduced type to check the associated constraints. If we
26017 have a partial-concept-id, rebuild the argument list so that
26018 we check using the extra arguments. */
26019 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26020 tree cargs = CHECK_CONSTR_ARGS (constr);
26021 if (TREE_VEC_LENGTH (cargs) > 1)
26023 cargs = copy_node (cargs);
26024 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26026 else
26027 cargs = targs;
26028 if (!constraints_satisfied_p (constr, cargs))
26030 if (complain & tf_warning_or_error)
26032 switch (context)
26034 case adc_unspecified:
26035 case adc_unify:
26036 error("placeholder constraints not satisfied");
26037 break;
26038 case adc_variable_type:
26039 case adc_decomp_type:
26040 error ("deduced initializer does not satisfy "
26041 "placeholder constraints");
26042 break;
26043 case adc_return_type:
26044 error ("deduced return type does not satisfy "
26045 "placeholder constraints");
26046 break;
26047 case adc_requirement:
26048 error ("deduced expression type does not satisfy "
26049 "placeholder constraints");
26050 break;
26052 diagnose_constraints (input_location, constr, targs);
26054 return error_mark_node;
26058 if (processing_template_decl && context != adc_unify)
26059 outer_targs = current_template_args ();
26060 targs = add_to_template_args (outer_targs, targs);
26061 return tsubst (type, targs, complain, NULL_TREE);
26064 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26065 result. */
26067 tree
26068 splice_late_return_type (tree type, tree late_return_type)
26070 if (is_auto (type))
26072 if (late_return_type)
26073 return late_return_type;
26075 tree idx = get_template_parm_index (type);
26076 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26077 /* In an abbreviated function template we didn't know we were dealing
26078 with a function template when we saw the auto return type, so update
26079 it to have the correct level. */
26080 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26082 return type;
26085 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26086 'decltype(auto)' or a deduced class template. */
26088 bool
26089 is_auto (const_tree type)
26091 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26092 && (TYPE_IDENTIFIER (type) == auto_identifier
26093 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26094 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26095 return true;
26096 else
26097 return false;
26100 /* for_each_template_parm callback for type_uses_auto. */
26103 is_auto_r (tree tp, void */*data*/)
26105 return is_auto (tp);
26108 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26109 a use of `auto'. Returns NULL_TREE otherwise. */
26111 tree
26112 type_uses_auto (tree type)
26114 if (type == NULL_TREE)
26115 return NULL_TREE;
26116 else if (flag_concepts)
26118 /* The Concepts TS allows multiple autos in one type-specifier; just
26119 return the first one we find, do_auto_deduction will collect all of
26120 them. */
26121 if (uses_template_parms (type))
26122 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26123 /*visited*/NULL, /*nondeduced*/true);
26124 else
26125 return NULL_TREE;
26127 else
26128 return find_type_usage (type, is_auto);
26131 /* For a given template T, return the vector of typedefs referenced
26132 in T for which access check is needed at T instantiation time.
26133 T is either a FUNCTION_DECL or a RECORD_TYPE.
26134 Those typedefs were added to T by the function
26135 append_type_to_template_for_access_check. */
26137 vec<qualified_typedef_usage_t, va_gc> *
26138 get_types_needing_access_check (tree t)
26140 tree ti;
26141 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26143 if (!t || t == error_mark_node)
26144 return NULL;
26146 if (!(ti = get_template_info (t)))
26147 return NULL;
26149 if (CLASS_TYPE_P (t)
26150 || TREE_CODE (t) == FUNCTION_DECL)
26152 if (!TI_TEMPLATE (ti))
26153 return NULL;
26155 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26158 return result;
26161 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26162 tied to T. That list of typedefs will be access checked at
26163 T instantiation time.
26164 T is either a FUNCTION_DECL or a RECORD_TYPE.
26165 TYPE_DECL is a TYPE_DECL node representing a typedef.
26166 SCOPE is the scope through which TYPE_DECL is accessed.
26167 LOCATION is the location of the usage point of TYPE_DECL.
26169 This function is a subroutine of
26170 append_type_to_template_for_access_check. */
26172 static void
26173 append_type_to_template_for_access_check_1 (tree t,
26174 tree type_decl,
26175 tree scope,
26176 location_t location)
26178 qualified_typedef_usage_t typedef_usage;
26179 tree ti;
26181 if (!t || t == error_mark_node)
26182 return;
26184 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26185 || CLASS_TYPE_P (t))
26186 && type_decl
26187 && TREE_CODE (type_decl) == TYPE_DECL
26188 && scope);
26190 if (!(ti = get_template_info (t)))
26191 return;
26193 gcc_assert (TI_TEMPLATE (ti));
26195 typedef_usage.typedef_decl = type_decl;
26196 typedef_usage.context = scope;
26197 typedef_usage.locus = location;
26199 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26202 /* Append TYPE_DECL to the template TEMPL.
26203 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26204 At TEMPL instanciation time, TYPE_DECL will be checked to see
26205 if it can be accessed through SCOPE.
26206 LOCATION is the location of the usage point of TYPE_DECL.
26208 e.g. consider the following code snippet:
26210 class C
26212 typedef int myint;
26215 template<class U> struct S
26217 C::myint mi; // <-- usage point of the typedef C::myint
26220 S<char> s;
26222 At S<char> instantiation time, we need to check the access of C::myint
26223 In other words, we need to check the access of the myint typedef through
26224 the C scope. For that purpose, this function will add the myint typedef
26225 and the scope C through which its being accessed to a list of typedefs
26226 tied to the template S. That list will be walked at template instantiation
26227 time and access check performed on each typedefs it contains.
26228 Note that this particular code snippet should yield an error because
26229 myint is private to C. */
26231 void
26232 append_type_to_template_for_access_check (tree templ,
26233 tree type_decl,
26234 tree scope,
26235 location_t location)
26237 qualified_typedef_usage_t *iter;
26238 unsigned i;
26240 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26242 /* Make sure we don't append the type to the template twice. */
26243 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26244 if (iter->typedef_decl == type_decl && scope == iter->context)
26245 return;
26247 append_type_to_template_for_access_check_1 (templ, type_decl,
26248 scope, location);
26251 /* Convert the generic type parameters in PARM that match the types given in the
26252 range [START_IDX, END_IDX) from the current_template_parms into generic type
26253 packs. */
26255 tree
26256 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26258 tree current = current_template_parms;
26259 int depth = TMPL_PARMS_DEPTH (current);
26260 current = INNERMOST_TEMPLATE_PARMS (current);
26261 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26263 for (int i = 0; i < start_idx; ++i)
26264 TREE_VEC_ELT (replacement, i)
26265 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26267 for (int i = start_idx; i < end_idx; ++i)
26269 /* Create a distinct parameter pack type from the current parm and add it
26270 to the replacement args to tsubst below into the generic function
26271 parameter. */
26273 tree o = TREE_TYPE (TREE_VALUE
26274 (TREE_VEC_ELT (current, i)));
26275 tree t = copy_type (o);
26276 TEMPLATE_TYPE_PARM_INDEX (t)
26277 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26278 o, 0, 0, tf_none);
26279 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26280 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26281 TYPE_MAIN_VARIANT (t) = t;
26282 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26283 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26284 TREE_VEC_ELT (replacement, i) = t;
26285 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26288 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26289 TREE_VEC_ELT (replacement, i)
26290 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26292 /* If there are more levels then build up the replacement with the outer
26293 template parms. */
26294 if (depth > 1)
26295 replacement = add_to_template_args (template_parms_to_args
26296 (TREE_CHAIN (current_template_parms)),
26297 replacement);
26299 return tsubst (parm, replacement, tf_none, NULL_TREE);
26302 /* Entries in the decl_constraint hash table. */
26303 struct GTY((for_user)) constr_entry
26305 tree decl;
26306 tree ci;
26309 /* Hashing function and equality for constraint entries. */
26310 struct constr_hasher : ggc_ptr_hash<constr_entry>
26312 static hashval_t hash (constr_entry *e)
26314 return (hashval_t)DECL_UID (e->decl);
26317 static bool equal (constr_entry *e1, constr_entry *e2)
26319 return e1->decl == e2->decl;
26323 /* A mapping from declarations to constraint information. Note that
26324 both templates and their underlying declarations are mapped to the
26325 same constraint information.
26327 FIXME: This is defined in pt.c because garbage collection
26328 code is not being generated for constraint.cc. */
26330 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26332 /* Returns the template constraints of declaration T. If T is not
26333 constrained, return NULL_TREE. Note that T must be non-null. */
26335 tree
26336 get_constraints (tree t)
26338 if (!flag_concepts)
26339 return NULL_TREE;
26341 gcc_assert (DECL_P (t));
26342 if (TREE_CODE (t) == TEMPLATE_DECL)
26343 t = DECL_TEMPLATE_RESULT (t);
26344 constr_entry elt = { t, NULL_TREE };
26345 constr_entry* found = decl_constraints->find (&elt);
26346 if (found)
26347 return found->ci;
26348 else
26349 return NULL_TREE;
26352 /* Associate the given constraint information CI with the declaration
26353 T. If T is a template, then the constraints are associated with
26354 its underlying declaration. Don't build associations if CI is
26355 NULL_TREE. */
26357 void
26358 set_constraints (tree t, tree ci)
26360 if (!ci)
26361 return;
26362 gcc_assert (t && flag_concepts);
26363 if (TREE_CODE (t) == TEMPLATE_DECL)
26364 t = DECL_TEMPLATE_RESULT (t);
26365 gcc_assert (!get_constraints (t));
26366 constr_entry elt = {t, ci};
26367 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26368 constr_entry* entry = ggc_alloc<constr_entry> ();
26369 *entry = elt;
26370 *slot = entry;
26373 /* Remove the associated constraints of the declaration T. */
26375 void
26376 remove_constraints (tree t)
26378 gcc_assert (DECL_P (t));
26379 if (TREE_CODE (t) == TEMPLATE_DECL)
26380 t = DECL_TEMPLATE_RESULT (t);
26382 constr_entry elt = {t, NULL_TREE};
26383 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26384 if (slot)
26385 decl_constraints->clear_slot (slot);
26388 /* Memoized satisfaction results for declarations. This
26389 maps the pair (constraint_info, arguments) to the result computed
26390 by constraints_satisfied_p. */
26392 struct GTY((for_user)) constraint_sat_entry
26394 tree ci;
26395 tree args;
26396 tree result;
26399 /* Hashing function and equality for constraint entries. */
26401 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26403 static hashval_t hash (constraint_sat_entry *e)
26405 hashval_t val = iterative_hash_object(e->ci, 0);
26406 return iterative_hash_template_arg (e->args, val);
26409 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26411 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26415 /* Memoized satisfaction results for concept checks. */
26417 struct GTY((for_user)) concept_spec_entry
26419 tree tmpl;
26420 tree args;
26421 tree result;
26424 /* Hashing function and equality for constraint entries. */
26426 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26428 static hashval_t hash (concept_spec_entry *e)
26430 return hash_tmpl_and_args (e->tmpl, e->args);
26433 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26435 ++comparing_specializations;
26436 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26437 --comparing_specializations;
26438 return eq;
26442 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26443 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26445 /* Search for a memoized satisfaction result. Returns one of the
26446 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26448 tree
26449 lookup_constraint_satisfaction (tree ci, tree args)
26451 constraint_sat_entry elt = { ci, args, NULL_TREE };
26452 constraint_sat_entry* found = constraint_memos->find (&elt);
26453 if (found)
26454 return found->result;
26455 else
26456 return NULL_TREE;
26459 /* Memoize the result of a satisfication test. Returns the saved result. */
26461 tree
26462 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26464 constraint_sat_entry elt = {ci, args, result};
26465 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26466 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26467 *entry = elt;
26468 *slot = entry;
26469 return result;
26472 /* Search for a memoized satisfaction result for a concept. */
26474 tree
26475 lookup_concept_satisfaction (tree tmpl, tree args)
26477 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26478 concept_spec_entry* found = concept_memos->find (&elt);
26479 if (found)
26480 return found->result;
26481 else
26482 return NULL_TREE;
26485 /* Memoize the result of a concept check. Returns the saved result. */
26487 tree
26488 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26490 concept_spec_entry elt = {tmpl, args, result};
26491 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26492 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26493 *entry = elt;
26494 *slot = entry;
26495 return result;
26498 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26500 /* Returns a prior concept specialization. This returns the substituted
26501 and normalized constraints defined by the concept. */
26503 tree
26504 get_concept_expansion (tree tmpl, tree args)
26506 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26507 concept_spec_entry* found = concept_expansions->find (&elt);
26508 if (found)
26509 return found->result;
26510 else
26511 return NULL_TREE;
26514 /* Save a concept expansion for later. */
26516 tree
26517 save_concept_expansion (tree tmpl, tree args, tree def)
26519 concept_spec_entry elt = {tmpl, args, def};
26520 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26521 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26522 *entry = elt;
26523 *slot = entry;
26524 return def;
26527 static hashval_t
26528 hash_subsumption_args (tree t1, tree t2)
26530 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26531 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26532 int val = 0;
26533 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26534 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26535 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26536 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26537 return val;
26540 /* Compare the constraints of two subsumption entries. The LEFT1 and
26541 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26542 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26544 static bool
26545 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26547 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26548 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26549 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26550 CHECK_CONSTR_ARGS (right1)))
26551 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26552 CHECK_CONSTR_ARGS (right2));
26553 return false;
26556 /* Key/value pair for learning and memoizing subsumption results. This
26557 associates a pair of check constraints (including arguments) with
26558 a boolean value indicating the result. */
26560 struct GTY((for_user)) subsumption_entry
26562 tree t1;
26563 tree t2;
26564 bool result;
26567 /* Hashing function and equality for constraint entries. */
26569 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26571 static hashval_t hash (subsumption_entry *e)
26573 return hash_subsumption_args (e->t1, e->t2);
26576 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26578 ++comparing_specializations;
26579 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26580 --comparing_specializations;
26581 return eq;
26585 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26587 /* Search for a previously cached subsumption result. */
26589 bool*
26590 lookup_subsumption_result (tree t1, tree t2)
26592 subsumption_entry elt = { t1, t2, false };
26593 subsumption_entry* found = subsumption_table->find (&elt);
26594 if (found)
26595 return &found->result;
26596 else
26597 return 0;
26600 /* Save a subsumption result. */
26602 bool
26603 save_subsumption_result (tree t1, tree t2, bool result)
26605 subsumption_entry elt = {t1, t2, result};
26606 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26607 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26608 *entry = elt;
26609 *slot = entry;
26610 return result;
26613 /* Set up the hash table for constraint association. */
26615 void
26616 init_constraint_processing (void)
26618 if (!flag_concepts)
26619 return;
26621 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26622 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26623 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26624 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26625 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26628 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26629 0..N-1. */
26631 void
26632 declare_integer_pack (void)
26634 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26635 build_function_type_list (integer_type_node,
26636 integer_type_node,
26637 NULL_TREE),
26638 NULL_TREE, ECF_CONST);
26639 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26640 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26643 /* Set up the hash tables for template instantiations. */
26645 void
26646 init_template_processing (void)
26648 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26649 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26651 if (cxx_dialect >= cxx11)
26652 declare_integer_pack ();
26655 /* Print stats about the template hash tables for -fstats. */
26657 void
26658 print_template_statistics (void)
26660 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26661 "%f collisions\n", (long) decl_specializations->size (),
26662 (long) decl_specializations->elements (),
26663 decl_specializations->collisions ());
26664 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26665 "%f collisions\n", (long) type_specializations->size (),
26666 (long) type_specializations->elements (),
26667 type_specializations->collisions ());
26670 #if CHECKING_P
26672 namespace selftest {
26674 /* Verify that build_non_dependent_expr () works, for various expressions,
26675 and that location wrappers don't affect the results. */
26677 static void
26678 test_build_non_dependent_expr ()
26680 location_t loc = BUILTINS_LOCATION;
26682 /* Verify constants, without and with location wrappers. */
26683 tree int_cst = build_int_cst (integer_type_node, 42);
26684 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26686 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26687 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26688 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26690 tree string_lit = build_string (4, "foo");
26691 TREE_TYPE (string_lit) = char_array_type_node;
26692 string_lit = fix_string_type (string_lit);
26693 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26695 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26696 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26697 ASSERT_EQ (wrapped_string_lit,
26698 build_non_dependent_expr (wrapped_string_lit));
26701 /* Verify that type_dependent_expression_p () works correctly, even
26702 in the presence of location wrapper nodes. */
26704 static void
26705 test_type_dependent_expression_p ()
26707 location_t loc = BUILTINS_LOCATION;
26709 tree name = get_identifier ("foo");
26711 /* If no templates are involved, nothing is type-dependent. */
26712 gcc_assert (!processing_template_decl);
26713 ASSERT_FALSE (type_dependent_expression_p (name));
26715 ++processing_template_decl;
26717 /* Within a template, an unresolved name is always type-dependent. */
26718 ASSERT_TRUE (type_dependent_expression_p (name));
26720 /* Ensure it copes with NULL_TREE and errors. */
26721 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26722 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26724 /* A USING_DECL in a template should be type-dependent, even if wrapped
26725 with a location wrapper (PR c++/83799). */
26726 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26727 TREE_TYPE (using_decl) = integer_type_node;
26728 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26729 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26730 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26731 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26733 --processing_template_decl;
26736 /* Run all of the selftests within this file. */
26738 void
26739 cp_pt_c_tests ()
26741 test_build_non_dependent_expr ();
26742 test_type_dependent_expression_p ();
26745 } // namespace selftest
26747 #endif /* #if CHECKING_P */
26749 #include "gt-cp-pt.h"