Remove obsolete ECOFF support.
[official-gcc.git] / gcc / cp / pt.c
blob710333ddabaca795be672775719bf0889f221f78
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
66 static GTY(()) struct tinst_level *current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
78 // Implementation of the RAII helper for creating new local
79 // specializations.
80 local_specialization_stack::local_specialization_stack (lss_policy policy)
81 : saved (local_specializations)
83 if (policy == lss_blank || !saved)
84 local_specializations = new hash_map<tree, tree>;
85 else
86 local_specializations = new hash_map<tree, tree>(*saved);
89 local_specialization_stack::~local_specialization_stack ()
91 delete local_specializations;
92 local_specializations = saved;
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth;
98 struct GTY((for_user)) spec_entry
100 tree tmpl;
101 tree args;
102 tree spec;
105 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 static hashval_t hash (spec_entry *);
108 static bool equal (spec_entry *, spec_entry *);
111 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113 static GTY (()) hash_table<spec_hasher> *type_specializations;
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130 enum template_base_result {
131 tbr_incomplete_type,
132 tbr_ambiguous_baseclass,
133 tbr_success
136 static void push_access_scope (tree);
137 static void pop_access_scope (tree);
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static tree add_outermost_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
157 tree);
158 static int type_unification_real (tree, tree, tree, const tree *,
159 unsigned int, int, unification_kind_t, int,
160 vec<deferred_access_check, va_gc> **,
161 bool);
162 static void note_template_header (int);
163 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
164 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
165 static tree convert_template_argument (tree, tree, tree,
166 tsubst_flags_t, int, tree);
167 static tree for_each_template_parm (tree, tree_fn_t, void*,
168 hash_set<tree> *, bool, tree_fn_t = NULL);
169 static tree expand_template_argument_pack (tree);
170 static tree build_template_parm_index (int, int, int, tree, tree);
171 static bool inline_needs_template_parms (tree, bool);
172 static void push_inline_template_parms_recursive (tree, int);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static void tsubst_default_arguments (tree, tsubst_flags_t);
203 static tree for_each_template_parm_r (tree *, int *, void *);
204 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
205 static void copy_default_args_to_explicit_spec (tree);
206 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
207 static bool dependent_template_arg_p (tree);
208 static bool any_template_arguments_need_structural_equality_p (tree);
209 static bool dependent_type_p_r (tree);
210 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_decl (tree, tree, tsubst_flags_t);
212 static void perform_typedefs_access_check (tree tmpl, tree targs);
213 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
214 location_t);
215 static tree listify (tree);
216 static tree listify_autos (tree, tree);
217 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
218 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 static bool complex_alias_template_p (const_tree tmpl);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
230 static void
231 push_access_scope (tree t)
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
243 if (TREE_CODE (t) == FUNCTION_DECL)
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
254 static void
255 pop_access_scope (tree t)
257 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
274 tree
275 finish_member_template_decl (tree decl)
277 if (decl == error_mark_node)
278 return error_mark_node;
280 gcc_assert (DECL_P (decl));
282 if (TREE_CODE (decl) == TYPE_DECL)
284 tree type;
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
297 return NULL_TREE;
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
308 else
309 return decl;
311 else
312 error ("invalid member template declaration %qD", decl);
314 return error_mark_node;
317 /* Create a template info node. */
319 tree
320 build_template_info (tree template_decl, tree template_args)
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
328 /* Return the template info node corresponding to T, whatever T is. */
330 tree
331 get_template_info (const_tree t)
333 tree tinfo = NULL_TREE;
335 if (!t || t == error_mark_node)
336 return NULL;
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
353 return tinfo;
356 /* Returns the template nesting level of the indicated class TYPE.
358 For example, in:
359 template <class T>
360 struct A
362 template <class U>
363 struct B {};
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
375 template_class_depth (tree type)
377 int depth;
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
381 tree tinfo = get_template_info (type);
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
387 if (DECL_P (type))
388 type = CP_DECL_CONTEXT (type);
389 else if (LAMBDA_TYPE_P (type))
390 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
391 else
392 type = CP_TYPE_CONTEXT (type);
395 return depth;
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
401 static bool
402 inline_needs_template_parms (tree decl, bool nsdmi)
404 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
405 return false;
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
408 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
414 innermost first. */
416 static void
417 push_inline_template_parms_recursive (tree parmlist, int levels)
419 tree parms = TREE_VALUE (parmlist);
420 int i;
422 if (levels > 1)
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
425 ++processing_template_decl;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl),
428 parms, current_template_parms);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
431 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
432 NULL);
433 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
435 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
437 if (error_operand_p (parm))
438 continue;
440 gcc_assert (DECL_P (parm));
442 switch (TREE_CODE (parm))
444 case TYPE_DECL:
445 case TEMPLATE_DECL:
446 pushdecl (parm);
447 break;
449 case PARM_DECL:
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
452 break;
454 default:
455 gcc_unreachable ();
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
464 void
465 maybe_begin_member_template_processing (tree decl)
467 tree parms;
468 int levels = 0;
469 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471 if (nsdmi)
473 tree ctx = DECL_CONTEXT (decl);
474 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx)
477 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 if (inline_needs_template_parms (decl, nsdmi))
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 --levels;
488 parms = TREE_CHAIN (parms);
491 push_inline_template_parms_recursive (parms, levels);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
499 /* Undo the effects of maybe_begin_member_template_processing. */
501 void
502 maybe_end_member_template_processing (void)
504 int i;
505 int last;
507 if (inline_parm_levels.length () == 0)
508 return;
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
522 static tree
523 add_to_template_args (tree args, tree extra_args)
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542 return new_args;
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
555 tree new_args;
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569 new_args = add_to_template_args (args, extra_args);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574 return new_args;
577 /* Return the N levels of innermost template arguments from the ARGS. */
579 tree
580 get_innermost_template_args (tree args, int n)
582 tree new_args;
583 int extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
605 return new_args;
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
618 gcc_assert (n >= 0);
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
636 return new_args;
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
642 void
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl),
667 make_tree_vec (0),
668 current_template_parms);
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
675 static bool
676 check_specialization_scope (void)
678 tree scope = current_scope ();
680 /* [temp.expl.spec]
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
688 is a member. */
689 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
691 error ("explicit specialization in non-namespace scope %qD", scope);
692 return false;
695 /* [temp.expl.spec]
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms)
705 error ("enclosing class templates are not explicitly specialized");
706 return false;
709 return true;
712 /* We've just seen template <>. */
714 bool
715 begin_specialization (void)
717 begin_scope (sk_template_spec, NULL);
718 note_template_header (1);
719 return check_specialization_scope ();
722 /* Called at then end of processing a declaration preceded by
723 template<>. */
725 void
726 end_specialization (void)
728 finish_scope ();
729 reset_specialization ();
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
735 void
736 reset_specialization (void)
738 processing_specialization = 0;
739 template_header_count = 0;
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
745 static void
746 note_template_header (int specialization)
748 processing_specialization = specialization;
749 template_header_count++;
752 /* We're beginning an explicit instantiation. */
754 void
755 begin_explicit_instantiation (void)
757 gcc_assert (!processing_explicit_instantiation);
758 processing_explicit_instantiation = true;
762 void
763 end_explicit_instantiation (void)
765 gcc_assert (processing_explicit_instantiation);
766 processing_explicit_instantiation = false;
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
774 static bool
775 check_specialization_namespace (tree tmpl)
777 tree tpl_ns = decl_namespace_context (tmpl);
779 /* [tmpl.expl.spec]
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
793 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
794 /* Same or enclosing namespace. */
795 return true;
796 else
798 permerror (input_location,
799 "specialization of %qD in different namespace", tmpl);
800 inform (DECL_SOURCE_LOCATION (tmpl),
801 " from definition of %q#D", tmpl);
802 return false;
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
809 static void
810 check_explicit_instantiation_namespace (tree spec)
812 tree ns;
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns = decl_namespace_context (spec);
817 if (!is_nested_namespace (current_namespace, ns))
818 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec, current_namespace, ns);
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
826 static tree
827 maybe_new_partial_specialization (tree type)
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
832 // template<typename T>
833 // struct S;
835 // template<typename T>
836 // struct S<T*>;
838 // Here, S<T*> is an implicit instantiation of S whose type
839 // is incomplete.
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
841 return type;
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
846 // template<typename T>
847 // requires Integral<T>
848 // struct S<T*>;
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
861 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 tree args = CLASSTYPE_TI_ARGS (type);
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms)
867 return NULL_TREE;
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
871 return NULL_TREE;
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr = current_template_constraints ();
877 if (type == TREE_TYPE (tmpl))
879 tree main_constr = get_constraints (tmpl);
880 if (equivalent_constraints (type_constr, main_constr))
881 return NULL_TREE;
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
887 while (specs)
889 tree spec_tmpl = TREE_VALUE (specs);
890 tree spec_args = TREE_PURPOSE (specs);
891 tree spec_constr = get_constraints (spec_tmpl);
892 if (comp_template_args (args, spec_args)
893 && equivalent_constraints (type_constr, spec_constr))
894 return NULL_TREE;
895 specs = TREE_CHAIN (specs);
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t = make_class_type (TREE_CODE (type));
901 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
902 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
909 // Build the corresponding type decl.
910 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
911 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
912 DECL_SOURCE_LOCATION (d) = input_location;
914 return t;
917 return NULL_TREE;
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
923 tree
924 maybe_process_partial_specialization (tree type)
926 tree context;
928 if (type == error_mark_node)
929 return error_mark_node;
931 /* A lambda that appears in specialization context is not itself a
932 specialization. */
933 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
934 return type;
936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
938 error ("name of class shadows template template parameter %qD",
939 TYPE_NAME (type));
940 return error_mark_node;
943 context = TYPE_CONTEXT (type);
945 if (TYPE_ALIAS_P (type))
947 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
949 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo));
952 else
953 error ("explicit specialization of non-template %qT", type);
954 return error_mark_node;
956 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
961 template <> class C<int>;
965 template <class T> class C<T*>;
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969 if (tree t = maybe_new_partial_specialization (type))
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
972 && !at_namespace_scope_p ())
973 return error_mark_node;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
976 if (processing_template_decl)
978 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
979 if (decl == error_mark_node)
980 return error_mark_node;
981 return TREE_TYPE (decl);
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
985 error ("specialization of %qT after instantiation", type);
986 else if (errorcount && !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node;
994 else if (CLASS_TYPE_P (type)
995 && !CLASSTYPE_USE_TEMPLATE (type)
996 && CLASSTYPE_TEMPLATE_INFO (type)
997 && context && CLASS_TYPE_P (context)
998 && CLASSTYPE_TEMPLATE_INFO (context))
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1003 template <> template <class U> class C<int>::D;
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1007 earlier like:
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1017 && !COMPLETE_TYPE_P (type))
1019 tree t;
1020 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1022 if (current_namespace
1023 != decl_namespace_context (tmpl))
1025 permerror (input_location,
1026 "specializing %q#T in different namespace", type);
1027 permerror (DECL_SOURCE_LOCATION (tmpl),
1028 " from definition of %q#D", tmpl);
1031 /* Check for invalid specialization after instantiation:
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1036 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1037 t; t = TREE_CHAIN (t))
1039 tree inst = TREE_VALUE (t);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1047 spec_entry elt;
1048 spec_entry *entry;
1050 elt.tmpl = most_general_template (tmpl);
1051 elt.args = CLASSTYPE_TI_ARGS (inst);
1052 elt.spec = inst;
1054 type_specializations->remove_elt (&elt);
1056 elt.tmpl = tmpl;
1057 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1059 spec_entry **slot
1060 = type_specializations->find_slot (&elt, INSERT);
1061 entry = ggc_alloc<spec_entry> ();
1062 *entry = elt;
1063 *slot = entry;
1065 else
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1069 type, inst);
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1074 class template. */
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1077 CLASSTYPE_TI_ARGS (type)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 else if (processing_specialization)
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1086 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1087 "of %qD not allowed by ISO C++", type);
1088 else
1090 error ("explicit specialization of non-template %qT", type);
1091 return error_mark_node;
1095 return type;
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1102 static inline bool
1103 optimize_specialization_lookup_p (tree tmpl)
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1106 && DECL_CLASS_SCOPE_P (tmpl)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1108 parameter. */
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1114 specialization. */
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1117 && !DECL_CONV_FN_P (tmpl)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1121 template <typename T>
1122 struct S { friend A::f(); };
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1134 static void
1135 verify_unstripped_args (tree args)
1137 ++processing_template_decl;
1138 if (!any_dependent_template_arguments_p (args))
1140 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1141 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 tree arg = TREE_VEC_ELT (inner, i);
1144 if (TREE_CODE (arg) == TEMPLATE_DECL)
1145 /* OK */;
1146 else if (TYPE_P (arg))
1147 gcc_assert (strip_typedefs (arg, NULL) == arg);
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 --processing_template_decl;
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1172 static tree
1173 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1175 if (tmpl == NULL_TREE)
1176 return NULL_TREE;
1178 if (args == error_mark_node)
1179 return NULL_TREE;
1181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl) == FIELD_DECL);
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args)
1187 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1189 : template_class_depth (DECL_CONTEXT (tmpl))));
1191 if (flag_checking)
1192 verify_unstripped_args (args);
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (lambda_fn_in_template_p (tmpl))
1197 return NULL_TREE;
1199 if (optimize_specialization_lookup_p (tmpl))
1201 /* The template arguments actually apply to the containing
1202 class. Find the class specialization with those
1203 arguments. */
1204 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1205 tree class_specialization
1206 = retrieve_specialization (class_template, args, 0);
1207 if (!class_specialization)
1208 return NULL_TREE;
1210 /* Find the instance of TMPL. */
1211 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1212 for (ovl_iterator iter (fns); iter; ++iter)
1214 tree fn = *iter;
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1221 return NULL_TREE;
1223 else
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1245 return NULL_TREE;
1248 /* Like retrieve_specialization, but for local declarations. */
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263 is_specialization_of (tree decl, tree tmpl)
1265 tree t;
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1275 else
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1287 return 0;
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1296 bool need_template = true;
1297 int template_depth;
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1330 For example, given a template friend declaration
1332 template <class T> friend void A<T>::f();
1334 the member function below is considered a friend
1336 template <> struct A<int> {
1337 void f();
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1356 and DECL's are
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1370 while (current_depth < template_depth)
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1380 context = TYPE_CONTEXT (context);
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1410 else
1411 decl_type = TREE_TYPE (decl);
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1431 return compparms (decl_args_type, friend_args_type);
1433 else
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1469 return false;
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1501 template <class T> void f(T t1, T t = T())
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1546 else
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1593 check_specialization_namespace (tmpl);
1595 return fn;
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1600 tree dd = duplicate_decls (spec, fn, is_friend);
1601 if (dd == error_mark_node)
1602 /* We've already complained in duplicate_decls. */
1603 return error_mark_node;
1605 if (dd == NULL_TREE && DECL_INITIAL (spec))
1606 /* Dup decl failed, but this is a new definition. Set the
1607 line number so any errors match this new
1608 definition. */
1609 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1611 return fn;
1614 else if (fn)
1615 return duplicate_decls (spec, fn, is_friend);
1617 /* A specialization must be declared in the same namespace as the
1618 template it is specializing. */
1619 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1620 && !check_specialization_namespace (tmpl))
1621 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1623 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1625 spec_entry *entry = ggc_alloc<spec_entry> ();
1626 gcc_assert (tmpl && args && spec);
1627 *entry = elt;
1628 *slot = entry;
1629 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1630 && PRIMARY_TEMPLATE_P (tmpl)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1632 || variable_template_p (tmpl))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1641 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 return spec;
1647 /* Returns true iff two spec_entry nodes are equivalent. */
1649 int comparing_specializations;
1651 bool
1652 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1654 int equal;
1656 ++comparing_specializations;
1657 equal = (e1->tmpl == e2->tmpl
1658 && comp_template_args (e1->args, e2->args));
1659 if (equal && flag_concepts
1660 /* tmpl could be a FIELD_DECL for a capture pack. */
1661 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1662 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1663 && uses_template_parms (e1->args))
1665 /* Partial specializations of a variable template can be distinguished by
1666 constraints. */
1667 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1668 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1669 equal = equivalent_constraints (c1, c2);
1671 --comparing_specializations;
1673 return equal;
1676 /* Returns a hash for a template TMPL and template arguments ARGS. */
1678 static hashval_t
1679 hash_tmpl_and_args (tree tmpl, tree args)
1681 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1682 return iterative_hash_template_arg (args, val);
1685 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1686 ignoring SPEC. */
1688 hashval_t
1689 spec_hasher::hash (spec_entry *e)
1691 return hash_tmpl_and_args (e->tmpl, e->args);
1694 /* Recursively calculate a hash value for a template argument ARG, for use
1695 in the hash tables of template specializations. */
1697 hashval_t
1698 iterative_hash_template_arg (tree arg, hashval_t val)
1700 unsigned HOST_WIDE_INT i;
1701 enum tree_code code;
1702 char tclass;
1704 if (arg == NULL_TREE)
1705 return iterative_hash_object (arg, val);
1707 if (!TYPE_P (arg))
1708 STRIP_NOPS (arg);
1710 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1711 gcc_unreachable ();
1713 code = TREE_CODE (arg);
1714 tclass = TREE_CODE_CLASS (code);
1716 val = iterative_hash_object (code, val);
1718 switch (code)
1720 case ERROR_MARK:
1721 return val;
1723 case IDENTIFIER_NODE:
1724 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1726 case TREE_VEC:
1728 int i, len = TREE_VEC_LENGTH (arg);
1729 for (i = 0; i < len; ++i)
1730 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1731 return val;
1734 case TYPE_PACK_EXPANSION:
1735 case EXPR_PACK_EXPANSION:
1736 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1737 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1739 case TYPE_ARGUMENT_PACK:
1740 case NONTYPE_ARGUMENT_PACK:
1741 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1743 case TREE_LIST:
1744 for (; arg; arg = TREE_CHAIN (arg))
1745 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1746 return val;
1748 case OVERLOAD:
1749 for (lkp_iterator iter (arg); iter; ++iter)
1750 val = iterative_hash_template_arg (*iter, val);
1751 return val;
1753 case CONSTRUCTOR:
1755 tree field, value;
1756 iterative_hash_template_arg (TREE_TYPE (arg), val);
1757 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1759 val = iterative_hash_template_arg (field, val);
1760 val = iterative_hash_template_arg (value, val);
1762 return val;
1765 case PARM_DECL:
1766 if (!DECL_ARTIFICIAL (arg))
1768 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1769 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1771 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1773 case TARGET_EXPR:
1774 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1776 case PTRMEM_CST:
1777 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1778 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1780 case TEMPLATE_PARM_INDEX:
1781 val = iterative_hash_template_arg
1782 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1783 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1784 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1786 case TRAIT_EXPR:
1787 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1788 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1789 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1791 case BASELINK:
1792 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1793 val);
1794 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1795 val);
1797 case MODOP_EXPR:
1798 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1799 code = TREE_CODE (TREE_OPERAND (arg, 1));
1800 val = iterative_hash_object (code, val);
1801 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1803 case LAMBDA_EXPR:
1804 /* A lambda can't appear in a template arg, but don't crash on
1805 erroneous input. */
1806 gcc_assert (seen_error ());
1807 return val;
1809 case CAST_EXPR:
1810 case IMPLICIT_CONV_EXPR:
1811 case STATIC_CAST_EXPR:
1812 case REINTERPRET_CAST_EXPR:
1813 case CONST_CAST_EXPR:
1814 case DYNAMIC_CAST_EXPR:
1815 case NEW_EXPR:
1816 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1817 /* Now hash operands as usual. */
1818 break;
1820 default:
1821 break;
1824 switch (tclass)
1826 case tcc_type:
1827 if (alias_template_specialization_p (arg))
1829 // We want an alias specialization that survived strip_typedefs
1830 // to hash differently from its TYPE_CANONICAL, to avoid hash
1831 // collisions that compare as different in template_args_equal.
1832 // These could be dependent specializations that strip_typedefs
1833 // left alone, or untouched specializations because
1834 // coerce_template_parms returns the unconverted template
1835 // arguments if it sees incomplete argument packs.
1836 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1837 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1839 if (TYPE_CANONICAL (arg))
1840 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1841 val);
1842 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1843 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1844 /* Otherwise just compare the types during lookup. */
1845 return val;
1847 case tcc_declaration:
1848 case tcc_constant:
1849 return iterative_hash_expr (arg, val);
1851 default:
1852 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1854 unsigned n = cp_tree_operand_length (arg);
1855 for (i = 0; i < n; ++i)
1856 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1857 return val;
1860 gcc_unreachable ();
1861 return 0;
1864 /* Unregister the specialization SPEC as a specialization of TMPL.
1865 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1866 if the SPEC was listed as a specialization of TMPL.
1868 Note that SPEC has been ggc_freed, so we can't look inside it. */
1870 bool
1871 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1873 spec_entry *entry;
1874 spec_entry elt;
1876 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1877 elt.args = TI_ARGS (tinfo);
1878 elt.spec = NULL_TREE;
1880 entry = decl_specializations->find (&elt);
1881 if (entry != NULL)
1883 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1884 gcc_assert (new_spec != NULL_TREE);
1885 entry->spec = new_spec;
1886 return 1;
1889 return 0;
1892 /* Like register_specialization, but for local declarations. We are
1893 registering SPEC, an instantiation of TMPL. */
1895 void
1896 register_local_specialization (tree spec, tree tmpl)
1898 gcc_assert (tmpl != spec);
1899 local_specializations->put (tmpl, spec);
1902 /* TYPE is a class type. Returns true if TYPE is an explicitly
1903 specialized class. */
1905 bool
1906 explicit_class_specialization_p (tree type)
1908 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1909 return false;
1910 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1913 /* Print the list of functions at FNS, going through all the overloads
1914 for each element of the list. Alternatively, FNS can not be a
1915 TREE_LIST, in which case it will be printed together with all the
1916 overloads.
1918 MORE and *STR should respectively be FALSE and NULL when the function
1919 is called from the outside. They are used internally on recursive
1920 calls. print_candidates manages the two parameters and leaves NULL
1921 in *STR when it ends. */
1923 static void
1924 print_candidates_1 (tree fns, char **str, bool more = false)
1926 if (TREE_CODE (fns) == TREE_LIST)
1927 for (; fns; fns = TREE_CHAIN (fns))
1928 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1929 else
1930 for (lkp_iterator iter (fns); iter;)
1932 tree cand = *iter;
1933 ++iter;
1935 const char *pfx = *str;
1936 if (!pfx)
1938 if (more || iter)
1939 pfx = _("candidates are:");
1940 else
1941 pfx = _("candidate is:");
1942 *str = get_spaces (pfx);
1944 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1948 /* Print the list of candidate FNS in an error message. FNS can also
1949 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1951 void
1952 print_candidates (tree fns)
1954 char *str = NULL;
1955 print_candidates_1 (fns, &str);
1956 free (str);
1959 /* Get a (possibly) constrained template declaration for the
1960 purpose of ordering candidates. */
1961 static tree
1962 get_template_for_ordering (tree list)
1964 gcc_assert (TREE_CODE (list) == TREE_LIST);
1965 tree f = TREE_VALUE (list);
1966 if (tree ti = DECL_TEMPLATE_INFO (f))
1967 return TI_TEMPLATE (ti);
1968 return f;
1971 /* Among candidates having the same signature, return the
1972 most constrained or NULL_TREE if there is no best candidate.
1973 If the signatures of candidates vary (e.g., template
1974 specialization vs. member function), then there can be no
1975 most constrained.
1977 Note that we don't compare constraints on the functions
1978 themselves, but rather those of their templates. */
1979 static tree
1980 most_constrained_function (tree candidates)
1982 // Try to find the best candidate in a first pass.
1983 tree champ = candidates;
1984 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1986 int winner = more_constrained (get_template_for_ordering (champ),
1987 get_template_for_ordering (c));
1988 if (winner == -1)
1989 champ = c; // The candidate is more constrained
1990 else if (winner == 0)
1991 return NULL_TREE; // Neither is more constrained
1994 // Verify that the champ is better than previous candidates.
1995 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1996 if (!more_constrained (get_template_for_ordering (champ),
1997 get_template_for_ordering (c)))
1998 return NULL_TREE;
2001 return champ;
2005 /* Returns the template (one of the functions given by TEMPLATE_ID)
2006 which can be specialized to match the indicated DECL with the
2007 explicit template args given in TEMPLATE_ID. The DECL may be
2008 NULL_TREE if none is available. In that case, the functions in
2009 TEMPLATE_ID are non-members.
2011 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2012 specialization of a member template.
2014 The TEMPLATE_COUNT is the number of references to qualifying
2015 template classes that appeared in the name of the function. See
2016 check_explicit_specialization for a more accurate description.
2018 TSK indicates what kind of template declaration (if any) is being
2019 declared. TSK_TEMPLATE indicates that the declaration given by
2020 DECL, though a FUNCTION_DECL, has template parameters, and is
2021 therefore a template function.
2023 The template args (those explicitly specified and those deduced)
2024 are output in a newly created vector *TARGS_OUT.
2026 If it is impossible to determine the result, an error message is
2027 issued. The error_mark_node is returned to indicate failure. */
2029 static tree
2030 determine_specialization (tree template_id,
2031 tree decl,
2032 tree* targs_out,
2033 int need_member_template,
2034 int template_count,
2035 tmpl_spec_kind tsk)
2037 tree fns;
2038 tree targs;
2039 tree explicit_targs;
2040 tree candidates = NULL_TREE;
2042 /* A TREE_LIST of templates of which DECL may be a specialization.
2043 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2044 corresponding TREE_PURPOSE is the set of template arguments that,
2045 when used to instantiate the template, would produce a function
2046 with the signature of DECL. */
2047 tree templates = NULL_TREE;
2048 int header_count;
2049 cp_binding_level *b;
2051 *targs_out = NULL_TREE;
2053 if (template_id == error_mark_node || decl == error_mark_node)
2054 return error_mark_node;
2056 /* We shouldn't be specializing a member template of an
2057 unspecialized class template; we already gave an error in
2058 check_specialization_scope, now avoid crashing. */
2059 if (template_count && DECL_CLASS_SCOPE_P (decl)
2060 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2062 gcc_assert (errorcount);
2063 return error_mark_node;
2066 fns = TREE_OPERAND (template_id, 0);
2067 explicit_targs = TREE_OPERAND (template_id, 1);
2069 if (fns == error_mark_node)
2070 return error_mark_node;
2072 /* Check for baselinks. */
2073 if (BASELINK_P (fns))
2074 fns = BASELINK_FUNCTIONS (fns);
2076 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2078 error ("%qD is not a function template", fns);
2079 return error_mark_node;
2081 else if (VAR_P (decl) && !variable_template_p (fns))
2083 error ("%qD is not a variable template", fns);
2084 return error_mark_node;
2087 /* Count the number of template headers specified for this
2088 specialization. */
2089 header_count = 0;
2090 for (b = current_binding_level;
2091 b->kind == sk_template_parms;
2092 b = b->level_chain)
2093 ++header_count;
2095 tree orig_fns = fns;
2097 if (variable_template_p (fns))
2099 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2100 targs = coerce_template_parms (parms, explicit_targs, fns,
2101 tf_warning_or_error,
2102 /*req_all*/true, /*use_defarg*/true);
2103 if (targs != error_mark_node)
2104 templates = tree_cons (targs, fns, templates);
2106 else for (lkp_iterator iter (fns); iter; ++iter)
2108 tree fn = *iter;
2110 if (TREE_CODE (fn) == TEMPLATE_DECL)
2112 tree decl_arg_types;
2113 tree fn_arg_types;
2114 tree insttype;
2116 /* In case of explicit specialization, we need to check if
2117 the number of template headers appearing in the specialization
2118 is correct. This is usually done in check_explicit_specialization,
2119 but the check done there cannot be exhaustive when specializing
2120 member functions. Consider the following code:
2122 template <> void A<int>::f(int);
2123 template <> template <> void A<int>::f(int);
2125 Assuming that A<int> is not itself an explicit specialization
2126 already, the first line specializes "f" which is a non-template
2127 member function, whilst the second line specializes "f" which
2128 is a template member function. So both lines are syntactically
2129 correct, and check_explicit_specialization does not reject
2130 them.
2132 Here, we can do better, as we are matching the specialization
2133 against the declarations. We count the number of template
2134 headers, and we check if they match TEMPLATE_COUNT + 1
2135 (TEMPLATE_COUNT is the number of qualifying template classes,
2136 plus there must be another header for the member template
2137 itself).
2139 Notice that if header_count is zero, this is not a
2140 specialization but rather a template instantiation, so there
2141 is no check we can perform here. */
2142 if (header_count && header_count != template_count + 1)
2143 continue;
2145 /* Check that the number of template arguments at the
2146 innermost level for DECL is the same as for FN. */
2147 if (current_binding_level->kind == sk_template_parms
2148 && !current_binding_level->explicit_spec_p
2149 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2150 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2151 (current_template_parms))))
2152 continue;
2154 /* DECL might be a specialization of FN. */
2155 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2156 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2158 /* For a non-static member function, we need to make sure
2159 that the const qualification is the same. Since
2160 get_bindings does not try to merge the "this" parameter,
2161 we must do the comparison explicitly. */
2162 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2163 && !same_type_p (TREE_VALUE (fn_arg_types),
2164 TREE_VALUE (decl_arg_types)))
2165 continue;
2167 /* Skip the "this" parameter and, for constructors of
2168 classes with virtual bases, the VTT parameter. A
2169 full specialization of a constructor will have a VTT
2170 parameter, but a template never will. */
2171 decl_arg_types
2172 = skip_artificial_parms_for (decl, decl_arg_types);
2173 fn_arg_types
2174 = skip_artificial_parms_for (fn, fn_arg_types);
2176 /* Function templates cannot be specializations; there are
2177 no partial specializations of functions. Therefore, if
2178 the type of DECL does not match FN, there is no
2179 match.
2181 Note that it should never be the case that we have both
2182 candidates added here, and for regular member functions
2183 below. */
2184 if (tsk == tsk_template)
2186 if (compparms (fn_arg_types, decl_arg_types))
2187 candidates = tree_cons (NULL_TREE, fn, candidates);
2188 continue;
2191 /* See whether this function might be a specialization of this
2192 template. Suppress access control because we might be trying
2193 to make this specialization a friend, and we have already done
2194 access control for the declaration of the specialization. */
2195 push_deferring_access_checks (dk_no_check);
2196 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2197 pop_deferring_access_checks ();
2199 if (!targs)
2200 /* We cannot deduce template arguments that when used to
2201 specialize TMPL will produce DECL. */
2202 continue;
2204 /* Remove, from the set of candidates, all those functions
2205 whose constraints are not satisfied. */
2206 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2207 continue;
2209 // Then, try to form the new function type.
2210 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2211 if (insttype == error_mark_node)
2212 continue;
2213 fn_arg_types
2214 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2215 if (!compparms (fn_arg_types, decl_arg_types))
2216 continue;
2218 /* Save this template, and the arguments deduced. */
2219 templates = tree_cons (targs, fn, templates);
2221 else if (need_member_template)
2222 /* FN is an ordinary member function, and we need a
2223 specialization of a member template. */
2225 else if (TREE_CODE (fn) != FUNCTION_DECL)
2226 /* We can get IDENTIFIER_NODEs here in certain erroneous
2227 cases. */
2229 else if (!DECL_FUNCTION_MEMBER_P (fn))
2230 /* This is just an ordinary non-member function. Nothing can
2231 be a specialization of that. */
2233 else if (DECL_ARTIFICIAL (fn))
2234 /* Cannot specialize functions that are created implicitly. */
2236 else
2238 tree decl_arg_types;
2240 /* This is an ordinary member function. However, since
2241 we're here, we can assume its enclosing class is a
2242 template class. For example,
2244 template <typename T> struct S { void f(); };
2245 template <> void S<int>::f() {}
2247 Here, S<int>::f is a non-template, but S<int> is a
2248 template class. If FN has the same type as DECL, we
2249 might be in business. */
2251 if (!DECL_TEMPLATE_INFO (fn))
2252 /* Its enclosing class is an explicit specialization
2253 of a template class. This is not a candidate. */
2254 continue;
2256 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2257 TREE_TYPE (TREE_TYPE (fn))))
2258 /* The return types differ. */
2259 continue;
2261 /* Adjust the type of DECL in case FN is a static member. */
2262 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2263 if (DECL_STATIC_FUNCTION_P (fn)
2264 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2265 decl_arg_types = TREE_CHAIN (decl_arg_types);
2267 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2268 decl_arg_types))
2269 continue;
2271 // If the deduced arguments do not satisfy the constraints,
2272 // this is not a candidate.
2273 if (flag_concepts && !constraints_satisfied_p (fn))
2274 continue;
2276 // Add the candidate.
2277 candidates = tree_cons (NULL_TREE, fn, candidates);
2281 if (templates && TREE_CHAIN (templates))
2283 /* We have:
2285 [temp.expl.spec]
2287 It is possible for a specialization with a given function
2288 signature to be instantiated from more than one function
2289 template. In such cases, explicit specification of the
2290 template arguments must be used to uniquely identify the
2291 function template specialization being specialized.
2293 Note that here, there's no suggestion that we're supposed to
2294 determine which of the candidate templates is most
2295 specialized. However, we, also have:
2297 [temp.func.order]
2299 Partial ordering of overloaded function template
2300 declarations is used in the following contexts to select
2301 the function template to which a function template
2302 specialization refers:
2304 -- when an explicit specialization refers to a function
2305 template.
2307 So, we do use the partial ordering rules, at least for now.
2308 This extension can only serve to make invalid programs valid,
2309 so it's safe. And, there is strong anecdotal evidence that
2310 the committee intended the partial ordering rules to apply;
2311 the EDG front end has that behavior, and John Spicer claims
2312 that the committee simply forgot to delete the wording in
2313 [temp.expl.spec]. */
2314 tree tmpl = most_specialized_instantiation (templates);
2315 if (tmpl != error_mark_node)
2317 templates = tmpl;
2318 TREE_CHAIN (templates) = NULL_TREE;
2322 // Concepts allows multiple declarations of member functions
2323 // with the same signature. Like above, we need to rely on
2324 // on the partial ordering of those candidates to determine which
2325 // is the best.
2326 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2328 if (tree cand = most_constrained_function (candidates))
2330 candidates = cand;
2331 TREE_CHAIN (cand) = NULL_TREE;
2335 if (templates == NULL_TREE && candidates == NULL_TREE)
2337 error ("template-id %qD for %q+D does not match any template "
2338 "declaration", template_id, decl);
2339 if (header_count && header_count != template_count + 1)
2340 inform (input_location, "saw %d %<template<>%>, need %d for "
2341 "specializing a member function template",
2342 header_count, template_count + 1);
2343 else
2344 print_candidates (orig_fns);
2345 return error_mark_node;
2347 else if ((templates && TREE_CHAIN (templates))
2348 || (candidates && TREE_CHAIN (candidates))
2349 || (templates && candidates))
2351 error ("ambiguous template specialization %qD for %q+D",
2352 template_id, decl);
2353 candidates = chainon (candidates, templates);
2354 print_candidates (candidates);
2355 return error_mark_node;
2358 /* We have one, and exactly one, match. */
2359 if (candidates)
2361 tree fn = TREE_VALUE (candidates);
2362 *targs_out = copy_node (DECL_TI_ARGS (fn));
2364 // Propagate the candidate's constraints to the declaration.
2365 set_constraints (decl, get_constraints (fn));
2367 /* DECL is a re-declaration or partial instantiation of a template
2368 function. */
2369 if (TREE_CODE (fn) == TEMPLATE_DECL)
2370 return fn;
2371 /* It was a specialization of an ordinary member function in a
2372 template class. */
2373 return DECL_TI_TEMPLATE (fn);
2376 /* It was a specialization of a template. */
2377 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2378 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2380 *targs_out = copy_node (targs);
2381 SET_TMPL_ARGS_LEVEL (*targs_out,
2382 TMPL_ARGS_DEPTH (*targs_out),
2383 TREE_PURPOSE (templates));
2385 else
2386 *targs_out = TREE_PURPOSE (templates);
2387 return TREE_VALUE (templates);
2390 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2391 but with the default argument values filled in from those in the
2392 TMPL_TYPES. */
2394 static tree
2395 copy_default_args_to_explicit_spec_1 (tree spec_types,
2396 tree tmpl_types)
2398 tree new_spec_types;
2400 if (!spec_types)
2401 return NULL_TREE;
2403 if (spec_types == void_list_node)
2404 return void_list_node;
2406 /* Substitute into the rest of the list. */
2407 new_spec_types =
2408 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2409 TREE_CHAIN (tmpl_types));
2411 /* Add the default argument for this parameter. */
2412 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2413 TREE_VALUE (spec_types),
2414 new_spec_types);
2417 /* DECL is an explicit specialization. Replicate default arguments
2418 from the template it specializes. (That way, code like:
2420 template <class T> void f(T = 3);
2421 template <> void f(double);
2422 void g () { f (); }
2424 works, as required.) An alternative approach would be to look up
2425 the correct default arguments at the call-site, but this approach
2426 is consistent with how implicit instantiations are handled. */
2428 static void
2429 copy_default_args_to_explicit_spec (tree decl)
2431 tree tmpl;
2432 tree spec_types;
2433 tree tmpl_types;
2434 tree new_spec_types;
2435 tree old_type;
2436 tree new_type;
2437 tree t;
2438 tree object_type = NULL_TREE;
2439 tree in_charge = NULL_TREE;
2440 tree vtt = NULL_TREE;
2442 /* See if there's anything we need to do. */
2443 tmpl = DECL_TI_TEMPLATE (decl);
2444 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2445 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2446 if (TREE_PURPOSE (t))
2447 break;
2448 if (!t)
2449 return;
2451 old_type = TREE_TYPE (decl);
2452 spec_types = TYPE_ARG_TYPES (old_type);
2454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2456 /* Remove the this pointer, but remember the object's type for
2457 CV quals. */
2458 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2459 spec_types = TREE_CHAIN (spec_types);
2460 tmpl_types = TREE_CHAIN (tmpl_types);
2462 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2464 /* DECL may contain more parameters than TMPL due to the extra
2465 in-charge parameter in constructors and destructors. */
2466 in_charge = spec_types;
2467 spec_types = TREE_CHAIN (spec_types);
2469 if (DECL_HAS_VTT_PARM_P (decl))
2471 vtt = spec_types;
2472 spec_types = TREE_CHAIN (spec_types);
2476 /* Compute the merged default arguments. */
2477 new_spec_types =
2478 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2480 /* Compute the new FUNCTION_TYPE. */
2481 if (object_type)
2483 if (vtt)
2484 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2485 TREE_VALUE (vtt),
2486 new_spec_types);
2488 if (in_charge)
2489 /* Put the in-charge parameter back. */
2490 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2491 TREE_VALUE (in_charge),
2492 new_spec_types);
2494 new_type = build_method_type_directly (object_type,
2495 TREE_TYPE (old_type),
2496 new_spec_types);
2498 else
2499 new_type = build_function_type (TREE_TYPE (old_type),
2500 new_spec_types);
2501 new_type = cp_build_type_attribute_variant (new_type,
2502 TYPE_ATTRIBUTES (old_type));
2503 new_type = build_exception_variant (new_type,
2504 TYPE_RAISES_EXCEPTIONS (old_type));
2506 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2507 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2509 TREE_TYPE (decl) = new_type;
2512 /* Return the number of template headers we expect to see for a definition
2513 or specialization of CTYPE or one of its non-template members. */
2516 num_template_headers_for_class (tree ctype)
2518 int num_templates = 0;
2520 while (ctype && CLASS_TYPE_P (ctype))
2522 /* You're supposed to have one `template <...>' for every
2523 template class, but you don't need one for a full
2524 specialization. For example:
2526 template <class T> struct S{};
2527 template <> struct S<int> { void f(); };
2528 void S<int>::f () {}
2530 is correct; there shouldn't be a `template <>' for the
2531 definition of `S<int>::f'. */
2532 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2533 /* If CTYPE does not have template information of any
2534 kind, then it is not a template, nor is it nested
2535 within a template. */
2536 break;
2537 if (explicit_class_specialization_p (ctype))
2538 break;
2539 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2540 ++num_templates;
2542 ctype = TYPE_CONTEXT (ctype);
2545 return num_templates;
2548 /* Do a simple sanity check on the template headers that precede the
2549 variable declaration DECL. */
2551 void
2552 check_template_variable (tree decl)
2554 tree ctx = CP_DECL_CONTEXT (decl);
2555 int wanted = num_template_headers_for_class (ctx);
2556 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2557 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2559 if (cxx_dialect < cxx14)
2560 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2561 "variable templates only available with "
2562 "-std=c++14 or -std=gnu++14");
2564 // Namespace-scope variable templates should have a template header.
2565 ++wanted;
2567 if (template_header_count > wanted)
2569 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2570 "too many template headers for %qD "
2571 "(should be %d)",
2572 decl, wanted);
2573 if (warned && CLASS_TYPE_P (ctx)
2574 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2575 inform (DECL_SOURCE_LOCATION (decl),
2576 "members of an explicitly specialized class are defined "
2577 "without a template header");
2581 /* An explicit specialization whose declarator-id or class-head-name is not
2582 qualified shall be declared in the nearest enclosing namespace of the
2583 template, or, if the namespace is inline (7.3.1), any namespace from its
2584 enclosing namespace set.
2586 If the name declared in the explicit instantiation is an unqualified name,
2587 the explicit instantiation shall appear in the namespace where its template
2588 is declared or, if that namespace is inline (7.3.1), any namespace from its
2589 enclosing namespace set. */
2591 void
2592 check_unqualified_spec_or_inst (tree t, location_t loc)
2594 tree tmpl = most_general_template (t);
2595 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2596 && !is_nested_namespace (current_namespace,
2597 CP_DECL_CONTEXT (tmpl), true))
2599 if (processing_specialization)
2600 permerror (loc, "explicit specialization of %qD outside its "
2601 "namespace must use a nested-name-specifier", tmpl);
2602 else if (processing_explicit_instantiation
2603 && cxx_dialect >= cxx11)
2604 /* This was allowed in C++98, so only pedwarn. */
2605 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2606 "outside its namespace must use a nested-name-"
2607 "specifier", tmpl);
2611 /* Check to see if the function just declared, as indicated in
2612 DECLARATOR, and in DECL, is a specialization of a function
2613 template. We may also discover that the declaration is an explicit
2614 instantiation at this point.
2616 Returns DECL, or an equivalent declaration that should be used
2617 instead if all goes well. Issues an error message if something is
2618 amiss. Returns error_mark_node if the error is not easily
2619 recoverable.
2621 FLAGS is a bitmask consisting of the following flags:
2623 2: The function has a definition.
2624 4: The function is a friend.
2626 The TEMPLATE_COUNT is the number of references to qualifying
2627 template classes that appeared in the name of the function. For
2628 example, in
2630 template <class T> struct S { void f(); };
2631 void S<int>::f();
2633 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2634 classes are not counted in the TEMPLATE_COUNT, so that in
2636 template <class T> struct S {};
2637 template <> struct S<int> { void f(); }
2638 template <> void S<int>::f();
2640 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2641 invalid; there should be no template <>.)
2643 If the function is a specialization, it is marked as such via
2644 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2645 is set up correctly, and it is added to the list of specializations
2646 for that template. */
2648 tree
2649 check_explicit_specialization (tree declarator,
2650 tree decl,
2651 int template_count,
2652 int flags)
2654 int have_def = flags & 2;
2655 int is_friend = flags & 4;
2656 bool is_concept = flags & 8;
2657 int specialization = 0;
2658 int explicit_instantiation = 0;
2659 int member_specialization = 0;
2660 tree ctype = DECL_CLASS_CONTEXT (decl);
2661 tree dname = DECL_NAME (decl);
2662 tmpl_spec_kind tsk;
2664 if (is_friend)
2666 if (!processing_specialization)
2667 tsk = tsk_none;
2668 else
2669 tsk = tsk_excessive_parms;
2671 else
2672 tsk = current_tmpl_spec_kind (template_count);
2674 switch (tsk)
2676 case tsk_none:
2677 if (processing_specialization && !VAR_P (decl))
2679 specialization = 1;
2680 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2682 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2684 if (is_friend)
2685 /* This could be something like:
2687 template <class T> void f(T);
2688 class S { friend void f<>(int); } */
2689 specialization = 1;
2690 else
2692 /* This case handles bogus declarations like template <>
2693 template <class T> void f<int>(); */
2695 error ("template-id %qD in declaration of primary template",
2696 declarator);
2697 return decl;
2700 break;
2702 case tsk_invalid_member_spec:
2703 /* The error has already been reported in
2704 check_specialization_scope. */
2705 return error_mark_node;
2707 case tsk_invalid_expl_inst:
2708 error ("template parameter list used in explicit instantiation");
2710 /* Fall through. */
2712 case tsk_expl_inst:
2713 if (have_def)
2714 error ("definition provided for explicit instantiation");
2716 explicit_instantiation = 1;
2717 break;
2719 case tsk_excessive_parms:
2720 case tsk_insufficient_parms:
2721 if (tsk == tsk_excessive_parms)
2722 error ("too many template parameter lists in declaration of %qD",
2723 decl);
2724 else if (template_header_count)
2725 error("too few template parameter lists in declaration of %qD", decl);
2726 else
2727 error("explicit specialization of %qD must be introduced by "
2728 "%<template <>%>", decl);
2730 /* Fall through. */
2731 case tsk_expl_spec:
2732 if (is_concept)
2733 error ("explicit specialization declared %<concept%>");
2735 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2736 /* In cases like template<> constexpr bool v = true;
2737 We'll give an error in check_template_variable. */
2738 break;
2740 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2741 if (ctype)
2742 member_specialization = 1;
2743 else
2744 specialization = 1;
2745 break;
2747 case tsk_template:
2748 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2750 /* This case handles bogus declarations like template <>
2751 template <class T> void f<int>(); */
2753 if (!uses_template_parms (declarator))
2754 error ("template-id %qD in declaration of primary template",
2755 declarator);
2756 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2758 /* Partial specialization of variable template. */
2759 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2760 specialization = 1;
2761 goto ok;
2763 else if (cxx_dialect < cxx14)
2764 error ("non-type partial specialization %qD "
2765 "is not allowed", declarator);
2766 else
2767 error ("non-class, non-variable partial specialization %qD "
2768 "is not allowed", declarator);
2769 return decl;
2770 ok:;
2773 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2774 /* This is a specialization of a member template, without
2775 specialization the containing class. Something like:
2777 template <class T> struct S {
2778 template <class U> void f (U);
2780 template <> template <class U> void S<int>::f(U) {}
2782 That's a specialization -- but of the entire template. */
2783 specialization = 1;
2784 break;
2786 default:
2787 gcc_unreachable ();
2790 if ((specialization || member_specialization)
2791 /* This doesn't apply to variable templates. */
2792 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2793 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2795 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2796 for (; t; t = TREE_CHAIN (t))
2797 if (TREE_PURPOSE (t))
2799 permerror (input_location,
2800 "default argument specified in explicit specialization");
2801 break;
2805 if (specialization || member_specialization || explicit_instantiation)
2807 tree tmpl = NULL_TREE;
2808 tree targs = NULL_TREE;
2809 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2811 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2812 if (!was_template_id)
2814 tree fns;
2816 gcc_assert (identifier_p (declarator));
2817 if (ctype)
2818 fns = dname;
2819 else
2821 /* If there is no class context, the explicit instantiation
2822 must be at namespace scope. */
2823 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2825 /* Find the namespace binding, using the declaration
2826 context. */
2827 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2828 false, true);
2829 if (fns == error_mark_node)
2830 /* If lookup fails, look for a friend declaration so we can
2831 give a better diagnostic. */
2832 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2833 /*type*/false, /*complain*/true,
2834 /*hidden*/true);
2836 if (fns == error_mark_node || !is_overloaded_fn (fns))
2838 error ("%qD is not a template function", dname);
2839 fns = error_mark_node;
2843 declarator = lookup_template_function (fns, NULL_TREE);
2846 if (declarator == error_mark_node)
2847 return error_mark_node;
2849 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2851 if (!explicit_instantiation)
2852 /* A specialization in class scope. This is invalid,
2853 but the error will already have been flagged by
2854 check_specialization_scope. */
2855 return error_mark_node;
2856 else
2858 /* It's not valid to write an explicit instantiation in
2859 class scope, e.g.:
2861 class C { template void f(); }
2863 This case is caught by the parser. However, on
2864 something like:
2866 template class C { void f(); };
2868 (which is invalid) we can get here. The error will be
2869 issued later. */
2873 return decl;
2875 else if (ctype != NULL_TREE
2876 && (identifier_p (TREE_OPERAND (declarator, 0))))
2878 // We'll match variable templates in start_decl.
2879 if (VAR_P (decl))
2880 return decl;
2882 /* Find the list of functions in ctype that have the same
2883 name as the declared function. */
2884 tree name = TREE_OPERAND (declarator, 0);
2886 if (constructor_name_p (name, ctype))
2888 if (DECL_CONSTRUCTOR_P (decl)
2889 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2890 : !CLASSTYPE_DESTRUCTOR (ctype))
2892 /* From [temp.expl.spec]:
2894 If such an explicit specialization for the member
2895 of a class template names an implicitly-declared
2896 special member function (clause _special_), the
2897 program is ill-formed.
2899 Similar language is found in [temp.explicit]. */
2900 error ("specialization of implicitly-declared special member function");
2901 return error_mark_node;
2904 name = DECL_NAME (decl);
2907 /* For a type-conversion operator, We might be looking for
2908 `operator int' which will be a specialization of
2909 `operator T'. Grab all the conversion operators, and
2910 then select from them. */
2911 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2912 ? conv_op_identifier : name);
2914 if (fns == NULL_TREE)
2916 error ("no member function %qD declared in %qT", name, ctype);
2917 return error_mark_node;
2919 else
2920 TREE_OPERAND (declarator, 0) = fns;
2923 /* Figure out what exactly is being specialized at this point.
2924 Note that for an explicit instantiation, even one for a
2925 member function, we cannot tell a priori whether the
2926 instantiation is for a member template, or just a member
2927 function of a template class. Even if a member template is
2928 being instantiated, the member template arguments may be
2929 elided if they can be deduced from the rest of the
2930 declaration. */
2931 tmpl = determine_specialization (declarator, decl,
2932 &targs,
2933 member_specialization,
2934 template_count,
2935 tsk);
2937 if (!tmpl || tmpl == error_mark_node)
2938 /* We couldn't figure out what this declaration was
2939 specializing. */
2940 return error_mark_node;
2941 else
2943 if (TREE_CODE (decl) == FUNCTION_DECL
2944 && DECL_HIDDEN_FRIEND_P (tmpl))
2946 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2947 "friend declaration %qD is not visible to "
2948 "explicit specialization", tmpl))
2949 inform (DECL_SOURCE_LOCATION (tmpl),
2950 "friend declaration here");
2952 else if (!ctype && !is_friend
2953 && CP_DECL_CONTEXT (decl) == current_namespace)
2954 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2956 tree gen_tmpl = most_general_template (tmpl);
2958 if (explicit_instantiation)
2960 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2961 is done by do_decl_instantiation later. */
2963 int arg_depth = TMPL_ARGS_DEPTH (targs);
2964 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2966 if (arg_depth > parm_depth)
2968 /* If TMPL is not the most general template (for
2969 example, if TMPL is a friend template that is
2970 injected into namespace scope), then there will
2971 be too many levels of TARGS. Remove some of them
2972 here. */
2973 int i;
2974 tree new_targs;
2976 new_targs = make_tree_vec (parm_depth);
2977 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2978 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2979 = TREE_VEC_ELT (targs, i);
2980 targs = new_targs;
2983 return instantiate_template (tmpl, targs, tf_error);
2986 /* If we thought that the DECL was a member function, but it
2987 turns out to be specializing a static member function,
2988 make DECL a static member function as well. */
2989 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2990 && DECL_STATIC_FUNCTION_P (tmpl)
2991 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2992 revert_static_member_fn (decl);
2994 /* If this is a specialization of a member template of a
2995 template class, we want to return the TEMPLATE_DECL, not
2996 the specialization of it. */
2997 if (tsk == tsk_template && !was_template_id)
2999 tree result = DECL_TEMPLATE_RESULT (tmpl);
3000 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3001 DECL_INITIAL (result) = NULL_TREE;
3002 if (have_def)
3004 tree parm;
3005 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3006 DECL_SOURCE_LOCATION (result)
3007 = DECL_SOURCE_LOCATION (decl);
3008 /* We want to use the argument list specified in the
3009 definition, not in the original declaration. */
3010 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3011 for (parm = DECL_ARGUMENTS (result); parm;
3012 parm = DECL_CHAIN (parm))
3013 DECL_CONTEXT (parm) = result;
3015 return register_specialization (tmpl, gen_tmpl, targs,
3016 is_friend, 0);
3019 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3020 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3022 if (was_template_id)
3023 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3025 /* Inherit default function arguments from the template
3026 DECL is specializing. */
3027 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3028 copy_default_args_to_explicit_spec (decl);
3030 /* This specialization has the same protection as the
3031 template it specializes. */
3032 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3033 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3035 /* 7.1.1-1 [dcl.stc]
3037 A storage-class-specifier shall not be specified in an
3038 explicit specialization...
3040 The parser rejects these, so unless action is taken here,
3041 explicit function specializations will always appear with
3042 global linkage.
3044 The action recommended by the C++ CWG in response to C++
3045 defect report 605 is to make the storage class and linkage
3046 of the explicit specialization match the templated function:
3048 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3050 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3052 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3053 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3055 /* A concept cannot be specialized. */
3056 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3058 error ("explicit specialization of function concept %qD",
3059 gen_tmpl);
3060 return error_mark_node;
3063 /* This specialization has the same linkage and visibility as
3064 the function template it specializes. */
3065 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3066 if (! TREE_PUBLIC (decl))
3068 DECL_INTERFACE_KNOWN (decl) = 1;
3069 DECL_NOT_REALLY_EXTERN (decl) = 1;
3071 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3072 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3074 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3075 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3079 /* If DECL is a friend declaration, declared using an
3080 unqualified name, the namespace associated with DECL may
3081 have been set incorrectly. For example, in:
3083 template <typename T> void f(T);
3084 namespace N {
3085 struct S { friend void f<int>(int); }
3088 we will have set the DECL_CONTEXT for the friend
3089 declaration to N, rather than to the global namespace. */
3090 if (DECL_NAMESPACE_SCOPE_P (decl))
3091 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3093 if (is_friend && !have_def)
3094 /* This is not really a declaration of a specialization.
3095 It's just the name of an instantiation. But, it's not
3096 a request for an instantiation, either. */
3097 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3098 else if (TREE_CODE (decl) == FUNCTION_DECL)
3099 /* A specialization is not necessarily COMDAT. */
3100 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3101 && DECL_DECLARED_INLINE_P (decl));
3102 else if (VAR_P (decl))
3103 DECL_COMDAT (decl) = false;
3105 /* If this is a full specialization, register it so that we can find
3106 it again. Partial specializations will be registered in
3107 process_partial_specialization. */
3108 if (!processing_template_decl)
3109 decl = register_specialization (decl, gen_tmpl, targs,
3110 is_friend, 0);
3112 /* A 'structor should already have clones. */
3113 gcc_assert (decl == error_mark_node
3114 || variable_template_p (tmpl)
3115 || !(DECL_CONSTRUCTOR_P (decl)
3116 || DECL_DESTRUCTOR_P (decl))
3117 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3121 return decl;
3124 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3125 parameters. These are represented in the same format used for
3126 DECL_TEMPLATE_PARMS. */
3129 comp_template_parms (const_tree parms1, const_tree parms2)
3131 const_tree p1;
3132 const_tree p2;
3134 if (parms1 == parms2)
3135 return 1;
3137 for (p1 = parms1, p2 = parms2;
3138 p1 != NULL_TREE && p2 != NULL_TREE;
3139 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3141 tree t1 = TREE_VALUE (p1);
3142 tree t2 = TREE_VALUE (p2);
3143 int i;
3145 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3146 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3148 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3149 return 0;
3151 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3153 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3154 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3156 /* If either of the template parameters are invalid, assume
3157 they match for the sake of error recovery. */
3158 if (error_operand_p (parm1) || error_operand_p (parm2))
3159 return 1;
3161 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3162 return 0;
3164 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3165 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3166 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3167 continue;
3168 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3169 return 0;
3173 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3174 /* One set of parameters has more parameters lists than the
3175 other. */
3176 return 0;
3178 return 1;
3181 /* Determine whether PARM is a parameter pack. */
3183 bool
3184 template_parameter_pack_p (const_tree parm)
3186 /* Determine if we have a non-type template parameter pack. */
3187 if (TREE_CODE (parm) == PARM_DECL)
3188 return (DECL_TEMPLATE_PARM_P (parm)
3189 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3190 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3191 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3193 /* If this is a list of template parameters, we could get a
3194 TYPE_DECL or a TEMPLATE_DECL. */
3195 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3196 parm = TREE_TYPE (parm);
3198 /* Otherwise it must be a type template parameter. */
3199 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3200 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3201 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3204 /* Determine if T is a function parameter pack. */
3206 bool
3207 function_parameter_pack_p (const_tree t)
3209 if (t && TREE_CODE (t) == PARM_DECL)
3210 return DECL_PACK_P (t);
3211 return false;
3214 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3215 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3217 tree
3218 get_function_template_decl (const_tree primary_func_tmpl_inst)
3220 if (! primary_func_tmpl_inst
3221 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3222 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3223 return NULL;
3225 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3228 /* Return true iff the function parameter PARAM_DECL was expanded
3229 from the function parameter pack PACK. */
3231 bool
3232 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3234 if (DECL_ARTIFICIAL (param_decl)
3235 || !function_parameter_pack_p (pack))
3236 return false;
3238 /* The parameter pack and its pack arguments have the same
3239 DECL_PARM_INDEX. */
3240 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3243 /* Determine whether ARGS describes a variadic template args list,
3244 i.e., one that is terminated by a template argument pack. */
3246 static bool
3247 template_args_variadic_p (tree args)
3249 int nargs;
3250 tree last_parm;
3252 if (args == NULL_TREE)
3253 return false;
3255 args = INNERMOST_TEMPLATE_ARGS (args);
3256 nargs = TREE_VEC_LENGTH (args);
3258 if (nargs == 0)
3259 return false;
3261 last_parm = TREE_VEC_ELT (args, nargs - 1);
3263 return ARGUMENT_PACK_P (last_parm);
3266 /* Generate a new name for the parameter pack name NAME (an
3267 IDENTIFIER_NODE) that incorporates its */
3269 static tree
3270 make_ith_pack_parameter_name (tree name, int i)
3272 /* Munge the name to include the parameter index. */
3273 #define NUMBUF_LEN 128
3274 char numbuf[NUMBUF_LEN];
3275 char* newname;
3276 int newname_len;
3278 if (name == NULL_TREE)
3279 return name;
3280 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3281 newname_len = IDENTIFIER_LENGTH (name)
3282 + strlen (numbuf) + 2;
3283 newname = (char*)alloca (newname_len);
3284 snprintf (newname, newname_len,
3285 "%s#%i", IDENTIFIER_POINTER (name), i);
3286 return get_identifier (newname);
3289 /* Return true if T is a primary function, class or alias template
3290 instantiation. */
3292 bool
3293 primary_template_instantiation_p (const_tree t)
3295 if (!t)
3296 return false;
3298 if (TREE_CODE (t) == FUNCTION_DECL)
3299 return DECL_LANG_SPECIFIC (t)
3300 && DECL_TEMPLATE_INSTANTIATION (t)
3301 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3302 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3303 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3304 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3305 else if (alias_template_specialization_p (t))
3306 return true;
3307 return false;
3310 /* Return true if PARM is a template template parameter. */
3312 bool
3313 template_template_parameter_p (const_tree parm)
3315 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3318 /* Return true iff PARM is a DECL representing a type template
3319 parameter. */
3321 bool
3322 template_type_parameter_p (const_tree parm)
3324 return (parm
3325 && (TREE_CODE (parm) == TYPE_DECL
3326 || TREE_CODE (parm) == TEMPLATE_DECL)
3327 && DECL_TEMPLATE_PARM_P (parm));
3330 /* Return the template parameters of T if T is a
3331 primary template instantiation, NULL otherwise. */
3333 tree
3334 get_primary_template_innermost_parameters (const_tree t)
3336 tree parms = NULL, template_info = NULL;
3338 if ((template_info = get_template_info (t))
3339 && primary_template_instantiation_p (t))
3340 parms = INNERMOST_TEMPLATE_PARMS
3341 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3343 return parms;
3346 /* Return the template parameters of the LEVELth level from the full list
3347 of template parameters PARMS. */
3349 tree
3350 get_template_parms_at_level (tree parms, int level)
3352 tree p;
3353 if (!parms
3354 || TREE_CODE (parms) != TREE_LIST
3355 || level > TMPL_PARMS_DEPTH (parms))
3356 return NULL_TREE;
3358 for (p = parms; p; p = TREE_CHAIN (p))
3359 if (TMPL_PARMS_DEPTH (p) == level)
3360 return p;
3362 return NULL_TREE;
3365 /* Returns the template arguments of T if T is a template instantiation,
3366 NULL otherwise. */
3368 tree
3369 get_template_innermost_arguments (const_tree t)
3371 tree args = NULL, template_info = NULL;
3373 if ((template_info = get_template_info (t))
3374 && TI_ARGS (template_info))
3375 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3377 return args;
3380 /* Return the argument pack elements of T if T is a template argument pack,
3381 NULL otherwise. */
3383 tree
3384 get_template_argument_pack_elems (const_tree t)
3386 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3387 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3388 return NULL;
3390 return ARGUMENT_PACK_ARGS (t);
3393 /* True iff FN is a function representing a built-in variadic parameter
3394 pack. */
3396 bool
3397 builtin_pack_fn_p (tree fn)
3399 if (!fn
3400 || TREE_CODE (fn) != FUNCTION_DECL
3401 || !DECL_IS_BUILTIN (fn))
3402 return false;
3404 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3405 return true;
3407 return false;
3410 /* True iff CALL is a call to a function representing a built-in variadic
3411 parameter pack. */
3413 static bool
3414 builtin_pack_call_p (tree call)
3416 if (TREE_CODE (call) != CALL_EXPR)
3417 return false;
3418 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3421 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3423 static tree
3424 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3425 tree in_decl)
3427 tree ohi = CALL_EXPR_ARG (call, 0);
3428 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3429 false/*fn*/, true/*int_cst*/);
3431 if (value_dependent_expression_p (hi))
3433 if (hi != ohi)
3435 call = copy_node (call);
3436 CALL_EXPR_ARG (call, 0) = hi;
3438 tree ex = make_pack_expansion (call, complain);
3439 tree vec = make_tree_vec (1);
3440 TREE_VEC_ELT (vec, 0) = ex;
3441 return vec;
3443 else
3445 hi = cxx_constant_value (hi);
3446 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3448 /* Calculate the largest value of len that won't make the size of the vec
3449 overflow an int. The compiler will exceed resource limits long before
3450 this, but it seems a decent place to diagnose. */
3451 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3453 if (len < 0 || len > max)
3455 if ((complain & tf_error)
3456 && hi != error_mark_node)
3457 error ("argument to __integer_pack must be between 0 and %d", max);
3458 return error_mark_node;
3461 tree vec = make_tree_vec (len);
3463 for (int i = 0; i < len; ++i)
3464 TREE_VEC_ELT (vec, i) = size_int (i);
3466 return vec;
3470 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3471 CALL. */
3473 static tree
3474 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3475 tree in_decl)
3477 if (!builtin_pack_call_p (call))
3478 return NULL_TREE;
3480 tree fn = CALL_EXPR_FN (call);
3482 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3483 return expand_integer_pack (call, args, complain, in_decl);
3485 return NULL_TREE;
3488 /* Structure used to track the progress of find_parameter_packs_r. */
3489 struct find_parameter_pack_data
3491 /* TREE_LIST that will contain all of the parameter packs found by
3492 the traversal. */
3493 tree* parameter_packs;
3495 /* Set of AST nodes that have been visited by the traversal. */
3496 hash_set<tree> *visited;
3498 /* True iff we're making a type pack expansion. */
3499 bool type_pack_expansion_p;
3502 /* Identifies all of the argument packs that occur in a template
3503 argument and appends them to the TREE_LIST inside DATA, which is a
3504 find_parameter_pack_data structure. This is a subroutine of
3505 make_pack_expansion and uses_parameter_packs. */
3506 static tree
3507 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3509 tree t = *tp;
3510 struct find_parameter_pack_data* ppd =
3511 (struct find_parameter_pack_data*)data;
3512 bool parameter_pack_p = false;
3514 /* Handle type aliases/typedefs. */
3515 if (TYPE_ALIAS_P (t))
3517 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3518 cp_walk_tree (&TI_ARGS (tinfo),
3519 &find_parameter_packs_r,
3520 ppd, ppd->visited);
3521 *walk_subtrees = 0;
3522 return NULL_TREE;
3525 /* Identify whether this is a parameter pack or not. */
3526 switch (TREE_CODE (t))
3528 case TEMPLATE_PARM_INDEX:
3529 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3530 parameter_pack_p = true;
3531 break;
3533 case TEMPLATE_TYPE_PARM:
3534 t = TYPE_MAIN_VARIANT (t);
3535 /* FALLTHRU */
3536 case TEMPLATE_TEMPLATE_PARM:
3537 /* If the placeholder appears in the decl-specifier-seq of a function
3538 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3539 is a pack expansion, the invented template parameter is a template
3540 parameter pack. */
3541 if (ppd->type_pack_expansion_p && is_auto (t))
3542 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3543 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3544 parameter_pack_p = true;
3545 break;
3547 case FIELD_DECL:
3548 case PARM_DECL:
3549 if (DECL_PACK_P (t))
3551 /* We don't want to walk into the type of a PARM_DECL,
3552 because we don't want to see the type parameter pack. */
3553 *walk_subtrees = 0;
3554 parameter_pack_p = true;
3556 break;
3558 /* Look through a lambda capture proxy to the field pack. */
3559 case VAR_DECL:
3560 if (DECL_HAS_VALUE_EXPR_P (t))
3562 tree v = DECL_VALUE_EXPR (t);
3563 cp_walk_tree (&v,
3564 &find_parameter_packs_r,
3565 ppd, ppd->visited);
3566 *walk_subtrees = 0;
3568 else if (variable_template_specialization_p (t))
3570 cp_walk_tree (&DECL_TI_ARGS (t),
3571 find_parameter_packs_r,
3572 ppd, ppd->visited);
3573 *walk_subtrees = 0;
3575 break;
3577 case CALL_EXPR:
3578 if (builtin_pack_call_p (t))
3579 parameter_pack_p = true;
3580 break;
3582 case BASES:
3583 parameter_pack_p = true;
3584 break;
3585 default:
3586 /* Not a parameter pack. */
3587 break;
3590 if (parameter_pack_p)
3592 /* Add this parameter pack to the list. */
3593 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3596 if (TYPE_P (t))
3597 cp_walk_tree (&TYPE_CONTEXT (t),
3598 &find_parameter_packs_r, ppd, ppd->visited);
3600 /* This switch statement will return immediately if we don't find a
3601 parameter pack. */
3602 switch (TREE_CODE (t))
3604 case TEMPLATE_PARM_INDEX:
3605 return NULL_TREE;
3607 case BOUND_TEMPLATE_TEMPLATE_PARM:
3608 /* Check the template itself. */
3609 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3610 &find_parameter_packs_r, ppd, ppd->visited);
3611 /* Check the template arguments. */
3612 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3613 ppd->visited);
3614 *walk_subtrees = 0;
3615 return NULL_TREE;
3617 case TEMPLATE_TYPE_PARM:
3618 case TEMPLATE_TEMPLATE_PARM:
3619 return NULL_TREE;
3621 case PARM_DECL:
3622 return NULL_TREE;
3624 case DECL_EXPR:
3625 /* Ignore the declaration of a capture proxy for a parameter pack. */
3626 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3627 *walk_subtrees = 0;
3628 return NULL_TREE;
3630 case RECORD_TYPE:
3631 if (TYPE_PTRMEMFUNC_P (t))
3632 return NULL_TREE;
3633 /* Fall through. */
3635 case UNION_TYPE:
3636 case ENUMERAL_TYPE:
3637 if (TYPE_TEMPLATE_INFO (t))
3638 cp_walk_tree (&TYPE_TI_ARGS (t),
3639 &find_parameter_packs_r, ppd, ppd->visited);
3641 *walk_subtrees = 0;
3642 return NULL_TREE;
3644 case TEMPLATE_DECL:
3645 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3646 return NULL_TREE;
3647 gcc_fallthrough();
3649 case CONSTRUCTOR:
3650 cp_walk_tree (&TREE_TYPE (t),
3651 &find_parameter_packs_r, ppd, ppd->visited);
3652 return NULL_TREE;
3654 case TYPENAME_TYPE:
3655 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3656 ppd, ppd->visited);
3657 *walk_subtrees = 0;
3658 return NULL_TREE;
3660 case TYPE_PACK_EXPANSION:
3661 case EXPR_PACK_EXPANSION:
3662 *walk_subtrees = 0;
3663 return NULL_TREE;
3665 case INTEGER_TYPE:
3666 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3667 ppd, ppd->visited);
3668 *walk_subtrees = 0;
3669 return NULL_TREE;
3671 case IDENTIFIER_NODE:
3672 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3673 ppd->visited);
3674 *walk_subtrees = 0;
3675 return NULL_TREE;
3677 case LAMBDA_EXPR:
3679 tree fn = lambda_function (t);
3680 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3681 ppd->visited);
3682 *walk_subtrees = 0;
3683 return NULL_TREE;
3686 case DECLTYPE_TYPE:
3688 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3689 type_pack_expansion_p to false so that any placeholders
3690 within the expression don't get marked as parameter packs. */
3691 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3692 ppd->type_pack_expansion_p = false;
3693 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3694 ppd, ppd->visited);
3695 ppd->type_pack_expansion_p = type_pack_expansion_p;
3696 *walk_subtrees = 0;
3697 return NULL_TREE;
3700 default:
3701 return NULL_TREE;
3704 return NULL_TREE;
3707 /* Determines if the expression or type T uses any parameter packs. */
3708 bool
3709 uses_parameter_packs (tree t)
3711 tree parameter_packs = NULL_TREE;
3712 struct find_parameter_pack_data ppd;
3713 ppd.parameter_packs = &parameter_packs;
3714 ppd.visited = new hash_set<tree>;
3715 ppd.type_pack_expansion_p = false;
3716 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3717 delete ppd.visited;
3718 return parameter_packs != NULL_TREE;
3721 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3722 representation a base-class initializer into a parameter pack
3723 expansion. If all goes well, the resulting node will be an
3724 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3725 respectively. */
3726 tree
3727 make_pack_expansion (tree arg, tsubst_flags_t complain)
3729 tree result;
3730 tree parameter_packs = NULL_TREE;
3731 bool for_types = false;
3732 struct find_parameter_pack_data ppd;
3734 if (!arg || arg == error_mark_node)
3735 return arg;
3737 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3739 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3740 class initializer. In this case, the TREE_PURPOSE will be a
3741 _TYPE node (representing the base class expansion we're
3742 initializing) and the TREE_VALUE will be a TREE_LIST
3743 containing the initialization arguments.
3745 The resulting expansion looks somewhat different from most
3746 expansions. Rather than returning just one _EXPANSION, we
3747 return a TREE_LIST whose TREE_PURPOSE is a
3748 TYPE_PACK_EXPANSION containing the bases that will be
3749 initialized. The TREE_VALUE will be identical to the
3750 original TREE_VALUE, which is a list of arguments that will
3751 be passed to each base. We do not introduce any new pack
3752 expansion nodes into the TREE_VALUE (although it is possible
3753 that some already exist), because the TREE_PURPOSE and
3754 TREE_VALUE all need to be expanded together with the same
3755 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3756 resulting TREE_PURPOSE will mention the parameter packs in
3757 both the bases and the arguments to the bases. */
3758 tree purpose;
3759 tree value;
3760 tree parameter_packs = NULL_TREE;
3762 /* Determine which parameter packs will be used by the base
3763 class expansion. */
3764 ppd.visited = new hash_set<tree>;
3765 ppd.parameter_packs = &parameter_packs;
3766 ppd.type_pack_expansion_p = true;
3767 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3768 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3769 &ppd, ppd.visited);
3771 if (parameter_packs == NULL_TREE)
3773 if (complain & tf_error)
3774 error ("base initializer expansion %qT contains no parameter packs",
3775 arg);
3776 delete ppd.visited;
3777 return error_mark_node;
3780 if (TREE_VALUE (arg) != void_type_node)
3782 /* Collect the sets of parameter packs used in each of the
3783 initialization arguments. */
3784 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3786 /* Determine which parameter packs will be expanded in this
3787 argument. */
3788 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3789 &ppd, ppd.visited);
3793 delete ppd.visited;
3795 /* Create the pack expansion type for the base type. */
3796 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3797 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3798 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3799 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3801 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3802 they will rarely be compared to anything. */
3803 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3805 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3808 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3809 for_types = true;
3811 /* Build the PACK_EXPANSION_* node. */
3812 result = for_types
3813 ? cxx_make_type (TYPE_PACK_EXPANSION)
3814 : make_node (EXPR_PACK_EXPANSION);
3815 SET_PACK_EXPANSION_PATTERN (result, arg);
3816 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3818 /* Propagate type and const-expression information. */
3819 TREE_TYPE (result) = TREE_TYPE (arg);
3820 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3821 /* Mark this read now, since the expansion might be length 0. */
3822 mark_exp_read (arg);
3824 else
3825 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3826 they will rarely be compared to anything. */
3827 SET_TYPE_STRUCTURAL_EQUALITY (result);
3829 /* Determine which parameter packs will be expanded. */
3830 ppd.parameter_packs = &parameter_packs;
3831 ppd.visited = new hash_set<tree>;
3832 ppd.type_pack_expansion_p = TYPE_P (arg);
3833 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3834 delete ppd.visited;
3836 /* Make sure we found some parameter packs. */
3837 if (parameter_packs == NULL_TREE)
3839 if (complain & tf_error)
3841 if (TYPE_P (arg))
3842 error ("expansion pattern %qT contains no argument packs", arg);
3843 else
3844 error ("expansion pattern %qE contains no argument packs", arg);
3846 return error_mark_node;
3848 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3850 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3852 return result;
3855 /* Checks T for any "bare" parameter packs, which have not yet been
3856 expanded, and issues an error if any are found. This operation can
3857 only be done on full expressions or types (e.g., an expression
3858 statement, "if" condition, etc.), because we could have expressions like:
3860 foo(f(g(h(args)))...)
3862 where "args" is a parameter pack. check_for_bare_parameter_packs
3863 should not be called for the subexpressions args, h(args),
3864 g(h(args)), or f(g(h(args))), because we would produce erroneous
3865 error messages.
3867 Returns TRUE and emits an error if there were bare parameter packs,
3868 returns FALSE otherwise. */
3869 bool
3870 check_for_bare_parameter_packs (tree t)
3872 tree parameter_packs = NULL_TREE;
3873 struct find_parameter_pack_data ppd;
3875 if (!processing_template_decl || !t || t == error_mark_node)
3876 return false;
3878 /* A lambda might use a parameter pack from the containing context. */
3879 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3880 return false;
3882 if (TREE_CODE (t) == TYPE_DECL)
3883 t = TREE_TYPE (t);
3885 ppd.parameter_packs = &parameter_packs;
3886 ppd.visited = new hash_set<tree>;
3887 ppd.type_pack_expansion_p = false;
3888 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3889 delete ppd.visited;
3891 if (parameter_packs)
3893 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3894 error_at (loc, "parameter packs not expanded with %<...%>:");
3895 while (parameter_packs)
3897 tree pack = TREE_VALUE (parameter_packs);
3898 tree name = NULL_TREE;
3900 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3901 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3902 name = TYPE_NAME (pack);
3903 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3904 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3905 else if (TREE_CODE (pack) == CALL_EXPR)
3906 name = DECL_NAME (CALL_EXPR_FN (pack));
3907 else
3908 name = DECL_NAME (pack);
3910 if (name)
3911 inform (loc, " %qD", name);
3912 else
3913 inform (loc, " <anonymous>");
3915 parameter_packs = TREE_CHAIN (parameter_packs);
3918 return true;
3921 return false;
3924 /* Expand any parameter packs that occur in the template arguments in
3925 ARGS. */
3926 tree
3927 expand_template_argument_pack (tree args)
3929 if (args == error_mark_node)
3930 return error_mark_node;
3932 tree result_args = NULL_TREE;
3933 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3934 int num_result_args = -1;
3935 int non_default_args_count = -1;
3937 /* First, determine if we need to expand anything, and the number of
3938 slots we'll need. */
3939 for (in_arg = 0; in_arg < nargs; ++in_arg)
3941 tree arg = TREE_VEC_ELT (args, in_arg);
3942 if (arg == NULL_TREE)
3943 return args;
3944 if (ARGUMENT_PACK_P (arg))
3946 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3947 if (num_result_args < 0)
3948 num_result_args = in_arg + num_packed;
3949 else
3950 num_result_args += num_packed;
3952 else
3954 if (num_result_args >= 0)
3955 num_result_args++;
3959 /* If no expansion is necessary, we're done. */
3960 if (num_result_args < 0)
3961 return args;
3963 /* Expand arguments. */
3964 result_args = make_tree_vec (num_result_args);
3965 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3966 non_default_args_count =
3967 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3968 for (in_arg = 0; in_arg < nargs; ++in_arg)
3970 tree arg = TREE_VEC_ELT (args, in_arg);
3971 if (ARGUMENT_PACK_P (arg))
3973 tree packed = ARGUMENT_PACK_ARGS (arg);
3974 int i, num_packed = TREE_VEC_LENGTH (packed);
3975 for (i = 0; i < num_packed; ++i, ++out_arg)
3976 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3977 if (non_default_args_count > 0)
3978 non_default_args_count += num_packed - 1;
3980 else
3982 TREE_VEC_ELT (result_args, out_arg) = arg;
3983 ++out_arg;
3986 if (non_default_args_count >= 0)
3987 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3988 return result_args;
3991 /* Checks if DECL shadows a template parameter.
3993 [temp.local]: A template-parameter shall not be redeclared within its
3994 scope (including nested scopes).
3996 Emits an error and returns TRUE if the DECL shadows a parameter,
3997 returns FALSE otherwise. */
3999 bool
4000 check_template_shadow (tree decl)
4002 tree olddecl;
4004 /* If we're not in a template, we can't possibly shadow a template
4005 parameter. */
4006 if (!current_template_parms)
4007 return true;
4009 /* Figure out what we're shadowing. */
4010 decl = OVL_FIRST (decl);
4011 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4013 /* If there's no previous binding for this name, we're not shadowing
4014 anything, let alone a template parameter. */
4015 if (!olddecl)
4016 return true;
4018 /* If we're not shadowing a template parameter, we're done. Note
4019 that OLDDECL might be an OVERLOAD (or perhaps even an
4020 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4021 node. */
4022 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4023 return true;
4025 /* We check for decl != olddecl to avoid bogus errors for using a
4026 name inside a class. We check TPFI to avoid duplicate errors for
4027 inline member templates. */
4028 if (decl == olddecl
4029 || (DECL_TEMPLATE_PARM_P (decl)
4030 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4031 return true;
4033 /* Don't complain about the injected class name, as we've already
4034 complained about the class itself. */
4035 if (DECL_SELF_REFERENCE_P (decl))
4036 return false;
4038 if (DECL_TEMPLATE_PARM_P (decl))
4039 error ("declaration of template parameter %q+D shadows "
4040 "template parameter", decl);
4041 else
4042 error ("declaration of %q+#D shadows template parameter", decl);
4043 inform (DECL_SOURCE_LOCATION (olddecl),
4044 "template parameter %qD declared here", olddecl);
4045 return false;
4048 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4049 ORIG_LEVEL, DECL, and TYPE. */
4051 static tree
4052 build_template_parm_index (int index,
4053 int level,
4054 int orig_level,
4055 tree decl,
4056 tree type)
4058 tree t = make_node (TEMPLATE_PARM_INDEX);
4059 TEMPLATE_PARM_IDX (t) = index;
4060 TEMPLATE_PARM_LEVEL (t) = level;
4061 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4062 TEMPLATE_PARM_DECL (t) = decl;
4063 TREE_TYPE (t) = type;
4064 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4065 TREE_READONLY (t) = TREE_READONLY (decl);
4067 return t;
4070 /* Find the canonical type parameter for the given template type
4071 parameter. Returns the canonical type parameter, which may be TYPE
4072 if no such parameter existed. */
4074 static tree
4075 canonical_type_parameter (tree type)
4077 tree list;
4078 int idx = TEMPLATE_TYPE_IDX (type);
4079 if (!canonical_template_parms)
4080 vec_alloc (canonical_template_parms, idx + 1);
4082 if (canonical_template_parms->length () <= (unsigned) idx)
4083 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4085 list = (*canonical_template_parms)[idx];
4086 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4087 list = TREE_CHAIN (list);
4089 if (list)
4090 return TREE_VALUE (list);
4091 else
4093 (*canonical_template_parms)[idx]
4094 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4095 return type;
4099 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4100 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4101 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4102 new one is created. */
4104 static tree
4105 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4106 tsubst_flags_t complain)
4108 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4109 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4110 != TEMPLATE_PARM_LEVEL (index) - levels)
4111 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4113 tree orig_decl = TEMPLATE_PARM_DECL (index);
4114 tree decl, t;
4116 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4117 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4118 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4119 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4120 DECL_ARTIFICIAL (decl) = 1;
4121 SET_DECL_TEMPLATE_PARM_P (decl);
4123 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4124 TEMPLATE_PARM_LEVEL (index) - levels,
4125 TEMPLATE_PARM_ORIG_LEVEL (index),
4126 decl, type);
4127 TEMPLATE_PARM_DESCENDANTS (index) = t;
4128 TEMPLATE_PARM_PARAMETER_PACK (t)
4129 = TEMPLATE_PARM_PARAMETER_PACK (index);
4131 /* Template template parameters need this. */
4132 if (TREE_CODE (decl) == TEMPLATE_DECL)
4134 DECL_TEMPLATE_RESULT (decl)
4135 = build_decl (DECL_SOURCE_LOCATION (decl),
4136 TYPE_DECL, DECL_NAME (decl), type);
4137 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4138 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4139 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4143 return TEMPLATE_PARM_DESCENDANTS (index);
4146 /* Process information from new template parameter PARM and append it
4147 to the LIST being built. This new parameter is a non-type
4148 parameter iff IS_NON_TYPE is true. This new parameter is a
4149 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4150 is in PARM_LOC. */
4152 tree
4153 process_template_parm (tree list, location_t parm_loc, tree parm,
4154 bool is_non_type, bool is_parameter_pack)
4156 tree decl = 0;
4157 int idx = 0;
4159 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4160 tree defval = TREE_PURPOSE (parm);
4161 tree constr = TREE_TYPE (parm);
4163 if (list)
4165 tree p = tree_last (list);
4167 if (p && TREE_VALUE (p) != error_mark_node)
4169 p = TREE_VALUE (p);
4170 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4171 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4172 else
4173 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4176 ++idx;
4179 if (is_non_type)
4181 parm = TREE_VALUE (parm);
4183 SET_DECL_TEMPLATE_PARM_P (parm);
4185 if (TREE_TYPE (parm) != error_mark_node)
4187 /* [temp.param]
4189 The top-level cv-qualifiers on the template-parameter are
4190 ignored when determining its type. */
4191 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4192 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4193 TREE_TYPE (parm) = error_mark_node;
4194 else if (uses_parameter_packs (TREE_TYPE (parm))
4195 && !is_parameter_pack
4196 /* If we're in a nested template parameter list, the template
4197 template parameter could be a parameter pack. */
4198 && processing_template_parmlist == 1)
4200 /* This template parameter is not a parameter pack, but it
4201 should be. Complain about "bare" parameter packs. */
4202 check_for_bare_parameter_packs (TREE_TYPE (parm));
4204 /* Recover by calling this a parameter pack. */
4205 is_parameter_pack = true;
4209 /* A template parameter is not modifiable. */
4210 TREE_CONSTANT (parm) = 1;
4211 TREE_READONLY (parm) = 1;
4212 decl = build_decl (parm_loc,
4213 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4214 TREE_CONSTANT (decl) = 1;
4215 TREE_READONLY (decl) = 1;
4216 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4217 = build_template_parm_index (idx, processing_template_decl,
4218 processing_template_decl,
4219 decl, TREE_TYPE (parm));
4221 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4222 = is_parameter_pack;
4224 else
4226 tree t;
4227 parm = TREE_VALUE (TREE_VALUE (parm));
4229 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4231 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4232 /* This is for distinguishing between real templates and template
4233 template parameters */
4234 TREE_TYPE (parm) = t;
4235 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4236 decl = parm;
4238 else
4240 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4241 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4242 decl = build_decl (parm_loc,
4243 TYPE_DECL, parm, t);
4246 TYPE_NAME (t) = decl;
4247 TYPE_STUB_DECL (t) = decl;
4248 parm = decl;
4249 TEMPLATE_TYPE_PARM_INDEX (t)
4250 = build_template_parm_index (idx, processing_template_decl,
4251 processing_template_decl,
4252 decl, TREE_TYPE (parm));
4253 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4254 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4256 DECL_ARTIFICIAL (decl) = 1;
4257 SET_DECL_TEMPLATE_PARM_P (decl);
4259 /* Build requirements for the type/template parameter.
4260 This must be done after SET_DECL_TEMPLATE_PARM_P or
4261 process_template_parm could fail. */
4262 tree reqs = finish_shorthand_constraint (parm, constr);
4264 pushdecl (decl);
4266 /* Build the parameter node linking the parameter declaration,
4267 its default argument (if any), and its constraints (if any). */
4268 parm = build_tree_list (defval, parm);
4269 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4271 return chainon (list, parm);
4274 /* The end of a template parameter list has been reached. Process the
4275 tree list into a parameter vector, converting each parameter into a more
4276 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4277 as PARM_DECLs. */
4279 tree
4280 end_template_parm_list (tree parms)
4282 int nparms;
4283 tree parm, next;
4284 tree saved_parmlist = make_tree_vec (list_length (parms));
4286 /* Pop the dummy parameter level and add the real one. */
4287 current_template_parms = TREE_CHAIN (current_template_parms);
4289 current_template_parms
4290 = tree_cons (size_int (processing_template_decl),
4291 saved_parmlist, current_template_parms);
4293 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4295 next = TREE_CHAIN (parm);
4296 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4297 TREE_CHAIN (parm) = NULL_TREE;
4300 --processing_template_parmlist;
4302 return saved_parmlist;
4305 // Explicitly indicate the end of the template parameter list. We assume
4306 // that the current template parameters have been constructed and/or
4307 // managed explicitly, as when creating new template template parameters
4308 // from a shorthand constraint.
4309 void
4310 end_template_parm_list ()
4312 --processing_template_parmlist;
4315 /* end_template_decl is called after a template declaration is seen. */
4317 void
4318 end_template_decl (void)
4320 reset_specialization ();
4322 if (! processing_template_decl)
4323 return;
4325 /* This matches the pushlevel in begin_template_parm_list. */
4326 finish_scope ();
4328 --processing_template_decl;
4329 current_template_parms = TREE_CHAIN (current_template_parms);
4332 /* Takes a TREE_LIST representing a template parameter and convert it
4333 into an argument suitable to be passed to the type substitution
4334 functions. Note that If the TREE_LIST contains an error_mark
4335 node, the returned argument is error_mark_node. */
4337 tree
4338 template_parm_to_arg (tree t)
4341 if (t == NULL_TREE
4342 || TREE_CODE (t) != TREE_LIST)
4343 return t;
4345 if (error_operand_p (TREE_VALUE (t)))
4346 return error_mark_node;
4348 t = TREE_VALUE (t);
4350 if (TREE_CODE (t) == TYPE_DECL
4351 || TREE_CODE (t) == TEMPLATE_DECL)
4353 t = TREE_TYPE (t);
4355 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4357 /* Turn this argument into a TYPE_ARGUMENT_PACK
4358 with a single element, which expands T. */
4359 tree vec = make_tree_vec (1);
4360 if (CHECKING_P)
4361 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4363 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4365 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4366 SET_ARGUMENT_PACK_ARGS (t, vec);
4369 else
4371 t = DECL_INITIAL (t);
4373 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4375 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4376 with a single element, which expands T. */
4377 tree vec = make_tree_vec (1);
4378 if (CHECKING_P)
4379 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4381 t = convert_from_reference (t);
4382 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4384 t = make_node (NONTYPE_ARGUMENT_PACK);
4385 SET_ARGUMENT_PACK_ARGS (t, vec);
4387 else
4388 t = convert_from_reference (t);
4390 return t;
4393 /* Given a single level of template parameters (a TREE_VEC), return it
4394 as a set of template arguments. */
4396 static tree
4397 template_parms_level_to_args (tree parms)
4399 tree a = copy_node (parms);
4400 TREE_TYPE (a) = NULL_TREE;
4401 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4402 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4404 if (CHECKING_P)
4405 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4407 return a;
4410 /* Given a set of template parameters, return them as a set of template
4411 arguments. The template parameters are represented as a TREE_VEC, in
4412 the form documented in cp-tree.h for template arguments. */
4414 static tree
4415 template_parms_to_args (tree parms)
4417 tree header;
4418 tree args = NULL_TREE;
4419 int length = TMPL_PARMS_DEPTH (parms);
4420 int l = length;
4422 /* If there is only one level of template parameters, we do not
4423 create a TREE_VEC of TREE_VECs. Instead, we return a single
4424 TREE_VEC containing the arguments. */
4425 if (length > 1)
4426 args = make_tree_vec (length);
4428 for (header = parms; header; header = TREE_CHAIN (header))
4430 tree a = template_parms_level_to_args (TREE_VALUE (header));
4432 if (length > 1)
4433 TREE_VEC_ELT (args, --l) = a;
4434 else
4435 args = a;
4438 return args;
4441 /* Within the declaration of a template, return the currently active
4442 template parameters as an argument TREE_VEC. */
4444 static tree
4445 current_template_args (void)
4447 return template_parms_to_args (current_template_parms);
4450 /* Update the declared TYPE by doing any lookups which were thought to be
4451 dependent, but are not now that we know the SCOPE of the declarator. */
4453 tree
4454 maybe_update_decl_type (tree orig_type, tree scope)
4456 tree type = orig_type;
4458 if (type == NULL_TREE)
4459 return type;
4461 if (TREE_CODE (orig_type) == TYPE_DECL)
4462 type = TREE_TYPE (type);
4464 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4465 && dependent_type_p (type)
4466 /* Don't bother building up the args in this case. */
4467 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4469 /* tsubst in the args corresponding to the template parameters,
4470 including auto if present. Most things will be unchanged, but
4471 make_typename_type and tsubst_qualified_id will resolve
4472 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4473 tree args = current_template_args ();
4474 tree auto_node = type_uses_auto (type);
4475 tree pushed;
4476 if (auto_node)
4478 tree auto_vec = make_tree_vec (1);
4479 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4480 args = add_to_template_args (args, auto_vec);
4482 pushed = push_scope (scope);
4483 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4484 if (pushed)
4485 pop_scope (scope);
4488 if (type == error_mark_node)
4489 return orig_type;
4491 if (TREE_CODE (orig_type) == TYPE_DECL)
4493 if (same_type_p (type, TREE_TYPE (orig_type)))
4494 type = orig_type;
4495 else
4496 type = TYPE_NAME (type);
4498 return type;
4501 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4502 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4503 the new template is a member template. */
4505 tree
4506 build_template_decl (tree decl, tree parms, bool member_template_p)
4508 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4509 DECL_TEMPLATE_PARMS (tmpl) = parms;
4510 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4511 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4512 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4514 return tmpl;
4517 struct template_parm_data
4519 /* The level of the template parameters we are currently
4520 processing. */
4521 int level;
4523 /* The index of the specialization argument we are currently
4524 processing. */
4525 int current_arg;
4527 /* An array whose size is the number of template parameters. The
4528 elements are nonzero if the parameter has been used in any one
4529 of the arguments processed so far. */
4530 int* parms;
4532 /* An array whose size is the number of template arguments. The
4533 elements are nonzero if the argument makes use of template
4534 parameters of this level. */
4535 int* arg_uses_template_parms;
4538 /* Subroutine of push_template_decl used to see if each template
4539 parameter in a partial specialization is used in the explicit
4540 argument list. If T is of the LEVEL given in DATA (which is
4541 treated as a template_parm_data*), then DATA->PARMS is marked
4542 appropriately. */
4544 static int
4545 mark_template_parm (tree t, void* data)
4547 int level;
4548 int idx;
4549 struct template_parm_data* tpd = (struct template_parm_data*) data;
4551 template_parm_level_and_index (t, &level, &idx);
4553 if (level == tpd->level)
4555 tpd->parms[idx] = 1;
4556 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4559 /* In C++17 the type of a non-type argument is a deduced context. */
4560 if (cxx_dialect >= cxx17
4561 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4562 for_each_template_parm (TREE_TYPE (t),
4563 &mark_template_parm,
4564 data,
4565 NULL,
4566 /*include_nondeduced_p=*/false);
4568 /* Return zero so that for_each_template_parm will continue the
4569 traversal of the tree; we want to mark *every* template parm. */
4570 return 0;
4573 /* Process the partial specialization DECL. */
4575 static tree
4576 process_partial_specialization (tree decl)
4578 tree type = TREE_TYPE (decl);
4579 tree tinfo = get_template_info (decl);
4580 tree maintmpl = TI_TEMPLATE (tinfo);
4581 tree specargs = TI_ARGS (tinfo);
4582 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4583 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4584 tree inner_parms;
4585 tree inst;
4586 int nargs = TREE_VEC_LENGTH (inner_args);
4587 int ntparms;
4588 int i;
4589 bool did_error_intro = false;
4590 struct template_parm_data tpd;
4591 struct template_parm_data tpd2;
4593 gcc_assert (current_template_parms);
4595 /* A concept cannot be specialized. */
4596 if (flag_concepts && variable_concept_p (maintmpl))
4598 error ("specialization of variable concept %q#D", maintmpl);
4599 return error_mark_node;
4602 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4603 ntparms = TREE_VEC_LENGTH (inner_parms);
4605 /* We check that each of the template parameters given in the
4606 partial specialization is used in the argument list to the
4607 specialization. For example:
4609 template <class T> struct S;
4610 template <class T> struct S<T*>;
4612 The second declaration is OK because `T*' uses the template
4613 parameter T, whereas
4615 template <class T> struct S<int>;
4617 is no good. Even trickier is:
4619 template <class T>
4620 struct S1
4622 template <class U>
4623 struct S2;
4624 template <class U>
4625 struct S2<T>;
4628 The S2<T> declaration is actually invalid; it is a
4629 full-specialization. Of course,
4631 template <class U>
4632 struct S2<T (*)(U)>;
4634 or some such would have been OK. */
4635 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4636 tpd.parms = XALLOCAVEC (int, ntparms);
4637 memset (tpd.parms, 0, sizeof (int) * ntparms);
4639 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4640 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4641 for (i = 0; i < nargs; ++i)
4643 tpd.current_arg = i;
4644 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4645 &mark_template_parm,
4646 &tpd,
4647 NULL,
4648 /*include_nondeduced_p=*/false);
4650 for (i = 0; i < ntparms; ++i)
4651 if (tpd.parms[i] == 0)
4653 /* One of the template parms was not used in a deduced context in the
4654 specialization. */
4655 if (!did_error_intro)
4657 error ("template parameters not deducible in "
4658 "partial specialization:");
4659 did_error_intro = true;
4662 inform (input_location, " %qD",
4663 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4666 if (did_error_intro)
4667 return error_mark_node;
4669 /* [temp.class.spec]
4671 The argument list of the specialization shall not be identical to
4672 the implicit argument list of the primary template. */
4673 tree main_args
4674 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4675 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4676 && (!flag_concepts
4677 || !strictly_subsumes (current_template_constraints (),
4678 get_constraints (maintmpl))))
4680 if (!flag_concepts)
4681 error ("partial specialization %q+D does not specialize "
4682 "any template arguments", decl);
4683 else
4684 error ("partial specialization %q+D does not specialize any "
4685 "template arguments and is not more constrained than", decl);
4686 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4689 /* A partial specialization that replaces multiple parameters of the
4690 primary template with a pack expansion is less specialized for those
4691 parameters. */
4692 if (nargs < DECL_NTPARMS (maintmpl))
4694 error ("partial specialization is not more specialized than the "
4695 "primary template because it replaces multiple parameters "
4696 "with a pack expansion");
4697 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4698 /* Avoid crash in process_partial_specialization. */
4699 return decl;
4702 /* If we aren't in a dependent class, we can actually try deduction. */
4703 else if (tpd.level == 1
4704 /* FIXME we should be able to handle a partial specialization of a
4705 partial instantiation, but currently we can't (c++/41727). */
4706 && TMPL_ARGS_DEPTH (specargs) == 1
4707 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4709 if (permerror (input_location, "partial specialization %qD is not "
4710 "more specialized than", decl))
4711 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4712 maintmpl);
4715 /* [temp.class.spec]
4717 A partially specialized non-type argument expression shall not
4718 involve template parameters of the partial specialization except
4719 when the argument expression is a simple identifier.
4721 The type of a template parameter corresponding to a specialized
4722 non-type argument shall not be dependent on a parameter of the
4723 specialization.
4725 Also, we verify that pack expansions only occur at the
4726 end of the argument list. */
4727 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4728 tpd2.parms = 0;
4729 for (i = 0; i < nargs; ++i)
4731 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4732 tree arg = TREE_VEC_ELT (inner_args, i);
4733 tree packed_args = NULL_TREE;
4734 int j, len = 1;
4736 if (ARGUMENT_PACK_P (arg))
4738 /* Extract the arguments from the argument pack. We'll be
4739 iterating over these in the following loop. */
4740 packed_args = ARGUMENT_PACK_ARGS (arg);
4741 len = TREE_VEC_LENGTH (packed_args);
4744 for (j = 0; j < len; j++)
4746 if (packed_args)
4747 /* Get the Jth argument in the parameter pack. */
4748 arg = TREE_VEC_ELT (packed_args, j);
4750 if (PACK_EXPANSION_P (arg))
4752 /* Pack expansions must come at the end of the
4753 argument list. */
4754 if ((packed_args && j < len - 1)
4755 || (!packed_args && i < nargs - 1))
4757 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4758 error ("parameter pack argument %qE must be at the "
4759 "end of the template argument list", arg);
4760 else
4761 error ("parameter pack argument %qT must be at the "
4762 "end of the template argument list", arg);
4766 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4767 /* We only care about the pattern. */
4768 arg = PACK_EXPANSION_PATTERN (arg);
4770 if (/* These first two lines are the `non-type' bit. */
4771 !TYPE_P (arg)
4772 && TREE_CODE (arg) != TEMPLATE_DECL
4773 /* This next two lines are the `argument expression is not just a
4774 simple identifier' condition and also the `specialized
4775 non-type argument' bit. */
4776 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4777 && !(REFERENCE_REF_P (arg)
4778 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4780 if ((!packed_args && tpd.arg_uses_template_parms[i])
4781 || (packed_args && uses_template_parms (arg)))
4782 error ("template argument %qE involves template parameter(s)",
4783 arg);
4784 else
4786 /* Look at the corresponding template parameter,
4787 marking which template parameters its type depends
4788 upon. */
4789 tree type = TREE_TYPE (parm);
4791 if (!tpd2.parms)
4793 /* We haven't yet initialized TPD2. Do so now. */
4794 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4795 /* The number of parameters here is the number in the
4796 main template, which, as checked in the assertion
4797 above, is NARGS. */
4798 tpd2.parms = XALLOCAVEC (int, nargs);
4799 tpd2.level =
4800 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4803 /* Mark the template parameters. But this time, we're
4804 looking for the template parameters of the main
4805 template, not in the specialization. */
4806 tpd2.current_arg = i;
4807 tpd2.arg_uses_template_parms[i] = 0;
4808 memset (tpd2.parms, 0, sizeof (int) * nargs);
4809 for_each_template_parm (type,
4810 &mark_template_parm,
4811 &tpd2,
4812 NULL,
4813 /*include_nondeduced_p=*/false);
4815 if (tpd2.arg_uses_template_parms [i])
4817 /* The type depended on some template parameters.
4818 If they are fully specialized in the
4819 specialization, that's OK. */
4820 int j;
4821 int count = 0;
4822 for (j = 0; j < nargs; ++j)
4823 if (tpd2.parms[j] != 0
4824 && tpd.arg_uses_template_parms [j])
4825 ++count;
4826 if (count != 0)
4827 error_n (input_location, count,
4828 "type %qT of template argument %qE depends "
4829 "on a template parameter",
4830 "type %qT of template argument %qE depends "
4831 "on template parameters",
4832 type,
4833 arg);
4840 /* We should only get here once. */
4841 if (TREE_CODE (decl) == TYPE_DECL)
4842 gcc_assert (!COMPLETE_TYPE_P (type));
4844 // Build the template decl.
4845 tree tmpl = build_template_decl (decl, current_template_parms,
4846 DECL_MEMBER_TEMPLATE_P (maintmpl));
4847 TREE_TYPE (tmpl) = type;
4848 DECL_TEMPLATE_RESULT (tmpl) = decl;
4849 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4850 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4851 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4853 /* Give template template parms a DECL_CONTEXT of the template
4854 for which they are a parameter. */
4855 for (i = 0; i < ntparms; ++i)
4857 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4858 if (TREE_CODE (parm) == TEMPLATE_DECL)
4859 DECL_CONTEXT (parm) = tmpl;
4862 if (VAR_P (decl))
4863 /* We didn't register this in check_explicit_specialization so we could
4864 wait until the constraints were set. */
4865 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4866 else
4867 associate_classtype_constraints (type);
4869 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4870 = tree_cons (specargs, tmpl,
4871 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4872 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4874 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4875 inst = TREE_CHAIN (inst))
4877 tree instance = TREE_VALUE (inst);
4878 if (TYPE_P (instance)
4879 ? (COMPLETE_TYPE_P (instance)
4880 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4881 : DECL_TEMPLATE_INSTANTIATION (instance))
4883 tree spec = most_specialized_partial_spec (instance, tf_none);
4884 tree inst_decl = (DECL_P (instance)
4885 ? instance : TYPE_NAME (instance));
4886 if (!spec)
4887 /* OK */;
4888 else if (spec == error_mark_node)
4889 permerror (input_location,
4890 "declaration of %qD ambiguates earlier template "
4891 "instantiation for %qD", decl, inst_decl);
4892 else if (TREE_VALUE (spec) == tmpl)
4893 permerror (input_location,
4894 "partial specialization of %qD after instantiation "
4895 "of %qD", decl, inst_decl);
4899 return decl;
4902 /* PARM is a template parameter of some form; return the corresponding
4903 TEMPLATE_PARM_INDEX. */
4905 static tree
4906 get_template_parm_index (tree parm)
4908 if (TREE_CODE (parm) == PARM_DECL
4909 || TREE_CODE (parm) == CONST_DECL)
4910 parm = DECL_INITIAL (parm);
4911 else if (TREE_CODE (parm) == TYPE_DECL
4912 || TREE_CODE (parm) == TEMPLATE_DECL)
4913 parm = TREE_TYPE (parm);
4914 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4915 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4916 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4917 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4918 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4919 return parm;
4922 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4923 parameter packs used by the template parameter PARM. */
4925 static void
4926 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4928 /* A type parm can't refer to another parm. */
4929 if (TREE_CODE (parm) == TYPE_DECL)
4930 return;
4931 else if (TREE_CODE (parm) == PARM_DECL)
4933 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4934 ppd, ppd->visited);
4935 return;
4938 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4940 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4941 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4942 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4945 /* PARM is a template parameter pack. Return any parameter packs used in
4946 its type or the type of any of its template parameters. If there are
4947 any such packs, it will be instantiated into a fixed template parameter
4948 list by partial instantiation rather than be fully deduced. */
4950 tree
4951 fixed_parameter_pack_p (tree parm)
4953 /* This can only be true in a member template. */
4954 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4955 return NULL_TREE;
4956 /* This can only be true for a parameter pack. */
4957 if (!template_parameter_pack_p (parm))
4958 return NULL_TREE;
4959 /* A type parm can't refer to another parm. */
4960 if (TREE_CODE (parm) == TYPE_DECL)
4961 return NULL_TREE;
4963 tree parameter_packs = NULL_TREE;
4964 struct find_parameter_pack_data ppd;
4965 ppd.parameter_packs = &parameter_packs;
4966 ppd.visited = new hash_set<tree>;
4967 ppd.type_pack_expansion_p = false;
4969 fixed_parameter_pack_p_1 (parm, &ppd);
4971 delete ppd.visited;
4972 return parameter_packs;
4975 /* Check that a template declaration's use of default arguments and
4976 parameter packs is not invalid. Here, PARMS are the template
4977 parameters. IS_PRIMARY is true if DECL is the thing declared by
4978 a primary template. IS_PARTIAL is true if DECL is a partial
4979 specialization.
4981 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4982 declaration (but not a definition); 1 indicates a declaration, 2
4983 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4984 emitted for extraneous default arguments.
4986 Returns TRUE if there were no errors found, FALSE otherwise. */
4988 bool
4989 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4990 bool is_partial, int is_friend_decl)
4992 const char *msg;
4993 int last_level_to_check;
4994 tree parm_level;
4995 bool no_errors = true;
4997 /* [temp.param]
4999 A default template-argument shall not be specified in a
5000 function template declaration or a function template definition, nor
5001 in the template-parameter-list of the definition of a member of a
5002 class template. */
5004 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5005 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5006 /* You can't have a function template declaration in a local
5007 scope, nor you can you define a member of a class template in a
5008 local scope. */
5009 return true;
5011 if ((TREE_CODE (decl) == TYPE_DECL
5012 && TREE_TYPE (decl)
5013 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5014 || (TREE_CODE (decl) == FUNCTION_DECL
5015 && LAMBDA_FUNCTION_P (decl)))
5016 /* A lambda doesn't have an explicit declaration; don't complain
5017 about the parms of the enclosing class. */
5018 return true;
5020 if (current_class_type
5021 && !TYPE_BEING_DEFINED (current_class_type)
5022 && DECL_LANG_SPECIFIC (decl)
5023 && DECL_DECLARES_FUNCTION_P (decl)
5024 /* If this is either a friend defined in the scope of the class
5025 or a member function. */
5026 && (DECL_FUNCTION_MEMBER_P (decl)
5027 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5028 : DECL_FRIEND_CONTEXT (decl)
5029 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5030 : false)
5031 /* And, if it was a member function, it really was defined in
5032 the scope of the class. */
5033 && (!DECL_FUNCTION_MEMBER_P (decl)
5034 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5035 /* We already checked these parameters when the template was
5036 declared, so there's no need to do it again now. This function
5037 was defined in class scope, but we're processing its body now
5038 that the class is complete. */
5039 return true;
5041 /* Core issue 226 (C++0x only): the following only applies to class
5042 templates. */
5043 if (is_primary
5044 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5046 /* [temp.param]
5048 If a template-parameter has a default template-argument, all
5049 subsequent template-parameters shall have a default
5050 template-argument supplied. */
5051 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5053 tree inner_parms = TREE_VALUE (parm_level);
5054 int ntparms = TREE_VEC_LENGTH (inner_parms);
5055 int seen_def_arg_p = 0;
5056 int i;
5058 for (i = 0; i < ntparms; ++i)
5060 tree parm = TREE_VEC_ELT (inner_parms, i);
5062 if (parm == error_mark_node)
5063 continue;
5065 if (TREE_PURPOSE (parm))
5066 seen_def_arg_p = 1;
5067 else if (seen_def_arg_p
5068 && !template_parameter_pack_p (TREE_VALUE (parm)))
5070 error ("no default argument for %qD", TREE_VALUE (parm));
5071 /* For better subsequent error-recovery, we indicate that
5072 there should have been a default argument. */
5073 TREE_PURPOSE (parm) = error_mark_node;
5074 no_errors = false;
5076 else if (!is_partial
5077 && !is_friend_decl
5078 /* Don't complain about an enclosing partial
5079 specialization. */
5080 && parm_level == parms
5081 && TREE_CODE (decl) == TYPE_DECL
5082 && i < ntparms - 1
5083 && template_parameter_pack_p (TREE_VALUE (parm))
5084 /* A fixed parameter pack will be partially
5085 instantiated into a fixed length list. */
5086 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5088 /* A primary class template can only have one
5089 parameter pack, at the end of the template
5090 parameter list. */
5092 error ("parameter pack %q+D must be at the end of the"
5093 " template parameter list", TREE_VALUE (parm));
5095 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5096 = error_mark_node;
5097 no_errors = false;
5103 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5104 || is_partial
5105 || !is_primary
5106 || is_friend_decl)
5107 /* For an ordinary class template, default template arguments are
5108 allowed at the innermost level, e.g.:
5109 template <class T = int>
5110 struct S {};
5111 but, in a partial specialization, they're not allowed even
5112 there, as we have in [temp.class.spec]:
5114 The template parameter list of a specialization shall not
5115 contain default template argument values.
5117 So, for a partial specialization, or for a function template
5118 (in C++98/C++03), we look at all of them. */
5120 else
5121 /* But, for a primary class template that is not a partial
5122 specialization we look at all template parameters except the
5123 innermost ones. */
5124 parms = TREE_CHAIN (parms);
5126 /* Figure out what error message to issue. */
5127 if (is_friend_decl == 2)
5128 msg = G_("default template arguments may not be used in function template "
5129 "friend re-declaration");
5130 else if (is_friend_decl)
5131 msg = G_("default template arguments may not be used in function template "
5132 "friend declarations");
5133 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5134 msg = G_("default template arguments may not be used in function templates "
5135 "without -std=c++11 or -std=gnu++11");
5136 else if (is_partial)
5137 msg = G_("default template arguments may not be used in "
5138 "partial specializations");
5139 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5140 msg = G_("default argument for template parameter for class enclosing %qD");
5141 else
5142 /* Per [temp.param]/9, "A default template-argument shall not be
5143 specified in the template-parameter-lists of the definition of
5144 a member of a class template that appears outside of the member's
5145 class.", thus if we aren't handling a member of a class template
5146 there is no need to examine the parameters. */
5147 return true;
5149 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5150 /* If we're inside a class definition, there's no need to
5151 examine the parameters to the class itself. On the one
5152 hand, they will be checked when the class is defined, and,
5153 on the other, default arguments are valid in things like:
5154 template <class T = double>
5155 struct S { template <class U> void f(U); };
5156 Here the default argument for `S' has no bearing on the
5157 declaration of `f'. */
5158 last_level_to_check = template_class_depth (current_class_type) + 1;
5159 else
5160 /* Check everything. */
5161 last_level_to_check = 0;
5163 for (parm_level = parms;
5164 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5165 parm_level = TREE_CHAIN (parm_level))
5167 tree inner_parms = TREE_VALUE (parm_level);
5168 int i;
5169 int ntparms;
5171 ntparms = TREE_VEC_LENGTH (inner_parms);
5172 for (i = 0; i < ntparms; ++i)
5174 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5175 continue;
5177 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5179 if (msg)
5181 no_errors = false;
5182 if (is_friend_decl == 2)
5183 return no_errors;
5185 error (msg, decl);
5186 msg = 0;
5189 /* Clear out the default argument so that we are not
5190 confused later. */
5191 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5195 /* At this point, if we're still interested in issuing messages,
5196 they must apply to classes surrounding the object declared. */
5197 if (msg)
5198 msg = G_("default argument for template parameter for class "
5199 "enclosing %qD");
5202 return no_errors;
5205 /* Worker for push_template_decl_real, called via
5206 for_each_template_parm. DATA is really an int, indicating the
5207 level of the parameters we are interested in. If T is a template
5208 parameter of that level, return nonzero. */
5210 static int
5211 template_parm_this_level_p (tree t, void* data)
5213 int this_level = *(int *)data;
5214 int level;
5216 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5217 level = TEMPLATE_PARM_LEVEL (t);
5218 else
5219 level = TEMPLATE_TYPE_LEVEL (t);
5220 return level == this_level;
5223 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5224 DATA is really an int, indicating the innermost outer level of parameters.
5225 If T is a template parameter of that level or further out, return
5226 nonzero. */
5228 static int
5229 template_parm_outer_level (tree t, void *data)
5231 int this_level = *(int *)data;
5232 int level;
5234 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5235 level = TEMPLATE_PARM_LEVEL (t);
5236 else
5237 level = TEMPLATE_TYPE_LEVEL (t);
5238 return level <= this_level;
5241 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5242 parameters given by current_template_args, or reuses a
5243 previously existing one, if appropriate. Returns the DECL, or an
5244 equivalent one, if it is replaced via a call to duplicate_decls.
5246 If IS_FRIEND is true, DECL is a friend declaration. */
5248 tree
5249 push_template_decl_real (tree decl, bool is_friend)
5251 tree tmpl;
5252 tree args;
5253 tree info;
5254 tree ctx;
5255 bool is_primary;
5256 bool is_partial;
5257 int new_template_p = 0;
5258 /* True if the template is a member template, in the sense of
5259 [temp.mem]. */
5260 bool member_template_p = false;
5262 if (decl == error_mark_node || !current_template_parms)
5263 return error_mark_node;
5265 /* See if this is a partial specialization. */
5266 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5267 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5268 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5269 || (VAR_P (decl)
5270 && DECL_LANG_SPECIFIC (decl)
5271 && DECL_TEMPLATE_SPECIALIZATION (decl)
5272 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5274 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5275 is_friend = true;
5277 if (is_friend)
5278 /* For a friend, we want the context of the friend function, not
5279 the type of which it is a friend. */
5280 ctx = CP_DECL_CONTEXT (decl);
5281 else if (CP_DECL_CONTEXT (decl)
5282 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5283 /* In the case of a virtual function, we want the class in which
5284 it is defined. */
5285 ctx = CP_DECL_CONTEXT (decl);
5286 else
5287 /* Otherwise, if we're currently defining some class, the DECL
5288 is assumed to be a member of the class. */
5289 ctx = current_scope ();
5291 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5292 ctx = NULL_TREE;
5294 if (!DECL_CONTEXT (decl))
5295 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5297 /* See if this is a primary template. */
5298 if (is_friend && ctx
5299 && uses_template_parms_level (ctx, processing_template_decl))
5300 /* A friend template that specifies a class context, i.e.
5301 template <typename T> friend void A<T>::f();
5302 is not primary. */
5303 is_primary = false;
5304 else if (TREE_CODE (decl) == TYPE_DECL
5305 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5306 is_primary = false;
5307 else
5308 is_primary = template_parm_scope_p ();
5310 if (is_primary)
5312 warning (OPT_Wtemplates, "template %qD declared", decl);
5314 if (DECL_CLASS_SCOPE_P (decl))
5315 member_template_p = true;
5316 if (TREE_CODE (decl) == TYPE_DECL
5317 && anon_aggrname_p (DECL_NAME (decl)))
5319 error ("template class without a name");
5320 return error_mark_node;
5322 else if (TREE_CODE (decl) == FUNCTION_DECL)
5324 if (member_template_p)
5326 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5327 error ("member template %qD may not have virt-specifiers", decl);
5329 if (DECL_DESTRUCTOR_P (decl))
5331 /* [temp.mem]
5333 A destructor shall not be a member template. */
5334 error ("destructor %qD declared as member template", decl);
5335 return error_mark_node;
5337 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5338 && (!prototype_p (TREE_TYPE (decl))
5339 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5340 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5341 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5342 == void_list_node)))
5344 /* [basic.stc.dynamic.allocation]
5346 An allocation function can be a function
5347 template. ... Template allocation functions shall
5348 have two or more parameters. */
5349 error ("invalid template declaration of %qD", decl);
5350 return error_mark_node;
5353 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5354 && CLASS_TYPE_P (TREE_TYPE (decl)))
5356 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5357 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5358 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5360 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5361 if (TREE_CODE (t) == TYPE_DECL)
5362 t = TREE_TYPE (t);
5363 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5364 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5367 else if (TREE_CODE (decl) == TYPE_DECL
5368 && TYPE_DECL_ALIAS_P (decl))
5369 /* alias-declaration */
5370 gcc_assert (!DECL_ARTIFICIAL (decl));
5371 else if (VAR_P (decl))
5372 /* C++14 variable template. */;
5373 else
5375 error ("template declaration of %q#D", decl);
5376 return error_mark_node;
5380 /* Check to see that the rules regarding the use of default
5381 arguments are not being violated. */
5382 check_default_tmpl_args (decl, current_template_parms,
5383 is_primary, is_partial, /*is_friend_decl=*/0);
5385 /* Ensure that there are no parameter packs in the type of this
5386 declaration that have not been expanded. */
5387 if (TREE_CODE (decl) == FUNCTION_DECL)
5389 /* Check each of the arguments individually to see if there are
5390 any bare parameter packs. */
5391 tree type = TREE_TYPE (decl);
5392 tree arg = DECL_ARGUMENTS (decl);
5393 tree argtype = TYPE_ARG_TYPES (type);
5395 while (arg && argtype)
5397 if (!DECL_PACK_P (arg)
5398 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5400 /* This is a PARM_DECL that contains unexpanded parameter
5401 packs. We have already complained about this in the
5402 check_for_bare_parameter_packs call, so just replace
5403 these types with ERROR_MARK_NODE. */
5404 TREE_TYPE (arg) = error_mark_node;
5405 TREE_VALUE (argtype) = error_mark_node;
5408 arg = DECL_CHAIN (arg);
5409 argtype = TREE_CHAIN (argtype);
5412 /* Check for bare parameter packs in the return type and the
5413 exception specifiers. */
5414 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5415 /* Errors were already issued, set return type to int
5416 as the frontend doesn't expect error_mark_node as
5417 the return type. */
5418 TREE_TYPE (type) = integer_type_node;
5419 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5420 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5422 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5423 && TYPE_DECL_ALIAS_P (decl))
5424 ? DECL_ORIGINAL_TYPE (decl)
5425 : TREE_TYPE (decl)))
5427 TREE_TYPE (decl) = error_mark_node;
5428 return error_mark_node;
5431 if (is_partial)
5432 return process_partial_specialization (decl);
5434 args = current_template_args ();
5436 if (!ctx
5437 || TREE_CODE (ctx) == FUNCTION_DECL
5438 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5439 || (TREE_CODE (decl) == TYPE_DECL
5440 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5441 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5443 if (DECL_LANG_SPECIFIC (decl)
5444 && DECL_TEMPLATE_INFO (decl)
5445 && DECL_TI_TEMPLATE (decl))
5446 tmpl = DECL_TI_TEMPLATE (decl);
5447 /* If DECL is a TYPE_DECL for a class-template, then there won't
5448 be DECL_LANG_SPECIFIC. The information equivalent to
5449 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5450 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5451 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5452 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5454 /* Since a template declaration already existed for this
5455 class-type, we must be redeclaring it here. Make sure
5456 that the redeclaration is valid. */
5457 redeclare_class_template (TREE_TYPE (decl),
5458 current_template_parms,
5459 current_template_constraints ());
5460 /* We don't need to create a new TEMPLATE_DECL; just use the
5461 one we already had. */
5462 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5464 else
5466 tmpl = build_template_decl (decl, current_template_parms,
5467 member_template_p);
5468 new_template_p = 1;
5470 if (DECL_LANG_SPECIFIC (decl)
5471 && DECL_TEMPLATE_SPECIALIZATION (decl))
5473 /* A specialization of a member template of a template
5474 class. */
5475 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5476 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5477 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5481 else
5483 tree a, t, current, parms;
5484 int i;
5485 tree tinfo = get_template_info (decl);
5487 if (!tinfo)
5489 error ("template definition of non-template %q#D", decl);
5490 return error_mark_node;
5493 tmpl = TI_TEMPLATE (tinfo);
5495 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5496 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5497 && DECL_TEMPLATE_SPECIALIZATION (decl)
5498 && DECL_MEMBER_TEMPLATE_P (tmpl))
5500 tree new_tmpl;
5502 /* The declaration is a specialization of a member
5503 template, declared outside the class. Therefore, the
5504 innermost template arguments will be NULL, so we
5505 replace them with the arguments determined by the
5506 earlier call to check_explicit_specialization. */
5507 args = DECL_TI_ARGS (decl);
5509 new_tmpl
5510 = build_template_decl (decl, current_template_parms,
5511 member_template_p);
5512 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5513 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5514 DECL_TI_TEMPLATE (decl) = new_tmpl;
5515 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5516 DECL_TEMPLATE_INFO (new_tmpl)
5517 = build_template_info (tmpl, args);
5519 register_specialization (new_tmpl,
5520 most_general_template (tmpl),
5521 args,
5522 is_friend, 0);
5523 return decl;
5526 /* Make sure the template headers we got make sense. */
5528 parms = DECL_TEMPLATE_PARMS (tmpl);
5529 i = TMPL_PARMS_DEPTH (parms);
5530 if (TMPL_ARGS_DEPTH (args) != i)
5532 error ("expected %d levels of template parms for %q#D, got %d",
5533 i, decl, TMPL_ARGS_DEPTH (args));
5534 DECL_INTERFACE_KNOWN (decl) = 1;
5535 return error_mark_node;
5537 else
5538 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5540 a = TMPL_ARGS_LEVEL (args, i);
5541 t = INNERMOST_TEMPLATE_PARMS (parms);
5543 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5545 if (current == decl)
5546 error ("got %d template parameters for %q#D",
5547 TREE_VEC_LENGTH (a), decl);
5548 else
5549 error ("got %d template parameters for %q#T",
5550 TREE_VEC_LENGTH (a), current);
5551 error (" but %d required", TREE_VEC_LENGTH (t));
5552 /* Avoid crash in import_export_decl. */
5553 DECL_INTERFACE_KNOWN (decl) = 1;
5554 return error_mark_node;
5557 if (current == decl)
5558 current = ctx;
5559 else if (current == NULL_TREE)
5560 /* Can happen in erroneous input. */
5561 break;
5562 else
5563 current = get_containing_scope (current);
5566 /* Check that the parms are used in the appropriate qualifying scopes
5567 in the declarator. */
5568 if (!comp_template_args
5569 (TI_ARGS (tinfo),
5570 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5572 error ("template arguments to %qD do not match original "
5573 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5574 if (!uses_template_parms (TI_ARGS (tinfo)))
5575 inform (input_location, "use %<template<>%> for"
5576 " an explicit specialization");
5577 /* Avoid crash in import_export_decl. */
5578 DECL_INTERFACE_KNOWN (decl) = 1;
5579 return error_mark_node;
5583 DECL_TEMPLATE_RESULT (tmpl) = decl;
5584 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5586 /* Push template declarations for global functions and types. Note
5587 that we do not try to push a global template friend declared in a
5588 template class; such a thing may well depend on the template
5589 parameters of the class. */
5590 if (new_template_p && !ctx
5591 && !(is_friend && template_class_depth (current_class_type) > 0))
5593 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5594 if (tmpl == error_mark_node)
5595 return error_mark_node;
5597 /* Hide template friend classes that haven't been declared yet. */
5598 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5600 DECL_ANTICIPATED (tmpl) = 1;
5601 DECL_FRIEND_P (tmpl) = 1;
5605 if (is_primary)
5607 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5609 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5611 /* Give template template parms a DECL_CONTEXT of the template
5612 for which they are a parameter. */
5613 parms = INNERMOST_TEMPLATE_PARMS (parms);
5614 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5616 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5617 if (TREE_CODE (parm) == TEMPLATE_DECL)
5618 DECL_CONTEXT (parm) = tmpl;
5621 if (TREE_CODE (decl) == TYPE_DECL
5622 && TYPE_DECL_ALIAS_P (decl)
5623 && complex_alias_template_p (tmpl))
5624 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5627 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5628 back to its most general template. If TMPL is a specialization,
5629 ARGS may only have the innermost set of arguments. Add the missing
5630 argument levels if necessary. */
5631 if (DECL_TEMPLATE_INFO (tmpl))
5632 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5634 info = build_template_info (tmpl, args);
5636 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5637 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5638 else
5640 if (is_primary)
5641 retrofit_lang_decl (decl);
5642 if (DECL_LANG_SPECIFIC (decl))
5643 DECL_TEMPLATE_INFO (decl) = info;
5646 if (flag_implicit_templates
5647 && !is_friend
5648 && TREE_PUBLIC (decl)
5649 && VAR_OR_FUNCTION_DECL_P (decl))
5650 /* Set DECL_COMDAT on template instantiations; if we force
5651 them to be emitted by explicit instantiation or -frepo,
5652 mark_needed will tell cgraph to do the right thing. */
5653 DECL_COMDAT (decl) = true;
5655 return DECL_TEMPLATE_RESULT (tmpl);
5658 tree
5659 push_template_decl (tree decl)
5661 return push_template_decl_real (decl, false);
5664 /* FN is an inheriting constructor that inherits from the constructor
5665 template INHERITED; turn FN into a constructor template with a matching
5666 template header. */
5668 tree
5669 add_inherited_template_parms (tree fn, tree inherited)
5671 tree inner_parms
5672 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5673 inner_parms = copy_node (inner_parms);
5674 tree parms
5675 = tree_cons (size_int (processing_template_decl + 1),
5676 inner_parms, current_template_parms);
5677 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5678 tree args = template_parms_to_args (parms);
5679 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5680 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5681 DECL_TEMPLATE_RESULT (tmpl) = fn;
5682 DECL_ARTIFICIAL (tmpl) = true;
5683 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5684 return tmpl;
5687 /* Called when a class template TYPE is redeclared with the indicated
5688 template PARMS, e.g.:
5690 template <class T> struct S;
5691 template <class T> struct S {}; */
5693 bool
5694 redeclare_class_template (tree type, tree parms, tree cons)
5696 tree tmpl;
5697 tree tmpl_parms;
5698 int i;
5700 if (!TYPE_TEMPLATE_INFO (type))
5702 error ("%qT is not a template type", type);
5703 return false;
5706 tmpl = TYPE_TI_TEMPLATE (type);
5707 if (!PRIMARY_TEMPLATE_P (tmpl))
5708 /* The type is nested in some template class. Nothing to worry
5709 about here; there are no new template parameters for the nested
5710 type. */
5711 return true;
5713 if (!parms)
5715 error ("template specifiers not specified in declaration of %qD",
5716 tmpl);
5717 return false;
5720 parms = INNERMOST_TEMPLATE_PARMS (parms);
5721 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5723 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5725 error_n (input_location, TREE_VEC_LENGTH (parms),
5726 "redeclared with %d template parameter",
5727 "redeclared with %d template parameters",
5728 TREE_VEC_LENGTH (parms));
5729 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5730 "previous declaration %qD used %d template parameter",
5731 "previous declaration %qD used %d template parameters",
5732 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5733 return false;
5736 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5738 tree tmpl_parm;
5739 tree parm;
5740 tree tmpl_default;
5741 tree parm_default;
5743 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5744 || TREE_VEC_ELT (parms, i) == error_mark_node)
5745 continue;
5747 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5748 if (error_operand_p (tmpl_parm))
5749 return false;
5751 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5752 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5753 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5755 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5756 TEMPLATE_DECL. */
5757 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5758 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5759 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5760 || (TREE_CODE (tmpl_parm) != PARM_DECL
5761 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5762 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5763 || (TREE_CODE (tmpl_parm) == PARM_DECL
5764 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5765 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5767 error ("template parameter %q+#D", tmpl_parm);
5768 error ("redeclared here as %q#D", parm);
5769 return false;
5772 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5774 /* We have in [temp.param]:
5776 A template-parameter may not be given default arguments
5777 by two different declarations in the same scope. */
5778 error_at (input_location, "redefinition of default argument for %q#D", parm);
5779 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5780 "original definition appeared here");
5781 return false;
5784 if (parm_default != NULL_TREE)
5785 /* Update the previous template parameters (which are the ones
5786 that will really count) with the new default value. */
5787 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5788 else if (tmpl_default != NULL_TREE)
5789 /* Update the new parameters, too; they'll be used as the
5790 parameters for any members. */
5791 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5793 /* Give each template template parm in this redeclaration a
5794 DECL_CONTEXT of the template for which they are a parameter. */
5795 if (TREE_CODE (parm) == TEMPLATE_DECL)
5797 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5798 DECL_CONTEXT (parm) = tmpl;
5801 if (TREE_CODE (parm) == TYPE_DECL)
5802 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5805 // Cannot redeclare a class template with a different set of constraints.
5806 if (!equivalent_constraints (get_constraints (tmpl), cons))
5808 error_at (input_location, "redeclaration %q#D with different "
5809 "constraints", tmpl);
5810 inform (DECL_SOURCE_LOCATION (tmpl),
5811 "original declaration appeared here");
5814 return true;
5817 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5818 to be used when the caller has already checked
5819 (processing_template_decl
5820 && !instantiation_dependent_expression_p (expr)
5821 && potential_constant_expression (expr))
5822 and cleared processing_template_decl. */
5824 tree
5825 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5827 return tsubst_copy_and_build (expr,
5828 /*args=*/NULL_TREE,
5829 complain,
5830 /*in_decl=*/NULL_TREE,
5831 /*function_p=*/false,
5832 /*integral_constant_expression_p=*/true);
5835 /* Simplify EXPR if it is a non-dependent expression. Returns the
5836 (possibly simplified) expression. */
5838 tree
5839 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5841 if (expr == NULL_TREE)
5842 return NULL_TREE;
5844 /* If we're in a template, but EXPR isn't value dependent, simplify
5845 it. We're supposed to treat:
5847 template <typename T> void f(T[1 + 1]);
5848 template <typename T> void f(T[2]);
5850 as two declarations of the same function, for example. */
5851 if (processing_template_decl
5852 && is_nondependent_constant_expression (expr))
5854 processing_template_decl_sentinel s;
5855 expr = instantiate_non_dependent_expr_internal (expr, complain);
5857 return expr;
5860 tree
5861 instantiate_non_dependent_expr (tree expr)
5863 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5866 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5867 an uninstantiated expression. */
5869 tree
5870 instantiate_non_dependent_or_null (tree expr)
5872 if (expr == NULL_TREE)
5873 return NULL_TREE;
5874 if (processing_template_decl)
5876 if (!is_nondependent_constant_expression (expr))
5877 expr = NULL_TREE;
5878 else
5880 processing_template_decl_sentinel s;
5881 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5884 return expr;
5887 /* True iff T is a specialization of a variable template. */
5889 bool
5890 variable_template_specialization_p (tree t)
5892 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5893 return false;
5894 tree tmpl = DECL_TI_TEMPLATE (t);
5895 return variable_template_p (tmpl);
5898 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5899 template declaration, or a TYPE_DECL for an alias declaration. */
5901 bool
5902 alias_type_or_template_p (tree t)
5904 if (t == NULL_TREE)
5905 return false;
5906 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5907 || (TYPE_P (t)
5908 && TYPE_NAME (t)
5909 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5910 || DECL_ALIAS_TEMPLATE_P (t));
5913 /* Return TRUE iff T is a specialization of an alias template. */
5915 bool
5916 alias_template_specialization_p (const_tree t)
5918 /* It's an alias template specialization if it's an alias and its
5919 TYPE_NAME is a specialization of a primary template. */
5920 if (TYPE_ALIAS_P (t))
5921 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5922 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5924 return false;
5927 /* An alias template is complex from a SFINAE perspective if a template-id
5928 using that alias can be ill-formed when the expansion is not, as with
5929 the void_t template. We determine this by checking whether the
5930 expansion for the alias template uses all its template parameters. */
5932 struct uses_all_template_parms_data
5934 int level;
5935 bool *seen;
5938 static int
5939 uses_all_template_parms_r (tree t, void *data_)
5941 struct uses_all_template_parms_data &data
5942 = *(struct uses_all_template_parms_data*)data_;
5943 tree idx = get_template_parm_index (t);
5945 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5946 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5947 return 0;
5950 static bool
5951 complex_alias_template_p (const_tree tmpl)
5953 struct uses_all_template_parms_data data;
5954 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5955 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5956 data.level = TMPL_PARMS_DEPTH (parms);
5957 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5958 data.seen = XALLOCAVEC (bool, len);
5959 for (int i = 0; i < len; ++i)
5960 data.seen[i] = false;
5962 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5963 for (int i = 0; i < len; ++i)
5964 if (!data.seen[i])
5965 return true;
5966 return false;
5969 /* Return TRUE iff T is a specialization of a complex alias template with
5970 dependent template-arguments. */
5972 bool
5973 dependent_alias_template_spec_p (const_tree t)
5975 if (!alias_template_specialization_p (t))
5976 return false;
5978 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5979 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5980 return false;
5982 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5983 if (!any_dependent_template_arguments_p (args))
5984 return false;
5986 return true;
5989 /* Return the number of innermost template parameters in TMPL. */
5991 static int
5992 num_innermost_template_parms (tree tmpl)
5994 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5995 return TREE_VEC_LENGTH (parms);
5998 /* Return either TMPL or another template that it is equivalent to under DR
5999 1286: An alias that just changes the name of a template is equivalent to
6000 the other template. */
6002 static tree
6003 get_underlying_template (tree tmpl)
6005 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6006 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6008 /* Determine if the alias is equivalent to an underlying template. */
6009 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6010 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6011 if (!tinfo)
6012 break;
6014 tree underlying = TI_TEMPLATE (tinfo);
6015 if (!PRIMARY_TEMPLATE_P (underlying)
6016 || (num_innermost_template_parms (tmpl)
6017 != num_innermost_template_parms (underlying)))
6018 break;
6020 tree alias_args = INNERMOST_TEMPLATE_ARGS
6021 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6022 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6023 break;
6025 /* Alias is equivalent. Strip it and repeat. */
6026 tmpl = underlying;
6029 return tmpl;
6032 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6033 must be a reference-to-function or a pointer-to-function type, as specified
6034 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6035 and check that the resulting function has external linkage. */
6037 static tree
6038 convert_nontype_argument_function (tree type, tree expr,
6039 tsubst_flags_t complain)
6041 tree fns = expr;
6042 tree fn, fn_no_ptr;
6043 linkage_kind linkage;
6045 fn = instantiate_type (type, fns, tf_none);
6046 if (fn == error_mark_node)
6047 return error_mark_node;
6049 if (value_dependent_expression_p (fn))
6050 goto accept;
6052 fn_no_ptr = strip_fnptr_conv (fn);
6053 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6054 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6055 if (BASELINK_P (fn_no_ptr))
6056 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6058 /* [temp.arg.nontype]/1
6060 A template-argument for a non-type, non-template template-parameter
6061 shall be one of:
6062 [...]
6063 -- the address of an object or function with external [C++11: or
6064 internal] linkage. */
6066 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6068 if (complain & tf_error)
6070 error ("%qE is not a valid template argument for type %qT",
6071 expr, type);
6072 if (TYPE_PTR_P (type))
6073 inform (input_location, "it must be the address of a function "
6074 "with external linkage");
6075 else
6076 inform (input_location, "it must be the name of a function with "
6077 "external linkage");
6079 return NULL_TREE;
6082 linkage = decl_linkage (fn_no_ptr);
6083 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6085 if (complain & tf_error)
6087 if (cxx_dialect >= cxx11)
6088 error ("%qE is not a valid template argument for type %qT "
6089 "because %qD has no linkage",
6090 expr, type, fn_no_ptr);
6091 else
6092 error ("%qE is not a valid template argument for type %qT "
6093 "because %qD does not have external linkage",
6094 expr, type, fn_no_ptr);
6096 return NULL_TREE;
6099 accept:
6100 if (TREE_CODE (type) == REFERENCE_TYPE)
6101 fn = build_address (fn);
6102 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6103 fn = build_nop (type, fn);
6105 return fn;
6108 /* Subroutine of convert_nontype_argument.
6109 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6110 Emit an error otherwise. */
6112 static bool
6113 check_valid_ptrmem_cst_expr (tree type, tree expr,
6114 tsubst_flags_t complain)
6116 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6117 tree orig_expr = expr;
6118 STRIP_NOPS (expr);
6119 if (null_ptr_cst_p (expr))
6120 return true;
6121 if (TREE_CODE (expr) == PTRMEM_CST
6122 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6123 PTRMEM_CST_CLASS (expr)))
6124 return true;
6125 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6126 return true;
6127 if (processing_template_decl
6128 && TREE_CODE (expr) == ADDR_EXPR
6129 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6130 return true;
6131 if (complain & tf_error)
6133 error_at (loc, "%qE is not a valid template argument for type %qT",
6134 orig_expr, type);
6135 if (TREE_CODE (expr) != PTRMEM_CST)
6136 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6137 else
6138 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6140 return false;
6143 /* Returns TRUE iff the address of OP is value-dependent.
6145 14.6.2.4 [temp.dep.temp]:
6146 A non-integral non-type template-argument is dependent if its type is
6147 dependent or it has either of the following forms
6148 qualified-id
6149 & qualified-id
6150 and contains a nested-name-specifier which specifies a class-name that
6151 names a dependent type.
6153 We generalize this to just say that the address of a member of a
6154 dependent class is value-dependent; the above doesn't cover the
6155 address of a static data member named with an unqualified-id. */
6157 static bool
6158 has_value_dependent_address (tree op)
6160 /* We could use get_inner_reference here, but there's no need;
6161 this is only relevant for template non-type arguments, which
6162 can only be expressed as &id-expression. */
6163 if (DECL_P (op))
6165 tree ctx = CP_DECL_CONTEXT (op);
6166 if (TYPE_P (ctx) && dependent_type_p (ctx))
6167 return true;
6170 return false;
6173 /* The next set of functions are used for providing helpful explanatory
6174 diagnostics for failed overload resolution. Their messages should be
6175 indented by two spaces for consistency with the messages in
6176 call.c */
6178 static int
6179 unify_success (bool /*explain_p*/)
6181 return 0;
6184 /* Other failure functions should call this one, to provide a single function
6185 for setting a breakpoint on. */
6187 static int
6188 unify_invalid (bool /*explain_p*/)
6190 return 1;
6193 static int
6194 unify_parameter_deduction_failure (bool explain_p, tree parm)
6196 if (explain_p)
6197 inform (input_location,
6198 " couldn't deduce template parameter %qD", parm);
6199 return unify_invalid (explain_p);
6202 static int
6203 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6205 if (explain_p)
6206 inform (input_location,
6207 " types %qT and %qT have incompatible cv-qualifiers",
6208 parm, arg);
6209 return unify_invalid (explain_p);
6212 static int
6213 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6215 if (explain_p)
6216 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6217 return unify_invalid (explain_p);
6220 static int
6221 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6223 if (explain_p)
6224 inform (input_location,
6225 " template parameter %qD is not a parameter pack, but "
6226 "argument %qD is",
6227 parm, arg);
6228 return unify_invalid (explain_p);
6231 static int
6232 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6234 if (explain_p)
6235 inform (input_location,
6236 " template argument %qE does not match "
6237 "pointer-to-member constant %qE",
6238 arg, parm);
6239 return unify_invalid (explain_p);
6242 static int
6243 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6245 if (explain_p)
6246 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6247 return unify_invalid (explain_p);
6250 static int
6251 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6253 if (explain_p)
6254 inform (input_location,
6255 " inconsistent parameter pack deduction with %qT and %qT",
6256 old_arg, new_arg);
6257 return unify_invalid (explain_p);
6260 static int
6261 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6263 if (explain_p)
6265 if (TYPE_P (parm))
6266 inform (input_location,
6267 " deduced conflicting types for parameter %qT (%qT and %qT)",
6268 parm, first, second);
6269 else
6270 inform (input_location,
6271 " deduced conflicting values for non-type parameter "
6272 "%qE (%qE and %qE)", parm, first, second);
6274 return unify_invalid (explain_p);
6277 static int
6278 unify_vla_arg (bool explain_p, tree arg)
6280 if (explain_p)
6281 inform (input_location,
6282 " variable-sized array type %qT is not "
6283 "a valid template argument",
6284 arg);
6285 return unify_invalid (explain_p);
6288 static int
6289 unify_method_type_error (bool explain_p, tree arg)
6291 if (explain_p)
6292 inform (input_location,
6293 " member function type %qT is not a valid template argument",
6294 arg);
6295 return unify_invalid (explain_p);
6298 static int
6299 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6301 if (explain_p)
6303 if (least_p)
6304 inform_n (input_location, wanted,
6305 " candidate expects at least %d argument, %d provided",
6306 " candidate expects at least %d arguments, %d provided",
6307 wanted, have);
6308 else
6309 inform_n (input_location, wanted,
6310 " candidate expects %d argument, %d provided",
6311 " candidate expects %d arguments, %d provided",
6312 wanted, have);
6314 return unify_invalid (explain_p);
6317 static int
6318 unify_too_many_arguments (bool explain_p, int have, int wanted)
6320 return unify_arity (explain_p, have, wanted);
6323 static int
6324 unify_too_few_arguments (bool explain_p, int have, int wanted,
6325 bool least_p = false)
6327 return unify_arity (explain_p, have, wanted, least_p);
6330 static int
6331 unify_arg_conversion (bool explain_p, tree to_type,
6332 tree from_type, tree arg)
6334 if (explain_p)
6335 inform (EXPR_LOC_OR_LOC (arg, input_location),
6336 " cannot convert %qE (type %qT) to type %qT",
6337 arg, from_type, to_type);
6338 return unify_invalid (explain_p);
6341 static int
6342 unify_no_common_base (bool explain_p, enum template_base_result r,
6343 tree parm, tree arg)
6345 if (explain_p)
6346 switch (r)
6348 case tbr_ambiguous_baseclass:
6349 inform (input_location, " %qT is an ambiguous base class of %qT",
6350 parm, arg);
6351 break;
6352 default:
6353 inform (input_location, " %qT is not derived from %qT", arg, parm);
6354 break;
6356 return unify_invalid (explain_p);
6359 static int
6360 unify_inconsistent_template_template_parameters (bool explain_p)
6362 if (explain_p)
6363 inform (input_location,
6364 " template parameters of a template template argument are "
6365 "inconsistent with other deduced template arguments");
6366 return unify_invalid (explain_p);
6369 static int
6370 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6372 if (explain_p)
6373 inform (input_location,
6374 " can't deduce a template for %qT from non-template type %qT",
6375 parm, arg);
6376 return unify_invalid (explain_p);
6379 static int
6380 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6382 if (explain_p)
6383 inform (input_location,
6384 " template argument %qE does not match %qE", arg, parm);
6385 return unify_invalid (explain_p);
6388 /* Attempt to convert the non-type template parameter EXPR to the
6389 indicated TYPE. If the conversion is successful, return the
6390 converted value. If the conversion is unsuccessful, return
6391 NULL_TREE if we issued an error message, or error_mark_node if we
6392 did not. We issue error messages for out-and-out bad template
6393 parameters, but not simply because the conversion failed, since we
6394 might be just trying to do argument deduction. Both TYPE and EXPR
6395 must be non-dependent.
6397 The conversion follows the special rules described in
6398 [temp.arg.nontype], and it is much more strict than an implicit
6399 conversion.
6401 This function is called twice for each template argument (see
6402 lookup_template_class for a more accurate description of this
6403 problem). This means that we need to handle expressions which
6404 are not valid in a C++ source, but can be created from the
6405 first call (for instance, casts to perform conversions). These
6406 hacks can go away after we fix the double coercion problem. */
6408 static tree
6409 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6411 tree expr_type;
6412 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6413 tree orig_expr = expr;
6415 /* Detect immediately string literals as invalid non-type argument.
6416 This special-case is not needed for correctness (we would easily
6417 catch this later), but only to provide better diagnostic for this
6418 common user mistake. As suggested by DR 100, we do not mention
6419 linkage issues in the diagnostic as this is not the point. */
6420 /* FIXME we're making this OK. */
6421 if (TREE_CODE (expr) == STRING_CST)
6423 if (complain & tf_error)
6424 error ("%qE is not a valid template argument for type %qT "
6425 "because string literals can never be used in this context",
6426 expr, type);
6427 return NULL_TREE;
6430 /* Add the ADDR_EXPR now for the benefit of
6431 value_dependent_expression_p. */
6432 if (TYPE_PTROBV_P (type)
6433 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6435 expr = decay_conversion (expr, complain);
6436 if (expr == error_mark_node)
6437 return error_mark_node;
6440 /* If we are in a template, EXPR may be non-dependent, but still
6441 have a syntactic, rather than semantic, form. For example, EXPR
6442 might be a SCOPE_REF, rather than the VAR_DECL to which the
6443 SCOPE_REF refers. Preserving the qualifying scope is necessary
6444 so that access checking can be performed when the template is
6445 instantiated -- but here we need the resolved form so that we can
6446 convert the argument. */
6447 bool non_dep = false;
6448 if (TYPE_REF_OBJ_P (type)
6449 && has_value_dependent_address (expr))
6450 /* If we want the address and it's value-dependent, don't fold. */;
6451 else if (processing_template_decl
6452 && is_nondependent_constant_expression (expr))
6453 non_dep = true;
6454 if (error_operand_p (expr))
6455 return error_mark_node;
6456 expr_type = TREE_TYPE (expr);
6458 /* If the argument is non-dependent, perform any conversions in
6459 non-dependent context as well. */
6460 processing_template_decl_sentinel s (non_dep);
6461 if (non_dep)
6462 expr = instantiate_non_dependent_expr_internal (expr, complain);
6464 if (value_dependent_expression_p (expr))
6465 expr = canonicalize_expr_argument (expr, complain);
6467 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6468 to a non-type argument of "nullptr". */
6469 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6470 expr = fold_simple (convert (type, expr));
6472 /* In C++11, integral or enumeration non-type template arguments can be
6473 arbitrary constant expressions. Pointer and pointer to
6474 member arguments can be general constant expressions that evaluate
6475 to a null value, but otherwise still need to be of a specific form. */
6476 if (cxx_dialect >= cxx11)
6478 if (TREE_CODE (expr) == PTRMEM_CST)
6479 /* A PTRMEM_CST is already constant, and a valid template
6480 argument for a parameter of pointer to member type, we just want
6481 to leave it in that form rather than lower it to a
6482 CONSTRUCTOR. */;
6483 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6484 || cxx_dialect >= cxx17)
6486 /* C++17: A template-argument for a non-type template-parameter shall
6487 be a converted constant expression (8.20) of the type of the
6488 template-parameter. */
6489 expr = build_converted_constant_expr (type, expr, complain);
6490 if (expr == error_mark_node)
6491 return error_mark_node;
6492 expr = maybe_constant_value (expr);
6493 expr = convert_from_reference (expr);
6495 else if (TYPE_PTR_OR_PTRMEM_P (type))
6497 tree folded = maybe_constant_value (expr);
6498 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6499 : null_member_pointer_value_p (folded))
6500 expr = folded;
6504 if (TREE_CODE (type) == REFERENCE_TYPE)
6505 expr = mark_lvalue_use (expr);
6506 else
6507 expr = mark_rvalue_use (expr);
6509 /* HACK: Due to double coercion, we can get a
6510 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6511 which is the tree that we built on the first call (see
6512 below when coercing to reference to object or to reference to
6513 function). We just strip everything and get to the arg.
6514 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6515 for examples. */
6516 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6518 tree probe_type, probe = expr;
6519 if (REFERENCE_REF_P (probe))
6520 probe = TREE_OPERAND (probe, 0);
6521 probe_type = TREE_TYPE (probe);
6522 if (TREE_CODE (probe) == NOP_EXPR)
6524 /* ??? Maybe we could use convert_from_reference here, but we
6525 would need to relax its constraints because the NOP_EXPR
6526 could actually change the type to something more cv-qualified,
6527 and this is not folded by convert_from_reference. */
6528 tree addr = TREE_OPERAND (probe, 0);
6529 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6530 && TREE_CODE (addr) == ADDR_EXPR
6531 && TYPE_PTR_P (TREE_TYPE (addr))
6532 && (same_type_ignoring_top_level_qualifiers_p
6533 (TREE_TYPE (probe_type),
6534 TREE_TYPE (TREE_TYPE (addr)))))
6536 expr = TREE_OPERAND (addr, 0);
6537 expr_type = TREE_TYPE (probe_type);
6542 /* [temp.arg.nontype]/5, bullet 1
6544 For a non-type template-parameter of integral or enumeration type,
6545 integral promotions (_conv.prom_) and integral conversions
6546 (_conv.integral_) are applied. */
6547 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6549 if (cxx_dialect < cxx11)
6551 tree t = build_converted_constant_expr (type, expr, complain);
6552 t = maybe_constant_value (t);
6553 if (t != error_mark_node)
6554 expr = t;
6557 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6558 return error_mark_node;
6560 /* Notice that there are constant expressions like '4 % 0' which
6561 do not fold into integer constants. */
6562 if (TREE_CODE (expr) != INTEGER_CST
6563 && !value_dependent_expression_p (expr))
6565 if (complain & tf_error)
6567 int errs = errorcount, warns = warningcount + werrorcount;
6568 if (!require_potential_constant_expression (expr))
6569 expr = error_mark_node;
6570 else
6571 expr = cxx_constant_value (expr);
6572 if (errorcount > errs || warningcount + werrorcount > warns)
6573 inform (loc, "in template argument for type %qT ", type);
6574 if (expr == error_mark_node)
6575 return NULL_TREE;
6576 /* else cxx_constant_value complained but gave us
6577 a real constant, so go ahead. */
6578 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6580 else
6581 return NULL_TREE;
6584 /* Avoid typedef problems. */
6585 if (TREE_TYPE (expr) != type)
6586 expr = fold_convert (type, expr);
6588 /* [temp.arg.nontype]/5, bullet 2
6590 For a non-type template-parameter of type pointer to object,
6591 qualification conversions (_conv.qual_) and the array-to-pointer
6592 conversion (_conv.array_) are applied. */
6593 else if (TYPE_PTROBV_P (type))
6595 tree decayed = expr;
6597 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6598 decay_conversion or an explicit cast. If it's a problematic cast,
6599 we'll complain about it below. */
6600 if (TREE_CODE (expr) == NOP_EXPR)
6602 tree probe = expr;
6603 STRIP_NOPS (probe);
6604 if (TREE_CODE (probe) == ADDR_EXPR
6605 && TYPE_PTR_P (TREE_TYPE (probe)))
6607 expr = probe;
6608 expr_type = TREE_TYPE (expr);
6612 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6614 A template-argument for a non-type, non-template template-parameter
6615 shall be one of: [...]
6617 -- the name of a non-type template-parameter;
6618 -- the address of an object or function with external linkage, [...]
6619 expressed as "& id-expression" where the & is optional if the name
6620 refers to a function or array, or if the corresponding
6621 template-parameter is a reference.
6623 Here, we do not care about functions, as they are invalid anyway
6624 for a parameter of type pointer-to-object. */
6626 if (value_dependent_expression_p (expr))
6627 /* Non-type template parameters are OK. */
6629 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6630 /* Null pointer values are OK in C++11. */;
6631 else if (TREE_CODE (expr) != ADDR_EXPR)
6633 if (VAR_P (expr))
6635 if (complain & tf_error)
6636 error ("%qD is not a valid template argument "
6637 "because %qD is a variable, not the address of "
6638 "a variable", orig_expr, expr);
6639 return NULL_TREE;
6641 if (POINTER_TYPE_P (expr_type))
6643 if (complain & tf_error)
6644 error ("%qE is not a valid template argument for %qT "
6645 "because it is not the address of a variable",
6646 orig_expr, type);
6647 return NULL_TREE;
6649 /* Other values, like integer constants, might be valid
6650 non-type arguments of some other type. */
6651 return error_mark_node;
6653 else
6655 tree decl = TREE_OPERAND (expr, 0);
6657 if (!VAR_P (decl))
6659 if (complain & tf_error)
6660 error ("%qE is not a valid template argument of type %qT "
6661 "because %qE is not a variable", orig_expr, type, decl);
6662 return NULL_TREE;
6664 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6666 if (complain & tf_error)
6667 error ("%qE is not a valid template argument of type %qT "
6668 "because %qD does not have external linkage",
6669 orig_expr, type, decl);
6670 return NULL_TREE;
6672 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6673 && decl_linkage (decl) == lk_none)
6675 if (complain & tf_error)
6676 error ("%qE is not a valid template argument of type %qT "
6677 "because %qD has no linkage", orig_expr, type, decl);
6678 return NULL_TREE;
6680 /* C++17: For a non-type template-parameter of reference or pointer
6681 type, the value of the constant expression shall not refer to (or
6682 for a pointer type, shall not be the address of):
6683 * a subobject (4.5),
6684 * a temporary object (15.2),
6685 * a string literal (5.13.5),
6686 * the result of a typeid expression (8.2.8), or
6687 * a predefined __func__ variable (11.4.1). */
6688 else if (DECL_ARTIFICIAL (decl))
6690 if (complain & tf_error)
6691 error ("the address of %qD is not a valid template argument",
6692 decl);
6693 return NULL_TREE;
6695 else if (!same_type_ignoring_top_level_qualifiers_p
6696 (strip_array_types (TREE_TYPE (type)),
6697 strip_array_types (TREE_TYPE (decl))))
6699 if (complain & tf_error)
6700 error ("the address of the %qT subobject of %qD is not a "
6701 "valid template argument", TREE_TYPE (type), decl);
6702 return NULL_TREE;
6704 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6706 if (complain & tf_error)
6707 error ("the address of %qD is not a valid template argument "
6708 "because it does not have static storage duration",
6709 decl);
6710 return NULL_TREE;
6714 expr = decayed;
6716 expr = perform_qualification_conversions (type, expr);
6717 if (expr == error_mark_node)
6718 return error_mark_node;
6720 /* [temp.arg.nontype]/5, bullet 3
6722 For a non-type template-parameter of type reference to object, no
6723 conversions apply. The type referred to by the reference may be more
6724 cv-qualified than the (otherwise identical) type of the
6725 template-argument. The template-parameter is bound directly to the
6726 template-argument, which must be an lvalue. */
6727 else if (TYPE_REF_OBJ_P (type))
6729 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6730 expr_type))
6731 return error_mark_node;
6733 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6735 if (complain & tf_error)
6736 error ("%qE is not a valid template argument for type %qT "
6737 "because of conflicts in cv-qualification", expr, type);
6738 return NULL_TREE;
6741 if (!lvalue_p (expr))
6743 if (complain & tf_error)
6744 error ("%qE is not a valid template argument for type %qT "
6745 "because it is not an lvalue", expr, type);
6746 return NULL_TREE;
6749 /* [temp.arg.nontype]/1
6751 A template-argument for a non-type, non-template template-parameter
6752 shall be one of: [...]
6754 -- the address of an object or function with external linkage. */
6755 if (INDIRECT_REF_P (expr)
6756 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6758 expr = TREE_OPERAND (expr, 0);
6759 if (DECL_P (expr))
6761 if (complain & tf_error)
6762 error ("%q#D is not a valid template argument for type %qT "
6763 "because a reference variable does not have a constant "
6764 "address", expr, type);
6765 return NULL_TREE;
6769 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6770 && value_dependent_expression_p (expr))
6771 /* OK, dependent reference. We don't want to ask whether a DECL is
6772 itself value-dependent, since what we want here is its address. */;
6773 else
6775 if (!DECL_P (expr))
6777 if (complain & tf_error)
6778 error ("%qE is not a valid template argument for type %qT "
6779 "because it is not an object with linkage",
6780 expr, type);
6781 return NULL_TREE;
6784 /* DR 1155 allows internal linkage in C++11 and up. */
6785 linkage_kind linkage = decl_linkage (expr);
6786 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6788 if (complain & tf_error)
6789 error ("%qE is not a valid template argument for type %qT "
6790 "because object %qD does not have linkage",
6791 expr, type, expr);
6792 return NULL_TREE;
6795 expr = build_address (expr);
6798 if (!same_type_p (type, TREE_TYPE (expr)))
6799 expr = build_nop (type, expr);
6801 /* [temp.arg.nontype]/5, bullet 4
6803 For a non-type template-parameter of type pointer to function, only
6804 the function-to-pointer conversion (_conv.func_) is applied. If the
6805 template-argument represents a set of overloaded functions (or a
6806 pointer to such), the matching function is selected from the set
6807 (_over.over_). */
6808 else if (TYPE_PTRFN_P (type))
6810 /* If the argument is a template-id, we might not have enough
6811 context information to decay the pointer. */
6812 if (!type_unknown_p (expr_type))
6814 expr = decay_conversion (expr, complain);
6815 if (expr == error_mark_node)
6816 return error_mark_node;
6819 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6820 /* Null pointer values are OK in C++11. */
6821 return perform_qualification_conversions (type, expr);
6823 expr = convert_nontype_argument_function (type, expr, complain);
6824 if (!expr || expr == error_mark_node)
6825 return expr;
6827 /* [temp.arg.nontype]/5, bullet 5
6829 For a non-type template-parameter of type reference to function, no
6830 conversions apply. If the template-argument represents a set of
6831 overloaded functions, the matching function is selected from the set
6832 (_over.over_). */
6833 else if (TYPE_REFFN_P (type))
6835 if (TREE_CODE (expr) == ADDR_EXPR)
6837 if (complain & tf_error)
6839 error ("%qE is not a valid template argument for type %qT "
6840 "because it is a pointer", expr, type);
6841 inform (input_location, "try using %qE instead",
6842 TREE_OPERAND (expr, 0));
6844 return NULL_TREE;
6847 expr = convert_nontype_argument_function (type, expr, complain);
6848 if (!expr || expr == error_mark_node)
6849 return expr;
6851 /* [temp.arg.nontype]/5, bullet 6
6853 For a non-type template-parameter of type pointer to member function,
6854 no conversions apply. If the template-argument represents a set of
6855 overloaded member functions, the matching member function is selected
6856 from the set (_over.over_). */
6857 else if (TYPE_PTRMEMFUNC_P (type))
6859 expr = instantiate_type (type, expr, tf_none);
6860 if (expr == error_mark_node)
6861 return error_mark_node;
6863 /* [temp.arg.nontype] bullet 1 says the pointer to member
6864 expression must be a pointer-to-member constant. */
6865 if (!value_dependent_expression_p (expr)
6866 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6867 return NULL_TREE;
6869 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6870 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6871 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6872 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6874 /* [temp.arg.nontype]/5, bullet 7
6876 For a non-type template-parameter of type pointer to data member,
6877 qualification conversions (_conv.qual_) are applied. */
6878 else if (TYPE_PTRDATAMEM_P (type))
6880 /* [temp.arg.nontype] bullet 1 says the pointer to member
6881 expression must be a pointer-to-member constant. */
6882 if (!value_dependent_expression_p (expr)
6883 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6884 return NULL_TREE;
6886 expr = perform_qualification_conversions (type, expr);
6887 if (expr == error_mark_node)
6888 return expr;
6890 else if (NULLPTR_TYPE_P (type))
6892 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6894 if (complain & tf_error)
6895 error ("%qE is not a valid template argument for type %qT "
6896 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6897 return NULL_TREE;
6899 return expr;
6901 /* A template non-type parameter must be one of the above. */
6902 else
6903 gcc_unreachable ();
6905 /* Sanity check: did we actually convert the argument to the
6906 right type? */
6907 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6908 (type, TREE_TYPE (expr)));
6909 return convert_from_reference (expr);
6912 /* Subroutine of coerce_template_template_parms, which returns 1 if
6913 PARM_PARM and ARG_PARM match using the rule for the template
6914 parameters of template template parameters. Both PARM and ARG are
6915 template parameters; the rest of the arguments are the same as for
6916 coerce_template_template_parms.
6918 static int
6919 coerce_template_template_parm (tree parm,
6920 tree arg,
6921 tsubst_flags_t complain,
6922 tree in_decl,
6923 tree outer_args)
6925 if (arg == NULL_TREE || error_operand_p (arg)
6926 || parm == NULL_TREE || error_operand_p (parm))
6927 return 0;
6929 if (TREE_CODE (arg) != TREE_CODE (parm))
6930 return 0;
6932 switch (TREE_CODE (parm))
6934 case TEMPLATE_DECL:
6935 /* We encounter instantiations of templates like
6936 template <template <template <class> class> class TT>
6937 class C; */
6939 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6940 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6942 if (!coerce_template_template_parms
6943 (parmparm, argparm, complain, in_decl, outer_args))
6944 return 0;
6946 /* Fall through. */
6948 case TYPE_DECL:
6949 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6950 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6951 /* Argument is a parameter pack but parameter is not. */
6952 return 0;
6953 break;
6955 case PARM_DECL:
6956 /* The tsubst call is used to handle cases such as
6958 template <int> class C {};
6959 template <class T, template <T> class TT> class D {};
6960 D<int, C> d;
6962 i.e. the parameter list of TT depends on earlier parameters. */
6963 if (!uses_template_parms (TREE_TYPE (arg)))
6965 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6966 if (!uses_template_parms (t)
6967 && !same_type_p (t, TREE_TYPE (arg)))
6968 return 0;
6971 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6972 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6973 /* Argument is a parameter pack but parameter is not. */
6974 return 0;
6976 break;
6978 default:
6979 gcc_unreachable ();
6982 return 1;
6985 /* Coerce template argument list ARGLIST for use with template
6986 template-parameter TEMPL. */
6988 static tree
6989 coerce_template_args_for_ttp (tree templ, tree arglist,
6990 tsubst_flags_t complain)
6992 /* Consider an example where a template template parameter declared as
6994 template <class T, class U = std::allocator<T> > class TT
6996 The template parameter level of T and U are one level larger than
6997 of TT. To proper process the default argument of U, say when an
6998 instantiation `TT<int>' is seen, we need to build the full
6999 arguments containing {int} as the innermost level. Outer levels,
7000 available when not appearing as default template argument, can be
7001 obtained from the arguments of the enclosing template.
7003 Suppose that TT is later substituted with std::vector. The above
7004 instantiation is `TT<int, std::allocator<T> >' with TT at
7005 level 1, and T at level 2, while the template arguments at level 1
7006 becomes {std::vector} and the inner level 2 is {int}. */
7008 tree outer = DECL_CONTEXT (templ);
7009 if (outer)
7011 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7012 /* We want arguments for the partial specialization, not arguments for
7013 the primary template. */
7014 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7015 else
7016 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7018 else if (current_template_parms)
7020 /* This is an argument of the current template, so we haven't set
7021 DECL_CONTEXT yet. */
7022 tree relevant_template_parms;
7024 /* Parameter levels that are greater than the level of the given
7025 template template parm are irrelevant. */
7026 relevant_template_parms = current_template_parms;
7027 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7028 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7029 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7031 outer = template_parms_to_args (relevant_template_parms);
7034 if (outer)
7035 arglist = add_to_template_args (outer, arglist);
7037 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7038 return coerce_template_parms (parmlist, arglist, templ,
7039 complain,
7040 /*require_all_args=*/true,
7041 /*use_default_args=*/true);
7044 /* A cache of template template parameters with match-all default
7045 arguments. */
7046 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7047 static void
7048 store_defaulted_ttp (tree v, tree t)
7050 if (!defaulted_ttp_cache)
7051 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7052 defaulted_ttp_cache->put (v, t);
7054 static tree
7055 lookup_defaulted_ttp (tree v)
7057 if (defaulted_ttp_cache)
7058 if (tree *p = defaulted_ttp_cache->get (v))
7059 return *p;
7060 return NULL_TREE;
7063 /* T is a bound template template-parameter. Copy its arguments into default
7064 arguments of the template template-parameter's template parameters. */
7066 static tree
7067 add_defaults_to_ttp (tree otmpl)
7069 if (tree c = lookup_defaulted_ttp (otmpl))
7070 return c;
7072 tree ntmpl = copy_node (otmpl);
7074 tree ntype = copy_node (TREE_TYPE (otmpl));
7075 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7076 TYPE_MAIN_VARIANT (ntype) = ntype;
7077 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7078 TYPE_NAME (ntype) = ntmpl;
7079 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7081 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7082 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7083 TEMPLATE_PARM_DECL (idx) = ntmpl;
7084 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7086 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7087 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7088 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7089 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7090 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7092 tree o = TREE_VEC_ELT (vec, i);
7093 if (!template_parameter_pack_p (TREE_VALUE (o)))
7095 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7096 TREE_PURPOSE (n) = any_targ_node;
7100 store_defaulted_ttp (otmpl, ntmpl);
7101 return ntmpl;
7104 /* ARG is a bound potential template template-argument, and PARGS is a list
7105 of arguments for the corresponding template template-parameter. Adjust
7106 PARGS as appropriate for application to ARG's template, and if ARG is a
7107 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7108 arguments to the template template parameter. */
7110 static tree
7111 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7113 ++processing_template_decl;
7114 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7115 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7117 /* When comparing two template template-parameters in partial ordering,
7118 rewrite the one currently being used as an argument to have default
7119 arguments for all parameters. */
7120 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7121 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7122 if (pargs != error_mark_node)
7123 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7124 TYPE_TI_ARGS (arg));
7126 else
7128 tree aparms
7129 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7130 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7131 /*require_all*/true,
7132 /*use_default*/true);
7134 --processing_template_decl;
7135 return pargs;
7138 /* Subroutine of unify for the case when PARM is a
7139 BOUND_TEMPLATE_TEMPLATE_PARM. */
7141 static int
7142 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7143 bool explain_p)
7145 tree parmvec = TYPE_TI_ARGS (parm);
7146 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7148 /* The template template parm might be variadic and the argument
7149 not, so flatten both argument lists. */
7150 parmvec = expand_template_argument_pack (parmvec);
7151 argvec = expand_template_argument_pack (argvec);
7153 if (flag_new_ttp)
7155 /* In keeping with P0522R0, adjust P's template arguments
7156 to apply to A's template; then flatten it again. */
7157 tree nparmvec = parmvec;
7158 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7159 nparmvec = expand_template_argument_pack (nparmvec);
7161 if (unify (tparms, targs, nparmvec, argvec,
7162 UNIFY_ALLOW_NONE, explain_p))
7163 return 1;
7165 /* If the P0522 adjustment eliminated a pack expansion, deduce
7166 empty packs. */
7167 if (flag_new_ttp
7168 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7169 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7170 DEDUCE_EXACT, /*sub*/true, explain_p))
7171 return 1;
7173 else
7175 /* Deduce arguments T, i from TT<T> or TT<i>.
7176 We check each element of PARMVEC and ARGVEC individually
7177 rather than the whole TREE_VEC since they can have
7178 different number of elements, which is allowed under N2555. */
7180 int len = TREE_VEC_LENGTH (parmvec);
7182 /* Check if the parameters end in a pack, making them
7183 variadic. */
7184 int parm_variadic_p = 0;
7185 if (len > 0
7186 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7187 parm_variadic_p = 1;
7189 for (int i = 0; i < len - parm_variadic_p; ++i)
7190 /* If the template argument list of P contains a pack
7191 expansion that is not the last template argument, the
7192 entire template argument list is a non-deduced
7193 context. */
7194 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7195 return unify_success (explain_p);
7197 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7198 return unify_too_few_arguments (explain_p,
7199 TREE_VEC_LENGTH (argvec), len);
7201 for (int i = 0; i < len - parm_variadic_p; ++i)
7202 if (unify (tparms, targs,
7203 TREE_VEC_ELT (parmvec, i),
7204 TREE_VEC_ELT (argvec, i),
7205 UNIFY_ALLOW_NONE, explain_p))
7206 return 1;
7208 if (parm_variadic_p
7209 && unify_pack_expansion (tparms, targs,
7210 parmvec, argvec,
7211 DEDUCE_EXACT,
7212 /*subr=*/true, explain_p))
7213 return 1;
7216 return 0;
7219 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7220 template template parameters. Both PARM_PARMS and ARG_PARMS are
7221 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7222 or PARM_DECL.
7224 Consider the example:
7225 template <class T> class A;
7226 template<template <class U> class TT> class B;
7228 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7229 the parameters to A, and OUTER_ARGS contains A. */
7231 static int
7232 coerce_template_template_parms (tree parm_parms,
7233 tree arg_parms,
7234 tsubst_flags_t complain,
7235 tree in_decl,
7236 tree outer_args)
7238 int nparms, nargs, i;
7239 tree parm, arg;
7240 int variadic_p = 0;
7242 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7243 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7245 nparms = TREE_VEC_LENGTH (parm_parms);
7246 nargs = TREE_VEC_LENGTH (arg_parms);
7248 if (flag_new_ttp)
7250 /* P0522R0: A template template-parameter P is at least as specialized as
7251 a template template-argument A if, given the following rewrite to two
7252 function templates, the function template corresponding to P is at
7253 least as specialized as the function template corresponding to A
7254 according to the partial ordering rules for function templates
7255 ([temp.func.order]). Given an invented class template X with the
7256 template parameter list of A (including default arguments):
7258 * Each of the two function templates has the same template parameters,
7259 respectively, as P or A.
7261 * Each function template has a single function parameter whose type is
7262 a specialization of X with template arguments corresponding to the
7263 template parameters from the respective function template where, for
7264 each template parameter PP in the template parameter list of the
7265 function template, a corresponding template argument AA is formed. If
7266 PP declares a parameter pack, then AA is the pack expansion
7267 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7269 If the rewrite produces an invalid type, then P is not at least as
7270 specialized as A. */
7272 /* So coerce P's args to apply to A's parms, and then deduce between A's
7273 args and the converted args. If that succeeds, A is at least as
7274 specialized as P, so they match.*/
7275 tree pargs = template_parms_level_to_args (parm_parms);
7276 ++processing_template_decl;
7277 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7278 /*require_all*/true, /*use_default*/true);
7279 --processing_template_decl;
7280 if (pargs != error_mark_node)
7282 tree targs = make_tree_vec (nargs);
7283 tree aargs = template_parms_level_to_args (arg_parms);
7284 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7285 /*explain*/false))
7286 return 1;
7290 /* Determine whether we have a parameter pack at the end of the
7291 template template parameter's template parameter list. */
7292 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7294 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7296 if (error_operand_p (parm))
7297 return 0;
7299 switch (TREE_CODE (parm))
7301 case TEMPLATE_DECL:
7302 case TYPE_DECL:
7303 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7304 variadic_p = 1;
7305 break;
7307 case PARM_DECL:
7308 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7309 variadic_p = 1;
7310 break;
7312 default:
7313 gcc_unreachable ();
7317 if (nargs != nparms
7318 && !(variadic_p && nargs >= nparms - 1))
7319 return 0;
7321 /* Check all of the template parameters except the parameter pack at
7322 the end (if any). */
7323 for (i = 0; i < nparms - variadic_p; ++i)
7325 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7326 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7327 continue;
7329 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7330 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7332 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7333 outer_args))
7334 return 0;
7338 if (variadic_p)
7340 /* Check each of the template parameters in the template
7341 argument against the template parameter pack at the end of
7342 the template template parameter. */
7343 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7344 return 0;
7346 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7348 for (; i < nargs; ++i)
7350 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7351 continue;
7353 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7355 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7356 outer_args))
7357 return 0;
7361 return 1;
7364 /* Verifies that the deduced template arguments (in TARGS) for the
7365 template template parameters (in TPARMS) represent valid bindings,
7366 by comparing the template parameter list of each template argument
7367 to the template parameter list of its corresponding template
7368 template parameter, in accordance with DR150. This
7369 routine can only be called after all template arguments have been
7370 deduced. It will return TRUE if all of the template template
7371 parameter bindings are okay, FALSE otherwise. */
7372 bool
7373 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7375 int i, ntparms = TREE_VEC_LENGTH (tparms);
7376 bool ret = true;
7378 /* We're dealing with template parms in this process. */
7379 ++processing_template_decl;
7381 targs = INNERMOST_TEMPLATE_ARGS (targs);
7383 for (i = 0; i < ntparms; ++i)
7385 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7386 tree targ = TREE_VEC_ELT (targs, i);
7388 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7390 tree packed_args = NULL_TREE;
7391 int idx, len = 1;
7393 if (ARGUMENT_PACK_P (targ))
7395 /* Look inside the argument pack. */
7396 packed_args = ARGUMENT_PACK_ARGS (targ);
7397 len = TREE_VEC_LENGTH (packed_args);
7400 for (idx = 0; idx < len; ++idx)
7402 tree targ_parms = NULL_TREE;
7404 if (packed_args)
7405 /* Extract the next argument from the argument
7406 pack. */
7407 targ = TREE_VEC_ELT (packed_args, idx);
7409 if (PACK_EXPANSION_P (targ))
7410 /* Look at the pattern of the pack expansion. */
7411 targ = PACK_EXPANSION_PATTERN (targ);
7413 /* Extract the template parameters from the template
7414 argument. */
7415 if (TREE_CODE (targ) == TEMPLATE_DECL)
7416 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7417 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7418 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7420 /* Verify that we can coerce the template template
7421 parameters from the template argument to the template
7422 parameter. This requires an exact match. */
7423 if (targ_parms
7424 && !coerce_template_template_parms
7425 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7426 targ_parms,
7427 tf_none,
7428 tparm,
7429 targs))
7431 ret = false;
7432 goto out;
7438 out:
7440 --processing_template_decl;
7441 return ret;
7444 /* Since type attributes aren't mangled, we need to strip them from
7445 template type arguments. */
7447 static tree
7448 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7450 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7451 return arg;
7452 bool removed_attributes = false;
7453 tree canon = strip_typedefs (arg, &removed_attributes);
7454 if (removed_attributes
7455 && (complain & tf_warning))
7456 warning (OPT_Wignored_attributes,
7457 "ignoring attributes on template argument %qT", arg);
7458 return canon;
7461 /* And from inside dependent non-type arguments like sizeof(Type). */
7463 static tree
7464 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7466 if (!arg || arg == error_mark_node)
7467 return arg;
7468 bool removed_attributes = false;
7469 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7470 if (removed_attributes
7471 && (complain & tf_warning))
7472 warning (OPT_Wignored_attributes,
7473 "ignoring attributes in template argument %qE", arg);
7474 return canon;
7477 // A template declaration can be substituted for a constrained
7478 // template template parameter only when the argument is more
7479 // constrained than the parameter.
7480 static bool
7481 is_compatible_template_arg (tree parm, tree arg)
7483 tree parm_cons = get_constraints (parm);
7485 /* For now, allow constrained template template arguments
7486 and unconstrained template template parameters. */
7487 if (parm_cons == NULL_TREE)
7488 return true;
7490 tree arg_cons = get_constraints (arg);
7492 // If the template parameter is constrained, we need to rewrite its
7493 // constraints in terms of the ARG's template parameters. This ensures
7494 // that all of the template parameter types will have the same depth.
7496 // Note that this is only valid when coerce_template_template_parm is
7497 // true for the innermost template parameters of PARM and ARG. In other
7498 // words, because coercion is successful, this conversion will be valid.
7499 if (parm_cons)
7501 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7502 parm_cons = tsubst_constraint_info (parm_cons,
7503 INNERMOST_TEMPLATE_ARGS (args),
7504 tf_none, NULL_TREE);
7505 if (parm_cons == error_mark_node)
7506 return false;
7509 return subsumes (parm_cons, arg_cons);
7512 // Convert a placeholder argument into a binding to the original
7513 // parameter. The original parameter is saved as the TREE_TYPE of
7514 // ARG.
7515 static inline tree
7516 convert_wildcard_argument (tree parm, tree arg)
7518 TREE_TYPE (arg) = parm;
7519 return arg;
7522 /* Convert the indicated template ARG as necessary to match the
7523 indicated template PARM. Returns the converted ARG, or
7524 error_mark_node if the conversion was unsuccessful. Error and
7525 warning messages are issued under control of COMPLAIN. This
7526 conversion is for the Ith parameter in the parameter list. ARGS is
7527 the full set of template arguments deduced so far. */
7529 static tree
7530 convert_template_argument (tree parm,
7531 tree arg,
7532 tree args,
7533 tsubst_flags_t complain,
7534 int i,
7535 tree in_decl)
7537 tree orig_arg;
7538 tree val;
7539 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7541 if (parm == error_mark_node)
7542 return error_mark_node;
7544 /* Trivially convert placeholders. */
7545 if (TREE_CODE (arg) == WILDCARD_DECL)
7546 return convert_wildcard_argument (parm, arg);
7548 if (arg == any_targ_node)
7549 return arg;
7551 if (TREE_CODE (arg) == TREE_LIST
7552 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7554 /* The template argument was the name of some
7555 member function. That's usually
7556 invalid, but static members are OK. In any
7557 case, grab the underlying fields/functions
7558 and issue an error later if required. */
7559 orig_arg = TREE_VALUE (arg);
7560 TREE_TYPE (arg) = unknown_type_node;
7563 orig_arg = arg;
7565 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7566 requires_type = (TREE_CODE (parm) == TYPE_DECL
7567 || requires_tmpl_type);
7569 /* When determining whether an argument pack expansion is a template,
7570 look at the pattern. */
7571 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7572 arg = PACK_EXPANSION_PATTERN (arg);
7574 /* Deal with an injected-class-name used as a template template arg. */
7575 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7577 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7578 if (TREE_CODE (t) == TEMPLATE_DECL)
7580 if (cxx_dialect >= cxx11)
7581 /* OK under DR 1004. */;
7582 else if (complain & tf_warning_or_error)
7583 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7584 " used as template template argument", TYPE_NAME (arg));
7585 else if (flag_pedantic_errors)
7586 t = arg;
7588 arg = t;
7592 is_tmpl_type =
7593 ((TREE_CODE (arg) == TEMPLATE_DECL
7594 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7595 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7596 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7597 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7599 if (is_tmpl_type
7600 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7601 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7602 arg = TYPE_STUB_DECL (arg);
7604 is_type = TYPE_P (arg) || is_tmpl_type;
7606 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7607 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7609 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7611 if (complain & tf_error)
7612 error ("invalid use of destructor %qE as a type", orig_arg);
7613 return error_mark_node;
7616 permerror (input_location,
7617 "to refer to a type member of a template parameter, "
7618 "use %<typename %E%>", orig_arg);
7620 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7621 TREE_OPERAND (arg, 1),
7622 typename_type,
7623 complain);
7624 arg = orig_arg;
7625 is_type = 1;
7627 if (is_type != requires_type)
7629 if (in_decl)
7631 if (complain & tf_error)
7633 error ("type/value mismatch at argument %d in template "
7634 "parameter list for %qD",
7635 i + 1, in_decl);
7636 if (is_type)
7637 inform (input_location,
7638 " expected a constant of type %qT, got %qT",
7639 TREE_TYPE (parm),
7640 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7641 else if (requires_tmpl_type)
7642 inform (input_location,
7643 " expected a class template, got %qE", orig_arg);
7644 else
7645 inform (input_location,
7646 " expected a type, got %qE", orig_arg);
7649 return error_mark_node;
7651 if (is_tmpl_type ^ requires_tmpl_type)
7653 if (in_decl && (complain & tf_error))
7655 error ("type/value mismatch at argument %d in template "
7656 "parameter list for %qD",
7657 i + 1, in_decl);
7658 if (is_tmpl_type)
7659 inform (input_location,
7660 " expected a type, got %qT", DECL_NAME (arg));
7661 else
7662 inform (input_location,
7663 " expected a class template, got %qT", orig_arg);
7665 return error_mark_node;
7668 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7669 /* We already did the appropriate conversion when packing args. */
7670 val = orig_arg;
7671 else if (is_type)
7673 if (requires_tmpl_type)
7675 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7676 /* The number of argument required is not known yet.
7677 Just accept it for now. */
7678 val = orig_arg;
7679 else
7681 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7682 tree argparm;
7684 /* Strip alias templates that are equivalent to another
7685 template. */
7686 arg = get_underlying_template (arg);
7687 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7689 if (coerce_template_template_parms (parmparm, argparm,
7690 complain, in_decl,
7691 args))
7693 val = arg;
7695 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7696 TEMPLATE_DECL. */
7697 if (val != error_mark_node)
7699 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7700 val = TREE_TYPE (val);
7701 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7702 val = make_pack_expansion (val, complain);
7705 else
7707 if (in_decl && (complain & tf_error))
7709 error ("type/value mismatch at argument %d in "
7710 "template parameter list for %qD",
7711 i + 1, in_decl);
7712 inform (input_location,
7713 " expected a template of type %qD, got %qT",
7714 parm, orig_arg);
7717 val = error_mark_node;
7720 // Check that the constraints are compatible before allowing the
7721 // substitution.
7722 if (val != error_mark_node)
7723 if (!is_compatible_template_arg (parm, arg))
7725 if (in_decl && (complain & tf_error))
7727 error ("constraint mismatch at argument %d in "
7728 "template parameter list for %qD",
7729 i + 1, in_decl);
7730 inform (input_location, " expected %qD but got %qD",
7731 parm, arg);
7733 val = error_mark_node;
7737 else
7738 val = orig_arg;
7739 /* We only form one instance of each template specialization.
7740 Therefore, if we use a non-canonical variant (i.e., a
7741 typedef), any future messages referring to the type will use
7742 the typedef, which is confusing if those future uses do not
7743 themselves also use the typedef. */
7744 if (TYPE_P (val))
7745 val = canonicalize_type_argument (val, complain);
7747 else
7749 tree t = TREE_TYPE (parm);
7751 if (tree a = type_uses_auto (t))
7753 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7754 if (t == error_mark_node)
7755 return error_mark_node;
7757 else
7758 t = tsubst (t, args, complain, in_decl);
7760 if (invalid_nontype_parm_type_p (t, complain))
7761 return error_mark_node;
7763 if (!type_dependent_expression_p (orig_arg)
7764 && !uses_template_parms (t))
7765 /* We used to call digest_init here. However, digest_init
7766 will report errors, which we don't want when complain
7767 is zero. More importantly, digest_init will try too
7768 hard to convert things: for example, `0' should not be
7769 converted to pointer type at this point according to
7770 the standard. Accepting this is not merely an
7771 extension, since deciding whether or not these
7772 conversions can occur is part of determining which
7773 function template to call, or whether a given explicit
7774 argument specification is valid. */
7775 val = convert_nontype_argument (t, orig_arg, complain);
7776 else
7777 val = canonicalize_expr_argument (orig_arg, complain);
7779 if (val == NULL_TREE)
7780 val = error_mark_node;
7781 else if (val == error_mark_node && (complain & tf_error))
7782 error ("could not convert template argument %qE from %qT to %qT",
7783 orig_arg, TREE_TYPE (orig_arg), t);
7785 if (INDIRECT_REF_P (val))
7787 /* Reject template arguments that are references to built-in
7788 functions with no library fallbacks. */
7789 const_tree inner = TREE_OPERAND (val, 0);
7790 const_tree innertype = TREE_TYPE (inner);
7791 if (innertype
7792 && TREE_CODE (innertype) == REFERENCE_TYPE
7793 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7794 && 0 < TREE_OPERAND_LENGTH (inner)
7795 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7796 return error_mark_node;
7799 if (TREE_CODE (val) == SCOPE_REF)
7801 /* Strip typedefs from the SCOPE_REF. */
7802 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7803 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7804 complain);
7805 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7806 QUALIFIED_NAME_IS_TEMPLATE (val));
7810 return val;
7813 /* Coerces the remaining template arguments in INNER_ARGS (from
7814 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7815 Returns the coerced argument pack. PARM_IDX is the position of this
7816 parameter in the template parameter list. ARGS is the original
7817 template argument list. */
7818 static tree
7819 coerce_template_parameter_pack (tree parms,
7820 int parm_idx,
7821 tree args,
7822 tree inner_args,
7823 int arg_idx,
7824 tree new_args,
7825 int* lost,
7826 tree in_decl,
7827 tsubst_flags_t complain)
7829 tree parm = TREE_VEC_ELT (parms, parm_idx);
7830 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7831 tree packed_args;
7832 tree argument_pack;
7833 tree packed_parms = NULL_TREE;
7835 if (arg_idx > nargs)
7836 arg_idx = nargs;
7838 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7840 /* When the template parameter is a non-type template parameter pack
7841 or template template parameter pack whose type or template
7842 parameters use parameter packs, we know exactly how many arguments
7843 we are looking for. Build a vector of the instantiated decls for
7844 these template parameters in PACKED_PARMS. */
7845 /* We can't use make_pack_expansion here because it would interpret a
7846 _DECL as a use rather than a declaration. */
7847 tree decl = TREE_VALUE (parm);
7848 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7849 SET_PACK_EXPANSION_PATTERN (exp, decl);
7850 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7851 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7853 TREE_VEC_LENGTH (args)--;
7854 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7855 TREE_VEC_LENGTH (args)++;
7857 if (packed_parms == error_mark_node)
7858 return error_mark_node;
7860 /* If we're doing a partial instantiation of a member template,
7861 verify that all of the types used for the non-type
7862 template parameter pack are, in fact, valid for non-type
7863 template parameters. */
7864 if (arg_idx < nargs
7865 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7867 int j, len = TREE_VEC_LENGTH (packed_parms);
7868 for (j = 0; j < len; ++j)
7870 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7871 if (invalid_nontype_parm_type_p (t, complain))
7872 return error_mark_node;
7874 /* We don't know how many args we have yet, just
7875 use the unconverted ones for now. */
7876 return NULL_TREE;
7879 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7881 /* Check if we have a placeholder pack, which indicates we're
7882 in the context of a introduction list. In that case we want
7883 to match this pack to the single placeholder. */
7884 else if (arg_idx < nargs
7885 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7886 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7888 nargs = arg_idx + 1;
7889 packed_args = make_tree_vec (1);
7891 else
7892 packed_args = make_tree_vec (nargs - arg_idx);
7894 /* Convert the remaining arguments, which will be a part of the
7895 parameter pack "parm". */
7896 int first_pack_arg = arg_idx;
7897 for (; arg_idx < nargs; ++arg_idx)
7899 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7900 tree actual_parm = TREE_VALUE (parm);
7901 int pack_idx = arg_idx - first_pack_arg;
7903 if (packed_parms)
7905 /* Once we've packed as many args as we have types, stop. */
7906 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7907 break;
7908 else if (PACK_EXPANSION_P (arg))
7909 /* We don't know how many args we have yet, just
7910 use the unconverted ones for now. */
7911 return NULL_TREE;
7912 else
7913 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7916 if (arg == error_mark_node)
7918 if (complain & tf_error)
7919 error ("template argument %d is invalid", arg_idx + 1);
7921 else
7922 arg = convert_template_argument (actual_parm,
7923 arg, new_args, complain, parm_idx,
7924 in_decl);
7925 if (arg == error_mark_node)
7926 (*lost)++;
7927 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7930 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7931 && TREE_VEC_LENGTH (packed_args) > 0)
7933 if (complain & tf_error)
7934 error ("wrong number of template arguments (%d, should be %d)",
7935 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7936 return error_mark_node;
7939 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7940 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7941 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7942 else
7944 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7945 TREE_CONSTANT (argument_pack) = 1;
7948 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7949 if (CHECKING_P)
7950 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7951 TREE_VEC_LENGTH (packed_args));
7952 return argument_pack;
7955 /* Returns the number of pack expansions in the template argument vector
7956 ARGS. */
7958 static int
7959 pack_expansion_args_count (tree args)
7961 int i;
7962 int count = 0;
7963 if (args)
7964 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7966 tree elt = TREE_VEC_ELT (args, i);
7967 if (elt && PACK_EXPANSION_P (elt))
7968 ++count;
7970 return count;
7973 /* Convert all template arguments to their appropriate types, and
7974 return a vector containing the innermost resulting template
7975 arguments. If any error occurs, return error_mark_node. Error and
7976 warning messages are issued under control of COMPLAIN.
7978 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7979 for arguments not specified in ARGS. Otherwise, if
7980 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7981 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7982 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7983 ARGS. */
7985 static tree
7986 coerce_template_parms (tree parms,
7987 tree args,
7988 tree in_decl,
7989 tsubst_flags_t complain,
7990 bool require_all_args,
7991 bool use_default_args)
7993 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7994 tree orig_inner_args;
7995 tree inner_args;
7996 tree new_args;
7997 tree new_inner_args;
7998 int saved_unevaluated_operand;
7999 int saved_inhibit_evaluation_warnings;
8001 /* When used as a boolean value, indicates whether this is a
8002 variadic template parameter list. Since it's an int, we can also
8003 subtract it from nparms to get the number of non-variadic
8004 parameters. */
8005 int variadic_p = 0;
8006 int variadic_args_p = 0;
8007 int post_variadic_parms = 0;
8009 /* Likewise for parameters with default arguments. */
8010 int default_p = 0;
8012 if (args == error_mark_node)
8013 return error_mark_node;
8015 nparms = TREE_VEC_LENGTH (parms);
8017 /* Determine if there are any parameter packs or default arguments. */
8018 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8020 tree parm = TREE_VEC_ELT (parms, parm_idx);
8021 if (variadic_p)
8022 ++post_variadic_parms;
8023 if (template_parameter_pack_p (TREE_VALUE (parm)))
8024 ++variadic_p;
8025 if (TREE_PURPOSE (parm))
8026 ++default_p;
8029 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8030 /* If there are no parameters that follow a parameter pack, we need to
8031 expand any argument packs so that we can deduce a parameter pack from
8032 some non-packed args followed by an argument pack, as in variadic85.C.
8033 If there are such parameters, we need to leave argument packs intact
8034 so the arguments are assigned properly. This can happen when dealing
8035 with a nested class inside a partial specialization of a class
8036 template, as in variadic92.C, or when deducing a template parameter pack
8037 from a sub-declarator, as in variadic114.C. */
8038 if (!post_variadic_parms)
8039 inner_args = expand_template_argument_pack (inner_args);
8041 /* Count any pack expansion args. */
8042 variadic_args_p = pack_expansion_args_count (inner_args);
8044 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8045 if ((nargs - variadic_args_p > nparms && !variadic_p)
8046 || (nargs < nparms - variadic_p
8047 && require_all_args
8048 && !variadic_args_p
8049 && (!use_default_args
8050 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8051 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8053 if (complain & tf_error)
8055 if (variadic_p || default_p)
8057 nparms -= variadic_p + default_p;
8058 error ("wrong number of template arguments "
8059 "(%d, should be at least %d)", nargs, nparms);
8061 else
8062 error ("wrong number of template arguments "
8063 "(%d, should be %d)", nargs, nparms);
8065 if (in_decl)
8066 inform (DECL_SOURCE_LOCATION (in_decl),
8067 "provided for %qD", in_decl);
8070 return error_mark_node;
8072 /* We can't pass a pack expansion to a non-pack parameter of an alias
8073 template (DR 1430). */
8074 else if (in_decl
8075 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8076 || concept_template_p (in_decl))
8077 && variadic_args_p
8078 && nargs - variadic_args_p < nparms - variadic_p)
8080 if (complain & tf_error)
8082 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8084 tree arg = TREE_VEC_ELT (inner_args, i);
8085 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8087 if (PACK_EXPANSION_P (arg)
8088 && !template_parameter_pack_p (parm))
8090 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8091 error_at (location_of (arg),
8092 "pack expansion argument for non-pack parameter "
8093 "%qD of alias template %qD", parm, in_decl);
8094 else
8095 error_at (location_of (arg),
8096 "pack expansion argument for non-pack parameter "
8097 "%qD of concept %qD", parm, in_decl);
8098 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8099 goto found;
8102 gcc_unreachable ();
8103 found:;
8105 return error_mark_node;
8108 /* We need to evaluate the template arguments, even though this
8109 template-id may be nested within a "sizeof". */
8110 saved_unevaluated_operand = cp_unevaluated_operand;
8111 cp_unevaluated_operand = 0;
8112 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8113 c_inhibit_evaluation_warnings = 0;
8114 new_inner_args = make_tree_vec (nparms);
8115 new_args = add_outermost_template_args (args, new_inner_args);
8116 int pack_adjust = 0;
8117 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8119 tree arg;
8120 tree parm;
8122 /* Get the Ith template parameter. */
8123 parm = TREE_VEC_ELT (parms, parm_idx);
8125 if (parm == error_mark_node)
8127 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8128 continue;
8131 /* Calculate the next argument. */
8132 if (arg_idx < nargs)
8133 arg = TREE_VEC_ELT (inner_args, arg_idx);
8134 else
8135 arg = NULL_TREE;
8137 if (template_parameter_pack_p (TREE_VALUE (parm))
8138 && !(arg && ARGUMENT_PACK_P (arg)))
8140 /* Some arguments will be placed in the
8141 template parameter pack PARM. */
8142 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8143 inner_args, arg_idx,
8144 new_args, &lost,
8145 in_decl, complain);
8147 if (arg == NULL_TREE)
8149 /* We don't know how many args we have yet, just use the
8150 unconverted (and still packed) ones for now. */
8151 new_inner_args = orig_inner_args;
8152 arg_idx = nargs;
8153 break;
8156 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8158 /* Store this argument. */
8159 if (arg == error_mark_node)
8161 lost++;
8162 /* We are done with all of the arguments. */
8163 arg_idx = nargs;
8165 else
8167 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8168 arg_idx += pack_adjust;
8171 continue;
8173 else if (arg)
8175 if (PACK_EXPANSION_P (arg))
8177 /* "If every valid specialization of a variadic template
8178 requires an empty template parameter pack, the template is
8179 ill-formed, no diagnostic required." So check that the
8180 pattern works with this parameter. */
8181 tree pattern = PACK_EXPANSION_PATTERN (arg);
8182 tree conv = convert_template_argument (TREE_VALUE (parm),
8183 pattern, new_args,
8184 complain, parm_idx,
8185 in_decl);
8186 if (conv == error_mark_node)
8188 if (complain & tf_error)
8189 inform (input_location, "so any instantiation with a "
8190 "non-empty parameter pack would be ill-formed");
8191 ++lost;
8193 else if (TYPE_P (conv) && !TYPE_P (pattern))
8194 /* Recover from missing typename. */
8195 TREE_VEC_ELT (inner_args, arg_idx)
8196 = make_pack_expansion (conv, complain);
8198 /* We don't know how many args we have yet, just
8199 use the unconverted ones for now. */
8200 new_inner_args = inner_args;
8201 arg_idx = nargs;
8202 break;
8205 else if (require_all_args)
8207 /* There must be a default arg in this case. */
8208 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8209 complain, in_decl);
8210 /* The position of the first default template argument,
8211 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8212 Record that. */
8213 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8214 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8215 arg_idx - pack_adjust);
8217 else
8218 break;
8220 if (arg == error_mark_node)
8222 if (complain & tf_error)
8223 error ("template argument %d is invalid", arg_idx + 1);
8225 else if (!arg)
8226 /* This only occurs if there was an error in the template
8227 parameter list itself (which we would already have
8228 reported) that we are trying to recover from, e.g., a class
8229 template with a parameter list such as
8230 template<typename..., typename>. */
8231 ++lost;
8232 else
8233 arg = convert_template_argument (TREE_VALUE (parm),
8234 arg, new_args, complain,
8235 parm_idx, in_decl);
8237 if (arg == error_mark_node)
8238 lost++;
8239 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8241 cp_unevaluated_operand = saved_unevaluated_operand;
8242 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8244 if (variadic_p && arg_idx < nargs)
8246 if (complain & tf_error)
8248 error ("wrong number of template arguments "
8249 "(%d, should be %d)", nargs, arg_idx);
8250 if (in_decl)
8251 error ("provided for %q+D", in_decl);
8253 return error_mark_node;
8256 if (lost)
8257 return error_mark_node;
8259 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8260 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8261 TREE_VEC_LENGTH (new_inner_args));
8263 return new_inner_args;
8266 /* Convert all template arguments to their appropriate types, and
8267 return a vector containing the innermost resulting template
8268 arguments. If any error occurs, return error_mark_node. Error and
8269 warning messages are not issued.
8271 Note that no function argument deduction is performed, and default
8272 arguments are used to fill in unspecified arguments. */
8273 tree
8274 coerce_template_parms (tree parms, tree args, tree in_decl)
8276 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8279 /* Convert all template arguments to their appropriate type, and
8280 instantiate default arguments as needed. This returns a vector
8281 containing the innermost resulting template arguments, or
8282 error_mark_node if unsuccessful. */
8283 tree
8284 coerce_template_parms (tree parms, tree args, tree in_decl,
8285 tsubst_flags_t complain)
8287 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8290 /* Like coerce_template_parms. If PARMS represents all template
8291 parameters levels, this function returns a vector of vectors
8292 representing all the resulting argument levels. Note that in this
8293 case, only the innermost arguments are coerced because the
8294 outermost ones are supposed to have been coerced already.
8296 Otherwise, if PARMS represents only (the innermost) vector of
8297 parameters, this function returns a vector containing just the
8298 innermost resulting arguments. */
8300 static tree
8301 coerce_innermost_template_parms (tree parms,
8302 tree args,
8303 tree in_decl,
8304 tsubst_flags_t complain,
8305 bool require_all_args,
8306 bool use_default_args)
8308 int parms_depth = TMPL_PARMS_DEPTH (parms);
8309 int args_depth = TMPL_ARGS_DEPTH (args);
8310 tree coerced_args;
8312 if (parms_depth > 1)
8314 coerced_args = make_tree_vec (parms_depth);
8315 tree level;
8316 int cur_depth;
8318 for (level = parms, cur_depth = parms_depth;
8319 parms_depth > 0 && level != NULL_TREE;
8320 level = TREE_CHAIN (level), --cur_depth)
8322 tree l;
8323 if (cur_depth == args_depth)
8324 l = coerce_template_parms (TREE_VALUE (level),
8325 args, in_decl, complain,
8326 require_all_args,
8327 use_default_args);
8328 else
8329 l = TMPL_ARGS_LEVEL (args, cur_depth);
8331 if (l == error_mark_node)
8332 return error_mark_node;
8334 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8337 else
8338 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8339 args, in_decl, complain,
8340 require_all_args,
8341 use_default_args);
8342 return coerced_args;
8345 /* Returns 1 if template args OT and NT are equivalent. */
8348 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8350 if (nt == ot)
8351 return 1;
8352 if (nt == NULL_TREE || ot == NULL_TREE)
8353 return false;
8354 if (nt == any_targ_node || ot == any_targ_node)
8355 return true;
8357 if (TREE_CODE (nt) == TREE_VEC)
8358 /* For member templates */
8359 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8360 else if (PACK_EXPANSION_P (ot))
8361 return (PACK_EXPANSION_P (nt)
8362 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8363 PACK_EXPANSION_PATTERN (nt))
8364 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8365 PACK_EXPANSION_EXTRA_ARGS (nt)));
8366 else if (ARGUMENT_PACK_P (ot))
8368 int i, len;
8369 tree opack, npack;
8371 if (!ARGUMENT_PACK_P (nt))
8372 return 0;
8374 opack = ARGUMENT_PACK_ARGS (ot);
8375 npack = ARGUMENT_PACK_ARGS (nt);
8376 len = TREE_VEC_LENGTH (opack);
8377 if (TREE_VEC_LENGTH (npack) != len)
8378 return 0;
8379 for (i = 0; i < len; ++i)
8380 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8381 TREE_VEC_ELT (npack, i)))
8382 return 0;
8383 return 1;
8385 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8386 gcc_unreachable ();
8387 else if (TYPE_P (nt))
8389 if (!TYPE_P (ot))
8390 return false;
8391 /* Don't treat an alias template specialization with dependent
8392 arguments as equivalent to its underlying type when used as a
8393 template argument; we need them to be distinct so that we
8394 substitute into the specialization arguments at instantiation
8395 time. And aliases can't be equivalent without being ==, so
8396 we don't need to look any deeper.
8398 During partial ordering, however, we need to treat them normally so
8399 that we can order uses of the same alias with different
8400 cv-qualification (79960). */
8401 if (!partial_order
8402 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8403 return false;
8404 else
8405 return same_type_p (ot, nt);
8407 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8408 return 0;
8409 else
8411 /* Try to treat a template non-type argument that has been converted
8412 to the parameter type as equivalent to one that hasn't yet. */
8413 for (enum tree_code code1 = TREE_CODE (ot);
8414 CONVERT_EXPR_CODE_P (code1)
8415 || code1 == NON_LVALUE_EXPR;
8416 code1 = TREE_CODE (ot))
8417 ot = TREE_OPERAND (ot, 0);
8418 for (enum tree_code code2 = TREE_CODE (nt);
8419 CONVERT_EXPR_CODE_P (code2)
8420 || code2 == NON_LVALUE_EXPR;
8421 code2 = TREE_CODE (nt))
8422 nt = TREE_OPERAND (nt, 0);
8424 return cp_tree_equal (ot, nt);
8428 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8429 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8430 NEWARG_PTR with the offending arguments if they are non-NULL. */
8433 comp_template_args (tree oldargs, tree newargs,
8434 tree *oldarg_ptr, tree *newarg_ptr,
8435 bool partial_order)
8437 int i;
8439 if (oldargs == newargs)
8440 return 1;
8442 if (!oldargs || !newargs)
8443 return 0;
8445 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8446 return 0;
8448 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8450 tree nt = TREE_VEC_ELT (newargs, i);
8451 tree ot = TREE_VEC_ELT (oldargs, i);
8453 if (! template_args_equal (ot, nt, partial_order))
8455 if (oldarg_ptr != NULL)
8456 *oldarg_ptr = ot;
8457 if (newarg_ptr != NULL)
8458 *newarg_ptr = nt;
8459 return 0;
8462 return 1;
8465 inline bool
8466 comp_template_args_porder (tree oargs, tree nargs)
8468 return comp_template_args (oargs, nargs, NULL, NULL, true);
8471 static void
8472 add_pending_template (tree d)
8474 tree ti = (TYPE_P (d)
8475 ? CLASSTYPE_TEMPLATE_INFO (d)
8476 : DECL_TEMPLATE_INFO (d));
8477 struct pending_template *pt;
8478 int level;
8480 if (TI_PENDING_TEMPLATE_FLAG (ti))
8481 return;
8483 /* We are called both from instantiate_decl, where we've already had a
8484 tinst_level pushed, and instantiate_template, where we haven't.
8485 Compensate. */
8486 level = !current_tinst_level || current_tinst_level->decl != d;
8488 if (level)
8489 push_tinst_level (d);
8491 pt = ggc_alloc<pending_template> ();
8492 pt->next = NULL;
8493 pt->tinst = current_tinst_level;
8494 if (last_pending_template)
8495 last_pending_template->next = pt;
8496 else
8497 pending_templates = pt;
8499 last_pending_template = pt;
8501 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8503 if (level)
8504 pop_tinst_level ();
8508 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8509 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8510 documentation for TEMPLATE_ID_EXPR. */
8512 tree
8513 lookup_template_function (tree fns, tree arglist)
8515 tree type;
8517 if (fns == error_mark_node || arglist == error_mark_node)
8518 return error_mark_node;
8520 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8522 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8524 error ("%q#D is not a function template", fns);
8525 return error_mark_node;
8528 if (BASELINK_P (fns))
8530 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8531 unknown_type_node,
8532 BASELINK_FUNCTIONS (fns),
8533 arglist);
8534 return fns;
8537 type = TREE_TYPE (fns);
8538 if (TREE_CODE (fns) == OVERLOAD || !type)
8539 type = unknown_type_node;
8541 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8544 /* Within the scope of a template class S<T>, the name S gets bound
8545 (in build_self_reference) to a TYPE_DECL for the class, not a
8546 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8547 or one of its enclosing classes, and that type is a template,
8548 return the associated TEMPLATE_DECL. Otherwise, the original
8549 DECL is returned.
8551 Also handle the case when DECL is a TREE_LIST of ambiguous
8552 injected-class-names from different bases. */
8554 tree
8555 maybe_get_template_decl_from_type_decl (tree decl)
8557 if (decl == NULL_TREE)
8558 return decl;
8560 /* DR 176: A lookup that finds an injected-class-name (10.2
8561 [class.member.lookup]) can result in an ambiguity in certain cases
8562 (for example, if it is found in more than one base class). If all of
8563 the injected-class-names that are found refer to specializations of
8564 the same class template, and if the name is followed by a
8565 template-argument-list, the reference refers to the class template
8566 itself and not a specialization thereof, and is not ambiguous. */
8567 if (TREE_CODE (decl) == TREE_LIST)
8569 tree t, tmpl = NULL_TREE;
8570 for (t = decl; t; t = TREE_CHAIN (t))
8572 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8573 if (!tmpl)
8574 tmpl = elt;
8575 else if (tmpl != elt)
8576 break;
8578 if (tmpl && t == NULL_TREE)
8579 return tmpl;
8580 else
8581 return decl;
8584 return (decl != NULL_TREE
8585 && DECL_SELF_REFERENCE_P (decl)
8586 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8587 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8590 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8591 parameters, find the desired type.
8593 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8595 IN_DECL, if non-NULL, is the template declaration we are trying to
8596 instantiate.
8598 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8599 the class we are looking up.
8601 Issue error and warning messages under control of COMPLAIN.
8603 If the template class is really a local class in a template
8604 function, then the FUNCTION_CONTEXT is the function in which it is
8605 being instantiated.
8607 ??? Note that this function is currently called *twice* for each
8608 template-id: the first time from the parser, while creating the
8609 incomplete type (finish_template_type), and the second type during the
8610 real instantiation (instantiate_template_class). This is surely something
8611 that we want to avoid. It also causes some problems with argument
8612 coercion (see convert_nontype_argument for more information on this). */
8614 static tree
8615 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8616 int entering_scope, tsubst_flags_t complain)
8618 tree templ = NULL_TREE, parmlist;
8619 tree t;
8620 spec_entry **slot;
8621 spec_entry *entry;
8622 spec_entry elt;
8623 hashval_t hash;
8625 if (identifier_p (d1))
8627 tree value = innermost_non_namespace_value (d1);
8628 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8629 templ = value;
8630 else
8632 if (context)
8633 push_decl_namespace (context);
8634 templ = lookup_name (d1);
8635 templ = maybe_get_template_decl_from_type_decl (templ);
8636 if (context)
8637 pop_decl_namespace ();
8639 if (templ)
8640 context = DECL_CONTEXT (templ);
8642 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8644 tree type = TREE_TYPE (d1);
8646 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8647 an implicit typename for the second A. Deal with it. */
8648 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8649 type = TREE_TYPE (type);
8651 if (CLASSTYPE_TEMPLATE_INFO (type))
8653 templ = CLASSTYPE_TI_TEMPLATE (type);
8654 d1 = DECL_NAME (templ);
8657 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8658 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8660 templ = TYPE_TI_TEMPLATE (d1);
8661 d1 = DECL_NAME (templ);
8663 else if (DECL_TYPE_TEMPLATE_P (d1))
8665 templ = d1;
8666 d1 = DECL_NAME (templ);
8667 context = DECL_CONTEXT (templ);
8669 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8671 templ = d1;
8672 d1 = DECL_NAME (templ);
8675 /* Issue an error message if we didn't find a template. */
8676 if (! templ)
8678 if (complain & tf_error)
8679 error ("%qT is not a template", d1);
8680 return error_mark_node;
8683 if (TREE_CODE (templ) != TEMPLATE_DECL
8684 /* Make sure it's a user visible template, if it was named by
8685 the user. */
8686 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8687 && !PRIMARY_TEMPLATE_P (templ)))
8689 if (complain & tf_error)
8691 error ("non-template type %qT used as a template", d1);
8692 if (in_decl)
8693 error ("for template declaration %q+D", in_decl);
8695 return error_mark_node;
8698 complain &= ~tf_user;
8700 /* An alias that just changes the name of a template is equivalent to the
8701 other template, so if any of the arguments are pack expansions, strip
8702 the alias to avoid problems with a pack expansion passed to a non-pack
8703 alias template parameter (DR 1430). */
8704 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8705 templ = get_underlying_template (templ);
8707 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8709 tree parm;
8710 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8711 if (arglist2 == error_mark_node
8712 || (!uses_template_parms (arglist2)
8713 && check_instantiated_args (templ, arglist2, complain)))
8714 return error_mark_node;
8716 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8717 return parm;
8719 else
8721 tree template_type = TREE_TYPE (templ);
8722 tree gen_tmpl;
8723 tree type_decl;
8724 tree found = NULL_TREE;
8725 int arg_depth;
8726 int parm_depth;
8727 int is_dependent_type;
8728 int use_partial_inst_tmpl = false;
8730 if (template_type == error_mark_node)
8731 /* An error occurred while building the template TEMPL, and a
8732 diagnostic has most certainly been emitted for that
8733 already. Let's propagate that error. */
8734 return error_mark_node;
8736 gen_tmpl = most_general_template (templ);
8737 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8738 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8739 arg_depth = TMPL_ARGS_DEPTH (arglist);
8741 if (arg_depth == 1 && parm_depth > 1)
8743 /* We've been given an incomplete set of template arguments.
8744 For example, given:
8746 template <class T> struct S1 {
8747 template <class U> struct S2 {};
8748 template <class U> struct S2<U*> {};
8751 we will be called with an ARGLIST of `U*', but the
8752 TEMPLATE will be `template <class T> template
8753 <class U> struct S1<T>::S2'. We must fill in the missing
8754 arguments. */
8755 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8756 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8757 arg_depth = TMPL_ARGS_DEPTH (arglist);
8760 /* Now we should have enough arguments. */
8761 gcc_assert (parm_depth == arg_depth);
8763 /* From here on, we're only interested in the most general
8764 template. */
8766 /* Calculate the BOUND_ARGS. These will be the args that are
8767 actually tsubst'd into the definition to create the
8768 instantiation. */
8769 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8770 complain,
8771 /*require_all_args=*/true,
8772 /*use_default_args=*/true);
8774 if (arglist == error_mark_node)
8775 /* We were unable to bind the arguments. */
8776 return error_mark_node;
8778 /* In the scope of a template class, explicit references to the
8779 template class refer to the type of the template, not any
8780 instantiation of it. For example, in:
8782 template <class T> class C { void f(C<T>); }
8784 the `C<T>' is just the same as `C'. Outside of the
8785 class, however, such a reference is an instantiation. */
8786 if (entering_scope
8787 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8788 || currently_open_class (template_type))
8790 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8792 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8793 return template_type;
8796 /* If we already have this specialization, return it. */
8797 elt.tmpl = gen_tmpl;
8798 elt.args = arglist;
8799 elt.spec = NULL_TREE;
8800 hash = spec_hasher::hash (&elt);
8801 entry = type_specializations->find_with_hash (&elt, hash);
8803 if (entry)
8804 return entry->spec;
8806 /* If the the template's constraints are not satisfied,
8807 then we cannot form a valid type.
8809 Note that the check is deferred until after the hash
8810 lookup. This prevents redundant checks on previously
8811 instantiated specializations. */
8812 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8814 if (complain & tf_error)
8816 error ("template constraint failure");
8817 diagnose_constraints (input_location, gen_tmpl, arglist);
8819 return error_mark_node;
8822 is_dependent_type = uses_template_parms (arglist);
8824 /* If the deduced arguments are invalid, then the binding
8825 failed. */
8826 if (!is_dependent_type
8827 && check_instantiated_args (gen_tmpl,
8828 INNERMOST_TEMPLATE_ARGS (arglist),
8829 complain))
8830 return error_mark_node;
8832 if (!is_dependent_type
8833 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8834 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8835 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8837 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8838 DECL_NAME (gen_tmpl),
8839 /*tag_scope=*/ts_global);
8840 return found;
8843 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8844 complain, in_decl);
8845 if (context == error_mark_node)
8846 return error_mark_node;
8848 if (!context)
8849 context = global_namespace;
8851 /* Create the type. */
8852 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8854 /* The user referred to a specialization of an alias
8855 template represented by GEN_TMPL.
8857 [temp.alias]/2 says:
8859 When a template-id refers to the specialization of an
8860 alias template, it is equivalent to the associated
8861 type obtained by substitution of its
8862 template-arguments for the template-parameters in the
8863 type-id of the alias template. */
8865 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8866 /* Note that the call above (by indirectly calling
8867 register_specialization in tsubst_decl) registers the
8868 TYPE_DECL representing the specialization of the alias
8869 template. So next time someone substitutes ARGLIST for
8870 the template parms into the alias template (GEN_TMPL),
8871 she'll get that TYPE_DECL back. */
8873 if (t == error_mark_node)
8874 return t;
8876 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8878 if (!is_dependent_type)
8880 set_current_access_from_decl (TYPE_NAME (template_type));
8881 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8882 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8883 arglist, complain, in_decl),
8884 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8885 arglist, complain, in_decl),
8886 SCOPED_ENUM_P (template_type), NULL);
8888 if (t == error_mark_node)
8889 return t;
8891 else
8893 /* We don't want to call start_enum for this type, since
8894 the values for the enumeration constants may involve
8895 template parameters. And, no one should be interested
8896 in the enumeration constants for such a type. */
8897 t = cxx_make_type (ENUMERAL_TYPE);
8898 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8900 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8901 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8902 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8904 else if (CLASS_TYPE_P (template_type))
8906 t = make_class_type (TREE_CODE (template_type));
8907 CLASSTYPE_DECLARED_CLASS (t)
8908 = CLASSTYPE_DECLARED_CLASS (template_type);
8909 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8911 /* A local class. Make sure the decl gets registered properly. */
8912 if (context == current_function_decl)
8913 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8915 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8916 /* This instantiation is another name for the primary
8917 template type. Set the TYPE_CANONICAL field
8918 appropriately. */
8919 TYPE_CANONICAL (t) = template_type;
8920 else if (any_template_arguments_need_structural_equality_p (arglist))
8921 /* Some of the template arguments require structural
8922 equality testing, so this template class requires
8923 structural equality testing. */
8924 SET_TYPE_STRUCTURAL_EQUALITY (t);
8926 else
8927 gcc_unreachable ();
8929 /* If we called start_enum or pushtag above, this information
8930 will already be set up. */
8931 if (!TYPE_NAME (t))
8933 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8935 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8936 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8937 DECL_SOURCE_LOCATION (type_decl)
8938 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8940 else
8941 type_decl = TYPE_NAME (t);
8943 if (CLASS_TYPE_P (template_type))
8945 TREE_PRIVATE (type_decl)
8946 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8947 TREE_PROTECTED (type_decl)
8948 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8949 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8951 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8952 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8956 if (OVERLOAD_TYPE_P (t)
8957 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8959 static const char *tags[] = {"abi_tag", "may_alias"};
8961 for (unsigned ix = 0; ix != 2; ix++)
8963 tree attributes
8964 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8966 if (attributes)
8967 TYPE_ATTRIBUTES (t)
8968 = tree_cons (TREE_PURPOSE (attributes),
8969 TREE_VALUE (attributes),
8970 TYPE_ATTRIBUTES (t));
8974 /* Let's consider the explicit specialization of a member
8975 of a class template specialization that is implicitly instantiated,
8976 e.g.:
8977 template<class T>
8978 struct S
8980 template<class U> struct M {}; //#0
8983 template<>
8984 template<>
8985 struct S<int>::M<char> //#1
8987 int i;
8989 [temp.expl.spec]/4 says this is valid.
8991 In this case, when we write:
8992 S<int>::M<char> m;
8994 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8995 the one of #0.
8997 When we encounter #1, we want to store the partial instantiation
8998 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9000 For all cases other than this "explicit specialization of member of a
9001 class template", we just want to store the most general template into
9002 the CLASSTYPE_TI_TEMPLATE of M.
9004 This case of "explicit specialization of member of a class template"
9005 only happens when:
9006 1/ the enclosing class is an instantiation of, and therefore not
9007 the same as, the context of the most general template, and
9008 2/ we aren't looking at the partial instantiation itself, i.e.
9009 the innermost arguments are not the same as the innermost parms of
9010 the most general template.
9012 So it's only when 1/ and 2/ happens that we want to use the partial
9013 instantiation of the member template in lieu of its most general
9014 template. */
9016 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9017 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9018 /* the enclosing class must be an instantiation... */
9019 && CLASS_TYPE_P (context)
9020 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9022 TREE_VEC_LENGTH (arglist)--;
9023 ++processing_template_decl;
9024 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9025 tree partial_inst_args =
9026 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9027 arglist, complain, NULL_TREE);
9028 --processing_template_decl;
9029 TREE_VEC_LENGTH (arglist)++;
9030 if (partial_inst_args == error_mark_node)
9031 return error_mark_node;
9032 use_partial_inst_tmpl =
9033 /*...and we must not be looking at the partial instantiation
9034 itself. */
9035 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9036 partial_inst_args);
9039 if (!use_partial_inst_tmpl)
9040 /* This case is easy; there are no member templates involved. */
9041 found = gen_tmpl;
9042 else
9044 /* This is a full instantiation of a member template. Find
9045 the partial instantiation of which this is an instance. */
9047 /* Temporarily reduce by one the number of levels in the ARGLIST
9048 so as to avoid comparing the last set of arguments. */
9049 TREE_VEC_LENGTH (arglist)--;
9050 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9051 TREE_VEC_LENGTH (arglist)++;
9052 /* FOUND is either a proper class type, or an alias
9053 template specialization. In the later case, it's a
9054 TYPE_DECL, resulting from the substituting of arguments
9055 for parameters in the TYPE_DECL of the alias template
9056 done earlier. So be careful while getting the template
9057 of FOUND. */
9058 found = (TREE_CODE (found) == TEMPLATE_DECL
9059 ? found
9060 : (TREE_CODE (found) == TYPE_DECL
9061 ? DECL_TI_TEMPLATE (found)
9062 : CLASSTYPE_TI_TEMPLATE (found)));
9065 // Build template info for the new specialization.
9066 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9068 elt.spec = t;
9069 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9070 entry = ggc_alloc<spec_entry> ();
9071 *entry = elt;
9072 *slot = entry;
9074 /* Note this use of the partial instantiation so we can check it
9075 later in maybe_process_partial_specialization. */
9076 DECL_TEMPLATE_INSTANTIATIONS (found)
9077 = tree_cons (arglist, t,
9078 DECL_TEMPLATE_INSTANTIATIONS (found));
9080 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9081 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9082 /* Now that the type has been registered on the instantiations
9083 list, we set up the enumerators. Because the enumeration
9084 constants may involve the enumeration type itself, we make
9085 sure to register the type first, and then create the
9086 constants. That way, doing tsubst_expr for the enumeration
9087 constants won't result in recursive calls here; we'll find
9088 the instantiation and exit above. */
9089 tsubst_enum (template_type, t, arglist);
9091 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9092 /* If the type makes use of template parameters, the
9093 code that generates debugging information will crash. */
9094 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9096 /* Possibly limit visibility based on template args. */
9097 TREE_PUBLIC (type_decl) = 1;
9098 determine_visibility (type_decl);
9100 inherit_targ_abi_tags (t);
9102 return t;
9106 /* Wrapper for lookup_template_class_1. */
9108 tree
9109 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9110 int entering_scope, tsubst_flags_t complain)
9112 tree ret;
9113 timevar_push (TV_TEMPLATE_INST);
9114 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9115 entering_scope, complain);
9116 timevar_pop (TV_TEMPLATE_INST);
9117 return ret;
9120 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9122 tree
9123 lookup_template_variable (tree templ, tree arglist)
9125 /* The type of the expression is NULL_TREE since the template-id could refer
9126 to an explicit or partial specialization. */
9127 tree type = NULL_TREE;
9128 if (flag_concepts && variable_concept_p (templ))
9129 /* Except that concepts are always bool. */
9130 type = boolean_type_node;
9131 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9134 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9136 tree
9137 finish_template_variable (tree var, tsubst_flags_t complain)
9139 tree templ = TREE_OPERAND (var, 0);
9140 tree arglist = TREE_OPERAND (var, 1);
9142 /* We never want to return a VAR_DECL for a variable concept, since they
9143 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9144 bool concept_p = flag_concepts && variable_concept_p (templ);
9145 if (concept_p && processing_template_decl)
9146 return var;
9148 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9149 arglist = add_outermost_template_args (tmpl_args, arglist);
9151 templ = most_general_template (templ);
9152 tree parms = DECL_TEMPLATE_PARMS (templ);
9153 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9154 /*req_all*/true,
9155 /*use_default*/true);
9157 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9159 if (complain & tf_error)
9161 error ("use of invalid variable template %qE", var);
9162 diagnose_constraints (location_of (var), templ, arglist);
9164 return error_mark_node;
9167 /* If a template-id refers to a specialization of a variable
9168 concept, then the expression is true if and only if the
9169 concept's constraints are satisfied by the given template
9170 arguments.
9172 NOTE: This is an extension of Concepts Lite TS that
9173 allows constraints to be used in expressions. */
9174 if (concept_p)
9176 tree decl = DECL_TEMPLATE_RESULT (templ);
9177 return evaluate_variable_concept (decl, arglist);
9180 return instantiate_template (templ, arglist, complain);
9183 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9184 TARGS template args, and instantiate it if it's not dependent. */
9186 tree
9187 lookup_and_finish_template_variable (tree templ, tree targs,
9188 tsubst_flags_t complain)
9190 templ = lookup_template_variable (templ, targs);
9191 if (!any_dependent_template_arguments_p (targs))
9193 templ = finish_template_variable (templ, complain);
9194 mark_used (templ);
9197 return convert_from_reference (templ);
9201 struct pair_fn_data
9203 tree_fn_t fn;
9204 tree_fn_t any_fn;
9205 void *data;
9206 /* True when we should also visit template parameters that occur in
9207 non-deduced contexts. */
9208 bool include_nondeduced_p;
9209 hash_set<tree> *visited;
9212 /* Called from for_each_template_parm via walk_tree. */
9214 static tree
9215 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9217 tree t = *tp;
9218 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9219 tree_fn_t fn = pfd->fn;
9220 void *data = pfd->data;
9221 tree result = NULL_TREE;
9223 #define WALK_SUBTREE(NODE) \
9224 do \
9226 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9227 pfd->include_nondeduced_p, \
9228 pfd->any_fn); \
9229 if (result) goto out; \
9231 while (0)
9233 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9234 return t;
9236 if (TYPE_P (t)
9237 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9238 WALK_SUBTREE (TYPE_CONTEXT (t));
9240 switch (TREE_CODE (t))
9242 case RECORD_TYPE:
9243 if (TYPE_PTRMEMFUNC_P (t))
9244 break;
9245 /* Fall through. */
9247 case UNION_TYPE:
9248 case ENUMERAL_TYPE:
9249 if (!TYPE_TEMPLATE_INFO (t))
9250 *walk_subtrees = 0;
9251 else
9252 WALK_SUBTREE (TYPE_TI_ARGS (t));
9253 break;
9255 case INTEGER_TYPE:
9256 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9257 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9258 break;
9260 case METHOD_TYPE:
9261 /* Since we're not going to walk subtrees, we have to do this
9262 explicitly here. */
9263 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9264 /* Fall through. */
9266 case FUNCTION_TYPE:
9267 /* Check the return type. */
9268 WALK_SUBTREE (TREE_TYPE (t));
9270 /* Check the parameter types. Since default arguments are not
9271 instantiated until they are needed, the TYPE_ARG_TYPES may
9272 contain expressions that involve template parameters. But,
9273 no-one should be looking at them yet. And, once they're
9274 instantiated, they don't contain template parameters, so
9275 there's no point in looking at them then, either. */
9277 tree parm;
9279 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9280 WALK_SUBTREE (TREE_VALUE (parm));
9282 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9283 want walk_tree walking into them itself. */
9284 *walk_subtrees = 0;
9287 if (flag_noexcept_type)
9289 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9290 if (spec)
9291 WALK_SUBTREE (TREE_PURPOSE (spec));
9293 break;
9295 case TYPEOF_TYPE:
9296 case UNDERLYING_TYPE:
9297 if (pfd->include_nondeduced_p
9298 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9299 pfd->visited,
9300 pfd->include_nondeduced_p,
9301 pfd->any_fn))
9302 return error_mark_node;
9303 break;
9305 case FUNCTION_DECL:
9306 case VAR_DECL:
9307 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9308 WALK_SUBTREE (DECL_TI_ARGS (t));
9309 /* Fall through. */
9311 case PARM_DECL:
9312 case CONST_DECL:
9313 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9314 WALK_SUBTREE (DECL_INITIAL (t));
9315 if (DECL_CONTEXT (t)
9316 && pfd->include_nondeduced_p)
9317 WALK_SUBTREE (DECL_CONTEXT (t));
9318 break;
9320 case BOUND_TEMPLATE_TEMPLATE_PARM:
9321 /* Record template parameters such as `T' inside `TT<T>'. */
9322 WALK_SUBTREE (TYPE_TI_ARGS (t));
9323 /* Fall through. */
9325 case TEMPLATE_TEMPLATE_PARM:
9326 case TEMPLATE_TYPE_PARM:
9327 case TEMPLATE_PARM_INDEX:
9328 if (fn && (*fn)(t, data))
9329 return t;
9330 else if (!fn)
9331 return t;
9332 break;
9334 case TEMPLATE_DECL:
9335 /* A template template parameter is encountered. */
9336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9337 WALK_SUBTREE (TREE_TYPE (t));
9339 /* Already substituted template template parameter */
9340 *walk_subtrees = 0;
9341 break;
9343 case TYPENAME_TYPE:
9344 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9345 partial instantiation. */
9346 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9347 break;
9349 case CONSTRUCTOR:
9350 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9351 && pfd->include_nondeduced_p)
9352 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9353 break;
9355 case INDIRECT_REF:
9356 case COMPONENT_REF:
9357 /* If there's no type, then this thing must be some expression
9358 involving template parameters. */
9359 if (!fn && !TREE_TYPE (t))
9360 return error_mark_node;
9361 break;
9363 case MODOP_EXPR:
9364 case CAST_EXPR:
9365 case IMPLICIT_CONV_EXPR:
9366 case REINTERPRET_CAST_EXPR:
9367 case CONST_CAST_EXPR:
9368 case STATIC_CAST_EXPR:
9369 case DYNAMIC_CAST_EXPR:
9370 case ARROW_EXPR:
9371 case DOTSTAR_EXPR:
9372 case TYPEID_EXPR:
9373 case PSEUDO_DTOR_EXPR:
9374 if (!fn)
9375 return error_mark_node;
9376 break;
9378 default:
9379 break;
9382 #undef WALK_SUBTREE
9384 /* We didn't find any template parameters we liked. */
9385 out:
9386 return result;
9389 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9390 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9391 call FN with the parameter and the DATA.
9392 If FN returns nonzero, the iteration is terminated, and
9393 for_each_template_parm returns 1. Otherwise, the iteration
9394 continues. If FN never returns a nonzero value, the value
9395 returned by for_each_template_parm is 0. If FN is NULL, it is
9396 considered to be the function which always returns 1.
9398 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9399 parameters that occur in non-deduced contexts. When false, only
9400 visits those template parameters that can be deduced. */
9402 static tree
9403 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9404 hash_set<tree> *visited,
9405 bool include_nondeduced_p,
9406 tree_fn_t any_fn)
9408 struct pair_fn_data pfd;
9409 tree result;
9411 /* Set up. */
9412 pfd.fn = fn;
9413 pfd.any_fn = any_fn;
9414 pfd.data = data;
9415 pfd.include_nondeduced_p = include_nondeduced_p;
9417 /* Walk the tree. (Conceptually, we would like to walk without
9418 duplicates, but for_each_template_parm_r recursively calls
9419 for_each_template_parm, so we would need to reorganize a fair
9420 bit to use walk_tree_without_duplicates, so we keep our own
9421 visited list.) */
9422 if (visited)
9423 pfd.visited = visited;
9424 else
9425 pfd.visited = new hash_set<tree>;
9426 result = cp_walk_tree (&t,
9427 for_each_template_parm_r,
9428 &pfd,
9429 pfd.visited);
9431 /* Clean up. */
9432 if (!visited)
9434 delete pfd.visited;
9435 pfd.visited = 0;
9438 return result;
9441 /* Returns true if T depends on any template parameter. */
9444 uses_template_parms (tree t)
9446 if (t == NULL_TREE)
9447 return false;
9449 bool dependent_p;
9450 int saved_processing_template_decl;
9452 saved_processing_template_decl = processing_template_decl;
9453 if (!saved_processing_template_decl)
9454 processing_template_decl = 1;
9455 if (TYPE_P (t))
9456 dependent_p = dependent_type_p (t);
9457 else if (TREE_CODE (t) == TREE_VEC)
9458 dependent_p = any_dependent_template_arguments_p (t);
9459 else if (TREE_CODE (t) == TREE_LIST)
9460 dependent_p = (uses_template_parms (TREE_VALUE (t))
9461 || uses_template_parms (TREE_CHAIN (t)));
9462 else if (TREE_CODE (t) == TYPE_DECL)
9463 dependent_p = dependent_type_p (TREE_TYPE (t));
9464 else if (DECL_P (t)
9465 || EXPR_P (t)
9466 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9467 || TREE_CODE (t) == OVERLOAD
9468 || BASELINK_P (t)
9469 || identifier_p (t)
9470 || TREE_CODE (t) == TRAIT_EXPR
9471 || TREE_CODE (t) == CONSTRUCTOR
9472 || CONSTANT_CLASS_P (t))
9473 dependent_p = (type_dependent_expression_p (t)
9474 || value_dependent_expression_p (t));
9475 else
9477 gcc_assert (t == error_mark_node);
9478 dependent_p = false;
9481 processing_template_decl = saved_processing_template_decl;
9483 return dependent_p;
9486 /* Returns true iff current_function_decl is an incompletely instantiated
9487 template. Useful instead of processing_template_decl because the latter
9488 is set to 0 during instantiate_non_dependent_expr. */
9490 bool
9491 in_template_function (void)
9493 tree fn = current_function_decl;
9494 bool ret;
9495 ++processing_template_decl;
9496 ret = (fn && DECL_LANG_SPECIFIC (fn)
9497 && DECL_TEMPLATE_INFO (fn)
9498 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9499 --processing_template_decl;
9500 return ret;
9503 /* Returns true iff we are currently within a template other than a
9504 default-capturing generic lambda, so we don't need to worry about semantic
9505 processing. */
9507 bool
9508 processing_nonlambda_template (void)
9510 return processing_template_decl && !need_generic_capture ();
9513 /* Returns true if T depends on any template parameter with level LEVEL. */
9515 bool
9516 uses_template_parms_level (tree t, int level)
9518 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9519 /*include_nondeduced_p=*/true);
9522 /* Returns true if the signature of DECL depends on any template parameter from
9523 its enclosing class. */
9525 bool
9526 uses_outer_template_parms (tree decl)
9528 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9529 if (depth == 0)
9530 return false;
9531 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9532 &depth, NULL, /*include_nondeduced_p=*/true))
9533 return true;
9534 if (PRIMARY_TEMPLATE_P (decl)
9535 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9536 (DECL_TEMPLATE_PARMS (decl)),
9537 template_parm_outer_level,
9538 &depth, NULL, /*include_nondeduced_p=*/true))
9539 return true;
9540 tree ci = get_constraints (decl);
9541 if (ci)
9542 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9543 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9544 &depth, NULL, /*nondeduced*/true))
9545 return true;
9546 return false;
9549 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9550 ill-formed translation unit, i.e. a variable or function that isn't
9551 usable in a constant expression. */
9553 static inline bool
9554 neglectable_inst_p (tree d)
9556 return (DECL_P (d)
9557 && !undeduced_auto_decl (d)
9558 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9559 : decl_maybe_constant_var_p (d)));
9562 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9563 neglectable and instantiated from within an erroneous instantiation. */
9565 static bool
9566 limit_bad_template_recursion (tree decl)
9568 struct tinst_level *lev = current_tinst_level;
9569 int errs = errorcount + sorrycount;
9570 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9571 return false;
9573 for (; lev; lev = lev->next)
9574 if (neglectable_inst_p (lev->decl))
9575 break;
9577 return (lev && errs > lev->errors);
9580 static int tinst_depth;
9581 extern int max_tinst_depth;
9582 int depth_reached;
9584 static GTY(()) struct tinst_level *last_error_tinst_level;
9586 /* We're starting to instantiate D; record the template instantiation context
9587 for diagnostics and to restore it later. */
9589 bool
9590 push_tinst_level (tree d)
9592 return push_tinst_level_loc (d, input_location);
9595 /* We're starting to instantiate D; record the template instantiation context
9596 at LOC for diagnostics and to restore it later. */
9598 bool
9599 push_tinst_level_loc (tree d, location_t loc)
9601 struct tinst_level *new_level;
9603 if (tinst_depth >= max_tinst_depth)
9605 /* Tell error.c not to try to instantiate any templates. */
9606 at_eof = 2;
9607 fatal_error (input_location,
9608 "template instantiation depth exceeds maximum of %d"
9609 " (use -ftemplate-depth= to increase the maximum)",
9610 max_tinst_depth);
9611 return false;
9614 /* If the current instantiation caused problems, don't let it instantiate
9615 anything else. Do allow deduction substitution and decls usable in
9616 constant expressions. */
9617 if (limit_bad_template_recursion (d))
9618 return false;
9620 /* When not -quiet, dump template instantiations other than functions, since
9621 announce_function will take care of those. */
9622 if (!quiet_flag
9623 && TREE_CODE (d) != TREE_LIST
9624 && TREE_CODE (d) != FUNCTION_DECL)
9625 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9627 new_level = ggc_alloc<tinst_level> ();
9628 new_level->decl = d;
9629 new_level->locus = loc;
9630 new_level->errors = errorcount+sorrycount;
9631 new_level->in_system_header_p = in_system_header_at (input_location);
9632 new_level->next = current_tinst_level;
9633 current_tinst_level = new_level;
9635 ++tinst_depth;
9636 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9637 depth_reached = tinst_depth;
9639 return true;
9642 /* We're done instantiating this template; return to the instantiation
9643 context. */
9645 void
9646 pop_tinst_level (void)
9648 /* Restore the filename and line number stashed away when we started
9649 this instantiation. */
9650 input_location = current_tinst_level->locus;
9651 current_tinst_level = current_tinst_level->next;
9652 --tinst_depth;
9655 /* We're instantiating a deferred template; restore the template
9656 instantiation context in which the instantiation was requested, which
9657 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9659 static tree
9660 reopen_tinst_level (struct tinst_level *level)
9662 struct tinst_level *t;
9664 tinst_depth = 0;
9665 for (t = level; t; t = t->next)
9666 ++tinst_depth;
9668 current_tinst_level = level;
9669 pop_tinst_level ();
9670 if (current_tinst_level)
9671 current_tinst_level->errors = errorcount+sorrycount;
9672 return level->decl;
9675 /* Returns the TINST_LEVEL which gives the original instantiation
9676 context. */
9678 struct tinst_level *
9679 outermost_tinst_level (void)
9681 struct tinst_level *level = current_tinst_level;
9682 if (level)
9683 while (level->next)
9684 level = level->next;
9685 return level;
9688 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9689 vector of template arguments, as for tsubst.
9691 Returns an appropriate tsubst'd friend declaration. */
9693 static tree
9694 tsubst_friend_function (tree decl, tree args)
9696 tree new_friend;
9698 if (TREE_CODE (decl) == FUNCTION_DECL
9699 && DECL_TEMPLATE_INSTANTIATION (decl)
9700 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9701 /* This was a friend declared with an explicit template
9702 argument list, e.g.:
9704 friend void f<>(T);
9706 to indicate that f was a template instantiation, not a new
9707 function declaration. Now, we have to figure out what
9708 instantiation of what template. */
9710 tree template_id, arglist, fns;
9711 tree new_args;
9712 tree tmpl;
9713 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9715 /* Friend functions are looked up in the containing namespace scope.
9716 We must enter that scope, to avoid finding member functions of the
9717 current class with same name. */
9718 push_nested_namespace (ns);
9719 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9720 tf_warning_or_error, NULL_TREE,
9721 /*integral_constant_expression_p=*/false);
9722 pop_nested_namespace (ns);
9723 arglist = tsubst (DECL_TI_ARGS (decl), args,
9724 tf_warning_or_error, NULL_TREE);
9725 template_id = lookup_template_function (fns, arglist);
9727 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9728 tmpl = determine_specialization (template_id, new_friend,
9729 &new_args,
9730 /*need_member_template=*/0,
9731 TREE_VEC_LENGTH (args),
9732 tsk_none);
9733 return instantiate_template (tmpl, new_args, tf_error);
9736 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9738 /* The NEW_FRIEND will look like an instantiation, to the
9739 compiler, but is not an instantiation from the point of view of
9740 the language. For example, we might have had:
9742 template <class T> struct S {
9743 template <class U> friend void f(T, U);
9746 Then, in S<int>, template <class U> void f(int, U) is not an
9747 instantiation of anything. */
9748 if (new_friend == error_mark_node)
9749 return error_mark_node;
9751 DECL_USE_TEMPLATE (new_friend) = 0;
9752 if (TREE_CODE (decl) == TEMPLATE_DECL)
9754 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9755 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9756 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9759 /* The mangled name for the NEW_FRIEND is incorrect. The function
9760 is not a template instantiation and should not be mangled like
9761 one. Therefore, we forget the mangling here; we'll recompute it
9762 later if we need it. */
9763 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9765 SET_DECL_RTL (new_friend, NULL);
9766 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9769 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9771 tree old_decl;
9772 tree new_friend_template_info;
9773 tree new_friend_result_template_info;
9774 tree ns;
9775 int new_friend_is_defn;
9777 /* We must save some information from NEW_FRIEND before calling
9778 duplicate decls since that function will free NEW_FRIEND if
9779 possible. */
9780 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9781 new_friend_is_defn =
9782 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9783 (template_for_substitution (new_friend)))
9784 != NULL_TREE);
9785 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9787 /* This declaration is a `primary' template. */
9788 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9790 new_friend_result_template_info
9791 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9793 else
9794 new_friend_result_template_info = NULL_TREE;
9796 /* Inside pushdecl_namespace_level, we will push into the
9797 current namespace. However, the friend function should go
9798 into the namespace of the template. */
9799 ns = decl_namespace_context (new_friend);
9800 push_nested_namespace (ns);
9801 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9802 pop_nested_namespace (ns);
9804 if (old_decl == error_mark_node)
9805 return error_mark_node;
9807 if (old_decl != new_friend)
9809 /* This new friend declaration matched an existing
9810 declaration. For example, given:
9812 template <class T> void f(T);
9813 template <class U> class C {
9814 template <class T> friend void f(T) {}
9817 the friend declaration actually provides the definition
9818 of `f', once C has been instantiated for some type. So,
9819 old_decl will be the out-of-class template declaration,
9820 while new_friend is the in-class definition.
9822 But, if `f' was called before this point, the
9823 instantiation of `f' will have DECL_TI_ARGS corresponding
9824 to `T' but not to `U', references to which might appear
9825 in the definition of `f'. Previously, the most general
9826 template for an instantiation of `f' was the out-of-class
9827 version; now it is the in-class version. Therefore, we
9828 run through all specialization of `f', adding to their
9829 DECL_TI_ARGS appropriately. In particular, they need a
9830 new set of outer arguments, corresponding to the
9831 arguments for this class instantiation.
9833 The same situation can arise with something like this:
9835 friend void f(int);
9836 template <class T> class C {
9837 friend void f(T) {}
9840 when `C<int>' is instantiated. Now, `f(int)' is defined
9841 in the class. */
9843 if (!new_friend_is_defn)
9844 /* On the other hand, if the in-class declaration does
9845 *not* provide a definition, then we don't want to alter
9846 existing definitions. We can just leave everything
9847 alone. */
9849 else
9851 tree new_template = TI_TEMPLATE (new_friend_template_info);
9852 tree new_args = TI_ARGS (new_friend_template_info);
9854 /* Overwrite whatever template info was there before, if
9855 any, with the new template information pertaining to
9856 the declaration. */
9857 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9859 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9861 /* We should have called reregister_specialization in
9862 duplicate_decls. */
9863 gcc_assert (retrieve_specialization (new_template,
9864 new_args, 0)
9865 == old_decl);
9867 /* Instantiate it if the global has already been used. */
9868 if (DECL_ODR_USED (old_decl))
9869 instantiate_decl (old_decl, /*defer_ok=*/true,
9870 /*expl_inst_class_mem_p=*/false);
9872 else
9874 tree t;
9876 /* Indicate that the old function template is a partial
9877 instantiation. */
9878 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9879 = new_friend_result_template_info;
9881 gcc_assert (new_template
9882 == most_general_template (new_template));
9883 gcc_assert (new_template != old_decl);
9885 /* Reassign any specializations already in the hash table
9886 to the new more general template, and add the
9887 additional template args. */
9888 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9889 t != NULL_TREE;
9890 t = TREE_CHAIN (t))
9892 tree spec = TREE_VALUE (t);
9893 spec_entry elt;
9895 elt.tmpl = old_decl;
9896 elt.args = DECL_TI_ARGS (spec);
9897 elt.spec = NULL_TREE;
9899 decl_specializations->remove_elt (&elt);
9901 DECL_TI_ARGS (spec)
9902 = add_outermost_template_args (new_args,
9903 DECL_TI_ARGS (spec));
9905 register_specialization
9906 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9909 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9913 /* The information from NEW_FRIEND has been merged into OLD_DECL
9914 by duplicate_decls. */
9915 new_friend = old_decl;
9918 else
9920 tree context = DECL_CONTEXT (new_friend);
9921 bool dependent_p;
9923 /* In the code
9924 template <class T> class C {
9925 template <class U> friend void C1<U>::f (); // case 1
9926 friend void C2<T>::f (); // case 2
9928 we only need to make sure CONTEXT is a complete type for
9929 case 2. To distinguish between the two cases, we note that
9930 CONTEXT of case 1 remains dependent type after tsubst while
9931 this isn't true for case 2. */
9932 ++processing_template_decl;
9933 dependent_p = dependent_type_p (context);
9934 --processing_template_decl;
9936 if (!dependent_p
9937 && !complete_type_or_else (context, NULL_TREE))
9938 return error_mark_node;
9940 if (COMPLETE_TYPE_P (context))
9942 tree fn = new_friend;
9943 /* do_friend adds the TEMPLATE_DECL for any member friend
9944 template even if it isn't a member template, i.e.
9945 template <class T> friend A<T>::f();
9946 Look through it in that case. */
9947 if (TREE_CODE (fn) == TEMPLATE_DECL
9948 && !PRIMARY_TEMPLATE_P (fn))
9949 fn = DECL_TEMPLATE_RESULT (fn);
9950 /* Check to see that the declaration is really present, and,
9951 possibly obtain an improved declaration. */
9952 fn = check_classfn (context, fn, NULL_TREE);
9954 if (fn)
9955 new_friend = fn;
9959 return new_friend;
9962 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9963 template arguments, as for tsubst.
9965 Returns an appropriate tsubst'd friend type or error_mark_node on
9966 failure. */
9968 static tree
9969 tsubst_friend_class (tree friend_tmpl, tree args)
9971 tree friend_type;
9972 tree tmpl;
9973 tree context;
9975 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9977 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9978 return TREE_TYPE (t);
9981 context = CP_DECL_CONTEXT (friend_tmpl);
9983 if (context != global_namespace)
9985 if (TREE_CODE (context) == NAMESPACE_DECL)
9986 push_nested_namespace (context);
9987 else
9988 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9991 /* Look for a class template declaration. We look for hidden names
9992 because two friend declarations of the same template are the
9993 same. For example, in:
9995 struct A {
9996 template <typename> friend class F;
9998 template <typename> struct B {
9999 template <typename> friend class F;
10002 both F templates are the same. */
10003 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10004 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10006 /* But, if we don't find one, it might be because we're in a
10007 situation like this:
10009 template <class T>
10010 struct S {
10011 template <class U>
10012 friend struct S;
10015 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10016 for `S<int>', not the TEMPLATE_DECL. */
10017 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10019 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10020 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10023 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10025 /* The friend template has already been declared. Just
10026 check to see that the declarations match, and install any new
10027 default parameters. We must tsubst the default parameters,
10028 of course. We only need the innermost template parameters
10029 because that is all that redeclare_class_template will look
10030 at. */
10031 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10032 > TMPL_ARGS_DEPTH (args))
10034 tree parms;
10035 location_t saved_input_location;
10036 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10037 args, tf_warning_or_error);
10039 saved_input_location = input_location;
10040 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10041 tree cons = get_constraints (tmpl);
10042 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10043 input_location = saved_input_location;
10047 friend_type = TREE_TYPE (tmpl);
10049 else
10051 /* The friend template has not already been declared. In this
10052 case, the instantiation of the template class will cause the
10053 injection of this template into the global scope. */
10054 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10055 if (tmpl == error_mark_node)
10056 return error_mark_node;
10058 /* The new TMPL is not an instantiation of anything, so we
10059 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10060 the new type because that is supposed to be the corresponding
10061 template decl, i.e., TMPL. */
10062 DECL_USE_TEMPLATE (tmpl) = 0;
10063 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10064 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10065 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10066 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10068 /* Inject this template into the global scope. */
10069 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10072 if (context != global_namespace)
10074 if (TREE_CODE (context) == NAMESPACE_DECL)
10075 pop_nested_namespace (context);
10076 else
10077 pop_nested_class ();
10080 return friend_type;
10083 /* Returns zero if TYPE cannot be completed later due to circularity.
10084 Otherwise returns one. */
10086 static int
10087 can_complete_type_without_circularity (tree type)
10089 if (type == NULL_TREE || type == error_mark_node)
10090 return 0;
10091 else if (COMPLETE_TYPE_P (type))
10092 return 1;
10093 else if (TREE_CODE (type) == ARRAY_TYPE)
10094 return can_complete_type_without_circularity (TREE_TYPE (type));
10095 else if (CLASS_TYPE_P (type)
10096 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10097 return 0;
10098 else
10099 return 1;
10102 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10103 tsubst_flags_t, tree);
10105 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10106 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10108 static tree
10109 tsubst_attribute (tree t, tree *decl_p, tree args,
10110 tsubst_flags_t complain, tree in_decl)
10112 gcc_assert (ATTR_IS_DEPENDENT (t));
10114 tree val = TREE_VALUE (t);
10115 if (val == NULL_TREE)
10116 /* Nothing to do. */;
10117 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10118 && is_attribute_p ("omp declare simd",
10119 get_attribute_name (t)))
10121 tree clauses = TREE_VALUE (val);
10122 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10123 complain, in_decl);
10124 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10125 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10126 tree parms = DECL_ARGUMENTS (*decl_p);
10127 clauses
10128 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10129 if (clauses)
10130 val = build_tree_list (NULL_TREE, clauses);
10131 else
10132 val = NULL_TREE;
10134 /* If the first attribute argument is an identifier, don't
10135 pass it through tsubst. Attributes like mode, format,
10136 cleanup and several target specific attributes expect it
10137 unmodified. */
10138 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10140 tree chain
10141 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10142 /*integral_constant_expression_p=*/false);
10143 if (chain != TREE_CHAIN (val))
10144 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10146 else if (PACK_EXPANSION_P (val))
10148 /* An attribute pack expansion. */
10149 tree purp = TREE_PURPOSE (t);
10150 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10151 if (pack == error_mark_node)
10152 return error_mark_node;
10153 int len = TREE_VEC_LENGTH (pack);
10154 tree list = NULL_TREE;
10155 tree *q = &list;
10156 for (int i = 0; i < len; ++i)
10158 tree elt = TREE_VEC_ELT (pack, i);
10159 *q = build_tree_list (purp, elt);
10160 q = &TREE_CHAIN (*q);
10162 return list;
10164 else
10165 val = tsubst_expr (val, args, complain, in_decl,
10166 /*integral_constant_expression_p=*/false);
10168 if (val != TREE_VALUE (t))
10169 return build_tree_list (TREE_PURPOSE (t), val);
10170 return t;
10173 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10174 unchanged or a new TREE_LIST chain. */
10176 static tree
10177 tsubst_attributes (tree attributes, tree args,
10178 tsubst_flags_t complain, tree in_decl)
10180 tree last_dep = NULL_TREE;
10182 for (tree t = attributes; t; t = TREE_CHAIN (t))
10183 if (ATTR_IS_DEPENDENT (t))
10185 last_dep = t;
10186 attributes = copy_list (attributes);
10187 break;
10190 if (last_dep)
10191 for (tree *p = &attributes; *p; )
10193 tree t = *p;
10194 if (ATTR_IS_DEPENDENT (t))
10196 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10197 if (subst != t)
10199 *p = subst;
10201 p = &TREE_CHAIN (*p);
10202 while (*p);
10203 *p = TREE_CHAIN (t);
10204 continue;
10207 p = &TREE_CHAIN (*p);
10210 return attributes;
10213 /* Apply any attributes which had to be deferred until instantiation
10214 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10215 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10217 static void
10218 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10219 tree args, tsubst_flags_t complain, tree in_decl)
10221 tree last_dep = NULL_TREE;
10222 tree t;
10223 tree *p;
10225 if (attributes == NULL_TREE)
10226 return;
10228 if (DECL_P (*decl_p))
10230 if (TREE_TYPE (*decl_p) == error_mark_node)
10231 return;
10232 p = &DECL_ATTRIBUTES (*decl_p);
10233 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10234 to our attributes parameter. */
10235 gcc_assert (*p == attributes);
10237 else
10239 p = &TYPE_ATTRIBUTES (*decl_p);
10240 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10241 lookup_template_class_1, and should be preserved. */
10242 gcc_assert (*p != attributes);
10243 while (*p)
10244 p = &TREE_CHAIN (*p);
10247 for (t = attributes; t; t = TREE_CHAIN (t))
10248 if (ATTR_IS_DEPENDENT (t))
10250 last_dep = t;
10251 attributes = copy_list (attributes);
10252 break;
10255 *p = attributes;
10256 if (last_dep)
10258 tree late_attrs = NULL_TREE;
10259 tree *q = &late_attrs;
10261 for (; *p; )
10263 t = *p;
10264 if (ATTR_IS_DEPENDENT (t))
10266 *p = TREE_CHAIN (t);
10267 TREE_CHAIN (t) = NULL_TREE;
10268 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10270 q = &TREE_CHAIN (*q);
10271 while (*q);
10273 else
10274 p = &TREE_CHAIN (t);
10277 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10281 /* Perform (or defer) access check for typedefs that were referenced
10282 from within the template TMPL code.
10283 This is a subroutine of instantiate_decl and instantiate_class_template.
10284 TMPL is the template to consider and TARGS is the list of arguments of
10285 that template. */
10287 static void
10288 perform_typedefs_access_check (tree tmpl, tree targs)
10290 location_t saved_location;
10291 unsigned i;
10292 qualified_typedef_usage_t *iter;
10294 if (!tmpl
10295 || (!CLASS_TYPE_P (tmpl)
10296 && TREE_CODE (tmpl) != FUNCTION_DECL))
10297 return;
10299 saved_location = input_location;
10300 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10302 tree type_decl = iter->typedef_decl;
10303 tree type_scope = iter->context;
10305 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10306 continue;
10308 if (uses_template_parms (type_decl))
10309 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10310 if (uses_template_parms (type_scope))
10311 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10313 /* Make access check error messages point to the location
10314 of the use of the typedef. */
10315 input_location = iter->locus;
10316 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10317 type_decl, type_decl,
10318 tf_warning_or_error);
10320 input_location = saved_location;
10323 static tree
10324 instantiate_class_template_1 (tree type)
10326 tree templ, args, pattern, t, member;
10327 tree typedecl;
10328 tree pbinfo;
10329 tree base_list;
10330 unsigned int saved_maximum_field_alignment;
10331 tree fn_context;
10333 if (type == error_mark_node)
10334 return error_mark_node;
10336 if (COMPLETE_OR_OPEN_TYPE_P (type)
10337 || uses_template_parms (type))
10338 return type;
10340 /* Figure out which template is being instantiated. */
10341 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10342 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10344 /* Determine what specialization of the original template to
10345 instantiate. */
10346 t = most_specialized_partial_spec (type, tf_warning_or_error);
10347 if (t == error_mark_node)
10349 TYPE_BEING_DEFINED (type) = 1;
10350 return error_mark_node;
10352 else if (t)
10354 /* This TYPE is actually an instantiation of a partial
10355 specialization. We replace the innermost set of ARGS with
10356 the arguments appropriate for substitution. For example,
10357 given:
10359 template <class T> struct S {};
10360 template <class T> struct S<T*> {};
10362 and supposing that we are instantiating S<int*>, ARGS will
10363 presently be {int*} -- but we need {int}. */
10364 pattern = TREE_TYPE (t);
10365 args = TREE_PURPOSE (t);
10367 else
10369 pattern = TREE_TYPE (templ);
10370 args = CLASSTYPE_TI_ARGS (type);
10373 /* If the template we're instantiating is incomplete, then clearly
10374 there's nothing we can do. */
10375 if (!COMPLETE_TYPE_P (pattern))
10376 return type;
10378 /* If we've recursively instantiated too many templates, stop. */
10379 if (! push_tinst_level (type))
10380 return type;
10382 /* Now we're really doing the instantiation. Mark the type as in
10383 the process of being defined. */
10384 TYPE_BEING_DEFINED (type) = 1;
10386 /* We may be in the middle of deferred access check. Disable
10387 it now. */
10388 push_deferring_access_checks (dk_no_deferred);
10390 int saved_unevaluated_operand = cp_unevaluated_operand;
10391 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10393 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10394 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10395 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10396 fn_context = error_mark_node;
10397 if (!fn_context)
10398 push_to_top_level ();
10399 else
10401 cp_unevaluated_operand = 0;
10402 c_inhibit_evaluation_warnings = 0;
10404 /* Use #pragma pack from the template context. */
10405 saved_maximum_field_alignment = maximum_field_alignment;
10406 maximum_field_alignment = TYPE_PRECISION (pattern);
10408 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10410 /* Set the input location to the most specialized template definition.
10411 This is needed if tsubsting causes an error. */
10412 typedecl = TYPE_MAIN_DECL (pattern);
10413 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10414 DECL_SOURCE_LOCATION (typedecl);
10416 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10417 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10418 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10419 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10420 if (ANON_AGGR_TYPE_P (pattern))
10421 SET_ANON_AGGR_TYPE_P (type);
10422 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10424 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10425 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10426 /* Adjust visibility for template arguments. */
10427 determine_visibility (TYPE_MAIN_DECL (type));
10429 if (CLASS_TYPE_P (type))
10430 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10432 pbinfo = TYPE_BINFO (pattern);
10434 /* We should never instantiate a nested class before its enclosing
10435 class; we need to look up the nested class by name before we can
10436 instantiate it, and that lookup should instantiate the enclosing
10437 class. */
10438 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10439 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10441 base_list = NULL_TREE;
10442 if (BINFO_N_BASE_BINFOS (pbinfo))
10444 tree pbase_binfo;
10445 tree pushed_scope;
10446 int i;
10448 /* We must enter the scope containing the type, as that is where
10449 the accessibility of types named in dependent bases are
10450 looked up from. */
10451 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10453 /* Substitute into each of the bases to determine the actual
10454 basetypes. */
10455 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10457 tree base;
10458 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10459 tree expanded_bases = NULL_TREE;
10460 int idx, len = 1;
10462 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10464 expanded_bases =
10465 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10466 args, tf_error, NULL_TREE);
10467 if (expanded_bases == error_mark_node)
10468 continue;
10470 len = TREE_VEC_LENGTH (expanded_bases);
10473 for (idx = 0; idx < len; idx++)
10475 if (expanded_bases)
10476 /* Extract the already-expanded base class. */
10477 base = TREE_VEC_ELT (expanded_bases, idx);
10478 else
10479 /* Substitute to figure out the base class. */
10480 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10481 NULL_TREE);
10483 if (base == error_mark_node)
10484 continue;
10486 base_list = tree_cons (access, base, base_list);
10487 if (BINFO_VIRTUAL_P (pbase_binfo))
10488 TREE_TYPE (base_list) = integer_type_node;
10492 /* The list is now in reverse order; correct that. */
10493 base_list = nreverse (base_list);
10495 if (pushed_scope)
10496 pop_scope (pushed_scope);
10498 /* Now call xref_basetypes to set up all the base-class
10499 information. */
10500 xref_basetypes (type, base_list);
10502 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10503 (int) ATTR_FLAG_TYPE_IN_PLACE,
10504 args, tf_error, NULL_TREE);
10505 fixup_attribute_variants (type);
10507 /* Now that our base classes are set up, enter the scope of the
10508 class, so that name lookups into base classes, etc. will work
10509 correctly. This is precisely analogous to what we do in
10510 begin_class_definition when defining an ordinary non-template
10511 class, except we also need to push the enclosing classes. */
10512 push_nested_class (type);
10514 /* Now members are processed in the order of declaration. */
10515 for (member = CLASSTYPE_DECL_LIST (pattern);
10516 member; member = TREE_CHAIN (member))
10518 tree t = TREE_VALUE (member);
10520 if (TREE_PURPOSE (member))
10522 if (TYPE_P (t))
10524 /* Build new CLASSTYPE_NESTED_UTDS. */
10526 tree newtag;
10527 bool class_template_p;
10529 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10530 && TYPE_LANG_SPECIFIC (t)
10531 && CLASSTYPE_IS_TEMPLATE (t));
10532 /* If the member is a class template, then -- even after
10533 substitution -- there may be dependent types in the
10534 template argument list for the class. We increment
10535 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10536 that function will assume that no types are dependent
10537 when outside of a template. */
10538 if (class_template_p)
10539 ++processing_template_decl;
10540 newtag = tsubst (t, args, tf_error, NULL_TREE);
10541 if (class_template_p)
10542 --processing_template_decl;
10543 if (newtag == error_mark_node)
10544 continue;
10546 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10548 tree name = TYPE_IDENTIFIER (t);
10550 if (class_template_p)
10551 /* Unfortunately, lookup_template_class sets
10552 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10553 instantiation (i.e., for the type of a member
10554 template class nested within a template class.)
10555 This behavior is required for
10556 maybe_process_partial_specialization to work
10557 correctly, but is not accurate in this case;
10558 the TAG is not an instantiation of anything.
10559 (The corresponding TEMPLATE_DECL is an
10560 instantiation, but the TYPE is not.) */
10561 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10563 /* Now, we call pushtag to put this NEWTAG into the scope of
10564 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10565 pushtag calling push_template_decl. We don't have to do
10566 this for enums because it will already have been done in
10567 tsubst_enum. */
10568 if (name)
10569 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10570 pushtag (name, newtag, /*tag_scope=*/ts_current);
10573 else if (DECL_DECLARES_FUNCTION_P (t))
10575 tree r;
10577 if (TREE_CODE (t) == TEMPLATE_DECL)
10578 ++processing_template_decl;
10579 r = tsubst (t, args, tf_error, NULL_TREE);
10580 if (TREE_CODE (t) == TEMPLATE_DECL)
10581 --processing_template_decl;
10582 set_current_access_from_decl (r);
10583 finish_member_declaration (r);
10584 /* Instantiate members marked with attribute used. */
10585 if (r != error_mark_node && DECL_PRESERVE_P (r))
10586 mark_used (r);
10587 if (TREE_CODE (r) == FUNCTION_DECL
10588 && DECL_OMP_DECLARE_REDUCTION_P (r))
10589 cp_check_omp_declare_reduction (r);
10591 else if (DECL_CLASS_TEMPLATE_P (t)
10592 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10593 /* A closure type for a lambda in a default argument for a
10594 member template. Ignore it; it will be instantiated with
10595 the default argument. */;
10596 else
10598 /* Build new TYPE_FIELDS. */
10599 if (TREE_CODE (t) == STATIC_ASSERT)
10601 tree condition;
10603 ++c_inhibit_evaluation_warnings;
10604 condition =
10605 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10606 tf_warning_or_error, NULL_TREE,
10607 /*integral_constant_expression_p=*/true);
10608 --c_inhibit_evaluation_warnings;
10610 finish_static_assert (condition,
10611 STATIC_ASSERT_MESSAGE (t),
10612 STATIC_ASSERT_SOURCE_LOCATION (t),
10613 /*member_p=*/true);
10615 else if (TREE_CODE (t) != CONST_DECL)
10617 tree r;
10618 tree vec = NULL_TREE;
10619 int len = 1;
10621 /* The file and line for this declaration, to
10622 assist in error message reporting. Since we
10623 called push_tinst_level above, we don't need to
10624 restore these. */
10625 input_location = DECL_SOURCE_LOCATION (t);
10627 if (TREE_CODE (t) == TEMPLATE_DECL)
10628 ++processing_template_decl;
10629 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10630 if (TREE_CODE (t) == TEMPLATE_DECL)
10631 --processing_template_decl;
10633 if (TREE_CODE (r) == TREE_VEC)
10635 /* A capture pack became multiple fields. */
10636 vec = r;
10637 len = TREE_VEC_LENGTH (vec);
10640 for (int i = 0; i < len; ++i)
10642 if (vec)
10643 r = TREE_VEC_ELT (vec, i);
10644 if (VAR_P (r))
10646 /* In [temp.inst]:
10648 [t]he initialization (and any associated
10649 side-effects) of a static data member does
10650 not occur unless the static data member is
10651 itself used in a way that requires the
10652 definition of the static data member to
10653 exist.
10655 Therefore, we do not substitute into the
10656 initialized for the static data member here. */
10657 finish_static_data_member_decl
10659 /*init=*/NULL_TREE,
10660 /*init_const_expr_p=*/false,
10661 /*asmspec_tree=*/NULL_TREE,
10662 /*flags=*/0);
10663 /* Instantiate members marked with attribute used. */
10664 if (r != error_mark_node && DECL_PRESERVE_P (r))
10665 mark_used (r);
10667 else if (TREE_CODE (r) == FIELD_DECL)
10669 /* Determine whether R has a valid type and can be
10670 completed later. If R is invalid, then its type
10671 is replaced by error_mark_node. */
10672 tree rtype = TREE_TYPE (r);
10673 if (can_complete_type_without_circularity (rtype))
10674 complete_type (rtype);
10676 if (!complete_or_array_type_p (rtype))
10678 /* If R's type couldn't be completed and
10679 it isn't a flexible array member (whose
10680 type is incomplete by definition) give
10681 an error. */
10682 cxx_incomplete_type_error (r, rtype);
10683 TREE_TYPE (r) = error_mark_node;
10687 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10688 such a thing will already have been added to the field
10689 list by tsubst_enum in finish_member_declaration in the
10690 CLASSTYPE_NESTED_UTDS case above. */
10691 if (!(TREE_CODE (r) == TYPE_DECL
10692 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10693 && DECL_ARTIFICIAL (r)))
10695 set_current_access_from_decl (r);
10696 finish_member_declaration (r);
10702 else
10704 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10705 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10707 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10709 tree friend_type = t;
10710 bool adjust_processing_template_decl = false;
10712 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10714 /* template <class T> friend class C; */
10715 friend_type = tsubst_friend_class (friend_type, args);
10716 adjust_processing_template_decl = true;
10718 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10720 /* template <class T> friend class C::D; */
10721 friend_type = tsubst (friend_type, args,
10722 tf_warning_or_error, NULL_TREE);
10723 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10724 friend_type = TREE_TYPE (friend_type);
10725 adjust_processing_template_decl = true;
10727 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10728 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10730 /* This could be either
10732 friend class T::C;
10734 when dependent_type_p is false or
10736 template <class U> friend class T::C;
10738 otherwise. */
10739 /* Bump processing_template_decl in case this is something like
10740 template <class T> friend struct A<T>::B. */
10741 ++processing_template_decl;
10742 friend_type = tsubst (friend_type, args,
10743 tf_warning_or_error, NULL_TREE);
10744 if (dependent_type_p (friend_type))
10745 adjust_processing_template_decl = true;
10746 --processing_template_decl;
10748 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10749 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10750 && TYPE_HIDDEN_P (friend_type))
10752 /* friend class C;
10754 where C hasn't been declared yet. Let's lookup name
10755 from namespace scope directly, bypassing any name that
10756 come from dependent base class. */
10757 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10759 /* The call to xref_tag_from_type does injection for friend
10760 classes. */
10761 push_nested_namespace (ns);
10762 friend_type =
10763 xref_tag_from_type (friend_type, NULL_TREE,
10764 /*tag_scope=*/ts_current);
10765 pop_nested_namespace (ns);
10767 else if (uses_template_parms (friend_type))
10768 /* friend class C<T>; */
10769 friend_type = tsubst (friend_type, args,
10770 tf_warning_or_error, NULL_TREE);
10771 /* Otherwise it's
10773 friend class C;
10775 where C is already declared or
10777 friend class C<int>;
10779 We don't have to do anything in these cases. */
10781 if (adjust_processing_template_decl)
10782 /* Trick make_friend_class into realizing that the friend
10783 we're adding is a template, not an ordinary class. It's
10784 important that we use make_friend_class since it will
10785 perform some error-checking and output cross-reference
10786 information. */
10787 ++processing_template_decl;
10789 if (friend_type != error_mark_node)
10790 make_friend_class (type, friend_type, /*complain=*/false);
10792 if (adjust_processing_template_decl)
10793 --processing_template_decl;
10795 else
10797 /* Build new DECL_FRIENDLIST. */
10798 tree r;
10800 /* The file and line for this declaration, to
10801 assist in error message reporting. Since we
10802 called push_tinst_level above, we don't need to
10803 restore these. */
10804 input_location = DECL_SOURCE_LOCATION (t);
10806 if (TREE_CODE (t) == TEMPLATE_DECL)
10808 ++processing_template_decl;
10809 push_deferring_access_checks (dk_no_check);
10812 r = tsubst_friend_function (t, args);
10813 add_friend (type, r, /*complain=*/false);
10814 if (TREE_CODE (t) == TEMPLATE_DECL)
10816 pop_deferring_access_checks ();
10817 --processing_template_decl;
10823 if (fn_context)
10825 /* Restore these before substituting into the lambda capture
10826 initializers. */
10827 cp_unevaluated_operand = saved_unevaluated_operand;
10828 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10831 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10833 tree decl = lambda_function (type);
10834 if (decl)
10836 if (cxx_dialect >= cxx17)
10837 CLASSTYPE_LITERAL_P (type) = true;
10839 if (!DECL_TEMPLATE_INFO (decl)
10840 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10842 /* Set function_depth to avoid garbage collection. */
10843 ++function_depth;
10844 instantiate_decl (decl, /*defer_ok=*/false, false);
10845 --function_depth;
10848 /* We need to instantiate the capture list from the template
10849 after we've instantiated the closure members, but before we
10850 consider adding the conversion op. Also keep any captures
10851 that may have been added during instantiation of the op(). */
10852 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10853 tree tmpl_cap
10854 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10855 args, tf_warning_or_error, NULL_TREE,
10856 false, false);
10858 LAMBDA_EXPR_CAPTURE_LIST (expr)
10859 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10861 maybe_add_lambda_conv_op (type);
10863 else
10864 gcc_assert (errorcount);
10867 /* Set the file and line number information to whatever is given for
10868 the class itself. This puts error messages involving generated
10869 implicit functions at a predictable point, and the same point
10870 that would be used for non-template classes. */
10871 input_location = DECL_SOURCE_LOCATION (typedecl);
10873 unreverse_member_declarations (type);
10874 finish_struct_1 (type);
10875 TYPE_BEING_DEFINED (type) = 0;
10877 /* We don't instantiate default arguments for member functions. 14.7.1:
10879 The implicit instantiation of a class template specialization causes
10880 the implicit instantiation of the declarations, but not of the
10881 definitions or default arguments, of the class member functions,
10882 member classes, static data members and member templates.... */
10884 /* Some typedefs referenced from within the template code need to be access
10885 checked at template instantiation time, i.e now. These types were
10886 added to the template at parsing time. Let's get those and perform
10887 the access checks then. */
10888 perform_typedefs_access_check (pattern, args);
10889 perform_deferred_access_checks (tf_warning_or_error);
10890 pop_nested_class ();
10891 maximum_field_alignment = saved_maximum_field_alignment;
10892 if (!fn_context)
10893 pop_from_top_level ();
10894 pop_deferring_access_checks ();
10895 pop_tinst_level ();
10897 /* The vtable for a template class can be emitted in any translation
10898 unit in which the class is instantiated. When there is no key
10899 method, however, finish_struct_1 will already have added TYPE to
10900 the keyed_classes. */
10901 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10902 vec_safe_push (keyed_classes, type);
10904 return type;
10907 /* Wrapper for instantiate_class_template_1. */
10909 tree
10910 instantiate_class_template (tree type)
10912 tree ret;
10913 timevar_push (TV_TEMPLATE_INST);
10914 ret = instantiate_class_template_1 (type);
10915 timevar_pop (TV_TEMPLATE_INST);
10916 return ret;
10919 static tree
10920 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10922 tree r;
10924 if (!t)
10925 r = t;
10926 else if (TYPE_P (t))
10927 r = tsubst (t, args, complain, in_decl);
10928 else
10930 if (!(complain & tf_warning))
10931 ++c_inhibit_evaluation_warnings;
10932 r = tsubst_expr (t, args, complain, in_decl,
10933 /*integral_constant_expression_p=*/true);
10934 if (!(complain & tf_warning))
10935 --c_inhibit_evaluation_warnings;
10937 return r;
10940 /* Given a function parameter pack TMPL_PARM and some function parameters
10941 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10942 and set *SPEC_P to point at the next point in the list. */
10944 tree
10945 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10947 /* Collect all of the extra "packed" parameters into an
10948 argument pack. */
10949 tree parmvec;
10950 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10951 tree spec_parm = *spec_p;
10952 int i, len;
10954 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10955 if (tmpl_parm
10956 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10957 break;
10959 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10960 parmvec = make_tree_vec (len);
10961 spec_parm = *spec_p;
10962 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10963 TREE_VEC_ELT (parmvec, i) = spec_parm;
10965 /* Build the argument packs. */
10966 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10967 *spec_p = spec_parm;
10969 return argpack;
10972 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10973 NONTYPE_ARGUMENT_PACK. */
10975 static tree
10976 make_fnparm_pack (tree spec_parm)
10978 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10981 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10982 pack expansion with no extra args, 2 if it has extra args, or 0
10983 if it is not a pack expansion. */
10985 static int
10986 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10988 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10989 if (i >= TREE_VEC_LENGTH (vec))
10990 return 0;
10991 tree elt = TREE_VEC_ELT (vec, i);
10992 if (DECL_P (elt))
10993 /* A decl pack is itself an expansion. */
10994 elt = TREE_TYPE (elt);
10995 if (!PACK_EXPANSION_P (elt))
10996 return 0;
10997 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10998 return 2;
10999 return 1;
11003 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11005 static tree
11006 make_argument_pack_select (tree arg_pack, unsigned index)
11008 tree aps = make_node (ARGUMENT_PACK_SELECT);
11010 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11011 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11013 return aps;
11016 /* This is a subroutine of tsubst_pack_expansion.
11018 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11019 mechanism to store the (non complete list of) arguments of the
11020 substitution and return a non substituted pack expansion, in order
11021 to wait for when we have enough arguments to really perform the
11022 substitution. */
11024 static bool
11025 use_pack_expansion_extra_args_p (tree parm_packs,
11026 int arg_pack_len,
11027 bool has_empty_arg)
11029 /* If one pack has an expansion and another pack has a normal
11030 argument or if one pack has an empty argument and an another
11031 one hasn't then tsubst_pack_expansion cannot perform the
11032 substitution and need to fall back on the
11033 PACK_EXPANSION_EXTRA mechanism. */
11034 if (parm_packs == NULL_TREE)
11035 return false;
11036 else if (has_empty_arg)
11037 return true;
11039 bool has_expansion_arg = false;
11040 for (int i = 0 ; i < arg_pack_len; ++i)
11042 bool has_non_expansion_arg = false;
11043 for (tree parm_pack = parm_packs;
11044 parm_pack;
11045 parm_pack = TREE_CHAIN (parm_pack))
11047 tree arg = TREE_VALUE (parm_pack);
11049 int exp = argument_pack_element_is_expansion_p (arg, i);
11050 if (exp == 2)
11051 /* We can't substitute a pack expansion with extra args into
11052 our pattern. */
11053 return true;
11054 else if (exp)
11055 has_expansion_arg = true;
11056 else
11057 has_non_expansion_arg = true;
11060 if (has_expansion_arg && has_non_expansion_arg)
11061 return true;
11063 return false;
11066 /* [temp.variadic]/6 says that:
11068 The instantiation of a pack expansion [...]
11069 produces a list E1,E2, ..., En, where N is the number of elements
11070 in the pack expansion parameters.
11072 This subroutine of tsubst_pack_expansion produces one of these Ei.
11074 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11075 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11076 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11077 INDEX is the index 'i' of the element Ei to produce. ARGS,
11078 COMPLAIN, and IN_DECL are the same parameters as for the
11079 tsubst_pack_expansion function.
11081 The function returns the resulting Ei upon successful completion,
11082 or error_mark_node.
11084 Note that this function possibly modifies the ARGS parameter, so
11085 it's the responsibility of the caller to restore it. */
11087 static tree
11088 gen_elem_of_pack_expansion_instantiation (tree pattern,
11089 tree parm_packs,
11090 unsigned index,
11091 tree args /* This parm gets
11092 modified. */,
11093 tsubst_flags_t complain,
11094 tree in_decl)
11096 tree t;
11097 bool ith_elem_is_expansion = false;
11099 /* For each parameter pack, change the substitution of the parameter
11100 pack to the ith argument in its argument pack, then expand the
11101 pattern. */
11102 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11104 tree parm = TREE_PURPOSE (pack);
11105 tree arg_pack = TREE_VALUE (pack);
11106 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11108 ith_elem_is_expansion |=
11109 argument_pack_element_is_expansion_p (arg_pack, index);
11111 /* Select the Ith argument from the pack. */
11112 if (TREE_CODE (parm) == PARM_DECL
11113 || TREE_CODE (parm) == FIELD_DECL)
11115 if (index == 0)
11117 aps = make_argument_pack_select (arg_pack, index);
11118 if (!mark_used (parm, complain) && !(complain & tf_error))
11119 return error_mark_node;
11120 register_local_specialization (aps, parm);
11122 else
11123 aps = retrieve_local_specialization (parm);
11125 else
11127 int idx, level;
11128 template_parm_level_and_index (parm, &level, &idx);
11130 if (index == 0)
11132 aps = make_argument_pack_select (arg_pack, index);
11133 /* Update the corresponding argument. */
11134 TMPL_ARG (args, level, idx) = aps;
11136 else
11137 /* Re-use the ARGUMENT_PACK_SELECT. */
11138 aps = TMPL_ARG (args, level, idx);
11140 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11143 /* Substitute into the PATTERN with the (possibly altered)
11144 arguments. */
11145 if (pattern == in_decl)
11146 /* Expanding a fixed parameter pack from
11147 coerce_template_parameter_pack. */
11148 t = tsubst_decl (pattern, args, complain);
11149 else if (pattern == error_mark_node)
11150 t = error_mark_node;
11151 else if (constraint_p (pattern))
11153 if (processing_template_decl)
11154 t = tsubst_constraint (pattern, args, complain, in_decl);
11155 else
11156 t = (constraints_satisfied_p (pattern, args)
11157 ? boolean_true_node : boolean_false_node);
11159 else if (!TYPE_P (pattern))
11160 t = tsubst_expr (pattern, args, complain, in_decl,
11161 /*integral_constant_expression_p=*/false);
11162 else
11163 t = tsubst (pattern, args, complain, in_decl);
11165 /* If the Ith argument pack element is a pack expansion, then
11166 the Ith element resulting from the substituting is going to
11167 be a pack expansion as well. */
11168 if (ith_elem_is_expansion)
11169 t = make_pack_expansion (t, complain);
11171 return t;
11174 /* When the unexpanded parameter pack in a fold expression expands to an empty
11175 sequence, the value of the expression is as follows; the program is
11176 ill-formed if the operator is not listed in this table.
11178 && true
11179 || false
11180 , void() */
11182 tree
11183 expand_empty_fold (tree t, tsubst_flags_t complain)
11185 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11186 if (!FOLD_EXPR_MODIFY_P (t))
11187 switch (code)
11189 case TRUTH_ANDIF_EXPR:
11190 return boolean_true_node;
11191 case TRUTH_ORIF_EXPR:
11192 return boolean_false_node;
11193 case COMPOUND_EXPR:
11194 return void_node;
11195 default:
11196 break;
11199 if (complain & tf_error)
11200 error_at (location_of (t),
11201 "fold of empty expansion over %O", code);
11202 return error_mark_node;
11205 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11206 form an expression that combines the two terms using the
11207 operator of T. */
11209 static tree
11210 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11212 tree op = FOLD_EXPR_OP (t);
11213 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11215 // Handle compound assignment operators.
11216 if (FOLD_EXPR_MODIFY_P (t))
11217 return build_x_modify_expr (input_location, left, code, right, complain);
11219 switch (code)
11221 case COMPOUND_EXPR:
11222 return build_x_compound_expr (input_location, left, right, complain);
11223 case DOTSTAR_EXPR:
11224 return build_m_component_ref (left, right, complain);
11225 default:
11226 return build_x_binary_op (input_location, code,
11227 left, TREE_CODE (left),
11228 right, TREE_CODE (right),
11229 /*overload=*/NULL,
11230 complain);
11234 /* Substitute ARGS into the pack of a fold expression T. */
11236 static inline tree
11237 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11239 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11242 /* Substitute ARGS into the pack of a fold expression T. */
11244 static inline tree
11245 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11247 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11250 /* Expand a PACK of arguments into a grouped as left fold.
11251 Given a pack containing elements A0, A1, ..., An and an
11252 operator @, this builds the expression:
11254 ((A0 @ A1) @ A2) ... @ An
11256 Note that PACK must not be empty.
11258 The operator is defined by the original fold expression T. */
11260 static tree
11261 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11263 tree left = TREE_VEC_ELT (pack, 0);
11264 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11266 tree right = TREE_VEC_ELT (pack, i);
11267 left = fold_expression (t, left, right, complain);
11269 return left;
11272 /* Substitute into a unary left fold expression. */
11274 static tree
11275 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11276 tree in_decl)
11278 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11279 if (pack == error_mark_node)
11280 return error_mark_node;
11281 if (PACK_EXPANSION_P (pack))
11283 tree r = copy_node (t);
11284 FOLD_EXPR_PACK (r) = pack;
11285 return r;
11287 if (TREE_VEC_LENGTH (pack) == 0)
11288 return expand_empty_fold (t, complain);
11289 else
11290 return expand_left_fold (t, pack, complain);
11293 /* Substitute into a binary left fold expression.
11295 Do ths by building a single (non-empty) vector of argumnts and
11296 building the expression from those elements. */
11298 static tree
11299 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11300 tree in_decl)
11302 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11303 if (pack == error_mark_node)
11304 return error_mark_node;
11305 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11306 if (init == error_mark_node)
11307 return error_mark_node;
11309 if (PACK_EXPANSION_P (pack))
11311 tree r = copy_node (t);
11312 FOLD_EXPR_PACK (r) = pack;
11313 FOLD_EXPR_INIT (r) = init;
11314 return r;
11317 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11318 TREE_VEC_ELT (vec, 0) = init;
11319 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11320 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11322 return expand_left_fold (t, vec, complain);
11325 /* Expand a PACK of arguments into a grouped as right fold.
11326 Given a pack containing elementns A0, A1, ..., and an
11327 operator @, this builds the expression:
11329 A0@ ... (An-2 @ (An-1 @ An))
11331 Note that PACK must not be empty.
11333 The operator is defined by the original fold expression T. */
11335 tree
11336 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11338 // Build the expression.
11339 int n = TREE_VEC_LENGTH (pack);
11340 tree right = TREE_VEC_ELT (pack, n - 1);
11341 for (--n; n != 0; --n)
11343 tree left = TREE_VEC_ELT (pack, n - 1);
11344 right = fold_expression (t, left, right, complain);
11346 return right;
11349 /* Substitute into a unary right fold expression. */
11351 static tree
11352 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11353 tree in_decl)
11355 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11356 if (pack == error_mark_node)
11357 return error_mark_node;
11358 if (PACK_EXPANSION_P (pack))
11360 tree r = copy_node (t);
11361 FOLD_EXPR_PACK (r) = pack;
11362 return r;
11364 if (TREE_VEC_LENGTH (pack) == 0)
11365 return expand_empty_fold (t, complain);
11366 else
11367 return expand_right_fold (t, pack, complain);
11370 /* Substitute into a binary right fold expression.
11372 Do ths by building a single (non-empty) vector of arguments and
11373 building the expression from those elements. */
11375 static tree
11376 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11377 tree in_decl)
11379 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11380 if (pack == error_mark_node)
11381 return error_mark_node;
11382 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11383 if (init == error_mark_node)
11384 return error_mark_node;
11386 if (PACK_EXPANSION_P (pack))
11388 tree r = copy_node (t);
11389 FOLD_EXPR_PACK (r) = pack;
11390 FOLD_EXPR_INIT (r) = init;
11391 return r;
11394 int n = TREE_VEC_LENGTH (pack);
11395 tree vec = make_tree_vec (n + 1);
11396 for (int i = 0; i < n; ++i)
11397 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11398 TREE_VEC_ELT (vec, n) = init;
11400 return expand_right_fold (t, vec, complain);
11404 /* Substitute ARGS into T, which is an pack expansion
11405 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11406 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11407 (if only a partial substitution could be performed) or
11408 ERROR_MARK_NODE if there was an error. */
11409 tree
11410 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11411 tree in_decl)
11413 tree pattern;
11414 tree pack, packs = NULL_TREE;
11415 bool unsubstituted_packs = false;
11416 int i, len = -1;
11417 tree result;
11418 hash_map<tree, tree> *saved_local_specializations = NULL;
11419 bool need_local_specializations = false;
11420 int levels;
11422 gcc_assert (PACK_EXPANSION_P (t));
11423 pattern = PACK_EXPANSION_PATTERN (t);
11425 /* Add in any args remembered from an earlier partial instantiation. */
11426 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11428 levels = TMPL_ARGS_DEPTH (args);
11430 /* Determine the argument packs that will instantiate the parameter
11431 packs used in the expansion expression. While we're at it,
11432 compute the number of arguments to be expanded and make sure it
11433 is consistent. */
11434 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11435 pack = TREE_CHAIN (pack))
11437 tree parm_pack = TREE_VALUE (pack);
11438 tree arg_pack = NULL_TREE;
11439 tree orig_arg = NULL_TREE;
11440 int level = 0;
11442 if (TREE_CODE (parm_pack) == BASES)
11444 gcc_assert (parm_pack == pattern);
11445 if (BASES_DIRECT (parm_pack))
11446 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11447 args, complain, in_decl, false));
11448 else
11449 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11450 args, complain, in_decl, false));
11452 else if (builtin_pack_call_p (parm_pack))
11454 /* ??? Support use in other patterns. */
11455 gcc_assert (parm_pack == pattern);
11456 return expand_builtin_pack_call (parm_pack, args,
11457 complain, in_decl);
11459 else if (TREE_CODE (parm_pack) == PARM_DECL)
11461 /* We know we have correct local_specializations if this
11462 expansion is at function scope, or if we're dealing with a
11463 local parameter in a requires expression; for the latter,
11464 tsubst_requires_expr set it up appropriately. */
11465 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11466 arg_pack = retrieve_local_specialization (parm_pack);
11467 else
11468 /* We can't rely on local_specializations for a parameter
11469 name used later in a function declaration (such as in a
11470 late-specified return type). Even if it exists, it might
11471 have the wrong value for a recursive call. */
11472 need_local_specializations = true;
11474 if (!arg_pack)
11476 /* This parameter pack was used in an unevaluated context. Just
11477 make a dummy decl, since it's only used for its type. */
11478 arg_pack = tsubst_decl (parm_pack, args, complain);
11479 if (arg_pack && DECL_PACK_P (arg_pack))
11480 /* Partial instantiation of the parm_pack, we can't build
11481 up an argument pack yet. */
11482 arg_pack = NULL_TREE;
11483 else
11484 arg_pack = make_fnparm_pack (arg_pack);
11487 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11488 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11489 else
11491 int idx;
11492 template_parm_level_and_index (parm_pack, &level, &idx);
11494 if (level <= levels)
11495 arg_pack = TMPL_ARG (args, level, idx);
11498 orig_arg = arg_pack;
11499 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11500 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11502 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11503 /* This can only happen if we forget to expand an argument
11504 pack somewhere else. Just return an error, silently. */
11506 result = make_tree_vec (1);
11507 TREE_VEC_ELT (result, 0) = error_mark_node;
11508 return result;
11511 if (arg_pack)
11513 int my_len =
11514 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11516 /* Don't bother trying to do a partial substitution with
11517 incomplete packs; we'll try again after deduction. */
11518 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11519 return t;
11521 if (len < 0)
11522 len = my_len;
11523 else if (len != my_len)
11525 if (!(complain & tf_error))
11526 /* Fail quietly. */;
11527 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11528 error ("mismatched argument pack lengths while expanding %qT",
11529 pattern);
11530 else
11531 error ("mismatched argument pack lengths while expanding %qE",
11532 pattern);
11533 return error_mark_node;
11536 /* Keep track of the parameter packs and their corresponding
11537 argument packs. */
11538 packs = tree_cons (parm_pack, arg_pack, packs);
11539 TREE_TYPE (packs) = orig_arg;
11541 else
11543 /* We can't substitute for this parameter pack. We use a flag as
11544 well as the missing_level counter because function parameter
11545 packs don't have a level. */
11546 gcc_assert (processing_template_decl);
11547 unsubstituted_packs = true;
11551 /* If the expansion is just T..., return the matching argument pack, unless
11552 we need to call convert_from_reference on all the elements. This is an
11553 important optimization; see c++/68422. */
11554 if (!unsubstituted_packs
11555 && TREE_PURPOSE (packs) == pattern)
11557 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11558 /* Types need no adjustment, nor does sizeof..., and if we still have
11559 some pack expansion args we won't do anything yet. */
11560 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11561 || PACK_EXPANSION_SIZEOF_P (t)
11562 || pack_expansion_args_count (args))
11563 return args;
11564 /* Also optimize expression pack expansions if we can tell that the
11565 elements won't have reference type. */
11566 tree type = TREE_TYPE (pattern);
11567 if (type && TREE_CODE (type) != REFERENCE_TYPE
11568 && !PACK_EXPANSION_P (type)
11569 && !WILDCARD_TYPE_P (type))
11570 return args;
11571 /* Otherwise use the normal path so we get convert_from_reference. */
11574 /* We cannot expand this expansion expression, because we don't have
11575 all of the argument packs we need. */
11576 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11578 /* We got some full packs, but we can't substitute them in until we
11579 have values for all the packs. So remember these until then. */
11581 t = make_pack_expansion (pattern, complain);
11582 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11583 return t;
11585 else if (unsubstituted_packs)
11587 /* There were no real arguments, we're just replacing a parameter
11588 pack with another version of itself. Substitute into the
11589 pattern and return a PACK_EXPANSION_*. The caller will need to
11590 deal with that. */
11591 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11592 t = tsubst_expr (pattern, args, complain, in_decl,
11593 /*integral_constant_expression_p=*/false);
11594 else
11595 t = tsubst (pattern, args, complain, in_decl);
11596 t = make_pack_expansion (t, complain);
11597 return t;
11600 gcc_assert (len >= 0);
11602 if (need_local_specializations)
11604 /* We're in a late-specified return type, so create our own local
11605 specializations map; the current map is either NULL or (in the
11606 case of recursive unification) might have bindings that we don't
11607 want to use or alter. */
11608 saved_local_specializations = local_specializations;
11609 local_specializations = new hash_map<tree, tree>;
11612 /* For each argument in each argument pack, substitute into the
11613 pattern. */
11614 result = make_tree_vec (len);
11615 tree elem_args = copy_template_args (args);
11616 for (i = 0; i < len; ++i)
11618 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11620 elem_args, complain,
11621 in_decl);
11622 TREE_VEC_ELT (result, i) = t;
11623 if (t == error_mark_node)
11625 result = error_mark_node;
11626 break;
11630 /* Update ARGS to restore the substitution from parameter packs to
11631 their argument packs. */
11632 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11634 tree parm = TREE_PURPOSE (pack);
11636 if (TREE_CODE (parm) == PARM_DECL
11637 || TREE_CODE (parm) == FIELD_DECL)
11638 register_local_specialization (TREE_TYPE (pack), parm);
11639 else
11641 int idx, level;
11643 if (TREE_VALUE (pack) == NULL_TREE)
11644 continue;
11646 template_parm_level_and_index (parm, &level, &idx);
11648 /* Update the corresponding argument. */
11649 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11650 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11651 TREE_TYPE (pack);
11652 else
11653 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11657 if (need_local_specializations)
11659 delete local_specializations;
11660 local_specializations = saved_local_specializations;
11663 /* If the dependent pack arguments were such that we end up with only a
11664 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11665 if (len == 1 && TREE_CODE (result) == TREE_VEC
11666 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11667 return TREE_VEC_ELT (result, 0);
11669 return result;
11672 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11673 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11674 parameter packs; all parms generated from a function parameter pack will
11675 have the same DECL_PARM_INDEX. */
11677 tree
11678 get_pattern_parm (tree parm, tree tmpl)
11680 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11681 tree patparm;
11683 if (DECL_ARTIFICIAL (parm))
11685 for (patparm = DECL_ARGUMENTS (pattern);
11686 patparm; patparm = DECL_CHAIN (patparm))
11687 if (DECL_ARTIFICIAL (patparm)
11688 && DECL_NAME (parm) == DECL_NAME (patparm))
11689 break;
11691 else
11693 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11694 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11695 gcc_assert (DECL_PARM_INDEX (patparm)
11696 == DECL_PARM_INDEX (parm));
11699 return patparm;
11702 /* Make an argument pack out of the TREE_VEC VEC. */
11704 static tree
11705 make_argument_pack (tree vec)
11707 tree pack;
11708 tree elt = TREE_VEC_ELT (vec, 0);
11709 if (TYPE_P (elt))
11710 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11711 else
11713 pack = make_node (NONTYPE_ARGUMENT_PACK);
11714 TREE_CONSTANT (pack) = 1;
11716 SET_ARGUMENT_PACK_ARGS (pack, vec);
11717 return pack;
11720 /* Return an exact copy of template args T that can be modified
11721 independently. */
11723 static tree
11724 copy_template_args (tree t)
11726 if (t == error_mark_node)
11727 return t;
11729 int len = TREE_VEC_LENGTH (t);
11730 tree new_vec = make_tree_vec (len);
11732 for (int i = 0; i < len; ++i)
11734 tree elt = TREE_VEC_ELT (t, i);
11735 if (elt && TREE_CODE (elt) == TREE_VEC)
11736 elt = copy_template_args (elt);
11737 TREE_VEC_ELT (new_vec, i) = elt;
11740 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11741 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11743 return new_vec;
11746 /* Substitute ARGS into the vector or list of template arguments T. */
11748 static tree
11749 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11751 tree orig_t = t;
11752 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11753 tree *elts;
11755 if (t == error_mark_node)
11756 return error_mark_node;
11758 len = TREE_VEC_LENGTH (t);
11759 elts = XALLOCAVEC (tree, len);
11761 for (i = 0; i < len; i++)
11763 tree orig_arg = TREE_VEC_ELT (t, i);
11764 tree new_arg;
11766 if (TREE_CODE (orig_arg) == TREE_VEC)
11767 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11768 else if (PACK_EXPANSION_P (orig_arg))
11770 /* Substitute into an expansion expression. */
11771 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11773 if (TREE_CODE (new_arg) == TREE_VEC)
11774 /* Add to the expanded length adjustment the number of
11775 expanded arguments. We subtract one from this
11776 measurement, because the argument pack expression
11777 itself is already counted as 1 in
11778 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11779 the argument pack is empty. */
11780 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11782 else if (ARGUMENT_PACK_P (orig_arg))
11784 /* Substitute into each of the arguments. */
11785 new_arg = TYPE_P (orig_arg)
11786 ? cxx_make_type (TREE_CODE (orig_arg))
11787 : make_node (TREE_CODE (orig_arg));
11789 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11790 args, complain, in_decl);
11791 if (pack_args == error_mark_node)
11792 new_arg = error_mark_node;
11793 else
11794 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11796 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11797 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11799 else
11800 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11802 if (new_arg == error_mark_node)
11803 return error_mark_node;
11805 elts[i] = new_arg;
11806 if (new_arg != orig_arg)
11807 need_new = 1;
11810 if (!need_new)
11811 return t;
11813 /* Make space for the expanded arguments coming from template
11814 argument packs. */
11815 t = make_tree_vec (len + expanded_len_adjust);
11816 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11817 arguments for a member template.
11818 In that case each TREE_VEC in ORIG_T represents a level of template
11819 arguments, and ORIG_T won't carry any non defaulted argument count.
11820 It will rather be the nested TREE_VECs that will carry one.
11821 In other words, ORIG_T carries a non defaulted argument count only
11822 if it doesn't contain any nested TREE_VEC. */
11823 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11825 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11826 count += expanded_len_adjust;
11827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11829 for (i = 0, out = 0; i < len; i++)
11831 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11832 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11833 && TREE_CODE (elts[i]) == TREE_VEC)
11835 int idx;
11837 /* Now expand the template argument pack "in place". */
11838 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11839 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11841 else
11843 TREE_VEC_ELT (t, out) = elts[i];
11844 out++;
11848 return t;
11851 /* Substitute ARGS into one level PARMS of template parameters. */
11853 static tree
11854 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11856 if (parms == error_mark_node)
11857 return error_mark_node;
11859 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11861 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11863 tree tuple = TREE_VEC_ELT (parms, i);
11865 if (tuple == error_mark_node)
11866 continue;
11868 TREE_VEC_ELT (new_vec, i) =
11869 tsubst_template_parm (tuple, args, complain);
11872 return new_vec;
11875 /* Return the result of substituting ARGS into the template parameters
11876 given by PARMS. If there are m levels of ARGS and m + n levels of
11877 PARMS, then the result will contain n levels of PARMS. For
11878 example, if PARMS is `template <class T> template <class U>
11879 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11880 result will be `template <int*, double, class V>'. */
11882 static tree
11883 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11885 tree r = NULL_TREE;
11886 tree* new_parms;
11888 /* When substituting into a template, we must set
11889 PROCESSING_TEMPLATE_DECL as the template parameters may be
11890 dependent if they are based on one-another, and the dependency
11891 predicates are short-circuit outside of templates. */
11892 ++processing_template_decl;
11894 for (new_parms = &r;
11895 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11896 new_parms = &(TREE_CHAIN (*new_parms)),
11897 parms = TREE_CHAIN (parms))
11899 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11900 args, complain);
11901 *new_parms =
11902 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11903 - TMPL_ARGS_DEPTH (args)),
11904 new_vec, NULL_TREE);
11907 --processing_template_decl;
11909 return r;
11912 /* Return the result of substituting ARGS into one template parameter
11913 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11914 parameter and which TREE_PURPOSE is the default argument of the
11915 template parameter. */
11917 static tree
11918 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11920 tree default_value, parm_decl;
11922 if (args == NULL_TREE
11923 || t == NULL_TREE
11924 || t == error_mark_node)
11925 return t;
11927 gcc_assert (TREE_CODE (t) == TREE_LIST);
11929 default_value = TREE_PURPOSE (t);
11930 parm_decl = TREE_VALUE (t);
11932 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11933 if (TREE_CODE (parm_decl) == PARM_DECL
11934 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11935 parm_decl = error_mark_node;
11936 default_value = tsubst_template_arg (default_value, args,
11937 complain, NULL_TREE);
11939 return build_tree_list (default_value, parm_decl);
11942 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11943 type T. If T is not an aggregate or enumeration type, it is
11944 handled as if by tsubst. IN_DECL is as for tsubst. If
11945 ENTERING_SCOPE is nonzero, T is the context for a template which
11946 we are presently tsubst'ing. Return the substituted value. */
11948 static tree
11949 tsubst_aggr_type (tree t,
11950 tree args,
11951 tsubst_flags_t complain,
11952 tree in_decl,
11953 int entering_scope)
11955 if (t == NULL_TREE)
11956 return NULL_TREE;
11958 switch (TREE_CODE (t))
11960 case RECORD_TYPE:
11961 if (TYPE_PTRMEMFUNC_P (t))
11962 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11964 /* Fall through. */
11965 case ENUMERAL_TYPE:
11966 case UNION_TYPE:
11967 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11969 tree argvec;
11970 tree context;
11971 tree r;
11972 int saved_unevaluated_operand;
11973 int saved_inhibit_evaluation_warnings;
11975 /* In "sizeof(X<I>)" we need to evaluate "I". */
11976 saved_unevaluated_operand = cp_unevaluated_operand;
11977 cp_unevaluated_operand = 0;
11978 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11979 c_inhibit_evaluation_warnings = 0;
11981 /* First, determine the context for the type we are looking
11982 up. */
11983 context = TYPE_CONTEXT (t);
11984 if (context && TYPE_P (context))
11986 context = tsubst_aggr_type (context, args, complain,
11987 in_decl, /*entering_scope=*/1);
11988 /* If context is a nested class inside a class template,
11989 it may still need to be instantiated (c++/33959). */
11990 context = complete_type (context);
11993 /* Then, figure out what arguments are appropriate for the
11994 type we are trying to find. For example, given:
11996 template <class T> struct S;
11997 template <class T, class U> void f(T, U) { S<U> su; }
11999 and supposing that we are instantiating f<int, double>,
12000 then our ARGS will be {int, double}, but, when looking up
12001 S we only want {double}. */
12002 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12003 complain, in_decl);
12004 if (argvec == error_mark_node)
12005 r = error_mark_node;
12006 else
12008 r = lookup_template_class (t, argvec, in_decl, context,
12009 entering_scope, complain);
12010 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12013 cp_unevaluated_operand = saved_unevaluated_operand;
12014 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12016 return r;
12018 else
12019 /* This is not a template type, so there's nothing to do. */
12020 return t;
12022 default:
12023 return tsubst (t, args, complain, in_decl);
12027 static GTY(()) hash_map<tree, tree> *defarg_inst;
12029 /* Substitute into the default argument ARG (a default argument for
12030 FN), which has the indicated TYPE. */
12032 tree
12033 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12034 tsubst_flags_t complain)
12036 tree saved_class_ptr = NULL_TREE;
12037 tree saved_class_ref = NULL_TREE;
12038 int errs = errorcount + sorrycount;
12040 /* This can happen in invalid code. */
12041 if (TREE_CODE (arg) == DEFAULT_ARG)
12042 return arg;
12044 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12045 parm = chain_index (parmnum, parm);
12046 tree parmtype = TREE_TYPE (parm);
12047 if (DECL_BY_REFERENCE (parm))
12048 parmtype = TREE_TYPE (parmtype);
12049 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12051 tree *slot;
12052 if (defarg_inst && (slot = defarg_inst->get (parm)))
12053 return *slot;
12055 /* This default argument came from a template. Instantiate the
12056 default argument here, not in tsubst. In the case of
12057 something like:
12059 template <class T>
12060 struct S {
12061 static T t();
12062 void f(T = t());
12065 we must be careful to do name lookup in the scope of S<T>,
12066 rather than in the current class. */
12067 push_access_scope (fn);
12068 /* The "this" pointer is not valid in a default argument. */
12069 if (cfun)
12071 saved_class_ptr = current_class_ptr;
12072 cp_function_chain->x_current_class_ptr = NULL_TREE;
12073 saved_class_ref = current_class_ref;
12074 cp_function_chain->x_current_class_ref = NULL_TREE;
12077 start_lambda_scope (parm);
12079 push_deferring_access_checks(dk_no_deferred);
12080 /* The default argument expression may cause implicitly defined
12081 member functions to be synthesized, which will result in garbage
12082 collection. We must treat this situation as if we were within
12083 the body of function so as to avoid collecting live data on the
12084 stack. */
12085 ++function_depth;
12086 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12087 complain, NULL_TREE,
12088 /*integral_constant_expression_p=*/false);
12089 --function_depth;
12090 pop_deferring_access_checks();
12092 finish_lambda_scope ();
12094 /* Restore the "this" pointer. */
12095 if (cfun)
12097 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12098 cp_function_chain->x_current_class_ref = saved_class_ref;
12101 if (errorcount+sorrycount > errs
12102 && (complain & tf_warning_or_error))
12103 inform (input_location,
12104 " when instantiating default argument for call to %qD", fn);
12106 /* Make sure the default argument is reasonable. */
12107 arg = check_default_argument (type, arg, complain);
12109 pop_access_scope (fn);
12111 if (arg != error_mark_node && !cp_unevaluated_operand)
12113 if (!defarg_inst)
12114 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12115 defarg_inst->put (parm, arg);
12118 return arg;
12121 /* Substitute into all the default arguments for FN. */
12123 static void
12124 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12126 tree arg;
12127 tree tmpl_args;
12129 tmpl_args = DECL_TI_ARGS (fn);
12131 /* If this function is not yet instantiated, we certainly don't need
12132 its default arguments. */
12133 if (uses_template_parms (tmpl_args))
12134 return;
12135 /* Don't do this again for clones. */
12136 if (DECL_CLONED_FUNCTION_P (fn))
12137 return;
12139 int i = 0;
12140 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12141 arg;
12142 arg = TREE_CHAIN (arg), ++i)
12143 if (TREE_PURPOSE (arg))
12144 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12145 TREE_VALUE (arg),
12146 TREE_PURPOSE (arg),
12147 complain);
12150 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12152 static tree
12153 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12154 tree lambda_fntype)
12156 tree gen_tmpl, argvec;
12157 hashval_t hash = 0;
12158 tree in_decl = t;
12160 /* Nobody should be tsubst'ing into non-template functions. */
12161 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12163 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12165 /* If T is not dependent, just return it. */
12166 if (!uses_template_parms (DECL_TI_ARGS (t)))
12167 return t;
12169 /* Calculate the most general template of which R is a
12170 specialization, and the complete set of arguments used to
12171 specialize R. */
12172 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12173 argvec = tsubst_template_args (DECL_TI_ARGS
12174 (DECL_TEMPLATE_RESULT
12175 (DECL_TI_TEMPLATE (t))),
12176 args, complain, in_decl);
12177 if (argvec == error_mark_node)
12178 return error_mark_node;
12180 /* Check to see if we already have this specialization. */
12181 if (!lambda_fntype)
12183 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12184 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12185 return spec;
12188 /* We can see more levels of arguments than parameters if
12189 there was a specialization of a member template, like
12190 this:
12192 template <class T> struct S { template <class U> void f(); }
12193 template <> template <class U> void S<int>::f(U);
12195 Here, we'll be substituting into the specialization,
12196 because that's where we can find the code we actually
12197 want to generate, but we'll have enough arguments for
12198 the most general template.
12200 We also deal with the peculiar case:
12202 template <class T> struct S {
12203 template <class U> friend void f();
12205 template <class U> void f() {}
12206 template S<int>;
12207 template void f<double>();
12209 Here, the ARGS for the instantiation of will be {int,
12210 double}. But, we only need as many ARGS as there are
12211 levels of template parameters in CODE_PATTERN. We are
12212 careful not to get fooled into reducing the ARGS in
12213 situations like:
12215 template <class T> struct S { template <class U> void f(U); }
12216 template <class T> template <> void S<T>::f(int) {}
12218 which we can spot because the pattern will be a
12219 specialization in this case. */
12220 int args_depth = TMPL_ARGS_DEPTH (args);
12221 int parms_depth =
12222 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12224 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12225 args = get_innermost_template_args (args, parms_depth);
12227 else
12229 /* This special case arises when we have something like this:
12231 template <class T> struct S {
12232 friend void f<int>(int, double);
12235 Here, the DECL_TI_TEMPLATE for the friend declaration
12236 will be an IDENTIFIER_NODE. We are being called from
12237 tsubst_friend_function, and we want only to create a
12238 new decl (R) with appropriate types so that we can call
12239 determine_specialization. */
12240 gen_tmpl = NULL_TREE;
12241 argvec = NULL_TREE;
12244 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12245 : NULL_TREE);
12246 tree ctx = closure ? closure : DECL_CONTEXT (t);
12247 bool member = ctx && TYPE_P (ctx);
12249 if (member && !closure)
12250 ctx = tsubst_aggr_type (ctx, args,
12251 complain, t, /*entering_scope=*/1);
12253 tree type = (lambda_fntype ? lambda_fntype
12254 : tsubst (TREE_TYPE (t), args,
12255 complain | tf_fndecl_type, in_decl));
12256 if (type == error_mark_node)
12257 return error_mark_node;
12259 /* If we hit excessive deduction depth, the type is bogus even if
12260 it isn't error_mark_node, so don't build a decl. */
12261 if (excessive_deduction_depth)
12262 return error_mark_node;
12264 /* We do NOT check for matching decls pushed separately at this
12265 point, as they may not represent instantiations of this
12266 template, and in any case are considered separate under the
12267 discrete model. */
12268 tree r = copy_decl (t);
12269 DECL_USE_TEMPLATE (r) = 0;
12270 TREE_TYPE (r) = type;
12271 /* Clear out the mangled name and RTL for the instantiation. */
12272 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12273 SET_DECL_RTL (r, NULL);
12274 /* Leave DECL_INITIAL set on deleted instantiations. */
12275 if (!DECL_DELETED_FN (r))
12276 DECL_INITIAL (r) = NULL_TREE;
12277 DECL_CONTEXT (r) = ctx;
12279 /* OpenMP UDRs have the only argument a reference to the declared
12280 type. We want to diagnose if the declared type is a reference,
12281 which is invalid, but as references to references are usually
12282 quietly merged, diagnose it here. */
12283 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12285 tree argtype
12286 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12287 argtype = tsubst (argtype, args, complain, in_decl);
12288 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12289 error_at (DECL_SOURCE_LOCATION (t),
12290 "reference type %qT in "
12291 "%<#pragma omp declare reduction%>", argtype);
12292 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12293 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12294 argtype);
12297 if (member && DECL_CONV_FN_P (r))
12298 /* Type-conversion operator. Reconstruct the name, in
12299 case it's the name of one of the template's parameters. */
12300 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12302 tree parms = DECL_ARGUMENTS (t);
12303 if (closure)
12304 parms = DECL_CHAIN (parms);
12305 parms = tsubst (parms, args, complain, t);
12306 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12307 DECL_CONTEXT (parm) = r;
12308 if (closure)
12310 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12311 DECL_CHAIN (tparm) = parms;
12312 parms = tparm;
12314 DECL_ARGUMENTS (r) = parms;
12315 DECL_RESULT (r) = NULL_TREE;
12317 TREE_STATIC (r) = 0;
12318 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12319 DECL_EXTERNAL (r) = 1;
12320 /* If this is an instantiation of a function with internal
12321 linkage, we already know what object file linkage will be
12322 assigned to the instantiation. */
12323 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12324 DECL_DEFER_OUTPUT (r) = 0;
12325 DECL_CHAIN (r) = NULL_TREE;
12326 DECL_PENDING_INLINE_INFO (r) = 0;
12327 DECL_PENDING_INLINE_P (r) = 0;
12328 DECL_SAVED_TREE (r) = NULL_TREE;
12329 DECL_STRUCT_FUNCTION (r) = NULL;
12330 TREE_USED (r) = 0;
12331 /* We'll re-clone as appropriate in instantiate_template. */
12332 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12334 /* If we aren't complaining now, return on error before we register
12335 the specialization so that we'll complain eventually. */
12336 if ((complain & tf_error) == 0
12337 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12338 && !grok_op_properties (r, /*complain=*/false))
12339 return error_mark_node;
12341 /* When instantiating a constrained member, substitute
12342 into the constraints to create a new constraint. */
12343 if (tree ci = get_constraints (t))
12344 if (member)
12346 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12347 set_constraints (r, ci);
12350 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12351 this in the special friend case mentioned above where
12352 GEN_TMPL is NULL. */
12353 if (gen_tmpl && !closure)
12355 DECL_TEMPLATE_INFO (r)
12356 = build_template_info (gen_tmpl, argvec);
12357 SET_DECL_IMPLICIT_INSTANTIATION (r);
12359 tree new_r
12360 = register_specialization (r, gen_tmpl, argvec, false, hash);
12361 if (new_r != r)
12362 /* We instantiated this while substituting into
12363 the type earlier (template/friend54.C). */
12364 return new_r;
12366 /* We're not supposed to instantiate default arguments
12367 until they are called, for a template. But, for a
12368 declaration like:
12370 template <class T> void f ()
12371 { extern void g(int i = T()); }
12373 we should do the substitution when the template is
12374 instantiated. We handle the member function case in
12375 instantiate_class_template since the default arguments
12376 might refer to other members of the class. */
12377 if (!member
12378 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12379 && !uses_template_parms (argvec))
12380 tsubst_default_arguments (r, complain);
12382 else
12383 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12385 /* Copy the list of befriending classes. */
12386 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12387 *friends;
12388 friends = &TREE_CHAIN (*friends))
12390 *friends = copy_node (*friends);
12391 TREE_VALUE (*friends)
12392 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12395 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12397 maybe_retrofit_in_chrg (r);
12398 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12399 return error_mark_node;
12400 /* If this is an instantiation of a member template, clone it.
12401 If it isn't, that'll be handled by
12402 clone_constructors_and_destructors. */
12403 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12404 clone_function_decl (r, /*update_methods=*/false);
12406 else if ((complain & tf_error) != 0
12407 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12408 && !grok_op_properties (r, /*complain=*/true))
12409 return error_mark_node;
12411 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12412 SET_DECL_FRIEND_CONTEXT (r,
12413 tsubst (DECL_FRIEND_CONTEXT (t),
12414 args, complain, in_decl));
12416 /* Possibly limit visibility based on template args. */
12417 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12418 if (DECL_VISIBILITY_SPECIFIED (t))
12420 DECL_VISIBILITY_SPECIFIED (r) = 0;
12421 DECL_ATTRIBUTES (r)
12422 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12424 determine_visibility (r);
12425 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12426 && !processing_template_decl)
12427 defaulted_late_check (r);
12429 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12430 args, complain, in_decl);
12431 return r;
12434 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12436 static tree
12437 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12438 tree lambda_fntype)
12440 /* We can get here when processing a member function template,
12441 member class template, or template template parameter. */
12442 tree decl = DECL_TEMPLATE_RESULT (t);
12443 tree in_decl = t;
12444 tree spec;
12445 tree tmpl_args;
12446 tree full_args;
12447 tree r;
12448 hashval_t hash = 0;
12450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12452 /* Template template parameter is treated here. */
12453 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12454 if (new_type == error_mark_node)
12455 r = error_mark_node;
12456 /* If we get a real template back, return it. This can happen in
12457 the context of most_specialized_partial_spec. */
12458 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12459 r = new_type;
12460 else
12461 /* The new TEMPLATE_DECL was built in
12462 reduce_template_parm_level. */
12463 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12464 return r;
12467 if (!lambda_fntype)
12469 /* We might already have an instance of this template.
12470 The ARGS are for the surrounding class type, so the
12471 full args contain the tsubst'd args for the context,
12472 plus the innermost args from the template decl. */
12473 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12474 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12475 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12476 /* Because this is a template, the arguments will still be
12477 dependent, even after substitution. If
12478 PROCESSING_TEMPLATE_DECL is not set, the dependency
12479 predicates will short-circuit. */
12480 ++processing_template_decl;
12481 full_args = tsubst_template_args (tmpl_args, args,
12482 complain, in_decl);
12483 --processing_template_decl;
12484 if (full_args == error_mark_node)
12485 return error_mark_node;
12487 /* If this is a default template template argument,
12488 tsubst might not have changed anything. */
12489 if (full_args == tmpl_args)
12490 return t;
12492 hash = hash_tmpl_and_args (t, full_args);
12493 spec = retrieve_specialization (t, full_args, hash);
12494 if (spec != NULL_TREE)
12495 return spec;
12498 /* Make a new template decl. It will be similar to the
12499 original, but will record the current template arguments.
12500 We also create a new function declaration, which is just
12501 like the old one, but points to this new template, rather
12502 than the old one. */
12503 r = copy_decl (t);
12504 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12505 DECL_CHAIN (r) = NULL_TREE;
12507 // Build new template info linking to the original template decl.
12508 if (!lambda_fntype)
12510 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12511 SET_DECL_IMPLICIT_INSTANTIATION (r);
12513 else
12514 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12516 /* The template parameters for this new template are all the
12517 template parameters for the old template, except the
12518 outermost level of parameters. */
12519 DECL_TEMPLATE_PARMS (r)
12520 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12521 complain);
12523 if (TREE_CODE (decl) == TYPE_DECL
12524 && !TYPE_DECL_ALIAS_P (decl))
12526 tree new_type;
12527 ++processing_template_decl;
12528 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12529 --processing_template_decl;
12530 if (new_type == error_mark_node)
12531 return error_mark_node;
12533 TREE_TYPE (r) = new_type;
12534 /* For a partial specialization, we need to keep pointing to
12535 the primary template. */
12536 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12537 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12538 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12539 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12540 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12542 else
12544 tree new_decl;
12545 ++processing_template_decl;
12546 if (TREE_CODE (decl) == FUNCTION_DECL)
12547 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12548 else
12549 new_decl = tsubst (decl, args, complain, in_decl);
12550 --processing_template_decl;
12551 if (new_decl == error_mark_node)
12552 return error_mark_node;
12554 DECL_TEMPLATE_RESULT (r) = new_decl;
12555 TREE_TYPE (r) = TREE_TYPE (new_decl);
12556 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12557 if (lambda_fntype)
12559 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12560 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12562 else
12564 DECL_TI_TEMPLATE (new_decl) = r;
12565 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12569 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12570 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12572 if (PRIMARY_TEMPLATE_P (t))
12573 DECL_PRIMARY_TEMPLATE (r) = r;
12575 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12576 && !lambda_fntype)
12577 /* Record this non-type partial instantiation. */
12578 register_specialization (r, t,
12579 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12580 false, hash);
12582 return r;
12585 /* True if FN is the op() for a lambda in an uninstantiated template. */
12587 bool
12588 lambda_fn_in_template_p (tree fn)
12590 if (!fn || !LAMBDA_FUNCTION_P (fn))
12591 return false;
12592 tree closure = DECL_CONTEXT (fn);
12593 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12596 /* True if FN is the op() for a lambda regenerated from a lambda in an
12597 uninstantiated template. */
12599 bool
12600 regenerated_lambda_fn_p (tree fn)
12602 return (LAMBDA_FUNCTION_P (fn)
12603 && !DECL_TEMPLATE_INSTANTIATION (fn));
12606 /* We're instantiating a variable from template function TCTX. Return the
12607 corresponding current enclosing scope. This gets complicated because lambda
12608 functions in templates are regenerated rather than instantiated, but generic
12609 lambda functions are subsequently instantiated. */
12611 static tree
12612 enclosing_instantiation_of (tree tctx)
12614 tree fn = current_function_decl;
12615 int lambda_count = 0;
12617 for (; tctx && lambda_fn_in_template_p (tctx);
12618 tctx = decl_function_context (tctx))
12619 ++lambda_count;
12620 for (; fn; fn = decl_function_context (fn))
12622 tree lambda = fn;
12623 int flambda_count = 0;
12624 for (; fn && regenerated_lambda_fn_p (fn);
12625 fn = decl_function_context (fn))
12626 ++flambda_count;
12627 if (DECL_TEMPLATE_INFO (fn)
12628 ? most_general_template (fn) != most_general_template (tctx)
12629 : fn != tctx)
12630 continue;
12631 if (lambda_count)
12633 fn = lambda;
12634 while (flambda_count-- > lambda_count)
12635 fn = decl_function_context (fn);
12637 return fn;
12639 gcc_unreachable ();
12642 /* Substitute the ARGS into the T, which is a _DECL. Return the
12643 result of the substitution. Issue error and warning messages under
12644 control of COMPLAIN. */
12646 static tree
12647 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12649 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12650 location_t saved_loc;
12651 tree r = NULL_TREE;
12652 tree in_decl = t;
12653 hashval_t hash = 0;
12655 /* Set the filename and linenumber to improve error-reporting. */
12656 saved_loc = input_location;
12657 input_location = DECL_SOURCE_LOCATION (t);
12659 switch (TREE_CODE (t))
12661 case TEMPLATE_DECL:
12662 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12663 break;
12665 case FUNCTION_DECL:
12666 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12667 break;
12669 case PARM_DECL:
12671 tree type = NULL_TREE;
12672 int i, len = 1;
12673 tree expanded_types = NULL_TREE;
12674 tree prev_r = NULL_TREE;
12675 tree first_r = NULL_TREE;
12677 if (DECL_PACK_P (t))
12679 /* If there is a local specialization that isn't a
12680 parameter pack, it means that we're doing a "simple"
12681 substitution from inside tsubst_pack_expansion. Just
12682 return the local specialization (which will be a single
12683 parm). */
12684 tree spec = retrieve_local_specialization (t);
12685 if (spec
12686 && TREE_CODE (spec) == PARM_DECL
12687 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12688 RETURN (spec);
12690 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12691 the parameters in this function parameter pack. */
12692 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12693 complain, in_decl);
12694 if (TREE_CODE (expanded_types) == TREE_VEC)
12696 len = TREE_VEC_LENGTH (expanded_types);
12698 /* Zero-length parameter packs are boring. Just substitute
12699 into the chain. */
12700 if (len == 0)
12701 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12702 TREE_CHAIN (t)));
12704 else
12706 /* All we did was update the type. Make a note of that. */
12707 type = expanded_types;
12708 expanded_types = NULL_TREE;
12712 /* Loop through all of the parameters we'll build. When T is
12713 a function parameter pack, LEN is the number of expanded
12714 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12715 r = NULL_TREE;
12716 for (i = 0; i < len; ++i)
12718 prev_r = r;
12719 r = copy_node (t);
12720 if (DECL_TEMPLATE_PARM_P (t))
12721 SET_DECL_TEMPLATE_PARM_P (r);
12723 if (expanded_types)
12724 /* We're on the Ith parameter of the function parameter
12725 pack. */
12727 /* Get the Ith type. */
12728 type = TREE_VEC_ELT (expanded_types, i);
12730 /* Rename the parameter to include the index. */
12731 DECL_NAME (r)
12732 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12734 else if (!type)
12735 /* We're dealing with a normal parameter. */
12736 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12738 type = type_decays_to (type);
12739 TREE_TYPE (r) = type;
12740 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12742 if (DECL_INITIAL (r))
12744 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12745 DECL_INITIAL (r) = TREE_TYPE (r);
12746 else
12747 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12748 complain, in_decl);
12751 DECL_CONTEXT (r) = NULL_TREE;
12753 if (!DECL_TEMPLATE_PARM_P (r))
12754 DECL_ARG_TYPE (r) = type_passed_as (type);
12756 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12757 args, complain, in_decl);
12759 /* Keep track of the first new parameter we
12760 generate. That's what will be returned to the
12761 caller. */
12762 if (!first_r)
12763 first_r = r;
12765 /* Build a proper chain of parameters when substituting
12766 into a function parameter pack. */
12767 if (prev_r)
12768 DECL_CHAIN (prev_r) = r;
12771 /* If cp_unevaluated_operand is set, we're just looking for a
12772 single dummy parameter, so don't keep going. */
12773 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12774 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12775 complain, DECL_CHAIN (t));
12777 /* FIRST_R contains the start of the chain we've built. */
12778 r = first_r;
12780 break;
12782 case FIELD_DECL:
12784 tree type = NULL_TREE;
12785 tree vec = NULL_TREE;
12786 tree expanded_types = NULL_TREE;
12787 int len = 1;
12789 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12791 /* This field is a lambda capture pack. Return a TREE_VEC of
12792 the expanded fields to instantiate_class_template_1 and
12793 store them in the specializations hash table as a
12794 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12795 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12796 complain, in_decl);
12797 if (TREE_CODE (expanded_types) == TREE_VEC)
12799 len = TREE_VEC_LENGTH (expanded_types);
12800 vec = make_tree_vec (len);
12802 else
12804 /* All we did was update the type. Make a note of that. */
12805 type = expanded_types;
12806 expanded_types = NULL_TREE;
12810 for (int i = 0; i < len; ++i)
12812 r = copy_decl (t);
12813 if (expanded_types)
12815 type = TREE_VEC_ELT (expanded_types, i);
12816 DECL_NAME (r)
12817 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12819 else if (!type)
12820 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12822 if (type == error_mark_node)
12823 RETURN (error_mark_node);
12824 TREE_TYPE (r) = type;
12825 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12827 if (DECL_C_BIT_FIELD (r))
12828 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12829 number of bits. */
12830 DECL_BIT_FIELD_REPRESENTATIVE (r)
12831 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12832 complain, in_decl,
12833 /*integral_constant_expression_p=*/true);
12834 if (DECL_INITIAL (t))
12836 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12837 NSDMI in perform_member_init. Still set DECL_INITIAL
12838 so that we know there is one. */
12839 DECL_INITIAL (r) = void_node;
12840 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12841 retrofit_lang_decl (r);
12842 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12844 /* We don't have to set DECL_CONTEXT here; it is set by
12845 finish_member_declaration. */
12846 DECL_CHAIN (r) = NULL_TREE;
12848 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12849 args, complain, in_decl);
12851 if (vec)
12852 TREE_VEC_ELT (vec, i) = r;
12855 if (vec)
12857 r = vec;
12858 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12859 SET_ARGUMENT_PACK_ARGS (pack, vec);
12860 register_specialization (pack, t, args, false, 0);
12863 break;
12865 case USING_DECL:
12866 /* We reach here only for member using decls. We also need to check
12867 uses_template_parms because DECL_DEPENDENT_P is not set for a
12868 using-declaration that designates a member of the current
12869 instantiation (c++/53549). */
12870 if (DECL_DEPENDENT_P (t)
12871 || uses_template_parms (USING_DECL_SCOPE (t)))
12873 tree scope = USING_DECL_SCOPE (t);
12874 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12875 if (PACK_EXPANSION_P (scope))
12877 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12878 int len = TREE_VEC_LENGTH (vec);
12879 r = make_tree_vec (len);
12880 for (int i = 0; i < len; ++i)
12882 tree escope = TREE_VEC_ELT (vec, i);
12883 tree elt = do_class_using_decl (escope, name);
12884 if (!elt)
12886 r = error_mark_node;
12887 break;
12889 else
12891 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12892 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12894 TREE_VEC_ELT (r, i) = elt;
12897 else
12899 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12900 complain, in_decl);
12901 r = do_class_using_decl (inst_scope, name);
12902 if (!r)
12903 r = error_mark_node;
12904 else
12906 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12907 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12911 else
12913 r = copy_node (t);
12914 DECL_CHAIN (r) = NULL_TREE;
12916 break;
12918 case TYPE_DECL:
12919 case VAR_DECL:
12921 tree argvec = NULL_TREE;
12922 tree gen_tmpl = NULL_TREE;
12923 tree spec;
12924 tree tmpl = NULL_TREE;
12925 tree ctx;
12926 tree type = NULL_TREE;
12927 bool local_p;
12929 if (TREE_TYPE (t) == error_mark_node)
12930 RETURN (error_mark_node);
12932 if (TREE_CODE (t) == TYPE_DECL
12933 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12935 /* If this is the canonical decl, we don't have to
12936 mess with instantiations, and often we can't (for
12937 typename, template type parms and such). Note that
12938 TYPE_NAME is not correct for the above test if
12939 we've copied the type for a typedef. */
12940 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12941 if (type == error_mark_node)
12942 RETURN (error_mark_node);
12943 r = TYPE_NAME (type);
12944 break;
12947 /* Check to see if we already have the specialization we
12948 need. */
12949 spec = NULL_TREE;
12950 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12952 /* T is a static data member or namespace-scope entity.
12953 We have to substitute into namespace-scope variables
12954 (not just variable templates) because of cases like:
12956 template <class T> void f() { extern T t; }
12958 where the entity referenced is not known until
12959 instantiation time. */
12960 local_p = false;
12961 ctx = DECL_CONTEXT (t);
12962 if (DECL_CLASS_SCOPE_P (t))
12964 ctx = tsubst_aggr_type (ctx, args,
12965 complain,
12966 in_decl, /*entering_scope=*/1);
12967 /* If CTX is unchanged, then T is in fact the
12968 specialization we want. That situation occurs when
12969 referencing a static data member within in its own
12970 class. We can use pointer equality, rather than
12971 same_type_p, because DECL_CONTEXT is always
12972 canonical... */
12973 if (ctx == DECL_CONTEXT (t)
12974 /* ... unless T is a member template; in which
12975 case our caller can be willing to create a
12976 specialization of that template represented
12977 by T. */
12978 && !(DECL_TI_TEMPLATE (t)
12979 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12980 spec = t;
12983 if (!spec)
12985 tmpl = DECL_TI_TEMPLATE (t);
12986 gen_tmpl = most_general_template (tmpl);
12987 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12988 if (argvec != error_mark_node)
12989 argvec = (coerce_innermost_template_parms
12990 (DECL_TEMPLATE_PARMS (gen_tmpl),
12991 argvec, t, complain,
12992 /*all*/true, /*defarg*/true));
12993 if (argvec == error_mark_node)
12994 RETURN (error_mark_node);
12995 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12996 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12999 else
13001 /* A local variable. */
13002 local_p = true;
13003 /* Subsequent calls to pushdecl will fill this in. */
13004 ctx = NULL_TREE;
13005 /* Unless this is a reference to a static variable from an
13006 enclosing function, in which case we need to fill it in now. */
13007 if (TREE_STATIC (t))
13009 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13010 if (fn != current_function_decl)
13011 ctx = fn;
13013 spec = retrieve_local_specialization (t);
13015 /* If we already have the specialization we need, there is
13016 nothing more to do. */
13017 if (spec)
13019 r = spec;
13020 break;
13023 /* Create a new node for the specialization we need. */
13024 r = copy_decl (t);
13025 if (type == NULL_TREE)
13027 if (is_typedef_decl (t))
13028 type = DECL_ORIGINAL_TYPE (t);
13029 else
13030 type = TREE_TYPE (t);
13031 if (VAR_P (t)
13032 && VAR_HAD_UNKNOWN_BOUND (t)
13033 && type != error_mark_node)
13034 type = strip_array_domain (type);
13035 tree sub_args = args;
13036 if (tree auto_node = type_uses_auto (type))
13038 /* Mask off any template args past the variable's context so we
13039 don't replace the auto with an unrelated argument. */
13040 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13041 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13042 if (extra > 0)
13043 /* This should never happen with the new lambda instantiation
13044 model, but keep the handling just in case. */
13045 gcc_assert (!CHECKING_P),
13046 sub_args = strip_innermost_template_args (args, extra);
13048 type = tsubst (type, sub_args, complain, in_decl);
13050 if (VAR_P (r))
13052 /* Even if the original location is out of scope, the
13053 newly substituted one is not. */
13054 DECL_DEAD_FOR_LOCAL (r) = 0;
13055 DECL_INITIALIZED_P (r) = 0;
13056 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13057 if (type == error_mark_node)
13058 RETURN (error_mark_node);
13059 if (TREE_CODE (type) == FUNCTION_TYPE)
13061 /* It may seem that this case cannot occur, since:
13063 typedef void f();
13064 void g() { f x; }
13066 declares a function, not a variable. However:
13068 typedef void f();
13069 template <typename T> void g() { T t; }
13070 template void g<f>();
13072 is an attempt to declare a variable with function
13073 type. */
13074 error ("variable %qD has function type",
13075 /* R is not yet sufficiently initialized, so we
13076 just use its name. */
13077 DECL_NAME (r));
13078 RETURN (error_mark_node);
13080 type = complete_type (type);
13081 /* Wait until cp_finish_decl to set this again, to handle
13082 circular dependency (template/instantiate6.C). */
13083 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13084 type = check_var_type (DECL_NAME (r), type);
13086 if (DECL_HAS_VALUE_EXPR_P (t))
13088 tree ve = DECL_VALUE_EXPR (t);
13089 ve = tsubst_expr (ve, args, complain, in_decl,
13090 /*constant_expression_p=*/false);
13091 if (REFERENCE_REF_P (ve))
13093 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13094 ve = TREE_OPERAND (ve, 0);
13096 SET_DECL_VALUE_EXPR (r, ve);
13098 if (CP_DECL_THREAD_LOCAL_P (r)
13099 && !processing_template_decl)
13100 set_decl_tls_model (r, decl_default_tls_model (r));
13102 else if (DECL_SELF_REFERENCE_P (t))
13103 SET_DECL_SELF_REFERENCE_P (r);
13104 TREE_TYPE (r) = type;
13105 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13106 DECL_CONTEXT (r) = ctx;
13107 /* Clear out the mangled name and RTL for the instantiation. */
13108 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13109 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13110 SET_DECL_RTL (r, NULL);
13111 /* The initializer must not be expanded until it is required;
13112 see [temp.inst]. */
13113 DECL_INITIAL (r) = NULL_TREE;
13114 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13115 if (VAR_P (r))
13117 SET_DECL_MODE (r, VOIDmode);
13119 /* Possibly limit visibility based on template args. */
13120 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13121 if (DECL_VISIBILITY_SPECIFIED (t))
13123 DECL_VISIBILITY_SPECIFIED (r) = 0;
13124 DECL_ATTRIBUTES (r)
13125 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13127 determine_visibility (r);
13130 if (!local_p)
13132 /* A static data member declaration is always marked
13133 external when it is declared in-class, even if an
13134 initializer is present. We mimic the non-template
13135 processing here. */
13136 DECL_EXTERNAL (r) = 1;
13137 if (DECL_NAMESPACE_SCOPE_P (t))
13138 DECL_NOT_REALLY_EXTERN (r) = 1;
13140 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13141 SET_DECL_IMPLICIT_INSTANTIATION (r);
13142 register_specialization (r, gen_tmpl, argvec, false, hash);
13144 else
13146 if (DECL_LANG_SPECIFIC (r))
13147 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13148 if (!cp_unevaluated_operand)
13149 register_local_specialization (r, t);
13152 DECL_CHAIN (r) = NULL_TREE;
13154 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13155 /*flags=*/0,
13156 args, complain, in_decl);
13158 /* Preserve a typedef that names a type. */
13159 if (is_typedef_decl (r) && type != error_mark_node)
13161 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13162 set_underlying_type (r);
13163 if (TYPE_DECL_ALIAS_P (r))
13164 /* An alias template specialization can be dependent
13165 even if its underlying type is not. */
13166 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13169 layout_decl (r, 0);
13171 break;
13173 default:
13174 gcc_unreachable ();
13176 #undef RETURN
13178 out:
13179 /* Restore the file and line information. */
13180 input_location = saved_loc;
13182 return r;
13185 /* Substitute into the ARG_TYPES of a function type.
13186 If END is a TREE_CHAIN, leave it and any following types
13187 un-substituted. */
13189 static tree
13190 tsubst_arg_types (tree arg_types,
13191 tree args,
13192 tree end,
13193 tsubst_flags_t complain,
13194 tree in_decl)
13196 tree remaining_arg_types;
13197 tree type = NULL_TREE;
13198 int i = 1;
13199 tree expanded_args = NULL_TREE;
13200 tree default_arg;
13202 if (!arg_types || arg_types == void_list_node || arg_types == end)
13203 return arg_types;
13205 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13206 args, end, complain, in_decl);
13207 if (remaining_arg_types == error_mark_node)
13208 return error_mark_node;
13210 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13212 /* For a pack expansion, perform substitution on the
13213 entire expression. Later on, we'll handle the arguments
13214 one-by-one. */
13215 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13216 args, complain, in_decl);
13218 if (TREE_CODE (expanded_args) == TREE_VEC)
13219 /* So that we'll spin through the parameters, one by one. */
13220 i = TREE_VEC_LENGTH (expanded_args);
13221 else
13223 /* We only partially substituted into the parameter
13224 pack. Our type is TYPE_PACK_EXPANSION. */
13225 type = expanded_args;
13226 expanded_args = NULL_TREE;
13230 while (i > 0) {
13231 --i;
13233 if (expanded_args)
13234 type = TREE_VEC_ELT (expanded_args, i);
13235 else if (!type)
13236 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13238 if (type == error_mark_node)
13239 return error_mark_node;
13240 if (VOID_TYPE_P (type))
13242 if (complain & tf_error)
13244 error ("invalid parameter type %qT", type);
13245 if (in_decl)
13246 error ("in declaration %q+D", in_decl);
13248 return error_mark_node;
13250 /* DR 657. */
13251 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13252 return error_mark_node;
13254 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13255 top-level qualifiers as required. */
13256 type = cv_unqualified (type_decays_to (type));
13258 /* We do not substitute into default arguments here. The standard
13259 mandates that they be instantiated only when needed, which is
13260 done in build_over_call. */
13261 default_arg = TREE_PURPOSE (arg_types);
13263 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13264 since the new op() won't have any associated template arguments for us
13265 to refer to later. */
13266 if (lambda_fn_in_template_p (in_decl))
13267 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13268 false/*fn*/, false/*constexpr*/);
13270 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13272 /* We've instantiated a template before its default arguments
13273 have been parsed. This can happen for a nested template
13274 class, and is not an error unless we require the default
13275 argument in a call of this function. */
13276 remaining_arg_types =
13277 tree_cons (default_arg, type, remaining_arg_types);
13278 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13280 else
13281 remaining_arg_types =
13282 hash_tree_cons (default_arg, type, remaining_arg_types);
13285 return remaining_arg_types;
13288 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13289 *not* handle the exception-specification for FNTYPE, because the
13290 initial substitution of explicitly provided template parameters
13291 during argument deduction forbids substitution into the
13292 exception-specification:
13294 [temp.deduct]
13296 All references in the function type of the function template to the
13297 corresponding template parameters are replaced by the specified tem-
13298 plate argument values. If a substitution in a template parameter or
13299 in the function type of the function template results in an invalid
13300 type, type deduction fails. [Note: The equivalent substitution in
13301 exception specifications is done only when the function is instanti-
13302 ated, at which point a program is ill-formed if the substitution
13303 results in an invalid type.] */
13305 static tree
13306 tsubst_function_type (tree t,
13307 tree args,
13308 tsubst_flags_t complain,
13309 tree in_decl)
13311 tree return_type;
13312 tree arg_types = NULL_TREE;
13313 tree fntype;
13315 /* The TYPE_CONTEXT is not used for function/method types. */
13316 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13318 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13319 failure. */
13320 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13322 if (late_return_type_p)
13324 /* Substitute the argument types. */
13325 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13326 complain, in_decl);
13327 if (arg_types == error_mark_node)
13328 return error_mark_node;
13330 tree save_ccp = current_class_ptr;
13331 tree save_ccr = current_class_ref;
13332 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13333 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13334 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13335 if (do_inject)
13337 /* DR 1207: 'this' is in scope in the trailing return type. */
13338 inject_this_parameter (this_type, cp_type_quals (this_type));
13341 /* Substitute the return type. */
13342 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13344 if (do_inject)
13346 current_class_ptr = save_ccp;
13347 current_class_ref = save_ccr;
13350 else
13351 /* Substitute the return type. */
13352 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13354 if (return_type == error_mark_node)
13355 return error_mark_node;
13356 /* DR 486 clarifies that creation of a function type with an
13357 invalid return type is a deduction failure. */
13358 if (TREE_CODE (return_type) == ARRAY_TYPE
13359 || TREE_CODE (return_type) == FUNCTION_TYPE)
13361 if (complain & tf_error)
13363 if (TREE_CODE (return_type) == ARRAY_TYPE)
13364 error ("function returning an array");
13365 else
13366 error ("function returning a function");
13368 return error_mark_node;
13370 /* And DR 657. */
13371 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13372 return error_mark_node;
13374 if (!late_return_type_p)
13376 /* Substitute the argument types. */
13377 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13378 complain, in_decl);
13379 if (arg_types == error_mark_node)
13380 return error_mark_node;
13383 /* Construct a new type node and return it. */
13384 if (TREE_CODE (t) == FUNCTION_TYPE)
13386 fntype = build_function_type (return_type, arg_types);
13387 fntype = apply_memfn_quals (fntype,
13388 type_memfn_quals (t),
13389 type_memfn_rqual (t));
13391 else
13393 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13394 /* Don't pick up extra function qualifiers from the basetype. */
13395 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13396 if (! MAYBE_CLASS_TYPE_P (r))
13398 /* [temp.deduct]
13400 Type deduction may fail for any of the following
13401 reasons:
13403 -- Attempting to create "pointer to member of T" when T
13404 is not a class type. */
13405 if (complain & tf_error)
13406 error ("creating pointer to member function of non-class type %qT",
13408 return error_mark_node;
13411 fntype = build_method_type_directly (r, return_type,
13412 TREE_CHAIN (arg_types));
13413 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13415 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13417 if (late_return_type_p)
13418 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13420 return fntype;
13423 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13424 ARGS into that specification, and return the substituted
13425 specification. If there is no specification, return NULL_TREE. */
13427 static tree
13428 tsubst_exception_specification (tree fntype,
13429 tree args,
13430 tsubst_flags_t complain,
13431 tree in_decl,
13432 bool defer_ok)
13434 tree specs;
13435 tree new_specs;
13437 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13438 new_specs = NULL_TREE;
13439 if (specs && TREE_PURPOSE (specs))
13441 /* A noexcept-specifier. */
13442 tree expr = TREE_PURPOSE (specs);
13443 if (TREE_CODE (expr) == INTEGER_CST)
13444 new_specs = expr;
13445 else if (defer_ok)
13447 /* Defer instantiation of noexcept-specifiers to avoid
13448 excessive instantiations (c++/49107). */
13449 new_specs = make_node (DEFERRED_NOEXCEPT);
13450 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13452 /* We already partially instantiated this member template,
13453 so combine the new args with the old. */
13454 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13455 = DEFERRED_NOEXCEPT_PATTERN (expr);
13456 DEFERRED_NOEXCEPT_ARGS (new_specs)
13457 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13459 else
13461 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13462 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13465 else
13466 new_specs = tsubst_copy_and_build
13467 (expr, args, complain, in_decl, /*function_p=*/false,
13468 /*integral_constant_expression_p=*/true);
13469 new_specs = build_noexcept_spec (new_specs, complain);
13471 else if (specs)
13473 if (! TREE_VALUE (specs))
13474 new_specs = specs;
13475 else
13476 while (specs)
13478 tree spec;
13479 int i, len = 1;
13480 tree expanded_specs = NULL_TREE;
13482 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13484 /* Expand the pack expansion type. */
13485 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13486 args, complain,
13487 in_decl);
13489 if (expanded_specs == error_mark_node)
13490 return error_mark_node;
13491 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13492 len = TREE_VEC_LENGTH (expanded_specs);
13493 else
13495 /* We're substituting into a member template, so
13496 we got a TYPE_PACK_EXPANSION back. Add that
13497 expansion and move on. */
13498 gcc_assert (TREE_CODE (expanded_specs)
13499 == TYPE_PACK_EXPANSION);
13500 new_specs = add_exception_specifier (new_specs,
13501 expanded_specs,
13502 complain);
13503 specs = TREE_CHAIN (specs);
13504 continue;
13508 for (i = 0; i < len; ++i)
13510 if (expanded_specs)
13511 spec = TREE_VEC_ELT (expanded_specs, i);
13512 else
13513 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13514 if (spec == error_mark_node)
13515 return spec;
13516 new_specs = add_exception_specifier (new_specs, spec,
13517 complain);
13520 specs = TREE_CHAIN (specs);
13523 return new_specs;
13526 /* Take the tree structure T and replace template parameters used
13527 therein with the argument vector ARGS. IN_DECL is an associated
13528 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13529 Issue error and warning messages under control of COMPLAIN. Note
13530 that we must be relatively non-tolerant of extensions here, in
13531 order to preserve conformance; if we allow substitutions that
13532 should not be allowed, we may allow argument deductions that should
13533 not succeed, and therefore report ambiguous overload situations
13534 where there are none. In theory, we could allow the substitution,
13535 but indicate that it should have failed, and allow our caller to
13536 make sure that the right thing happens, but we don't try to do this
13537 yet.
13539 This function is used for dealing with types, decls and the like;
13540 for expressions, use tsubst_expr or tsubst_copy. */
13542 tree
13543 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13545 enum tree_code code;
13546 tree type, r = NULL_TREE;
13548 if (t == NULL_TREE || t == error_mark_node
13549 || t == integer_type_node
13550 || t == void_type_node
13551 || t == char_type_node
13552 || t == unknown_type_node
13553 || TREE_CODE (t) == NAMESPACE_DECL
13554 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13555 return t;
13557 if (DECL_P (t))
13558 return tsubst_decl (t, args, complain);
13560 if (args == NULL_TREE)
13561 return t;
13563 code = TREE_CODE (t);
13565 if (code == IDENTIFIER_NODE)
13566 type = IDENTIFIER_TYPE_VALUE (t);
13567 else
13568 type = TREE_TYPE (t);
13570 gcc_assert (type != unknown_type_node);
13572 /* Reuse typedefs. We need to do this to handle dependent attributes,
13573 such as attribute aligned. */
13574 if (TYPE_P (t)
13575 && typedef_variant_p (t))
13577 tree decl = TYPE_NAME (t);
13579 if (alias_template_specialization_p (t))
13581 /* DECL represents an alias template and we want to
13582 instantiate it. */
13583 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13584 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13585 r = instantiate_alias_template (tmpl, gen_args, complain);
13587 else if (DECL_CLASS_SCOPE_P (decl)
13588 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13589 && uses_template_parms (DECL_CONTEXT (decl)))
13591 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13592 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13593 r = retrieve_specialization (tmpl, gen_args, 0);
13595 else if (DECL_FUNCTION_SCOPE_P (decl)
13596 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13597 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13598 r = retrieve_local_specialization (decl);
13599 else
13600 /* The typedef is from a non-template context. */
13601 return t;
13603 if (r)
13605 r = TREE_TYPE (r);
13606 r = cp_build_qualified_type_real
13607 (r, cp_type_quals (t) | cp_type_quals (r),
13608 complain | tf_ignore_bad_quals);
13609 return r;
13611 else
13613 /* We don't have an instantiation yet, so drop the typedef. */
13614 int quals = cp_type_quals (t);
13615 t = DECL_ORIGINAL_TYPE (decl);
13616 t = cp_build_qualified_type_real (t, quals,
13617 complain | tf_ignore_bad_quals);
13621 bool fndecl_type = (complain & tf_fndecl_type);
13622 complain &= ~tf_fndecl_type;
13624 if (type
13625 && code != TYPENAME_TYPE
13626 && code != TEMPLATE_TYPE_PARM
13627 && code != TEMPLATE_PARM_INDEX
13628 && code != IDENTIFIER_NODE
13629 && code != FUNCTION_TYPE
13630 && code != METHOD_TYPE)
13631 type = tsubst (type, args, complain, in_decl);
13632 if (type == error_mark_node)
13633 return error_mark_node;
13635 switch (code)
13637 case RECORD_TYPE:
13638 case UNION_TYPE:
13639 case ENUMERAL_TYPE:
13640 return tsubst_aggr_type (t, args, complain, in_decl,
13641 /*entering_scope=*/0);
13643 case ERROR_MARK:
13644 case IDENTIFIER_NODE:
13645 case VOID_TYPE:
13646 case REAL_TYPE:
13647 case COMPLEX_TYPE:
13648 case VECTOR_TYPE:
13649 case BOOLEAN_TYPE:
13650 case NULLPTR_TYPE:
13651 case LANG_TYPE:
13652 return t;
13654 case INTEGER_TYPE:
13655 if (t == integer_type_node)
13656 return t;
13658 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13659 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13660 return t;
13663 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13665 max = tsubst_expr (omax, args, complain, in_decl,
13666 /*integral_constant_expression_p=*/false);
13668 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13669 needed. */
13670 if (TREE_CODE (max) == NOP_EXPR
13671 && TREE_SIDE_EFFECTS (omax)
13672 && !TREE_TYPE (max))
13673 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13675 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13676 with TREE_SIDE_EFFECTS that indicates this is not an integral
13677 constant expression. */
13678 if (processing_template_decl
13679 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13681 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13682 TREE_SIDE_EFFECTS (max) = 1;
13685 return compute_array_index_type (NULL_TREE, max, complain);
13688 case TEMPLATE_TYPE_PARM:
13689 case TEMPLATE_TEMPLATE_PARM:
13690 case BOUND_TEMPLATE_TEMPLATE_PARM:
13691 case TEMPLATE_PARM_INDEX:
13693 int idx;
13694 int level;
13695 int levels;
13696 tree arg = NULL_TREE;
13698 /* Early in template argument deduction substitution, we don't
13699 want to reduce the level of 'auto', or it will be confused
13700 with a normal template parm in subsequent deduction. */
13701 if (is_auto (t) && (complain & tf_partial))
13702 return t;
13704 r = NULL_TREE;
13706 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13707 template_parm_level_and_index (t, &level, &idx);
13709 levels = TMPL_ARGS_DEPTH (args);
13710 if (level <= levels
13711 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13713 arg = TMPL_ARG (args, level, idx);
13715 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13717 /* See through ARGUMENT_PACK_SELECT arguments. */
13718 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13719 /* If the selected argument is an expansion E, that most
13720 likely means we were called from
13721 gen_elem_of_pack_expansion_instantiation during the
13722 substituting of pack an argument pack (which Ith
13723 element is a pack expansion, where I is
13724 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13725 In this case, the Ith element resulting from this
13726 substituting is going to be a pack expansion, which
13727 pattern is the pattern of E. Let's return the
13728 pattern of E, and
13729 gen_elem_of_pack_expansion_instantiation will
13730 build the resulting pack expansion from it. */
13731 if (PACK_EXPANSION_P (arg))
13733 /* Make sure we aren't throwing away arg info. */
13734 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13735 arg = PACK_EXPANSION_PATTERN (arg);
13740 if (arg == error_mark_node)
13741 return error_mark_node;
13742 else if (arg != NULL_TREE)
13744 if (ARGUMENT_PACK_P (arg))
13745 /* If ARG is an argument pack, we don't actually want to
13746 perform a substitution here, because substitutions
13747 for argument packs are only done
13748 element-by-element. We can get to this point when
13749 substituting the type of a non-type template
13750 parameter pack, when that type actually contains
13751 template parameter packs from an outer template, e.g.,
13753 template<typename... Types> struct A {
13754 template<Types... Values> struct B { };
13755 }; */
13756 return t;
13758 if (code == TEMPLATE_TYPE_PARM)
13760 int quals;
13761 gcc_assert (TYPE_P (arg));
13763 quals = cp_type_quals (arg) | cp_type_quals (t);
13765 return cp_build_qualified_type_real
13766 (arg, quals, complain | tf_ignore_bad_quals);
13768 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13770 /* We are processing a type constructed from a
13771 template template parameter. */
13772 tree argvec = tsubst (TYPE_TI_ARGS (t),
13773 args, complain, in_decl);
13774 if (argvec == error_mark_node)
13775 return error_mark_node;
13777 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13778 || TREE_CODE (arg) == TEMPLATE_DECL
13779 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13781 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13782 /* Consider this code:
13784 template <template <class> class Template>
13785 struct Internal {
13786 template <class Arg> using Bind = Template<Arg>;
13789 template <template <class> class Template, class Arg>
13790 using Instantiate = Template<Arg>; //#0
13792 template <template <class> class Template,
13793 class Argument>
13794 using Bind =
13795 Instantiate<Internal<Template>::template Bind,
13796 Argument>; //#1
13798 When #1 is parsed, the
13799 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13800 parameter `Template' in #0 matches the
13801 UNBOUND_CLASS_TEMPLATE representing the argument
13802 `Internal<Template>::template Bind'; We then want
13803 to assemble the type `Bind<Argument>' that can't
13804 be fully created right now, because
13805 `Internal<Template>' not being complete, the Bind
13806 template cannot be looked up in that context. So
13807 we need to "store" `Bind<Argument>' for later
13808 when the context of Bind becomes complete. Let's
13809 store that in a TYPENAME_TYPE. */
13810 return make_typename_type (TYPE_CONTEXT (arg),
13811 build_nt (TEMPLATE_ID_EXPR,
13812 TYPE_IDENTIFIER (arg),
13813 argvec),
13814 typename_type,
13815 complain);
13817 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13818 are resolving nested-types in the signature of a
13819 member function templates. Otherwise ARG is a
13820 TEMPLATE_DECL and is the real template to be
13821 instantiated. */
13822 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13823 arg = TYPE_NAME (arg);
13825 r = lookup_template_class (arg,
13826 argvec, in_decl,
13827 DECL_CONTEXT (arg),
13828 /*entering_scope=*/0,
13829 complain);
13830 return cp_build_qualified_type_real
13831 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13833 else if (code == TEMPLATE_TEMPLATE_PARM)
13834 return arg;
13835 else
13836 /* TEMPLATE_PARM_INDEX. */
13837 return convert_from_reference (unshare_expr (arg));
13840 if (level == 1)
13841 /* This can happen during the attempted tsubst'ing in
13842 unify. This means that we don't yet have any information
13843 about the template parameter in question. */
13844 return t;
13846 /* If we get here, we must have been looking at a parm for a
13847 more deeply nested template. Make a new version of this
13848 template parameter, but with a lower level. */
13849 switch (code)
13851 case TEMPLATE_TYPE_PARM:
13852 case TEMPLATE_TEMPLATE_PARM:
13853 case BOUND_TEMPLATE_TEMPLATE_PARM:
13854 if (cp_type_quals (t))
13856 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13857 r = cp_build_qualified_type_real
13858 (r, cp_type_quals (t),
13859 complain | (code == TEMPLATE_TYPE_PARM
13860 ? tf_ignore_bad_quals : 0));
13862 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13863 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13864 && (r = (TEMPLATE_PARM_DESCENDANTS
13865 (TEMPLATE_TYPE_PARM_INDEX (t))))
13866 && (r = TREE_TYPE (r))
13867 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13868 /* Break infinite recursion when substituting the constraints
13869 of a constrained placeholder. */;
13870 else
13872 r = copy_type (t);
13873 TEMPLATE_TYPE_PARM_INDEX (r)
13874 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13875 r, levels, args, complain);
13876 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13877 TYPE_MAIN_VARIANT (r) = r;
13878 TYPE_POINTER_TO (r) = NULL_TREE;
13879 TYPE_REFERENCE_TO (r) = NULL_TREE;
13881 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13883 /* Propagate constraints on placeholders. */
13884 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13885 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13886 = tsubst_constraint (constr, args, complain, in_decl);
13887 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13889 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13890 pl = tsubst (pl, args, complain, in_decl);
13891 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13895 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13896 /* We have reduced the level of the template
13897 template parameter, but not the levels of its
13898 template parameters, so canonical_type_parameter
13899 will not be able to find the canonical template
13900 template parameter for this level. Thus, we
13901 require structural equality checking to compare
13902 TEMPLATE_TEMPLATE_PARMs. */
13903 SET_TYPE_STRUCTURAL_EQUALITY (r);
13904 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13905 SET_TYPE_STRUCTURAL_EQUALITY (r);
13906 else
13907 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13909 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13911 tree tinfo = TYPE_TEMPLATE_INFO (t);
13912 /* We might need to substitute into the types of non-type
13913 template parameters. */
13914 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13915 complain, in_decl);
13916 if (tmpl == error_mark_node)
13917 return error_mark_node;
13918 tree argvec = tsubst (TI_ARGS (tinfo), args,
13919 complain, in_decl);
13920 if (argvec == error_mark_node)
13921 return error_mark_node;
13923 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13924 = build_template_info (tmpl, argvec);
13927 break;
13929 case TEMPLATE_PARM_INDEX:
13930 /* OK, now substitute the type of the non-type parameter. We
13931 couldn't do it earlier because it might be an auto parameter,
13932 and we wouldn't need to if we had an argument. */
13933 type = tsubst (type, args, complain, in_decl);
13934 if (type == error_mark_node)
13935 return error_mark_node;
13936 r = reduce_template_parm_level (t, type, levels, args, complain);
13937 break;
13939 default:
13940 gcc_unreachable ();
13943 return r;
13946 case TREE_LIST:
13948 tree purpose, value, chain;
13950 if (t == void_list_node)
13951 return t;
13953 purpose = TREE_PURPOSE (t);
13954 if (purpose)
13956 purpose = tsubst (purpose, args, complain, in_decl);
13957 if (purpose == error_mark_node)
13958 return error_mark_node;
13960 value = TREE_VALUE (t);
13961 if (value)
13963 value = tsubst (value, args, complain, in_decl);
13964 if (value == error_mark_node)
13965 return error_mark_node;
13967 chain = TREE_CHAIN (t);
13968 if (chain && chain != void_type_node)
13970 chain = tsubst (chain, args, complain, in_decl);
13971 if (chain == error_mark_node)
13972 return error_mark_node;
13974 if (purpose == TREE_PURPOSE (t)
13975 && value == TREE_VALUE (t)
13976 && chain == TREE_CHAIN (t))
13977 return t;
13978 return hash_tree_cons (purpose, value, chain);
13981 case TREE_BINFO:
13982 /* We should never be tsubsting a binfo. */
13983 gcc_unreachable ();
13985 case TREE_VEC:
13986 /* A vector of template arguments. */
13987 gcc_assert (!type);
13988 return tsubst_template_args (t, args, complain, in_decl);
13990 case POINTER_TYPE:
13991 case REFERENCE_TYPE:
13993 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13994 return t;
13996 /* [temp.deduct]
13998 Type deduction may fail for any of the following
13999 reasons:
14001 -- Attempting to create a pointer to reference type.
14002 -- Attempting to create a reference to a reference type or
14003 a reference to void.
14005 Core issue 106 says that creating a reference to a reference
14006 during instantiation is no longer a cause for failure. We
14007 only enforce this check in strict C++98 mode. */
14008 if ((TREE_CODE (type) == REFERENCE_TYPE
14009 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14010 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14012 static location_t last_loc;
14014 /* We keep track of the last time we issued this error
14015 message to avoid spewing a ton of messages during a
14016 single bad template instantiation. */
14017 if (complain & tf_error
14018 && last_loc != input_location)
14020 if (VOID_TYPE_P (type))
14021 error ("forming reference to void");
14022 else if (code == POINTER_TYPE)
14023 error ("forming pointer to reference type %qT", type);
14024 else
14025 error ("forming reference to reference type %qT", type);
14026 last_loc = input_location;
14029 return error_mark_node;
14031 else if (TREE_CODE (type) == FUNCTION_TYPE
14032 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14033 || type_memfn_rqual (type) != REF_QUAL_NONE))
14035 if (complain & tf_error)
14037 if (code == POINTER_TYPE)
14038 error ("forming pointer to qualified function type %qT",
14039 type);
14040 else
14041 error ("forming reference to qualified function type %qT",
14042 type);
14044 return error_mark_node;
14046 else if (code == POINTER_TYPE)
14048 r = build_pointer_type (type);
14049 if (TREE_CODE (type) == METHOD_TYPE)
14050 r = build_ptrmemfunc_type (r);
14052 else if (TREE_CODE (type) == REFERENCE_TYPE)
14053 /* In C++0x, during template argument substitution, when there is an
14054 attempt to create a reference to a reference type, reference
14055 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14057 "If a template-argument for a template-parameter T names a type
14058 that is a reference to a type A, an attempt to create the type
14059 'lvalue reference to cv T' creates the type 'lvalue reference to
14060 A,' while an attempt to create the type type rvalue reference to
14061 cv T' creates the type T"
14063 r = cp_build_reference_type
14064 (TREE_TYPE (type),
14065 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14066 else
14067 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14068 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14070 if (r != error_mark_node)
14071 /* Will this ever be needed for TYPE_..._TO values? */
14072 layout_type (r);
14074 return r;
14076 case OFFSET_TYPE:
14078 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14079 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14081 /* [temp.deduct]
14083 Type deduction may fail for any of the following
14084 reasons:
14086 -- Attempting to create "pointer to member of T" when T
14087 is not a class type. */
14088 if (complain & tf_error)
14089 error ("creating pointer to member of non-class type %qT", r);
14090 return error_mark_node;
14092 if (TREE_CODE (type) == REFERENCE_TYPE)
14094 if (complain & tf_error)
14095 error ("creating pointer to member reference type %qT", type);
14096 return error_mark_node;
14098 if (VOID_TYPE_P (type))
14100 if (complain & tf_error)
14101 error ("creating pointer to member of type void");
14102 return error_mark_node;
14104 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14105 if (TREE_CODE (type) == FUNCTION_TYPE)
14107 /* The type of the implicit object parameter gets its
14108 cv-qualifiers from the FUNCTION_TYPE. */
14109 tree memptr;
14110 tree method_type
14111 = build_memfn_type (type, r, type_memfn_quals (type),
14112 type_memfn_rqual (type));
14113 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14114 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14115 complain);
14117 else
14118 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14119 cp_type_quals (t),
14120 complain);
14122 case FUNCTION_TYPE:
14123 case METHOD_TYPE:
14125 tree fntype;
14126 tree specs;
14127 fntype = tsubst_function_type (t, args, complain, in_decl);
14128 if (fntype == error_mark_node)
14129 return error_mark_node;
14131 /* Substitute the exception specification. */
14132 specs = tsubst_exception_specification (t, args, complain, in_decl,
14133 /*defer_ok*/fndecl_type);
14134 if (specs == error_mark_node)
14135 return error_mark_node;
14136 if (specs)
14137 fntype = build_exception_variant (fntype, specs);
14138 return fntype;
14140 case ARRAY_TYPE:
14142 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14143 if (domain == error_mark_node)
14144 return error_mark_node;
14146 /* As an optimization, we avoid regenerating the array type if
14147 it will obviously be the same as T. */
14148 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14149 return t;
14151 /* These checks should match the ones in create_array_type_for_decl.
14153 [temp.deduct]
14155 The deduction may fail for any of the following reasons:
14157 -- Attempting to create an array with an element type that
14158 is void, a function type, or a reference type, or [DR337]
14159 an abstract class type. */
14160 if (VOID_TYPE_P (type)
14161 || TREE_CODE (type) == FUNCTION_TYPE
14162 || (TREE_CODE (type) == ARRAY_TYPE
14163 && TYPE_DOMAIN (type) == NULL_TREE)
14164 || TREE_CODE (type) == REFERENCE_TYPE)
14166 if (complain & tf_error)
14167 error ("creating array of %qT", type);
14168 return error_mark_node;
14171 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14172 return error_mark_node;
14174 r = build_cplus_array_type (type, domain);
14176 if (TYPE_USER_ALIGN (t))
14178 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14179 TYPE_USER_ALIGN (r) = 1;
14182 return r;
14185 case TYPENAME_TYPE:
14187 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14188 in_decl, /*entering_scope=*/1);
14189 if (ctx == error_mark_node)
14190 return error_mark_node;
14192 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14193 complain, in_decl);
14194 if (f == error_mark_node)
14195 return error_mark_node;
14197 if (!MAYBE_CLASS_TYPE_P (ctx))
14199 if (complain & tf_error)
14200 error ("%qT is not a class, struct, or union type", ctx);
14201 return error_mark_node;
14203 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14205 /* Normally, make_typename_type does not require that the CTX
14206 have complete type in order to allow things like:
14208 template <class T> struct S { typename S<T>::X Y; };
14210 But, such constructs have already been resolved by this
14211 point, so here CTX really should have complete type, unless
14212 it's a partial instantiation. */
14213 ctx = complete_type (ctx);
14214 if (!COMPLETE_TYPE_P (ctx))
14216 if (complain & tf_error)
14217 cxx_incomplete_type_error (NULL_TREE, ctx);
14218 return error_mark_node;
14222 f = make_typename_type (ctx, f, typename_type,
14223 complain | tf_keep_type_decl);
14224 if (f == error_mark_node)
14225 return f;
14226 if (TREE_CODE (f) == TYPE_DECL)
14228 complain |= tf_ignore_bad_quals;
14229 f = TREE_TYPE (f);
14232 if (TREE_CODE (f) != TYPENAME_TYPE)
14234 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14236 if (complain & tf_error)
14237 error ("%qT resolves to %qT, which is not an enumeration type",
14238 t, f);
14239 else
14240 return error_mark_node;
14242 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14244 if (complain & tf_error)
14245 error ("%qT resolves to %qT, which is is not a class type",
14246 t, f);
14247 else
14248 return error_mark_node;
14252 return cp_build_qualified_type_real
14253 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14256 case UNBOUND_CLASS_TEMPLATE:
14258 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14259 in_decl, /*entering_scope=*/1);
14260 tree name = TYPE_IDENTIFIER (t);
14261 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14263 if (ctx == error_mark_node || name == error_mark_node)
14264 return error_mark_node;
14266 if (parm_list)
14267 parm_list = tsubst_template_parms (parm_list, args, complain);
14268 return make_unbound_class_template (ctx, name, parm_list, complain);
14271 case TYPEOF_TYPE:
14273 tree type;
14275 ++cp_unevaluated_operand;
14276 ++c_inhibit_evaluation_warnings;
14278 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14279 complain, in_decl,
14280 /*integral_constant_expression_p=*/false);
14282 --cp_unevaluated_operand;
14283 --c_inhibit_evaluation_warnings;
14285 type = finish_typeof (type);
14286 return cp_build_qualified_type_real (type,
14287 cp_type_quals (t)
14288 | cp_type_quals (type),
14289 complain);
14292 case DECLTYPE_TYPE:
14294 tree type;
14296 ++cp_unevaluated_operand;
14297 ++c_inhibit_evaluation_warnings;
14299 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14300 complain|tf_decltype, in_decl,
14301 /*function_p*/false,
14302 /*integral_constant_expression*/false);
14304 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14306 if (type == NULL_TREE)
14308 if (complain & tf_error)
14309 error ("empty initializer in lambda init-capture");
14310 type = error_mark_node;
14312 else if (TREE_CODE (type) == TREE_LIST)
14313 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14316 --cp_unevaluated_operand;
14317 --c_inhibit_evaluation_warnings;
14319 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14320 type = lambda_capture_field_type (type,
14321 DECLTYPE_FOR_INIT_CAPTURE (t),
14322 DECLTYPE_FOR_REF_CAPTURE (t));
14323 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14324 type = lambda_proxy_type (type);
14325 else
14327 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14328 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14329 && EXPR_P (type))
14330 /* In a template ~id could be either a complement expression
14331 or an unqualified-id naming a destructor; if instantiating
14332 it produces an expression, it's not an id-expression or
14333 member access. */
14334 id = false;
14335 type = finish_decltype_type (type, id, complain);
14337 return cp_build_qualified_type_real (type,
14338 cp_type_quals (t)
14339 | cp_type_quals (type),
14340 complain | tf_ignore_bad_quals);
14343 case UNDERLYING_TYPE:
14345 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14346 complain, in_decl);
14347 return finish_underlying_type (type);
14350 case TYPE_ARGUMENT_PACK:
14351 case NONTYPE_ARGUMENT_PACK:
14353 tree r;
14355 if (code == NONTYPE_ARGUMENT_PACK)
14356 r = make_node (code);
14357 else
14358 r = cxx_make_type (code);
14360 tree pack_args = ARGUMENT_PACK_ARGS (t);
14361 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14362 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14364 return r;
14367 case VOID_CST:
14368 case INTEGER_CST:
14369 case REAL_CST:
14370 case STRING_CST:
14371 case PLUS_EXPR:
14372 case MINUS_EXPR:
14373 case NEGATE_EXPR:
14374 case NOP_EXPR:
14375 case INDIRECT_REF:
14376 case ADDR_EXPR:
14377 case CALL_EXPR:
14378 case ARRAY_REF:
14379 case SCOPE_REF:
14380 /* We should use one of the expression tsubsts for these codes. */
14381 gcc_unreachable ();
14383 default:
14384 sorry ("use of %qs in template", get_tree_code_name (code));
14385 return error_mark_node;
14389 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14390 expression on the left-hand side of the "." or "->" operator. A
14391 baselink indicates a function from a base class. Both the
14392 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14393 bases of the template class, rather than the instantiated class.
14394 In addition, lookups that were not ambiguous before may be
14395 ambiguous now. Therefore, we perform the lookup again. */
14397 static tree
14398 tsubst_baselink (tree baselink, tree object_type,
14399 tree args, tsubst_flags_t complain, tree in_decl)
14401 bool qualified = BASELINK_QUALIFIED_P (baselink);
14403 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14404 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14406 tree optype = BASELINK_OPTYPE (baselink);
14407 optype = tsubst (optype, args, complain, in_decl);
14409 tree template_args = NULL_TREE;
14410 bool template_id_p = false;
14411 tree fns = BASELINK_FUNCTIONS (baselink);
14412 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14414 template_id_p = true;
14415 template_args = TREE_OPERAND (fns, 1);
14416 fns = TREE_OPERAND (fns, 0);
14417 if (template_args)
14418 template_args = tsubst_template_args (template_args, args,
14419 complain, in_decl);
14422 tree name = OVL_NAME (fns);
14423 if (IDENTIFIER_CONV_OP_P (name))
14424 name = make_conv_op_name (optype);
14426 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14427 if (!baselink)
14429 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14430 error ("cannot call constructor %<%T::%D%> directly",
14431 qualifying_scope, name);
14432 return error_mark_node;
14435 /* If lookup found a single function, mark it as used at this point.
14436 (If it lookup found multiple functions the one selected later by
14437 overload resolution will be marked as used at that point.) */
14438 if (BASELINK_P (baselink))
14439 fns = BASELINK_FUNCTIONS (baselink);
14440 if (!template_id_p && !really_overloaded_fn (fns)
14441 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14442 return error_mark_node;
14444 if (BASELINK_P (baselink))
14446 /* Add back the template arguments, if present. */
14447 if (template_id_p)
14448 BASELINK_FUNCTIONS (baselink)
14449 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14450 BASELINK_FUNCTIONS (baselink), template_args);
14452 /* Update the conversion operator type. */
14453 BASELINK_OPTYPE (baselink) = optype;
14456 if (!object_type)
14457 object_type = current_class_type;
14459 if (qualified || name == complete_dtor_identifier)
14461 baselink = adjust_result_of_qualified_name_lookup (baselink,
14462 qualifying_scope,
14463 object_type);
14464 if (!qualified)
14465 /* We need to call adjust_result_of_qualified_name_lookup in case the
14466 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14467 so that we still get virtual function binding. */
14468 BASELINK_QUALIFIED_P (baselink) = false;
14471 return baselink;
14474 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14475 true if the qualified-id will be a postfix-expression in-and-of
14476 itself; false if more of the postfix-expression follows the
14477 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14478 of "&". */
14480 static tree
14481 tsubst_qualified_id (tree qualified_id, tree args,
14482 tsubst_flags_t complain, tree in_decl,
14483 bool done, bool address_p)
14485 tree expr;
14486 tree scope;
14487 tree name;
14488 bool is_template;
14489 tree template_args;
14490 location_t loc = UNKNOWN_LOCATION;
14492 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14494 /* Figure out what name to look up. */
14495 name = TREE_OPERAND (qualified_id, 1);
14496 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14498 is_template = true;
14499 loc = EXPR_LOCATION (name);
14500 template_args = TREE_OPERAND (name, 1);
14501 if (template_args)
14502 template_args = tsubst_template_args (template_args, args,
14503 complain, in_decl);
14504 if (template_args == error_mark_node)
14505 return error_mark_node;
14506 name = TREE_OPERAND (name, 0);
14508 else
14510 is_template = false;
14511 template_args = NULL_TREE;
14514 /* Substitute into the qualifying scope. When there are no ARGS, we
14515 are just trying to simplify a non-dependent expression. In that
14516 case the qualifying scope may be dependent, and, in any case,
14517 substituting will not help. */
14518 scope = TREE_OPERAND (qualified_id, 0);
14519 if (args)
14521 scope = tsubst (scope, args, complain, in_decl);
14522 expr = tsubst_copy (name, args, complain, in_decl);
14524 else
14525 expr = name;
14527 if (dependent_scope_p (scope))
14529 if (is_template)
14530 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14531 tree r = build_qualified_name (NULL_TREE, scope, expr,
14532 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14533 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14534 return r;
14537 if (!BASELINK_P (name) && !DECL_P (expr))
14539 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14541 /* A BIT_NOT_EXPR is used to represent a destructor. */
14542 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14544 error ("qualifying type %qT does not match destructor name ~%qT",
14545 scope, TREE_OPERAND (expr, 0));
14546 expr = error_mark_node;
14548 else
14549 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14550 /*is_type_p=*/0, false);
14552 else
14553 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14554 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14555 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14557 if (complain & tf_error)
14559 error ("dependent-name %qE is parsed as a non-type, but "
14560 "instantiation yields a type", qualified_id);
14561 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14563 return error_mark_node;
14567 if (DECL_P (expr))
14569 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14570 scope);
14571 /* Remember that there was a reference to this entity. */
14572 if (!mark_used (expr, complain) && !(complain & tf_error))
14573 return error_mark_node;
14576 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14578 if (complain & tf_error)
14579 qualified_name_lookup_error (scope,
14580 TREE_OPERAND (qualified_id, 1),
14581 expr, input_location);
14582 return error_mark_node;
14585 if (is_template)
14587 if (variable_template_p (expr))
14588 expr = lookup_and_finish_template_variable (expr, template_args,
14589 complain);
14590 else
14591 expr = lookup_template_function (expr, template_args);
14594 if (expr == error_mark_node && complain & tf_error)
14595 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14596 expr, input_location);
14597 else if (TYPE_P (scope))
14599 expr = (adjust_result_of_qualified_name_lookup
14600 (expr, scope, current_nonlambda_class_type ()));
14601 expr = (finish_qualified_id_expr
14602 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14603 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14604 /*template_arg_p=*/false, complain));
14607 /* Expressions do not generally have reference type. */
14608 if (TREE_CODE (expr) != SCOPE_REF
14609 /* However, if we're about to form a pointer-to-member, we just
14610 want the referenced member referenced. */
14611 && TREE_CODE (expr) != OFFSET_REF)
14612 expr = convert_from_reference (expr);
14614 if (REF_PARENTHESIZED_P (qualified_id))
14615 expr = force_paren_expr (expr);
14617 return expr;
14620 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14621 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14622 for tsubst. */
14624 static tree
14625 tsubst_init (tree init, tree decl, tree args,
14626 tsubst_flags_t complain, tree in_decl)
14628 if (!init)
14629 return NULL_TREE;
14631 init = tsubst_expr (init, args, complain, in_decl, false);
14633 if (!init && TREE_TYPE (decl) != error_mark_node)
14635 /* If we had an initializer but it
14636 instantiated to nothing,
14637 value-initialize the object. This will
14638 only occur when the initializer was a
14639 pack expansion where the parameter packs
14640 used in that expansion were of length
14641 zero. */
14642 init = build_value_init (TREE_TYPE (decl),
14643 complain);
14644 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14645 init = get_target_expr_sfinae (init, complain);
14646 if (TREE_CODE (init) == TARGET_EXPR)
14647 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14650 return init;
14653 /* Like tsubst, but deals with expressions. This function just replaces
14654 template parms; to finish processing the resultant expression, use
14655 tsubst_copy_and_build or tsubst_expr. */
14657 static tree
14658 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14660 enum tree_code code;
14661 tree r;
14663 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14664 return t;
14666 code = TREE_CODE (t);
14668 switch (code)
14670 case PARM_DECL:
14671 r = retrieve_local_specialization (t);
14673 if (r == NULL_TREE)
14675 /* We get here for a use of 'this' in an NSDMI. */
14676 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14677 return current_class_ptr;
14679 /* This can happen for a parameter name used later in a function
14680 declaration (such as in a late-specified return type). Just
14681 make a dummy decl, since it's only used for its type. */
14682 gcc_assert (cp_unevaluated_operand != 0);
14683 r = tsubst_decl (t, args, complain);
14684 /* Give it the template pattern as its context; its true context
14685 hasn't been instantiated yet and this is good enough for
14686 mangling. */
14687 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14690 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14691 r = ARGUMENT_PACK_SELECT_ARG (r);
14692 if (!mark_used (r, complain) && !(complain & tf_error))
14693 return error_mark_node;
14694 return r;
14696 case CONST_DECL:
14698 tree enum_type;
14699 tree v;
14701 if (DECL_TEMPLATE_PARM_P (t))
14702 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14703 /* There is no need to substitute into namespace-scope
14704 enumerators. */
14705 if (DECL_NAMESPACE_SCOPE_P (t))
14706 return t;
14707 /* If ARGS is NULL, then T is known to be non-dependent. */
14708 if (args == NULL_TREE)
14709 return scalar_constant_value (t);
14711 /* Unfortunately, we cannot just call lookup_name here.
14712 Consider:
14714 template <int I> int f() {
14715 enum E { a = I };
14716 struct S { void g() { E e = a; } };
14719 When we instantiate f<7>::S::g(), say, lookup_name is not
14720 clever enough to find f<7>::a. */
14721 enum_type
14722 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14723 /*entering_scope=*/0);
14725 for (v = TYPE_VALUES (enum_type);
14726 v != NULL_TREE;
14727 v = TREE_CHAIN (v))
14728 if (TREE_PURPOSE (v) == DECL_NAME (t))
14729 return TREE_VALUE (v);
14731 /* We didn't find the name. That should never happen; if
14732 name-lookup found it during preliminary parsing, we
14733 should find it again here during instantiation. */
14734 gcc_unreachable ();
14736 return t;
14738 case FIELD_DECL:
14739 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14741 /* Check for a local specialization set up by
14742 tsubst_pack_expansion. */
14743 if (tree r = retrieve_local_specialization (t))
14745 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14746 r = ARGUMENT_PACK_SELECT_ARG (r);
14747 return r;
14750 /* When retrieving a capture pack from a generic lambda, remove the
14751 lambda call op's own template argument list from ARGS. Only the
14752 template arguments active for the closure type should be used to
14753 retrieve the pack specialization. */
14754 if (LAMBDA_FUNCTION_P (current_function_decl)
14755 && (template_class_depth (DECL_CONTEXT (t))
14756 != TMPL_ARGS_DEPTH (args)))
14757 args = strip_innermost_template_args (args, 1);
14759 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14760 tsubst_decl put in the hash table. */
14761 return retrieve_specialization (t, args, 0);
14764 if (DECL_CONTEXT (t))
14766 tree ctx;
14768 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14769 /*entering_scope=*/1);
14770 if (ctx != DECL_CONTEXT (t))
14772 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14773 if (!r)
14775 if (complain & tf_error)
14776 error ("using invalid field %qD", t);
14777 return error_mark_node;
14779 return r;
14783 return t;
14785 case VAR_DECL:
14786 case FUNCTION_DECL:
14787 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14788 r = tsubst (t, args, complain, in_decl);
14789 else if (local_variable_p (t)
14790 && uses_template_parms (DECL_CONTEXT (t)))
14792 r = retrieve_local_specialization (t);
14793 if (r == NULL_TREE)
14795 /* First try name lookup to find the instantiation. */
14796 r = lookup_name (DECL_NAME (t));
14797 if (r && !is_capture_proxy (r))
14799 /* Make sure that the one we found is the one we want. */
14800 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14801 if (ctx != DECL_CONTEXT (r))
14802 r = NULL_TREE;
14805 if (r)
14806 /* OK */;
14807 else
14809 /* This can happen for a variable used in a
14810 late-specified return type of a local lambda, or for a
14811 local static or constant. Building a new VAR_DECL
14812 should be OK in all those cases. */
14813 r = tsubst_decl (t, args, complain);
14814 if (local_specializations)
14815 /* Avoid infinite recursion (79640). */
14816 register_local_specialization (r, t);
14817 if (decl_maybe_constant_var_p (r))
14819 /* We can't call cp_finish_decl, so handle the
14820 initializer by hand. */
14821 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14822 complain, in_decl);
14823 if (!processing_template_decl)
14824 init = maybe_constant_init (init);
14825 if (processing_template_decl
14826 ? potential_constant_expression (init)
14827 : reduced_constant_expression_p (init))
14828 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14829 = TREE_CONSTANT (r) = true;
14830 DECL_INITIAL (r) = init;
14831 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14832 TREE_TYPE (r)
14833 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14834 complain, adc_variable_type);
14836 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14837 || decl_constant_var_p (r)
14838 || errorcount || sorrycount);
14839 if (!processing_template_decl
14840 && !TREE_STATIC (r))
14841 r = process_outer_var_ref (r, complain);
14843 /* Remember this for subsequent uses. */
14844 if (local_specializations)
14845 register_local_specialization (r, t);
14848 else
14849 r = t;
14850 if (!mark_used (r, complain))
14851 return error_mark_node;
14852 return r;
14854 case NAMESPACE_DECL:
14855 return t;
14857 case OVERLOAD:
14858 /* An OVERLOAD will always be a non-dependent overload set; an
14859 overload set from function scope will just be represented with an
14860 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14861 gcc_assert (!uses_template_parms (t));
14862 /* We must have marked any lookups as persistent. */
14863 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14864 return t;
14866 case BASELINK:
14867 return tsubst_baselink (t, current_nonlambda_class_type (),
14868 args, complain, in_decl);
14870 case TEMPLATE_DECL:
14871 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14872 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14873 args, complain, in_decl);
14874 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14875 return tsubst (t, args, complain, in_decl);
14876 else if (DECL_CLASS_SCOPE_P (t)
14877 && uses_template_parms (DECL_CONTEXT (t)))
14879 /* Template template argument like the following example need
14880 special treatment:
14882 template <template <class> class TT> struct C {};
14883 template <class T> struct D {
14884 template <class U> struct E {};
14885 C<E> c; // #1
14887 D<int> d; // #2
14889 We are processing the template argument `E' in #1 for
14890 the template instantiation #2. Originally, `E' is a
14891 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14892 have to substitute this with one having context `D<int>'. */
14894 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14895 if (dependent_scope_p (context))
14897 /* When rewriting a constructor into a deduction guide, a
14898 non-dependent name can become dependent, so memtmpl<args>
14899 becomes context::template memtmpl<args>. */
14900 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14901 return build_qualified_name (type, context, DECL_NAME (t),
14902 /*template*/true);
14904 return lookup_field (context, DECL_NAME(t), 0, false);
14906 else
14907 /* Ordinary template template argument. */
14908 return t;
14910 case CAST_EXPR:
14911 case REINTERPRET_CAST_EXPR:
14912 case CONST_CAST_EXPR:
14913 case STATIC_CAST_EXPR:
14914 case DYNAMIC_CAST_EXPR:
14915 case IMPLICIT_CONV_EXPR:
14916 case CONVERT_EXPR:
14917 case NOP_EXPR:
14919 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14920 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14921 return build1 (code, type, op0);
14924 case SIZEOF_EXPR:
14925 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14926 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14928 tree expanded, op = TREE_OPERAND (t, 0);
14929 int len = 0;
14931 if (SIZEOF_EXPR_TYPE_P (t))
14932 op = TREE_TYPE (op);
14934 ++cp_unevaluated_operand;
14935 ++c_inhibit_evaluation_warnings;
14936 /* We only want to compute the number of arguments. */
14937 if (PACK_EXPANSION_P (op))
14938 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14939 else
14940 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14941 args, complain, in_decl);
14942 --cp_unevaluated_operand;
14943 --c_inhibit_evaluation_warnings;
14945 if (TREE_CODE (expanded) == TREE_VEC)
14947 len = TREE_VEC_LENGTH (expanded);
14948 /* Set TREE_USED for the benefit of -Wunused. */
14949 for (int i = 0; i < len; i++)
14950 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14951 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14954 if (expanded == error_mark_node)
14955 return error_mark_node;
14956 else if (PACK_EXPANSION_P (expanded)
14957 || (TREE_CODE (expanded) == TREE_VEC
14958 && pack_expansion_args_count (expanded)))
14961 if (PACK_EXPANSION_P (expanded))
14962 /* OK. */;
14963 else if (TREE_VEC_LENGTH (expanded) == 1)
14964 expanded = TREE_VEC_ELT (expanded, 0);
14965 else
14966 expanded = make_argument_pack (expanded);
14968 if (TYPE_P (expanded))
14969 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14970 complain & tf_error);
14971 else
14972 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14973 complain & tf_error);
14975 else
14976 return build_int_cst (size_type_node, len);
14978 if (SIZEOF_EXPR_TYPE_P (t))
14980 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14981 args, complain, in_decl);
14982 r = build1 (NOP_EXPR, r, error_mark_node);
14983 r = build1 (SIZEOF_EXPR,
14984 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14985 SIZEOF_EXPR_TYPE_P (r) = 1;
14986 return r;
14988 /* Fall through */
14990 case INDIRECT_REF:
14991 case NEGATE_EXPR:
14992 case TRUTH_NOT_EXPR:
14993 case BIT_NOT_EXPR:
14994 case ADDR_EXPR:
14995 case UNARY_PLUS_EXPR: /* Unary + */
14996 case ALIGNOF_EXPR:
14997 case AT_ENCODE_EXPR:
14998 case ARROW_EXPR:
14999 case THROW_EXPR:
15000 case TYPEID_EXPR:
15001 case REALPART_EXPR:
15002 case IMAGPART_EXPR:
15003 case PAREN_EXPR:
15005 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15006 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15007 return build1 (code, type, op0);
15010 case COMPONENT_REF:
15012 tree object;
15013 tree name;
15015 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15016 name = TREE_OPERAND (t, 1);
15017 if (TREE_CODE (name) == BIT_NOT_EXPR)
15019 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15020 complain, in_decl);
15021 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15023 else if (TREE_CODE (name) == SCOPE_REF
15024 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15026 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15027 complain, in_decl);
15028 name = TREE_OPERAND (name, 1);
15029 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15030 complain, in_decl);
15031 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15032 name = build_qualified_name (/*type=*/NULL_TREE,
15033 base, name,
15034 /*template_p=*/false);
15036 else if (BASELINK_P (name))
15037 name = tsubst_baselink (name,
15038 non_reference (TREE_TYPE (object)),
15039 args, complain,
15040 in_decl);
15041 else
15042 name = tsubst_copy (name, args, complain, in_decl);
15043 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15046 case PLUS_EXPR:
15047 case MINUS_EXPR:
15048 case MULT_EXPR:
15049 case TRUNC_DIV_EXPR:
15050 case CEIL_DIV_EXPR:
15051 case FLOOR_DIV_EXPR:
15052 case ROUND_DIV_EXPR:
15053 case EXACT_DIV_EXPR:
15054 case BIT_AND_EXPR:
15055 case BIT_IOR_EXPR:
15056 case BIT_XOR_EXPR:
15057 case TRUNC_MOD_EXPR:
15058 case FLOOR_MOD_EXPR:
15059 case TRUTH_ANDIF_EXPR:
15060 case TRUTH_ORIF_EXPR:
15061 case TRUTH_AND_EXPR:
15062 case TRUTH_OR_EXPR:
15063 case RSHIFT_EXPR:
15064 case LSHIFT_EXPR:
15065 case RROTATE_EXPR:
15066 case LROTATE_EXPR:
15067 case EQ_EXPR:
15068 case NE_EXPR:
15069 case MAX_EXPR:
15070 case MIN_EXPR:
15071 case LE_EXPR:
15072 case GE_EXPR:
15073 case LT_EXPR:
15074 case GT_EXPR:
15075 case COMPOUND_EXPR:
15076 case DOTSTAR_EXPR:
15077 case MEMBER_REF:
15078 case PREDECREMENT_EXPR:
15079 case PREINCREMENT_EXPR:
15080 case POSTDECREMENT_EXPR:
15081 case POSTINCREMENT_EXPR:
15083 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15084 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15085 return build_nt (code, op0, op1);
15088 case SCOPE_REF:
15090 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15091 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15092 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15093 QUALIFIED_NAME_IS_TEMPLATE (t));
15096 case ARRAY_REF:
15098 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15099 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15100 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15103 case CALL_EXPR:
15105 int n = VL_EXP_OPERAND_LENGTH (t);
15106 tree result = build_vl_exp (CALL_EXPR, n);
15107 int i;
15108 for (i = 0; i < n; i++)
15109 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15110 complain, in_decl);
15111 return result;
15114 case COND_EXPR:
15115 case MODOP_EXPR:
15116 case PSEUDO_DTOR_EXPR:
15117 case VEC_PERM_EXPR:
15119 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15120 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15121 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15122 r = build_nt (code, op0, op1, op2);
15123 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15124 return r;
15127 case NEW_EXPR:
15129 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15130 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15131 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15132 r = build_nt (code, op0, op1, op2);
15133 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15134 return r;
15137 case DELETE_EXPR:
15139 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15140 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15141 r = build_nt (code, op0, op1);
15142 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15143 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15144 return r;
15147 case TEMPLATE_ID_EXPR:
15149 /* Substituted template arguments */
15150 tree fn = TREE_OPERAND (t, 0);
15151 tree targs = TREE_OPERAND (t, 1);
15153 fn = tsubst_copy (fn, args, complain, in_decl);
15154 if (targs)
15155 targs = tsubst_template_args (targs, args, complain, in_decl);
15157 return lookup_template_function (fn, targs);
15160 case TREE_LIST:
15162 tree purpose, value, chain;
15164 if (t == void_list_node)
15165 return t;
15167 purpose = TREE_PURPOSE (t);
15168 if (purpose)
15169 purpose = tsubst_copy (purpose, args, complain, in_decl);
15170 value = TREE_VALUE (t);
15171 if (value)
15172 value = tsubst_copy (value, args, complain, in_decl);
15173 chain = TREE_CHAIN (t);
15174 if (chain && chain != void_type_node)
15175 chain = tsubst_copy (chain, args, complain, in_decl);
15176 if (purpose == TREE_PURPOSE (t)
15177 && value == TREE_VALUE (t)
15178 && chain == TREE_CHAIN (t))
15179 return t;
15180 return tree_cons (purpose, value, chain);
15183 case RECORD_TYPE:
15184 case UNION_TYPE:
15185 case ENUMERAL_TYPE:
15186 case INTEGER_TYPE:
15187 case TEMPLATE_TYPE_PARM:
15188 case TEMPLATE_TEMPLATE_PARM:
15189 case BOUND_TEMPLATE_TEMPLATE_PARM:
15190 case TEMPLATE_PARM_INDEX:
15191 case POINTER_TYPE:
15192 case REFERENCE_TYPE:
15193 case OFFSET_TYPE:
15194 case FUNCTION_TYPE:
15195 case METHOD_TYPE:
15196 case ARRAY_TYPE:
15197 case TYPENAME_TYPE:
15198 case UNBOUND_CLASS_TEMPLATE:
15199 case TYPEOF_TYPE:
15200 case DECLTYPE_TYPE:
15201 case TYPE_DECL:
15202 return tsubst (t, args, complain, in_decl);
15204 case USING_DECL:
15205 t = DECL_NAME (t);
15206 /* Fall through. */
15207 case IDENTIFIER_NODE:
15208 if (IDENTIFIER_CONV_OP_P (t))
15210 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15211 return make_conv_op_name (new_type);
15213 else
15214 return t;
15216 case CONSTRUCTOR:
15217 /* This is handled by tsubst_copy_and_build. */
15218 gcc_unreachable ();
15220 case VA_ARG_EXPR:
15222 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15223 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15224 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15227 case CLEANUP_POINT_EXPR:
15228 /* We shouldn't have built any of these during initial template
15229 generation. Instead, they should be built during instantiation
15230 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15231 gcc_unreachable ();
15233 case OFFSET_REF:
15235 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15236 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15237 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15238 r = build2 (code, type, op0, op1);
15239 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15240 if (!mark_used (TREE_OPERAND (r, 1), complain)
15241 && !(complain & tf_error))
15242 return error_mark_node;
15243 return r;
15246 case EXPR_PACK_EXPANSION:
15247 error ("invalid use of pack expansion expression");
15248 return error_mark_node;
15250 case NONTYPE_ARGUMENT_PACK:
15251 error ("use %<...%> to expand argument pack");
15252 return error_mark_node;
15254 case VOID_CST:
15255 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15256 return t;
15258 case INTEGER_CST:
15259 case REAL_CST:
15260 case STRING_CST:
15261 case COMPLEX_CST:
15263 /* Instantiate any typedefs in the type. */
15264 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15265 r = fold_convert (type, t);
15266 gcc_assert (TREE_CODE (r) == code);
15267 return r;
15270 case PTRMEM_CST:
15271 /* These can sometimes show up in a partial instantiation, but never
15272 involve template parms. */
15273 gcc_assert (!uses_template_parms (t));
15274 return t;
15276 case UNARY_LEFT_FOLD_EXPR:
15277 return tsubst_unary_left_fold (t, args, complain, in_decl);
15278 case UNARY_RIGHT_FOLD_EXPR:
15279 return tsubst_unary_right_fold (t, args, complain, in_decl);
15280 case BINARY_LEFT_FOLD_EXPR:
15281 return tsubst_binary_left_fold (t, args, complain, in_decl);
15282 case BINARY_RIGHT_FOLD_EXPR:
15283 return tsubst_binary_right_fold (t, args, complain, in_decl);
15284 case PREDICT_EXPR:
15285 return t;
15287 default:
15288 /* We shouldn't get here, but keep going if !flag_checking. */
15289 if (flag_checking)
15290 gcc_unreachable ();
15291 return t;
15295 /* Helper function for tsubst_omp_clauses, used for instantiation of
15296 OMP_CLAUSE_DECL of clauses. */
15298 static tree
15299 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15300 tree in_decl)
15302 if (decl == NULL_TREE)
15303 return NULL_TREE;
15305 /* Handle an OpenMP array section represented as a TREE_LIST (or
15306 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15307 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15308 TREE_LIST. We can handle it exactly the same as an array section
15309 (purpose, value, and a chain), even though the nomenclature
15310 (low_bound, length, etc) is different. */
15311 if (TREE_CODE (decl) == TREE_LIST)
15313 tree low_bound
15314 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15315 /*integral_constant_expression_p=*/false);
15316 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15317 /*integral_constant_expression_p=*/false);
15318 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15319 in_decl);
15320 if (TREE_PURPOSE (decl) == low_bound
15321 && TREE_VALUE (decl) == length
15322 && TREE_CHAIN (decl) == chain)
15323 return decl;
15324 tree ret = tree_cons (low_bound, length, chain);
15325 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15326 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15327 return ret;
15329 tree ret = tsubst_expr (decl, args, complain, in_decl,
15330 /*integral_constant_expression_p=*/false);
15331 /* Undo convert_from_reference tsubst_expr could have called. */
15332 if (decl
15333 && REFERENCE_REF_P (ret)
15334 && !REFERENCE_REF_P (decl))
15335 ret = TREE_OPERAND (ret, 0);
15336 return ret;
15339 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15341 static tree
15342 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15343 tree args, tsubst_flags_t complain, tree in_decl)
15345 tree new_clauses = NULL_TREE, nc, oc;
15346 tree linear_no_step = NULL_TREE;
15348 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15350 nc = copy_node (oc);
15351 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15352 new_clauses = nc;
15354 switch (OMP_CLAUSE_CODE (nc))
15356 case OMP_CLAUSE_LASTPRIVATE:
15357 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15359 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15360 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15361 in_decl, /*integral_constant_expression_p=*/false);
15362 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15363 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15365 /* FALLTHRU */
15366 case OMP_CLAUSE_PRIVATE:
15367 case OMP_CLAUSE_SHARED:
15368 case OMP_CLAUSE_FIRSTPRIVATE:
15369 case OMP_CLAUSE_COPYIN:
15370 case OMP_CLAUSE_COPYPRIVATE:
15371 case OMP_CLAUSE_UNIFORM:
15372 case OMP_CLAUSE_DEPEND:
15373 case OMP_CLAUSE_FROM:
15374 case OMP_CLAUSE_TO:
15375 case OMP_CLAUSE_MAP:
15376 case OMP_CLAUSE_USE_DEVICE_PTR:
15377 case OMP_CLAUSE_IS_DEVICE_PTR:
15378 OMP_CLAUSE_DECL (nc)
15379 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15380 in_decl);
15381 break;
15382 case OMP_CLAUSE_TILE:
15383 case OMP_CLAUSE_IF:
15384 case OMP_CLAUSE_NUM_THREADS:
15385 case OMP_CLAUSE_SCHEDULE:
15386 case OMP_CLAUSE_COLLAPSE:
15387 case OMP_CLAUSE_FINAL:
15388 case OMP_CLAUSE_DEVICE:
15389 case OMP_CLAUSE_DIST_SCHEDULE:
15390 case OMP_CLAUSE_NUM_TEAMS:
15391 case OMP_CLAUSE_THREAD_LIMIT:
15392 case OMP_CLAUSE_SAFELEN:
15393 case OMP_CLAUSE_SIMDLEN:
15394 case OMP_CLAUSE_NUM_TASKS:
15395 case OMP_CLAUSE_GRAINSIZE:
15396 case OMP_CLAUSE_PRIORITY:
15397 case OMP_CLAUSE_ORDERED:
15398 case OMP_CLAUSE_HINT:
15399 case OMP_CLAUSE_NUM_GANGS:
15400 case OMP_CLAUSE_NUM_WORKERS:
15401 case OMP_CLAUSE_VECTOR_LENGTH:
15402 case OMP_CLAUSE_WORKER:
15403 case OMP_CLAUSE_VECTOR:
15404 case OMP_CLAUSE_ASYNC:
15405 case OMP_CLAUSE_WAIT:
15406 OMP_CLAUSE_OPERAND (nc, 0)
15407 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15408 in_decl, /*integral_constant_expression_p=*/false);
15409 break;
15410 case OMP_CLAUSE_REDUCTION:
15411 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15413 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15414 if (TREE_CODE (placeholder) == SCOPE_REF)
15416 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15417 complain, in_decl);
15418 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15419 = build_qualified_name (NULL_TREE, scope,
15420 TREE_OPERAND (placeholder, 1),
15421 false);
15423 else
15424 gcc_assert (identifier_p (placeholder));
15426 OMP_CLAUSE_DECL (nc)
15427 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15428 in_decl);
15429 break;
15430 case OMP_CLAUSE_GANG:
15431 case OMP_CLAUSE_ALIGNED:
15432 OMP_CLAUSE_DECL (nc)
15433 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15434 in_decl);
15435 OMP_CLAUSE_OPERAND (nc, 1)
15436 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15437 in_decl, /*integral_constant_expression_p=*/false);
15438 break;
15439 case OMP_CLAUSE_LINEAR:
15440 OMP_CLAUSE_DECL (nc)
15441 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15442 in_decl);
15443 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15445 gcc_assert (!linear_no_step);
15446 linear_no_step = nc;
15448 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15449 OMP_CLAUSE_LINEAR_STEP (nc)
15450 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15451 complain, in_decl);
15452 else
15453 OMP_CLAUSE_LINEAR_STEP (nc)
15454 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15455 in_decl,
15456 /*integral_constant_expression_p=*/false);
15457 break;
15458 case OMP_CLAUSE_NOWAIT:
15459 case OMP_CLAUSE_DEFAULT:
15460 case OMP_CLAUSE_UNTIED:
15461 case OMP_CLAUSE_MERGEABLE:
15462 case OMP_CLAUSE_INBRANCH:
15463 case OMP_CLAUSE_NOTINBRANCH:
15464 case OMP_CLAUSE_PROC_BIND:
15465 case OMP_CLAUSE_FOR:
15466 case OMP_CLAUSE_PARALLEL:
15467 case OMP_CLAUSE_SECTIONS:
15468 case OMP_CLAUSE_TASKGROUP:
15469 case OMP_CLAUSE_NOGROUP:
15470 case OMP_CLAUSE_THREADS:
15471 case OMP_CLAUSE_SIMD:
15472 case OMP_CLAUSE_DEFAULTMAP:
15473 case OMP_CLAUSE_INDEPENDENT:
15474 case OMP_CLAUSE_AUTO:
15475 case OMP_CLAUSE_SEQ:
15476 break;
15477 default:
15478 gcc_unreachable ();
15480 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15481 switch (OMP_CLAUSE_CODE (nc))
15483 case OMP_CLAUSE_SHARED:
15484 case OMP_CLAUSE_PRIVATE:
15485 case OMP_CLAUSE_FIRSTPRIVATE:
15486 case OMP_CLAUSE_LASTPRIVATE:
15487 case OMP_CLAUSE_COPYPRIVATE:
15488 case OMP_CLAUSE_LINEAR:
15489 case OMP_CLAUSE_REDUCTION:
15490 case OMP_CLAUSE_USE_DEVICE_PTR:
15491 case OMP_CLAUSE_IS_DEVICE_PTR:
15492 /* tsubst_expr on SCOPE_REF results in returning
15493 finish_non_static_data_member result. Undo that here. */
15494 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15495 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15496 == IDENTIFIER_NODE))
15498 tree t = OMP_CLAUSE_DECL (nc);
15499 tree v = t;
15500 while (v)
15501 switch (TREE_CODE (v))
15503 case COMPONENT_REF:
15504 case MEM_REF:
15505 case INDIRECT_REF:
15506 CASE_CONVERT:
15507 case POINTER_PLUS_EXPR:
15508 v = TREE_OPERAND (v, 0);
15509 continue;
15510 case PARM_DECL:
15511 if (DECL_CONTEXT (v) == current_function_decl
15512 && DECL_ARTIFICIAL (v)
15513 && DECL_NAME (v) == this_identifier)
15514 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15515 /* FALLTHRU */
15516 default:
15517 v = NULL_TREE;
15518 break;
15521 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15522 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15523 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15524 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15525 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15527 tree decl = OMP_CLAUSE_DECL (nc);
15528 if (VAR_P (decl))
15530 retrofit_lang_decl (decl);
15531 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15534 break;
15535 default:
15536 break;
15540 new_clauses = nreverse (new_clauses);
15541 if (ort != C_ORT_OMP_DECLARE_SIMD)
15543 new_clauses = finish_omp_clauses (new_clauses, ort);
15544 if (linear_no_step)
15545 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15546 if (nc == linear_no_step)
15548 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15549 break;
15552 return new_clauses;
15555 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15557 static tree
15558 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15559 tree in_decl)
15561 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15563 tree purpose, value, chain;
15565 if (t == NULL)
15566 return t;
15568 if (TREE_CODE (t) != TREE_LIST)
15569 return tsubst_copy_and_build (t, args, complain, in_decl,
15570 /*function_p=*/false,
15571 /*integral_constant_expression_p=*/false);
15573 if (t == void_list_node)
15574 return t;
15576 purpose = TREE_PURPOSE (t);
15577 if (purpose)
15578 purpose = RECUR (purpose);
15579 value = TREE_VALUE (t);
15580 if (value)
15582 if (TREE_CODE (value) != LABEL_DECL)
15583 value = RECUR (value);
15584 else
15586 value = lookup_label (DECL_NAME (value));
15587 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15588 TREE_USED (value) = 1;
15591 chain = TREE_CHAIN (t);
15592 if (chain && chain != void_type_node)
15593 chain = RECUR (chain);
15594 return tree_cons (purpose, value, chain);
15595 #undef RECUR
15598 /* Used to temporarily communicate the list of #pragma omp parallel
15599 clauses to #pragma omp for instantiation if they are combined
15600 together. */
15602 static tree *omp_parallel_combined_clauses;
15604 /* Substitute one OMP_FOR iterator. */
15606 static void
15607 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15608 tree initv, tree condv, tree incrv, tree *clauses,
15609 tree args, tsubst_flags_t complain, tree in_decl,
15610 bool integral_constant_expression_p)
15612 #define RECUR(NODE) \
15613 tsubst_expr ((NODE), args, complain, in_decl, \
15614 integral_constant_expression_p)
15615 tree decl, init, cond, incr;
15617 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15618 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15620 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15622 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15623 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15626 decl = TREE_OPERAND (init, 0);
15627 init = TREE_OPERAND (init, 1);
15628 tree decl_expr = NULL_TREE;
15629 if (init && TREE_CODE (init) == DECL_EXPR)
15631 /* We need to jump through some hoops to handle declarations in the
15632 init-statement, since we might need to handle auto deduction,
15633 but we need to keep control of initialization. */
15634 decl_expr = init;
15635 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15636 decl = tsubst_decl (decl, args, complain);
15638 else
15640 if (TREE_CODE (decl) == SCOPE_REF)
15642 decl = RECUR (decl);
15643 if (TREE_CODE (decl) == COMPONENT_REF)
15645 tree v = decl;
15646 while (v)
15647 switch (TREE_CODE (v))
15649 case COMPONENT_REF:
15650 case MEM_REF:
15651 case INDIRECT_REF:
15652 CASE_CONVERT:
15653 case POINTER_PLUS_EXPR:
15654 v = TREE_OPERAND (v, 0);
15655 continue;
15656 case PARM_DECL:
15657 if (DECL_CONTEXT (v) == current_function_decl
15658 && DECL_ARTIFICIAL (v)
15659 && DECL_NAME (v) == this_identifier)
15661 decl = TREE_OPERAND (decl, 1);
15662 decl = omp_privatize_field (decl, false);
15664 /* FALLTHRU */
15665 default:
15666 v = NULL_TREE;
15667 break;
15671 else
15672 decl = RECUR (decl);
15674 init = RECUR (init);
15676 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15677 if (auto_node && init)
15678 TREE_TYPE (decl)
15679 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15681 gcc_assert (!type_dependent_expression_p (decl));
15683 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15685 if (decl_expr)
15687 /* Declare the variable, but don't let that initialize it. */
15688 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15689 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15690 RECUR (decl_expr);
15691 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15694 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15695 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15696 if (TREE_CODE (incr) == MODIFY_EXPR)
15698 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15699 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15700 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15701 NOP_EXPR, rhs, complain);
15703 else
15704 incr = RECUR (incr);
15705 TREE_VEC_ELT (declv, i) = decl;
15706 TREE_VEC_ELT (initv, i) = init;
15707 TREE_VEC_ELT (condv, i) = cond;
15708 TREE_VEC_ELT (incrv, i) = incr;
15709 return;
15712 if (decl_expr)
15714 /* Declare and initialize the variable. */
15715 RECUR (decl_expr);
15716 init = NULL_TREE;
15718 else if (init)
15720 tree *pc;
15721 int j;
15722 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15724 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15726 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15727 && OMP_CLAUSE_DECL (*pc) == decl)
15728 break;
15729 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15730 && OMP_CLAUSE_DECL (*pc) == decl)
15732 if (j)
15733 break;
15734 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15735 tree c = *pc;
15736 *pc = OMP_CLAUSE_CHAIN (c);
15737 OMP_CLAUSE_CHAIN (c) = *clauses;
15738 *clauses = c;
15740 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15741 && OMP_CLAUSE_DECL (*pc) == decl)
15743 error ("iteration variable %qD should not be firstprivate",
15744 decl);
15745 *pc = OMP_CLAUSE_CHAIN (*pc);
15747 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15748 && OMP_CLAUSE_DECL (*pc) == decl)
15750 error ("iteration variable %qD should not be reduction",
15751 decl);
15752 *pc = OMP_CLAUSE_CHAIN (*pc);
15754 else
15755 pc = &OMP_CLAUSE_CHAIN (*pc);
15757 if (*pc)
15758 break;
15760 if (*pc == NULL_TREE)
15762 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15763 OMP_CLAUSE_DECL (c) = decl;
15764 c = finish_omp_clauses (c, C_ORT_OMP);
15765 if (c)
15767 OMP_CLAUSE_CHAIN (c) = *clauses;
15768 *clauses = c;
15772 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15773 if (COMPARISON_CLASS_P (cond))
15775 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15776 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15777 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15779 else
15780 cond = RECUR (cond);
15781 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15782 switch (TREE_CODE (incr))
15784 case PREINCREMENT_EXPR:
15785 case PREDECREMENT_EXPR:
15786 case POSTINCREMENT_EXPR:
15787 case POSTDECREMENT_EXPR:
15788 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15789 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15790 break;
15791 case MODIFY_EXPR:
15792 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15793 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15795 tree rhs = TREE_OPERAND (incr, 1);
15796 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15797 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15798 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15799 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15800 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15801 rhs0, rhs1));
15803 else
15804 incr = RECUR (incr);
15805 break;
15806 case MODOP_EXPR:
15807 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15808 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15810 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15811 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15812 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15813 TREE_TYPE (decl), lhs,
15814 RECUR (TREE_OPERAND (incr, 2))));
15816 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15817 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15818 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15820 tree rhs = TREE_OPERAND (incr, 2);
15821 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15822 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15823 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15824 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15825 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15826 rhs0, rhs1));
15828 else
15829 incr = RECUR (incr);
15830 break;
15831 default:
15832 incr = RECUR (incr);
15833 break;
15836 TREE_VEC_ELT (declv, i) = decl;
15837 TREE_VEC_ELT (initv, i) = init;
15838 TREE_VEC_ELT (condv, i) = cond;
15839 TREE_VEC_ELT (incrv, i) = incr;
15840 #undef RECUR
15843 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15844 of OMP_TARGET's body. */
15846 static tree
15847 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15849 *walk_subtrees = 0;
15850 switch (TREE_CODE (*tp))
15852 case OMP_TEAMS:
15853 return *tp;
15854 case BIND_EXPR:
15855 case STATEMENT_LIST:
15856 *walk_subtrees = 1;
15857 break;
15858 default:
15859 break;
15861 return NULL_TREE;
15864 /* Helper function for tsubst_expr. For decomposition declaration
15865 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15866 also the corresponding decls representing the identifiers
15867 of the decomposition declaration. Return DECL if successful
15868 or error_mark_node otherwise, set *FIRST to the first decl
15869 in the list chained through DECL_CHAIN and *CNT to the number
15870 of such decls. */
15872 static tree
15873 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15874 tsubst_flags_t complain, tree in_decl, tree *first,
15875 unsigned int *cnt)
15877 tree decl2, decl3, prev = decl;
15878 *cnt = 0;
15879 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15880 for (decl2 = DECL_CHAIN (pattern_decl);
15881 decl2
15882 && VAR_P (decl2)
15883 && DECL_DECOMPOSITION_P (decl2)
15884 && DECL_NAME (decl2);
15885 decl2 = DECL_CHAIN (decl2))
15887 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15889 gcc_assert (errorcount);
15890 return error_mark_node;
15892 (*cnt)++;
15893 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15894 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15895 tree v = DECL_VALUE_EXPR (decl2);
15896 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15897 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15898 decl3 = tsubst (decl2, args, complain, in_decl);
15899 SET_DECL_VALUE_EXPR (decl2, v);
15900 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15901 if (VAR_P (decl3))
15902 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15903 maybe_push_decl (decl3);
15904 if (error_operand_p (decl3))
15905 decl = error_mark_node;
15906 else if (decl != error_mark_node
15907 && DECL_CHAIN (decl3) != prev)
15909 gcc_assert (errorcount);
15910 decl = error_mark_node;
15912 else
15913 prev = decl3;
15915 *first = prev;
15916 return decl;
15919 /* Like tsubst_copy for expressions, etc. but also does semantic
15920 processing. */
15922 tree
15923 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15924 bool integral_constant_expression_p)
15926 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15927 #define RECUR(NODE) \
15928 tsubst_expr ((NODE), args, complain, in_decl, \
15929 integral_constant_expression_p)
15931 tree stmt, tmp;
15932 tree r;
15933 location_t loc;
15935 if (t == NULL_TREE || t == error_mark_node)
15936 return t;
15938 loc = input_location;
15939 if (EXPR_HAS_LOCATION (t))
15940 input_location = EXPR_LOCATION (t);
15941 if (STATEMENT_CODE_P (TREE_CODE (t)))
15942 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15944 switch (TREE_CODE (t))
15946 case STATEMENT_LIST:
15948 tree_stmt_iterator i;
15949 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15950 RECUR (tsi_stmt (i));
15951 break;
15954 case CTOR_INITIALIZER:
15955 finish_mem_initializers (tsubst_initializer_list
15956 (TREE_OPERAND (t, 0), args));
15957 break;
15959 case RETURN_EXPR:
15960 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15961 break;
15963 case EXPR_STMT:
15964 tmp = RECUR (EXPR_STMT_EXPR (t));
15965 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15966 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15967 else
15968 finish_expr_stmt (tmp);
15969 break;
15971 case USING_STMT:
15972 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15973 /*attribs=*/NULL_TREE);
15974 break;
15976 case DECL_EXPR:
15978 tree decl, pattern_decl;
15979 tree init;
15981 pattern_decl = decl = DECL_EXPR_DECL (t);
15982 if (TREE_CODE (decl) == LABEL_DECL)
15983 finish_label_decl (DECL_NAME (decl));
15984 else if (TREE_CODE (decl) == USING_DECL)
15986 tree scope = USING_DECL_SCOPE (decl);
15987 tree name = DECL_NAME (decl);
15989 scope = tsubst (scope, args, complain, in_decl);
15990 decl = lookup_qualified_name (scope, name,
15991 /*is_type_p=*/false,
15992 /*complain=*/false);
15993 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15994 qualified_name_lookup_error (scope, name, decl, input_location);
15995 else
15996 finish_local_using_decl (decl, scope, name);
15998 else if (DECL_PACK_P (decl))
16000 /* Don't build up decls for a variadic capture proxy, we'll
16001 instantiate the elements directly as needed. */
16002 break;
16004 else if (is_capture_proxy (decl)
16005 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16007 /* We're in tsubst_lambda_expr, we've already inserted a new
16008 capture proxy, so look it up and register it. */
16009 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16010 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16011 gcc_assert (inst != decl && is_capture_proxy (inst));
16012 register_local_specialization (inst, decl);
16013 break;
16015 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16016 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16017 /* Don't copy the old closure; we'll create a new one in
16018 tsubst_lambda_expr. */
16019 break;
16020 else
16022 init = DECL_INITIAL (decl);
16023 decl = tsubst (decl, args, complain, in_decl);
16024 if (decl != error_mark_node)
16026 /* By marking the declaration as instantiated, we avoid
16027 trying to instantiate it. Since instantiate_decl can't
16028 handle local variables, and since we've already done
16029 all that needs to be done, that's the right thing to
16030 do. */
16031 if (VAR_P (decl))
16032 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16033 if (VAR_P (decl)
16034 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16035 /* Anonymous aggregates are a special case. */
16036 finish_anon_union (decl);
16037 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16039 DECL_CONTEXT (decl) = current_function_decl;
16040 if (DECL_NAME (decl) == this_identifier)
16042 tree lam = DECL_CONTEXT (current_function_decl);
16043 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16044 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16046 insert_capture_proxy (decl);
16048 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16049 /* We already did a pushtag. */;
16050 else if (TREE_CODE (decl) == FUNCTION_DECL
16051 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16052 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16054 DECL_CONTEXT (decl) = NULL_TREE;
16055 pushdecl (decl);
16056 DECL_CONTEXT (decl) = current_function_decl;
16057 cp_check_omp_declare_reduction (decl);
16059 else
16061 int const_init = false;
16062 maybe_push_decl (decl);
16063 if (VAR_P (decl)
16064 && DECL_PRETTY_FUNCTION_P (decl))
16066 /* For __PRETTY_FUNCTION__ we have to adjust the
16067 initializer. */
16068 const char *const name
16069 = cxx_printable_name (current_function_decl, 2);
16070 init = cp_fname_init (name, &TREE_TYPE (decl));
16072 else
16073 init = tsubst_init (init, decl, args, complain, in_decl);
16075 if (VAR_P (decl))
16076 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16077 (pattern_decl));
16078 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16079 if (VAR_P (decl)
16080 && DECL_DECOMPOSITION_P (decl)
16081 && TREE_TYPE (pattern_decl) != error_mark_node)
16083 unsigned int cnt;
16084 tree first;
16085 decl = tsubst_decomp_names (decl, pattern_decl, args,
16086 complain, in_decl, &first,
16087 &cnt);
16088 if (decl != error_mark_node)
16089 cp_finish_decomp (decl, first, cnt);
16095 break;
16098 case FOR_STMT:
16099 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16100 RECUR (FOR_INIT_STMT (t));
16101 finish_init_stmt (stmt);
16102 tmp = RECUR (FOR_COND (t));
16103 finish_for_cond (tmp, stmt, false);
16104 tmp = RECUR (FOR_EXPR (t));
16105 finish_for_expr (tmp, stmt);
16106 RECUR (FOR_BODY (t));
16107 finish_for_stmt (stmt);
16108 break;
16110 case RANGE_FOR_STMT:
16112 tree decl, expr;
16113 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16114 decl = RANGE_FOR_DECL (t);
16115 decl = tsubst (decl, args, complain, in_decl);
16116 maybe_push_decl (decl);
16117 expr = RECUR (RANGE_FOR_EXPR (t));
16118 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16120 unsigned int cnt;
16121 tree first;
16122 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16123 complain, in_decl, &first, &cnt);
16124 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16125 RANGE_FOR_IVDEP (t));
16127 else
16128 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16129 RANGE_FOR_IVDEP (t));
16130 RECUR (RANGE_FOR_BODY (t));
16131 finish_for_stmt (stmt);
16133 break;
16135 case WHILE_STMT:
16136 stmt = begin_while_stmt ();
16137 tmp = RECUR (WHILE_COND (t));
16138 finish_while_stmt_cond (tmp, stmt, false);
16139 RECUR (WHILE_BODY (t));
16140 finish_while_stmt (stmt);
16141 break;
16143 case DO_STMT:
16144 stmt = begin_do_stmt ();
16145 RECUR (DO_BODY (t));
16146 finish_do_body (stmt);
16147 tmp = RECUR (DO_COND (t));
16148 finish_do_stmt (tmp, stmt, false);
16149 break;
16151 case IF_STMT:
16152 stmt = begin_if_stmt ();
16153 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16154 tmp = RECUR (IF_COND (t));
16155 tmp = finish_if_stmt_cond (tmp, stmt);
16156 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16157 /* Don't instantiate the THEN_CLAUSE. */;
16158 else
16160 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16161 if (inhibit)
16162 ++c_inhibit_evaluation_warnings;
16163 RECUR (THEN_CLAUSE (t));
16164 if (inhibit)
16165 --c_inhibit_evaluation_warnings;
16167 finish_then_clause (stmt);
16169 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16170 /* Don't instantiate the ELSE_CLAUSE. */;
16171 else if (ELSE_CLAUSE (t))
16173 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16174 begin_else_clause (stmt);
16175 if (inhibit)
16176 ++c_inhibit_evaluation_warnings;
16177 RECUR (ELSE_CLAUSE (t));
16178 if (inhibit)
16179 --c_inhibit_evaluation_warnings;
16180 finish_else_clause (stmt);
16183 finish_if_stmt (stmt);
16184 break;
16186 case BIND_EXPR:
16187 if (BIND_EXPR_BODY_BLOCK (t))
16188 stmt = begin_function_body ();
16189 else
16190 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16191 ? BCS_TRY_BLOCK : 0);
16193 RECUR (BIND_EXPR_BODY (t));
16195 if (BIND_EXPR_BODY_BLOCK (t))
16196 finish_function_body (stmt);
16197 else
16198 finish_compound_stmt (stmt);
16199 break;
16201 case BREAK_STMT:
16202 finish_break_stmt ();
16203 break;
16205 case CONTINUE_STMT:
16206 finish_continue_stmt ();
16207 break;
16209 case SWITCH_STMT:
16210 stmt = begin_switch_stmt ();
16211 tmp = RECUR (SWITCH_STMT_COND (t));
16212 finish_switch_cond (tmp, stmt);
16213 RECUR (SWITCH_STMT_BODY (t));
16214 finish_switch_stmt (stmt);
16215 break;
16217 case CASE_LABEL_EXPR:
16219 tree low = RECUR (CASE_LOW (t));
16220 tree high = RECUR (CASE_HIGH (t));
16221 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16222 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16223 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16224 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16226 break;
16228 case LABEL_EXPR:
16230 tree decl = LABEL_EXPR_LABEL (t);
16231 tree label;
16233 label = finish_label_stmt (DECL_NAME (decl));
16234 if (TREE_CODE (label) == LABEL_DECL)
16235 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16236 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16237 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16239 break;
16241 case GOTO_EXPR:
16242 tmp = GOTO_DESTINATION (t);
16243 if (TREE_CODE (tmp) != LABEL_DECL)
16244 /* Computed goto's must be tsubst'd into. On the other hand,
16245 non-computed gotos must not be; the identifier in question
16246 will have no binding. */
16247 tmp = RECUR (tmp);
16248 else
16249 tmp = DECL_NAME (tmp);
16250 finish_goto_stmt (tmp);
16251 break;
16253 case ASM_EXPR:
16255 tree string = RECUR (ASM_STRING (t));
16256 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16257 complain, in_decl);
16258 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16259 complain, in_decl);
16260 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16261 complain, in_decl);
16262 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16263 complain, in_decl);
16264 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16265 clobbers, labels);
16266 tree asm_expr = tmp;
16267 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16268 asm_expr = TREE_OPERAND (asm_expr, 0);
16269 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16271 break;
16273 case TRY_BLOCK:
16274 if (CLEANUP_P (t))
16276 stmt = begin_try_block ();
16277 RECUR (TRY_STMTS (t));
16278 finish_cleanup_try_block (stmt);
16279 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16281 else
16283 tree compound_stmt = NULL_TREE;
16285 if (FN_TRY_BLOCK_P (t))
16286 stmt = begin_function_try_block (&compound_stmt);
16287 else
16288 stmt = begin_try_block ();
16290 RECUR (TRY_STMTS (t));
16292 if (FN_TRY_BLOCK_P (t))
16293 finish_function_try_block (stmt);
16294 else
16295 finish_try_block (stmt);
16297 RECUR (TRY_HANDLERS (t));
16298 if (FN_TRY_BLOCK_P (t))
16299 finish_function_handler_sequence (stmt, compound_stmt);
16300 else
16301 finish_handler_sequence (stmt);
16303 break;
16305 case HANDLER:
16307 tree decl = HANDLER_PARMS (t);
16309 if (decl)
16311 decl = tsubst (decl, args, complain, in_decl);
16312 /* Prevent instantiate_decl from trying to instantiate
16313 this variable. We've already done all that needs to be
16314 done. */
16315 if (decl != error_mark_node)
16316 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16318 stmt = begin_handler ();
16319 finish_handler_parms (decl, stmt);
16320 RECUR (HANDLER_BODY (t));
16321 finish_handler (stmt);
16323 break;
16325 case TAG_DEFN:
16326 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16327 if (CLASS_TYPE_P (tmp))
16329 /* Local classes are not independent templates; they are
16330 instantiated along with their containing function. And this
16331 way we don't have to deal with pushing out of one local class
16332 to instantiate a member of another local class. */
16333 /* Closures are handled by the LAMBDA_EXPR. */
16334 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16335 complete_type (tmp);
16336 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16337 if ((VAR_P (fld)
16338 || (TREE_CODE (fld) == FUNCTION_DECL
16339 && !DECL_ARTIFICIAL (fld)))
16340 && DECL_TEMPLATE_INSTANTIATION (fld))
16341 instantiate_decl (fld, /*defer_ok=*/false,
16342 /*expl_inst_class=*/false);
16344 break;
16346 case STATIC_ASSERT:
16348 tree condition;
16350 ++c_inhibit_evaluation_warnings;
16351 condition =
16352 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16353 args,
16354 complain, in_decl,
16355 /*integral_constant_expression_p=*/true);
16356 --c_inhibit_evaluation_warnings;
16358 finish_static_assert (condition,
16359 STATIC_ASSERT_MESSAGE (t),
16360 STATIC_ASSERT_SOURCE_LOCATION (t),
16361 /*member_p=*/false);
16363 break;
16365 case OACC_KERNELS:
16366 case OACC_PARALLEL:
16367 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16368 in_decl);
16369 stmt = begin_omp_parallel ();
16370 RECUR (OMP_BODY (t));
16371 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16372 break;
16374 case OMP_PARALLEL:
16375 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16376 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16377 complain, in_decl);
16378 if (OMP_PARALLEL_COMBINED (t))
16379 omp_parallel_combined_clauses = &tmp;
16380 stmt = begin_omp_parallel ();
16381 RECUR (OMP_PARALLEL_BODY (t));
16382 gcc_assert (omp_parallel_combined_clauses == NULL);
16383 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16384 = OMP_PARALLEL_COMBINED (t);
16385 pop_omp_privatization_clauses (r);
16386 break;
16388 case OMP_TASK:
16389 r = push_omp_privatization_clauses (false);
16390 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16391 complain, in_decl);
16392 stmt = begin_omp_task ();
16393 RECUR (OMP_TASK_BODY (t));
16394 finish_omp_task (tmp, stmt);
16395 pop_omp_privatization_clauses (r);
16396 break;
16398 case OMP_FOR:
16399 case OMP_SIMD:
16400 case CILK_SIMD:
16401 case CILK_FOR:
16402 case OMP_DISTRIBUTE:
16403 case OMP_TASKLOOP:
16404 case OACC_LOOP:
16406 tree clauses, body, pre_body;
16407 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16408 tree orig_declv = NULL_TREE;
16409 tree incrv = NULL_TREE;
16410 enum c_omp_region_type ort = C_ORT_OMP;
16411 int i;
16413 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16414 ort = C_ORT_CILK;
16415 else if (TREE_CODE (t) == OACC_LOOP)
16416 ort = C_ORT_ACC;
16418 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16419 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16420 in_decl);
16421 if (OMP_FOR_INIT (t) != NULL_TREE)
16423 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16424 if (OMP_FOR_ORIG_DECLS (t))
16425 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16426 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16427 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16428 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16431 stmt = begin_omp_structured_block ();
16433 pre_body = push_stmt_list ();
16434 RECUR (OMP_FOR_PRE_BODY (t));
16435 pre_body = pop_stmt_list (pre_body);
16437 if (OMP_FOR_INIT (t) != NULL_TREE)
16438 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16439 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16440 incrv, &clauses, args, complain, in_decl,
16441 integral_constant_expression_p);
16442 omp_parallel_combined_clauses = NULL;
16444 body = push_stmt_list ();
16445 RECUR (OMP_FOR_BODY (t));
16446 body = pop_stmt_list (body);
16448 if (OMP_FOR_INIT (t) != NULL_TREE)
16449 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16450 orig_declv, initv, condv, incrv, body, pre_body,
16451 NULL, clauses);
16452 else
16454 t = make_node (TREE_CODE (t));
16455 TREE_TYPE (t) = void_type_node;
16456 OMP_FOR_BODY (t) = body;
16457 OMP_FOR_PRE_BODY (t) = pre_body;
16458 OMP_FOR_CLAUSES (t) = clauses;
16459 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16460 add_stmt (t);
16463 add_stmt (finish_omp_structured_block (stmt));
16464 pop_omp_privatization_clauses (r);
16466 break;
16468 case OMP_SECTIONS:
16469 omp_parallel_combined_clauses = NULL;
16470 /* FALLTHRU */
16471 case OMP_SINGLE:
16472 case OMP_TEAMS:
16473 case OMP_CRITICAL:
16474 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16475 && OMP_TEAMS_COMBINED (t));
16476 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16477 in_decl);
16478 stmt = push_stmt_list ();
16479 RECUR (OMP_BODY (t));
16480 stmt = pop_stmt_list (stmt);
16482 t = copy_node (t);
16483 OMP_BODY (t) = stmt;
16484 OMP_CLAUSES (t) = tmp;
16485 add_stmt (t);
16486 pop_omp_privatization_clauses (r);
16487 break;
16489 case OACC_DATA:
16490 case OMP_TARGET_DATA:
16491 case OMP_TARGET:
16492 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16493 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16494 in_decl);
16495 keep_next_level (true);
16496 stmt = begin_omp_structured_block ();
16498 RECUR (OMP_BODY (t));
16499 stmt = finish_omp_structured_block (stmt);
16501 t = copy_node (t);
16502 OMP_BODY (t) = stmt;
16503 OMP_CLAUSES (t) = tmp;
16504 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16506 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16507 if (teams)
16509 /* For combined target teams, ensure the num_teams and
16510 thread_limit clause expressions are evaluated on the host,
16511 before entering the target construct. */
16512 tree c;
16513 for (c = OMP_TEAMS_CLAUSES (teams);
16514 c; c = OMP_CLAUSE_CHAIN (c))
16515 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16516 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16517 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16519 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16520 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16521 if (expr == error_mark_node)
16522 continue;
16523 tmp = TARGET_EXPR_SLOT (expr);
16524 add_stmt (expr);
16525 OMP_CLAUSE_OPERAND (c, 0) = expr;
16526 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16527 OMP_CLAUSE_FIRSTPRIVATE);
16528 OMP_CLAUSE_DECL (tc) = tmp;
16529 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16530 OMP_TARGET_CLAUSES (t) = tc;
16534 add_stmt (t);
16535 break;
16537 case OACC_DECLARE:
16538 t = copy_node (t);
16539 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16540 complain, in_decl);
16541 OACC_DECLARE_CLAUSES (t) = tmp;
16542 add_stmt (t);
16543 break;
16545 case OMP_TARGET_UPDATE:
16546 case OMP_TARGET_ENTER_DATA:
16547 case OMP_TARGET_EXIT_DATA:
16548 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16549 complain, in_decl);
16550 t = copy_node (t);
16551 OMP_STANDALONE_CLAUSES (t) = tmp;
16552 add_stmt (t);
16553 break;
16555 case OACC_ENTER_DATA:
16556 case OACC_EXIT_DATA:
16557 case OACC_UPDATE:
16558 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16559 complain, in_decl);
16560 t = copy_node (t);
16561 OMP_STANDALONE_CLAUSES (t) = tmp;
16562 add_stmt (t);
16563 break;
16565 case OMP_ORDERED:
16566 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16567 complain, in_decl);
16568 stmt = push_stmt_list ();
16569 RECUR (OMP_BODY (t));
16570 stmt = pop_stmt_list (stmt);
16572 t = copy_node (t);
16573 OMP_BODY (t) = stmt;
16574 OMP_ORDERED_CLAUSES (t) = tmp;
16575 add_stmt (t);
16576 break;
16578 case OMP_SECTION:
16579 case OMP_MASTER:
16580 case OMP_TASKGROUP:
16581 stmt = push_stmt_list ();
16582 RECUR (OMP_BODY (t));
16583 stmt = pop_stmt_list (stmt);
16585 t = copy_node (t);
16586 OMP_BODY (t) = stmt;
16587 add_stmt (t);
16588 break;
16590 case OMP_ATOMIC:
16591 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16592 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16594 tree op1 = TREE_OPERAND (t, 1);
16595 tree rhs1 = NULL_TREE;
16596 tree lhs, rhs;
16597 if (TREE_CODE (op1) == COMPOUND_EXPR)
16599 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16600 op1 = TREE_OPERAND (op1, 1);
16602 lhs = RECUR (TREE_OPERAND (op1, 0));
16603 rhs = RECUR (TREE_OPERAND (op1, 1));
16604 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16605 NULL_TREE, NULL_TREE, rhs1,
16606 OMP_ATOMIC_SEQ_CST (t));
16608 else
16610 tree op1 = TREE_OPERAND (t, 1);
16611 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16612 tree rhs1 = NULL_TREE;
16613 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16614 enum tree_code opcode = NOP_EXPR;
16615 if (code == OMP_ATOMIC_READ)
16617 v = RECUR (TREE_OPERAND (op1, 0));
16618 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16620 else if (code == OMP_ATOMIC_CAPTURE_OLD
16621 || code == OMP_ATOMIC_CAPTURE_NEW)
16623 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16624 v = RECUR (TREE_OPERAND (op1, 0));
16625 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16626 if (TREE_CODE (op11) == COMPOUND_EXPR)
16628 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16629 op11 = TREE_OPERAND (op11, 1);
16631 lhs = RECUR (TREE_OPERAND (op11, 0));
16632 rhs = RECUR (TREE_OPERAND (op11, 1));
16633 opcode = TREE_CODE (op11);
16634 if (opcode == MODIFY_EXPR)
16635 opcode = NOP_EXPR;
16637 else
16639 code = OMP_ATOMIC;
16640 lhs = RECUR (TREE_OPERAND (op1, 0));
16641 rhs = RECUR (TREE_OPERAND (op1, 1));
16643 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16644 OMP_ATOMIC_SEQ_CST (t));
16646 break;
16648 case TRANSACTION_EXPR:
16650 int flags = 0;
16651 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16652 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16654 if (TRANSACTION_EXPR_IS_STMT (t))
16656 tree body = TRANSACTION_EXPR_BODY (t);
16657 tree noex = NULL_TREE;
16658 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16660 noex = MUST_NOT_THROW_COND (body);
16661 if (noex == NULL_TREE)
16662 noex = boolean_true_node;
16663 body = TREE_OPERAND (body, 0);
16665 stmt = begin_transaction_stmt (input_location, NULL, flags);
16666 RECUR (body);
16667 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16669 else
16671 stmt = build_transaction_expr (EXPR_LOCATION (t),
16672 RECUR (TRANSACTION_EXPR_BODY (t)),
16673 flags, NULL_TREE);
16674 RETURN (stmt);
16677 break;
16679 case MUST_NOT_THROW_EXPR:
16681 tree op0 = RECUR (TREE_OPERAND (t, 0));
16682 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16683 RETURN (build_must_not_throw_expr (op0, cond));
16686 case EXPR_PACK_EXPANSION:
16687 error ("invalid use of pack expansion expression");
16688 RETURN (error_mark_node);
16690 case NONTYPE_ARGUMENT_PACK:
16691 error ("use %<...%> to expand argument pack");
16692 RETURN (error_mark_node);
16694 case CILK_SPAWN_STMT:
16695 cfun->calls_cilk_spawn = 1;
16696 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16698 case CILK_SYNC_STMT:
16699 RETURN (build_cilk_sync ());
16701 case COMPOUND_EXPR:
16702 tmp = RECUR (TREE_OPERAND (t, 0));
16703 if (tmp == NULL_TREE)
16704 /* If the first operand was a statement, we're done with it. */
16705 RETURN (RECUR (TREE_OPERAND (t, 1)));
16706 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16707 RECUR (TREE_OPERAND (t, 1)),
16708 complain));
16710 case ANNOTATE_EXPR:
16711 tmp = RECUR (TREE_OPERAND (t, 0));
16712 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16713 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16715 default:
16716 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16718 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16719 /*function_p=*/false,
16720 integral_constant_expression_p));
16723 RETURN (NULL_TREE);
16724 out:
16725 input_location = loc;
16726 return r;
16727 #undef RECUR
16728 #undef RETURN
16731 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16732 function. For description of the body see comment above
16733 cp_parser_omp_declare_reduction_exprs. */
16735 static void
16736 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16738 if (t == NULL_TREE || t == error_mark_node)
16739 return;
16741 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16743 tree_stmt_iterator tsi;
16744 int i;
16745 tree stmts[7];
16746 memset (stmts, 0, sizeof stmts);
16747 for (i = 0, tsi = tsi_start (t);
16748 i < 7 && !tsi_end_p (tsi);
16749 i++, tsi_next (&tsi))
16750 stmts[i] = tsi_stmt (tsi);
16751 gcc_assert (tsi_end_p (tsi));
16753 if (i >= 3)
16755 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16756 && TREE_CODE (stmts[1]) == DECL_EXPR);
16757 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16758 args, complain, in_decl);
16759 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16760 args, complain, in_decl);
16761 DECL_CONTEXT (omp_out) = current_function_decl;
16762 DECL_CONTEXT (omp_in) = current_function_decl;
16763 keep_next_level (true);
16764 tree block = begin_omp_structured_block ();
16765 tsubst_expr (stmts[2], args, complain, in_decl, false);
16766 block = finish_omp_structured_block (block);
16767 block = maybe_cleanup_point_expr_void (block);
16768 add_decl_expr (omp_out);
16769 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16770 TREE_NO_WARNING (omp_out) = 1;
16771 add_decl_expr (omp_in);
16772 finish_expr_stmt (block);
16774 if (i >= 6)
16776 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16777 && TREE_CODE (stmts[4]) == DECL_EXPR);
16778 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16779 args, complain, in_decl);
16780 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16781 args, complain, in_decl);
16782 DECL_CONTEXT (omp_priv) = current_function_decl;
16783 DECL_CONTEXT (omp_orig) = current_function_decl;
16784 keep_next_level (true);
16785 tree block = begin_omp_structured_block ();
16786 tsubst_expr (stmts[5], args, complain, in_decl, false);
16787 block = finish_omp_structured_block (block);
16788 block = maybe_cleanup_point_expr_void (block);
16789 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16790 add_decl_expr (omp_priv);
16791 add_decl_expr (omp_orig);
16792 finish_expr_stmt (block);
16793 if (i == 7)
16794 add_decl_expr (omp_orig);
16798 /* T is a postfix-expression that is not being used in a function
16799 call. Return the substituted version of T. */
16801 static tree
16802 tsubst_non_call_postfix_expression (tree t, tree args,
16803 tsubst_flags_t complain,
16804 tree in_decl)
16806 if (TREE_CODE (t) == SCOPE_REF)
16807 t = tsubst_qualified_id (t, args, complain, in_decl,
16808 /*done=*/false, /*address_p=*/false);
16809 else
16810 t = tsubst_copy_and_build (t, args, complain, in_decl,
16811 /*function_p=*/false,
16812 /*integral_constant_expression_p=*/false);
16814 return t;
16817 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16818 instantiation context. Instantiating a pack expansion containing a lambda
16819 might result in multiple lambdas all based on the same lambda in the
16820 template. */
16822 tree
16823 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16825 tree oldfn = lambda_function (t);
16826 in_decl = oldfn;
16828 tree r = build_lambda_expr ();
16830 LAMBDA_EXPR_LOCATION (r)
16831 = LAMBDA_EXPR_LOCATION (t);
16832 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16833 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16834 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16836 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16837 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16838 else
16839 record_lambda_scope (r);
16841 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16842 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16844 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16845 cap = TREE_CHAIN (cap))
16847 tree field = TREE_PURPOSE (cap);
16848 if (PACK_EXPANSION_P (field))
16849 field = PACK_EXPANSION_PATTERN (field);
16850 field = tsubst_decl (field, args, complain);
16852 if (field == error_mark_node)
16853 return error_mark_node;
16855 tree init = TREE_VALUE (cap);
16856 if (PACK_EXPANSION_P (init))
16857 init = tsubst_pack_expansion (init, args, complain, in_decl);
16858 else
16859 init = tsubst_copy_and_build (init, args, complain, in_decl,
16860 /*fn*/false, /*constexpr*/false);
16862 if (TREE_CODE (field) == TREE_VEC)
16864 int len = TREE_VEC_LENGTH (field);
16865 gcc_assert (TREE_CODE (init) == TREE_VEC
16866 && TREE_VEC_LENGTH (init) == len);
16867 for (int i = 0; i < len; ++i)
16868 LAMBDA_EXPR_CAPTURE_LIST (r)
16869 = tree_cons (TREE_VEC_ELT (field, i),
16870 TREE_VEC_ELT (init, i),
16871 LAMBDA_EXPR_CAPTURE_LIST (r));
16873 else
16875 LAMBDA_EXPR_CAPTURE_LIST (r)
16876 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16878 if (id_equal (DECL_NAME (field), "__this"))
16879 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16883 tree type = begin_lambda_type (r);
16885 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16886 determine_visibility (TYPE_NAME (type));
16888 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16890 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16891 ? DECL_TI_TEMPLATE (oldfn)
16892 : NULL_TREE);
16894 tree fntype = static_fn_type (oldfn);
16895 if (oldtmpl)
16896 ++processing_template_decl;
16897 fntype = tsubst (fntype, args, complain, in_decl);
16898 if (oldtmpl)
16899 --processing_template_decl;
16901 if (fntype == error_mark_node)
16902 r = error_mark_node;
16903 else
16905 /* Fix the type of 'this'. */
16906 fntype = build_memfn_type (fntype, type,
16907 type_memfn_quals (fntype),
16908 type_memfn_rqual (fntype));
16909 tree fn, tmpl;
16910 if (oldtmpl)
16912 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16913 fn = DECL_TEMPLATE_RESULT (tmpl);
16914 finish_member_declaration (tmpl);
16916 else
16918 tmpl = NULL_TREE;
16919 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16920 finish_member_declaration (fn);
16923 /* Let finish_function set this. */
16924 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16926 bool nested = cfun;
16927 if (nested)
16928 push_function_context ();
16930 local_specialization_stack s (lss_copy);
16932 tree body = start_lambda_function (fn, r);
16934 register_parameter_specializations (oldfn, fn);
16936 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16937 /*constexpr*/false);
16939 finish_lambda_function (body);
16941 if (nested)
16942 pop_function_context ();
16944 /* The capture list was built up in reverse order; fix that now. */
16945 LAMBDA_EXPR_CAPTURE_LIST (r)
16946 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16948 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16950 maybe_add_lambda_conv_op (type);
16953 finish_struct (type, /*attr*/NULL_TREE);
16955 insert_pending_capture_proxies ();
16957 return r;
16960 /* Like tsubst but deals with expressions and performs semantic
16961 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16963 tree
16964 tsubst_copy_and_build (tree t,
16965 tree args,
16966 tsubst_flags_t complain,
16967 tree in_decl,
16968 bool function_p,
16969 bool integral_constant_expression_p)
16971 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16972 #define RECUR(NODE) \
16973 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16974 /*function_p=*/false, \
16975 integral_constant_expression_p)
16977 tree retval, op1;
16978 location_t loc;
16980 if (t == NULL_TREE || t == error_mark_node)
16981 return t;
16983 loc = input_location;
16984 if (EXPR_HAS_LOCATION (t))
16985 input_location = EXPR_LOCATION (t);
16987 /* N3276 decltype magic only applies to calls at the top level or on the
16988 right side of a comma. */
16989 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16990 complain &= ~tf_decltype;
16992 switch (TREE_CODE (t))
16994 case USING_DECL:
16995 t = DECL_NAME (t);
16996 /* Fall through. */
16997 case IDENTIFIER_NODE:
16999 tree decl;
17000 cp_id_kind idk;
17001 bool non_integral_constant_expression_p;
17002 const char *error_msg;
17004 if (IDENTIFIER_CONV_OP_P (t))
17006 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17007 t = make_conv_op_name (new_type);
17010 /* Look up the name. */
17011 decl = lookup_name (t);
17013 /* By convention, expressions use ERROR_MARK_NODE to indicate
17014 failure, not NULL_TREE. */
17015 if (decl == NULL_TREE)
17016 decl = error_mark_node;
17018 decl = finish_id_expression (t, decl, NULL_TREE,
17019 &idk,
17020 integral_constant_expression_p,
17021 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17022 &non_integral_constant_expression_p,
17023 /*template_p=*/false,
17024 /*done=*/true,
17025 /*address_p=*/false,
17026 /*template_arg_p=*/false,
17027 &error_msg,
17028 input_location);
17029 if (error_msg)
17030 error (error_msg);
17031 if (!function_p && identifier_p (decl))
17033 if (complain & tf_error)
17034 unqualified_name_lookup_error (decl);
17035 decl = error_mark_node;
17037 RETURN (decl);
17040 case TEMPLATE_ID_EXPR:
17042 tree object;
17043 tree templ = RECUR (TREE_OPERAND (t, 0));
17044 tree targs = TREE_OPERAND (t, 1);
17046 if (targs)
17047 targs = tsubst_template_args (targs, args, complain, in_decl);
17048 if (targs == error_mark_node)
17049 return error_mark_node;
17051 if (TREE_CODE (templ) == SCOPE_REF)
17053 tree name = TREE_OPERAND (templ, 1);
17054 tree tid = lookup_template_function (name, targs);
17055 TREE_OPERAND (templ, 1) = tid;
17056 return templ;
17059 if (variable_template_p (templ))
17060 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17062 if (TREE_CODE (templ) == COMPONENT_REF)
17064 object = TREE_OPERAND (templ, 0);
17065 templ = TREE_OPERAND (templ, 1);
17067 else
17068 object = NULL_TREE;
17069 templ = lookup_template_function (templ, targs);
17071 if (object)
17072 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17073 object, templ, NULL_TREE));
17074 else
17075 RETURN (baselink_for_fns (templ));
17078 case INDIRECT_REF:
17080 tree r = RECUR (TREE_OPERAND (t, 0));
17082 if (REFERENCE_REF_P (t))
17084 /* A type conversion to reference type will be enclosed in
17085 such an indirect ref, but the substitution of the cast
17086 will have also added such an indirect ref. */
17087 r = convert_from_reference (r);
17089 else
17090 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17091 complain|decltype_flag);
17093 if (TREE_CODE (r) == INDIRECT_REF)
17094 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17096 RETURN (r);
17099 case NOP_EXPR:
17101 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17102 tree op0 = RECUR (TREE_OPERAND (t, 0));
17103 RETURN (build_nop (type, op0));
17106 case IMPLICIT_CONV_EXPR:
17108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17109 tree expr = RECUR (TREE_OPERAND (t, 0));
17110 int flags = LOOKUP_IMPLICIT;
17111 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17112 flags = LOOKUP_NORMAL;
17113 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17114 flags));
17117 case CONVERT_EXPR:
17119 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17120 tree op0 = RECUR (TREE_OPERAND (t, 0));
17121 RETURN (build1 (CONVERT_EXPR, type, op0));
17124 case CAST_EXPR:
17125 case REINTERPRET_CAST_EXPR:
17126 case CONST_CAST_EXPR:
17127 case DYNAMIC_CAST_EXPR:
17128 case STATIC_CAST_EXPR:
17130 tree type;
17131 tree op, r = NULL_TREE;
17133 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17134 if (integral_constant_expression_p
17135 && !cast_valid_in_integral_constant_expression_p (type))
17137 if (complain & tf_error)
17138 error ("a cast to a type other than an integral or "
17139 "enumeration type cannot appear in a constant-expression");
17140 RETURN (error_mark_node);
17143 op = RECUR (TREE_OPERAND (t, 0));
17145 warning_sentinel s(warn_useless_cast);
17146 switch (TREE_CODE (t))
17148 case CAST_EXPR:
17149 r = build_functional_cast (type, op, complain);
17150 break;
17151 case REINTERPRET_CAST_EXPR:
17152 r = build_reinterpret_cast (type, op, complain);
17153 break;
17154 case CONST_CAST_EXPR:
17155 r = build_const_cast (type, op, complain);
17156 break;
17157 case DYNAMIC_CAST_EXPR:
17158 r = build_dynamic_cast (type, op, complain);
17159 break;
17160 case STATIC_CAST_EXPR:
17161 r = build_static_cast (type, op, complain);
17162 break;
17163 default:
17164 gcc_unreachable ();
17167 RETURN (r);
17170 case POSTDECREMENT_EXPR:
17171 case POSTINCREMENT_EXPR:
17172 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17173 args, complain, in_decl);
17174 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17175 complain|decltype_flag));
17177 case PREDECREMENT_EXPR:
17178 case PREINCREMENT_EXPR:
17179 case NEGATE_EXPR:
17180 case BIT_NOT_EXPR:
17181 case ABS_EXPR:
17182 case TRUTH_NOT_EXPR:
17183 case UNARY_PLUS_EXPR: /* Unary + */
17184 case REALPART_EXPR:
17185 case IMAGPART_EXPR:
17186 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17187 RECUR (TREE_OPERAND (t, 0)),
17188 complain|decltype_flag));
17190 case FIX_TRUNC_EXPR:
17191 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17192 false, complain));
17194 case ADDR_EXPR:
17195 op1 = TREE_OPERAND (t, 0);
17196 if (TREE_CODE (op1) == LABEL_DECL)
17197 RETURN (finish_label_address_expr (DECL_NAME (op1),
17198 EXPR_LOCATION (op1)));
17199 if (TREE_CODE (op1) == SCOPE_REF)
17200 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17201 /*done=*/true, /*address_p=*/true);
17202 else
17203 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17204 in_decl);
17205 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17206 complain|decltype_flag));
17208 case PLUS_EXPR:
17209 case MINUS_EXPR:
17210 case MULT_EXPR:
17211 case TRUNC_DIV_EXPR:
17212 case CEIL_DIV_EXPR:
17213 case FLOOR_DIV_EXPR:
17214 case ROUND_DIV_EXPR:
17215 case EXACT_DIV_EXPR:
17216 case BIT_AND_EXPR:
17217 case BIT_IOR_EXPR:
17218 case BIT_XOR_EXPR:
17219 case TRUNC_MOD_EXPR:
17220 case FLOOR_MOD_EXPR:
17221 case TRUTH_ANDIF_EXPR:
17222 case TRUTH_ORIF_EXPR:
17223 case TRUTH_AND_EXPR:
17224 case TRUTH_OR_EXPR:
17225 case RSHIFT_EXPR:
17226 case LSHIFT_EXPR:
17227 case RROTATE_EXPR:
17228 case LROTATE_EXPR:
17229 case EQ_EXPR:
17230 case NE_EXPR:
17231 case MAX_EXPR:
17232 case MIN_EXPR:
17233 case LE_EXPR:
17234 case GE_EXPR:
17235 case LT_EXPR:
17236 case GT_EXPR:
17237 case MEMBER_REF:
17238 case DOTSTAR_EXPR:
17240 warning_sentinel s1(warn_type_limits);
17241 warning_sentinel s2(warn_div_by_zero);
17242 warning_sentinel s3(warn_logical_op);
17243 warning_sentinel s4(warn_tautological_compare);
17244 tree op0 = RECUR (TREE_OPERAND (t, 0));
17245 tree op1 = RECUR (TREE_OPERAND (t, 1));
17246 tree r = build_x_binary_op
17247 (input_location, TREE_CODE (t),
17248 op0,
17249 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17250 ? ERROR_MARK
17251 : TREE_CODE (TREE_OPERAND (t, 0))),
17252 op1,
17253 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17254 ? ERROR_MARK
17255 : TREE_CODE (TREE_OPERAND (t, 1))),
17256 /*overload=*/NULL,
17257 complain|decltype_flag);
17258 if (EXPR_P (r) && TREE_NO_WARNING (t))
17259 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17261 RETURN (r);
17264 case POINTER_PLUS_EXPR:
17266 tree op0 = RECUR (TREE_OPERAND (t, 0));
17267 tree op1 = RECUR (TREE_OPERAND (t, 1));
17268 return fold_build_pointer_plus (op0, op1);
17271 case SCOPE_REF:
17272 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17273 /*address_p=*/false));
17274 case ARRAY_REF:
17275 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17276 args, complain, in_decl);
17277 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17278 RECUR (TREE_OPERAND (t, 1)),
17279 complain|decltype_flag));
17281 case ARRAY_NOTATION_REF:
17283 tree start_index, length, stride;
17284 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17285 args, complain, in_decl);
17286 start_index = RECUR (ARRAY_NOTATION_START (t));
17287 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17288 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17289 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17290 length, stride, TREE_TYPE (op1)));
17292 case SIZEOF_EXPR:
17293 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17294 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17295 RETURN (tsubst_copy (t, args, complain, in_decl));
17296 /* Fall through */
17298 case ALIGNOF_EXPR:
17300 tree r;
17302 op1 = TREE_OPERAND (t, 0);
17303 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17304 op1 = TREE_TYPE (op1);
17305 if (!args)
17307 /* When there are no ARGS, we are trying to evaluate a
17308 non-dependent expression from the parser. Trying to do
17309 the substitutions may not work. */
17310 if (!TYPE_P (op1))
17311 op1 = TREE_TYPE (op1);
17313 else
17315 ++cp_unevaluated_operand;
17316 ++c_inhibit_evaluation_warnings;
17317 if (TYPE_P (op1))
17318 op1 = tsubst (op1, args, complain, in_decl);
17319 else
17320 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17321 /*function_p=*/false,
17322 /*integral_constant_expression_p=*/
17323 false);
17324 --cp_unevaluated_operand;
17325 --c_inhibit_evaluation_warnings;
17327 if (TYPE_P (op1))
17328 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17329 complain & tf_error);
17330 else
17331 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17332 complain & tf_error);
17333 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17335 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17337 if (!processing_template_decl && TYPE_P (op1))
17339 r = build_min (SIZEOF_EXPR, size_type_node,
17340 build1 (NOP_EXPR, op1, error_mark_node));
17341 SIZEOF_EXPR_TYPE_P (r) = 1;
17343 else
17344 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17345 TREE_SIDE_EFFECTS (r) = 0;
17346 TREE_READONLY (r) = 1;
17348 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17350 RETURN (r);
17353 case AT_ENCODE_EXPR:
17355 op1 = TREE_OPERAND (t, 0);
17356 ++cp_unevaluated_operand;
17357 ++c_inhibit_evaluation_warnings;
17358 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17359 /*function_p=*/false,
17360 /*integral_constant_expression_p=*/false);
17361 --cp_unevaluated_operand;
17362 --c_inhibit_evaluation_warnings;
17363 RETURN (objc_build_encode_expr (op1));
17366 case NOEXCEPT_EXPR:
17367 op1 = TREE_OPERAND (t, 0);
17368 ++cp_unevaluated_operand;
17369 ++c_inhibit_evaluation_warnings;
17370 ++cp_noexcept_operand;
17371 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17372 /*function_p=*/false,
17373 /*integral_constant_expression_p=*/false);
17374 --cp_unevaluated_operand;
17375 --c_inhibit_evaluation_warnings;
17376 --cp_noexcept_operand;
17377 RETURN (finish_noexcept_expr (op1, complain));
17379 case MODOP_EXPR:
17381 warning_sentinel s(warn_div_by_zero);
17382 tree lhs = RECUR (TREE_OPERAND (t, 0));
17383 tree rhs = RECUR (TREE_OPERAND (t, 2));
17384 tree r = build_x_modify_expr
17385 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17386 complain|decltype_flag);
17387 /* TREE_NO_WARNING must be set if either the expression was
17388 parenthesized or it uses an operator such as >>= rather
17389 than plain assignment. In the former case, it was already
17390 set and must be copied. In the latter case,
17391 build_x_modify_expr sets it and it must not be reset
17392 here. */
17393 if (TREE_NO_WARNING (t))
17394 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17396 RETURN (r);
17399 case ARROW_EXPR:
17400 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17401 args, complain, in_decl);
17402 /* Remember that there was a reference to this entity. */
17403 if (DECL_P (op1)
17404 && !mark_used (op1, complain) && !(complain & tf_error))
17405 RETURN (error_mark_node);
17406 RETURN (build_x_arrow (input_location, op1, complain));
17408 case NEW_EXPR:
17410 tree placement = RECUR (TREE_OPERAND (t, 0));
17411 tree init = RECUR (TREE_OPERAND (t, 3));
17412 vec<tree, va_gc> *placement_vec;
17413 vec<tree, va_gc> *init_vec;
17414 tree ret;
17416 if (placement == NULL_TREE)
17417 placement_vec = NULL;
17418 else
17420 placement_vec = make_tree_vector ();
17421 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17422 vec_safe_push (placement_vec, TREE_VALUE (placement));
17425 /* If there was an initializer in the original tree, but it
17426 instantiated to an empty list, then we should pass a
17427 non-NULL empty vector to tell build_new that it was an
17428 empty initializer() rather than no initializer. This can
17429 only happen when the initializer is a pack expansion whose
17430 parameter packs are of length zero. */
17431 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17432 init_vec = NULL;
17433 else
17435 init_vec = make_tree_vector ();
17436 if (init == void_node)
17437 gcc_assert (init_vec != NULL);
17438 else
17440 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17441 vec_safe_push (init_vec, TREE_VALUE (init));
17445 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17446 tree op2 = RECUR (TREE_OPERAND (t, 2));
17447 ret = build_new (&placement_vec, op1, op2, &init_vec,
17448 NEW_EXPR_USE_GLOBAL (t),
17449 complain);
17451 if (placement_vec != NULL)
17452 release_tree_vector (placement_vec);
17453 if (init_vec != NULL)
17454 release_tree_vector (init_vec);
17456 RETURN (ret);
17459 case DELETE_EXPR:
17461 tree op0 = RECUR (TREE_OPERAND (t, 0));
17462 tree op1 = RECUR (TREE_OPERAND (t, 1));
17463 RETURN (delete_sanity (op0, op1,
17464 DELETE_EXPR_USE_VEC (t),
17465 DELETE_EXPR_USE_GLOBAL (t),
17466 complain));
17469 case COMPOUND_EXPR:
17471 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17472 complain & ~tf_decltype, in_decl,
17473 /*function_p=*/false,
17474 integral_constant_expression_p);
17475 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17476 op0,
17477 RECUR (TREE_OPERAND (t, 1)),
17478 complain|decltype_flag));
17481 case CALL_EXPR:
17483 tree function;
17484 vec<tree, va_gc> *call_args;
17485 unsigned int nargs, i;
17486 bool qualified_p;
17487 bool koenig_p;
17488 tree ret;
17490 function = CALL_EXPR_FN (t);
17491 /* Internal function with no arguments. */
17492 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17493 RETURN (t);
17495 /* When we parsed the expression, we determined whether or
17496 not Koenig lookup should be performed. */
17497 koenig_p = KOENIG_LOOKUP_P (t);
17498 if (function == NULL_TREE)
17500 koenig_p = false;
17501 qualified_p = false;
17503 else if (TREE_CODE (function) == SCOPE_REF)
17505 qualified_p = true;
17506 function = tsubst_qualified_id (function, args, complain, in_decl,
17507 /*done=*/false,
17508 /*address_p=*/false);
17510 else if (koenig_p && identifier_p (function))
17512 /* Do nothing; calling tsubst_copy_and_build on an identifier
17513 would incorrectly perform unqualified lookup again.
17515 Note that we can also have an IDENTIFIER_NODE if the earlier
17516 unqualified lookup found a member function; in that case
17517 koenig_p will be false and we do want to do the lookup
17518 again to find the instantiated member function.
17520 FIXME but doing that causes c++/15272, so we need to stop
17521 using IDENTIFIER_NODE in that situation. */
17522 qualified_p = false;
17524 else
17526 if (TREE_CODE (function) == COMPONENT_REF)
17528 tree op = TREE_OPERAND (function, 1);
17530 qualified_p = (TREE_CODE (op) == SCOPE_REF
17531 || (BASELINK_P (op)
17532 && BASELINK_QUALIFIED_P (op)));
17534 else
17535 qualified_p = false;
17537 if (TREE_CODE (function) == ADDR_EXPR
17538 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17539 /* Avoid error about taking the address of a constructor. */
17540 function = TREE_OPERAND (function, 0);
17542 function = tsubst_copy_and_build (function, args, complain,
17543 in_decl,
17544 !qualified_p,
17545 integral_constant_expression_p);
17547 if (BASELINK_P (function))
17548 qualified_p = true;
17551 nargs = call_expr_nargs (t);
17552 call_args = make_tree_vector ();
17553 for (i = 0; i < nargs; ++i)
17555 tree arg = CALL_EXPR_ARG (t, i);
17557 if (!PACK_EXPANSION_P (arg))
17558 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17559 else
17561 /* Expand the pack expansion and push each entry onto
17562 CALL_ARGS. */
17563 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17564 if (TREE_CODE (arg) == TREE_VEC)
17566 unsigned int len, j;
17568 len = TREE_VEC_LENGTH (arg);
17569 for (j = 0; j < len; ++j)
17571 tree value = TREE_VEC_ELT (arg, j);
17572 if (value != NULL_TREE)
17573 value = convert_from_reference (value);
17574 vec_safe_push (call_args, value);
17577 else
17579 /* A partial substitution. Add one entry. */
17580 vec_safe_push (call_args, arg);
17585 /* We do not perform argument-dependent lookup if normal
17586 lookup finds a non-function, in accordance with the
17587 expected resolution of DR 218. */
17588 if (koenig_p
17589 && ((is_overloaded_fn (function)
17590 /* If lookup found a member function, the Koenig lookup is
17591 not appropriate, even if an unqualified-name was used
17592 to denote the function. */
17593 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17594 || identifier_p (function))
17595 /* Only do this when substitution turns a dependent call
17596 into a non-dependent call. */
17597 && type_dependent_expression_p_push (t)
17598 && !any_type_dependent_arguments_p (call_args))
17599 function = perform_koenig_lookup (function, call_args, tf_none);
17601 if (function != NULL_TREE
17602 && identifier_p (function)
17603 && !any_type_dependent_arguments_p (call_args))
17605 if (koenig_p && (complain & tf_warning_or_error))
17607 /* For backwards compatibility and good diagnostics, try
17608 the unqualified lookup again if we aren't in SFINAE
17609 context. */
17610 tree unq = (tsubst_copy_and_build
17611 (function, args, complain, in_decl, true,
17612 integral_constant_expression_p));
17613 if (unq == error_mark_node)
17615 release_tree_vector (call_args);
17616 RETURN (error_mark_node);
17619 if (unq != function)
17621 /* In a lambda fn, we have to be careful to not
17622 introduce new this captures. Legacy code can't
17623 be using lambdas anyway, so it's ok to be
17624 stricter. */
17625 bool in_lambda = (current_class_type
17626 && LAMBDA_TYPE_P (current_class_type));
17627 char const *const msg
17628 = G_("%qD was not declared in this scope, "
17629 "and no declarations were found by "
17630 "argument-dependent lookup at the point "
17631 "of instantiation");
17633 bool diag = true;
17634 if (in_lambda)
17635 error_at (EXPR_LOC_OR_LOC (t, input_location),
17636 msg, function);
17637 else
17638 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17639 msg, function);
17640 if (diag)
17642 tree fn = unq;
17644 if (INDIRECT_REF_P (fn))
17645 fn = TREE_OPERAND (fn, 0);
17646 if (is_overloaded_fn (fn))
17647 fn = get_first_fn (fn);
17649 if (!DECL_P (fn))
17650 /* Can't say anything more. */;
17651 else if (DECL_CLASS_SCOPE_P (fn))
17653 location_t loc = EXPR_LOC_OR_LOC (t,
17654 input_location);
17655 inform (loc,
17656 "declarations in dependent base %qT are "
17657 "not found by unqualified lookup",
17658 DECL_CLASS_CONTEXT (fn));
17659 if (current_class_ptr)
17660 inform (loc,
17661 "use %<this->%D%> instead", function);
17662 else
17663 inform (loc,
17664 "use %<%T::%D%> instead",
17665 current_class_name, function);
17667 else
17668 inform (DECL_SOURCE_LOCATION (fn),
17669 "%qD declared here, later in the "
17670 "translation unit", fn);
17671 if (in_lambda)
17673 release_tree_vector (call_args);
17674 RETURN (error_mark_node);
17678 function = unq;
17681 if (identifier_p (function))
17683 if (complain & tf_error)
17684 unqualified_name_lookup_error (function);
17685 release_tree_vector (call_args);
17686 RETURN (error_mark_node);
17690 /* Remember that there was a reference to this entity. */
17691 if (function != NULL_TREE
17692 && DECL_P (function)
17693 && !mark_used (function, complain) && !(complain & tf_error))
17695 release_tree_vector (call_args);
17696 RETURN (error_mark_node);
17699 /* Put back tf_decltype for the actual call. */
17700 complain |= decltype_flag;
17702 if (function == NULL_TREE)
17703 switch (CALL_EXPR_IFN (t))
17705 case IFN_LAUNDER:
17706 gcc_assert (nargs == 1);
17707 if (vec_safe_length (call_args) != 1)
17709 error_at (EXPR_LOC_OR_LOC (t, input_location),
17710 "wrong number of arguments to "
17711 "%<__builtin_launder%>");
17712 ret = error_mark_node;
17714 else
17715 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17716 input_location),
17717 (*call_args)[0], complain);
17718 break;
17720 default:
17721 /* Unsupported internal function with arguments. */
17722 gcc_unreachable ();
17724 else if (TREE_CODE (function) == OFFSET_REF)
17725 ret = build_offset_ref_call_from_tree (function, &call_args,
17726 complain);
17727 else if (TREE_CODE (function) == COMPONENT_REF)
17729 tree instance = TREE_OPERAND (function, 0);
17730 tree fn = TREE_OPERAND (function, 1);
17732 if (processing_template_decl
17733 && (type_dependent_expression_p (instance)
17734 || (!BASELINK_P (fn)
17735 && TREE_CODE (fn) != FIELD_DECL)
17736 || type_dependent_expression_p (fn)
17737 || any_type_dependent_arguments_p (call_args)))
17738 ret = build_min_nt_call_vec (function, call_args);
17739 else if (!BASELINK_P (fn))
17740 ret = finish_call_expr (function, &call_args,
17741 /*disallow_virtual=*/false,
17742 /*koenig_p=*/false,
17743 complain);
17744 else
17745 ret = (build_new_method_call
17746 (instance, fn,
17747 &call_args, NULL_TREE,
17748 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17749 /*fn_p=*/NULL,
17750 complain));
17752 else
17753 ret = finish_call_expr (function, &call_args,
17754 /*disallow_virtual=*/qualified_p,
17755 koenig_p,
17756 complain);
17758 release_tree_vector (call_args);
17760 if (ret != error_mark_node)
17762 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17763 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17764 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17765 bool thk = CALL_FROM_THUNK_P (t);
17766 if (op || ord || rev || thk)
17768 function = extract_call_expr (ret);
17769 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17770 CALL_EXPR_ORDERED_ARGS (function) = ord;
17771 CALL_EXPR_REVERSE_ARGS (function) = rev;
17772 if (thk)
17774 CALL_FROM_THUNK_P (function) = true;
17775 /* The thunk location is not interesting. */
17776 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17781 RETURN (ret);
17784 case COND_EXPR:
17786 tree cond = RECUR (TREE_OPERAND (t, 0));
17787 tree folded_cond = fold_non_dependent_expr (cond);
17788 tree exp1, exp2;
17790 if (TREE_CODE (folded_cond) == INTEGER_CST)
17792 if (integer_zerop (folded_cond))
17794 ++c_inhibit_evaluation_warnings;
17795 exp1 = RECUR (TREE_OPERAND (t, 1));
17796 --c_inhibit_evaluation_warnings;
17797 exp2 = RECUR (TREE_OPERAND (t, 2));
17799 else
17801 exp1 = RECUR (TREE_OPERAND (t, 1));
17802 ++c_inhibit_evaluation_warnings;
17803 exp2 = RECUR (TREE_OPERAND (t, 2));
17804 --c_inhibit_evaluation_warnings;
17806 cond = folded_cond;
17808 else
17810 exp1 = RECUR (TREE_OPERAND (t, 1));
17811 exp2 = RECUR (TREE_OPERAND (t, 2));
17814 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17815 cond, exp1, exp2, complain));
17818 case PSEUDO_DTOR_EXPR:
17820 tree op0 = RECUR (TREE_OPERAND (t, 0));
17821 tree op1 = RECUR (TREE_OPERAND (t, 1));
17822 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17823 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17824 input_location));
17827 case TREE_LIST:
17829 tree purpose, value, chain;
17831 if (t == void_list_node)
17832 RETURN (t);
17834 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17835 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17837 /* We have pack expansions, so expand those and
17838 create a new list out of it. */
17839 tree purposevec = NULL_TREE;
17840 tree valuevec = NULL_TREE;
17841 tree chain;
17842 int i, len = -1;
17844 /* Expand the argument expressions. */
17845 if (TREE_PURPOSE (t))
17846 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17847 complain, in_decl);
17848 if (TREE_VALUE (t))
17849 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17850 complain, in_decl);
17852 /* Build the rest of the list. */
17853 chain = TREE_CHAIN (t);
17854 if (chain && chain != void_type_node)
17855 chain = RECUR (chain);
17857 /* Determine the number of arguments. */
17858 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17860 len = TREE_VEC_LENGTH (purposevec);
17861 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17863 else if (TREE_CODE (valuevec) == TREE_VEC)
17864 len = TREE_VEC_LENGTH (valuevec);
17865 else
17867 /* Since we only performed a partial substitution into
17868 the argument pack, we only RETURN (a single list
17869 node. */
17870 if (purposevec == TREE_PURPOSE (t)
17871 && valuevec == TREE_VALUE (t)
17872 && chain == TREE_CHAIN (t))
17873 RETURN (t);
17875 RETURN (tree_cons (purposevec, valuevec, chain));
17878 /* Convert the argument vectors into a TREE_LIST */
17879 i = len;
17880 while (i > 0)
17882 /* Grab the Ith values. */
17883 i--;
17884 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17885 : NULL_TREE;
17886 value
17887 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17888 : NULL_TREE;
17890 /* Build the list (backwards). */
17891 chain = tree_cons (purpose, value, chain);
17894 RETURN (chain);
17897 purpose = TREE_PURPOSE (t);
17898 if (purpose)
17899 purpose = RECUR (purpose);
17900 value = TREE_VALUE (t);
17901 if (value)
17902 value = RECUR (value);
17903 chain = TREE_CHAIN (t);
17904 if (chain && chain != void_type_node)
17905 chain = RECUR (chain);
17906 if (purpose == TREE_PURPOSE (t)
17907 && value == TREE_VALUE (t)
17908 && chain == TREE_CHAIN (t))
17909 RETURN (t);
17910 RETURN (tree_cons (purpose, value, chain));
17913 case COMPONENT_REF:
17915 tree object;
17916 tree object_type;
17917 tree member;
17918 tree r;
17920 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17921 args, complain, in_decl);
17922 /* Remember that there was a reference to this entity. */
17923 if (DECL_P (object)
17924 && !mark_used (object, complain) && !(complain & tf_error))
17925 RETURN (error_mark_node);
17926 object_type = TREE_TYPE (object);
17928 member = TREE_OPERAND (t, 1);
17929 if (BASELINK_P (member))
17930 member = tsubst_baselink (member,
17931 non_reference (TREE_TYPE (object)),
17932 args, complain, in_decl);
17933 else
17934 member = tsubst_copy (member, args, complain, in_decl);
17935 if (member == error_mark_node)
17936 RETURN (error_mark_node);
17938 if (TREE_CODE (member) == FIELD_DECL)
17940 r = finish_non_static_data_member (member, object, NULL_TREE);
17941 if (TREE_CODE (r) == COMPONENT_REF)
17942 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17943 RETURN (r);
17945 else if (type_dependent_expression_p (object))
17946 /* We can't do much here. */;
17947 else if (!CLASS_TYPE_P (object_type))
17949 if (scalarish_type_p (object_type))
17951 tree s = NULL_TREE;
17952 tree dtor = member;
17954 if (TREE_CODE (dtor) == SCOPE_REF)
17956 s = TREE_OPERAND (dtor, 0);
17957 dtor = TREE_OPERAND (dtor, 1);
17959 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17961 dtor = TREE_OPERAND (dtor, 0);
17962 if (TYPE_P (dtor))
17963 RETURN (finish_pseudo_destructor_expr
17964 (object, s, dtor, input_location));
17968 else if (TREE_CODE (member) == SCOPE_REF
17969 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17971 /* Lookup the template functions now that we know what the
17972 scope is. */
17973 tree scope = TREE_OPERAND (member, 0);
17974 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17975 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17976 member = lookup_qualified_name (scope, tmpl,
17977 /*is_type_p=*/false,
17978 /*complain=*/false);
17979 if (BASELINK_P (member))
17981 BASELINK_FUNCTIONS (member)
17982 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17983 args);
17984 member = (adjust_result_of_qualified_name_lookup
17985 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17986 object_type));
17988 else
17990 qualified_name_lookup_error (scope, tmpl, member,
17991 input_location);
17992 RETURN (error_mark_node);
17995 else if (TREE_CODE (member) == SCOPE_REF
17996 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17997 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17999 if (complain & tf_error)
18001 if (TYPE_P (TREE_OPERAND (member, 0)))
18002 error ("%qT is not a class or namespace",
18003 TREE_OPERAND (member, 0));
18004 else
18005 error ("%qD is not a class or namespace",
18006 TREE_OPERAND (member, 0));
18008 RETURN (error_mark_node);
18011 r = finish_class_member_access_expr (object, member,
18012 /*template_p=*/false,
18013 complain);
18014 if (TREE_CODE (r) == COMPONENT_REF)
18015 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18016 RETURN (r);
18019 case THROW_EXPR:
18020 RETURN (build_throw
18021 (RECUR (TREE_OPERAND (t, 0))));
18023 case CONSTRUCTOR:
18025 vec<constructor_elt, va_gc> *n;
18026 constructor_elt *ce;
18027 unsigned HOST_WIDE_INT idx;
18028 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18029 bool process_index_p;
18030 int newlen;
18031 bool need_copy_p = false;
18032 tree r;
18034 if (type == error_mark_node)
18035 RETURN (error_mark_node);
18037 /* digest_init will do the wrong thing if we let it. */
18038 if (type && TYPE_PTRMEMFUNC_P (type))
18039 RETURN (t);
18041 /* We do not want to process the index of aggregate
18042 initializers as they are identifier nodes which will be
18043 looked up by digest_init. */
18044 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18046 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18047 newlen = vec_safe_length (n);
18048 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18050 if (ce->index && process_index_p
18051 /* An identifier index is looked up in the type
18052 being initialized, not the current scope. */
18053 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18054 ce->index = RECUR (ce->index);
18056 if (PACK_EXPANSION_P (ce->value))
18058 /* Substitute into the pack expansion. */
18059 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18060 in_decl);
18062 if (ce->value == error_mark_node
18063 || PACK_EXPANSION_P (ce->value))
18065 else if (TREE_VEC_LENGTH (ce->value) == 1)
18066 /* Just move the argument into place. */
18067 ce->value = TREE_VEC_ELT (ce->value, 0);
18068 else
18070 /* Update the length of the final CONSTRUCTOR
18071 arguments vector, and note that we will need to
18072 copy.*/
18073 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18074 need_copy_p = true;
18077 else
18078 ce->value = RECUR (ce->value);
18081 if (need_copy_p)
18083 vec<constructor_elt, va_gc> *old_n = n;
18085 vec_alloc (n, newlen);
18086 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18088 if (TREE_CODE (ce->value) == TREE_VEC)
18090 int i, len = TREE_VEC_LENGTH (ce->value);
18091 for (i = 0; i < len; ++i)
18092 CONSTRUCTOR_APPEND_ELT (n, 0,
18093 TREE_VEC_ELT (ce->value, i));
18095 else
18096 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18100 r = build_constructor (init_list_type_node, n);
18101 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18103 if (TREE_HAS_CONSTRUCTOR (t))
18105 fcl_t cl = fcl_functional;
18106 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18107 cl = fcl_c99;
18108 RETURN (finish_compound_literal (type, r, complain, cl));
18111 TREE_TYPE (r) = type;
18112 RETURN (r);
18115 case TYPEID_EXPR:
18117 tree operand_0 = TREE_OPERAND (t, 0);
18118 if (TYPE_P (operand_0))
18120 operand_0 = tsubst (operand_0, args, complain, in_decl);
18121 RETURN (get_typeid (operand_0, complain));
18123 else
18125 operand_0 = RECUR (operand_0);
18126 RETURN (build_typeid (operand_0, complain));
18130 case VAR_DECL:
18131 if (!args)
18132 RETURN (t);
18133 else if (DECL_PACK_P (t))
18135 /* We don't build decls for an instantiation of a
18136 variadic capture proxy, we instantiate the elements
18137 when needed. */
18138 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18139 return RECUR (DECL_VALUE_EXPR (t));
18141 /* Fall through */
18143 case PARM_DECL:
18145 tree r = tsubst_copy (t, args, complain, in_decl);
18146 /* ??? We're doing a subset of finish_id_expression here. */
18147 if (VAR_P (r)
18148 && !processing_template_decl
18149 && !cp_unevaluated_operand
18150 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18151 && CP_DECL_THREAD_LOCAL_P (r))
18153 if (tree wrap = get_tls_wrapper_fn (r))
18154 /* Replace an evaluated use of the thread_local variable with
18155 a call to its wrapper. */
18156 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18158 else if (outer_automatic_var_p (r))
18159 r = process_outer_var_ref (r, complain);
18161 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18162 /* If the original type was a reference, we'll be wrapped in
18163 the appropriate INDIRECT_REF. */
18164 r = convert_from_reference (r);
18165 RETURN (r);
18168 case VA_ARG_EXPR:
18170 tree op0 = RECUR (TREE_OPERAND (t, 0));
18171 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18172 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18175 case OFFSETOF_EXPR:
18177 tree object_ptr
18178 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18179 in_decl, /*function_p=*/false,
18180 /*integral_constant_expression_p=*/false);
18181 RETURN (finish_offsetof (object_ptr,
18182 RECUR (TREE_OPERAND (t, 0)),
18183 EXPR_LOCATION (t)));
18186 case ADDRESSOF_EXPR:
18187 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18188 RECUR (TREE_OPERAND (t, 0)), complain));
18190 case TRAIT_EXPR:
18192 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18193 complain, in_decl);
18195 tree type2 = TRAIT_EXPR_TYPE2 (t);
18196 if (type2 && TREE_CODE (type2) == TREE_LIST)
18197 type2 = RECUR (type2);
18198 else if (type2)
18199 type2 = tsubst (type2, args, complain, in_decl);
18201 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18204 case STMT_EXPR:
18206 tree old_stmt_expr = cur_stmt_expr;
18207 tree stmt_expr = begin_stmt_expr ();
18209 cur_stmt_expr = stmt_expr;
18210 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18211 integral_constant_expression_p);
18212 stmt_expr = finish_stmt_expr (stmt_expr, false);
18213 cur_stmt_expr = old_stmt_expr;
18215 /* If the resulting list of expression statement is empty,
18216 fold it further into void_node. */
18217 if (empty_expr_stmt_p (stmt_expr))
18218 stmt_expr = void_node;
18220 RETURN (stmt_expr);
18223 case LAMBDA_EXPR:
18225 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18227 RETURN (build_lambda_object (r));
18230 case TARGET_EXPR:
18231 /* We can get here for a constant initializer of non-dependent type.
18232 FIXME stop folding in cp_parser_initializer_clause. */
18234 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18235 complain);
18236 RETURN (r);
18239 case TRANSACTION_EXPR:
18240 RETURN (tsubst_expr(t, args, complain, in_decl,
18241 integral_constant_expression_p));
18243 case PAREN_EXPR:
18244 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18246 case VEC_PERM_EXPR:
18248 tree op0 = RECUR (TREE_OPERAND (t, 0));
18249 tree op1 = RECUR (TREE_OPERAND (t, 1));
18250 tree op2 = RECUR (TREE_OPERAND (t, 2));
18251 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18252 complain));
18255 case REQUIRES_EXPR:
18256 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18258 default:
18259 /* Handle Objective-C++ constructs, if appropriate. */
18261 tree subst
18262 = objcp_tsubst_copy_and_build (t, args, complain,
18263 in_decl, /*function_p=*/false);
18264 if (subst)
18265 RETURN (subst);
18267 RETURN (tsubst_copy (t, args, complain, in_decl));
18270 #undef RECUR
18271 #undef RETURN
18272 out:
18273 input_location = loc;
18274 return retval;
18277 /* Verify that the instantiated ARGS are valid. For type arguments,
18278 make sure that the type's linkage is ok. For non-type arguments,
18279 make sure they are constants if they are integral or enumerations.
18280 Emit an error under control of COMPLAIN, and return TRUE on error. */
18282 static bool
18283 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18285 if (dependent_template_arg_p (t))
18286 return false;
18287 if (ARGUMENT_PACK_P (t))
18289 tree vec = ARGUMENT_PACK_ARGS (t);
18290 int len = TREE_VEC_LENGTH (vec);
18291 bool result = false;
18292 int i;
18294 for (i = 0; i < len; ++i)
18295 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18296 result = true;
18297 return result;
18299 else if (TYPE_P (t))
18301 /* [basic.link]: A name with no linkage (notably, the name
18302 of a class or enumeration declared in a local scope)
18303 shall not be used to declare an entity with linkage.
18304 This implies that names with no linkage cannot be used as
18305 template arguments
18307 DR 757 relaxes this restriction for C++0x. */
18308 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18309 : no_linkage_check (t, /*relaxed_p=*/false));
18311 if (nt)
18313 /* DR 488 makes use of a type with no linkage cause
18314 type deduction to fail. */
18315 if (complain & tf_error)
18317 if (TYPE_UNNAMED_P (nt))
18318 error ("%qT is/uses unnamed type", t);
18319 else
18320 error ("template argument for %qD uses local type %qT",
18321 tmpl, t);
18323 return true;
18325 /* In order to avoid all sorts of complications, we do not
18326 allow variably-modified types as template arguments. */
18327 else if (variably_modified_type_p (t, NULL_TREE))
18329 if (complain & tf_error)
18330 error ("%qT is a variably modified type", t);
18331 return true;
18334 /* Class template and alias template arguments should be OK. */
18335 else if (DECL_TYPE_TEMPLATE_P (t))
18337 /* A non-type argument of integral or enumerated type must be a
18338 constant. */
18339 else if (TREE_TYPE (t)
18340 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18341 && !REFERENCE_REF_P (t)
18342 && !TREE_CONSTANT (t))
18344 if (complain & tf_error)
18345 error ("integral expression %qE is not constant", t);
18346 return true;
18348 return false;
18351 static bool
18352 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18354 int ix, len = DECL_NTPARMS (tmpl);
18355 bool result = false;
18357 for (ix = 0; ix != len; ix++)
18359 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18360 result = true;
18362 if (result && (complain & tf_error))
18363 error (" trying to instantiate %qD", tmpl);
18364 return result;
18367 /* We're out of SFINAE context now, so generate diagnostics for the access
18368 errors we saw earlier when instantiating D from TMPL and ARGS. */
18370 static void
18371 recheck_decl_substitution (tree d, tree tmpl, tree args)
18373 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18374 tree type = TREE_TYPE (pattern);
18375 location_t loc = input_location;
18377 push_access_scope (d);
18378 push_deferring_access_checks (dk_no_deferred);
18379 input_location = DECL_SOURCE_LOCATION (pattern);
18380 tsubst (type, args, tf_warning_or_error, d);
18381 input_location = loc;
18382 pop_deferring_access_checks ();
18383 pop_access_scope (d);
18386 /* Instantiate the indicated variable, function, or alias template TMPL with
18387 the template arguments in TARG_PTR. */
18389 static tree
18390 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18392 tree targ_ptr = orig_args;
18393 tree fndecl;
18394 tree gen_tmpl;
18395 tree spec;
18396 bool access_ok = true;
18398 if (tmpl == error_mark_node)
18399 return error_mark_node;
18401 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18403 /* If this function is a clone, handle it specially. */
18404 if (DECL_CLONED_FUNCTION_P (tmpl))
18406 tree spec;
18407 tree clone;
18409 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18410 DECL_CLONED_FUNCTION. */
18411 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18412 targ_ptr, complain);
18413 if (spec == error_mark_node)
18414 return error_mark_node;
18416 /* Look for the clone. */
18417 FOR_EACH_CLONE (clone, spec)
18418 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18419 return clone;
18420 /* We should always have found the clone by now. */
18421 gcc_unreachable ();
18422 return NULL_TREE;
18425 if (targ_ptr == error_mark_node)
18426 return error_mark_node;
18428 /* Check to see if we already have this specialization. */
18429 gen_tmpl = most_general_template (tmpl);
18430 if (TMPL_ARGS_DEPTH (targ_ptr)
18431 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18432 /* targ_ptr only has the innermost template args, so add the outer ones
18433 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18434 the case of a non-dependent call within a template definition). */
18435 targ_ptr = (add_outermost_template_args
18436 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18437 targ_ptr));
18439 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18440 but it doesn't seem to be on the hot path. */
18441 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18443 gcc_assert (tmpl == gen_tmpl
18444 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18445 == spec)
18446 || fndecl == NULL_TREE);
18448 if (spec != NULL_TREE)
18450 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18452 if (complain & tf_error)
18453 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18454 return error_mark_node;
18456 return spec;
18459 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18460 complain))
18461 return error_mark_node;
18463 /* We are building a FUNCTION_DECL, during which the access of its
18464 parameters and return types have to be checked. However this
18465 FUNCTION_DECL which is the desired context for access checking
18466 is not built yet. We solve this chicken-and-egg problem by
18467 deferring all checks until we have the FUNCTION_DECL. */
18468 push_deferring_access_checks (dk_deferred);
18470 /* Instantiation of the function happens in the context of the function
18471 template, not the context of the overload resolution we're doing. */
18472 push_to_top_level ();
18473 /* If there are dependent arguments, e.g. because we're doing partial
18474 ordering, make sure processing_template_decl stays set. */
18475 if (uses_template_parms (targ_ptr))
18476 ++processing_template_decl;
18477 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18479 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18480 complain, gen_tmpl, true);
18481 push_nested_class (ctx);
18484 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18486 fndecl = NULL_TREE;
18487 if (VAR_P (pattern))
18489 /* We need to determine if we're using a partial or explicit
18490 specialization now, because the type of the variable could be
18491 different. */
18492 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18493 tree elt = most_specialized_partial_spec (tid, complain);
18494 if (elt == error_mark_node)
18495 pattern = error_mark_node;
18496 else if (elt)
18498 tree partial_tmpl = TREE_VALUE (elt);
18499 tree partial_args = TREE_PURPOSE (elt);
18500 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18501 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18505 /* Substitute template parameters to obtain the specialization. */
18506 if (fndecl == NULL_TREE)
18507 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18508 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18509 pop_nested_class ();
18510 pop_from_top_level ();
18512 if (fndecl == error_mark_node)
18514 pop_deferring_access_checks ();
18515 return error_mark_node;
18518 /* The DECL_TI_TEMPLATE should always be the immediate parent
18519 template, not the most general template. */
18520 DECL_TI_TEMPLATE (fndecl) = tmpl;
18521 DECL_TI_ARGS (fndecl) = targ_ptr;
18523 /* Now we know the specialization, compute access previously
18524 deferred. Do no access control for inheriting constructors,
18525 as we already checked access for the inherited constructor. */
18526 if (!(flag_new_inheriting_ctors
18527 && DECL_INHERITED_CTOR (fndecl)))
18529 push_access_scope (fndecl);
18530 if (!perform_deferred_access_checks (complain))
18531 access_ok = false;
18532 pop_access_scope (fndecl);
18534 pop_deferring_access_checks ();
18536 /* If we've just instantiated the main entry point for a function,
18537 instantiate all the alternate entry points as well. We do this
18538 by cloning the instantiation of the main entry point, not by
18539 instantiating the template clones. */
18540 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18541 clone_function_decl (fndecl, /*update_methods=*/false);
18543 if (!access_ok)
18545 if (!(complain & tf_error))
18547 /* Remember to reinstantiate when we're out of SFINAE so the user
18548 can see the errors. */
18549 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18551 return error_mark_node;
18553 return fndecl;
18556 /* Wrapper for instantiate_template_1. */
18558 tree
18559 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18561 tree ret;
18562 timevar_push (TV_TEMPLATE_INST);
18563 ret = instantiate_template_1 (tmpl, orig_args, complain);
18564 timevar_pop (TV_TEMPLATE_INST);
18565 return ret;
18568 /* Instantiate the alias template TMPL with ARGS. Also push a template
18569 instantiation level, which instantiate_template doesn't do because
18570 functions and variables have sufficient context established by the
18571 callers. */
18573 static tree
18574 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18576 struct pending_template *old_last_pend = last_pending_template;
18577 struct tinst_level *old_error_tinst = last_error_tinst_level;
18578 if (tmpl == error_mark_node || args == error_mark_node)
18579 return error_mark_node;
18580 tree tinst = build_tree_list (tmpl, args);
18581 if (!push_tinst_level (tinst))
18583 ggc_free (tinst);
18584 return error_mark_node;
18587 args =
18588 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18589 args, tmpl, complain,
18590 /*require_all_args=*/true,
18591 /*use_default_args=*/true);
18593 tree r = instantiate_template (tmpl, args, complain);
18594 pop_tinst_level ();
18595 /* We can't free this if a pending_template entry or last_error_tinst_level
18596 is pointing at it. */
18597 if (last_pending_template == old_last_pend
18598 && last_error_tinst_level == old_error_tinst)
18599 ggc_free (tinst);
18601 return r;
18604 /* PARM is a template parameter pack for FN. Returns true iff
18605 PARM is used in a deducible way in the argument list of FN. */
18607 static bool
18608 pack_deducible_p (tree parm, tree fn)
18610 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18611 for (; t; t = TREE_CHAIN (t))
18613 tree type = TREE_VALUE (t);
18614 tree packs;
18615 if (!PACK_EXPANSION_P (type))
18616 continue;
18617 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18618 packs; packs = TREE_CHAIN (packs))
18619 if (template_args_equal (TREE_VALUE (packs), parm))
18621 /* The template parameter pack is used in a function parameter
18622 pack. If this is the end of the parameter list, the
18623 template parameter pack is deducible. */
18624 if (TREE_CHAIN (t) == void_list_node)
18625 return true;
18626 else
18627 /* Otherwise, not. Well, it could be deduced from
18628 a non-pack parameter, but doing so would end up with
18629 a deduction mismatch, so don't bother. */
18630 return false;
18633 /* The template parameter pack isn't used in any function parameter
18634 packs, but it might be used deeper, e.g. tuple<Args...>. */
18635 return true;
18638 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18639 NARGS elements of the arguments that are being used when calling
18640 it. TARGS is a vector into which the deduced template arguments
18641 are placed.
18643 Returns either a FUNCTION_DECL for the matching specialization of FN or
18644 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18645 true, diagnostics will be printed to explain why it failed.
18647 If FN is a conversion operator, or we are trying to produce a specific
18648 specialization, RETURN_TYPE is the return type desired.
18650 The EXPLICIT_TARGS are explicit template arguments provided via a
18651 template-id.
18653 The parameter STRICT is one of:
18655 DEDUCE_CALL:
18656 We are deducing arguments for a function call, as in
18657 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18658 deducing arguments for a call to the result of a conversion
18659 function template, as in [over.call.object].
18661 DEDUCE_CONV:
18662 We are deducing arguments for a conversion function, as in
18663 [temp.deduct.conv].
18665 DEDUCE_EXACT:
18666 We are deducing arguments when doing an explicit instantiation
18667 as in [temp.explicit], when determining an explicit specialization
18668 as in [temp.expl.spec], or when taking the address of a function
18669 template, as in [temp.deduct.funcaddr]. */
18671 tree
18672 fn_type_unification (tree fn,
18673 tree explicit_targs,
18674 tree targs,
18675 const tree *args,
18676 unsigned int nargs,
18677 tree return_type,
18678 unification_kind_t strict,
18679 int flags,
18680 bool explain_p,
18681 bool decltype_p)
18683 tree parms;
18684 tree fntype;
18685 tree decl = NULL_TREE;
18686 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18687 bool ok;
18688 static int deduction_depth;
18689 struct pending_template *old_last_pend = last_pending_template;
18690 struct tinst_level *old_error_tinst = last_error_tinst_level;
18692 tree orig_fn = fn;
18693 if (flag_new_inheriting_ctors)
18694 fn = strip_inheriting_ctors (fn);
18696 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18697 tree tinst;
18698 tree r = error_mark_node;
18700 tree full_targs = targs;
18701 if (TMPL_ARGS_DEPTH (targs)
18702 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18703 full_targs = (add_outermost_template_args
18704 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18705 targs));
18707 if (decltype_p)
18708 complain |= tf_decltype;
18710 /* In C++0x, it's possible to have a function template whose type depends
18711 on itself recursively. This is most obvious with decltype, but can also
18712 occur with enumeration scope (c++/48969). So we need to catch infinite
18713 recursion and reject the substitution at deduction time; this function
18714 will return error_mark_node for any repeated substitution.
18716 This also catches excessive recursion such as when f<N> depends on
18717 f<N-1> across all integers, and returns error_mark_node for all the
18718 substitutions back up to the initial one.
18720 This is, of course, not reentrant. */
18721 if (excessive_deduction_depth)
18722 return error_mark_node;
18723 tinst = build_tree_list (fn, NULL_TREE);
18724 ++deduction_depth;
18726 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18728 fntype = TREE_TYPE (fn);
18729 if (explicit_targs)
18731 /* [temp.deduct]
18733 The specified template arguments must match the template
18734 parameters in kind (i.e., type, nontype, template), and there
18735 must not be more arguments than there are parameters;
18736 otherwise type deduction fails.
18738 Nontype arguments must match the types of the corresponding
18739 nontype template parameters, or must be convertible to the
18740 types of the corresponding nontype parameters as specified in
18741 _temp.arg.nontype_, otherwise type deduction fails.
18743 All references in the function type of the function template
18744 to the corresponding template parameters are replaced by the
18745 specified template argument values. If a substitution in a
18746 template parameter or in the function type of the function
18747 template results in an invalid type, type deduction fails. */
18748 int i, len = TREE_VEC_LENGTH (tparms);
18749 location_t loc = input_location;
18750 bool incomplete = false;
18752 if (explicit_targs == error_mark_node)
18753 goto fail;
18755 if (TMPL_ARGS_DEPTH (explicit_targs)
18756 < TMPL_ARGS_DEPTH (full_targs))
18757 explicit_targs = add_outermost_template_args (full_targs,
18758 explicit_targs);
18760 /* Adjust any explicit template arguments before entering the
18761 substitution context. */
18762 explicit_targs
18763 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18764 complain,
18765 /*require_all_args=*/false,
18766 /*use_default_args=*/false));
18767 if (explicit_targs == error_mark_node)
18768 goto fail;
18770 /* Substitute the explicit args into the function type. This is
18771 necessary so that, for instance, explicitly declared function
18772 arguments can match null pointed constants. If we were given
18773 an incomplete set of explicit args, we must not do semantic
18774 processing during substitution as we could create partial
18775 instantiations. */
18776 for (i = 0; i < len; i++)
18778 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18779 bool parameter_pack = false;
18780 tree targ = TREE_VEC_ELT (explicit_targs, i);
18782 /* Dig out the actual parm. */
18783 if (TREE_CODE (parm) == TYPE_DECL
18784 || TREE_CODE (parm) == TEMPLATE_DECL)
18786 parm = TREE_TYPE (parm);
18787 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18789 else if (TREE_CODE (parm) == PARM_DECL)
18791 parm = DECL_INITIAL (parm);
18792 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18795 if (!parameter_pack && targ == NULL_TREE)
18796 /* No explicit argument for this template parameter. */
18797 incomplete = true;
18799 if (parameter_pack && pack_deducible_p (parm, fn))
18801 /* Mark the argument pack as "incomplete". We could
18802 still deduce more arguments during unification.
18803 We remove this mark in type_unification_real. */
18804 if (targ)
18806 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18807 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18808 = ARGUMENT_PACK_ARGS (targ);
18811 /* We have some incomplete argument packs. */
18812 incomplete = true;
18816 TREE_VALUE (tinst) = explicit_targs;
18817 if (!push_tinst_level (tinst))
18819 excessive_deduction_depth = true;
18820 goto fail;
18822 processing_template_decl += incomplete;
18823 input_location = DECL_SOURCE_LOCATION (fn);
18824 /* Ignore any access checks; we'll see them again in
18825 instantiate_template and they might have the wrong
18826 access path at this point. */
18827 push_deferring_access_checks (dk_deferred);
18828 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18829 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18830 pop_deferring_access_checks ();
18831 input_location = loc;
18832 processing_template_decl -= incomplete;
18833 pop_tinst_level ();
18835 if (fntype == error_mark_node)
18836 goto fail;
18838 /* Place the explicitly specified arguments in TARGS. */
18839 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18840 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18841 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18844 /* Never do unification on the 'this' parameter. */
18845 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18847 if (return_type && strict == DEDUCE_CALL)
18849 /* We're deducing for a call to the result of a template conversion
18850 function. The parms we really want are in return_type. */
18851 if (POINTER_TYPE_P (return_type))
18852 return_type = TREE_TYPE (return_type);
18853 parms = TYPE_ARG_TYPES (return_type);
18855 else if (return_type)
18857 tree *new_args;
18859 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18860 new_args = XALLOCAVEC (tree, nargs + 1);
18861 new_args[0] = return_type;
18862 memcpy (new_args + 1, args, nargs * sizeof (tree));
18863 args = new_args;
18864 ++nargs;
18867 /* We allow incomplete unification without an error message here
18868 because the standard doesn't seem to explicitly prohibit it. Our
18869 callers must be ready to deal with unification failures in any
18870 event. */
18872 TREE_VALUE (tinst) = targs;
18873 /* If we aren't explaining yet, push tinst context so we can see where
18874 any errors (e.g. from class instantiations triggered by instantiation
18875 of default template arguments) come from. If we are explaining, this
18876 context is redundant. */
18877 if (!explain_p && !push_tinst_level (tinst))
18879 excessive_deduction_depth = true;
18880 goto fail;
18883 /* type_unification_real will pass back any access checks from default
18884 template argument substitution. */
18885 vec<deferred_access_check, va_gc> *checks;
18886 checks = NULL;
18888 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18889 full_targs, parms, args, nargs, /*subr=*/0,
18890 strict, flags, &checks, explain_p);
18891 if (!explain_p)
18892 pop_tinst_level ();
18893 if (!ok)
18894 goto fail;
18896 /* Now that we have bindings for all of the template arguments,
18897 ensure that the arguments deduced for the template template
18898 parameters have compatible template parameter lists. We cannot
18899 check this property before we have deduced all template
18900 arguments, because the template parameter types of a template
18901 template parameter might depend on prior template parameters
18902 deduced after the template template parameter. The following
18903 ill-formed example illustrates this issue:
18905 template<typename T, template<T> class C> void f(C<5>, T);
18907 template<int N> struct X {};
18909 void g() {
18910 f(X<5>(), 5l); // error: template argument deduction fails
18913 The template parameter list of 'C' depends on the template type
18914 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18915 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18916 time that we deduce 'C'. */
18917 if (!template_template_parm_bindings_ok_p
18918 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18920 unify_inconsistent_template_template_parameters (explain_p);
18921 goto fail;
18924 /* All is well so far. Now, check:
18926 [temp.deduct]
18928 When all template arguments have been deduced, all uses of
18929 template parameters in nondeduced contexts are replaced with
18930 the corresponding deduced argument values. If the
18931 substitution results in an invalid type, as described above,
18932 type deduction fails. */
18933 TREE_VALUE (tinst) = targs;
18934 if (!push_tinst_level (tinst))
18936 excessive_deduction_depth = true;
18937 goto fail;
18940 /* Also collect access checks from the instantiation. */
18941 reopen_deferring_access_checks (checks);
18943 decl = instantiate_template (fn, targs, complain);
18945 checks = get_deferred_access_checks ();
18946 pop_deferring_access_checks ();
18948 pop_tinst_level ();
18950 if (decl == error_mark_node)
18951 goto fail;
18953 /* Now perform any access checks encountered during substitution. */
18954 push_access_scope (decl);
18955 ok = perform_access_checks (checks, complain);
18956 pop_access_scope (decl);
18957 if (!ok)
18958 goto fail;
18960 /* If we're looking for an exact match, check that what we got
18961 is indeed an exact match. It might not be if some template
18962 parameters are used in non-deduced contexts. But don't check
18963 for an exact match if we have dependent template arguments;
18964 in that case we're doing partial ordering, and we already know
18965 that we have two candidates that will provide the actual type. */
18966 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18968 tree substed = TREE_TYPE (decl);
18969 unsigned int i;
18971 tree sarg
18972 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18973 if (return_type)
18974 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18975 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18976 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18978 unify_type_mismatch (explain_p, args[i],
18979 TREE_VALUE (sarg));
18980 goto fail;
18984 /* After doing deduction with the inherited constructor, actually return an
18985 instantiation of the inheriting constructor. */
18986 if (orig_fn != fn)
18987 decl = instantiate_template (orig_fn, targs, complain);
18989 r = decl;
18991 fail:
18992 --deduction_depth;
18993 if (excessive_deduction_depth)
18995 if (deduction_depth == 0)
18996 /* Reset once we're all the way out. */
18997 excessive_deduction_depth = false;
19000 /* We can't free this if a pending_template entry or last_error_tinst_level
19001 is pointing at it. */
19002 if (last_pending_template == old_last_pend
19003 && last_error_tinst_level == old_error_tinst)
19004 ggc_free (tinst);
19006 return r;
19009 /* Adjust types before performing type deduction, as described in
19010 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19011 sections are symmetric. PARM is the type of a function parameter
19012 or the return type of the conversion function. ARG is the type of
19013 the argument passed to the call, or the type of the value
19014 initialized with the result of the conversion function.
19015 ARG_EXPR is the original argument expression, which may be null. */
19017 static int
19018 maybe_adjust_types_for_deduction (unification_kind_t strict,
19019 tree* parm,
19020 tree* arg,
19021 tree arg_expr)
19023 int result = 0;
19025 switch (strict)
19027 case DEDUCE_CALL:
19028 break;
19030 case DEDUCE_CONV:
19031 /* Swap PARM and ARG throughout the remainder of this
19032 function; the handling is precisely symmetric since PARM
19033 will initialize ARG rather than vice versa. */
19034 std::swap (parm, arg);
19035 break;
19037 case DEDUCE_EXACT:
19038 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19039 too, but here handle it by stripping the reference from PARM
19040 rather than by adding it to ARG. */
19041 if (TREE_CODE (*parm) == REFERENCE_TYPE
19042 && TYPE_REF_IS_RVALUE (*parm)
19043 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19044 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19045 && TREE_CODE (*arg) == REFERENCE_TYPE
19046 && !TYPE_REF_IS_RVALUE (*arg))
19047 *parm = TREE_TYPE (*parm);
19048 /* Nothing else to do in this case. */
19049 return 0;
19051 default:
19052 gcc_unreachable ();
19055 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19057 /* [temp.deduct.call]
19059 If P is not a reference type:
19061 --If A is an array type, the pointer type produced by the
19062 array-to-pointer standard conversion (_conv.array_) is
19063 used in place of A for type deduction; otherwise,
19065 --If A is a function type, the pointer type produced by
19066 the function-to-pointer standard conversion
19067 (_conv.func_) is used in place of A for type deduction;
19068 otherwise,
19070 --If A is a cv-qualified type, the top level
19071 cv-qualifiers of A's type are ignored for type
19072 deduction. */
19073 if (TREE_CODE (*arg) == ARRAY_TYPE)
19074 *arg = build_pointer_type (TREE_TYPE (*arg));
19075 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19076 *arg = build_pointer_type (*arg);
19077 else
19078 *arg = TYPE_MAIN_VARIANT (*arg);
19081 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19082 reference to a cv-unqualified template parameter that does not represent a
19083 template parameter of a class template (during class template argument
19084 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19085 an lvalue, the type "lvalue reference to A" is used in place of A for type
19086 deduction. */
19087 if (TREE_CODE (*parm) == REFERENCE_TYPE
19088 && TYPE_REF_IS_RVALUE (*parm)
19089 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19090 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19091 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19092 && (arg_expr ? lvalue_p (arg_expr)
19093 /* try_one_overload doesn't provide an arg_expr, but
19094 functions are always lvalues. */
19095 : TREE_CODE (*arg) == FUNCTION_TYPE))
19096 *arg = build_reference_type (*arg);
19098 /* [temp.deduct.call]
19100 If P is a cv-qualified type, the top level cv-qualifiers
19101 of P's type are ignored for type deduction. If P is a
19102 reference type, the type referred to by P is used for
19103 type deduction. */
19104 *parm = TYPE_MAIN_VARIANT (*parm);
19105 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19107 *parm = TREE_TYPE (*parm);
19108 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19111 /* DR 322. For conversion deduction, remove a reference type on parm
19112 too (which has been swapped into ARG). */
19113 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19114 *arg = TREE_TYPE (*arg);
19116 return result;
19119 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19120 template which does contain any deducible template parameters; check if
19121 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19122 unify_one_argument. */
19124 static int
19125 check_non_deducible_conversion (tree parm, tree arg, int strict,
19126 int flags, bool explain_p)
19128 tree type;
19130 if (!TYPE_P (arg))
19131 type = TREE_TYPE (arg);
19132 else
19133 type = arg;
19135 if (same_type_p (parm, type))
19136 return unify_success (explain_p);
19138 if (strict == DEDUCE_CONV)
19140 if (can_convert_arg (type, parm, NULL_TREE, flags,
19141 explain_p ? tf_warning_or_error : tf_none))
19142 return unify_success (explain_p);
19144 else if (strict != DEDUCE_EXACT)
19146 if (can_convert_arg (parm, type,
19147 TYPE_P (arg) ? NULL_TREE : arg,
19148 flags, explain_p ? tf_warning_or_error : tf_none))
19149 return unify_success (explain_p);
19152 if (strict == DEDUCE_EXACT)
19153 return unify_type_mismatch (explain_p, parm, arg);
19154 else
19155 return unify_arg_conversion (explain_p, parm, type, arg);
19158 static bool uses_deducible_template_parms (tree type);
19160 /* Returns true iff the expression EXPR is one from which a template
19161 argument can be deduced. In other words, if it's an undecorated
19162 use of a template non-type parameter. */
19164 static bool
19165 deducible_expression (tree expr)
19167 /* Strip implicit conversions. */
19168 while (CONVERT_EXPR_P (expr))
19169 expr = TREE_OPERAND (expr, 0);
19170 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19173 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19174 deducible way; that is, if it has a max value of <PARM> - 1. */
19176 static bool
19177 deducible_array_bound (tree domain)
19179 if (domain == NULL_TREE)
19180 return false;
19182 tree max = TYPE_MAX_VALUE (domain);
19183 if (TREE_CODE (max) != MINUS_EXPR)
19184 return false;
19186 return deducible_expression (TREE_OPERAND (max, 0));
19189 /* Returns true iff the template arguments ARGS use a template parameter
19190 in a deducible way. */
19192 static bool
19193 deducible_template_args (tree args)
19195 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19197 bool deducible;
19198 tree elt = TREE_VEC_ELT (args, i);
19199 if (ARGUMENT_PACK_P (elt))
19200 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19201 else
19203 if (PACK_EXPANSION_P (elt))
19204 elt = PACK_EXPANSION_PATTERN (elt);
19205 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19206 deducible = true;
19207 else if (TYPE_P (elt))
19208 deducible = uses_deducible_template_parms (elt);
19209 else
19210 deducible = deducible_expression (elt);
19212 if (deducible)
19213 return true;
19215 return false;
19218 /* Returns true iff TYPE contains any deducible references to template
19219 parameters, as per 14.8.2.5. */
19221 static bool
19222 uses_deducible_template_parms (tree type)
19224 if (PACK_EXPANSION_P (type))
19225 type = PACK_EXPANSION_PATTERN (type);
19227 /* T
19228 cv-list T
19229 TT<T>
19230 TT<i>
19231 TT<> */
19232 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19233 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19234 return true;
19236 /* T*
19238 T&& */
19239 if (POINTER_TYPE_P (type))
19240 return uses_deducible_template_parms (TREE_TYPE (type));
19242 /* T[integer-constant ]
19243 type [i] */
19244 if (TREE_CODE (type) == ARRAY_TYPE)
19245 return (uses_deducible_template_parms (TREE_TYPE (type))
19246 || deducible_array_bound (TYPE_DOMAIN (type)));
19248 /* T type ::*
19249 type T::*
19250 T T::*
19251 T (type ::*)()
19252 type (T::*)()
19253 type (type ::*)(T)
19254 type (T::*)(T)
19255 T (type ::*)(T)
19256 T (T::*)()
19257 T (T::*)(T) */
19258 if (TYPE_PTRMEM_P (type))
19259 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19260 || (uses_deducible_template_parms
19261 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19263 /* template-name <T> (where template-name refers to a class template)
19264 template-name <i> (where template-name refers to a class template) */
19265 if (CLASS_TYPE_P (type)
19266 && CLASSTYPE_TEMPLATE_INFO (type)
19267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19268 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19269 (CLASSTYPE_TI_ARGS (type)));
19271 /* type (T)
19273 T(T) */
19274 if (TREE_CODE (type) == FUNCTION_TYPE
19275 || TREE_CODE (type) == METHOD_TYPE)
19277 if (uses_deducible_template_parms (TREE_TYPE (type)))
19278 return true;
19279 tree parm = TYPE_ARG_TYPES (type);
19280 if (TREE_CODE (type) == METHOD_TYPE)
19281 parm = TREE_CHAIN (parm);
19282 for (; parm; parm = TREE_CHAIN (parm))
19283 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19284 return true;
19287 return false;
19290 /* Subroutine of type_unification_real and unify_pack_expansion to
19291 handle unification of a single P/A pair. Parameters are as
19292 for those functions. */
19294 static int
19295 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19296 int subr, unification_kind_t strict,
19297 bool explain_p)
19299 tree arg_expr = NULL_TREE;
19300 int arg_strict;
19302 if (arg == error_mark_node || parm == error_mark_node)
19303 return unify_invalid (explain_p);
19304 if (arg == unknown_type_node)
19305 /* We can't deduce anything from this, but we might get all the
19306 template args from other function args. */
19307 return unify_success (explain_p);
19309 /* Implicit conversions (Clause 4) will be performed on a function
19310 argument to convert it to the type of the corresponding function
19311 parameter if the parameter type contains no template-parameters that
19312 participate in template argument deduction. */
19313 if (strict != DEDUCE_EXACT
19314 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19315 /* For function parameters with no deducible template parameters,
19316 just return. We'll check non-dependent conversions later. */
19317 return unify_success (explain_p);
19319 switch (strict)
19321 case DEDUCE_CALL:
19322 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19323 | UNIFY_ALLOW_MORE_CV_QUAL
19324 | UNIFY_ALLOW_DERIVED);
19325 break;
19327 case DEDUCE_CONV:
19328 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19329 break;
19331 case DEDUCE_EXACT:
19332 arg_strict = UNIFY_ALLOW_NONE;
19333 break;
19335 default:
19336 gcc_unreachable ();
19339 /* We only do these transformations if this is the top-level
19340 parameter_type_list in a call or declaration matching; in other
19341 situations (nested function declarators, template argument lists) we
19342 won't be comparing a type to an expression, and we don't do any type
19343 adjustments. */
19344 if (!subr)
19346 if (!TYPE_P (arg))
19348 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19349 if (type_unknown_p (arg))
19351 /* [temp.deduct.type] A template-argument can be
19352 deduced from a pointer to function or pointer
19353 to member function argument if the set of
19354 overloaded functions does not contain function
19355 templates and at most one of a set of
19356 overloaded functions provides a unique
19357 match. */
19358 resolve_overloaded_unification (tparms, targs, parm,
19359 arg, strict,
19360 arg_strict, explain_p);
19361 /* If a unique match was not found, this is a
19362 non-deduced context, so we still succeed. */
19363 return unify_success (explain_p);
19366 arg_expr = arg;
19367 arg = unlowered_expr_type (arg);
19368 if (arg == error_mark_node)
19369 return unify_invalid (explain_p);
19372 arg_strict |=
19373 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19375 else
19376 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19377 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19378 return unify_template_argument_mismatch (explain_p, parm, arg);
19380 /* For deduction from an init-list we need the actual list. */
19381 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19382 arg = arg_expr;
19383 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19386 /* for_each_template_parm callback that always returns 0. */
19388 static int
19389 zero_r (tree, void *)
19391 return 0;
19394 /* for_each_template_parm any_fn callback to handle deduction of a template
19395 type argument from the type of an array bound. */
19397 static int
19398 array_deduction_r (tree t, void *data)
19400 tree_pair_p d = (tree_pair_p)data;
19401 tree &tparms = d->purpose;
19402 tree &targs = d->value;
19404 if (TREE_CODE (t) == ARRAY_TYPE)
19405 if (tree dom = TYPE_DOMAIN (t))
19406 if (tree max = TYPE_MAX_VALUE (dom))
19408 if (TREE_CODE (max) == MINUS_EXPR)
19409 max = TREE_OPERAND (max, 0);
19410 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19411 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19412 UNIFY_ALLOW_NONE, /*explain*/false);
19415 /* Keep walking. */
19416 return 0;
19419 /* Try to deduce any not-yet-deduced template type arguments from the type of
19420 an array bound. This is handled separately from unify because 14.8.2.5 says
19421 "The type of a type parameter is only deduced from an array bound if it is
19422 not otherwise deduced." */
19424 static void
19425 try_array_deduction (tree tparms, tree targs, tree parm)
19427 tree_pair_s data = { tparms, targs };
19428 hash_set<tree> visited;
19429 for_each_template_parm (parm, zero_r, &data, &visited,
19430 /*nondeduced*/false, array_deduction_r);
19433 /* Most parms like fn_type_unification.
19435 If SUBR is 1, we're being called recursively (to unify the
19436 arguments of a function or method parameter of a function
19437 template).
19439 CHECKS is a pointer to a vector of access checks encountered while
19440 substituting default template arguments. */
19442 static int
19443 type_unification_real (tree tparms,
19444 tree full_targs,
19445 tree xparms,
19446 const tree *xargs,
19447 unsigned int xnargs,
19448 int subr,
19449 unification_kind_t strict,
19450 int flags,
19451 vec<deferred_access_check, va_gc> **checks,
19452 bool explain_p)
19454 tree parm, arg;
19455 int i;
19456 int ntparms = TREE_VEC_LENGTH (tparms);
19457 int saw_undeduced = 0;
19458 tree parms;
19459 const tree *args;
19460 unsigned int nargs;
19461 unsigned int ia;
19463 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19464 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19465 gcc_assert (ntparms > 0);
19467 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19469 /* Reset the number of non-defaulted template arguments contained
19470 in TARGS. */
19471 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19473 again:
19474 parms = xparms;
19475 args = xargs;
19476 nargs = xnargs;
19478 ia = 0;
19479 while (parms && parms != void_list_node
19480 && ia < nargs)
19482 parm = TREE_VALUE (parms);
19484 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19485 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19486 /* For a function parameter pack that occurs at the end of the
19487 parameter-declaration-list, the type A of each remaining
19488 argument of the call is compared with the type P of the
19489 declarator-id of the function parameter pack. */
19490 break;
19492 parms = TREE_CHAIN (parms);
19494 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19495 /* For a function parameter pack that does not occur at the
19496 end of the parameter-declaration-list, the type of the
19497 parameter pack is a non-deduced context. */
19498 continue;
19500 arg = args[ia];
19501 ++ia;
19503 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19504 explain_p))
19505 return 1;
19508 if (parms
19509 && parms != void_list_node
19510 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19512 /* Unify the remaining arguments with the pack expansion type. */
19513 tree argvec;
19514 tree parmvec = make_tree_vec (1);
19516 /* Allocate a TREE_VEC and copy in all of the arguments */
19517 argvec = make_tree_vec (nargs - ia);
19518 for (i = 0; ia < nargs; ++ia, ++i)
19519 TREE_VEC_ELT (argvec, i) = args[ia];
19521 /* Copy the parameter into parmvec. */
19522 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19523 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19524 /*subr=*/subr, explain_p))
19525 return 1;
19527 /* Advance to the end of the list of parameters. */
19528 parms = TREE_CHAIN (parms);
19531 /* Fail if we've reached the end of the parm list, and more args
19532 are present, and the parm list isn't variadic. */
19533 if (ia < nargs && parms == void_list_node)
19534 return unify_too_many_arguments (explain_p, nargs, ia);
19535 /* Fail if parms are left and they don't have default values and
19536 they aren't all deduced as empty packs (c++/57397). This is
19537 consistent with sufficient_parms_p. */
19538 if (parms && parms != void_list_node
19539 && TREE_PURPOSE (parms) == NULL_TREE)
19541 unsigned int count = nargs;
19542 tree p = parms;
19543 bool type_pack_p;
19546 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19547 if (!type_pack_p)
19548 count++;
19549 p = TREE_CHAIN (p);
19551 while (p && p != void_list_node);
19552 if (count != nargs)
19553 return unify_too_few_arguments (explain_p, ia, count,
19554 type_pack_p);
19557 if (!subr)
19559 tsubst_flags_t complain = (explain_p
19560 ? tf_warning_or_error
19561 : tf_none);
19562 bool tried_array_deduction = (cxx_dialect < cxx17);
19564 for (i = 0; i < ntparms; i++)
19566 tree targ = TREE_VEC_ELT (targs, i);
19567 tree tparm = TREE_VEC_ELT (tparms, i);
19569 /* Clear the "incomplete" flags on all argument packs now so that
19570 substituting them into later default arguments works. */
19571 if (targ && ARGUMENT_PACK_P (targ))
19573 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19574 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19577 if (targ || tparm == error_mark_node)
19578 continue;
19579 tparm = TREE_VALUE (tparm);
19581 if (TREE_CODE (tparm) == TYPE_DECL
19582 && !tried_array_deduction)
19584 try_array_deduction (tparms, targs, xparms);
19585 tried_array_deduction = true;
19586 if (TREE_VEC_ELT (targs, i))
19587 continue;
19590 /* If this is an undeduced nontype parameter that depends on
19591 a type parameter, try another pass; its type may have been
19592 deduced from a later argument than the one from which
19593 this parameter can be deduced. */
19594 if (TREE_CODE (tparm) == PARM_DECL
19595 && uses_template_parms (TREE_TYPE (tparm))
19596 && saw_undeduced < 2)
19598 saw_undeduced = 1;
19599 continue;
19602 /* Core issue #226 (C++0x) [temp.deduct]:
19604 If a template argument has not been deduced, its
19605 default template argument, if any, is used.
19607 When we are in C++98 mode, TREE_PURPOSE will either
19608 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19609 to explicitly check cxx_dialect here. */
19610 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19611 /* OK, there is a default argument. Wait until after the
19612 conversion check to do substitution. */
19613 continue;
19615 /* If the type parameter is a parameter pack, then it will
19616 be deduced to an empty parameter pack. */
19617 if (template_parameter_pack_p (tparm))
19619 tree arg;
19621 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19623 arg = make_node (NONTYPE_ARGUMENT_PACK);
19624 TREE_CONSTANT (arg) = 1;
19626 else
19627 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19629 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19631 TREE_VEC_ELT (targs, i) = arg;
19632 continue;
19635 return unify_parameter_deduction_failure (explain_p, tparm);
19638 /* DR 1391: All parameters have args, now check non-dependent parms for
19639 convertibility. */
19640 if (saw_undeduced < 2)
19641 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19642 parms && parms != void_list_node && ia < nargs; )
19644 parm = TREE_VALUE (parms);
19646 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19647 && (!TREE_CHAIN (parms)
19648 || TREE_CHAIN (parms) == void_list_node))
19649 /* For a function parameter pack that occurs at the end of the
19650 parameter-declaration-list, the type A of each remaining
19651 argument of the call is compared with the type P of the
19652 declarator-id of the function parameter pack. */
19653 break;
19655 parms = TREE_CHAIN (parms);
19657 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19658 /* For a function parameter pack that does not occur at the
19659 end of the parameter-declaration-list, the type of the
19660 parameter pack is a non-deduced context. */
19661 continue;
19663 arg = args[ia];
19664 ++ia;
19666 if (uses_template_parms (parm))
19667 continue;
19668 if (check_non_deducible_conversion (parm, arg, strict, flags,
19669 explain_p))
19670 return 1;
19673 /* Now substitute into the default template arguments. */
19674 for (i = 0; i < ntparms; i++)
19676 tree targ = TREE_VEC_ELT (targs, i);
19677 tree tparm = TREE_VEC_ELT (tparms, i);
19679 if (targ || tparm == error_mark_node)
19680 continue;
19681 tree parm = TREE_VALUE (tparm);
19683 if (TREE_CODE (parm) == PARM_DECL
19684 && uses_template_parms (TREE_TYPE (parm))
19685 && saw_undeduced < 2)
19686 continue;
19688 tree arg = TREE_PURPOSE (tparm);
19689 reopen_deferring_access_checks (*checks);
19690 location_t save_loc = input_location;
19691 if (DECL_P (parm))
19692 input_location = DECL_SOURCE_LOCATION (parm);
19693 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19694 if (!uses_template_parms (arg))
19695 arg = convert_template_argument (parm, arg, full_targs, complain,
19696 i, NULL_TREE);
19697 else if (saw_undeduced < 2)
19698 arg = NULL_TREE;
19699 else
19700 arg = error_mark_node;
19701 input_location = save_loc;
19702 *checks = get_deferred_access_checks ();
19703 pop_deferring_access_checks ();
19704 if (arg == error_mark_node)
19705 return 1;
19706 else if (arg)
19708 TREE_VEC_ELT (targs, i) = arg;
19709 /* The position of the first default template argument,
19710 is also the number of non-defaulted arguments in TARGS.
19711 Record that. */
19712 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19713 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19717 if (saw_undeduced++ == 1)
19718 goto again;
19721 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19722 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19724 return unify_success (explain_p);
19727 /* Subroutine of type_unification_real. Args are like the variables
19728 at the call site. ARG is an overloaded function (or template-id);
19729 we try deducing template args from each of the overloads, and if
19730 only one succeeds, we go with that. Modifies TARGS and returns
19731 true on success. */
19733 static bool
19734 resolve_overloaded_unification (tree tparms,
19735 tree targs,
19736 tree parm,
19737 tree arg,
19738 unification_kind_t strict,
19739 int sub_strict,
19740 bool explain_p)
19742 tree tempargs = copy_node (targs);
19743 int good = 0;
19744 tree goodfn = NULL_TREE;
19745 bool addr_p;
19747 if (TREE_CODE (arg) == ADDR_EXPR)
19749 arg = TREE_OPERAND (arg, 0);
19750 addr_p = true;
19752 else
19753 addr_p = false;
19755 if (TREE_CODE (arg) == COMPONENT_REF)
19756 /* Handle `&x' where `x' is some static or non-static member
19757 function name. */
19758 arg = TREE_OPERAND (arg, 1);
19760 if (TREE_CODE (arg) == OFFSET_REF)
19761 arg = TREE_OPERAND (arg, 1);
19763 /* Strip baselink information. */
19764 if (BASELINK_P (arg))
19765 arg = BASELINK_FUNCTIONS (arg);
19767 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19769 /* If we got some explicit template args, we need to plug them into
19770 the affected templates before we try to unify, in case the
19771 explicit args will completely resolve the templates in question. */
19773 int ok = 0;
19774 tree expl_subargs = TREE_OPERAND (arg, 1);
19775 arg = TREE_OPERAND (arg, 0);
19777 for (lkp_iterator iter (arg); iter; ++iter)
19779 tree fn = *iter;
19780 tree subargs, elem;
19782 if (TREE_CODE (fn) != TEMPLATE_DECL)
19783 continue;
19785 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19786 expl_subargs, NULL_TREE, tf_none,
19787 /*require_all_args=*/true,
19788 /*use_default_args=*/true);
19789 if (subargs != error_mark_node
19790 && !any_dependent_template_arguments_p (subargs))
19792 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19793 if (try_one_overload (tparms, targs, tempargs, parm,
19794 elem, strict, sub_strict, addr_p, explain_p)
19795 && (!goodfn || !same_type_p (goodfn, elem)))
19797 goodfn = elem;
19798 ++good;
19801 else if (subargs)
19802 ++ok;
19804 /* If no templates (or more than one) are fully resolved by the
19805 explicit arguments, this template-id is a non-deduced context; it
19806 could still be OK if we deduce all template arguments for the
19807 enclosing call through other arguments. */
19808 if (good != 1)
19809 good = ok;
19811 else if (TREE_CODE (arg) != OVERLOAD
19812 && TREE_CODE (arg) != FUNCTION_DECL)
19813 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19814 -- but the deduction does not succeed because the expression is
19815 not just the function on its own. */
19816 return false;
19817 else
19818 for (lkp_iterator iter (arg); iter; ++iter)
19820 tree fn = *iter;
19821 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19822 strict, sub_strict, addr_p, explain_p)
19823 && (!goodfn || !decls_match (goodfn, fn)))
19825 goodfn = fn;
19826 ++good;
19830 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19831 to function or pointer to member function argument if the set of
19832 overloaded functions does not contain function templates and at most
19833 one of a set of overloaded functions provides a unique match.
19835 So if we found multiple possibilities, we return success but don't
19836 deduce anything. */
19838 if (good == 1)
19840 int i = TREE_VEC_LENGTH (targs);
19841 for (; i--; )
19842 if (TREE_VEC_ELT (tempargs, i))
19844 tree old = TREE_VEC_ELT (targs, i);
19845 tree new_ = TREE_VEC_ELT (tempargs, i);
19846 if (new_ && old && ARGUMENT_PACK_P (old)
19847 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19848 /* Don't forget explicit template arguments in a pack. */
19849 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19850 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19851 TREE_VEC_ELT (targs, i) = new_;
19854 if (good)
19855 return true;
19857 return false;
19860 /* Core DR 115: In contexts where deduction is done and fails, or in
19861 contexts where deduction is not done, if a template argument list is
19862 specified and it, along with any default template arguments, identifies
19863 a single function template specialization, then the template-id is an
19864 lvalue for the function template specialization. */
19866 tree
19867 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19869 tree expr, offset, baselink;
19870 bool addr;
19872 if (!type_unknown_p (orig_expr))
19873 return orig_expr;
19875 expr = orig_expr;
19876 addr = false;
19877 offset = NULL_TREE;
19878 baselink = NULL_TREE;
19880 if (TREE_CODE (expr) == ADDR_EXPR)
19882 expr = TREE_OPERAND (expr, 0);
19883 addr = true;
19885 if (TREE_CODE (expr) == OFFSET_REF)
19887 offset = expr;
19888 expr = TREE_OPERAND (expr, 1);
19890 if (BASELINK_P (expr))
19892 baselink = expr;
19893 expr = BASELINK_FUNCTIONS (expr);
19896 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19898 int good = 0;
19899 tree goodfn = NULL_TREE;
19901 /* If we got some explicit template args, we need to plug them into
19902 the affected templates before we try to unify, in case the
19903 explicit args will completely resolve the templates in question. */
19905 tree expl_subargs = TREE_OPERAND (expr, 1);
19906 tree arg = TREE_OPERAND (expr, 0);
19907 tree badfn = NULL_TREE;
19908 tree badargs = NULL_TREE;
19910 for (lkp_iterator iter (arg); iter; ++iter)
19912 tree fn = *iter;
19913 tree subargs, elem;
19915 if (TREE_CODE (fn) != TEMPLATE_DECL)
19916 continue;
19918 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19919 expl_subargs, NULL_TREE, tf_none,
19920 /*require_all_args=*/true,
19921 /*use_default_args=*/true);
19922 if (subargs != error_mark_node
19923 && !any_dependent_template_arguments_p (subargs))
19925 elem = instantiate_template (fn, subargs, tf_none);
19926 if (elem == error_mark_node)
19928 badfn = fn;
19929 badargs = subargs;
19931 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19933 goodfn = elem;
19934 ++good;
19938 if (good == 1)
19940 mark_used (goodfn);
19941 expr = goodfn;
19942 if (baselink)
19943 expr = build_baselink (BASELINK_BINFO (baselink),
19944 BASELINK_ACCESS_BINFO (baselink),
19945 expr, BASELINK_OPTYPE (baselink));
19946 if (offset)
19948 tree base
19949 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19950 expr = build_offset_ref (base, expr, addr, complain);
19952 if (addr)
19953 expr = cp_build_addr_expr (expr, complain);
19954 return expr;
19956 else if (good == 0 && badargs && (complain & tf_error))
19957 /* There were no good options and at least one bad one, so let the
19958 user know what the problem is. */
19959 instantiate_template (badfn, badargs, complain);
19961 return orig_expr;
19964 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19965 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19966 different overloads deduce different arguments for a given parm.
19967 ADDR_P is true if the expression for which deduction is being
19968 performed was of the form "& fn" rather than simply "fn".
19970 Returns 1 on success. */
19972 static int
19973 try_one_overload (tree tparms,
19974 tree orig_targs,
19975 tree targs,
19976 tree parm,
19977 tree arg,
19978 unification_kind_t strict,
19979 int sub_strict,
19980 bool addr_p,
19981 bool explain_p)
19983 int nargs;
19984 tree tempargs;
19985 int i;
19987 if (arg == error_mark_node)
19988 return 0;
19990 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19991 to function or pointer to member function argument if the set of
19992 overloaded functions does not contain function templates and at most
19993 one of a set of overloaded functions provides a unique match.
19995 So if this is a template, just return success. */
19997 if (uses_template_parms (arg))
19998 return 1;
20000 if (TREE_CODE (arg) == METHOD_TYPE)
20001 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20002 else if (addr_p)
20003 arg = build_pointer_type (arg);
20005 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20007 /* We don't copy orig_targs for this because if we have already deduced
20008 some template args from previous args, unify would complain when we
20009 try to deduce a template parameter for the same argument, even though
20010 there isn't really a conflict. */
20011 nargs = TREE_VEC_LENGTH (targs);
20012 tempargs = make_tree_vec (nargs);
20014 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20015 return 0;
20017 /* First make sure we didn't deduce anything that conflicts with
20018 explicitly specified args. */
20019 for (i = nargs; i--; )
20021 tree elt = TREE_VEC_ELT (tempargs, i);
20022 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20024 if (!elt)
20025 /*NOP*/;
20026 else if (uses_template_parms (elt))
20027 /* Since we're unifying against ourselves, we will fill in
20028 template args used in the function parm list with our own
20029 template parms. Discard them. */
20030 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20031 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20033 /* Check that the argument at each index of the deduced argument pack
20034 is equivalent to the corresponding explicitly specified argument.
20035 We may have deduced more arguments than were explicitly specified,
20036 and that's OK. */
20038 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20039 that's wrong if we deduce the same argument pack from multiple
20040 function arguments: it's only incomplete the first time. */
20042 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20043 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20045 if (TREE_VEC_LENGTH (deduced_pack)
20046 < TREE_VEC_LENGTH (explicit_pack))
20047 return 0;
20049 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20050 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20051 TREE_VEC_ELT (deduced_pack, j)))
20052 return 0;
20054 else if (oldelt && !template_args_equal (oldelt, elt))
20055 return 0;
20058 for (i = nargs; i--; )
20060 tree elt = TREE_VEC_ELT (tempargs, i);
20062 if (elt)
20063 TREE_VEC_ELT (targs, i) = elt;
20066 return 1;
20069 /* PARM is a template class (perhaps with unbound template
20070 parameters). ARG is a fully instantiated type. If ARG can be
20071 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20072 TARGS are as for unify. */
20074 static tree
20075 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20076 bool explain_p)
20078 tree copy_of_targs;
20080 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20081 return NULL_TREE;
20082 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20083 /* Matches anything. */;
20084 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20085 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20086 return NULL_TREE;
20088 /* We need to make a new template argument vector for the call to
20089 unify. If we used TARGS, we'd clutter it up with the result of
20090 the attempted unification, even if this class didn't work out.
20091 We also don't want to commit ourselves to all the unifications
20092 we've already done, since unification is supposed to be done on
20093 an argument-by-argument basis. In other words, consider the
20094 following pathological case:
20096 template <int I, int J, int K>
20097 struct S {};
20099 template <int I, int J>
20100 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20102 template <int I, int J, int K>
20103 void f(S<I, J, K>, S<I, I, I>);
20105 void g() {
20106 S<0, 0, 0> s0;
20107 S<0, 1, 2> s2;
20109 f(s0, s2);
20112 Now, by the time we consider the unification involving `s2', we
20113 already know that we must have `f<0, 0, 0>'. But, even though
20114 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20115 because there are two ways to unify base classes of S<0, 1, 2>
20116 with S<I, I, I>. If we kept the already deduced knowledge, we
20117 would reject the possibility I=1. */
20118 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20120 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20122 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20123 return NULL_TREE;
20124 return arg;
20127 /* If unification failed, we're done. */
20128 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20129 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20130 return NULL_TREE;
20132 return arg;
20135 /* Given a template type PARM and a class type ARG, find the unique
20136 base type in ARG that is an instance of PARM. We do not examine
20137 ARG itself; only its base-classes. If there is not exactly one
20138 appropriate base class, return NULL_TREE. PARM may be the type of
20139 a partial specialization, as well as a plain template type. Used
20140 by unify. */
20142 static enum template_base_result
20143 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20144 bool explain_p, tree *result)
20146 tree rval = NULL_TREE;
20147 tree binfo;
20149 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20151 binfo = TYPE_BINFO (complete_type (arg));
20152 if (!binfo)
20154 /* The type could not be completed. */
20155 *result = NULL_TREE;
20156 return tbr_incomplete_type;
20159 /* Walk in inheritance graph order. The search order is not
20160 important, and this avoids multiple walks of virtual bases. */
20161 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20163 tree r = try_class_unification (tparms, targs, parm,
20164 BINFO_TYPE (binfo), explain_p);
20166 if (r)
20168 /* If there is more than one satisfactory baseclass, then:
20170 [temp.deduct.call]
20172 If they yield more than one possible deduced A, the type
20173 deduction fails.
20175 applies. */
20176 if (rval && !same_type_p (r, rval))
20178 *result = NULL_TREE;
20179 return tbr_ambiguous_baseclass;
20182 rval = r;
20186 *result = rval;
20187 return tbr_success;
20190 /* Returns the level of DECL, which declares a template parameter. */
20192 static int
20193 template_decl_level (tree decl)
20195 switch (TREE_CODE (decl))
20197 case TYPE_DECL:
20198 case TEMPLATE_DECL:
20199 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20201 case PARM_DECL:
20202 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20204 default:
20205 gcc_unreachable ();
20207 return 0;
20210 /* Decide whether ARG can be unified with PARM, considering only the
20211 cv-qualifiers of each type, given STRICT as documented for unify.
20212 Returns nonzero iff the unification is OK on that basis. */
20214 static int
20215 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20217 int arg_quals = cp_type_quals (arg);
20218 int parm_quals = cp_type_quals (parm);
20220 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20221 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20223 /* Although a CVR qualifier is ignored when being applied to a
20224 substituted template parameter ([8.3.2]/1 for example), that
20225 does not allow us to unify "const T" with "int&" because both
20226 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20227 It is ok when we're allowing additional CV qualifiers
20228 at the outer level [14.8.2.1]/3,1st bullet. */
20229 if ((TREE_CODE (arg) == REFERENCE_TYPE
20230 || TREE_CODE (arg) == FUNCTION_TYPE
20231 || TREE_CODE (arg) == METHOD_TYPE)
20232 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20233 return 0;
20235 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20236 && (parm_quals & TYPE_QUAL_RESTRICT))
20237 return 0;
20240 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20241 && (arg_quals & parm_quals) != parm_quals)
20242 return 0;
20244 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20245 && (parm_quals & arg_quals) != arg_quals)
20246 return 0;
20248 return 1;
20251 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20252 void
20253 template_parm_level_and_index (tree parm, int* level, int* index)
20255 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20256 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20257 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20259 *index = TEMPLATE_TYPE_IDX (parm);
20260 *level = TEMPLATE_TYPE_LEVEL (parm);
20262 else
20264 *index = TEMPLATE_PARM_IDX (parm);
20265 *level = TEMPLATE_PARM_LEVEL (parm);
20269 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20270 do { \
20271 if (unify (TP, TA, P, A, S, EP)) \
20272 return 1; \
20273 } while (0)
20275 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20276 expansion at the end of PACKED_PARMS. Returns 0 if the type
20277 deduction succeeds, 1 otherwise. STRICT is the same as in
20278 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20279 function call argument list. We'll need to adjust the arguments to make them
20280 types. SUBR tells us if this is from a recursive call to
20281 type_unification_real, or for comparing two template argument
20282 lists. */
20284 static int
20285 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20286 tree packed_args, unification_kind_t strict,
20287 bool subr, bool explain_p)
20289 tree parm
20290 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20291 tree pattern = PACK_EXPANSION_PATTERN (parm);
20292 tree pack, packs = NULL_TREE;
20293 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20295 /* Add in any args remembered from an earlier partial instantiation. */
20296 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20298 packed_args = expand_template_argument_pack (packed_args);
20300 int len = TREE_VEC_LENGTH (packed_args);
20302 /* Determine the parameter packs we will be deducing from the
20303 pattern, and record their current deductions. */
20304 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20305 pack; pack = TREE_CHAIN (pack))
20307 tree parm_pack = TREE_VALUE (pack);
20308 int idx, level;
20310 /* Determine the index and level of this parameter pack. */
20311 template_parm_level_and_index (parm_pack, &level, &idx);
20313 /* Keep track of the parameter packs and their corresponding
20314 argument packs. */
20315 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20316 TREE_TYPE (packs) = make_tree_vec (len - start);
20319 /* Loop through all of the arguments that have not yet been
20320 unified and unify each with the pattern. */
20321 for (i = start; i < len; i++)
20323 tree parm;
20324 bool any_explicit = false;
20325 tree arg = TREE_VEC_ELT (packed_args, i);
20327 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20328 or the element of its argument pack at the current index if
20329 this argument was explicitly specified. */
20330 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20332 int idx, level;
20333 tree arg, pargs;
20334 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20336 arg = NULL_TREE;
20337 if (TREE_VALUE (pack)
20338 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20339 && (i - start < TREE_VEC_LENGTH (pargs)))
20341 any_explicit = true;
20342 arg = TREE_VEC_ELT (pargs, i - start);
20344 TMPL_ARG (targs, level, idx) = arg;
20347 /* If we had explicit template arguments, substitute them into the
20348 pattern before deduction. */
20349 if (any_explicit)
20351 /* Some arguments might still be unspecified or dependent. */
20352 bool dependent;
20353 ++processing_template_decl;
20354 dependent = any_dependent_template_arguments_p (targs);
20355 if (!dependent)
20356 --processing_template_decl;
20357 parm = tsubst (pattern, targs,
20358 explain_p ? tf_warning_or_error : tf_none,
20359 NULL_TREE);
20360 if (dependent)
20361 --processing_template_decl;
20362 if (parm == error_mark_node)
20363 return 1;
20365 else
20366 parm = pattern;
20368 /* Unify the pattern with the current argument. */
20369 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20370 explain_p))
20371 return 1;
20373 /* For each parameter pack, collect the deduced value. */
20374 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20376 int idx, level;
20377 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20379 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20380 TMPL_ARG (targs, level, idx);
20384 /* Verify that the results of unification with the parameter packs
20385 produce results consistent with what we've seen before, and make
20386 the deduced argument packs available. */
20387 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20389 tree old_pack = TREE_VALUE (pack);
20390 tree new_args = TREE_TYPE (pack);
20391 int i, len = TREE_VEC_LENGTH (new_args);
20392 int idx, level;
20393 bool nondeduced_p = false;
20395 /* By default keep the original deduced argument pack.
20396 If necessary, more specific code is going to update the
20397 resulting deduced argument later down in this function. */
20398 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20399 TMPL_ARG (targs, level, idx) = old_pack;
20401 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20402 actually deduce anything. */
20403 for (i = 0; i < len && !nondeduced_p; ++i)
20404 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20405 nondeduced_p = true;
20406 if (nondeduced_p)
20407 continue;
20409 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20411 /* If we had fewer function args than explicit template args,
20412 just use the explicits. */
20413 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20414 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20415 if (len < explicit_len)
20416 new_args = explicit_args;
20419 if (!old_pack)
20421 tree result;
20422 /* Build the deduced *_ARGUMENT_PACK. */
20423 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20425 result = make_node (NONTYPE_ARGUMENT_PACK);
20426 TREE_CONSTANT (result) = 1;
20428 else
20429 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20431 SET_ARGUMENT_PACK_ARGS (result, new_args);
20433 /* Note the deduced argument packs for this parameter
20434 pack. */
20435 TMPL_ARG (targs, level, idx) = result;
20437 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20438 && (ARGUMENT_PACK_ARGS (old_pack)
20439 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20441 /* We only had the explicitly-provided arguments before, but
20442 now we have a complete set of arguments. */
20443 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20445 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20446 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20447 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20449 else
20451 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20452 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20454 if (!comp_template_args (old_args, new_args,
20455 &bad_old_arg, &bad_new_arg))
20456 /* Inconsistent unification of this parameter pack. */
20457 return unify_parameter_pack_inconsistent (explain_p,
20458 bad_old_arg,
20459 bad_new_arg);
20463 return unify_success (explain_p);
20466 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20467 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20468 parameters and return value are as for unify. */
20470 static int
20471 unify_array_domain (tree tparms, tree targs,
20472 tree parm_dom, tree arg_dom,
20473 bool explain_p)
20475 tree parm_max;
20476 tree arg_max;
20477 bool parm_cst;
20478 bool arg_cst;
20480 /* Our representation of array types uses "N - 1" as the
20481 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20482 not an integer constant. We cannot unify arbitrarily
20483 complex expressions, so we eliminate the MINUS_EXPRs
20484 here. */
20485 parm_max = TYPE_MAX_VALUE (parm_dom);
20486 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20487 if (!parm_cst)
20489 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20490 parm_max = TREE_OPERAND (parm_max, 0);
20492 arg_max = TYPE_MAX_VALUE (arg_dom);
20493 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20494 if (!arg_cst)
20496 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20497 trying to unify the type of a variable with the type
20498 of a template parameter. For example:
20500 template <unsigned int N>
20501 void f (char (&) [N]);
20502 int g();
20503 void h(int i) {
20504 char a[g(i)];
20505 f(a);
20508 Here, the type of the ARG will be "int [g(i)]", and
20509 may be a SAVE_EXPR, etc. */
20510 if (TREE_CODE (arg_max) != MINUS_EXPR)
20511 return unify_vla_arg (explain_p, arg_dom);
20512 arg_max = TREE_OPERAND (arg_max, 0);
20515 /* If only one of the bounds used a MINUS_EXPR, compensate
20516 by adding one to the other bound. */
20517 if (parm_cst && !arg_cst)
20518 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20519 integer_type_node,
20520 parm_max,
20521 integer_one_node);
20522 else if (arg_cst && !parm_cst)
20523 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20524 integer_type_node,
20525 arg_max,
20526 integer_one_node);
20528 return unify (tparms, targs, parm_max, arg_max,
20529 UNIFY_ALLOW_INTEGER, explain_p);
20532 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20534 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20536 static pa_kind_t
20537 pa_kind (tree t)
20539 if (PACK_EXPANSION_P (t))
20540 t = PACK_EXPANSION_PATTERN (t);
20541 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20542 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20543 || DECL_TYPE_TEMPLATE_P (t))
20544 return pa_tmpl;
20545 else if (TYPE_P (t))
20546 return pa_type;
20547 else
20548 return pa_expr;
20551 /* Deduce the value of template parameters. TPARMS is the (innermost)
20552 set of template parameters to a template. TARGS is the bindings
20553 for those template parameters, as determined thus far; TARGS may
20554 include template arguments for outer levels of template parameters
20555 as well. PARM is a parameter to a template function, or a
20556 subcomponent of that parameter; ARG is the corresponding argument.
20557 This function attempts to match PARM with ARG in a manner
20558 consistent with the existing assignments in TARGS. If more values
20559 are deduced, then TARGS is updated.
20561 Returns 0 if the type deduction succeeds, 1 otherwise. The
20562 parameter STRICT is a bitwise or of the following flags:
20564 UNIFY_ALLOW_NONE:
20565 Require an exact match between PARM and ARG.
20566 UNIFY_ALLOW_MORE_CV_QUAL:
20567 Allow the deduced ARG to be more cv-qualified (by qualification
20568 conversion) than ARG.
20569 UNIFY_ALLOW_LESS_CV_QUAL:
20570 Allow the deduced ARG to be less cv-qualified than ARG.
20571 UNIFY_ALLOW_DERIVED:
20572 Allow the deduced ARG to be a template base class of ARG,
20573 or a pointer to a template base class of the type pointed to by
20574 ARG.
20575 UNIFY_ALLOW_INTEGER:
20576 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20577 case for more information.
20578 UNIFY_ALLOW_OUTER_LEVEL:
20579 This is the outermost level of a deduction. Used to determine validity
20580 of qualification conversions. A valid qualification conversion must
20581 have const qualified pointers leading up to the inner type which
20582 requires additional CV quals, except at the outer level, where const
20583 is not required [conv.qual]. It would be normal to set this flag in
20584 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20585 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20586 This is the outermost level of a deduction, and PARM can be more CV
20587 qualified at this point.
20588 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20589 This is the outermost level of a deduction, and PARM can be less CV
20590 qualified at this point. */
20592 static int
20593 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20594 bool explain_p)
20596 int idx;
20597 tree targ;
20598 tree tparm;
20599 int strict_in = strict;
20600 tsubst_flags_t complain = (explain_p
20601 ? tf_warning_or_error
20602 : tf_none);
20604 /* I don't think this will do the right thing with respect to types.
20605 But the only case I've seen it in so far has been array bounds, where
20606 signedness is the only information lost, and I think that will be
20607 okay. */
20608 while (CONVERT_EXPR_P (parm))
20609 parm = TREE_OPERAND (parm, 0);
20611 if (arg == error_mark_node)
20612 return unify_invalid (explain_p);
20613 if (arg == unknown_type_node
20614 || arg == init_list_type_node)
20615 /* We can't deduce anything from this, but we might get all the
20616 template args from other function args. */
20617 return unify_success (explain_p);
20619 if (parm == any_targ_node || arg == any_targ_node)
20620 return unify_success (explain_p);
20622 /* If PARM uses template parameters, then we can't bail out here,
20623 even if ARG == PARM, since we won't record unifications for the
20624 template parameters. We might need them if we're trying to
20625 figure out which of two things is more specialized. */
20626 if (arg == parm && !uses_template_parms (parm))
20627 return unify_success (explain_p);
20629 /* Handle init lists early, so the rest of the function can assume
20630 we're dealing with a type. */
20631 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20633 tree elt, elttype;
20634 unsigned i;
20635 tree orig_parm = parm;
20637 /* Replace T with std::initializer_list<T> for deduction. */
20638 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20639 && flag_deduce_init_list)
20640 parm = listify (parm);
20642 if (!is_std_init_list (parm)
20643 && TREE_CODE (parm) != ARRAY_TYPE)
20644 /* We can only deduce from an initializer list argument if the
20645 parameter is std::initializer_list or an array; otherwise this
20646 is a non-deduced context. */
20647 return unify_success (explain_p);
20649 if (TREE_CODE (parm) == ARRAY_TYPE)
20650 elttype = TREE_TYPE (parm);
20651 else
20653 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20654 /* Deduction is defined in terms of a single type, so just punt
20655 on the (bizarre) std::initializer_list<T...>. */
20656 if (PACK_EXPANSION_P (elttype))
20657 return unify_success (explain_p);
20660 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20662 int elt_strict = strict;
20664 if (elt == error_mark_node)
20665 return unify_invalid (explain_p);
20667 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20669 tree type = TREE_TYPE (elt);
20670 if (type == error_mark_node)
20671 return unify_invalid (explain_p);
20672 /* It should only be possible to get here for a call. */
20673 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20674 elt_strict |= maybe_adjust_types_for_deduction
20675 (DEDUCE_CALL, &elttype, &type, elt);
20676 elt = type;
20679 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20680 explain_p);
20683 if (TREE_CODE (parm) == ARRAY_TYPE
20684 && deducible_array_bound (TYPE_DOMAIN (parm)))
20686 /* Also deduce from the length of the initializer list. */
20687 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20688 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20689 if (idx == error_mark_node)
20690 return unify_invalid (explain_p);
20691 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20692 idx, explain_p);
20695 /* If the std::initializer_list<T> deduction worked, replace the
20696 deduced A with std::initializer_list<A>. */
20697 if (orig_parm != parm)
20699 idx = TEMPLATE_TYPE_IDX (orig_parm);
20700 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20701 targ = listify (targ);
20702 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20704 return unify_success (explain_p);
20707 /* If parm and arg aren't the same kind of thing (template, type, or
20708 expression), fail early. */
20709 if (pa_kind (parm) != pa_kind (arg))
20710 return unify_invalid (explain_p);
20712 /* Immediately reject some pairs that won't unify because of
20713 cv-qualification mismatches. */
20714 if (TREE_CODE (arg) == TREE_CODE (parm)
20715 && TYPE_P (arg)
20716 /* It is the elements of the array which hold the cv quals of an array
20717 type, and the elements might be template type parms. We'll check
20718 when we recurse. */
20719 && TREE_CODE (arg) != ARRAY_TYPE
20720 /* We check the cv-qualifiers when unifying with template type
20721 parameters below. We want to allow ARG `const T' to unify with
20722 PARM `T' for example, when computing which of two templates
20723 is more specialized, for example. */
20724 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20725 && !check_cv_quals_for_unify (strict_in, arg, parm))
20726 return unify_cv_qual_mismatch (explain_p, parm, arg);
20728 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20729 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20730 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20731 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20732 strict &= ~UNIFY_ALLOW_DERIVED;
20733 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20734 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20736 switch (TREE_CODE (parm))
20738 case TYPENAME_TYPE:
20739 case SCOPE_REF:
20740 case UNBOUND_CLASS_TEMPLATE:
20741 /* In a type which contains a nested-name-specifier, template
20742 argument values cannot be deduced for template parameters used
20743 within the nested-name-specifier. */
20744 return unify_success (explain_p);
20746 case TEMPLATE_TYPE_PARM:
20747 case TEMPLATE_TEMPLATE_PARM:
20748 case BOUND_TEMPLATE_TEMPLATE_PARM:
20749 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20750 if (error_operand_p (tparm))
20751 return unify_invalid (explain_p);
20753 if (TEMPLATE_TYPE_LEVEL (parm)
20754 != template_decl_level (tparm))
20755 /* The PARM is not one we're trying to unify. Just check
20756 to see if it matches ARG. */
20758 if (TREE_CODE (arg) == TREE_CODE (parm)
20759 && (is_auto (parm) ? is_auto (arg)
20760 : same_type_p (parm, arg)))
20761 return unify_success (explain_p);
20762 else
20763 return unify_type_mismatch (explain_p, parm, arg);
20765 idx = TEMPLATE_TYPE_IDX (parm);
20766 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20767 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20768 if (error_operand_p (tparm))
20769 return unify_invalid (explain_p);
20771 /* Check for mixed types and values. */
20772 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20773 && TREE_CODE (tparm) != TYPE_DECL)
20774 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20775 && TREE_CODE (tparm) != TEMPLATE_DECL))
20776 gcc_unreachable ();
20778 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20780 if ((strict_in & UNIFY_ALLOW_DERIVED)
20781 && CLASS_TYPE_P (arg))
20783 /* First try to match ARG directly. */
20784 tree t = try_class_unification (tparms, targs, parm, arg,
20785 explain_p);
20786 if (!t)
20788 /* Otherwise, look for a suitable base of ARG, as below. */
20789 enum template_base_result r;
20790 r = get_template_base (tparms, targs, parm, arg,
20791 explain_p, &t);
20792 if (!t)
20793 return unify_no_common_base (explain_p, r, parm, arg);
20794 arg = t;
20797 /* ARG must be constructed from a template class or a template
20798 template parameter. */
20799 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20800 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20801 return unify_template_deduction_failure (explain_p, parm, arg);
20803 /* Deduce arguments T, i from TT<T> or TT<i>. */
20804 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20805 return 1;
20807 arg = TYPE_TI_TEMPLATE (arg);
20809 /* Fall through to deduce template name. */
20812 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20813 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20815 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20817 /* Simple cases: Value already set, does match or doesn't. */
20818 if (targ != NULL_TREE && template_args_equal (targ, arg))
20819 return unify_success (explain_p);
20820 else if (targ)
20821 return unify_inconsistency (explain_p, parm, targ, arg);
20823 else
20825 /* If PARM is `const T' and ARG is only `int', we don't have
20826 a match unless we are allowing additional qualification.
20827 If ARG is `const int' and PARM is just `T' that's OK;
20828 that binds `const int' to `T'. */
20829 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20830 arg, parm))
20831 return unify_cv_qual_mismatch (explain_p, parm, arg);
20833 /* Consider the case where ARG is `const volatile int' and
20834 PARM is `const T'. Then, T should be `volatile int'. */
20835 arg = cp_build_qualified_type_real
20836 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20837 if (arg == error_mark_node)
20838 return unify_invalid (explain_p);
20840 /* Simple cases: Value already set, does match or doesn't. */
20841 if (targ != NULL_TREE && same_type_p (targ, arg))
20842 return unify_success (explain_p);
20843 else if (targ)
20844 return unify_inconsistency (explain_p, parm, targ, arg);
20846 /* Make sure that ARG is not a variable-sized array. (Note
20847 that were talking about variable-sized arrays (like
20848 `int[n]'), rather than arrays of unknown size (like
20849 `int[]').) We'll get very confused by such a type since
20850 the bound of the array is not constant, and therefore
20851 not mangleable. Besides, such types are not allowed in
20852 ISO C++, so we can do as we please here. We do allow
20853 them for 'auto' deduction, since that isn't ABI-exposed. */
20854 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20855 return unify_vla_arg (explain_p, arg);
20857 /* Strip typedefs as in convert_template_argument. */
20858 arg = canonicalize_type_argument (arg, tf_none);
20861 /* If ARG is a parameter pack or an expansion, we cannot unify
20862 against it unless PARM is also a parameter pack. */
20863 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20864 && !template_parameter_pack_p (parm))
20865 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20867 /* If the argument deduction results is a METHOD_TYPE,
20868 then there is a problem.
20869 METHOD_TYPE doesn't map to any real C++ type the result of
20870 the deduction can not be of that type. */
20871 if (TREE_CODE (arg) == METHOD_TYPE)
20872 return unify_method_type_error (explain_p, arg);
20874 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20875 return unify_success (explain_p);
20877 case TEMPLATE_PARM_INDEX:
20878 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20879 if (error_operand_p (tparm))
20880 return unify_invalid (explain_p);
20882 if (TEMPLATE_PARM_LEVEL (parm)
20883 != template_decl_level (tparm))
20885 /* The PARM is not one we're trying to unify. Just check
20886 to see if it matches ARG. */
20887 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20888 && cp_tree_equal (parm, arg));
20889 if (result)
20890 unify_expression_unequal (explain_p, parm, arg);
20891 return result;
20894 idx = TEMPLATE_PARM_IDX (parm);
20895 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20897 if (targ)
20899 int x = !cp_tree_equal (targ, arg);
20900 if (x)
20901 unify_inconsistency (explain_p, parm, targ, arg);
20902 return x;
20905 /* [temp.deduct.type] If, in the declaration of a function template
20906 with a non-type template-parameter, the non-type
20907 template-parameter is used in an expression in the function
20908 parameter-list and, if the corresponding template-argument is
20909 deduced, the template-argument type shall match the type of the
20910 template-parameter exactly, except that a template-argument
20911 deduced from an array bound may be of any integral type.
20912 The non-type parameter might use already deduced type parameters. */
20913 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20914 if (tree a = type_uses_auto (tparm))
20916 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20917 if (tparm == error_mark_node)
20918 return 1;
20921 if (!TREE_TYPE (arg))
20922 /* Template-parameter dependent expression. Just accept it for now.
20923 It will later be processed in convert_template_argument. */
20925 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20926 non_reference (tparm)))
20927 /* OK */;
20928 else if ((strict & UNIFY_ALLOW_INTEGER)
20929 && CP_INTEGRAL_TYPE_P (tparm))
20930 /* Convert the ARG to the type of PARM; the deduced non-type
20931 template argument must exactly match the types of the
20932 corresponding parameter. */
20933 arg = fold (build_nop (tparm, arg));
20934 else if (uses_template_parms (tparm))
20936 /* We haven't deduced the type of this parameter yet. */
20937 if (cxx_dialect >= cxx17
20938 /* We deduce from array bounds in try_array_deduction. */
20939 && !(strict & UNIFY_ALLOW_INTEGER))
20941 /* Deduce it from the non-type argument. */
20942 tree atype = TREE_TYPE (arg);
20943 RECUR_AND_CHECK_FAILURE (tparms, targs,
20944 tparm, atype,
20945 UNIFY_ALLOW_NONE, explain_p);
20947 else
20948 /* Try again later. */
20949 return unify_success (explain_p);
20951 else
20952 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20954 /* If ARG is a parameter pack or an expansion, we cannot unify
20955 against it unless PARM is also a parameter pack. */
20956 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20957 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20958 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20961 bool removed_attr = false;
20962 arg = strip_typedefs_expr (arg, &removed_attr);
20964 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20965 return unify_success (explain_p);
20967 case PTRMEM_CST:
20969 /* A pointer-to-member constant can be unified only with
20970 another constant. */
20971 if (TREE_CODE (arg) != PTRMEM_CST)
20972 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20974 /* Just unify the class member. It would be useless (and possibly
20975 wrong, depending on the strict flags) to unify also
20976 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20977 arg refer to the same variable, even if through different
20978 classes. For instance:
20980 struct A { int x; };
20981 struct B : A { };
20983 Unification of &A::x and &B::x must succeed. */
20984 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20985 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20988 case POINTER_TYPE:
20990 if (!TYPE_PTR_P (arg))
20991 return unify_type_mismatch (explain_p, parm, arg);
20993 /* [temp.deduct.call]
20995 A can be another pointer or pointer to member type that can
20996 be converted to the deduced A via a qualification
20997 conversion (_conv.qual_).
20999 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21000 This will allow for additional cv-qualification of the
21001 pointed-to types if appropriate. */
21003 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21004 /* The derived-to-base conversion only persists through one
21005 level of pointers. */
21006 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21008 return unify (tparms, targs, TREE_TYPE (parm),
21009 TREE_TYPE (arg), strict, explain_p);
21012 case REFERENCE_TYPE:
21013 if (TREE_CODE (arg) != REFERENCE_TYPE)
21014 return unify_type_mismatch (explain_p, parm, arg);
21015 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21016 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21018 case ARRAY_TYPE:
21019 if (TREE_CODE (arg) != ARRAY_TYPE)
21020 return unify_type_mismatch (explain_p, parm, arg);
21021 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21022 != (TYPE_DOMAIN (arg) == NULL_TREE))
21023 return unify_type_mismatch (explain_p, parm, arg);
21024 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21025 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21026 if (TYPE_DOMAIN (parm) != NULL_TREE)
21027 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21028 TYPE_DOMAIN (arg), explain_p);
21029 return unify_success (explain_p);
21031 case REAL_TYPE:
21032 case COMPLEX_TYPE:
21033 case VECTOR_TYPE:
21034 case INTEGER_TYPE:
21035 case BOOLEAN_TYPE:
21036 case ENUMERAL_TYPE:
21037 case VOID_TYPE:
21038 case NULLPTR_TYPE:
21039 if (TREE_CODE (arg) != TREE_CODE (parm))
21040 return unify_type_mismatch (explain_p, parm, arg);
21042 /* We have already checked cv-qualification at the top of the
21043 function. */
21044 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21045 return unify_type_mismatch (explain_p, parm, arg);
21047 /* As far as unification is concerned, this wins. Later checks
21048 will invalidate it if necessary. */
21049 return unify_success (explain_p);
21051 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21052 /* Type INTEGER_CST can come from ordinary constant template args. */
21053 case INTEGER_CST:
21054 while (CONVERT_EXPR_P (arg))
21055 arg = TREE_OPERAND (arg, 0);
21057 if (TREE_CODE (arg) != INTEGER_CST)
21058 return unify_template_argument_mismatch (explain_p, parm, arg);
21059 return (tree_int_cst_equal (parm, arg)
21060 ? unify_success (explain_p)
21061 : unify_template_argument_mismatch (explain_p, parm, arg));
21063 case TREE_VEC:
21065 int i, len, argslen;
21066 int parm_variadic_p = 0;
21068 if (TREE_CODE (arg) != TREE_VEC)
21069 return unify_template_argument_mismatch (explain_p, parm, arg);
21071 len = TREE_VEC_LENGTH (parm);
21072 argslen = TREE_VEC_LENGTH (arg);
21074 /* Check for pack expansions in the parameters. */
21075 for (i = 0; i < len; ++i)
21077 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21079 if (i == len - 1)
21080 /* We can unify against something with a trailing
21081 parameter pack. */
21082 parm_variadic_p = 1;
21083 else
21084 /* [temp.deduct.type]/9: If the template argument list of
21085 P contains a pack expansion that is not the last
21086 template argument, the entire template argument list
21087 is a non-deduced context. */
21088 return unify_success (explain_p);
21092 /* If we don't have enough arguments to satisfy the parameters
21093 (not counting the pack expression at the end), or we have
21094 too many arguments for a parameter list that doesn't end in
21095 a pack expression, we can't unify. */
21096 if (parm_variadic_p
21097 ? argslen < len - parm_variadic_p
21098 : argslen != len)
21099 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21101 /* Unify all of the parameters that precede the (optional)
21102 pack expression. */
21103 for (i = 0; i < len - parm_variadic_p; ++i)
21105 RECUR_AND_CHECK_FAILURE (tparms, targs,
21106 TREE_VEC_ELT (parm, i),
21107 TREE_VEC_ELT (arg, i),
21108 UNIFY_ALLOW_NONE, explain_p);
21110 if (parm_variadic_p)
21111 return unify_pack_expansion (tparms, targs, parm, arg,
21112 DEDUCE_EXACT,
21113 /*subr=*/true, explain_p);
21114 return unify_success (explain_p);
21117 case RECORD_TYPE:
21118 case UNION_TYPE:
21119 if (TREE_CODE (arg) != TREE_CODE (parm))
21120 return unify_type_mismatch (explain_p, parm, arg);
21122 if (TYPE_PTRMEMFUNC_P (parm))
21124 if (!TYPE_PTRMEMFUNC_P (arg))
21125 return unify_type_mismatch (explain_p, parm, arg);
21127 return unify (tparms, targs,
21128 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21129 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21130 strict, explain_p);
21132 else if (TYPE_PTRMEMFUNC_P (arg))
21133 return unify_type_mismatch (explain_p, parm, arg);
21135 if (CLASSTYPE_TEMPLATE_INFO (parm))
21137 tree t = NULL_TREE;
21139 if (strict_in & UNIFY_ALLOW_DERIVED)
21141 /* First, we try to unify the PARM and ARG directly. */
21142 t = try_class_unification (tparms, targs,
21143 parm, arg, explain_p);
21145 if (!t)
21147 /* Fallback to the special case allowed in
21148 [temp.deduct.call]:
21150 If P is a class, and P has the form
21151 template-id, then A can be a derived class of
21152 the deduced A. Likewise, if P is a pointer to
21153 a class of the form template-id, A can be a
21154 pointer to a derived class pointed to by the
21155 deduced A. */
21156 enum template_base_result r;
21157 r = get_template_base (tparms, targs, parm, arg,
21158 explain_p, &t);
21160 if (!t)
21162 /* Don't give the derived diagnostic if we're
21163 already dealing with the same template. */
21164 bool same_template
21165 = (CLASSTYPE_TEMPLATE_INFO (arg)
21166 && (CLASSTYPE_TI_TEMPLATE (parm)
21167 == CLASSTYPE_TI_TEMPLATE (arg)));
21168 return unify_no_common_base (explain_p && !same_template,
21169 r, parm, arg);
21173 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21174 && (CLASSTYPE_TI_TEMPLATE (parm)
21175 == CLASSTYPE_TI_TEMPLATE (arg)))
21176 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21177 Then, we should unify `int' and `U'. */
21178 t = arg;
21179 else
21180 /* There's no chance of unification succeeding. */
21181 return unify_type_mismatch (explain_p, parm, arg);
21183 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21184 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21186 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21187 return unify_type_mismatch (explain_p, parm, arg);
21188 return unify_success (explain_p);
21190 case METHOD_TYPE:
21191 case FUNCTION_TYPE:
21193 unsigned int nargs;
21194 tree *args;
21195 tree a;
21196 unsigned int i;
21198 if (TREE_CODE (arg) != TREE_CODE (parm))
21199 return unify_type_mismatch (explain_p, parm, arg);
21201 /* CV qualifications for methods can never be deduced, they must
21202 match exactly. We need to check them explicitly here,
21203 because type_unification_real treats them as any other
21204 cv-qualified parameter. */
21205 if (TREE_CODE (parm) == METHOD_TYPE
21206 && (!check_cv_quals_for_unify
21207 (UNIFY_ALLOW_NONE,
21208 class_of_this_parm (arg),
21209 class_of_this_parm (parm))))
21210 return unify_cv_qual_mismatch (explain_p, parm, arg);
21211 if (TREE_CODE (arg) == FUNCTION_TYPE
21212 && type_memfn_quals (parm) != type_memfn_quals (arg))
21213 return unify_cv_qual_mismatch (explain_p, parm, arg);
21214 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21215 return unify_type_mismatch (explain_p, parm, arg);
21217 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21218 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21220 nargs = list_length (TYPE_ARG_TYPES (arg));
21221 args = XALLOCAVEC (tree, nargs);
21222 for (a = TYPE_ARG_TYPES (arg), i = 0;
21223 a != NULL_TREE && a != void_list_node;
21224 a = TREE_CHAIN (a), ++i)
21225 args[i] = TREE_VALUE (a);
21226 nargs = i;
21228 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21229 args, nargs, 1, DEDUCE_EXACT,
21230 LOOKUP_NORMAL, NULL, explain_p))
21231 return 1;
21233 if (flag_noexcept_type)
21235 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21236 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21237 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21238 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21239 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21240 && uses_template_parms (TREE_PURPOSE (pspec)))
21241 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21242 TREE_PURPOSE (aspec),
21243 UNIFY_ALLOW_NONE, explain_p);
21244 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21245 return unify_type_mismatch (explain_p, parm, arg);
21248 return 0;
21251 case OFFSET_TYPE:
21252 /* Unify a pointer to member with a pointer to member function, which
21253 deduces the type of the member as a function type. */
21254 if (TYPE_PTRMEMFUNC_P (arg))
21256 /* Check top-level cv qualifiers */
21257 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21258 return unify_cv_qual_mismatch (explain_p, parm, arg);
21260 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21261 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21262 UNIFY_ALLOW_NONE, explain_p);
21264 /* Determine the type of the function we are unifying against. */
21265 tree fntype = static_fn_type (arg);
21267 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21270 if (TREE_CODE (arg) != OFFSET_TYPE)
21271 return unify_type_mismatch (explain_p, parm, arg);
21272 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21273 TYPE_OFFSET_BASETYPE (arg),
21274 UNIFY_ALLOW_NONE, explain_p);
21275 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21276 strict, explain_p);
21278 case CONST_DECL:
21279 if (DECL_TEMPLATE_PARM_P (parm))
21280 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21281 if (arg != scalar_constant_value (parm))
21282 return unify_template_argument_mismatch (explain_p, parm, arg);
21283 return unify_success (explain_p);
21285 case FIELD_DECL:
21286 case TEMPLATE_DECL:
21287 /* Matched cases are handled by the ARG == PARM test above. */
21288 return unify_template_argument_mismatch (explain_p, parm, arg);
21290 case VAR_DECL:
21291 /* We might get a variable as a non-type template argument in parm if the
21292 corresponding parameter is type-dependent. Make any necessary
21293 adjustments based on whether arg is a reference. */
21294 if (CONSTANT_CLASS_P (arg))
21295 parm = fold_non_dependent_expr (parm);
21296 else if (REFERENCE_REF_P (arg))
21298 tree sub = TREE_OPERAND (arg, 0);
21299 STRIP_NOPS (sub);
21300 if (TREE_CODE (sub) == ADDR_EXPR)
21301 arg = TREE_OPERAND (sub, 0);
21303 /* Now use the normal expression code to check whether they match. */
21304 goto expr;
21306 case TYPE_ARGUMENT_PACK:
21307 case NONTYPE_ARGUMENT_PACK:
21308 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21309 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21311 case TYPEOF_TYPE:
21312 case DECLTYPE_TYPE:
21313 case UNDERLYING_TYPE:
21314 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21315 or UNDERLYING_TYPE nodes. */
21316 return unify_success (explain_p);
21318 case ERROR_MARK:
21319 /* Unification fails if we hit an error node. */
21320 return unify_invalid (explain_p);
21322 case INDIRECT_REF:
21323 if (REFERENCE_REF_P (parm))
21325 bool pexp = PACK_EXPANSION_P (arg);
21326 if (pexp)
21327 arg = PACK_EXPANSION_PATTERN (arg);
21328 if (REFERENCE_REF_P (arg))
21329 arg = TREE_OPERAND (arg, 0);
21330 if (pexp)
21331 arg = make_pack_expansion (arg, complain);
21332 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21333 strict, explain_p);
21335 /* FALLTHRU */
21337 default:
21338 /* An unresolved overload is a nondeduced context. */
21339 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21340 return unify_success (explain_p);
21341 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21342 expr:
21343 /* We must be looking at an expression. This can happen with
21344 something like:
21346 template <int I>
21347 void foo(S<I>, S<I + 2>);
21349 This is a "nondeduced context":
21351 [deduct.type]
21353 The nondeduced contexts are:
21355 --A type that is a template-id in which one or more of
21356 the template-arguments is an expression that references
21357 a template-parameter.
21359 In these cases, we assume deduction succeeded, but don't
21360 actually infer any unifications. */
21362 if (!uses_template_parms (parm)
21363 && !template_args_equal (parm, arg))
21364 return unify_expression_unequal (explain_p, parm, arg);
21365 else
21366 return unify_success (explain_p);
21369 #undef RECUR_AND_CHECK_FAILURE
21371 /* Note that DECL can be defined in this translation unit, if
21372 required. */
21374 static void
21375 mark_definable (tree decl)
21377 tree clone;
21378 DECL_NOT_REALLY_EXTERN (decl) = 1;
21379 FOR_EACH_CLONE (clone, decl)
21380 DECL_NOT_REALLY_EXTERN (clone) = 1;
21383 /* Called if RESULT is explicitly instantiated, or is a member of an
21384 explicitly instantiated class. */
21386 void
21387 mark_decl_instantiated (tree result, int extern_p)
21389 SET_DECL_EXPLICIT_INSTANTIATION (result);
21391 /* If this entity has already been written out, it's too late to
21392 make any modifications. */
21393 if (TREE_ASM_WRITTEN (result))
21394 return;
21396 /* For anonymous namespace we don't need to do anything. */
21397 if (decl_anon_ns_mem_p (result))
21399 gcc_assert (!TREE_PUBLIC (result));
21400 return;
21403 if (TREE_CODE (result) != FUNCTION_DECL)
21404 /* The TREE_PUBLIC flag for function declarations will have been
21405 set correctly by tsubst. */
21406 TREE_PUBLIC (result) = 1;
21408 /* This might have been set by an earlier implicit instantiation. */
21409 DECL_COMDAT (result) = 0;
21411 if (extern_p)
21412 DECL_NOT_REALLY_EXTERN (result) = 0;
21413 else
21415 mark_definable (result);
21416 mark_needed (result);
21417 /* Always make artificials weak. */
21418 if (DECL_ARTIFICIAL (result) && flag_weak)
21419 comdat_linkage (result);
21420 /* For WIN32 we also want to put explicit instantiations in
21421 linkonce sections. */
21422 else if (TREE_PUBLIC (result))
21423 maybe_make_one_only (result);
21426 /* If EXTERN_P, then this function will not be emitted -- unless
21427 followed by an explicit instantiation, at which point its linkage
21428 will be adjusted. If !EXTERN_P, then this function will be
21429 emitted here. In neither circumstance do we want
21430 import_export_decl to adjust the linkage. */
21431 DECL_INTERFACE_KNOWN (result) = 1;
21434 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21435 important template arguments. If any are missing, we check whether
21436 they're important by using error_mark_node for substituting into any
21437 args that were used for partial ordering (the ones between ARGS and END)
21438 and seeing if it bubbles up. */
21440 static bool
21441 check_undeduced_parms (tree targs, tree args, tree end)
21443 bool found = false;
21444 int i;
21445 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21446 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21448 found = true;
21449 TREE_VEC_ELT (targs, i) = error_mark_node;
21451 if (found)
21453 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21454 if (substed == error_mark_node)
21455 return true;
21457 return false;
21460 /* Given two function templates PAT1 and PAT2, return:
21462 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21463 -1 if PAT2 is more specialized than PAT1.
21464 0 if neither is more specialized.
21466 LEN indicates the number of parameters we should consider
21467 (defaulted parameters should not be considered).
21469 The 1998 std underspecified function template partial ordering, and
21470 DR214 addresses the issue. We take pairs of arguments, one from
21471 each of the templates, and deduce them against each other. One of
21472 the templates will be more specialized if all the *other*
21473 template's arguments deduce against its arguments and at least one
21474 of its arguments *does* *not* deduce against the other template's
21475 corresponding argument. Deduction is done as for class templates.
21476 The arguments used in deduction have reference and top level cv
21477 qualifiers removed. Iff both arguments were originally reference
21478 types *and* deduction succeeds in both directions, an lvalue reference
21479 wins against an rvalue reference and otherwise the template
21480 with the more cv-qualified argument wins for that pairing (if
21481 neither is more cv-qualified, they both are equal). Unlike regular
21482 deduction, after all the arguments have been deduced in this way,
21483 we do *not* verify the deduced template argument values can be
21484 substituted into non-deduced contexts.
21486 The logic can be a bit confusing here, because we look at deduce1 and
21487 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21488 can find template arguments for pat1 to make arg1 look like arg2, that
21489 means that arg2 is at least as specialized as arg1. */
21492 more_specialized_fn (tree pat1, tree pat2, int len)
21494 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21495 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21496 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21497 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21498 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21499 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21500 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21501 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21502 tree origs1, origs2;
21503 bool lose1 = false;
21504 bool lose2 = false;
21506 /* Remove the this parameter from non-static member functions. If
21507 one is a non-static member function and the other is not a static
21508 member function, remove the first parameter from that function
21509 also. This situation occurs for operator functions where we
21510 locate both a member function (with this pointer) and non-member
21511 operator (with explicit first operand). */
21512 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21514 len--; /* LEN is the number of significant arguments for DECL1 */
21515 args1 = TREE_CHAIN (args1);
21516 if (!DECL_STATIC_FUNCTION_P (decl2))
21517 args2 = TREE_CHAIN (args2);
21519 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21521 args2 = TREE_CHAIN (args2);
21522 if (!DECL_STATIC_FUNCTION_P (decl1))
21524 len--;
21525 args1 = TREE_CHAIN (args1);
21529 /* If only one is a conversion operator, they are unordered. */
21530 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21531 return 0;
21533 /* Consider the return type for a conversion function */
21534 if (DECL_CONV_FN_P (decl1))
21536 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21537 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21538 len++;
21541 processing_template_decl++;
21543 origs1 = args1;
21544 origs2 = args2;
21546 while (len--
21547 /* Stop when an ellipsis is seen. */
21548 && args1 != NULL_TREE && args2 != NULL_TREE)
21550 tree arg1 = TREE_VALUE (args1);
21551 tree arg2 = TREE_VALUE (args2);
21552 int deduce1, deduce2;
21553 int quals1 = -1;
21554 int quals2 = -1;
21555 int ref1 = 0;
21556 int ref2 = 0;
21558 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21559 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21561 /* When both arguments are pack expansions, we need only
21562 unify the patterns themselves. */
21563 arg1 = PACK_EXPANSION_PATTERN (arg1);
21564 arg2 = PACK_EXPANSION_PATTERN (arg2);
21566 /* This is the last comparison we need to do. */
21567 len = 0;
21570 /* DR 1847: If a particular P contains no template-parameters that
21571 participate in template argument deduction, that P is not used to
21572 determine the ordering. */
21573 if (!uses_deducible_template_parms (arg1)
21574 && !uses_deducible_template_parms (arg2))
21575 goto next;
21577 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21579 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21580 arg1 = TREE_TYPE (arg1);
21581 quals1 = cp_type_quals (arg1);
21584 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21586 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21587 arg2 = TREE_TYPE (arg2);
21588 quals2 = cp_type_quals (arg2);
21591 arg1 = TYPE_MAIN_VARIANT (arg1);
21592 arg2 = TYPE_MAIN_VARIANT (arg2);
21594 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21596 int i, len2 = remaining_arguments (args2);
21597 tree parmvec = make_tree_vec (1);
21598 tree argvec = make_tree_vec (len2);
21599 tree ta = args2;
21601 /* Setup the parameter vector, which contains only ARG1. */
21602 TREE_VEC_ELT (parmvec, 0) = arg1;
21604 /* Setup the argument vector, which contains the remaining
21605 arguments. */
21606 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21607 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21609 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21610 argvec, DEDUCE_EXACT,
21611 /*subr=*/true, /*explain_p=*/false)
21612 == 0);
21614 /* We cannot deduce in the other direction, because ARG1 is
21615 a pack expansion but ARG2 is not. */
21616 deduce2 = 0;
21618 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21620 int i, len1 = remaining_arguments (args1);
21621 tree parmvec = make_tree_vec (1);
21622 tree argvec = make_tree_vec (len1);
21623 tree ta = args1;
21625 /* Setup the parameter vector, which contains only ARG1. */
21626 TREE_VEC_ELT (parmvec, 0) = arg2;
21628 /* Setup the argument vector, which contains the remaining
21629 arguments. */
21630 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21631 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21633 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21634 argvec, DEDUCE_EXACT,
21635 /*subr=*/true, /*explain_p=*/false)
21636 == 0);
21638 /* We cannot deduce in the other direction, because ARG2 is
21639 a pack expansion but ARG1 is not.*/
21640 deduce1 = 0;
21643 else
21645 /* The normal case, where neither argument is a pack
21646 expansion. */
21647 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21648 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21649 == 0);
21650 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21651 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21652 == 0);
21655 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21656 arg2, then arg2 is not as specialized as arg1. */
21657 if (!deduce1)
21658 lose2 = true;
21659 if (!deduce2)
21660 lose1 = true;
21662 /* "If, for a given type, deduction succeeds in both directions
21663 (i.e., the types are identical after the transformations above)
21664 and both P and A were reference types (before being replaced with
21665 the type referred to above):
21666 - if the type from the argument template was an lvalue reference and
21667 the type from the parameter template was not, the argument type is
21668 considered to be more specialized than the other; otherwise,
21669 - if the type from the argument template is more cv-qualified
21670 than the type from the parameter template (as described above),
21671 the argument type is considered to be more specialized than the other;
21672 otherwise,
21673 - neither type is more specialized than the other." */
21675 if (deduce1 && deduce2)
21677 if (ref1 && ref2 && ref1 != ref2)
21679 if (ref1 > ref2)
21680 lose1 = true;
21681 else
21682 lose2 = true;
21684 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21686 if ((quals1 & quals2) == quals2)
21687 lose2 = true;
21688 if ((quals1 & quals2) == quals1)
21689 lose1 = true;
21693 if (lose1 && lose2)
21694 /* We've failed to deduce something in either direction.
21695 These must be unordered. */
21696 break;
21698 next:
21700 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21701 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21702 /* We have already processed all of the arguments in our
21703 handing of the pack expansion type. */
21704 len = 0;
21706 args1 = TREE_CHAIN (args1);
21707 args2 = TREE_CHAIN (args2);
21710 /* "In most cases, all template parameters must have values in order for
21711 deduction to succeed, but for partial ordering purposes a template
21712 parameter may remain without a value provided it is not used in the
21713 types being used for partial ordering."
21715 Thus, if we are missing any of the targs1 we need to substitute into
21716 origs1, then pat2 is not as specialized as pat1. This can happen when
21717 there is a nondeduced context. */
21718 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21719 lose2 = true;
21720 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21721 lose1 = true;
21723 processing_template_decl--;
21725 /* If both deductions succeed, the partial ordering selects the more
21726 constrained template. */
21727 if (!lose1 && !lose2)
21729 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21730 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21731 lose1 = !subsumes_constraints (c1, c2);
21732 lose2 = !subsumes_constraints (c2, c1);
21735 /* All things being equal, if the next argument is a pack expansion
21736 for one function but not for the other, prefer the
21737 non-variadic function. FIXME this is bogus; see c++/41958. */
21738 if (lose1 == lose2
21739 && args1 && TREE_VALUE (args1)
21740 && args2 && TREE_VALUE (args2))
21742 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21743 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21746 if (lose1 == lose2)
21747 return 0;
21748 else if (!lose1)
21749 return 1;
21750 else
21751 return -1;
21754 /* Determine which of two partial specializations of TMPL is more
21755 specialized.
21757 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21758 to the first partial specialization. The TREE_PURPOSE is the
21759 innermost set of template parameters for the partial
21760 specialization. PAT2 is similar, but for the second template.
21762 Return 1 if the first partial specialization is more specialized;
21763 -1 if the second is more specialized; 0 if neither is more
21764 specialized.
21766 See [temp.class.order] for information about determining which of
21767 two templates is more specialized. */
21769 static int
21770 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21772 tree targs;
21773 int winner = 0;
21774 bool any_deductions = false;
21776 tree tmpl1 = TREE_VALUE (pat1);
21777 tree tmpl2 = TREE_VALUE (pat2);
21778 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21779 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21781 /* Just like what happens for functions, if we are ordering between
21782 different template specializations, we may encounter dependent
21783 types in the arguments, and we need our dependency check functions
21784 to behave correctly. */
21785 ++processing_template_decl;
21786 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21787 if (targs)
21789 --winner;
21790 any_deductions = true;
21793 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21794 if (targs)
21796 ++winner;
21797 any_deductions = true;
21799 --processing_template_decl;
21801 /* If both deductions succeed, the partial ordering selects the more
21802 constrained template. */
21803 if (!winner && any_deductions)
21804 return more_constrained (tmpl1, tmpl2);
21806 /* In the case of a tie where at least one of the templates
21807 has a parameter pack at the end, the template with the most
21808 non-packed parameters wins. */
21809 if (winner == 0
21810 && any_deductions
21811 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21812 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21814 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21815 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21816 int len1 = TREE_VEC_LENGTH (args1);
21817 int len2 = TREE_VEC_LENGTH (args2);
21819 /* We don't count the pack expansion at the end. */
21820 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21821 --len1;
21822 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21823 --len2;
21825 if (len1 > len2)
21826 return 1;
21827 else if (len1 < len2)
21828 return -1;
21831 return winner;
21834 /* Return the template arguments that will produce the function signature
21835 DECL from the function template FN, with the explicit template
21836 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21837 also match. Return NULL_TREE if no satisfactory arguments could be
21838 found. */
21840 static tree
21841 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21843 int ntparms = DECL_NTPARMS (fn);
21844 tree targs = make_tree_vec (ntparms);
21845 tree decl_type = TREE_TYPE (decl);
21846 tree decl_arg_types;
21847 tree *args;
21848 unsigned int nargs, ix;
21849 tree arg;
21851 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21853 /* Never do unification on the 'this' parameter. */
21854 decl_arg_types = skip_artificial_parms_for (decl,
21855 TYPE_ARG_TYPES (decl_type));
21857 nargs = list_length (decl_arg_types);
21858 args = XALLOCAVEC (tree, nargs);
21859 for (arg = decl_arg_types, ix = 0;
21860 arg != NULL_TREE && arg != void_list_node;
21861 arg = TREE_CHAIN (arg), ++ix)
21862 args[ix] = TREE_VALUE (arg);
21864 if (fn_type_unification (fn, explicit_args, targs,
21865 args, ix,
21866 (check_rettype || DECL_CONV_FN_P (fn)
21867 ? TREE_TYPE (decl_type) : NULL_TREE),
21868 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21869 /*decltype*/false)
21870 == error_mark_node)
21871 return NULL_TREE;
21873 return targs;
21876 /* Return the innermost template arguments that, when applied to a partial
21877 specialization SPEC_TMPL of TMPL, yield the ARGS.
21879 For example, suppose we have:
21881 template <class T, class U> struct S {};
21882 template <class T> struct S<T*, int> {};
21884 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21885 partial specialization and the ARGS will be {double*, int}. The resulting
21886 vector will be {double}, indicating that `T' is bound to `double'. */
21888 static tree
21889 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21891 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21892 tree spec_args
21893 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21894 int i, ntparms = TREE_VEC_LENGTH (tparms);
21895 tree deduced_args;
21896 tree innermost_deduced_args;
21898 innermost_deduced_args = make_tree_vec (ntparms);
21899 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21901 deduced_args = copy_node (args);
21902 SET_TMPL_ARGS_LEVEL (deduced_args,
21903 TMPL_ARGS_DEPTH (deduced_args),
21904 innermost_deduced_args);
21906 else
21907 deduced_args = innermost_deduced_args;
21909 bool tried_array_deduction = (cxx_dialect < cxx17);
21910 again:
21911 if (unify (tparms, deduced_args,
21912 INNERMOST_TEMPLATE_ARGS (spec_args),
21913 INNERMOST_TEMPLATE_ARGS (args),
21914 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21915 return NULL_TREE;
21917 for (i = 0; i < ntparms; ++i)
21918 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21920 if (!tried_array_deduction)
21922 try_array_deduction (tparms, innermost_deduced_args,
21923 INNERMOST_TEMPLATE_ARGS (spec_args));
21924 tried_array_deduction = true;
21925 if (TREE_VEC_ELT (innermost_deduced_args, i))
21926 goto again;
21928 return NULL_TREE;
21931 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21932 if (!push_tinst_level (tinst))
21934 excessive_deduction_depth = true;
21935 return NULL_TREE;
21938 /* Verify that nondeduced template arguments agree with the type
21939 obtained from argument deduction.
21941 For example:
21943 struct A { typedef int X; };
21944 template <class T, class U> struct C {};
21945 template <class T> struct C<T, typename T::X> {};
21947 Then with the instantiation `C<A, int>', we can deduce that
21948 `T' is `A' but unify () does not check whether `typename T::X'
21949 is `int'. */
21950 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21952 if (spec_args != error_mark_node)
21953 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21954 INNERMOST_TEMPLATE_ARGS (spec_args),
21955 tmpl, tf_none, false, false);
21957 pop_tinst_level ();
21959 if (spec_args == error_mark_node
21960 /* We only need to check the innermost arguments; the other
21961 arguments will always agree. */
21962 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21963 INNERMOST_TEMPLATE_ARGS (args)))
21964 return NULL_TREE;
21966 /* Now that we have bindings for all of the template arguments,
21967 ensure that the arguments deduced for the template template
21968 parameters have compatible template parameter lists. See the use
21969 of template_template_parm_bindings_ok_p in fn_type_unification
21970 for more information. */
21971 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21972 return NULL_TREE;
21974 return deduced_args;
21977 // Compare two function templates T1 and T2 by deducing bindings
21978 // from one against the other. If both deductions succeed, compare
21979 // constraints to see which is more constrained.
21980 static int
21981 more_specialized_inst (tree t1, tree t2)
21983 int fate = 0;
21984 int count = 0;
21986 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21988 --fate;
21989 ++count;
21992 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21994 ++fate;
21995 ++count;
21998 // If both deductions succeed, then one may be more constrained.
21999 if (count == 2 && fate == 0)
22000 fate = more_constrained (t1, t2);
22002 return fate;
22005 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22006 Return the TREE_LIST node with the most specialized template, if
22007 any. If there is no most specialized template, the error_mark_node
22008 is returned.
22010 Note that this function does not look at, or modify, the
22011 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22012 returned is one of the elements of INSTANTIATIONS, callers may
22013 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22014 and retrieve it from the value returned. */
22016 tree
22017 most_specialized_instantiation (tree templates)
22019 tree fn, champ;
22021 ++processing_template_decl;
22023 champ = templates;
22024 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22026 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22027 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22028 if (fate == -1)
22029 champ = fn;
22030 else if (!fate)
22032 /* Equally specialized, move to next function. If there
22033 is no next function, nothing's most specialized. */
22034 fn = TREE_CHAIN (fn);
22035 champ = fn;
22036 if (!fn)
22037 break;
22041 if (champ)
22042 /* Now verify that champ is better than everything earlier in the
22043 instantiation list. */
22044 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22045 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22047 champ = NULL_TREE;
22048 break;
22052 processing_template_decl--;
22054 if (!champ)
22055 return error_mark_node;
22057 return champ;
22060 /* If DECL is a specialization of some template, return the most
22061 general such template. Otherwise, returns NULL_TREE.
22063 For example, given:
22065 template <class T> struct S { template <class U> void f(U); };
22067 if TMPL is `template <class U> void S<int>::f(U)' this will return
22068 the full template. This function will not trace past partial
22069 specializations, however. For example, given in addition:
22071 template <class T> struct S<T*> { template <class U> void f(U); };
22073 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22074 `template <class T> template <class U> S<T*>::f(U)'. */
22076 tree
22077 most_general_template (tree decl)
22079 if (TREE_CODE (decl) != TEMPLATE_DECL)
22081 if (tree tinfo = get_template_info (decl))
22082 decl = TI_TEMPLATE (tinfo);
22083 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22084 template friend, or a FIELD_DECL for a capture pack. */
22085 if (TREE_CODE (decl) != TEMPLATE_DECL)
22086 return NULL_TREE;
22089 /* Look for more and more general templates. */
22090 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22092 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22093 (See cp-tree.h for details.) */
22094 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22095 break;
22097 if (CLASS_TYPE_P (TREE_TYPE (decl))
22098 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22099 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22100 break;
22102 /* Stop if we run into an explicitly specialized class template. */
22103 if (!DECL_NAMESPACE_SCOPE_P (decl)
22104 && DECL_CONTEXT (decl)
22105 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22106 break;
22108 decl = DECL_TI_TEMPLATE (decl);
22111 return decl;
22114 /* Return the most specialized of the template partial specializations
22115 which can produce TARGET, a specialization of some class or variable
22116 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22117 a TEMPLATE_DECL node corresponding to the partial specialization, while
22118 the TREE_PURPOSE is the set of template arguments that must be
22119 substituted into the template pattern in order to generate TARGET.
22121 If the choice of partial specialization is ambiguous, a diagnostic
22122 is issued, and the error_mark_node is returned. If there are no
22123 partial specializations matching TARGET, then NULL_TREE is
22124 returned, indicating that the primary template should be used. */
22126 static tree
22127 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22129 tree list = NULL_TREE;
22130 tree t;
22131 tree champ;
22132 int fate;
22133 bool ambiguous_p;
22134 tree outer_args = NULL_TREE;
22135 tree tmpl, args;
22137 if (TYPE_P (target))
22139 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22140 tmpl = TI_TEMPLATE (tinfo);
22141 args = TI_ARGS (tinfo);
22143 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22145 tmpl = TREE_OPERAND (target, 0);
22146 args = TREE_OPERAND (target, 1);
22148 else if (VAR_P (target))
22150 tree tinfo = DECL_TEMPLATE_INFO (target);
22151 tmpl = TI_TEMPLATE (tinfo);
22152 args = TI_ARGS (tinfo);
22154 else
22155 gcc_unreachable ();
22157 tree main_tmpl = most_general_template (tmpl);
22159 /* For determining which partial specialization to use, only the
22160 innermost args are interesting. */
22161 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22163 outer_args = strip_innermost_template_args (args, 1);
22164 args = INNERMOST_TEMPLATE_ARGS (args);
22167 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22169 tree spec_args;
22170 tree spec_tmpl = TREE_VALUE (t);
22172 if (outer_args)
22174 /* Substitute in the template args from the enclosing class. */
22175 ++processing_template_decl;
22176 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22177 --processing_template_decl;
22180 if (spec_tmpl == error_mark_node)
22181 return error_mark_node;
22183 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22184 if (spec_args)
22186 if (outer_args)
22187 spec_args = add_to_template_args (outer_args, spec_args);
22189 /* Keep the candidate only if the constraints are satisfied,
22190 or if we're not compiling with concepts. */
22191 if (!flag_concepts
22192 || constraints_satisfied_p (spec_tmpl, spec_args))
22194 list = tree_cons (spec_args, TREE_VALUE (t), list);
22195 TREE_TYPE (list) = TREE_TYPE (t);
22200 if (! list)
22201 return NULL_TREE;
22203 ambiguous_p = false;
22204 t = list;
22205 champ = t;
22206 t = TREE_CHAIN (t);
22207 for (; t; t = TREE_CHAIN (t))
22209 fate = more_specialized_partial_spec (tmpl, champ, t);
22210 if (fate == 1)
22212 else
22214 if (fate == 0)
22216 t = TREE_CHAIN (t);
22217 if (! t)
22219 ambiguous_p = true;
22220 break;
22223 champ = t;
22227 if (!ambiguous_p)
22228 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22230 fate = more_specialized_partial_spec (tmpl, champ, t);
22231 if (fate != 1)
22233 ambiguous_p = true;
22234 break;
22238 if (ambiguous_p)
22240 const char *str;
22241 char *spaces = NULL;
22242 if (!(complain & tf_error))
22243 return error_mark_node;
22244 if (TYPE_P (target))
22245 error ("ambiguous template instantiation for %q#T", target);
22246 else
22247 error ("ambiguous template instantiation for %q#D", target);
22248 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22249 for (t = list; t; t = TREE_CHAIN (t))
22251 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22252 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22253 "%s %#qS", spaces ? spaces : str, subst);
22254 spaces = spaces ? spaces : get_spaces (str);
22256 free (spaces);
22257 return error_mark_node;
22260 return champ;
22263 /* Explicitly instantiate DECL. */
22265 void
22266 do_decl_instantiation (tree decl, tree storage)
22268 tree result = NULL_TREE;
22269 int extern_p = 0;
22271 if (!decl || decl == error_mark_node)
22272 /* An error occurred, for which grokdeclarator has already issued
22273 an appropriate message. */
22274 return;
22275 else if (! DECL_LANG_SPECIFIC (decl))
22277 error ("explicit instantiation of non-template %q#D", decl);
22278 return;
22281 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22282 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22284 if (VAR_P (decl) && !var_templ)
22286 /* There is an asymmetry here in the way VAR_DECLs and
22287 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22288 the latter, the DECL we get back will be marked as a
22289 template instantiation, and the appropriate
22290 DECL_TEMPLATE_INFO will be set up. This does not happen for
22291 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22292 should handle VAR_DECLs as it currently handles
22293 FUNCTION_DECLs. */
22294 if (!DECL_CLASS_SCOPE_P (decl))
22296 error ("%qD is not a static data member of a class template", decl);
22297 return;
22299 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22300 if (!result || !VAR_P (result))
22302 error ("no matching template for %qD found", decl);
22303 return;
22305 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22307 error ("type %qT for explicit instantiation %qD does not match "
22308 "declared type %qT", TREE_TYPE (result), decl,
22309 TREE_TYPE (decl));
22310 return;
22313 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22315 error ("explicit instantiation of %q#D", decl);
22316 return;
22318 else
22319 result = decl;
22321 /* Check for various error cases. Note that if the explicit
22322 instantiation is valid the RESULT will currently be marked as an
22323 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22324 until we get here. */
22326 if (DECL_TEMPLATE_SPECIALIZATION (result))
22328 /* DR 259 [temp.spec].
22330 Both an explicit instantiation and a declaration of an explicit
22331 specialization shall not appear in a program unless the explicit
22332 instantiation follows a declaration of the explicit specialization.
22334 For a given set of template parameters, if an explicit
22335 instantiation of a template appears after a declaration of an
22336 explicit specialization for that template, the explicit
22337 instantiation has no effect. */
22338 return;
22340 else if (DECL_EXPLICIT_INSTANTIATION (result))
22342 /* [temp.spec]
22344 No program shall explicitly instantiate any template more
22345 than once.
22347 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22348 the first instantiation was `extern' and the second is not,
22349 and EXTERN_P for the opposite case. */
22350 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22351 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22352 /* If an "extern" explicit instantiation follows an ordinary
22353 explicit instantiation, the template is instantiated. */
22354 if (extern_p)
22355 return;
22357 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22359 error ("no matching template for %qD found", result);
22360 return;
22362 else if (!DECL_TEMPLATE_INFO (result))
22364 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22365 return;
22368 if (storage == NULL_TREE)
22370 else if (storage == ridpointers[(int) RID_EXTERN])
22372 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22373 pedwarn (input_location, OPT_Wpedantic,
22374 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22375 "instantiations");
22376 extern_p = 1;
22378 else
22379 error ("storage class %qD applied to template instantiation", storage);
22381 check_explicit_instantiation_namespace (result);
22382 mark_decl_instantiated (result, extern_p);
22383 if (! extern_p)
22384 instantiate_decl (result, /*defer_ok=*/true,
22385 /*expl_inst_class_mem_p=*/false);
22388 static void
22389 mark_class_instantiated (tree t, int extern_p)
22391 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22392 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22393 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22394 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22395 if (! extern_p)
22397 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22398 rest_of_type_compilation (t, 1);
22402 /* Called from do_type_instantiation through binding_table_foreach to
22403 do recursive instantiation for the type bound in ENTRY. */
22404 static void
22405 bt_instantiate_type_proc (binding_entry entry, void *data)
22407 tree storage = *(tree *) data;
22409 if (MAYBE_CLASS_TYPE_P (entry->type)
22410 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22411 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22414 /* Perform an explicit instantiation of template class T. STORAGE, if
22415 non-null, is the RID for extern, inline or static. COMPLAIN is
22416 nonzero if this is called from the parser, zero if called recursively,
22417 since the standard is unclear (as detailed below). */
22419 void
22420 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22422 int extern_p = 0;
22423 int nomem_p = 0;
22424 int static_p = 0;
22425 int previous_instantiation_extern_p = 0;
22427 if (TREE_CODE (t) == TYPE_DECL)
22428 t = TREE_TYPE (t);
22430 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22432 tree tmpl =
22433 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22434 if (tmpl)
22435 error ("explicit instantiation of non-class template %qD", tmpl);
22436 else
22437 error ("explicit instantiation of non-template type %qT", t);
22438 return;
22441 complete_type (t);
22443 if (!COMPLETE_TYPE_P (t))
22445 if (complain & tf_error)
22446 error ("explicit instantiation of %q#T before definition of template",
22448 return;
22451 if (storage != NULL_TREE)
22453 if (!in_system_header_at (input_location))
22455 if (storage == ridpointers[(int) RID_EXTERN])
22457 if (cxx_dialect == cxx98)
22458 pedwarn (input_location, OPT_Wpedantic,
22459 "ISO C++ 1998 forbids the use of %<extern%> on "
22460 "explicit instantiations");
22462 else
22463 pedwarn (input_location, OPT_Wpedantic,
22464 "ISO C++ forbids the use of %qE"
22465 " on explicit instantiations", storage);
22468 if (storage == ridpointers[(int) RID_INLINE])
22469 nomem_p = 1;
22470 else if (storage == ridpointers[(int) RID_EXTERN])
22471 extern_p = 1;
22472 else if (storage == ridpointers[(int) RID_STATIC])
22473 static_p = 1;
22474 else
22476 error ("storage class %qD applied to template instantiation",
22477 storage);
22478 extern_p = 0;
22482 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22484 /* DR 259 [temp.spec].
22486 Both an explicit instantiation and a declaration of an explicit
22487 specialization shall not appear in a program unless the explicit
22488 instantiation follows a declaration of the explicit specialization.
22490 For a given set of template parameters, if an explicit
22491 instantiation of a template appears after a declaration of an
22492 explicit specialization for that template, the explicit
22493 instantiation has no effect. */
22494 return;
22496 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22498 /* [temp.spec]
22500 No program shall explicitly instantiate any template more
22501 than once.
22503 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22504 instantiation was `extern'. If EXTERN_P then the second is.
22505 These cases are OK. */
22506 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22508 if (!previous_instantiation_extern_p && !extern_p
22509 && (complain & tf_error))
22510 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22512 /* If we've already instantiated the template, just return now. */
22513 if (!CLASSTYPE_INTERFACE_ONLY (t))
22514 return;
22517 check_explicit_instantiation_namespace (TYPE_NAME (t));
22518 mark_class_instantiated (t, extern_p);
22520 if (nomem_p)
22521 return;
22523 /* In contrast to implicit instantiation, where only the
22524 declarations, and not the definitions, of members are
22525 instantiated, we have here:
22527 [temp.explicit]
22529 The explicit instantiation of a class template specialization
22530 implies the instantiation of all of its members not
22531 previously explicitly specialized in the translation unit
22532 containing the explicit instantiation.
22534 Of course, we can't instantiate member template classes, since we
22535 don't have any arguments for them. Note that the standard is
22536 unclear on whether the instantiation of the members are
22537 *explicit* instantiations or not. However, the most natural
22538 interpretation is that it should be an explicit
22539 instantiation. */
22540 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22541 if ((VAR_P (fld)
22542 || (TREE_CODE (fld) == FUNCTION_DECL
22543 && !static_p
22544 && user_provided_p (fld)))
22545 && DECL_TEMPLATE_INSTANTIATION (fld))
22547 mark_decl_instantiated (fld, extern_p);
22548 if (! extern_p)
22549 instantiate_decl (fld, /*defer_ok=*/true,
22550 /*expl_inst_class_mem_p=*/true);
22553 if (CLASSTYPE_NESTED_UTDS (t))
22554 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22555 bt_instantiate_type_proc, &storage);
22558 /* Given a function DECL, which is a specialization of TMPL, modify
22559 DECL to be a re-instantiation of TMPL with the same template
22560 arguments. TMPL should be the template into which tsubst'ing
22561 should occur for DECL, not the most general template.
22563 One reason for doing this is a scenario like this:
22565 template <class T>
22566 void f(const T&, int i);
22568 void g() { f(3, 7); }
22570 template <class T>
22571 void f(const T& t, const int i) { }
22573 Note that when the template is first instantiated, with
22574 instantiate_template, the resulting DECL will have no name for the
22575 first parameter, and the wrong type for the second. So, when we go
22576 to instantiate the DECL, we regenerate it. */
22578 static void
22579 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22581 /* The arguments used to instantiate DECL, from the most general
22582 template. */
22583 tree code_pattern;
22585 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22587 /* Make sure that we can see identifiers, and compute access
22588 correctly. */
22589 push_access_scope (decl);
22591 if (TREE_CODE (decl) == FUNCTION_DECL)
22593 tree decl_parm;
22594 tree pattern_parm;
22595 tree specs;
22596 int args_depth;
22597 int parms_depth;
22599 args_depth = TMPL_ARGS_DEPTH (args);
22600 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22601 if (args_depth > parms_depth)
22602 args = get_innermost_template_args (args, parms_depth);
22604 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22605 args, tf_error, NULL_TREE,
22606 /*defer_ok*/false);
22607 if (specs && specs != error_mark_node)
22608 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22609 specs);
22611 /* Merge parameter declarations. */
22612 decl_parm = skip_artificial_parms_for (decl,
22613 DECL_ARGUMENTS (decl));
22614 pattern_parm
22615 = skip_artificial_parms_for (code_pattern,
22616 DECL_ARGUMENTS (code_pattern));
22617 while (decl_parm && !DECL_PACK_P (pattern_parm))
22619 tree parm_type;
22620 tree attributes;
22622 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22623 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22624 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22625 NULL_TREE);
22626 parm_type = type_decays_to (parm_type);
22627 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22628 TREE_TYPE (decl_parm) = parm_type;
22629 attributes = DECL_ATTRIBUTES (pattern_parm);
22630 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22632 DECL_ATTRIBUTES (decl_parm) = attributes;
22633 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22635 decl_parm = DECL_CHAIN (decl_parm);
22636 pattern_parm = DECL_CHAIN (pattern_parm);
22638 /* Merge any parameters that match with the function parameter
22639 pack. */
22640 if (pattern_parm && DECL_PACK_P (pattern_parm))
22642 int i, len;
22643 tree expanded_types;
22644 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22645 the parameters in this function parameter pack. */
22646 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22647 args, tf_error, NULL_TREE);
22648 len = TREE_VEC_LENGTH (expanded_types);
22649 for (i = 0; i < len; i++)
22651 tree parm_type;
22652 tree attributes;
22654 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22655 /* Rename the parameter to include the index. */
22656 DECL_NAME (decl_parm) =
22657 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22658 parm_type = TREE_VEC_ELT (expanded_types, i);
22659 parm_type = type_decays_to (parm_type);
22660 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22661 TREE_TYPE (decl_parm) = parm_type;
22662 attributes = DECL_ATTRIBUTES (pattern_parm);
22663 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22665 DECL_ATTRIBUTES (decl_parm) = attributes;
22666 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22668 decl_parm = DECL_CHAIN (decl_parm);
22671 /* Merge additional specifiers from the CODE_PATTERN. */
22672 if (DECL_DECLARED_INLINE_P (code_pattern)
22673 && !DECL_DECLARED_INLINE_P (decl))
22674 DECL_DECLARED_INLINE_P (decl) = 1;
22676 else if (VAR_P (decl))
22678 start_lambda_scope (decl);
22679 DECL_INITIAL (decl) =
22680 tsubst_expr (DECL_INITIAL (code_pattern), args,
22681 tf_error, DECL_TI_TEMPLATE (decl),
22682 /*integral_constant_expression_p=*/false);
22683 finish_lambda_scope ();
22684 if (VAR_HAD_UNKNOWN_BOUND (decl))
22685 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22686 tf_error, DECL_TI_TEMPLATE (decl));
22688 else
22689 gcc_unreachable ();
22691 pop_access_scope (decl);
22694 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22695 substituted to get DECL. */
22697 tree
22698 template_for_substitution (tree decl)
22700 tree tmpl = DECL_TI_TEMPLATE (decl);
22702 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22703 for the instantiation. This is not always the most general
22704 template. Consider, for example:
22706 template <class T>
22707 struct S { template <class U> void f();
22708 template <> void f<int>(); };
22710 and an instantiation of S<double>::f<int>. We want TD to be the
22711 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22712 while (/* An instantiation cannot have a definition, so we need a
22713 more general template. */
22714 DECL_TEMPLATE_INSTANTIATION (tmpl)
22715 /* We must also deal with friend templates. Given:
22717 template <class T> struct S {
22718 template <class U> friend void f() {};
22721 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22722 so far as the language is concerned, but that's still
22723 where we get the pattern for the instantiation from. On
22724 other hand, if the definition comes outside the class, say:
22726 template <class T> struct S {
22727 template <class U> friend void f();
22729 template <class U> friend void f() {}
22731 we don't need to look any further. That's what the check for
22732 DECL_INITIAL is for. */
22733 || (TREE_CODE (decl) == FUNCTION_DECL
22734 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22735 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22737 /* The present template, TD, should not be a definition. If it
22738 were a definition, we should be using it! Note that we
22739 cannot restructure the loop to just keep going until we find
22740 a template with a definition, since that might go too far if
22741 a specialization was declared, but not defined. */
22743 /* Fetch the more general template. */
22744 tmpl = DECL_TI_TEMPLATE (tmpl);
22747 return tmpl;
22750 /* Returns true if we need to instantiate this template instance even if we
22751 know we aren't going to emit it. */
22753 bool
22754 always_instantiate_p (tree decl)
22756 /* We always instantiate inline functions so that we can inline them. An
22757 explicit instantiation declaration prohibits implicit instantiation of
22758 non-inline functions. With high levels of optimization, we would
22759 normally inline non-inline functions -- but we're not allowed to do
22760 that for "extern template" functions. Therefore, we check
22761 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22762 return ((TREE_CODE (decl) == FUNCTION_DECL
22763 && (DECL_DECLARED_INLINE_P (decl)
22764 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22765 /* And we need to instantiate static data members so that
22766 their initializers are available in integral constant
22767 expressions. */
22768 || (VAR_P (decl)
22769 && decl_maybe_constant_var_p (decl)));
22772 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22773 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22774 error, true otherwise. */
22776 bool
22777 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22779 tree fntype, spec, noex, clone;
22781 /* Don't instantiate a noexcept-specification from template context. */
22782 if (processing_template_decl)
22783 return true;
22785 if (DECL_CLONED_FUNCTION_P (fn))
22786 fn = DECL_CLONED_FUNCTION (fn);
22787 fntype = TREE_TYPE (fn);
22788 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22790 if (!spec || !TREE_PURPOSE (spec))
22791 return true;
22793 noex = TREE_PURPOSE (spec);
22795 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22797 static hash_set<tree>* fns = new hash_set<tree>;
22798 bool added = false;
22799 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22800 spec = get_defaulted_eh_spec (fn, complain);
22801 else if (!(added = !fns->add (fn)))
22803 /* If hash_set::add returns true, the element was already there. */
22804 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22805 DECL_SOURCE_LOCATION (fn));
22806 error_at (loc,
22807 "exception specification of %qD depends on itself",
22808 fn);
22809 spec = noexcept_false_spec;
22811 else if (push_tinst_level (fn))
22813 push_access_scope (fn);
22814 push_deferring_access_checks (dk_no_deferred);
22815 input_location = DECL_SOURCE_LOCATION (fn);
22816 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22817 DEFERRED_NOEXCEPT_ARGS (noex),
22818 tf_warning_or_error, fn,
22819 /*function_p=*/false,
22820 /*integral_constant_expression_p=*/true);
22821 pop_deferring_access_checks ();
22822 pop_access_scope (fn);
22823 pop_tinst_level ();
22824 spec = build_noexcept_spec (noex, tf_warning_or_error);
22825 if (spec == error_mark_node)
22826 spec = noexcept_false_spec;
22828 else
22829 spec = noexcept_false_spec;
22831 if (added)
22832 fns->remove (fn);
22834 if (spec == error_mark_node)
22835 return false;
22837 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22840 FOR_EACH_CLONE (clone, fn)
22842 if (TREE_TYPE (clone) == fntype)
22843 TREE_TYPE (clone) = TREE_TYPE (fn);
22844 else
22845 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22848 return true;
22851 /* We're starting to process the function INST, an instantiation of PATTERN;
22852 add their parameters to local_specializations. */
22854 static void
22855 register_parameter_specializations (tree pattern, tree inst)
22857 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22858 tree spec_parm = DECL_ARGUMENTS (inst);
22859 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22861 register_local_specialization (spec_parm, tmpl_parm);
22862 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22863 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22865 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22867 if (!DECL_PACK_P (tmpl_parm))
22869 register_local_specialization (spec_parm, tmpl_parm);
22870 spec_parm = DECL_CHAIN (spec_parm);
22872 else
22874 /* Register the (value) argument pack as a specialization of
22875 TMPL_PARM, then move on. */
22876 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22877 register_local_specialization (argpack, tmpl_parm);
22880 gcc_assert (!spec_parm);
22883 /* Produce the definition of D, a _DECL generated from a template. If
22884 DEFER_OK is true, then we don't have to actually do the
22885 instantiation now; we just have to do it sometime. Normally it is
22886 an error if this is an explicit instantiation but D is undefined.
22887 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22888 instantiated class template. */
22890 tree
22891 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22893 tree tmpl = DECL_TI_TEMPLATE (d);
22894 tree gen_args;
22895 tree args;
22896 tree td;
22897 tree code_pattern;
22898 tree spec;
22899 tree gen_tmpl;
22900 bool pattern_defined;
22901 location_t saved_loc = input_location;
22902 int saved_unevaluated_operand = cp_unevaluated_operand;
22903 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22904 bool external_p;
22905 bool deleted_p;
22907 /* This function should only be used to instantiate templates for
22908 functions and static member variables. */
22909 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22911 /* A concept is never instantiated. */
22912 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22914 /* Variables are never deferred; if instantiation is required, they
22915 are instantiated right away. That allows for better code in the
22916 case that an expression refers to the value of the variable --
22917 if the variable has a constant value the referring expression can
22918 take advantage of that fact. */
22919 if (VAR_P (d))
22920 defer_ok = false;
22922 /* Don't instantiate cloned functions. Instead, instantiate the
22923 functions they cloned. */
22924 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22925 d = DECL_CLONED_FUNCTION (d);
22927 if (DECL_TEMPLATE_INSTANTIATED (d)
22928 || (TREE_CODE (d) == FUNCTION_DECL
22929 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22930 || DECL_TEMPLATE_SPECIALIZATION (d))
22931 /* D has already been instantiated or explicitly specialized, so
22932 there's nothing for us to do here.
22934 It might seem reasonable to check whether or not D is an explicit
22935 instantiation, and, if so, stop here. But when an explicit
22936 instantiation is deferred until the end of the compilation,
22937 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22938 the instantiation. */
22939 return d;
22941 /* Check to see whether we know that this template will be
22942 instantiated in some other file, as with "extern template"
22943 extension. */
22944 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22946 /* In general, we do not instantiate such templates. */
22947 if (external_p && !always_instantiate_p (d))
22948 return d;
22950 gen_tmpl = most_general_template (tmpl);
22951 gen_args = DECL_TI_ARGS (d);
22953 if (tmpl != gen_tmpl)
22954 /* We should already have the extra args. */
22955 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22956 == TMPL_ARGS_DEPTH (gen_args));
22957 /* And what's in the hash table should match D. */
22958 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22959 || spec == NULL_TREE);
22961 /* This needs to happen before any tsubsting. */
22962 if (! push_tinst_level (d))
22963 return d;
22965 timevar_push (TV_TEMPLATE_INST);
22967 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22968 for the instantiation. */
22969 td = template_for_substitution (d);
22970 args = gen_args;
22972 if (VAR_P (d))
22974 /* Look up an explicit specialization, if any. */
22975 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22976 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22977 if (elt && elt != error_mark_node)
22979 td = TREE_VALUE (elt);
22980 args = TREE_PURPOSE (elt);
22984 code_pattern = DECL_TEMPLATE_RESULT (td);
22986 /* We should never be trying to instantiate a member of a class
22987 template or partial specialization. */
22988 gcc_assert (d != code_pattern);
22990 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22991 || DECL_TEMPLATE_SPECIALIZATION (td))
22992 /* In the case of a friend template whose definition is provided
22993 outside the class, we may have too many arguments. Drop the
22994 ones we don't need. The same is true for specializations. */
22995 args = get_innermost_template_args
22996 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22998 if (TREE_CODE (d) == FUNCTION_DECL)
23000 deleted_p = DECL_DELETED_FN (code_pattern);
23001 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23002 && DECL_INITIAL (code_pattern) != error_mark_node)
23003 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23004 || deleted_p);
23006 else
23008 deleted_p = false;
23009 if (DECL_CLASS_SCOPE_P (code_pattern))
23010 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23011 || DECL_INLINE_VAR_P (code_pattern));
23012 else
23013 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23016 /* We may be in the middle of deferred access check. Disable it now. */
23017 push_deferring_access_checks (dk_no_deferred);
23019 /* Unless an explicit instantiation directive has already determined
23020 the linkage of D, remember that a definition is available for
23021 this entity. */
23022 if (pattern_defined
23023 && !DECL_INTERFACE_KNOWN (d)
23024 && !DECL_NOT_REALLY_EXTERN (d))
23025 mark_definable (d);
23027 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23028 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23029 input_location = DECL_SOURCE_LOCATION (d);
23031 /* If D is a member of an explicitly instantiated class template,
23032 and no definition is available, treat it like an implicit
23033 instantiation. */
23034 if (!pattern_defined && expl_inst_class_mem_p
23035 && DECL_EXPLICIT_INSTANTIATION (d))
23037 /* Leave linkage flags alone on instantiations with anonymous
23038 visibility. */
23039 if (TREE_PUBLIC (d))
23041 DECL_NOT_REALLY_EXTERN (d) = 0;
23042 DECL_INTERFACE_KNOWN (d) = 0;
23044 SET_DECL_IMPLICIT_INSTANTIATION (d);
23047 /* Defer all other templates, unless we have been explicitly
23048 forbidden from doing so. */
23049 if (/* If there is no definition, we cannot instantiate the
23050 template. */
23051 ! pattern_defined
23052 /* If it's OK to postpone instantiation, do so. */
23053 || defer_ok
23054 /* If this is a static data member that will be defined
23055 elsewhere, we don't want to instantiate the entire data
23056 member, but we do want to instantiate the initializer so that
23057 we can substitute that elsewhere. */
23058 || (external_p && VAR_P (d))
23059 /* Handle here a deleted function too, avoid generating
23060 its body (c++/61080). */
23061 || deleted_p)
23063 /* The definition of the static data member is now required so
23064 we must substitute the initializer. */
23065 if (VAR_P (d)
23066 && !DECL_INITIAL (d)
23067 && DECL_INITIAL (code_pattern))
23069 tree ns;
23070 tree init;
23071 bool const_init = false;
23072 bool enter_context = DECL_CLASS_SCOPE_P (d);
23074 ns = decl_namespace_context (d);
23075 push_nested_namespace (ns);
23076 if (enter_context)
23077 push_nested_class (DECL_CONTEXT (d));
23078 init = tsubst_expr (DECL_INITIAL (code_pattern),
23079 args,
23080 tf_warning_or_error, NULL_TREE,
23081 /*integral_constant_expression_p=*/false);
23082 /* If instantiating the initializer involved instantiating this
23083 again, don't call cp_finish_decl twice. */
23084 if (!DECL_INITIAL (d))
23086 /* Make sure the initializer is still constant, in case of
23087 circular dependency (template/instantiate6.C). */
23088 const_init
23089 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23090 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23091 /*asmspec_tree=*/NULL_TREE,
23092 LOOKUP_ONLYCONVERTING);
23094 if (enter_context)
23095 pop_nested_class ();
23096 pop_nested_namespace (ns);
23099 /* We restore the source position here because it's used by
23100 add_pending_template. */
23101 input_location = saved_loc;
23103 if (at_eof && !pattern_defined
23104 && DECL_EXPLICIT_INSTANTIATION (d)
23105 && DECL_NOT_REALLY_EXTERN (d))
23106 /* [temp.explicit]
23108 The definition of a non-exported function template, a
23109 non-exported member function template, or a non-exported
23110 member function or static data member of a class template
23111 shall be present in every translation unit in which it is
23112 explicitly instantiated. */
23113 permerror (input_location, "explicit instantiation of %qD "
23114 "but no definition available", d);
23116 /* If we're in unevaluated context, we just wanted to get the
23117 constant value; this isn't an odr use, so don't queue
23118 a full instantiation. */
23119 if (cp_unevaluated_operand != 0)
23120 goto out;
23121 /* ??? Historically, we have instantiated inline functions, even
23122 when marked as "extern template". */
23123 if (!(external_p && VAR_P (d)))
23124 add_pending_template (d);
23125 goto out;
23127 /* Tell the repository that D is available in this translation unit
23128 -- and see if it is supposed to be instantiated here. */
23129 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23131 /* In a PCH file, despite the fact that the repository hasn't
23132 requested instantiation in the PCH it is still possible that
23133 an instantiation will be required in a file that includes the
23134 PCH. */
23135 if (pch_file)
23136 add_pending_template (d);
23137 /* Instantiate inline functions so that the inliner can do its
23138 job, even though we'll not be emitting a copy of this
23139 function. */
23140 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23141 goto out;
23144 bool push_to_top, nested;
23145 tree fn_context;
23146 fn_context = decl_function_context (d);
23147 nested = current_function_decl != NULL_TREE;
23148 push_to_top = !(nested && fn_context == current_function_decl);
23150 vec<tree> omp_privatization_save;
23151 if (nested)
23152 save_omp_privatization_clauses (omp_privatization_save);
23154 if (push_to_top)
23155 push_to_top_level ();
23156 else
23158 push_function_context ();
23159 cp_unevaluated_operand = 0;
23160 c_inhibit_evaluation_warnings = 0;
23163 /* Mark D as instantiated so that recursive calls to
23164 instantiate_decl do not try to instantiate it again. */
23165 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23167 /* Regenerate the declaration in case the template has been modified
23168 by a subsequent redeclaration. */
23169 regenerate_decl_from_template (d, td, args);
23171 /* We already set the file and line above. Reset them now in case
23172 they changed as a result of calling regenerate_decl_from_template. */
23173 input_location = DECL_SOURCE_LOCATION (d);
23175 if (VAR_P (d))
23177 tree init;
23178 bool const_init = false;
23180 /* Clear out DECL_RTL; whatever was there before may not be right
23181 since we've reset the type of the declaration. */
23182 SET_DECL_RTL (d, NULL);
23183 DECL_IN_AGGR_P (d) = 0;
23185 /* The initializer is placed in DECL_INITIAL by
23186 regenerate_decl_from_template so we don't need to
23187 push/pop_access_scope again here. Pull it out so that
23188 cp_finish_decl can process it. */
23189 init = DECL_INITIAL (d);
23190 DECL_INITIAL (d) = NULL_TREE;
23191 DECL_INITIALIZED_P (d) = 0;
23193 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23194 initializer. That function will defer actual emission until
23195 we have a chance to determine linkage. */
23196 DECL_EXTERNAL (d) = 0;
23198 /* Enter the scope of D so that access-checking works correctly. */
23199 bool enter_context = DECL_CLASS_SCOPE_P (d);
23200 if (enter_context)
23201 push_nested_class (DECL_CONTEXT (d));
23203 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23204 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23206 if (enter_context)
23207 pop_nested_class ();
23209 if (variable_template_p (gen_tmpl))
23210 note_variable_template_instantiation (d);
23212 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23213 synthesize_method (d);
23214 else if (TREE_CODE (d) == FUNCTION_DECL)
23216 /* Set up the list of local specializations. */
23217 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23218 tree block = NULL_TREE;
23220 /* Set up context. */
23221 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23222 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23223 block = push_stmt_list ();
23224 else
23225 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23227 /* Some typedefs referenced from within the template code need to be
23228 access checked at template instantiation time, i.e now. These
23229 types were added to the template at parsing time. Let's get those
23230 and perform the access checks then. */
23231 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23232 args);
23234 /* Create substitution entries for the parameters. */
23235 register_parameter_specializations (code_pattern, d);
23237 /* Substitute into the body of the function. */
23238 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23239 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23240 tf_warning_or_error, tmpl);
23241 else
23243 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23244 tf_warning_or_error, tmpl,
23245 /*integral_constant_expression_p=*/false);
23247 /* Set the current input_location to the end of the function
23248 so that finish_function knows where we are. */
23249 input_location
23250 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23252 /* Remember if we saw an infinite loop in the template. */
23253 current_function_infinite_loop
23254 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23257 /* Finish the function. */
23258 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23259 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23260 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23261 else
23263 d = finish_function (/*inline_p=*/false);
23264 expand_or_defer_fn (d);
23267 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23268 cp_check_omp_declare_reduction (d);
23271 /* We're not deferring instantiation any more. */
23272 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23274 if (push_to_top)
23275 pop_from_top_level ();
23276 else
23277 pop_function_context ();
23279 if (nested)
23280 restore_omp_privatization_clauses (omp_privatization_save);
23282 out:
23283 pop_deferring_access_checks ();
23284 timevar_pop (TV_TEMPLATE_INST);
23285 pop_tinst_level ();
23286 input_location = saved_loc;
23287 cp_unevaluated_operand = saved_unevaluated_operand;
23288 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23290 return d;
23293 /* Run through the list of templates that we wish we could
23294 instantiate, and instantiate any we can. RETRIES is the
23295 number of times we retry pending template instantiation. */
23297 void
23298 instantiate_pending_templates (int retries)
23300 int reconsider;
23301 location_t saved_loc = input_location;
23303 /* Instantiating templates may trigger vtable generation. This in turn
23304 may require further template instantiations. We place a limit here
23305 to avoid infinite loop. */
23306 if (pending_templates && retries >= max_tinst_depth)
23308 tree decl = pending_templates->tinst->decl;
23310 fatal_error (input_location,
23311 "template instantiation depth exceeds maximum of %d"
23312 " instantiating %q+D, possibly from virtual table generation"
23313 " (use -ftemplate-depth= to increase the maximum)",
23314 max_tinst_depth, decl);
23315 if (TREE_CODE (decl) == FUNCTION_DECL)
23316 /* Pretend that we defined it. */
23317 DECL_INITIAL (decl) = error_mark_node;
23318 return;
23323 struct pending_template **t = &pending_templates;
23324 struct pending_template *last = NULL;
23325 reconsider = 0;
23326 while (*t)
23328 tree instantiation = reopen_tinst_level ((*t)->tinst);
23329 bool complete = false;
23331 if (TYPE_P (instantiation))
23333 if (!COMPLETE_TYPE_P (instantiation))
23335 instantiate_class_template (instantiation);
23336 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23337 for (tree fld = TYPE_FIELDS (instantiation);
23338 fld; fld = TREE_CHAIN (fld))
23339 if ((VAR_P (fld)
23340 || (TREE_CODE (fld) == FUNCTION_DECL
23341 && !DECL_ARTIFICIAL (fld)))
23342 && DECL_TEMPLATE_INSTANTIATION (fld))
23343 instantiate_decl (fld,
23344 /*defer_ok=*/false,
23345 /*expl_inst_class_mem_p=*/false);
23347 if (COMPLETE_TYPE_P (instantiation))
23348 reconsider = 1;
23351 complete = COMPLETE_TYPE_P (instantiation);
23353 else
23355 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23356 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23358 instantiation
23359 = instantiate_decl (instantiation,
23360 /*defer_ok=*/false,
23361 /*expl_inst_class_mem_p=*/false);
23362 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23363 reconsider = 1;
23366 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23367 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23370 if (complete)
23371 /* If INSTANTIATION has been instantiated, then we don't
23372 need to consider it again in the future. */
23373 *t = (*t)->next;
23374 else
23376 last = *t;
23377 t = &(*t)->next;
23379 tinst_depth = 0;
23380 current_tinst_level = NULL;
23382 last_pending_template = last;
23384 while (reconsider);
23386 input_location = saved_loc;
23389 /* Substitute ARGVEC into T, which is a list of initializers for
23390 either base class or a non-static data member. The TREE_PURPOSEs
23391 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23392 instantiate_decl. */
23394 static tree
23395 tsubst_initializer_list (tree t, tree argvec)
23397 tree inits = NULL_TREE;
23399 for (; t; t = TREE_CHAIN (t))
23401 tree decl;
23402 tree init;
23403 tree expanded_bases = NULL_TREE;
23404 tree expanded_arguments = NULL_TREE;
23405 int i, len = 1;
23407 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23409 tree expr;
23410 tree arg;
23412 /* Expand the base class expansion type into separate base
23413 classes. */
23414 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23415 tf_warning_or_error,
23416 NULL_TREE);
23417 if (expanded_bases == error_mark_node)
23418 continue;
23420 /* We'll be building separate TREE_LISTs of arguments for
23421 each base. */
23422 len = TREE_VEC_LENGTH (expanded_bases);
23423 expanded_arguments = make_tree_vec (len);
23424 for (i = 0; i < len; i++)
23425 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23427 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23428 expand each argument in the TREE_VALUE of t. */
23429 expr = make_node (EXPR_PACK_EXPANSION);
23430 PACK_EXPANSION_LOCAL_P (expr) = true;
23431 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23432 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23434 if (TREE_VALUE (t) == void_type_node)
23435 /* VOID_TYPE_NODE is used to indicate
23436 value-initialization. */
23438 for (i = 0; i < len; i++)
23439 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23441 else
23443 /* Substitute parameter packs into each argument in the
23444 TREE_LIST. */
23445 in_base_initializer = 1;
23446 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23448 tree expanded_exprs;
23450 /* Expand the argument. */
23451 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23452 expanded_exprs
23453 = tsubst_pack_expansion (expr, argvec,
23454 tf_warning_or_error,
23455 NULL_TREE);
23456 if (expanded_exprs == error_mark_node)
23457 continue;
23459 /* Prepend each of the expanded expressions to the
23460 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23461 for (i = 0; i < len; i++)
23463 TREE_VEC_ELT (expanded_arguments, i) =
23464 tree_cons (NULL_TREE,
23465 TREE_VEC_ELT (expanded_exprs, i),
23466 TREE_VEC_ELT (expanded_arguments, i));
23469 in_base_initializer = 0;
23471 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23472 since we built them backwards. */
23473 for (i = 0; i < len; i++)
23475 TREE_VEC_ELT (expanded_arguments, i) =
23476 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23481 for (i = 0; i < len; ++i)
23483 if (expanded_bases)
23485 decl = TREE_VEC_ELT (expanded_bases, i);
23486 decl = expand_member_init (decl);
23487 init = TREE_VEC_ELT (expanded_arguments, i);
23489 else
23491 tree tmp;
23492 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23493 tf_warning_or_error, NULL_TREE);
23495 decl = expand_member_init (decl);
23496 if (decl && !DECL_P (decl))
23497 in_base_initializer = 1;
23499 init = TREE_VALUE (t);
23500 tmp = init;
23501 if (init != void_type_node)
23502 init = tsubst_expr (init, argvec,
23503 tf_warning_or_error, NULL_TREE,
23504 /*integral_constant_expression_p=*/false);
23505 if (init == NULL_TREE && tmp != NULL_TREE)
23506 /* If we had an initializer but it instantiated to nothing,
23507 value-initialize the object. This will only occur when
23508 the initializer was a pack expansion where the parameter
23509 packs used in that expansion were of length zero. */
23510 init = void_type_node;
23511 in_base_initializer = 0;
23514 if (decl)
23516 init = build_tree_list (decl, init);
23517 TREE_CHAIN (init) = inits;
23518 inits = init;
23522 return inits;
23525 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23527 static void
23528 set_current_access_from_decl (tree decl)
23530 if (TREE_PRIVATE (decl))
23531 current_access_specifier = access_private_node;
23532 else if (TREE_PROTECTED (decl))
23533 current_access_specifier = access_protected_node;
23534 else
23535 current_access_specifier = access_public_node;
23538 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23539 is the instantiation (which should have been created with
23540 start_enum) and ARGS are the template arguments to use. */
23542 static void
23543 tsubst_enum (tree tag, tree newtag, tree args)
23545 tree e;
23547 if (SCOPED_ENUM_P (newtag))
23548 begin_scope (sk_scoped_enum, newtag);
23550 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23552 tree value;
23553 tree decl;
23555 decl = TREE_VALUE (e);
23556 /* Note that in a template enum, the TREE_VALUE is the
23557 CONST_DECL, not the corresponding INTEGER_CST. */
23558 value = tsubst_expr (DECL_INITIAL (decl),
23559 args, tf_warning_or_error, NULL_TREE,
23560 /*integral_constant_expression_p=*/true);
23562 /* Give this enumeration constant the correct access. */
23563 set_current_access_from_decl (decl);
23565 /* Actually build the enumerator itself. Here we're assuming that
23566 enumerators can't have dependent attributes. */
23567 build_enumerator (DECL_NAME (decl), value, newtag,
23568 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23571 if (SCOPED_ENUM_P (newtag))
23572 finish_scope ();
23574 finish_enum_value_list (newtag);
23575 finish_enum (newtag);
23577 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23578 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23581 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23582 its type -- but without substituting the innermost set of template
23583 arguments. So, innermost set of template parameters will appear in
23584 the type. */
23586 tree
23587 get_mostly_instantiated_function_type (tree decl)
23589 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23590 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23593 /* Return truthvalue if we're processing a template different from
23594 the last one involved in diagnostics. */
23595 bool
23596 problematic_instantiation_changed (void)
23598 return current_tinst_level != last_error_tinst_level;
23601 /* Remember current template involved in diagnostics. */
23602 void
23603 record_last_problematic_instantiation (void)
23605 last_error_tinst_level = current_tinst_level;
23608 struct tinst_level *
23609 current_instantiation (void)
23611 return current_tinst_level;
23614 /* Return TRUE if current_function_decl is being instantiated, false
23615 otherwise. */
23617 bool
23618 instantiating_current_function_p (void)
23620 return (current_instantiation ()
23621 && current_instantiation ()->decl == current_function_decl);
23624 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23625 type. Return false for ok, true for disallowed. Issue error and
23626 inform messages under control of COMPLAIN. */
23628 static bool
23629 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23631 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23632 return false;
23633 else if (POINTER_TYPE_P (type))
23634 return false;
23635 else if (TYPE_PTRMEM_P (type))
23636 return false;
23637 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23638 return false;
23639 else if (TREE_CODE (type) == TYPENAME_TYPE)
23640 return false;
23641 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23642 return false;
23643 else if (TREE_CODE (type) == NULLPTR_TYPE)
23644 return false;
23645 /* A bound template template parm could later be instantiated to have a valid
23646 nontype parm type via an alias template. */
23647 else if (cxx_dialect >= cxx11
23648 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23649 return false;
23651 if (complain & tf_error)
23653 if (type == error_mark_node)
23654 inform (input_location, "invalid template non-type parameter");
23655 else
23656 error ("%q#T is not a valid type for a template non-type parameter",
23657 type);
23659 return true;
23662 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23663 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23665 static bool
23666 dependent_type_p_r (tree type)
23668 tree scope;
23670 /* [temp.dep.type]
23672 A type is dependent if it is:
23674 -- a template parameter. Template template parameters are types
23675 for us (since TYPE_P holds true for them) so we handle
23676 them here. */
23677 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23678 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23679 return true;
23680 /* -- a qualified-id with a nested-name-specifier which contains a
23681 class-name that names a dependent type or whose unqualified-id
23682 names a dependent type. */
23683 if (TREE_CODE (type) == TYPENAME_TYPE)
23684 return true;
23686 /* An alias template specialization can be dependent even if the
23687 resulting type is not. */
23688 if (dependent_alias_template_spec_p (type))
23689 return true;
23691 /* -- a cv-qualified type where the cv-unqualified type is
23692 dependent.
23693 No code is necessary for this bullet; the code below handles
23694 cv-qualified types, and we don't want to strip aliases with
23695 TYPE_MAIN_VARIANT because of DR 1558. */
23696 /* -- a compound type constructed from any dependent type. */
23697 if (TYPE_PTRMEM_P (type))
23698 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23699 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23700 (type)));
23701 else if (TYPE_PTR_P (type)
23702 || TREE_CODE (type) == REFERENCE_TYPE)
23703 return dependent_type_p (TREE_TYPE (type));
23704 else if (TREE_CODE (type) == FUNCTION_TYPE
23705 || TREE_CODE (type) == METHOD_TYPE)
23707 tree arg_type;
23709 if (dependent_type_p (TREE_TYPE (type)))
23710 return true;
23711 for (arg_type = TYPE_ARG_TYPES (type);
23712 arg_type;
23713 arg_type = TREE_CHAIN (arg_type))
23714 if (dependent_type_p (TREE_VALUE (arg_type)))
23715 return true;
23716 if (cxx_dialect >= cxx17)
23718 /* A value-dependent noexcept-specifier makes the type dependent. */
23719 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23720 if (spec && TREE_PURPOSE (spec)
23721 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23722 return true;
23724 return false;
23726 /* -- an array type constructed from any dependent type or whose
23727 size is specified by a constant expression that is
23728 value-dependent.
23730 We checked for type- and value-dependence of the bounds in
23731 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23732 if (TREE_CODE (type) == ARRAY_TYPE)
23734 if (TYPE_DOMAIN (type)
23735 && dependent_type_p (TYPE_DOMAIN (type)))
23736 return true;
23737 return dependent_type_p (TREE_TYPE (type));
23740 /* -- a template-id in which either the template name is a template
23741 parameter ... */
23742 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23743 return true;
23744 /* ... or any of the template arguments is a dependent type or
23745 an expression that is type-dependent or value-dependent. */
23746 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23747 && (any_dependent_template_arguments_p
23748 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23749 return true;
23751 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23752 dependent; if the argument of the `typeof' expression is not
23753 type-dependent, then it should already been have resolved. */
23754 if (TREE_CODE (type) == TYPEOF_TYPE
23755 || TREE_CODE (type) == DECLTYPE_TYPE
23756 || TREE_CODE (type) == UNDERLYING_TYPE)
23757 return true;
23759 /* A template argument pack is dependent if any of its packed
23760 arguments are. */
23761 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23763 tree args = ARGUMENT_PACK_ARGS (type);
23764 int i, len = TREE_VEC_LENGTH (args);
23765 for (i = 0; i < len; ++i)
23766 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23767 return true;
23770 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23771 be template parameters. */
23772 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23773 return true;
23775 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23776 return true;
23778 /* The standard does not specifically mention types that are local
23779 to template functions or local classes, but they should be
23780 considered dependent too. For example:
23782 template <int I> void f() {
23783 enum E { a = I };
23784 S<sizeof (E)> s;
23787 The size of `E' cannot be known until the value of `I' has been
23788 determined. Therefore, `E' must be considered dependent. */
23789 scope = TYPE_CONTEXT (type);
23790 if (scope && TYPE_P (scope))
23791 return dependent_type_p (scope);
23792 /* Don't use type_dependent_expression_p here, as it can lead
23793 to infinite recursion trying to determine whether a lambda
23794 nested in a lambda is dependent (c++/47687). */
23795 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23796 && DECL_LANG_SPECIFIC (scope)
23797 && DECL_TEMPLATE_INFO (scope)
23798 && (any_dependent_template_arguments_p
23799 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23800 return true;
23802 /* Other types are non-dependent. */
23803 return false;
23806 /* Returns TRUE if TYPE is dependent, in the sense of
23807 [temp.dep.type]. Note that a NULL type is considered dependent. */
23809 bool
23810 dependent_type_p (tree type)
23812 /* If there are no template parameters in scope, then there can't be
23813 any dependent types. */
23814 if (!processing_template_decl)
23816 /* If we are not processing a template, then nobody should be
23817 providing us with a dependent type. */
23818 gcc_assert (type);
23819 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23820 return false;
23823 /* If the type is NULL, we have not computed a type for the entity
23824 in question; in that case, the type is dependent. */
23825 if (!type)
23826 return true;
23828 /* Erroneous types can be considered non-dependent. */
23829 if (type == error_mark_node)
23830 return false;
23832 /* Getting here with global_type_node means we improperly called this
23833 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23834 gcc_checking_assert (type != global_type_node);
23836 /* If we have not already computed the appropriate value for TYPE,
23837 do so now. */
23838 if (!TYPE_DEPENDENT_P_VALID (type))
23840 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23841 TYPE_DEPENDENT_P_VALID (type) = 1;
23844 return TYPE_DEPENDENT_P (type);
23847 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23848 lookup. In other words, a dependent type that is not the current
23849 instantiation. */
23851 bool
23852 dependent_scope_p (tree scope)
23854 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23855 && !currently_open_class (scope));
23858 /* T is a SCOPE_REF; return whether we need to consider it
23859 instantiation-dependent so that we can check access at instantiation
23860 time even though we know which member it resolves to. */
23862 static bool
23863 instantiation_dependent_scope_ref_p (tree t)
23865 if (DECL_P (TREE_OPERAND (t, 1))
23866 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23867 && accessible_in_template_p (TREE_OPERAND (t, 0),
23868 TREE_OPERAND (t, 1)))
23869 return false;
23870 else
23871 return true;
23874 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23875 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23876 expression. */
23878 /* Note that this predicate is not appropriate for general expressions;
23879 only constant expressions (that satisfy potential_constant_expression)
23880 can be tested for value dependence. */
23882 bool
23883 value_dependent_expression_p (tree expression)
23885 if (!processing_template_decl || expression == NULL_TREE)
23886 return false;
23888 /* A name declared with a dependent type. */
23889 if (DECL_P (expression) && type_dependent_expression_p (expression))
23890 return true;
23892 switch (TREE_CODE (expression))
23894 case BASELINK:
23895 /* A dependent member function of the current instantiation. */
23896 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23898 case FUNCTION_DECL:
23899 /* A dependent member function of the current instantiation. */
23900 if (DECL_CLASS_SCOPE_P (expression)
23901 && dependent_type_p (DECL_CONTEXT (expression)))
23902 return true;
23903 break;
23905 case IDENTIFIER_NODE:
23906 /* A name that has not been looked up -- must be dependent. */
23907 return true;
23909 case TEMPLATE_PARM_INDEX:
23910 /* A non-type template parm. */
23911 return true;
23913 case CONST_DECL:
23914 /* A non-type template parm. */
23915 if (DECL_TEMPLATE_PARM_P (expression))
23916 return true;
23917 return value_dependent_expression_p (DECL_INITIAL (expression));
23919 case VAR_DECL:
23920 /* A constant with literal type and is initialized
23921 with an expression that is value-dependent.
23923 Note that a non-dependent parenthesized initializer will have
23924 already been replaced with its constant value, so if we see
23925 a TREE_LIST it must be dependent. */
23926 if (DECL_INITIAL (expression)
23927 && decl_constant_var_p (expression)
23928 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23929 /* cp_finish_decl doesn't fold reference initializers. */
23930 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23931 || type_dependent_expression_p (DECL_INITIAL (expression))
23932 || value_dependent_expression_p (DECL_INITIAL (expression))))
23933 return true;
23934 if (DECL_HAS_VALUE_EXPR_P (expression))
23936 tree value_expr = DECL_VALUE_EXPR (expression);
23937 if (type_dependent_expression_p (value_expr))
23938 return true;
23940 return false;
23942 case DYNAMIC_CAST_EXPR:
23943 case STATIC_CAST_EXPR:
23944 case CONST_CAST_EXPR:
23945 case REINTERPRET_CAST_EXPR:
23946 case CAST_EXPR:
23947 /* These expressions are value-dependent if the type to which
23948 the cast occurs is dependent or the expression being casted
23949 is value-dependent. */
23951 tree type = TREE_TYPE (expression);
23953 if (dependent_type_p (type))
23954 return true;
23956 /* A functional cast has a list of operands. */
23957 expression = TREE_OPERAND (expression, 0);
23958 if (!expression)
23960 /* If there are no operands, it must be an expression such
23961 as "int()". This should not happen for aggregate types
23962 because it would form non-constant expressions. */
23963 gcc_assert (cxx_dialect >= cxx11
23964 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23966 return false;
23969 if (TREE_CODE (expression) == TREE_LIST)
23970 return any_value_dependent_elements_p (expression);
23972 return value_dependent_expression_p (expression);
23975 case SIZEOF_EXPR:
23976 if (SIZEOF_EXPR_TYPE_P (expression))
23977 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23978 /* FALLTHRU */
23979 case ALIGNOF_EXPR:
23980 case TYPEID_EXPR:
23981 /* A `sizeof' expression is value-dependent if the operand is
23982 type-dependent or is a pack expansion. */
23983 expression = TREE_OPERAND (expression, 0);
23984 if (PACK_EXPANSION_P (expression))
23985 return true;
23986 else if (TYPE_P (expression))
23987 return dependent_type_p (expression);
23988 return instantiation_dependent_uneval_expression_p (expression);
23990 case AT_ENCODE_EXPR:
23991 /* An 'encode' expression is value-dependent if the operand is
23992 type-dependent. */
23993 expression = TREE_OPERAND (expression, 0);
23994 return dependent_type_p (expression);
23996 case NOEXCEPT_EXPR:
23997 expression = TREE_OPERAND (expression, 0);
23998 return instantiation_dependent_uneval_expression_p (expression);
24000 case SCOPE_REF:
24001 /* All instantiation-dependent expressions should also be considered
24002 value-dependent. */
24003 return instantiation_dependent_scope_ref_p (expression);
24005 case COMPONENT_REF:
24006 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24007 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24009 case NONTYPE_ARGUMENT_PACK:
24010 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24011 is value-dependent. */
24013 tree values = ARGUMENT_PACK_ARGS (expression);
24014 int i, len = TREE_VEC_LENGTH (values);
24016 for (i = 0; i < len; ++i)
24017 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24018 return true;
24020 return false;
24023 case TRAIT_EXPR:
24025 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24027 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24028 return true;
24030 if (!type2)
24031 return false;
24033 if (TREE_CODE (type2) != TREE_LIST)
24034 return dependent_type_p (type2);
24036 for (; type2; type2 = TREE_CHAIN (type2))
24037 if (dependent_type_p (TREE_VALUE (type2)))
24038 return true;
24040 return false;
24043 case MODOP_EXPR:
24044 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24045 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24047 case ARRAY_REF:
24048 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24049 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24051 case ADDR_EXPR:
24053 tree op = TREE_OPERAND (expression, 0);
24054 return (value_dependent_expression_p (op)
24055 || has_value_dependent_address (op));
24058 case REQUIRES_EXPR:
24059 /* Treat all requires-expressions as value-dependent so
24060 we don't try to fold them. */
24061 return true;
24063 case TYPE_REQ:
24064 return dependent_type_p (TREE_OPERAND (expression, 0));
24066 case CALL_EXPR:
24068 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24069 return true;
24070 tree fn = get_callee_fndecl (expression);
24071 int i, nargs;
24072 nargs = call_expr_nargs (expression);
24073 for (i = 0; i < nargs; ++i)
24075 tree op = CALL_EXPR_ARG (expression, i);
24076 /* In a call to a constexpr member function, look through the
24077 implicit ADDR_EXPR on the object argument so that it doesn't
24078 cause the call to be considered value-dependent. We also
24079 look through it in potential_constant_expression. */
24080 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24081 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24082 && TREE_CODE (op) == ADDR_EXPR)
24083 op = TREE_OPERAND (op, 0);
24084 if (value_dependent_expression_p (op))
24085 return true;
24087 return false;
24090 case TEMPLATE_ID_EXPR:
24091 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24092 type-dependent. */
24093 return type_dependent_expression_p (expression)
24094 || variable_concept_p (TREE_OPERAND (expression, 0));
24096 case CONSTRUCTOR:
24098 unsigned ix;
24099 tree val;
24100 if (dependent_type_p (TREE_TYPE (expression)))
24101 return true;
24102 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24103 if (value_dependent_expression_p (val))
24104 return true;
24105 return false;
24108 case STMT_EXPR:
24109 /* Treat a GNU statement expression as dependent to avoid crashing
24110 under instantiate_non_dependent_expr; it can't be constant. */
24111 return true;
24113 default:
24114 /* A constant expression is value-dependent if any subexpression is
24115 value-dependent. */
24116 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24118 case tcc_reference:
24119 case tcc_unary:
24120 case tcc_comparison:
24121 case tcc_binary:
24122 case tcc_expression:
24123 case tcc_vl_exp:
24125 int i, len = cp_tree_operand_length (expression);
24127 for (i = 0; i < len; i++)
24129 tree t = TREE_OPERAND (expression, i);
24131 /* In some cases, some of the operands may be missing.
24132 (For example, in the case of PREDECREMENT_EXPR, the
24133 amount to increment by may be missing.) That doesn't
24134 make the expression dependent. */
24135 if (t && value_dependent_expression_p (t))
24136 return true;
24139 break;
24140 default:
24141 break;
24143 break;
24146 /* The expression is not value-dependent. */
24147 return false;
24150 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24151 [temp.dep.expr]. Note that an expression with no type is
24152 considered dependent. Other parts of the compiler arrange for an
24153 expression with type-dependent subexpressions to have no type, so
24154 this function doesn't have to be fully recursive. */
24156 bool
24157 type_dependent_expression_p (tree expression)
24159 if (!processing_template_decl)
24160 return false;
24162 if (expression == NULL_TREE || expression == error_mark_node)
24163 return false;
24165 /* An unresolved name is always dependent. */
24166 if (identifier_p (expression)
24167 || TREE_CODE (expression) == USING_DECL
24168 || TREE_CODE (expression) == WILDCARD_DECL)
24169 return true;
24171 /* A fold expression is type-dependent. */
24172 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24173 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24174 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24175 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24176 return true;
24178 /* Some expression forms are never type-dependent. */
24179 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24180 || TREE_CODE (expression) == SIZEOF_EXPR
24181 || TREE_CODE (expression) == ALIGNOF_EXPR
24182 || TREE_CODE (expression) == AT_ENCODE_EXPR
24183 || TREE_CODE (expression) == NOEXCEPT_EXPR
24184 || TREE_CODE (expression) == TRAIT_EXPR
24185 || TREE_CODE (expression) == TYPEID_EXPR
24186 || TREE_CODE (expression) == DELETE_EXPR
24187 || TREE_CODE (expression) == VEC_DELETE_EXPR
24188 || TREE_CODE (expression) == THROW_EXPR
24189 || TREE_CODE (expression) == REQUIRES_EXPR)
24190 return false;
24192 /* The types of these expressions depends only on the type to which
24193 the cast occurs. */
24194 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24195 || TREE_CODE (expression) == STATIC_CAST_EXPR
24196 || TREE_CODE (expression) == CONST_CAST_EXPR
24197 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24198 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24199 || TREE_CODE (expression) == CAST_EXPR)
24200 return dependent_type_p (TREE_TYPE (expression));
24202 /* The types of these expressions depends only on the type created
24203 by the expression. */
24204 if (TREE_CODE (expression) == NEW_EXPR
24205 || TREE_CODE (expression) == VEC_NEW_EXPR)
24207 /* For NEW_EXPR tree nodes created inside a template, either
24208 the object type itself or a TREE_LIST may appear as the
24209 operand 1. */
24210 tree type = TREE_OPERAND (expression, 1);
24211 if (TREE_CODE (type) == TREE_LIST)
24212 /* This is an array type. We need to check array dimensions
24213 as well. */
24214 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24215 || value_dependent_expression_p
24216 (TREE_OPERAND (TREE_VALUE (type), 1));
24217 else
24218 return dependent_type_p (type);
24221 if (TREE_CODE (expression) == SCOPE_REF)
24223 tree scope = TREE_OPERAND (expression, 0);
24224 tree name = TREE_OPERAND (expression, 1);
24226 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24227 contains an identifier associated by name lookup with one or more
24228 declarations declared with a dependent type, or...a
24229 nested-name-specifier or qualified-id that names a member of an
24230 unknown specialization. */
24231 return (type_dependent_expression_p (name)
24232 || dependent_scope_p (scope));
24235 if (TREE_CODE (expression) == TEMPLATE_DECL
24236 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24237 return uses_outer_template_parms (expression);
24239 if (TREE_CODE (expression) == STMT_EXPR)
24240 expression = stmt_expr_value_expr (expression);
24242 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24244 tree elt;
24245 unsigned i;
24247 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24249 if (type_dependent_expression_p (elt))
24250 return true;
24252 return false;
24255 /* A static data member of the current instantiation with incomplete
24256 array type is type-dependent, as the definition and specializations
24257 can have different bounds. */
24258 if (VAR_P (expression)
24259 && DECL_CLASS_SCOPE_P (expression)
24260 && dependent_type_p (DECL_CONTEXT (expression))
24261 && VAR_HAD_UNKNOWN_BOUND (expression))
24262 return true;
24264 /* An array of unknown bound depending on a variadic parameter, eg:
24266 template<typename... Args>
24267 void foo (Args... args)
24269 int arr[] = { args... };
24272 template<int... vals>
24273 void bar ()
24275 int arr[] = { vals... };
24278 If the array has no length and has an initializer, it must be that
24279 we couldn't determine its length in cp_complete_array_type because
24280 it is dependent. */
24281 if (VAR_P (expression)
24282 && TREE_TYPE (expression) != NULL_TREE
24283 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24284 && !TYPE_DOMAIN (TREE_TYPE (expression))
24285 && DECL_INITIAL (expression))
24286 return true;
24288 /* A function or variable template-id is type-dependent if it has any
24289 dependent template arguments. */
24290 if (VAR_OR_FUNCTION_DECL_P (expression)
24291 && DECL_LANG_SPECIFIC (expression)
24292 && DECL_TEMPLATE_INFO (expression))
24294 /* Consider the innermost template arguments, since those are the ones
24295 that come from the template-id; the template arguments for the
24296 enclosing class do not make it type-dependent unless they are used in
24297 the type of the decl. */
24298 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24299 && (any_dependent_template_arguments_p
24300 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24301 return true;
24304 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24305 type-dependent. Checking this is important for functions with auto return
24306 type, which looks like a dependent type. */
24307 if (TREE_CODE (expression) == FUNCTION_DECL
24308 && !(DECL_CLASS_SCOPE_P (expression)
24309 && dependent_type_p (DECL_CONTEXT (expression)))
24310 && !(DECL_FRIEND_P (expression)
24311 && (!DECL_FRIEND_CONTEXT (expression)
24312 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24313 && !DECL_LOCAL_FUNCTION_P (expression))
24315 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24316 || undeduced_auto_decl (expression));
24317 return false;
24320 /* Always dependent, on the number of arguments if nothing else. */
24321 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24322 return true;
24324 if (TREE_TYPE (expression) == unknown_type_node)
24326 if (TREE_CODE (expression) == ADDR_EXPR)
24327 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24328 if (TREE_CODE (expression) == COMPONENT_REF
24329 || TREE_CODE (expression) == OFFSET_REF)
24331 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24332 return true;
24333 expression = TREE_OPERAND (expression, 1);
24334 if (identifier_p (expression))
24335 return false;
24337 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24338 if (TREE_CODE (expression) == SCOPE_REF)
24339 return false;
24341 if (BASELINK_P (expression))
24343 if (BASELINK_OPTYPE (expression)
24344 && dependent_type_p (BASELINK_OPTYPE (expression)))
24345 return true;
24346 expression = BASELINK_FUNCTIONS (expression);
24349 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24351 if (any_dependent_template_arguments_p
24352 (TREE_OPERAND (expression, 1)))
24353 return true;
24354 expression = TREE_OPERAND (expression, 0);
24355 if (identifier_p (expression))
24356 return true;
24359 gcc_assert (TREE_CODE (expression) == OVERLOAD
24360 || TREE_CODE (expression) == FUNCTION_DECL);
24362 for (lkp_iterator iter (expression); iter; ++iter)
24363 if (type_dependent_expression_p (*iter))
24364 return true;
24366 return false;
24369 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24371 /* Dependent type attributes might not have made it from the decl to
24372 the type yet. */
24373 if (DECL_P (expression)
24374 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24375 return true;
24377 return (dependent_type_p (TREE_TYPE (expression)));
24380 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24381 type-dependent if the expression refers to a member of the current
24382 instantiation and the type of the referenced member is dependent, or the
24383 class member access expression refers to a member of an unknown
24384 specialization.
24386 This function returns true if the OBJECT in such a class member access
24387 expression is of an unknown specialization. */
24389 bool
24390 type_dependent_object_expression_p (tree object)
24392 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24393 dependent. */
24394 if (TREE_CODE (object) == IDENTIFIER_NODE)
24395 return true;
24396 tree scope = TREE_TYPE (object);
24397 return (!scope || dependent_scope_p (scope));
24400 /* walk_tree callback function for instantiation_dependent_expression_p,
24401 below. Returns non-zero if a dependent subexpression is found. */
24403 static tree
24404 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24405 void * /*data*/)
24407 if (TYPE_P (*tp))
24409 /* We don't have to worry about decltype currently because decltype
24410 of an instantiation-dependent expr is a dependent type. This
24411 might change depending on the resolution of DR 1172. */
24412 *walk_subtrees = false;
24413 return NULL_TREE;
24415 enum tree_code code = TREE_CODE (*tp);
24416 switch (code)
24418 /* Don't treat an argument list as dependent just because it has no
24419 TREE_TYPE. */
24420 case TREE_LIST:
24421 case TREE_VEC:
24422 return NULL_TREE;
24424 case TEMPLATE_PARM_INDEX:
24425 return *tp;
24427 /* Handle expressions with type operands. */
24428 case SIZEOF_EXPR:
24429 case ALIGNOF_EXPR:
24430 case TYPEID_EXPR:
24431 case AT_ENCODE_EXPR:
24433 tree op = TREE_OPERAND (*tp, 0);
24434 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24435 op = TREE_TYPE (op);
24436 if (TYPE_P (op))
24438 if (dependent_type_p (op))
24439 return *tp;
24440 else
24442 *walk_subtrees = false;
24443 return NULL_TREE;
24446 break;
24449 case COMPONENT_REF:
24450 if (identifier_p (TREE_OPERAND (*tp, 1)))
24451 /* In a template, finish_class_member_access_expr creates a
24452 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24453 type-dependent, so that we can check access control at
24454 instantiation time (PR 42277). See also Core issue 1273. */
24455 return *tp;
24456 break;
24458 case SCOPE_REF:
24459 if (instantiation_dependent_scope_ref_p (*tp))
24460 return *tp;
24461 else
24462 break;
24464 /* Treat statement-expressions as dependent. */
24465 case BIND_EXPR:
24466 return *tp;
24468 /* Treat requires-expressions as dependent. */
24469 case REQUIRES_EXPR:
24470 return *tp;
24472 case CALL_EXPR:
24473 /* Treat calls to function concepts as dependent. */
24474 if (function_concept_check_p (*tp))
24475 return *tp;
24476 break;
24478 case TEMPLATE_ID_EXPR:
24479 /* And variable concepts. */
24480 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24481 return *tp;
24482 break;
24484 default:
24485 break;
24488 if (type_dependent_expression_p (*tp))
24489 return *tp;
24490 else
24491 return NULL_TREE;
24494 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24495 sense defined by the ABI:
24497 "An expression is instantiation-dependent if it is type-dependent
24498 or value-dependent, or it has a subexpression that is type-dependent
24499 or value-dependent."
24501 Except don't actually check value-dependence for unevaluated expressions,
24502 because in sizeof(i) we don't care about the value of i. Checking
24503 type-dependence will in turn check value-dependence of array bounds/template
24504 arguments as needed. */
24506 bool
24507 instantiation_dependent_uneval_expression_p (tree expression)
24509 tree result;
24511 if (!processing_template_decl)
24512 return false;
24514 if (expression == error_mark_node)
24515 return false;
24517 result = cp_walk_tree_without_duplicates (&expression,
24518 instantiation_dependent_r, NULL);
24519 return result != NULL_TREE;
24522 /* As above, but also check value-dependence of the expression as a whole. */
24524 bool
24525 instantiation_dependent_expression_p (tree expression)
24527 return (instantiation_dependent_uneval_expression_p (expression)
24528 || value_dependent_expression_p (expression));
24531 /* Like type_dependent_expression_p, but it also works while not processing
24532 a template definition, i.e. during substitution or mangling. */
24534 bool
24535 type_dependent_expression_p_push (tree expr)
24537 bool b;
24538 ++processing_template_decl;
24539 b = type_dependent_expression_p (expr);
24540 --processing_template_decl;
24541 return b;
24544 /* Returns TRUE if ARGS contains a type-dependent expression. */
24546 bool
24547 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24549 unsigned int i;
24550 tree arg;
24552 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24554 if (type_dependent_expression_p (arg))
24555 return true;
24557 return false;
24560 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24561 expressions) contains any type-dependent expressions. */
24563 bool
24564 any_type_dependent_elements_p (const_tree list)
24566 for (; list; list = TREE_CHAIN (list))
24567 if (type_dependent_expression_p (TREE_VALUE (list)))
24568 return true;
24570 return false;
24573 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24574 expressions) contains any value-dependent expressions. */
24576 bool
24577 any_value_dependent_elements_p (const_tree list)
24579 for (; list; list = TREE_CHAIN (list))
24580 if (value_dependent_expression_p (TREE_VALUE (list)))
24581 return true;
24583 return false;
24586 /* Returns TRUE if the ARG (a template argument) is dependent. */
24588 bool
24589 dependent_template_arg_p (tree arg)
24591 if (!processing_template_decl)
24592 return false;
24594 /* Assume a template argument that was wrongly written by the user
24595 is dependent. This is consistent with what
24596 any_dependent_template_arguments_p [that calls this function]
24597 does. */
24598 if (!arg || arg == error_mark_node)
24599 return true;
24601 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24602 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24604 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24605 return true;
24606 if (TREE_CODE (arg) == TEMPLATE_DECL)
24608 if (DECL_TEMPLATE_PARM_P (arg))
24609 return true;
24610 /* A member template of a dependent class is not necessarily
24611 type-dependent, but it is a dependent template argument because it
24612 will be a member of an unknown specialization to that template. */
24613 tree scope = CP_DECL_CONTEXT (arg);
24614 return TYPE_P (scope) && dependent_type_p (scope);
24616 else if (ARGUMENT_PACK_P (arg))
24618 tree args = ARGUMENT_PACK_ARGS (arg);
24619 int i, len = TREE_VEC_LENGTH (args);
24620 for (i = 0; i < len; ++i)
24622 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24623 return true;
24626 return false;
24628 else if (TYPE_P (arg))
24629 return dependent_type_p (arg);
24630 else
24631 return (type_dependent_expression_p (arg)
24632 || value_dependent_expression_p (arg));
24635 /* Returns true if ARGS (a collection of template arguments) contains
24636 any types that require structural equality testing. */
24638 bool
24639 any_template_arguments_need_structural_equality_p (tree args)
24641 int i;
24642 int j;
24644 if (!args)
24645 return false;
24646 if (args == error_mark_node)
24647 return true;
24649 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24651 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24652 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24654 tree arg = TREE_VEC_ELT (level, j);
24655 tree packed_args = NULL_TREE;
24656 int k, len = 1;
24658 if (ARGUMENT_PACK_P (arg))
24660 /* Look inside the argument pack. */
24661 packed_args = ARGUMENT_PACK_ARGS (arg);
24662 len = TREE_VEC_LENGTH (packed_args);
24665 for (k = 0; k < len; ++k)
24667 if (packed_args)
24668 arg = TREE_VEC_ELT (packed_args, k);
24670 if (error_operand_p (arg))
24671 return true;
24672 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24673 continue;
24674 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24675 return true;
24676 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24677 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24678 return true;
24683 return false;
24686 /* Returns true if ARGS (a collection of template arguments) contains
24687 any dependent arguments. */
24689 bool
24690 any_dependent_template_arguments_p (const_tree args)
24692 int i;
24693 int j;
24695 if (!args)
24696 return false;
24697 if (args == error_mark_node)
24698 return true;
24700 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24702 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24703 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24704 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24705 return true;
24708 return false;
24711 /* Returns TRUE if the template TMPL is type-dependent. */
24713 bool
24714 dependent_template_p (tree tmpl)
24716 if (TREE_CODE (tmpl) == OVERLOAD)
24718 for (lkp_iterator iter (tmpl); iter; ++iter)
24719 if (dependent_template_p (*iter))
24720 return true;
24721 return false;
24724 /* Template template parameters are dependent. */
24725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24726 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24727 return true;
24728 /* So are names that have not been looked up. */
24729 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24730 return true;
24731 return false;
24734 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24736 bool
24737 dependent_template_id_p (tree tmpl, tree args)
24739 return (dependent_template_p (tmpl)
24740 || any_dependent_template_arguments_p (args));
24743 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24744 are dependent. */
24746 bool
24747 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24749 int i;
24751 if (!processing_template_decl)
24752 return false;
24754 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24756 tree decl = TREE_VEC_ELT (declv, i);
24757 tree init = TREE_VEC_ELT (initv, i);
24758 tree cond = TREE_VEC_ELT (condv, i);
24759 tree incr = TREE_VEC_ELT (incrv, i);
24761 if (type_dependent_expression_p (decl)
24762 || TREE_CODE (decl) == SCOPE_REF)
24763 return true;
24765 if (init && type_dependent_expression_p (init))
24766 return true;
24768 if (type_dependent_expression_p (cond))
24769 return true;
24771 if (COMPARISON_CLASS_P (cond)
24772 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24773 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24774 return true;
24776 if (TREE_CODE (incr) == MODOP_EXPR)
24778 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24779 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24780 return true;
24782 else if (type_dependent_expression_p (incr))
24783 return true;
24784 else if (TREE_CODE (incr) == MODIFY_EXPR)
24786 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24787 return true;
24788 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24790 tree t = TREE_OPERAND (incr, 1);
24791 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24792 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24793 return true;
24798 return false;
24801 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24802 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24803 no such TYPE can be found. Note that this function peers inside
24804 uninstantiated templates and therefore should be used only in
24805 extremely limited situations. ONLY_CURRENT_P restricts this
24806 peering to the currently open classes hierarchy (which is required
24807 when comparing types). */
24809 tree
24810 resolve_typename_type (tree type, bool only_current_p)
24812 tree scope;
24813 tree name;
24814 tree decl;
24815 int quals;
24816 tree pushed_scope;
24817 tree result;
24819 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24821 scope = TYPE_CONTEXT (type);
24822 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24823 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24824 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24825 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24826 identifier of the TYPENAME_TYPE anymore.
24827 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24828 TYPENAME_TYPE instead, we avoid messing up with a possible
24829 typedef variant case. */
24830 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24832 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24833 it first before we can figure out what NAME refers to. */
24834 if (TREE_CODE (scope) == TYPENAME_TYPE)
24836 if (TYPENAME_IS_RESOLVING_P (scope))
24837 /* Given a class template A with a dependent base with nested type C,
24838 typedef typename A::C::C C will land us here, as trying to resolve
24839 the initial A::C leads to the local C typedef, which leads back to
24840 A::C::C. So we break the recursion now. */
24841 return type;
24842 else
24843 scope = resolve_typename_type (scope, only_current_p);
24845 /* If we don't know what SCOPE refers to, then we cannot resolve the
24846 TYPENAME_TYPE. */
24847 if (!CLASS_TYPE_P (scope))
24848 return type;
24849 /* If this is a typedef, we don't want to look inside (c++/11987). */
24850 if (typedef_variant_p (type))
24851 return type;
24852 /* If SCOPE isn't the template itself, it will not have a valid
24853 TYPE_FIELDS list. */
24854 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24855 /* scope is either the template itself or a compatible instantiation
24856 like X<T>, so look up the name in the original template. */
24857 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24858 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24859 gcc_checking_assert (uses_template_parms (scope));
24860 /* If scope has no fields, it can't be a current instantiation. Check this
24861 before currently_open_class to avoid infinite recursion (71515). */
24862 if (!TYPE_FIELDS (scope))
24863 return type;
24864 /* If the SCOPE is not the current instantiation, there's no reason
24865 to look inside it. */
24866 if (only_current_p && !currently_open_class (scope))
24867 return type;
24868 /* Enter the SCOPE so that name lookup will be resolved as if we
24869 were in the class definition. In particular, SCOPE will no
24870 longer be considered a dependent type. */
24871 pushed_scope = push_scope (scope);
24872 /* Look up the declaration. */
24873 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24874 tf_warning_or_error);
24876 result = NULL_TREE;
24878 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24879 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24880 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24881 if (!decl)
24882 /*nop*/;
24883 else if (identifier_p (fullname)
24884 && TREE_CODE (decl) == TYPE_DECL)
24886 result = TREE_TYPE (decl);
24887 if (result == error_mark_node)
24888 result = NULL_TREE;
24890 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24891 && DECL_CLASS_TEMPLATE_P (decl))
24893 /* Obtain the template and the arguments. */
24894 tree tmpl = TREE_OPERAND (fullname, 0);
24895 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24897 /* We get here with a plain identifier because a previous tentative
24898 parse of the nested-name-specifier as part of a ptr-operator saw
24899 ::template X<A>. The use of ::template is necessary in a
24900 ptr-operator, but wrong in a declarator-id.
24902 [temp.names]: In a qualified-id of a declarator-id, the keyword
24903 template shall not appear at the top level. */
24904 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24905 "keyword %<template%> not allowed in declarator-id");
24906 tmpl = decl;
24908 tree args = TREE_OPERAND (fullname, 1);
24909 /* Instantiate the template. */
24910 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24911 /*entering_scope=*/true,
24912 tf_error | tf_user);
24913 if (result == error_mark_node)
24914 result = NULL_TREE;
24917 /* Leave the SCOPE. */
24918 if (pushed_scope)
24919 pop_scope (pushed_scope);
24921 /* If we failed to resolve it, return the original typename. */
24922 if (!result)
24923 return type;
24925 /* If lookup found a typename type, resolve that too. */
24926 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24928 /* Ill-formed programs can cause infinite recursion here, so we
24929 must catch that. */
24930 TYPENAME_IS_RESOLVING_P (result) = 1;
24931 result = resolve_typename_type (result, only_current_p);
24932 TYPENAME_IS_RESOLVING_P (result) = 0;
24935 /* Qualify the resulting type. */
24936 quals = cp_type_quals (type);
24937 if (quals)
24938 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24940 return result;
24943 /* EXPR is an expression which is not type-dependent. Return a proxy
24944 for EXPR that can be used to compute the types of larger
24945 expressions containing EXPR. */
24947 tree
24948 build_non_dependent_expr (tree expr)
24950 tree inner_expr;
24952 /* When checking, try to get a constant value for all non-dependent
24953 expressions in order to expose bugs in *_dependent_expression_p
24954 and constexpr. This can affect code generation, see PR70704, so
24955 only do this for -fchecking=2. */
24956 if (flag_checking > 1
24957 && cxx_dialect >= cxx11
24958 /* Don't do this during nsdmi parsing as it can lead to
24959 unexpected recursive instantiations. */
24960 && !parsing_nsdmi ()
24961 /* Don't do this during concept expansion either and for
24962 the same reason. */
24963 && !expanding_concept ())
24964 fold_non_dependent_expr (expr);
24966 /* Preserve OVERLOADs; the functions must be available to resolve
24967 types. */
24968 inner_expr = expr;
24969 if (TREE_CODE (inner_expr) == STMT_EXPR)
24970 inner_expr = stmt_expr_value_expr (inner_expr);
24971 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24972 inner_expr = TREE_OPERAND (inner_expr, 0);
24973 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24974 inner_expr = TREE_OPERAND (inner_expr, 1);
24975 if (is_overloaded_fn (inner_expr)
24976 || TREE_CODE (inner_expr) == OFFSET_REF)
24977 return expr;
24978 /* There is no need to return a proxy for a variable. */
24979 if (VAR_P (expr))
24980 return expr;
24981 /* Preserve string constants; conversions from string constants to
24982 "char *" are allowed, even though normally a "const char *"
24983 cannot be used to initialize a "char *". */
24984 if (TREE_CODE (expr) == STRING_CST)
24985 return expr;
24986 /* Preserve void and arithmetic constants, as an optimization -- there is no
24987 reason to create a new node. */
24988 if (TREE_CODE (expr) == VOID_CST
24989 || TREE_CODE (expr) == INTEGER_CST
24990 || TREE_CODE (expr) == REAL_CST)
24991 return expr;
24992 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24993 There is at least one place where we want to know that a
24994 particular expression is a throw-expression: when checking a ?:
24995 expression, there are special rules if the second or third
24996 argument is a throw-expression. */
24997 if (TREE_CODE (expr) == THROW_EXPR)
24998 return expr;
25000 /* Don't wrap an initializer list, we need to be able to look inside. */
25001 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25002 return expr;
25004 /* Don't wrap a dummy object, we need to be able to test for it. */
25005 if (is_dummy_object (expr))
25006 return expr;
25008 if (TREE_CODE (expr) == COND_EXPR)
25009 return build3 (COND_EXPR,
25010 TREE_TYPE (expr),
25011 TREE_OPERAND (expr, 0),
25012 (TREE_OPERAND (expr, 1)
25013 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25014 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25015 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25016 if (TREE_CODE (expr) == COMPOUND_EXPR
25017 && !COMPOUND_EXPR_OVERLOADED (expr))
25018 return build2 (COMPOUND_EXPR,
25019 TREE_TYPE (expr),
25020 TREE_OPERAND (expr, 0),
25021 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25023 /* If the type is unknown, it can't really be non-dependent */
25024 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25026 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25027 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25030 /* ARGS is a vector of expressions as arguments to a function call.
25031 Replace the arguments with equivalent non-dependent expressions.
25032 This modifies ARGS in place. */
25034 void
25035 make_args_non_dependent (vec<tree, va_gc> *args)
25037 unsigned int ix;
25038 tree arg;
25040 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25042 tree newarg = build_non_dependent_expr (arg);
25043 if (newarg != arg)
25044 (*args)[ix] = newarg;
25048 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25049 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25050 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25052 static tree
25053 make_auto_1 (tree name, bool set_canonical)
25055 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25056 TYPE_NAME (au) = build_decl (input_location,
25057 TYPE_DECL, name, au);
25058 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25059 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25060 (0, processing_template_decl + 1, processing_template_decl + 1,
25061 TYPE_NAME (au), NULL_TREE);
25062 if (set_canonical)
25063 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25064 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25065 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25067 return au;
25070 tree
25071 make_decltype_auto (void)
25073 return make_auto_1 (decltype_auto_identifier, true);
25076 tree
25077 make_auto (void)
25079 return make_auto_1 (auto_identifier, true);
25082 /* Return a C++17 deduction placeholder for class template TMPL. */
25084 tree
25085 make_template_placeholder (tree tmpl)
25087 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25088 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25089 return t;
25092 /* True iff T is a C++17 class template deduction placeholder. */
25094 bool
25095 template_placeholder_p (tree t)
25097 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25100 /* Make a "constrained auto" type-specifier. This is an
25101 auto type with constraints that must be associated after
25102 deduction. The constraint is formed from the given
25103 CONC and its optional sequence of arguments, which are
25104 non-null if written as partial-concept-id. */
25106 tree
25107 make_constrained_auto (tree con, tree args)
25109 tree type = make_auto_1 (auto_identifier, false);
25111 /* Build the constraint. */
25112 tree tmpl = DECL_TI_TEMPLATE (con);
25113 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25114 expr = build_concept_check (expr, type, args);
25116 tree constr = normalize_expression (expr);
25117 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25119 /* Our canonical type depends on the constraint. */
25120 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25122 /* Attach the constraint to the type declaration. */
25123 tree decl = TYPE_NAME (type);
25124 return decl;
25127 /* Given type ARG, return std::initializer_list<ARG>. */
25129 static tree
25130 listify (tree arg)
25132 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25134 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25136 gcc_rich_location richloc (input_location);
25137 maybe_add_include_fixit (&richloc, "<initializer_list>");
25138 error_at (&richloc,
25139 "deducing from brace-enclosed initializer list"
25140 " requires %<#include <initializer_list>%>");
25142 return error_mark_node;
25144 tree argvec = make_tree_vec (1);
25145 TREE_VEC_ELT (argvec, 0) = arg;
25147 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25148 NULL_TREE, 0, tf_warning_or_error);
25151 /* Replace auto in TYPE with std::initializer_list<auto>. */
25153 static tree
25154 listify_autos (tree type, tree auto_node)
25156 tree init_auto = listify (auto_node);
25157 tree argvec = make_tree_vec (1);
25158 TREE_VEC_ELT (argvec, 0) = init_auto;
25159 if (processing_template_decl)
25160 argvec = add_to_template_args (current_template_args (), argvec);
25161 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25164 /* Hash traits for hashing possibly constrained 'auto'
25165 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25167 struct auto_hash : default_hash_traits<tree>
25169 static inline hashval_t hash (tree);
25170 static inline bool equal (tree, tree);
25173 /* Hash the 'auto' T. */
25175 inline hashval_t
25176 auto_hash::hash (tree t)
25178 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25179 /* Matching constrained-type-specifiers denote the same template
25180 parameter, so hash the constraint. */
25181 return hash_placeholder_constraint (c);
25182 else
25183 /* But unconstrained autos are all separate, so just hash the pointer. */
25184 return iterative_hash_object (t, 0);
25187 /* Compare two 'auto's. */
25189 inline bool
25190 auto_hash::equal (tree t1, tree t2)
25192 if (t1 == t2)
25193 return true;
25195 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25196 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25198 /* Two unconstrained autos are distinct. */
25199 if (!c1 || !c2)
25200 return false;
25202 return equivalent_placeholder_constraints (c1, c2);
25205 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25206 constrained) auto, add it to the vector. */
25208 static int
25209 extract_autos_r (tree t, void *data)
25211 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25212 if (is_auto (t))
25214 /* All the autos were built with index 0; fix that up now. */
25215 tree *p = hash.find_slot (t, INSERT);
25216 unsigned idx;
25217 if (*p)
25218 /* If this is a repeated constrained-type-specifier, use the index we
25219 chose before. */
25220 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25221 else
25223 /* Otherwise this is new, so use the current count. */
25224 *p = t;
25225 idx = hash.elements () - 1;
25227 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25230 /* Always keep walking. */
25231 return 0;
25234 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25235 says they can appear anywhere in the type. */
25237 static tree
25238 extract_autos (tree type)
25240 hash_set<tree> visited;
25241 hash_table<auto_hash> hash (2);
25243 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25245 tree tree_vec = make_tree_vec (hash.elements());
25246 for (hash_table<auto_hash>::iterator iter = hash.begin();
25247 iter != hash.end(); ++iter)
25249 tree elt = *iter;
25250 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25251 TREE_VEC_ELT (tree_vec, i)
25252 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25255 return tree_vec;
25258 /* The stem for deduction guide names. */
25259 const char *const dguide_base = "__dguide_";
25261 /* Return the name for a deduction guide for class template TMPL. */
25263 tree
25264 dguide_name (tree tmpl)
25266 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25267 tree tname = TYPE_IDENTIFIER (type);
25268 char *buf = (char *) alloca (1 + strlen (dguide_base)
25269 + IDENTIFIER_LENGTH (tname));
25270 memcpy (buf, dguide_base, strlen (dguide_base));
25271 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25272 IDENTIFIER_LENGTH (tname) + 1);
25273 tree dname = get_identifier (buf);
25274 TREE_TYPE (dname) = type;
25275 return dname;
25278 /* True if NAME is the name of a deduction guide. */
25280 bool
25281 dguide_name_p (tree name)
25283 return (TREE_TYPE (name)
25284 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25285 strlen (dguide_base)));
25288 /* True if FN is a deduction guide. */
25290 bool
25291 deduction_guide_p (const_tree fn)
25293 if (DECL_P (fn))
25294 if (tree name = DECL_NAME (fn))
25295 return dguide_name_p (name);
25296 return false;
25299 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25301 bool
25302 copy_guide_p (const_tree fn)
25304 gcc_assert (deduction_guide_p (fn));
25305 if (!DECL_ARTIFICIAL (fn))
25306 return false;
25307 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25308 return (TREE_CHAIN (parms) == void_list_node
25309 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25312 /* True if FN is a guide generated from a constructor template. */
25314 bool
25315 template_guide_p (const_tree fn)
25317 gcc_assert (deduction_guide_p (fn));
25318 if (!DECL_ARTIFICIAL (fn))
25319 return false;
25320 tree tmpl = DECL_TI_TEMPLATE (fn);
25321 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25322 return PRIMARY_TEMPLATE_P (org);
25323 return false;
25326 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25327 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25328 template parameter types. Note that the handling of template template
25329 parameters relies on current_template_parms being set appropriately for the
25330 new template. */
25332 static tree
25333 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25334 tree tsubst_args, tsubst_flags_t complain)
25336 tree oldidx = get_template_parm_index (olddecl);
25338 tree newtype;
25339 if (TREE_CODE (olddecl) == TYPE_DECL
25340 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25342 tree oldtype = TREE_TYPE (olddecl);
25343 newtype = cxx_make_type (TREE_CODE (oldtype));
25344 TYPE_MAIN_VARIANT (newtype) = newtype;
25345 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25346 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25347 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25349 else
25350 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25351 complain, NULL_TREE);
25353 tree newdecl
25354 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25355 DECL_NAME (olddecl), newtype);
25356 SET_DECL_TEMPLATE_PARM_P (newdecl);
25358 tree newidx;
25359 if (TREE_CODE (olddecl) == TYPE_DECL
25360 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25362 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25363 = build_template_parm_index (index, level, level,
25364 newdecl, newtype);
25365 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25366 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25367 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25368 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25370 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25372 DECL_TEMPLATE_RESULT (newdecl)
25373 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25374 DECL_NAME (olddecl), newtype);
25375 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25376 // First create a copy (ttargs) of tsubst_args with an
25377 // additional level for the template template parameter's own
25378 // template parameters (ttparms).
25379 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25380 (DECL_TEMPLATE_PARMS (olddecl)));
25381 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25382 tree ttargs = make_tree_vec (depth + 1);
25383 for (int i = 0; i < depth; ++i)
25384 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25385 TREE_VEC_ELT (ttargs, depth)
25386 = template_parms_level_to_args (ttparms);
25387 // Substitute ttargs into ttparms to fix references to
25388 // other template parameters.
25389 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25390 complain);
25391 // Now substitute again with args based on tparms, to reduce
25392 // the level of the ttparms.
25393 ttargs = current_template_args ();
25394 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25395 complain);
25396 // Finally, tack the adjusted parms onto tparms.
25397 ttparms = tree_cons (size_int (depth), ttparms,
25398 current_template_parms);
25399 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25402 else
25404 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25405 tree newconst
25406 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25407 TREE_CODE (oldconst),
25408 DECL_NAME (oldconst), newtype);
25409 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25410 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25411 SET_DECL_TEMPLATE_PARM_P (newconst);
25412 newidx = build_template_parm_index (index, level, level,
25413 newconst, newtype);
25414 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25415 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25416 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25419 return newdecl;
25422 /* Returns a C++17 class deduction guide template based on the constructor
25423 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25424 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25426 static tree
25427 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25429 tree type, tparms, targs, fparms, fargs, ci;
25430 bool memtmpl = false;
25431 bool explicit_p;
25432 location_t loc;
25433 tree fn_tmpl = NULL_TREE;
25435 if (TYPE_P (ctor))
25437 type = ctor;
25438 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25439 if (copy_p)
25441 type = TREE_TYPE (type);
25442 fparms = tree_cons (NULL_TREE, type, void_list_node);
25444 else
25445 fparms = void_list_node;
25447 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25448 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25449 targs = CLASSTYPE_TI_ARGS (type);
25450 ci = NULL_TREE;
25451 fargs = NULL_TREE;
25452 loc = DECL_SOURCE_LOCATION (ctmpl);
25453 explicit_p = false;
25455 else
25457 ++processing_template_decl;
25459 fn_tmpl
25460 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25461 : DECL_TI_TEMPLATE (ctor));
25462 if (outer_args)
25463 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25464 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25466 type = DECL_CONTEXT (ctor);
25468 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25469 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25470 fully specialized args for the enclosing class. Strip those off, as
25471 the deduction guide won't have those template parameters. */
25472 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25473 TMPL_PARMS_DEPTH (tparms));
25474 /* Discard the 'this' parameter. */
25475 fparms = FUNCTION_ARG_CHAIN (ctor);
25476 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25477 ci = get_constraints (ctor);
25478 loc = DECL_SOURCE_LOCATION (ctor);
25479 explicit_p = DECL_NONCONVERTING_P (ctor);
25481 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25483 memtmpl = true;
25485 /* For a member template constructor, we need to flatten the two
25486 template parameter lists into one, and then adjust the function
25487 signature accordingly. This gets...complicated. */
25488 tree save_parms = current_template_parms;
25490 /* For a member template we should have two levels of parms/args, one
25491 for the class and one for the constructor. We stripped
25492 specialized args for further enclosing classes above. */
25493 const int depth = 2;
25494 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25496 /* Template args for translating references to the two-level template
25497 parameters into references to the one-level template parameters we
25498 are creating. */
25499 tree tsubst_args = copy_node (targs);
25500 TMPL_ARGS_LEVEL (tsubst_args, depth)
25501 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25503 /* Template parms for the constructor template. */
25504 tree ftparms = TREE_VALUE (tparms);
25505 unsigned flen = TREE_VEC_LENGTH (ftparms);
25506 /* Template parms for the class template. */
25507 tparms = TREE_CHAIN (tparms);
25508 tree ctparms = TREE_VALUE (tparms);
25509 unsigned clen = TREE_VEC_LENGTH (ctparms);
25510 /* Template parms for the deduction guide start as a copy of the
25511 template parms for the class. We set current_template_parms for
25512 lookup_template_class_1. */
25513 current_template_parms = tparms = copy_node (tparms);
25514 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25515 for (unsigned i = 0; i < clen; ++i)
25516 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25518 /* Now we need to rewrite the constructor parms to append them to the
25519 class parms. */
25520 for (unsigned i = 0; i < flen; ++i)
25522 unsigned index = i + clen;
25523 unsigned level = 1;
25524 tree oldelt = TREE_VEC_ELT (ftparms, i);
25525 tree olddecl = TREE_VALUE (oldelt);
25526 tree newdecl = rewrite_template_parm (olddecl, index, level,
25527 tsubst_args, complain);
25528 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25529 tsubst_args, complain, ctor);
25530 tree list = build_tree_list (newdef, newdecl);
25531 TEMPLATE_PARM_CONSTRAINTS (list)
25532 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25533 tsubst_args, complain, ctor);
25534 TREE_VEC_ELT (new_vec, index) = list;
25535 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25538 /* Now we have a final set of template parms to substitute into the
25539 function signature. */
25540 targs = template_parms_to_args (tparms);
25541 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25542 complain, ctor);
25543 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25544 if (ci)
25545 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25547 current_template_parms = save_parms;
25549 --processing_template_decl;
25552 if (!memtmpl)
25554 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25555 tparms = copy_node (tparms);
25556 INNERMOST_TEMPLATE_PARMS (tparms)
25557 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25560 tree fntype = build_function_type (type, fparms);
25561 tree ded_fn = build_lang_decl_loc (loc,
25562 FUNCTION_DECL,
25563 dguide_name (type), fntype);
25564 DECL_ARGUMENTS (ded_fn) = fargs;
25565 DECL_ARTIFICIAL (ded_fn) = true;
25566 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25567 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25568 DECL_ARTIFICIAL (ded_tmpl) = true;
25569 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25570 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25571 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25572 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25573 if (DECL_P (ctor))
25574 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25575 if (ci)
25576 set_constraints (ded_tmpl, ci);
25578 return ded_tmpl;
25581 /* Deduce template arguments for the class template placeholder PTYPE for
25582 template TMPL based on the initializer INIT, and return the resulting
25583 type. */
25585 static tree
25586 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25587 tsubst_flags_t complain)
25589 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25591 /* We should have handled this in the caller. */
25592 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25593 return ptype;
25594 if (complain & tf_error)
25595 error ("non-class template %qT used without template arguments", tmpl);
25596 return error_mark_node;
25599 tree type = TREE_TYPE (tmpl);
25601 bool try_list_ctor = false;
25603 vec<tree,va_gc> *args;
25604 if (init == NULL_TREE
25605 || TREE_CODE (init) == TREE_LIST)
25606 args = make_tree_vector_from_list (init);
25607 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25609 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25610 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25612 /* As an exception, the first phase in 16.3.1.7 (considering the
25613 initializer list as a single argument) is omitted if the
25614 initializer list consists of a single expression of type cv U,
25615 where U is a specialization of C or a class derived from a
25616 specialization of C. */
25617 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25618 tree etype = TREE_TYPE (elt);
25620 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25621 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25622 int err = unify (tparms, targs, type, etype,
25623 UNIFY_ALLOW_DERIVED, /*explain*/false);
25624 if (err == 0)
25625 try_list_ctor = false;
25626 ggc_free (targs);
25628 if (try_list_ctor || is_std_init_list (type))
25629 args = make_tree_vector_single (init);
25630 else
25631 args = make_tree_vector_from_ctor (init);
25633 else
25634 args = make_tree_vector_single (init);
25636 tree dname = dguide_name (tmpl);
25637 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25638 /*type*/false, /*complain*/false,
25639 /*hidden*/false);
25640 bool elided = false;
25641 if (cands == error_mark_node)
25642 cands = NULL_TREE;
25644 /* Prune explicit deduction guides in copy-initialization context. */
25645 if (flags & LOOKUP_ONLYCONVERTING)
25647 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25648 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25649 elided = true;
25651 if (elided)
25653 /* Found a nonconverting guide, prune the candidates. */
25654 tree pruned = NULL_TREE;
25655 for (lkp_iterator iter (cands); iter; ++iter)
25656 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25657 pruned = lookup_add (*iter, pruned);
25659 cands = pruned;
25663 tree outer_args = NULL_TREE;
25664 if (DECL_CLASS_SCOPE_P (tmpl)
25665 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25667 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25668 type = TREE_TYPE (most_general_template (tmpl));
25671 bool saw_ctor = false;
25672 // FIXME cache artificial deduction guides
25673 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25675 tree guide = build_deduction_guide (*iter, outer_args, complain);
25676 if ((flags & LOOKUP_ONLYCONVERTING)
25677 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25678 elided = true;
25679 else
25680 cands = lookup_add (guide, cands);
25682 saw_ctor = true;
25685 tree call = error_mark_node;
25687 /* If this is list-initialization and the class has a list constructor, first
25688 try deducing from the list as a single argument, as [over.match.list]. */
25689 tree list_cands = NULL_TREE;
25690 if (try_list_ctor && cands)
25691 for (lkp_iterator iter (cands); iter; ++iter)
25693 tree dg = *iter;
25694 if (is_list_ctor (dg))
25695 list_cands = lookup_add (dg, list_cands);
25697 if (list_cands)
25699 ++cp_unevaluated_operand;
25700 call = build_new_function_call (list_cands, &args, tf_decltype);
25701 --cp_unevaluated_operand;
25703 if (call == error_mark_node)
25705 /* That didn't work, now try treating the list as a sequence of
25706 arguments. */
25707 release_tree_vector (args);
25708 args = make_tree_vector_from_ctor (init);
25712 /* Maybe generate an implicit deduction guide. */
25713 if (call == error_mark_node && args->length () < 2)
25715 tree gtype = NULL_TREE;
25717 if (args->length () == 1)
25718 /* Generate a copy guide. */
25719 gtype = build_reference_type (type);
25720 else if (!saw_ctor)
25721 /* Generate a default guide. */
25722 gtype = type;
25724 if (gtype)
25726 tree guide = build_deduction_guide (gtype, outer_args, complain);
25727 cands = lookup_add (guide, cands);
25731 if (elided && !cands)
25733 error ("cannot deduce template arguments for copy-initialization"
25734 " of %qT, as it has no non-explicit deduction guides or "
25735 "user-declared constructors", type);
25736 return error_mark_node;
25738 else if (!cands && call == error_mark_node)
25740 error ("cannot deduce template arguments of %qT, as it has no viable "
25741 "deduction guides", type);
25742 return error_mark_node;
25745 if (call == error_mark_node)
25747 ++cp_unevaluated_operand;
25748 call = build_new_function_call (cands, &args, tf_decltype);
25749 --cp_unevaluated_operand;
25752 if (call == error_mark_node && (complain & tf_warning_or_error))
25754 error ("class template argument deduction failed:");
25756 ++cp_unevaluated_operand;
25757 call = build_new_function_call (cands, &args, complain | tf_decltype);
25758 --cp_unevaluated_operand;
25760 if (elided)
25761 inform (input_location, "explicit deduction guides not considered "
25762 "for copy-initialization");
25765 release_tree_vector (args);
25767 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25770 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25771 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25773 tree
25774 do_auto_deduction (tree type, tree init, tree auto_node)
25776 return do_auto_deduction (type, init, auto_node,
25777 tf_warning_or_error,
25778 adc_unspecified);
25781 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25782 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25783 The CONTEXT determines the context in which auto deduction is performed
25784 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25785 OUTER_TARGS are used during template argument deduction
25786 (context == adc_unify) to properly substitute the result, and is ignored
25787 in other contexts.
25789 For partial-concept-ids, extra args may be appended to the list of deduced
25790 template arguments prior to determining constraint satisfaction. */
25792 tree
25793 do_auto_deduction (tree type, tree init, tree auto_node,
25794 tsubst_flags_t complain, auto_deduction_context context,
25795 tree outer_targs, int flags)
25797 tree targs;
25799 if (init == error_mark_node)
25800 return error_mark_node;
25802 if (init && type_dependent_expression_p (init)
25803 && context != adc_unify)
25804 /* Defining a subset of type-dependent expressions that we can deduce
25805 from ahead of time isn't worth the trouble. */
25806 return type;
25808 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25809 /* C++17 class template argument deduction. */
25810 return do_class_deduction (type, tmpl, init, flags, complain);
25812 if (TREE_TYPE (init) == NULL_TREE)
25813 /* Nothing we can do with this, even in deduction context. */
25814 return type;
25816 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25817 with either a new invented type template parameter U or, if the
25818 initializer is a braced-init-list (8.5.4), with
25819 std::initializer_list<U>. */
25820 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25822 if (!DIRECT_LIST_INIT_P (init))
25823 type = listify_autos (type, auto_node);
25824 else if (CONSTRUCTOR_NELTS (init) == 1)
25825 init = CONSTRUCTOR_ELT (init, 0)->value;
25826 else
25828 if (complain & tf_warning_or_error)
25830 if (permerror (input_location, "direct-list-initialization of "
25831 "%<auto%> requires exactly one element"))
25832 inform (input_location,
25833 "for deduction to %<std::initializer_list%>, use copy-"
25834 "list-initialization (i.e. add %<=%> before the %<{%>)");
25836 type = listify_autos (type, auto_node);
25840 if (type == error_mark_node)
25841 return error_mark_node;
25843 init = resolve_nondeduced_context (init, complain);
25845 if (context == adc_decomp_type
25846 && auto_node == type
25847 && init != error_mark_node
25848 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25849 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25850 and initializer has array type, deduce cv-qualified array type. */
25851 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25852 complain);
25853 else if (AUTO_IS_DECLTYPE (auto_node))
25855 bool id = (DECL_P (init)
25856 || ((TREE_CODE (init) == COMPONENT_REF
25857 || TREE_CODE (init) == SCOPE_REF)
25858 && !REF_PARENTHESIZED_P (init)));
25859 targs = make_tree_vec (1);
25860 TREE_VEC_ELT (targs, 0)
25861 = finish_decltype_type (init, id, tf_warning_or_error);
25862 if (type != auto_node)
25864 if (complain & tf_error)
25865 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25866 return error_mark_node;
25869 else
25871 tree parms = build_tree_list (NULL_TREE, type);
25872 tree tparms;
25874 if (flag_concepts)
25875 tparms = extract_autos (type);
25876 else
25878 tparms = make_tree_vec (1);
25879 TREE_VEC_ELT (tparms, 0)
25880 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25883 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25884 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25885 DEDUCE_CALL, LOOKUP_NORMAL,
25886 NULL, /*explain_p=*/false);
25887 if (val > 0)
25889 if (processing_template_decl)
25890 /* Try again at instantiation time. */
25891 return type;
25892 if (type && type != error_mark_node
25893 && (complain & tf_error))
25894 /* If type is error_mark_node a diagnostic must have been
25895 emitted by now. Also, having a mention to '<type error>'
25896 in the diagnostic is not really useful to the user. */
25898 if (cfun && auto_node == current_function_auto_return_pattern
25899 && LAMBDA_FUNCTION_P (current_function_decl))
25900 error ("unable to deduce lambda return type from %qE", init);
25901 else
25902 error ("unable to deduce %qT from %qE", type, init);
25903 type_unification_real (tparms, targs, parms, &init, 1, 0,
25904 DEDUCE_CALL, LOOKUP_NORMAL,
25905 NULL, /*explain_p=*/true);
25907 return error_mark_node;
25911 /* Check any placeholder constraints against the deduced type. */
25912 if (flag_concepts && !processing_template_decl)
25913 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25915 /* Use the deduced type to check the associated constraints. If we
25916 have a partial-concept-id, rebuild the argument list so that
25917 we check using the extra arguments. */
25918 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25919 tree cargs = CHECK_CONSTR_ARGS (constr);
25920 if (TREE_VEC_LENGTH (cargs) > 1)
25922 cargs = copy_node (cargs);
25923 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25925 else
25926 cargs = targs;
25927 if (!constraints_satisfied_p (constr, cargs))
25929 if (complain & tf_warning_or_error)
25931 switch (context)
25933 case adc_unspecified:
25934 case adc_unify:
25935 error("placeholder constraints not satisfied");
25936 break;
25937 case adc_variable_type:
25938 case adc_decomp_type:
25939 error ("deduced initializer does not satisfy "
25940 "placeholder constraints");
25941 break;
25942 case adc_return_type:
25943 error ("deduced return type does not satisfy "
25944 "placeholder constraints");
25945 break;
25946 case adc_requirement:
25947 error ("deduced expression type does not satisfy "
25948 "placeholder constraints");
25949 break;
25951 diagnose_constraints (input_location, constr, targs);
25953 return error_mark_node;
25957 if (processing_template_decl && context != adc_unify)
25958 outer_targs = current_template_args ();
25959 targs = add_to_template_args (outer_targs, targs);
25960 return tsubst (type, targs, complain, NULL_TREE);
25963 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25964 result. */
25966 tree
25967 splice_late_return_type (tree type, tree late_return_type)
25969 if (is_auto (type))
25971 if (late_return_type)
25972 return late_return_type;
25974 tree idx = get_template_parm_index (type);
25975 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25976 /* In an abbreviated function template we didn't know we were dealing
25977 with a function template when we saw the auto return type, so update
25978 it to have the correct level. */
25979 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25981 return type;
25984 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25985 'decltype(auto)' or a deduced class template. */
25987 bool
25988 is_auto (const_tree type)
25990 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25991 && (TYPE_IDENTIFIER (type) == auto_identifier
25992 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25993 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25994 return true;
25995 else
25996 return false;
25999 /* for_each_template_parm callback for type_uses_auto. */
26002 is_auto_r (tree tp, void */*data*/)
26004 return is_auto (tp);
26007 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26008 a use of `auto'. Returns NULL_TREE otherwise. */
26010 tree
26011 type_uses_auto (tree type)
26013 if (type == NULL_TREE)
26014 return NULL_TREE;
26015 else if (flag_concepts)
26017 /* The Concepts TS allows multiple autos in one type-specifier; just
26018 return the first one we find, do_auto_deduction will collect all of
26019 them. */
26020 if (uses_template_parms (type))
26021 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26022 /*visited*/NULL, /*nondeduced*/true);
26023 else
26024 return NULL_TREE;
26026 else
26027 return find_type_usage (type, is_auto);
26030 /* For a given template T, return the vector of typedefs referenced
26031 in T for which access check is needed at T instantiation time.
26032 T is either a FUNCTION_DECL or a RECORD_TYPE.
26033 Those typedefs were added to T by the function
26034 append_type_to_template_for_access_check. */
26036 vec<qualified_typedef_usage_t, va_gc> *
26037 get_types_needing_access_check (tree t)
26039 tree ti;
26040 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26042 if (!t || t == error_mark_node)
26043 return NULL;
26045 if (!(ti = get_template_info (t)))
26046 return NULL;
26048 if (CLASS_TYPE_P (t)
26049 || TREE_CODE (t) == FUNCTION_DECL)
26051 if (!TI_TEMPLATE (ti))
26052 return NULL;
26054 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26057 return result;
26060 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26061 tied to T. That list of typedefs will be access checked at
26062 T instantiation time.
26063 T is either a FUNCTION_DECL or a RECORD_TYPE.
26064 TYPE_DECL is a TYPE_DECL node representing a typedef.
26065 SCOPE is the scope through which TYPE_DECL is accessed.
26066 LOCATION is the location of the usage point of TYPE_DECL.
26068 This function is a subroutine of
26069 append_type_to_template_for_access_check. */
26071 static void
26072 append_type_to_template_for_access_check_1 (tree t,
26073 tree type_decl,
26074 tree scope,
26075 location_t location)
26077 qualified_typedef_usage_t typedef_usage;
26078 tree ti;
26080 if (!t || t == error_mark_node)
26081 return;
26083 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26084 || CLASS_TYPE_P (t))
26085 && type_decl
26086 && TREE_CODE (type_decl) == TYPE_DECL
26087 && scope);
26089 if (!(ti = get_template_info (t)))
26090 return;
26092 gcc_assert (TI_TEMPLATE (ti));
26094 typedef_usage.typedef_decl = type_decl;
26095 typedef_usage.context = scope;
26096 typedef_usage.locus = location;
26098 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26101 /* Append TYPE_DECL to the template TEMPL.
26102 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26103 At TEMPL instanciation time, TYPE_DECL will be checked to see
26104 if it can be accessed through SCOPE.
26105 LOCATION is the location of the usage point of TYPE_DECL.
26107 e.g. consider the following code snippet:
26109 class C
26111 typedef int myint;
26114 template<class U> struct S
26116 C::myint mi; // <-- usage point of the typedef C::myint
26119 S<char> s;
26121 At S<char> instantiation time, we need to check the access of C::myint
26122 In other words, we need to check the access of the myint typedef through
26123 the C scope. For that purpose, this function will add the myint typedef
26124 and the scope C through which its being accessed to a list of typedefs
26125 tied to the template S. That list will be walked at template instantiation
26126 time and access check performed on each typedefs it contains.
26127 Note that this particular code snippet should yield an error because
26128 myint is private to C. */
26130 void
26131 append_type_to_template_for_access_check (tree templ,
26132 tree type_decl,
26133 tree scope,
26134 location_t location)
26136 qualified_typedef_usage_t *iter;
26137 unsigned i;
26139 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26141 /* Make sure we don't append the type to the template twice. */
26142 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26143 if (iter->typedef_decl == type_decl && scope == iter->context)
26144 return;
26146 append_type_to_template_for_access_check_1 (templ, type_decl,
26147 scope, location);
26150 /* Convert the generic type parameters in PARM that match the types given in the
26151 range [START_IDX, END_IDX) from the current_template_parms into generic type
26152 packs. */
26154 tree
26155 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26157 tree current = current_template_parms;
26158 int depth = TMPL_PARMS_DEPTH (current);
26159 current = INNERMOST_TEMPLATE_PARMS (current);
26160 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26162 for (int i = 0; i < start_idx; ++i)
26163 TREE_VEC_ELT (replacement, i)
26164 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26166 for (int i = start_idx; i < end_idx; ++i)
26168 /* Create a distinct parameter pack type from the current parm and add it
26169 to the replacement args to tsubst below into the generic function
26170 parameter. */
26172 tree o = TREE_TYPE (TREE_VALUE
26173 (TREE_VEC_ELT (current, i)));
26174 tree t = copy_type (o);
26175 TEMPLATE_TYPE_PARM_INDEX (t)
26176 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26177 o, 0, 0, tf_none);
26178 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26179 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26180 TYPE_MAIN_VARIANT (t) = t;
26181 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26182 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26183 TREE_VEC_ELT (replacement, i) = t;
26184 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26187 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26188 TREE_VEC_ELT (replacement, i)
26189 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26191 /* If there are more levels then build up the replacement with the outer
26192 template parms. */
26193 if (depth > 1)
26194 replacement = add_to_template_args (template_parms_to_args
26195 (TREE_CHAIN (current_template_parms)),
26196 replacement);
26198 return tsubst (parm, replacement, tf_none, NULL_TREE);
26201 /* Entries in the decl_constraint hash table. */
26202 struct GTY((for_user)) constr_entry
26204 tree decl;
26205 tree ci;
26208 /* Hashing function and equality for constraint entries. */
26209 struct constr_hasher : ggc_ptr_hash<constr_entry>
26211 static hashval_t hash (constr_entry *e)
26213 return (hashval_t)DECL_UID (e->decl);
26216 static bool equal (constr_entry *e1, constr_entry *e2)
26218 return e1->decl == e2->decl;
26222 /* A mapping from declarations to constraint information. Note that
26223 both templates and their underlying declarations are mapped to the
26224 same constraint information.
26226 FIXME: This is defined in pt.c because garbage collection
26227 code is not being generated for constraint.cc. */
26229 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26231 /* Returns the template constraints of declaration T. If T is not
26232 constrained, return NULL_TREE. Note that T must be non-null. */
26234 tree
26235 get_constraints (tree t)
26237 if (!flag_concepts)
26238 return NULL_TREE;
26240 gcc_assert (DECL_P (t));
26241 if (TREE_CODE (t) == TEMPLATE_DECL)
26242 t = DECL_TEMPLATE_RESULT (t);
26243 constr_entry elt = { t, NULL_TREE };
26244 constr_entry* found = decl_constraints->find (&elt);
26245 if (found)
26246 return found->ci;
26247 else
26248 return NULL_TREE;
26251 /* Associate the given constraint information CI with the declaration
26252 T. If T is a template, then the constraints are associated with
26253 its underlying declaration. Don't build associations if CI is
26254 NULL_TREE. */
26256 void
26257 set_constraints (tree t, tree ci)
26259 if (!ci)
26260 return;
26261 gcc_assert (t && flag_concepts);
26262 if (TREE_CODE (t) == TEMPLATE_DECL)
26263 t = DECL_TEMPLATE_RESULT (t);
26264 gcc_assert (!get_constraints (t));
26265 constr_entry elt = {t, ci};
26266 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26267 constr_entry* entry = ggc_alloc<constr_entry> ();
26268 *entry = elt;
26269 *slot = entry;
26272 /* Remove the associated constraints of the declaration T. */
26274 void
26275 remove_constraints (tree t)
26277 gcc_assert (DECL_P (t));
26278 if (TREE_CODE (t) == TEMPLATE_DECL)
26279 t = DECL_TEMPLATE_RESULT (t);
26281 constr_entry elt = {t, NULL_TREE};
26282 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26283 if (slot)
26284 decl_constraints->clear_slot (slot);
26287 /* Memoized satisfaction results for declarations. This
26288 maps the pair (constraint_info, arguments) to the result computed
26289 by constraints_satisfied_p. */
26291 struct GTY((for_user)) constraint_sat_entry
26293 tree ci;
26294 tree args;
26295 tree result;
26298 /* Hashing function and equality for constraint entries. */
26300 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26302 static hashval_t hash (constraint_sat_entry *e)
26304 hashval_t val = iterative_hash_object(e->ci, 0);
26305 return iterative_hash_template_arg (e->args, val);
26308 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26310 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26314 /* Memoized satisfaction results for concept checks. */
26316 struct GTY((for_user)) concept_spec_entry
26318 tree tmpl;
26319 tree args;
26320 tree result;
26323 /* Hashing function and equality for constraint entries. */
26325 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26327 static hashval_t hash (concept_spec_entry *e)
26329 return hash_tmpl_and_args (e->tmpl, e->args);
26332 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26334 ++comparing_specializations;
26335 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26336 --comparing_specializations;
26337 return eq;
26341 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26342 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26344 /* Search for a memoized satisfaction result. Returns one of the
26345 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26347 tree
26348 lookup_constraint_satisfaction (tree ci, tree args)
26350 constraint_sat_entry elt = { ci, args, NULL_TREE };
26351 constraint_sat_entry* found = constraint_memos->find (&elt);
26352 if (found)
26353 return found->result;
26354 else
26355 return NULL_TREE;
26358 /* Memoize the result of a satisfication test. Returns the saved result. */
26360 tree
26361 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26363 constraint_sat_entry elt = {ci, args, result};
26364 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26365 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26366 *entry = elt;
26367 *slot = entry;
26368 return result;
26371 /* Search for a memoized satisfaction result for a concept. */
26373 tree
26374 lookup_concept_satisfaction (tree tmpl, tree args)
26376 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26377 concept_spec_entry* found = concept_memos->find (&elt);
26378 if (found)
26379 return found->result;
26380 else
26381 return NULL_TREE;
26384 /* Memoize the result of a concept check. Returns the saved result. */
26386 tree
26387 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26389 concept_spec_entry elt = {tmpl, args, result};
26390 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26391 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26392 *entry = elt;
26393 *slot = entry;
26394 return result;
26397 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26399 /* Returns a prior concept specialization. This returns the substituted
26400 and normalized constraints defined by the concept. */
26402 tree
26403 get_concept_expansion (tree tmpl, tree args)
26405 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26406 concept_spec_entry* found = concept_expansions->find (&elt);
26407 if (found)
26408 return found->result;
26409 else
26410 return NULL_TREE;
26413 /* Save a concept expansion for later. */
26415 tree
26416 save_concept_expansion (tree tmpl, tree args, tree def)
26418 concept_spec_entry elt = {tmpl, args, def};
26419 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26420 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26421 *entry = elt;
26422 *slot = entry;
26423 return def;
26426 static hashval_t
26427 hash_subsumption_args (tree t1, tree t2)
26429 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26430 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26431 int val = 0;
26432 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26433 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26434 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26435 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26436 return val;
26439 /* Compare the constraints of two subsumption entries. The LEFT1 and
26440 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26441 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26443 static bool
26444 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26446 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26447 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26448 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26449 CHECK_CONSTR_ARGS (right1)))
26450 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26451 CHECK_CONSTR_ARGS (right2));
26452 return false;
26455 /* Key/value pair for learning and memoizing subsumption results. This
26456 associates a pair of check constraints (including arguments) with
26457 a boolean value indicating the result. */
26459 struct GTY((for_user)) subsumption_entry
26461 tree t1;
26462 tree t2;
26463 bool result;
26466 /* Hashing function and equality for constraint entries. */
26468 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26470 static hashval_t hash (subsumption_entry *e)
26472 return hash_subsumption_args (e->t1, e->t2);
26475 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26477 ++comparing_specializations;
26478 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26479 --comparing_specializations;
26480 return eq;
26484 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26486 /* Search for a previously cached subsumption result. */
26488 bool*
26489 lookup_subsumption_result (tree t1, tree t2)
26491 subsumption_entry elt = { t1, t2, false };
26492 subsumption_entry* found = subsumption_table->find (&elt);
26493 if (found)
26494 return &found->result;
26495 else
26496 return 0;
26499 /* Save a subsumption result. */
26501 bool
26502 save_subsumption_result (tree t1, tree t2, bool result)
26504 subsumption_entry elt = {t1, t2, result};
26505 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26506 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26507 *entry = elt;
26508 *slot = entry;
26509 return result;
26512 /* Set up the hash table for constraint association. */
26514 void
26515 init_constraint_processing (void)
26517 if (!flag_concepts)
26518 return;
26520 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26521 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26522 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26523 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26524 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26527 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26528 0..N-1. */
26530 void
26531 declare_integer_pack (void)
26533 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26534 build_function_type_list (integer_type_node,
26535 integer_type_node,
26536 NULL_TREE),
26537 NULL_TREE, ECF_CONST);
26538 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26539 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26542 /* Set up the hash tables for template instantiations. */
26544 void
26545 init_template_processing (void)
26547 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26548 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26550 if (cxx_dialect >= cxx11)
26551 declare_integer_pack ();
26554 /* Print stats about the template hash tables for -fstats. */
26556 void
26557 print_template_statistics (void)
26559 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26560 "%f collisions\n", (long) decl_specializations->size (),
26561 (long) decl_specializations->elements (),
26562 decl_specializations->collisions ());
26563 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26564 "%f collisions\n", (long) type_specializations->size (),
26565 (long) type_specializations->elements (),
26566 type_specializations->collisions ());
26569 #include "gt-cp-pt.h"