Fix more variadic capture issues.
[official-gcc.git] / gcc / cp / pt.c
blob02d448e99b67d1a866c064688d8b3485dd6a3aff
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
61 int processing_template_parmlist;
62 static int template_header_count;
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
67 static GTY(()) struct tinst_level *current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations;
93 local_specializations = saved;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
99 struct GTY((for_user)) spec_entry
101 tree tmpl;
102 tree args;
103 tree spec;
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225 static tree enclosing_instantiation_of (tree tctx);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
232 static void
233 push_access_scope (tree t)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
245 if (TREE_CODE (t) == FUNCTION_DECL)
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
256 static void
257 pop_access_scope (tree t)
259 if (TREE_CODE (t) == FUNCTION_DECL)
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
276 tree
277 finish_member_template_decl (tree decl)
279 if (decl == error_mark_node)
280 return error_mark_node;
282 gcc_assert (DECL_P (decl));
284 if (TREE_CODE (decl) == TYPE_DECL)
286 tree type;
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
299 return NULL_TREE;
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
310 else
311 return decl;
313 else
314 error ("invalid member template declaration %qD", decl);
316 return error_mark_node;
319 /* Create a template info node. */
321 tree
322 build_template_info (tree template_decl, tree template_args)
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
330 /* Return the template info node corresponding to T, whatever T is. */
332 tree
333 get_template_info (const_tree t)
335 tree tinfo = NULL_TREE;
337 if (!t || t == error_mark_node)
338 return NULL;
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
355 return tinfo;
358 /* Returns the template nesting level of the indicated class TYPE.
360 For example, in:
361 template <class T>
362 struct A
364 template <class U>
365 struct B {};
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
377 template_class_depth (tree type)
379 int depth;
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
383 tree tinfo = get_template_info (type);
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
397 return depth;
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
421 tree parms = TREE_VALUE (parmlist);
422 int i;
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
439 if (error_operand_p (parm))
440 continue;
442 gcc_assert (DECL_P (parm));
444 switch (TREE_CODE (parm))
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
456 default:
457 gcc_unreachable ();
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
466 void
467 maybe_begin_member_template_processing (tree decl)
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
473 if (nsdmi)
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
482 if (inline_needs_template_parms (decl, nsdmi))
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
489 --levels;
490 parms = TREE_CHAIN (parms);
493 push_inline_template_parms_recursive (parms, levels);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
501 /* Undo the effects of maybe_begin_member_template_processing. */
503 void
504 maybe_end_member_template_processing (void)
506 int i;
507 int last;
509 if (inline_parm_levels.length () == 0)
510 return;
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
524 static tree
525 add_to_template_args (tree args, tree extra_args)
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
544 return new_args;
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
557 tree new_args;
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
571 new_args = add_to_template_args (args, extra_args);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
576 return new_args;
579 /* Return the N levels of innermost template arguments from the ARGS. */
581 tree
582 get_innermost_template_args (tree args, int n)
584 tree new_args;
585 int extra_levels;
586 int i;
588 gcc_assert (n >= 0);
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
607 return new_args;
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
620 gcc_assert (n >= 0);
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
638 return new_args;
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
644 void
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
677 static bool
678 check_specialization_scope (void)
680 tree scope = current_scope ();
682 /* [temp.expl.spec]
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
697 /* [temp.expl.spec]
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
711 return true;
714 /* We've just seen template <>. */
716 bool
717 begin_specialization (void)
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
727 void
728 end_specialization (void)
730 finish_scope ();
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
737 void
738 reset_specialization (void)
740 processing_specialization = 0;
741 template_header_count = 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
747 static void
748 note_template_header (int specialization)
750 processing_specialization = specialization;
751 template_header_count++;
754 /* We're beginning an explicit instantiation. */
756 void
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
764 void
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
776 static bool
777 check_specialization_namespace (tree tmpl)
779 tree tpl_ns = decl_namespace_context (tmpl);
781 /* [tmpl.expl.spec]
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
811 static void
812 check_explicit_instantiation_namespace (tree spec)
814 tree ns;
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
835 // struct S;
837 // template<typename T>
838 // struct S<T*>;
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
879 if (type == TREE_TYPE (tmpl))
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
916 return t;
919 return NULL_TREE;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
925 tree
926 maybe_process_partial_specialization (tree type)
928 tree context;
930 if (type == error_mark_node)
931 return error_mark_node;
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
945 context = TYPE_CONTEXT (type);
947 if (TYPE_ALIAS_P (type))
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t = maybe_new_partial_specialization (type))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1056 type_specializations->remove_elt (&elt);
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1083 else if (processing_specialization)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1097 return type;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1136 static void
1137 verify_unstripped_args (tree args)
1139 ++processing_template_decl;
1140 if (!any_dependent_template_arguments_p (args))
1142 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1153 else
1154 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 --processing_template_decl;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1174 static tree
1175 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1177 if (tmpl == NULL_TREE)
1178 return NULL_TREE;
1180 if (args == error_mark_node)
1181 return NULL_TREE;
1183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl) == FIELD_DECL);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args)
1189 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1191 : template_class_depth (DECL_CONTEXT (tmpl))));
1193 if (flag_checking)
1194 verify_unstripped_args (args);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl))
1199 return NULL_TREE;
1201 if (optimize_specialization_lookup_p (tmpl))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1205 arguments. */
1206 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1207 tree class_specialization
1208 = retrieve_specialization (class_template, args, 0);
1209 if (!class_specialization)
1210 return NULL_TREE;
1212 /* Find the instance of TMPL. */
1213 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1214 for (ovl_iterator iter (fns); iter; ++iter)
1216 tree fn = *iter;
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1223 return NULL_TREE;
1225 else
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1247 return NULL_TREE;
1250 /* Like retrieve_specialization, but for local declarations. */
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl, tree tmpl)
1267 tree t;
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1277 else
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1289 return 0;
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1298 bool need_template = true;
1299 int template_depth;
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1339 void f();
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1358 and DECL's are
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1372 while (current_depth < template_depth)
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1382 context = TYPE_CONTEXT (context);
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1412 else
1413 decl_type = TREE_TYPE (decl);
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1433 return compparms (decl_args_type, friend_args_type);
1435 else
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1471 return false;
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1548 else
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1595 check_specialization_namespace (tmpl);
1597 return fn;
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1602 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1613 return fn;
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1646 return spec;
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations;
1653 bool
1654 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1656 int equal;
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1673 --comparing_specializations;
1675 return equal;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1680 static hashval_t
1681 hash_tmpl_and_args (tree tmpl, tree args)
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1690 hashval_t
1691 spec_hasher::hash (spec_entry *e)
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1699 hashval_t
1700 iterative_hash_template_arg (tree arg, hashval_t val)
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1718 val = iterative_hash_object (code, val);
1720 switch (code)
1722 case ERROR_MARK:
1723 return val;
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1728 case TREE_VEC:
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1750 case OVERLOAD:
1751 for (lkp_iterator iter (arg); iter; ++iter)
1752 val = iterative_hash_template_arg (*iter, val);
1753 return val;
1755 case CONSTRUCTOR:
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1764 return val;
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1822 default:
1823 break;
1826 switch (tclass)
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1862 gcc_unreachable ();
1863 return 0;
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1872 bool
1873 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1875 spec_entry *entry;
1876 spec_entry elt;
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1891 return 0;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1897 void
1898 register_local_specialization (tree spec, tree tmpl)
1900 gcc_assert (tmpl != spec);
1901 local_specializations->put (tmpl, spec);
1904 /* TYPE is a class type. Returns true if TYPE is an explicitly
1905 specialized class. */
1907 bool
1908 explicit_class_specialization_p (tree type)
1910 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1911 return false;
1912 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1915 /* Print the list of functions at FNS, going through all the overloads
1916 for each element of the list. Alternatively, FNS can not be a
1917 TREE_LIST, in which case it will be printed together with all the
1918 overloads.
1920 MORE and *STR should respectively be FALSE and NULL when the function
1921 is called from the outside. They are used internally on recursive
1922 calls. print_candidates manages the two parameters and leaves NULL
1923 in *STR when it ends. */
1925 static void
1926 print_candidates_1 (tree fns, char **str, bool more = false)
1928 if (TREE_CODE (fns) == TREE_LIST)
1929 for (; fns; fns = TREE_CHAIN (fns))
1930 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1931 else
1932 for (lkp_iterator iter (fns); iter;)
1934 tree cand = *iter;
1935 ++iter;
1937 const char *pfx = *str;
1938 if (!pfx)
1940 if (more || iter)
1941 pfx = _("candidates are:");
1942 else
1943 pfx = _("candidate is:");
1944 *str = get_spaces (pfx);
1946 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1950 /* Print the list of candidate FNS in an error message. FNS can also
1951 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1953 void
1954 print_candidates (tree fns)
1956 char *str = NULL;
1957 print_candidates_1 (fns, &str);
1958 free (str);
1961 /* Get a (possibly) constrained template declaration for the
1962 purpose of ordering candidates. */
1963 static tree
1964 get_template_for_ordering (tree list)
1966 gcc_assert (TREE_CODE (list) == TREE_LIST);
1967 tree f = TREE_VALUE (list);
1968 if (tree ti = DECL_TEMPLATE_INFO (f))
1969 return TI_TEMPLATE (ti);
1970 return f;
1973 /* Among candidates having the same signature, return the
1974 most constrained or NULL_TREE if there is no best candidate.
1975 If the signatures of candidates vary (e.g., template
1976 specialization vs. member function), then there can be no
1977 most constrained.
1979 Note that we don't compare constraints on the functions
1980 themselves, but rather those of their templates. */
1981 static tree
1982 most_constrained_function (tree candidates)
1984 // Try to find the best candidate in a first pass.
1985 tree champ = candidates;
1986 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1988 int winner = more_constrained (get_template_for_ordering (champ),
1989 get_template_for_ordering (c));
1990 if (winner == -1)
1991 champ = c; // The candidate is more constrained
1992 else if (winner == 0)
1993 return NULL_TREE; // Neither is more constrained
1996 // Verify that the champ is better than previous candidates.
1997 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1998 if (!more_constrained (get_template_for_ordering (champ),
1999 get_template_for_ordering (c)))
2000 return NULL_TREE;
2003 return champ;
2007 /* Returns the template (one of the functions given by TEMPLATE_ID)
2008 which can be specialized to match the indicated DECL with the
2009 explicit template args given in TEMPLATE_ID. The DECL may be
2010 NULL_TREE if none is available. In that case, the functions in
2011 TEMPLATE_ID are non-members.
2013 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2014 specialization of a member template.
2016 The TEMPLATE_COUNT is the number of references to qualifying
2017 template classes that appeared in the name of the function. See
2018 check_explicit_specialization for a more accurate description.
2020 TSK indicates what kind of template declaration (if any) is being
2021 declared. TSK_TEMPLATE indicates that the declaration given by
2022 DECL, though a FUNCTION_DECL, has template parameters, and is
2023 therefore a template function.
2025 The template args (those explicitly specified and those deduced)
2026 are output in a newly created vector *TARGS_OUT.
2028 If it is impossible to determine the result, an error message is
2029 issued. The error_mark_node is returned to indicate failure. */
2031 static tree
2032 determine_specialization (tree template_id,
2033 tree decl,
2034 tree* targs_out,
2035 int need_member_template,
2036 int template_count,
2037 tmpl_spec_kind tsk)
2039 tree fns;
2040 tree targs;
2041 tree explicit_targs;
2042 tree candidates = NULL_TREE;
2044 /* A TREE_LIST of templates of which DECL may be a specialization.
2045 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2046 corresponding TREE_PURPOSE is the set of template arguments that,
2047 when used to instantiate the template, would produce a function
2048 with the signature of DECL. */
2049 tree templates = NULL_TREE;
2050 int header_count;
2051 cp_binding_level *b;
2053 *targs_out = NULL_TREE;
2055 if (template_id == error_mark_node || decl == error_mark_node)
2056 return error_mark_node;
2058 /* We shouldn't be specializing a member template of an
2059 unspecialized class template; we already gave an error in
2060 check_specialization_scope, now avoid crashing. */
2061 if (template_count && DECL_CLASS_SCOPE_P (decl)
2062 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2064 gcc_assert (errorcount);
2065 return error_mark_node;
2068 fns = TREE_OPERAND (template_id, 0);
2069 explicit_targs = TREE_OPERAND (template_id, 1);
2071 if (fns == error_mark_node)
2072 return error_mark_node;
2074 /* Check for baselinks. */
2075 if (BASELINK_P (fns))
2076 fns = BASELINK_FUNCTIONS (fns);
2078 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2080 error ("%qD is not a function template", fns);
2081 return error_mark_node;
2083 else if (VAR_P (decl) && !variable_template_p (fns))
2085 error ("%qD is not a variable template", fns);
2086 return error_mark_node;
2089 /* Count the number of template headers specified for this
2090 specialization. */
2091 header_count = 0;
2092 for (b = current_binding_level;
2093 b->kind == sk_template_parms;
2094 b = b->level_chain)
2095 ++header_count;
2097 tree orig_fns = fns;
2099 if (variable_template_p (fns))
2101 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2102 targs = coerce_template_parms (parms, explicit_targs, fns,
2103 tf_warning_or_error,
2104 /*req_all*/true, /*use_defarg*/true);
2105 if (targs != error_mark_node)
2106 templates = tree_cons (targs, fns, templates);
2108 else for (lkp_iterator iter (fns); iter; ++iter)
2110 tree fn = *iter;
2112 if (TREE_CODE (fn) == TEMPLATE_DECL)
2114 tree decl_arg_types;
2115 tree fn_arg_types;
2116 tree insttype;
2118 /* In case of explicit specialization, we need to check if
2119 the number of template headers appearing in the specialization
2120 is correct. This is usually done in check_explicit_specialization,
2121 but the check done there cannot be exhaustive when specializing
2122 member functions. Consider the following code:
2124 template <> void A<int>::f(int);
2125 template <> template <> void A<int>::f(int);
2127 Assuming that A<int> is not itself an explicit specialization
2128 already, the first line specializes "f" which is a non-template
2129 member function, whilst the second line specializes "f" which
2130 is a template member function. So both lines are syntactically
2131 correct, and check_explicit_specialization does not reject
2132 them.
2134 Here, we can do better, as we are matching the specialization
2135 against the declarations. We count the number of template
2136 headers, and we check if they match TEMPLATE_COUNT + 1
2137 (TEMPLATE_COUNT is the number of qualifying template classes,
2138 plus there must be another header for the member template
2139 itself).
2141 Notice that if header_count is zero, this is not a
2142 specialization but rather a template instantiation, so there
2143 is no check we can perform here. */
2144 if (header_count && header_count != template_count + 1)
2145 continue;
2147 /* Check that the number of template arguments at the
2148 innermost level for DECL is the same as for FN. */
2149 if (current_binding_level->kind == sk_template_parms
2150 && !current_binding_level->explicit_spec_p
2151 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2152 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2153 (current_template_parms))))
2154 continue;
2156 /* DECL might be a specialization of FN. */
2157 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2158 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2160 /* For a non-static member function, we need to make sure
2161 that the const qualification is the same. Since
2162 get_bindings does not try to merge the "this" parameter,
2163 we must do the comparison explicitly. */
2164 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2165 && !same_type_p (TREE_VALUE (fn_arg_types),
2166 TREE_VALUE (decl_arg_types)))
2167 continue;
2169 /* Skip the "this" parameter and, for constructors of
2170 classes with virtual bases, the VTT parameter. A
2171 full specialization of a constructor will have a VTT
2172 parameter, but a template never will. */
2173 decl_arg_types
2174 = skip_artificial_parms_for (decl, decl_arg_types);
2175 fn_arg_types
2176 = skip_artificial_parms_for (fn, fn_arg_types);
2178 /* Function templates cannot be specializations; there are
2179 no partial specializations of functions. Therefore, if
2180 the type of DECL does not match FN, there is no
2181 match.
2183 Note that it should never be the case that we have both
2184 candidates added here, and for regular member functions
2185 below. */
2186 if (tsk == tsk_template)
2188 if (compparms (fn_arg_types, decl_arg_types))
2189 candidates = tree_cons (NULL_TREE, fn, candidates);
2190 continue;
2193 /* See whether this function might be a specialization of this
2194 template. Suppress access control because we might be trying
2195 to make this specialization a friend, and we have already done
2196 access control for the declaration of the specialization. */
2197 push_deferring_access_checks (dk_no_check);
2198 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2199 pop_deferring_access_checks ();
2201 if (!targs)
2202 /* We cannot deduce template arguments that when used to
2203 specialize TMPL will produce DECL. */
2204 continue;
2206 /* Remove, from the set of candidates, all those functions
2207 whose constraints are not satisfied. */
2208 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2209 continue;
2211 // Then, try to form the new function type.
2212 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2213 if (insttype == error_mark_node)
2214 continue;
2215 fn_arg_types
2216 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2217 if (!compparms (fn_arg_types, decl_arg_types))
2218 continue;
2220 /* Save this template, and the arguments deduced. */
2221 templates = tree_cons (targs, fn, templates);
2223 else if (need_member_template)
2224 /* FN is an ordinary member function, and we need a
2225 specialization of a member template. */
2227 else if (TREE_CODE (fn) != FUNCTION_DECL)
2228 /* We can get IDENTIFIER_NODEs here in certain erroneous
2229 cases. */
2231 else if (!DECL_FUNCTION_MEMBER_P (fn))
2232 /* This is just an ordinary non-member function. Nothing can
2233 be a specialization of that. */
2235 else if (DECL_ARTIFICIAL (fn))
2236 /* Cannot specialize functions that are created implicitly. */
2238 else
2240 tree decl_arg_types;
2242 /* This is an ordinary member function. However, since
2243 we're here, we can assume its enclosing class is a
2244 template class. For example,
2246 template <typename T> struct S { void f(); };
2247 template <> void S<int>::f() {}
2249 Here, S<int>::f is a non-template, but S<int> is a
2250 template class. If FN has the same type as DECL, we
2251 might be in business. */
2253 if (!DECL_TEMPLATE_INFO (fn))
2254 /* Its enclosing class is an explicit specialization
2255 of a template class. This is not a candidate. */
2256 continue;
2258 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2259 TREE_TYPE (TREE_TYPE (fn))))
2260 /* The return types differ. */
2261 continue;
2263 /* Adjust the type of DECL in case FN is a static member. */
2264 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2265 if (DECL_STATIC_FUNCTION_P (fn)
2266 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2267 decl_arg_types = TREE_CHAIN (decl_arg_types);
2269 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2270 decl_arg_types))
2271 continue;
2273 // If the deduced arguments do not satisfy the constraints,
2274 // this is not a candidate.
2275 if (flag_concepts && !constraints_satisfied_p (fn))
2276 continue;
2278 // Add the candidate.
2279 candidates = tree_cons (NULL_TREE, fn, candidates);
2283 if (templates && TREE_CHAIN (templates))
2285 /* We have:
2287 [temp.expl.spec]
2289 It is possible for a specialization with a given function
2290 signature to be instantiated from more than one function
2291 template. In such cases, explicit specification of the
2292 template arguments must be used to uniquely identify the
2293 function template specialization being specialized.
2295 Note that here, there's no suggestion that we're supposed to
2296 determine which of the candidate templates is most
2297 specialized. However, we, also have:
2299 [temp.func.order]
2301 Partial ordering of overloaded function template
2302 declarations is used in the following contexts to select
2303 the function template to which a function template
2304 specialization refers:
2306 -- when an explicit specialization refers to a function
2307 template.
2309 So, we do use the partial ordering rules, at least for now.
2310 This extension can only serve to make invalid programs valid,
2311 so it's safe. And, there is strong anecdotal evidence that
2312 the committee intended the partial ordering rules to apply;
2313 the EDG front end has that behavior, and John Spicer claims
2314 that the committee simply forgot to delete the wording in
2315 [temp.expl.spec]. */
2316 tree tmpl = most_specialized_instantiation (templates);
2317 if (tmpl != error_mark_node)
2319 templates = tmpl;
2320 TREE_CHAIN (templates) = NULL_TREE;
2324 // Concepts allows multiple declarations of member functions
2325 // with the same signature. Like above, we need to rely on
2326 // on the partial ordering of those candidates to determine which
2327 // is the best.
2328 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2330 if (tree cand = most_constrained_function (candidates))
2332 candidates = cand;
2333 TREE_CHAIN (cand) = NULL_TREE;
2337 if (templates == NULL_TREE && candidates == NULL_TREE)
2339 error ("template-id %qD for %q+D does not match any template "
2340 "declaration", template_id, decl);
2341 if (header_count && header_count != template_count + 1)
2342 inform (input_location, "saw %d %<template<>%>, need %d for "
2343 "specializing a member function template",
2344 header_count, template_count + 1);
2345 else
2346 print_candidates (orig_fns);
2347 return error_mark_node;
2349 else if ((templates && TREE_CHAIN (templates))
2350 || (candidates && TREE_CHAIN (candidates))
2351 || (templates && candidates))
2353 error ("ambiguous template specialization %qD for %q+D",
2354 template_id, decl);
2355 candidates = chainon (candidates, templates);
2356 print_candidates (candidates);
2357 return error_mark_node;
2360 /* We have one, and exactly one, match. */
2361 if (candidates)
2363 tree fn = TREE_VALUE (candidates);
2364 *targs_out = copy_node (DECL_TI_ARGS (fn));
2366 // Propagate the candidate's constraints to the declaration.
2367 set_constraints (decl, get_constraints (fn));
2369 /* DECL is a re-declaration or partial instantiation of a template
2370 function. */
2371 if (TREE_CODE (fn) == TEMPLATE_DECL)
2372 return fn;
2373 /* It was a specialization of an ordinary member function in a
2374 template class. */
2375 return DECL_TI_TEMPLATE (fn);
2378 /* It was a specialization of a template. */
2379 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2382 *targs_out = copy_node (targs);
2383 SET_TMPL_ARGS_LEVEL (*targs_out,
2384 TMPL_ARGS_DEPTH (*targs_out),
2385 TREE_PURPOSE (templates));
2387 else
2388 *targs_out = TREE_PURPOSE (templates);
2389 return TREE_VALUE (templates);
2392 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2393 but with the default argument values filled in from those in the
2394 TMPL_TYPES. */
2396 static tree
2397 copy_default_args_to_explicit_spec_1 (tree spec_types,
2398 tree tmpl_types)
2400 tree new_spec_types;
2402 if (!spec_types)
2403 return NULL_TREE;
2405 if (spec_types == void_list_node)
2406 return void_list_node;
2408 /* Substitute into the rest of the list. */
2409 new_spec_types =
2410 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2411 TREE_CHAIN (tmpl_types));
2413 /* Add the default argument for this parameter. */
2414 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2415 TREE_VALUE (spec_types),
2416 new_spec_types);
2419 /* DECL is an explicit specialization. Replicate default arguments
2420 from the template it specializes. (That way, code like:
2422 template <class T> void f(T = 3);
2423 template <> void f(double);
2424 void g () { f (); }
2426 works, as required.) An alternative approach would be to look up
2427 the correct default arguments at the call-site, but this approach
2428 is consistent with how implicit instantiations are handled. */
2430 static void
2431 copy_default_args_to_explicit_spec (tree decl)
2433 tree tmpl;
2434 tree spec_types;
2435 tree tmpl_types;
2436 tree new_spec_types;
2437 tree old_type;
2438 tree new_type;
2439 tree t;
2440 tree object_type = NULL_TREE;
2441 tree in_charge = NULL_TREE;
2442 tree vtt = NULL_TREE;
2444 /* See if there's anything we need to do. */
2445 tmpl = DECL_TI_TEMPLATE (decl);
2446 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2447 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2448 if (TREE_PURPOSE (t))
2449 break;
2450 if (!t)
2451 return;
2453 old_type = TREE_TYPE (decl);
2454 spec_types = TYPE_ARG_TYPES (old_type);
2456 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2458 /* Remove the this pointer, but remember the object's type for
2459 CV quals. */
2460 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2461 spec_types = TREE_CHAIN (spec_types);
2462 tmpl_types = TREE_CHAIN (tmpl_types);
2464 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2466 /* DECL may contain more parameters than TMPL due to the extra
2467 in-charge parameter in constructors and destructors. */
2468 in_charge = spec_types;
2469 spec_types = TREE_CHAIN (spec_types);
2471 if (DECL_HAS_VTT_PARM_P (decl))
2473 vtt = spec_types;
2474 spec_types = TREE_CHAIN (spec_types);
2478 /* Compute the merged default arguments. */
2479 new_spec_types =
2480 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2482 /* Compute the new FUNCTION_TYPE. */
2483 if (object_type)
2485 if (vtt)
2486 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2487 TREE_VALUE (vtt),
2488 new_spec_types);
2490 if (in_charge)
2491 /* Put the in-charge parameter back. */
2492 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2493 TREE_VALUE (in_charge),
2494 new_spec_types);
2496 new_type = build_method_type_directly (object_type,
2497 TREE_TYPE (old_type),
2498 new_spec_types);
2500 else
2501 new_type = build_function_type (TREE_TYPE (old_type),
2502 new_spec_types);
2503 new_type = cp_build_type_attribute_variant (new_type,
2504 TYPE_ATTRIBUTES (old_type));
2505 new_type = build_exception_variant (new_type,
2506 TYPE_RAISES_EXCEPTIONS (old_type));
2508 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2509 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2511 TREE_TYPE (decl) = new_type;
2514 /* Return the number of template headers we expect to see for a definition
2515 or specialization of CTYPE or one of its non-template members. */
2518 num_template_headers_for_class (tree ctype)
2520 int num_templates = 0;
2522 while (ctype && CLASS_TYPE_P (ctype))
2524 /* You're supposed to have one `template <...>' for every
2525 template class, but you don't need one for a full
2526 specialization. For example:
2528 template <class T> struct S{};
2529 template <> struct S<int> { void f(); };
2530 void S<int>::f () {}
2532 is correct; there shouldn't be a `template <>' for the
2533 definition of `S<int>::f'. */
2534 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2535 /* If CTYPE does not have template information of any
2536 kind, then it is not a template, nor is it nested
2537 within a template. */
2538 break;
2539 if (explicit_class_specialization_p (ctype))
2540 break;
2541 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2542 ++num_templates;
2544 ctype = TYPE_CONTEXT (ctype);
2547 return num_templates;
2550 /* Do a simple sanity check on the template headers that precede the
2551 variable declaration DECL. */
2553 void
2554 check_template_variable (tree decl)
2556 tree ctx = CP_DECL_CONTEXT (decl);
2557 int wanted = num_template_headers_for_class (ctx);
2558 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2559 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2561 if (cxx_dialect < cxx14)
2562 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2563 "variable templates only available with "
2564 "-std=c++14 or -std=gnu++14");
2566 // Namespace-scope variable templates should have a template header.
2567 ++wanted;
2569 if (template_header_count > wanted)
2571 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2572 "too many template headers for %qD "
2573 "(should be %d)",
2574 decl, wanted);
2575 if (warned && CLASS_TYPE_P (ctx)
2576 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2577 inform (DECL_SOURCE_LOCATION (decl),
2578 "members of an explicitly specialized class are defined "
2579 "without a template header");
2583 /* An explicit specialization whose declarator-id or class-head-name is not
2584 qualified shall be declared in the nearest enclosing namespace of the
2585 template, or, if the namespace is inline (7.3.1), any namespace from its
2586 enclosing namespace set.
2588 If the name declared in the explicit instantiation is an unqualified name,
2589 the explicit instantiation shall appear in the namespace where its template
2590 is declared or, if that namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set. */
2593 void
2594 check_unqualified_spec_or_inst (tree t, location_t loc)
2596 tree tmpl = most_general_template (t);
2597 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2598 && !is_nested_namespace (current_namespace,
2599 CP_DECL_CONTEXT (tmpl), true))
2601 if (processing_specialization)
2602 permerror (loc, "explicit specialization of %qD outside its "
2603 "namespace must use a nested-name-specifier", tmpl);
2604 else if (processing_explicit_instantiation
2605 && cxx_dialect >= cxx11)
2606 /* This was allowed in C++98, so only pedwarn. */
2607 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2608 "outside its namespace must use a nested-name-"
2609 "specifier", tmpl);
2613 /* Check to see if the function just declared, as indicated in
2614 DECLARATOR, and in DECL, is a specialization of a function
2615 template. We may also discover that the declaration is an explicit
2616 instantiation at this point.
2618 Returns DECL, or an equivalent declaration that should be used
2619 instead if all goes well. Issues an error message if something is
2620 amiss. Returns error_mark_node if the error is not easily
2621 recoverable.
2623 FLAGS is a bitmask consisting of the following flags:
2625 2: The function has a definition.
2626 4: The function is a friend.
2628 The TEMPLATE_COUNT is the number of references to qualifying
2629 template classes that appeared in the name of the function. For
2630 example, in
2632 template <class T> struct S { void f(); };
2633 void S<int>::f();
2635 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2636 classes are not counted in the TEMPLATE_COUNT, so that in
2638 template <class T> struct S {};
2639 template <> struct S<int> { void f(); }
2640 template <> void S<int>::f();
2642 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2643 invalid; there should be no template <>.)
2645 If the function is a specialization, it is marked as such via
2646 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2647 is set up correctly, and it is added to the list of specializations
2648 for that template. */
2650 tree
2651 check_explicit_specialization (tree declarator,
2652 tree decl,
2653 int template_count,
2654 int flags)
2656 int have_def = flags & 2;
2657 int is_friend = flags & 4;
2658 bool is_concept = flags & 8;
2659 int specialization = 0;
2660 int explicit_instantiation = 0;
2661 int member_specialization = 0;
2662 tree ctype = DECL_CLASS_CONTEXT (decl);
2663 tree dname = DECL_NAME (decl);
2664 tmpl_spec_kind tsk;
2666 if (is_friend)
2668 if (!processing_specialization)
2669 tsk = tsk_none;
2670 else
2671 tsk = tsk_excessive_parms;
2673 else
2674 tsk = current_tmpl_spec_kind (template_count);
2676 switch (tsk)
2678 case tsk_none:
2679 if (processing_specialization && !VAR_P (decl))
2681 specialization = 1;
2682 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2684 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2686 if (is_friend)
2687 /* This could be something like:
2689 template <class T> void f(T);
2690 class S { friend void f<>(int); } */
2691 specialization = 1;
2692 else
2694 /* This case handles bogus declarations like template <>
2695 template <class T> void f<int>(); */
2697 error ("template-id %qD in declaration of primary template",
2698 declarator);
2699 return decl;
2702 break;
2704 case tsk_invalid_member_spec:
2705 /* The error has already been reported in
2706 check_specialization_scope. */
2707 return error_mark_node;
2709 case tsk_invalid_expl_inst:
2710 error ("template parameter list used in explicit instantiation");
2712 /* Fall through. */
2714 case tsk_expl_inst:
2715 if (have_def)
2716 error ("definition provided for explicit instantiation");
2718 explicit_instantiation = 1;
2719 break;
2721 case tsk_excessive_parms:
2722 case tsk_insufficient_parms:
2723 if (tsk == tsk_excessive_parms)
2724 error ("too many template parameter lists in declaration of %qD",
2725 decl);
2726 else if (template_header_count)
2727 error("too few template parameter lists in declaration of %qD", decl);
2728 else
2729 error("explicit specialization of %qD must be introduced by "
2730 "%<template <>%>", decl);
2732 /* Fall through. */
2733 case tsk_expl_spec:
2734 if (is_concept)
2735 error ("explicit specialization declared %<concept%>");
2737 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2738 /* In cases like template<> constexpr bool v = true;
2739 We'll give an error in check_template_variable. */
2740 break;
2742 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2743 if (ctype)
2744 member_specialization = 1;
2745 else
2746 specialization = 1;
2747 break;
2749 case tsk_template:
2750 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2752 /* This case handles bogus declarations like template <>
2753 template <class T> void f<int>(); */
2755 if (!uses_template_parms (declarator))
2756 error ("template-id %qD in declaration of primary template",
2757 declarator);
2758 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2760 /* Partial specialization of variable template. */
2761 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2762 specialization = 1;
2763 goto ok;
2765 else if (cxx_dialect < cxx14)
2766 error ("non-type partial specialization %qD "
2767 "is not allowed", declarator);
2768 else
2769 error ("non-class, non-variable partial specialization %qD "
2770 "is not allowed", declarator);
2771 return decl;
2772 ok:;
2775 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2776 /* This is a specialization of a member template, without
2777 specialization the containing class. Something like:
2779 template <class T> struct S {
2780 template <class U> void f (U);
2782 template <> template <class U> void S<int>::f(U) {}
2784 That's a specialization -- but of the entire template. */
2785 specialization = 1;
2786 break;
2788 default:
2789 gcc_unreachable ();
2792 if ((specialization || member_specialization)
2793 /* This doesn't apply to variable templates. */
2794 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2795 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2797 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2798 for (; t; t = TREE_CHAIN (t))
2799 if (TREE_PURPOSE (t))
2801 permerror (input_location,
2802 "default argument specified in explicit specialization");
2803 break;
2807 if (specialization || member_specialization || explicit_instantiation)
2809 tree tmpl = NULL_TREE;
2810 tree targs = NULL_TREE;
2811 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2813 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2814 if (!was_template_id)
2816 tree fns;
2818 gcc_assert (identifier_p (declarator));
2819 if (ctype)
2820 fns = dname;
2821 else
2823 /* If there is no class context, the explicit instantiation
2824 must be at namespace scope. */
2825 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2827 /* Find the namespace binding, using the declaration
2828 context. */
2829 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2830 false, true);
2831 if (fns == error_mark_node)
2832 /* If lookup fails, look for a friend declaration so we can
2833 give a better diagnostic. */
2834 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2835 /*type*/false, /*complain*/true,
2836 /*hidden*/true);
2838 if (fns == error_mark_node || !is_overloaded_fn (fns))
2840 error ("%qD is not a template function", dname);
2841 fns = error_mark_node;
2845 declarator = lookup_template_function (fns, NULL_TREE);
2848 if (declarator == error_mark_node)
2849 return error_mark_node;
2851 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2853 if (!explicit_instantiation)
2854 /* A specialization in class scope. This is invalid,
2855 but the error will already have been flagged by
2856 check_specialization_scope. */
2857 return error_mark_node;
2858 else
2860 /* It's not valid to write an explicit instantiation in
2861 class scope, e.g.:
2863 class C { template void f(); }
2865 This case is caught by the parser. However, on
2866 something like:
2868 template class C { void f(); };
2870 (which is invalid) we can get here. The error will be
2871 issued later. */
2875 return decl;
2877 else if (ctype != NULL_TREE
2878 && (identifier_p (TREE_OPERAND (declarator, 0))))
2880 // We'll match variable templates in start_decl.
2881 if (VAR_P (decl))
2882 return decl;
2884 /* Find the list of functions in ctype that have the same
2885 name as the declared function. */
2886 tree name = TREE_OPERAND (declarator, 0);
2888 if (constructor_name_p (name, ctype))
2890 if (DECL_CONSTRUCTOR_P (decl)
2891 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2892 : !CLASSTYPE_DESTRUCTOR (ctype))
2894 /* From [temp.expl.spec]:
2896 If such an explicit specialization for the member
2897 of a class template names an implicitly-declared
2898 special member function (clause _special_), the
2899 program is ill-formed.
2901 Similar language is found in [temp.explicit]. */
2902 error ("specialization of implicitly-declared special member function");
2903 return error_mark_node;
2906 name = DECL_NAME (decl);
2909 /* For a type-conversion operator, We might be looking for
2910 `operator int' which will be a specialization of
2911 `operator T'. Grab all the conversion operators, and
2912 then select from them. */
2913 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2914 ? conv_op_identifier : name);
2916 if (fns == NULL_TREE)
2918 error ("no member function %qD declared in %qT", name, ctype);
2919 return error_mark_node;
2921 else
2922 TREE_OPERAND (declarator, 0) = fns;
2925 /* Figure out what exactly is being specialized at this point.
2926 Note that for an explicit instantiation, even one for a
2927 member function, we cannot tell a priori whether the
2928 instantiation is for a member template, or just a member
2929 function of a template class. Even if a member template is
2930 being instantiated, the member template arguments may be
2931 elided if they can be deduced from the rest of the
2932 declaration. */
2933 tmpl = determine_specialization (declarator, decl,
2934 &targs,
2935 member_specialization,
2936 template_count,
2937 tsk);
2939 if (!tmpl || tmpl == error_mark_node)
2940 /* We couldn't figure out what this declaration was
2941 specializing. */
2942 return error_mark_node;
2943 else
2945 if (TREE_CODE (decl) == FUNCTION_DECL
2946 && DECL_HIDDEN_FRIEND_P (tmpl))
2948 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2949 "friend declaration %qD is not visible to "
2950 "explicit specialization", tmpl))
2951 inform (DECL_SOURCE_LOCATION (tmpl),
2952 "friend declaration here");
2954 else if (!ctype && !is_friend
2955 && CP_DECL_CONTEXT (decl) == current_namespace)
2956 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2958 tree gen_tmpl = most_general_template (tmpl);
2960 if (explicit_instantiation)
2962 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2963 is done by do_decl_instantiation later. */
2965 int arg_depth = TMPL_ARGS_DEPTH (targs);
2966 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2968 if (arg_depth > parm_depth)
2970 /* If TMPL is not the most general template (for
2971 example, if TMPL is a friend template that is
2972 injected into namespace scope), then there will
2973 be too many levels of TARGS. Remove some of them
2974 here. */
2975 int i;
2976 tree new_targs;
2978 new_targs = make_tree_vec (parm_depth);
2979 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2980 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2981 = TREE_VEC_ELT (targs, i);
2982 targs = new_targs;
2985 return instantiate_template (tmpl, targs, tf_error);
2988 /* If we thought that the DECL was a member function, but it
2989 turns out to be specializing a static member function,
2990 make DECL a static member function as well. */
2991 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2992 && DECL_STATIC_FUNCTION_P (tmpl)
2993 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2994 revert_static_member_fn (decl);
2996 /* If this is a specialization of a member template of a
2997 template class, we want to return the TEMPLATE_DECL, not
2998 the specialization of it. */
2999 if (tsk == tsk_template && !was_template_id)
3001 tree result = DECL_TEMPLATE_RESULT (tmpl);
3002 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3003 DECL_INITIAL (result) = NULL_TREE;
3004 if (have_def)
3006 tree parm;
3007 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3008 DECL_SOURCE_LOCATION (result)
3009 = DECL_SOURCE_LOCATION (decl);
3010 /* We want to use the argument list specified in the
3011 definition, not in the original declaration. */
3012 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3013 for (parm = DECL_ARGUMENTS (result); parm;
3014 parm = DECL_CHAIN (parm))
3015 DECL_CONTEXT (parm) = result;
3017 return register_specialization (tmpl, gen_tmpl, targs,
3018 is_friend, 0);
3021 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3022 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3024 if (was_template_id)
3025 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3027 /* Inherit default function arguments from the template
3028 DECL is specializing. */
3029 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3030 copy_default_args_to_explicit_spec (decl);
3032 /* This specialization has the same protection as the
3033 template it specializes. */
3034 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3035 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3037 /* 7.1.1-1 [dcl.stc]
3039 A storage-class-specifier shall not be specified in an
3040 explicit specialization...
3042 The parser rejects these, so unless action is taken here,
3043 explicit function specializations will always appear with
3044 global linkage.
3046 The action recommended by the C++ CWG in response to C++
3047 defect report 605 is to make the storage class and linkage
3048 of the explicit specialization match the templated function:
3050 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3052 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3054 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3055 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3057 /* A concept cannot be specialized. */
3058 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3060 error ("explicit specialization of function concept %qD",
3061 gen_tmpl);
3062 return error_mark_node;
3065 /* This specialization has the same linkage and visibility as
3066 the function template it specializes. */
3067 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3068 if (! TREE_PUBLIC (decl))
3070 DECL_INTERFACE_KNOWN (decl) = 1;
3071 DECL_NOT_REALLY_EXTERN (decl) = 1;
3073 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3074 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3076 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3077 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3081 /* If DECL is a friend declaration, declared using an
3082 unqualified name, the namespace associated with DECL may
3083 have been set incorrectly. For example, in:
3085 template <typename T> void f(T);
3086 namespace N {
3087 struct S { friend void f<int>(int); }
3090 we will have set the DECL_CONTEXT for the friend
3091 declaration to N, rather than to the global namespace. */
3092 if (DECL_NAMESPACE_SCOPE_P (decl))
3093 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3095 if (is_friend && !have_def)
3096 /* This is not really a declaration of a specialization.
3097 It's just the name of an instantiation. But, it's not
3098 a request for an instantiation, either. */
3099 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3100 else if (TREE_CODE (decl) == FUNCTION_DECL)
3101 /* A specialization is not necessarily COMDAT. */
3102 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3103 && DECL_DECLARED_INLINE_P (decl));
3104 else if (VAR_P (decl))
3105 DECL_COMDAT (decl) = false;
3107 /* If this is a full specialization, register it so that we can find
3108 it again. Partial specializations will be registered in
3109 process_partial_specialization. */
3110 if (!processing_template_decl)
3111 decl = register_specialization (decl, gen_tmpl, targs,
3112 is_friend, 0);
3114 /* A 'structor should already have clones. */
3115 gcc_assert (decl == error_mark_node
3116 || variable_template_p (tmpl)
3117 || !(DECL_CONSTRUCTOR_P (decl)
3118 || DECL_DESTRUCTOR_P (decl))
3119 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3123 return decl;
3126 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3127 parameters. These are represented in the same format used for
3128 DECL_TEMPLATE_PARMS. */
3131 comp_template_parms (const_tree parms1, const_tree parms2)
3133 const_tree p1;
3134 const_tree p2;
3136 if (parms1 == parms2)
3137 return 1;
3139 for (p1 = parms1, p2 = parms2;
3140 p1 != NULL_TREE && p2 != NULL_TREE;
3141 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3143 tree t1 = TREE_VALUE (p1);
3144 tree t2 = TREE_VALUE (p2);
3145 int i;
3147 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3148 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3150 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3151 return 0;
3153 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3155 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3156 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3158 /* If either of the template parameters are invalid, assume
3159 they match for the sake of error recovery. */
3160 if (error_operand_p (parm1) || error_operand_p (parm2))
3161 return 1;
3163 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3164 return 0;
3166 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3167 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3168 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3169 continue;
3170 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3171 return 0;
3175 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3176 /* One set of parameters has more parameters lists than the
3177 other. */
3178 return 0;
3180 return 1;
3183 /* Determine whether PARM is a parameter pack. */
3185 bool
3186 template_parameter_pack_p (const_tree parm)
3188 /* Determine if we have a non-type template parameter pack. */
3189 if (TREE_CODE (parm) == PARM_DECL)
3190 return (DECL_TEMPLATE_PARM_P (parm)
3191 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3192 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3193 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3195 /* If this is a list of template parameters, we could get a
3196 TYPE_DECL or a TEMPLATE_DECL. */
3197 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3198 parm = TREE_TYPE (parm);
3200 /* Otherwise it must be a type template parameter. */
3201 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3202 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3203 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3206 /* Determine if T is a function parameter pack. */
3208 bool
3209 function_parameter_pack_p (const_tree t)
3211 if (t && TREE_CODE (t) == PARM_DECL)
3212 return DECL_PACK_P (t);
3213 return false;
3216 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3217 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3219 tree
3220 get_function_template_decl (const_tree primary_func_tmpl_inst)
3222 if (! primary_func_tmpl_inst
3223 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3224 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3225 return NULL;
3227 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3230 /* Return true iff the function parameter PARAM_DECL was expanded
3231 from the function parameter pack PACK. */
3233 bool
3234 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3236 if (DECL_ARTIFICIAL (param_decl)
3237 || !function_parameter_pack_p (pack))
3238 return false;
3240 /* The parameter pack and its pack arguments have the same
3241 DECL_PARM_INDEX. */
3242 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3245 /* Determine whether ARGS describes a variadic template args list,
3246 i.e., one that is terminated by a template argument pack. */
3248 static bool
3249 template_args_variadic_p (tree args)
3251 int nargs;
3252 tree last_parm;
3254 if (args == NULL_TREE)
3255 return false;
3257 args = INNERMOST_TEMPLATE_ARGS (args);
3258 nargs = TREE_VEC_LENGTH (args);
3260 if (nargs == 0)
3261 return false;
3263 last_parm = TREE_VEC_ELT (args, nargs - 1);
3265 return ARGUMENT_PACK_P (last_parm);
3268 /* Generate a new name for the parameter pack name NAME (an
3269 IDENTIFIER_NODE) that incorporates its */
3271 static tree
3272 make_ith_pack_parameter_name (tree name, int i)
3274 /* Munge the name to include the parameter index. */
3275 #define NUMBUF_LEN 128
3276 char numbuf[NUMBUF_LEN];
3277 char* newname;
3278 int newname_len;
3280 if (name == NULL_TREE)
3281 return name;
3282 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3283 newname_len = IDENTIFIER_LENGTH (name)
3284 + strlen (numbuf) + 2;
3285 newname = (char*)alloca (newname_len);
3286 snprintf (newname, newname_len,
3287 "%s#%i", IDENTIFIER_POINTER (name), i);
3288 return get_identifier (newname);
3291 /* Return true if T is a primary function, class or alias template
3292 specialization, not including the template pattern. */
3294 bool
3295 primary_template_specialization_p (const_tree t)
3297 if (!t)
3298 return false;
3300 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3301 return (DECL_LANG_SPECIFIC (t)
3302 && DECL_USE_TEMPLATE (t)
3303 && DECL_TEMPLATE_INFO (t)
3304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3305 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3306 return (CLASSTYPE_TEMPLATE_INFO (t)
3307 && CLASSTYPE_USE_TEMPLATE (t)
3308 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3309 else if (alias_template_specialization_p (t))
3310 return true;
3311 return false;
3314 /* Return true if PARM is a template template parameter. */
3316 bool
3317 template_template_parameter_p (const_tree parm)
3319 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3322 /* Return true iff PARM is a DECL representing a type template
3323 parameter. */
3325 bool
3326 template_type_parameter_p (const_tree parm)
3328 return (parm
3329 && (TREE_CODE (parm) == TYPE_DECL
3330 || TREE_CODE (parm) == TEMPLATE_DECL)
3331 && DECL_TEMPLATE_PARM_P (parm));
3334 /* Return the template parameters of T if T is a
3335 primary template instantiation, NULL otherwise. */
3337 tree
3338 get_primary_template_innermost_parameters (const_tree t)
3340 tree parms = NULL, template_info = NULL;
3342 if ((template_info = get_template_info (t))
3343 && primary_template_specialization_p (t))
3344 parms = INNERMOST_TEMPLATE_PARMS
3345 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3347 return parms;
3350 /* Return the template parameters of the LEVELth level from the full list
3351 of template parameters PARMS. */
3353 tree
3354 get_template_parms_at_level (tree parms, int level)
3356 tree p;
3357 if (!parms
3358 || TREE_CODE (parms) != TREE_LIST
3359 || level > TMPL_PARMS_DEPTH (parms))
3360 return NULL_TREE;
3362 for (p = parms; p; p = TREE_CHAIN (p))
3363 if (TMPL_PARMS_DEPTH (p) == level)
3364 return p;
3366 return NULL_TREE;
3369 /* Returns the template arguments of T if T is a template instantiation,
3370 NULL otherwise. */
3372 tree
3373 get_template_innermost_arguments (const_tree t)
3375 tree args = NULL, template_info = NULL;
3377 if ((template_info = get_template_info (t))
3378 && TI_ARGS (template_info))
3379 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3381 return args;
3384 /* Return the argument pack elements of T if T is a template argument pack,
3385 NULL otherwise. */
3387 tree
3388 get_template_argument_pack_elems (const_tree t)
3390 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3391 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3392 return NULL;
3394 return ARGUMENT_PACK_ARGS (t);
3397 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3398 ARGUMENT_PACK_SELECT represents. */
3400 static tree
3401 argument_pack_select_arg (tree t)
3403 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3404 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3406 /* If the selected argument is an expansion E, that most likely means we were
3407 called from gen_elem_of_pack_expansion_instantiation during the
3408 substituting of an argument pack (of which the Ith element is a pack
3409 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3410 In this case, the Ith element resulting from this substituting is going to
3411 be a pack expansion, which pattern is the pattern of E. Let's return the
3412 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3413 resulting pack expansion from it. */
3414 if (PACK_EXPANSION_P (arg))
3416 /* Make sure we aren't throwing away arg info. */
3417 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3418 arg = PACK_EXPANSION_PATTERN (arg);
3421 return arg;
3425 /* True iff FN is a function representing a built-in variadic parameter
3426 pack. */
3428 bool
3429 builtin_pack_fn_p (tree fn)
3431 if (!fn
3432 || TREE_CODE (fn) != FUNCTION_DECL
3433 || !DECL_IS_BUILTIN (fn))
3434 return false;
3436 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3437 return true;
3439 return false;
3442 /* True iff CALL is a call to a function representing a built-in variadic
3443 parameter pack. */
3445 static bool
3446 builtin_pack_call_p (tree call)
3448 if (TREE_CODE (call) != CALL_EXPR)
3449 return false;
3450 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3453 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3455 static tree
3456 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3457 tree in_decl)
3459 tree ohi = CALL_EXPR_ARG (call, 0);
3460 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3461 false/*fn*/, true/*int_cst*/);
3463 if (value_dependent_expression_p (hi))
3465 if (hi != ohi)
3467 call = copy_node (call);
3468 CALL_EXPR_ARG (call, 0) = hi;
3470 tree ex = make_pack_expansion (call, complain);
3471 tree vec = make_tree_vec (1);
3472 TREE_VEC_ELT (vec, 0) = ex;
3473 return vec;
3475 else
3477 hi = cxx_constant_value (hi);
3478 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3480 /* Calculate the largest value of len that won't make the size of the vec
3481 overflow an int. The compiler will exceed resource limits long before
3482 this, but it seems a decent place to diagnose. */
3483 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3485 if (len < 0 || len > max)
3487 if ((complain & tf_error)
3488 && hi != error_mark_node)
3489 error ("argument to __integer_pack must be between 0 and %d", max);
3490 return error_mark_node;
3493 tree vec = make_tree_vec (len);
3495 for (int i = 0; i < len; ++i)
3496 TREE_VEC_ELT (vec, i) = size_int (i);
3498 return vec;
3502 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3503 CALL. */
3505 static tree
3506 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3507 tree in_decl)
3509 if (!builtin_pack_call_p (call))
3510 return NULL_TREE;
3512 tree fn = CALL_EXPR_FN (call);
3514 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3515 return expand_integer_pack (call, args, complain, in_decl);
3517 return NULL_TREE;
3520 /* Structure used to track the progress of find_parameter_packs_r. */
3521 struct find_parameter_pack_data
3523 /* TREE_LIST that will contain all of the parameter packs found by
3524 the traversal. */
3525 tree* parameter_packs;
3527 /* Set of AST nodes that have been visited by the traversal. */
3528 hash_set<tree> *visited;
3530 /* True iff we're making a type pack expansion. */
3531 bool type_pack_expansion_p;
3534 /* Identifies all of the argument packs that occur in a template
3535 argument and appends them to the TREE_LIST inside DATA, which is a
3536 find_parameter_pack_data structure. This is a subroutine of
3537 make_pack_expansion and uses_parameter_packs. */
3538 static tree
3539 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3541 tree t = *tp;
3542 struct find_parameter_pack_data* ppd =
3543 (struct find_parameter_pack_data*)data;
3544 bool parameter_pack_p = false;
3546 /* Handle type aliases/typedefs. */
3547 if (TYPE_ALIAS_P (t))
3549 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3550 cp_walk_tree (&TI_ARGS (tinfo),
3551 &find_parameter_packs_r,
3552 ppd, ppd->visited);
3553 *walk_subtrees = 0;
3554 return NULL_TREE;
3557 /* Identify whether this is a parameter pack or not. */
3558 switch (TREE_CODE (t))
3560 case TEMPLATE_PARM_INDEX:
3561 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3562 parameter_pack_p = true;
3563 break;
3565 case TEMPLATE_TYPE_PARM:
3566 t = TYPE_MAIN_VARIANT (t);
3567 /* FALLTHRU */
3568 case TEMPLATE_TEMPLATE_PARM:
3569 /* If the placeholder appears in the decl-specifier-seq of a function
3570 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3571 is a pack expansion, the invented template parameter is a template
3572 parameter pack. */
3573 if (ppd->type_pack_expansion_p && is_auto (t))
3574 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3575 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3576 parameter_pack_p = true;
3577 break;
3579 case FIELD_DECL:
3580 case PARM_DECL:
3581 if (DECL_PACK_P (t))
3583 /* We don't want to walk into the type of a PARM_DECL,
3584 because we don't want to see the type parameter pack. */
3585 *walk_subtrees = 0;
3586 parameter_pack_p = true;
3588 break;
3590 case VAR_DECL:
3591 if (DECL_PACK_P (t))
3593 /* We don't want to walk into the type of a variadic capture proxy,
3594 because we don't want to see the type parameter pack. */
3595 *walk_subtrees = 0;
3596 parameter_pack_p = true;
3598 else if (variable_template_specialization_p (t))
3600 cp_walk_tree (&DECL_TI_ARGS (t),
3601 find_parameter_packs_r,
3602 ppd, ppd->visited);
3603 *walk_subtrees = 0;
3605 break;
3607 case CALL_EXPR:
3608 if (builtin_pack_call_p (t))
3609 parameter_pack_p = true;
3610 break;
3612 case BASES:
3613 parameter_pack_p = true;
3614 break;
3615 default:
3616 /* Not a parameter pack. */
3617 break;
3620 if (parameter_pack_p)
3622 /* Add this parameter pack to the list. */
3623 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3626 if (TYPE_P (t))
3627 cp_walk_tree (&TYPE_CONTEXT (t),
3628 &find_parameter_packs_r, ppd, ppd->visited);
3630 /* This switch statement will return immediately if we don't find a
3631 parameter pack. */
3632 switch (TREE_CODE (t))
3634 case TEMPLATE_PARM_INDEX:
3635 return NULL_TREE;
3637 case BOUND_TEMPLATE_TEMPLATE_PARM:
3638 /* Check the template itself. */
3639 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3640 &find_parameter_packs_r, ppd, ppd->visited);
3641 /* Check the template arguments. */
3642 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3643 ppd->visited);
3644 *walk_subtrees = 0;
3645 return NULL_TREE;
3647 case TEMPLATE_TYPE_PARM:
3648 case TEMPLATE_TEMPLATE_PARM:
3649 return NULL_TREE;
3651 case PARM_DECL:
3652 return NULL_TREE;
3654 case DECL_EXPR:
3655 /* Ignore the declaration of a capture proxy for a parameter pack. */
3656 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3657 *walk_subtrees = 0;
3658 return NULL_TREE;
3660 case RECORD_TYPE:
3661 if (TYPE_PTRMEMFUNC_P (t))
3662 return NULL_TREE;
3663 /* Fall through. */
3665 case UNION_TYPE:
3666 case ENUMERAL_TYPE:
3667 if (TYPE_TEMPLATE_INFO (t))
3668 cp_walk_tree (&TYPE_TI_ARGS (t),
3669 &find_parameter_packs_r, ppd, ppd->visited);
3671 *walk_subtrees = 0;
3672 return NULL_TREE;
3674 case TEMPLATE_DECL:
3675 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3676 return NULL_TREE;
3677 gcc_fallthrough();
3679 case CONSTRUCTOR:
3680 cp_walk_tree (&TREE_TYPE (t),
3681 &find_parameter_packs_r, ppd, ppd->visited);
3682 return NULL_TREE;
3684 case TYPENAME_TYPE:
3685 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3686 ppd, ppd->visited);
3687 *walk_subtrees = 0;
3688 return NULL_TREE;
3690 case TYPE_PACK_EXPANSION:
3691 case EXPR_PACK_EXPANSION:
3692 *walk_subtrees = 0;
3693 return NULL_TREE;
3695 case INTEGER_TYPE:
3696 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3697 ppd, ppd->visited);
3698 *walk_subtrees = 0;
3699 return NULL_TREE;
3701 case IDENTIFIER_NODE:
3702 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3703 ppd->visited);
3704 *walk_subtrees = 0;
3705 return NULL_TREE;
3707 case LAMBDA_EXPR:
3709 /* Look at explicit captures. */
3710 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3711 cap; cap = TREE_CHAIN (cap))
3712 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3713 ppd->visited);
3714 /* Since we defer implicit capture, look in the body as well. */
3715 tree fn = lambda_function (t);
3716 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3717 ppd->visited);
3718 *walk_subtrees = 0;
3719 return NULL_TREE;
3722 case DECLTYPE_TYPE:
3724 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3725 type_pack_expansion_p to false so that any placeholders
3726 within the expression don't get marked as parameter packs. */
3727 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3728 ppd->type_pack_expansion_p = false;
3729 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3730 ppd, ppd->visited);
3731 ppd->type_pack_expansion_p = type_pack_expansion_p;
3732 *walk_subtrees = 0;
3733 return NULL_TREE;
3736 default:
3737 return NULL_TREE;
3740 return NULL_TREE;
3743 /* Determines if the expression or type T uses any parameter packs. */
3744 bool
3745 uses_parameter_packs (tree t)
3747 tree parameter_packs = NULL_TREE;
3748 struct find_parameter_pack_data ppd;
3749 ppd.parameter_packs = &parameter_packs;
3750 ppd.visited = new hash_set<tree>;
3751 ppd.type_pack_expansion_p = false;
3752 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3753 delete ppd.visited;
3754 return parameter_packs != NULL_TREE;
3757 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3758 representation a base-class initializer into a parameter pack
3759 expansion. If all goes well, the resulting node will be an
3760 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3761 respectively. */
3762 tree
3763 make_pack_expansion (tree arg, tsubst_flags_t complain)
3765 tree result;
3766 tree parameter_packs = NULL_TREE;
3767 bool for_types = false;
3768 struct find_parameter_pack_data ppd;
3770 if (!arg || arg == error_mark_node)
3771 return arg;
3773 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3775 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3776 class initializer. In this case, the TREE_PURPOSE will be a
3777 _TYPE node (representing the base class expansion we're
3778 initializing) and the TREE_VALUE will be a TREE_LIST
3779 containing the initialization arguments.
3781 The resulting expansion looks somewhat different from most
3782 expansions. Rather than returning just one _EXPANSION, we
3783 return a TREE_LIST whose TREE_PURPOSE is a
3784 TYPE_PACK_EXPANSION containing the bases that will be
3785 initialized. The TREE_VALUE will be identical to the
3786 original TREE_VALUE, which is a list of arguments that will
3787 be passed to each base. We do not introduce any new pack
3788 expansion nodes into the TREE_VALUE (although it is possible
3789 that some already exist), because the TREE_PURPOSE and
3790 TREE_VALUE all need to be expanded together with the same
3791 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3792 resulting TREE_PURPOSE will mention the parameter packs in
3793 both the bases and the arguments to the bases. */
3794 tree purpose;
3795 tree value;
3796 tree parameter_packs = NULL_TREE;
3798 /* Determine which parameter packs will be used by the base
3799 class expansion. */
3800 ppd.visited = new hash_set<tree>;
3801 ppd.parameter_packs = &parameter_packs;
3802 ppd.type_pack_expansion_p = true;
3803 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3804 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3805 &ppd, ppd.visited);
3807 if (parameter_packs == NULL_TREE)
3809 if (complain & tf_error)
3810 error ("base initializer expansion %qT contains no parameter packs",
3811 arg);
3812 delete ppd.visited;
3813 return error_mark_node;
3816 if (TREE_VALUE (arg) != void_type_node)
3818 /* Collect the sets of parameter packs used in each of the
3819 initialization arguments. */
3820 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3822 /* Determine which parameter packs will be expanded in this
3823 argument. */
3824 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3825 &ppd, ppd.visited);
3829 delete ppd.visited;
3831 /* Create the pack expansion type for the base type. */
3832 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3833 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3834 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3835 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3837 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3838 they will rarely be compared to anything. */
3839 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3841 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3844 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3845 for_types = true;
3847 /* Build the PACK_EXPANSION_* node. */
3848 result = for_types
3849 ? cxx_make_type (TYPE_PACK_EXPANSION)
3850 : make_node (EXPR_PACK_EXPANSION);
3851 SET_PACK_EXPANSION_PATTERN (result, arg);
3852 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3854 /* Propagate type and const-expression information. */
3855 TREE_TYPE (result) = TREE_TYPE (arg);
3856 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3857 /* Mark this read now, since the expansion might be length 0. */
3858 mark_exp_read (arg);
3860 else
3861 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3862 they will rarely be compared to anything. */
3863 SET_TYPE_STRUCTURAL_EQUALITY (result);
3865 /* Determine which parameter packs will be expanded. */
3866 ppd.parameter_packs = &parameter_packs;
3867 ppd.visited = new hash_set<tree>;
3868 ppd.type_pack_expansion_p = TYPE_P (arg);
3869 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3870 delete ppd.visited;
3872 /* Make sure we found some parameter packs. */
3873 if (parameter_packs == NULL_TREE)
3875 if (complain & tf_error)
3877 if (TYPE_P (arg))
3878 error ("expansion pattern %qT contains no argument packs", arg);
3879 else
3880 error ("expansion pattern %qE contains no argument packs", arg);
3882 return error_mark_node;
3884 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3886 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3888 return result;
3891 /* Checks T for any "bare" parameter packs, which have not yet been
3892 expanded, and issues an error if any are found. This operation can
3893 only be done on full expressions or types (e.g., an expression
3894 statement, "if" condition, etc.), because we could have expressions like:
3896 foo(f(g(h(args)))...)
3898 where "args" is a parameter pack. check_for_bare_parameter_packs
3899 should not be called for the subexpressions args, h(args),
3900 g(h(args)), or f(g(h(args))), because we would produce erroneous
3901 error messages.
3903 Returns TRUE and emits an error if there were bare parameter packs,
3904 returns FALSE otherwise. */
3905 bool
3906 check_for_bare_parameter_packs (tree t)
3908 tree parameter_packs = NULL_TREE;
3909 struct find_parameter_pack_data ppd;
3911 if (!processing_template_decl || !t || t == error_mark_node)
3912 return false;
3914 /* A lambda might use a parameter pack from the containing context. */
3915 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
3916 return false;
3918 if (TREE_CODE (t) == TYPE_DECL)
3919 t = TREE_TYPE (t);
3921 ppd.parameter_packs = &parameter_packs;
3922 ppd.visited = new hash_set<tree>;
3923 ppd.type_pack_expansion_p = false;
3924 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3925 delete ppd.visited;
3927 if (parameter_packs)
3929 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3930 error_at (loc, "parameter packs not expanded with %<...%>:");
3931 while (parameter_packs)
3933 tree pack = TREE_VALUE (parameter_packs);
3934 tree name = NULL_TREE;
3936 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3937 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3938 name = TYPE_NAME (pack);
3939 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3940 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3941 else if (TREE_CODE (pack) == CALL_EXPR)
3942 name = DECL_NAME (CALL_EXPR_FN (pack));
3943 else
3944 name = DECL_NAME (pack);
3946 if (name)
3947 inform (loc, " %qD", name);
3948 else
3949 inform (loc, " <anonymous>");
3951 parameter_packs = TREE_CHAIN (parameter_packs);
3954 return true;
3957 return false;
3960 /* Expand any parameter packs that occur in the template arguments in
3961 ARGS. */
3962 tree
3963 expand_template_argument_pack (tree args)
3965 if (args == error_mark_node)
3966 return error_mark_node;
3968 tree result_args = NULL_TREE;
3969 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3970 int num_result_args = -1;
3971 int non_default_args_count = -1;
3973 /* First, determine if we need to expand anything, and the number of
3974 slots we'll need. */
3975 for (in_arg = 0; in_arg < nargs; ++in_arg)
3977 tree arg = TREE_VEC_ELT (args, in_arg);
3978 if (arg == NULL_TREE)
3979 return args;
3980 if (ARGUMENT_PACK_P (arg))
3982 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3983 if (num_result_args < 0)
3984 num_result_args = in_arg + num_packed;
3985 else
3986 num_result_args += num_packed;
3988 else
3990 if (num_result_args >= 0)
3991 num_result_args++;
3995 /* If no expansion is necessary, we're done. */
3996 if (num_result_args < 0)
3997 return args;
3999 /* Expand arguments. */
4000 result_args = make_tree_vec (num_result_args);
4001 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4002 non_default_args_count =
4003 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4004 for (in_arg = 0; in_arg < nargs; ++in_arg)
4006 tree arg = TREE_VEC_ELT (args, in_arg);
4007 if (ARGUMENT_PACK_P (arg))
4009 tree packed = ARGUMENT_PACK_ARGS (arg);
4010 int i, num_packed = TREE_VEC_LENGTH (packed);
4011 for (i = 0; i < num_packed; ++i, ++out_arg)
4012 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4013 if (non_default_args_count > 0)
4014 non_default_args_count += num_packed - 1;
4016 else
4018 TREE_VEC_ELT (result_args, out_arg) = arg;
4019 ++out_arg;
4022 if (non_default_args_count >= 0)
4023 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4024 return result_args;
4027 /* Checks if DECL shadows a template parameter.
4029 [temp.local]: A template-parameter shall not be redeclared within its
4030 scope (including nested scopes).
4032 Emits an error and returns TRUE if the DECL shadows a parameter,
4033 returns FALSE otherwise. */
4035 bool
4036 check_template_shadow (tree decl)
4038 tree olddecl;
4040 /* If we're not in a template, we can't possibly shadow a template
4041 parameter. */
4042 if (!current_template_parms)
4043 return true;
4045 /* Figure out what we're shadowing. */
4046 decl = OVL_FIRST (decl);
4047 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4049 /* If there's no previous binding for this name, we're not shadowing
4050 anything, let alone a template parameter. */
4051 if (!olddecl)
4052 return true;
4054 /* If we're not shadowing a template parameter, we're done. Note
4055 that OLDDECL might be an OVERLOAD (or perhaps even an
4056 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4057 node. */
4058 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4059 return true;
4061 /* We check for decl != olddecl to avoid bogus errors for using a
4062 name inside a class. We check TPFI to avoid duplicate errors for
4063 inline member templates. */
4064 if (decl == olddecl
4065 || (DECL_TEMPLATE_PARM_P (decl)
4066 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4067 return true;
4069 /* Don't complain about the injected class name, as we've already
4070 complained about the class itself. */
4071 if (DECL_SELF_REFERENCE_P (decl))
4072 return false;
4074 if (DECL_TEMPLATE_PARM_P (decl))
4075 error ("declaration of template parameter %q+D shadows "
4076 "template parameter", decl);
4077 else
4078 error ("declaration of %q+#D shadows template parameter", decl);
4079 inform (DECL_SOURCE_LOCATION (olddecl),
4080 "template parameter %qD declared here", olddecl);
4081 return false;
4084 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4085 ORIG_LEVEL, DECL, and TYPE. */
4087 static tree
4088 build_template_parm_index (int index,
4089 int level,
4090 int orig_level,
4091 tree decl,
4092 tree type)
4094 tree t = make_node (TEMPLATE_PARM_INDEX);
4095 TEMPLATE_PARM_IDX (t) = index;
4096 TEMPLATE_PARM_LEVEL (t) = level;
4097 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4098 TEMPLATE_PARM_DECL (t) = decl;
4099 TREE_TYPE (t) = type;
4100 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4101 TREE_READONLY (t) = TREE_READONLY (decl);
4103 return t;
4106 /* Find the canonical type parameter for the given template type
4107 parameter. Returns the canonical type parameter, which may be TYPE
4108 if no such parameter existed. */
4110 static tree
4111 canonical_type_parameter (tree type)
4113 tree list;
4114 int idx = TEMPLATE_TYPE_IDX (type);
4115 if (!canonical_template_parms)
4116 vec_alloc (canonical_template_parms, idx + 1);
4118 if (canonical_template_parms->length () <= (unsigned) idx)
4119 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4121 list = (*canonical_template_parms)[idx];
4122 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4123 list = TREE_CHAIN (list);
4125 if (list)
4126 return TREE_VALUE (list);
4127 else
4129 (*canonical_template_parms)[idx]
4130 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4131 return type;
4135 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4136 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4137 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4138 new one is created. */
4140 static tree
4141 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4142 tsubst_flags_t complain)
4144 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4145 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4146 != TEMPLATE_PARM_LEVEL (index) - levels)
4147 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4149 tree orig_decl = TEMPLATE_PARM_DECL (index);
4150 tree decl, t;
4152 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4153 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4154 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4155 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4156 DECL_ARTIFICIAL (decl) = 1;
4157 SET_DECL_TEMPLATE_PARM_P (decl);
4159 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4160 TEMPLATE_PARM_LEVEL (index) - levels,
4161 TEMPLATE_PARM_ORIG_LEVEL (index),
4162 decl, type);
4163 TEMPLATE_PARM_DESCENDANTS (index) = t;
4164 TEMPLATE_PARM_PARAMETER_PACK (t)
4165 = TEMPLATE_PARM_PARAMETER_PACK (index);
4167 /* Template template parameters need this. */
4168 if (TREE_CODE (decl) == TEMPLATE_DECL)
4170 DECL_TEMPLATE_RESULT (decl)
4171 = build_decl (DECL_SOURCE_LOCATION (decl),
4172 TYPE_DECL, DECL_NAME (decl), type);
4173 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4174 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4175 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4179 return TEMPLATE_PARM_DESCENDANTS (index);
4182 /* Process information from new template parameter PARM and append it
4183 to the LIST being built. This new parameter is a non-type
4184 parameter iff IS_NON_TYPE is true. This new parameter is a
4185 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4186 is in PARM_LOC. */
4188 tree
4189 process_template_parm (tree list, location_t parm_loc, tree parm,
4190 bool is_non_type, bool is_parameter_pack)
4192 tree decl = 0;
4193 int idx = 0;
4195 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4196 tree defval = TREE_PURPOSE (parm);
4197 tree constr = TREE_TYPE (parm);
4199 if (list)
4201 tree p = tree_last (list);
4203 if (p && TREE_VALUE (p) != error_mark_node)
4205 p = TREE_VALUE (p);
4206 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4207 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4208 else
4209 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4212 ++idx;
4215 if (is_non_type)
4217 parm = TREE_VALUE (parm);
4219 SET_DECL_TEMPLATE_PARM_P (parm);
4221 if (TREE_TYPE (parm) != error_mark_node)
4223 /* [temp.param]
4225 The top-level cv-qualifiers on the template-parameter are
4226 ignored when determining its type. */
4227 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4228 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4229 TREE_TYPE (parm) = error_mark_node;
4230 else if (uses_parameter_packs (TREE_TYPE (parm))
4231 && !is_parameter_pack
4232 /* If we're in a nested template parameter list, the template
4233 template parameter could be a parameter pack. */
4234 && processing_template_parmlist == 1)
4236 /* This template parameter is not a parameter pack, but it
4237 should be. Complain about "bare" parameter packs. */
4238 check_for_bare_parameter_packs (TREE_TYPE (parm));
4240 /* Recover by calling this a parameter pack. */
4241 is_parameter_pack = true;
4245 /* A template parameter is not modifiable. */
4246 TREE_CONSTANT (parm) = 1;
4247 TREE_READONLY (parm) = 1;
4248 decl = build_decl (parm_loc,
4249 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4250 TREE_CONSTANT (decl) = 1;
4251 TREE_READONLY (decl) = 1;
4252 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4253 = build_template_parm_index (idx, processing_template_decl,
4254 processing_template_decl,
4255 decl, TREE_TYPE (parm));
4257 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4258 = is_parameter_pack;
4260 else
4262 tree t;
4263 parm = TREE_VALUE (TREE_VALUE (parm));
4265 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4267 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4268 /* This is for distinguishing between real templates and template
4269 template parameters */
4270 TREE_TYPE (parm) = t;
4271 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4272 decl = parm;
4274 else
4276 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4277 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4278 decl = build_decl (parm_loc,
4279 TYPE_DECL, parm, t);
4282 TYPE_NAME (t) = decl;
4283 TYPE_STUB_DECL (t) = decl;
4284 parm = decl;
4285 TEMPLATE_TYPE_PARM_INDEX (t)
4286 = build_template_parm_index (idx, processing_template_decl,
4287 processing_template_decl,
4288 decl, TREE_TYPE (parm));
4289 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4290 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4292 DECL_ARTIFICIAL (decl) = 1;
4293 SET_DECL_TEMPLATE_PARM_P (decl);
4295 /* Build requirements for the type/template parameter.
4296 This must be done after SET_DECL_TEMPLATE_PARM_P or
4297 process_template_parm could fail. */
4298 tree reqs = finish_shorthand_constraint (parm, constr);
4300 pushdecl (decl);
4302 /* Build the parameter node linking the parameter declaration,
4303 its default argument (if any), and its constraints (if any). */
4304 parm = build_tree_list (defval, parm);
4305 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4307 return chainon (list, parm);
4310 /* The end of a template parameter list has been reached. Process the
4311 tree list into a parameter vector, converting each parameter into a more
4312 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4313 as PARM_DECLs. */
4315 tree
4316 end_template_parm_list (tree parms)
4318 int nparms;
4319 tree parm, next;
4320 tree saved_parmlist = make_tree_vec (list_length (parms));
4322 /* Pop the dummy parameter level and add the real one. */
4323 current_template_parms = TREE_CHAIN (current_template_parms);
4325 current_template_parms
4326 = tree_cons (size_int (processing_template_decl),
4327 saved_parmlist, current_template_parms);
4329 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4331 next = TREE_CHAIN (parm);
4332 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4333 TREE_CHAIN (parm) = NULL_TREE;
4336 --processing_template_parmlist;
4338 return saved_parmlist;
4341 // Explicitly indicate the end of the template parameter list. We assume
4342 // that the current template parameters have been constructed and/or
4343 // managed explicitly, as when creating new template template parameters
4344 // from a shorthand constraint.
4345 void
4346 end_template_parm_list ()
4348 --processing_template_parmlist;
4351 /* end_template_decl is called after a template declaration is seen. */
4353 void
4354 end_template_decl (void)
4356 reset_specialization ();
4358 if (! processing_template_decl)
4359 return;
4361 /* This matches the pushlevel in begin_template_parm_list. */
4362 finish_scope ();
4364 --processing_template_decl;
4365 current_template_parms = TREE_CHAIN (current_template_parms);
4368 /* Takes a TREE_LIST representing a template parameter and convert it
4369 into an argument suitable to be passed to the type substitution
4370 functions. Note that If the TREE_LIST contains an error_mark
4371 node, the returned argument is error_mark_node. */
4373 tree
4374 template_parm_to_arg (tree t)
4377 if (t == NULL_TREE
4378 || TREE_CODE (t) != TREE_LIST)
4379 return t;
4381 if (error_operand_p (TREE_VALUE (t)))
4382 return error_mark_node;
4384 t = TREE_VALUE (t);
4386 if (TREE_CODE (t) == TYPE_DECL
4387 || TREE_CODE (t) == TEMPLATE_DECL)
4389 t = TREE_TYPE (t);
4391 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4393 /* Turn this argument into a TYPE_ARGUMENT_PACK
4394 with a single element, which expands T. */
4395 tree vec = make_tree_vec (1);
4396 if (CHECKING_P)
4397 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4399 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4401 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4402 SET_ARGUMENT_PACK_ARGS (t, vec);
4405 else
4407 t = DECL_INITIAL (t);
4409 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4411 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4412 with a single element, which expands T. */
4413 tree vec = make_tree_vec (1);
4414 if (CHECKING_P)
4415 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4417 t = convert_from_reference (t);
4418 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4420 t = make_node (NONTYPE_ARGUMENT_PACK);
4421 SET_ARGUMENT_PACK_ARGS (t, vec);
4423 else
4424 t = convert_from_reference (t);
4426 return t;
4429 /* Given a single level of template parameters (a TREE_VEC), return it
4430 as a set of template arguments. */
4432 static tree
4433 template_parms_level_to_args (tree parms)
4435 tree a = copy_node (parms);
4436 TREE_TYPE (a) = NULL_TREE;
4437 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4438 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4440 if (CHECKING_P)
4441 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4443 return a;
4446 /* Given a set of template parameters, return them as a set of template
4447 arguments. The template parameters are represented as a TREE_VEC, in
4448 the form documented in cp-tree.h for template arguments. */
4450 static tree
4451 template_parms_to_args (tree parms)
4453 tree header;
4454 tree args = NULL_TREE;
4455 int length = TMPL_PARMS_DEPTH (parms);
4456 int l = length;
4458 /* If there is only one level of template parameters, we do not
4459 create a TREE_VEC of TREE_VECs. Instead, we return a single
4460 TREE_VEC containing the arguments. */
4461 if (length > 1)
4462 args = make_tree_vec (length);
4464 for (header = parms; header; header = TREE_CHAIN (header))
4466 tree a = template_parms_level_to_args (TREE_VALUE (header));
4468 if (length > 1)
4469 TREE_VEC_ELT (args, --l) = a;
4470 else
4471 args = a;
4474 return args;
4477 /* Within the declaration of a template, return the currently active
4478 template parameters as an argument TREE_VEC. */
4480 static tree
4481 current_template_args (void)
4483 return template_parms_to_args (current_template_parms);
4486 /* Update the declared TYPE by doing any lookups which were thought to be
4487 dependent, but are not now that we know the SCOPE of the declarator. */
4489 tree
4490 maybe_update_decl_type (tree orig_type, tree scope)
4492 tree type = orig_type;
4494 if (type == NULL_TREE)
4495 return type;
4497 if (TREE_CODE (orig_type) == TYPE_DECL)
4498 type = TREE_TYPE (type);
4500 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4501 && dependent_type_p (type)
4502 /* Don't bother building up the args in this case. */
4503 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4505 /* tsubst in the args corresponding to the template parameters,
4506 including auto if present. Most things will be unchanged, but
4507 make_typename_type and tsubst_qualified_id will resolve
4508 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4509 tree args = current_template_args ();
4510 tree auto_node = type_uses_auto (type);
4511 tree pushed;
4512 if (auto_node)
4514 tree auto_vec = make_tree_vec (1);
4515 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4516 args = add_to_template_args (args, auto_vec);
4518 pushed = push_scope (scope);
4519 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4520 if (pushed)
4521 pop_scope (scope);
4524 if (type == error_mark_node)
4525 return orig_type;
4527 if (TREE_CODE (orig_type) == TYPE_DECL)
4529 if (same_type_p (type, TREE_TYPE (orig_type)))
4530 type = orig_type;
4531 else
4532 type = TYPE_NAME (type);
4534 return type;
4537 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4538 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4539 the new template is a member template. */
4541 tree
4542 build_template_decl (tree decl, tree parms, bool member_template_p)
4544 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4545 DECL_TEMPLATE_PARMS (tmpl) = parms;
4546 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4547 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4548 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4550 return tmpl;
4553 struct template_parm_data
4555 /* The level of the template parameters we are currently
4556 processing. */
4557 int level;
4559 /* The index of the specialization argument we are currently
4560 processing. */
4561 int current_arg;
4563 /* An array whose size is the number of template parameters. The
4564 elements are nonzero if the parameter has been used in any one
4565 of the arguments processed so far. */
4566 int* parms;
4568 /* An array whose size is the number of template arguments. The
4569 elements are nonzero if the argument makes use of template
4570 parameters of this level. */
4571 int* arg_uses_template_parms;
4574 /* Subroutine of push_template_decl used to see if each template
4575 parameter in a partial specialization is used in the explicit
4576 argument list. If T is of the LEVEL given in DATA (which is
4577 treated as a template_parm_data*), then DATA->PARMS is marked
4578 appropriately. */
4580 static int
4581 mark_template_parm (tree t, void* data)
4583 int level;
4584 int idx;
4585 struct template_parm_data* tpd = (struct template_parm_data*) data;
4587 template_parm_level_and_index (t, &level, &idx);
4589 if (level == tpd->level)
4591 tpd->parms[idx] = 1;
4592 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4595 /* In C++17 the type of a non-type argument is a deduced context. */
4596 if (cxx_dialect >= cxx17
4597 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4598 for_each_template_parm (TREE_TYPE (t),
4599 &mark_template_parm,
4600 data,
4601 NULL,
4602 /*include_nondeduced_p=*/false);
4604 /* Return zero so that for_each_template_parm will continue the
4605 traversal of the tree; we want to mark *every* template parm. */
4606 return 0;
4609 /* Process the partial specialization DECL. */
4611 static tree
4612 process_partial_specialization (tree decl)
4614 tree type = TREE_TYPE (decl);
4615 tree tinfo = get_template_info (decl);
4616 tree maintmpl = TI_TEMPLATE (tinfo);
4617 tree specargs = TI_ARGS (tinfo);
4618 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4619 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4620 tree inner_parms;
4621 tree inst;
4622 int nargs = TREE_VEC_LENGTH (inner_args);
4623 int ntparms;
4624 int i;
4625 bool did_error_intro = false;
4626 struct template_parm_data tpd;
4627 struct template_parm_data tpd2;
4629 gcc_assert (current_template_parms);
4631 /* A concept cannot be specialized. */
4632 if (flag_concepts && variable_concept_p (maintmpl))
4634 error ("specialization of variable concept %q#D", maintmpl);
4635 return error_mark_node;
4638 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4639 ntparms = TREE_VEC_LENGTH (inner_parms);
4641 /* We check that each of the template parameters given in the
4642 partial specialization is used in the argument list to the
4643 specialization. For example:
4645 template <class T> struct S;
4646 template <class T> struct S<T*>;
4648 The second declaration is OK because `T*' uses the template
4649 parameter T, whereas
4651 template <class T> struct S<int>;
4653 is no good. Even trickier is:
4655 template <class T>
4656 struct S1
4658 template <class U>
4659 struct S2;
4660 template <class U>
4661 struct S2<T>;
4664 The S2<T> declaration is actually invalid; it is a
4665 full-specialization. Of course,
4667 template <class U>
4668 struct S2<T (*)(U)>;
4670 or some such would have been OK. */
4671 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4672 tpd.parms = XALLOCAVEC (int, ntparms);
4673 memset (tpd.parms, 0, sizeof (int) * ntparms);
4675 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4676 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4677 for (i = 0; i < nargs; ++i)
4679 tpd.current_arg = i;
4680 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4681 &mark_template_parm,
4682 &tpd,
4683 NULL,
4684 /*include_nondeduced_p=*/false);
4686 for (i = 0; i < ntparms; ++i)
4687 if (tpd.parms[i] == 0)
4689 /* One of the template parms was not used in a deduced context in the
4690 specialization. */
4691 if (!did_error_intro)
4693 error ("template parameters not deducible in "
4694 "partial specialization:");
4695 did_error_intro = true;
4698 inform (input_location, " %qD",
4699 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4702 if (did_error_intro)
4703 return error_mark_node;
4705 /* [temp.class.spec]
4707 The argument list of the specialization shall not be identical to
4708 the implicit argument list of the primary template. */
4709 tree main_args
4710 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4711 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4712 && (!flag_concepts
4713 || !strictly_subsumes (current_template_constraints (),
4714 get_constraints (maintmpl))))
4716 if (!flag_concepts)
4717 error ("partial specialization %q+D does not specialize "
4718 "any template arguments", decl);
4719 else
4720 error ("partial specialization %q+D does not specialize any "
4721 "template arguments and is not more constrained than", decl);
4722 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4725 /* A partial specialization that replaces multiple parameters of the
4726 primary template with a pack expansion is less specialized for those
4727 parameters. */
4728 if (nargs < DECL_NTPARMS (maintmpl))
4730 error ("partial specialization is not more specialized than the "
4731 "primary template because it replaces multiple parameters "
4732 "with a pack expansion");
4733 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4734 /* Avoid crash in process_partial_specialization. */
4735 return decl;
4738 /* If we aren't in a dependent class, we can actually try deduction. */
4739 else if (tpd.level == 1
4740 /* FIXME we should be able to handle a partial specialization of a
4741 partial instantiation, but currently we can't (c++/41727). */
4742 && TMPL_ARGS_DEPTH (specargs) == 1
4743 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4745 if (permerror (input_location, "partial specialization %qD is not "
4746 "more specialized than", decl))
4747 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4748 maintmpl);
4751 /* [temp.class.spec]
4753 A partially specialized non-type argument expression shall not
4754 involve template parameters of the partial specialization except
4755 when the argument expression is a simple identifier.
4757 The type of a template parameter corresponding to a specialized
4758 non-type argument shall not be dependent on a parameter of the
4759 specialization.
4761 Also, we verify that pack expansions only occur at the
4762 end of the argument list. */
4763 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4764 tpd2.parms = 0;
4765 for (i = 0; i < nargs; ++i)
4767 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4768 tree arg = TREE_VEC_ELT (inner_args, i);
4769 tree packed_args = NULL_TREE;
4770 int j, len = 1;
4772 if (ARGUMENT_PACK_P (arg))
4774 /* Extract the arguments from the argument pack. We'll be
4775 iterating over these in the following loop. */
4776 packed_args = ARGUMENT_PACK_ARGS (arg);
4777 len = TREE_VEC_LENGTH (packed_args);
4780 for (j = 0; j < len; j++)
4782 if (packed_args)
4783 /* Get the Jth argument in the parameter pack. */
4784 arg = TREE_VEC_ELT (packed_args, j);
4786 if (PACK_EXPANSION_P (arg))
4788 /* Pack expansions must come at the end of the
4789 argument list. */
4790 if ((packed_args && j < len - 1)
4791 || (!packed_args && i < nargs - 1))
4793 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4794 error ("parameter pack argument %qE must be at the "
4795 "end of the template argument list", arg);
4796 else
4797 error ("parameter pack argument %qT must be at the "
4798 "end of the template argument list", arg);
4802 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4803 /* We only care about the pattern. */
4804 arg = PACK_EXPANSION_PATTERN (arg);
4806 if (/* These first two lines are the `non-type' bit. */
4807 !TYPE_P (arg)
4808 && TREE_CODE (arg) != TEMPLATE_DECL
4809 /* This next two lines are the `argument expression is not just a
4810 simple identifier' condition and also the `specialized
4811 non-type argument' bit. */
4812 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4813 && !(REFERENCE_REF_P (arg)
4814 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4816 if ((!packed_args && tpd.arg_uses_template_parms[i])
4817 || (packed_args && uses_template_parms (arg)))
4818 error ("template argument %qE involves template parameter(s)",
4819 arg);
4820 else
4822 /* Look at the corresponding template parameter,
4823 marking which template parameters its type depends
4824 upon. */
4825 tree type = TREE_TYPE (parm);
4827 if (!tpd2.parms)
4829 /* We haven't yet initialized TPD2. Do so now. */
4830 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4831 /* The number of parameters here is the number in the
4832 main template, which, as checked in the assertion
4833 above, is NARGS. */
4834 tpd2.parms = XALLOCAVEC (int, nargs);
4835 tpd2.level =
4836 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4839 /* Mark the template parameters. But this time, we're
4840 looking for the template parameters of the main
4841 template, not in the specialization. */
4842 tpd2.current_arg = i;
4843 tpd2.arg_uses_template_parms[i] = 0;
4844 memset (tpd2.parms, 0, sizeof (int) * nargs);
4845 for_each_template_parm (type,
4846 &mark_template_parm,
4847 &tpd2,
4848 NULL,
4849 /*include_nondeduced_p=*/false);
4851 if (tpd2.arg_uses_template_parms [i])
4853 /* The type depended on some template parameters.
4854 If they are fully specialized in the
4855 specialization, that's OK. */
4856 int j;
4857 int count = 0;
4858 for (j = 0; j < nargs; ++j)
4859 if (tpd2.parms[j] != 0
4860 && tpd.arg_uses_template_parms [j])
4861 ++count;
4862 if (count != 0)
4863 error_n (input_location, count,
4864 "type %qT of template argument %qE depends "
4865 "on a template parameter",
4866 "type %qT of template argument %qE depends "
4867 "on template parameters",
4868 type,
4869 arg);
4876 /* We should only get here once. */
4877 if (TREE_CODE (decl) == TYPE_DECL)
4878 gcc_assert (!COMPLETE_TYPE_P (type));
4880 // Build the template decl.
4881 tree tmpl = build_template_decl (decl, current_template_parms,
4882 DECL_MEMBER_TEMPLATE_P (maintmpl));
4883 TREE_TYPE (tmpl) = type;
4884 DECL_TEMPLATE_RESULT (tmpl) = decl;
4885 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4886 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4887 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4889 /* Give template template parms a DECL_CONTEXT of the template
4890 for which they are a parameter. */
4891 for (i = 0; i < ntparms; ++i)
4893 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4894 if (TREE_CODE (parm) == TEMPLATE_DECL)
4895 DECL_CONTEXT (parm) = tmpl;
4898 if (VAR_P (decl))
4899 /* We didn't register this in check_explicit_specialization so we could
4900 wait until the constraints were set. */
4901 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4902 else
4903 associate_classtype_constraints (type);
4905 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4906 = tree_cons (specargs, tmpl,
4907 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4908 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4910 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4911 inst = TREE_CHAIN (inst))
4913 tree instance = TREE_VALUE (inst);
4914 if (TYPE_P (instance)
4915 ? (COMPLETE_TYPE_P (instance)
4916 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4917 : DECL_TEMPLATE_INSTANTIATION (instance))
4919 tree spec = most_specialized_partial_spec (instance, tf_none);
4920 tree inst_decl = (DECL_P (instance)
4921 ? instance : TYPE_NAME (instance));
4922 if (!spec)
4923 /* OK */;
4924 else if (spec == error_mark_node)
4925 permerror (input_location,
4926 "declaration of %qD ambiguates earlier template "
4927 "instantiation for %qD", decl, inst_decl);
4928 else if (TREE_VALUE (spec) == tmpl)
4929 permerror (input_location,
4930 "partial specialization of %qD after instantiation "
4931 "of %qD", decl, inst_decl);
4935 return decl;
4938 /* PARM is a template parameter of some form; return the corresponding
4939 TEMPLATE_PARM_INDEX. */
4941 static tree
4942 get_template_parm_index (tree parm)
4944 if (TREE_CODE (parm) == PARM_DECL
4945 || TREE_CODE (parm) == CONST_DECL)
4946 parm = DECL_INITIAL (parm);
4947 else if (TREE_CODE (parm) == TYPE_DECL
4948 || TREE_CODE (parm) == TEMPLATE_DECL)
4949 parm = TREE_TYPE (parm);
4950 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4951 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4952 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4953 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4954 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4955 return parm;
4958 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4959 parameter packs used by the template parameter PARM. */
4961 static void
4962 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4964 /* A type parm can't refer to another parm. */
4965 if (TREE_CODE (parm) == TYPE_DECL)
4966 return;
4967 else if (TREE_CODE (parm) == PARM_DECL)
4969 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4970 ppd, ppd->visited);
4971 return;
4974 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4976 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4977 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4978 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4981 /* PARM is a template parameter pack. Return any parameter packs used in
4982 its type or the type of any of its template parameters. If there are
4983 any such packs, it will be instantiated into a fixed template parameter
4984 list by partial instantiation rather than be fully deduced. */
4986 tree
4987 fixed_parameter_pack_p (tree parm)
4989 /* This can only be true in a member template. */
4990 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4991 return NULL_TREE;
4992 /* This can only be true for a parameter pack. */
4993 if (!template_parameter_pack_p (parm))
4994 return NULL_TREE;
4995 /* A type parm can't refer to another parm. */
4996 if (TREE_CODE (parm) == TYPE_DECL)
4997 return NULL_TREE;
4999 tree parameter_packs = NULL_TREE;
5000 struct find_parameter_pack_data ppd;
5001 ppd.parameter_packs = &parameter_packs;
5002 ppd.visited = new hash_set<tree>;
5003 ppd.type_pack_expansion_p = false;
5005 fixed_parameter_pack_p_1 (parm, &ppd);
5007 delete ppd.visited;
5008 return parameter_packs;
5011 /* Check that a template declaration's use of default arguments and
5012 parameter packs is not invalid. Here, PARMS are the template
5013 parameters. IS_PRIMARY is true if DECL is the thing declared by
5014 a primary template. IS_PARTIAL is true if DECL is a partial
5015 specialization.
5017 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5018 function template declaration or a friend class template
5019 declaration. In the function case, 1 indicates a declaration, 2
5020 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5021 emitted for extraneous default arguments.
5023 Returns TRUE if there were no errors found, FALSE otherwise. */
5025 bool
5026 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5027 bool is_partial, int is_friend_decl)
5029 const char *msg;
5030 int last_level_to_check;
5031 tree parm_level;
5032 bool no_errors = true;
5034 /* [temp.param]
5036 A default template-argument shall not be specified in a
5037 function template declaration or a function template definition, nor
5038 in the template-parameter-list of the definition of a member of a
5039 class template. */
5041 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5042 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5043 /* You can't have a function template declaration in a local
5044 scope, nor you can you define a member of a class template in a
5045 local scope. */
5046 return true;
5048 if ((TREE_CODE (decl) == TYPE_DECL
5049 && TREE_TYPE (decl)
5050 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5051 || (TREE_CODE (decl) == FUNCTION_DECL
5052 && LAMBDA_FUNCTION_P (decl)))
5053 /* A lambda doesn't have an explicit declaration; don't complain
5054 about the parms of the enclosing class. */
5055 return true;
5057 if (current_class_type
5058 && !TYPE_BEING_DEFINED (current_class_type)
5059 && DECL_LANG_SPECIFIC (decl)
5060 && DECL_DECLARES_FUNCTION_P (decl)
5061 /* If this is either a friend defined in the scope of the class
5062 or a member function. */
5063 && (DECL_FUNCTION_MEMBER_P (decl)
5064 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5065 : DECL_FRIEND_CONTEXT (decl)
5066 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5067 : false)
5068 /* And, if it was a member function, it really was defined in
5069 the scope of the class. */
5070 && (!DECL_FUNCTION_MEMBER_P (decl)
5071 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5072 /* We already checked these parameters when the template was
5073 declared, so there's no need to do it again now. This function
5074 was defined in class scope, but we're processing its body now
5075 that the class is complete. */
5076 return true;
5078 /* Core issue 226 (C++0x only): the following only applies to class
5079 templates. */
5080 if (is_primary
5081 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5083 /* [temp.param]
5085 If a template-parameter has a default template-argument, all
5086 subsequent template-parameters shall have a default
5087 template-argument supplied. */
5088 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5090 tree inner_parms = TREE_VALUE (parm_level);
5091 int ntparms = TREE_VEC_LENGTH (inner_parms);
5092 int seen_def_arg_p = 0;
5093 int i;
5095 for (i = 0; i < ntparms; ++i)
5097 tree parm = TREE_VEC_ELT (inner_parms, i);
5099 if (parm == error_mark_node)
5100 continue;
5102 if (TREE_PURPOSE (parm))
5103 seen_def_arg_p = 1;
5104 else if (seen_def_arg_p
5105 && !template_parameter_pack_p (TREE_VALUE (parm)))
5107 error ("no default argument for %qD", TREE_VALUE (parm));
5108 /* For better subsequent error-recovery, we indicate that
5109 there should have been a default argument. */
5110 TREE_PURPOSE (parm) = error_mark_node;
5111 no_errors = false;
5113 else if (!is_partial
5114 && !is_friend_decl
5115 /* Don't complain about an enclosing partial
5116 specialization. */
5117 && parm_level == parms
5118 && TREE_CODE (decl) == TYPE_DECL
5119 && i < ntparms - 1
5120 && template_parameter_pack_p (TREE_VALUE (parm))
5121 /* A fixed parameter pack will be partially
5122 instantiated into a fixed length list. */
5123 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5125 /* A primary class template can only have one
5126 parameter pack, at the end of the template
5127 parameter list. */
5129 error ("parameter pack %q+D must be at the end of the"
5130 " template parameter list", TREE_VALUE (parm));
5132 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5133 = error_mark_node;
5134 no_errors = false;
5140 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5141 || is_partial
5142 || !is_primary
5143 || is_friend_decl)
5144 /* For an ordinary class template, default template arguments are
5145 allowed at the innermost level, e.g.:
5146 template <class T = int>
5147 struct S {};
5148 but, in a partial specialization, they're not allowed even
5149 there, as we have in [temp.class.spec]:
5151 The template parameter list of a specialization shall not
5152 contain default template argument values.
5154 So, for a partial specialization, or for a function template
5155 (in C++98/C++03), we look at all of them. */
5157 else
5158 /* But, for a primary class template that is not a partial
5159 specialization we look at all template parameters except the
5160 innermost ones. */
5161 parms = TREE_CHAIN (parms);
5163 /* Figure out what error message to issue. */
5164 if (is_friend_decl == 2)
5165 msg = G_("default template arguments may not be used in function template "
5166 "friend re-declaration");
5167 else if (is_friend_decl)
5168 msg = G_("default template arguments may not be used in template "
5169 "friend declarations");
5170 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5171 msg = G_("default template arguments may not be used in function templates "
5172 "without -std=c++11 or -std=gnu++11");
5173 else if (is_partial)
5174 msg = G_("default template arguments may not be used in "
5175 "partial specializations");
5176 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5177 msg = G_("default argument for template parameter for class enclosing %qD");
5178 else
5179 /* Per [temp.param]/9, "A default template-argument shall not be
5180 specified in the template-parameter-lists of the definition of
5181 a member of a class template that appears outside of the member's
5182 class.", thus if we aren't handling a member of a class template
5183 there is no need to examine the parameters. */
5184 return true;
5186 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5187 /* If we're inside a class definition, there's no need to
5188 examine the parameters to the class itself. On the one
5189 hand, they will be checked when the class is defined, and,
5190 on the other, default arguments are valid in things like:
5191 template <class T = double>
5192 struct S { template <class U> void f(U); };
5193 Here the default argument for `S' has no bearing on the
5194 declaration of `f'. */
5195 last_level_to_check = template_class_depth (current_class_type) + 1;
5196 else
5197 /* Check everything. */
5198 last_level_to_check = 0;
5200 for (parm_level = parms;
5201 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5202 parm_level = TREE_CHAIN (parm_level))
5204 tree inner_parms = TREE_VALUE (parm_level);
5205 int i;
5206 int ntparms;
5208 ntparms = TREE_VEC_LENGTH (inner_parms);
5209 for (i = 0; i < ntparms; ++i)
5211 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5212 continue;
5214 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5216 if (msg)
5218 no_errors = false;
5219 if (is_friend_decl == 2)
5220 return no_errors;
5222 error (msg, decl);
5223 msg = 0;
5226 /* Clear out the default argument so that we are not
5227 confused later. */
5228 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5232 /* At this point, if we're still interested in issuing messages,
5233 they must apply to classes surrounding the object declared. */
5234 if (msg)
5235 msg = G_("default argument for template parameter for class "
5236 "enclosing %qD");
5239 return no_errors;
5242 /* Worker for push_template_decl_real, called via
5243 for_each_template_parm. DATA is really an int, indicating the
5244 level of the parameters we are interested in. If T is a template
5245 parameter of that level, return nonzero. */
5247 static int
5248 template_parm_this_level_p (tree t, void* data)
5250 int this_level = *(int *)data;
5251 int level;
5253 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5254 level = TEMPLATE_PARM_LEVEL (t);
5255 else
5256 level = TEMPLATE_TYPE_LEVEL (t);
5257 return level == this_level;
5260 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5261 DATA is really an int, indicating the innermost outer level of parameters.
5262 If T is a template parameter of that level or further out, return
5263 nonzero. */
5265 static int
5266 template_parm_outer_level (tree t, void *data)
5268 int this_level = *(int *)data;
5269 int level;
5271 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5272 level = TEMPLATE_PARM_LEVEL (t);
5273 else
5274 level = TEMPLATE_TYPE_LEVEL (t);
5275 return level <= this_level;
5278 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5279 parameters given by current_template_args, or reuses a
5280 previously existing one, if appropriate. Returns the DECL, or an
5281 equivalent one, if it is replaced via a call to duplicate_decls.
5283 If IS_FRIEND is true, DECL is a friend declaration. */
5285 tree
5286 push_template_decl_real (tree decl, bool is_friend)
5288 tree tmpl;
5289 tree args;
5290 tree info;
5291 tree ctx;
5292 bool is_primary;
5293 bool is_partial;
5294 int new_template_p = 0;
5295 /* True if the template is a member template, in the sense of
5296 [temp.mem]. */
5297 bool member_template_p = false;
5299 if (decl == error_mark_node || !current_template_parms)
5300 return error_mark_node;
5302 /* See if this is a partial specialization. */
5303 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5304 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5305 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5306 || (VAR_P (decl)
5307 && DECL_LANG_SPECIFIC (decl)
5308 && DECL_TEMPLATE_SPECIALIZATION (decl)
5309 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5311 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5312 is_friend = true;
5314 if (is_friend)
5315 /* For a friend, we want the context of the friend, not
5316 the type of which it is a friend. */
5317 ctx = CP_DECL_CONTEXT (decl);
5318 else if (CP_DECL_CONTEXT (decl)
5319 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5320 /* In the case of a virtual function, we want the class in which
5321 it is defined. */
5322 ctx = CP_DECL_CONTEXT (decl);
5323 else
5324 /* Otherwise, if we're currently defining some class, the DECL
5325 is assumed to be a member of the class. */
5326 ctx = current_scope ();
5328 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5329 ctx = NULL_TREE;
5331 if (!DECL_CONTEXT (decl))
5332 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5334 /* See if this is a primary template. */
5335 if (is_friend && ctx
5336 && uses_template_parms_level (ctx, processing_template_decl))
5337 /* A friend template that specifies a class context, i.e.
5338 template <typename T> friend void A<T>::f();
5339 is not primary. */
5340 is_primary = false;
5341 else if (TREE_CODE (decl) == TYPE_DECL
5342 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5343 is_primary = false;
5344 else
5345 is_primary = template_parm_scope_p ();
5347 if (is_primary)
5349 warning (OPT_Wtemplates, "template %qD declared", decl);
5351 if (DECL_CLASS_SCOPE_P (decl))
5352 member_template_p = true;
5353 if (TREE_CODE (decl) == TYPE_DECL
5354 && anon_aggrname_p (DECL_NAME (decl)))
5356 error ("template class without a name");
5357 return error_mark_node;
5359 else if (TREE_CODE (decl) == FUNCTION_DECL)
5361 if (member_template_p)
5363 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5364 error ("member template %qD may not have virt-specifiers", decl);
5366 if (DECL_DESTRUCTOR_P (decl))
5368 /* [temp.mem]
5370 A destructor shall not be a member template. */
5371 error ("destructor %qD declared as member template", decl);
5372 return error_mark_node;
5374 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5375 && (!prototype_p (TREE_TYPE (decl))
5376 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5377 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5378 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5379 == void_list_node)))
5381 /* [basic.stc.dynamic.allocation]
5383 An allocation function can be a function
5384 template. ... Template allocation functions shall
5385 have two or more parameters. */
5386 error ("invalid template declaration of %qD", decl);
5387 return error_mark_node;
5390 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5391 && CLASS_TYPE_P (TREE_TYPE (decl)))
5393 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5394 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5395 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5397 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5398 if (TREE_CODE (t) == TYPE_DECL)
5399 t = TREE_TYPE (t);
5400 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5401 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5404 else if (TREE_CODE (decl) == TYPE_DECL
5405 && TYPE_DECL_ALIAS_P (decl))
5406 /* alias-declaration */
5407 gcc_assert (!DECL_ARTIFICIAL (decl));
5408 else if (VAR_P (decl))
5409 /* C++14 variable template. */;
5410 else
5412 error ("template declaration of %q#D", decl);
5413 return error_mark_node;
5417 /* Check to see that the rules regarding the use of default
5418 arguments are not being violated. We check args for a friend
5419 functions when we know whether it's a definition, introducing
5420 declaration or re-declaration. */
5421 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5422 check_default_tmpl_args (decl, current_template_parms,
5423 is_primary, is_partial, is_friend);
5425 /* Ensure that there are no parameter packs in the type of this
5426 declaration that have not been expanded. */
5427 if (TREE_CODE (decl) == FUNCTION_DECL)
5429 /* Check each of the arguments individually to see if there are
5430 any bare parameter packs. */
5431 tree type = TREE_TYPE (decl);
5432 tree arg = DECL_ARGUMENTS (decl);
5433 tree argtype = TYPE_ARG_TYPES (type);
5435 while (arg && argtype)
5437 if (!DECL_PACK_P (arg)
5438 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5440 /* This is a PARM_DECL that contains unexpanded parameter
5441 packs. We have already complained about this in the
5442 check_for_bare_parameter_packs call, so just replace
5443 these types with ERROR_MARK_NODE. */
5444 TREE_TYPE (arg) = error_mark_node;
5445 TREE_VALUE (argtype) = error_mark_node;
5448 arg = DECL_CHAIN (arg);
5449 argtype = TREE_CHAIN (argtype);
5452 /* Check for bare parameter packs in the return type and the
5453 exception specifiers. */
5454 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5455 /* Errors were already issued, set return type to int
5456 as the frontend doesn't expect error_mark_node as
5457 the return type. */
5458 TREE_TYPE (type) = integer_type_node;
5459 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5460 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5462 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5463 && TYPE_DECL_ALIAS_P (decl))
5464 ? DECL_ORIGINAL_TYPE (decl)
5465 : TREE_TYPE (decl)))
5467 TREE_TYPE (decl) = error_mark_node;
5468 return error_mark_node;
5471 if (is_partial)
5472 return process_partial_specialization (decl);
5474 args = current_template_args ();
5476 if (!ctx
5477 || TREE_CODE (ctx) == FUNCTION_DECL
5478 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5479 || (TREE_CODE (decl) == TYPE_DECL
5480 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5481 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5483 if (DECL_LANG_SPECIFIC (decl)
5484 && DECL_TEMPLATE_INFO (decl)
5485 && DECL_TI_TEMPLATE (decl))
5486 tmpl = DECL_TI_TEMPLATE (decl);
5487 /* If DECL is a TYPE_DECL for a class-template, then there won't
5488 be DECL_LANG_SPECIFIC. The information equivalent to
5489 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5490 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5491 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5492 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5494 /* Since a template declaration already existed for this
5495 class-type, we must be redeclaring it here. Make sure
5496 that the redeclaration is valid. */
5497 redeclare_class_template (TREE_TYPE (decl),
5498 current_template_parms,
5499 current_template_constraints ());
5500 /* We don't need to create a new TEMPLATE_DECL; just use the
5501 one we already had. */
5502 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5504 else
5506 tmpl = build_template_decl (decl, current_template_parms,
5507 member_template_p);
5508 new_template_p = 1;
5510 if (DECL_LANG_SPECIFIC (decl)
5511 && DECL_TEMPLATE_SPECIALIZATION (decl))
5513 /* A specialization of a member template of a template
5514 class. */
5515 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5516 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5517 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5521 else
5523 tree a, t, current, parms;
5524 int i;
5525 tree tinfo = get_template_info (decl);
5527 if (!tinfo)
5529 error ("template definition of non-template %q#D", decl);
5530 return error_mark_node;
5533 tmpl = TI_TEMPLATE (tinfo);
5535 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5536 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5537 && DECL_TEMPLATE_SPECIALIZATION (decl)
5538 && DECL_MEMBER_TEMPLATE_P (tmpl))
5540 tree new_tmpl;
5542 /* The declaration is a specialization of a member
5543 template, declared outside the class. Therefore, the
5544 innermost template arguments will be NULL, so we
5545 replace them with the arguments determined by the
5546 earlier call to check_explicit_specialization. */
5547 args = DECL_TI_ARGS (decl);
5549 new_tmpl
5550 = build_template_decl (decl, current_template_parms,
5551 member_template_p);
5552 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5553 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5554 DECL_TI_TEMPLATE (decl) = new_tmpl;
5555 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5556 DECL_TEMPLATE_INFO (new_tmpl)
5557 = build_template_info (tmpl, args);
5559 register_specialization (new_tmpl,
5560 most_general_template (tmpl),
5561 args,
5562 is_friend, 0);
5563 return decl;
5566 /* Make sure the template headers we got make sense. */
5568 parms = DECL_TEMPLATE_PARMS (tmpl);
5569 i = TMPL_PARMS_DEPTH (parms);
5570 if (TMPL_ARGS_DEPTH (args) != i)
5572 error ("expected %d levels of template parms for %q#D, got %d",
5573 i, decl, TMPL_ARGS_DEPTH (args));
5574 DECL_INTERFACE_KNOWN (decl) = 1;
5575 return error_mark_node;
5577 else
5578 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5580 a = TMPL_ARGS_LEVEL (args, i);
5581 t = INNERMOST_TEMPLATE_PARMS (parms);
5583 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5585 if (current == decl)
5586 error ("got %d template parameters for %q#D",
5587 TREE_VEC_LENGTH (a), decl);
5588 else
5589 error ("got %d template parameters for %q#T",
5590 TREE_VEC_LENGTH (a), current);
5591 error (" but %d required", TREE_VEC_LENGTH (t));
5592 /* Avoid crash in import_export_decl. */
5593 DECL_INTERFACE_KNOWN (decl) = 1;
5594 return error_mark_node;
5597 if (current == decl)
5598 current = ctx;
5599 else if (current == NULL_TREE)
5600 /* Can happen in erroneous input. */
5601 break;
5602 else
5603 current = get_containing_scope (current);
5606 /* Check that the parms are used in the appropriate qualifying scopes
5607 in the declarator. */
5608 if (!comp_template_args
5609 (TI_ARGS (tinfo),
5610 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5612 error ("template arguments to %qD do not match original "
5613 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5614 if (!uses_template_parms (TI_ARGS (tinfo)))
5615 inform (input_location, "use %<template<>%> for"
5616 " an explicit specialization");
5617 /* Avoid crash in import_export_decl. */
5618 DECL_INTERFACE_KNOWN (decl) = 1;
5619 return error_mark_node;
5623 DECL_TEMPLATE_RESULT (tmpl) = decl;
5624 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5626 /* Push template declarations for global functions and types. Note
5627 that we do not try to push a global template friend declared in a
5628 template class; such a thing may well depend on the template
5629 parameters of the class. */
5630 if (new_template_p && !ctx
5631 && !(is_friend && template_class_depth (current_class_type) > 0))
5633 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5634 if (tmpl == error_mark_node)
5635 return error_mark_node;
5637 /* Hide template friend classes that haven't been declared yet. */
5638 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5640 DECL_ANTICIPATED (tmpl) = 1;
5641 DECL_FRIEND_P (tmpl) = 1;
5645 if (is_primary)
5647 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5649 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5651 /* Give template template parms a DECL_CONTEXT of the template
5652 for which they are a parameter. */
5653 parms = INNERMOST_TEMPLATE_PARMS (parms);
5654 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5656 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5657 if (TREE_CODE (parm) == TEMPLATE_DECL)
5658 DECL_CONTEXT (parm) = tmpl;
5661 if (TREE_CODE (decl) == TYPE_DECL
5662 && TYPE_DECL_ALIAS_P (decl)
5663 && complex_alias_template_p (tmpl))
5664 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5667 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5668 back to its most general template. If TMPL is a specialization,
5669 ARGS may only have the innermost set of arguments. Add the missing
5670 argument levels if necessary. */
5671 if (DECL_TEMPLATE_INFO (tmpl))
5672 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5674 info = build_template_info (tmpl, args);
5676 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5677 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5678 else
5680 if (is_primary)
5681 retrofit_lang_decl (decl);
5682 if (DECL_LANG_SPECIFIC (decl))
5683 DECL_TEMPLATE_INFO (decl) = info;
5686 if (flag_implicit_templates
5687 && !is_friend
5688 && TREE_PUBLIC (decl)
5689 && VAR_OR_FUNCTION_DECL_P (decl))
5690 /* Set DECL_COMDAT on template instantiations; if we force
5691 them to be emitted by explicit instantiation or -frepo,
5692 mark_needed will tell cgraph to do the right thing. */
5693 DECL_COMDAT (decl) = true;
5695 return DECL_TEMPLATE_RESULT (tmpl);
5698 tree
5699 push_template_decl (tree decl)
5701 return push_template_decl_real (decl, false);
5704 /* FN is an inheriting constructor that inherits from the constructor
5705 template INHERITED; turn FN into a constructor template with a matching
5706 template header. */
5708 tree
5709 add_inherited_template_parms (tree fn, tree inherited)
5711 tree inner_parms
5712 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5713 inner_parms = copy_node (inner_parms);
5714 tree parms
5715 = tree_cons (size_int (processing_template_decl + 1),
5716 inner_parms, current_template_parms);
5717 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5718 tree args = template_parms_to_args (parms);
5719 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5720 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5721 DECL_TEMPLATE_RESULT (tmpl) = fn;
5722 DECL_ARTIFICIAL (tmpl) = true;
5723 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5724 return tmpl;
5727 /* Called when a class template TYPE is redeclared with the indicated
5728 template PARMS, e.g.:
5730 template <class T> struct S;
5731 template <class T> struct S {}; */
5733 bool
5734 redeclare_class_template (tree type, tree parms, tree cons)
5736 tree tmpl;
5737 tree tmpl_parms;
5738 int i;
5740 if (!TYPE_TEMPLATE_INFO (type))
5742 error ("%qT is not a template type", type);
5743 return false;
5746 tmpl = TYPE_TI_TEMPLATE (type);
5747 if (!PRIMARY_TEMPLATE_P (tmpl))
5748 /* The type is nested in some template class. Nothing to worry
5749 about here; there are no new template parameters for the nested
5750 type. */
5751 return true;
5753 if (!parms)
5755 error ("template specifiers not specified in declaration of %qD",
5756 tmpl);
5757 return false;
5760 parms = INNERMOST_TEMPLATE_PARMS (parms);
5761 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5763 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5765 error_n (input_location, TREE_VEC_LENGTH (parms),
5766 "redeclared with %d template parameter",
5767 "redeclared with %d template parameters",
5768 TREE_VEC_LENGTH (parms));
5769 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5770 "previous declaration %qD used %d template parameter",
5771 "previous declaration %qD used %d template parameters",
5772 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5773 return false;
5776 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5778 tree tmpl_parm;
5779 tree parm;
5780 tree tmpl_default;
5781 tree parm_default;
5783 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5784 || TREE_VEC_ELT (parms, i) == error_mark_node)
5785 continue;
5787 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5788 if (error_operand_p (tmpl_parm))
5789 return false;
5791 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5792 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5793 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5795 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5796 TEMPLATE_DECL. */
5797 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5798 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5799 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5800 || (TREE_CODE (tmpl_parm) != PARM_DECL
5801 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5802 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5803 || (TREE_CODE (tmpl_parm) == PARM_DECL
5804 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5805 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5807 error ("template parameter %q+#D", tmpl_parm);
5808 error ("redeclared here as %q#D", parm);
5809 return false;
5812 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5814 /* We have in [temp.param]:
5816 A template-parameter may not be given default arguments
5817 by two different declarations in the same scope. */
5818 error_at (input_location, "redefinition of default argument for %q#D", parm);
5819 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5820 "original definition appeared here");
5821 return false;
5824 if (parm_default != NULL_TREE)
5825 /* Update the previous template parameters (which are the ones
5826 that will really count) with the new default value. */
5827 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5828 else if (tmpl_default != NULL_TREE)
5829 /* Update the new parameters, too; they'll be used as the
5830 parameters for any members. */
5831 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5833 /* Give each template template parm in this redeclaration a
5834 DECL_CONTEXT of the template for which they are a parameter. */
5835 if (TREE_CODE (parm) == TEMPLATE_DECL)
5837 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5838 DECL_CONTEXT (parm) = tmpl;
5841 if (TREE_CODE (parm) == TYPE_DECL)
5842 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5845 // Cannot redeclare a class template with a different set of constraints.
5846 if (!equivalent_constraints (get_constraints (tmpl), cons))
5848 error_at (input_location, "redeclaration %q#D with different "
5849 "constraints", tmpl);
5850 inform (DECL_SOURCE_LOCATION (tmpl),
5851 "original declaration appeared here");
5854 return true;
5857 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5858 to be used when the caller has already checked
5859 (processing_template_decl
5860 && !instantiation_dependent_expression_p (expr)
5861 && potential_constant_expression (expr))
5862 and cleared processing_template_decl. */
5864 tree
5865 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5867 return tsubst_copy_and_build (expr,
5868 /*args=*/NULL_TREE,
5869 complain,
5870 /*in_decl=*/NULL_TREE,
5871 /*function_p=*/false,
5872 /*integral_constant_expression_p=*/true);
5875 /* Simplify EXPR if it is a non-dependent expression. Returns the
5876 (possibly simplified) expression. */
5878 tree
5879 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5881 if (expr == NULL_TREE)
5882 return NULL_TREE;
5884 /* If we're in a template, but EXPR isn't value dependent, simplify
5885 it. We're supposed to treat:
5887 template <typename T> void f(T[1 + 1]);
5888 template <typename T> void f(T[2]);
5890 as two declarations of the same function, for example. */
5891 if (processing_template_decl
5892 && is_nondependent_constant_expression (expr))
5894 processing_template_decl_sentinel s;
5895 expr = instantiate_non_dependent_expr_internal (expr, complain);
5897 return expr;
5900 tree
5901 instantiate_non_dependent_expr (tree expr)
5903 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5906 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5907 an uninstantiated expression. */
5909 tree
5910 instantiate_non_dependent_or_null (tree expr)
5912 if (expr == NULL_TREE)
5913 return NULL_TREE;
5914 if (processing_template_decl)
5916 if (!is_nondependent_constant_expression (expr))
5917 expr = NULL_TREE;
5918 else
5920 processing_template_decl_sentinel s;
5921 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5924 return expr;
5927 /* True iff T is a specialization of a variable template. */
5929 bool
5930 variable_template_specialization_p (tree t)
5932 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5933 return false;
5934 tree tmpl = DECL_TI_TEMPLATE (t);
5935 return variable_template_p (tmpl);
5938 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5939 template declaration, or a TYPE_DECL for an alias declaration. */
5941 bool
5942 alias_type_or_template_p (tree t)
5944 if (t == NULL_TREE)
5945 return false;
5946 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5947 || (TYPE_P (t)
5948 && TYPE_NAME (t)
5949 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5950 || DECL_ALIAS_TEMPLATE_P (t));
5953 /* Return TRUE iff T is a specialization of an alias template. */
5955 bool
5956 alias_template_specialization_p (const_tree t)
5958 /* It's an alias template specialization if it's an alias and its
5959 TYPE_NAME is a specialization of a primary template. */
5960 if (TYPE_ALIAS_P (t))
5961 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5962 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5964 return false;
5967 /* An alias template is complex from a SFINAE perspective if a template-id
5968 using that alias can be ill-formed when the expansion is not, as with
5969 the void_t template. We determine this by checking whether the
5970 expansion for the alias template uses all its template parameters. */
5972 struct uses_all_template_parms_data
5974 int level;
5975 bool *seen;
5978 static int
5979 uses_all_template_parms_r (tree t, void *data_)
5981 struct uses_all_template_parms_data &data
5982 = *(struct uses_all_template_parms_data*)data_;
5983 tree idx = get_template_parm_index (t);
5985 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5986 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5987 return 0;
5990 static bool
5991 complex_alias_template_p (const_tree tmpl)
5993 struct uses_all_template_parms_data data;
5994 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5995 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5996 data.level = TMPL_PARMS_DEPTH (parms);
5997 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5998 data.seen = XALLOCAVEC (bool, len);
5999 for (int i = 0; i < len; ++i)
6000 data.seen[i] = false;
6002 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6003 for (int i = 0; i < len; ++i)
6004 if (!data.seen[i])
6005 return true;
6006 return false;
6009 /* Return TRUE iff T is a specialization of a complex alias template with
6010 dependent template-arguments. */
6012 bool
6013 dependent_alias_template_spec_p (const_tree t)
6015 if (!alias_template_specialization_p (t))
6016 return false;
6018 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6019 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6020 return false;
6022 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6023 if (!any_dependent_template_arguments_p (args))
6024 return false;
6026 return true;
6029 /* Return the number of innermost template parameters in TMPL. */
6031 static int
6032 num_innermost_template_parms (tree tmpl)
6034 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6035 return TREE_VEC_LENGTH (parms);
6038 /* Return either TMPL or another template that it is equivalent to under DR
6039 1286: An alias that just changes the name of a template is equivalent to
6040 the other template. */
6042 static tree
6043 get_underlying_template (tree tmpl)
6045 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6046 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6048 /* Determine if the alias is equivalent to an underlying template. */
6049 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6050 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6051 if (!tinfo)
6052 break;
6054 tree underlying = TI_TEMPLATE (tinfo);
6055 if (!PRIMARY_TEMPLATE_P (underlying)
6056 || (num_innermost_template_parms (tmpl)
6057 != num_innermost_template_parms (underlying)))
6058 break;
6060 tree alias_args = INNERMOST_TEMPLATE_ARGS
6061 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6062 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6063 break;
6065 /* Alias is equivalent. Strip it and repeat. */
6066 tmpl = underlying;
6069 return tmpl;
6072 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6073 must be a reference-to-function or a pointer-to-function type, as specified
6074 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6075 and check that the resulting function has external linkage. */
6077 static tree
6078 convert_nontype_argument_function (tree type, tree expr,
6079 tsubst_flags_t complain)
6081 tree fns = expr;
6082 tree fn, fn_no_ptr;
6083 linkage_kind linkage;
6085 fn = instantiate_type (type, fns, tf_none);
6086 if (fn == error_mark_node)
6087 return error_mark_node;
6089 if (value_dependent_expression_p (fn))
6090 goto accept;
6092 fn_no_ptr = strip_fnptr_conv (fn);
6093 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6094 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6095 if (BASELINK_P (fn_no_ptr))
6096 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6098 /* [temp.arg.nontype]/1
6100 A template-argument for a non-type, non-template template-parameter
6101 shall be one of:
6102 [...]
6103 -- the address of an object or function with external [C++11: or
6104 internal] linkage. */
6106 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6108 if (complain & tf_error)
6110 error ("%qE is not a valid template argument for type %qT",
6111 expr, type);
6112 if (TYPE_PTR_P (type))
6113 inform (input_location, "it must be the address of a function "
6114 "with external linkage");
6115 else
6116 inform (input_location, "it must be the name of a function with "
6117 "external linkage");
6119 return NULL_TREE;
6122 linkage = decl_linkage (fn_no_ptr);
6123 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6125 if (complain & tf_error)
6127 if (cxx_dialect >= cxx11)
6128 error ("%qE is not a valid template argument for type %qT "
6129 "because %qD has no linkage",
6130 expr, type, fn_no_ptr);
6131 else
6132 error ("%qE is not a valid template argument for type %qT "
6133 "because %qD does not have external linkage",
6134 expr, type, fn_no_ptr);
6136 return NULL_TREE;
6139 accept:
6140 if (TREE_CODE (type) == REFERENCE_TYPE)
6141 fn = build_address (fn);
6142 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6143 fn = build_nop (type, fn);
6145 return fn;
6148 /* Subroutine of convert_nontype_argument.
6149 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6150 Emit an error otherwise. */
6152 static bool
6153 check_valid_ptrmem_cst_expr (tree type, tree expr,
6154 tsubst_flags_t complain)
6156 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6157 tree orig_expr = expr;
6158 STRIP_NOPS (expr);
6159 if (null_ptr_cst_p (expr))
6160 return true;
6161 if (TREE_CODE (expr) == PTRMEM_CST
6162 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6163 PTRMEM_CST_CLASS (expr)))
6164 return true;
6165 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6166 return true;
6167 if (processing_template_decl
6168 && TREE_CODE (expr) == ADDR_EXPR
6169 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6170 return true;
6171 if (complain & tf_error)
6173 error_at (loc, "%qE is not a valid template argument for type %qT",
6174 orig_expr, type);
6175 if (TREE_CODE (expr) != PTRMEM_CST)
6176 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6177 else
6178 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6180 return false;
6183 /* Returns TRUE iff the address of OP is value-dependent.
6185 14.6.2.4 [temp.dep.temp]:
6186 A non-integral non-type template-argument is dependent if its type is
6187 dependent or it has either of the following forms
6188 qualified-id
6189 & qualified-id
6190 and contains a nested-name-specifier which specifies a class-name that
6191 names a dependent type.
6193 We generalize this to just say that the address of a member of a
6194 dependent class is value-dependent; the above doesn't cover the
6195 address of a static data member named with an unqualified-id. */
6197 static bool
6198 has_value_dependent_address (tree op)
6200 /* We could use get_inner_reference here, but there's no need;
6201 this is only relevant for template non-type arguments, which
6202 can only be expressed as &id-expression. */
6203 if (DECL_P (op))
6205 tree ctx = CP_DECL_CONTEXT (op);
6206 if (TYPE_P (ctx) && dependent_type_p (ctx))
6207 return true;
6210 return false;
6213 /* The next set of functions are used for providing helpful explanatory
6214 diagnostics for failed overload resolution. Their messages should be
6215 indented by two spaces for consistency with the messages in
6216 call.c */
6218 static int
6219 unify_success (bool /*explain_p*/)
6221 return 0;
6224 /* Other failure functions should call this one, to provide a single function
6225 for setting a breakpoint on. */
6227 static int
6228 unify_invalid (bool /*explain_p*/)
6230 return 1;
6233 static int
6234 unify_parameter_deduction_failure (bool explain_p, tree parm)
6236 if (explain_p)
6237 inform (input_location,
6238 " couldn't deduce template parameter %qD", parm);
6239 return unify_invalid (explain_p);
6242 static int
6243 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6245 if (explain_p)
6246 inform (input_location,
6247 " types %qT and %qT have incompatible cv-qualifiers",
6248 parm, arg);
6249 return unify_invalid (explain_p);
6252 static int
6253 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6255 if (explain_p)
6256 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6257 return unify_invalid (explain_p);
6260 static int
6261 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6263 if (explain_p)
6264 inform (input_location,
6265 " template parameter %qD is not a parameter pack, but "
6266 "argument %qD is",
6267 parm, arg);
6268 return unify_invalid (explain_p);
6271 static int
6272 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6274 if (explain_p)
6275 inform (input_location,
6276 " template argument %qE does not match "
6277 "pointer-to-member constant %qE",
6278 arg, parm);
6279 return unify_invalid (explain_p);
6282 static int
6283 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6285 if (explain_p)
6286 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6287 return unify_invalid (explain_p);
6290 static int
6291 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6293 if (explain_p)
6294 inform (input_location,
6295 " inconsistent parameter pack deduction with %qT and %qT",
6296 old_arg, new_arg);
6297 return unify_invalid (explain_p);
6300 static int
6301 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6303 if (explain_p)
6305 if (TYPE_P (parm))
6306 inform (input_location,
6307 " deduced conflicting types for parameter %qT (%qT and %qT)",
6308 parm, first, second);
6309 else
6310 inform (input_location,
6311 " deduced conflicting values for non-type parameter "
6312 "%qE (%qE and %qE)", parm, first, second);
6314 return unify_invalid (explain_p);
6317 static int
6318 unify_vla_arg (bool explain_p, tree arg)
6320 if (explain_p)
6321 inform (input_location,
6322 " variable-sized array type %qT is not "
6323 "a valid template argument",
6324 arg);
6325 return unify_invalid (explain_p);
6328 static int
6329 unify_method_type_error (bool explain_p, tree arg)
6331 if (explain_p)
6332 inform (input_location,
6333 " member function type %qT is not a valid template argument",
6334 arg);
6335 return unify_invalid (explain_p);
6338 static int
6339 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6341 if (explain_p)
6343 if (least_p)
6344 inform_n (input_location, wanted,
6345 " candidate expects at least %d argument, %d provided",
6346 " candidate expects at least %d arguments, %d provided",
6347 wanted, have);
6348 else
6349 inform_n (input_location, wanted,
6350 " candidate expects %d argument, %d provided",
6351 " candidate expects %d arguments, %d provided",
6352 wanted, have);
6354 return unify_invalid (explain_p);
6357 static int
6358 unify_too_many_arguments (bool explain_p, int have, int wanted)
6360 return unify_arity (explain_p, have, wanted);
6363 static int
6364 unify_too_few_arguments (bool explain_p, int have, int wanted,
6365 bool least_p = false)
6367 return unify_arity (explain_p, have, wanted, least_p);
6370 static int
6371 unify_arg_conversion (bool explain_p, tree to_type,
6372 tree from_type, tree arg)
6374 if (explain_p)
6375 inform (EXPR_LOC_OR_LOC (arg, input_location),
6376 " cannot convert %qE (type %qT) to type %qT",
6377 arg, from_type, to_type);
6378 return unify_invalid (explain_p);
6381 static int
6382 unify_no_common_base (bool explain_p, enum template_base_result r,
6383 tree parm, tree arg)
6385 if (explain_p)
6386 switch (r)
6388 case tbr_ambiguous_baseclass:
6389 inform (input_location, " %qT is an ambiguous base class of %qT",
6390 parm, arg);
6391 break;
6392 default:
6393 inform (input_location, " %qT is not derived from %qT", arg, parm);
6394 break;
6396 return unify_invalid (explain_p);
6399 static int
6400 unify_inconsistent_template_template_parameters (bool explain_p)
6402 if (explain_p)
6403 inform (input_location,
6404 " template parameters of a template template argument are "
6405 "inconsistent with other deduced template arguments");
6406 return unify_invalid (explain_p);
6409 static int
6410 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6412 if (explain_p)
6413 inform (input_location,
6414 " can't deduce a template for %qT from non-template type %qT",
6415 parm, arg);
6416 return unify_invalid (explain_p);
6419 static int
6420 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6422 if (explain_p)
6423 inform (input_location,
6424 " template argument %qE does not match %qE", arg, parm);
6425 return unify_invalid (explain_p);
6428 /* Attempt to convert the non-type template parameter EXPR to the
6429 indicated TYPE. If the conversion is successful, return the
6430 converted value. If the conversion is unsuccessful, return
6431 NULL_TREE if we issued an error message, or error_mark_node if we
6432 did not. We issue error messages for out-and-out bad template
6433 parameters, but not simply because the conversion failed, since we
6434 might be just trying to do argument deduction. Both TYPE and EXPR
6435 must be non-dependent.
6437 The conversion follows the special rules described in
6438 [temp.arg.nontype], and it is much more strict than an implicit
6439 conversion.
6441 This function is called twice for each template argument (see
6442 lookup_template_class for a more accurate description of this
6443 problem). This means that we need to handle expressions which
6444 are not valid in a C++ source, but can be created from the
6445 first call (for instance, casts to perform conversions). These
6446 hacks can go away after we fix the double coercion problem. */
6448 static tree
6449 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6451 tree expr_type;
6452 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6453 tree orig_expr = expr;
6455 /* Detect immediately string literals as invalid non-type argument.
6456 This special-case is not needed for correctness (we would easily
6457 catch this later), but only to provide better diagnostic for this
6458 common user mistake. As suggested by DR 100, we do not mention
6459 linkage issues in the diagnostic as this is not the point. */
6460 /* FIXME we're making this OK. */
6461 if (TREE_CODE (expr) == STRING_CST)
6463 if (complain & tf_error)
6464 error ("%qE is not a valid template argument for type %qT "
6465 "because string literals can never be used in this context",
6466 expr, type);
6467 return NULL_TREE;
6470 /* Add the ADDR_EXPR now for the benefit of
6471 value_dependent_expression_p. */
6472 if (TYPE_PTROBV_P (type)
6473 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6475 expr = decay_conversion (expr, complain);
6476 if (expr == error_mark_node)
6477 return error_mark_node;
6480 /* If we are in a template, EXPR may be non-dependent, but still
6481 have a syntactic, rather than semantic, form. For example, EXPR
6482 might be a SCOPE_REF, rather than the VAR_DECL to which the
6483 SCOPE_REF refers. Preserving the qualifying scope is necessary
6484 so that access checking can be performed when the template is
6485 instantiated -- but here we need the resolved form so that we can
6486 convert the argument. */
6487 bool non_dep = false;
6488 if (TYPE_REF_OBJ_P (type)
6489 && has_value_dependent_address (expr))
6490 /* If we want the address and it's value-dependent, don't fold. */;
6491 else if (processing_template_decl
6492 && is_nondependent_constant_expression (expr))
6493 non_dep = true;
6494 if (error_operand_p (expr))
6495 return error_mark_node;
6496 expr_type = TREE_TYPE (expr);
6498 /* If the argument is non-dependent, perform any conversions in
6499 non-dependent context as well. */
6500 processing_template_decl_sentinel s (non_dep);
6501 if (non_dep)
6502 expr = instantiate_non_dependent_expr_internal (expr, complain);
6504 if (value_dependent_expression_p (expr))
6505 expr = canonicalize_expr_argument (expr, complain);
6507 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6508 to a non-type argument of "nullptr". */
6509 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6510 expr = fold_simple (convert (type, expr));
6512 /* In C++11, integral or enumeration non-type template arguments can be
6513 arbitrary constant expressions. Pointer and pointer to
6514 member arguments can be general constant expressions that evaluate
6515 to a null value, but otherwise still need to be of a specific form. */
6516 if (cxx_dialect >= cxx11)
6518 if (TREE_CODE (expr) == PTRMEM_CST)
6519 /* A PTRMEM_CST is already constant, and a valid template
6520 argument for a parameter of pointer to member type, we just want
6521 to leave it in that form rather than lower it to a
6522 CONSTRUCTOR. */;
6523 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6524 || cxx_dialect >= cxx17)
6526 /* C++17: A template-argument for a non-type template-parameter shall
6527 be a converted constant expression (8.20) of the type of the
6528 template-parameter. */
6529 expr = build_converted_constant_expr (type, expr, complain);
6530 if (expr == error_mark_node)
6531 return error_mark_node;
6532 expr = maybe_constant_value (expr);
6533 expr = convert_from_reference (expr);
6535 else if (TYPE_PTR_OR_PTRMEM_P (type))
6537 tree folded = maybe_constant_value (expr);
6538 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6539 : null_member_pointer_value_p (folded))
6540 expr = folded;
6544 if (TREE_CODE (type) == REFERENCE_TYPE)
6545 expr = mark_lvalue_use (expr);
6546 else
6547 expr = mark_rvalue_use (expr);
6549 /* HACK: Due to double coercion, we can get a
6550 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6551 which is the tree that we built on the first call (see
6552 below when coercing to reference to object or to reference to
6553 function). We just strip everything and get to the arg.
6554 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6555 for examples. */
6556 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6558 tree probe_type, probe = expr;
6559 if (REFERENCE_REF_P (probe))
6560 probe = TREE_OPERAND (probe, 0);
6561 probe_type = TREE_TYPE (probe);
6562 if (TREE_CODE (probe) == NOP_EXPR)
6564 /* ??? Maybe we could use convert_from_reference here, but we
6565 would need to relax its constraints because the NOP_EXPR
6566 could actually change the type to something more cv-qualified,
6567 and this is not folded by convert_from_reference. */
6568 tree addr = TREE_OPERAND (probe, 0);
6569 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6570 && TREE_CODE (addr) == ADDR_EXPR
6571 && TYPE_PTR_P (TREE_TYPE (addr))
6572 && (same_type_ignoring_top_level_qualifiers_p
6573 (TREE_TYPE (probe_type),
6574 TREE_TYPE (TREE_TYPE (addr)))))
6576 expr = TREE_OPERAND (addr, 0);
6577 expr_type = TREE_TYPE (probe_type);
6582 /* [temp.arg.nontype]/5, bullet 1
6584 For a non-type template-parameter of integral or enumeration type,
6585 integral promotions (_conv.prom_) and integral conversions
6586 (_conv.integral_) are applied. */
6587 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6589 if (cxx_dialect < cxx11)
6591 tree t = build_converted_constant_expr (type, expr, complain);
6592 t = maybe_constant_value (t);
6593 if (t != error_mark_node)
6594 expr = t;
6597 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6598 return error_mark_node;
6600 /* Notice that there are constant expressions like '4 % 0' which
6601 do not fold into integer constants. */
6602 if (TREE_CODE (expr) != INTEGER_CST
6603 && !value_dependent_expression_p (expr))
6605 if (complain & tf_error)
6607 int errs = errorcount, warns = warningcount + werrorcount;
6608 if (!require_potential_constant_expression (expr))
6609 expr = error_mark_node;
6610 else
6611 expr = cxx_constant_value (expr);
6612 if (errorcount > errs || warningcount + werrorcount > warns)
6613 inform (loc, "in template argument for type %qT ", type);
6614 if (expr == error_mark_node)
6615 return NULL_TREE;
6616 /* else cxx_constant_value complained but gave us
6617 a real constant, so go ahead. */
6618 if (TREE_CODE (expr) != INTEGER_CST)
6620 /* Some assemble time constant expressions like
6621 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6622 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6623 as we can emit them into .rodata initializers of
6624 variables, yet they can't fold into an INTEGER_CST at
6625 compile time. Refuse them here. */
6626 gcc_checking_assert (reduced_constant_expression_p (expr));
6627 error_at (loc, "template argument %qE for type %qT not "
6628 "a constant integer", expr, type);
6629 return NULL_TREE;
6632 else
6633 return NULL_TREE;
6636 /* Avoid typedef problems. */
6637 if (TREE_TYPE (expr) != type)
6638 expr = fold_convert (type, expr);
6640 /* [temp.arg.nontype]/5, bullet 2
6642 For a non-type template-parameter of type pointer to object,
6643 qualification conversions (_conv.qual_) and the array-to-pointer
6644 conversion (_conv.array_) are applied. */
6645 else if (TYPE_PTROBV_P (type))
6647 tree decayed = expr;
6649 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6650 decay_conversion or an explicit cast. If it's a problematic cast,
6651 we'll complain about it below. */
6652 if (TREE_CODE (expr) == NOP_EXPR)
6654 tree probe = expr;
6655 STRIP_NOPS (probe);
6656 if (TREE_CODE (probe) == ADDR_EXPR
6657 && TYPE_PTR_P (TREE_TYPE (probe)))
6659 expr = probe;
6660 expr_type = TREE_TYPE (expr);
6664 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6666 A template-argument for a non-type, non-template template-parameter
6667 shall be one of: [...]
6669 -- the name of a non-type template-parameter;
6670 -- the address of an object or function with external linkage, [...]
6671 expressed as "& id-expression" where the & is optional if the name
6672 refers to a function or array, or if the corresponding
6673 template-parameter is a reference.
6675 Here, we do not care about functions, as they are invalid anyway
6676 for a parameter of type pointer-to-object. */
6678 if (value_dependent_expression_p (expr))
6679 /* Non-type template parameters are OK. */
6681 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6682 /* Null pointer values are OK in C++11. */;
6683 else if (TREE_CODE (expr) != ADDR_EXPR)
6685 if (VAR_P (expr))
6687 if (complain & tf_error)
6688 error ("%qD is not a valid template argument "
6689 "because %qD is a variable, not the address of "
6690 "a variable", orig_expr, expr);
6691 return NULL_TREE;
6693 if (POINTER_TYPE_P (expr_type))
6695 if (complain & tf_error)
6696 error ("%qE is not a valid template argument for %qT "
6697 "because it is not the address of a variable",
6698 orig_expr, type);
6699 return NULL_TREE;
6701 /* Other values, like integer constants, might be valid
6702 non-type arguments of some other type. */
6703 return error_mark_node;
6705 else
6707 tree decl = TREE_OPERAND (expr, 0);
6709 if (!VAR_P (decl))
6711 if (complain & tf_error)
6712 error ("%qE is not a valid template argument of type %qT "
6713 "because %qE is not a variable", orig_expr, type, decl);
6714 return NULL_TREE;
6716 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6718 if (complain & tf_error)
6719 error ("%qE is not a valid template argument of type %qT "
6720 "because %qD does not have external linkage",
6721 orig_expr, type, decl);
6722 return NULL_TREE;
6724 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6725 && decl_linkage (decl) == lk_none)
6727 if (complain & tf_error)
6728 error ("%qE is not a valid template argument of type %qT "
6729 "because %qD has no linkage", orig_expr, type, decl);
6730 return NULL_TREE;
6732 /* C++17: For a non-type template-parameter of reference or pointer
6733 type, the value of the constant expression shall not refer to (or
6734 for a pointer type, shall not be the address of):
6735 * a subobject (4.5),
6736 * a temporary object (15.2),
6737 * a string literal (5.13.5),
6738 * the result of a typeid expression (8.2.8), or
6739 * a predefined __func__ variable (11.4.1). */
6740 else if (DECL_ARTIFICIAL (decl))
6742 if (complain & tf_error)
6743 error ("the address of %qD is not a valid template argument",
6744 decl);
6745 return NULL_TREE;
6747 else if (!same_type_ignoring_top_level_qualifiers_p
6748 (strip_array_types (TREE_TYPE (type)),
6749 strip_array_types (TREE_TYPE (decl))))
6751 if (complain & tf_error)
6752 error ("the address of the %qT subobject of %qD is not a "
6753 "valid template argument", TREE_TYPE (type), decl);
6754 return NULL_TREE;
6756 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6758 if (complain & tf_error)
6759 error ("the address of %qD is not a valid template argument "
6760 "because it does not have static storage duration",
6761 decl);
6762 return NULL_TREE;
6766 expr = decayed;
6768 expr = perform_qualification_conversions (type, expr);
6769 if (expr == error_mark_node)
6770 return error_mark_node;
6772 /* [temp.arg.nontype]/5, bullet 3
6774 For a non-type template-parameter of type reference to object, no
6775 conversions apply. The type referred to by the reference may be more
6776 cv-qualified than the (otherwise identical) type of the
6777 template-argument. The template-parameter is bound directly to the
6778 template-argument, which must be an lvalue. */
6779 else if (TYPE_REF_OBJ_P (type))
6781 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6782 expr_type))
6783 return error_mark_node;
6785 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6787 if (complain & tf_error)
6788 error ("%qE is not a valid template argument for type %qT "
6789 "because of conflicts in cv-qualification", expr, type);
6790 return NULL_TREE;
6793 if (!lvalue_p (expr))
6795 if (complain & tf_error)
6796 error ("%qE is not a valid template argument for type %qT "
6797 "because it is not an lvalue", expr, type);
6798 return NULL_TREE;
6801 /* [temp.arg.nontype]/1
6803 A template-argument for a non-type, non-template template-parameter
6804 shall be one of: [...]
6806 -- the address of an object or function with external linkage. */
6807 if (INDIRECT_REF_P (expr)
6808 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6810 expr = TREE_OPERAND (expr, 0);
6811 if (DECL_P (expr))
6813 if (complain & tf_error)
6814 error ("%q#D is not a valid template argument for type %qT "
6815 "because a reference variable does not have a constant "
6816 "address", expr, type);
6817 return NULL_TREE;
6821 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6822 && value_dependent_expression_p (expr))
6823 /* OK, dependent reference. We don't want to ask whether a DECL is
6824 itself value-dependent, since what we want here is its address. */;
6825 else
6827 if (!DECL_P (expr))
6829 if (complain & tf_error)
6830 error ("%qE is not a valid template argument for type %qT "
6831 "because it is not an object with linkage",
6832 expr, type);
6833 return NULL_TREE;
6836 /* DR 1155 allows internal linkage in C++11 and up. */
6837 linkage_kind linkage = decl_linkage (expr);
6838 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6840 if (complain & tf_error)
6841 error ("%qE is not a valid template argument for type %qT "
6842 "because object %qD does not have linkage",
6843 expr, type, expr);
6844 return NULL_TREE;
6847 expr = build_address (expr);
6850 if (!same_type_p (type, TREE_TYPE (expr)))
6851 expr = build_nop (type, expr);
6853 /* [temp.arg.nontype]/5, bullet 4
6855 For a non-type template-parameter of type pointer to function, only
6856 the function-to-pointer conversion (_conv.func_) is applied. If the
6857 template-argument represents a set of overloaded functions (or a
6858 pointer to such), the matching function is selected from the set
6859 (_over.over_). */
6860 else if (TYPE_PTRFN_P (type))
6862 /* If the argument is a template-id, we might not have enough
6863 context information to decay the pointer. */
6864 if (!type_unknown_p (expr_type))
6866 expr = decay_conversion (expr, complain);
6867 if (expr == error_mark_node)
6868 return error_mark_node;
6871 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6872 /* Null pointer values are OK in C++11. */
6873 return perform_qualification_conversions (type, expr);
6875 expr = convert_nontype_argument_function (type, expr, complain);
6876 if (!expr || expr == error_mark_node)
6877 return expr;
6879 /* [temp.arg.nontype]/5, bullet 5
6881 For a non-type template-parameter of type reference to function, no
6882 conversions apply. If the template-argument represents a set of
6883 overloaded functions, the matching function is selected from the set
6884 (_over.over_). */
6885 else if (TYPE_REFFN_P (type))
6887 if (TREE_CODE (expr) == ADDR_EXPR)
6889 if (complain & tf_error)
6891 error ("%qE is not a valid template argument for type %qT "
6892 "because it is a pointer", expr, type);
6893 inform (input_location, "try using %qE instead",
6894 TREE_OPERAND (expr, 0));
6896 return NULL_TREE;
6899 expr = convert_nontype_argument_function (type, expr, complain);
6900 if (!expr || expr == error_mark_node)
6901 return expr;
6903 /* [temp.arg.nontype]/5, bullet 6
6905 For a non-type template-parameter of type pointer to member function,
6906 no conversions apply. If the template-argument represents a set of
6907 overloaded member functions, the matching member function is selected
6908 from the set (_over.over_). */
6909 else if (TYPE_PTRMEMFUNC_P (type))
6911 expr = instantiate_type (type, expr, tf_none);
6912 if (expr == error_mark_node)
6913 return error_mark_node;
6915 /* [temp.arg.nontype] bullet 1 says the pointer to member
6916 expression must be a pointer-to-member constant. */
6917 if (!value_dependent_expression_p (expr)
6918 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6919 return NULL_TREE;
6921 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6922 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6923 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6924 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6926 /* [temp.arg.nontype]/5, bullet 7
6928 For a non-type template-parameter of type pointer to data member,
6929 qualification conversions (_conv.qual_) are applied. */
6930 else if (TYPE_PTRDATAMEM_P (type))
6932 /* [temp.arg.nontype] bullet 1 says the pointer to member
6933 expression must be a pointer-to-member constant. */
6934 if (!value_dependent_expression_p (expr)
6935 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6936 return NULL_TREE;
6938 expr = perform_qualification_conversions (type, expr);
6939 if (expr == error_mark_node)
6940 return expr;
6942 else if (NULLPTR_TYPE_P (type))
6944 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6946 if (complain & tf_error)
6947 error ("%qE is not a valid template argument for type %qT "
6948 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6949 return NULL_TREE;
6951 return expr;
6953 /* A template non-type parameter must be one of the above. */
6954 else
6955 gcc_unreachable ();
6957 /* Sanity check: did we actually convert the argument to the
6958 right type? */
6959 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6960 (type, TREE_TYPE (expr)));
6961 return convert_from_reference (expr);
6964 /* Subroutine of coerce_template_template_parms, which returns 1 if
6965 PARM_PARM and ARG_PARM match using the rule for the template
6966 parameters of template template parameters. Both PARM and ARG are
6967 template parameters; the rest of the arguments are the same as for
6968 coerce_template_template_parms.
6970 static int
6971 coerce_template_template_parm (tree parm,
6972 tree arg,
6973 tsubst_flags_t complain,
6974 tree in_decl,
6975 tree outer_args)
6977 if (arg == NULL_TREE || error_operand_p (arg)
6978 || parm == NULL_TREE || error_operand_p (parm))
6979 return 0;
6981 if (TREE_CODE (arg) != TREE_CODE (parm))
6982 return 0;
6984 switch (TREE_CODE (parm))
6986 case TEMPLATE_DECL:
6987 /* We encounter instantiations of templates like
6988 template <template <template <class> class> class TT>
6989 class C; */
6991 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6992 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6994 if (!coerce_template_template_parms
6995 (parmparm, argparm, complain, in_decl, outer_args))
6996 return 0;
6998 /* Fall through. */
7000 case TYPE_DECL:
7001 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7002 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7003 /* Argument is a parameter pack but parameter is not. */
7004 return 0;
7005 break;
7007 case PARM_DECL:
7008 /* The tsubst call is used to handle cases such as
7010 template <int> class C {};
7011 template <class T, template <T> class TT> class D {};
7012 D<int, C> d;
7014 i.e. the parameter list of TT depends on earlier parameters. */
7015 if (!uses_template_parms (TREE_TYPE (arg)))
7017 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7018 if (!uses_template_parms (t)
7019 && !same_type_p (t, TREE_TYPE (arg)))
7020 return 0;
7023 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7024 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7025 /* Argument is a parameter pack but parameter is not. */
7026 return 0;
7028 break;
7030 default:
7031 gcc_unreachable ();
7034 return 1;
7037 /* Coerce template argument list ARGLIST for use with template
7038 template-parameter TEMPL. */
7040 static tree
7041 coerce_template_args_for_ttp (tree templ, tree arglist,
7042 tsubst_flags_t complain)
7044 /* Consider an example where a template template parameter declared as
7046 template <class T, class U = std::allocator<T> > class TT
7048 The template parameter level of T and U are one level larger than
7049 of TT. To proper process the default argument of U, say when an
7050 instantiation `TT<int>' is seen, we need to build the full
7051 arguments containing {int} as the innermost level. Outer levels,
7052 available when not appearing as default template argument, can be
7053 obtained from the arguments of the enclosing template.
7055 Suppose that TT is later substituted with std::vector. The above
7056 instantiation is `TT<int, std::allocator<T> >' with TT at
7057 level 1, and T at level 2, while the template arguments at level 1
7058 becomes {std::vector} and the inner level 2 is {int}. */
7060 tree outer = DECL_CONTEXT (templ);
7061 if (outer)
7063 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7064 /* We want arguments for the partial specialization, not arguments for
7065 the primary template. */
7066 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7067 else
7068 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7070 else if (current_template_parms)
7072 /* This is an argument of the current template, so we haven't set
7073 DECL_CONTEXT yet. */
7074 tree relevant_template_parms;
7076 /* Parameter levels that are greater than the level of the given
7077 template template parm are irrelevant. */
7078 relevant_template_parms = current_template_parms;
7079 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7080 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7081 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7083 outer = template_parms_to_args (relevant_template_parms);
7086 if (outer)
7087 arglist = add_to_template_args (outer, arglist);
7089 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7090 return coerce_template_parms (parmlist, arglist, templ,
7091 complain,
7092 /*require_all_args=*/true,
7093 /*use_default_args=*/true);
7096 /* A cache of template template parameters with match-all default
7097 arguments. */
7098 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7099 static void
7100 store_defaulted_ttp (tree v, tree t)
7102 if (!defaulted_ttp_cache)
7103 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7104 defaulted_ttp_cache->put (v, t);
7106 static tree
7107 lookup_defaulted_ttp (tree v)
7109 if (defaulted_ttp_cache)
7110 if (tree *p = defaulted_ttp_cache->get (v))
7111 return *p;
7112 return NULL_TREE;
7115 /* T is a bound template template-parameter. Copy its arguments into default
7116 arguments of the template template-parameter's template parameters. */
7118 static tree
7119 add_defaults_to_ttp (tree otmpl)
7121 if (tree c = lookup_defaulted_ttp (otmpl))
7122 return c;
7124 tree ntmpl = copy_node (otmpl);
7126 tree ntype = copy_node (TREE_TYPE (otmpl));
7127 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7128 TYPE_MAIN_VARIANT (ntype) = ntype;
7129 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7130 TYPE_NAME (ntype) = ntmpl;
7131 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7133 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7134 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7135 TEMPLATE_PARM_DECL (idx) = ntmpl;
7136 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7138 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7139 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7140 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7141 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7142 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7144 tree o = TREE_VEC_ELT (vec, i);
7145 if (!template_parameter_pack_p (TREE_VALUE (o)))
7147 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7148 TREE_PURPOSE (n) = any_targ_node;
7152 store_defaulted_ttp (otmpl, ntmpl);
7153 return ntmpl;
7156 /* ARG is a bound potential template template-argument, and PARGS is a list
7157 of arguments for the corresponding template template-parameter. Adjust
7158 PARGS as appropriate for application to ARG's template, and if ARG is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7160 arguments to the template template parameter. */
7162 static tree
7163 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7165 ++processing_template_decl;
7166 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7167 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7169 /* When comparing two template template-parameters in partial ordering,
7170 rewrite the one currently being used as an argument to have default
7171 arguments for all parameters. */
7172 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7173 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7174 if (pargs != error_mark_node)
7175 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7176 TYPE_TI_ARGS (arg));
7178 else
7180 tree aparms
7181 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7182 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7183 /*require_all*/true,
7184 /*use_default*/true);
7186 --processing_template_decl;
7187 return pargs;
7190 /* Subroutine of unify for the case when PARM is a
7191 BOUND_TEMPLATE_TEMPLATE_PARM. */
7193 static int
7194 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7195 bool explain_p)
7197 tree parmvec = TYPE_TI_ARGS (parm);
7198 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7200 /* The template template parm might be variadic and the argument
7201 not, so flatten both argument lists. */
7202 parmvec = expand_template_argument_pack (parmvec);
7203 argvec = expand_template_argument_pack (argvec);
7205 if (flag_new_ttp)
7207 /* In keeping with P0522R0, adjust P's template arguments
7208 to apply to A's template; then flatten it again. */
7209 tree nparmvec = parmvec;
7210 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7211 nparmvec = expand_template_argument_pack (nparmvec);
7213 if (unify (tparms, targs, nparmvec, argvec,
7214 UNIFY_ALLOW_NONE, explain_p))
7215 return 1;
7217 /* If the P0522 adjustment eliminated a pack expansion, deduce
7218 empty packs. */
7219 if (flag_new_ttp
7220 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7221 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7222 DEDUCE_EXACT, /*sub*/true, explain_p))
7223 return 1;
7225 else
7227 /* Deduce arguments T, i from TT<T> or TT<i>.
7228 We check each element of PARMVEC and ARGVEC individually
7229 rather than the whole TREE_VEC since they can have
7230 different number of elements, which is allowed under N2555. */
7232 int len = TREE_VEC_LENGTH (parmvec);
7234 /* Check if the parameters end in a pack, making them
7235 variadic. */
7236 int parm_variadic_p = 0;
7237 if (len > 0
7238 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7239 parm_variadic_p = 1;
7241 for (int i = 0; i < len - parm_variadic_p; ++i)
7242 /* If the template argument list of P contains a pack
7243 expansion that is not the last template argument, the
7244 entire template argument list is a non-deduced
7245 context. */
7246 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7247 return unify_success (explain_p);
7249 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7250 return unify_too_few_arguments (explain_p,
7251 TREE_VEC_LENGTH (argvec), len);
7253 for (int i = 0; i < len - parm_variadic_p; ++i)
7254 if (unify (tparms, targs,
7255 TREE_VEC_ELT (parmvec, i),
7256 TREE_VEC_ELT (argvec, i),
7257 UNIFY_ALLOW_NONE, explain_p))
7258 return 1;
7260 if (parm_variadic_p
7261 && unify_pack_expansion (tparms, targs,
7262 parmvec, argvec,
7263 DEDUCE_EXACT,
7264 /*subr=*/true, explain_p))
7265 return 1;
7268 return 0;
7271 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7272 template template parameters. Both PARM_PARMS and ARG_PARMS are
7273 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7274 or PARM_DECL.
7276 Consider the example:
7277 template <class T> class A;
7278 template<template <class U> class TT> class B;
7280 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7281 the parameters to A, and OUTER_ARGS contains A. */
7283 static int
7284 coerce_template_template_parms (tree parm_parms,
7285 tree arg_parms,
7286 tsubst_flags_t complain,
7287 tree in_decl,
7288 tree outer_args)
7290 int nparms, nargs, i;
7291 tree parm, arg;
7292 int variadic_p = 0;
7294 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7295 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7297 nparms = TREE_VEC_LENGTH (parm_parms);
7298 nargs = TREE_VEC_LENGTH (arg_parms);
7300 if (flag_new_ttp)
7302 /* P0522R0: A template template-parameter P is at least as specialized as
7303 a template template-argument A if, given the following rewrite to two
7304 function templates, the function template corresponding to P is at
7305 least as specialized as the function template corresponding to A
7306 according to the partial ordering rules for function templates
7307 ([temp.func.order]). Given an invented class template X with the
7308 template parameter list of A (including default arguments):
7310 * Each of the two function templates has the same template parameters,
7311 respectively, as P or A.
7313 * Each function template has a single function parameter whose type is
7314 a specialization of X with template arguments corresponding to the
7315 template parameters from the respective function template where, for
7316 each template parameter PP in the template parameter list of the
7317 function template, a corresponding template argument AA is formed. If
7318 PP declares a parameter pack, then AA is the pack expansion
7319 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7321 If the rewrite produces an invalid type, then P is not at least as
7322 specialized as A. */
7324 /* So coerce P's args to apply to A's parms, and then deduce between A's
7325 args and the converted args. If that succeeds, A is at least as
7326 specialized as P, so they match.*/
7327 tree pargs = template_parms_level_to_args (parm_parms);
7328 ++processing_template_decl;
7329 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7330 /*require_all*/true, /*use_default*/true);
7331 --processing_template_decl;
7332 if (pargs != error_mark_node)
7334 tree targs = make_tree_vec (nargs);
7335 tree aargs = template_parms_level_to_args (arg_parms);
7336 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7337 /*explain*/false))
7338 return 1;
7342 /* Determine whether we have a parameter pack at the end of the
7343 template template parameter's template parameter list. */
7344 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7346 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7348 if (error_operand_p (parm))
7349 return 0;
7351 switch (TREE_CODE (parm))
7353 case TEMPLATE_DECL:
7354 case TYPE_DECL:
7355 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7356 variadic_p = 1;
7357 break;
7359 case PARM_DECL:
7360 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7361 variadic_p = 1;
7362 break;
7364 default:
7365 gcc_unreachable ();
7369 if (nargs != nparms
7370 && !(variadic_p && nargs >= nparms - 1))
7371 return 0;
7373 /* Check all of the template parameters except the parameter pack at
7374 the end (if any). */
7375 for (i = 0; i < nparms - variadic_p; ++i)
7377 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7378 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7379 continue;
7381 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7382 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7384 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7385 outer_args))
7386 return 0;
7390 if (variadic_p)
7392 /* Check each of the template parameters in the template
7393 argument against the template parameter pack at the end of
7394 the template template parameter. */
7395 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7396 return 0;
7398 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7400 for (; i < nargs; ++i)
7402 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7403 continue;
7405 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7407 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7408 outer_args))
7409 return 0;
7413 return 1;
7416 /* Verifies that the deduced template arguments (in TARGS) for the
7417 template template parameters (in TPARMS) represent valid bindings,
7418 by comparing the template parameter list of each template argument
7419 to the template parameter list of its corresponding template
7420 template parameter, in accordance with DR150. This
7421 routine can only be called after all template arguments have been
7422 deduced. It will return TRUE if all of the template template
7423 parameter bindings are okay, FALSE otherwise. */
7424 bool
7425 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7427 int i, ntparms = TREE_VEC_LENGTH (tparms);
7428 bool ret = true;
7430 /* We're dealing with template parms in this process. */
7431 ++processing_template_decl;
7433 targs = INNERMOST_TEMPLATE_ARGS (targs);
7435 for (i = 0; i < ntparms; ++i)
7437 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7438 tree targ = TREE_VEC_ELT (targs, i);
7440 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7442 tree packed_args = NULL_TREE;
7443 int idx, len = 1;
7445 if (ARGUMENT_PACK_P (targ))
7447 /* Look inside the argument pack. */
7448 packed_args = ARGUMENT_PACK_ARGS (targ);
7449 len = TREE_VEC_LENGTH (packed_args);
7452 for (idx = 0; idx < len; ++idx)
7454 tree targ_parms = NULL_TREE;
7456 if (packed_args)
7457 /* Extract the next argument from the argument
7458 pack. */
7459 targ = TREE_VEC_ELT (packed_args, idx);
7461 if (PACK_EXPANSION_P (targ))
7462 /* Look at the pattern of the pack expansion. */
7463 targ = PACK_EXPANSION_PATTERN (targ);
7465 /* Extract the template parameters from the template
7466 argument. */
7467 if (TREE_CODE (targ) == TEMPLATE_DECL)
7468 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7469 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7470 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7472 /* Verify that we can coerce the template template
7473 parameters from the template argument to the template
7474 parameter. This requires an exact match. */
7475 if (targ_parms
7476 && !coerce_template_template_parms
7477 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7478 targ_parms,
7479 tf_none,
7480 tparm,
7481 targs))
7483 ret = false;
7484 goto out;
7490 out:
7492 --processing_template_decl;
7493 return ret;
7496 /* Since type attributes aren't mangled, we need to strip them from
7497 template type arguments. */
7499 static tree
7500 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7502 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7503 return arg;
7504 bool removed_attributes = false;
7505 tree canon = strip_typedefs (arg, &removed_attributes);
7506 if (removed_attributes
7507 && (complain & tf_warning))
7508 warning (OPT_Wignored_attributes,
7509 "ignoring attributes on template argument %qT", arg);
7510 return canon;
7513 /* And from inside dependent non-type arguments like sizeof(Type). */
7515 static tree
7516 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7518 if (!arg || arg == error_mark_node)
7519 return arg;
7520 bool removed_attributes = false;
7521 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7522 if (removed_attributes
7523 && (complain & tf_warning))
7524 warning (OPT_Wignored_attributes,
7525 "ignoring attributes in template argument %qE", arg);
7526 return canon;
7529 // A template declaration can be substituted for a constrained
7530 // template template parameter only when the argument is more
7531 // constrained than the parameter.
7532 static bool
7533 is_compatible_template_arg (tree parm, tree arg)
7535 tree parm_cons = get_constraints (parm);
7537 /* For now, allow constrained template template arguments
7538 and unconstrained template template parameters. */
7539 if (parm_cons == NULL_TREE)
7540 return true;
7542 tree arg_cons = get_constraints (arg);
7544 // If the template parameter is constrained, we need to rewrite its
7545 // constraints in terms of the ARG's template parameters. This ensures
7546 // that all of the template parameter types will have the same depth.
7548 // Note that this is only valid when coerce_template_template_parm is
7549 // true for the innermost template parameters of PARM and ARG. In other
7550 // words, because coercion is successful, this conversion will be valid.
7551 if (parm_cons)
7553 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7554 parm_cons = tsubst_constraint_info (parm_cons,
7555 INNERMOST_TEMPLATE_ARGS (args),
7556 tf_none, NULL_TREE);
7557 if (parm_cons == error_mark_node)
7558 return false;
7561 return subsumes (parm_cons, arg_cons);
7564 // Convert a placeholder argument into a binding to the original
7565 // parameter. The original parameter is saved as the TREE_TYPE of
7566 // ARG.
7567 static inline tree
7568 convert_wildcard_argument (tree parm, tree arg)
7570 TREE_TYPE (arg) = parm;
7571 return arg;
7574 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7575 because one of them is dependent. But we need to represent the
7576 conversion for the benefit of cp_tree_equal. */
7578 static tree
7579 maybe_convert_nontype_argument (tree type, tree arg)
7581 /* Auto parms get no conversion. */
7582 if (type_uses_auto (type))
7583 return arg;
7584 /* We don't need or want to add this conversion now if we're going to use the
7585 argument for deduction. */
7586 if (value_dependent_expression_p (arg))
7587 return arg;
7589 type = cv_unqualified (type);
7590 tree argtype = TREE_TYPE (arg);
7591 if (same_type_p (type, argtype))
7592 return arg;
7594 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7595 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7596 return arg;
7599 /* Convert the indicated template ARG as necessary to match the
7600 indicated template PARM. Returns the converted ARG, or
7601 error_mark_node if the conversion was unsuccessful. Error and
7602 warning messages are issued under control of COMPLAIN. This
7603 conversion is for the Ith parameter in the parameter list. ARGS is
7604 the full set of template arguments deduced so far. */
7606 static tree
7607 convert_template_argument (tree parm,
7608 tree arg,
7609 tree args,
7610 tsubst_flags_t complain,
7611 int i,
7612 tree in_decl)
7614 tree orig_arg;
7615 tree val;
7616 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7618 if (parm == error_mark_node)
7619 return error_mark_node;
7621 /* Trivially convert placeholders. */
7622 if (TREE_CODE (arg) == WILDCARD_DECL)
7623 return convert_wildcard_argument (parm, arg);
7625 if (arg == any_targ_node)
7626 return arg;
7628 if (TREE_CODE (arg) == TREE_LIST
7629 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7631 /* The template argument was the name of some
7632 member function. That's usually
7633 invalid, but static members are OK. In any
7634 case, grab the underlying fields/functions
7635 and issue an error later if required. */
7636 orig_arg = TREE_VALUE (arg);
7637 TREE_TYPE (arg) = unknown_type_node;
7640 orig_arg = arg;
7642 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7643 requires_type = (TREE_CODE (parm) == TYPE_DECL
7644 || requires_tmpl_type);
7646 /* When determining whether an argument pack expansion is a template,
7647 look at the pattern. */
7648 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7649 arg = PACK_EXPANSION_PATTERN (arg);
7651 /* Deal with an injected-class-name used as a template template arg. */
7652 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7654 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7655 if (TREE_CODE (t) == TEMPLATE_DECL)
7657 if (cxx_dialect >= cxx11)
7658 /* OK under DR 1004. */;
7659 else if (complain & tf_warning_or_error)
7660 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7661 " used as template template argument", TYPE_NAME (arg));
7662 else if (flag_pedantic_errors)
7663 t = arg;
7665 arg = t;
7669 is_tmpl_type =
7670 ((TREE_CODE (arg) == TEMPLATE_DECL
7671 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7672 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7673 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7674 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7676 if (is_tmpl_type
7677 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7678 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7679 arg = TYPE_STUB_DECL (arg);
7681 is_type = TYPE_P (arg) || is_tmpl_type;
7683 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7684 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7686 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7688 if (complain & tf_error)
7689 error ("invalid use of destructor %qE as a type", orig_arg);
7690 return error_mark_node;
7693 permerror (input_location,
7694 "to refer to a type member of a template parameter, "
7695 "use %<typename %E%>", orig_arg);
7697 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7698 TREE_OPERAND (arg, 1),
7699 typename_type,
7700 complain);
7701 arg = orig_arg;
7702 is_type = 1;
7704 if (is_type != requires_type)
7706 if (in_decl)
7708 if (complain & tf_error)
7710 error ("type/value mismatch at argument %d in template "
7711 "parameter list for %qD",
7712 i + 1, in_decl);
7713 if (is_type)
7714 inform (input_location,
7715 " expected a constant of type %qT, got %qT",
7716 TREE_TYPE (parm),
7717 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7718 else if (requires_tmpl_type)
7719 inform (input_location,
7720 " expected a class template, got %qE", orig_arg);
7721 else
7722 inform (input_location,
7723 " expected a type, got %qE", orig_arg);
7726 return error_mark_node;
7728 if (is_tmpl_type ^ requires_tmpl_type)
7730 if (in_decl && (complain & tf_error))
7732 error ("type/value mismatch at argument %d in template "
7733 "parameter list for %qD",
7734 i + 1, in_decl);
7735 if (is_tmpl_type)
7736 inform (input_location,
7737 " expected a type, got %qT", DECL_NAME (arg));
7738 else
7739 inform (input_location,
7740 " expected a class template, got %qT", orig_arg);
7742 return error_mark_node;
7745 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7746 /* We already did the appropriate conversion when packing args. */
7747 val = orig_arg;
7748 else if (is_type)
7750 if (requires_tmpl_type)
7752 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7753 /* The number of argument required is not known yet.
7754 Just accept it for now. */
7755 val = orig_arg;
7756 else
7758 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7759 tree argparm;
7761 /* Strip alias templates that are equivalent to another
7762 template. */
7763 arg = get_underlying_template (arg);
7764 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7766 if (coerce_template_template_parms (parmparm, argparm,
7767 complain, in_decl,
7768 args))
7770 val = arg;
7772 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7773 TEMPLATE_DECL. */
7774 if (val != error_mark_node)
7776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7777 val = TREE_TYPE (val);
7778 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7779 val = make_pack_expansion (val, complain);
7782 else
7784 if (in_decl && (complain & tf_error))
7786 error ("type/value mismatch at argument %d in "
7787 "template parameter list for %qD",
7788 i + 1, in_decl);
7789 inform (input_location,
7790 " expected a template of type %qD, got %qT",
7791 parm, orig_arg);
7794 val = error_mark_node;
7797 // Check that the constraints are compatible before allowing the
7798 // substitution.
7799 if (val != error_mark_node)
7800 if (!is_compatible_template_arg (parm, arg))
7802 if (in_decl && (complain & tf_error))
7804 error ("constraint mismatch at argument %d in "
7805 "template parameter list for %qD",
7806 i + 1, in_decl);
7807 inform (input_location, " expected %qD but got %qD",
7808 parm, arg);
7810 val = error_mark_node;
7814 else
7815 val = orig_arg;
7816 /* We only form one instance of each template specialization.
7817 Therefore, if we use a non-canonical variant (i.e., a
7818 typedef), any future messages referring to the type will use
7819 the typedef, which is confusing if those future uses do not
7820 themselves also use the typedef. */
7821 if (TYPE_P (val))
7822 val = canonicalize_type_argument (val, complain);
7824 else
7826 tree t = TREE_TYPE (parm);
7828 if (tree a = type_uses_auto (t))
7830 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7831 if (t == error_mark_node)
7832 return error_mark_node;
7834 else
7835 t = tsubst (t, args, complain, in_decl);
7837 if (invalid_nontype_parm_type_p (t, complain))
7838 return error_mark_node;
7840 if (!type_dependent_expression_p (orig_arg)
7841 && !uses_template_parms (t))
7842 /* We used to call digest_init here. However, digest_init
7843 will report errors, which we don't want when complain
7844 is zero. More importantly, digest_init will try too
7845 hard to convert things: for example, `0' should not be
7846 converted to pointer type at this point according to
7847 the standard. Accepting this is not merely an
7848 extension, since deciding whether or not these
7849 conversions can occur is part of determining which
7850 function template to call, or whether a given explicit
7851 argument specification is valid. */
7852 val = convert_nontype_argument (t, orig_arg, complain);
7853 else
7855 val = canonicalize_expr_argument (orig_arg, complain);
7856 val = maybe_convert_nontype_argument (t, val);
7860 if (val == NULL_TREE)
7861 val = error_mark_node;
7862 else if (val == error_mark_node && (complain & tf_error))
7863 error ("could not convert template argument %qE from %qT to %qT",
7864 orig_arg, TREE_TYPE (orig_arg), t);
7866 if (INDIRECT_REF_P (val))
7868 /* Reject template arguments that are references to built-in
7869 functions with no library fallbacks. */
7870 const_tree inner = TREE_OPERAND (val, 0);
7871 const_tree innertype = TREE_TYPE (inner);
7872 if (innertype
7873 && TREE_CODE (innertype) == REFERENCE_TYPE
7874 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7875 && TREE_OPERAND_LENGTH (inner) > 0
7876 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7877 return error_mark_node;
7880 if (TREE_CODE (val) == SCOPE_REF)
7882 /* Strip typedefs from the SCOPE_REF. */
7883 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7884 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7885 complain);
7886 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7887 QUALIFIED_NAME_IS_TEMPLATE (val));
7891 return val;
7894 /* Coerces the remaining template arguments in INNER_ARGS (from
7895 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7896 Returns the coerced argument pack. PARM_IDX is the position of this
7897 parameter in the template parameter list. ARGS is the original
7898 template argument list. */
7899 static tree
7900 coerce_template_parameter_pack (tree parms,
7901 int parm_idx,
7902 tree args,
7903 tree inner_args,
7904 int arg_idx,
7905 tree new_args,
7906 int* lost,
7907 tree in_decl,
7908 tsubst_flags_t complain)
7910 tree parm = TREE_VEC_ELT (parms, parm_idx);
7911 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7912 tree packed_args;
7913 tree argument_pack;
7914 tree packed_parms = NULL_TREE;
7916 if (arg_idx > nargs)
7917 arg_idx = nargs;
7919 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7921 /* When the template parameter is a non-type template parameter pack
7922 or template template parameter pack whose type or template
7923 parameters use parameter packs, we know exactly how many arguments
7924 we are looking for. Build a vector of the instantiated decls for
7925 these template parameters in PACKED_PARMS. */
7926 /* We can't use make_pack_expansion here because it would interpret a
7927 _DECL as a use rather than a declaration. */
7928 tree decl = TREE_VALUE (parm);
7929 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7930 SET_PACK_EXPANSION_PATTERN (exp, decl);
7931 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7932 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7934 TREE_VEC_LENGTH (args)--;
7935 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7936 TREE_VEC_LENGTH (args)++;
7938 if (packed_parms == error_mark_node)
7939 return error_mark_node;
7941 /* If we're doing a partial instantiation of a member template,
7942 verify that all of the types used for the non-type
7943 template parameter pack are, in fact, valid for non-type
7944 template parameters. */
7945 if (arg_idx < nargs
7946 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7948 int j, len = TREE_VEC_LENGTH (packed_parms);
7949 for (j = 0; j < len; ++j)
7951 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7952 if (invalid_nontype_parm_type_p (t, complain))
7953 return error_mark_node;
7955 /* We don't know how many args we have yet, just
7956 use the unconverted ones for now. */
7957 return NULL_TREE;
7960 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7962 /* Check if we have a placeholder pack, which indicates we're
7963 in the context of a introduction list. In that case we want
7964 to match this pack to the single placeholder. */
7965 else if (arg_idx < nargs
7966 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7967 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7969 nargs = arg_idx + 1;
7970 packed_args = make_tree_vec (1);
7972 else
7973 packed_args = make_tree_vec (nargs - arg_idx);
7975 /* Convert the remaining arguments, which will be a part of the
7976 parameter pack "parm". */
7977 int first_pack_arg = arg_idx;
7978 for (; arg_idx < nargs; ++arg_idx)
7980 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7981 tree actual_parm = TREE_VALUE (parm);
7982 int pack_idx = arg_idx - first_pack_arg;
7984 if (packed_parms)
7986 /* Once we've packed as many args as we have types, stop. */
7987 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7988 break;
7989 else if (PACK_EXPANSION_P (arg))
7990 /* We don't know how many args we have yet, just
7991 use the unconverted ones for now. */
7992 return NULL_TREE;
7993 else
7994 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7997 if (arg == error_mark_node)
7999 if (complain & tf_error)
8000 error ("template argument %d is invalid", arg_idx + 1);
8002 else
8003 arg = convert_template_argument (actual_parm,
8004 arg, new_args, complain, parm_idx,
8005 in_decl);
8006 if (arg == error_mark_node)
8007 (*lost)++;
8008 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8011 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8012 && TREE_VEC_LENGTH (packed_args) > 0)
8014 if (complain & tf_error)
8015 error ("wrong number of template arguments (%d, should be %d)",
8016 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8017 return error_mark_node;
8020 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8021 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8022 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8023 else
8025 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8026 TREE_CONSTANT (argument_pack) = 1;
8029 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8030 if (CHECKING_P)
8031 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8032 TREE_VEC_LENGTH (packed_args));
8033 return argument_pack;
8036 /* Returns the number of pack expansions in the template argument vector
8037 ARGS. */
8039 static int
8040 pack_expansion_args_count (tree args)
8042 int i;
8043 int count = 0;
8044 if (args)
8045 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8047 tree elt = TREE_VEC_ELT (args, i);
8048 if (elt && PACK_EXPANSION_P (elt))
8049 ++count;
8051 return count;
8054 /* Convert all template arguments to their appropriate types, and
8055 return a vector containing the innermost resulting template
8056 arguments. If any error occurs, return error_mark_node. Error and
8057 warning messages are issued under control of COMPLAIN.
8059 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8060 for arguments not specified in ARGS. Otherwise, if
8061 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8062 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8063 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8064 ARGS. */
8066 static tree
8067 coerce_template_parms (tree parms,
8068 tree args,
8069 tree in_decl,
8070 tsubst_flags_t complain,
8071 bool require_all_args,
8072 bool use_default_args)
8074 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8075 tree orig_inner_args;
8076 tree inner_args;
8077 tree new_args;
8078 tree new_inner_args;
8079 int saved_unevaluated_operand;
8080 int saved_inhibit_evaluation_warnings;
8082 /* When used as a boolean value, indicates whether this is a
8083 variadic template parameter list. Since it's an int, we can also
8084 subtract it from nparms to get the number of non-variadic
8085 parameters. */
8086 int variadic_p = 0;
8087 int variadic_args_p = 0;
8088 int post_variadic_parms = 0;
8090 /* Likewise for parameters with default arguments. */
8091 int default_p = 0;
8093 if (args == error_mark_node)
8094 return error_mark_node;
8096 nparms = TREE_VEC_LENGTH (parms);
8098 /* Determine if there are any parameter packs or default arguments. */
8099 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8101 tree parm = TREE_VEC_ELT (parms, parm_idx);
8102 if (variadic_p)
8103 ++post_variadic_parms;
8104 if (template_parameter_pack_p (TREE_VALUE (parm)))
8105 ++variadic_p;
8106 if (TREE_PURPOSE (parm))
8107 ++default_p;
8110 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8111 /* If there are no parameters that follow a parameter pack, we need to
8112 expand any argument packs so that we can deduce a parameter pack from
8113 some non-packed args followed by an argument pack, as in variadic85.C.
8114 If there are such parameters, we need to leave argument packs intact
8115 so the arguments are assigned properly. This can happen when dealing
8116 with a nested class inside a partial specialization of a class
8117 template, as in variadic92.C, or when deducing a template parameter pack
8118 from a sub-declarator, as in variadic114.C. */
8119 if (!post_variadic_parms)
8120 inner_args = expand_template_argument_pack (inner_args);
8122 /* Count any pack expansion args. */
8123 variadic_args_p = pack_expansion_args_count (inner_args);
8125 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8126 if ((nargs - variadic_args_p > nparms && !variadic_p)
8127 || (nargs < nparms - variadic_p
8128 && require_all_args
8129 && !variadic_args_p
8130 && (!use_default_args
8131 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8132 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8134 if (complain & tf_error)
8136 if (variadic_p || default_p)
8138 nparms -= variadic_p + default_p;
8139 error ("wrong number of template arguments "
8140 "(%d, should be at least %d)", nargs, nparms);
8142 else
8143 error ("wrong number of template arguments "
8144 "(%d, should be %d)", nargs, nparms);
8146 if (in_decl)
8147 inform (DECL_SOURCE_LOCATION (in_decl),
8148 "provided for %qD", in_decl);
8151 return error_mark_node;
8153 /* We can't pass a pack expansion to a non-pack parameter of an alias
8154 template (DR 1430). */
8155 else if (in_decl
8156 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8157 || concept_template_p (in_decl))
8158 && variadic_args_p
8159 && nargs - variadic_args_p < nparms - variadic_p)
8161 if (complain & tf_error)
8163 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8165 tree arg = TREE_VEC_ELT (inner_args, i);
8166 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8168 if (PACK_EXPANSION_P (arg)
8169 && !template_parameter_pack_p (parm))
8171 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8172 error_at (location_of (arg),
8173 "pack expansion argument for non-pack parameter "
8174 "%qD of alias template %qD", parm, in_decl);
8175 else
8176 error_at (location_of (arg),
8177 "pack expansion argument for non-pack parameter "
8178 "%qD of concept %qD", parm, in_decl);
8179 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8180 goto found;
8183 gcc_unreachable ();
8184 found:;
8186 return error_mark_node;
8189 /* We need to evaluate the template arguments, even though this
8190 template-id may be nested within a "sizeof". */
8191 saved_unevaluated_operand = cp_unevaluated_operand;
8192 cp_unevaluated_operand = 0;
8193 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8194 c_inhibit_evaluation_warnings = 0;
8195 new_inner_args = make_tree_vec (nparms);
8196 new_args = add_outermost_template_args (args, new_inner_args);
8197 int pack_adjust = 0;
8198 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8200 tree arg;
8201 tree parm;
8203 /* Get the Ith template parameter. */
8204 parm = TREE_VEC_ELT (parms, parm_idx);
8206 if (parm == error_mark_node)
8208 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8209 continue;
8212 /* Calculate the next argument. */
8213 if (arg_idx < nargs)
8214 arg = TREE_VEC_ELT (inner_args, arg_idx);
8215 else
8216 arg = NULL_TREE;
8218 if (template_parameter_pack_p (TREE_VALUE (parm))
8219 && !(arg && ARGUMENT_PACK_P (arg)))
8221 /* Some arguments will be placed in the
8222 template parameter pack PARM. */
8223 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8224 inner_args, arg_idx,
8225 new_args, &lost,
8226 in_decl, complain);
8228 if (arg == NULL_TREE)
8230 /* We don't know how many args we have yet, just use the
8231 unconverted (and still packed) ones for now. */
8232 new_inner_args = orig_inner_args;
8233 arg_idx = nargs;
8234 break;
8237 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8239 /* Store this argument. */
8240 if (arg == error_mark_node)
8242 lost++;
8243 /* We are done with all of the arguments. */
8244 arg_idx = nargs;
8246 else
8248 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8249 arg_idx += pack_adjust;
8252 continue;
8254 else if (arg)
8256 if (PACK_EXPANSION_P (arg))
8258 /* "If every valid specialization of a variadic template
8259 requires an empty template parameter pack, the template is
8260 ill-formed, no diagnostic required." So check that the
8261 pattern works with this parameter. */
8262 tree pattern = PACK_EXPANSION_PATTERN (arg);
8263 tree conv = convert_template_argument (TREE_VALUE (parm),
8264 pattern, new_args,
8265 complain, parm_idx,
8266 in_decl);
8267 if (conv == error_mark_node)
8269 if (complain & tf_error)
8270 inform (input_location, "so any instantiation with a "
8271 "non-empty parameter pack would be ill-formed");
8272 ++lost;
8274 else if (TYPE_P (conv) && !TYPE_P (pattern))
8275 /* Recover from missing typename. */
8276 TREE_VEC_ELT (inner_args, arg_idx)
8277 = make_pack_expansion (conv, complain);
8279 /* We don't know how many args we have yet, just
8280 use the unconverted ones for now. */
8281 new_inner_args = inner_args;
8282 arg_idx = nargs;
8283 break;
8286 else if (require_all_args)
8288 /* There must be a default arg in this case. */
8289 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8290 complain, in_decl);
8291 /* The position of the first default template argument,
8292 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8293 Record that. */
8294 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8295 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8296 arg_idx - pack_adjust);
8298 else
8299 break;
8301 if (arg == error_mark_node)
8303 if (complain & tf_error)
8304 error ("template argument %d is invalid", arg_idx + 1);
8306 else if (!arg)
8307 /* This only occurs if there was an error in the template
8308 parameter list itself (which we would already have
8309 reported) that we are trying to recover from, e.g., a class
8310 template with a parameter list such as
8311 template<typename..., typename>. */
8312 ++lost;
8313 else
8314 arg = convert_template_argument (TREE_VALUE (parm),
8315 arg, new_args, complain,
8316 parm_idx, in_decl);
8318 if (arg == error_mark_node)
8319 lost++;
8320 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8322 cp_unevaluated_operand = saved_unevaluated_operand;
8323 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8325 if (variadic_p && arg_idx < nargs)
8327 if (complain & tf_error)
8329 error ("wrong number of template arguments "
8330 "(%d, should be %d)", nargs, arg_idx);
8331 if (in_decl)
8332 error ("provided for %q+D", in_decl);
8334 return error_mark_node;
8337 if (lost)
8338 return error_mark_node;
8340 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8341 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8342 TREE_VEC_LENGTH (new_inner_args));
8344 return new_inner_args;
8347 /* Convert all template arguments to their appropriate types, and
8348 return a vector containing the innermost resulting template
8349 arguments. If any error occurs, return error_mark_node. Error and
8350 warning messages are not issued.
8352 Note that no function argument deduction is performed, and default
8353 arguments are used to fill in unspecified arguments. */
8354 tree
8355 coerce_template_parms (tree parms, tree args, tree in_decl)
8357 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8360 /* Convert all template arguments to their appropriate type, and
8361 instantiate default arguments as needed. This returns a vector
8362 containing the innermost resulting template arguments, or
8363 error_mark_node if unsuccessful. */
8364 tree
8365 coerce_template_parms (tree parms, tree args, tree in_decl,
8366 tsubst_flags_t complain)
8368 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8371 /* Like coerce_template_parms. If PARMS represents all template
8372 parameters levels, this function returns a vector of vectors
8373 representing all the resulting argument levels. Note that in this
8374 case, only the innermost arguments are coerced because the
8375 outermost ones are supposed to have been coerced already.
8377 Otherwise, if PARMS represents only (the innermost) vector of
8378 parameters, this function returns a vector containing just the
8379 innermost resulting arguments. */
8381 static tree
8382 coerce_innermost_template_parms (tree parms,
8383 tree args,
8384 tree in_decl,
8385 tsubst_flags_t complain,
8386 bool require_all_args,
8387 bool use_default_args)
8389 int parms_depth = TMPL_PARMS_DEPTH (parms);
8390 int args_depth = TMPL_ARGS_DEPTH (args);
8391 tree coerced_args;
8393 if (parms_depth > 1)
8395 coerced_args = make_tree_vec (parms_depth);
8396 tree level;
8397 int cur_depth;
8399 for (level = parms, cur_depth = parms_depth;
8400 parms_depth > 0 && level != NULL_TREE;
8401 level = TREE_CHAIN (level), --cur_depth)
8403 tree l;
8404 if (cur_depth == args_depth)
8405 l = coerce_template_parms (TREE_VALUE (level),
8406 args, in_decl, complain,
8407 require_all_args,
8408 use_default_args);
8409 else
8410 l = TMPL_ARGS_LEVEL (args, cur_depth);
8412 if (l == error_mark_node)
8413 return error_mark_node;
8415 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8418 else
8419 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8420 args, in_decl, complain,
8421 require_all_args,
8422 use_default_args);
8423 return coerced_args;
8426 /* Returns 1 if template args OT and NT are equivalent. */
8429 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8431 if (nt == ot)
8432 return 1;
8433 if (nt == NULL_TREE || ot == NULL_TREE)
8434 return false;
8435 if (nt == any_targ_node || ot == any_targ_node)
8436 return true;
8438 if (TREE_CODE (nt) == TREE_VEC)
8439 /* For member templates */
8440 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8441 else if (PACK_EXPANSION_P (ot))
8442 return (PACK_EXPANSION_P (nt)
8443 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8444 PACK_EXPANSION_PATTERN (nt))
8445 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8446 PACK_EXPANSION_EXTRA_ARGS (nt)));
8447 else if (ARGUMENT_PACK_P (ot))
8449 int i, len;
8450 tree opack, npack;
8452 if (!ARGUMENT_PACK_P (nt))
8453 return 0;
8455 opack = ARGUMENT_PACK_ARGS (ot);
8456 npack = ARGUMENT_PACK_ARGS (nt);
8457 len = TREE_VEC_LENGTH (opack);
8458 if (TREE_VEC_LENGTH (npack) != len)
8459 return 0;
8460 for (i = 0; i < len; ++i)
8461 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8462 TREE_VEC_ELT (npack, i)))
8463 return 0;
8464 return 1;
8466 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8467 gcc_unreachable ();
8468 else if (TYPE_P (nt))
8470 if (!TYPE_P (ot))
8471 return false;
8472 /* Don't treat an alias template specialization with dependent
8473 arguments as equivalent to its underlying type when used as a
8474 template argument; we need them to be distinct so that we
8475 substitute into the specialization arguments at instantiation
8476 time. And aliases can't be equivalent without being ==, so
8477 we don't need to look any deeper.
8479 During partial ordering, however, we need to treat them normally so
8480 that we can order uses of the same alias with different
8481 cv-qualification (79960). */
8482 if (!partial_order
8483 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8484 return false;
8485 else
8486 return same_type_p (ot, nt);
8488 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8489 return 0;
8490 else
8492 /* Try to treat a template non-type argument that has been converted
8493 to the parameter type as equivalent to one that hasn't yet. */
8494 for (enum tree_code code1 = TREE_CODE (ot);
8495 CONVERT_EXPR_CODE_P (code1)
8496 || code1 == NON_LVALUE_EXPR;
8497 code1 = TREE_CODE (ot))
8498 ot = TREE_OPERAND (ot, 0);
8499 for (enum tree_code code2 = TREE_CODE (nt);
8500 CONVERT_EXPR_CODE_P (code2)
8501 || code2 == NON_LVALUE_EXPR;
8502 code2 = TREE_CODE (nt))
8503 nt = TREE_OPERAND (nt, 0);
8505 return cp_tree_equal (ot, nt);
8509 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8510 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8511 NEWARG_PTR with the offending arguments if they are non-NULL. */
8514 comp_template_args (tree oldargs, tree newargs,
8515 tree *oldarg_ptr, tree *newarg_ptr,
8516 bool partial_order)
8518 int i;
8520 if (oldargs == newargs)
8521 return 1;
8523 if (!oldargs || !newargs)
8524 return 0;
8526 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8527 return 0;
8529 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8531 tree nt = TREE_VEC_ELT (newargs, i);
8532 tree ot = TREE_VEC_ELT (oldargs, i);
8534 if (! template_args_equal (ot, nt, partial_order))
8536 if (oldarg_ptr != NULL)
8537 *oldarg_ptr = ot;
8538 if (newarg_ptr != NULL)
8539 *newarg_ptr = nt;
8540 return 0;
8543 return 1;
8546 inline bool
8547 comp_template_args_porder (tree oargs, tree nargs)
8549 return comp_template_args (oargs, nargs, NULL, NULL, true);
8552 static void
8553 add_pending_template (tree d)
8555 tree ti = (TYPE_P (d)
8556 ? CLASSTYPE_TEMPLATE_INFO (d)
8557 : DECL_TEMPLATE_INFO (d));
8558 struct pending_template *pt;
8559 int level;
8561 if (TI_PENDING_TEMPLATE_FLAG (ti))
8562 return;
8564 /* We are called both from instantiate_decl, where we've already had a
8565 tinst_level pushed, and instantiate_template, where we haven't.
8566 Compensate. */
8567 level = !current_tinst_level || current_tinst_level->decl != d;
8569 if (level)
8570 push_tinst_level (d);
8572 pt = ggc_alloc<pending_template> ();
8573 pt->next = NULL;
8574 pt->tinst = current_tinst_level;
8575 if (last_pending_template)
8576 last_pending_template->next = pt;
8577 else
8578 pending_templates = pt;
8580 last_pending_template = pt;
8582 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8584 if (level)
8585 pop_tinst_level ();
8589 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8590 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8591 documentation for TEMPLATE_ID_EXPR. */
8593 tree
8594 lookup_template_function (tree fns, tree arglist)
8596 tree type;
8598 if (fns == error_mark_node || arglist == error_mark_node)
8599 return error_mark_node;
8601 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8603 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8605 error ("%q#D is not a function template", fns);
8606 return error_mark_node;
8609 if (BASELINK_P (fns))
8611 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8612 unknown_type_node,
8613 BASELINK_FUNCTIONS (fns),
8614 arglist);
8615 return fns;
8618 type = TREE_TYPE (fns);
8619 if (TREE_CODE (fns) == OVERLOAD || !type)
8620 type = unknown_type_node;
8622 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8625 /* Within the scope of a template class S<T>, the name S gets bound
8626 (in build_self_reference) to a TYPE_DECL for the class, not a
8627 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8628 or one of its enclosing classes, and that type is a template,
8629 return the associated TEMPLATE_DECL. Otherwise, the original
8630 DECL is returned.
8632 Also handle the case when DECL is a TREE_LIST of ambiguous
8633 injected-class-names from different bases. */
8635 tree
8636 maybe_get_template_decl_from_type_decl (tree decl)
8638 if (decl == NULL_TREE)
8639 return decl;
8641 /* DR 176: A lookup that finds an injected-class-name (10.2
8642 [class.member.lookup]) can result in an ambiguity in certain cases
8643 (for example, if it is found in more than one base class). If all of
8644 the injected-class-names that are found refer to specializations of
8645 the same class template, and if the name is followed by a
8646 template-argument-list, the reference refers to the class template
8647 itself and not a specialization thereof, and is not ambiguous. */
8648 if (TREE_CODE (decl) == TREE_LIST)
8650 tree t, tmpl = NULL_TREE;
8651 for (t = decl; t; t = TREE_CHAIN (t))
8653 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8654 if (!tmpl)
8655 tmpl = elt;
8656 else if (tmpl != elt)
8657 break;
8659 if (tmpl && t == NULL_TREE)
8660 return tmpl;
8661 else
8662 return decl;
8665 return (decl != NULL_TREE
8666 && DECL_SELF_REFERENCE_P (decl)
8667 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8668 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8671 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8672 parameters, find the desired type.
8674 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8676 IN_DECL, if non-NULL, is the template declaration we are trying to
8677 instantiate.
8679 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8680 the class we are looking up.
8682 Issue error and warning messages under control of COMPLAIN.
8684 If the template class is really a local class in a template
8685 function, then the FUNCTION_CONTEXT is the function in which it is
8686 being instantiated.
8688 ??? Note that this function is currently called *twice* for each
8689 template-id: the first time from the parser, while creating the
8690 incomplete type (finish_template_type), and the second type during the
8691 real instantiation (instantiate_template_class). This is surely something
8692 that we want to avoid. It also causes some problems with argument
8693 coercion (see convert_nontype_argument for more information on this). */
8695 static tree
8696 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8697 int entering_scope, tsubst_flags_t complain)
8699 tree templ = NULL_TREE, parmlist;
8700 tree t;
8701 spec_entry **slot;
8702 spec_entry *entry;
8703 spec_entry elt;
8704 hashval_t hash;
8706 if (identifier_p (d1))
8708 tree value = innermost_non_namespace_value (d1);
8709 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8710 templ = value;
8711 else
8713 if (context)
8714 push_decl_namespace (context);
8715 templ = lookup_name (d1);
8716 templ = maybe_get_template_decl_from_type_decl (templ);
8717 if (context)
8718 pop_decl_namespace ();
8720 if (templ)
8721 context = DECL_CONTEXT (templ);
8723 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8725 tree type = TREE_TYPE (d1);
8727 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8728 an implicit typename for the second A. Deal with it. */
8729 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8730 type = TREE_TYPE (type);
8732 if (CLASSTYPE_TEMPLATE_INFO (type))
8734 templ = CLASSTYPE_TI_TEMPLATE (type);
8735 d1 = DECL_NAME (templ);
8738 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8739 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8741 templ = TYPE_TI_TEMPLATE (d1);
8742 d1 = DECL_NAME (templ);
8744 else if (DECL_TYPE_TEMPLATE_P (d1))
8746 templ = d1;
8747 d1 = DECL_NAME (templ);
8748 context = DECL_CONTEXT (templ);
8750 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8752 templ = d1;
8753 d1 = DECL_NAME (templ);
8756 /* Issue an error message if we didn't find a template. */
8757 if (! templ)
8759 if (complain & tf_error)
8760 error ("%qT is not a template", d1);
8761 return error_mark_node;
8764 if (TREE_CODE (templ) != TEMPLATE_DECL
8765 /* Make sure it's a user visible template, if it was named by
8766 the user. */
8767 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8768 && !PRIMARY_TEMPLATE_P (templ)))
8770 if (complain & tf_error)
8772 error ("non-template type %qT used as a template", d1);
8773 if (in_decl)
8774 error ("for template declaration %q+D", in_decl);
8776 return error_mark_node;
8779 complain &= ~tf_user;
8781 /* An alias that just changes the name of a template is equivalent to the
8782 other template, so if any of the arguments are pack expansions, strip
8783 the alias to avoid problems with a pack expansion passed to a non-pack
8784 alias template parameter (DR 1430). */
8785 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8786 templ = get_underlying_template (templ);
8788 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8790 tree parm;
8791 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8792 if (arglist2 == error_mark_node
8793 || (!uses_template_parms (arglist2)
8794 && check_instantiated_args (templ, arglist2, complain)))
8795 return error_mark_node;
8797 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8798 return parm;
8800 else
8802 tree template_type = TREE_TYPE (templ);
8803 tree gen_tmpl;
8804 tree type_decl;
8805 tree found = NULL_TREE;
8806 int arg_depth;
8807 int parm_depth;
8808 int is_dependent_type;
8809 int use_partial_inst_tmpl = false;
8811 if (template_type == error_mark_node)
8812 /* An error occurred while building the template TEMPL, and a
8813 diagnostic has most certainly been emitted for that
8814 already. Let's propagate that error. */
8815 return error_mark_node;
8817 gen_tmpl = most_general_template (templ);
8818 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8819 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8820 arg_depth = TMPL_ARGS_DEPTH (arglist);
8822 if (arg_depth == 1 && parm_depth > 1)
8824 /* We've been given an incomplete set of template arguments.
8825 For example, given:
8827 template <class T> struct S1 {
8828 template <class U> struct S2 {};
8829 template <class U> struct S2<U*> {};
8832 we will be called with an ARGLIST of `U*', but the
8833 TEMPLATE will be `template <class T> template
8834 <class U> struct S1<T>::S2'. We must fill in the missing
8835 arguments. */
8836 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8837 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8838 arg_depth = TMPL_ARGS_DEPTH (arglist);
8841 /* Now we should have enough arguments. */
8842 gcc_assert (parm_depth == arg_depth);
8844 /* From here on, we're only interested in the most general
8845 template. */
8847 /* Calculate the BOUND_ARGS. These will be the args that are
8848 actually tsubst'd into the definition to create the
8849 instantiation. */
8850 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8851 complain,
8852 /*require_all_args=*/true,
8853 /*use_default_args=*/true);
8855 if (arglist == error_mark_node)
8856 /* We were unable to bind the arguments. */
8857 return error_mark_node;
8859 /* In the scope of a template class, explicit references to the
8860 template class refer to the type of the template, not any
8861 instantiation of it. For example, in:
8863 template <class T> class C { void f(C<T>); }
8865 the `C<T>' is just the same as `C'. Outside of the
8866 class, however, such a reference is an instantiation. */
8867 if (entering_scope
8868 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8869 || currently_open_class (template_type))
8871 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8873 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8874 return template_type;
8877 /* If we already have this specialization, return it. */
8878 elt.tmpl = gen_tmpl;
8879 elt.args = arglist;
8880 elt.spec = NULL_TREE;
8881 hash = spec_hasher::hash (&elt);
8882 entry = type_specializations->find_with_hash (&elt, hash);
8884 if (entry)
8885 return entry->spec;
8887 /* If the the template's constraints are not satisfied,
8888 then we cannot form a valid type.
8890 Note that the check is deferred until after the hash
8891 lookup. This prevents redundant checks on previously
8892 instantiated specializations. */
8893 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8895 if (complain & tf_error)
8897 error ("template constraint failure");
8898 diagnose_constraints (input_location, gen_tmpl, arglist);
8900 return error_mark_node;
8903 is_dependent_type = uses_template_parms (arglist);
8905 /* If the deduced arguments are invalid, then the binding
8906 failed. */
8907 if (!is_dependent_type
8908 && check_instantiated_args (gen_tmpl,
8909 INNERMOST_TEMPLATE_ARGS (arglist),
8910 complain))
8911 return error_mark_node;
8913 if (!is_dependent_type
8914 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8915 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8916 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8918 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8919 DECL_NAME (gen_tmpl),
8920 /*tag_scope=*/ts_global);
8921 return found;
8924 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8925 complain, in_decl);
8926 if (context == error_mark_node)
8927 return error_mark_node;
8929 if (!context)
8930 context = global_namespace;
8932 /* Create the type. */
8933 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8935 /* The user referred to a specialization of an alias
8936 template represented by GEN_TMPL.
8938 [temp.alias]/2 says:
8940 When a template-id refers to the specialization of an
8941 alias template, it is equivalent to the associated
8942 type obtained by substitution of its
8943 template-arguments for the template-parameters in the
8944 type-id of the alias template. */
8946 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8947 /* Note that the call above (by indirectly calling
8948 register_specialization in tsubst_decl) registers the
8949 TYPE_DECL representing the specialization of the alias
8950 template. So next time someone substitutes ARGLIST for
8951 the template parms into the alias template (GEN_TMPL),
8952 she'll get that TYPE_DECL back. */
8954 if (t == error_mark_node)
8955 return t;
8957 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8959 if (!is_dependent_type)
8961 set_current_access_from_decl (TYPE_NAME (template_type));
8962 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8963 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8964 arglist, complain, in_decl),
8965 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8966 arglist, complain, in_decl),
8967 SCOPED_ENUM_P (template_type), NULL);
8969 if (t == error_mark_node)
8970 return t;
8972 else
8974 /* We don't want to call start_enum for this type, since
8975 the values for the enumeration constants may involve
8976 template parameters. And, no one should be interested
8977 in the enumeration constants for such a type. */
8978 t = cxx_make_type (ENUMERAL_TYPE);
8979 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8981 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8982 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8983 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8985 else if (CLASS_TYPE_P (template_type))
8987 /* Lambda closures are regenerated in tsubst_lambda_expr, not
8988 instantiated here. */
8989 gcc_assert (!LAMBDA_TYPE_P (template_type));
8991 t = make_class_type (TREE_CODE (template_type));
8992 CLASSTYPE_DECLARED_CLASS (t)
8993 = CLASSTYPE_DECLARED_CLASS (template_type);
8994 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8996 /* A local class. Make sure the decl gets registered properly. */
8997 if (context == current_function_decl)
8998 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9000 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9001 /* This instantiation is another name for the primary
9002 template type. Set the TYPE_CANONICAL field
9003 appropriately. */
9004 TYPE_CANONICAL (t) = template_type;
9005 else if (any_template_arguments_need_structural_equality_p (arglist))
9006 /* Some of the template arguments require structural
9007 equality testing, so this template class requires
9008 structural equality testing. */
9009 SET_TYPE_STRUCTURAL_EQUALITY (t);
9011 else
9012 gcc_unreachable ();
9014 /* If we called start_enum or pushtag above, this information
9015 will already be set up. */
9016 if (!TYPE_NAME (t))
9018 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9020 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9021 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9022 DECL_SOURCE_LOCATION (type_decl)
9023 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9025 else
9026 type_decl = TYPE_NAME (t);
9028 if (CLASS_TYPE_P (template_type))
9030 TREE_PRIVATE (type_decl)
9031 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9032 TREE_PROTECTED (type_decl)
9033 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9034 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9036 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9037 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9041 if (OVERLOAD_TYPE_P (t)
9042 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9044 static const char *tags[] = {"abi_tag", "may_alias"};
9046 for (unsigned ix = 0; ix != 2; ix++)
9048 tree attributes
9049 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9051 if (attributes)
9052 TYPE_ATTRIBUTES (t)
9053 = tree_cons (TREE_PURPOSE (attributes),
9054 TREE_VALUE (attributes),
9055 TYPE_ATTRIBUTES (t));
9059 /* Let's consider the explicit specialization of a member
9060 of a class template specialization that is implicitly instantiated,
9061 e.g.:
9062 template<class T>
9063 struct S
9065 template<class U> struct M {}; //#0
9068 template<>
9069 template<>
9070 struct S<int>::M<char> //#1
9072 int i;
9074 [temp.expl.spec]/4 says this is valid.
9076 In this case, when we write:
9077 S<int>::M<char> m;
9079 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9080 the one of #0.
9082 When we encounter #1, we want to store the partial instantiation
9083 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9085 For all cases other than this "explicit specialization of member of a
9086 class template", we just want to store the most general template into
9087 the CLASSTYPE_TI_TEMPLATE of M.
9089 This case of "explicit specialization of member of a class template"
9090 only happens when:
9091 1/ the enclosing class is an instantiation of, and therefore not
9092 the same as, the context of the most general template, and
9093 2/ we aren't looking at the partial instantiation itself, i.e.
9094 the innermost arguments are not the same as the innermost parms of
9095 the most general template.
9097 So it's only when 1/ and 2/ happens that we want to use the partial
9098 instantiation of the member template in lieu of its most general
9099 template. */
9101 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9102 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9103 /* the enclosing class must be an instantiation... */
9104 && CLASS_TYPE_P (context)
9105 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9107 TREE_VEC_LENGTH (arglist)--;
9108 ++processing_template_decl;
9109 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9110 tree partial_inst_args =
9111 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9112 arglist, complain, NULL_TREE);
9113 --processing_template_decl;
9114 TREE_VEC_LENGTH (arglist)++;
9115 if (partial_inst_args == error_mark_node)
9116 return error_mark_node;
9117 use_partial_inst_tmpl =
9118 /*...and we must not be looking at the partial instantiation
9119 itself. */
9120 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9121 partial_inst_args);
9124 if (!use_partial_inst_tmpl)
9125 /* This case is easy; there are no member templates involved. */
9126 found = gen_tmpl;
9127 else
9129 /* This is a full instantiation of a member template. Find
9130 the partial instantiation of which this is an instance. */
9132 /* Temporarily reduce by one the number of levels in the ARGLIST
9133 so as to avoid comparing the last set of arguments. */
9134 TREE_VEC_LENGTH (arglist)--;
9135 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9136 TREE_VEC_LENGTH (arglist)++;
9137 /* FOUND is either a proper class type, or an alias
9138 template specialization. In the later case, it's a
9139 TYPE_DECL, resulting from the substituting of arguments
9140 for parameters in the TYPE_DECL of the alias template
9141 done earlier. So be careful while getting the template
9142 of FOUND. */
9143 found = (TREE_CODE (found) == TEMPLATE_DECL
9144 ? found
9145 : (TREE_CODE (found) == TYPE_DECL
9146 ? DECL_TI_TEMPLATE (found)
9147 : CLASSTYPE_TI_TEMPLATE (found)));
9150 // Build template info for the new specialization.
9151 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9153 elt.spec = t;
9154 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9155 entry = ggc_alloc<spec_entry> ();
9156 *entry = elt;
9157 *slot = entry;
9159 /* Note this use of the partial instantiation so we can check it
9160 later in maybe_process_partial_specialization. */
9161 DECL_TEMPLATE_INSTANTIATIONS (found)
9162 = tree_cons (arglist, t,
9163 DECL_TEMPLATE_INSTANTIATIONS (found));
9165 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9166 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9167 /* Now that the type has been registered on the instantiations
9168 list, we set up the enumerators. Because the enumeration
9169 constants may involve the enumeration type itself, we make
9170 sure to register the type first, and then create the
9171 constants. That way, doing tsubst_expr for the enumeration
9172 constants won't result in recursive calls here; we'll find
9173 the instantiation and exit above. */
9174 tsubst_enum (template_type, t, arglist);
9176 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9177 /* If the type makes use of template parameters, the
9178 code that generates debugging information will crash. */
9179 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9181 /* Possibly limit visibility based on template args. */
9182 TREE_PUBLIC (type_decl) = 1;
9183 determine_visibility (type_decl);
9185 inherit_targ_abi_tags (t);
9187 return t;
9191 /* Wrapper for lookup_template_class_1. */
9193 tree
9194 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9195 int entering_scope, tsubst_flags_t complain)
9197 tree ret;
9198 timevar_push (TV_TEMPLATE_INST);
9199 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9200 entering_scope, complain);
9201 timevar_pop (TV_TEMPLATE_INST);
9202 return ret;
9205 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9207 tree
9208 lookup_template_variable (tree templ, tree arglist)
9210 /* The type of the expression is NULL_TREE since the template-id could refer
9211 to an explicit or partial specialization. */
9212 tree type = NULL_TREE;
9213 if (flag_concepts && variable_concept_p (templ))
9214 /* Except that concepts are always bool. */
9215 type = boolean_type_node;
9216 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9219 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9221 tree
9222 finish_template_variable (tree var, tsubst_flags_t complain)
9224 tree templ = TREE_OPERAND (var, 0);
9225 tree arglist = TREE_OPERAND (var, 1);
9227 /* We never want to return a VAR_DECL for a variable concept, since they
9228 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9229 bool concept_p = flag_concepts && variable_concept_p (templ);
9230 if (concept_p && processing_template_decl)
9231 return var;
9233 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9234 arglist = add_outermost_template_args (tmpl_args, arglist);
9236 templ = most_general_template (templ);
9237 tree parms = DECL_TEMPLATE_PARMS (templ);
9238 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9239 /*req_all*/true,
9240 /*use_default*/true);
9242 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9244 if (complain & tf_error)
9246 error ("use of invalid variable template %qE", var);
9247 diagnose_constraints (location_of (var), templ, arglist);
9249 return error_mark_node;
9252 /* If a template-id refers to a specialization of a variable
9253 concept, then the expression is true if and only if the
9254 concept's constraints are satisfied by the given template
9255 arguments.
9257 NOTE: This is an extension of Concepts Lite TS that
9258 allows constraints to be used in expressions. */
9259 if (concept_p)
9261 tree decl = DECL_TEMPLATE_RESULT (templ);
9262 return evaluate_variable_concept (decl, arglist);
9265 return instantiate_template (templ, arglist, complain);
9268 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9269 TARGS template args, and instantiate it if it's not dependent. */
9271 tree
9272 lookup_and_finish_template_variable (tree templ, tree targs,
9273 tsubst_flags_t complain)
9275 templ = lookup_template_variable (templ, targs);
9276 if (!any_dependent_template_arguments_p (targs))
9278 templ = finish_template_variable (templ, complain);
9279 mark_used (templ);
9282 return convert_from_reference (templ);
9286 struct pair_fn_data
9288 tree_fn_t fn;
9289 tree_fn_t any_fn;
9290 void *data;
9291 /* True when we should also visit template parameters that occur in
9292 non-deduced contexts. */
9293 bool include_nondeduced_p;
9294 hash_set<tree> *visited;
9297 /* Called from for_each_template_parm via walk_tree. */
9299 static tree
9300 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9302 tree t = *tp;
9303 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9304 tree_fn_t fn = pfd->fn;
9305 void *data = pfd->data;
9306 tree result = NULL_TREE;
9308 #define WALK_SUBTREE(NODE) \
9309 do \
9311 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9312 pfd->include_nondeduced_p, \
9313 pfd->any_fn); \
9314 if (result) goto out; \
9316 while (0)
9318 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9319 return t;
9321 if (TYPE_P (t)
9322 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9323 WALK_SUBTREE (TYPE_CONTEXT (t));
9325 switch (TREE_CODE (t))
9327 case RECORD_TYPE:
9328 if (TYPE_PTRMEMFUNC_P (t))
9329 break;
9330 /* Fall through. */
9332 case UNION_TYPE:
9333 case ENUMERAL_TYPE:
9334 if (!TYPE_TEMPLATE_INFO (t))
9335 *walk_subtrees = 0;
9336 else
9337 WALK_SUBTREE (TYPE_TI_ARGS (t));
9338 break;
9340 case INTEGER_TYPE:
9341 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9342 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9343 break;
9345 case METHOD_TYPE:
9346 /* Since we're not going to walk subtrees, we have to do this
9347 explicitly here. */
9348 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9349 /* Fall through. */
9351 case FUNCTION_TYPE:
9352 /* Check the return type. */
9353 WALK_SUBTREE (TREE_TYPE (t));
9355 /* Check the parameter types. Since default arguments are not
9356 instantiated until they are needed, the TYPE_ARG_TYPES may
9357 contain expressions that involve template parameters. But,
9358 no-one should be looking at them yet. And, once they're
9359 instantiated, they don't contain template parameters, so
9360 there's no point in looking at them then, either. */
9362 tree parm;
9364 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9365 WALK_SUBTREE (TREE_VALUE (parm));
9367 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9368 want walk_tree walking into them itself. */
9369 *walk_subtrees = 0;
9372 if (flag_noexcept_type)
9374 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9375 if (spec)
9376 WALK_SUBTREE (TREE_PURPOSE (spec));
9378 break;
9380 case TYPEOF_TYPE:
9381 case UNDERLYING_TYPE:
9382 if (pfd->include_nondeduced_p
9383 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9384 pfd->visited,
9385 pfd->include_nondeduced_p,
9386 pfd->any_fn))
9387 return error_mark_node;
9388 break;
9390 case FUNCTION_DECL:
9391 case VAR_DECL:
9392 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9393 WALK_SUBTREE (DECL_TI_ARGS (t));
9394 /* Fall through. */
9396 case PARM_DECL:
9397 case CONST_DECL:
9398 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9399 WALK_SUBTREE (DECL_INITIAL (t));
9400 if (DECL_CONTEXT (t)
9401 && pfd->include_nondeduced_p)
9402 WALK_SUBTREE (DECL_CONTEXT (t));
9403 break;
9405 case BOUND_TEMPLATE_TEMPLATE_PARM:
9406 /* Record template parameters such as `T' inside `TT<T>'. */
9407 WALK_SUBTREE (TYPE_TI_ARGS (t));
9408 /* Fall through. */
9410 case TEMPLATE_TEMPLATE_PARM:
9411 case TEMPLATE_TYPE_PARM:
9412 case TEMPLATE_PARM_INDEX:
9413 if (fn && (*fn)(t, data))
9414 return t;
9415 else if (!fn)
9416 return t;
9417 break;
9419 case TEMPLATE_DECL:
9420 /* A template template parameter is encountered. */
9421 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9422 WALK_SUBTREE (TREE_TYPE (t));
9424 /* Already substituted template template parameter */
9425 *walk_subtrees = 0;
9426 break;
9428 case TYPENAME_TYPE:
9429 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9430 partial instantiation. */
9431 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9432 break;
9434 case CONSTRUCTOR:
9435 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9436 && pfd->include_nondeduced_p)
9437 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9438 break;
9440 case INDIRECT_REF:
9441 case COMPONENT_REF:
9442 /* If there's no type, then this thing must be some expression
9443 involving template parameters. */
9444 if (!fn && !TREE_TYPE (t))
9445 return error_mark_node;
9446 break;
9448 case MODOP_EXPR:
9449 case CAST_EXPR:
9450 case IMPLICIT_CONV_EXPR:
9451 case REINTERPRET_CAST_EXPR:
9452 case CONST_CAST_EXPR:
9453 case STATIC_CAST_EXPR:
9454 case DYNAMIC_CAST_EXPR:
9455 case ARROW_EXPR:
9456 case DOTSTAR_EXPR:
9457 case TYPEID_EXPR:
9458 case PSEUDO_DTOR_EXPR:
9459 if (!fn)
9460 return error_mark_node;
9461 break;
9463 default:
9464 break;
9467 #undef WALK_SUBTREE
9469 /* We didn't find any template parameters we liked. */
9470 out:
9471 return result;
9474 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9475 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9476 call FN with the parameter and the DATA.
9477 If FN returns nonzero, the iteration is terminated, and
9478 for_each_template_parm returns 1. Otherwise, the iteration
9479 continues. If FN never returns a nonzero value, the value
9480 returned by for_each_template_parm is 0. If FN is NULL, it is
9481 considered to be the function which always returns 1.
9483 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9484 parameters that occur in non-deduced contexts. When false, only
9485 visits those template parameters that can be deduced. */
9487 static tree
9488 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9489 hash_set<tree> *visited,
9490 bool include_nondeduced_p,
9491 tree_fn_t any_fn)
9493 struct pair_fn_data pfd;
9494 tree result;
9496 /* Set up. */
9497 pfd.fn = fn;
9498 pfd.any_fn = any_fn;
9499 pfd.data = data;
9500 pfd.include_nondeduced_p = include_nondeduced_p;
9502 /* Walk the tree. (Conceptually, we would like to walk without
9503 duplicates, but for_each_template_parm_r recursively calls
9504 for_each_template_parm, so we would need to reorganize a fair
9505 bit to use walk_tree_without_duplicates, so we keep our own
9506 visited list.) */
9507 if (visited)
9508 pfd.visited = visited;
9509 else
9510 pfd.visited = new hash_set<tree>;
9511 result = cp_walk_tree (&t,
9512 for_each_template_parm_r,
9513 &pfd,
9514 pfd.visited);
9516 /* Clean up. */
9517 if (!visited)
9519 delete pfd.visited;
9520 pfd.visited = 0;
9523 return result;
9526 /* Returns true if T depends on any template parameter. */
9529 uses_template_parms (tree t)
9531 if (t == NULL_TREE)
9532 return false;
9534 bool dependent_p;
9535 int saved_processing_template_decl;
9537 saved_processing_template_decl = processing_template_decl;
9538 if (!saved_processing_template_decl)
9539 processing_template_decl = 1;
9540 if (TYPE_P (t))
9541 dependent_p = dependent_type_p (t);
9542 else if (TREE_CODE (t) == TREE_VEC)
9543 dependent_p = any_dependent_template_arguments_p (t);
9544 else if (TREE_CODE (t) == TREE_LIST)
9545 dependent_p = (uses_template_parms (TREE_VALUE (t))
9546 || uses_template_parms (TREE_CHAIN (t)));
9547 else if (TREE_CODE (t) == TYPE_DECL)
9548 dependent_p = dependent_type_p (TREE_TYPE (t));
9549 else if (DECL_P (t)
9550 || EXPR_P (t)
9551 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9552 || TREE_CODE (t) == OVERLOAD
9553 || BASELINK_P (t)
9554 || identifier_p (t)
9555 || TREE_CODE (t) == TRAIT_EXPR
9556 || TREE_CODE (t) == CONSTRUCTOR
9557 || CONSTANT_CLASS_P (t))
9558 dependent_p = (type_dependent_expression_p (t)
9559 || value_dependent_expression_p (t));
9560 else
9562 gcc_assert (t == error_mark_node);
9563 dependent_p = false;
9566 processing_template_decl = saved_processing_template_decl;
9568 return dependent_p;
9571 /* Returns true iff current_function_decl is an incompletely instantiated
9572 template. Useful instead of processing_template_decl because the latter
9573 is set to 0 during instantiate_non_dependent_expr. */
9575 bool
9576 in_template_function (void)
9578 tree fn = current_function_decl;
9579 bool ret;
9580 ++processing_template_decl;
9581 ret = (fn && DECL_LANG_SPECIFIC (fn)
9582 && DECL_TEMPLATE_INFO (fn)
9583 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9584 --processing_template_decl;
9585 return ret;
9588 /* Returns true if T depends on any template parameter with level LEVEL. */
9590 bool
9591 uses_template_parms_level (tree t, int level)
9593 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9594 /*include_nondeduced_p=*/true);
9597 /* Returns true if the signature of DECL depends on any template parameter from
9598 its enclosing class. */
9600 bool
9601 uses_outer_template_parms (tree decl)
9603 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9604 if (depth == 0)
9605 return false;
9606 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9607 &depth, NULL, /*include_nondeduced_p=*/true))
9608 return true;
9609 if (PRIMARY_TEMPLATE_P (decl)
9610 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9611 (DECL_TEMPLATE_PARMS (decl)),
9612 template_parm_outer_level,
9613 &depth, NULL, /*include_nondeduced_p=*/true))
9614 return true;
9615 tree ci = get_constraints (decl);
9616 if (ci)
9617 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9618 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9619 &depth, NULL, /*nondeduced*/true))
9620 return true;
9621 return false;
9624 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9625 ill-formed translation unit, i.e. a variable or function that isn't
9626 usable in a constant expression. */
9628 static inline bool
9629 neglectable_inst_p (tree d)
9631 return (DECL_P (d)
9632 && !undeduced_auto_decl (d)
9633 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9634 : decl_maybe_constant_var_p (d)));
9637 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9638 neglectable and instantiated from within an erroneous instantiation. */
9640 static bool
9641 limit_bad_template_recursion (tree decl)
9643 struct tinst_level *lev = current_tinst_level;
9644 int errs = errorcount + sorrycount;
9645 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9646 return false;
9648 for (; lev; lev = lev->next)
9649 if (neglectable_inst_p (lev->decl))
9650 break;
9652 return (lev && errs > lev->errors);
9655 static int tinst_depth;
9656 extern int max_tinst_depth;
9657 int depth_reached;
9659 static GTY(()) struct tinst_level *last_error_tinst_level;
9661 /* We're starting to instantiate D; record the template instantiation context
9662 for diagnostics and to restore it later. */
9664 bool
9665 push_tinst_level (tree d)
9667 return push_tinst_level_loc (d, input_location);
9670 /* We're starting to instantiate D; record the template instantiation context
9671 at LOC for diagnostics and to restore it later. */
9673 bool
9674 push_tinst_level_loc (tree d, location_t loc)
9676 struct tinst_level *new_level;
9678 if (tinst_depth >= max_tinst_depth)
9680 /* Tell error.c not to try to instantiate any templates. */
9681 at_eof = 2;
9682 fatal_error (input_location,
9683 "template instantiation depth exceeds maximum of %d"
9684 " (use -ftemplate-depth= to increase the maximum)",
9685 max_tinst_depth);
9686 return false;
9689 /* If the current instantiation caused problems, don't let it instantiate
9690 anything else. Do allow deduction substitution and decls usable in
9691 constant expressions. */
9692 if (limit_bad_template_recursion (d))
9693 return false;
9695 /* When not -quiet, dump template instantiations other than functions, since
9696 announce_function will take care of those. */
9697 if (!quiet_flag
9698 && TREE_CODE (d) != TREE_LIST
9699 && TREE_CODE (d) != FUNCTION_DECL)
9700 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9702 new_level = ggc_alloc<tinst_level> ();
9703 new_level->decl = d;
9704 new_level->locus = loc;
9705 new_level->errors = errorcount+sorrycount;
9706 new_level->in_system_header_p = in_system_header_at (input_location);
9707 new_level->next = current_tinst_level;
9708 current_tinst_level = new_level;
9710 ++tinst_depth;
9711 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9712 depth_reached = tinst_depth;
9714 return true;
9717 /* We're done instantiating this template; return to the instantiation
9718 context. */
9720 void
9721 pop_tinst_level (void)
9723 /* Restore the filename and line number stashed away when we started
9724 this instantiation. */
9725 input_location = current_tinst_level->locus;
9726 current_tinst_level = current_tinst_level->next;
9727 --tinst_depth;
9730 /* We're instantiating a deferred template; restore the template
9731 instantiation context in which the instantiation was requested, which
9732 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9734 static tree
9735 reopen_tinst_level (struct tinst_level *level)
9737 struct tinst_level *t;
9739 tinst_depth = 0;
9740 for (t = level; t; t = t->next)
9741 ++tinst_depth;
9743 current_tinst_level = level;
9744 pop_tinst_level ();
9745 if (current_tinst_level)
9746 current_tinst_level->errors = errorcount+sorrycount;
9747 return level->decl;
9750 /* Returns the TINST_LEVEL which gives the original instantiation
9751 context. */
9753 struct tinst_level *
9754 outermost_tinst_level (void)
9756 struct tinst_level *level = current_tinst_level;
9757 if (level)
9758 while (level->next)
9759 level = level->next;
9760 return level;
9763 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9764 vector of template arguments, as for tsubst.
9766 Returns an appropriate tsubst'd friend declaration. */
9768 static tree
9769 tsubst_friend_function (tree decl, tree args)
9771 tree new_friend;
9773 if (TREE_CODE (decl) == FUNCTION_DECL
9774 && DECL_TEMPLATE_INSTANTIATION (decl)
9775 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9776 /* This was a friend declared with an explicit template
9777 argument list, e.g.:
9779 friend void f<>(T);
9781 to indicate that f was a template instantiation, not a new
9782 function declaration. Now, we have to figure out what
9783 instantiation of what template. */
9785 tree template_id, arglist, fns;
9786 tree new_args;
9787 tree tmpl;
9788 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9790 /* Friend functions are looked up in the containing namespace scope.
9791 We must enter that scope, to avoid finding member functions of the
9792 current class with same name. */
9793 push_nested_namespace (ns);
9794 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9795 tf_warning_or_error, NULL_TREE,
9796 /*integral_constant_expression_p=*/false);
9797 pop_nested_namespace (ns);
9798 arglist = tsubst (DECL_TI_ARGS (decl), args,
9799 tf_warning_or_error, NULL_TREE);
9800 template_id = lookup_template_function (fns, arglist);
9802 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9803 tmpl = determine_specialization (template_id, new_friend,
9804 &new_args,
9805 /*need_member_template=*/0,
9806 TREE_VEC_LENGTH (args),
9807 tsk_none);
9808 return instantiate_template (tmpl, new_args, tf_error);
9811 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9813 /* The NEW_FRIEND will look like an instantiation, to the
9814 compiler, but is not an instantiation from the point of view of
9815 the language. For example, we might have had:
9817 template <class T> struct S {
9818 template <class U> friend void f(T, U);
9821 Then, in S<int>, template <class U> void f(int, U) is not an
9822 instantiation of anything. */
9823 if (new_friend == error_mark_node)
9824 return error_mark_node;
9826 DECL_USE_TEMPLATE (new_friend) = 0;
9827 if (TREE_CODE (decl) == TEMPLATE_DECL)
9829 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9830 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9831 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9834 /* The mangled name for the NEW_FRIEND is incorrect. The function
9835 is not a template instantiation and should not be mangled like
9836 one. Therefore, we forget the mangling here; we'll recompute it
9837 later if we need it. */
9838 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9840 SET_DECL_RTL (new_friend, NULL);
9841 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9844 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9846 tree old_decl;
9847 tree new_friend_template_info;
9848 tree new_friend_result_template_info;
9849 tree ns;
9850 int new_friend_is_defn;
9852 /* We must save some information from NEW_FRIEND before calling
9853 duplicate decls since that function will free NEW_FRIEND if
9854 possible. */
9855 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9856 new_friend_is_defn =
9857 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9858 (template_for_substitution (new_friend)))
9859 != NULL_TREE);
9860 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9862 /* This declaration is a `primary' template. */
9863 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9865 new_friend_result_template_info
9866 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9868 else
9869 new_friend_result_template_info = NULL_TREE;
9871 /* Inside pushdecl_namespace_level, we will push into the
9872 current namespace. However, the friend function should go
9873 into the namespace of the template. */
9874 ns = decl_namespace_context (new_friend);
9875 push_nested_namespace (ns);
9876 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9877 pop_nested_namespace (ns);
9879 if (old_decl == error_mark_node)
9880 return error_mark_node;
9882 if (old_decl != new_friend)
9884 /* This new friend declaration matched an existing
9885 declaration. For example, given:
9887 template <class T> void f(T);
9888 template <class U> class C {
9889 template <class T> friend void f(T) {}
9892 the friend declaration actually provides the definition
9893 of `f', once C has been instantiated for some type. So,
9894 old_decl will be the out-of-class template declaration,
9895 while new_friend is the in-class definition.
9897 But, if `f' was called before this point, the
9898 instantiation of `f' will have DECL_TI_ARGS corresponding
9899 to `T' but not to `U', references to which might appear
9900 in the definition of `f'. Previously, the most general
9901 template for an instantiation of `f' was the out-of-class
9902 version; now it is the in-class version. Therefore, we
9903 run through all specialization of `f', adding to their
9904 DECL_TI_ARGS appropriately. In particular, they need a
9905 new set of outer arguments, corresponding to the
9906 arguments for this class instantiation.
9908 The same situation can arise with something like this:
9910 friend void f(int);
9911 template <class T> class C {
9912 friend void f(T) {}
9915 when `C<int>' is instantiated. Now, `f(int)' is defined
9916 in the class. */
9918 if (!new_friend_is_defn)
9919 /* On the other hand, if the in-class declaration does
9920 *not* provide a definition, then we don't want to alter
9921 existing definitions. We can just leave everything
9922 alone. */
9924 else
9926 tree new_template = TI_TEMPLATE (new_friend_template_info);
9927 tree new_args = TI_ARGS (new_friend_template_info);
9929 /* Overwrite whatever template info was there before, if
9930 any, with the new template information pertaining to
9931 the declaration. */
9932 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9934 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9936 /* We should have called reregister_specialization in
9937 duplicate_decls. */
9938 gcc_assert (retrieve_specialization (new_template,
9939 new_args, 0)
9940 == old_decl);
9942 /* Instantiate it if the global has already been used. */
9943 if (DECL_ODR_USED (old_decl))
9944 instantiate_decl (old_decl, /*defer_ok=*/true,
9945 /*expl_inst_class_mem_p=*/false);
9947 else
9949 tree t;
9951 /* Indicate that the old function template is a partial
9952 instantiation. */
9953 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9954 = new_friend_result_template_info;
9956 gcc_assert (new_template
9957 == most_general_template (new_template));
9958 gcc_assert (new_template != old_decl);
9960 /* Reassign any specializations already in the hash table
9961 to the new more general template, and add the
9962 additional template args. */
9963 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9964 t != NULL_TREE;
9965 t = TREE_CHAIN (t))
9967 tree spec = TREE_VALUE (t);
9968 spec_entry elt;
9970 elt.tmpl = old_decl;
9971 elt.args = DECL_TI_ARGS (spec);
9972 elt.spec = NULL_TREE;
9974 decl_specializations->remove_elt (&elt);
9976 DECL_TI_ARGS (spec)
9977 = add_outermost_template_args (new_args,
9978 DECL_TI_ARGS (spec));
9980 register_specialization
9981 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9984 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9988 /* The information from NEW_FRIEND has been merged into OLD_DECL
9989 by duplicate_decls. */
9990 new_friend = old_decl;
9993 else
9995 tree context = DECL_CONTEXT (new_friend);
9996 bool dependent_p;
9998 /* In the code
9999 template <class T> class C {
10000 template <class U> friend void C1<U>::f (); // case 1
10001 friend void C2<T>::f (); // case 2
10003 we only need to make sure CONTEXT is a complete type for
10004 case 2. To distinguish between the two cases, we note that
10005 CONTEXT of case 1 remains dependent type after tsubst while
10006 this isn't true for case 2. */
10007 ++processing_template_decl;
10008 dependent_p = dependent_type_p (context);
10009 --processing_template_decl;
10011 if (!dependent_p
10012 && !complete_type_or_else (context, NULL_TREE))
10013 return error_mark_node;
10015 if (COMPLETE_TYPE_P (context))
10017 tree fn = new_friend;
10018 /* do_friend adds the TEMPLATE_DECL for any member friend
10019 template even if it isn't a member template, i.e.
10020 template <class T> friend A<T>::f();
10021 Look through it in that case. */
10022 if (TREE_CODE (fn) == TEMPLATE_DECL
10023 && !PRIMARY_TEMPLATE_P (fn))
10024 fn = DECL_TEMPLATE_RESULT (fn);
10025 /* Check to see that the declaration is really present, and,
10026 possibly obtain an improved declaration. */
10027 fn = check_classfn (context, fn, NULL_TREE);
10029 if (fn)
10030 new_friend = fn;
10034 return new_friend;
10037 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10038 template arguments, as for tsubst.
10040 Returns an appropriate tsubst'd friend type or error_mark_node on
10041 failure. */
10043 static tree
10044 tsubst_friend_class (tree friend_tmpl, tree args)
10046 tree tmpl;
10048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10050 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10051 return TREE_TYPE (tmpl);
10054 tree context = CP_DECL_CONTEXT (friend_tmpl);
10055 if (TREE_CODE (context) == NAMESPACE_DECL)
10056 push_nested_namespace (context);
10057 else
10058 push_nested_class (context);
10060 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10061 /*non_class=*/false, /*block_p=*/false,
10062 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10064 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10066 /* The friend template has already been declared. Just
10067 check to see that the declarations match, and install any new
10068 default parameters. We must tsubst the default parameters,
10069 of course. We only need the innermost template parameters
10070 because that is all that redeclare_class_template will look
10071 at. */
10072 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10073 > TMPL_ARGS_DEPTH (args))
10075 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10076 args, tf_warning_or_error);
10077 location_t saved_input_location = input_location;
10078 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10079 tree cons = get_constraints (tmpl);
10080 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10081 input_location = saved_input_location;
10084 else
10086 /* The friend template has not already been declared. In this
10087 case, the instantiation of the template class will cause the
10088 injection of this template into the namespace scope. */
10089 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10091 if (tmpl != error_mark_node)
10093 /* The new TMPL is not an instantiation of anything, so we
10094 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10095 for the new type because that is supposed to be the
10096 corresponding template decl, i.e., TMPL. */
10097 DECL_USE_TEMPLATE (tmpl) = 0;
10098 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10099 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10100 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10101 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10103 /* It is hidden. */
10104 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10105 DECL_ANTICIPATED (tmpl)
10106 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10108 /* Inject this template into the enclosing namspace scope. */
10109 tmpl = pushdecl_namespace_level (tmpl, true);
10113 if (TREE_CODE (context) == NAMESPACE_DECL)
10114 pop_nested_namespace (context);
10115 else
10116 pop_nested_class ();
10118 return TREE_TYPE (tmpl);
10121 /* Returns zero if TYPE cannot be completed later due to circularity.
10122 Otherwise returns one. */
10124 static int
10125 can_complete_type_without_circularity (tree type)
10127 if (type == NULL_TREE || type == error_mark_node)
10128 return 0;
10129 else if (COMPLETE_TYPE_P (type))
10130 return 1;
10131 else if (TREE_CODE (type) == ARRAY_TYPE)
10132 return can_complete_type_without_circularity (TREE_TYPE (type));
10133 else if (CLASS_TYPE_P (type)
10134 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10135 return 0;
10136 else
10137 return 1;
10140 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10141 tsubst_flags_t, tree);
10143 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10144 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10146 static tree
10147 tsubst_attribute (tree t, tree *decl_p, tree args,
10148 tsubst_flags_t complain, tree in_decl)
10150 gcc_assert (ATTR_IS_DEPENDENT (t));
10152 tree val = TREE_VALUE (t);
10153 if (val == NULL_TREE)
10154 /* Nothing to do. */;
10155 else if ((flag_openmp || flag_openmp_simd)
10156 && is_attribute_p ("omp declare simd",
10157 get_attribute_name (t)))
10159 tree clauses = TREE_VALUE (val);
10160 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10161 complain, in_decl);
10162 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10163 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10164 tree parms = DECL_ARGUMENTS (*decl_p);
10165 clauses
10166 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10167 if (clauses)
10168 val = build_tree_list (NULL_TREE, clauses);
10169 else
10170 val = NULL_TREE;
10172 /* If the first attribute argument is an identifier, don't
10173 pass it through tsubst. Attributes like mode, format,
10174 cleanup and several target specific attributes expect it
10175 unmodified. */
10176 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10178 tree chain
10179 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10180 /*integral_constant_expression_p=*/false);
10181 if (chain != TREE_CHAIN (val))
10182 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10184 else if (PACK_EXPANSION_P (val))
10186 /* An attribute pack expansion. */
10187 tree purp = TREE_PURPOSE (t);
10188 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10189 if (pack == error_mark_node)
10190 return error_mark_node;
10191 int len = TREE_VEC_LENGTH (pack);
10192 tree list = NULL_TREE;
10193 tree *q = &list;
10194 for (int i = 0; i < len; ++i)
10196 tree elt = TREE_VEC_ELT (pack, i);
10197 *q = build_tree_list (purp, elt);
10198 q = &TREE_CHAIN (*q);
10200 return list;
10202 else
10203 val = tsubst_expr (val, args, complain, in_decl,
10204 /*integral_constant_expression_p=*/false);
10206 if (val != TREE_VALUE (t))
10207 return build_tree_list (TREE_PURPOSE (t), val);
10208 return t;
10211 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10212 unchanged or a new TREE_LIST chain. */
10214 static tree
10215 tsubst_attributes (tree attributes, tree args,
10216 tsubst_flags_t complain, tree in_decl)
10218 tree last_dep = NULL_TREE;
10220 for (tree t = attributes; t; t = TREE_CHAIN (t))
10221 if (ATTR_IS_DEPENDENT (t))
10223 last_dep = t;
10224 attributes = copy_list (attributes);
10225 break;
10228 if (last_dep)
10229 for (tree *p = &attributes; *p; )
10231 tree t = *p;
10232 if (ATTR_IS_DEPENDENT (t))
10234 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10235 if (subst != t)
10237 *p = subst;
10239 p = &TREE_CHAIN (*p);
10240 while (*p);
10241 *p = TREE_CHAIN (t);
10242 continue;
10245 p = &TREE_CHAIN (*p);
10248 return attributes;
10251 /* Apply any attributes which had to be deferred until instantiation
10252 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10253 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10255 static void
10256 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10257 tree args, tsubst_flags_t complain, tree in_decl)
10259 tree last_dep = NULL_TREE;
10260 tree t;
10261 tree *p;
10263 if (attributes == NULL_TREE)
10264 return;
10266 if (DECL_P (*decl_p))
10268 if (TREE_TYPE (*decl_p) == error_mark_node)
10269 return;
10270 p = &DECL_ATTRIBUTES (*decl_p);
10271 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10272 to our attributes parameter. */
10273 gcc_assert (*p == attributes);
10275 else
10277 p = &TYPE_ATTRIBUTES (*decl_p);
10278 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10279 lookup_template_class_1, and should be preserved. */
10280 gcc_assert (*p != attributes);
10281 while (*p)
10282 p = &TREE_CHAIN (*p);
10285 for (t = attributes; t; t = TREE_CHAIN (t))
10286 if (ATTR_IS_DEPENDENT (t))
10288 last_dep = t;
10289 attributes = copy_list (attributes);
10290 break;
10293 *p = attributes;
10294 if (last_dep)
10296 tree late_attrs = NULL_TREE;
10297 tree *q = &late_attrs;
10299 for (; *p; )
10301 t = *p;
10302 if (ATTR_IS_DEPENDENT (t))
10304 *p = TREE_CHAIN (t);
10305 TREE_CHAIN (t) = NULL_TREE;
10306 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10308 q = &TREE_CHAIN (*q);
10309 while (*q);
10311 else
10312 p = &TREE_CHAIN (t);
10315 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10319 /* Perform (or defer) access check for typedefs that were referenced
10320 from within the template TMPL code.
10321 This is a subroutine of instantiate_decl and instantiate_class_template.
10322 TMPL is the template to consider and TARGS is the list of arguments of
10323 that template. */
10325 static void
10326 perform_typedefs_access_check (tree tmpl, tree targs)
10328 location_t saved_location;
10329 unsigned i;
10330 qualified_typedef_usage_t *iter;
10332 if (!tmpl
10333 || (!CLASS_TYPE_P (tmpl)
10334 && TREE_CODE (tmpl) != FUNCTION_DECL))
10335 return;
10337 saved_location = input_location;
10338 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10340 tree type_decl = iter->typedef_decl;
10341 tree type_scope = iter->context;
10343 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10344 continue;
10346 if (uses_template_parms (type_decl))
10347 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10348 if (uses_template_parms (type_scope))
10349 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10351 /* Make access check error messages point to the location
10352 of the use of the typedef. */
10353 input_location = iter->locus;
10354 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10355 type_decl, type_decl,
10356 tf_warning_or_error);
10358 input_location = saved_location;
10361 static tree
10362 instantiate_class_template_1 (tree type)
10364 tree templ, args, pattern, t, member;
10365 tree typedecl;
10366 tree pbinfo;
10367 tree base_list;
10368 unsigned int saved_maximum_field_alignment;
10369 tree fn_context;
10371 if (type == error_mark_node)
10372 return error_mark_node;
10374 if (COMPLETE_OR_OPEN_TYPE_P (type)
10375 || uses_template_parms (type))
10376 return type;
10378 /* Figure out which template is being instantiated. */
10379 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10380 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10382 /* Mark the type as in the process of being defined. */
10383 TYPE_BEING_DEFINED (type) = 1;
10385 /* Determine what specialization of the original template to
10386 instantiate. */
10387 t = most_specialized_partial_spec (type, tf_warning_or_error);
10388 if (t == error_mark_node)
10389 return error_mark_node;
10390 else if (t)
10392 /* This TYPE is actually an instantiation of a partial
10393 specialization. We replace the innermost set of ARGS with
10394 the arguments appropriate for substitution. For example,
10395 given:
10397 template <class T> struct S {};
10398 template <class T> struct S<T*> {};
10400 and supposing that we are instantiating S<int*>, ARGS will
10401 presently be {int*} -- but we need {int}. */
10402 pattern = TREE_TYPE (t);
10403 args = TREE_PURPOSE (t);
10405 else
10407 pattern = TREE_TYPE (templ);
10408 args = CLASSTYPE_TI_ARGS (type);
10411 /* If the template we're instantiating is incomplete, then clearly
10412 there's nothing we can do. */
10413 if (!COMPLETE_TYPE_P (pattern))
10415 /* We can try again later. */
10416 TYPE_BEING_DEFINED (type) = 0;
10417 return type;
10420 /* If we've recursively instantiated too many templates, stop. */
10421 if (! push_tinst_level (type))
10422 return type;
10424 /* We may be in the middle of deferred access check. Disable
10425 it now. */
10426 push_deferring_access_checks (dk_no_deferred);
10428 int saved_unevaluated_operand = cp_unevaluated_operand;
10429 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10431 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10432 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10433 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10434 fn_context = error_mark_node;
10435 if (!fn_context)
10436 push_to_top_level ();
10437 else
10439 cp_unevaluated_operand = 0;
10440 c_inhibit_evaluation_warnings = 0;
10442 /* Use #pragma pack from the template context. */
10443 saved_maximum_field_alignment = maximum_field_alignment;
10444 maximum_field_alignment = TYPE_PRECISION (pattern);
10446 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10448 /* Set the input location to the most specialized template definition.
10449 This is needed if tsubsting causes an error. */
10450 typedecl = TYPE_MAIN_DECL (pattern);
10451 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10452 DECL_SOURCE_LOCATION (typedecl);
10454 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10455 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10456 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10457 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10458 if (ANON_AGGR_TYPE_P (pattern))
10459 SET_ANON_AGGR_TYPE_P (type);
10460 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10462 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10463 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10464 /* Adjust visibility for template arguments. */
10465 determine_visibility (TYPE_MAIN_DECL (type));
10467 if (CLASS_TYPE_P (type))
10468 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10470 pbinfo = TYPE_BINFO (pattern);
10472 /* We should never instantiate a nested class before its enclosing
10473 class; we need to look up the nested class by name before we can
10474 instantiate it, and that lookup should instantiate the enclosing
10475 class. */
10476 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10477 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10479 base_list = NULL_TREE;
10480 if (BINFO_N_BASE_BINFOS (pbinfo))
10482 tree pbase_binfo;
10483 tree pushed_scope;
10484 int i;
10486 /* We must enter the scope containing the type, as that is where
10487 the accessibility of types named in dependent bases are
10488 looked up from. */
10489 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10491 /* Substitute into each of the bases to determine the actual
10492 basetypes. */
10493 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10495 tree base;
10496 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10497 tree expanded_bases = NULL_TREE;
10498 int idx, len = 1;
10500 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10502 expanded_bases =
10503 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10504 args, tf_error, NULL_TREE);
10505 if (expanded_bases == error_mark_node)
10506 continue;
10508 len = TREE_VEC_LENGTH (expanded_bases);
10511 for (idx = 0; idx < len; idx++)
10513 if (expanded_bases)
10514 /* Extract the already-expanded base class. */
10515 base = TREE_VEC_ELT (expanded_bases, idx);
10516 else
10517 /* Substitute to figure out the base class. */
10518 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10519 NULL_TREE);
10521 if (base == error_mark_node)
10522 continue;
10524 base_list = tree_cons (access, base, base_list);
10525 if (BINFO_VIRTUAL_P (pbase_binfo))
10526 TREE_TYPE (base_list) = integer_type_node;
10530 /* The list is now in reverse order; correct that. */
10531 base_list = nreverse (base_list);
10533 if (pushed_scope)
10534 pop_scope (pushed_scope);
10536 /* Now call xref_basetypes to set up all the base-class
10537 information. */
10538 xref_basetypes (type, base_list);
10540 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10541 (int) ATTR_FLAG_TYPE_IN_PLACE,
10542 args, tf_error, NULL_TREE);
10543 fixup_attribute_variants (type);
10545 /* Now that our base classes are set up, enter the scope of the
10546 class, so that name lookups into base classes, etc. will work
10547 correctly. This is precisely analogous to what we do in
10548 begin_class_definition when defining an ordinary non-template
10549 class, except we also need to push the enclosing classes. */
10550 push_nested_class (type);
10552 /* Now members are processed in the order of declaration. */
10553 for (member = CLASSTYPE_DECL_LIST (pattern);
10554 member; member = TREE_CHAIN (member))
10556 tree t = TREE_VALUE (member);
10558 if (TREE_PURPOSE (member))
10560 if (TYPE_P (t))
10562 if (LAMBDA_TYPE_P (t))
10563 /* A closure type for a lambda in an NSDMI or default argument.
10564 Ignore it; it will be regenerated when needed. */
10565 continue;
10567 /* Build new CLASSTYPE_NESTED_UTDS. */
10569 tree newtag;
10570 bool class_template_p;
10572 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10573 && TYPE_LANG_SPECIFIC (t)
10574 && CLASSTYPE_IS_TEMPLATE (t));
10575 /* If the member is a class template, then -- even after
10576 substitution -- there may be dependent types in the
10577 template argument list for the class. We increment
10578 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10579 that function will assume that no types are dependent
10580 when outside of a template. */
10581 if (class_template_p)
10582 ++processing_template_decl;
10583 newtag = tsubst (t, args, tf_error, NULL_TREE);
10584 if (class_template_p)
10585 --processing_template_decl;
10586 if (newtag == error_mark_node)
10587 continue;
10589 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10591 tree name = TYPE_IDENTIFIER (t);
10593 if (class_template_p)
10594 /* Unfortunately, lookup_template_class sets
10595 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10596 instantiation (i.e., for the type of a member
10597 template class nested within a template class.)
10598 This behavior is required for
10599 maybe_process_partial_specialization to work
10600 correctly, but is not accurate in this case;
10601 the TAG is not an instantiation of anything.
10602 (The corresponding TEMPLATE_DECL is an
10603 instantiation, but the TYPE is not.) */
10604 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10606 /* Now, we call pushtag to put this NEWTAG into the scope of
10607 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10608 pushtag calling push_template_decl. We don't have to do
10609 this for enums because it will already have been done in
10610 tsubst_enum. */
10611 if (name)
10612 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10613 pushtag (name, newtag, /*tag_scope=*/ts_current);
10616 else if (DECL_DECLARES_FUNCTION_P (t))
10618 tree r;
10620 if (TREE_CODE (t) == TEMPLATE_DECL)
10621 ++processing_template_decl;
10622 r = tsubst (t, args, tf_error, NULL_TREE);
10623 if (TREE_CODE (t) == TEMPLATE_DECL)
10624 --processing_template_decl;
10625 set_current_access_from_decl (r);
10626 finish_member_declaration (r);
10627 /* Instantiate members marked with attribute used. */
10628 if (r != error_mark_node && DECL_PRESERVE_P (r))
10629 mark_used (r);
10630 if (TREE_CODE (r) == FUNCTION_DECL
10631 && DECL_OMP_DECLARE_REDUCTION_P (r))
10632 cp_check_omp_declare_reduction (r);
10634 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10635 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10636 /* A closure type for a lambda in an NSDMI or default argument.
10637 Ignore it; it will be regenerated when needed. */;
10638 else
10640 /* Build new TYPE_FIELDS. */
10641 if (TREE_CODE (t) == STATIC_ASSERT)
10643 tree condition;
10645 ++c_inhibit_evaluation_warnings;
10646 condition =
10647 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10648 tf_warning_or_error, NULL_TREE,
10649 /*integral_constant_expression_p=*/true);
10650 --c_inhibit_evaluation_warnings;
10652 finish_static_assert (condition,
10653 STATIC_ASSERT_MESSAGE (t),
10654 STATIC_ASSERT_SOURCE_LOCATION (t),
10655 /*member_p=*/true);
10657 else if (TREE_CODE (t) != CONST_DECL)
10659 tree r;
10660 tree vec = NULL_TREE;
10661 int len = 1;
10663 /* The file and line for this declaration, to
10664 assist in error message reporting. Since we
10665 called push_tinst_level above, we don't need to
10666 restore these. */
10667 input_location = DECL_SOURCE_LOCATION (t);
10669 if (TREE_CODE (t) == TEMPLATE_DECL)
10670 ++processing_template_decl;
10671 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10672 if (TREE_CODE (t) == TEMPLATE_DECL)
10673 --processing_template_decl;
10675 if (TREE_CODE (r) == TREE_VEC)
10677 /* A capture pack became multiple fields. */
10678 vec = r;
10679 len = TREE_VEC_LENGTH (vec);
10682 for (int i = 0; i < len; ++i)
10684 if (vec)
10685 r = TREE_VEC_ELT (vec, i);
10686 if (VAR_P (r))
10688 /* In [temp.inst]:
10690 [t]he initialization (and any associated
10691 side-effects) of a static data member does
10692 not occur unless the static data member is
10693 itself used in a way that requires the
10694 definition of the static data member to
10695 exist.
10697 Therefore, we do not substitute into the
10698 initialized for the static data member here. */
10699 finish_static_data_member_decl
10701 /*init=*/NULL_TREE,
10702 /*init_const_expr_p=*/false,
10703 /*asmspec_tree=*/NULL_TREE,
10704 /*flags=*/0);
10705 /* Instantiate members marked with attribute used. */
10706 if (r != error_mark_node && DECL_PRESERVE_P (r))
10707 mark_used (r);
10709 else if (TREE_CODE (r) == FIELD_DECL)
10711 /* Determine whether R has a valid type and can be
10712 completed later. If R is invalid, then its type
10713 is replaced by error_mark_node. */
10714 tree rtype = TREE_TYPE (r);
10715 if (can_complete_type_without_circularity (rtype))
10716 complete_type (rtype);
10718 if (!complete_or_array_type_p (rtype))
10720 /* If R's type couldn't be completed and
10721 it isn't a flexible array member (whose
10722 type is incomplete by definition) give
10723 an error. */
10724 cxx_incomplete_type_error (r, rtype);
10725 TREE_TYPE (r) = error_mark_node;
10729 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10730 such a thing will already have been added to the field
10731 list by tsubst_enum in finish_member_declaration in the
10732 CLASSTYPE_NESTED_UTDS case above. */
10733 if (!(TREE_CODE (r) == TYPE_DECL
10734 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10735 && DECL_ARTIFICIAL (r)))
10737 set_current_access_from_decl (r);
10738 finish_member_declaration (r);
10744 else
10746 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10747 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10749 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10751 tree friend_type = t;
10752 bool adjust_processing_template_decl = false;
10754 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10756 /* template <class T> friend class C; */
10757 friend_type = tsubst_friend_class (friend_type, args);
10758 adjust_processing_template_decl = true;
10760 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10762 /* template <class T> friend class C::D; */
10763 friend_type = tsubst (friend_type, args,
10764 tf_warning_or_error, NULL_TREE);
10765 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10766 friend_type = TREE_TYPE (friend_type);
10767 adjust_processing_template_decl = true;
10769 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10770 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10772 /* This could be either
10774 friend class T::C;
10776 when dependent_type_p is false or
10778 template <class U> friend class T::C;
10780 otherwise. */
10781 /* Bump processing_template_decl in case this is something like
10782 template <class T> friend struct A<T>::B. */
10783 ++processing_template_decl;
10784 friend_type = tsubst (friend_type, args,
10785 tf_warning_or_error, NULL_TREE);
10786 if (dependent_type_p (friend_type))
10787 adjust_processing_template_decl = true;
10788 --processing_template_decl;
10790 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10791 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10792 && TYPE_HIDDEN_P (friend_type))
10794 /* friend class C;
10796 where C hasn't been declared yet. Let's lookup name
10797 from namespace scope directly, bypassing any name that
10798 come from dependent base class. */
10799 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10801 /* The call to xref_tag_from_type does injection for friend
10802 classes. */
10803 push_nested_namespace (ns);
10804 friend_type =
10805 xref_tag_from_type (friend_type, NULL_TREE,
10806 /*tag_scope=*/ts_current);
10807 pop_nested_namespace (ns);
10809 else if (uses_template_parms (friend_type))
10810 /* friend class C<T>; */
10811 friend_type = tsubst (friend_type, args,
10812 tf_warning_or_error, NULL_TREE);
10813 /* Otherwise it's
10815 friend class C;
10817 where C is already declared or
10819 friend class C<int>;
10821 We don't have to do anything in these cases. */
10823 if (adjust_processing_template_decl)
10824 /* Trick make_friend_class into realizing that the friend
10825 we're adding is a template, not an ordinary class. It's
10826 important that we use make_friend_class since it will
10827 perform some error-checking and output cross-reference
10828 information. */
10829 ++processing_template_decl;
10831 if (friend_type != error_mark_node)
10832 make_friend_class (type, friend_type, /*complain=*/false);
10834 if (adjust_processing_template_decl)
10835 --processing_template_decl;
10837 else
10839 /* Build new DECL_FRIENDLIST. */
10840 tree r;
10842 /* The file and line for this declaration, to
10843 assist in error message reporting. Since we
10844 called push_tinst_level above, we don't need to
10845 restore these. */
10846 input_location = DECL_SOURCE_LOCATION (t);
10848 if (TREE_CODE (t) == TEMPLATE_DECL)
10850 ++processing_template_decl;
10851 push_deferring_access_checks (dk_no_check);
10854 r = tsubst_friend_function (t, args);
10855 add_friend (type, r, /*complain=*/false);
10856 if (TREE_CODE (t) == TEMPLATE_DECL)
10858 pop_deferring_access_checks ();
10859 --processing_template_decl;
10865 if (fn_context)
10867 /* Restore these before substituting into the lambda capture
10868 initializers. */
10869 cp_unevaluated_operand = saved_unevaluated_operand;
10870 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
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 || VAR_P (parm)
11125 || TREE_CODE (parm) == FIELD_DECL)
11127 if (index == 0)
11129 aps = make_argument_pack_select (arg_pack, index);
11130 if (!mark_used (parm, complain) && !(complain & tf_error))
11131 return error_mark_node;
11132 register_local_specialization (aps, parm);
11134 else
11135 aps = retrieve_local_specialization (parm);
11137 else
11139 int idx, level;
11140 template_parm_level_and_index (parm, &level, &idx);
11142 if (index == 0)
11144 aps = make_argument_pack_select (arg_pack, index);
11145 /* Update the corresponding argument. */
11146 TMPL_ARG (args, level, idx) = aps;
11148 else
11149 /* Re-use the ARGUMENT_PACK_SELECT. */
11150 aps = TMPL_ARG (args, level, idx);
11152 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11155 /* Substitute into the PATTERN with the (possibly altered)
11156 arguments. */
11157 if (pattern == in_decl)
11158 /* Expanding a fixed parameter pack from
11159 coerce_template_parameter_pack. */
11160 t = tsubst_decl (pattern, args, complain);
11161 else if (pattern == error_mark_node)
11162 t = error_mark_node;
11163 else if (constraint_p (pattern))
11165 if (processing_template_decl)
11166 t = tsubst_constraint (pattern, args, complain, in_decl);
11167 else
11168 t = (constraints_satisfied_p (pattern, args)
11169 ? boolean_true_node : boolean_false_node);
11171 else if (!TYPE_P (pattern))
11172 t = tsubst_expr (pattern, args, complain, in_decl,
11173 /*integral_constant_expression_p=*/false);
11174 else
11175 t = tsubst (pattern, args, complain, in_decl);
11177 /* If the Ith argument pack element is a pack expansion, then
11178 the Ith element resulting from the substituting is going to
11179 be a pack expansion as well. */
11180 if (ith_elem_is_expansion)
11181 t = make_pack_expansion (t, complain);
11183 return t;
11186 /* When the unexpanded parameter pack in a fold expression expands to an empty
11187 sequence, the value of the expression is as follows; the program is
11188 ill-formed if the operator is not listed in this table.
11190 && true
11191 || false
11192 , void() */
11194 tree
11195 expand_empty_fold (tree t, tsubst_flags_t complain)
11197 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11198 if (!FOLD_EXPR_MODIFY_P (t))
11199 switch (code)
11201 case TRUTH_ANDIF_EXPR:
11202 return boolean_true_node;
11203 case TRUTH_ORIF_EXPR:
11204 return boolean_false_node;
11205 case COMPOUND_EXPR:
11206 return void_node;
11207 default:
11208 break;
11211 if (complain & tf_error)
11212 error_at (location_of (t),
11213 "fold of empty expansion over %O", code);
11214 return error_mark_node;
11217 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11218 form an expression that combines the two terms using the
11219 operator of T. */
11221 static tree
11222 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11224 tree op = FOLD_EXPR_OP (t);
11225 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11227 // Handle compound assignment operators.
11228 if (FOLD_EXPR_MODIFY_P (t))
11229 return build_x_modify_expr (input_location, left, code, right, complain);
11231 switch (code)
11233 case COMPOUND_EXPR:
11234 return build_x_compound_expr (input_location, left, right, complain);
11235 case DOTSTAR_EXPR:
11236 return build_m_component_ref (left, right, complain);
11237 default:
11238 return build_x_binary_op (input_location, code,
11239 left, TREE_CODE (left),
11240 right, TREE_CODE (right),
11241 /*overload=*/NULL,
11242 complain);
11246 /* Substitute ARGS into the pack of a fold expression T. */
11248 static inline tree
11249 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11251 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11254 /* Substitute ARGS into the pack of a fold expression T. */
11256 static inline tree
11257 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11259 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11262 /* Expand a PACK of arguments into a grouped as left fold.
11263 Given a pack containing elements A0, A1, ..., An and an
11264 operator @, this builds the expression:
11266 ((A0 @ A1) @ A2) ... @ An
11268 Note that PACK must not be empty.
11270 The operator is defined by the original fold expression T. */
11272 static tree
11273 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11275 tree left = TREE_VEC_ELT (pack, 0);
11276 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11278 tree right = TREE_VEC_ELT (pack, i);
11279 left = fold_expression (t, left, right, complain);
11281 return left;
11284 /* Substitute into a unary left fold expression. */
11286 static tree
11287 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11288 tree in_decl)
11290 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11291 if (pack == error_mark_node)
11292 return error_mark_node;
11293 if (PACK_EXPANSION_P (pack))
11295 tree r = copy_node (t);
11296 FOLD_EXPR_PACK (r) = pack;
11297 return r;
11299 if (TREE_VEC_LENGTH (pack) == 0)
11300 return expand_empty_fold (t, complain);
11301 else
11302 return expand_left_fold (t, pack, complain);
11305 /* Substitute into a binary left fold expression.
11307 Do ths by building a single (non-empty) vector of argumnts and
11308 building the expression from those elements. */
11310 static tree
11311 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11312 tree in_decl)
11314 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11315 if (pack == error_mark_node)
11316 return error_mark_node;
11317 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11318 if (init == error_mark_node)
11319 return error_mark_node;
11321 if (PACK_EXPANSION_P (pack))
11323 tree r = copy_node (t);
11324 FOLD_EXPR_PACK (r) = pack;
11325 FOLD_EXPR_INIT (r) = init;
11326 return r;
11329 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11330 TREE_VEC_ELT (vec, 0) = init;
11331 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11332 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11334 return expand_left_fold (t, vec, complain);
11337 /* Expand a PACK of arguments into a grouped as right fold.
11338 Given a pack containing elementns A0, A1, ..., and an
11339 operator @, this builds the expression:
11341 A0@ ... (An-2 @ (An-1 @ An))
11343 Note that PACK must not be empty.
11345 The operator is defined by the original fold expression T. */
11347 tree
11348 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11350 // Build the expression.
11351 int n = TREE_VEC_LENGTH (pack);
11352 tree right = TREE_VEC_ELT (pack, n - 1);
11353 for (--n; n != 0; --n)
11355 tree left = TREE_VEC_ELT (pack, n - 1);
11356 right = fold_expression (t, left, right, complain);
11358 return right;
11361 /* Substitute into a unary right fold expression. */
11363 static tree
11364 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11365 tree in_decl)
11367 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11368 if (pack == error_mark_node)
11369 return error_mark_node;
11370 if (PACK_EXPANSION_P (pack))
11372 tree r = copy_node (t);
11373 FOLD_EXPR_PACK (r) = pack;
11374 return r;
11376 if (TREE_VEC_LENGTH (pack) == 0)
11377 return expand_empty_fold (t, complain);
11378 else
11379 return expand_right_fold (t, pack, complain);
11382 /* Substitute into a binary right fold expression.
11384 Do ths by building a single (non-empty) vector of arguments and
11385 building the expression from those elements. */
11387 static tree
11388 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11389 tree in_decl)
11391 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11392 if (pack == error_mark_node)
11393 return error_mark_node;
11394 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11395 if (init == error_mark_node)
11396 return error_mark_node;
11398 if (PACK_EXPANSION_P (pack))
11400 tree r = copy_node (t);
11401 FOLD_EXPR_PACK (r) = pack;
11402 FOLD_EXPR_INIT (r) = init;
11403 return r;
11406 int n = TREE_VEC_LENGTH (pack);
11407 tree vec = make_tree_vec (n + 1);
11408 for (int i = 0; i < n; ++i)
11409 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11410 TREE_VEC_ELT (vec, n) = init;
11412 return expand_right_fold (t, vec, complain);
11415 /* Walk through the pattern of a pack expansion, adding everything in
11416 local_specializations to a list. */
11418 struct el_data
11420 tree extra;
11421 tsubst_flags_t complain;
11423 static tree
11424 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11426 el_data &data = *reinterpret_cast<el_data*>(data_);
11427 tree *extra = &data.extra;
11428 tsubst_flags_t complain = data.complain;
11429 if (tree spec = retrieve_local_specialization (*tp))
11431 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11433 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11434 tree args = ARGUMENT_PACK_ARGS (spec);
11435 if (TREE_VEC_LENGTH (args) == 1)
11437 tree elt = TREE_VEC_ELT (args, 0);
11438 if (PACK_EXPANSION_P (elt))
11439 elt = PACK_EXPANSION_PATTERN (elt);
11440 if (DECL_PACK_P (elt))
11441 spec = elt;
11443 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11445 /* Handle lambda capture here, since we aren't doing any
11446 substitution now, and so tsubst_copy won't call
11447 process_outer_var_ref. */
11448 tree args = ARGUMENT_PACK_ARGS (spec);
11449 int len = TREE_VEC_LENGTH (args);
11450 for (int i = 0; i < len; ++i)
11452 tree arg = TREE_VEC_ELT (args, i);
11453 tree carg = arg;
11454 if (outer_automatic_var_p (arg))
11455 carg = process_outer_var_ref (arg, complain);
11456 if (carg != arg)
11458 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11459 proxies. */
11460 if (i == 0)
11462 spec = copy_node (spec);
11463 args = copy_node (args);
11464 SET_ARGUMENT_PACK_ARGS (spec, args);
11465 register_local_specialization (spec, *tp);
11467 TREE_VEC_ELT (args, i) = carg;
11472 if (outer_automatic_var_p (spec))
11473 spec = process_outer_var_ref (spec, complain);
11474 *extra = tree_cons (*tp, spec, *extra);
11476 return NULL_TREE;
11478 static tree
11479 extract_local_specs (tree pattern, tsubst_flags_t complain)
11481 el_data data = { NULL_TREE, complain };
11482 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11483 return data.extra;
11486 /* Substitute ARGS into T, which is an pack expansion
11487 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11488 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11489 (if only a partial substitution could be performed) or
11490 ERROR_MARK_NODE if there was an error. */
11491 tree
11492 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11493 tree in_decl)
11495 tree pattern;
11496 tree pack, packs = NULL_TREE;
11497 bool unsubstituted_packs = false;
11498 bool unsubstituted_fn_pack = false;
11499 int i, len = -1;
11500 tree result;
11501 hash_map<tree, tree> *saved_local_specializations = NULL;
11502 bool need_local_specializations = false;
11503 int levels;
11505 gcc_assert (PACK_EXPANSION_P (t));
11506 pattern = PACK_EXPANSION_PATTERN (t);
11508 /* Add in any args remembered from an earlier partial instantiation. */
11509 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11510 if (extra && TREE_CODE (extra) == TREE_LIST)
11512 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11514 /* The partial instantiation involved local declarations collected in
11515 extract_local_specs; map from the general template to our local
11516 context. */
11517 tree gen = TREE_PURPOSE (elt);
11518 tree inst = TREE_VALUE (elt);
11519 if (DECL_PACK_P (inst))
11520 inst = retrieve_local_specialization (inst);
11521 /* else inst is already a full instantiation of the pack. */
11522 register_local_specialization (inst, gen);
11524 gcc_assert (!TREE_PURPOSE (extra));
11525 extra = TREE_VALUE (extra);
11527 args = add_to_template_args (extra, args);
11529 levels = TMPL_ARGS_DEPTH (args);
11531 /* Determine the argument packs that will instantiate the parameter
11532 packs used in the expansion expression. While we're at it,
11533 compute the number of arguments to be expanded and make sure it
11534 is consistent. */
11535 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11536 pack = TREE_CHAIN (pack))
11538 tree parm_pack = TREE_VALUE (pack);
11539 tree arg_pack = NULL_TREE;
11540 tree orig_arg = NULL_TREE;
11541 int level = 0;
11543 if (TREE_CODE (parm_pack) == BASES)
11545 gcc_assert (parm_pack == pattern);
11546 if (BASES_DIRECT (parm_pack))
11547 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11548 args, complain, in_decl, false));
11549 else
11550 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11551 args, complain, in_decl, false));
11553 else if (builtin_pack_call_p (parm_pack))
11555 /* ??? Support use in other patterns. */
11556 gcc_assert (parm_pack == pattern);
11557 return expand_builtin_pack_call (parm_pack, args,
11558 complain, in_decl);
11560 else if (TREE_CODE (parm_pack) == PARM_DECL)
11562 /* We know we have correct local_specializations if this
11563 expansion is at function scope, or if we're dealing with a
11564 local parameter in a requires expression; for the latter,
11565 tsubst_requires_expr set it up appropriately. */
11566 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11567 arg_pack = retrieve_local_specialization (parm_pack);
11568 else
11569 /* We can't rely on local_specializations for a parameter
11570 name used later in a function declaration (such as in a
11571 late-specified return type). Even if it exists, it might
11572 have the wrong value for a recursive call. */
11573 need_local_specializations = true;
11575 if (!arg_pack)
11577 /* This parameter pack was used in an unevaluated context. Just
11578 make a dummy decl, since it's only used for its type. */
11579 arg_pack = tsubst_decl (parm_pack, args, complain);
11580 if (arg_pack && DECL_PACK_P (arg_pack))
11581 /* Partial instantiation of the parm_pack, we can't build
11582 up an argument pack yet. */
11583 arg_pack = NULL_TREE;
11584 else
11585 arg_pack = make_fnparm_pack (arg_pack);
11587 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11588 /* This argument pack isn't fully instantiated yet. We set this
11589 flag rather than clear arg_pack because we do want to do the
11590 optimization below, and we don't want to substitute directly
11591 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11592 where it isn't expected). */
11593 unsubstituted_fn_pack = true;
11595 else if (is_normal_capture_proxy (parm_pack))
11597 arg_pack = retrieve_local_specialization (parm_pack);
11598 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11599 unsubstituted_fn_pack = true;
11601 else
11603 int idx;
11604 template_parm_level_and_index (parm_pack, &level, &idx);
11606 if (level <= levels)
11607 arg_pack = TMPL_ARG (args, level, idx);
11610 orig_arg = arg_pack;
11611 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11612 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11614 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11615 /* This can only happen if we forget to expand an argument
11616 pack somewhere else. Just return an error, silently. */
11618 result = make_tree_vec (1);
11619 TREE_VEC_ELT (result, 0) = error_mark_node;
11620 return result;
11623 if (arg_pack)
11625 int my_len =
11626 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11628 /* Don't bother trying to do a partial substitution with
11629 incomplete packs; we'll try again after deduction. */
11630 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11631 return t;
11633 if (len < 0)
11634 len = my_len;
11635 else if (len != my_len
11636 && !unsubstituted_fn_pack)
11638 if (!(complain & tf_error))
11639 /* Fail quietly. */;
11640 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11641 error ("mismatched argument pack lengths while expanding %qT",
11642 pattern);
11643 else
11644 error ("mismatched argument pack lengths while expanding %qE",
11645 pattern);
11646 return error_mark_node;
11649 /* Keep track of the parameter packs and their corresponding
11650 argument packs. */
11651 packs = tree_cons (parm_pack, arg_pack, packs);
11652 TREE_TYPE (packs) = orig_arg;
11654 else
11656 /* We can't substitute for this parameter pack. We use a flag as
11657 well as the missing_level counter because function parameter
11658 packs don't have a level. */
11659 gcc_assert (processing_template_decl);
11660 unsubstituted_packs = true;
11664 /* If the expansion is just T..., return the matching argument pack, unless
11665 we need to call convert_from_reference on all the elements. This is an
11666 important optimization; see c++/68422. */
11667 if (!unsubstituted_packs
11668 && TREE_PURPOSE (packs) == pattern)
11670 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11672 /* If the argument pack is a single pack expansion, pull it out. */
11673 if (TREE_VEC_LENGTH (args) == 1
11674 && pack_expansion_args_count (args))
11675 return TREE_VEC_ELT (args, 0);
11677 /* Types need no adjustment, nor does sizeof..., and if we still have
11678 some pack expansion args we won't do anything yet. */
11679 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11680 || PACK_EXPANSION_SIZEOF_P (t)
11681 || pack_expansion_args_count (args))
11682 return args;
11683 /* Also optimize expression pack expansions if we can tell that the
11684 elements won't have reference type. */
11685 tree type = TREE_TYPE (pattern);
11686 if (type && TREE_CODE (type) != REFERENCE_TYPE
11687 && !PACK_EXPANSION_P (type)
11688 && !WILDCARD_TYPE_P (type))
11689 return args;
11690 /* Otherwise use the normal path so we get convert_from_reference. */
11693 /* We cannot expand this expansion expression, because we don't have
11694 all of the argument packs we need. */
11695 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11697 /* We got some full packs, but we can't substitute them in until we
11698 have values for all the packs. So remember these until then. */
11700 t = make_pack_expansion (pattern, complain);
11701 tree extra = args;
11702 if (local_specializations)
11703 if (tree locals = extract_local_specs (pattern, complain))
11704 extra = tree_cons (NULL_TREE, extra, locals);
11705 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11706 return t;
11708 else if (unsubstituted_packs)
11710 /* There were no real arguments, we're just replacing a parameter
11711 pack with another version of itself. Substitute into the
11712 pattern and return a PACK_EXPANSION_*. The caller will need to
11713 deal with that. */
11714 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11715 t = tsubst_expr (pattern, args, complain, in_decl,
11716 /*integral_constant_expression_p=*/false);
11717 else
11718 t = tsubst (pattern, args, complain, in_decl);
11719 t = make_pack_expansion (t, complain);
11720 return t;
11723 gcc_assert (len >= 0);
11725 if (need_local_specializations)
11727 /* We're in a late-specified return type, so create our own local
11728 specializations map; the current map is either NULL or (in the
11729 case of recursive unification) might have bindings that we don't
11730 want to use or alter. */
11731 saved_local_specializations = local_specializations;
11732 local_specializations = new hash_map<tree, tree>;
11735 /* For each argument in each argument pack, substitute into the
11736 pattern. */
11737 result = make_tree_vec (len);
11738 tree elem_args = copy_template_args (args);
11739 for (i = 0; i < len; ++i)
11741 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11743 elem_args, complain,
11744 in_decl);
11745 TREE_VEC_ELT (result, i) = t;
11746 if (t == error_mark_node)
11748 result = error_mark_node;
11749 break;
11753 /* Update ARGS to restore the substitution from parameter packs to
11754 their argument packs. */
11755 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11757 tree parm = TREE_PURPOSE (pack);
11759 if (TREE_CODE (parm) == PARM_DECL
11760 || VAR_P (parm)
11761 || TREE_CODE (parm) == FIELD_DECL)
11762 register_local_specialization (TREE_TYPE (pack), parm);
11763 else
11765 int idx, level;
11767 if (TREE_VALUE (pack) == NULL_TREE)
11768 continue;
11770 template_parm_level_and_index (parm, &level, &idx);
11772 /* Update the corresponding argument. */
11773 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11774 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11775 TREE_TYPE (pack);
11776 else
11777 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11781 if (need_local_specializations)
11783 delete local_specializations;
11784 local_specializations = saved_local_specializations;
11787 /* If the dependent pack arguments were such that we end up with only a
11788 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11789 if (len == 1 && TREE_CODE (result) == TREE_VEC
11790 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11791 return TREE_VEC_ELT (result, 0);
11793 return result;
11796 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11797 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11798 parameter packs; all parms generated from a function parameter pack will
11799 have the same DECL_PARM_INDEX. */
11801 tree
11802 get_pattern_parm (tree parm, tree tmpl)
11804 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11805 tree patparm;
11807 if (DECL_ARTIFICIAL (parm))
11809 for (patparm = DECL_ARGUMENTS (pattern);
11810 patparm; patparm = DECL_CHAIN (patparm))
11811 if (DECL_ARTIFICIAL (patparm)
11812 && DECL_NAME (parm) == DECL_NAME (patparm))
11813 break;
11815 else
11817 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11818 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11819 gcc_assert (DECL_PARM_INDEX (patparm)
11820 == DECL_PARM_INDEX (parm));
11823 return patparm;
11826 /* Make an argument pack out of the TREE_VEC VEC. */
11828 static tree
11829 make_argument_pack (tree vec)
11831 tree pack;
11832 tree elt = TREE_VEC_ELT (vec, 0);
11833 if (TYPE_P (elt))
11834 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11835 else
11837 pack = make_node (NONTYPE_ARGUMENT_PACK);
11838 TREE_CONSTANT (pack) = 1;
11840 SET_ARGUMENT_PACK_ARGS (pack, vec);
11841 return pack;
11844 /* Return an exact copy of template args T that can be modified
11845 independently. */
11847 static tree
11848 copy_template_args (tree t)
11850 if (t == error_mark_node)
11851 return t;
11853 int len = TREE_VEC_LENGTH (t);
11854 tree new_vec = make_tree_vec (len);
11856 for (int i = 0; i < len; ++i)
11858 tree elt = TREE_VEC_ELT (t, i);
11859 if (elt && TREE_CODE (elt) == TREE_VEC)
11860 elt = copy_template_args (elt);
11861 TREE_VEC_ELT (new_vec, i) = elt;
11864 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11865 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11867 return new_vec;
11870 /* Substitute ARGS into the vector or list of template arguments T. */
11872 static tree
11873 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11875 tree orig_t = t;
11876 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11877 tree *elts;
11879 if (t == error_mark_node)
11880 return error_mark_node;
11882 len = TREE_VEC_LENGTH (t);
11883 elts = XALLOCAVEC (tree, len);
11885 for (i = 0; i < len; i++)
11887 tree orig_arg = TREE_VEC_ELT (t, i);
11888 tree new_arg;
11890 if (TREE_CODE (orig_arg) == TREE_VEC)
11891 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11892 else if (PACK_EXPANSION_P (orig_arg))
11894 /* Substitute into an expansion expression. */
11895 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11897 if (TREE_CODE (new_arg) == TREE_VEC)
11898 /* Add to the expanded length adjustment the number of
11899 expanded arguments. We subtract one from this
11900 measurement, because the argument pack expression
11901 itself is already counted as 1 in
11902 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11903 the argument pack is empty. */
11904 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11906 else if (ARGUMENT_PACK_P (orig_arg))
11908 /* Substitute into each of the arguments. */
11909 new_arg = TYPE_P (orig_arg)
11910 ? cxx_make_type (TREE_CODE (orig_arg))
11911 : make_node (TREE_CODE (orig_arg));
11913 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11914 args, complain, in_decl);
11915 if (pack_args == error_mark_node)
11916 new_arg = error_mark_node;
11917 else
11918 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11920 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11921 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11923 else
11924 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11926 if (new_arg == error_mark_node)
11927 return error_mark_node;
11929 elts[i] = new_arg;
11930 if (new_arg != orig_arg)
11931 need_new = 1;
11934 if (!need_new)
11935 return t;
11937 /* Make space for the expanded arguments coming from template
11938 argument packs. */
11939 t = make_tree_vec (len + expanded_len_adjust);
11940 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11941 arguments for a member template.
11942 In that case each TREE_VEC in ORIG_T represents a level of template
11943 arguments, and ORIG_T won't carry any non defaulted argument count.
11944 It will rather be the nested TREE_VECs that will carry one.
11945 In other words, ORIG_T carries a non defaulted argument count only
11946 if it doesn't contain any nested TREE_VEC. */
11947 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11949 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11950 count += expanded_len_adjust;
11951 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11953 for (i = 0, out = 0; i < len; i++)
11955 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11956 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11957 && TREE_CODE (elts[i]) == TREE_VEC)
11959 int idx;
11961 /* Now expand the template argument pack "in place". */
11962 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11963 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11965 else
11967 TREE_VEC_ELT (t, out) = elts[i];
11968 out++;
11972 return t;
11975 /* Substitute ARGS into one level PARMS of template parameters. */
11977 static tree
11978 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11980 if (parms == error_mark_node)
11981 return error_mark_node;
11983 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11985 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11987 tree tuple = TREE_VEC_ELT (parms, i);
11989 if (tuple == error_mark_node)
11990 continue;
11992 TREE_VEC_ELT (new_vec, i) =
11993 tsubst_template_parm (tuple, args, complain);
11996 return new_vec;
11999 /* Return the result of substituting ARGS into the template parameters
12000 given by PARMS. If there are m levels of ARGS and m + n levels of
12001 PARMS, then the result will contain n levels of PARMS. For
12002 example, if PARMS is `template <class T> template <class U>
12003 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12004 result will be `template <int*, double, class V>'. */
12006 static tree
12007 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12009 tree r = NULL_TREE;
12010 tree* new_parms;
12012 /* When substituting into a template, we must set
12013 PROCESSING_TEMPLATE_DECL as the template parameters may be
12014 dependent if they are based on one-another, and the dependency
12015 predicates are short-circuit outside of templates. */
12016 ++processing_template_decl;
12018 for (new_parms = &r;
12019 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12020 new_parms = &(TREE_CHAIN (*new_parms)),
12021 parms = TREE_CHAIN (parms))
12023 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12024 args, complain);
12025 *new_parms =
12026 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12027 - TMPL_ARGS_DEPTH (args)),
12028 new_vec, NULL_TREE);
12031 --processing_template_decl;
12033 return r;
12036 /* Return the result of substituting ARGS into one template parameter
12037 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12038 parameter and which TREE_PURPOSE is the default argument of the
12039 template parameter. */
12041 static tree
12042 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12044 tree default_value, parm_decl;
12046 if (args == NULL_TREE
12047 || t == NULL_TREE
12048 || t == error_mark_node)
12049 return t;
12051 gcc_assert (TREE_CODE (t) == TREE_LIST);
12053 default_value = TREE_PURPOSE (t);
12054 parm_decl = TREE_VALUE (t);
12056 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12057 if (TREE_CODE (parm_decl) == PARM_DECL
12058 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12059 parm_decl = error_mark_node;
12060 default_value = tsubst_template_arg (default_value, args,
12061 complain, NULL_TREE);
12063 return build_tree_list (default_value, parm_decl);
12066 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12067 type T. If T is not an aggregate or enumeration type, it is
12068 handled as if by tsubst. IN_DECL is as for tsubst. If
12069 ENTERING_SCOPE is nonzero, T is the context for a template which
12070 we are presently tsubst'ing. Return the substituted value. */
12072 static tree
12073 tsubst_aggr_type (tree t,
12074 tree args,
12075 tsubst_flags_t complain,
12076 tree in_decl,
12077 int entering_scope)
12079 if (t == NULL_TREE)
12080 return NULL_TREE;
12082 switch (TREE_CODE (t))
12084 case RECORD_TYPE:
12085 if (TYPE_PTRMEMFUNC_P (t))
12086 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12088 /* Fall through. */
12089 case ENUMERAL_TYPE:
12090 case UNION_TYPE:
12091 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12093 tree argvec;
12094 tree context;
12095 tree r;
12096 int saved_unevaluated_operand;
12097 int saved_inhibit_evaluation_warnings;
12099 /* In "sizeof(X<I>)" we need to evaluate "I". */
12100 saved_unevaluated_operand = cp_unevaluated_operand;
12101 cp_unevaluated_operand = 0;
12102 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12103 c_inhibit_evaluation_warnings = 0;
12105 /* First, determine the context for the type we are looking
12106 up. */
12107 context = TYPE_CONTEXT (t);
12108 if (context && TYPE_P (context))
12110 context = tsubst_aggr_type (context, args, complain,
12111 in_decl, /*entering_scope=*/1);
12112 /* If context is a nested class inside a class template,
12113 it may still need to be instantiated (c++/33959). */
12114 context = complete_type (context);
12117 /* Then, figure out what arguments are appropriate for the
12118 type we are trying to find. For example, given:
12120 template <class T> struct S;
12121 template <class T, class U> void f(T, U) { S<U> su; }
12123 and supposing that we are instantiating f<int, double>,
12124 then our ARGS will be {int, double}, but, when looking up
12125 S we only want {double}. */
12126 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12127 complain, in_decl);
12128 if (argvec == error_mark_node)
12129 r = error_mark_node;
12130 else
12132 r = lookup_template_class (t, argvec, in_decl, context,
12133 entering_scope, complain);
12134 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12137 cp_unevaluated_operand = saved_unevaluated_operand;
12138 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12140 return r;
12142 else
12143 /* This is not a template type, so there's nothing to do. */
12144 return t;
12146 default:
12147 return tsubst (t, args, complain, in_decl);
12151 static GTY((cache)) tree_cache_map *defarg_inst;
12153 /* Substitute into the default argument ARG (a default argument for
12154 FN), which has the indicated TYPE. */
12156 tree
12157 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12158 tsubst_flags_t complain)
12160 tree saved_class_ptr = NULL_TREE;
12161 tree saved_class_ref = NULL_TREE;
12162 int errs = errorcount + sorrycount;
12164 /* This can happen in invalid code. */
12165 if (TREE_CODE (arg) == DEFAULT_ARG)
12166 return arg;
12168 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12169 parm = chain_index (parmnum, parm);
12170 tree parmtype = TREE_TYPE (parm);
12171 if (DECL_BY_REFERENCE (parm))
12172 parmtype = TREE_TYPE (parmtype);
12173 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12175 tree *slot;
12176 if (defarg_inst && (slot = defarg_inst->get (parm)))
12177 return *slot;
12179 /* This default argument came from a template. Instantiate the
12180 default argument here, not in tsubst. In the case of
12181 something like:
12183 template <class T>
12184 struct S {
12185 static T t();
12186 void f(T = t());
12189 we must be careful to do name lookup in the scope of S<T>,
12190 rather than in the current class. */
12191 push_access_scope (fn);
12192 /* The "this" pointer is not valid in a default argument. */
12193 if (cfun)
12195 saved_class_ptr = current_class_ptr;
12196 cp_function_chain->x_current_class_ptr = NULL_TREE;
12197 saved_class_ref = current_class_ref;
12198 cp_function_chain->x_current_class_ref = NULL_TREE;
12201 start_lambda_scope (parm);
12203 push_deferring_access_checks(dk_no_deferred);
12204 /* The default argument expression may cause implicitly defined
12205 member functions to be synthesized, which will result in garbage
12206 collection. We must treat this situation as if we were within
12207 the body of function so as to avoid collecting live data on the
12208 stack. */
12209 ++function_depth;
12210 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12211 complain, NULL_TREE,
12212 /*integral_constant_expression_p=*/false);
12213 --function_depth;
12214 pop_deferring_access_checks();
12216 finish_lambda_scope ();
12218 /* Restore the "this" pointer. */
12219 if (cfun)
12221 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12222 cp_function_chain->x_current_class_ref = saved_class_ref;
12225 if (errorcount+sorrycount > errs
12226 && (complain & tf_warning_or_error))
12227 inform (input_location,
12228 " when instantiating default argument for call to %qD", fn);
12230 /* Make sure the default argument is reasonable. */
12231 arg = check_default_argument (type, arg, complain);
12233 pop_access_scope (fn);
12235 if (arg != error_mark_node && !cp_unevaluated_operand)
12237 if (!defarg_inst)
12238 defarg_inst = tree_cache_map::create_ggc (37);
12239 defarg_inst->put (parm, arg);
12242 return arg;
12245 /* Substitute into all the default arguments for FN. */
12247 static void
12248 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12250 tree arg;
12251 tree tmpl_args;
12253 tmpl_args = DECL_TI_ARGS (fn);
12255 /* If this function is not yet instantiated, we certainly don't need
12256 its default arguments. */
12257 if (uses_template_parms (tmpl_args))
12258 return;
12259 /* Don't do this again for clones. */
12260 if (DECL_CLONED_FUNCTION_P (fn))
12261 return;
12263 int i = 0;
12264 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12265 arg;
12266 arg = TREE_CHAIN (arg), ++i)
12267 if (TREE_PURPOSE (arg))
12268 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12269 TREE_VALUE (arg),
12270 TREE_PURPOSE (arg),
12271 complain);
12274 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12276 static tree
12277 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12278 tree lambda_fntype)
12280 tree gen_tmpl, argvec;
12281 hashval_t hash = 0;
12282 tree in_decl = t;
12284 /* Nobody should be tsubst'ing into non-template functions. */
12285 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12287 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12289 /* If T is not dependent, just return it. */
12290 if (!uses_template_parms (DECL_TI_ARGS (t)))
12291 return t;
12293 /* Calculate the most general template of which R is a
12294 specialization. */
12295 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12297 /* We're substituting a lambda function under tsubst_lambda_expr but not
12298 directly from it; find the matching function we're already inside.
12299 But don't do this if T is a generic lambda with a single level of
12300 template parms, as in that case we're doing a normal instantiation. */
12301 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12302 && (!generic_lambda_fn_p (t)
12303 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12304 return enclosing_instantiation_of (t);
12306 /* Calculate the complete set of arguments used to
12307 specialize R. */
12308 argvec = tsubst_template_args (DECL_TI_ARGS
12309 (DECL_TEMPLATE_RESULT
12310 (DECL_TI_TEMPLATE (t))),
12311 args, complain, in_decl);
12312 if (argvec == error_mark_node)
12313 return error_mark_node;
12315 /* Check to see if we already have this specialization. */
12316 if (!lambda_fntype)
12318 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12319 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12320 return spec;
12323 /* We can see more levels of arguments than parameters if
12324 there was a specialization of a member template, like
12325 this:
12327 template <class T> struct S { template <class U> void f(); }
12328 template <> template <class U> void S<int>::f(U);
12330 Here, we'll be substituting into the specialization,
12331 because that's where we can find the code we actually
12332 want to generate, but we'll have enough arguments for
12333 the most general template.
12335 We also deal with the peculiar case:
12337 template <class T> struct S {
12338 template <class U> friend void f();
12340 template <class U> void f() {}
12341 template S<int>;
12342 template void f<double>();
12344 Here, the ARGS for the instantiation of will be {int,
12345 double}. But, we only need as many ARGS as there are
12346 levels of template parameters in CODE_PATTERN. We are
12347 careful not to get fooled into reducing the ARGS in
12348 situations like:
12350 template <class T> struct S { template <class U> void f(U); }
12351 template <class T> template <> void S<T>::f(int) {}
12353 which we can spot because the pattern will be a
12354 specialization in this case. */
12355 int args_depth = TMPL_ARGS_DEPTH (args);
12356 int parms_depth =
12357 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12359 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12360 args = get_innermost_template_args (args, parms_depth);
12362 else
12364 /* This special case arises when we have something like this:
12366 template <class T> struct S {
12367 friend void f<int>(int, double);
12370 Here, the DECL_TI_TEMPLATE for the friend declaration
12371 will be an IDENTIFIER_NODE. We are being called from
12372 tsubst_friend_function, and we want only to create a
12373 new decl (R) with appropriate types so that we can call
12374 determine_specialization. */
12375 gen_tmpl = NULL_TREE;
12376 argvec = NULL_TREE;
12379 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12380 : NULL_TREE);
12381 tree ctx = closure ? closure : DECL_CONTEXT (t);
12382 bool member = ctx && TYPE_P (ctx);
12384 if (member && !closure)
12385 ctx = tsubst_aggr_type (ctx, args,
12386 complain, t, /*entering_scope=*/1);
12388 tree type = (lambda_fntype ? lambda_fntype
12389 : tsubst (TREE_TYPE (t), args,
12390 complain | tf_fndecl_type, in_decl));
12391 if (type == error_mark_node)
12392 return error_mark_node;
12394 /* If we hit excessive deduction depth, the type is bogus even if
12395 it isn't error_mark_node, so don't build a decl. */
12396 if (excessive_deduction_depth)
12397 return error_mark_node;
12399 /* We do NOT check for matching decls pushed separately at this
12400 point, as they may not represent instantiations of this
12401 template, and in any case are considered separate under the
12402 discrete model. */
12403 tree r = copy_decl (t);
12404 DECL_USE_TEMPLATE (r) = 0;
12405 TREE_TYPE (r) = type;
12406 /* Clear out the mangled name and RTL for the instantiation. */
12407 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12408 SET_DECL_RTL (r, NULL);
12409 /* Leave DECL_INITIAL set on deleted instantiations. */
12410 if (!DECL_DELETED_FN (r))
12411 DECL_INITIAL (r) = NULL_TREE;
12412 DECL_CONTEXT (r) = ctx;
12414 /* OpenMP UDRs have the only argument a reference to the declared
12415 type. We want to diagnose if the declared type is a reference,
12416 which is invalid, but as references to references are usually
12417 quietly merged, diagnose it here. */
12418 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12420 tree argtype
12421 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12422 argtype = tsubst (argtype, args, complain, in_decl);
12423 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12424 error_at (DECL_SOURCE_LOCATION (t),
12425 "reference type %qT in "
12426 "%<#pragma omp declare reduction%>", argtype);
12427 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12428 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12429 argtype);
12432 if (member && DECL_CONV_FN_P (r))
12433 /* Type-conversion operator. Reconstruct the name, in
12434 case it's the name of one of the template's parameters. */
12435 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12437 tree parms = DECL_ARGUMENTS (t);
12438 if (closure)
12439 parms = DECL_CHAIN (parms);
12440 parms = tsubst (parms, args, complain, t);
12441 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12442 DECL_CONTEXT (parm) = r;
12443 if (closure)
12445 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12446 DECL_CHAIN (tparm) = parms;
12447 parms = tparm;
12449 DECL_ARGUMENTS (r) = parms;
12450 DECL_RESULT (r) = NULL_TREE;
12452 TREE_STATIC (r) = 0;
12453 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12454 DECL_EXTERNAL (r) = 1;
12455 /* If this is an instantiation of a function with internal
12456 linkage, we already know what object file linkage will be
12457 assigned to the instantiation. */
12458 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12459 DECL_DEFER_OUTPUT (r) = 0;
12460 DECL_CHAIN (r) = NULL_TREE;
12461 DECL_PENDING_INLINE_INFO (r) = 0;
12462 DECL_PENDING_INLINE_P (r) = 0;
12463 DECL_SAVED_TREE (r) = NULL_TREE;
12464 DECL_STRUCT_FUNCTION (r) = NULL;
12465 TREE_USED (r) = 0;
12466 /* We'll re-clone as appropriate in instantiate_template. */
12467 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12469 /* If we aren't complaining now, return on error before we register
12470 the specialization so that we'll complain eventually. */
12471 if ((complain & tf_error) == 0
12472 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12473 && !grok_op_properties (r, /*complain=*/false))
12474 return error_mark_node;
12476 /* When instantiating a constrained member, substitute
12477 into the constraints to create a new constraint. */
12478 if (tree ci = get_constraints (t))
12479 if (member)
12481 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12482 set_constraints (r, ci);
12485 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12486 this in the special friend case mentioned above where
12487 GEN_TMPL is NULL. */
12488 if (gen_tmpl && !closure)
12490 DECL_TEMPLATE_INFO (r)
12491 = build_template_info (gen_tmpl, argvec);
12492 SET_DECL_IMPLICIT_INSTANTIATION (r);
12494 tree new_r
12495 = register_specialization (r, gen_tmpl, argvec, false, hash);
12496 if (new_r != r)
12497 /* We instantiated this while substituting into
12498 the type earlier (template/friend54.C). */
12499 return new_r;
12501 /* We're not supposed to instantiate default arguments
12502 until they are called, for a template. But, for a
12503 declaration like:
12505 template <class T> void f ()
12506 { extern void g(int i = T()); }
12508 we should do the substitution when the template is
12509 instantiated. We handle the member function case in
12510 instantiate_class_template since the default arguments
12511 might refer to other members of the class. */
12512 if (!member
12513 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12514 && !uses_template_parms (argvec))
12515 tsubst_default_arguments (r, complain);
12517 else
12518 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12520 /* Copy the list of befriending classes. */
12521 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12522 *friends;
12523 friends = &TREE_CHAIN (*friends))
12525 *friends = copy_node (*friends);
12526 TREE_VALUE (*friends)
12527 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12530 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12532 maybe_retrofit_in_chrg (r);
12533 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12534 return error_mark_node;
12535 /* If this is an instantiation of a member template, clone it.
12536 If it isn't, that'll be handled by
12537 clone_constructors_and_destructors. */
12538 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12539 clone_function_decl (r, /*update_methods=*/false);
12541 else if ((complain & tf_error) != 0
12542 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12543 && !grok_op_properties (r, /*complain=*/true))
12544 return error_mark_node;
12546 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12547 SET_DECL_FRIEND_CONTEXT (r,
12548 tsubst (DECL_FRIEND_CONTEXT (t),
12549 args, complain, in_decl));
12551 /* Possibly limit visibility based on template args. */
12552 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12553 if (DECL_VISIBILITY_SPECIFIED (t))
12555 DECL_VISIBILITY_SPECIFIED (r) = 0;
12556 DECL_ATTRIBUTES (r)
12557 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12559 determine_visibility (r);
12560 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12561 && !processing_template_decl)
12562 defaulted_late_check (r);
12564 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12565 args, complain, in_decl);
12566 return r;
12569 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12571 static tree
12572 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12573 tree lambda_fntype)
12575 /* We can get here when processing a member function template,
12576 member class template, or template template parameter. */
12577 tree decl = DECL_TEMPLATE_RESULT (t);
12578 tree in_decl = t;
12579 tree spec;
12580 tree tmpl_args;
12581 tree full_args;
12582 tree r;
12583 hashval_t hash = 0;
12585 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12587 /* Template template parameter is treated here. */
12588 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12589 if (new_type == error_mark_node)
12590 r = error_mark_node;
12591 /* If we get a real template back, return it. This can happen in
12592 the context of most_specialized_partial_spec. */
12593 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12594 r = new_type;
12595 else
12596 /* The new TEMPLATE_DECL was built in
12597 reduce_template_parm_level. */
12598 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12599 return r;
12602 if (!lambda_fntype)
12604 /* We might already have an instance of this template.
12605 The ARGS are for the surrounding class type, so the
12606 full args contain the tsubst'd args for the context,
12607 plus the innermost args from the template decl. */
12608 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12609 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12610 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12611 /* Because this is a template, the arguments will still be
12612 dependent, even after substitution. If
12613 PROCESSING_TEMPLATE_DECL is not set, the dependency
12614 predicates will short-circuit. */
12615 ++processing_template_decl;
12616 full_args = tsubst_template_args (tmpl_args, args,
12617 complain, in_decl);
12618 --processing_template_decl;
12619 if (full_args == error_mark_node)
12620 return error_mark_node;
12622 /* If this is a default template template argument,
12623 tsubst might not have changed anything. */
12624 if (full_args == tmpl_args)
12625 return t;
12627 hash = hash_tmpl_and_args (t, full_args);
12628 spec = retrieve_specialization (t, full_args, hash);
12629 if (spec != NULL_TREE)
12630 return spec;
12633 /* Make a new template decl. It will be similar to the
12634 original, but will record the current template arguments.
12635 We also create a new function declaration, which is just
12636 like the old one, but points to this new template, rather
12637 than the old one. */
12638 r = copy_decl (t);
12639 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12640 DECL_CHAIN (r) = NULL_TREE;
12642 // Build new template info linking to the original template decl.
12643 if (!lambda_fntype)
12645 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12646 SET_DECL_IMPLICIT_INSTANTIATION (r);
12648 else
12649 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12651 /* The template parameters for this new template are all the
12652 template parameters for the old template, except the
12653 outermost level of parameters. */
12654 DECL_TEMPLATE_PARMS (r)
12655 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12656 complain);
12658 if (TREE_CODE (decl) == TYPE_DECL
12659 && !TYPE_DECL_ALIAS_P (decl))
12661 tree new_type;
12662 ++processing_template_decl;
12663 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12664 --processing_template_decl;
12665 if (new_type == error_mark_node)
12666 return error_mark_node;
12668 TREE_TYPE (r) = new_type;
12669 /* For a partial specialization, we need to keep pointing to
12670 the primary template. */
12671 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12672 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12673 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12674 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12675 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12677 else
12679 tree new_decl;
12680 ++processing_template_decl;
12681 if (TREE_CODE (decl) == FUNCTION_DECL)
12682 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12683 else
12684 new_decl = tsubst (decl, args, complain, in_decl);
12685 --processing_template_decl;
12686 if (new_decl == error_mark_node)
12687 return error_mark_node;
12689 DECL_TEMPLATE_RESULT (r) = new_decl;
12690 TREE_TYPE (r) = TREE_TYPE (new_decl);
12691 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12692 if (lambda_fntype)
12694 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12695 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12697 else
12699 DECL_TI_TEMPLATE (new_decl) = r;
12700 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12704 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12705 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12707 if (PRIMARY_TEMPLATE_P (t))
12708 DECL_PRIMARY_TEMPLATE (r) = r;
12710 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12711 && !lambda_fntype)
12712 /* Record this non-type partial instantiation. */
12713 register_specialization (r, t,
12714 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12715 false, hash);
12717 return r;
12720 /* True if FN is the op() for a lambda in an uninstantiated template. */
12722 bool
12723 lambda_fn_in_template_p (tree fn)
12725 if (!fn || !LAMBDA_FUNCTION_P (fn))
12726 return false;
12727 tree closure = DECL_CONTEXT (fn);
12728 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12731 /* We're instantiating a variable from template function TCTX. Return the
12732 corresponding current enclosing scope. This gets complicated because lambda
12733 functions in templates are regenerated rather than instantiated, but generic
12734 lambda functions are subsequently instantiated. */
12736 static tree
12737 enclosing_instantiation_of (tree otctx)
12739 tree tctx = otctx;
12740 tree fn = current_function_decl;
12741 int lambda_count = 0;
12743 for (; tctx && lambda_fn_in_template_p (tctx);
12744 tctx = decl_function_context (tctx))
12745 ++lambda_count;
12746 for (; fn; fn = decl_function_context (fn))
12748 tree ofn = fn;
12749 int flambda_count = 0;
12750 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12751 fn = decl_function_context (fn))
12752 ++flambda_count;
12753 if (DECL_TEMPLATE_INFO (fn)
12754 ? most_general_template (fn) != most_general_template (tctx)
12755 : fn != tctx)
12756 continue;
12757 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12758 || DECL_CONV_FN_P (ofn));
12759 return ofn;
12761 gcc_unreachable ();
12764 /* Substitute the ARGS into the T, which is a _DECL. Return the
12765 result of the substitution. Issue error and warning messages under
12766 control of COMPLAIN. */
12768 static tree
12769 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12771 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12772 location_t saved_loc;
12773 tree r = NULL_TREE;
12774 tree in_decl = t;
12775 hashval_t hash = 0;
12777 /* Set the filename and linenumber to improve error-reporting. */
12778 saved_loc = input_location;
12779 input_location = DECL_SOURCE_LOCATION (t);
12781 switch (TREE_CODE (t))
12783 case TEMPLATE_DECL:
12784 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12785 break;
12787 case FUNCTION_DECL:
12788 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12789 break;
12791 case PARM_DECL:
12793 tree type = NULL_TREE;
12794 int i, len = 1;
12795 tree expanded_types = NULL_TREE;
12796 tree prev_r = NULL_TREE;
12797 tree first_r = NULL_TREE;
12799 if (DECL_PACK_P (t))
12801 /* If there is a local specialization that isn't a
12802 parameter pack, it means that we're doing a "simple"
12803 substitution from inside tsubst_pack_expansion. Just
12804 return the local specialization (which will be a single
12805 parm). */
12806 tree spec = retrieve_local_specialization (t);
12807 if (spec
12808 && TREE_CODE (spec) == PARM_DECL
12809 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12810 RETURN (spec);
12812 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12813 the parameters in this function parameter pack. */
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);
12820 /* Zero-length parameter packs are boring. Just substitute
12821 into the chain. */
12822 if (len == 0)
12823 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12824 TREE_CHAIN (t)));
12826 else
12828 /* All we did was update the type. Make a note of that. */
12829 type = expanded_types;
12830 expanded_types = NULL_TREE;
12834 /* Loop through all of the parameters we'll build. When T is
12835 a function parameter pack, LEN is the number of expanded
12836 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12837 r = NULL_TREE;
12838 for (i = 0; i < len; ++i)
12840 prev_r = r;
12841 r = copy_node (t);
12842 if (DECL_TEMPLATE_PARM_P (t))
12843 SET_DECL_TEMPLATE_PARM_P (r);
12845 if (expanded_types)
12846 /* We're on the Ith parameter of the function parameter
12847 pack. */
12849 /* Get the Ith type. */
12850 type = TREE_VEC_ELT (expanded_types, i);
12852 /* Rename the parameter to include the index. */
12853 DECL_NAME (r)
12854 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12856 else if (!type)
12857 /* We're dealing with a normal parameter. */
12858 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12860 type = type_decays_to (type);
12861 TREE_TYPE (r) = type;
12862 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12864 if (DECL_INITIAL (r))
12866 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12867 DECL_INITIAL (r) = TREE_TYPE (r);
12868 else
12869 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12870 complain, in_decl);
12873 DECL_CONTEXT (r) = NULL_TREE;
12875 if (!DECL_TEMPLATE_PARM_P (r))
12876 DECL_ARG_TYPE (r) = type_passed_as (type);
12878 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12879 args, complain, in_decl);
12881 /* Keep track of the first new parameter we
12882 generate. That's what will be returned to the
12883 caller. */
12884 if (!first_r)
12885 first_r = r;
12887 /* Build a proper chain of parameters when substituting
12888 into a function parameter pack. */
12889 if (prev_r)
12890 DECL_CHAIN (prev_r) = r;
12893 /* If cp_unevaluated_operand is set, we're just looking for a
12894 single dummy parameter, so don't keep going. */
12895 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12896 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12897 complain, DECL_CHAIN (t));
12899 /* FIRST_R contains the start of the chain we've built. */
12900 r = first_r;
12902 break;
12904 case FIELD_DECL:
12906 tree type = NULL_TREE;
12907 tree vec = NULL_TREE;
12908 tree expanded_types = NULL_TREE;
12909 int len = 1;
12911 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12913 /* This field is a lambda capture pack. Return a TREE_VEC of
12914 the expanded fields to instantiate_class_template_1. */
12915 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12916 complain, in_decl);
12917 if (TREE_CODE (expanded_types) == TREE_VEC)
12919 len = TREE_VEC_LENGTH (expanded_types);
12920 vec = make_tree_vec (len);
12922 else
12924 /* All we did was update the type. Make a note of that. */
12925 type = expanded_types;
12926 expanded_types = NULL_TREE;
12930 for (int i = 0; i < len; ++i)
12932 r = copy_decl (t);
12933 if (expanded_types)
12935 type = TREE_VEC_ELT (expanded_types, i);
12936 DECL_NAME (r)
12937 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12939 else if (!type)
12940 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12942 if (type == error_mark_node)
12943 RETURN (error_mark_node);
12944 TREE_TYPE (r) = type;
12945 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12947 if (DECL_C_BIT_FIELD (r))
12948 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12949 number of bits. */
12950 DECL_BIT_FIELD_REPRESENTATIVE (r)
12951 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12952 complain, in_decl,
12953 /*integral_constant_expression_p=*/true);
12954 if (DECL_INITIAL (t))
12956 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12957 NSDMI in perform_member_init. Still set DECL_INITIAL
12958 so that we know there is one. */
12959 DECL_INITIAL (r) = void_node;
12960 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12961 retrofit_lang_decl (r);
12962 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12964 /* We don't have to set DECL_CONTEXT here; it is set by
12965 finish_member_declaration. */
12966 DECL_CHAIN (r) = NULL_TREE;
12968 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12969 args, complain, in_decl);
12971 if (vec)
12972 TREE_VEC_ELT (vec, i) = r;
12975 if (vec)
12976 r = vec;
12978 break;
12980 case USING_DECL:
12981 /* We reach here only for member using decls. We also need to check
12982 uses_template_parms because DECL_DEPENDENT_P is not set for a
12983 using-declaration that designates a member of the current
12984 instantiation (c++/53549). */
12985 if (DECL_DEPENDENT_P (t)
12986 || uses_template_parms (USING_DECL_SCOPE (t)))
12988 tree scope = USING_DECL_SCOPE (t);
12989 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12990 if (PACK_EXPANSION_P (scope))
12992 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12993 int len = TREE_VEC_LENGTH (vec);
12994 r = make_tree_vec (len);
12995 for (int i = 0; i < len; ++i)
12997 tree escope = TREE_VEC_ELT (vec, i);
12998 tree elt = do_class_using_decl (escope, name);
12999 if (!elt)
13001 r = error_mark_node;
13002 break;
13004 else
13006 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13007 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13009 TREE_VEC_ELT (r, i) = elt;
13012 else
13014 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13015 complain, in_decl);
13016 r = do_class_using_decl (inst_scope, name);
13017 if (!r)
13018 r = error_mark_node;
13019 else
13021 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13022 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13026 else
13028 r = copy_node (t);
13029 DECL_CHAIN (r) = NULL_TREE;
13031 break;
13033 case TYPE_DECL:
13034 case VAR_DECL:
13036 tree argvec = NULL_TREE;
13037 tree gen_tmpl = NULL_TREE;
13038 tree spec;
13039 tree tmpl = NULL_TREE;
13040 tree ctx;
13041 tree type = NULL_TREE;
13042 bool local_p;
13044 if (TREE_TYPE (t) == error_mark_node)
13045 RETURN (error_mark_node);
13047 if (TREE_CODE (t) == TYPE_DECL
13048 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13050 /* If this is the canonical decl, we don't have to
13051 mess with instantiations, and often we can't (for
13052 typename, template type parms and such). Note that
13053 TYPE_NAME is not correct for the above test if
13054 we've copied the type for a typedef. */
13055 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13056 if (type == error_mark_node)
13057 RETURN (error_mark_node);
13058 r = TYPE_NAME (type);
13059 break;
13062 /* Check to see if we already have the specialization we
13063 need. */
13064 spec = NULL_TREE;
13065 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13067 /* T is a static data member or namespace-scope entity.
13068 We have to substitute into namespace-scope variables
13069 (not just variable templates) because of cases like:
13071 template <class T> void f() { extern T t; }
13073 where the entity referenced is not known until
13074 instantiation time. */
13075 local_p = false;
13076 ctx = DECL_CONTEXT (t);
13077 if (DECL_CLASS_SCOPE_P (t))
13079 ctx = tsubst_aggr_type (ctx, args,
13080 complain,
13081 in_decl, /*entering_scope=*/1);
13082 /* If CTX is unchanged, then T is in fact the
13083 specialization we want. That situation occurs when
13084 referencing a static data member within in its own
13085 class. We can use pointer equality, rather than
13086 same_type_p, because DECL_CONTEXT is always
13087 canonical... */
13088 if (ctx == DECL_CONTEXT (t)
13089 /* ... unless T is a member template; in which
13090 case our caller can be willing to create a
13091 specialization of that template represented
13092 by T. */
13093 && !(DECL_TI_TEMPLATE (t)
13094 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13095 spec = t;
13098 if (!spec)
13100 tmpl = DECL_TI_TEMPLATE (t);
13101 gen_tmpl = most_general_template (tmpl);
13102 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13103 if (argvec != error_mark_node)
13104 argvec = (coerce_innermost_template_parms
13105 (DECL_TEMPLATE_PARMS (gen_tmpl),
13106 argvec, t, complain,
13107 /*all*/true, /*defarg*/true));
13108 if (argvec == error_mark_node)
13109 RETURN (error_mark_node);
13110 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13111 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13114 else
13116 /* A local variable. */
13117 local_p = true;
13118 /* Subsequent calls to pushdecl will fill this in. */
13119 ctx = NULL_TREE;
13120 /* Unless this is a reference to a static variable from an
13121 enclosing function, in which case we need to fill it in now. */
13122 if (TREE_STATIC (t))
13124 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13125 if (fn != current_function_decl)
13126 ctx = fn;
13128 spec = retrieve_local_specialization (t);
13130 /* If we already have the specialization we need, there is
13131 nothing more to do. */
13132 if (spec)
13134 r = spec;
13135 break;
13138 /* Create a new node for the specialization we need. */
13139 r = copy_decl (t);
13140 if (type == NULL_TREE)
13142 if (is_typedef_decl (t))
13143 type = DECL_ORIGINAL_TYPE (t);
13144 else
13145 type = TREE_TYPE (t);
13146 if (VAR_P (t)
13147 && VAR_HAD_UNKNOWN_BOUND (t)
13148 && type != error_mark_node)
13149 type = strip_array_domain (type);
13150 tree sub_args = args;
13151 if (tree auto_node = type_uses_auto (type))
13153 /* Mask off any template args past the variable's context so we
13154 don't replace the auto with an unrelated argument. */
13155 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13156 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13157 if (extra > 0)
13158 /* This should never happen with the new lambda instantiation
13159 model, but keep the handling just in case. */
13160 gcc_assert (!CHECKING_P),
13161 sub_args = strip_innermost_template_args (args, extra);
13163 type = tsubst (type, sub_args, complain, in_decl);
13165 if (VAR_P (r))
13167 /* Even if the original location is out of scope, the
13168 newly substituted one is not. */
13169 DECL_DEAD_FOR_LOCAL (r) = 0;
13170 DECL_INITIALIZED_P (r) = 0;
13171 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13172 if (type == error_mark_node)
13173 RETURN (error_mark_node);
13174 if (TREE_CODE (type) == FUNCTION_TYPE)
13176 /* It may seem that this case cannot occur, since:
13178 typedef void f();
13179 void g() { f x; }
13181 declares a function, not a variable. However:
13183 typedef void f();
13184 template <typename T> void g() { T t; }
13185 template void g<f>();
13187 is an attempt to declare a variable with function
13188 type. */
13189 error ("variable %qD has function type",
13190 /* R is not yet sufficiently initialized, so we
13191 just use its name. */
13192 DECL_NAME (r));
13193 RETURN (error_mark_node);
13195 type = complete_type (type);
13196 /* Wait until cp_finish_decl to set this again, to handle
13197 circular dependency (template/instantiate6.C). */
13198 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13199 type = check_var_type (DECL_NAME (r), type);
13201 if (DECL_HAS_VALUE_EXPR_P (t))
13203 tree ve = DECL_VALUE_EXPR (t);
13204 ve = tsubst_expr (ve, args, complain, in_decl,
13205 /*constant_expression_p=*/false);
13206 if (REFERENCE_REF_P (ve))
13208 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13209 ve = TREE_OPERAND (ve, 0);
13211 SET_DECL_VALUE_EXPR (r, ve);
13213 if (CP_DECL_THREAD_LOCAL_P (r)
13214 && !processing_template_decl)
13215 set_decl_tls_model (r, decl_default_tls_model (r));
13217 else if (DECL_SELF_REFERENCE_P (t))
13218 SET_DECL_SELF_REFERENCE_P (r);
13219 TREE_TYPE (r) = type;
13220 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13221 DECL_CONTEXT (r) = ctx;
13222 /* Clear out the mangled name and RTL for the instantiation. */
13223 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13224 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13225 SET_DECL_RTL (r, NULL);
13226 /* The initializer must not be expanded until it is required;
13227 see [temp.inst]. */
13228 DECL_INITIAL (r) = NULL_TREE;
13229 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13230 if (VAR_P (r))
13232 if (DECL_LANG_SPECIFIC (r))
13233 SET_DECL_DEPENDENT_INIT_P (r, false);
13235 SET_DECL_MODE (r, VOIDmode);
13237 /* Possibly limit visibility based on template args. */
13238 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13239 if (DECL_VISIBILITY_SPECIFIED (t))
13241 DECL_VISIBILITY_SPECIFIED (r) = 0;
13242 DECL_ATTRIBUTES (r)
13243 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13245 determine_visibility (r);
13248 if (!local_p)
13250 /* A static data member declaration is always marked
13251 external when it is declared in-class, even if an
13252 initializer is present. We mimic the non-template
13253 processing here. */
13254 DECL_EXTERNAL (r) = 1;
13255 if (DECL_NAMESPACE_SCOPE_P (t))
13256 DECL_NOT_REALLY_EXTERN (r) = 1;
13258 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13259 SET_DECL_IMPLICIT_INSTANTIATION (r);
13260 register_specialization (r, gen_tmpl, argvec, false, hash);
13262 else
13264 if (DECL_LANG_SPECIFIC (r))
13265 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13266 if (!cp_unevaluated_operand)
13267 register_local_specialization (r, t);
13270 DECL_CHAIN (r) = NULL_TREE;
13272 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13273 /*flags=*/0,
13274 args, complain, in_decl);
13276 /* Preserve a typedef that names a type. */
13277 if (is_typedef_decl (r) && type != error_mark_node)
13279 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13280 set_underlying_type (r);
13281 if (TYPE_DECL_ALIAS_P (r))
13282 /* An alias template specialization can be dependent
13283 even if its underlying type is not. */
13284 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13287 layout_decl (r, 0);
13289 break;
13291 default:
13292 gcc_unreachable ();
13294 #undef RETURN
13296 out:
13297 /* Restore the file and line information. */
13298 input_location = saved_loc;
13300 return r;
13303 /* Substitute into the ARG_TYPES of a function type.
13304 If END is a TREE_CHAIN, leave it and any following types
13305 un-substituted. */
13307 static tree
13308 tsubst_arg_types (tree arg_types,
13309 tree args,
13310 tree end,
13311 tsubst_flags_t complain,
13312 tree in_decl)
13314 tree remaining_arg_types;
13315 tree type = NULL_TREE;
13316 int i = 1;
13317 tree expanded_args = NULL_TREE;
13318 tree default_arg;
13320 if (!arg_types || arg_types == void_list_node || arg_types == end)
13321 return arg_types;
13323 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13324 args, end, complain, in_decl);
13325 if (remaining_arg_types == error_mark_node)
13326 return error_mark_node;
13328 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13330 /* For a pack expansion, perform substitution on the
13331 entire expression. Later on, we'll handle the arguments
13332 one-by-one. */
13333 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13334 args, complain, in_decl);
13336 if (TREE_CODE (expanded_args) == TREE_VEC)
13337 /* So that we'll spin through the parameters, one by one. */
13338 i = TREE_VEC_LENGTH (expanded_args);
13339 else
13341 /* We only partially substituted into the parameter
13342 pack. Our type is TYPE_PACK_EXPANSION. */
13343 type = expanded_args;
13344 expanded_args = NULL_TREE;
13348 while (i > 0) {
13349 --i;
13351 if (expanded_args)
13352 type = TREE_VEC_ELT (expanded_args, i);
13353 else if (!type)
13354 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13356 if (type == error_mark_node)
13357 return error_mark_node;
13358 if (VOID_TYPE_P (type))
13360 if (complain & tf_error)
13362 error ("invalid parameter type %qT", type);
13363 if (in_decl)
13364 error ("in declaration %q+D", in_decl);
13366 return error_mark_node;
13368 /* DR 657. */
13369 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13370 return error_mark_node;
13372 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13373 top-level qualifiers as required. */
13374 type = cv_unqualified (type_decays_to (type));
13376 /* We do not substitute into default arguments here. The standard
13377 mandates that they be instantiated only when needed, which is
13378 done in build_over_call. */
13379 default_arg = TREE_PURPOSE (arg_types);
13381 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13382 since the new op() won't have any associated template arguments for us
13383 to refer to later. */
13384 if (lambda_fn_in_template_p (in_decl))
13385 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13386 false/*fn*/, false/*constexpr*/);
13388 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13390 /* We've instantiated a template before its default arguments
13391 have been parsed. This can happen for a nested template
13392 class, and is not an error unless we require the default
13393 argument in a call of this function. */
13394 remaining_arg_types =
13395 tree_cons (default_arg, type, remaining_arg_types);
13396 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13398 else
13399 remaining_arg_types =
13400 hash_tree_cons (default_arg, type, remaining_arg_types);
13403 return remaining_arg_types;
13406 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13407 *not* handle the exception-specification for FNTYPE, because the
13408 initial substitution of explicitly provided template parameters
13409 during argument deduction forbids substitution into the
13410 exception-specification:
13412 [temp.deduct]
13414 All references in the function type of the function template to the
13415 corresponding template parameters are replaced by the specified tem-
13416 plate argument values. If a substitution in a template parameter or
13417 in the function type of the function template results in an invalid
13418 type, type deduction fails. [Note: The equivalent substitution in
13419 exception specifications is done only when the function is instanti-
13420 ated, at which point a program is ill-formed if the substitution
13421 results in an invalid type.] */
13423 static tree
13424 tsubst_function_type (tree t,
13425 tree args,
13426 tsubst_flags_t complain,
13427 tree in_decl)
13429 tree return_type;
13430 tree arg_types = NULL_TREE;
13431 tree fntype;
13433 /* The TYPE_CONTEXT is not used for function/method types. */
13434 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13436 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13437 failure. */
13438 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13440 if (late_return_type_p)
13442 /* Substitute the argument types. */
13443 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13444 complain, in_decl);
13445 if (arg_types == error_mark_node)
13446 return error_mark_node;
13448 tree save_ccp = current_class_ptr;
13449 tree save_ccr = current_class_ref;
13450 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13451 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13452 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13453 if (do_inject)
13455 /* DR 1207: 'this' is in scope in the trailing return type. */
13456 inject_this_parameter (this_type, cp_type_quals (this_type));
13459 /* Substitute the return type. */
13460 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13462 if (do_inject)
13464 current_class_ptr = save_ccp;
13465 current_class_ref = save_ccr;
13468 else
13469 /* Substitute the return type. */
13470 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13472 if (return_type == error_mark_node)
13473 return error_mark_node;
13474 /* DR 486 clarifies that creation of a function type with an
13475 invalid return type is a deduction failure. */
13476 if (TREE_CODE (return_type) == ARRAY_TYPE
13477 || TREE_CODE (return_type) == FUNCTION_TYPE)
13479 if (complain & tf_error)
13481 if (TREE_CODE (return_type) == ARRAY_TYPE)
13482 error ("function returning an array");
13483 else
13484 error ("function returning a function");
13486 return error_mark_node;
13488 /* And DR 657. */
13489 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13490 return error_mark_node;
13492 if (!late_return_type_p)
13494 /* Substitute the argument types. */
13495 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13496 complain, in_decl);
13497 if (arg_types == error_mark_node)
13498 return error_mark_node;
13501 /* Construct a new type node and return it. */
13502 if (TREE_CODE (t) == FUNCTION_TYPE)
13504 fntype = build_function_type (return_type, arg_types);
13505 fntype = apply_memfn_quals (fntype,
13506 type_memfn_quals (t),
13507 type_memfn_rqual (t));
13509 else
13511 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13512 /* Don't pick up extra function qualifiers from the basetype. */
13513 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13514 if (! MAYBE_CLASS_TYPE_P (r))
13516 /* [temp.deduct]
13518 Type deduction may fail for any of the following
13519 reasons:
13521 -- Attempting to create "pointer to member of T" when T
13522 is not a class type. */
13523 if (complain & tf_error)
13524 error ("creating pointer to member function of non-class type %qT",
13526 return error_mark_node;
13529 fntype = build_method_type_directly (r, return_type,
13530 TREE_CHAIN (arg_types));
13531 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13533 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13535 if (late_return_type_p)
13536 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13538 return fntype;
13541 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13542 ARGS into that specification, and return the substituted
13543 specification. If there is no specification, return NULL_TREE. */
13545 static tree
13546 tsubst_exception_specification (tree fntype,
13547 tree args,
13548 tsubst_flags_t complain,
13549 tree in_decl,
13550 bool defer_ok)
13552 tree specs;
13553 tree new_specs;
13555 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13556 new_specs = NULL_TREE;
13557 if (specs && TREE_PURPOSE (specs))
13559 /* A noexcept-specifier. */
13560 tree expr = TREE_PURPOSE (specs);
13561 if (TREE_CODE (expr) == INTEGER_CST)
13562 new_specs = expr;
13563 else if (defer_ok)
13565 /* Defer instantiation of noexcept-specifiers to avoid
13566 excessive instantiations (c++/49107). */
13567 new_specs = make_node (DEFERRED_NOEXCEPT);
13568 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13570 /* We already partially instantiated this member template,
13571 so combine the new args with the old. */
13572 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13573 = DEFERRED_NOEXCEPT_PATTERN (expr);
13574 DEFERRED_NOEXCEPT_ARGS (new_specs)
13575 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13577 else
13579 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13580 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13583 else
13584 new_specs = tsubst_copy_and_build
13585 (expr, args, complain, in_decl, /*function_p=*/false,
13586 /*integral_constant_expression_p=*/true);
13587 new_specs = build_noexcept_spec (new_specs, complain);
13589 else if (specs)
13591 if (! TREE_VALUE (specs))
13592 new_specs = specs;
13593 else
13594 while (specs)
13596 tree spec;
13597 int i, len = 1;
13598 tree expanded_specs = NULL_TREE;
13600 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13602 /* Expand the pack expansion type. */
13603 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13604 args, complain,
13605 in_decl);
13607 if (expanded_specs == error_mark_node)
13608 return error_mark_node;
13609 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13610 len = TREE_VEC_LENGTH (expanded_specs);
13611 else
13613 /* We're substituting into a member template, so
13614 we got a TYPE_PACK_EXPANSION back. Add that
13615 expansion and move on. */
13616 gcc_assert (TREE_CODE (expanded_specs)
13617 == TYPE_PACK_EXPANSION);
13618 new_specs = add_exception_specifier (new_specs,
13619 expanded_specs,
13620 complain);
13621 specs = TREE_CHAIN (specs);
13622 continue;
13626 for (i = 0; i < len; ++i)
13628 if (expanded_specs)
13629 spec = TREE_VEC_ELT (expanded_specs, i);
13630 else
13631 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13632 if (spec == error_mark_node)
13633 return spec;
13634 new_specs = add_exception_specifier (new_specs, spec,
13635 complain);
13638 specs = TREE_CHAIN (specs);
13641 return new_specs;
13644 /* Take the tree structure T and replace template parameters used
13645 therein with the argument vector ARGS. IN_DECL is an associated
13646 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13647 Issue error and warning messages under control of COMPLAIN. Note
13648 that we must be relatively non-tolerant of extensions here, in
13649 order to preserve conformance; if we allow substitutions that
13650 should not be allowed, we may allow argument deductions that should
13651 not succeed, and therefore report ambiguous overload situations
13652 where there are none. In theory, we could allow the substitution,
13653 but indicate that it should have failed, and allow our caller to
13654 make sure that the right thing happens, but we don't try to do this
13655 yet.
13657 This function is used for dealing with types, decls and the like;
13658 for expressions, use tsubst_expr or tsubst_copy. */
13660 tree
13661 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13663 enum tree_code code;
13664 tree type, r = NULL_TREE;
13666 if (t == NULL_TREE || t == error_mark_node
13667 || t == integer_type_node
13668 || t == void_type_node
13669 || t == char_type_node
13670 || t == unknown_type_node
13671 || TREE_CODE (t) == NAMESPACE_DECL
13672 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13673 return t;
13675 if (DECL_P (t))
13676 return tsubst_decl (t, args, complain);
13678 if (args == NULL_TREE)
13679 return t;
13681 code = TREE_CODE (t);
13683 if (code == IDENTIFIER_NODE)
13684 type = IDENTIFIER_TYPE_VALUE (t);
13685 else
13686 type = TREE_TYPE (t);
13688 gcc_assert (type != unknown_type_node);
13690 /* Reuse typedefs. We need to do this to handle dependent attributes,
13691 such as attribute aligned. */
13692 if (TYPE_P (t)
13693 && typedef_variant_p (t))
13695 tree decl = TYPE_NAME (t);
13697 if (alias_template_specialization_p (t))
13699 /* DECL represents an alias template and we want to
13700 instantiate it. */
13701 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13702 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13703 r = instantiate_alias_template (tmpl, gen_args, complain);
13705 else if (DECL_CLASS_SCOPE_P (decl)
13706 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13707 && uses_template_parms (DECL_CONTEXT (decl)))
13709 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13710 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13711 r = retrieve_specialization (tmpl, gen_args, 0);
13713 else if (DECL_FUNCTION_SCOPE_P (decl)
13714 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13715 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13716 r = retrieve_local_specialization (decl);
13717 else
13718 /* The typedef is from a non-template context. */
13719 return t;
13721 if (r)
13723 r = TREE_TYPE (r);
13724 r = cp_build_qualified_type_real
13725 (r, cp_type_quals (t) | cp_type_quals (r),
13726 complain | tf_ignore_bad_quals);
13727 return r;
13729 else
13731 /* We don't have an instantiation yet, so drop the typedef. */
13732 int quals = cp_type_quals (t);
13733 t = DECL_ORIGINAL_TYPE (decl);
13734 t = cp_build_qualified_type_real (t, quals,
13735 complain | tf_ignore_bad_quals);
13739 bool fndecl_type = (complain & tf_fndecl_type);
13740 complain &= ~tf_fndecl_type;
13742 if (type
13743 && code != TYPENAME_TYPE
13744 && code != TEMPLATE_TYPE_PARM
13745 && code != TEMPLATE_PARM_INDEX
13746 && code != IDENTIFIER_NODE
13747 && code != FUNCTION_TYPE
13748 && code != METHOD_TYPE)
13749 type = tsubst (type, args, complain, in_decl);
13750 if (type == error_mark_node)
13751 return error_mark_node;
13753 switch (code)
13755 case RECORD_TYPE:
13756 case UNION_TYPE:
13757 case ENUMERAL_TYPE:
13758 return tsubst_aggr_type (t, args, complain, in_decl,
13759 /*entering_scope=*/0);
13761 case ERROR_MARK:
13762 case IDENTIFIER_NODE:
13763 case VOID_TYPE:
13764 case REAL_TYPE:
13765 case COMPLEX_TYPE:
13766 case VECTOR_TYPE:
13767 case BOOLEAN_TYPE:
13768 case NULLPTR_TYPE:
13769 case LANG_TYPE:
13770 return t;
13772 case INTEGER_TYPE:
13773 if (t == integer_type_node)
13774 return t;
13776 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13777 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13778 return t;
13781 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13783 max = tsubst_expr (omax, args, complain, in_decl,
13784 /*integral_constant_expression_p=*/false);
13786 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13787 needed. */
13788 if (TREE_CODE (max) == NOP_EXPR
13789 && TREE_SIDE_EFFECTS (omax)
13790 && !TREE_TYPE (max))
13791 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13793 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13794 with TREE_SIDE_EFFECTS that indicates this is not an integral
13795 constant expression. */
13796 if (processing_template_decl
13797 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13799 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13800 TREE_SIDE_EFFECTS (max) = 1;
13803 return compute_array_index_type (NULL_TREE, max, complain);
13806 case TEMPLATE_TYPE_PARM:
13807 case TEMPLATE_TEMPLATE_PARM:
13808 case BOUND_TEMPLATE_TEMPLATE_PARM:
13809 case TEMPLATE_PARM_INDEX:
13811 int idx;
13812 int level;
13813 int levels;
13814 tree arg = NULL_TREE;
13816 /* Early in template argument deduction substitution, we don't
13817 want to reduce the level of 'auto', or it will be confused
13818 with a normal template parm in subsequent deduction. */
13819 if (is_auto (t) && (complain & tf_partial))
13820 return t;
13822 r = NULL_TREE;
13824 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13825 template_parm_level_and_index (t, &level, &idx);
13827 levels = TMPL_ARGS_DEPTH (args);
13828 if (level <= levels
13829 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13831 arg = TMPL_ARG (args, level, idx);
13833 /* See through ARGUMENT_PACK_SELECT arguments. */
13834 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13835 arg = argument_pack_select_arg (arg);
13838 if (arg == error_mark_node)
13839 return error_mark_node;
13840 else if (arg != NULL_TREE)
13842 if (ARGUMENT_PACK_P (arg))
13843 /* If ARG is an argument pack, we don't actually want to
13844 perform a substitution here, because substitutions
13845 for argument packs are only done
13846 element-by-element. We can get to this point when
13847 substituting the type of a non-type template
13848 parameter pack, when that type actually contains
13849 template parameter packs from an outer template, e.g.,
13851 template<typename... Types> struct A {
13852 template<Types... Values> struct B { };
13853 }; */
13854 return t;
13856 if (code == TEMPLATE_TYPE_PARM)
13858 int quals;
13859 gcc_assert (TYPE_P (arg));
13861 quals = cp_type_quals (arg) | cp_type_quals (t);
13863 return cp_build_qualified_type_real
13864 (arg, quals, complain | tf_ignore_bad_quals);
13866 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13868 /* We are processing a type constructed from a
13869 template template parameter. */
13870 tree argvec = tsubst (TYPE_TI_ARGS (t),
13871 args, complain, in_decl);
13872 if (argvec == error_mark_node)
13873 return error_mark_node;
13875 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13876 || TREE_CODE (arg) == TEMPLATE_DECL
13877 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13879 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13880 /* Consider this code:
13882 template <template <class> class Template>
13883 struct Internal {
13884 template <class Arg> using Bind = Template<Arg>;
13887 template <template <class> class Template, class Arg>
13888 using Instantiate = Template<Arg>; //#0
13890 template <template <class> class Template,
13891 class Argument>
13892 using Bind =
13893 Instantiate<Internal<Template>::template Bind,
13894 Argument>; //#1
13896 When #1 is parsed, the
13897 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13898 parameter `Template' in #0 matches the
13899 UNBOUND_CLASS_TEMPLATE representing the argument
13900 `Internal<Template>::template Bind'; We then want
13901 to assemble the type `Bind<Argument>' that can't
13902 be fully created right now, because
13903 `Internal<Template>' not being complete, the Bind
13904 template cannot be looked up in that context. So
13905 we need to "store" `Bind<Argument>' for later
13906 when the context of Bind becomes complete. Let's
13907 store that in a TYPENAME_TYPE. */
13908 return make_typename_type (TYPE_CONTEXT (arg),
13909 build_nt (TEMPLATE_ID_EXPR,
13910 TYPE_IDENTIFIER (arg),
13911 argvec),
13912 typename_type,
13913 complain);
13915 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13916 are resolving nested-types in the signature of a
13917 member function templates. Otherwise ARG is a
13918 TEMPLATE_DECL and is the real template to be
13919 instantiated. */
13920 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13921 arg = TYPE_NAME (arg);
13923 r = lookup_template_class (arg,
13924 argvec, in_decl,
13925 DECL_CONTEXT (arg),
13926 /*entering_scope=*/0,
13927 complain);
13928 return cp_build_qualified_type_real
13929 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13931 else if (code == TEMPLATE_TEMPLATE_PARM)
13932 return arg;
13933 else
13934 /* TEMPLATE_PARM_INDEX. */
13935 return convert_from_reference (unshare_expr (arg));
13938 if (level == 1)
13939 /* This can happen during the attempted tsubst'ing in
13940 unify. This means that we don't yet have any information
13941 about the template parameter in question. */
13942 return t;
13944 /* If we get here, we must have been looking at a parm for a
13945 more deeply nested template. Make a new version of this
13946 template parameter, but with a lower level. */
13947 switch (code)
13949 case TEMPLATE_TYPE_PARM:
13950 case TEMPLATE_TEMPLATE_PARM:
13951 case BOUND_TEMPLATE_TEMPLATE_PARM:
13952 if (cp_type_quals (t))
13954 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13955 r = cp_build_qualified_type_real
13956 (r, cp_type_quals (t),
13957 complain | (code == TEMPLATE_TYPE_PARM
13958 ? tf_ignore_bad_quals : 0));
13960 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13961 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13962 && (r = (TEMPLATE_PARM_DESCENDANTS
13963 (TEMPLATE_TYPE_PARM_INDEX (t))))
13964 && (r = TREE_TYPE (r))
13965 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13966 /* Break infinite recursion when substituting the constraints
13967 of a constrained placeholder. */;
13968 else
13970 r = copy_type (t);
13971 TEMPLATE_TYPE_PARM_INDEX (r)
13972 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13973 r, levels, args, complain);
13974 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13975 TYPE_MAIN_VARIANT (r) = r;
13976 TYPE_POINTER_TO (r) = NULL_TREE;
13977 TYPE_REFERENCE_TO (r) = NULL_TREE;
13979 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13981 /* Propagate constraints on placeholders. */
13982 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13983 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13984 = tsubst_constraint (constr, args, complain, in_decl);
13985 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13988 pl = tsubst (pl, args, complain, in_decl);
13989 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13993 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13994 /* We have reduced the level of the template
13995 template parameter, but not the levels of its
13996 template parameters, so canonical_type_parameter
13997 will not be able to find the canonical template
13998 template parameter for this level. Thus, we
13999 require structural equality checking to compare
14000 TEMPLATE_TEMPLATE_PARMs. */
14001 SET_TYPE_STRUCTURAL_EQUALITY (r);
14002 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14003 SET_TYPE_STRUCTURAL_EQUALITY (r);
14004 else
14005 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14007 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14009 tree tinfo = TYPE_TEMPLATE_INFO (t);
14010 /* We might need to substitute into the types of non-type
14011 template parameters. */
14012 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14013 complain, in_decl);
14014 if (tmpl == error_mark_node)
14015 return error_mark_node;
14016 tree argvec = tsubst (TI_ARGS (tinfo), args,
14017 complain, in_decl);
14018 if (argvec == error_mark_node)
14019 return error_mark_node;
14021 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14022 = build_template_info (tmpl, argvec);
14025 break;
14027 case TEMPLATE_PARM_INDEX:
14028 /* OK, now substitute the type of the non-type parameter. We
14029 couldn't do it earlier because it might be an auto parameter,
14030 and we wouldn't need to if we had an argument. */
14031 type = tsubst (type, args, complain, in_decl);
14032 if (type == error_mark_node)
14033 return error_mark_node;
14034 r = reduce_template_parm_level (t, type, levels, args, complain);
14035 break;
14037 default:
14038 gcc_unreachable ();
14041 return r;
14044 case TREE_LIST:
14046 tree purpose, value, chain;
14048 if (t == void_list_node)
14049 return t;
14051 purpose = TREE_PURPOSE (t);
14052 if (purpose)
14054 purpose = tsubst (purpose, args, complain, in_decl);
14055 if (purpose == error_mark_node)
14056 return error_mark_node;
14058 value = TREE_VALUE (t);
14059 if (value)
14061 value = tsubst (value, args, complain, in_decl);
14062 if (value == error_mark_node)
14063 return error_mark_node;
14065 chain = TREE_CHAIN (t);
14066 if (chain && chain != void_type_node)
14068 chain = tsubst (chain, args, complain, in_decl);
14069 if (chain == error_mark_node)
14070 return error_mark_node;
14072 if (purpose == TREE_PURPOSE (t)
14073 && value == TREE_VALUE (t)
14074 && chain == TREE_CHAIN (t))
14075 return t;
14076 return hash_tree_cons (purpose, value, chain);
14079 case TREE_BINFO:
14080 /* We should never be tsubsting a binfo. */
14081 gcc_unreachable ();
14083 case TREE_VEC:
14084 /* A vector of template arguments. */
14085 gcc_assert (!type);
14086 return tsubst_template_args (t, args, complain, in_decl);
14088 case POINTER_TYPE:
14089 case REFERENCE_TYPE:
14091 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14092 return t;
14094 /* [temp.deduct]
14096 Type deduction may fail for any of the following
14097 reasons:
14099 -- Attempting to create a pointer to reference type.
14100 -- Attempting to create a reference to a reference type or
14101 a reference to void.
14103 Core issue 106 says that creating a reference to a reference
14104 during instantiation is no longer a cause for failure. We
14105 only enforce this check in strict C++98 mode. */
14106 if ((TREE_CODE (type) == REFERENCE_TYPE
14107 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14108 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14110 static location_t last_loc;
14112 /* We keep track of the last time we issued this error
14113 message to avoid spewing a ton of messages during a
14114 single bad template instantiation. */
14115 if (complain & tf_error
14116 && last_loc != input_location)
14118 if (VOID_TYPE_P (type))
14119 error ("forming reference to void");
14120 else if (code == POINTER_TYPE)
14121 error ("forming pointer to reference type %qT", type);
14122 else
14123 error ("forming reference to reference type %qT", type);
14124 last_loc = input_location;
14127 return error_mark_node;
14129 else if (TREE_CODE (type) == FUNCTION_TYPE
14130 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14131 || type_memfn_rqual (type) != REF_QUAL_NONE))
14133 if (complain & tf_error)
14135 if (code == POINTER_TYPE)
14136 error ("forming pointer to qualified function type %qT",
14137 type);
14138 else
14139 error ("forming reference to qualified function type %qT",
14140 type);
14142 return error_mark_node;
14144 else if (code == POINTER_TYPE)
14146 r = build_pointer_type (type);
14147 if (TREE_CODE (type) == METHOD_TYPE)
14148 r = build_ptrmemfunc_type (r);
14150 else if (TREE_CODE (type) == REFERENCE_TYPE)
14151 /* In C++0x, during template argument substitution, when there is an
14152 attempt to create a reference to a reference type, reference
14153 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14155 "If a template-argument for a template-parameter T names a type
14156 that is a reference to a type A, an attempt to create the type
14157 'lvalue reference to cv T' creates the type 'lvalue reference to
14158 A,' while an attempt to create the type type rvalue reference to
14159 cv T' creates the type T"
14161 r = cp_build_reference_type
14162 (TREE_TYPE (type),
14163 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14164 else
14165 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14166 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14168 if (r != error_mark_node)
14169 /* Will this ever be needed for TYPE_..._TO values? */
14170 layout_type (r);
14172 return r;
14174 case OFFSET_TYPE:
14176 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14177 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14179 /* [temp.deduct]
14181 Type deduction may fail for any of the following
14182 reasons:
14184 -- Attempting to create "pointer to member of T" when T
14185 is not a class type. */
14186 if (complain & tf_error)
14187 error ("creating pointer to member of non-class type %qT", r);
14188 return error_mark_node;
14190 if (TREE_CODE (type) == REFERENCE_TYPE)
14192 if (complain & tf_error)
14193 error ("creating pointer to member reference type %qT", type);
14194 return error_mark_node;
14196 if (VOID_TYPE_P (type))
14198 if (complain & tf_error)
14199 error ("creating pointer to member of type void");
14200 return error_mark_node;
14202 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14203 if (TREE_CODE (type) == FUNCTION_TYPE)
14205 /* The type of the implicit object parameter gets its
14206 cv-qualifiers from the FUNCTION_TYPE. */
14207 tree memptr;
14208 tree method_type
14209 = build_memfn_type (type, r, type_memfn_quals (type),
14210 type_memfn_rqual (type));
14211 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14212 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14213 complain);
14215 else
14216 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14217 cp_type_quals (t),
14218 complain);
14220 case FUNCTION_TYPE:
14221 case METHOD_TYPE:
14223 tree fntype;
14224 tree specs;
14225 fntype = tsubst_function_type (t, args, complain, in_decl);
14226 if (fntype == error_mark_node)
14227 return error_mark_node;
14229 /* Substitute the exception specification. */
14230 specs = tsubst_exception_specification (t, args, complain, in_decl,
14231 /*defer_ok*/fndecl_type);
14232 if (specs == error_mark_node)
14233 return error_mark_node;
14234 if (specs)
14235 fntype = build_exception_variant (fntype, specs);
14236 return fntype;
14238 case ARRAY_TYPE:
14240 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14241 if (domain == error_mark_node)
14242 return error_mark_node;
14244 /* As an optimization, we avoid regenerating the array type if
14245 it will obviously be the same as T. */
14246 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14247 return t;
14249 /* These checks should match the ones in create_array_type_for_decl.
14251 [temp.deduct]
14253 The deduction may fail for any of the following reasons:
14255 -- Attempting to create an array with an element type that
14256 is void, a function type, or a reference type, or [DR337]
14257 an abstract class type. */
14258 if (VOID_TYPE_P (type)
14259 || TREE_CODE (type) == FUNCTION_TYPE
14260 || (TREE_CODE (type) == ARRAY_TYPE
14261 && TYPE_DOMAIN (type) == NULL_TREE)
14262 || TREE_CODE (type) == REFERENCE_TYPE)
14264 if (complain & tf_error)
14265 error ("creating array of %qT", type);
14266 return error_mark_node;
14269 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14270 return error_mark_node;
14272 r = build_cplus_array_type (type, domain);
14274 if (TYPE_USER_ALIGN (t))
14276 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14277 TYPE_USER_ALIGN (r) = 1;
14280 return r;
14283 case TYPENAME_TYPE:
14285 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14286 in_decl, /*entering_scope=*/1);
14287 if (ctx == error_mark_node)
14288 return error_mark_node;
14290 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14291 complain, in_decl);
14292 if (f == error_mark_node)
14293 return error_mark_node;
14295 if (!MAYBE_CLASS_TYPE_P (ctx))
14297 if (complain & tf_error)
14298 error ("%qT is not a class, struct, or union type", ctx);
14299 return error_mark_node;
14301 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14303 /* Normally, make_typename_type does not require that the CTX
14304 have complete type in order to allow things like:
14306 template <class T> struct S { typename S<T>::X Y; };
14308 But, such constructs have already been resolved by this
14309 point, so here CTX really should have complete type, unless
14310 it's a partial instantiation. */
14311 ctx = complete_type (ctx);
14312 if (!COMPLETE_TYPE_P (ctx))
14314 if (complain & tf_error)
14315 cxx_incomplete_type_error (NULL_TREE, ctx);
14316 return error_mark_node;
14320 f = make_typename_type (ctx, f, typename_type,
14321 complain | tf_keep_type_decl);
14322 if (f == error_mark_node)
14323 return f;
14324 if (TREE_CODE (f) == TYPE_DECL)
14326 complain |= tf_ignore_bad_quals;
14327 f = TREE_TYPE (f);
14330 if (TREE_CODE (f) != TYPENAME_TYPE)
14332 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14334 if (complain & tf_error)
14335 error ("%qT resolves to %qT, which is not an enumeration type",
14336 t, f);
14337 else
14338 return error_mark_node;
14340 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14342 if (complain & tf_error)
14343 error ("%qT resolves to %qT, which is is not a class type",
14344 t, f);
14345 else
14346 return error_mark_node;
14350 return cp_build_qualified_type_real
14351 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14354 case UNBOUND_CLASS_TEMPLATE:
14356 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14357 in_decl, /*entering_scope=*/1);
14358 tree name = TYPE_IDENTIFIER (t);
14359 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14361 if (ctx == error_mark_node || name == error_mark_node)
14362 return error_mark_node;
14364 if (parm_list)
14365 parm_list = tsubst_template_parms (parm_list, args, complain);
14366 return make_unbound_class_template (ctx, name, parm_list, complain);
14369 case TYPEOF_TYPE:
14371 tree type;
14373 ++cp_unevaluated_operand;
14374 ++c_inhibit_evaluation_warnings;
14376 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14377 complain, in_decl,
14378 /*integral_constant_expression_p=*/false);
14380 --cp_unevaluated_operand;
14381 --c_inhibit_evaluation_warnings;
14383 type = finish_typeof (type);
14384 return cp_build_qualified_type_real (type,
14385 cp_type_quals (t)
14386 | cp_type_quals (type),
14387 complain);
14390 case DECLTYPE_TYPE:
14392 tree type;
14394 ++cp_unevaluated_operand;
14395 ++c_inhibit_evaluation_warnings;
14397 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14398 complain|tf_decltype, in_decl,
14399 /*function_p*/false,
14400 /*integral_constant_expression*/false);
14402 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14404 if (type == NULL_TREE)
14406 if (complain & tf_error)
14407 error ("empty initializer in lambda init-capture");
14408 type = error_mark_node;
14410 else if (TREE_CODE (type) == TREE_LIST)
14411 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14414 --cp_unevaluated_operand;
14415 --c_inhibit_evaluation_warnings;
14417 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14418 type = lambda_capture_field_type (type,
14419 DECLTYPE_FOR_INIT_CAPTURE (t),
14420 DECLTYPE_FOR_REF_CAPTURE (t));
14421 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14422 type = lambda_proxy_type (type);
14423 else
14425 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14426 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14427 && EXPR_P (type))
14428 /* In a template ~id could be either a complement expression
14429 or an unqualified-id naming a destructor; if instantiating
14430 it produces an expression, it's not an id-expression or
14431 member access. */
14432 id = false;
14433 type = finish_decltype_type (type, id, complain);
14435 return cp_build_qualified_type_real (type,
14436 cp_type_quals (t)
14437 | cp_type_quals (type),
14438 complain | tf_ignore_bad_quals);
14441 case UNDERLYING_TYPE:
14443 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14444 complain, in_decl);
14445 return finish_underlying_type (type);
14448 case TYPE_ARGUMENT_PACK:
14449 case NONTYPE_ARGUMENT_PACK:
14451 tree r;
14453 if (code == NONTYPE_ARGUMENT_PACK)
14454 r = make_node (code);
14455 else
14456 r = cxx_make_type (code);
14458 tree pack_args = ARGUMENT_PACK_ARGS (t);
14459 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14460 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14462 return r;
14465 case VOID_CST:
14466 case INTEGER_CST:
14467 case REAL_CST:
14468 case STRING_CST:
14469 case PLUS_EXPR:
14470 case MINUS_EXPR:
14471 case NEGATE_EXPR:
14472 case NOP_EXPR:
14473 case INDIRECT_REF:
14474 case ADDR_EXPR:
14475 case CALL_EXPR:
14476 case ARRAY_REF:
14477 case SCOPE_REF:
14478 /* We should use one of the expression tsubsts for these codes. */
14479 gcc_unreachable ();
14481 default:
14482 sorry ("use of %qs in template", get_tree_code_name (code));
14483 return error_mark_node;
14487 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14488 expression on the left-hand side of the "." or "->" operator. We
14489 only do the lookup if we had a dependent BASELINK. Otherwise we
14490 adjust it onto the instantiated heirarchy. */
14492 static tree
14493 tsubst_baselink (tree baselink, tree object_type,
14494 tree args, tsubst_flags_t complain, tree in_decl)
14496 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14497 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14498 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14500 tree optype = BASELINK_OPTYPE (baselink);
14501 optype = tsubst (optype, args, complain, in_decl);
14503 tree template_args = NULL_TREE;
14504 bool template_id_p = false;
14505 tree fns = BASELINK_FUNCTIONS (baselink);
14506 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14508 template_id_p = true;
14509 template_args = TREE_OPERAND (fns, 1);
14510 fns = TREE_OPERAND (fns, 0);
14511 if (template_args)
14512 template_args = tsubst_template_args (template_args, args,
14513 complain, in_decl);
14516 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14517 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14518 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14520 if (dependent_p)
14522 tree name = OVL_NAME (fns);
14523 if (IDENTIFIER_CONV_OP_P (name))
14524 name = make_conv_op_name (optype);
14526 if (name == complete_dtor_identifier)
14527 /* Treat as-if non-dependent below. */
14528 dependent_p = false;
14530 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14531 if (!baselink)
14533 if ((complain & tf_error)
14534 && constructor_name_p (name, qualifying_scope))
14535 error ("cannot call constructor %<%T::%D%> directly",
14536 qualifying_scope, name);
14537 return error_mark_node;
14540 if (BASELINK_P (baselink))
14541 fns = BASELINK_FUNCTIONS (baselink);
14543 else
14544 /* We're going to overwrite pieces below, make a duplicate. */
14545 baselink = copy_node (baselink);
14547 /* If lookup found a single function, mark it as used at this point.
14548 (If lookup found multiple functions the one selected later by
14549 overload resolution will be marked as used at that point.) */
14550 if (!template_id_p && !really_overloaded_fn (fns)
14551 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14552 return error_mark_node;
14554 if (BASELINK_P (baselink))
14556 /* Add back the template arguments, if present. */
14557 if (template_id_p)
14558 BASELINK_FUNCTIONS (baselink)
14559 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14561 /* Update the conversion operator type. */
14562 BASELINK_OPTYPE (baselink) = optype;
14565 if (!object_type)
14566 object_type = current_class_type;
14568 if (qualified_p || !dependent_p)
14570 baselink = adjust_result_of_qualified_name_lookup (baselink,
14571 qualifying_scope,
14572 object_type);
14573 if (!qualified_p)
14574 /* We need to call adjust_result_of_qualified_name_lookup in case the
14575 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14576 so that we still get virtual function binding. */
14577 BASELINK_QUALIFIED_P (baselink) = false;
14580 return baselink;
14583 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14584 true if the qualified-id will be a postfix-expression in-and-of
14585 itself; false if more of the postfix-expression follows the
14586 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14587 of "&". */
14589 static tree
14590 tsubst_qualified_id (tree qualified_id, tree args,
14591 tsubst_flags_t complain, tree in_decl,
14592 bool done, bool address_p)
14594 tree expr;
14595 tree scope;
14596 tree name;
14597 bool is_template;
14598 tree template_args;
14599 location_t loc = UNKNOWN_LOCATION;
14601 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14603 /* Figure out what name to look up. */
14604 name = TREE_OPERAND (qualified_id, 1);
14605 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14607 is_template = true;
14608 loc = EXPR_LOCATION (name);
14609 template_args = TREE_OPERAND (name, 1);
14610 if (template_args)
14611 template_args = tsubst_template_args (template_args, args,
14612 complain, in_decl);
14613 if (template_args == error_mark_node)
14614 return error_mark_node;
14615 name = TREE_OPERAND (name, 0);
14617 else
14619 is_template = false;
14620 template_args = NULL_TREE;
14623 /* Substitute into the qualifying scope. When there are no ARGS, we
14624 are just trying to simplify a non-dependent expression. In that
14625 case the qualifying scope may be dependent, and, in any case,
14626 substituting will not help. */
14627 scope = TREE_OPERAND (qualified_id, 0);
14628 if (args)
14630 scope = tsubst (scope, args, complain, in_decl);
14631 expr = tsubst_copy (name, args, complain, in_decl);
14633 else
14634 expr = name;
14636 if (dependent_scope_p (scope))
14638 if (is_template)
14639 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14640 tree r = build_qualified_name (NULL_TREE, scope, expr,
14641 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14642 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14643 return r;
14646 if (!BASELINK_P (name) && !DECL_P (expr))
14648 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14650 /* A BIT_NOT_EXPR is used to represent a destructor. */
14651 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14653 error ("qualifying type %qT does not match destructor name ~%qT",
14654 scope, TREE_OPERAND (expr, 0));
14655 expr = error_mark_node;
14657 else
14658 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14659 /*is_type_p=*/0, false);
14661 else
14662 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14663 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14664 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14666 if (complain & tf_error)
14668 error ("dependent-name %qE is parsed as a non-type, but "
14669 "instantiation yields a type", qualified_id);
14670 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14672 return error_mark_node;
14676 if (DECL_P (expr))
14678 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14679 scope);
14680 /* Remember that there was a reference to this entity. */
14681 if (!mark_used (expr, complain) && !(complain & tf_error))
14682 return error_mark_node;
14685 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14687 if (complain & tf_error)
14688 qualified_name_lookup_error (scope,
14689 TREE_OPERAND (qualified_id, 1),
14690 expr, input_location);
14691 return error_mark_node;
14694 if (is_template)
14696 if (variable_template_p (expr))
14697 expr = lookup_and_finish_template_variable (expr, template_args,
14698 complain);
14699 else
14700 expr = lookup_template_function (expr, template_args);
14703 if (expr == error_mark_node && complain & tf_error)
14704 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14705 expr, input_location);
14706 else if (TYPE_P (scope))
14708 expr = (adjust_result_of_qualified_name_lookup
14709 (expr, scope, current_nonlambda_class_type ()));
14710 expr = (finish_qualified_id_expr
14711 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14712 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14713 /*template_arg_p=*/false, complain));
14716 /* Expressions do not generally have reference type. */
14717 if (TREE_CODE (expr) != SCOPE_REF
14718 /* However, if we're about to form a pointer-to-member, we just
14719 want the referenced member referenced. */
14720 && TREE_CODE (expr) != OFFSET_REF)
14721 expr = convert_from_reference (expr);
14723 if (REF_PARENTHESIZED_P (qualified_id))
14724 expr = force_paren_expr (expr);
14726 return expr;
14729 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14730 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14731 for tsubst. */
14733 static tree
14734 tsubst_init (tree init, tree decl, tree args,
14735 tsubst_flags_t complain, tree in_decl)
14737 if (!init)
14738 return NULL_TREE;
14740 init = tsubst_expr (init, args, complain, in_decl, false);
14742 if (!init && TREE_TYPE (decl) != error_mark_node)
14744 /* If we had an initializer but it
14745 instantiated to nothing,
14746 value-initialize the object. This will
14747 only occur when the initializer was a
14748 pack expansion where the parameter packs
14749 used in that expansion were of length
14750 zero. */
14751 init = build_value_init (TREE_TYPE (decl),
14752 complain);
14753 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14754 init = get_target_expr_sfinae (init, complain);
14755 if (TREE_CODE (init) == TARGET_EXPR)
14756 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14759 return init;
14762 /* Like tsubst, but deals with expressions. This function just replaces
14763 template parms; to finish processing the resultant expression, use
14764 tsubst_copy_and_build or tsubst_expr. */
14766 static tree
14767 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14769 enum tree_code code;
14770 tree r;
14772 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14773 return t;
14775 code = TREE_CODE (t);
14777 switch (code)
14779 case PARM_DECL:
14780 r = retrieve_local_specialization (t);
14782 if (r == NULL_TREE)
14784 /* We get here for a use of 'this' in an NSDMI. */
14785 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14786 return current_class_ptr;
14788 /* This can happen for a parameter name used later in a function
14789 declaration (such as in a late-specified return type). Just
14790 make a dummy decl, since it's only used for its type. */
14791 gcc_assert (cp_unevaluated_operand != 0);
14792 r = tsubst_decl (t, args, complain);
14793 /* Give it the template pattern as its context; its true context
14794 hasn't been instantiated yet and this is good enough for
14795 mangling. */
14796 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14799 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14800 r = argument_pack_select_arg (r);
14801 if (!mark_used (r, complain) && !(complain & tf_error))
14802 return error_mark_node;
14803 return r;
14805 case CONST_DECL:
14807 tree enum_type;
14808 tree v;
14810 if (DECL_TEMPLATE_PARM_P (t))
14811 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14812 /* There is no need to substitute into namespace-scope
14813 enumerators. */
14814 if (DECL_NAMESPACE_SCOPE_P (t))
14815 return t;
14816 /* If ARGS is NULL, then T is known to be non-dependent. */
14817 if (args == NULL_TREE)
14818 return scalar_constant_value (t);
14820 /* Unfortunately, we cannot just call lookup_name here.
14821 Consider:
14823 template <int I> int f() {
14824 enum E { a = I };
14825 struct S { void g() { E e = a; } };
14828 When we instantiate f<7>::S::g(), say, lookup_name is not
14829 clever enough to find f<7>::a. */
14830 enum_type
14831 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14832 /*entering_scope=*/0);
14834 for (v = TYPE_VALUES (enum_type);
14835 v != NULL_TREE;
14836 v = TREE_CHAIN (v))
14837 if (TREE_PURPOSE (v) == DECL_NAME (t))
14838 return TREE_VALUE (v);
14840 /* We didn't find the name. That should never happen; if
14841 name-lookup found it during preliminary parsing, we
14842 should find it again here during instantiation. */
14843 gcc_unreachable ();
14845 return t;
14847 case FIELD_DECL:
14848 if (DECL_CONTEXT (t))
14850 tree ctx;
14852 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14853 /*entering_scope=*/1);
14854 if (ctx != DECL_CONTEXT (t))
14856 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14857 if (!r)
14859 if (complain & tf_error)
14860 error ("using invalid field %qD", t);
14861 return error_mark_node;
14863 return r;
14867 return t;
14869 case VAR_DECL:
14870 case FUNCTION_DECL:
14871 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14872 r = tsubst (t, args, complain, in_decl);
14873 else if (local_variable_p (t)
14874 && uses_template_parms (DECL_CONTEXT (t)))
14876 r = retrieve_local_specialization (t);
14877 if (r == NULL_TREE)
14879 /* First try name lookup to find the instantiation. */
14880 r = lookup_name (DECL_NAME (t));
14881 if (r && !is_capture_proxy (r))
14883 /* Make sure that the one we found is the one we want. */
14884 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14885 if (ctx != DECL_CONTEXT (r))
14886 r = NULL_TREE;
14889 if (r)
14890 /* OK */;
14891 else
14893 /* This can happen for a variable used in a
14894 late-specified return type of a local lambda, or for a
14895 local static or constant. Building a new VAR_DECL
14896 should be OK in all those cases. */
14897 r = tsubst_decl (t, args, complain);
14898 if (local_specializations)
14899 /* Avoid infinite recursion (79640). */
14900 register_local_specialization (r, t);
14901 if (decl_maybe_constant_var_p (r))
14903 /* We can't call cp_finish_decl, so handle the
14904 initializer by hand. */
14905 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14906 complain, in_decl);
14907 if (!processing_template_decl)
14908 init = maybe_constant_init (init);
14909 if (processing_template_decl
14910 ? potential_constant_expression (init)
14911 : reduced_constant_expression_p (init))
14912 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14913 = TREE_CONSTANT (r) = true;
14914 DECL_INITIAL (r) = init;
14915 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14916 TREE_TYPE (r)
14917 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14918 complain, adc_variable_type);
14920 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14921 || decl_constant_var_p (r)
14922 || errorcount || sorrycount);
14923 if (!processing_template_decl
14924 && !TREE_STATIC (r))
14925 r = process_outer_var_ref (r, complain);
14927 /* Remember this for subsequent uses. */
14928 if (local_specializations)
14929 register_local_specialization (r, t);
14931 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14932 r = argument_pack_select_arg (r);
14934 else
14935 r = t;
14936 if (!mark_used (r, complain))
14937 return error_mark_node;
14938 return r;
14940 case NAMESPACE_DECL:
14941 return t;
14943 case OVERLOAD:
14944 /* An OVERLOAD will always be a non-dependent overload set; an
14945 overload set from function scope will just be represented with an
14946 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14947 gcc_assert (!uses_template_parms (t));
14948 /* We must have marked any lookups as persistent. */
14949 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14950 return t;
14952 case BASELINK:
14953 return tsubst_baselink (t, current_nonlambda_class_type (),
14954 args, complain, in_decl);
14956 case TEMPLATE_DECL:
14957 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14958 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14959 args, complain, in_decl);
14960 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14961 return tsubst (t, args, complain, in_decl);
14962 else if (DECL_CLASS_SCOPE_P (t)
14963 && uses_template_parms (DECL_CONTEXT (t)))
14965 /* Template template argument like the following example need
14966 special treatment:
14968 template <template <class> class TT> struct C {};
14969 template <class T> struct D {
14970 template <class U> struct E {};
14971 C<E> c; // #1
14973 D<int> d; // #2
14975 We are processing the template argument `E' in #1 for
14976 the template instantiation #2. Originally, `E' is a
14977 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14978 have to substitute this with one having context `D<int>'. */
14980 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14981 if (dependent_scope_p (context))
14983 /* When rewriting a constructor into a deduction guide, a
14984 non-dependent name can become dependent, so memtmpl<args>
14985 becomes context::template memtmpl<args>. */
14986 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14987 return build_qualified_name (type, context, DECL_NAME (t),
14988 /*template*/true);
14990 return lookup_field (context, DECL_NAME(t), 0, false);
14992 else
14993 /* Ordinary template template argument. */
14994 return t;
14996 case NON_LVALUE_EXPR:
14997 case VIEW_CONVERT_EXPR:
14999 /* Handle location wrappers by substituting the wrapped node
15000 first, *then* reusing the resulting type. Doing the type
15001 first ensures that we handle template parameters and
15002 parameter pack expansions. */
15003 gcc_assert (location_wrapper_p (t));
15004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15005 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15008 case CAST_EXPR:
15009 case REINTERPRET_CAST_EXPR:
15010 case CONST_CAST_EXPR:
15011 case STATIC_CAST_EXPR:
15012 case DYNAMIC_CAST_EXPR:
15013 case IMPLICIT_CONV_EXPR:
15014 case CONVERT_EXPR:
15015 case NOP_EXPR:
15017 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15018 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15019 return build1 (code, type, op0);
15022 case SIZEOF_EXPR:
15023 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15024 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15026 tree expanded, op = TREE_OPERAND (t, 0);
15027 int len = 0;
15029 if (SIZEOF_EXPR_TYPE_P (t))
15030 op = TREE_TYPE (op);
15032 ++cp_unevaluated_operand;
15033 ++c_inhibit_evaluation_warnings;
15034 /* We only want to compute the number of arguments. */
15035 if (PACK_EXPANSION_P (op))
15036 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15037 else
15038 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15039 args, complain, in_decl);
15040 --cp_unevaluated_operand;
15041 --c_inhibit_evaluation_warnings;
15043 if (TREE_CODE (expanded) == TREE_VEC)
15045 len = TREE_VEC_LENGTH (expanded);
15046 /* Set TREE_USED for the benefit of -Wunused. */
15047 for (int i = 0; i < len; i++)
15048 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15049 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15052 if (expanded == error_mark_node)
15053 return error_mark_node;
15054 else if (PACK_EXPANSION_P (expanded)
15055 || (TREE_CODE (expanded) == TREE_VEC
15056 && pack_expansion_args_count (expanded)))
15059 if (PACK_EXPANSION_P (expanded))
15060 /* OK. */;
15061 else if (TREE_VEC_LENGTH (expanded) == 1)
15062 expanded = TREE_VEC_ELT (expanded, 0);
15063 else
15064 expanded = make_argument_pack (expanded);
15066 if (TYPE_P (expanded))
15067 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15068 complain & tf_error);
15069 else
15070 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15071 complain & tf_error);
15073 else
15074 return build_int_cst (size_type_node, len);
15076 if (SIZEOF_EXPR_TYPE_P (t))
15078 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15079 args, complain, in_decl);
15080 r = build1 (NOP_EXPR, r, error_mark_node);
15081 r = build1 (SIZEOF_EXPR,
15082 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15083 SIZEOF_EXPR_TYPE_P (r) = 1;
15084 return r;
15086 /* Fall through */
15088 case INDIRECT_REF:
15089 case NEGATE_EXPR:
15090 case TRUTH_NOT_EXPR:
15091 case BIT_NOT_EXPR:
15092 case ADDR_EXPR:
15093 case UNARY_PLUS_EXPR: /* Unary + */
15094 case ALIGNOF_EXPR:
15095 case AT_ENCODE_EXPR:
15096 case ARROW_EXPR:
15097 case THROW_EXPR:
15098 case TYPEID_EXPR:
15099 case REALPART_EXPR:
15100 case IMAGPART_EXPR:
15101 case PAREN_EXPR:
15103 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15104 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15105 return build1 (code, type, op0);
15108 case COMPONENT_REF:
15110 tree object;
15111 tree name;
15113 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15114 name = TREE_OPERAND (t, 1);
15115 if (TREE_CODE (name) == BIT_NOT_EXPR)
15117 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15118 complain, in_decl);
15119 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15121 else if (TREE_CODE (name) == SCOPE_REF
15122 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15124 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15125 complain, in_decl);
15126 name = TREE_OPERAND (name, 1);
15127 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15128 complain, in_decl);
15129 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15130 name = build_qualified_name (/*type=*/NULL_TREE,
15131 base, name,
15132 /*template_p=*/false);
15134 else if (BASELINK_P (name))
15135 name = tsubst_baselink (name,
15136 non_reference (TREE_TYPE (object)),
15137 args, complain,
15138 in_decl);
15139 else
15140 name = tsubst_copy (name, args, complain, in_decl);
15141 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15144 case PLUS_EXPR:
15145 case MINUS_EXPR:
15146 case MULT_EXPR:
15147 case TRUNC_DIV_EXPR:
15148 case CEIL_DIV_EXPR:
15149 case FLOOR_DIV_EXPR:
15150 case ROUND_DIV_EXPR:
15151 case EXACT_DIV_EXPR:
15152 case BIT_AND_EXPR:
15153 case BIT_IOR_EXPR:
15154 case BIT_XOR_EXPR:
15155 case TRUNC_MOD_EXPR:
15156 case FLOOR_MOD_EXPR:
15157 case TRUTH_ANDIF_EXPR:
15158 case TRUTH_ORIF_EXPR:
15159 case TRUTH_AND_EXPR:
15160 case TRUTH_OR_EXPR:
15161 case RSHIFT_EXPR:
15162 case LSHIFT_EXPR:
15163 case RROTATE_EXPR:
15164 case LROTATE_EXPR:
15165 case EQ_EXPR:
15166 case NE_EXPR:
15167 case MAX_EXPR:
15168 case MIN_EXPR:
15169 case LE_EXPR:
15170 case GE_EXPR:
15171 case LT_EXPR:
15172 case GT_EXPR:
15173 case COMPOUND_EXPR:
15174 case DOTSTAR_EXPR:
15175 case MEMBER_REF:
15176 case PREDECREMENT_EXPR:
15177 case PREINCREMENT_EXPR:
15178 case POSTDECREMENT_EXPR:
15179 case POSTINCREMENT_EXPR:
15181 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15182 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15183 return build_nt (code, op0, op1);
15186 case SCOPE_REF:
15188 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15189 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15190 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15191 QUALIFIED_NAME_IS_TEMPLATE (t));
15194 case ARRAY_REF:
15196 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15197 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15198 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15201 case CALL_EXPR:
15203 int n = VL_EXP_OPERAND_LENGTH (t);
15204 tree result = build_vl_exp (CALL_EXPR, n);
15205 int i;
15206 for (i = 0; i < n; i++)
15207 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15208 complain, in_decl);
15209 return result;
15212 case COND_EXPR:
15213 case MODOP_EXPR:
15214 case PSEUDO_DTOR_EXPR:
15215 case VEC_PERM_EXPR:
15217 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15218 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15219 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15220 r = build_nt (code, op0, op1, op2);
15221 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15222 return r;
15225 case NEW_EXPR:
15227 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15228 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15229 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15230 r = build_nt (code, op0, op1, op2);
15231 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15232 return r;
15235 case DELETE_EXPR:
15237 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15238 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15239 r = build_nt (code, op0, op1);
15240 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15241 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15242 return r;
15245 case TEMPLATE_ID_EXPR:
15247 /* Substituted template arguments */
15248 tree fn = TREE_OPERAND (t, 0);
15249 tree targs = TREE_OPERAND (t, 1);
15251 fn = tsubst_copy (fn, args, complain, in_decl);
15252 if (targs)
15253 targs = tsubst_template_args (targs, args, complain, in_decl);
15255 return lookup_template_function (fn, targs);
15258 case TREE_LIST:
15260 tree purpose, value, chain;
15262 if (t == void_list_node)
15263 return t;
15265 purpose = TREE_PURPOSE (t);
15266 if (purpose)
15267 purpose = tsubst_copy (purpose, args, complain, in_decl);
15268 value = TREE_VALUE (t);
15269 if (value)
15270 value = tsubst_copy (value, args, complain, in_decl);
15271 chain = TREE_CHAIN (t);
15272 if (chain && chain != void_type_node)
15273 chain = tsubst_copy (chain, args, complain, in_decl);
15274 if (purpose == TREE_PURPOSE (t)
15275 && value == TREE_VALUE (t)
15276 && chain == TREE_CHAIN (t))
15277 return t;
15278 return tree_cons (purpose, value, chain);
15281 case RECORD_TYPE:
15282 case UNION_TYPE:
15283 case ENUMERAL_TYPE:
15284 case INTEGER_TYPE:
15285 case TEMPLATE_TYPE_PARM:
15286 case TEMPLATE_TEMPLATE_PARM:
15287 case BOUND_TEMPLATE_TEMPLATE_PARM:
15288 case TEMPLATE_PARM_INDEX:
15289 case POINTER_TYPE:
15290 case REFERENCE_TYPE:
15291 case OFFSET_TYPE:
15292 case FUNCTION_TYPE:
15293 case METHOD_TYPE:
15294 case ARRAY_TYPE:
15295 case TYPENAME_TYPE:
15296 case UNBOUND_CLASS_TEMPLATE:
15297 case TYPEOF_TYPE:
15298 case DECLTYPE_TYPE:
15299 case TYPE_DECL:
15300 return tsubst (t, args, complain, in_decl);
15302 case USING_DECL:
15303 t = DECL_NAME (t);
15304 /* Fall through. */
15305 case IDENTIFIER_NODE:
15306 if (IDENTIFIER_CONV_OP_P (t))
15308 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15309 return make_conv_op_name (new_type);
15311 else
15312 return t;
15314 case CONSTRUCTOR:
15315 /* This is handled by tsubst_copy_and_build. */
15316 gcc_unreachable ();
15318 case VA_ARG_EXPR:
15320 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15321 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15322 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15325 case CLEANUP_POINT_EXPR:
15326 /* We shouldn't have built any of these during initial template
15327 generation. Instead, they should be built during instantiation
15328 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15329 gcc_unreachable ();
15331 case OFFSET_REF:
15333 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15334 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15335 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15336 r = build2 (code, type, op0, op1);
15337 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15338 if (!mark_used (TREE_OPERAND (r, 1), complain)
15339 && !(complain & tf_error))
15340 return error_mark_node;
15341 return r;
15344 case EXPR_PACK_EXPANSION:
15345 error ("invalid use of pack expansion expression");
15346 return error_mark_node;
15348 case NONTYPE_ARGUMENT_PACK:
15349 error ("use %<...%> to expand argument pack");
15350 return error_mark_node;
15352 case VOID_CST:
15353 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15354 return t;
15356 case INTEGER_CST:
15357 case REAL_CST:
15358 case STRING_CST:
15359 case COMPLEX_CST:
15361 /* Instantiate any typedefs in the type. */
15362 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15363 r = fold_convert (type, t);
15364 gcc_assert (TREE_CODE (r) == code);
15365 return r;
15368 case PTRMEM_CST:
15369 /* These can sometimes show up in a partial instantiation, but never
15370 involve template parms. */
15371 gcc_assert (!uses_template_parms (t));
15372 return t;
15374 case UNARY_LEFT_FOLD_EXPR:
15375 return tsubst_unary_left_fold (t, args, complain, in_decl);
15376 case UNARY_RIGHT_FOLD_EXPR:
15377 return tsubst_unary_right_fold (t, args, complain, in_decl);
15378 case BINARY_LEFT_FOLD_EXPR:
15379 return tsubst_binary_left_fold (t, args, complain, in_decl);
15380 case BINARY_RIGHT_FOLD_EXPR:
15381 return tsubst_binary_right_fold (t, args, complain, in_decl);
15382 case PREDICT_EXPR:
15383 return t;
15385 case DEBUG_BEGIN_STMT:
15386 /* ??? There's no point in copying it for now, but maybe some
15387 day it will contain more information, such as a pointer back
15388 to the containing function, inlined copy or so. */
15389 return t;
15391 default:
15392 /* We shouldn't get here, but keep going if !flag_checking. */
15393 if (flag_checking)
15394 gcc_unreachable ();
15395 return t;
15399 /* Helper function for tsubst_omp_clauses, used for instantiation of
15400 OMP_CLAUSE_DECL of clauses. */
15402 static tree
15403 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15404 tree in_decl)
15406 if (decl == NULL_TREE)
15407 return NULL_TREE;
15409 /* Handle an OpenMP array section represented as a TREE_LIST (or
15410 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15411 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15412 TREE_LIST. We can handle it exactly the same as an array section
15413 (purpose, value, and a chain), even though the nomenclature
15414 (low_bound, length, etc) is different. */
15415 if (TREE_CODE (decl) == TREE_LIST)
15417 tree low_bound
15418 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15419 /*integral_constant_expression_p=*/false);
15420 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15421 /*integral_constant_expression_p=*/false);
15422 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15423 in_decl);
15424 if (TREE_PURPOSE (decl) == low_bound
15425 && TREE_VALUE (decl) == length
15426 && TREE_CHAIN (decl) == chain)
15427 return decl;
15428 tree ret = tree_cons (low_bound, length, chain);
15429 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15430 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15431 return ret;
15433 tree ret = tsubst_expr (decl, args, complain, in_decl,
15434 /*integral_constant_expression_p=*/false);
15435 /* Undo convert_from_reference tsubst_expr could have called. */
15436 if (decl
15437 && REFERENCE_REF_P (ret)
15438 && !REFERENCE_REF_P (decl))
15439 ret = TREE_OPERAND (ret, 0);
15440 return ret;
15443 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15445 static tree
15446 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15447 tree args, tsubst_flags_t complain, tree in_decl)
15449 tree new_clauses = NULL_TREE, nc, oc;
15450 tree linear_no_step = NULL_TREE;
15452 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15454 nc = copy_node (oc);
15455 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15456 new_clauses = nc;
15458 switch (OMP_CLAUSE_CODE (nc))
15460 case OMP_CLAUSE_LASTPRIVATE:
15461 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15463 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15464 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15465 in_decl, /*integral_constant_expression_p=*/false);
15466 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15467 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15469 /* FALLTHRU */
15470 case OMP_CLAUSE_PRIVATE:
15471 case OMP_CLAUSE_SHARED:
15472 case OMP_CLAUSE_FIRSTPRIVATE:
15473 case OMP_CLAUSE_COPYIN:
15474 case OMP_CLAUSE_COPYPRIVATE:
15475 case OMP_CLAUSE_UNIFORM:
15476 case OMP_CLAUSE_DEPEND:
15477 case OMP_CLAUSE_FROM:
15478 case OMP_CLAUSE_TO:
15479 case OMP_CLAUSE_MAP:
15480 case OMP_CLAUSE_USE_DEVICE_PTR:
15481 case OMP_CLAUSE_IS_DEVICE_PTR:
15482 OMP_CLAUSE_DECL (nc)
15483 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15484 in_decl);
15485 break;
15486 case OMP_CLAUSE_TILE:
15487 case OMP_CLAUSE_IF:
15488 case OMP_CLAUSE_NUM_THREADS:
15489 case OMP_CLAUSE_SCHEDULE:
15490 case OMP_CLAUSE_COLLAPSE:
15491 case OMP_CLAUSE_FINAL:
15492 case OMP_CLAUSE_DEVICE:
15493 case OMP_CLAUSE_DIST_SCHEDULE:
15494 case OMP_CLAUSE_NUM_TEAMS:
15495 case OMP_CLAUSE_THREAD_LIMIT:
15496 case OMP_CLAUSE_SAFELEN:
15497 case OMP_CLAUSE_SIMDLEN:
15498 case OMP_CLAUSE_NUM_TASKS:
15499 case OMP_CLAUSE_GRAINSIZE:
15500 case OMP_CLAUSE_PRIORITY:
15501 case OMP_CLAUSE_ORDERED:
15502 case OMP_CLAUSE_HINT:
15503 case OMP_CLAUSE_NUM_GANGS:
15504 case OMP_CLAUSE_NUM_WORKERS:
15505 case OMP_CLAUSE_VECTOR_LENGTH:
15506 case OMP_CLAUSE_WORKER:
15507 case OMP_CLAUSE_VECTOR:
15508 case OMP_CLAUSE_ASYNC:
15509 case OMP_CLAUSE_WAIT:
15510 OMP_CLAUSE_OPERAND (nc, 0)
15511 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15512 in_decl, /*integral_constant_expression_p=*/false);
15513 break;
15514 case OMP_CLAUSE_REDUCTION:
15515 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15517 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15518 if (TREE_CODE (placeholder) == SCOPE_REF)
15520 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15521 complain, in_decl);
15522 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15523 = build_qualified_name (NULL_TREE, scope,
15524 TREE_OPERAND (placeholder, 1),
15525 false);
15527 else
15528 gcc_assert (identifier_p (placeholder));
15530 OMP_CLAUSE_DECL (nc)
15531 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15532 in_decl);
15533 break;
15534 case OMP_CLAUSE_GANG:
15535 case OMP_CLAUSE_ALIGNED:
15536 OMP_CLAUSE_DECL (nc)
15537 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15538 in_decl);
15539 OMP_CLAUSE_OPERAND (nc, 1)
15540 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15541 in_decl, /*integral_constant_expression_p=*/false);
15542 break;
15543 case OMP_CLAUSE_LINEAR:
15544 OMP_CLAUSE_DECL (nc)
15545 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15546 in_decl);
15547 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15549 gcc_assert (!linear_no_step);
15550 linear_no_step = nc;
15552 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15553 OMP_CLAUSE_LINEAR_STEP (nc)
15554 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15555 complain, in_decl);
15556 else
15557 OMP_CLAUSE_LINEAR_STEP (nc)
15558 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15559 in_decl,
15560 /*integral_constant_expression_p=*/false);
15561 break;
15562 case OMP_CLAUSE_NOWAIT:
15563 case OMP_CLAUSE_DEFAULT:
15564 case OMP_CLAUSE_UNTIED:
15565 case OMP_CLAUSE_MERGEABLE:
15566 case OMP_CLAUSE_INBRANCH:
15567 case OMP_CLAUSE_NOTINBRANCH:
15568 case OMP_CLAUSE_PROC_BIND:
15569 case OMP_CLAUSE_FOR:
15570 case OMP_CLAUSE_PARALLEL:
15571 case OMP_CLAUSE_SECTIONS:
15572 case OMP_CLAUSE_TASKGROUP:
15573 case OMP_CLAUSE_NOGROUP:
15574 case OMP_CLAUSE_THREADS:
15575 case OMP_CLAUSE_SIMD:
15576 case OMP_CLAUSE_DEFAULTMAP:
15577 case OMP_CLAUSE_INDEPENDENT:
15578 case OMP_CLAUSE_AUTO:
15579 case OMP_CLAUSE_SEQ:
15580 break;
15581 default:
15582 gcc_unreachable ();
15584 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15585 switch (OMP_CLAUSE_CODE (nc))
15587 case OMP_CLAUSE_SHARED:
15588 case OMP_CLAUSE_PRIVATE:
15589 case OMP_CLAUSE_FIRSTPRIVATE:
15590 case OMP_CLAUSE_LASTPRIVATE:
15591 case OMP_CLAUSE_COPYPRIVATE:
15592 case OMP_CLAUSE_LINEAR:
15593 case OMP_CLAUSE_REDUCTION:
15594 case OMP_CLAUSE_USE_DEVICE_PTR:
15595 case OMP_CLAUSE_IS_DEVICE_PTR:
15596 /* tsubst_expr on SCOPE_REF results in returning
15597 finish_non_static_data_member result. Undo that here. */
15598 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15599 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15600 == IDENTIFIER_NODE))
15602 tree t = OMP_CLAUSE_DECL (nc);
15603 tree v = t;
15604 while (v)
15605 switch (TREE_CODE (v))
15607 case COMPONENT_REF:
15608 case MEM_REF:
15609 case INDIRECT_REF:
15610 CASE_CONVERT:
15611 case POINTER_PLUS_EXPR:
15612 v = TREE_OPERAND (v, 0);
15613 continue;
15614 case PARM_DECL:
15615 if (DECL_CONTEXT (v) == current_function_decl
15616 && DECL_ARTIFICIAL (v)
15617 && DECL_NAME (v) == this_identifier)
15618 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15619 /* FALLTHRU */
15620 default:
15621 v = NULL_TREE;
15622 break;
15625 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15626 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15627 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15628 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15629 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15631 tree decl = OMP_CLAUSE_DECL (nc);
15632 if (VAR_P (decl))
15634 retrofit_lang_decl (decl);
15635 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15638 break;
15639 default:
15640 break;
15644 new_clauses = nreverse (new_clauses);
15645 if (ort != C_ORT_OMP_DECLARE_SIMD)
15647 new_clauses = finish_omp_clauses (new_clauses, ort);
15648 if (linear_no_step)
15649 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15650 if (nc == linear_no_step)
15652 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15653 break;
15656 return new_clauses;
15659 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15661 static tree
15662 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15663 tree in_decl)
15665 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15667 tree purpose, value, chain;
15669 if (t == NULL)
15670 return t;
15672 if (TREE_CODE (t) != TREE_LIST)
15673 return tsubst_copy_and_build (t, args, complain, in_decl,
15674 /*function_p=*/false,
15675 /*integral_constant_expression_p=*/false);
15677 if (t == void_list_node)
15678 return t;
15680 purpose = TREE_PURPOSE (t);
15681 if (purpose)
15682 purpose = RECUR (purpose);
15683 value = TREE_VALUE (t);
15684 if (value)
15686 if (TREE_CODE (value) != LABEL_DECL)
15687 value = RECUR (value);
15688 else
15690 value = lookup_label (DECL_NAME (value));
15691 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15692 TREE_USED (value) = 1;
15695 chain = TREE_CHAIN (t);
15696 if (chain && chain != void_type_node)
15697 chain = RECUR (chain);
15698 return tree_cons (purpose, value, chain);
15699 #undef RECUR
15702 /* Used to temporarily communicate the list of #pragma omp parallel
15703 clauses to #pragma omp for instantiation if they are combined
15704 together. */
15706 static tree *omp_parallel_combined_clauses;
15708 /* Substitute one OMP_FOR iterator. */
15710 static void
15711 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15712 tree initv, tree condv, tree incrv, tree *clauses,
15713 tree args, tsubst_flags_t complain, tree in_decl,
15714 bool integral_constant_expression_p)
15716 #define RECUR(NODE) \
15717 tsubst_expr ((NODE), args, complain, in_decl, \
15718 integral_constant_expression_p)
15719 tree decl, init, cond, incr;
15721 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15722 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15724 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15726 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15727 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15730 decl = TREE_OPERAND (init, 0);
15731 init = TREE_OPERAND (init, 1);
15732 tree decl_expr = NULL_TREE;
15733 if (init && TREE_CODE (init) == DECL_EXPR)
15735 /* We need to jump through some hoops to handle declarations in the
15736 init-statement, since we might need to handle auto deduction,
15737 but we need to keep control of initialization. */
15738 decl_expr = init;
15739 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15740 decl = tsubst_decl (decl, args, complain);
15742 else
15744 if (TREE_CODE (decl) == SCOPE_REF)
15746 decl = RECUR (decl);
15747 if (TREE_CODE (decl) == COMPONENT_REF)
15749 tree v = decl;
15750 while (v)
15751 switch (TREE_CODE (v))
15753 case COMPONENT_REF:
15754 case MEM_REF:
15755 case INDIRECT_REF:
15756 CASE_CONVERT:
15757 case POINTER_PLUS_EXPR:
15758 v = TREE_OPERAND (v, 0);
15759 continue;
15760 case PARM_DECL:
15761 if (DECL_CONTEXT (v) == current_function_decl
15762 && DECL_ARTIFICIAL (v)
15763 && DECL_NAME (v) == this_identifier)
15765 decl = TREE_OPERAND (decl, 1);
15766 decl = omp_privatize_field (decl, false);
15768 /* FALLTHRU */
15769 default:
15770 v = NULL_TREE;
15771 break;
15775 else
15776 decl = RECUR (decl);
15778 init = RECUR (init);
15780 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15781 if (auto_node && init)
15782 TREE_TYPE (decl)
15783 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15785 gcc_assert (!type_dependent_expression_p (decl));
15787 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15789 if (decl_expr)
15791 /* Declare the variable, but don't let that initialize it. */
15792 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15793 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15794 RECUR (decl_expr);
15795 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15798 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15799 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15800 if (TREE_CODE (incr) == MODIFY_EXPR)
15802 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15803 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15804 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15805 NOP_EXPR, rhs, complain);
15807 else
15808 incr = RECUR (incr);
15809 TREE_VEC_ELT (declv, i) = decl;
15810 TREE_VEC_ELT (initv, i) = init;
15811 TREE_VEC_ELT (condv, i) = cond;
15812 TREE_VEC_ELT (incrv, i) = incr;
15813 return;
15816 if (decl_expr)
15818 /* Declare and initialize the variable. */
15819 RECUR (decl_expr);
15820 init = NULL_TREE;
15822 else if (init)
15824 tree *pc;
15825 int j;
15826 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15828 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15830 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15831 && OMP_CLAUSE_DECL (*pc) == decl)
15832 break;
15833 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15834 && OMP_CLAUSE_DECL (*pc) == decl)
15836 if (j)
15837 break;
15838 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15839 tree c = *pc;
15840 *pc = OMP_CLAUSE_CHAIN (c);
15841 OMP_CLAUSE_CHAIN (c) = *clauses;
15842 *clauses = c;
15844 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15845 && OMP_CLAUSE_DECL (*pc) == decl)
15847 error ("iteration variable %qD should not be firstprivate",
15848 decl);
15849 *pc = OMP_CLAUSE_CHAIN (*pc);
15851 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15852 && OMP_CLAUSE_DECL (*pc) == decl)
15854 error ("iteration variable %qD should not be reduction",
15855 decl);
15856 *pc = OMP_CLAUSE_CHAIN (*pc);
15858 else
15859 pc = &OMP_CLAUSE_CHAIN (*pc);
15861 if (*pc)
15862 break;
15864 if (*pc == NULL_TREE)
15866 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15867 OMP_CLAUSE_DECL (c) = decl;
15868 c = finish_omp_clauses (c, C_ORT_OMP);
15869 if (c)
15871 OMP_CLAUSE_CHAIN (c) = *clauses;
15872 *clauses = c;
15876 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15877 if (COMPARISON_CLASS_P (cond))
15879 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15880 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15881 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15883 else
15884 cond = RECUR (cond);
15885 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15886 switch (TREE_CODE (incr))
15888 case PREINCREMENT_EXPR:
15889 case PREDECREMENT_EXPR:
15890 case POSTINCREMENT_EXPR:
15891 case POSTDECREMENT_EXPR:
15892 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15893 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15894 break;
15895 case MODIFY_EXPR:
15896 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15897 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15899 tree rhs = TREE_OPERAND (incr, 1);
15900 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15901 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15902 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15903 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15904 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15905 rhs0, rhs1));
15907 else
15908 incr = RECUR (incr);
15909 break;
15910 case MODOP_EXPR:
15911 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15912 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15914 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15915 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15916 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15917 TREE_TYPE (decl), lhs,
15918 RECUR (TREE_OPERAND (incr, 2))));
15920 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15921 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15922 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15924 tree rhs = TREE_OPERAND (incr, 2);
15925 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15926 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15927 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15928 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15929 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15930 rhs0, rhs1));
15932 else
15933 incr = RECUR (incr);
15934 break;
15935 default:
15936 incr = RECUR (incr);
15937 break;
15940 TREE_VEC_ELT (declv, i) = decl;
15941 TREE_VEC_ELT (initv, i) = init;
15942 TREE_VEC_ELT (condv, i) = cond;
15943 TREE_VEC_ELT (incrv, i) = incr;
15944 #undef RECUR
15947 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15948 of OMP_TARGET's body. */
15950 static tree
15951 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15953 *walk_subtrees = 0;
15954 switch (TREE_CODE (*tp))
15956 case OMP_TEAMS:
15957 return *tp;
15958 case BIND_EXPR:
15959 case STATEMENT_LIST:
15960 *walk_subtrees = 1;
15961 break;
15962 default:
15963 break;
15965 return NULL_TREE;
15968 /* Helper function for tsubst_expr. For decomposition declaration
15969 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15970 also the corresponding decls representing the identifiers
15971 of the decomposition declaration. Return DECL if successful
15972 or error_mark_node otherwise, set *FIRST to the first decl
15973 in the list chained through DECL_CHAIN and *CNT to the number
15974 of such decls. */
15976 static tree
15977 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15978 tsubst_flags_t complain, tree in_decl, tree *first,
15979 unsigned int *cnt)
15981 tree decl2, decl3, prev = decl;
15982 *cnt = 0;
15983 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15984 for (decl2 = DECL_CHAIN (pattern_decl);
15985 decl2
15986 && VAR_P (decl2)
15987 && DECL_DECOMPOSITION_P (decl2)
15988 && DECL_NAME (decl2);
15989 decl2 = DECL_CHAIN (decl2))
15991 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15993 gcc_assert (errorcount);
15994 return error_mark_node;
15996 (*cnt)++;
15997 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15998 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15999 tree v = DECL_VALUE_EXPR (decl2);
16000 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16001 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16002 decl3 = tsubst (decl2, args, complain, in_decl);
16003 SET_DECL_VALUE_EXPR (decl2, v);
16004 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16005 if (VAR_P (decl3))
16006 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16007 maybe_push_decl (decl3);
16008 if (error_operand_p (decl3))
16009 decl = error_mark_node;
16010 else if (decl != error_mark_node
16011 && DECL_CHAIN (decl3) != prev)
16013 gcc_assert (errorcount);
16014 decl = error_mark_node;
16016 else
16017 prev = decl3;
16019 *first = prev;
16020 return decl;
16023 /* Like tsubst_copy for expressions, etc. but also does semantic
16024 processing. */
16026 tree
16027 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16028 bool integral_constant_expression_p)
16030 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16031 #define RECUR(NODE) \
16032 tsubst_expr ((NODE), args, complain, in_decl, \
16033 integral_constant_expression_p)
16035 tree stmt, tmp;
16036 tree r;
16037 location_t loc;
16039 if (t == NULL_TREE || t == error_mark_node)
16040 return t;
16042 loc = input_location;
16043 if (EXPR_HAS_LOCATION (t))
16044 input_location = EXPR_LOCATION (t);
16045 if (STATEMENT_CODE_P (TREE_CODE (t)))
16046 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16048 switch (TREE_CODE (t))
16050 case STATEMENT_LIST:
16052 tree_stmt_iterator i;
16053 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16054 RECUR (tsi_stmt (i));
16055 break;
16058 case CTOR_INITIALIZER:
16059 finish_mem_initializers (tsubst_initializer_list
16060 (TREE_OPERAND (t, 0), args));
16061 break;
16063 case RETURN_EXPR:
16064 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16065 break;
16067 case EXPR_STMT:
16068 tmp = RECUR (EXPR_STMT_EXPR (t));
16069 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16070 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16071 else
16072 finish_expr_stmt (tmp);
16073 break;
16075 case USING_STMT:
16076 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16077 /*attribs=*/NULL_TREE);
16078 break;
16080 case DECL_EXPR:
16082 tree decl, pattern_decl;
16083 tree init;
16085 pattern_decl = decl = DECL_EXPR_DECL (t);
16086 if (TREE_CODE (decl) == LABEL_DECL)
16087 finish_label_decl (DECL_NAME (decl));
16088 else if (TREE_CODE (decl) == USING_DECL)
16090 tree scope = USING_DECL_SCOPE (decl);
16091 tree name = DECL_NAME (decl);
16093 scope = tsubst (scope, args, complain, in_decl);
16094 decl = lookup_qualified_name (scope, name,
16095 /*is_type_p=*/false,
16096 /*complain=*/false);
16097 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16098 qualified_name_lookup_error (scope, name, decl, input_location);
16099 else
16100 finish_local_using_decl (decl, scope, name);
16102 else if (is_capture_proxy (decl)
16103 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16105 /* We're in tsubst_lambda_expr, we've already inserted a new
16106 capture proxy, so look it up and register it. */
16107 tree inst;
16108 if (DECL_PACK_P (decl))
16110 inst = (retrieve_local_specialization
16111 (DECL_CAPTURED_VARIABLE (decl)));
16112 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16114 else
16116 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16117 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16118 gcc_assert (inst != decl && is_capture_proxy (inst));
16120 register_local_specialization (inst, decl);
16121 break;
16123 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16124 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16125 /* Don't copy the old closure; we'll create a new one in
16126 tsubst_lambda_expr. */
16127 break;
16128 else
16130 init = DECL_INITIAL (decl);
16131 decl = tsubst (decl, args, complain, in_decl);
16132 if (decl != error_mark_node)
16134 /* By marking the declaration as instantiated, we avoid
16135 trying to instantiate it. Since instantiate_decl can't
16136 handle local variables, and since we've already done
16137 all that needs to be done, that's the right thing to
16138 do. */
16139 if (VAR_P (decl))
16140 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16141 if (VAR_P (decl)
16142 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16143 /* Anonymous aggregates are a special case. */
16144 finish_anon_union (decl);
16145 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16147 DECL_CONTEXT (decl) = current_function_decl;
16148 if (DECL_NAME (decl) == this_identifier)
16150 tree lam = DECL_CONTEXT (current_function_decl);
16151 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16152 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16154 insert_capture_proxy (decl);
16156 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16157 /* We already did a pushtag. */;
16158 else if (TREE_CODE (decl) == FUNCTION_DECL
16159 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16160 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16162 DECL_CONTEXT (decl) = NULL_TREE;
16163 pushdecl (decl);
16164 DECL_CONTEXT (decl) = current_function_decl;
16165 cp_check_omp_declare_reduction (decl);
16167 else
16169 int const_init = false;
16170 maybe_push_decl (decl);
16171 if (VAR_P (decl)
16172 && DECL_PRETTY_FUNCTION_P (decl))
16174 /* For __PRETTY_FUNCTION__ we have to adjust the
16175 initializer. */
16176 const char *const name
16177 = cxx_printable_name (current_function_decl, 2);
16178 init = cp_fname_init (name, &TREE_TYPE (decl));
16180 else
16181 init = tsubst_init (init, decl, args, complain, in_decl);
16183 if (VAR_P (decl))
16184 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16185 (pattern_decl));
16186 if (VAR_P (decl)
16187 && DECL_DECOMPOSITION_P (decl)
16188 && TREE_TYPE (pattern_decl) != error_mark_node)
16190 unsigned int cnt;
16191 tree first;
16192 tree ndecl
16193 = tsubst_decomp_names (decl, pattern_decl, args,
16194 complain, in_decl, &first, &cnt);
16195 if (ndecl != error_mark_node)
16196 cp_maybe_mangle_decomp (ndecl, first, cnt);
16197 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16198 if (ndecl != error_mark_node)
16199 cp_finish_decomp (ndecl, first, cnt);
16201 else
16202 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16207 break;
16210 case FOR_STMT:
16211 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16212 RECUR (FOR_INIT_STMT (t));
16213 finish_init_stmt (stmt);
16214 tmp = RECUR (FOR_COND (t));
16215 finish_for_cond (tmp, stmt, false, 0);
16216 tmp = RECUR (FOR_EXPR (t));
16217 finish_for_expr (tmp, stmt);
16219 bool prev = note_iteration_stmt_body_start ();
16220 RECUR (FOR_BODY (t));
16221 note_iteration_stmt_body_end (prev);
16223 finish_for_stmt (stmt);
16224 break;
16226 case RANGE_FOR_STMT:
16228 /* Construct another range_for, if this is not a final
16229 substitution (for inside inside a generic lambda of a
16230 template). Otherwise convert to a regular for. */
16231 tree decl, expr;
16232 stmt = (processing_template_decl
16233 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16234 : begin_for_stmt (NULL_TREE, NULL_TREE));
16235 decl = RANGE_FOR_DECL (t);
16236 decl = tsubst (decl, args, complain, in_decl);
16237 maybe_push_decl (decl);
16238 expr = RECUR (RANGE_FOR_EXPR (t));
16240 tree decomp_first = NULL_TREE;
16241 unsigned decomp_cnt = 0;
16242 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16243 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16244 complain, in_decl,
16245 &decomp_first, &decomp_cnt);
16247 if (processing_template_decl)
16249 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16250 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16251 finish_range_for_decl (stmt, decl, expr);
16253 else
16255 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16256 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16257 stmt = cp_convert_range_for (stmt, decl, expr,
16258 decomp_first, decomp_cnt,
16259 RANGE_FOR_IVDEP (t), unroll);
16262 bool prev = note_iteration_stmt_body_start ();
16263 RECUR (RANGE_FOR_BODY (t));
16264 note_iteration_stmt_body_end (prev);
16265 finish_for_stmt (stmt);
16267 break;
16269 case WHILE_STMT:
16270 stmt = begin_while_stmt ();
16271 tmp = RECUR (WHILE_COND (t));
16272 finish_while_stmt_cond (tmp, stmt, false, 0);
16274 bool prev = note_iteration_stmt_body_start ();
16275 RECUR (WHILE_BODY (t));
16276 note_iteration_stmt_body_end (prev);
16278 finish_while_stmt (stmt);
16279 break;
16281 case DO_STMT:
16282 stmt = begin_do_stmt ();
16284 bool prev = note_iteration_stmt_body_start ();
16285 RECUR (DO_BODY (t));
16286 note_iteration_stmt_body_end (prev);
16288 finish_do_body (stmt);
16289 tmp = RECUR (DO_COND (t));
16290 finish_do_stmt (tmp, stmt, false, 0);
16291 break;
16293 case IF_STMT:
16294 stmt = begin_if_stmt ();
16295 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16296 tmp = RECUR (IF_COND (t));
16297 tmp = finish_if_stmt_cond (tmp, stmt);
16298 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16299 /* Don't instantiate the THEN_CLAUSE. */;
16300 else
16302 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16303 if (inhibit)
16304 ++c_inhibit_evaluation_warnings;
16305 RECUR (THEN_CLAUSE (t));
16306 if (inhibit)
16307 --c_inhibit_evaluation_warnings;
16309 finish_then_clause (stmt);
16311 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16312 /* Don't instantiate the ELSE_CLAUSE. */;
16313 else if (ELSE_CLAUSE (t))
16315 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16316 begin_else_clause (stmt);
16317 if (inhibit)
16318 ++c_inhibit_evaluation_warnings;
16319 RECUR (ELSE_CLAUSE (t));
16320 if (inhibit)
16321 --c_inhibit_evaluation_warnings;
16322 finish_else_clause (stmt);
16325 finish_if_stmt (stmt);
16326 break;
16328 case BIND_EXPR:
16329 if (BIND_EXPR_BODY_BLOCK (t))
16330 stmt = begin_function_body ();
16331 else
16332 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16333 ? BCS_TRY_BLOCK : 0);
16335 RECUR (BIND_EXPR_BODY (t));
16337 if (BIND_EXPR_BODY_BLOCK (t))
16338 finish_function_body (stmt);
16339 else
16340 finish_compound_stmt (stmt);
16341 break;
16343 case BREAK_STMT:
16344 finish_break_stmt ();
16345 break;
16347 case CONTINUE_STMT:
16348 finish_continue_stmt ();
16349 break;
16351 case SWITCH_STMT:
16352 stmt = begin_switch_stmt ();
16353 tmp = RECUR (SWITCH_STMT_COND (t));
16354 finish_switch_cond (tmp, stmt);
16355 RECUR (SWITCH_STMT_BODY (t));
16356 finish_switch_stmt (stmt);
16357 break;
16359 case CASE_LABEL_EXPR:
16361 tree low = RECUR (CASE_LOW (t));
16362 tree high = RECUR (CASE_HIGH (t));
16363 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16364 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16365 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16366 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16368 break;
16370 case LABEL_EXPR:
16372 tree decl = LABEL_EXPR_LABEL (t);
16373 tree label;
16375 label = finish_label_stmt (DECL_NAME (decl));
16376 if (TREE_CODE (label) == LABEL_DECL)
16377 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16378 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16379 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16381 break;
16383 case GOTO_EXPR:
16384 tmp = GOTO_DESTINATION (t);
16385 if (TREE_CODE (tmp) != LABEL_DECL)
16386 /* Computed goto's must be tsubst'd into. On the other hand,
16387 non-computed gotos must not be; the identifier in question
16388 will have no binding. */
16389 tmp = RECUR (tmp);
16390 else
16391 tmp = DECL_NAME (tmp);
16392 finish_goto_stmt (tmp);
16393 break;
16395 case ASM_EXPR:
16397 tree string = RECUR (ASM_STRING (t));
16398 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16399 complain, in_decl);
16400 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16401 complain, in_decl);
16402 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16403 complain, in_decl);
16404 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16405 complain, in_decl);
16406 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16407 clobbers, labels);
16408 tree asm_expr = tmp;
16409 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16410 asm_expr = TREE_OPERAND (asm_expr, 0);
16411 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16413 break;
16415 case TRY_BLOCK:
16416 if (CLEANUP_P (t))
16418 stmt = begin_try_block ();
16419 RECUR (TRY_STMTS (t));
16420 finish_cleanup_try_block (stmt);
16421 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16423 else
16425 tree compound_stmt = NULL_TREE;
16427 if (FN_TRY_BLOCK_P (t))
16428 stmt = begin_function_try_block (&compound_stmt);
16429 else
16430 stmt = begin_try_block ();
16432 RECUR (TRY_STMTS (t));
16434 if (FN_TRY_BLOCK_P (t))
16435 finish_function_try_block (stmt);
16436 else
16437 finish_try_block (stmt);
16439 RECUR (TRY_HANDLERS (t));
16440 if (FN_TRY_BLOCK_P (t))
16441 finish_function_handler_sequence (stmt, compound_stmt);
16442 else
16443 finish_handler_sequence (stmt);
16445 break;
16447 case HANDLER:
16449 tree decl = HANDLER_PARMS (t);
16451 if (decl)
16453 decl = tsubst (decl, args, complain, in_decl);
16454 /* Prevent instantiate_decl from trying to instantiate
16455 this variable. We've already done all that needs to be
16456 done. */
16457 if (decl != error_mark_node)
16458 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16460 stmt = begin_handler ();
16461 finish_handler_parms (decl, stmt);
16462 RECUR (HANDLER_BODY (t));
16463 finish_handler (stmt);
16465 break;
16467 case TAG_DEFN:
16468 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16469 if (CLASS_TYPE_P (tmp))
16471 /* Local classes are not independent templates; they are
16472 instantiated along with their containing function. And this
16473 way we don't have to deal with pushing out of one local class
16474 to instantiate a member of another local class. */
16475 /* Closures are handled by the LAMBDA_EXPR. */
16476 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16477 complete_type (tmp);
16478 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16479 if ((VAR_P (fld)
16480 || (TREE_CODE (fld) == FUNCTION_DECL
16481 && !DECL_ARTIFICIAL (fld)))
16482 && DECL_TEMPLATE_INSTANTIATION (fld))
16483 instantiate_decl (fld, /*defer_ok=*/false,
16484 /*expl_inst_class=*/false);
16486 break;
16488 case STATIC_ASSERT:
16490 tree condition;
16492 ++c_inhibit_evaluation_warnings;
16493 condition =
16494 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16495 args,
16496 complain, in_decl,
16497 /*integral_constant_expression_p=*/true);
16498 --c_inhibit_evaluation_warnings;
16500 finish_static_assert (condition,
16501 STATIC_ASSERT_MESSAGE (t),
16502 STATIC_ASSERT_SOURCE_LOCATION (t),
16503 /*member_p=*/false);
16505 break;
16507 case OACC_KERNELS:
16508 case OACC_PARALLEL:
16509 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16510 in_decl);
16511 stmt = begin_omp_parallel ();
16512 RECUR (OMP_BODY (t));
16513 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16514 break;
16516 case OMP_PARALLEL:
16517 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16518 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16519 complain, in_decl);
16520 if (OMP_PARALLEL_COMBINED (t))
16521 omp_parallel_combined_clauses = &tmp;
16522 stmt = begin_omp_parallel ();
16523 RECUR (OMP_PARALLEL_BODY (t));
16524 gcc_assert (omp_parallel_combined_clauses == NULL);
16525 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16526 = OMP_PARALLEL_COMBINED (t);
16527 pop_omp_privatization_clauses (r);
16528 break;
16530 case OMP_TASK:
16531 r = push_omp_privatization_clauses (false);
16532 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16533 complain, in_decl);
16534 stmt = begin_omp_task ();
16535 RECUR (OMP_TASK_BODY (t));
16536 finish_omp_task (tmp, stmt);
16537 pop_omp_privatization_clauses (r);
16538 break;
16540 case OMP_FOR:
16541 case OMP_SIMD:
16542 case OMP_DISTRIBUTE:
16543 case OMP_TASKLOOP:
16544 case OACC_LOOP:
16546 tree clauses, body, pre_body;
16547 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16548 tree orig_declv = NULL_TREE;
16549 tree incrv = NULL_TREE;
16550 enum c_omp_region_type ort = C_ORT_OMP;
16551 int i;
16553 if (TREE_CODE (t) == OACC_LOOP)
16554 ort = C_ORT_ACC;
16556 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16557 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16558 in_decl);
16559 if (OMP_FOR_INIT (t) != NULL_TREE)
16561 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16562 if (OMP_FOR_ORIG_DECLS (t))
16563 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16564 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16565 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16566 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16569 stmt = begin_omp_structured_block ();
16571 pre_body = push_stmt_list ();
16572 RECUR (OMP_FOR_PRE_BODY (t));
16573 pre_body = pop_stmt_list (pre_body);
16575 if (OMP_FOR_INIT (t) != NULL_TREE)
16576 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16577 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16578 incrv, &clauses, args, complain, in_decl,
16579 integral_constant_expression_p);
16580 omp_parallel_combined_clauses = NULL;
16582 body = push_stmt_list ();
16583 RECUR (OMP_FOR_BODY (t));
16584 body = pop_stmt_list (body);
16586 if (OMP_FOR_INIT (t) != NULL_TREE)
16587 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16588 orig_declv, initv, condv, incrv, body, pre_body,
16589 NULL, clauses);
16590 else
16592 t = make_node (TREE_CODE (t));
16593 TREE_TYPE (t) = void_type_node;
16594 OMP_FOR_BODY (t) = body;
16595 OMP_FOR_PRE_BODY (t) = pre_body;
16596 OMP_FOR_CLAUSES (t) = clauses;
16597 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16598 add_stmt (t);
16601 add_stmt (finish_omp_structured_block (stmt));
16602 pop_omp_privatization_clauses (r);
16604 break;
16606 case OMP_SECTIONS:
16607 omp_parallel_combined_clauses = NULL;
16608 /* FALLTHRU */
16609 case OMP_SINGLE:
16610 case OMP_TEAMS:
16611 case OMP_CRITICAL:
16612 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16613 && OMP_TEAMS_COMBINED (t));
16614 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16615 in_decl);
16616 stmt = push_stmt_list ();
16617 RECUR (OMP_BODY (t));
16618 stmt = pop_stmt_list (stmt);
16620 t = copy_node (t);
16621 OMP_BODY (t) = stmt;
16622 OMP_CLAUSES (t) = tmp;
16623 add_stmt (t);
16624 pop_omp_privatization_clauses (r);
16625 break;
16627 case OACC_DATA:
16628 case OMP_TARGET_DATA:
16629 case OMP_TARGET:
16630 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16631 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16632 in_decl);
16633 keep_next_level (true);
16634 stmt = begin_omp_structured_block ();
16636 RECUR (OMP_BODY (t));
16637 stmt = finish_omp_structured_block (stmt);
16639 t = copy_node (t);
16640 OMP_BODY (t) = stmt;
16641 OMP_CLAUSES (t) = tmp;
16642 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16644 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16645 if (teams)
16647 /* For combined target teams, ensure the num_teams and
16648 thread_limit clause expressions are evaluated on the host,
16649 before entering the target construct. */
16650 tree c;
16651 for (c = OMP_TEAMS_CLAUSES (teams);
16652 c; c = OMP_CLAUSE_CHAIN (c))
16653 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16654 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16655 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16657 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16658 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16659 if (expr == error_mark_node)
16660 continue;
16661 tmp = TARGET_EXPR_SLOT (expr);
16662 add_stmt (expr);
16663 OMP_CLAUSE_OPERAND (c, 0) = expr;
16664 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16665 OMP_CLAUSE_FIRSTPRIVATE);
16666 OMP_CLAUSE_DECL (tc) = tmp;
16667 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16668 OMP_TARGET_CLAUSES (t) = tc;
16672 add_stmt (t);
16673 break;
16675 case OACC_DECLARE:
16676 t = copy_node (t);
16677 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16678 complain, in_decl);
16679 OACC_DECLARE_CLAUSES (t) = tmp;
16680 add_stmt (t);
16681 break;
16683 case OMP_TARGET_UPDATE:
16684 case OMP_TARGET_ENTER_DATA:
16685 case OMP_TARGET_EXIT_DATA:
16686 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16687 complain, in_decl);
16688 t = copy_node (t);
16689 OMP_STANDALONE_CLAUSES (t) = tmp;
16690 add_stmt (t);
16691 break;
16693 case OACC_ENTER_DATA:
16694 case OACC_EXIT_DATA:
16695 case OACC_UPDATE:
16696 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16697 complain, in_decl);
16698 t = copy_node (t);
16699 OMP_STANDALONE_CLAUSES (t) = tmp;
16700 add_stmt (t);
16701 break;
16703 case OMP_ORDERED:
16704 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16705 complain, in_decl);
16706 stmt = push_stmt_list ();
16707 RECUR (OMP_BODY (t));
16708 stmt = pop_stmt_list (stmt);
16710 t = copy_node (t);
16711 OMP_BODY (t) = stmt;
16712 OMP_ORDERED_CLAUSES (t) = tmp;
16713 add_stmt (t);
16714 break;
16716 case OMP_SECTION:
16717 case OMP_MASTER:
16718 case OMP_TASKGROUP:
16719 stmt = push_stmt_list ();
16720 RECUR (OMP_BODY (t));
16721 stmt = pop_stmt_list (stmt);
16723 t = copy_node (t);
16724 OMP_BODY (t) = stmt;
16725 add_stmt (t);
16726 break;
16728 case OMP_ATOMIC:
16729 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16730 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16732 tree op1 = TREE_OPERAND (t, 1);
16733 tree rhs1 = NULL_TREE;
16734 tree lhs, rhs;
16735 if (TREE_CODE (op1) == COMPOUND_EXPR)
16737 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16738 op1 = TREE_OPERAND (op1, 1);
16740 lhs = RECUR (TREE_OPERAND (op1, 0));
16741 rhs = RECUR (TREE_OPERAND (op1, 1));
16742 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16743 NULL_TREE, NULL_TREE, rhs1,
16744 OMP_ATOMIC_SEQ_CST (t));
16746 else
16748 tree op1 = TREE_OPERAND (t, 1);
16749 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16750 tree rhs1 = NULL_TREE;
16751 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16752 enum tree_code opcode = NOP_EXPR;
16753 if (code == OMP_ATOMIC_READ)
16755 v = RECUR (TREE_OPERAND (op1, 0));
16756 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16758 else if (code == OMP_ATOMIC_CAPTURE_OLD
16759 || code == OMP_ATOMIC_CAPTURE_NEW)
16761 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16762 v = RECUR (TREE_OPERAND (op1, 0));
16763 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16764 if (TREE_CODE (op11) == COMPOUND_EXPR)
16766 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16767 op11 = TREE_OPERAND (op11, 1);
16769 lhs = RECUR (TREE_OPERAND (op11, 0));
16770 rhs = RECUR (TREE_OPERAND (op11, 1));
16771 opcode = TREE_CODE (op11);
16772 if (opcode == MODIFY_EXPR)
16773 opcode = NOP_EXPR;
16775 else
16777 code = OMP_ATOMIC;
16778 lhs = RECUR (TREE_OPERAND (op1, 0));
16779 rhs = RECUR (TREE_OPERAND (op1, 1));
16781 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16782 OMP_ATOMIC_SEQ_CST (t));
16784 break;
16786 case TRANSACTION_EXPR:
16788 int flags = 0;
16789 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16790 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16792 if (TRANSACTION_EXPR_IS_STMT (t))
16794 tree body = TRANSACTION_EXPR_BODY (t);
16795 tree noex = NULL_TREE;
16796 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16798 noex = MUST_NOT_THROW_COND (body);
16799 if (noex == NULL_TREE)
16800 noex = boolean_true_node;
16801 body = TREE_OPERAND (body, 0);
16803 stmt = begin_transaction_stmt (input_location, NULL, flags);
16804 RECUR (body);
16805 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16807 else
16809 stmt = build_transaction_expr (EXPR_LOCATION (t),
16810 RECUR (TRANSACTION_EXPR_BODY (t)),
16811 flags, NULL_TREE);
16812 RETURN (stmt);
16815 break;
16817 case MUST_NOT_THROW_EXPR:
16819 tree op0 = RECUR (TREE_OPERAND (t, 0));
16820 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16821 RETURN (build_must_not_throw_expr (op0, cond));
16824 case EXPR_PACK_EXPANSION:
16825 error ("invalid use of pack expansion expression");
16826 RETURN (error_mark_node);
16828 case NONTYPE_ARGUMENT_PACK:
16829 error ("use %<...%> to expand argument pack");
16830 RETURN (error_mark_node);
16832 case COMPOUND_EXPR:
16833 tmp = RECUR (TREE_OPERAND (t, 0));
16834 if (tmp == NULL_TREE)
16835 /* If the first operand was a statement, we're done with it. */
16836 RETURN (RECUR (TREE_OPERAND (t, 1)));
16837 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16838 RECUR (TREE_OPERAND (t, 1)),
16839 complain));
16841 case ANNOTATE_EXPR:
16842 tmp = RECUR (TREE_OPERAND (t, 0));
16843 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16844 TREE_TYPE (tmp), tmp,
16845 RECUR (TREE_OPERAND (t, 1)),
16846 RECUR (TREE_OPERAND (t, 2))));
16848 default:
16849 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16851 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16852 /*function_p=*/false,
16853 integral_constant_expression_p));
16856 RETURN (NULL_TREE);
16857 out:
16858 input_location = loc;
16859 return r;
16860 #undef RECUR
16861 #undef RETURN
16864 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16865 function. For description of the body see comment above
16866 cp_parser_omp_declare_reduction_exprs. */
16868 static void
16869 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16871 if (t == NULL_TREE || t == error_mark_node)
16872 return;
16874 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16876 tree_stmt_iterator tsi;
16877 int i;
16878 tree stmts[7];
16879 memset (stmts, 0, sizeof stmts);
16880 for (i = 0, tsi = tsi_start (t);
16881 i < 7 && !tsi_end_p (tsi);
16882 i++, tsi_next (&tsi))
16883 stmts[i] = tsi_stmt (tsi);
16884 gcc_assert (tsi_end_p (tsi));
16886 if (i >= 3)
16888 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16889 && TREE_CODE (stmts[1]) == DECL_EXPR);
16890 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16891 args, complain, in_decl);
16892 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16893 args, complain, in_decl);
16894 DECL_CONTEXT (omp_out) = current_function_decl;
16895 DECL_CONTEXT (omp_in) = current_function_decl;
16896 keep_next_level (true);
16897 tree block = begin_omp_structured_block ();
16898 tsubst_expr (stmts[2], args, complain, in_decl, false);
16899 block = finish_omp_structured_block (block);
16900 block = maybe_cleanup_point_expr_void (block);
16901 add_decl_expr (omp_out);
16902 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16903 TREE_NO_WARNING (omp_out) = 1;
16904 add_decl_expr (omp_in);
16905 finish_expr_stmt (block);
16907 if (i >= 6)
16909 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16910 && TREE_CODE (stmts[4]) == DECL_EXPR);
16911 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16912 args, complain, in_decl);
16913 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16914 args, complain, in_decl);
16915 DECL_CONTEXT (omp_priv) = current_function_decl;
16916 DECL_CONTEXT (omp_orig) = current_function_decl;
16917 keep_next_level (true);
16918 tree block = begin_omp_structured_block ();
16919 tsubst_expr (stmts[5], args, complain, in_decl, false);
16920 block = finish_omp_structured_block (block);
16921 block = maybe_cleanup_point_expr_void (block);
16922 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16923 add_decl_expr (omp_priv);
16924 add_decl_expr (omp_orig);
16925 finish_expr_stmt (block);
16926 if (i == 7)
16927 add_decl_expr (omp_orig);
16931 /* T is a postfix-expression that is not being used in a function
16932 call. Return the substituted version of T. */
16934 static tree
16935 tsubst_non_call_postfix_expression (tree t, tree args,
16936 tsubst_flags_t complain,
16937 tree in_decl)
16939 if (TREE_CODE (t) == SCOPE_REF)
16940 t = tsubst_qualified_id (t, args, complain, in_decl,
16941 /*done=*/false, /*address_p=*/false);
16942 else
16943 t = tsubst_copy_and_build (t, args, complain, in_decl,
16944 /*function_p=*/false,
16945 /*integral_constant_expression_p=*/false);
16947 return t;
16950 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16951 instantiation context. Instantiating a pack expansion containing a lambda
16952 might result in multiple lambdas all based on the same lambda in the
16953 template. */
16955 tree
16956 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16958 tree oldfn = lambda_function (t);
16959 in_decl = oldfn;
16961 tree r = build_lambda_expr ();
16963 LAMBDA_EXPR_LOCATION (r)
16964 = LAMBDA_EXPR_LOCATION (t);
16965 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16966 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16967 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16969 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16970 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16971 else
16972 record_lambda_scope (r);
16974 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16975 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16977 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16978 cap = TREE_CHAIN (cap))
16980 tree field = TREE_PURPOSE (cap);
16981 if (PACK_EXPANSION_P (field))
16982 field = PACK_EXPANSION_PATTERN (field);
16983 field = tsubst_decl (field, args, complain);
16985 if (field == error_mark_node)
16986 return error_mark_node;
16988 tree init = TREE_VALUE (cap);
16989 if (PACK_EXPANSION_P (init))
16990 init = tsubst_pack_expansion (init, args, complain, in_decl);
16991 else
16992 init = tsubst_copy_and_build (init, args, complain, in_decl,
16993 /*fn*/false, /*constexpr*/false);
16995 if (TREE_CODE (field) == TREE_VEC)
16997 int len = TREE_VEC_LENGTH (field);
16998 gcc_assert (TREE_CODE (init) == TREE_VEC
16999 && TREE_VEC_LENGTH (init) == len);
17000 for (int i = 0; i < len; ++i)
17001 LAMBDA_EXPR_CAPTURE_LIST (r)
17002 = tree_cons (TREE_VEC_ELT (field, i),
17003 TREE_VEC_ELT (init, i),
17004 LAMBDA_EXPR_CAPTURE_LIST (r));
17006 else
17008 LAMBDA_EXPR_CAPTURE_LIST (r)
17009 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17011 if (id_equal (DECL_NAME (field), "__this"))
17012 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17016 tree type = begin_lambda_type (r);
17018 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17019 determine_visibility (TYPE_NAME (type));
17021 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17023 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17024 ? DECL_TI_TEMPLATE (oldfn)
17025 : NULL_TREE);
17027 tree fntype = static_fn_type (oldfn);
17028 if (oldtmpl)
17029 ++processing_template_decl;
17030 fntype = tsubst (fntype, args, complain, in_decl);
17031 if (oldtmpl)
17032 --processing_template_decl;
17034 if (fntype == error_mark_node)
17035 r = error_mark_node;
17036 else
17038 /* Fix the type of 'this'. */
17039 fntype = build_memfn_type (fntype, type,
17040 type_memfn_quals (fntype),
17041 type_memfn_rqual (fntype));
17042 tree fn, tmpl;
17043 if (oldtmpl)
17045 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17046 fn = DECL_TEMPLATE_RESULT (tmpl);
17047 finish_member_declaration (tmpl);
17049 else
17051 tmpl = NULL_TREE;
17052 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17053 finish_member_declaration (fn);
17056 /* Let finish_function set this. */
17057 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17059 bool nested = cfun;
17060 if (nested)
17061 push_function_context ();
17063 local_specialization_stack s (lss_copy);
17065 tree body = start_lambda_function (fn, r);
17067 register_parameter_specializations (oldfn, fn);
17069 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17070 /*constexpr*/false);
17072 finish_lambda_function (body);
17074 if (nested)
17075 pop_function_context ();
17077 /* The capture list was built up in reverse order; fix that now. */
17078 LAMBDA_EXPR_CAPTURE_LIST (r)
17079 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17081 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17083 maybe_add_lambda_conv_op (type);
17086 finish_struct (type, /*attr*/NULL_TREE);
17088 insert_pending_capture_proxies ();
17090 return r;
17093 /* Like tsubst but deals with expressions and performs semantic
17094 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17096 tree
17097 tsubst_copy_and_build (tree t,
17098 tree args,
17099 tsubst_flags_t complain,
17100 tree in_decl,
17101 bool function_p,
17102 bool integral_constant_expression_p)
17104 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17105 #define RECUR(NODE) \
17106 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17107 /*function_p=*/false, \
17108 integral_constant_expression_p)
17110 tree retval, op1;
17111 location_t loc;
17113 if (t == NULL_TREE || t == error_mark_node)
17114 return t;
17116 loc = input_location;
17117 if (EXPR_HAS_LOCATION (t))
17118 input_location = EXPR_LOCATION (t);
17120 /* N3276 decltype magic only applies to calls at the top level or on the
17121 right side of a comma. */
17122 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17123 complain &= ~tf_decltype;
17125 switch (TREE_CODE (t))
17127 case USING_DECL:
17128 t = DECL_NAME (t);
17129 /* Fall through. */
17130 case IDENTIFIER_NODE:
17132 tree decl;
17133 cp_id_kind idk;
17134 bool non_integral_constant_expression_p;
17135 const char *error_msg;
17137 if (IDENTIFIER_CONV_OP_P (t))
17139 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17140 t = make_conv_op_name (new_type);
17143 /* Look up the name. */
17144 decl = lookup_name (t);
17146 /* By convention, expressions use ERROR_MARK_NODE to indicate
17147 failure, not NULL_TREE. */
17148 if (decl == NULL_TREE)
17149 decl = error_mark_node;
17151 decl = finish_id_expression (t, decl, NULL_TREE,
17152 &idk,
17153 integral_constant_expression_p,
17154 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17155 &non_integral_constant_expression_p,
17156 /*template_p=*/false,
17157 /*done=*/true,
17158 /*address_p=*/false,
17159 /*template_arg_p=*/false,
17160 &error_msg,
17161 input_location);
17162 if (error_msg)
17163 error (error_msg);
17164 if (!function_p && identifier_p (decl))
17166 if (complain & tf_error)
17167 unqualified_name_lookup_error (decl);
17168 decl = error_mark_node;
17170 RETURN (decl);
17173 case TEMPLATE_ID_EXPR:
17175 tree object;
17176 tree templ = RECUR (TREE_OPERAND (t, 0));
17177 tree targs = TREE_OPERAND (t, 1);
17179 if (targs)
17180 targs = tsubst_template_args (targs, args, complain, in_decl);
17181 if (targs == error_mark_node)
17182 return error_mark_node;
17184 if (TREE_CODE (templ) == SCOPE_REF)
17186 tree name = TREE_OPERAND (templ, 1);
17187 tree tid = lookup_template_function (name, targs);
17188 TREE_OPERAND (templ, 1) = tid;
17189 return templ;
17192 if (variable_template_p (templ))
17193 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17195 if (TREE_CODE (templ) == COMPONENT_REF)
17197 object = TREE_OPERAND (templ, 0);
17198 templ = TREE_OPERAND (templ, 1);
17200 else
17201 object = NULL_TREE;
17202 templ = lookup_template_function (templ, targs);
17204 if (object)
17205 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17206 object, templ, NULL_TREE));
17207 else
17208 RETURN (baselink_for_fns (templ));
17211 case INDIRECT_REF:
17213 tree r = RECUR (TREE_OPERAND (t, 0));
17215 if (REFERENCE_REF_P (t))
17217 /* A type conversion to reference type will be enclosed in
17218 such an indirect ref, but the substitution of the cast
17219 will have also added such an indirect ref. */
17220 r = convert_from_reference (r);
17222 else
17223 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17224 complain|decltype_flag);
17226 if (REF_PARENTHESIZED_P (t))
17227 r = force_paren_expr (r);
17229 RETURN (r);
17232 case NOP_EXPR:
17234 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17235 tree op0 = RECUR (TREE_OPERAND (t, 0));
17236 RETURN (build_nop (type, op0));
17239 case IMPLICIT_CONV_EXPR:
17241 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17242 tree expr = RECUR (TREE_OPERAND (t, 0));
17243 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17245 retval = copy_node (t);
17246 TREE_TYPE (retval) = type;
17247 TREE_OPERAND (retval, 0) = expr;
17248 RETURN (retval);
17250 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17251 /* We'll pass this to convert_nontype_argument again, we don't need
17252 to actually perform any conversion here. */
17253 RETURN (expr);
17254 int flags = LOOKUP_IMPLICIT;
17255 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17256 flags = LOOKUP_NORMAL;
17257 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17258 flags));
17261 case CONVERT_EXPR:
17263 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17264 tree op0 = RECUR (TREE_OPERAND (t, 0));
17265 RETURN (build1 (CONVERT_EXPR, type, op0));
17268 case CAST_EXPR:
17269 case REINTERPRET_CAST_EXPR:
17270 case CONST_CAST_EXPR:
17271 case DYNAMIC_CAST_EXPR:
17272 case STATIC_CAST_EXPR:
17274 tree type;
17275 tree op, r = NULL_TREE;
17277 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17278 if (integral_constant_expression_p
17279 && !cast_valid_in_integral_constant_expression_p (type))
17281 if (complain & tf_error)
17282 error ("a cast to a type other than an integral or "
17283 "enumeration type cannot appear in a constant-expression");
17284 RETURN (error_mark_node);
17287 op = RECUR (TREE_OPERAND (t, 0));
17289 warning_sentinel s(warn_useless_cast);
17290 warning_sentinel s2(warn_ignored_qualifiers);
17291 switch (TREE_CODE (t))
17293 case CAST_EXPR:
17294 r = build_functional_cast (type, op, complain);
17295 break;
17296 case REINTERPRET_CAST_EXPR:
17297 r = build_reinterpret_cast (type, op, complain);
17298 break;
17299 case CONST_CAST_EXPR:
17300 r = build_const_cast (type, op, complain);
17301 break;
17302 case DYNAMIC_CAST_EXPR:
17303 r = build_dynamic_cast (type, op, complain);
17304 break;
17305 case STATIC_CAST_EXPR:
17306 r = build_static_cast (type, op, complain);
17307 break;
17308 default:
17309 gcc_unreachable ();
17312 RETURN (r);
17315 case POSTDECREMENT_EXPR:
17316 case POSTINCREMENT_EXPR:
17317 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17318 args, complain, in_decl);
17319 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17320 complain|decltype_flag));
17322 case PREDECREMENT_EXPR:
17323 case PREINCREMENT_EXPR:
17324 case NEGATE_EXPR:
17325 case BIT_NOT_EXPR:
17326 case ABS_EXPR:
17327 case TRUTH_NOT_EXPR:
17328 case UNARY_PLUS_EXPR: /* Unary + */
17329 case REALPART_EXPR:
17330 case IMAGPART_EXPR:
17331 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17332 RECUR (TREE_OPERAND (t, 0)),
17333 complain|decltype_flag));
17335 case FIX_TRUNC_EXPR:
17336 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17337 false, complain));
17339 case ADDR_EXPR:
17340 op1 = TREE_OPERAND (t, 0);
17341 if (TREE_CODE (op1) == LABEL_DECL)
17342 RETURN (finish_label_address_expr (DECL_NAME (op1),
17343 EXPR_LOCATION (op1)));
17344 if (TREE_CODE (op1) == SCOPE_REF)
17345 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17346 /*done=*/true, /*address_p=*/true);
17347 else
17348 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17349 in_decl);
17350 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17351 complain|decltype_flag));
17353 case PLUS_EXPR:
17354 case MINUS_EXPR:
17355 case MULT_EXPR:
17356 case TRUNC_DIV_EXPR:
17357 case CEIL_DIV_EXPR:
17358 case FLOOR_DIV_EXPR:
17359 case ROUND_DIV_EXPR:
17360 case EXACT_DIV_EXPR:
17361 case BIT_AND_EXPR:
17362 case BIT_IOR_EXPR:
17363 case BIT_XOR_EXPR:
17364 case TRUNC_MOD_EXPR:
17365 case FLOOR_MOD_EXPR:
17366 case TRUTH_ANDIF_EXPR:
17367 case TRUTH_ORIF_EXPR:
17368 case TRUTH_AND_EXPR:
17369 case TRUTH_OR_EXPR:
17370 case RSHIFT_EXPR:
17371 case LSHIFT_EXPR:
17372 case RROTATE_EXPR:
17373 case LROTATE_EXPR:
17374 case EQ_EXPR:
17375 case NE_EXPR:
17376 case MAX_EXPR:
17377 case MIN_EXPR:
17378 case LE_EXPR:
17379 case GE_EXPR:
17380 case LT_EXPR:
17381 case GT_EXPR:
17382 case MEMBER_REF:
17383 case DOTSTAR_EXPR:
17385 warning_sentinel s1(warn_type_limits);
17386 warning_sentinel s2(warn_div_by_zero);
17387 warning_sentinel s3(warn_logical_op);
17388 warning_sentinel s4(warn_tautological_compare);
17389 tree op0 = RECUR (TREE_OPERAND (t, 0));
17390 tree op1 = RECUR (TREE_OPERAND (t, 1));
17391 tree r = build_x_binary_op
17392 (input_location, TREE_CODE (t),
17393 op0,
17394 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17395 ? ERROR_MARK
17396 : TREE_CODE (TREE_OPERAND (t, 0))),
17397 op1,
17398 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17399 ? ERROR_MARK
17400 : TREE_CODE (TREE_OPERAND (t, 1))),
17401 /*overload=*/NULL,
17402 complain|decltype_flag);
17403 if (EXPR_P (r) && TREE_NO_WARNING (t))
17404 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17406 RETURN (r);
17409 case POINTER_PLUS_EXPR:
17411 tree op0 = RECUR (TREE_OPERAND (t, 0));
17412 tree op1 = RECUR (TREE_OPERAND (t, 1));
17413 return fold_build_pointer_plus (op0, op1);
17416 case SCOPE_REF:
17417 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17418 /*address_p=*/false));
17419 case ARRAY_REF:
17420 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17421 args, complain, in_decl);
17422 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17423 RECUR (TREE_OPERAND (t, 1)),
17424 complain|decltype_flag));
17426 case SIZEOF_EXPR:
17427 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17428 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17429 RETURN (tsubst_copy (t, args, complain, in_decl));
17430 /* Fall through */
17432 case ALIGNOF_EXPR:
17434 tree r;
17436 op1 = TREE_OPERAND (t, 0);
17437 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17438 op1 = TREE_TYPE (op1);
17439 if (!args)
17441 /* When there are no ARGS, we are trying to evaluate a
17442 non-dependent expression from the parser. Trying to do
17443 the substitutions may not work. */
17444 if (!TYPE_P (op1))
17445 op1 = TREE_TYPE (op1);
17447 else
17449 ++cp_unevaluated_operand;
17450 ++c_inhibit_evaluation_warnings;
17451 if (TYPE_P (op1))
17452 op1 = tsubst (op1, args, complain, in_decl);
17453 else
17454 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17455 /*function_p=*/false,
17456 /*integral_constant_expression_p=*/
17457 false);
17458 --cp_unevaluated_operand;
17459 --c_inhibit_evaluation_warnings;
17461 if (TYPE_P (op1))
17462 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17463 complain & tf_error);
17464 else
17465 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17466 complain & tf_error);
17467 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17469 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17471 if (!processing_template_decl && TYPE_P (op1))
17473 r = build_min (SIZEOF_EXPR, size_type_node,
17474 build1 (NOP_EXPR, op1, error_mark_node));
17475 SIZEOF_EXPR_TYPE_P (r) = 1;
17477 else
17478 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17479 TREE_SIDE_EFFECTS (r) = 0;
17480 TREE_READONLY (r) = 1;
17482 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17484 RETURN (r);
17487 case AT_ENCODE_EXPR:
17489 op1 = TREE_OPERAND (t, 0);
17490 ++cp_unevaluated_operand;
17491 ++c_inhibit_evaluation_warnings;
17492 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17493 /*function_p=*/false,
17494 /*integral_constant_expression_p=*/false);
17495 --cp_unevaluated_operand;
17496 --c_inhibit_evaluation_warnings;
17497 RETURN (objc_build_encode_expr (op1));
17500 case NOEXCEPT_EXPR:
17501 op1 = TREE_OPERAND (t, 0);
17502 ++cp_unevaluated_operand;
17503 ++c_inhibit_evaluation_warnings;
17504 ++cp_noexcept_operand;
17505 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17506 /*function_p=*/false,
17507 /*integral_constant_expression_p=*/false);
17508 --cp_unevaluated_operand;
17509 --c_inhibit_evaluation_warnings;
17510 --cp_noexcept_operand;
17511 RETURN (finish_noexcept_expr (op1, complain));
17513 case MODOP_EXPR:
17515 warning_sentinel s(warn_div_by_zero);
17516 tree lhs = RECUR (TREE_OPERAND (t, 0));
17517 tree rhs = RECUR (TREE_OPERAND (t, 2));
17518 tree r = build_x_modify_expr
17519 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17520 complain|decltype_flag);
17521 /* TREE_NO_WARNING must be set if either the expression was
17522 parenthesized or it uses an operator such as >>= rather
17523 than plain assignment. In the former case, it was already
17524 set and must be copied. In the latter case,
17525 build_x_modify_expr sets it and it must not be reset
17526 here. */
17527 if (TREE_NO_WARNING (t))
17528 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17530 RETURN (r);
17533 case ARROW_EXPR:
17534 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17535 args, complain, in_decl);
17536 /* Remember that there was a reference to this entity. */
17537 if (DECL_P (op1)
17538 && !mark_used (op1, complain) && !(complain & tf_error))
17539 RETURN (error_mark_node);
17540 RETURN (build_x_arrow (input_location, op1, complain));
17542 case NEW_EXPR:
17544 tree placement = RECUR (TREE_OPERAND (t, 0));
17545 tree init = RECUR (TREE_OPERAND (t, 3));
17546 vec<tree, va_gc> *placement_vec;
17547 vec<tree, va_gc> *init_vec;
17548 tree ret;
17550 if (placement == NULL_TREE)
17551 placement_vec = NULL;
17552 else
17554 placement_vec = make_tree_vector ();
17555 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17556 vec_safe_push (placement_vec, TREE_VALUE (placement));
17559 /* If there was an initializer in the original tree, but it
17560 instantiated to an empty list, then we should pass a
17561 non-NULL empty vector to tell build_new that it was an
17562 empty initializer() rather than no initializer. This can
17563 only happen when the initializer is a pack expansion whose
17564 parameter packs are of length zero. */
17565 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17566 init_vec = NULL;
17567 else
17569 init_vec = make_tree_vector ();
17570 if (init == void_node)
17571 gcc_assert (init_vec != NULL);
17572 else
17574 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17575 vec_safe_push (init_vec, TREE_VALUE (init));
17579 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17580 tree op2 = RECUR (TREE_OPERAND (t, 2));
17581 ret = build_new (&placement_vec, op1, op2, &init_vec,
17582 NEW_EXPR_USE_GLOBAL (t),
17583 complain);
17585 if (placement_vec != NULL)
17586 release_tree_vector (placement_vec);
17587 if (init_vec != NULL)
17588 release_tree_vector (init_vec);
17590 RETURN (ret);
17593 case DELETE_EXPR:
17595 tree op0 = RECUR (TREE_OPERAND (t, 0));
17596 tree op1 = RECUR (TREE_OPERAND (t, 1));
17597 RETURN (delete_sanity (op0, op1,
17598 DELETE_EXPR_USE_VEC (t),
17599 DELETE_EXPR_USE_GLOBAL (t),
17600 complain));
17603 case COMPOUND_EXPR:
17605 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17606 complain & ~tf_decltype, in_decl,
17607 /*function_p=*/false,
17608 integral_constant_expression_p);
17609 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17610 op0,
17611 RECUR (TREE_OPERAND (t, 1)),
17612 complain|decltype_flag));
17615 case CALL_EXPR:
17617 tree function;
17618 vec<tree, va_gc> *call_args;
17619 unsigned int nargs, i;
17620 bool qualified_p;
17621 bool koenig_p;
17622 tree ret;
17624 function = CALL_EXPR_FN (t);
17625 /* Internal function with no arguments. */
17626 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17627 RETURN (t);
17629 /* When we parsed the expression, we determined whether or
17630 not Koenig lookup should be performed. */
17631 koenig_p = KOENIG_LOOKUP_P (t);
17632 if (function == NULL_TREE)
17634 koenig_p = false;
17635 qualified_p = false;
17637 else if (TREE_CODE (function) == SCOPE_REF)
17639 qualified_p = true;
17640 function = tsubst_qualified_id (function, args, complain, in_decl,
17641 /*done=*/false,
17642 /*address_p=*/false);
17644 else if (koenig_p && identifier_p (function))
17646 /* Do nothing; calling tsubst_copy_and_build on an identifier
17647 would incorrectly perform unqualified lookup again.
17649 Note that we can also have an IDENTIFIER_NODE if the earlier
17650 unqualified lookup found a member function; in that case
17651 koenig_p will be false and we do want to do the lookup
17652 again to find the instantiated member function.
17654 FIXME but doing that causes c++/15272, so we need to stop
17655 using IDENTIFIER_NODE in that situation. */
17656 qualified_p = false;
17658 else
17660 if (TREE_CODE (function) == COMPONENT_REF)
17662 tree op = TREE_OPERAND (function, 1);
17664 qualified_p = (TREE_CODE (op) == SCOPE_REF
17665 || (BASELINK_P (op)
17666 && BASELINK_QUALIFIED_P (op)));
17668 else
17669 qualified_p = false;
17671 if (TREE_CODE (function) == ADDR_EXPR
17672 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17673 /* Avoid error about taking the address of a constructor. */
17674 function = TREE_OPERAND (function, 0);
17676 function = tsubst_copy_and_build (function, args, complain,
17677 in_decl,
17678 !qualified_p,
17679 integral_constant_expression_p);
17681 if (BASELINK_P (function))
17682 qualified_p = true;
17685 nargs = call_expr_nargs (t);
17686 call_args = make_tree_vector ();
17687 for (i = 0; i < nargs; ++i)
17689 tree arg = CALL_EXPR_ARG (t, i);
17691 if (!PACK_EXPANSION_P (arg))
17692 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17693 else
17695 /* Expand the pack expansion and push each entry onto
17696 CALL_ARGS. */
17697 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17698 if (TREE_CODE (arg) == TREE_VEC)
17700 unsigned int len, j;
17702 len = TREE_VEC_LENGTH (arg);
17703 for (j = 0; j < len; ++j)
17705 tree value = TREE_VEC_ELT (arg, j);
17706 if (value != NULL_TREE)
17707 value = convert_from_reference (value);
17708 vec_safe_push (call_args, value);
17711 else
17713 /* A partial substitution. Add one entry. */
17714 vec_safe_push (call_args, arg);
17719 /* We do not perform argument-dependent lookup if normal
17720 lookup finds a non-function, in accordance with the
17721 expected resolution of DR 218. */
17722 if (koenig_p
17723 && ((is_overloaded_fn (function)
17724 /* If lookup found a member function, the Koenig lookup is
17725 not appropriate, even if an unqualified-name was used
17726 to denote the function. */
17727 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17728 || identifier_p (function))
17729 /* Only do this when substitution turns a dependent call
17730 into a non-dependent call. */
17731 && type_dependent_expression_p_push (t)
17732 && !any_type_dependent_arguments_p (call_args))
17733 function = perform_koenig_lookup (function, call_args, tf_none);
17735 if (function != NULL_TREE
17736 && identifier_p (function)
17737 && !any_type_dependent_arguments_p (call_args))
17739 if (koenig_p && (complain & tf_warning_or_error))
17741 /* For backwards compatibility and good diagnostics, try
17742 the unqualified lookup again if we aren't in SFINAE
17743 context. */
17744 tree unq = (tsubst_copy_and_build
17745 (function, args, complain, in_decl, true,
17746 integral_constant_expression_p));
17747 if (unq == error_mark_node)
17749 release_tree_vector (call_args);
17750 RETURN (error_mark_node);
17753 if (unq != function)
17755 /* In a lambda fn, we have to be careful to not
17756 introduce new this captures. Legacy code can't
17757 be using lambdas anyway, so it's ok to be
17758 stricter. */
17759 bool in_lambda = (current_class_type
17760 && LAMBDA_TYPE_P (current_class_type));
17761 char const *const msg
17762 = G_("%qD was not declared in this scope, "
17763 "and no declarations were found by "
17764 "argument-dependent lookup at the point "
17765 "of instantiation");
17767 bool diag = true;
17768 if (in_lambda)
17769 error_at (EXPR_LOC_OR_LOC (t, input_location),
17770 msg, function);
17771 else
17772 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17773 msg, function);
17774 if (diag)
17776 tree fn = unq;
17778 if (INDIRECT_REF_P (fn))
17779 fn = TREE_OPERAND (fn, 0);
17780 if (is_overloaded_fn (fn))
17781 fn = get_first_fn (fn);
17783 if (!DECL_P (fn))
17784 /* Can't say anything more. */;
17785 else if (DECL_CLASS_SCOPE_P (fn))
17787 location_t loc = EXPR_LOC_OR_LOC (t,
17788 input_location);
17789 inform (loc,
17790 "declarations in dependent base %qT are "
17791 "not found by unqualified lookup",
17792 DECL_CLASS_CONTEXT (fn));
17793 if (current_class_ptr)
17794 inform (loc,
17795 "use %<this->%D%> instead", function);
17796 else
17797 inform (loc,
17798 "use %<%T::%D%> instead",
17799 current_class_name, function);
17801 else
17802 inform (DECL_SOURCE_LOCATION (fn),
17803 "%qD declared here, later in the "
17804 "translation unit", fn);
17805 if (in_lambda)
17807 release_tree_vector (call_args);
17808 RETURN (error_mark_node);
17812 function = unq;
17815 if (identifier_p (function))
17817 if (complain & tf_error)
17818 unqualified_name_lookup_error (function);
17819 release_tree_vector (call_args);
17820 RETURN (error_mark_node);
17824 /* Remember that there was a reference to this entity. */
17825 if (function != NULL_TREE
17826 && DECL_P (function)
17827 && !mark_used (function, complain) && !(complain & tf_error))
17829 release_tree_vector (call_args);
17830 RETURN (error_mark_node);
17833 /* Put back tf_decltype for the actual call. */
17834 complain |= decltype_flag;
17836 if (function == NULL_TREE)
17837 switch (CALL_EXPR_IFN (t))
17839 case IFN_LAUNDER:
17840 gcc_assert (nargs == 1);
17841 if (vec_safe_length (call_args) != 1)
17843 error_at (EXPR_LOC_OR_LOC (t, input_location),
17844 "wrong number of arguments to "
17845 "%<__builtin_launder%>");
17846 ret = error_mark_node;
17848 else
17849 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17850 input_location),
17851 (*call_args)[0], complain);
17852 break;
17854 default:
17855 /* Unsupported internal function with arguments. */
17856 gcc_unreachable ();
17858 else if (TREE_CODE (function) == OFFSET_REF)
17859 ret = build_offset_ref_call_from_tree (function, &call_args,
17860 complain);
17861 else if (TREE_CODE (function) == COMPONENT_REF)
17863 tree instance = TREE_OPERAND (function, 0);
17864 tree fn = TREE_OPERAND (function, 1);
17866 if (processing_template_decl
17867 && (type_dependent_expression_p (instance)
17868 || (!BASELINK_P (fn)
17869 && TREE_CODE (fn) != FIELD_DECL)
17870 || type_dependent_expression_p (fn)
17871 || any_type_dependent_arguments_p (call_args)))
17872 ret = build_min_nt_call_vec (function, call_args);
17873 else if (!BASELINK_P (fn))
17874 ret = finish_call_expr (function, &call_args,
17875 /*disallow_virtual=*/false,
17876 /*koenig_p=*/false,
17877 complain);
17878 else
17879 ret = (build_new_method_call
17880 (instance, fn,
17881 &call_args, NULL_TREE,
17882 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17883 /*fn_p=*/NULL,
17884 complain));
17886 else
17887 ret = finish_call_expr (function, &call_args,
17888 /*disallow_virtual=*/qualified_p,
17889 koenig_p,
17890 complain);
17892 release_tree_vector (call_args);
17894 if (ret != error_mark_node)
17896 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17897 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17898 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17899 bool thk = CALL_FROM_THUNK_P (t);
17900 if (op || ord || rev || thk)
17902 function = extract_call_expr (ret);
17903 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17904 CALL_EXPR_ORDERED_ARGS (function) = ord;
17905 CALL_EXPR_REVERSE_ARGS (function) = rev;
17906 if (thk)
17908 if (TREE_CODE (function) == CALL_EXPR)
17909 CALL_FROM_THUNK_P (function) = true;
17910 else
17911 AGGR_INIT_FROM_THUNK_P (function) = true;
17912 /* The thunk location is not interesting. */
17913 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17918 RETURN (ret);
17921 case COND_EXPR:
17923 tree cond = RECUR (TREE_OPERAND (t, 0));
17924 tree folded_cond = fold_non_dependent_expr (cond);
17925 tree exp1, exp2;
17927 if (TREE_CODE (folded_cond) == INTEGER_CST)
17929 if (integer_zerop (folded_cond))
17931 ++c_inhibit_evaluation_warnings;
17932 exp1 = RECUR (TREE_OPERAND (t, 1));
17933 --c_inhibit_evaluation_warnings;
17934 exp2 = RECUR (TREE_OPERAND (t, 2));
17936 else
17938 exp1 = RECUR (TREE_OPERAND (t, 1));
17939 ++c_inhibit_evaluation_warnings;
17940 exp2 = RECUR (TREE_OPERAND (t, 2));
17941 --c_inhibit_evaluation_warnings;
17943 cond = folded_cond;
17945 else
17947 exp1 = RECUR (TREE_OPERAND (t, 1));
17948 exp2 = RECUR (TREE_OPERAND (t, 2));
17951 warning_sentinel s(warn_duplicated_branches);
17952 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17953 cond, exp1, exp2, complain));
17956 case PSEUDO_DTOR_EXPR:
17958 tree op0 = RECUR (TREE_OPERAND (t, 0));
17959 tree op1 = RECUR (TREE_OPERAND (t, 1));
17960 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17961 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17962 input_location));
17965 case TREE_LIST:
17967 tree purpose, value, chain;
17969 if (t == void_list_node)
17970 RETURN (t);
17972 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17973 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17975 /* We have pack expansions, so expand those and
17976 create a new list out of it. */
17977 tree purposevec = NULL_TREE;
17978 tree valuevec = NULL_TREE;
17979 tree chain;
17980 int i, len = -1;
17982 /* Expand the argument expressions. */
17983 if (TREE_PURPOSE (t))
17984 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17985 complain, in_decl);
17986 if (TREE_VALUE (t))
17987 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17988 complain, in_decl);
17990 /* Build the rest of the list. */
17991 chain = TREE_CHAIN (t);
17992 if (chain && chain != void_type_node)
17993 chain = RECUR (chain);
17995 /* Determine the number of arguments. */
17996 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17998 len = TREE_VEC_LENGTH (purposevec);
17999 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18001 else if (TREE_CODE (valuevec) == TREE_VEC)
18002 len = TREE_VEC_LENGTH (valuevec);
18003 else
18005 /* Since we only performed a partial substitution into
18006 the argument pack, we only RETURN (a single list
18007 node. */
18008 if (purposevec == TREE_PURPOSE (t)
18009 && valuevec == TREE_VALUE (t)
18010 && chain == TREE_CHAIN (t))
18011 RETURN (t);
18013 RETURN (tree_cons (purposevec, valuevec, chain));
18016 /* Convert the argument vectors into a TREE_LIST */
18017 i = len;
18018 while (i > 0)
18020 /* Grab the Ith values. */
18021 i--;
18022 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18023 : NULL_TREE;
18024 value
18025 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18026 : NULL_TREE;
18028 /* Build the list (backwards). */
18029 chain = tree_cons (purpose, value, chain);
18032 RETURN (chain);
18035 purpose = TREE_PURPOSE (t);
18036 if (purpose)
18037 purpose = RECUR (purpose);
18038 value = TREE_VALUE (t);
18039 if (value)
18040 value = RECUR (value);
18041 chain = TREE_CHAIN (t);
18042 if (chain && chain != void_type_node)
18043 chain = RECUR (chain);
18044 if (purpose == TREE_PURPOSE (t)
18045 && value == TREE_VALUE (t)
18046 && chain == TREE_CHAIN (t))
18047 RETURN (t);
18048 RETURN (tree_cons (purpose, value, chain));
18051 case COMPONENT_REF:
18053 tree object;
18054 tree object_type;
18055 tree member;
18056 tree r;
18058 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18059 args, complain, in_decl);
18060 /* Remember that there was a reference to this entity. */
18061 if (DECL_P (object)
18062 && !mark_used (object, complain) && !(complain & tf_error))
18063 RETURN (error_mark_node);
18064 object_type = TREE_TYPE (object);
18066 member = TREE_OPERAND (t, 1);
18067 if (BASELINK_P (member))
18068 member = tsubst_baselink (member,
18069 non_reference (TREE_TYPE (object)),
18070 args, complain, in_decl);
18071 else
18072 member = tsubst_copy (member, args, complain, in_decl);
18073 if (member == error_mark_node)
18074 RETURN (error_mark_node);
18076 if (TREE_CODE (member) == FIELD_DECL)
18078 r = finish_non_static_data_member (member, object, NULL_TREE);
18079 if (TREE_CODE (r) == COMPONENT_REF)
18080 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18081 RETURN (r);
18083 else if (type_dependent_expression_p (object))
18084 /* We can't do much here. */;
18085 else if (!CLASS_TYPE_P (object_type))
18087 if (scalarish_type_p (object_type))
18089 tree s = NULL_TREE;
18090 tree dtor = member;
18092 if (TREE_CODE (dtor) == SCOPE_REF)
18094 s = TREE_OPERAND (dtor, 0);
18095 dtor = TREE_OPERAND (dtor, 1);
18097 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18099 dtor = TREE_OPERAND (dtor, 0);
18100 if (TYPE_P (dtor))
18101 RETURN (finish_pseudo_destructor_expr
18102 (object, s, dtor, input_location));
18106 else if (TREE_CODE (member) == SCOPE_REF
18107 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18109 /* Lookup the template functions now that we know what the
18110 scope is. */
18111 tree scope = TREE_OPERAND (member, 0);
18112 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18113 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18114 member = lookup_qualified_name (scope, tmpl,
18115 /*is_type_p=*/false,
18116 /*complain=*/false);
18117 if (BASELINK_P (member))
18119 BASELINK_FUNCTIONS (member)
18120 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18121 args);
18122 member = (adjust_result_of_qualified_name_lookup
18123 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18124 object_type));
18126 else
18128 qualified_name_lookup_error (scope, tmpl, member,
18129 input_location);
18130 RETURN (error_mark_node);
18133 else if (TREE_CODE (member) == SCOPE_REF
18134 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18135 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18137 if (complain & tf_error)
18139 if (TYPE_P (TREE_OPERAND (member, 0)))
18140 error ("%qT is not a class or namespace",
18141 TREE_OPERAND (member, 0));
18142 else
18143 error ("%qD is not a class or namespace",
18144 TREE_OPERAND (member, 0));
18146 RETURN (error_mark_node);
18149 r = finish_class_member_access_expr (object, member,
18150 /*template_p=*/false,
18151 complain);
18152 if (TREE_CODE (r) == COMPONENT_REF)
18153 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18154 RETURN (r);
18157 case THROW_EXPR:
18158 RETURN (build_throw
18159 (RECUR (TREE_OPERAND (t, 0))));
18161 case CONSTRUCTOR:
18163 vec<constructor_elt, va_gc> *n;
18164 constructor_elt *ce;
18165 unsigned HOST_WIDE_INT idx;
18166 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18167 bool process_index_p;
18168 int newlen;
18169 bool need_copy_p = false;
18170 tree r;
18172 if (type == error_mark_node)
18173 RETURN (error_mark_node);
18175 /* We do not want to process the index of aggregate
18176 initializers as they are identifier nodes which will be
18177 looked up by digest_init. */
18178 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18180 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18181 newlen = vec_safe_length (n);
18182 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18184 if (ce->index && process_index_p
18185 /* An identifier index is looked up in the type
18186 being initialized, not the current scope. */
18187 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18188 ce->index = RECUR (ce->index);
18190 if (PACK_EXPANSION_P (ce->value))
18192 /* Substitute into the pack expansion. */
18193 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18194 in_decl);
18196 if (ce->value == error_mark_node
18197 || PACK_EXPANSION_P (ce->value))
18199 else if (TREE_VEC_LENGTH (ce->value) == 1)
18200 /* Just move the argument into place. */
18201 ce->value = TREE_VEC_ELT (ce->value, 0);
18202 else
18204 /* Update the length of the final CONSTRUCTOR
18205 arguments vector, and note that we will need to
18206 copy.*/
18207 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18208 need_copy_p = true;
18211 else
18212 ce->value = RECUR (ce->value);
18215 if (need_copy_p)
18217 vec<constructor_elt, va_gc> *old_n = n;
18219 vec_alloc (n, newlen);
18220 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18222 if (TREE_CODE (ce->value) == TREE_VEC)
18224 int i, len = TREE_VEC_LENGTH (ce->value);
18225 for (i = 0; i < len; ++i)
18226 CONSTRUCTOR_APPEND_ELT (n, 0,
18227 TREE_VEC_ELT (ce->value, i));
18229 else
18230 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18234 r = build_constructor (init_list_type_node, n);
18235 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18237 if (TREE_HAS_CONSTRUCTOR (t))
18239 fcl_t cl = fcl_functional;
18240 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18241 cl = fcl_c99;
18242 RETURN (finish_compound_literal (type, r, complain, cl));
18245 TREE_TYPE (r) = type;
18246 RETURN (r);
18249 case TYPEID_EXPR:
18251 tree operand_0 = TREE_OPERAND (t, 0);
18252 if (TYPE_P (operand_0))
18254 operand_0 = tsubst (operand_0, args, complain, in_decl);
18255 RETURN (get_typeid (operand_0, complain));
18257 else
18259 operand_0 = RECUR (operand_0);
18260 RETURN (build_typeid (operand_0, complain));
18264 case VAR_DECL:
18265 if (!args)
18266 RETURN (t);
18267 /* Fall through */
18269 case PARM_DECL:
18271 tree r = tsubst_copy (t, args, complain, in_decl);
18272 /* ??? We're doing a subset of finish_id_expression here. */
18273 if (VAR_P (r)
18274 && !processing_template_decl
18275 && !cp_unevaluated_operand
18276 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18277 && CP_DECL_THREAD_LOCAL_P (r))
18279 if (tree wrap = get_tls_wrapper_fn (r))
18280 /* Replace an evaluated use of the thread_local variable with
18281 a call to its wrapper. */
18282 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18284 else if (outer_automatic_var_p (r))
18285 r = process_outer_var_ref (r, complain);
18287 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18288 /* If the original type was a reference, we'll be wrapped in
18289 the appropriate INDIRECT_REF. */
18290 r = convert_from_reference (r);
18291 RETURN (r);
18294 case VA_ARG_EXPR:
18296 tree op0 = RECUR (TREE_OPERAND (t, 0));
18297 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18298 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18301 case OFFSETOF_EXPR:
18303 tree object_ptr
18304 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18305 in_decl, /*function_p=*/false,
18306 /*integral_constant_expression_p=*/false);
18307 RETURN (finish_offsetof (object_ptr,
18308 RECUR (TREE_OPERAND (t, 0)),
18309 EXPR_LOCATION (t)));
18312 case ADDRESSOF_EXPR:
18313 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18314 RECUR (TREE_OPERAND (t, 0)), complain));
18316 case TRAIT_EXPR:
18318 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18319 complain, in_decl);
18321 tree type2 = TRAIT_EXPR_TYPE2 (t);
18322 if (type2 && TREE_CODE (type2) == TREE_LIST)
18323 type2 = RECUR (type2);
18324 else if (type2)
18325 type2 = tsubst (type2, args, complain, in_decl);
18327 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18330 case STMT_EXPR:
18332 tree old_stmt_expr = cur_stmt_expr;
18333 tree stmt_expr = begin_stmt_expr ();
18335 cur_stmt_expr = stmt_expr;
18336 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18337 integral_constant_expression_p);
18338 stmt_expr = finish_stmt_expr (stmt_expr, false);
18339 cur_stmt_expr = old_stmt_expr;
18341 /* If the resulting list of expression statement is empty,
18342 fold it further into void_node. */
18343 if (empty_expr_stmt_p (stmt_expr))
18344 stmt_expr = void_node;
18346 RETURN (stmt_expr);
18349 case LAMBDA_EXPR:
18351 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18353 RETURN (build_lambda_object (r));
18356 case TARGET_EXPR:
18357 /* We can get here for a constant initializer of non-dependent type.
18358 FIXME stop folding in cp_parser_initializer_clause. */
18360 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18361 complain);
18362 RETURN (r);
18365 case TRANSACTION_EXPR:
18366 RETURN (tsubst_expr(t, args, complain, in_decl,
18367 integral_constant_expression_p));
18369 case PAREN_EXPR:
18370 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18372 case VEC_PERM_EXPR:
18374 tree op0 = RECUR (TREE_OPERAND (t, 0));
18375 tree op1 = RECUR (TREE_OPERAND (t, 1));
18376 tree op2 = RECUR (TREE_OPERAND (t, 2));
18377 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18378 complain));
18381 case REQUIRES_EXPR:
18382 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18384 case NON_LVALUE_EXPR:
18385 case VIEW_CONVERT_EXPR:
18386 /* We should only see these for location wrapper nodes, or within
18387 instantiate_non_dependent_expr (when args is NULL_TREE). */
18388 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18389 if (location_wrapper_p (t))
18390 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18391 EXPR_LOCATION (t)));
18392 /* fallthrough. */
18394 default:
18395 /* Handle Objective-C++ constructs, if appropriate. */
18397 tree subst
18398 = objcp_tsubst_copy_and_build (t, args, complain,
18399 in_decl, /*function_p=*/false);
18400 if (subst)
18401 RETURN (subst);
18403 RETURN (tsubst_copy (t, args, complain, in_decl));
18406 #undef RECUR
18407 #undef RETURN
18408 out:
18409 input_location = loc;
18410 return retval;
18413 /* Verify that the instantiated ARGS are valid. For type arguments,
18414 make sure that the type's linkage is ok. For non-type arguments,
18415 make sure they are constants if they are integral or enumerations.
18416 Emit an error under control of COMPLAIN, and return TRUE on error. */
18418 static bool
18419 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18421 if (dependent_template_arg_p (t))
18422 return false;
18423 if (ARGUMENT_PACK_P (t))
18425 tree vec = ARGUMENT_PACK_ARGS (t);
18426 int len = TREE_VEC_LENGTH (vec);
18427 bool result = false;
18428 int i;
18430 for (i = 0; i < len; ++i)
18431 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18432 result = true;
18433 return result;
18435 else if (TYPE_P (t))
18437 /* [basic.link]: A name with no linkage (notably, the name
18438 of a class or enumeration declared in a local scope)
18439 shall not be used to declare an entity with linkage.
18440 This implies that names with no linkage cannot be used as
18441 template arguments
18443 DR 757 relaxes this restriction for C++0x. */
18444 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18445 : no_linkage_check (t, /*relaxed_p=*/false));
18447 if (nt)
18449 /* DR 488 makes use of a type with no linkage cause
18450 type deduction to fail. */
18451 if (complain & tf_error)
18453 if (TYPE_UNNAMED_P (nt))
18454 error ("%qT is/uses unnamed type", t);
18455 else
18456 error ("template argument for %qD uses local type %qT",
18457 tmpl, t);
18459 return true;
18461 /* In order to avoid all sorts of complications, we do not
18462 allow variably-modified types as template arguments. */
18463 else if (variably_modified_type_p (t, NULL_TREE))
18465 if (complain & tf_error)
18466 error ("%qT is a variably modified type", t);
18467 return true;
18470 /* Class template and alias template arguments should be OK. */
18471 else if (DECL_TYPE_TEMPLATE_P (t))
18473 /* A non-type argument of integral or enumerated type must be a
18474 constant. */
18475 else if (TREE_TYPE (t)
18476 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18477 && !REFERENCE_REF_P (t)
18478 && !TREE_CONSTANT (t))
18480 if (complain & tf_error)
18481 error ("integral expression %qE is not constant", t);
18482 return true;
18484 return false;
18487 static bool
18488 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18490 int ix, len = DECL_NTPARMS (tmpl);
18491 bool result = false;
18493 for (ix = 0; ix != len; ix++)
18495 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18496 result = true;
18498 if (result && (complain & tf_error))
18499 error (" trying to instantiate %qD", tmpl);
18500 return result;
18503 /* We're out of SFINAE context now, so generate diagnostics for the access
18504 errors we saw earlier when instantiating D from TMPL and ARGS. */
18506 static void
18507 recheck_decl_substitution (tree d, tree tmpl, tree args)
18509 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18510 tree type = TREE_TYPE (pattern);
18511 location_t loc = input_location;
18513 push_access_scope (d);
18514 push_deferring_access_checks (dk_no_deferred);
18515 input_location = DECL_SOURCE_LOCATION (pattern);
18516 tsubst (type, args, tf_warning_or_error, d);
18517 input_location = loc;
18518 pop_deferring_access_checks ();
18519 pop_access_scope (d);
18522 /* Instantiate the indicated variable, function, or alias template TMPL with
18523 the template arguments in TARG_PTR. */
18525 static tree
18526 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18528 tree targ_ptr = orig_args;
18529 tree fndecl;
18530 tree gen_tmpl;
18531 tree spec;
18532 bool access_ok = true;
18534 if (tmpl == error_mark_node)
18535 return error_mark_node;
18537 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18539 /* If this function is a clone, handle it specially. */
18540 if (DECL_CLONED_FUNCTION_P (tmpl))
18542 tree spec;
18543 tree clone;
18545 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18546 DECL_CLONED_FUNCTION. */
18547 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18548 targ_ptr, complain);
18549 if (spec == error_mark_node)
18550 return error_mark_node;
18552 /* Look for the clone. */
18553 FOR_EACH_CLONE (clone, spec)
18554 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18555 return clone;
18556 /* We should always have found the clone by now. */
18557 gcc_unreachable ();
18558 return NULL_TREE;
18561 if (targ_ptr == error_mark_node)
18562 return error_mark_node;
18564 /* Check to see if we already have this specialization. */
18565 gen_tmpl = most_general_template (tmpl);
18566 if (TMPL_ARGS_DEPTH (targ_ptr)
18567 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18568 /* targ_ptr only has the innermost template args, so add the outer ones
18569 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18570 the case of a non-dependent call within a template definition). */
18571 targ_ptr = (add_outermost_template_args
18572 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18573 targ_ptr));
18575 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18576 but it doesn't seem to be on the hot path. */
18577 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18579 gcc_assert (tmpl == gen_tmpl
18580 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18581 == spec)
18582 || fndecl == NULL_TREE);
18584 if (spec != NULL_TREE)
18586 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18588 if (complain & tf_error)
18589 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18590 return error_mark_node;
18592 return spec;
18595 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18596 complain))
18597 return error_mark_node;
18599 /* We are building a FUNCTION_DECL, during which the access of its
18600 parameters and return types have to be checked. However this
18601 FUNCTION_DECL which is the desired context for access checking
18602 is not built yet. We solve this chicken-and-egg problem by
18603 deferring all checks until we have the FUNCTION_DECL. */
18604 push_deferring_access_checks (dk_deferred);
18606 /* Instantiation of the function happens in the context of the function
18607 template, not the context of the overload resolution we're doing. */
18608 push_to_top_level ();
18609 /* If there are dependent arguments, e.g. because we're doing partial
18610 ordering, make sure processing_template_decl stays set. */
18611 if (uses_template_parms (targ_ptr))
18612 ++processing_template_decl;
18613 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18615 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18616 complain, gen_tmpl, true);
18617 push_nested_class (ctx);
18620 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18622 fndecl = NULL_TREE;
18623 if (VAR_P (pattern))
18625 /* We need to determine if we're using a partial or explicit
18626 specialization now, because the type of the variable could be
18627 different. */
18628 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18629 tree elt = most_specialized_partial_spec (tid, complain);
18630 if (elt == error_mark_node)
18631 pattern = error_mark_node;
18632 else if (elt)
18634 tree partial_tmpl = TREE_VALUE (elt);
18635 tree partial_args = TREE_PURPOSE (elt);
18636 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18637 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18641 /* Substitute template parameters to obtain the specialization. */
18642 if (fndecl == NULL_TREE)
18643 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18644 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18645 pop_nested_class ();
18646 pop_from_top_level ();
18648 if (fndecl == error_mark_node)
18650 pop_deferring_access_checks ();
18651 return error_mark_node;
18654 /* The DECL_TI_TEMPLATE should always be the immediate parent
18655 template, not the most general template. */
18656 DECL_TI_TEMPLATE (fndecl) = tmpl;
18657 DECL_TI_ARGS (fndecl) = targ_ptr;
18659 /* Now we know the specialization, compute access previously
18660 deferred. Do no access control for inheriting constructors,
18661 as we already checked access for the inherited constructor. */
18662 if (!(flag_new_inheriting_ctors
18663 && DECL_INHERITED_CTOR (fndecl)))
18665 push_access_scope (fndecl);
18666 if (!perform_deferred_access_checks (complain))
18667 access_ok = false;
18668 pop_access_scope (fndecl);
18670 pop_deferring_access_checks ();
18672 /* If we've just instantiated the main entry point for a function,
18673 instantiate all the alternate entry points as well. We do this
18674 by cloning the instantiation of the main entry point, not by
18675 instantiating the template clones. */
18676 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18677 clone_function_decl (fndecl, /*update_methods=*/false);
18679 if (!access_ok)
18681 if (!(complain & tf_error))
18683 /* Remember to reinstantiate when we're out of SFINAE so the user
18684 can see the errors. */
18685 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18687 return error_mark_node;
18689 return fndecl;
18692 /* Wrapper for instantiate_template_1. */
18694 tree
18695 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18697 tree ret;
18698 timevar_push (TV_TEMPLATE_INST);
18699 ret = instantiate_template_1 (tmpl, orig_args, complain);
18700 timevar_pop (TV_TEMPLATE_INST);
18701 return ret;
18704 /* Instantiate the alias template TMPL with ARGS. Also push a template
18705 instantiation level, which instantiate_template doesn't do because
18706 functions and variables have sufficient context established by the
18707 callers. */
18709 static tree
18710 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18712 struct pending_template *old_last_pend = last_pending_template;
18713 struct tinst_level *old_error_tinst = last_error_tinst_level;
18714 if (tmpl == error_mark_node || args == error_mark_node)
18715 return error_mark_node;
18716 tree tinst = build_tree_list (tmpl, args);
18717 if (!push_tinst_level (tinst))
18719 ggc_free (tinst);
18720 return error_mark_node;
18723 args =
18724 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18725 args, tmpl, complain,
18726 /*require_all_args=*/true,
18727 /*use_default_args=*/true);
18729 tree r = instantiate_template (tmpl, args, complain);
18730 pop_tinst_level ();
18731 /* We can't free this if a pending_template entry or last_error_tinst_level
18732 is pointing at it. */
18733 if (last_pending_template == old_last_pend
18734 && last_error_tinst_level == old_error_tinst)
18735 ggc_free (tinst);
18737 return r;
18740 /* PARM is a template parameter pack for FN. Returns true iff
18741 PARM is used in a deducible way in the argument list of FN. */
18743 static bool
18744 pack_deducible_p (tree parm, tree fn)
18746 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18747 for (; t; t = TREE_CHAIN (t))
18749 tree type = TREE_VALUE (t);
18750 tree packs;
18751 if (!PACK_EXPANSION_P (type))
18752 continue;
18753 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18754 packs; packs = TREE_CHAIN (packs))
18755 if (template_args_equal (TREE_VALUE (packs), parm))
18757 /* The template parameter pack is used in a function parameter
18758 pack. If this is the end of the parameter list, the
18759 template parameter pack is deducible. */
18760 if (TREE_CHAIN (t) == void_list_node)
18761 return true;
18762 else
18763 /* Otherwise, not. Well, it could be deduced from
18764 a non-pack parameter, but doing so would end up with
18765 a deduction mismatch, so don't bother. */
18766 return false;
18769 /* The template parameter pack isn't used in any function parameter
18770 packs, but it might be used deeper, e.g. tuple<Args...>. */
18771 return true;
18774 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18775 NARGS elements of the arguments that are being used when calling
18776 it. TARGS is a vector into which the deduced template arguments
18777 are placed.
18779 Returns either a FUNCTION_DECL for the matching specialization of FN or
18780 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18781 true, diagnostics will be printed to explain why it failed.
18783 If FN is a conversion operator, or we are trying to produce a specific
18784 specialization, RETURN_TYPE is the return type desired.
18786 The EXPLICIT_TARGS are explicit template arguments provided via a
18787 template-id.
18789 The parameter STRICT is one of:
18791 DEDUCE_CALL:
18792 We are deducing arguments for a function call, as in
18793 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18794 deducing arguments for a call to the result of a conversion
18795 function template, as in [over.call.object].
18797 DEDUCE_CONV:
18798 We are deducing arguments for a conversion function, as in
18799 [temp.deduct.conv].
18801 DEDUCE_EXACT:
18802 We are deducing arguments when doing an explicit instantiation
18803 as in [temp.explicit], when determining an explicit specialization
18804 as in [temp.expl.spec], or when taking the address of a function
18805 template, as in [temp.deduct.funcaddr]. */
18807 tree
18808 fn_type_unification (tree fn,
18809 tree explicit_targs,
18810 tree targs,
18811 const tree *args,
18812 unsigned int nargs,
18813 tree return_type,
18814 unification_kind_t strict,
18815 int flags,
18816 bool explain_p,
18817 bool decltype_p)
18819 tree parms;
18820 tree fntype;
18821 tree decl = NULL_TREE;
18822 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18823 bool ok;
18824 static int deduction_depth;
18825 struct pending_template *old_last_pend = last_pending_template;
18826 struct tinst_level *old_error_tinst = last_error_tinst_level;
18828 tree orig_fn = fn;
18829 if (flag_new_inheriting_ctors)
18830 fn = strip_inheriting_ctors (fn);
18832 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18833 tree tinst;
18834 tree r = error_mark_node;
18836 tree full_targs = targs;
18837 if (TMPL_ARGS_DEPTH (targs)
18838 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18839 full_targs = (add_outermost_template_args
18840 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18841 targs));
18843 if (decltype_p)
18844 complain |= tf_decltype;
18846 /* In C++0x, it's possible to have a function template whose type depends
18847 on itself recursively. This is most obvious with decltype, but can also
18848 occur with enumeration scope (c++/48969). So we need to catch infinite
18849 recursion and reject the substitution at deduction time; this function
18850 will return error_mark_node for any repeated substitution.
18852 This also catches excessive recursion such as when f<N> depends on
18853 f<N-1> across all integers, and returns error_mark_node for all the
18854 substitutions back up to the initial one.
18856 This is, of course, not reentrant. */
18857 if (excessive_deduction_depth)
18858 return error_mark_node;
18859 tinst = build_tree_list (fn, NULL_TREE);
18860 ++deduction_depth;
18862 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18864 fntype = TREE_TYPE (fn);
18865 if (explicit_targs)
18867 /* [temp.deduct]
18869 The specified template arguments must match the template
18870 parameters in kind (i.e., type, nontype, template), and there
18871 must not be more arguments than there are parameters;
18872 otherwise type deduction fails.
18874 Nontype arguments must match the types of the corresponding
18875 nontype template parameters, or must be convertible to the
18876 types of the corresponding nontype parameters as specified in
18877 _temp.arg.nontype_, otherwise type deduction fails.
18879 All references in the function type of the function template
18880 to the corresponding template parameters are replaced by the
18881 specified template argument values. If a substitution in a
18882 template parameter or in the function type of the function
18883 template results in an invalid type, type deduction fails. */
18884 int i, len = TREE_VEC_LENGTH (tparms);
18885 location_t loc = input_location;
18886 bool incomplete = false;
18888 if (explicit_targs == error_mark_node)
18889 goto fail;
18891 if (TMPL_ARGS_DEPTH (explicit_targs)
18892 < TMPL_ARGS_DEPTH (full_targs))
18893 explicit_targs = add_outermost_template_args (full_targs,
18894 explicit_targs);
18896 /* Adjust any explicit template arguments before entering the
18897 substitution context. */
18898 explicit_targs
18899 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18900 complain,
18901 /*require_all_args=*/false,
18902 /*use_default_args=*/false));
18903 if (explicit_targs == error_mark_node)
18904 goto fail;
18906 /* Substitute the explicit args into the function type. This is
18907 necessary so that, for instance, explicitly declared function
18908 arguments can match null pointed constants. If we were given
18909 an incomplete set of explicit args, we must not do semantic
18910 processing during substitution as we could create partial
18911 instantiations. */
18912 for (i = 0; i < len; i++)
18914 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18915 bool parameter_pack = false;
18916 tree targ = TREE_VEC_ELT (explicit_targs, i);
18918 /* Dig out the actual parm. */
18919 if (TREE_CODE (parm) == TYPE_DECL
18920 || TREE_CODE (parm) == TEMPLATE_DECL)
18922 parm = TREE_TYPE (parm);
18923 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18925 else if (TREE_CODE (parm) == PARM_DECL)
18927 parm = DECL_INITIAL (parm);
18928 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18931 if (!parameter_pack && targ == NULL_TREE)
18932 /* No explicit argument for this template parameter. */
18933 incomplete = true;
18935 if (parameter_pack && pack_deducible_p (parm, fn))
18937 /* Mark the argument pack as "incomplete". We could
18938 still deduce more arguments during unification.
18939 We remove this mark in type_unification_real. */
18940 if (targ)
18942 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18943 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18944 = ARGUMENT_PACK_ARGS (targ);
18947 /* We have some incomplete argument packs. */
18948 incomplete = true;
18952 TREE_VALUE (tinst) = explicit_targs;
18953 if (!push_tinst_level (tinst))
18955 excessive_deduction_depth = true;
18956 goto fail;
18958 processing_template_decl += incomplete;
18959 input_location = DECL_SOURCE_LOCATION (fn);
18960 /* Ignore any access checks; we'll see them again in
18961 instantiate_template and they might have the wrong
18962 access path at this point. */
18963 push_deferring_access_checks (dk_deferred);
18964 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18965 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18966 pop_deferring_access_checks ();
18967 input_location = loc;
18968 processing_template_decl -= incomplete;
18969 pop_tinst_level ();
18971 if (fntype == error_mark_node)
18972 goto fail;
18974 /* Place the explicitly specified arguments in TARGS. */
18975 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18976 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18977 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18980 /* Never do unification on the 'this' parameter. */
18981 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18983 if (return_type && strict == DEDUCE_CALL)
18985 /* We're deducing for a call to the result of a template conversion
18986 function. The parms we really want are in return_type. */
18987 if (POINTER_TYPE_P (return_type))
18988 return_type = TREE_TYPE (return_type);
18989 parms = TYPE_ARG_TYPES (return_type);
18991 else if (return_type)
18993 tree *new_args;
18995 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18996 new_args = XALLOCAVEC (tree, nargs + 1);
18997 new_args[0] = return_type;
18998 memcpy (new_args + 1, args, nargs * sizeof (tree));
18999 args = new_args;
19000 ++nargs;
19003 /* We allow incomplete unification without an error message here
19004 because the standard doesn't seem to explicitly prohibit it. Our
19005 callers must be ready to deal with unification failures in any
19006 event. */
19008 TREE_VALUE (tinst) = targs;
19009 /* If we aren't explaining yet, push tinst context so we can see where
19010 any errors (e.g. from class instantiations triggered by instantiation
19011 of default template arguments) come from. If we are explaining, this
19012 context is redundant. */
19013 if (!explain_p && !push_tinst_level (tinst))
19015 excessive_deduction_depth = true;
19016 goto fail;
19019 /* type_unification_real will pass back any access checks from default
19020 template argument substitution. */
19021 vec<deferred_access_check, va_gc> *checks;
19022 checks = NULL;
19024 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19025 full_targs, parms, args, nargs, /*subr=*/0,
19026 strict, flags, &checks, explain_p);
19027 if (!explain_p)
19028 pop_tinst_level ();
19029 if (!ok)
19030 goto fail;
19032 /* Now that we have bindings for all of the template arguments,
19033 ensure that the arguments deduced for the template template
19034 parameters have compatible template parameter lists. We cannot
19035 check this property before we have deduced all template
19036 arguments, because the template parameter types of a template
19037 template parameter might depend on prior template parameters
19038 deduced after the template template parameter. The following
19039 ill-formed example illustrates this issue:
19041 template<typename T, template<T> class C> void f(C<5>, T);
19043 template<int N> struct X {};
19045 void g() {
19046 f(X<5>(), 5l); // error: template argument deduction fails
19049 The template parameter list of 'C' depends on the template type
19050 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19051 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19052 time that we deduce 'C'. */
19053 if (!template_template_parm_bindings_ok_p
19054 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19056 unify_inconsistent_template_template_parameters (explain_p);
19057 goto fail;
19060 /* All is well so far. Now, check:
19062 [temp.deduct]
19064 When all template arguments have been deduced, all uses of
19065 template parameters in nondeduced contexts are replaced with
19066 the corresponding deduced argument values. If the
19067 substitution results in an invalid type, as described above,
19068 type deduction fails. */
19069 TREE_VALUE (tinst) = targs;
19070 if (!push_tinst_level (tinst))
19072 excessive_deduction_depth = true;
19073 goto fail;
19076 /* Also collect access checks from the instantiation. */
19077 reopen_deferring_access_checks (checks);
19079 decl = instantiate_template (fn, targs, complain);
19081 checks = get_deferred_access_checks ();
19082 pop_deferring_access_checks ();
19084 pop_tinst_level ();
19086 if (decl == error_mark_node)
19087 goto fail;
19089 /* Now perform any access checks encountered during substitution. */
19090 push_access_scope (decl);
19091 ok = perform_access_checks (checks, complain);
19092 pop_access_scope (decl);
19093 if (!ok)
19094 goto fail;
19096 /* If we're looking for an exact match, check that what we got
19097 is indeed an exact match. It might not be if some template
19098 parameters are used in non-deduced contexts. But don't check
19099 for an exact match if we have dependent template arguments;
19100 in that case we're doing partial ordering, and we already know
19101 that we have two candidates that will provide the actual type. */
19102 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19104 tree substed = TREE_TYPE (decl);
19105 unsigned int i;
19107 tree sarg
19108 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19109 if (return_type)
19110 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19111 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19112 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19114 unify_type_mismatch (explain_p, args[i],
19115 TREE_VALUE (sarg));
19116 goto fail;
19120 /* After doing deduction with the inherited constructor, actually return an
19121 instantiation of the inheriting constructor. */
19122 if (orig_fn != fn)
19123 decl = instantiate_template (orig_fn, targs, complain);
19125 r = decl;
19127 fail:
19128 --deduction_depth;
19129 if (excessive_deduction_depth)
19131 if (deduction_depth == 0)
19132 /* Reset once we're all the way out. */
19133 excessive_deduction_depth = false;
19136 /* We can't free this if a pending_template entry or last_error_tinst_level
19137 is pointing at it. */
19138 if (last_pending_template == old_last_pend
19139 && last_error_tinst_level == old_error_tinst)
19140 ggc_free (tinst);
19142 return r;
19145 /* Adjust types before performing type deduction, as described in
19146 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19147 sections are symmetric. PARM is the type of a function parameter
19148 or the return type of the conversion function. ARG is the type of
19149 the argument passed to the call, or the type of the value
19150 initialized with the result of the conversion function.
19151 ARG_EXPR is the original argument expression, which may be null. */
19153 static int
19154 maybe_adjust_types_for_deduction (unification_kind_t strict,
19155 tree* parm,
19156 tree* arg,
19157 tree arg_expr)
19159 int result = 0;
19161 switch (strict)
19163 case DEDUCE_CALL:
19164 break;
19166 case DEDUCE_CONV:
19167 /* Swap PARM and ARG throughout the remainder of this
19168 function; the handling is precisely symmetric since PARM
19169 will initialize ARG rather than vice versa. */
19170 std::swap (parm, arg);
19171 break;
19173 case DEDUCE_EXACT:
19174 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19175 too, but here handle it by stripping the reference from PARM
19176 rather than by adding it to ARG. */
19177 if (TREE_CODE (*parm) == REFERENCE_TYPE
19178 && TYPE_REF_IS_RVALUE (*parm)
19179 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19180 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19181 && TREE_CODE (*arg) == REFERENCE_TYPE
19182 && !TYPE_REF_IS_RVALUE (*arg))
19183 *parm = TREE_TYPE (*parm);
19184 /* Nothing else to do in this case. */
19185 return 0;
19187 default:
19188 gcc_unreachable ();
19191 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19193 /* [temp.deduct.call]
19195 If P is not a reference type:
19197 --If A is an array type, the pointer type produced by the
19198 array-to-pointer standard conversion (_conv.array_) is
19199 used in place of A for type deduction; otherwise,
19201 --If A is a function type, the pointer type produced by
19202 the function-to-pointer standard conversion
19203 (_conv.func_) is used in place of A for type deduction;
19204 otherwise,
19206 --If A is a cv-qualified type, the top level
19207 cv-qualifiers of A's type are ignored for type
19208 deduction. */
19209 if (TREE_CODE (*arg) == ARRAY_TYPE)
19210 *arg = build_pointer_type (TREE_TYPE (*arg));
19211 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19212 *arg = build_pointer_type (*arg);
19213 else
19214 *arg = TYPE_MAIN_VARIANT (*arg);
19217 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19218 reference to a cv-unqualified template parameter that does not represent a
19219 template parameter of a class template (during class template argument
19220 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19221 an lvalue, the type "lvalue reference to A" is used in place of A for type
19222 deduction. */
19223 if (TREE_CODE (*parm) == REFERENCE_TYPE
19224 && TYPE_REF_IS_RVALUE (*parm)
19225 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19226 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19227 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19228 && (arg_expr ? lvalue_p (arg_expr)
19229 /* try_one_overload doesn't provide an arg_expr, but
19230 functions are always lvalues. */
19231 : TREE_CODE (*arg) == FUNCTION_TYPE))
19232 *arg = build_reference_type (*arg);
19234 /* [temp.deduct.call]
19236 If P is a cv-qualified type, the top level cv-qualifiers
19237 of P's type are ignored for type deduction. If P is a
19238 reference type, the type referred to by P is used for
19239 type deduction. */
19240 *parm = TYPE_MAIN_VARIANT (*parm);
19241 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19243 *parm = TREE_TYPE (*parm);
19244 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19247 /* DR 322. For conversion deduction, remove a reference type on parm
19248 too (which has been swapped into ARG). */
19249 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19250 *arg = TREE_TYPE (*arg);
19252 return result;
19255 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19256 template which does contain any deducible template parameters; check if
19257 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19258 unify_one_argument. */
19260 static int
19261 check_non_deducible_conversion (tree parm, tree arg, int strict,
19262 int flags, bool explain_p)
19264 tree type;
19266 if (!TYPE_P (arg))
19267 type = TREE_TYPE (arg);
19268 else
19269 type = arg;
19271 if (same_type_p (parm, type))
19272 return unify_success (explain_p);
19274 if (strict == DEDUCE_CONV)
19276 if (can_convert_arg (type, parm, NULL_TREE, flags,
19277 explain_p ? tf_warning_or_error : tf_none))
19278 return unify_success (explain_p);
19280 else if (strict != DEDUCE_EXACT)
19282 if (can_convert_arg (parm, type,
19283 TYPE_P (arg) ? NULL_TREE : arg,
19284 flags, explain_p ? tf_warning_or_error : tf_none))
19285 return unify_success (explain_p);
19288 if (strict == DEDUCE_EXACT)
19289 return unify_type_mismatch (explain_p, parm, arg);
19290 else
19291 return unify_arg_conversion (explain_p, parm, type, arg);
19294 static bool uses_deducible_template_parms (tree type);
19296 /* Returns true iff the expression EXPR is one from which a template
19297 argument can be deduced. In other words, if it's an undecorated
19298 use of a template non-type parameter. */
19300 static bool
19301 deducible_expression (tree expr)
19303 /* Strip implicit conversions. */
19304 while (CONVERT_EXPR_P (expr))
19305 expr = TREE_OPERAND (expr, 0);
19306 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19309 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19310 deducible way; that is, if it has a max value of <PARM> - 1. */
19312 static bool
19313 deducible_array_bound (tree domain)
19315 if (domain == NULL_TREE)
19316 return false;
19318 tree max = TYPE_MAX_VALUE (domain);
19319 if (TREE_CODE (max) != MINUS_EXPR)
19320 return false;
19322 return deducible_expression (TREE_OPERAND (max, 0));
19325 /* Returns true iff the template arguments ARGS use a template parameter
19326 in a deducible way. */
19328 static bool
19329 deducible_template_args (tree args)
19331 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19333 bool deducible;
19334 tree elt = TREE_VEC_ELT (args, i);
19335 if (ARGUMENT_PACK_P (elt))
19336 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19337 else
19339 if (PACK_EXPANSION_P (elt))
19340 elt = PACK_EXPANSION_PATTERN (elt);
19341 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19342 deducible = true;
19343 else if (TYPE_P (elt))
19344 deducible = uses_deducible_template_parms (elt);
19345 else
19346 deducible = deducible_expression (elt);
19348 if (deducible)
19349 return true;
19351 return false;
19354 /* Returns true iff TYPE contains any deducible references to template
19355 parameters, as per 14.8.2.5. */
19357 static bool
19358 uses_deducible_template_parms (tree type)
19360 if (PACK_EXPANSION_P (type))
19361 type = PACK_EXPANSION_PATTERN (type);
19363 /* T
19364 cv-list T
19365 TT<T>
19366 TT<i>
19367 TT<> */
19368 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19369 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19370 return true;
19372 /* T*
19374 T&& */
19375 if (POINTER_TYPE_P (type))
19376 return uses_deducible_template_parms (TREE_TYPE (type));
19378 /* T[integer-constant ]
19379 type [i] */
19380 if (TREE_CODE (type) == ARRAY_TYPE)
19381 return (uses_deducible_template_parms (TREE_TYPE (type))
19382 || deducible_array_bound (TYPE_DOMAIN (type)));
19384 /* T type ::*
19385 type T::*
19386 T T::*
19387 T (type ::*)()
19388 type (T::*)()
19389 type (type ::*)(T)
19390 type (T::*)(T)
19391 T (type ::*)(T)
19392 T (T::*)()
19393 T (T::*)(T) */
19394 if (TYPE_PTRMEM_P (type))
19395 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19396 || (uses_deducible_template_parms
19397 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19399 /* template-name <T> (where template-name refers to a class template)
19400 template-name <i> (where template-name refers to a class template) */
19401 if (CLASS_TYPE_P (type)
19402 && CLASSTYPE_TEMPLATE_INFO (type)
19403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19404 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19405 (CLASSTYPE_TI_ARGS (type)));
19407 /* type (T)
19409 T(T) */
19410 if (TREE_CODE (type) == FUNCTION_TYPE
19411 || TREE_CODE (type) == METHOD_TYPE)
19413 if (uses_deducible_template_parms (TREE_TYPE (type)))
19414 return true;
19415 tree parm = TYPE_ARG_TYPES (type);
19416 if (TREE_CODE (type) == METHOD_TYPE)
19417 parm = TREE_CHAIN (parm);
19418 for (; parm; parm = TREE_CHAIN (parm))
19419 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19420 return true;
19423 return false;
19426 /* Subroutine of type_unification_real and unify_pack_expansion to
19427 handle unification of a single P/A pair. Parameters are as
19428 for those functions. */
19430 static int
19431 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19432 int subr, unification_kind_t strict,
19433 bool explain_p)
19435 tree arg_expr = NULL_TREE;
19436 int arg_strict;
19438 if (arg == error_mark_node || parm == error_mark_node)
19439 return unify_invalid (explain_p);
19440 if (arg == unknown_type_node)
19441 /* We can't deduce anything from this, but we might get all the
19442 template args from other function args. */
19443 return unify_success (explain_p);
19445 /* Implicit conversions (Clause 4) will be performed on a function
19446 argument to convert it to the type of the corresponding function
19447 parameter if the parameter type contains no template-parameters that
19448 participate in template argument deduction. */
19449 if (strict != DEDUCE_EXACT
19450 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19451 /* For function parameters with no deducible template parameters,
19452 just return. We'll check non-dependent conversions later. */
19453 return unify_success (explain_p);
19455 switch (strict)
19457 case DEDUCE_CALL:
19458 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19459 | UNIFY_ALLOW_MORE_CV_QUAL
19460 | UNIFY_ALLOW_DERIVED);
19461 break;
19463 case DEDUCE_CONV:
19464 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19465 break;
19467 case DEDUCE_EXACT:
19468 arg_strict = UNIFY_ALLOW_NONE;
19469 break;
19471 default:
19472 gcc_unreachable ();
19475 /* We only do these transformations if this is the top-level
19476 parameter_type_list in a call or declaration matching; in other
19477 situations (nested function declarators, template argument lists) we
19478 won't be comparing a type to an expression, and we don't do any type
19479 adjustments. */
19480 if (!subr)
19482 if (!TYPE_P (arg))
19484 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19485 if (type_unknown_p (arg))
19487 /* [temp.deduct.type] A template-argument can be
19488 deduced from a pointer to function or pointer
19489 to member function argument if the set of
19490 overloaded functions does not contain function
19491 templates and at most one of a set of
19492 overloaded functions provides a unique
19493 match. */
19494 resolve_overloaded_unification (tparms, targs, parm,
19495 arg, strict,
19496 arg_strict, explain_p);
19497 /* If a unique match was not found, this is a
19498 non-deduced context, so we still succeed. */
19499 return unify_success (explain_p);
19502 arg_expr = arg;
19503 arg = unlowered_expr_type (arg);
19504 if (arg == error_mark_node)
19505 return unify_invalid (explain_p);
19508 arg_strict |=
19509 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19511 else
19512 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19513 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19514 return unify_template_argument_mismatch (explain_p, parm, arg);
19516 /* For deduction from an init-list we need the actual list. */
19517 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19518 arg = arg_expr;
19519 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19522 /* for_each_template_parm callback that always returns 0. */
19524 static int
19525 zero_r (tree, void *)
19527 return 0;
19530 /* for_each_template_parm any_fn callback to handle deduction of a template
19531 type argument from the type of an array bound. */
19533 static int
19534 array_deduction_r (tree t, void *data)
19536 tree_pair_p d = (tree_pair_p)data;
19537 tree &tparms = d->purpose;
19538 tree &targs = d->value;
19540 if (TREE_CODE (t) == ARRAY_TYPE)
19541 if (tree dom = TYPE_DOMAIN (t))
19542 if (tree max = TYPE_MAX_VALUE (dom))
19544 if (TREE_CODE (max) == MINUS_EXPR)
19545 max = TREE_OPERAND (max, 0);
19546 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19547 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19548 UNIFY_ALLOW_NONE, /*explain*/false);
19551 /* Keep walking. */
19552 return 0;
19555 /* Try to deduce any not-yet-deduced template type arguments from the type of
19556 an array bound. This is handled separately from unify because 14.8.2.5 says
19557 "The type of a type parameter is only deduced from an array bound if it is
19558 not otherwise deduced." */
19560 static void
19561 try_array_deduction (tree tparms, tree targs, tree parm)
19563 tree_pair_s data = { tparms, targs };
19564 hash_set<tree> visited;
19565 for_each_template_parm (parm, zero_r, &data, &visited,
19566 /*nondeduced*/false, array_deduction_r);
19569 /* Most parms like fn_type_unification.
19571 If SUBR is 1, we're being called recursively (to unify the
19572 arguments of a function or method parameter of a function
19573 template).
19575 CHECKS is a pointer to a vector of access checks encountered while
19576 substituting default template arguments. */
19578 static int
19579 type_unification_real (tree tparms,
19580 tree full_targs,
19581 tree xparms,
19582 const tree *xargs,
19583 unsigned int xnargs,
19584 int subr,
19585 unification_kind_t strict,
19586 int flags,
19587 vec<deferred_access_check, va_gc> **checks,
19588 bool explain_p)
19590 tree parm, arg;
19591 int i;
19592 int ntparms = TREE_VEC_LENGTH (tparms);
19593 int saw_undeduced = 0;
19594 tree parms;
19595 const tree *args;
19596 unsigned int nargs;
19597 unsigned int ia;
19599 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19600 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19601 gcc_assert (ntparms > 0);
19603 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19605 /* Reset the number of non-defaulted template arguments contained
19606 in TARGS. */
19607 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19609 again:
19610 parms = xparms;
19611 args = xargs;
19612 nargs = xnargs;
19614 ia = 0;
19615 while (parms && parms != void_list_node
19616 && ia < nargs)
19618 parm = TREE_VALUE (parms);
19620 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19621 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19622 /* For a function parameter pack that occurs at the end of the
19623 parameter-declaration-list, the type A of each remaining
19624 argument of the call is compared with the type P of the
19625 declarator-id of the function parameter pack. */
19626 break;
19628 parms = TREE_CHAIN (parms);
19630 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19631 /* For a function parameter pack that does not occur at the
19632 end of the parameter-declaration-list, the type of the
19633 parameter pack is a non-deduced context. */
19634 continue;
19636 arg = args[ia];
19637 ++ia;
19639 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19640 explain_p))
19641 return 1;
19644 if (parms
19645 && parms != void_list_node
19646 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19648 /* Unify the remaining arguments with the pack expansion type. */
19649 tree argvec;
19650 tree parmvec = make_tree_vec (1);
19652 /* Allocate a TREE_VEC and copy in all of the arguments */
19653 argvec = make_tree_vec (nargs - ia);
19654 for (i = 0; ia < nargs; ++ia, ++i)
19655 TREE_VEC_ELT (argvec, i) = args[ia];
19657 /* Copy the parameter into parmvec. */
19658 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19659 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19660 /*subr=*/subr, explain_p))
19661 return 1;
19663 /* Advance to the end of the list of parameters. */
19664 parms = TREE_CHAIN (parms);
19667 /* Fail if we've reached the end of the parm list, and more args
19668 are present, and the parm list isn't variadic. */
19669 if (ia < nargs && parms == void_list_node)
19670 return unify_too_many_arguments (explain_p, nargs, ia);
19671 /* Fail if parms are left and they don't have default values and
19672 they aren't all deduced as empty packs (c++/57397). This is
19673 consistent with sufficient_parms_p. */
19674 if (parms && parms != void_list_node
19675 && TREE_PURPOSE (parms) == NULL_TREE)
19677 unsigned int count = nargs;
19678 tree p = parms;
19679 bool type_pack_p;
19682 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19683 if (!type_pack_p)
19684 count++;
19685 p = TREE_CHAIN (p);
19687 while (p && p != void_list_node);
19688 if (count != nargs)
19689 return unify_too_few_arguments (explain_p, ia, count,
19690 type_pack_p);
19693 if (!subr)
19695 tsubst_flags_t complain = (explain_p
19696 ? tf_warning_or_error
19697 : tf_none);
19698 bool tried_array_deduction = (cxx_dialect < cxx17);
19700 for (i = 0; i < ntparms; i++)
19702 tree targ = TREE_VEC_ELT (targs, i);
19703 tree tparm = TREE_VEC_ELT (tparms, i);
19705 /* Clear the "incomplete" flags on all argument packs now so that
19706 substituting them into later default arguments works. */
19707 if (targ && ARGUMENT_PACK_P (targ))
19709 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19710 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19713 if (targ || tparm == error_mark_node)
19714 continue;
19715 tparm = TREE_VALUE (tparm);
19717 if (TREE_CODE (tparm) == TYPE_DECL
19718 && !tried_array_deduction)
19720 try_array_deduction (tparms, targs, xparms);
19721 tried_array_deduction = true;
19722 if (TREE_VEC_ELT (targs, i))
19723 continue;
19726 /* If this is an undeduced nontype parameter that depends on
19727 a type parameter, try another pass; its type may have been
19728 deduced from a later argument than the one from which
19729 this parameter can be deduced. */
19730 if (TREE_CODE (tparm) == PARM_DECL
19731 && uses_template_parms (TREE_TYPE (tparm))
19732 && saw_undeduced < 2)
19734 saw_undeduced = 1;
19735 continue;
19738 /* Core issue #226 (C++0x) [temp.deduct]:
19740 If a template argument has not been deduced, its
19741 default template argument, if any, is used.
19743 When we are in C++98 mode, TREE_PURPOSE will either
19744 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19745 to explicitly check cxx_dialect here. */
19746 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19747 /* OK, there is a default argument. Wait until after the
19748 conversion check to do substitution. */
19749 continue;
19751 /* If the type parameter is a parameter pack, then it will
19752 be deduced to an empty parameter pack. */
19753 if (template_parameter_pack_p (tparm))
19755 tree arg;
19757 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19759 arg = make_node (NONTYPE_ARGUMENT_PACK);
19760 TREE_CONSTANT (arg) = 1;
19762 else
19763 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19765 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19767 TREE_VEC_ELT (targs, i) = arg;
19768 continue;
19771 return unify_parameter_deduction_failure (explain_p, tparm);
19774 /* DR 1391: All parameters have args, now check non-dependent parms for
19775 convertibility. */
19776 if (saw_undeduced < 2)
19777 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19778 parms && parms != void_list_node && ia < nargs; )
19780 parm = TREE_VALUE (parms);
19782 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19783 && (!TREE_CHAIN (parms)
19784 || TREE_CHAIN (parms) == void_list_node))
19785 /* For a function parameter pack that occurs at the end of the
19786 parameter-declaration-list, the type A of each remaining
19787 argument of the call is compared with the type P of the
19788 declarator-id of the function parameter pack. */
19789 break;
19791 parms = TREE_CHAIN (parms);
19793 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19794 /* For a function parameter pack that does not occur at the
19795 end of the parameter-declaration-list, the type of the
19796 parameter pack is a non-deduced context. */
19797 continue;
19799 arg = args[ia];
19800 ++ia;
19802 if (uses_template_parms (parm))
19803 continue;
19804 if (check_non_deducible_conversion (parm, arg, strict, flags,
19805 explain_p))
19806 return 1;
19809 /* Now substitute into the default template arguments. */
19810 for (i = 0; i < ntparms; i++)
19812 tree targ = TREE_VEC_ELT (targs, i);
19813 tree tparm = TREE_VEC_ELT (tparms, i);
19815 if (targ || tparm == error_mark_node)
19816 continue;
19817 tree parm = TREE_VALUE (tparm);
19819 if (TREE_CODE (parm) == PARM_DECL
19820 && uses_template_parms (TREE_TYPE (parm))
19821 && saw_undeduced < 2)
19822 continue;
19824 tree arg = TREE_PURPOSE (tparm);
19825 reopen_deferring_access_checks (*checks);
19826 location_t save_loc = input_location;
19827 if (DECL_P (parm))
19828 input_location = DECL_SOURCE_LOCATION (parm);
19829 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19830 if (!uses_template_parms (arg))
19831 arg = convert_template_argument (parm, arg, full_targs, complain,
19832 i, NULL_TREE);
19833 else if (saw_undeduced < 2)
19834 arg = NULL_TREE;
19835 else
19836 arg = error_mark_node;
19837 input_location = save_loc;
19838 *checks = get_deferred_access_checks ();
19839 pop_deferring_access_checks ();
19840 if (arg == error_mark_node)
19841 return 1;
19842 else if (arg)
19844 TREE_VEC_ELT (targs, i) = arg;
19845 /* The position of the first default template argument,
19846 is also the number of non-defaulted arguments in TARGS.
19847 Record that. */
19848 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19853 if (saw_undeduced++ == 1)
19854 goto again;
19857 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19860 return unify_success (explain_p);
19863 /* Subroutine of type_unification_real. Args are like the variables
19864 at the call site. ARG is an overloaded function (or template-id);
19865 we try deducing template args from each of the overloads, and if
19866 only one succeeds, we go with that. Modifies TARGS and returns
19867 true on success. */
19869 static bool
19870 resolve_overloaded_unification (tree tparms,
19871 tree targs,
19872 tree parm,
19873 tree arg,
19874 unification_kind_t strict,
19875 int sub_strict,
19876 bool explain_p)
19878 tree tempargs = copy_node (targs);
19879 int good = 0;
19880 tree goodfn = NULL_TREE;
19881 bool addr_p;
19883 if (TREE_CODE (arg) == ADDR_EXPR)
19885 arg = TREE_OPERAND (arg, 0);
19886 addr_p = true;
19888 else
19889 addr_p = false;
19891 if (TREE_CODE (arg) == COMPONENT_REF)
19892 /* Handle `&x' where `x' is some static or non-static member
19893 function name. */
19894 arg = TREE_OPERAND (arg, 1);
19896 if (TREE_CODE (arg) == OFFSET_REF)
19897 arg = TREE_OPERAND (arg, 1);
19899 /* Strip baselink information. */
19900 if (BASELINK_P (arg))
19901 arg = BASELINK_FUNCTIONS (arg);
19903 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19905 /* If we got some explicit template args, we need to plug them into
19906 the affected templates before we try to unify, in case the
19907 explicit args will completely resolve the templates in question. */
19909 int ok = 0;
19910 tree expl_subargs = TREE_OPERAND (arg, 1);
19911 arg = TREE_OPERAND (arg, 0);
19913 for (lkp_iterator iter (arg); iter; ++iter)
19915 tree fn = *iter;
19916 tree subargs, elem;
19918 if (TREE_CODE (fn) != TEMPLATE_DECL)
19919 continue;
19921 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19922 expl_subargs, NULL_TREE, tf_none,
19923 /*require_all_args=*/true,
19924 /*use_default_args=*/true);
19925 if (subargs != error_mark_node
19926 && !any_dependent_template_arguments_p (subargs))
19928 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19929 if (try_one_overload (tparms, targs, tempargs, parm,
19930 elem, strict, sub_strict, addr_p, explain_p)
19931 && (!goodfn || !same_type_p (goodfn, elem)))
19933 goodfn = elem;
19934 ++good;
19937 else if (subargs)
19938 ++ok;
19940 /* If no templates (or more than one) are fully resolved by the
19941 explicit arguments, this template-id is a non-deduced context; it
19942 could still be OK if we deduce all template arguments for the
19943 enclosing call through other arguments. */
19944 if (good != 1)
19945 good = ok;
19947 else if (TREE_CODE (arg) != OVERLOAD
19948 && TREE_CODE (arg) != FUNCTION_DECL)
19949 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19950 -- but the deduction does not succeed because the expression is
19951 not just the function on its own. */
19952 return false;
19953 else
19954 for (lkp_iterator iter (arg); iter; ++iter)
19956 tree fn = *iter;
19957 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19958 strict, sub_strict, addr_p, explain_p)
19959 && (!goodfn || !decls_match (goodfn, fn)))
19961 goodfn = fn;
19962 ++good;
19966 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19967 to function or pointer to member function argument if the set of
19968 overloaded functions does not contain function templates and at most
19969 one of a set of overloaded functions provides a unique match.
19971 So if we found multiple possibilities, we return success but don't
19972 deduce anything. */
19974 if (good == 1)
19976 int i = TREE_VEC_LENGTH (targs);
19977 for (; i--; )
19978 if (TREE_VEC_ELT (tempargs, i))
19980 tree old = TREE_VEC_ELT (targs, i);
19981 tree new_ = TREE_VEC_ELT (tempargs, i);
19982 if (new_ && old && ARGUMENT_PACK_P (old)
19983 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19984 /* Don't forget explicit template arguments in a pack. */
19985 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19986 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19987 TREE_VEC_ELT (targs, i) = new_;
19990 if (good)
19991 return true;
19993 return false;
19996 /* Core DR 115: In contexts where deduction is done and fails, or in
19997 contexts where deduction is not done, if a template argument list is
19998 specified and it, along with any default template arguments, identifies
19999 a single function template specialization, then the template-id is an
20000 lvalue for the function template specialization. */
20002 tree
20003 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20005 tree expr, offset, baselink;
20006 bool addr;
20008 if (!type_unknown_p (orig_expr))
20009 return orig_expr;
20011 expr = orig_expr;
20012 addr = false;
20013 offset = NULL_TREE;
20014 baselink = NULL_TREE;
20016 if (TREE_CODE (expr) == ADDR_EXPR)
20018 expr = TREE_OPERAND (expr, 0);
20019 addr = true;
20021 if (TREE_CODE (expr) == OFFSET_REF)
20023 offset = expr;
20024 expr = TREE_OPERAND (expr, 1);
20026 if (BASELINK_P (expr))
20028 baselink = expr;
20029 expr = BASELINK_FUNCTIONS (expr);
20032 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20034 int good = 0;
20035 tree goodfn = NULL_TREE;
20037 /* If we got some explicit template args, we need to plug them into
20038 the affected templates before we try to unify, in case the
20039 explicit args will completely resolve the templates in question. */
20041 tree expl_subargs = TREE_OPERAND (expr, 1);
20042 tree arg = TREE_OPERAND (expr, 0);
20043 tree badfn = NULL_TREE;
20044 tree badargs = NULL_TREE;
20046 for (lkp_iterator iter (arg); iter; ++iter)
20048 tree fn = *iter;
20049 tree subargs, elem;
20051 if (TREE_CODE (fn) != TEMPLATE_DECL)
20052 continue;
20054 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20055 expl_subargs, NULL_TREE, tf_none,
20056 /*require_all_args=*/true,
20057 /*use_default_args=*/true);
20058 if (subargs != error_mark_node
20059 && !any_dependent_template_arguments_p (subargs))
20061 elem = instantiate_template (fn, subargs, tf_none);
20062 if (elem == error_mark_node)
20064 badfn = fn;
20065 badargs = subargs;
20067 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20069 goodfn = elem;
20070 ++good;
20074 if (good == 1)
20076 mark_used (goodfn);
20077 expr = goodfn;
20078 if (baselink)
20079 expr = build_baselink (BASELINK_BINFO (baselink),
20080 BASELINK_ACCESS_BINFO (baselink),
20081 expr, BASELINK_OPTYPE (baselink));
20082 if (offset)
20084 tree base
20085 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20086 expr = build_offset_ref (base, expr, addr, complain);
20088 if (addr)
20089 expr = cp_build_addr_expr (expr, complain);
20090 return expr;
20092 else if (good == 0 && badargs && (complain & tf_error))
20093 /* There were no good options and at least one bad one, so let the
20094 user know what the problem is. */
20095 instantiate_template (badfn, badargs, complain);
20097 return orig_expr;
20100 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20101 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20102 different overloads deduce different arguments for a given parm.
20103 ADDR_P is true if the expression for which deduction is being
20104 performed was of the form "& fn" rather than simply "fn".
20106 Returns 1 on success. */
20108 static int
20109 try_one_overload (tree tparms,
20110 tree orig_targs,
20111 tree targs,
20112 tree parm,
20113 tree arg,
20114 unification_kind_t strict,
20115 int sub_strict,
20116 bool addr_p,
20117 bool explain_p)
20119 int nargs;
20120 tree tempargs;
20121 int i;
20123 if (arg == error_mark_node)
20124 return 0;
20126 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20127 to function or pointer to member function argument if the set of
20128 overloaded functions does not contain function templates and at most
20129 one of a set of overloaded functions provides a unique match.
20131 So if this is a template, just return success. */
20133 if (uses_template_parms (arg))
20134 return 1;
20136 if (TREE_CODE (arg) == METHOD_TYPE)
20137 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20138 else if (addr_p)
20139 arg = build_pointer_type (arg);
20141 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20143 /* We don't copy orig_targs for this because if we have already deduced
20144 some template args from previous args, unify would complain when we
20145 try to deduce a template parameter for the same argument, even though
20146 there isn't really a conflict. */
20147 nargs = TREE_VEC_LENGTH (targs);
20148 tempargs = make_tree_vec (nargs);
20150 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20151 return 0;
20153 /* First make sure we didn't deduce anything that conflicts with
20154 explicitly specified args. */
20155 for (i = nargs; i--; )
20157 tree elt = TREE_VEC_ELT (tempargs, i);
20158 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20160 if (!elt)
20161 /*NOP*/;
20162 else if (uses_template_parms (elt))
20163 /* Since we're unifying against ourselves, we will fill in
20164 template args used in the function parm list with our own
20165 template parms. Discard them. */
20166 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20167 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20169 /* Check that the argument at each index of the deduced argument pack
20170 is equivalent to the corresponding explicitly specified argument.
20171 We may have deduced more arguments than were explicitly specified,
20172 and that's OK. */
20174 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20175 that's wrong if we deduce the same argument pack from multiple
20176 function arguments: it's only incomplete the first time. */
20178 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20179 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20181 if (TREE_VEC_LENGTH (deduced_pack)
20182 < TREE_VEC_LENGTH (explicit_pack))
20183 return 0;
20185 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20186 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20187 TREE_VEC_ELT (deduced_pack, j)))
20188 return 0;
20190 else if (oldelt && !template_args_equal (oldelt, elt))
20191 return 0;
20194 for (i = nargs; i--; )
20196 tree elt = TREE_VEC_ELT (tempargs, i);
20198 if (elt)
20199 TREE_VEC_ELT (targs, i) = elt;
20202 return 1;
20205 /* PARM is a template class (perhaps with unbound template
20206 parameters). ARG is a fully instantiated type. If ARG can be
20207 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20208 TARGS are as for unify. */
20210 static tree
20211 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20212 bool explain_p)
20214 tree copy_of_targs;
20216 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20217 return NULL_TREE;
20218 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20219 /* Matches anything. */;
20220 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20221 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20222 return NULL_TREE;
20224 /* We need to make a new template argument vector for the call to
20225 unify. If we used TARGS, we'd clutter it up with the result of
20226 the attempted unification, even if this class didn't work out.
20227 We also don't want to commit ourselves to all the unifications
20228 we've already done, since unification is supposed to be done on
20229 an argument-by-argument basis. In other words, consider the
20230 following pathological case:
20232 template <int I, int J, int K>
20233 struct S {};
20235 template <int I, int J>
20236 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20238 template <int I, int J, int K>
20239 void f(S<I, J, K>, S<I, I, I>);
20241 void g() {
20242 S<0, 0, 0> s0;
20243 S<0, 1, 2> s2;
20245 f(s0, s2);
20248 Now, by the time we consider the unification involving `s2', we
20249 already know that we must have `f<0, 0, 0>'. But, even though
20250 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20251 because there are two ways to unify base classes of S<0, 1, 2>
20252 with S<I, I, I>. If we kept the already deduced knowledge, we
20253 would reject the possibility I=1. */
20254 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20256 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20258 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20259 return NULL_TREE;
20260 return arg;
20263 /* If unification failed, we're done. */
20264 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20265 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20266 return NULL_TREE;
20268 return arg;
20271 /* Given a template type PARM and a class type ARG, find the unique
20272 base type in ARG that is an instance of PARM. We do not examine
20273 ARG itself; only its base-classes. If there is not exactly one
20274 appropriate base class, return NULL_TREE. PARM may be the type of
20275 a partial specialization, as well as a plain template type. Used
20276 by unify. */
20278 static enum template_base_result
20279 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20280 bool explain_p, tree *result)
20282 tree rval = NULL_TREE;
20283 tree binfo;
20285 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20287 binfo = TYPE_BINFO (complete_type (arg));
20288 if (!binfo)
20290 /* The type could not be completed. */
20291 *result = NULL_TREE;
20292 return tbr_incomplete_type;
20295 /* Walk in inheritance graph order. The search order is not
20296 important, and this avoids multiple walks of virtual bases. */
20297 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20299 tree r = try_class_unification (tparms, targs, parm,
20300 BINFO_TYPE (binfo), explain_p);
20302 if (r)
20304 /* If there is more than one satisfactory baseclass, then:
20306 [temp.deduct.call]
20308 If they yield more than one possible deduced A, the type
20309 deduction fails.
20311 applies. */
20312 if (rval && !same_type_p (r, rval))
20314 *result = NULL_TREE;
20315 return tbr_ambiguous_baseclass;
20318 rval = r;
20322 *result = rval;
20323 return tbr_success;
20326 /* Returns the level of DECL, which declares a template parameter. */
20328 static int
20329 template_decl_level (tree decl)
20331 switch (TREE_CODE (decl))
20333 case TYPE_DECL:
20334 case TEMPLATE_DECL:
20335 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20337 case PARM_DECL:
20338 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20340 default:
20341 gcc_unreachable ();
20343 return 0;
20346 /* Decide whether ARG can be unified with PARM, considering only the
20347 cv-qualifiers of each type, given STRICT as documented for unify.
20348 Returns nonzero iff the unification is OK on that basis. */
20350 static int
20351 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20353 int arg_quals = cp_type_quals (arg);
20354 int parm_quals = cp_type_quals (parm);
20356 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20357 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20359 /* Although a CVR qualifier is ignored when being applied to a
20360 substituted template parameter ([8.3.2]/1 for example), that
20361 does not allow us to unify "const T" with "int&" because both
20362 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20363 It is ok when we're allowing additional CV qualifiers
20364 at the outer level [14.8.2.1]/3,1st bullet. */
20365 if ((TREE_CODE (arg) == REFERENCE_TYPE
20366 || TREE_CODE (arg) == FUNCTION_TYPE
20367 || TREE_CODE (arg) == METHOD_TYPE)
20368 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20369 return 0;
20371 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20372 && (parm_quals & TYPE_QUAL_RESTRICT))
20373 return 0;
20376 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20377 && (arg_quals & parm_quals) != parm_quals)
20378 return 0;
20380 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20381 && (parm_quals & arg_quals) != arg_quals)
20382 return 0;
20384 return 1;
20387 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20388 void
20389 template_parm_level_and_index (tree parm, int* level, int* index)
20391 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20392 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20393 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20395 *index = TEMPLATE_TYPE_IDX (parm);
20396 *level = TEMPLATE_TYPE_LEVEL (parm);
20398 else
20400 *index = TEMPLATE_PARM_IDX (parm);
20401 *level = TEMPLATE_PARM_LEVEL (parm);
20405 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20406 do { \
20407 if (unify (TP, TA, P, A, S, EP)) \
20408 return 1; \
20409 } while (0)
20411 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20412 expansion at the end of PACKED_PARMS. Returns 0 if the type
20413 deduction succeeds, 1 otherwise. STRICT is the same as in
20414 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20415 function call argument list. We'll need to adjust the arguments to make them
20416 types. SUBR tells us if this is from a recursive call to
20417 type_unification_real, or for comparing two template argument
20418 lists. */
20420 static int
20421 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20422 tree packed_args, unification_kind_t strict,
20423 bool subr, bool explain_p)
20425 tree parm
20426 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20427 tree pattern = PACK_EXPANSION_PATTERN (parm);
20428 tree pack, packs = NULL_TREE;
20429 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20431 /* Add in any args remembered from an earlier partial instantiation. */
20432 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20433 int levels = TMPL_ARGS_DEPTH (targs);
20435 packed_args = expand_template_argument_pack (packed_args);
20437 int len = TREE_VEC_LENGTH (packed_args);
20439 /* Determine the parameter packs we will be deducing from the
20440 pattern, and record their current deductions. */
20441 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20442 pack; pack = TREE_CHAIN (pack))
20444 tree parm_pack = TREE_VALUE (pack);
20445 int idx, level;
20447 /* Determine the index and level of this parameter pack. */
20448 template_parm_level_and_index (parm_pack, &level, &idx);
20449 if (level < levels)
20450 continue;
20452 /* Keep track of the parameter packs and their corresponding
20453 argument packs. */
20454 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20455 TREE_TYPE (packs) = make_tree_vec (len - start);
20458 /* Loop through all of the arguments that have not yet been
20459 unified and unify each with the pattern. */
20460 for (i = start; i < len; i++)
20462 tree parm;
20463 bool any_explicit = false;
20464 tree arg = TREE_VEC_ELT (packed_args, i);
20466 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20467 or the element of its argument pack at the current index if
20468 this argument was explicitly specified. */
20469 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20471 int idx, level;
20472 tree arg, pargs;
20473 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20475 arg = NULL_TREE;
20476 if (TREE_VALUE (pack)
20477 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20478 && (i - start < TREE_VEC_LENGTH (pargs)))
20480 any_explicit = true;
20481 arg = TREE_VEC_ELT (pargs, i - start);
20483 TMPL_ARG (targs, level, idx) = arg;
20486 /* If we had explicit template arguments, substitute them into the
20487 pattern before deduction. */
20488 if (any_explicit)
20490 /* Some arguments might still be unspecified or dependent. */
20491 bool dependent;
20492 ++processing_template_decl;
20493 dependent = any_dependent_template_arguments_p (targs);
20494 if (!dependent)
20495 --processing_template_decl;
20496 parm = tsubst (pattern, targs,
20497 explain_p ? tf_warning_or_error : tf_none,
20498 NULL_TREE);
20499 if (dependent)
20500 --processing_template_decl;
20501 if (parm == error_mark_node)
20502 return 1;
20504 else
20505 parm = pattern;
20507 /* Unify the pattern with the current argument. */
20508 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20509 explain_p))
20510 return 1;
20512 /* For each parameter pack, collect the deduced value. */
20513 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20515 int idx, level;
20516 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20518 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20519 TMPL_ARG (targs, level, idx);
20523 /* Verify that the results of unification with the parameter packs
20524 produce results consistent with what we've seen before, and make
20525 the deduced argument packs available. */
20526 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20528 tree old_pack = TREE_VALUE (pack);
20529 tree new_args = TREE_TYPE (pack);
20530 int i, len = TREE_VEC_LENGTH (new_args);
20531 int idx, level;
20532 bool nondeduced_p = false;
20534 /* By default keep the original deduced argument pack.
20535 If necessary, more specific code is going to update the
20536 resulting deduced argument later down in this function. */
20537 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20538 TMPL_ARG (targs, level, idx) = old_pack;
20540 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20541 actually deduce anything. */
20542 for (i = 0; i < len && !nondeduced_p; ++i)
20543 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20544 nondeduced_p = true;
20545 if (nondeduced_p)
20546 continue;
20548 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20550 /* If we had fewer function args than explicit template args,
20551 just use the explicits. */
20552 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20553 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20554 if (len < explicit_len)
20555 new_args = explicit_args;
20558 if (!old_pack)
20560 tree result;
20561 /* Build the deduced *_ARGUMENT_PACK. */
20562 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20564 result = make_node (NONTYPE_ARGUMENT_PACK);
20565 TREE_CONSTANT (result) = 1;
20567 else
20568 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20570 SET_ARGUMENT_PACK_ARGS (result, new_args);
20572 /* Note the deduced argument packs for this parameter
20573 pack. */
20574 TMPL_ARG (targs, level, idx) = result;
20576 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20577 && (ARGUMENT_PACK_ARGS (old_pack)
20578 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20580 /* We only had the explicitly-provided arguments before, but
20581 now we have a complete set of arguments. */
20582 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20584 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20585 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20586 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20588 else
20590 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20591 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20593 if (!comp_template_args (old_args, new_args,
20594 &bad_old_arg, &bad_new_arg))
20595 /* Inconsistent unification of this parameter pack. */
20596 return unify_parameter_pack_inconsistent (explain_p,
20597 bad_old_arg,
20598 bad_new_arg);
20602 return unify_success (explain_p);
20605 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20606 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20607 parameters and return value are as for unify. */
20609 static int
20610 unify_array_domain (tree tparms, tree targs,
20611 tree parm_dom, tree arg_dom,
20612 bool explain_p)
20614 tree parm_max;
20615 tree arg_max;
20616 bool parm_cst;
20617 bool arg_cst;
20619 /* Our representation of array types uses "N - 1" as the
20620 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20621 not an integer constant. We cannot unify arbitrarily
20622 complex expressions, so we eliminate the MINUS_EXPRs
20623 here. */
20624 parm_max = TYPE_MAX_VALUE (parm_dom);
20625 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20626 if (!parm_cst)
20628 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20629 parm_max = TREE_OPERAND (parm_max, 0);
20631 arg_max = TYPE_MAX_VALUE (arg_dom);
20632 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20633 if (!arg_cst)
20635 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20636 trying to unify the type of a variable with the type
20637 of a template parameter. For example:
20639 template <unsigned int N>
20640 void f (char (&) [N]);
20641 int g();
20642 void h(int i) {
20643 char a[g(i)];
20644 f(a);
20647 Here, the type of the ARG will be "int [g(i)]", and
20648 may be a SAVE_EXPR, etc. */
20649 if (TREE_CODE (arg_max) != MINUS_EXPR)
20650 return unify_vla_arg (explain_p, arg_dom);
20651 arg_max = TREE_OPERAND (arg_max, 0);
20654 /* If only one of the bounds used a MINUS_EXPR, compensate
20655 by adding one to the other bound. */
20656 if (parm_cst && !arg_cst)
20657 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20658 integer_type_node,
20659 parm_max,
20660 integer_one_node);
20661 else if (arg_cst && !parm_cst)
20662 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20663 integer_type_node,
20664 arg_max,
20665 integer_one_node);
20667 return unify (tparms, targs, parm_max, arg_max,
20668 UNIFY_ALLOW_INTEGER, explain_p);
20671 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20673 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20675 static pa_kind_t
20676 pa_kind (tree t)
20678 if (PACK_EXPANSION_P (t))
20679 t = PACK_EXPANSION_PATTERN (t);
20680 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20681 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20682 || DECL_TYPE_TEMPLATE_P (t))
20683 return pa_tmpl;
20684 else if (TYPE_P (t))
20685 return pa_type;
20686 else
20687 return pa_expr;
20690 /* Deduce the value of template parameters. TPARMS is the (innermost)
20691 set of template parameters to a template. TARGS is the bindings
20692 for those template parameters, as determined thus far; TARGS may
20693 include template arguments for outer levels of template parameters
20694 as well. PARM is a parameter to a template function, or a
20695 subcomponent of that parameter; ARG is the corresponding argument.
20696 This function attempts to match PARM with ARG in a manner
20697 consistent with the existing assignments in TARGS. If more values
20698 are deduced, then TARGS is updated.
20700 Returns 0 if the type deduction succeeds, 1 otherwise. The
20701 parameter STRICT is a bitwise or of the following flags:
20703 UNIFY_ALLOW_NONE:
20704 Require an exact match between PARM and ARG.
20705 UNIFY_ALLOW_MORE_CV_QUAL:
20706 Allow the deduced ARG to be more cv-qualified (by qualification
20707 conversion) than ARG.
20708 UNIFY_ALLOW_LESS_CV_QUAL:
20709 Allow the deduced ARG to be less cv-qualified than ARG.
20710 UNIFY_ALLOW_DERIVED:
20711 Allow the deduced ARG to be a template base class of ARG,
20712 or a pointer to a template base class of the type pointed to by
20713 ARG.
20714 UNIFY_ALLOW_INTEGER:
20715 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20716 case for more information.
20717 UNIFY_ALLOW_OUTER_LEVEL:
20718 This is the outermost level of a deduction. Used to determine validity
20719 of qualification conversions. A valid qualification conversion must
20720 have const qualified pointers leading up to the inner type which
20721 requires additional CV quals, except at the outer level, where const
20722 is not required [conv.qual]. It would be normal to set this flag in
20723 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20724 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20725 This is the outermost level of a deduction, and PARM can be more CV
20726 qualified at this point.
20727 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20728 This is the outermost level of a deduction, and PARM can be less CV
20729 qualified at this point. */
20731 static int
20732 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20733 bool explain_p)
20735 int idx;
20736 tree targ;
20737 tree tparm;
20738 int strict_in = strict;
20739 tsubst_flags_t complain = (explain_p
20740 ? tf_warning_or_error
20741 : tf_none);
20743 /* I don't think this will do the right thing with respect to types.
20744 But the only case I've seen it in so far has been array bounds, where
20745 signedness is the only information lost, and I think that will be
20746 okay. */
20747 while (CONVERT_EXPR_P (parm))
20748 parm = TREE_OPERAND (parm, 0);
20750 if (arg == error_mark_node)
20751 return unify_invalid (explain_p);
20752 if (arg == unknown_type_node
20753 || arg == init_list_type_node)
20754 /* We can't deduce anything from this, but we might get all the
20755 template args from other function args. */
20756 return unify_success (explain_p);
20758 if (parm == any_targ_node || arg == any_targ_node)
20759 return unify_success (explain_p);
20761 /* If PARM uses template parameters, then we can't bail out here,
20762 even if ARG == PARM, since we won't record unifications for the
20763 template parameters. We might need them if we're trying to
20764 figure out which of two things is more specialized. */
20765 if (arg == parm && !uses_template_parms (parm))
20766 return unify_success (explain_p);
20768 /* Handle init lists early, so the rest of the function can assume
20769 we're dealing with a type. */
20770 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20772 tree elt, elttype;
20773 unsigned i;
20774 tree orig_parm = parm;
20776 /* Replace T with std::initializer_list<T> for deduction. */
20777 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20778 && flag_deduce_init_list)
20779 parm = listify (parm);
20781 if (!is_std_init_list (parm)
20782 && TREE_CODE (parm) != ARRAY_TYPE)
20783 /* We can only deduce from an initializer list argument if the
20784 parameter is std::initializer_list or an array; otherwise this
20785 is a non-deduced context. */
20786 return unify_success (explain_p);
20788 if (TREE_CODE (parm) == ARRAY_TYPE)
20789 elttype = TREE_TYPE (parm);
20790 else
20792 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20793 /* Deduction is defined in terms of a single type, so just punt
20794 on the (bizarre) std::initializer_list<T...>. */
20795 if (PACK_EXPANSION_P (elttype))
20796 return unify_success (explain_p);
20799 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20801 int elt_strict = strict;
20803 if (elt == error_mark_node)
20804 return unify_invalid (explain_p);
20806 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20808 tree type = TREE_TYPE (elt);
20809 if (type == error_mark_node)
20810 return unify_invalid (explain_p);
20811 /* It should only be possible to get here for a call. */
20812 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20813 elt_strict |= maybe_adjust_types_for_deduction
20814 (DEDUCE_CALL, &elttype, &type, elt);
20815 elt = type;
20818 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20819 explain_p);
20822 if (TREE_CODE (parm) == ARRAY_TYPE
20823 && deducible_array_bound (TYPE_DOMAIN (parm)))
20825 /* Also deduce from the length of the initializer list. */
20826 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20827 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20828 if (idx == error_mark_node)
20829 return unify_invalid (explain_p);
20830 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20831 idx, explain_p);
20834 /* If the std::initializer_list<T> deduction worked, replace the
20835 deduced A with std::initializer_list<A>. */
20836 if (orig_parm != parm)
20838 idx = TEMPLATE_TYPE_IDX (orig_parm);
20839 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20840 targ = listify (targ);
20841 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20843 return unify_success (explain_p);
20846 /* If parm and arg aren't the same kind of thing (template, type, or
20847 expression), fail early. */
20848 if (pa_kind (parm) != pa_kind (arg))
20849 return unify_invalid (explain_p);
20851 /* Immediately reject some pairs that won't unify because of
20852 cv-qualification mismatches. */
20853 if (TREE_CODE (arg) == TREE_CODE (parm)
20854 && TYPE_P (arg)
20855 /* It is the elements of the array which hold the cv quals of an array
20856 type, and the elements might be template type parms. We'll check
20857 when we recurse. */
20858 && TREE_CODE (arg) != ARRAY_TYPE
20859 /* We check the cv-qualifiers when unifying with template type
20860 parameters below. We want to allow ARG `const T' to unify with
20861 PARM `T' for example, when computing which of two templates
20862 is more specialized, for example. */
20863 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20864 && !check_cv_quals_for_unify (strict_in, arg, parm))
20865 return unify_cv_qual_mismatch (explain_p, parm, arg);
20867 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20868 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20869 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20870 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20871 strict &= ~UNIFY_ALLOW_DERIVED;
20872 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20873 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20875 switch (TREE_CODE (parm))
20877 case TYPENAME_TYPE:
20878 case SCOPE_REF:
20879 case UNBOUND_CLASS_TEMPLATE:
20880 /* In a type which contains a nested-name-specifier, template
20881 argument values cannot be deduced for template parameters used
20882 within the nested-name-specifier. */
20883 return unify_success (explain_p);
20885 case TEMPLATE_TYPE_PARM:
20886 case TEMPLATE_TEMPLATE_PARM:
20887 case BOUND_TEMPLATE_TEMPLATE_PARM:
20888 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20889 if (error_operand_p (tparm))
20890 return unify_invalid (explain_p);
20892 if (TEMPLATE_TYPE_LEVEL (parm)
20893 != template_decl_level (tparm))
20894 /* The PARM is not one we're trying to unify. Just check
20895 to see if it matches ARG. */
20897 if (TREE_CODE (arg) == TREE_CODE (parm)
20898 && (is_auto (parm) ? is_auto (arg)
20899 : same_type_p (parm, arg)))
20900 return unify_success (explain_p);
20901 else
20902 return unify_type_mismatch (explain_p, parm, arg);
20904 idx = TEMPLATE_TYPE_IDX (parm);
20905 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20906 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20907 if (error_operand_p (tparm))
20908 return unify_invalid (explain_p);
20910 /* Check for mixed types and values. */
20911 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20912 && TREE_CODE (tparm) != TYPE_DECL)
20913 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20914 && TREE_CODE (tparm) != TEMPLATE_DECL))
20915 gcc_unreachable ();
20917 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20919 if ((strict_in & UNIFY_ALLOW_DERIVED)
20920 && CLASS_TYPE_P (arg))
20922 /* First try to match ARG directly. */
20923 tree t = try_class_unification (tparms, targs, parm, arg,
20924 explain_p);
20925 if (!t)
20927 /* Otherwise, look for a suitable base of ARG, as below. */
20928 enum template_base_result r;
20929 r = get_template_base (tparms, targs, parm, arg,
20930 explain_p, &t);
20931 if (!t)
20932 return unify_no_common_base (explain_p, r, parm, arg);
20933 arg = t;
20936 /* ARG must be constructed from a template class or a template
20937 template parameter. */
20938 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20939 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20940 return unify_template_deduction_failure (explain_p, parm, arg);
20942 /* Deduce arguments T, i from TT<T> or TT<i>. */
20943 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20944 return 1;
20946 arg = TYPE_TI_TEMPLATE (arg);
20948 /* Fall through to deduce template name. */
20951 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20952 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20954 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20956 /* Simple cases: Value already set, does match or doesn't. */
20957 if (targ != NULL_TREE && template_args_equal (targ, arg))
20958 return unify_success (explain_p);
20959 else if (targ)
20960 return unify_inconsistency (explain_p, parm, targ, arg);
20962 else
20964 /* If PARM is `const T' and ARG is only `int', we don't have
20965 a match unless we are allowing additional qualification.
20966 If ARG is `const int' and PARM is just `T' that's OK;
20967 that binds `const int' to `T'. */
20968 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20969 arg, parm))
20970 return unify_cv_qual_mismatch (explain_p, parm, arg);
20972 /* Consider the case where ARG is `const volatile int' and
20973 PARM is `const T'. Then, T should be `volatile int'. */
20974 arg = cp_build_qualified_type_real
20975 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20976 if (arg == error_mark_node)
20977 return unify_invalid (explain_p);
20979 /* Simple cases: Value already set, does match or doesn't. */
20980 if (targ != NULL_TREE && same_type_p (targ, arg))
20981 return unify_success (explain_p);
20982 else if (targ)
20983 return unify_inconsistency (explain_p, parm, targ, arg);
20985 /* Make sure that ARG is not a variable-sized array. (Note
20986 that were talking about variable-sized arrays (like
20987 `int[n]'), rather than arrays of unknown size (like
20988 `int[]').) We'll get very confused by such a type since
20989 the bound of the array is not constant, and therefore
20990 not mangleable. Besides, such types are not allowed in
20991 ISO C++, so we can do as we please here. We do allow
20992 them for 'auto' deduction, since that isn't ABI-exposed. */
20993 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20994 return unify_vla_arg (explain_p, arg);
20996 /* Strip typedefs as in convert_template_argument. */
20997 arg = canonicalize_type_argument (arg, tf_none);
21000 /* If ARG is a parameter pack or an expansion, we cannot unify
21001 against it unless PARM is also a parameter pack. */
21002 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21003 && !template_parameter_pack_p (parm))
21004 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21006 /* If the argument deduction results is a METHOD_TYPE,
21007 then there is a problem.
21008 METHOD_TYPE doesn't map to any real C++ type the result of
21009 the deduction can not be of that type. */
21010 if (TREE_CODE (arg) == METHOD_TYPE)
21011 return unify_method_type_error (explain_p, arg);
21013 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21014 return unify_success (explain_p);
21016 case TEMPLATE_PARM_INDEX:
21017 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21018 if (error_operand_p (tparm))
21019 return unify_invalid (explain_p);
21021 if (TEMPLATE_PARM_LEVEL (parm)
21022 != template_decl_level (tparm))
21024 /* The PARM is not one we're trying to unify. Just check
21025 to see if it matches ARG. */
21026 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21027 && cp_tree_equal (parm, arg));
21028 if (result)
21029 unify_expression_unequal (explain_p, parm, arg);
21030 return result;
21033 idx = TEMPLATE_PARM_IDX (parm);
21034 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21036 if (targ)
21038 if ((strict & UNIFY_ALLOW_INTEGER)
21039 && TREE_TYPE (targ) && TREE_TYPE (arg)
21040 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21041 /* We're deducing from an array bound, the type doesn't matter. */
21042 arg = fold_convert (TREE_TYPE (targ), arg);
21043 int x = !cp_tree_equal (targ, arg);
21044 if (x)
21045 unify_inconsistency (explain_p, parm, targ, arg);
21046 return x;
21049 /* [temp.deduct.type] If, in the declaration of a function template
21050 with a non-type template-parameter, the non-type
21051 template-parameter is used in an expression in the function
21052 parameter-list and, if the corresponding template-argument is
21053 deduced, the template-argument type shall match the type of the
21054 template-parameter exactly, except that a template-argument
21055 deduced from an array bound may be of any integral type.
21056 The non-type parameter might use already deduced type parameters. */
21057 ++processing_template_decl;
21058 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21059 --processing_template_decl;
21060 if (tree a = type_uses_auto (tparm))
21062 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21063 if (tparm == error_mark_node)
21064 return 1;
21067 if (!TREE_TYPE (arg))
21068 /* Template-parameter dependent expression. Just accept it for now.
21069 It will later be processed in convert_template_argument. */
21071 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21072 non_reference (tparm)))
21073 /* OK */;
21074 else if ((strict & UNIFY_ALLOW_INTEGER)
21075 && CP_INTEGRAL_TYPE_P (tparm))
21076 /* Convert the ARG to the type of PARM; the deduced non-type
21077 template argument must exactly match the types of the
21078 corresponding parameter. */
21079 arg = fold (build_nop (tparm, arg));
21080 else if (uses_template_parms (tparm))
21082 /* We haven't deduced the type of this parameter yet. */
21083 if (cxx_dialect >= cxx17
21084 /* We deduce from array bounds in try_array_deduction. */
21085 && !(strict & UNIFY_ALLOW_INTEGER))
21087 /* Deduce it from the non-type argument. */
21088 tree atype = TREE_TYPE (arg);
21089 RECUR_AND_CHECK_FAILURE (tparms, targs,
21090 tparm, atype,
21091 UNIFY_ALLOW_NONE, explain_p);
21093 else
21094 /* Try again later. */
21095 return unify_success (explain_p);
21097 else
21098 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21100 /* If ARG is a parameter pack or an expansion, we cannot unify
21101 against it unless PARM is also a parameter pack. */
21102 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21103 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21104 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21107 bool removed_attr = false;
21108 arg = strip_typedefs_expr (arg, &removed_attr);
21110 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21111 return unify_success (explain_p);
21113 case PTRMEM_CST:
21115 /* A pointer-to-member constant can be unified only with
21116 another constant. */
21117 if (TREE_CODE (arg) != PTRMEM_CST)
21118 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21120 /* Just unify the class member. It would be useless (and possibly
21121 wrong, depending on the strict flags) to unify also
21122 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21123 arg refer to the same variable, even if through different
21124 classes. For instance:
21126 struct A { int x; };
21127 struct B : A { };
21129 Unification of &A::x and &B::x must succeed. */
21130 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21131 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21134 case POINTER_TYPE:
21136 if (!TYPE_PTR_P (arg))
21137 return unify_type_mismatch (explain_p, parm, arg);
21139 /* [temp.deduct.call]
21141 A can be another pointer or pointer to member type that can
21142 be converted to the deduced A via a qualification
21143 conversion (_conv.qual_).
21145 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21146 This will allow for additional cv-qualification of the
21147 pointed-to types if appropriate. */
21149 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21150 /* The derived-to-base conversion only persists through one
21151 level of pointers. */
21152 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21154 return unify (tparms, targs, TREE_TYPE (parm),
21155 TREE_TYPE (arg), strict, explain_p);
21158 case REFERENCE_TYPE:
21159 if (TREE_CODE (arg) != REFERENCE_TYPE)
21160 return unify_type_mismatch (explain_p, parm, arg);
21161 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21162 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21164 case ARRAY_TYPE:
21165 if (TREE_CODE (arg) != ARRAY_TYPE)
21166 return unify_type_mismatch (explain_p, parm, arg);
21167 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21168 != (TYPE_DOMAIN (arg) == NULL_TREE))
21169 return unify_type_mismatch (explain_p, parm, arg);
21170 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21171 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21172 if (TYPE_DOMAIN (parm) != NULL_TREE)
21173 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21174 TYPE_DOMAIN (arg), explain_p);
21175 return unify_success (explain_p);
21177 case REAL_TYPE:
21178 case COMPLEX_TYPE:
21179 case VECTOR_TYPE:
21180 case INTEGER_TYPE:
21181 case BOOLEAN_TYPE:
21182 case ENUMERAL_TYPE:
21183 case VOID_TYPE:
21184 case NULLPTR_TYPE:
21185 if (TREE_CODE (arg) != TREE_CODE (parm))
21186 return unify_type_mismatch (explain_p, parm, arg);
21188 /* We have already checked cv-qualification at the top of the
21189 function. */
21190 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21191 return unify_type_mismatch (explain_p, parm, arg);
21193 /* As far as unification is concerned, this wins. Later checks
21194 will invalidate it if necessary. */
21195 return unify_success (explain_p);
21197 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21198 /* Type INTEGER_CST can come from ordinary constant template args. */
21199 case INTEGER_CST:
21200 while (CONVERT_EXPR_P (arg))
21201 arg = TREE_OPERAND (arg, 0);
21203 if (TREE_CODE (arg) != INTEGER_CST)
21204 return unify_template_argument_mismatch (explain_p, parm, arg);
21205 return (tree_int_cst_equal (parm, arg)
21206 ? unify_success (explain_p)
21207 : unify_template_argument_mismatch (explain_p, parm, arg));
21209 case TREE_VEC:
21211 int i, len, argslen;
21212 int parm_variadic_p = 0;
21214 if (TREE_CODE (arg) != TREE_VEC)
21215 return unify_template_argument_mismatch (explain_p, parm, arg);
21217 len = TREE_VEC_LENGTH (parm);
21218 argslen = TREE_VEC_LENGTH (arg);
21220 /* Check for pack expansions in the parameters. */
21221 for (i = 0; i < len; ++i)
21223 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21225 if (i == len - 1)
21226 /* We can unify against something with a trailing
21227 parameter pack. */
21228 parm_variadic_p = 1;
21229 else
21230 /* [temp.deduct.type]/9: If the template argument list of
21231 P contains a pack expansion that is not the last
21232 template argument, the entire template argument list
21233 is a non-deduced context. */
21234 return unify_success (explain_p);
21238 /* If we don't have enough arguments to satisfy the parameters
21239 (not counting the pack expression at the end), or we have
21240 too many arguments for a parameter list that doesn't end in
21241 a pack expression, we can't unify. */
21242 if (parm_variadic_p
21243 ? argslen < len - parm_variadic_p
21244 : argslen != len)
21245 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21247 /* Unify all of the parameters that precede the (optional)
21248 pack expression. */
21249 for (i = 0; i < len - parm_variadic_p; ++i)
21251 RECUR_AND_CHECK_FAILURE (tparms, targs,
21252 TREE_VEC_ELT (parm, i),
21253 TREE_VEC_ELT (arg, i),
21254 UNIFY_ALLOW_NONE, explain_p);
21256 if (parm_variadic_p)
21257 return unify_pack_expansion (tparms, targs, parm, arg,
21258 DEDUCE_EXACT,
21259 /*subr=*/true, explain_p);
21260 return unify_success (explain_p);
21263 case RECORD_TYPE:
21264 case UNION_TYPE:
21265 if (TREE_CODE (arg) != TREE_CODE (parm))
21266 return unify_type_mismatch (explain_p, parm, arg);
21268 if (TYPE_PTRMEMFUNC_P (parm))
21270 if (!TYPE_PTRMEMFUNC_P (arg))
21271 return unify_type_mismatch (explain_p, parm, arg);
21273 return unify (tparms, targs,
21274 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21275 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21276 strict, explain_p);
21278 else if (TYPE_PTRMEMFUNC_P (arg))
21279 return unify_type_mismatch (explain_p, parm, arg);
21281 if (CLASSTYPE_TEMPLATE_INFO (parm))
21283 tree t = NULL_TREE;
21285 if (strict_in & UNIFY_ALLOW_DERIVED)
21287 /* First, we try to unify the PARM and ARG directly. */
21288 t = try_class_unification (tparms, targs,
21289 parm, arg, explain_p);
21291 if (!t)
21293 /* Fallback to the special case allowed in
21294 [temp.deduct.call]:
21296 If P is a class, and P has the form
21297 template-id, then A can be a derived class of
21298 the deduced A. Likewise, if P is a pointer to
21299 a class of the form template-id, A can be a
21300 pointer to a derived class pointed to by the
21301 deduced A. */
21302 enum template_base_result r;
21303 r = get_template_base (tparms, targs, parm, arg,
21304 explain_p, &t);
21306 if (!t)
21308 /* Don't give the derived diagnostic if we're
21309 already dealing with the same template. */
21310 bool same_template
21311 = (CLASSTYPE_TEMPLATE_INFO (arg)
21312 && (CLASSTYPE_TI_TEMPLATE (parm)
21313 == CLASSTYPE_TI_TEMPLATE (arg)));
21314 return unify_no_common_base (explain_p && !same_template,
21315 r, parm, arg);
21319 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21320 && (CLASSTYPE_TI_TEMPLATE (parm)
21321 == CLASSTYPE_TI_TEMPLATE (arg)))
21322 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21323 Then, we should unify `int' and `U'. */
21324 t = arg;
21325 else
21326 /* There's no chance of unification succeeding. */
21327 return unify_type_mismatch (explain_p, parm, arg);
21329 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21330 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21332 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21333 return unify_type_mismatch (explain_p, parm, arg);
21334 return unify_success (explain_p);
21336 case METHOD_TYPE:
21337 case FUNCTION_TYPE:
21339 unsigned int nargs;
21340 tree *args;
21341 tree a;
21342 unsigned int i;
21344 if (TREE_CODE (arg) != TREE_CODE (parm))
21345 return unify_type_mismatch (explain_p, parm, arg);
21347 /* CV qualifications for methods can never be deduced, they must
21348 match exactly. We need to check them explicitly here,
21349 because type_unification_real treats them as any other
21350 cv-qualified parameter. */
21351 if (TREE_CODE (parm) == METHOD_TYPE
21352 && (!check_cv_quals_for_unify
21353 (UNIFY_ALLOW_NONE,
21354 class_of_this_parm (arg),
21355 class_of_this_parm (parm))))
21356 return unify_cv_qual_mismatch (explain_p, parm, arg);
21357 if (TREE_CODE (arg) == FUNCTION_TYPE
21358 && type_memfn_quals (parm) != type_memfn_quals (arg))
21359 return unify_cv_qual_mismatch (explain_p, parm, arg);
21360 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21361 return unify_type_mismatch (explain_p, parm, arg);
21363 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21364 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21366 nargs = list_length (TYPE_ARG_TYPES (arg));
21367 args = XALLOCAVEC (tree, nargs);
21368 for (a = TYPE_ARG_TYPES (arg), i = 0;
21369 a != NULL_TREE && a != void_list_node;
21370 a = TREE_CHAIN (a), ++i)
21371 args[i] = TREE_VALUE (a);
21372 nargs = i;
21374 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21375 args, nargs, 1, DEDUCE_EXACT,
21376 LOOKUP_NORMAL, NULL, explain_p))
21377 return 1;
21379 if (flag_noexcept_type)
21381 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21382 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21383 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21384 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21385 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21386 && uses_template_parms (TREE_PURPOSE (pspec)))
21387 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21388 TREE_PURPOSE (aspec),
21389 UNIFY_ALLOW_NONE, explain_p);
21390 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21391 return unify_type_mismatch (explain_p, parm, arg);
21394 return 0;
21397 case OFFSET_TYPE:
21398 /* Unify a pointer to member with a pointer to member function, which
21399 deduces the type of the member as a function type. */
21400 if (TYPE_PTRMEMFUNC_P (arg))
21402 /* Check top-level cv qualifiers */
21403 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21404 return unify_cv_qual_mismatch (explain_p, parm, arg);
21406 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21407 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21408 UNIFY_ALLOW_NONE, explain_p);
21410 /* Determine the type of the function we are unifying against. */
21411 tree fntype = static_fn_type (arg);
21413 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21416 if (TREE_CODE (arg) != OFFSET_TYPE)
21417 return unify_type_mismatch (explain_p, parm, arg);
21418 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21419 TYPE_OFFSET_BASETYPE (arg),
21420 UNIFY_ALLOW_NONE, explain_p);
21421 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21422 strict, explain_p);
21424 case CONST_DECL:
21425 if (DECL_TEMPLATE_PARM_P (parm))
21426 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21427 if (arg != scalar_constant_value (parm))
21428 return unify_template_argument_mismatch (explain_p, parm, arg);
21429 return unify_success (explain_p);
21431 case FIELD_DECL:
21432 case TEMPLATE_DECL:
21433 /* Matched cases are handled by the ARG == PARM test above. */
21434 return unify_template_argument_mismatch (explain_p, parm, arg);
21436 case VAR_DECL:
21437 /* We might get a variable as a non-type template argument in parm if the
21438 corresponding parameter is type-dependent. Make any necessary
21439 adjustments based on whether arg is a reference. */
21440 if (CONSTANT_CLASS_P (arg))
21441 parm = fold_non_dependent_expr (parm);
21442 else if (REFERENCE_REF_P (arg))
21444 tree sub = TREE_OPERAND (arg, 0);
21445 STRIP_NOPS (sub);
21446 if (TREE_CODE (sub) == ADDR_EXPR)
21447 arg = TREE_OPERAND (sub, 0);
21449 /* Now use the normal expression code to check whether they match. */
21450 goto expr;
21452 case TYPE_ARGUMENT_PACK:
21453 case NONTYPE_ARGUMENT_PACK:
21454 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21455 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21457 case TYPEOF_TYPE:
21458 case DECLTYPE_TYPE:
21459 case UNDERLYING_TYPE:
21460 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21461 or UNDERLYING_TYPE nodes. */
21462 return unify_success (explain_p);
21464 case ERROR_MARK:
21465 /* Unification fails if we hit an error node. */
21466 return unify_invalid (explain_p);
21468 case INDIRECT_REF:
21469 if (REFERENCE_REF_P (parm))
21471 bool pexp = PACK_EXPANSION_P (arg);
21472 if (pexp)
21473 arg = PACK_EXPANSION_PATTERN (arg);
21474 if (REFERENCE_REF_P (arg))
21475 arg = TREE_OPERAND (arg, 0);
21476 if (pexp)
21477 arg = make_pack_expansion (arg, complain);
21478 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21479 strict, explain_p);
21481 /* FALLTHRU */
21483 default:
21484 /* An unresolved overload is a nondeduced context. */
21485 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21486 return unify_success (explain_p);
21487 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21488 expr:
21489 /* We must be looking at an expression. This can happen with
21490 something like:
21492 template <int I>
21493 void foo(S<I>, S<I + 2>);
21495 This is a "nondeduced context":
21497 [deduct.type]
21499 The nondeduced contexts are:
21501 --A type that is a template-id in which one or more of
21502 the template-arguments is an expression that references
21503 a template-parameter.
21505 In these cases, we assume deduction succeeded, but don't
21506 actually infer any unifications. */
21508 if (!uses_template_parms (parm)
21509 && !template_args_equal (parm, arg))
21510 return unify_expression_unequal (explain_p, parm, arg);
21511 else
21512 return unify_success (explain_p);
21515 #undef RECUR_AND_CHECK_FAILURE
21517 /* Note that DECL can be defined in this translation unit, if
21518 required. */
21520 static void
21521 mark_definable (tree decl)
21523 tree clone;
21524 DECL_NOT_REALLY_EXTERN (decl) = 1;
21525 FOR_EACH_CLONE (clone, decl)
21526 DECL_NOT_REALLY_EXTERN (clone) = 1;
21529 /* Called if RESULT is explicitly instantiated, or is a member of an
21530 explicitly instantiated class. */
21532 void
21533 mark_decl_instantiated (tree result, int extern_p)
21535 SET_DECL_EXPLICIT_INSTANTIATION (result);
21537 /* If this entity has already been written out, it's too late to
21538 make any modifications. */
21539 if (TREE_ASM_WRITTEN (result))
21540 return;
21542 /* For anonymous namespace we don't need to do anything. */
21543 if (decl_anon_ns_mem_p (result))
21545 gcc_assert (!TREE_PUBLIC (result));
21546 return;
21549 if (TREE_CODE (result) != FUNCTION_DECL)
21550 /* The TREE_PUBLIC flag for function declarations will have been
21551 set correctly by tsubst. */
21552 TREE_PUBLIC (result) = 1;
21554 /* This might have been set by an earlier implicit instantiation. */
21555 DECL_COMDAT (result) = 0;
21557 if (extern_p)
21558 DECL_NOT_REALLY_EXTERN (result) = 0;
21559 else
21561 mark_definable (result);
21562 mark_needed (result);
21563 /* Always make artificials weak. */
21564 if (DECL_ARTIFICIAL (result) && flag_weak)
21565 comdat_linkage (result);
21566 /* For WIN32 we also want to put explicit instantiations in
21567 linkonce sections. */
21568 else if (TREE_PUBLIC (result))
21569 maybe_make_one_only (result);
21572 /* If EXTERN_P, then this function will not be emitted -- unless
21573 followed by an explicit instantiation, at which point its linkage
21574 will be adjusted. If !EXTERN_P, then this function will be
21575 emitted here. In neither circumstance do we want
21576 import_export_decl to adjust the linkage. */
21577 DECL_INTERFACE_KNOWN (result) = 1;
21580 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21581 important template arguments. If any are missing, we check whether
21582 they're important by using error_mark_node for substituting into any
21583 args that were used for partial ordering (the ones between ARGS and END)
21584 and seeing if it bubbles up. */
21586 static bool
21587 check_undeduced_parms (tree targs, tree args, tree end)
21589 bool found = false;
21590 int i;
21591 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21592 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21594 found = true;
21595 TREE_VEC_ELT (targs, i) = error_mark_node;
21597 if (found)
21599 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21600 if (substed == error_mark_node)
21601 return true;
21603 return false;
21606 /* Given two function templates PAT1 and PAT2, return:
21608 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21609 -1 if PAT2 is more specialized than PAT1.
21610 0 if neither is more specialized.
21612 LEN indicates the number of parameters we should consider
21613 (defaulted parameters should not be considered).
21615 The 1998 std underspecified function template partial ordering, and
21616 DR214 addresses the issue. We take pairs of arguments, one from
21617 each of the templates, and deduce them against each other. One of
21618 the templates will be more specialized if all the *other*
21619 template's arguments deduce against its arguments and at least one
21620 of its arguments *does* *not* deduce against the other template's
21621 corresponding argument. Deduction is done as for class templates.
21622 The arguments used in deduction have reference and top level cv
21623 qualifiers removed. Iff both arguments were originally reference
21624 types *and* deduction succeeds in both directions, an lvalue reference
21625 wins against an rvalue reference and otherwise the template
21626 with the more cv-qualified argument wins for that pairing (if
21627 neither is more cv-qualified, they both are equal). Unlike regular
21628 deduction, after all the arguments have been deduced in this way,
21629 we do *not* verify the deduced template argument values can be
21630 substituted into non-deduced contexts.
21632 The logic can be a bit confusing here, because we look at deduce1 and
21633 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21634 can find template arguments for pat1 to make arg1 look like arg2, that
21635 means that arg2 is at least as specialized as arg1. */
21638 more_specialized_fn (tree pat1, tree pat2, int len)
21640 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21641 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21642 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21643 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21644 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21645 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21646 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21647 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21648 tree origs1, origs2;
21649 bool lose1 = false;
21650 bool lose2 = false;
21652 /* Remove the this parameter from non-static member functions. If
21653 one is a non-static member function and the other is not a static
21654 member function, remove the first parameter from that function
21655 also. This situation occurs for operator functions where we
21656 locate both a member function (with this pointer) and non-member
21657 operator (with explicit first operand). */
21658 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21660 len--; /* LEN is the number of significant arguments for DECL1 */
21661 args1 = TREE_CHAIN (args1);
21662 if (!DECL_STATIC_FUNCTION_P (decl2))
21663 args2 = TREE_CHAIN (args2);
21665 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21667 args2 = TREE_CHAIN (args2);
21668 if (!DECL_STATIC_FUNCTION_P (decl1))
21670 len--;
21671 args1 = TREE_CHAIN (args1);
21675 /* If only one is a conversion operator, they are unordered. */
21676 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21677 return 0;
21679 /* Consider the return type for a conversion function */
21680 if (DECL_CONV_FN_P (decl1))
21682 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21683 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21684 len++;
21687 processing_template_decl++;
21689 origs1 = args1;
21690 origs2 = args2;
21692 while (len--
21693 /* Stop when an ellipsis is seen. */
21694 && args1 != NULL_TREE && args2 != NULL_TREE)
21696 tree arg1 = TREE_VALUE (args1);
21697 tree arg2 = TREE_VALUE (args2);
21698 int deduce1, deduce2;
21699 int quals1 = -1;
21700 int quals2 = -1;
21701 int ref1 = 0;
21702 int ref2 = 0;
21704 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21705 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21707 /* When both arguments are pack expansions, we need only
21708 unify the patterns themselves. */
21709 arg1 = PACK_EXPANSION_PATTERN (arg1);
21710 arg2 = PACK_EXPANSION_PATTERN (arg2);
21712 /* This is the last comparison we need to do. */
21713 len = 0;
21716 /* DR 1847: If a particular P contains no template-parameters that
21717 participate in template argument deduction, that P is not used to
21718 determine the ordering. */
21719 if (!uses_deducible_template_parms (arg1)
21720 && !uses_deducible_template_parms (arg2))
21721 goto next;
21723 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21725 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21726 arg1 = TREE_TYPE (arg1);
21727 quals1 = cp_type_quals (arg1);
21730 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21732 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21733 arg2 = TREE_TYPE (arg2);
21734 quals2 = cp_type_quals (arg2);
21737 arg1 = TYPE_MAIN_VARIANT (arg1);
21738 arg2 = TYPE_MAIN_VARIANT (arg2);
21740 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21742 int i, len2 = remaining_arguments (args2);
21743 tree parmvec = make_tree_vec (1);
21744 tree argvec = make_tree_vec (len2);
21745 tree ta = args2;
21747 /* Setup the parameter vector, which contains only ARG1. */
21748 TREE_VEC_ELT (parmvec, 0) = arg1;
21750 /* Setup the argument vector, which contains the remaining
21751 arguments. */
21752 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21753 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21755 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21756 argvec, DEDUCE_EXACT,
21757 /*subr=*/true, /*explain_p=*/false)
21758 == 0);
21760 /* We cannot deduce in the other direction, because ARG1 is
21761 a pack expansion but ARG2 is not. */
21762 deduce2 = 0;
21764 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21766 int i, len1 = remaining_arguments (args1);
21767 tree parmvec = make_tree_vec (1);
21768 tree argvec = make_tree_vec (len1);
21769 tree ta = args1;
21771 /* Setup the parameter vector, which contains only ARG1. */
21772 TREE_VEC_ELT (parmvec, 0) = arg2;
21774 /* Setup the argument vector, which contains the remaining
21775 arguments. */
21776 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21777 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21779 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21780 argvec, DEDUCE_EXACT,
21781 /*subr=*/true, /*explain_p=*/false)
21782 == 0);
21784 /* We cannot deduce in the other direction, because ARG2 is
21785 a pack expansion but ARG1 is not.*/
21786 deduce1 = 0;
21789 else
21791 /* The normal case, where neither argument is a pack
21792 expansion. */
21793 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21794 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21795 == 0);
21796 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21797 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21798 == 0);
21801 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21802 arg2, then arg2 is not as specialized as arg1. */
21803 if (!deduce1)
21804 lose2 = true;
21805 if (!deduce2)
21806 lose1 = true;
21808 /* "If, for a given type, deduction succeeds in both directions
21809 (i.e., the types are identical after the transformations above)
21810 and both P and A were reference types (before being replaced with
21811 the type referred to above):
21812 - if the type from the argument template was an lvalue reference and
21813 the type from the parameter template was not, the argument type is
21814 considered to be more specialized than the other; otherwise,
21815 - if the type from the argument template is more cv-qualified
21816 than the type from the parameter template (as described above),
21817 the argument type is considered to be more specialized than the other;
21818 otherwise,
21819 - neither type is more specialized than the other." */
21821 if (deduce1 && deduce2)
21823 if (ref1 && ref2 && ref1 != ref2)
21825 if (ref1 > ref2)
21826 lose1 = true;
21827 else
21828 lose2 = true;
21830 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21832 if ((quals1 & quals2) == quals2)
21833 lose2 = true;
21834 if ((quals1 & quals2) == quals1)
21835 lose1 = true;
21839 if (lose1 && lose2)
21840 /* We've failed to deduce something in either direction.
21841 These must be unordered. */
21842 break;
21844 next:
21846 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21847 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21848 /* We have already processed all of the arguments in our
21849 handing of the pack expansion type. */
21850 len = 0;
21852 args1 = TREE_CHAIN (args1);
21853 args2 = TREE_CHAIN (args2);
21856 /* "In most cases, all template parameters must have values in order for
21857 deduction to succeed, but for partial ordering purposes a template
21858 parameter may remain without a value provided it is not used in the
21859 types being used for partial ordering."
21861 Thus, if we are missing any of the targs1 we need to substitute into
21862 origs1, then pat2 is not as specialized as pat1. This can happen when
21863 there is a nondeduced context. */
21864 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21865 lose2 = true;
21866 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21867 lose1 = true;
21869 processing_template_decl--;
21871 /* If both deductions succeed, the partial ordering selects the more
21872 constrained template. */
21873 if (!lose1 && !lose2)
21875 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21876 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21877 lose1 = !subsumes_constraints (c1, c2);
21878 lose2 = !subsumes_constraints (c2, c1);
21881 /* All things being equal, if the next argument is a pack expansion
21882 for one function but not for the other, prefer the
21883 non-variadic function. FIXME this is bogus; see c++/41958. */
21884 if (lose1 == lose2
21885 && args1 && TREE_VALUE (args1)
21886 && args2 && TREE_VALUE (args2))
21888 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21889 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21892 if (lose1 == lose2)
21893 return 0;
21894 else if (!lose1)
21895 return 1;
21896 else
21897 return -1;
21900 /* Determine which of two partial specializations of TMPL is more
21901 specialized.
21903 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21904 to the first partial specialization. The TREE_PURPOSE is the
21905 innermost set of template parameters for the partial
21906 specialization. PAT2 is similar, but for the second template.
21908 Return 1 if the first partial specialization is more specialized;
21909 -1 if the second is more specialized; 0 if neither is more
21910 specialized.
21912 See [temp.class.order] for information about determining which of
21913 two templates is more specialized. */
21915 static int
21916 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21918 tree targs;
21919 int winner = 0;
21920 bool any_deductions = false;
21922 tree tmpl1 = TREE_VALUE (pat1);
21923 tree tmpl2 = TREE_VALUE (pat2);
21924 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21925 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21927 /* Just like what happens for functions, if we are ordering between
21928 different template specializations, we may encounter dependent
21929 types in the arguments, and we need our dependency check functions
21930 to behave correctly. */
21931 ++processing_template_decl;
21932 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21933 if (targs)
21935 --winner;
21936 any_deductions = true;
21939 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21940 if (targs)
21942 ++winner;
21943 any_deductions = true;
21945 --processing_template_decl;
21947 /* If both deductions succeed, the partial ordering selects the more
21948 constrained template. */
21949 if (!winner && any_deductions)
21950 return more_constrained (tmpl1, tmpl2);
21952 /* In the case of a tie where at least one of the templates
21953 has a parameter pack at the end, the template with the most
21954 non-packed parameters wins. */
21955 if (winner == 0
21956 && any_deductions
21957 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21958 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21960 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21961 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21962 int len1 = TREE_VEC_LENGTH (args1);
21963 int len2 = TREE_VEC_LENGTH (args2);
21965 /* We don't count the pack expansion at the end. */
21966 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21967 --len1;
21968 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21969 --len2;
21971 if (len1 > len2)
21972 return 1;
21973 else if (len1 < len2)
21974 return -1;
21977 return winner;
21980 /* Return the template arguments that will produce the function signature
21981 DECL from the function template FN, with the explicit template
21982 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21983 also match. Return NULL_TREE if no satisfactory arguments could be
21984 found. */
21986 static tree
21987 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21989 int ntparms = DECL_NTPARMS (fn);
21990 tree targs = make_tree_vec (ntparms);
21991 tree decl_type = TREE_TYPE (decl);
21992 tree decl_arg_types;
21993 tree *args;
21994 unsigned int nargs, ix;
21995 tree arg;
21997 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21999 /* Never do unification on the 'this' parameter. */
22000 decl_arg_types = skip_artificial_parms_for (decl,
22001 TYPE_ARG_TYPES (decl_type));
22003 nargs = list_length (decl_arg_types);
22004 args = XALLOCAVEC (tree, nargs);
22005 for (arg = decl_arg_types, ix = 0;
22006 arg != NULL_TREE && arg != void_list_node;
22007 arg = TREE_CHAIN (arg), ++ix)
22008 args[ix] = TREE_VALUE (arg);
22010 if (fn_type_unification (fn, explicit_args, targs,
22011 args, ix,
22012 (check_rettype || DECL_CONV_FN_P (fn)
22013 ? TREE_TYPE (decl_type) : NULL_TREE),
22014 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22015 /*decltype*/false)
22016 == error_mark_node)
22017 return NULL_TREE;
22019 return targs;
22022 /* Return the innermost template arguments that, when applied to a partial
22023 specialization SPEC_TMPL of TMPL, yield the ARGS.
22025 For example, suppose we have:
22027 template <class T, class U> struct S {};
22028 template <class T> struct S<T*, int> {};
22030 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22031 partial specialization and the ARGS will be {double*, int}. The resulting
22032 vector will be {double}, indicating that `T' is bound to `double'. */
22034 static tree
22035 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22037 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22038 tree spec_args
22039 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22040 int i, ntparms = TREE_VEC_LENGTH (tparms);
22041 tree deduced_args;
22042 tree innermost_deduced_args;
22044 innermost_deduced_args = make_tree_vec (ntparms);
22045 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22047 deduced_args = copy_node (args);
22048 SET_TMPL_ARGS_LEVEL (deduced_args,
22049 TMPL_ARGS_DEPTH (deduced_args),
22050 innermost_deduced_args);
22052 else
22053 deduced_args = innermost_deduced_args;
22055 bool tried_array_deduction = (cxx_dialect < cxx17);
22056 again:
22057 if (unify (tparms, deduced_args,
22058 INNERMOST_TEMPLATE_ARGS (spec_args),
22059 INNERMOST_TEMPLATE_ARGS (args),
22060 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22061 return NULL_TREE;
22063 for (i = 0; i < ntparms; ++i)
22064 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22066 if (!tried_array_deduction)
22068 try_array_deduction (tparms, innermost_deduced_args,
22069 INNERMOST_TEMPLATE_ARGS (spec_args));
22070 tried_array_deduction = true;
22071 if (TREE_VEC_ELT (innermost_deduced_args, i))
22072 goto again;
22074 return NULL_TREE;
22077 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22078 if (!push_tinst_level (tinst))
22080 excessive_deduction_depth = true;
22081 return NULL_TREE;
22084 /* Verify that nondeduced template arguments agree with the type
22085 obtained from argument deduction.
22087 For example:
22089 struct A { typedef int X; };
22090 template <class T, class U> struct C {};
22091 template <class T> struct C<T, typename T::X> {};
22093 Then with the instantiation `C<A, int>', we can deduce that
22094 `T' is `A' but unify () does not check whether `typename T::X'
22095 is `int'. */
22096 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22098 if (spec_args != error_mark_node)
22099 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22100 INNERMOST_TEMPLATE_ARGS (spec_args),
22101 tmpl, tf_none, false, false);
22103 pop_tinst_level ();
22105 if (spec_args == error_mark_node
22106 /* We only need to check the innermost arguments; the other
22107 arguments will always agree. */
22108 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22109 INNERMOST_TEMPLATE_ARGS (args)))
22110 return NULL_TREE;
22112 /* Now that we have bindings for all of the template arguments,
22113 ensure that the arguments deduced for the template template
22114 parameters have compatible template parameter lists. See the use
22115 of template_template_parm_bindings_ok_p in fn_type_unification
22116 for more information. */
22117 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22118 return NULL_TREE;
22120 return deduced_args;
22123 // Compare two function templates T1 and T2 by deducing bindings
22124 // from one against the other. If both deductions succeed, compare
22125 // constraints to see which is more constrained.
22126 static int
22127 more_specialized_inst (tree t1, tree t2)
22129 int fate = 0;
22130 int count = 0;
22132 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22134 --fate;
22135 ++count;
22138 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22140 ++fate;
22141 ++count;
22144 // If both deductions succeed, then one may be more constrained.
22145 if (count == 2 && fate == 0)
22146 fate = more_constrained (t1, t2);
22148 return fate;
22151 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22152 Return the TREE_LIST node with the most specialized template, if
22153 any. If there is no most specialized template, the error_mark_node
22154 is returned.
22156 Note that this function does not look at, or modify, the
22157 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22158 returned is one of the elements of INSTANTIATIONS, callers may
22159 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22160 and retrieve it from the value returned. */
22162 tree
22163 most_specialized_instantiation (tree templates)
22165 tree fn, champ;
22167 ++processing_template_decl;
22169 champ = templates;
22170 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22172 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22173 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22174 if (fate == -1)
22175 champ = fn;
22176 else if (!fate)
22178 /* Equally specialized, move to next function. If there
22179 is no next function, nothing's most specialized. */
22180 fn = TREE_CHAIN (fn);
22181 champ = fn;
22182 if (!fn)
22183 break;
22187 if (champ)
22188 /* Now verify that champ is better than everything earlier in the
22189 instantiation list. */
22190 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22191 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22193 champ = NULL_TREE;
22194 break;
22198 processing_template_decl--;
22200 if (!champ)
22201 return error_mark_node;
22203 return champ;
22206 /* If DECL is a specialization of some template, return the most
22207 general such template. Otherwise, returns NULL_TREE.
22209 For example, given:
22211 template <class T> struct S { template <class U> void f(U); };
22213 if TMPL is `template <class U> void S<int>::f(U)' this will return
22214 the full template. This function will not trace past partial
22215 specializations, however. For example, given in addition:
22217 template <class T> struct S<T*> { template <class U> void f(U); };
22219 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22220 `template <class T> template <class U> S<T*>::f(U)'. */
22222 tree
22223 most_general_template (tree decl)
22225 if (TREE_CODE (decl) != TEMPLATE_DECL)
22227 if (tree tinfo = get_template_info (decl))
22228 decl = TI_TEMPLATE (tinfo);
22229 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22230 template friend, or a FIELD_DECL for a capture pack. */
22231 if (TREE_CODE (decl) != TEMPLATE_DECL)
22232 return NULL_TREE;
22235 /* Look for more and more general templates. */
22236 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22238 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22239 (See cp-tree.h for details.) */
22240 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22241 break;
22243 if (CLASS_TYPE_P (TREE_TYPE (decl))
22244 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22245 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22246 break;
22248 /* Stop if we run into an explicitly specialized class template. */
22249 if (!DECL_NAMESPACE_SCOPE_P (decl)
22250 && DECL_CONTEXT (decl)
22251 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22252 break;
22254 decl = DECL_TI_TEMPLATE (decl);
22257 return decl;
22260 /* Return the most specialized of the template partial specializations
22261 which can produce TARGET, a specialization of some class or variable
22262 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22263 a TEMPLATE_DECL node corresponding to the partial specialization, while
22264 the TREE_PURPOSE is the set of template arguments that must be
22265 substituted into the template pattern in order to generate TARGET.
22267 If the choice of partial specialization is ambiguous, a diagnostic
22268 is issued, and the error_mark_node is returned. If there are no
22269 partial specializations matching TARGET, then NULL_TREE is
22270 returned, indicating that the primary template should be used. */
22272 static tree
22273 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22275 tree list = NULL_TREE;
22276 tree t;
22277 tree champ;
22278 int fate;
22279 bool ambiguous_p;
22280 tree outer_args = NULL_TREE;
22281 tree tmpl, args;
22283 if (TYPE_P (target))
22285 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22286 tmpl = TI_TEMPLATE (tinfo);
22287 args = TI_ARGS (tinfo);
22289 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22291 tmpl = TREE_OPERAND (target, 0);
22292 args = TREE_OPERAND (target, 1);
22294 else if (VAR_P (target))
22296 tree tinfo = DECL_TEMPLATE_INFO (target);
22297 tmpl = TI_TEMPLATE (tinfo);
22298 args = TI_ARGS (tinfo);
22300 else
22301 gcc_unreachable ();
22303 tree main_tmpl = most_general_template (tmpl);
22305 /* For determining which partial specialization to use, only the
22306 innermost args are interesting. */
22307 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22309 outer_args = strip_innermost_template_args (args, 1);
22310 args = INNERMOST_TEMPLATE_ARGS (args);
22313 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22315 tree spec_args;
22316 tree spec_tmpl = TREE_VALUE (t);
22318 if (outer_args)
22320 /* Substitute in the template args from the enclosing class. */
22321 ++processing_template_decl;
22322 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22323 --processing_template_decl;
22326 if (spec_tmpl == error_mark_node)
22327 return error_mark_node;
22329 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22330 if (spec_args)
22332 if (outer_args)
22333 spec_args = add_to_template_args (outer_args, spec_args);
22335 /* Keep the candidate only if the constraints are satisfied,
22336 or if we're not compiling with concepts. */
22337 if (!flag_concepts
22338 || constraints_satisfied_p (spec_tmpl, spec_args))
22340 list = tree_cons (spec_args, TREE_VALUE (t), list);
22341 TREE_TYPE (list) = TREE_TYPE (t);
22346 if (! list)
22347 return NULL_TREE;
22349 ambiguous_p = false;
22350 t = list;
22351 champ = t;
22352 t = TREE_CHAIN (t);
22353 for (; t; t = TREE_CHAIN (t))
22355 fate = more_specialized_partial_spec (tmpl, champ, t);
22356 if (fate == 1)
22358 else
22360 if (fate == 0)
22362 t = TREE_CHAIN (t);
22363 if (! t)
22365 ambiguous_p = true;
22366 break;
22369 champ = t;
22373 if (!ambiguous_p)
22374 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22376 fate = more_specialized_partial_spec (tmpl, champ, t);
22377 if (fate != 1)
22379 ambiguous_p = true;
22380 break;
22384 if (ambiguous_p)
22386 const char *str;
22387 char *spaces = NULL;
22388 if (!(complain & tf_error))
22389 return error_mark_node;
22390 if (TYPE_P (target))
22391 error ("ambiguous template instantiation for %q#T", target);
22392 else
22393 error ("ambiguous template instantiation for %q#D", target);
22394 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22395 for (t = list; t; t = TREE_CHAIN (t))
22397 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22398 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22399 "%s %#qS", spaces ? spaces : str, subst);
22400 spaces = spaces ? spaces : get_spaces (str);
22402 free (spaces);
22403 return error_mark_node;
22406 return champ;
22409 /* Explicitly instantiate DECL. */
22411 void
22412 do_decl_instantiation (tree decl, tree storage)
22414 tree result = NULL_TREE;
22415 int extern_p = 0;
22417 if (!decl || decl == error_mark_node)
22418 /* An error occurred, for which grokdeclarator has already issued
22419 an appropriate message. */
22420 return;
22421 else if (! DECL_LANG_SPECIFIC (decl))
22423 error ("explicit instantiation of non-template %q#D", decl);
22424 return;
22427 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22428 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22430 if (VAR_P (decl) && !var_templ)
22432 /* There is an asymmetry here in the way VAR_DECLs and
22433 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22434 the latter, the DECL we get back will be marked as a
22435 template instantiation, and the appropriate
22436 DECL_TEMPLATE_INFO will be set up. This does not happen for
22437 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22438 should handle VAR_DECLs as it currently handles
22439 FUNCTION_DECLs. */
22440 if (!DECL_CLASS_SCOPE_P (decl))
22442 error ("%qD is not a static data member of a class template", decl);
22443 return;
22445 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22446 if (!result || !VAR_P (result))
22448 error ("no matching template for %qD found", decl);
22449 return;
22451 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22453 error ("type %qT for explicit instantiation %qD does not match "
22454 "declared type %qT", TREE_TYPE (result), decl,
22455 TREE_TYPE (decl));
22456 return;
22459 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22461 error ("explicit instantiation of %q#D", decl);
22462 return;
22464 else
22465 result = decl;
22467 /* Check for various error cases. Note that if the explicit
22468 instantiation is valid the RESULT will currently be marked as an
22469 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22470 until we get here. */
22472 if (DECL_TEMPLATE_SPECIALIZATION (result))
22474 /* DR 259 [temp.spec].
22476 Both an explicit instantiation and a declaration of an explicit
22477 specialization shall not appear in a program unless the explicit
22478 instantiation follows a declaration of the explicit specialization.
22480 For a given set of template parameters, if an explicit
22481 instantiation of a template appears after a declaration of an
22482 explicit specialization for that template, the explicit
22483 instantiation has no effect. */
22484 return;
22486 else if (DECL_EXPLICIT_INSTANTIATION (result))
22488 /* [temp.spec]
22490 No program shall explicitly instantiate any template more
22491 than once.
22493 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22494 the first instantiation was `extern' and the second is not,
22495 and EXTERN_P for the opposite case. */
22496 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22497 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22498 /* If an "extern" explicit instantiation follows an ordinary
22499 explicit instantiation, the template is instantiated. */
22500 if (extern_p)
22501 return;
22503 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22505 error ("no matching template for %qD found", result);
22506 return;
22508 else if (!DECL_TEMPLATE_INFO (result))
22510 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22511 return;
22514 if (storage == NULL_TREE)
22516 else if (storage == ridpointers[(int) RID_EXTERN])
22518 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22519 pedwarn (input_location, OPT_Wpedantic,
22520 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22521 "instantiations");
22522 extern_p = 1;
22524 else
22525 error ("storage class %qD applied to template instantiation", storage);
22527 check_explicit_instantiation_namespace (result);
22528 mark_decl_instantiated (result, extern_p);
22529 if (! extern_p)
22530 instantiate_decl (result, /*defer_ok=*/true,
22531 /*expl_inst_class_mem_p=*/false);
22534 static void
22535 mark_class_instantiated (tree t, int extern_p)
22537 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22538 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22539 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22540 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22541 if (! extern_p)
22543 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22544 rest_of_type_compilation (t, 1);
22548 /* Called from do_type_instantiation through binding_table_foreach to
22549 do recursive instantiation for the type bound in ENTRY. */
22550 static void
22551 bt_instantiate_type_proc (binding_entry entry, void *data)
22553 tree storage = *(tree *) data;
22555 if (MAYBE_CLASS_TYPE_P (entry->type)
22556 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22557 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22560 /* Perform an explicit instantiation of template class T. STORAGE, if
22561 non-null, is the RID for extern, inline or static. COMPLAIN is
22562 nonzero if this is called from the parser, zero if called recursively,
22563 since the standard is unclear (as detailed below). */
22565 void
22566 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22568 int extern_p = 0;
22569 int nomem_p = 0;
22570 int static_p = 0;
22571 int previous_instantiation_extern_p = 0;
22573 if (TREE_CODE (t) == TYPE_DECL)
22574 t = TREE_TYPE (t);
22576 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22578 tree tmpl =
22579 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22580 if (tmpl)
22581 error ("explicit instantiation of non-class template %qD", tmpl);
22582 else
22583 error ("explicit instantiation of non-template type %qT", t);
22584 return;
22587 complete_type (t);
22589 if (!COMPLETE_TYPE_P (t))
22591 if (complain & tf_error)
22592 error ("explicit instantiation of %q#T before definition of template",
22594 return;
22597 if (storage != NULL_TREE)
22599 if (!in_system_header_at (input_location))
22601 if (storage == ridpointers[(int) RID_EXTERN])
22603 if (cxx_dialect == cxx98)
22604 pedwarn (input_location, OPT_Wpedantic,
22605 "ISO C++ 1998 forbids the use of %<extern%> on "
22606 "explicit instantiations");
22608 else
22609 pedwarn (input_location, OPT_Wpedantic,
22610 "ISO C++ forbids the use of %qE"
22611 " on explicit instantiations", storage);
22614 if (storage == ridpointers[(int) RID_INLINE])
22615 nomem_p = 1;
22616 else if (storage == ridpointers[(int) RID_EXTERN])
22617 extern_p = 1;
22618 else if (storage == ridpointers[(int) RID_STATIC])
22619 static_p = 1;
22620 else
22622 error ("storage class %qD applied to template instantiation",
22623 storage);
22624 extern_p = 0;
22628 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22630 /* DR 259 [temp.spec].
22632 Both an explicit instantiation and a declaration of an explicit
22633 specialization shall not appear in a program unless the explicit
22634 instantiation follows a declaration of the explicit specialization.
22636 For a given set of template parameters, if an explicit
22637 instantiation of a template appears after a declaration of an
22638 explicit specialization for that template, the explicit
22639 instantiation has no effect. */
22640 return;
22642 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22644 /* [temp.spec]
22646 No program shall explicitly instantiate any template more
22647 than once.
22649 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22650 instantiation was `extern'. If EXTERN_P then the second is.
22651 These cases are OK. */
22652 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22654 if (!previous_instantiation_extern_p && !extern_p
22655 && (complain & tf_error))
22656 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22658 /* If we've already instantiated the template, just return now. */
22659 if (!CLASSTYPE_INTERFACE_ONLY (t))
22660 return;
22663 check_explicit_instantiation_namespace (TYPE_NAME (t));
22664 mark_class_instantiated (t, extern_p);
22666 if (nomem_p)
22667 return;
22669 /* In contrast to implicit instantiation, where only the
22670 declarations, and not the definitions, of members are
22671 instantiated, we have here:
22673 [temp.explicit]
22675 The explicit instantiation of a class template specialization
22676 implies the instantiation of all of its members not
22677 previously explicitly specialized in the translation unit
22678 containing the explicit instantiation.
22680 Of course, we can't instantiate member template classes, since we
22681 don't have any arguments for them. Note that the standard is
22682 unclear on whether the instantiation of the members are
22683 *explicit* instantiations or not. However, the most natural
22684 interpretation is that it should be an explicit
22685 instantiation. */
22686 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22687 if ((VAR_P (fld)
22688 || (TREE_CODE (fld) == FUNCTION_DECL
22689 && !static_p
22690 && user_provided_p (fld)))
22691 && DECL_TEMPLATE_INSTANTIATION (fld))
22693 mark_decl_instantiated (fld, extern_p);
22694 if (! extern_p)
22695 instantiate_decl (fld, /*defer_ok=*/true,
22696 /*expl_inst_class_mem_p=*/true);
22699 if (CLASSTYPE_NESTED_UTDS (t))
22700 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22701 bt_instantiate_type_proc, &storage);
22704 /* Given a function DECL, which is a specialization of TMPL, modify
22705 DECL to be a re-instantiation of TMPL with the same template
22706 arguments. TMPL should be the template into which tsubst'ing
22707 should occur for DECL, not the most general template.
22709 One reason for doing this is a scenario like this:
22711 template <class T>
22712 void f(const T&, int i);
22714 void g() { f(3, 7); }
22716 template <class T>
22717 void f(const T& t, const int i) { }
22719 Note that when the template is first instantiated, with
22720 instantiate_template, the resulting DECL will have no name for the
22721 first parameter, and the wrong type for the second. So, when we go
22722 to instantiate the DECL, we regenerate it. */
22724 static void
22725 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22727 /* The arguments used to instantiate DECL, from the most general
22728 template. */
22729 tree code_pattern;
22731 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22733 /* Make sure that we can see identifiers, and compute access
22734 correctly. */
22735 push_access_scope (decl);
22737 if (TREE_CODE (decl) == FUNCTION_DECL)
22739 tree decl_parm;
22740 tree pattern_parm;
22741 tree specs;
22742 int args_depth;
22743 int parms_depth;
22745 args_depth = TMPL_ARGS_DEPTH (args);
22746 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22747 if (args_depth > parms_depth)
22748 args = get_innermost_template_args (args, parms_depth);
22750 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22751 args, tf_error, NULL_TREE,
22752 /*defer_ok*/false);
22753 if (specs && specs != error_mark_node)
22754 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22755 specs);
22757 /* Merge parameter declarations. */
22758 decl_parm = skip_artificial_parms_for (decl,
22759 DECL_ARGUMENTS (decl));
22760 pattern_parm
22761 = skip_artificial_parms_for (code_pattern,
22762 DECL_ARGUMENTS (code_pattern));
22763 while (decl_parm && !DECL_PACK_P (pattern_parm))
22765 tree parm_type;
22766 tree attributes;
22768 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22769 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22770 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22771 NULL_TREE);
22772 parm_type = type_decays_to (parm_type);
22773 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22774 TREE_TYPE (decl_parm) = parm_type;
22775 attributes = DECL_ATTRIBUTES (pattern_parm);
22776 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22778 DECL_ATTRIBUTES (decl_parm) = attributes;
22779 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22781 decl_parm = DECL_CHAIN (decl_parm);
22782 pattern_parm = DECL_CHAIN (pattern_parm);
22784 /* Merge any parameters that match with the function parameter
22785 pack. */
22786 if (pattern_parm && DECL_PACK_P (pattern_parm))
22788 int i, len;
22789 tree expanded_types;
22790 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22791 the parameters in this function parameter pack. */
22792 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22793 args, tf_error, NULL_TREE);
22794 len = TREE_VEC_LENGTH (expanded_types);
22795 for (i = 0; i < len; i++)
22797 tree parm_type;
22798 tree attributes;
22800 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22801 /* Rename the parameter to include the index. */
22802 DECL_NAME (decl_parm) =
22803 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22804 parm_type = TREE_VEC_ELT (expanded_types, i);
22805 parm_type = type_decays_to (parm_type);
22806 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22807 TREE_TYPE (decl_parm) = parm_type;
22808 attributes = DECL_ATTRIBUTES (pattern_parm);
22809 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22811 DECL_ATTRIBUTES (decl_parm) = attributes;
22812 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22814 decl_parm = DECL_CHAIN (decl_parm);
22817 /* Merge additional specifiers from the CODE_PATTERN. */
22818 if (DECL_DECLARED_INLINE_P (code_pattern)
22819 && !DECL_DECLARED_INLINE_P (decl))
22820 DECL_DECLARED_INLINE_P (decl) = 1;
22822 else if (VAR_P (decl))
22824 start_lambda_scope (decl);
22825 DECL_INITIAL (decl) =
22826 tsubst_expr (DECL_INITIAL (code_pattern), args,
22827 tf_error, DECL_TI_TEMPLATE (decl),
22828 /*integral_constant_expression_p=*/false);
22829 finish_lambda_scope ();
22830 if (VAR_HAD_UNKNOWN_BOUND (decl))
22831 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22832 tf_error, DECL_TI_TEMPLATE (decl));
22834 else
22835 gcc_unreachable ();
22837 pop_access_scope (decl);
22840 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22841 substituted to get DECL. */
22843 tree
22844 template_for_substitution (tree decl)
22846 tree tmpl = DECL_TI_TEMPLATE (decl);
22848 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22849 for the instantiation. This is not always the most general
22850 template. Consider, for example:
22852 template <class T>
22853 struct S { template <class U> void f();
22854 template <> void f<int>(); };
22856 and an instantiation of S<double>::f<int>. We want TD to be the
22857 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22858 while (/* An instantiation cannot have a definition, so we need a
22859 more general template. */
22860 DECL_TEMPLATE_INSTANTIATION (tmpl)
22861 /* We must also deal with friend templates. Given:
22863 template <class T> struct S {
22864 template <class U> friend void f() {};
22867 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22868 so far as the language is concerned, but that's still
22869 where we get the pattern for the instantiation from. On
22870 other hand, if the definition comes outside the class, say:
22872 template <class T> struct S {
22873 template <class U> friend void f();
22875 template <class U> friend void f() {}
22877 we don't need to look any further. That's what the check for
22878 DECL_INITIAL is for. */
22879 || (TREE_CODE (decl) == FUNCTION_DECL
22880 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22881 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22883 /* The present template, TD, should not be a definition. If it
22884 were a definition, we should be using it! Note that we
22885 cannot restructure the loop to just keep going until we find
22886 a template with a definition, since that might go too far if
22887 a specialization was declared, but not defined. */
22889 /* Fetch the more general template. */
22890 tmpl = DECL_TI_TEMPLATE (tmpl);
22893 return tmpl;
22896 /* Returns true if we need to instantiate this template instance even if we
22897 know we aren't going to emit it. */
22899 bool
22900 always_instantiate_p (tree decl)
22902 /* We always instantiate inline functions so that we can inline them. An
22903 explicit instantiation declaration prohibits implicit instantiation of
22904 non-inline functions. With high levels of optimization, we would
22905 normally inline non-inline functions -- but we're not allowed to do
22906 that for "extern template" functions. Therefore, we check
22907 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22908 return ((TREE_CODE (decl) == FUNCTION_DECL
22909 && (DECL_DECLARED_INLINE_P (decl)
22910 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22911 /* And we need to instantiate static data members so that
22912 their initializers are available in integral constant
22913 expressions. */
22914 || (VAR_P (decl)
22915 && decl_maybe_constant_var_p (decl)));
22918 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22919 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22920 error, true otherwise. */
22922 bool
22923 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22925 tree fntype, spec, noex, clone;
22927 /* Don't instantiate a noexcept-specification from template context. */
22928 if (processing_template_decl)
22929 return true;
22931 if (DECL_CLONED_FUNCTION_P (fn))
22932 fn = DECL_CLONED_FUNCTION (fn);
22933 fntype = TREE_TYPE (fn);
22934 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22936 if (!spec || !TREE_PURPOSE (spec))
22937 return true;
22939 noex = TREE_PURPOSE (spec);
22941 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22943 static hash_set<tree>* fns = new hash_set<tree>;
22944 bool added = false;
22945 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22946 spec = get_defaulted_eh_spec (fn, complain);
22947 else if (!(added = !fns->add (fn)))
22949 /* If hash_set::add returns true, the element was already there. */
22950 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22951 DECL_SOURCE_LOCATION (fn));
22952 error_at (loc,
22953 "exception specification of %qD depends on itself",
22954 fn);
22955 spec = noexcept_false_spec;
22957 else if (push_tinst_level (fn))
22959 push_access_scope (fn);
22960 push_deferring_access_checks (dk_no_deferred);
22961 input_location = DECL_SOURCE_LOCATION (fn);
22962 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22963 DEFERRED_NOEXCEPT_ARGS (noex),
22964 tf_warning_or_error, fn,
22965 /*function_p=*/false,
22966 /*integral_constant_expression_p=*/true);
22967 pop_deferring_access_checks ();
22968 pop_access_scope (fn);
22969 pop_tinst_level ();
22970 spec = build_noexcept_spec (noex, tf_warning_or_error);
22971 if (spec == error_mark_node)
22972 spec = noexcept_false_spec;
22974 else
22975 spec = noexcept_false_spec;
22977 if (added)
22978 fns->remove (fn);
22980 if (spec == error_mark_node)
22981 return false;
22983 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22986 FOR_EACH_CLONE (clone, fn)
22988 if (TREE_TYPE (clone) == fntype)
22989 TREE_TYPE (clone) = TREE_TYPE (fn);
22990 else
22991 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22994 return true;
22997 /* We're starting to process the function INST, an instantiation of PATTERN;
22998 add their parameters to local_specializations. */
23000 static void
23001 register_parameter_specializations (tree pattern, tree inst)
23003 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23004 tree spec_parm = DECL_ARGUMENTS (inst);
23005 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23007 register_local_specialization (spec_parm, tmpl_parm);
23008 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23009 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23011 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23013 if (!DECL_PACK_P (tmpl_parm))
23015 register_local_specialization (spec_parm, tmpl_parm);
23016 spec_parm = DECL_CHAIN (spec_parm);
23018 else
23020 /* Register the (value) argument pack as a specialization of
23021 TMPL_PARM, then move on. */
23022 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23023 register_local_specialization (argpack, tmpl_parm);
23026 gcc_assert (!spec_parm);
23029 /* Produce the definition of D, a _DECL generated from a template. If
23030 DEFER_OK is true, then we don't have to actually do the
23031 instantiation now; we just have to do it sometime. Normally it is
23032 an error if this is an explicit instantiation but D is undefined.
23033 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23034 instantiated class template. */
23036 tree
23037 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23039 tree tmpl = DECL_TI_TEMPLATE (d);
23040 tree gen_args;
23041 tree args;
23042 tree td;
23043 tree code_pattern;
23044 tree spec;
23045 tree gen_tmpl;
23046 bool pattern_defined;
23047 location_t saved_loc = input_location;
23048 int saved_unevaluated_operand = cp_unevaluated_operand;
23049 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23050 bool external_p;
23051 bool deleted_p;
23053 /* This function should only be used to instantiate templates for
23054 functions and static member variables. */
23055 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23057 /* A concept is never instantiated. */
23058 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23060 /* Variables are never deferred; if instantiation is required, they
23061 are instantiated right away. That allows for better code in the
23062 case that an expression refers to the value of the variable --
23063 if the variable has a constant value the referring expression can
23064 take advantage of that fact. */
23065 if (VAR_P (d))
23066 defer_ok = false;
23068 /* Don't instantiate cloned functions. Instead, instantiate the
23069 functions they cloned. */
23070 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23071 d = DECL_CLONED_FUNCTION (d);
23073 if (DECL_TEMPLATE_INSTANTIATED (d)
23074 || (TREE_CODE (d) == FUNCTION_DECL
23075 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23076 || DECL_TEMPLATE_SPECIALIZATION (d))
23077 /* D has already been instantiated or explicitly specialized, so
23078 there's nothing for us to do here.
23080 It might seem reasonable to check whether or not D is an explicit
23081 instantiation, and, if so, stop here. But when an explicit
23082 instantiation is deferred until the end of the compilation,
23083 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23084 the instantiation. */
23085 return d;
23087 /* Check to see whether we know that this template will be
23088 instantiated in some other file, as with "extern template"
23089 extension. */
23090 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23092 /* In general, we do not instantiate such templates. */
23093 if (external_p && !always_instantiate_p (d))
23094 return d;
23096 gen_tmpl = most_general_template (tmpl);
23097 gen_args = DECL_TI_ARGS (d);
23099 if (tmpl != gen_tmpl)
23100 /* We should already have the extra args. */
23101 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23102 == TMPL_ARGS_DEPTH (gen_args));
23103 /* And what's in the hash table should match D. */
23104 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23105 || spec == NULL_TREE);
23107 /* This needs to happen before any tsubsting. */
23108 if (! push_tinst_level (d))
23109 return d;
23111 timevar_push (TV_TEMPLATE_INST);
23113 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23114 for the instantiation. */
23115 td = template_for_substitution (d);
23116 args = gen_args;
23118 if (VAR_P (d))
23120 /* Look up an explicit specialization, if any. */
23121 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23122 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23123 if (elt && elt != error_mark_node)
23125 td = TREE_VALUE (elt);
23126 args = TREE_PURPOSE (elt);
23130 code_pattern = DECL_TEMPLATE_RESULT (td);
23132 /* We should never be trying to instantiate a member of a class
23133 template or partial specialization. */
23134 gcc_assert (d != code_pattern);
23136 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23137 || DECL_TEMPLATE_SPECIALIZATION (td))
23138 /* In the case of a friend template whose definition is provided
23139 outside the class, we may have too many arguments. Drop the
23140 ones we don't need. The same is true for specializations. */
23141 args = get_innermost_template_args
23142 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23144 if (TREE_CODE (d) == FUNCTION_DECL)
23146 deleted_p = DECL_DELETED_FN (code_pattern);
23147 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23148 && DECL_INITIAL (code_pattern) != error_mark_node)
23149 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23150 || deleted_p);
23152 else
23154 deleted_p = false;
23155 if (DECL_CLASS_SCOPE_P (code_pattern))
23156 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23157 || DECL_INLINE_VAR_P (code_pattern));
23158 else
23159 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23162 /* We may be in the middle of deferred access check. Disable it now. */
23163 push_deferring_access_checks (dk_no_deferred);
23165 /* Unless an explicit instantiation directive has already determined
23166 the linkage of D, remember that a definition is available for
23167 this entity. */
23168 if (pattern_defined
23169 && !DECL_INTERFACE_KNOWN (d)
23170 && !DECL_NOT_REALLY_EXTERN (d))
23171 mark_definable (d);
23173 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23174 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23175 input_location = DECL_SOURCE_LOCATION (d);
23177 /* If D is a member of an explicitly instantiated class template,
23178 and no definition is available, treat it like an implicit
23179 instantiation. */
23180 if (!pattern_defined && expl_inst_class_mem_p
23181 && DECL_EXPLICIT_INSTANTIATION (d))
23183 /* Leave linkage flags alone on instantiations with anonymous
23184 visibility. */
23185 if (TREE_PUBLIC (d))
23187 DECL_NOT_REALLY_EXTERN (d) = 0;
23188 DECL_INTERFACE_KNOWN (d) = 0;
23190 SET_DECL_IMPLICIT_INSTANTIATION (d);
23193 /* Defer all other templates, unless we have been explicitly
23194 forbidden from doing so. */
23195 if (/* If there is no definition, we cannot instantiate the
23196 template. */
23197 ! pattern_defined
23198 /* If it's OK to postpone instantiation, do so. */
23199 || defer_ok
23200 /* If this is a static data member that will be defined
23201 elsewhere, we don't want to instantiate the entire data
23202 member, but we do want to instantiate the initializer so that
23203 we can substitute that elsewhere. */
23204 || (external_p && VAR_P (d))
23205 /* Handle here a deleted function too, avoid generating
23206 its body (c++/61080). */
23207 || deleted_p)
23209 /* The definition of the static data member is now required so
23210 we must substitute the initializer. */
23211 if (VAR_P (d)
23212 && !DECL_INITIAL (d)
23213 && DECL_INITIAL (code_pattern))
23215 tree ns;
23216 tree init;
23217 bool const_init = false;
23218 bool enter_context = DECL_CLASS_SCOPE_P (d);
23220 ns = decl_namespace_context (d);
23221 push_nested_namespace (ns);
23222 if (enter_context)
23223 push_nested_class (DECL_CONTEXT (d));
23224 init = tsubst_expr (DECL_INITIAL (code_pattern),
23225 args,
23226 tf_warning_or_error, NULL_TREE,
23227 /*integral_constant_expression_p=*/false);
23228 /* If instantiating the initializer involved instantiating this
23229 again, don't call cp_finish_decl twice. */
23230 if (!DECL_INITIAL (d))
23232 /* Make sure the initializer is still constant, in case of
23233 circular dependency (template/instantiate6.C). */
23234 const_init
23235 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23236 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23237 /*asmspec_tree=*/NULL_TREE,
23238 LOOKUP_ONLYCONVERTING);
23240 if (enter_context)
23241 pop_nested_class ();
23242 pop_nested_namespace (ns);
23245 /* We restore the source position here because it's used by
23246 add_pending_template. */
23247 input_location = saved_loc;
23249 if (at_eof && !pattern_defined
23250 && DECL_EXPLICIT_INSTANTIATION (d)
23251 && DECL_NOT_REALLY_EXTERN (d))
23252 /* [temp.explicit]
23254 The definition of a non-exported function template, a
23255 non-exported member function template, or a non-exported
23256 member function or static data member of a class template
23257 shall be present in every translation unit in which it is
23258 explicitly instantiated. */
23259 permerror (input_location, "explicit instantiation of %qD "
23260 "but no definition available", d);
23262 /* If we're in unevaluated context, we just wanted to get the
23263 constant value; this isn't an odr use, so don't queue
23264 a full instantiation. */
23265 if (cp_unevaluated_operand != 0)
23266 goto out;
23267 /* ??? Historically, we have instantiated inline functions, even
23268 when marked as "extern template". */
23269 if (!(external_p && VAR_P (d)))
23270 add_pending_template (d);
23271 goto out;
23273 /* Tell the repository that D is available in this translation unit
23274 -- and see if it is supposed to be instantiated here. */
23275 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23277 /* In a PCH file, despite the fact that the repository hasn't
23278 requested instantiation in the PCH it is still possible that
23279 an instantiation will be required in a file that includes the
23280 PCH. */
23281 if (pch_file)
23282 add_pending_template (d);
23283 /* Instantiate inline functions so that the inliner can do its
23284 job, even though we'll not be emitting a copy of this
23285 function. */
23286 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23287 goto out;
23290 bool push_to_top, nested;
23291 tree fn_context;
23292 fn_context = decl_function_context (d);
23293 nested = current_function_decl != NULL_TREE;
23294 push_to_top = !(nested && fn_context == current_function_decl);
23296 vec<tree> omp_privatization_save;
23297 if (nested)
23298 save_omp_privatization_clauses (omp_privatization_save);
23300 if (push_to_top)
23301 push_to_top_level ();
23302 else
23304 push_function_context ();
23305 cp_unevaluated_operand = 0;
23306 c_inhibit_evaluation_warnings = 0;
23309 /* Mark D as instantiated so that recursive calls to
23310 instantiate_decl do not try to instantiate it again. */
23311 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23313 /* Regenerate the declaration in case the template has been modified
23314 by a subsequent redeclaration. */
23315 regenerate_decl_from_template (d, td, args);
23317 /* We already set the file and line above. Reset them now in case
23318 they changed as a result of calling regenerate_decl_from_template. */
23319 input_location = DECL_SOURCE_LOCATION (d);
23321 if (VAR_P (d))
23323 tree init;
23324 bool const_init = false;
23326 /* Clear out DECL_RTL; whatever was there before may not be right
23327 since we've reset the type of the declaration. */
23328 SET_DECL_RTL (d, NULL);
23329 DECL_IN_AGGR_P (d) = 0;
23331 /* The initializer is placed in DECL_INITIAL by
23332 regenerate_decl_from_template so we don't need to
23333 push/pop_access_scope again here. Pull it out so that
23334 cp_finish_decl can process it. */
23335 init = DECL_INITIAL (d);
23336 DECL_INITIAL (d) = NULL_TREE;
23337 DECL_INITIALIZED_P (d) = 0;
23339 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23340 initializer. That function will defer actual emission until
23341 we have a chance to determine linkage. */
23342 DECL_EXTERNAL (d) = 0;
23344 /* Enter the scope of D so that access-checking works correctly. */
23345 bool enter_context = DECL_CLASS_SCOPE_P (d);
23346 if (enter_context)
23347 push_nested_class (DECL_CONTEXT (d));
23349 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23350 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23352 if (enter_context)
23353 pop_nested_class ();
23355 if (variable_template_p (gen_tmpl))
23356 note_variable_template_instantiation (d);
23358 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23359 synthesize_method (d);
23360 else if (TREE_CODE (d) == FUNCTION_DECL)
23362 /* Set up the list of local specializations. */
23363 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23364 tree block = NULL_TREE;
23366 /* Set up context. */
23367 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23368 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23369 block = push_stmt_list ();
23370 else
23371 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23373 /* Some typedefs referenced from within the template code need to be
23374 access checked at template instantiation time, i.e now. These
23375 types were added to the template at parsing time. Let's get those
23376 and perform the access checks then. */
23377 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23378 args);
23380 /* Create substitution entries for the parameters. */
23381 register_parameter_specializations (code_pattern, d);
23383 /* Substitute into the body of the function. */
23384 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23385 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23386 tf_warning_or_error, tmpl);
23387 else
23389 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23390 tf_warning_or_error, tmpl,
23391 /*integral_constant_expression_p=*/false);
23393 /* Set the current input_location to the end of the function
23394 so that finish_function knows where we are. */
23395 input_location
23396 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23398 /* Remember if we saw an infinite loop in the template. */
23399 current_function_infinite_loop
23400 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23403 /* Finish the function. */
23404 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23405 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23406 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23407 else
23409 d = finish_function (/*inline_p=*/false);
23410 expand_or_defer_fn (d);
23413 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23414 cp_check_omp_declare_reduction (d);
23417 /* We're not deferring instantiation any more. */
23418 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23420 if (push_to_top)
23421 pop_from_top_level ();
23422 else
23423 pop_function_context ();
23425 if (nested)
23426 restore_omp_privatization_clauses (omp_privatization_save);
23428 out:
23429 pop_deferring_access_checks ();
23430 timevar_pop (TV_TEMPLATE_INST);
23431 pop_tinst_level ();
23432 input_location = saved_loc;
23433 cp_unevaluated_operand = saved_unevaluated_operand;
23434 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23436 return d;
23439 /* Run through the list of templates that we wish we could
23440 instantiate, and instantiate any we can. RETRIES is the
23441 number of times we retry pending template instantiation. */
23443 void
23444 instantiate_pending_templates (int retries)
23446 int reconsider;
23447 location_t saved_loc = input_location;
23449 /* Instantiating templates may trigger vtable generation. This in turn
23450 may require further template instantiations. We place a limit here
23451 to avoid infinite loop. */
23452 if (pending_templates && retries >= max_tinst_depth)
23454 tree decl = pending_templates->tinst->decl;
23456 fatal_error (input_location,
23457 "template instantiation depth exceeds maximum of %d"
23458 " instantiating %q+D, possibly from virtual table generation"
23459 " (use -ftemplate-depth= to increase the maximum)",
23460 max_tinst_depth, decl);
23461 if (TREE_CODE (decl) == FUNCTION_DECL)
23462 /* Pretend that we defined it. */
23463 DECL_INITIAL (decl) = error_mark_node;
23464 return;
23469 struct pending_template **t = &pending_templates;
23470 struct pending_template *last = NULL;
23471 reconsider = 0;
23472 while (*t)
23474 tree instantiation = reopen_tinst_level ((*t)->tinst);
23475 bool complete = false;
23477 if (TYPE_P (instantiation))
23479 if (!COMPLETE_TYPE_P (instantiation))
23481 instantiate_class_template (instantiation);
23482 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23483 for (tree fld = TYPE_FIELDS (instantiation);
23484 fld; fld = TREE_CHAIN (fld))
23485 if ((VAR_P (fld)
23486 || (TREE_CODE (fld) == FUNCTION_DECL
23487 && !DECL_ARTIFICIAL (fld)))
23488 && DECL_TEMPLATE_INSTANTIATION (fld))
23489 instantiate_decl (fld,
23490 /*defer_ok=*/false,
23491 /*expl_inst_class_mem_p=*/false);
23493 if (COMPLETE_TYPE_P (instantiation))
23494 reconsider = 1;
23497 complete = COMPLETE_TYPE_P (instantiation);
23499 else
23501 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23502 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23504 instantiation
23505 = instantiate_decl (instantiation,
23506 /*defer_ok=*/false,
23507 /*expl_inst_class_mem_p=*/false);
23508 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23509 reconsider = 1;
23512 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23513 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23516 if (complete)
23517 /* If INSTANTIATION has been instantiated, then we don't
23518 need to consider it again in the future. */
23519 *t = (*t)->next;
23520 else
23522 last = *t;
23523 t = &(*t)->next;
23525 tinst_depth = 0;
23526 current_tinst_level = NULL;
23528 last_pending_template = last;
23530 while (reconsider);
23532 input_location = saved_loc;
23535 /* Substitute ARGVEC into T, which is a list of initializers for
23536 either base class or a non-static data member. The TREE_PURPOSEs
23537 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23538 instantiate_decl. */
23540 static tree
23541 tsubst_initializer_list (tree t, tree argvec)
23543 tree inits = NULL_TREE;
23545 for (; t; t = TREE_CHAIN (t))
23547 tree decl;
23548 tree init;
23549 tree expanded_bases = NULL_TREE;
23550 tree expanded_arguments = NULL_TREE;
23551 int i, len = 1;
23553 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23555 tree expr;
23556 tree arg;
23558 /* Expand the base class expansion type into separate base
23559 classes. */
23560 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23561 tf_warning_or_error,
23562 NULL_TREE);
23563 if (expanded_bases == error_mark_node)
23564 continue;
23566 /* We'll be building separate TREE_LISTs of arguments for
23567 each base. */
23568 len = TREE_VEC_LENGTH (expanded_bases);
23569 expanded_arguments = make_tree_vec (len);
23570 for (i = 0; i < len; i++)
23571 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23573 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23574 expand each argument in the TREE_VALUE of t. */
23575 expr = make_node (EXPR_PACK_EXPANSION);
23576 PACK_EXPANSION_LOCAL_P (expr) = true;
23577 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23578 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23580 if (TREE_VALUE (t) == void_type_node)
23581 /* VOID_TYPE_NODE is used to indicate
23582 value-initialization. */
23584 for (i = 0; i < len; i++)
23585 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23587 else
23589 /* Substitute parameter packs into each argument in the
23590 TREE_LIST. */
23591 in_base_initializer = 1;
23592 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23594 tree expanded_exprs;
23596 /* Expand the argument. */
23597 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23598 expanded_exprs
23599 = tsubst_pack_expansion (expr, argvec,
23600 tf_warning_or_error,
23601 NULL_TREE);
23602 if (expanded_exprs == error_mark_node)
23603 continue;
23605 /* Prepend each of the expanded expressions to the
23606 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23607 for (i = 0; i < len; i++)
23609 TREE_VEC_ELT (expanded_arguments, i) =
23610 tree_cons (NULL_TREE,
23611 TREE_VEC_ELT (expanded_exprs, i),
23612 TREE_VEC_ELT (expanded_arguments, i));
23615 in_base_initializer = 0;
23617 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23618 since we built them backwards. */
23619 for (i = 0; i < len; i++)
23621 TREE_VEC_ELT (expanded_arguments, i) =
23622 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23627 for (i = 0; i < len; ++i)
23629 if (expanded_bases)
23631 decl = TREE_VEC_ELT (expanded_bases, i);
23632 decl = expand_member_init (decl);
23633 init = TREE_VEC_ELT (expanded_arguments, i);
23635 else
23637 tree tmp;
23638 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23639 tf_warning_or_error, NULL_TREE);
23641 decl = expand_member_init (decl);
23642 if (decl && !DECL_P (decl))
23643 in_base_initializer = 1;
23645 init = TREE_VALUE (t);
23646 tmp = init;
23647 if (init != void_type_node)
23648 init = tsubst_expr (init, argvec,
23649 tf_warning_or_error, NULL_TREE,
23650 /*integral_constant_expression_p=*/false);
23651 if (init == NULL_TREE && tmp != NULL_TREE)
23652 /* If we had an initializer but it instantiated to nothing,
23653 value-initialize the object. This will only occur when
23654 the initializer was a pack expansion where the parameter
23655 packs used in that expansion were of length zero. */
23656 init = void_type_node;
23657 in_base_initializer = 0;
23660 if (decl)
23662 init = build_tree_list (decl, init);
23663 TREE_CHAIN (init) = inits;
23664 inits = init;
23668 return inits;
23671 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23673 static void
23674 set_current_access_from_decl (tree decl)
23676 if (TREE_PRIVATE (decl))
23677 current_access_specifier = access_private_node;
23678 else if (TREE_PROTECTED (decl))
23679 current_access_specifier = access_protected_node;
23680 else
23681 current_access_specifier = access_public_node;
23684 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23685 is the instantiation (which should have been created with
23686 start_enum) and ARGS are the template arguments to use. */
23688 static void
23689 tsubst_enum (tree tag, tree newtag, tree args)
23691 tree e;
23693 if (SCOPED_ENUM_P (newtag))
23694 begin_scope (sk_scoped_enum, newtag);
23696 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23698 tree value;
23699 tree decl;
23701 decl = TREE_VALUE (e);
23702 /* Note that in a template enum, the TREE_VALUE is the
23703 CONST_DECL, not the corresponding INTEGER_CST. */
23704 value = tsubst_expr (DECL_INITIAL (decl),
23705 args, tf_warning_or_error, NULL_TREE,
23706 /*integral_constant_expression_p=*/true);
23708 /* Give this enumeration constant the correct access. */
23709 set_current_access_from_decl (decl);
23711 /* Actually build the enumerator itself. Here we're assuming that
23712 enumerators can't have dependent attributes. */
23713 build_enumerator (DECL_NAME (decl), value, newtag,
23714 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23717 if (SCOPED_ENUM_P (newtag))
23718 finish_scope ();
23720 finish_enum_value_list (newtag);
23721 finish_enum (newtag);
23723 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23724 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23727 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23728 its type -- but without substituting the innermost set of template
23729 arguments. So, innermost set of template parameters will appear in
23730 the type. */
23732 tree
23733 get_mostly_instantiated_function_type (tree decl)
23735 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23736 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23739 /* Return truthvalue if we're processing a template different from
23740 the last one involved in diagnostics. */
23741 bool
23742 problematic_instantiation_changed (void)
23744 return current_tinst_level != last_error_tinst_level;
23747 /* Remember current template involved in diagnostics. */
23748 void
23749 record_last_problematic_instantiation (void)
23751 last_error_tinst_level = current_tinst_level;
23754 struct tinst_level *
23755 current_instantiation (void)
23757 return current_tinst_level;
23760 /* Return TRUE if current_function_decl is being instantiated, false
23761 otherwise. */
23763 bool
23764 instantiating_current_function_p (void)
23766 return (current_instantiation ()
23767 && current_instantiation ()->decl == current_function_decl);
23770 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23771 type. Return false for ok, true for disallowed. Issue error and
23772 inform messages under control of COMPLAIN. */
23774 static bool
23775 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23777 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23778 return false;
23779 else if (POINTER_TYPE_P (type))
23780 return false;
23781 else if (TYPE_PTRMEM_P (type))
23782 return false;
23783 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23784 return false;
23785 else if (TREE_CODE (type) == TYPENAME_TYPE)
23786 return false;
23787 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23788 return false;
23789 else if (TREE_CODE (type) == NULLPTR_TYPE)
23790 return false;
23791 /* A bound template template parm could later be instantiated to have a valid
23792 nontype parm type via an alias template. */
23793 else if (cxx_dialect >= cxx11
23794 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23795 return false;
23797 if (complain & tf_error)
23799 if (type == error_mark_node)
23800 inform (input_location, "invalid template non-type parameter");
23801 else
23802 error ("%q#T is not a valid type for a template non-type parameter",
23803 type);
23805 return true;
23808 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23809 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23811 static bool
23812 dependent_type_p_r (tree type)
23814 tree scope;
23816 /* [temp.dep.type]
23818 A type is dependent if it is:
23820 -- a template parameter. Template template parameters are types
23821 for us (since TYPE_P holds true for them) so we handle
23822 them here. */
23823 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23824 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23825 return true;
23826 /* -- a qualified-id with a nested-name-specifier which contains a
23827 class-name that names a dependent type or whose unqualified-id
23828 names a dependent type. */
23829 if (TREE_CODE (type) == TYPENAME_TYPE)
23830 return true;
23832 /* An alias template specialization can be dependent even if the
23833 resulting type is not. */
23834 if (dependent_alias_template_spec_p (type))
23835 return true;
23837 /* -- a cv-qualified type where the cv-unqualified type is
23838 dependent.
23839 No code is necessary for this bullet; the code below handles
23840 cv-qualified types, and we don't want to strip aliases with
23841 TYPE_MAIN_VARIANT because of DR 1558. */
23842 /* -- a compound type constructed from any dependent type. */
23843 if (TYPE_PTRMEM_P (type))
23844 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23845 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23846 (type)));
23847 else if (TYPE_PTR_P (type)
23848 || TREE_CODE (type) == REFERENCE_TYPE)
23849 return dependent_type_p (TREE_TYPE (type));
23850 else if (TREE_CODE (type) == FUNCTION_TYPE
23851 || TREE_CODE (type) == METHOD_TYPE)
23853 tree arg_type;
23855 if (dependent_type_p (TREE_TYPE (type)))
23856 return true;
23857 for (arg_type = TYPE_ARG_TYPES (type);
23858 arg_type;
23859 arg_type = TREE_CHAIN (arg_type))
23860 if (dependent_type_p (TREE_VALUE (arg_type)))
23861 return true;
23862 if (cxx_dialect >= cxx17)
23863 /* A value-dependent noexcept-specifier makes the type dependent. */
23864 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23865 if (tree noex = TREE_PURPOSE (spec))
23866 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23867 affect overload resolution and treating it as dependent breaks
23868 things. */
23869 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23870 && value_dependent_expression_p (noex))
23871 return true;
23872 return false;
23874 /* -- an array type constructed from any dependent type or whose
23875 size is specified by a constant expression that is
23876 value-dependent.
23878 We checked for type- and value-dependence of the bounds in
23879 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23880 if (TREE_CODE (type) == ARRAY_TYPE)
23882 if (TYPE_DOMAIN (type)
23883 && dependent_type_p (TYPE_DOMAIN (type)))
23884 return true;
23885 return dependent_type_p (TREE_TYPE (type));
23888 /* -- a template-id in which either the template name is a template
23889 parameter ... */
23890 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23891 return true;
23892 /* ... or any of the template arguments is a dependent type or
23893 an expression that is type-dependent or value-dependent. */
23894 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23895 && (any_dependent_template_arguments_p
23896 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23897 return true;
23899 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23900 dependent; if the argument of the `typeof' expression is not
23901 type-dependent, then it should already been have resolved. */
23902 if (TREE_CODE (type) == TYPEOF_TYPE
23903 || TREE_CODE (type) == DECLTYPE_TYPE
23904 || TREE_CODE (type) == UNDERLYING_TYPE)
23905 return true;
23907 /* A template argument pack is dependent if any of its packed
23908 arguments are. */
23909 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23911 tree args = ARGUMENT_PACK_ARGS (type);
23912 int i, len = TREE_VEC_LENGTH (args);
23913 for (i = 0; i < len; ++i)
23914 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23915 return true;
23918 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23919 be template parameters. */
23920 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23921 return true;
23923 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23924 return true;
23926 /* The standard does not specifically mention types that are local
23927 to template functions or local classes, but they should be
23928 considered dependent too. For example:
23930 template <int I> void f() {
23931 enum E { a = I };
23932 S<sizeof (E)> s;
23935 The size of `E' cannot be known until the value of `I' has been
23936 determined. Therefore, `E' must be considered dependent. */
23937 scope = TYPE_CONTEXT (type);
23938 if (scope && TYPE_P (scope))
23939 return dependent_type_p (scope);
23940 /* Don't use type_dependent_expression_p here, as it can lead
23941 to infinite recursion trying to determine whether a lambda
23942 nested in a lambda is dependent (c++/47687). */
23943 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23944 && DECL_LANG_SPECIFIC (scope)
23945 && DECL_TEMPLATE_INFO (scope)
23946 && (any_dependent_template_arguments_p
23947 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23948 return true;
23950 /* Other types are non-dependent. */
23951 return false;
23954 /* Returns TRUE if TYPE is dependent, in the sense of
23955 [temp.dep.type]. Note that a NULL type is considered dependent. */
23957 bool
23958 dependent_type_p (tree type)
23960 /* If there are no template parameters in scope, then there can't be
23961 any dependent types. */
23962 if (!processing_template_decl)
23964 /* If we are not processing a template, then nobody should be
23965 providing us with a dependent type. */
23966 gcc_assert (type);
23967 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23968 return false;
23971 /* If the type is NULL, we have not computed a type for the entity
23972 in question; in that case, the type is dependent. */
23973 if (!type)
23974 return true;
23976 /* Erroneous types can be considered non-dependent. */
23977 if (type == error_mark_node)
23978 return false;
23980 /* Getting here with global_type_node means we improperly called this
23981 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23982 gcc_checking_assert (type != global_type_node);
23984 /* If we have not already computed the appropriate value for TYPE,
23985 do so now. */
23986 if (!TYPE_DEPENDENT_P_VALID (type))
23988 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23989 TYPE_DEPENDENT_P_VALID (type) = 1;
23992 return TYPE_DEPENDENT_P (type);
23995 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23996 lookup. In other words, a dependent type that is not the current
23997 instantiation. */
23999 bool
24000 dependent_scope_p (tree scope)
24002 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24003 && !currently_open_class (scope));
24006 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24007 an unknown base of 'this' (and is therefore instantiation-dependent). */
24009 static bool
24010 unknown_base_ref_p (tree t)
24012 if (!current_class_ptr)
24013 return false;
24015 tree mem = TREE_OPERAND (t, 1);
24016 if (shared_member_p (mem))
24017 return false;
24019 tree cur = current_nonlambda_class_type ();
24020 if (!any_dependent_bases_p (cur))
24021 return false;
24023 tree ctx = TREE_OPERAND (t, 0);
24024 if (DERIVED_FROM_P (ctx, cur))
24025 return false;
24027 return true;
24030 /* T is a SCOPE_REF; return whether we need to consider it
24031 instantiation-dependent so that we can check access at instantiation
24032 time even though we know which member it resolves to. */
24034 static bool
24035 instantiation_dependent_scope_ref_p (tree t)
24037 if (DECL_P (TREE_OPERAND (t, 1))
24038 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24039 && !unknown_base_ref_p (t)
24040 && accessible_in_template_p (TREE_OPERAND (t, 0),
24041 TREE_OPERAND (t, 1)))
24042 return false;
24043 else
24044 return true;
24047 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24048 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24049 expression. */
24051 /* Note that this predicate is not appropriate for general expressions;
24052 only constant expressions (that satisfy potential_constant_expression)
24053 can be tested for value dependence. */
24055 bool
24056 value_dependent_expression_p (tree expression)
24058 if (!processing_template_decl || expression == NULL_TREE)
24059 return false;
24061 /* A type-dependent expression is also value-dependent. */
24062 if (type_dependent_expression_p (expression))
24063 return true;
24065 switch (TREE_CODE (expression))
24067 case BASELINK:
24068 /* A dependent member function of the current instantiation. */
24069 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24071 case FUNCTION_DECL:
24072 /* A dependent member function of the current instantiation. */
24073 if (DECL_CLASS_SCOPE_P (expression)
24074 && dependent_type_p (DECL_CONTEXT (expression)))
24075 return true;
24076 break;
24078 case IDENTIFIER_NODE:
24079 /* A name that has not been looked up -- must be dependent. */
24080 return true;
24082 case TEMPLATE_PARM_INDEX:
24083 /* A non-type template parm. */
24084 return true;
24086 case CONST_DECL:
24087 /* A non-type template parm. */
24088 if (DECL_TEMPLATE_PARM_P (expression))
24089 return true;
24090 return value_dependent_expression_p (DECL_INITIAL (expression));
24092 case VAR_DECL:
24093 /* A constant with literal type and is initialized
24094 with an expression that is value-dependent. */
24095 if (DECL_DEPENDENT_INIT_P (expression)
24096 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24097 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24098 return true;
24099 if (DECL_HAS_VALUE_EXPR_P (expression))
24101 tree value_expr = DECL_VALUE_EXPR (expression);
24102 if (value_dependent_expression_p (value_expr))
24103 return true;
24105 return false;
24107 case DYNAMIC_CAST_EXPR:
24108 case STATIC_CAST_EXPR:
24109 case CONST_CAST_EXPR:
24110 case REINTERPRET_CAST_EXPR:
24111 case CAST_EXPR:
24112 case IMPLICIT_CONV_EXPR:
24113 /* These expressions are value-dependent if the type to which
24114 the cast occurs is dependent or the expression being casted
24115 is value-dependent. */
24117 tree type = TREE_TYPE (expression);
24119 if (dependent_type_p (type))
24120 return true;
24122 /* A functional cast has a list of operands. */
24123 expression = TREE_OPERAND (expression, 0);
24124 if (!expression)
24126 /* If there are no operands, it must be an expression such
24127 as "int()". This should not happen for aggregate types
24128 because it would form non-constant expressions. */
24129 gcc_assert (cxx_dialect >= cxx11
24130 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24132 return false;
24135 if (TREE_CODE (expression) == TREE_LIST)
24136 return any_value_dependent_elements_p (expression);
24138 return value_dependent_expression_p (expression);
24141 case SIZEOF_EXPR:
24142 if (SIZEOF_EXPR_TYPE_P (expression))
24143 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24144 /* FALLTHRU */
24145 case ALIGNOF_EXPR:
24146 case TYPEID_EXPR:
24147 /* A `sizeof' expression is value-dependent if the operand is
24148 type-dependent or is a pack expansion. */
24149 expression = TREE_OPERAND (expression, 0);
24150 if (PACK_EXPANSION_P (expression))
24151 return true;
24152 else if (TYPE_P (expression))
24153 return dependent_type_p (expression);
24154 return instantiation_dependent_uneval_expression_p (expression);
24156 case AT_ENCODE_EXPR:
24157 /* An 'encode' expression is value-dependent if the operand is
24158 type-dependent. */
24159 expression = TREE_OPERAND (expression, 0);
24160 return dependent_type_p (expression);
24162 case NOEXCEPT_EXPR:
24163 expression = TREE_OPERAND (expression, 0);
24164 return instantiation_dependent_uneval_expression_p (expression);
24166 case SCOPE_REF:
24167 /* All instantiation-dependent expressions should also be considered
24168 value-dependent. */
24169 return instantiation_dependent_scope_ref_p (expression);
24171 case COMPONENT_REF:
24172 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24173 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24175 case NONTYPE_ARGUMENT_PACK:
24176 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24177 is value-dependent. */
24179 tree values = ARGUMENT_PACK_ARGS (expression);
24180 int i, len = TREE_VEC_LENGTH (values);
24182 for (i = 0; i < len; ++i)
24183 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24184 return true;
24186 return false;
24189 case TRAIT_EXPR:
24191 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24193 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24194 return true;
24196 if (!type2)
24197 return false;
24199 if (TREE_CODE (type2) != TREE_LIST)
24200 return dependent_type_p (type2);
24202 for (; type2; type2 = TREE_CHAIN (type2))
24203 if (dependent_type_p (TREE_VALUE (type2)))
24204 return true;
24206 return false;
24209 case MODOP_EXPR:
24210 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24211 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24213 case ARRAY_REF:
24214 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24215 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24217 case ADDR_EXPR:
24219 tree op = TREE_OPERAND (expression, 0);
24220 return (value_dependent_expression_p (op)
24221 || has_value_dependent_address (op));
24224 case REQUIRES_EXPR:
24225 /* Treat all requires-expressions as value-dependent so
24226 we don't try to fold them. */
24227 return true;
24229 case TYPE_REQ:
24230 return dependent_type_p (TREE_OPERAND (expression, 0));
24232 case CALL_EXPR:
24234 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24235 return true;
24236 tree fn = get_callee_fndecl (expression);
24237 int i, nargs;
24238 nargs = call_expr_nargs (expression);
24239 for (i = 0; i < nargs; ++i)
24241 tree op = CALL_EXPR_ARG (expression, i);
24242 /* In a call to a constexpr member function, look through the
24243 implicit ADDR_EXPR on the object argument so that it doesn't
24244 cause the call to be considered value-dependent. We also
24245 look through it in potential_constant_expression. */
24246 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24247 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24248 && TREE_CODE (op) == ADDR_EXPR)
24249 op = TREE_OPERAND (op, 0);
24250 if (value_dependent_expression_p (op))
24251 return true;
24253 return false;
24256 case TEMPLATE_ID_EXPR:
24257 return variable_concept_p (TREE_OPERAND (expression, 0));
24259 case CONSTRUCTOR:
24261 unsigned ix;
24262 tree val;
24263 if (dependent_type_p (TREE_TYPE (expression)))
24264 return true;
24265 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24266 if (value_dependent_expression_p (val))
24267 return true;
24268 return false;
24271 case STMT_EXPR:
24272 /* Treat a GNU statement expression as dependent to avoid crashing
24273 under instantiate_non_dependent_expr; it can't be constant. */
24274 return true;
24276 default:
24277 /* A constant expression is value-dependent if any subexpression is
24278 value-dependent. */
24279 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24281 case tcc_reference:
24282 case tcc_unary:
24283 case tcc_comparison:
24284 case tcc_binary:
24285 case tcc_expression:
24286 case tcc_vl_exp:
24288 int i, len = cp_tree_operand_length (expression);
24290 for (i = 0; i < len; i++)
24292 tree t = TREE_OPERAND (expression, i);
24294 /* In some cases, some of the operands may be missing.
24295 (For example, in the case of PREDECREMENT_EXPR, the
24296 amount to increment by may be missing.) That doesn't
24297 make the expression dependent. */
24298 if (t && value_dependent_expression_p (t))
24299 return true;
24302 break;
24303 default:
24304 break;
24306 break;
24309 /* The expression is not value-dependent. */
24310 return false;
24313 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24314 [temp.dep.expr]. Note that an expression with no type is
24315 considered dependent. Other parts of the compiler arrange for an
24316 expression with type-dependent subexpressions to have no type, so
24317 this function doesn't have to be fully recursive. */
24319 bool
24320 type_dependent_expression_p (tree expression)
24322 if (!processing_template_decl)
24323 return false;
24325 if (expression == NULL_TREE || expression == error_mark_node)
24326 return false;
24328 STRIP_ANY_LOCATION_WRAPPER (expression);
24330 /* An unresolved name is always dependent. */
24331 if (identifier_p (expression)
24332 || TREE_CODE (expression) == USING_DECL
24333 || TREE_CODE (expression) == WILDCARD_DECL)
24334 return true;
24336 /* A fold expression is type-dependent. */
24337 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24338 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24339 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24340 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24341 return true;
24343 /* Some expression forms are never type-dependent. */
24344 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24345 || TREE_CODE (expression) == SIZEOF_EXPR
24346 || TREE_CODE (expression) == ALIGNOF_EXPR
24347 || TREE_CODE (expression) == AT_ENCODE_EXPR
24348 || TREE_CODE (expression) == NOEXCEPT_EXPR
24349 || TREE_CODE (expression) == TRAIT_EXPR
24350 || TREE_CODE (expression) == TYPEID_EXPR
24351 || TREE_CODE (expression) == DELETE_EXPR
24352 || TREE_CODE (expression) == VEC_DELETE_EXPR
24353 || TREE_CODE (expression) == THROW_EXPR
24354 || TREE_CODE (expression) == REQUIRES_EXPR)
24355 return false;
24357 /* The types of these expressions depends only on the type to which
24358 the cast occurs. */
24359 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24360 || TREE_CODE (expression) == STATIC_CAST_EXPR
24361 || TREE_CODE (expression) == CONST_CAST_EXPR
24362 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24363 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24364 || TREE_CODE (expression) == CAST_EXPR)
24365 return dependent_type_p (TREE_TYPE (expression));
24367 /* The types of these expressions depends only on the type created
24368 by the expression. */
24369 if (TREE_CODE (expression) == NEW_EXPR
24370 || TREE_CODE (expression) == VEC_NEW_EXPR)
24372 /* For NEW_EXPR tree nodes created inside a template, either
24373 the object type itself or a TREE_LIST may appear as the
24374 operand 1. */
24375 tree type = TREE_OPERAND (expression, 1);
24376 if (TREE_CODE (type) == TREE_LIST)
24377 /* This is an array type. We need to check array dimensions
24378 as well. */
24379 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24380 || value_dependent_expression_p
24381 (TREE_OPERAND (TREE_VALUE (type), 1));
24382 else
24383 return dependent_type_p (type);
24386 if (TREE_CODE (expression) == SCOPE_REF)
24388 tree scope = TREE_OPERAND (expression, 0);
24389 tree name = TREE_OPERAND (expression, 1);
24391 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24392 contains an identifier associated by name lookup with one or more
24393 declarations declared with a dependent type, or...a
24394 nested-name-specifier or qualified-id that names a member of an
24395 unknown specialization. */
24396 return (type_dependent_expression_p (name)
24397 || dependent_scope_p (scope));
24400 if (TREE_CODE (expression) == TEMPLATE_DECL
24401 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24402 return uses_outer_template_parms (expression);
24404 if (TREE_CODE (expression) == STMT_EXPR)
24405 expression = stmt_expr_value_expr (expression);
24407 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24409 tree elt;
24410 unsigned i;
24412 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24414 if (type_dependent_expression_p (elt))
24415 return true;
24417 return false;
24420 /* A static data member of the current instantiation with incomplete
24421 array type is type-dependent, as the definition and specializations
24422 can have different bounds. */
24423 if (VAR_P (expression)
24424 && DECL_CLASS_SCOPE_P (expression)
24425 && dependent_type_p (DECL_CONTEXT (expression))
24426 && VAR_HAD_UNKNOWN_BOUND (expression))
24427 return true;
24429 /* An array of unknown bound depending on a variadic parameter, eg:
24431 template<typename... Args>
24432 void foo (Args... args)
24434 int arr[] = { args... };
24437 template<int... vals>
24438 void bar ()
24440 int arr[] = { vals... };
24443 If the array has no length and has an initializer, it must be that
24444 we couldn't determine its length in cp_complete_array_type because
24445 it is dependent. */
24446 if (VAR_P (expression)
24447 && TREE_TYPE (expression) != NULL_TREE
24448 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24449 && !TYPE_DOMAIN (TREE_TYPE (expression))
24450 && DECL_INITIAL (expression))
24451 return true;
24453 /* A function or variable template-id is type-dependent if it has any
24454 dependent template arguments. */
24455 if (VAR_OR_FUNCTION_DECL_P (expression)
24456 && DECL_LANG_SPECIFIC (expression)
24457 && DECL_TEMPLATE_INFO (expression))
24459 /* Consider the innermost template arguments, since those are the ones
24460 that come from the template-id; the template arguments for the
24461 enclosing class do not make it type-dependent unless they are used in
24462 the type of the decl. */
24463 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24464 && (any_dependent_template_arguments_p
24465 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24466 return true;
24469 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24470 type-dependent. Checking this is important for functions with auto return
24471 type, which looks like a dependent type. */
24472 if (TREE_CODE (expression) == FUNCTION_DECL
24473 && !(DECL_CLASS_SCOPE_P (expression)
24474 && dependent_type_p (DECL_CONTEXT (expression)))
24475 && !(DECL_FRIEND_P (expression)
24476 && (!DECL_FRIEND_CONTEXT (expression)
24477 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24478 && !DECL_LOCAL_FUNCTION_P (expression))
24480 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24481 || undeduced_auto_decl (expression));
24482 return false;
24485 /* Always dependent, on the number of arguments if nothing else. */
24486 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24487 return true;
24489 if (TREE_TYPE (expression) == unknown_type_node)
24491 if (TREE_CODE (expression) == ADDR_EXPR)
24492 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24493 if (TREE_CODE (expression) == COMPONENT_REF
24494 || TREE_CODE (expression) == OFFSET_REF)
24496 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24497 return true;
24498 expression = TREE_OPERAND (expression, 1);
24499 if (identifier_p (expression))
24500 return false;
24502 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24503 if (TREE_CODE (expression) == SCOPE_REF)
24504 return false;
24506 if (BASELINK_P (expression))
24508 if (BASELINK_OPTYPE (expression)
24509 && dependent_type_p (BASELINK_OPTYPE (expression)))
24510 return true;
24511 expression = BASELINK_FUNCTIONS (expression);
24514 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24516 if (any_dependent_template_arguments_p
24517 (TREE_OPERAND (expression, 1)))
24518 return true;
24519 expression = TREE_OPERAND (expression, 0);
24520 if (identifier_p (expression))
24521 return true;
24524 gcc_assert (TREE_CODE (expression) == OVERLOAD
24525 || TREE_CODE (expression) == FUNCTION_DECL);
24527 for (lkp_iterator iter (expression); iter; ++iter)
24528 if (type_dependent_expression_p (*iter))
24529 return true;
24531 return false;
24534 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24536 /* Dependent type attributes might not have made it from the decl to
24537 the type yet. */
24538 if (DECL_P (expression)
24539 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24540 return true;
24542 return (dependent_type_p (TREE_TYPE (expression)));
24545 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24546 type-dependent if the expression refers to a member of the current
24547 instantiation and the type of the referenced member is dependent, or the
24548 class member access expression refers to a member of an unknown
24549 specialization.
24551 This function returns true if the OBJECT in such a class member access
24552 expression is of an unknown specialization. */
24554 bool
24555 type_dependent_object_expression_p (tree object)
24557 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24558 dependent. */
24559 if (TREE_CODE (object) == IDENTIFIER_NODE)
24560 return true;
24561 tree scope = TREE_TYPE (object);
24562 return (!scope || dependent_scope_p (scope));
24565 /* walk_tree callback function for instantiation_dependent_expression_p,
24566 below. Returns non-zero if a dependent subexpression is found. */
24568 static tree
24569 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24570 void * /*data*/)
24572 if (TYPE_P (*tp))
24574 /* We don't have to worry about decltype currently because decltype
24575 of an instantiation-dependent expr is a dependent type. This
24576 might change depending on the resolution of DR 1172. */
24577 *walk_subtrees = false;
24578 return NULL_TREE;
24580 enum tree_code code = TREE_CODE (*tp);
24581 switch (code)
24583 /* Don't treat an argument list as dependent just because it has no
24584 TREE_TYPE. */
24585 case TREE_LIST:
24586 case TREE_VEC:
24587 return NULL_TREE;
24589 case TEMPLATE_PARM_INDEX:
24590 return *tp;
24592 /* Handle expressions with type operands. */
24593 case SIZEOF_EXPR:
24594 case ALIGNOF_EXPR:
24595 case TYPEID_EXPR:
24596 case AT_ENCODE_EXPR:
24598 tree op = TREE_OPERAND (*tp, 0);
24599 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24600 op = TREE_TYPE (op);
24601 if (TYPE_P (op))
24603 if (dependent_type_p (op))
24604 return *tp;
24605 else
24607 *walk_subtrees = false;
24608 return NULL_TREE;
24611 break;
24614 case COMPONENT_REF:
24615 if (identifier_p (TREE_OPERAND (*tp, 1)))
24616 /* In a template, finish_class_member_access_expr creates a
24617 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24618 type-dependent, so that we can check access control at
24619 instantiation time (PR 42277). See also Core issue 1273. */
24620 return *tp;
24621 break;
24623 case SCOPE_REF:
24624 if (instantiation_dependent_scope_ref_p (*tp))
24625 return *tp;
24626 else
24627 break;
24629 /* Treat statement-expressions as dependent. */
24630 case BIND_EXPR:
24631 return *tp;
24633 /* Treat requires-expressions as dependent. */
24634 case REQUIRES_EXPR:
24635 return *tp;
24637 case CALL_EXPR:
24638 /* Treat calls to function concepts as dependent. */
24639 if (function_concept_check_p (*tp))
24640 return *tp;
24641 break;
24643 case TEMPLATE_ID_EXPR:
24644 /* And variable concepts. */
24645 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24646 return *tp;
24647 break;
24649 default:
24650 break;
24653 if (type_dependent_expression_p (*tp))
24654 return *tp;
24655 else
24656 return NULL_TREE;
24659 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24660 sense defined by the ABI:
24662 "An expression is instantiation-dependent if it is type-dependent
24663 or value-dependent, or it has a subexpression that is type-dependent
24664 or value-dependent."
24666 Except don't actually check value-dependence for unevaluated expressions,
24667 because in sizeof(i) we don't care about the value of i. Checking
24668 type-dependence will in turn check value-dependence of array bounds/template
24669 arguments as needed. */
24671 bool
24672 instantiation_dependent_uneval_expression_p (tree expression)
24674 tree result;
24676 if (!processing_template_decl)
24677 return false;
24679 if (expression == error_mark_node)
24680 return false;
24682 result = cp_walk_tree_without_duplicates (&expression,
24683 instantiation_dependent_r, NULL);
24684 return result != NULL_TREE;
24687 /* As above, but also check value-dependence of the expression as a whole. */
24689 bool
24690 instantiation_dependent_expression_p (tree expression)
24692 return (instantiation_dependent_uneval_expression_p (expression)
24693 || value_dependent_expression_p (expression));
24696 /* Like type_dependent_expression_p, but it also works while not processing
24697 a template definition, i.e. during substitution or mangling. */
24699 bool
24700 type_dependent_expression_p_push (tree expr)
24702 bool b;
24703 ++processing_template_decl;
24704 b = type_dependent_expression_p (expr);
24705 --processing_template_decl;
24706 return b;
24709 /* Returns TRUE if ARGS contains a type-dependent expression. */
24711 bool
24712 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24714 unsigned int i;
24715 tree arg;
24717 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24719 if (type_dependent_expression_p (arg))
24720 return true;
24722 return false;
24725 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24726 expressions) contains any type-dependent expressions. */
24728 bool
24729 any_type_dependent_elements_p (const_tree list)
24731 for (; list; list = TREE_CHAIN (list))
24732 if (type_dependent_expression_p (TREE_VALUE (list)))
24733 return true;
24735 return false;
24738 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24739 expressions) contains any value-dependent expressions. */
24741 bool
24742 any_value_dependent_elements_p (const_tree list)
24744 for (; list; list = TREE_CHAIN (list))
24745 if (value_dependent_expression_p (TREE_VALUE (list)))
24746 return true;
24748 return false;
24751 /* Returns TRUE if the ARG (a template argument) is dependent. */
24753 bool
24754 dependent_template_arg_p (tree arg)
24756 if (!processing_template_decl)
24757 return false;
24759 /* Assume a template argument that was wrongly written by the user
24760 is dependent. This is consistent with what
24761 any_dependent_template_arguments_p [that calls this function]
24762 does. */
24763 if (!arg || arg == error_mark_node)
24764 return true;
24766 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24767 arg = argument_pack_select_arg (arg);
24769 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24770 return true;
24771 if (TREE_CODE (arg) == TEMPLATE_DECL)
24773 if (DECL_TEMPLATE_PARM_P (arg))
24774 return true;
24775 /* A member template of a dependent class is not necessarily
24776 type-dependent, but it is a dependent template argument because it
24777 will be a member of an unknown specialization to that template. */
24778 tree scope = CP_DECL_CONTEXT (arg);
24779 return TYPE_P (scope) && dependent_type_p (scope);
24781 else if (ARGUMENT_PACK_P (arg))
24783 tree args = ARGUMENT_PACK_ARGS (arg);
24784 int i, len = TREE_VEC_LENGTH (args);
24785 for (i = 0; i < len; ++i)
24787 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24788 return true;
24791 return false;
24793 else if (TYPE_P (arg))
24794 return dependent_type_p (arg);
24795 else
24796 return (type_dependent_expression_p (arg)
24797 || value_dependent_expression_p (arg));
24800 /* Returns true if ARGS (a collection of template arguments) contains
24801 any types that require structural equality testing. */
24803 bool
24804 any_template_arguments_need_structural_equality_p (tree args)
24806 int i;
24807 int j;
24809 if (!args)
24810 return false;
24811 if (args == error_mark_node)
24812 return true;
24814 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24816 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24817 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24819 tree arg = TREE_VEC_ELT (level, j);
24820 tree packed_args = NULL_TREE;
24821 int k, len = 1;
24823 if (ARGUMENT_PACK_P (arg))
24825 /* Look inside the argument pack. */
24826 packed_args = ARGUMENT_PACK_ARGS (arg);
24827 len = TREE_VEC_LENGTH (packed_args);
24830 for (k = 0; k < len; ++k)
24832 if (packed_args)
24833 arg = TREE_VEC_ELT (packed_args, k);
24835 if (error_operand_p (arg))
24836 return true;
24837 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24838 continue;
24839 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24840 return true;
24841 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24842 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24843 return true;
24848 return false;
24851 /* Returns true if ARGS (a collection of template arguments) contains
24852 any dependent arguments. */
24854 bool
24855 any_dependent_template_arguments_p (const_tree args)
24857 int i;
24858 int j;
24860 if (!args)
24861 return false;
24862 if (args == error_mark_node)
24863 return true;
24865 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24867 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24868 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24869 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24870 return true;
24873 return false;
24876 /* Returns TRUE if the template TMPL is type-dependent. */
24878 bool
24879 dependent_template_p (tree tmpl)
24881 if (TREE_CODE (tmpl) == OVERLOAD)
24883 for (lkp_iterator iter (tmpl); iter; ++iter)
24884 if (dependent_template_p (*iter))
24885 return true;
24886 return false;
24889 /* Template template parameters are dependent. */
24890 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24891 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24892 return true;
24893 /* So are names that have not been looked up. */
24894 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24895 return true;
24896 return false;
24899 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24901 bool
24902 dependent_template_id_p (tree tmpl, tree args)
24904 return (dependent_template_p (tmpl)
24905 || any_dependent_template_arguments_p (args));
24908 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24909 are dependent. */
24911 bool
24912 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24914 int i;
24916 if (!processing_template_decl)
24917 return false;
24919 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24921 tree decl = TREE_VEC_ELT (declv, i);
24922 tree init = TREE_VEC_ELT (initv, i);
24923 tree cond = TREE_VEC_ELT (condv, i);
24924 tree incr = TREE_VEC_ELT (incrv, i);
24926 if (type_dependent_expression_p (decl)
24927 || TREE_CODE (decl) == SCOPE_REF)
24928 return true;
24930 if (init && type_dependent_expression_p (init))
24931 return true;
24933 if (type_dependent_expression_p (cond))
24934 return true;
24936 if (COMPARISON_CLASS_P (cond)
24937 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24938 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24939 return true;
24941 if (TREE_CODE (incr) == MODOP_EXPR)
24943 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24944 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24945 return true;
24947 else if (type_dependent_expression_p (incr))
24948 return true;
24949 else if (TREE_CODE (incr) == MODIFY_EXPR)
24951 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24952 return true;
24953 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24955 tree t = TREE_OPERAND (incr, 1);
24956 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24957 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24958 return true;
24963 return false;
24966 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24967 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24968 no such TYPE can be found. Note that this function peers inside
24969 uninstantiated templates and therefore should be used only in
24970 extremely limited situations. ONLY_CURRENT_P restricts this
24971 peering to the currently open classes hierarchy (which is required
24972 when comparing types). */
24974 tree
24975 resolve_typename_type (tree type, bool only_current_p)
24977 tree scope;
24978 tree name;
24979 tree decl;
24980 int quals;
24981 tree pushed_scope;
24982 tree result;
24984 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24986 scope = TYPE_CONTEXT (type);
24987 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24988 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24989 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24990 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24991 identifier of the TYPENAME_TYPE anymore.
24992 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24993 TYPENAME_TYPE instead, we avoid messing up with a possible
24994 typedef variant case. */
24995 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24997 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24998 it first before we can figure out what NAME refers to. */
24999 if (TREE_CODE (scope) == TYPENAME_TYPE)
25001 if (TYPENAME_IS_RESOLVING_P (scope))
25002 /* Given a class template A with a dependent base with nested type C,
25003 typedef typename A::C::C C will land us here, as trying to resolve
25004 the initial A::C leads to the local C typedef, which leads back to
25005 A::C::C. So we break the recursion now. */
25006 return type;
25007 else
25008 scope = resolve_typename_type (scope, only_current_p);
25010 /* If we don't know what SCOPE refers to, then we cannot resolve the
25011 TYPENAME_TYPE. */
25012 if (!CLASS_TYPE_P (scope))
25013 return type;
25014 /* If this is a typedef, we don't want to look inside (c++/11987). */
25015 if (typedef_variant_p (type))
25016 return type;
25017 /* If SCOPE isn't the template itself, it will not have a valid
25018 TYPE_FIELDS list. */
25019 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25020 /* scope is either the template itself or a compatible instantiation
25021 like X<T>, so look up the name in the original template. */
25022 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25023 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25024 gcc_checking_assert (uses_template_parms (scope));
25025 /* If scope has no fields, it can't be a current instantiation. Check this
25026 before currently_open_class to avoid infinite recursion (71515). */
25027 if (!TYPE_FIELDS (scope))
25028 return type;
25029 /* If the SCOPE is not the current instantiation, there's no reason
25030 to look inside it. */
25031 if (only_current_p && !currently_open_class (scope))
25032 return type;
25033 /* Enter the SCOPE so that name lookup will be resolved as if we
25034 were in the class definition. In particular, SCOPE will no
25035 longer be considered a dependent type. */
25036 pushed_scope = push_scope (scope);
25037 /* Look up the declaration. */
25038 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25039 tf_warning_or_error);
25041 result = NULL_TREE;
25043 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25044 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25045 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25046 if (!decl)
25047 /*nop*/;
25048 else if (identifier_p (fullname)
25049 && TREE_CODE (decl) == TYPE_DECL)
25051 result = TREE_TYPE (decl);
25052 if (result == error_mark_node)
25053 result = NULL_TREE;
25055 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25056 && DECL_CLASS_TEMPLATE_P (decl))
25058 /* Obtain the template and the arguments. */
25059 tree tmpl = TREE_OPERAND (fullname, 0);
25060 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25062 /* We get here with a plain identifier because a previous tentative
25063 parse of the nested-name-specifier as part of a ptr-operator saw
25064 ::template X<A>. The use of ::template is necessary in a
25065 ptr-operator, but wrong in a declarator-id.
25067 [temp.names]: In a qualified-id of a declarator-id, the keyword
25068 template shall not appear at the top level. */
25069 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25070 "keyword %<template%> not allowed in declarator-id");
25071 tmpl = decl;
25073 tree args = TREE_OPERAND (fullname, 1);
25074 /* Instantiate the template. */
25075 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25076 /*entering_scope=*/true,
25077 tf_error | tf_user);
25078 if (result == error_mark_node)
25079 result = NULL_TREE;
25082 /* Leave the SCOPE. */
25083 if (pushed_scope)
25084 pop_scope (pushed_scope);
25086 /* If we failed to resolve it, return the original typename. */
25087 if (!result)
25088 return type;
25090 /* If lookup found a typename type, resolve that too. */
25091 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25093 /* Ill-formed programs can cause infinite recursion here, so we
25094 must catch that. */
25095 TYPENAME_IS_RESOLVING_P (result) = 1;
25096 result = resolve_typename_type (result, only_current_p);
25097 TYPENAME_IS_RESOLVING_P (result) = 0;
25100 /* Qualify the resulting type. */
25101 quals = cp_type_quals (type);
25102 if (quals)
25103 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25105 return result;
25108 /* EXPR is an expression which is not type-dependent. Return a proxy
25109 for EXPR that can be used to compute the types of larger
25110 expressions containing EXPR. */
25112 tree
25113 build_non_dependent_expr (tree expr)
25115 tree orig_expr = expr;
25116 tree inner_expr;
25118 /* When checking, try to get a constant value for all non-dependent
25119 expressions in order to expose bugs in *_dependent_expression_p
25120 and constexpr. This can affect code generation, see PR70704, so
25121 only do this for -fchecking=2. */
25122 if (flag_checking > 1
25123 && cxx_dialect >= cxx11
25124 /* Don't do this during nsdmi parsing as it can lead to
25125 unexpected recursive instantiations. */
25126 && !parsing_nsdmi ()
25127 /* Don't do this during concept expansion either and for
25128 the same reason. */
25129 && !expanding_concept ())
25130 fold_non_dependent_expr (expr);
25132 STRIP_ANY_LOCATION_WRAPPER (expr);
25134 /* Preserve OVERLOADs; the functions must be available to resolve
25135 types. */
25136 inner_expr = expr;
25137 if (TREE_CODE (inner_expr) == STMT_EXPR)
25138 inner_expr = stmt_expr_value_expr (inner_expr);
25139 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25140 inner_expr = TREE_OPERAND (inner_expr, 0);
25141 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25142 inner_expr = TREE_OPERAND (inner_expr, 1);
25143 if (is_overloaded_fn (inner_expr)
25144 || TREE_CODE (inner_expr) == OFFSET_REF)
25145 return orig_expr;
25146 /* There is no need to return a proxy for a variable. */
25147 if (VAR_P (expr))
25148 return orig_expr;
25149 /* Preserve string constants; conversions from string constants to
25150 "char *" are allowed, even though normally a "const char *"
25151 cannot be used to initialize a "char *". */
25152 if (TREE_CODE (expr) == STRING_CST)
25153 return orig_expr;
25154 /* Preserve void and arithmetic constants, as an optimization -- there is no
25155 reason to create a new node. */
25156 if (TREE_CODE (expr) == VOID_CST
25157 || TREE_CODE (expr) == INTEGER_CST
25158 || TREE_CODE (expr) == REAL_CST)
25159 return orig_expr;
25160 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25161 There is at least one place where we want to know that a
25162 particular expression is a throw-expression: when checking a ?:
25163 expression, there are special rules if the second or third
25164 argument is a throw-expression. */
25165 if (TREE_CODE (expr) == THROW_EXPR)
25166 return orig_expr;
25168 /* Don't wrap an initializer list, we need to be able to look inside. */
25169 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25170 return orig_expr;
25172 /* Don't wrap a dummy object, we need to be able to test for it. */
25173 if (is_dummy_object (expr))
25174 return orig_expr;
25176 if (TREE_CODE (expr) == COND_EXPR)
25177 return build3 (COND_EXPR,
25178 TREE_TYPE (expr),
25179 TREE_OPERAND (expr, 0),
25180 (TREE_OPERAND (expr, 1)
25181 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25182 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25183 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25184 if (TREE_CODE (expr) == COMPOUND_EXPR
25185 && !COMPOUND_EXPR_OVERLOADED (expr))
25186 return build2 (COMPOUND_EXPR,
25187 TREE_TYPE (expr),
25188 TREE_OPERAND (expr, 0),
25189 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25191 /* If the type is unknown, it can't really be non-dependent */
25192 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25194 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25195 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25198 /* ARGS is a vector of expressions as arguments to a function call.
25199 Replace the arguments with equivalent non-dependent expressions.
25200 This modifies ARGS in place. */
25202 void
25203 make_args_non_dependent (vec<tree, va_gc> *args)
25205 unsigned int ix;
25206 tree arg;
25208 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25210 tree newarg = build_non_dependent_expr (arg);
25211 if (newarg != arg)
25212 (*args)[ix] = newarg;
25216 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25217 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25218 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25220 static tree
25221 make_auto_1 (tree name, bool set_canonical)
25223 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25224 TYPE_NAME (au) = build_decl (input_location,
25225 TYPE_DECL, name, au);
25226 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25227 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25228 (0, processing_template_decl + 1, processing_template_decl + 1,
25229 TYPE_NAME (au), NULL_TREE);
25230 if (set_canonical)
25231 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25232 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25233 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25235 return au;
25238 tree
25239 make_decltype_auto (void)
25241 return make_auto_1 (decltype_auto_identifier, true);
25244 tree
25245 make_auto (void)
25247 return make_auto_1 (auto_identifier, true);
25250 /* Return a C++17 deduction placeholder for class template TMPL. */
25252 tree
25253 make_template_placeholder (tree tmpl)
25255 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25256 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25257 return t;
25260 /* True iff T is a C++17 class template deduction placeholder. */
25262 bool
25263 template_placeholder_p (tree t)
25265 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25268 /* Make a "constrained auto" type-specifier. This is an
25269 auto type with constraints that must be associated after
25270 deduction. The constraint is formed from the given
25271 CONC and its optional sequence of arguments, which are
25272 non-null if written as partial-concept-id. */
25274 tree
25275 make_constrained_auto (tree con, tree args)
25277 tree type = make_auto_1 (auto_identifier, false);
25279 /* Build the constraint. */
25280 tree tmpl = DECL_TI_TEMPLATE (con);
25281 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25282 expr = build_concept_check (expr, type, args);
25284 tree constr = normalize_expression (expr);
25285 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25287 /* Our canonical type depends on the constraint. */
25288 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25290 /* Attach the constraint to the type declaration. */
25291 tree decl = TYPE_NAME (type);
25292 return decl;
25295 /* Given type ARG, return std::initializer_list<ARG>. */
25297 static tree
25298 listify (tree arg)
25300 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25302 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25304 gcc_rich_location richloc (input_location);
25305 maybe_add_include_fixit (&richloc, "<initializer_list>");
25306 error_at (&richloc,
25307 "deducing from brace-enclosed initializer list"
25308 " requires %<#include <initializer_list>%>");
25310 return error_mark_node;
25312 tree argvec = make_tree_vec (1);
25313 TREE_VEC_ELT (argvec, 0) = arg;
25315 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25316 NULL_TREE, 0, tf_warning_or_error);
25319 /* Replace auto in TYPE with std::initializer_list<auto>. */
25321 static tree
25322 listify_autos (tree type, tree auto_node)
25324 tree init_auto = listify (auto_node);
25325 tree argvec = make_tree_vec (1);
25326 TREE_VEC_ELT (argvec, 0) = init_auto;
25327 if (processing_template_decl)
25328 argvec = add_to_template_args (current_template_args (), argvec);
25329 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25332 /* Hash traits for hashing possibly constrained 'auto'
25333 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25335 struct auto_hash : default_hash_traits<tree>
25337 static inline hashval_t hash (tree);
25338 static inline bool equal (tree, tree);
25341 /* Hash the 'auto' T. */
25343 inline hashval_t
25344 auto_hash::hash (tree t)
25346 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25347 /* Matching constrained-type-specifiers denote the same template
25348 parameter, so hash the constraint. */
25349 return hash_placeholder_constraint (c);
25350 else
25351 /* But unconstrained autos are all separate, so just hash the pointer. */
25352 return iterative_hash_object (t, 0);
25355 /* Compare two 'auto's. */
25357 inline bool
25358 auto_hash::equal (tree t1, tree t2)
25360 if (t1 == t2)
25361 return true;
25363 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25364 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25366 /* Two unconstrained autos are distinct. */
25367 if (!c1 || !c2)
25368 return false;
25370 return equivalent_placeholder_constraints (c1, c2);
25373 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25374 constrained) auto, add it to the vector. */
25376 static int
25377 extract_autos_r (tree t, void *data)
25379 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25380 if (is_auto (t))
25382 /* All the autos were built with index 0; fix that up now. */
25383 tree *p = hash.find_slot (t, INSERT);
25384 unsigned idx;
25385 if (*p)
25386 /* If this is a repeated constrained-type-specifier, use the index we
25387 chose before. */
25388 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25389 else
25391 /* Otherwise this is new, so use the current count. */
25392 *p = t;
25393 idx = hash.elements () - 1;
25395 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25398 /* Always keep walking. */
25399 return 0;
25402 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25403 says they can appear anywhere in the type. */
25405 static tree
25406 extract_autos (tree type)
25408 hash_set<tree> visited;
25409 hash_table<auto_hash> hash (2);
25411 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25413 tree tree_vec = make_tree_vec (hash.elements());
25414 for (hash_table<auto_hash>::iterator iter = hash.begin();
25415 iter != hash.end(); ++iter)
25417 tree elt = *iter;
25418 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25419 TREE_VEC_ELT (tree_vec, i)
25420 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25423 return tree_vec;
25426 /* The stem for deduction guide names. */
25427 const char *const dguide_base = "__dguide_";
25429 /* Return the name for a deduction guide for class template TMPL. */
25431 tree
25432 dguide_name (tree tmpl)
25434 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25435 tree tname = TYPE_IDENTIFIER (type);
25436 char *buf = (char *) alloca (1 + strlen (dguide_base)
25437 + IDENTIFIER_LENGTH (tname));
25438 memcpy (buf, dguide_base, strlen (dguide_base));
25439 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25440 IDENTIFIER_LENGTH (tname) + 1);
25441 tree dname = get_identifier (buf);
25442 TREE_TYPE (dname) = type;
25443 return dname;
25446 /* True if NAME is the name of a deduction guide. */
25448 bool
25449 dguide_name_p (tree name)
25451 return (TREE_TYPE (name)
25452 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25453 strlen (dguide_base)));
25456 /* True if FN is a deduction guide. */
25458 bool
25459 deduction_guide_p (const_tree fn)
25461 if (DECL_P (fn))
25462 if (tree name = DECL_NAME (fn))
25463 return dguide_name_p (name);
25464 return false;
25467 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25469 bool
25470 copy_guide_p (const_tree fn)
25472 gcc_assert (deduction_guide_p (fn));
25473 if (!DECL_ARTIFICIAL (fn))
25474 return false;
25475 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25476 return (TREE_CHAIN (parms) == void_list_node
25477 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25480 /* True if FN is a guide generated from a constructor template. */
25482 bool
25483 template_guide_p (const_tree fn)
25485 gcc_assert (deduction_guide_p (fn));
25486 if (!DECL_ARTIFICIAL (fn))
25487 return false;
25488 tree tmpl = DECL_TI_TEMPLATE (fn);
25489 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25490 return PRIMARY_TEMPLATE_P (org);
25491 return false;
25494 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25495 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25496 template parameter types. Note that the handling of template template
25497 parameters relies on current_template_parms being set appropriately for the
25498 new template. */
25500 static tree
25501 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25502 tree tsubst_args, tsubst_flags_t complain)
25504 tree oldidx = get_template_parm_index (olddecl);
25506 tree newtype;
25507 if (TREE_CODE (olddecl) == TYPE_DECL
25508 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25510 tree oldtype = TREE_TYPE (olddecl);
25511 newtype = cxx_make_type (TREE_CODE (oldtype));
25512 TYPE_MAIN_VARIANT (newtype) = newtype;
25513 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25514 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25515 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25517 else
25518 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25519 complain, NULL_TREE);
25521 tree newdecl
25522 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25523 DECL_NAME (olddecl), newtype);
25524 SET_DECL_TEMPLATE_PARM_P (newdecl);
25526 tree newidx;
25527 if (TREE_CODE (olddecl) == TYPE_DECL
25528 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25530 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25531 = build_template_parm_index (index, level, level,
25532 newdecl, newtype);
25533 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25534 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25535 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25536 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25538 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25540 DECL_TEMPLATE_RESULT (newdecl)
25541 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25542 DECL_NAME (olddecl), newtype);
25543 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25544 // First create a copy (ttargs) of tsubst_args with an
25545 // additional level for the template template parameter's own
25546 // template parameters (ttparms).
25547 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25548 (DECL_TEMPLATE_PARMS (olddecl)));
25549 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25550 tree ttargs = make_tree_vec (depth + 1);
25551 for (int i = 0; i < depth; ++i)
25552 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25553 TREE_VEC_ELT (ttargs, depth)
25554 = template_parms_level_to_args (ttparms);
25555 // Substitute ttargs into ttparms to fix references to
25556 // other template parameters.
25557 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25558 complain);
25559 // Now substitute again with args based on tparms, to reduce
25560 // the level of the ttparms.
25561 ttargs = current_template_args ();
25562 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25563 complain);
25564 // Finally, tack the adjusted parms onto tparms.
25565 ttparms = tree_cons (size_int (depth), ttparms,
25566 current_template_parms);
25567 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25570 else
25572 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25573 tree newconst
25574 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25575 TREE_CODE (oldconst),
25576 DECL_NAME (oldconst), newtype);
25577 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25578 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25579 SET_DECL_TEMPLATE_PARM_P (newconst);
25580 newidx = build_template_parm_index (index, level, level,
25581 newconst, newtype);
25582 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25583 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25584 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25587 return newdecl;
25590 /* Returns a C++17 class deduction guide template based on the constructor
25591 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25592 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25594 static tree
25595 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25597 tree type, tparms, targs, fparms, fargs, ci;
25598 bool memtmpl = false;
25599 bool explicit_p;
25600 location_t loc;
25601 tree fn_tmpl = NULL_TREE;
25603 if (TYPE_P (ctor))
25605 type = ctor;
25606 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25607 if (copy_p)
25609 type = TREE_TYPE (type);
25610 fparms = tree_cons (NULL_TREE, type, void_list_node);
25612 else
25613 fparms = void_list_node;
25615 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25616 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25617 targs = CLASSTYPE_TI_ARGS (type);
25618 ci = NULL_TREE;
25619 fargs = NULL_TREE;
25620 loc = DECL_SOURCE_LOCATION (ctmpl);
25621 explicit_p = false;
25623 else
25625 ++processing_template_decl;
25627 fn_tmpl
25628 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25629 : DECL_TI_TEMPLATE (ctor));
25630 if (outer_args)
25631 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25632 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25634 type = DECL_CONTEXT (ctor);
25636 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25637 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25638 fully specialized args for the enclosing class. Strip those off, as
25639 the deduction guide won't have those template parameters. */
25640 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25641 TMPL_PARMS_DEPTH (tparms));
25642 /* Discard the 'this' parameter. */
25643 fparms = FUNCTION_ARG_CHAIN (ctor);
25644 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25645 ci = get_constraints (ctor);
25646 loc = DECL_SOURCE_LOCATION (ctor);
25647 explicit_p = DECL_NONCONVERTING_P (ctor);
25649 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25651 memtmpl = true;
25653 /* For a member template constructor, we need to flatten the two
25654 template parameter lists into one, and then adjust the function
25655 signature accordingly. This gets...complicated. */
25656 tree save_parms = current_template_parms;
25658 /* For a member template we should have two levels of parms/args, one
25659 for the class and one for the constructor. We stripped
25660 specialized args for further enclosing classes above. */
25661 const int depth = 2;
25662 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25664 /* Template args for translating references to the two-level template
25665 parameters into references to the one-level template parameters we
25666 are creating. */
25667 tree tsubst_args = copy_node (targs);
25668 TMPL_ARGS_LEVEL (tsubst_args, depth)
25669 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25671 /* Template parms for the constructor template. */
25672 tree ftparms = TREE_VALUE (tparms);
25673 unsigned flen = TREE_VEC_LENGTH (ftparms);
25674 /* Template parms for the class template. */
25675 tparms = TREE_CHAIN (tparms);
25676 tree ctparms = TREE_VALUE (tparms);
25677 unsigned clen = TREE_VEC_LENGTH (ctparms);
25678 /* Template parms for the deduction guide start as a copy of the
25679 template parms for the class. We set current_template_parms for
25680 lookup_template_class_1. */
25681 current_template_parms = tparms = copy_node (tparms);
25682 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25683 for (unsigned i = 0; i < clen; ++i)
25684 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25686 /* Now we need to rewrite the constructor parms to append them to the
25687 class parms. */
25688 for (unsigned i = 0; i < flen; ++i)
25690 unsigned index = i + clen;
25691 unsigned level = 1;
25692 tree oldelt = TREE_VEC_ELT (ftparms, i);
25693 tree olddecl = TREE_VALUE (oldelt);
25694 tree newdecl = rewrite_template_parm (olddecl, index, level,
25695 tsubst_args, complain);
25696 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25697 tsubst_args, complain, ctor);
25698 tree list = build_tree_list (newdef, newdecl);
25699 TEMPLATE_PARM_CONSTRAINTS (list)
25700 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25701 tsubst_args, complain, ctor);
25702 TREE_VEC_ELT (new_vec, index) = list;
25703 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25706 /* Now we have a final set of template parms to substitute into the
25707 function signature. */
25708 targs = template_parms_to_args (tparms);
25709 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25710 complain, ctor);
25711 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25712 if (ci)
25713 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25715 current_template_parms = save_parms;
25717 --processing_template_decl;
25720 if (!memtmpl)
25722 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25723 tparms = copy_node (tparms);
25724 INNERMOST_TEMPLATE_PARMS (tparms)
25725 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25728 tree fntype = build_function_type (type, fparms);
25729 tree ded_fn = build_lang_decl_loc (loc,
25730 FUNCTION_DECL,
25731 dguide_name (type), fntype);
25732 DECL_ARGUMENTS (ded_fn) = fargs;
25733 DECL_ARTIFICIAL (ded_fn) = true;
25734 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25735 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25736 DECL_ARTIFICIAL (ded_tmpl) = true;
25737 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25738 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25739 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25740 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25741 if (DECL_P (ctor))
25742 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25743 if (ci)
25744 set_constraints (ded_tmpl, ci);
25746 return ded_tmpl;
25749 /* Deduce template arguments for the class template placeholder PTYPE for
25750 template TMPL based on the initializer INIT, and return the resulting
25751 type. */
25753 static tree
25754 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25755 tsubst_flags_t complain)
25757 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25759 /* We should have handled this in the caller. */
25760 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25761 return ptype;
25762 if (complain & tf_error)
25763 error ("non-class template %qT used without template arguments", tmpl);
25764 return error_mark_node;
25767 tree type = TREE_TYPE (tmpl);
25769 bool try_list_ctor = false;
25771 vec<tree,va_gc> *args;
25772 if (init == NULL_TREE
25773 || TREE_CODE (init) == TREE_LIST)
25774 args = make_tree_vector_from_list (init);
25775 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25777 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25778 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25780 /* As an exception, the first phase in 16.3.1.7 (considering the
25781 initializer list as a single argument) is omitted if the
25782 initializer list consists of a single expression of type cv U,
25783 where U is a specialization of C or a class derived from a
25784 specialization of C. */
25785 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25786 tree etype = TREE_TYPE (elt);
25788 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25789 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25790 int err = unify (tparms, targs, type, etype,
25791 UNIFY_ALLOW_DERIVED, /*explain*/false);
25792 if (err == 0)
25793 try_list_ctor = false;
25794 ggc_free (targs);
25796 if (try_list_ctor || is_std_init_list (type))
25797 args = make_tree_vector_single (init);
25798 else
25799 args = make_tree_vector_from_ctor (init);
25801 else
25802 args = make_tree_vector_single (init);
25804 tree dname = dguide_name (tmpl);
25805 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25806 /*type*/false, /*complain*/false,
25807 /*hidden*/false);
25808 bool elided = false;
25809 if (cands == error_mark_node)
25810 cands = NULL_TREE;
25812 /* Prune explicit deduction guides in copy-initialization context. */
25813 if (flags & LOOKUP_ONLYCONVERTING)
25815 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25816 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25817 elided = true;
25819 if (elided)
25821 /* Found a nonconverting guide, prune the candidates. */
25822 tree pruned = NULL_TREE;
25823 for (lkp_iterator iter (cands); iter; ++iter)
25824 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25825 pruned = lookup_add (*iter, pruned);
25827 cands = pruned;
25831 tree outer_args = NULL_TREE;
25832 if (DECL_CLASS_SCOPE_P (tmpl)
25833 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25835 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25836 type = TREE_TYPE (most_general_template (tmpl));
25839 bool saw_ctor = false;
25840 // FIXME cache artificial deduction guides
25841 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25843 tree guide = build_deduction_guide (*iter, outer_args, complain);
25844 if ((flags & LOOKUP_ONLYCONVERTING)
25845 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25846 elided = true;
25847 else
25848 cands = lookup_add (guide, cands);
25850 saw_ctor = true;
25853 tree call = error_mark_node;
25855 /* If this is list-initialization and the class has a list constructor, first
25856 try deducing from the list as a single argument, as [over.match.list]. */
25857 tree list_cands = NULL_TREE;
25858 if (try_list_ctor && cands)
25859 for (lkp_iterator iter (cands); iter; ++iter)
25861 tree dg = *iter;
25862 if (is_list_ctor (dg))
25863 list_cands = lookup_add (dg, list_cands);
25865 if (list_cands)
25867 ++cp_unevaluated_operand;
25868 call = build_new_function_call (list_cands, &args, tf_decltype);
25869 --cp_unevaluated_operand;
25871 if (call == error_mark_node)
25873 /* That didn't work, now try treating the list as a sequence of
25874 arguments. */
25875 release_tree_vector (args);
25876 args = make_tree_vector_from_ctor (init);
25880 /* Maybe generate an implicit deduction guide. */
25881 if (call == error_mark_node && args->length () < 2)
25883 tree gtype = NULL_TREE;
25885 if (args->length () == 1)
25886 /* Generate a copy guide. */
25887 gtype = build_reference_type (type);
25888 else if (!saw_ctor)
25889 /* Generate a default guide. */
25890 gtype = type;
25892 if (gtype)
25894 tree guide = build_deduction_guide (gtype, outer_args, complain);
25895 cands = lookup_add (guide, cands);
25899 if (elided && !cands)
25901 error ("cannot deduce template arguments for copy-initialization"
25902 " of %qT, as it has no non-explicit deduction guides or "
25903 "user-declared constructors", type);
25904 return error_mark_node;
25906 else if (!cands && call == error_mark_node)
25908 error ("cannot deduce template arguments of %qT, as it has no viable "
25909 "deduction guides", type);
25910 return error_mark_node;
25913 if (call == error_mark_node)
25915 ++cp_unevaluated_operand;
25916 call = build_new_function_call (cands, &args, tf_decltype);
25917 --cp_unevaluated_operand;
25920 if (call == error_mark_node && (complain & tf_warning_or_error))
25922 error ("class template argument deduction failed:");
25924 ++cp_unevaluated_operand;
25925 call = build_new_function_call (cands, &args, complain | tf_decltype);
25926 --cp_unevaluated_operand;
25928 if (elided)
25929 inform (input_location, "explicit deduction guides not considered "
25930 "for copy-initialization");
25933 release_tree_vector (args);
25935 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25938 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25939 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25941 tree
25942 do_auto_deduction (tree type, tree init, tree auto_node)
25944 return do_auto_deduction (type, init, auto_node,
25945 tf_warning_or_error,
25946 adc_unspecified);
25949 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25950 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25951 The CONTEXT determines the context in which auto deduction is performed
25952 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25953 OUTER_TARGS are used during template argument deduction
25954 (context == adc_unify) to properly substitute the result, and is ignored
25955 in other contexts.
25957 For partial-concept-ids, extra args may be appended to the list of deduced
25958 template arguments prior to determining constraint satisfaction. */
25960 tree
25961 do_auto_deduction (tree type, tree init, tree auto_node,
25962 tsubst_flags_t complain, auto_deduction_context context,
25963 tree outer_targs, int flags)
25965 tree targs;
25967 if (init == error_mark_node)
25968 return error_mark_node;
25970 if (init && type_dependent_expression_p (init)
25971 && context != adc_unify)
25972 /* Defining a subset of type-dependent expressions that we can deduce
25973 from ahead of time isn't worth the trouble. */
25974 return type;
25976 /* Similarly, we can't deduce from another undeduced decl. */
25977 if (init && undeduced_auto_decl (init))
25978 return type;
25980 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25981 /* C++17 class template argument deduction. */
25982 return do_class_deduction (type, tmpl, init, flags, complain);
25984 if (TREE_TYPE (init) == NULL_TREE)
25985 /* Nothing we can do with this, even in deduction context. */
25986 return type;
25988 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25989 with either a new invented type template parameter U or, if the
25990 initializer is a braced-init-list (8.5.4), with
25991 std::initializer_list<U>. */
25992 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25994 if (!DIRECT_LIST_INIT_P (init))
25995 type = listify_autos (type, auto_node);
25996 else if (CONSTRUCTOR_NELTS (init) == 1)
25997 init = CONSTRUCTOR_ELT (init, 0)->value;
25998 else
26000 if (complain & tf_warning_or_error)
26002 if (permerror (input_location, "direct-list-initialization of "
26003 "%<auto%> requires exactly one element"))
26004 inform (input_location,
26005 "for deduction to %<std::initializer_list%>, use copy-"
26006 "list-initialization (i.e. add %<=%> before the %<{%>)");
26008 type = listify_autos (type, auto_node);
26012 if (type == error_mark_node)
26013 return error_mark_node;
26015 init = resolve_nondeduced_context (init, complain);
26017 if (context == adc_decomp_type
26018 && auto_node == type
26019 && init != error_mark_node
26020 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26021 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26022 and initializer has array type, deduce cv-qualified array type. */
26023 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26024 complain);
26025 else if (AUTO_IS_DECLTYPE (auto_node))
26027 bool id = (DECL_P (init)
26028 || ((TREE_CODE (init) == COMPONENT_REF
26029 || TREE_CODE (init) == SCOPE_REF)
26030 && !REF_PARENTHESIZED_P (init)));
26031 targs = make_tree_vec (1);
26032 TREE_VEC_ELT (targs, 0)
26033 = finish_decltype_type (init, id, tf_warning_or_error);
26034 if (type != auto_node)
26036 if (complain & tf_error)
26037 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26038 return error_mark_node;
26041 else
26043 tree parms = build_tree_list (NULL_TREE, type);
26044 tree tparms;
26046 if (flag_concepts)
26047 tparms = extract_autos (type);
26048 else
26050 tparms = make_tree_vec (1);
26051 TREE_VEC_ELT (tparms, 0)
26052 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26055 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26056 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26057 DEDUCE_CALL, LOOKUP_NORMAL,
26058 NULL, /*explain_p=*/false);
26059 if (val > 0)
26061 if (processing_template_decl)
26062 /* Try again at instantiation time. */
26063 return type;
26064 if (type && type != error_mark_node
26065 && (complain & tf_error))
26066 /* If type is error_mark_node a diagnostic must have been
26067 emitted by now. Also, having a mention to '<type error>'
26068 in the diagnostic is not really useful to the user. */
26070 if (cfun && auto_node == current_function_auto_return_pattern
26071 && LAMBDA_FUNCTION_P (current_function_decl))
26072 error ("unable to deduce lambda return type from %qE", init);
26073 else
26074 error ("unable to deduce %qT from %qE", type, init);
26075 type_unification_real (tparms, targs, parms, &init, 1, 0,
26076 DEDUCE_CALL, LOOKUP_NORMAL,
26077 NULL, /*explain_p=*/true);
26079 return error_mark_node;
26083 /* Check any placeholder constraints against the deduced type. */
26084 if (flag_concepts && !processing_template_decl)
26085 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26087 /* Use the deduced type to check the associated constraints. If we
26088 have a partial-concept-id, rebuild the argument list so that
26089 we check using the extra arguments. */
26090 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26091 tree cargs = CHECK_CONSTR_ARGS (constr);
26092 if (TREE_VEC_LENGTH (cargs) > 1)
26094 cargs = copy_node (cargs);
26095 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26097 else
26098 cargs = targs;
26099 if (!constraints_satisfied_p (constr, cargs))
26101 if (complain & tf_warning_or_error)
26103 switch (context)
26105 case adc_unspecified:
26106 case adc_unify:
26107 error("placeholder constraints not satisfied");
26108 break;
26109 case adc_variable_type:
26110 case adc_decomp_type:
26111 error ("deduced initializer does not satisfy "
26112 "placeholder constraints");
26113 break;
26114 case adc_return_type:
26115 error ("deduced return type does not satisfy "
26116 "placeholder constraints");
26117 break;
26118 case adc_requirement:
26119 error ("deduced expression type does not satisfy "
26120 "placeholder constraints");
26121 break;
26123 diagnose_constraints (input_location, constr, targs);
26125 return error_mark_node;
26129 if (processing_template_decl && context != adc_unify)
26130 outer_targs = current_template_args ();
26131 targs = add_to_template_args (outer_targs, targs);
26132 return tsubst (type, targs, complain, NULL_TREE);
26135 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26136 result. */
26138 tree
26139 splice_late_return_type (tree type, tree late_return_type)
26141 if (is_auto (type))
26143 if (late_return_type)
26144 return late_return_type;
26146 tree idx = get_template_parm_index (type);
26147 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26148 /* In an abbreviated function template we didn't know we were dealing
26149 with a function template when we saw the auto return type, so update
26150 it to have the correct level. */
26151 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26153 return type;
26156 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26157 'decltype(auto)' or a deduced class template. */
26159 bool
26160 is_auto (const_tree type)
26162 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26163 && (TYPE_IDENTIFIER (type) == auto_identifier
26164 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26165 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26166 return true;
26167 else
26168 return false;
26171 /* for_each_template_parm callback for type_uses_auto. */
26174 is_auto_r (tree tp, void */*data*/)
26176 return is_auto (tp);
26179 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26180 a use of `auto'. Returns NULL_TREE otherwise. */
26182 tree
26183 type_uses_auto (tree type)
26185 if (type == NULL_TREE)
26186 return NULL_TREE;
26187 else if (flag_concepts)
26189 /* The Concepts TS allows multiple autos in one type-specifier; just
26190 return the first one we find, do_auto_deduction will collect all of
26191 them. */
26192 if (uses_template_parms (type))
26193 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26194 /*visited*/NULL, /*nondeduced*/true);
26195 else
26196 return NULL_TREE;
26198 else
26199 return find_type_usage (type, is_auto);
26202 /* For a given template T, return the vector of typedefs referenced
26203 in T for which access check is needed at T instantiation time.
26204 T is either a FUNCTION_DECL or a RECORD_TYPE.
26205 Those typedefs were added to T by the function
26206 append_type_to_template_for_access_check. */
26208 vec<qualified_typedef_usage_t, va_gc> *
26209 get_types_needing_access_check (tree t)
26211 tree ti;
26212 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26214 if (!t || t == error_mark_node)
26215 return NULL;
26217 if (!(ti = get_template_info (t)))
26218 return NULL;
26220 if (CLASS_TYPE_P (t)
26221 || TREE_CODE (t) == FUNCTION_DECL)
26223 if (!TI_TEMPLATE (ti))
26224 return NULL;
26226 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26229 return result;
26232 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26233 tied to T. That list of typedefs will be access checked at
26234 T instantiation time.
26235 T is either a FUNCTION_DECL or a RECORD_TYPE.
26236 TYPE_DECL is a TYPE_DECL node representing a typedef.
26237 SCOPE is the scope through which TYPE_DECL is accessed.
26238 LOCATION is the location of the usage point of TYPE_DECL.
26240 This function is a subroutine of
26241 append_type_to_template_for_access_check. */
26243 static void
26244 append_type_to_template_for_access_check_1 (tree t,
26245 tree type_decl,
26246 tree scope,
26247 location_t location)
26249 qualified_typedef_usage_t typedef_usage;
26250 tree ti;
26252 if (!t || t == error_mark_node)
26253 return;
26255 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26256 || CLASS_TYPE_P (t))
26257 && type_decl
26258 && TREE_CODE (type_decl) == TYPE_DECL
26259 && scope);
26261 if (!(ti = get_template_info (t)))
26262 return;
26264 gcc_assert (TI_TEMPLATE (ti));
26266 typedef_usage.typedef_decl = type_decl;
26267 typedef_usage.context = scope;
26268 typedef_usage.locus = location;
26270 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26273 /* Append TYPE_DECL to the template TEMPL.
26274 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26275 At TEMPL instanciation time, TYPE_DECL will be checked to see
26276 if it can be accessed through SCOPE.
26277 LOCATION is the location of the usage point of TYPE_DECL.
26279 e.g. consider the following code snippet:
26281 class C
26283 typedef int myint;
26286 template<class U> struct S
26288 C::myint mi; // <-- usage point of the typedef C::myint
26291 S<char> s;
26293 At S<char> instantiation time, we need to check the access of C::myint
26294 In other words, we need to check the access of the myint typedef through
26295 the C scope. For that purpose, this function will add the myint typedef
26296 and the scope C through which its being accessed to a list of typedefs
26297 tied to the template S. That list will be walked at template instantiation
26298 time and access check performed on each typedefs it contains.
26299 Note that this particular code snippet should yield an error because
26300 myint is private to C. */
26302 void
26303 append_type_to_template_for_access_check (tree templ,
26304 tree type_decl,
26305 tree scope,
26306 location_t location)
26308 qualified_typedef_usage_t *iter;
26309 unsigned i;
26311 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26313 /* Make sure we don't append the type to the template twice. */
26314 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26315 if (iter->typedef_decl == type_decl && scope == iter->context)
26316 return;
26318 append_type_to_template_for_access_check_1 (templ, type_decl,
26319 scope, location);
26322 /* Convert the generic type parameters in PARM that match the types given in the
26323 range [START_IDX, END_IDX) from the current_template_parms into generic type
26324 packs. */
26326 tree
26327 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26329 tree current = current_template_parms;
26330 int depth = TMPL_PARMS_DEPTH (current);
26331 current = INNERMOST_TEMPLATE_PARMS (current);
26332 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26334 for (int i = 0; i < start_idx; ++i)
26335 TREE_VEC_ELT (replacement, i)
26336 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26338 for (int i = start_idx; i < end_idx; ++i)
26340 /* Create a distinct parameter pack type from the current parm and add it
26341 to the replacement args to tsubst below into the generic function
26342 parameter. */
26344 tree o = TREE_TYPE (TREE_VALUE
26345 (TREE_VEC_ELT (current, i)));
26346 tree t = copy_type (o);
26347 TEMPLATE_TYPE_PARM_INDEX (t)
26348 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26349 o, 0, 0, tf_none);
26350 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26351 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26352 TYPE_MAIN_VARIANT (t) = t;
26353 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26354 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26355 TREE_VEC_ELT (replacement, i) = t;
26356 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26359 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26360 TREE_VEC_ELT (replacement, i)
26361 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26363 /* If there are more levels then build up the replacement with the outer
26364 template parms. */
26365 if (depth > 1)
26366 replacement = add_to_template_args (template_parms_to_args
26367 (TREE_CHAIN (current_template_parms)),
26368 replacement);
26370 return tsubst (parm, replacement, tf_none, NULL_TREE);
26373 /* Entries in the decl_constraint hash table. */
26374 struct GTY((for_user)) constr_entry
26376 tree decl;
26377 tree ci;
26380 /* Hashing function and equality for constraint entries. */
26381 struct constr_hasher : ggc_ptr_hash<constr_entry>
26383 static hashval_t hash (constr_entry *e)
26385 return (hashval_t)DECL_UID (e->decl);
26388 static bool equal (constr_entry *e1, constr_entry *e2)
26390 return e1->decl == e2->decl;
26394 /* A mapping from declarations to constraint information. Note that
26395 both templates and their underlying declarations are mapped to the
26396 same constraint information.
26398 FIXME: This is defined in pt.c because garbage collection
26399 code is not being generated for constraint.cc. */
26401 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26403 /* Returns the template constraints of declaration T. If T is not
26404 constrained, return NULL_TREE. Note that T must be non-null. */
26406 tree
26407 get_constraints (tree t)
26409 if (!flag_concepts)
26410 return NULL_TREE;
26412 gcc_assert (DECL_P (t));
26413 if (TREE_CODE (t) == TEMPLATE_DECL)
26414 t = DECL_TEMPLATE_RESULT (t);
26415 constr_entry elt = { t, NULL_TREE };
26416 constr_entry* found = decl_constraints->find (&elt);
26417 if (found)
26418 return found->ci;
26419 else
26420 return NULL_TREE;
26423 /* Associate the given constraint information CI with the declaration
26424 T. If T is a template, then the constraints are associated with
26425 its underlying declaration. Don't build associations if CI is
26426 NULL_TREE. */
26428 void
26429 set_constraints (tree t, tree ci)
26431 if (!ci)
26432 return;
26433 gcc_assert (t && flag_concepts);
26434 if (TREE_CODE (t) == TEMPLATE_DECL)
26435 t = DECL_TEMPLATE_RESULT (t);
26436 gcc_assert (!get_constraints (t));
26437 constr_entry elt = {t, ci};
26438 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26439 constr_entry* entry = ggc_alloc<constr_entry> ();
26440 *entry = elt;
26441 *slot = entry;
26444 /* Remove the associated constraints of the declaration T. */
26446 void
26447 remove_constraints (tree t)
26449 gcc_assert (DECL_P (t));
26450 if (TREE_CODE (t) == TEMPLATE_DECL)
26451 t = DECL_TEMPLATE_RESULT (t);
26453 constr_entry elt = {t, NULL_TREE};
26454 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26455 if (slot)
26456 decl_constraints->clear_slot (slot);
26459 /* Memoized satisfaction results for declarations. This
26460 maps the pair (constraint_info, arguments) to the result computed
26461 by constraints_satisfied_p. */
26463 struct GTY((for_user)) constraint_sat_entry
26465 tree ci;
26466 tree args;
26467 tree result;
26470 /* Hashing function and equality for constraint entries. */
26472 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26474 static hashval_t hash (constraint_sat_entry *e)
26476 hashval_t val = iterative_hash_object(e->ci, 0);
26477 return iterative_hash_template_arg (e->args, val);
26480 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26482 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26486 /* Memoized satisfaction results for concept checks. */
26488 struct GTY((for_user)) concept_spec_entry
26490 tree tmpl;
26491 tree args;
26492 tree result;
26495 /* Hashing function and equality for constraint entries. */
26497 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26499 static hashval_t hash (concept_spec_entry *e)
26501 return hash_tmpl_and_args (e->tmpl, e->args);
26504 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26506 ++comparing_specializations;
26507 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26508 --comparing_specializations;
26509 return eq;
26513 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26514 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26516 /* Search for a memoized satisfaction result. Returns one of the
26517 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26519 tree
26520 lookup_constraint_satisfaction (tree ci, tree args)
26522 constraint_sat_entry elt = { ci, args, NULL_TREE };
26523 constraint_sat_entry* found = constraint_memos->find (&elt);
26524 if (found)
26525 return found->result;
26526 else
26527 return NULL_TREE;
26530 /* Memoize the result of a satisfication test. Returns the saved result. */
26532 tree
26533 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26535 constraint_sat_entry elt = {ci, args, result};
26536 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26537 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26538 *entry = elt;
26539 *slot = entry;
26540 return result;
26543 /* Search for a memoized satisfaction result for a concept. */
26545 tree
26546 lookup_concept_satisfaction (tree tmpl, tree args)
26548 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26549 concept_spec_entry* found = concept_memos->find (&elt);
26550 if (found)
26551 return found->result;
26552 else
26553 return NULL_TREE;
26556 /* Memoize the result of a concept check. Returns the saved result. */
26558 tree
26559 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26561 concept_spec_entry elt = {tmpl, args, result};
26562 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26563 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26564 *entry = elt;
26565 *slot = entry;
26566 return result;
26569 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26571 /* Returns a prior concept specialization. This returns the substituted
26572 and normalized constraints defined by the concept. */
26574 tree
26575 get_concept_expansion (tree tmpl, tree args)
26577 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26578 concept_spec_entry* found = concept_expansions->find (&elt);
26579 if (found)
26580 return found->result;
26581 else
26582 return NULL_TREE;
26585 /* Save a concept expansion for later. */
26587 tree
26588 save_concept_expansion (tree tmpl, tree args, tree def)
26590 concept_spec_entry elt = {tmpl, args, def};
26591 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26592 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26593 *entry = elt;
26594 *slot = entry;
26595 return def;
26598 static hashval_t
26599 hash_subsumption_args (tree t1, tree t2)
26601 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26602 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26603 int val = 0;
26604 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26605 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26606 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26607 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26608 return val;
26611 /* Compare the constraints of two subsumption entries. The LEFT1 and
26612 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26613 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26615 static bool
26616 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26618 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26619 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26620 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26621 CHECK_CONSTR_ARGS (right1)))
26622 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26623 CHECK_CONSTR_ARGS (right2));
26624 return false;
26627 /* Key/value pair for learning and memoizing subsumption results. This
26628 associates a pair of check constraints (including arguments) with
26629 a boolean value indicating the result. */
26631 struct GTY((for_user)) subsumption_entry
26633 tree t1;
26634 tree t2;
26635 bool result;
26638 /* Hashing function and equality for constraint entries. */
26640 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26642 static hashval_t hash (subsumption_entry *e)
26644 return hash_subsumption_args (e->t1, e->t2);
26647 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26649 ++comparing_specializations;
26650 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26651 --comparing_specializations;
26652 return eq;
26656 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26658 /* Search for a previously cached subsumption result. */
26660 bool*
26661 lookup_subsumption_result (tree t1, tree t2)
26663 subsumption_entry elt = { t1, t2, false };
26664 subsumption_entry* found = subsumption_table->find (&elt);
26665 if (found)
26666 return &found->result;
26667 else
26668 return 0;
26671 /* Save a subsumption result. */
26673 bool
26674 save_subsumption_result (tree t1, tree t2, bool result)
26676 subsumption_entry elt = {t1, t2, result};
26677 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26678 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26679 *entry = elt;
26680 *slot = entry;
26681 return result;
26684 /* Set up the hash table for constraint association. */
26686 void
26687 init_constraint_processing (void)
26689 if (!flag_concepts)
26690 return;
26692 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26693 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26694 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26695 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26696 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26699 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26700 0..N-1. */
26702 void
26703 declare_integer_pack (void)
26705 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26706 build_function_type_list (integer_type_node,
26707 integer_type_node,
26708 NULL_TREE),
26709 NULL_TREE, ECF_CONST);
26710 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26711 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26714 /* Set up the hash tables for template instantiations. */
26716 void
26717 init_template_processing (void)
26719 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26720 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26722 if (cxx_dialect >= cxx11)
26723 declare_integer_pack ();
26726 /* Print stats about the template hash tables for -fstats. */
26728 void
26729 print_template_statistics (void)
26731 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26732 "%f collisions\n", (long) decl_specializations->size (),
26733 (long) decl_specializations->elements (),
26734 decl_specializations->collisions ());
26735 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26736 "%f collisions\n", (long) type_specializations->size (),
26737 (long) type_specializations->elements (),
26738 type_specializations->collisions ());
26741 #if CHECKING_P
26743 namespace selftest {
26745 /* Verify that build_non_dependent_expr () works, for various expressions,
26746 and that location wrappers don't affect the results. */
26748 static void
26749 test_build_non_dependent_expr ()
26751 location_t loc = BUILTINS_LOCATION;
26753 /* Verify constants, without and with location wrappers. */
26754 tree int_cst = build_int_cst (integer_type_node, 42);
26755 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26757 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26758 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26759 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26761 tree string_lit = build_string (4, "foo");
26762 TREE_TYPE (string_lit) = char_array_type_node;
26763 string_lit = fix_string_type (string_lit);
26764 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26766 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26767 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26768 ASSERT_EQ (wrapped_string_lit,
26769 build_non_dependent_expr (wrapped_string_lit));
26772 /* Verify that type_dependent_expression_p () works correctly, even
26773 in the presence of location wrapper nodes. */
26775 static void
26776 test_type_dependent_expression_p ()
26778 location_t loc = BUILTINS_LOCATION;
26780 tree name = get_identifier ("foo");
26782 /* If no templates are involved, nothing is type-dependent. */
26783 gcc_assert (!processing_template_decl);
26784 ASSERT_FALSE (type_dependent_expression_p (name));
26786 ++processing_template_decl;
26788 /* Within a template, an unresolved name is always type-dependent. */
26789 ASSERT_TRUE (type_dependent_expression_p (name));
26791 /* Ensure it copes with NULL_TREE and errors. */
26792 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26793 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26795 /* A USING_DECL in a template should be type-dependent, even if wrapped
26796 with a location wrapper (PR c++/83799). */
26797 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26798 TREE_TYPE (using_decl) = integer_type_node;
26799 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26800 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26801 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26802 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26804 --processing_template_decl;
26807 /* Run all of the selftests within this file. */
26809 void
26810 cp_pt_c_tests ()
26812 test_build_non_dependent_expr ();
26813 test_type_dependent_expression_p ();
26816 } // namespace selftest
26818 #endif /* #if CHECKING_P */
26820 #include "gt-cp-pt.h"