* doc/generic.texi (ANNOTATE_EXPR): Document 3rd operand.
[official-gcc.git] / gcc / cp / pt.c
blob8bc9c221da71ff328fcd235c593aafeff8498916
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 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7523 because one of them is dependent. But we need to represent the
7524 conversion for the benefit of cp_tree_equal. */
7526 static tree
7527 maybe_convert_nontype_argument (tree type, tree arg)
7529 /* Auto parms get no conversion. */
7530 if (type_uses_auto (type))
7531 return arg;
7532 /* We don't need or want to add this conversion now if we're going to use the
7533 argument for deduction. */
7534 if (value_dependent_expression_p (arg))
7535 return arg;
7537 type = cv_unqualified (type);
7538 tree argtype = TREE_TYPE (arg);
7539 if (same_type_p (type, argtype))
7540 return arg;
7542 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7543 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7544 return arg;
7547 /* Convert the indicated template ARG as necessary to match the
7548 indicated template PARM. Returns the converted ARG, or
7549 error_mark_node if the conversion was unsuccessful. Error and
7550 warning messages are issued under control of COMPLAIN. This
7551 conversion is for the Ith parameter in the parameter list. ARGS is
7552 the full set of template arguments deduced so far. */
7554 static tree
7555 convert_template_argument (tree parm,
7556 tree arg,
7557 tree args,
7558 tsubst_flags_t complain,
7559 int i,
7560 tree in_decl)
7562 tree orig_arg;
7563 tree val;
7564 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7566 if (parm == error_mark_node)
7567 return error_mark_node;
7569 /* Trivially convert placeholders. */
7570 if (TREE_CODE (arg) == WILDCARD_DECL)
7571 return convert_wildcard_argument (parm, arg);
7573 if (arg == any_targ_node)
7574 return arg;
7576 if (TREE_CODE (arg) == TREE_LIST
7577 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7579 /* The template argument was the name of some
7580 member function. That's usually
7581 invalid, but static members are OK. In any
7582 case, grab the underlying fields/functions
7583 and issue an error later if required. */
7584 orig_arg = TREE_VALUE (arg);
7585 TREE_TYPE (arg) = unknown_type_node;
7588 orig_arg = arg;
7590 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7591 requires_type = (TREE_CODE (parm) == TYPE_DECL
7592 || requires_tmpl_type);
7594 /* When determining whether an argument pack expansion is a template,
7595 look at the pattern. */
7596 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7597 arg = PACK_EXPANSION_PATTERN (arg);
7599 /* Deal with an injected-class-name used as a template template arg. */
7600 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7602 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7603 if (TREE_CODE (t) == TEMPLATE_DECL)
7605 if (cxx_dialect >= cxx11)
7606 /* OK under DR 1004. */;
7607 else if (complain & tf_warning_or_error)
7608 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7609 " used as template template argument", TYPE_NAME (arg));
7610 else if (flag_pedantic_errors)
7611 t = arg;
7613 arg = t;
7617 is_tmpl_type =
7618 ((TREE_CODE (arg) == TEMPLATE_DECL
7619 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7620 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7621 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7622 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7624 if (is_tmpl_type
7625 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7626 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7627 arg = TYPE_STUB_DECL (arg);
7629 is_type = TYPE_P (arg) || is_tmpl_type;
7631 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7632 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7634 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7636 if (complain & tf_error)
7637 error ("invalid use of destructor %qE as a type", orig_arg);
7638 return error_mark_node;
7641 permerror (input_location,
7642 "to refer to a type member of a template parameter, "
7643 "use %<typename %E%>", orig_arg);
7645 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7646 TREE_OPERAND (arg, 1),
7647 typename_type,
7648 complain);
7649 arg = orig_arg;
7650 is_type = 1;
7652 if (is_type != requires_type)
7654 if (in_decl)
7656 if (complain & tf_error)
7658 error ("type/value mismatch at argument %d in template "
7659 "parameter list for %qD",
7660 i + 1, in_decl);
7661 if (is_type)
7662 inform (input_location,
7663 " expected a constant of type %qT, got %qT",
7664 TREE_TYPE (parm),
7665 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7666 else if (requires_tmpl_type)
7667 inform (input_location,
7668 " expected a class template, got %qE", orig_arg);
7669 else
7670 inform (input_location,
7671 " expected a type, got %qE", orig_arg);
7674 return error_mark_node;
7676 if (is_tmpl_type ^ requires_tmpl_type)
7678 if (in_decl && (complain & tf_error))
7680 error ("type/value mismatch at argument %d in template "
7681 "parameter list for %qD",
7682 i + 1, in_decl);
7683 if (is_tmpl_type)
7684 inform (input_location,
7685 " expected a type, got %qT", DECL_NAME (arg));
7686 else
7687 inform (input_location,
7688 " expected a class template, got %qT", orig_arg);
7690 return error_mark_node;
7693 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7694 /* We already did the appropriate conversion when packing args. */
7695 val = orig_arg;
7696 else if (is_type)
7698 if (requires_tmpl_type)
7700 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7701 /* The number of argument required is not known yet.
7702 Just accept it for now. */
7703 val = orig_arg;
7704 else
7706 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7707 tree argparm;
7709 /* Strip alias templates that are equivalent to another
7710 template. */
7711 arg = get_underlying_template (arg);
7712 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7714 if (coerce_template_template_parms (parmparm, argparm,
7715 complain, in_decl,
7716 args))
7718 val = arg;
7720 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7721 TEMPLATE_DECL. */
7722 if (val != error_mark_node)
7724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7725 val = TREE_TYPE (val);
7726 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7727 val = make_pack_expansion (val, complain);
7730 else
7732 if (in_decl && (complain & tf_error))
7734 error ("type/value mismatch at argument %d in "
7735 "template parameter list for %qD",
7736 i + 1, in_decl);
7737 inform (input_location,
7738 " expected a template of type %qD, got %qT",
7739 parm, orig_arg);
7742 val = error_mark_node;
7745 // Check that the constraints are compatible before allowing the
7746 // substitution.
7747 if (val != error_mark_node)
7748 if (!is_compatible_template_arg (parm, arg))
7750 if (in_decl && (complain & tf_error))
7752 error ("constraint mismatch at argument %d in "
7753 "template parameter list for %qD",
7754 i + 1, in_decl);
7755 inform (input_location, " expected %qD but got %qD",
7756 parm, arg);
7758 val = error_mark_node;
7762 else
7763 val = orig_arg;
7764 /* We only form one instance of each template specialization.
7765 Therefore, if we use a non-canonical variant (i.e., a
7766 typedef), any future messages referring to the type will use
7767 the typedef, which is confusing if those future uses do not
7768 themselves also use the typedef. */
7769 if (TYPE_P (val))
7770 val = canonicalize_type_argument (val, complain);
7772 else
7774 tree t = TREE_TYPE (parm);
7776 if (tree a = type_uses_auto (t))
7778 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7779 if (t == error_mark_node)
7780 return error_mark_node;
7782 else
7783 t = tsubst (t, args, complain, in_decl);
7785 if (invalid_nontype_parm_type_p (t, complain))
7786 return error_mark_node;
7788 if (!type_dependent_expression_p (orig_arg)
7789 && !uses_template_parms (t))
7790 /* We used to call digest_init here. However, digest_init
7791 will report errors, which we don't want when complain
7792 is zero. More importantly, digest_init will try too
7793 hard to convert things: for example, `0' should not be
7794 converted to pointer type at this point according to
7795 the standard. Accepting this is not merely an
7796 extension, since deciding whether or not these
7797 conversions can occur is part of determining which
7798 function template to call, or whether a given explicit
7799 argument specification is valid. */
7800 val = convert_nontype_argument (t, orig_arg, complain);
7801 else
7803 val = canonicalize_expr_argument (orig_arg, complain);
7804 val = maybe_convert_nontype_argument (t, val);
7808 if (val == NULL_TREE)
7809 val = error_mark_node;
7810 else if (val == error_mark_node && (complain & tf_error))
7811 error ("could not convert template argument %qE from %qT to %qT",
7812 orig_arg, TREE_TYPE (orig_arg), t);
7814 if (INDIRECT_REF_P (val))
7816 /* Reject template arguments that are references to built-in
7817 functions with no library fallbacks. */
7818 const_tree inner = TREE_OPERAND (val, 0);
7819 const_tree innertype = TREE_TYPE (inner);
7820 if (innertype
7821 && TREE_CODE (innertype) == REFERENCE_TYPE
7822 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7823 && 0 < TREE_OPERAND_LENGTH (inner)
7824 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7825 return error_mark_node;
7828 if (TREE_CODE (val) == SCOPE_REF)
7830 /* Strip typedefs from the SCOPE_REF. */
7831 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7832 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7833 complain);
7834 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7835 QUALIFIED_NAME_IS_TEMPLATE (val));
7839 return val;
7842 /* Coerces the remaining template arguments in INNER_ARGS (from
7843 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7844 Returns the coerced argument pack. PARM_IDX is the position of this
7845 parameter in the template parameter list. ARGS is the original
7846 template argument list. */
7847 static tree
7848 coerce_template_parameter_pack (tree parms,
7849 int parm_idx,
7850 tree args,
7851 tree inner_args,
7852 int arg_idx,
7853 tree new_args,
7854 int* lost,
7855 tree in_decl,
7856 tsubst_flags_t complain)
7858 tree parm = TREE_VEC_ELT (parms, parm_idx);
7859 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7860 tree packed_args;
7861 tree argument_pack;
7862 tree packed_parms = NULL_TREE;
7864 if (arg_idx > nargs)
7865 arg_idx = nargs;
7867 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7869 /* When the template parameter is a non-type template parameter pack
7870 or template template parameter pack whose type or template
7871 parameters use parameter packs, we know exactly how many arguments
7872 we are looking for. Build a vector of the instantiated decls for
7873 these template parameters in PACKED_PARMS. */
7874 /* We can't use make_pack_expansion here because it would interpret a
7875 _DECL as a use rather than a declaration. */
7876 tree decl = TREE_VALUE (parm);
7877 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7878 SET_PACK_EXPANSION_PATTERN (exp, decl);
7879 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7880 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7882 TREE_VEC_LENGTH (args)--;
7883 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7884 TREE_VEC_LENGTH (args)++;
7886 if (packed_parms == error_mark_node)
7887 return error_mark_node;
7889 /* If we're doing a partial instantiation of a member template,
7890 verify that all of the types used for the non-type
7891 template parameter pack are, in fact, valid for non-type
7892 template parameters. */
7893 if (arg_idx < nargs
7894 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7896 int j, len = TREE_VEC_LENGTH (packed_parms);
7897 for (j = 0; j < len; ++j)
7899 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7900 if (invalid_nontype_parm_type_p (t, complain))
7901 return error_mark_node;
7903 /* We don't know how many args we have yet, just
7904 use the unconverted ones for now. */
7905 return NULL_TREE;
7908 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7910 /* Check if we have a placeholder pack, which indicates we're
7911 in the context of a introduction list. In that case we want
7912 to match this pack to the single placeholder. */
7913 else if (arg_idx < nargs
7914 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7915 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7917 nargs = arg_idx + 1;
7918 packed_args = make_tree_vec (1);
7920 else
7921 packed_args = make_tree_vec (nargs - arg_idx);
7923 /* Convert the remaining arguments, which will be a part of the
7924 parameter pack "parm". */
7925 int first_pack_arg = arg_idx;
7926 for (; arg_idx < nargs; ++arg_idx)
7928 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7929 tree actual_parm = TREE_VALUE (parm);
7930 int pack_idx = arg_idx - first_pack_arg;
7932 if (packed_parms)
7934 /* Once we've packed as many args as we have types, stop. */
7935 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7936 break;
7937 else if (PACK_EXPANSION_P (arg))
7938 /* We don't know how many args we have yet, just
7939 use the unconverted ones for now. */
7940 return NULL_TREE;
7941 else
7942 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7945 if (arg == error_mark_node)
7947 if (complain & tf_error)
7948 error ("template argument %d is invalid", arg_idx + 1);
7950 else
7951 arg = convert_template_argument (actual_parm,
7952 arg, new_args, complain, parm_idx,
7953 in_decl);
7954 if (arg == error_mark_node)
7955 (*lost)++;
7956 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7959 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7960 && TREE_VEC_LENGTH (packed_args) > 0)
7962 if (complain & tf_error)
7963 error ("wrong number of template arguments (%d, should be %d)",
7964 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7965 return error_mark_node;
7968 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7969 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7970 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7971 else
7973 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7974 TREE_CONSTANT (argument_pack) = 1;
7977 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7978 if (CHECKING_P)
7979 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7980 TREE_VEC_LENGTH (packed_args));
7981 return argument_pack;
7984 /* Returns the number of pack expansions in the template argument vector
7985 ARGS. */
7987 static int
7988 pack_expansion_args_count (tree args)
7990 int i;
7991 int count = 0;
7992 if (args)
7993 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7995 tree elt = TREE_VEC_ELT (args, i);
7996 if (elt && PACK_EXPANSION_P (elt))
7997 ++count;
7999 return count;
8002 /* Convert all template arguments to their appropriate types, and
8003 return a vector containing the innermost resulting template
8004 arguments. If any error occurs, return error_mark_node. Error and
8005 warning messages are issued under control of COMPLAIN.
8007 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8008 for arguments not specified in ARGS. Otherwise, if
8009 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8010 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8011 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8012 ARGS. */
8014 static tree
8015 coerce_template_parms (tree parms,
8016 tree args,
8017 tree in_decl,
8018 tsubst_flags_t complain,
8019 bool require_all_args,
8020 bool use_default_args)
8022 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8023 tree orig_inner_args;
8024 tree inner_args;
8025 tree new_args;
8026 tree new_inner_args;
8027 int saved_unevaluated_operand;
8028 int saved_inhibit_evaluation_warnings;
8030 /* When used as a boolean value, indicates whether this is a
8031 variadic template parameter list. Since it's an int, we can also
8032 subtract it from nparms to get the number of non-variadic
8033 parameters. */
8034 int variadic_p = 0;
8035 int variadic_args_p = 0;
8036 int post_variadic_parms = 0;
8038 /* Likewise for parameters with default arguments. */
8039 int default_p = 0;
8041 if (args == error_mark_node)
8042 return error_mark_node;
8044 nparms = TREE_VEC_LENGTH (parms);
8046 /* Determine if there are any parameter packs or default arguments. */
8047 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8049 tree parm = TREE_VEC_ELT (parms, parm_idx);
8050 if (variadic_p)
8051 ++post_variadic_parms;
8052 if (template_parameter_pack_p (TREE_VALUE (parm)))
8053 ++variadic_p;
8054 if (TREE_PURPOSE (parm))
8055 ++default_p;
8058 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8059 /* If there are no parameters that follow a parameter pack, we need to
8060 expand any argument packs so that we can deduce a parameter pack from
8061 some non-packed args followed by an argument pack, as in variadic85.C.
8062 If there are such parameters, we need to leave argument packs intact
8063 so the arguments are assigned properly. This can happen when dealing
8064 with a nested class inside a partial specialization of a class
8065 template, as in variadic92.C, or when deducing a template parameter pack
8066 from a sub-declarator, as in variadic114.C. */
8067 if (!post_variadic_parms)
8068 inner_args = expand_template_argument_pack (inner_args);
8070 /* Count any pack expansion args. */
8071 variadic_args_p = pack_expansion_args_count (inner_args);
8073 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8074 if ((nargs - variadic_args_p > nparms && !variadic_p)
8075 || (nargs < nparms - variadic_p
8076 && require_all_args
8077 && !variadic_args_p
8078 && (!use_default_args
8079 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8080 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8082 if (complain & tf_error)
8084 if (variadic_p || default_p)
8086 nparms -= variadic_p + default_p;
8087 error ("wrong number of template arguments "
8088 "(%d, should be at least %d)", nargs, nparms);
8090 else
8091 error ("wrong number of template arguments "
8092 "(%d, should be %d)", nargs, nparms);
8094 if (in_decl)
8095 inform (DECL_SOURCE_LOCATION (in_decl),
8096 "provided for %qD", in_decl);
8099 return error_mark_node;
8101 /* We can't pass a pack expansion to a non-pack parameter of an alias
8102 template (DR 1430). */
8103 else if (in_decl
8104 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8105 || concept_template_p (in_decl))
8106 && variadic_args_p
8107 && nargs - variadic_args_p < nparms - variadic_p)
8109 if (complain & tf_error)
8111 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8113 tree arg = TREE_VEC_ELT (inner_args, i);
8114 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8116 if (PACK_EXPANSION_P (arg)
8117 && !template_parameter_pack_p (parm))
8119 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8120 error_at (location_of (arg),
8121 "pack expansion argument for non-pack parameter "
8122 "%qD of alias template %qD", parm, in_decl);
8123 else
8124 error_at (location_of (arg),
8125 "pack expansion argument for non-pack parameter "
8126 "%qD of concept %qD", parm, in_decl);
8127 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8128 goto found;
8131 gcc_unreachable ();
8132 found:;
8134 return error_mark_node;
8137 /* We need to evaluate the template arguments, even though this
8138 template-id may be nested within a "sizeof". */
8139 saved_unevaluated_operand = cp_unevaluated_operand;
8140 cp_unevaluated_operand = 0;
8141 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8142 c_inhibit_evaluation_warnings = 0;
8143 new_inner_args = make_tree_vec (nparms);
8144 new_args = add_outermost_template_args (args, new_inner_args);
8145 int pack_adjust = 0;
8146 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8148 tree arg;
8149 tree parm;
8151 /* Get the Ith template parameter. */
8152 parm = TREE_VEC_ELT (parms, parm_idx);
8154 if (parm == error_mark_node)
8156 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8157 continue;
8160 /* Calculate the next argument. */
8161 if (arg_idx < nargs)
8162 arg = TREE_VEC_ELT (inner_args, arg_idx);
8163 else
8164 arg = NULL_TREE;
8166 if (template_parameter_pack_p (TREE_VALUE (parm))
8167 && !(arg && ARGUMENT_PACK_P (arg)))
8169 /* Some arguments will be placed in the
8170 template parameter pack PARM. */
8171 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8172 inner_args, arg_idx,
8173 new_args, &lost,
8174 in_decl, complain);
8176 if (arg == NULL_TREE)
8178 /* We don't know how many args we have yet, just use the
8179 unconverted (and still packed) ones for now. */
8180 new_inner_args = orig_inner_args;
8181 arg_idx = nargs;
8182 break;
8185 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8187 /* Store this argument. */
8188 if (arg == error_mark_node)
8190 lost++;
8191 /* We are done with all of the arguments. */
8192 arg_idx = nargs;
8194 else
8196 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8197 arg_idx += pack_adjust;
8200 continue;
8202 else if (arg)
8204 if (PACK_EXPANSION_P (arg))
8206 /* "If every valid specialization of a variadic template
8207 requires an empty template parameter pack, the template is
8208 ill-formed, no diagnostic required." So check that the
8209 pattern works with this parameter. */
8210 tree pattern = PACK_EXPANSION_PATTERN (arg);
8211 tree conv = convert_template_argument (TREE_VALUE (parm),
8212 pattern, new_args,
8213 complain, parm_idx,
8214 in_decl);
8215 if (conv == error_mark_node)
8217 if (complain & tf_error)
8218 inform (input_location, "so any instantiation with a "
8219 "non-empty parameter pack would be ill-formed");
8220 ++lost;
8222 else if (TYPE_P (conv) && !TYPE_P (pattern))
8223 /* Recover from missing typename. */
8224 TREE_VEC_ELT (inner_args, arg_idx)
8225 = make_pack_expansion (conv, complain);
8227 /* We don't know how many args we have yet, just
8228 use the unconverted ones for now. */
8229 new_inner_args = inner_args;
8230 arg_idx = nargs;
8231 break;
8234 else if (require_all_args)
8236 /* There must be a default arg in this case. */
8237 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8238 complain, in_decl);
8239 /* The position of the first default template argument,
8240 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8241 Record that. */
8242 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8243 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8244 arg_idx - pack_adjust);
8246 else
8247 break;
8249 if (arg == error_mark_node)
8251 if (complain & tf_error)
8252 error ("template argument %d is invalid", arg_idx + 1);
8254 else if (!arg)
8255 /* This only occurs if there was an error in the template
8256 parameter list itself (which we would already have
8257 reported) that we are trying to recover from, e.g., a class
8258 template with a parameter list such as
8259 template<typename..., typename>. */
8260 ++lost;
8261 else
8262 arg = convert_template_argument (TREE_VALUE (parm),
8263 arg, new_args, complain,
8264 parm_idx, in_decl);
8266 if (arg == error_mark_node)
8267 lost++;
8268 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8270 cp_unevaluated_operand = saved_unevaluated_operand;
8271 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8273 if (variadic_p && arg_idx < nargs)
8275 if (complain & tf_error)
8277 error ("wrong number of template arguments "
8278 "(%d, should be %d)", nargs, arg_idx);
8279 if (in_decl)
8280 error ("provided for %q+D", in_decl);
8282 return error_mark_node;
8285 if (lost)
8286 return error_mark_node;
8288 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8289 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8290 TREE_VEC_LENGTH (new_inner_args));
8292 return new_inner_args;
8295 /* Convert all template arguments to their appropriate types, and
8296 return a vector containing the innermost resulting template
8297 arguments. If any error occurs, return error_mark_node. Error and
8298 warning messages are not issued.
8300 Note that no function argument deduction is performed, and default
8301 arguments are used to fill in unspecified arguments. */
8302 tree
8303 coerce_template_parms (tree parms, tree args, tree in_decl)
8305 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8308 /* Convert all template arguments to their appropriate type, and
8309 instantiate default arguments as needed. This returns a vector
8310 containing the innermost resulting template arguments, or
8311 error_mark_node if unsuccessful. */
8312 tree
8313 coerce_template_parms (tree parms, tree args, tree in_decl,
8314 tsubst_flags_t complain)
8316 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8319 /* Like coerce_template_parms. If PARMS represents all template
8320 parameters levels, this function returns a vector of vectors
8321 representing all the resulting argument levels. Note that in this
8322 case, only the innermost arguments are coerced because the
8323 outermost ones are supposed to have been coerced already.
8325 Otherwise, if PARMS represents only (the innermost) vector of
8326 parameters, this function returns a vector containing just the
8327 innermost resulting arguments. */
8329 static tree
8330 coerce_innermost_template_parms (tree parms,
8331 tree args,
8332 tree in_decl,
8333 tsubst_flags_t complain,
8334 bool require_all_args,
8335 bool use_default_args)
8337 int parms_depth = TMPL_PARMS_DEPTH (parms);
8338 int args_depth = TMPL_ARGS_DEPTH (args);
8339 tree coerced_args;
8341 if (parms_depth > 1)
8343 coerced_args = make_tree_vec (parms_depth);
8344 tree level;
8345 int cur_depth;
8347 for (level = parms, cur_depth = parms_depth;
8348 parms_depth > 0 && level != NULL_TREE;
8349 level = TREE_CHAIN (level), --cur_depth)
8351 tree l;
8352 if (cur_depth == args_depth)
8353 l = coerce_template_parms (TREE_VALUE (level),
8354 args, in_decl, complain,
8355 require_all_args,
8356 use_default_args);
8357 else
8358 l = TMPL_ARGS_LEVEL (args, cur_depth);
8360 if (l == error_mark_node)
8361 return error_mark_node;
8363 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8366 else
8367 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8368 args, in_decl, complain,
8369 require_all_args,
8370 use_default_args);
8371 return coerced_args;
8374 /* Returns 1 if template args OT and NT are equivalent. */
8377 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8379 if (nt == ot)
8380 return 1;
8381 if (nt == NULL_TREE || ot == NULL_TREE)
8382 return false;
8383 if (nt == any_targ_node || ot == any_targ_node)
8384 return true;
8386 if (TREE_CODE (nt) == TREE_VEC)
8387 /* For member templates */
8388 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8389 else if (PACK_EXPANSION_P (ot))
8390 return (PACK_EXPANSION_P (nt)
8391 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8392 PACK_EXPANSION_PATTERN (nt))
8393 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8394 PACK_EXPANSION_EXTRA_ARGS (nt)));
8395 else if (ARGUMENT_PACK_P (ot))
8397 int i, len;
8398 tree opack, npack;
8400 if (!ARGUMENT_PACK_P (nt))
8401 return 0;
8403 opack = ARGUMENT_PACK_ARGS (ot);
8404 npack = ARGUMENT_PACK_ARGS (nt);
8405 len = TREE_VEC_LENGTH (opack);
8406 if (TREE_VEC_LENGTH (npack) != len)
8407 return 0;
8408 for (i = 0; i < len; ++i)
8409 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8410 TREE_VEC_ELT (npack, i)))
8411 return 0;
8412 return 1;
8414 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8415 gcc_unreachable ();
8416 else if (TYPE_P (nt))
8418 if (!TYPE_P (ot))
8419 return false;
8420 /* Don't treat an alias template specialization with dependent
8421 arguments as equivalent to its underlying type when used as a
8422 template argument; we need them to be distinct so that we
8423 substitute into the specialization arguments at instantiation
8424 time. And aliases can't be equivalent without being ==, so
8425 we don't need to look any deeper.
8427 During partial ordering, however, we need to treat them normally so
8428 that we can order uses of the same alias with different
8429 cv-qualification (79960). */
8430 if (!partial_order
8431 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8432 return false;
8433 else
8434 return same_type_p (ot, nt);
8436 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8437 return 0;
8438 else
8440 /* Try to treat a template non-type argument that has been converted
8441 to the parameter type as equivalent to one that hasn't yet. */
8442 for (enum tree_code code1 = TREE_CODE (ot);
8443 CONVERT_EXPR_CODE_P (code1)
8444 || code1 == NON_LVALUE_EXPR;
8445 code1 = TREE_CODE (ot))
8446 ot = TREE_OPERAND (ot, 0);
8447 for (enum tree_code code2 = TREE_CODE (nt);
8448 CONVERT_EXPR_CODE_P (code2)
8449 || code2 == NON_LVALUE_EXPR;
8450 code2 = TREE_CODE (nt))
8451 nt = TREE_OPERAND (nt, 0);
8453 return cp_tree_equal (ot, nt);
8457 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8458 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8459 NEWARG_PTR with the offending arguments if they are non-NULL. */
8462 comp_template_args (tree oldargs, tree newargs,
8463 tree *oldarg_ptr, tree *newarg_ptr,
8464 bool partial_order)
8466 int i;
8468 if (oldargs == newargs)
8469 return 1;
8471 if (!oldargs || !newargs)
8472 return 0;
8474 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8475 return 0;
8477 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8479 tree nt = TREE_VEC_ELT (newargs, i);
8480 tree ot = TREE_VEC_ELT (oldargs, i);
8482 if (! template_args_equal (ot, nt, partial_order))
8484 if (oldarg_ptr != NULL)
8485 *oldarg_ptr = ot;
8486 if (newarg_ptr != NULL)
8487 *newarg_ptr = nt;
8488 return 0;
8491 return 1;
8494 inline bool
8495 comp_template_args_porder (tree oargs, tree nargs)
8497 return comp_template_args (oargs, nargs, NULL, NULL, true);
8500 static void
8501 add_pending_template (tree d)
8503 tree ti = (TYPE_P (d)
8504 ? CLASSTYPE_TEMPLATE_INFO (d)
8505 : DECL_TEMPLATE_INFO (d));
8506 struct pending_template *pt;
8507 int level;
8509 if (TI_PENDING_TEMPLATE_FLAG (ti))
8510 return;
8512 /* We are called both from instantiate_decl, where we've already had a
8513 tinst_level pushed, and instantiate_template, where we haven't.
8514 Compensate. */
8515 level = !current_tinst_level || current_tinst_level->decl != d;
8517 if (level)
8518 push_tinst_level (d);
8520 pt = ggc_alloc<pending_template> ();
8521 pt->next = NULL;
8522 pt->tinst = current_tinst_level;
8523 if (last_pending_template)
8524 last_pending_template->next = pt;
8525 else
8526 pending_templates = pt;
8528 last_pending_template = pt;
8530 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8532 if (level)
8533 pop_tinst_level ();
8537 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8538 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8539 documentation for TEMPLATE_ID_EXPR. */
8541 tree
8542 lookup_template_function (tree fns, tree arglist)
8544 tree type;
8546 if (fns == error_mark_node || arglist == error_mark_node)
8547 return error_mark_node;
8549 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8551 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8553 error ("%q#D is not a function template", fns);
8554 return error_mark_node;
8557 if (BASELINK_P (fns))
8559 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8560 unknown_type_node,
8561 BASELINK_FUNCTIONS (fns),
8562 arglist);
8563 return fns;
8566 type = TREE_TYPE (fns);
8567 if (TREE_CODE (fns) == OVERLOAD || !type)
8568 type = unknown_type_node;
8570 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8573 /* Within the scope of a template class S<T>, the name S gets bound
8574 (in build_self_reference) to a TYPE_DECL for the class, not a
8575 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8576 or one of its enclosing classes, and that type is a template,
8577 return the associated TEMPLATE_DECL. Otherwise, the original
8578 DECL is returned.
8580 Also handle the case when DECL is a TREE_LIST of ambiguous
8581 injected-class-names from different bases. */
8583 tree
8584 maybe_get_template_decl_from_type_decl (tree decl)
8586 if (decl == NULL_TREE)
8587 return decl;
8589 /* DR 176: A lookup that finds an injected-class-name (10.2
8590 [class.member.lookup]) can result in an ambiguity in certain cases
8591 (for example, if it is found in more than one base class). If all of
8592 the injected-class-names that are found refer to specializations of
8593 the same class template, and if the name is followed by a
8594 template-argument-list, the reference refers to the class template
8595 itself and not a specialization thereof, and is not ambiguous. */
8596 if (TREE_CODE (decl) == TREE_LIST)
8598 tree t, tmpl = NULL_TREE;
8599 for (t = decl; t; t = TREE_CHAIN (t))
8601 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8602 if (!tmpl)
8603 tmpl = elt;
8604 else if (tmpl != elt)
8605 break;
8607 if (tmpl && t == NULL_TREE)
8608 return tmpl;
8609 else
8610 return decl;
8613 return (decl != NULL_TREE
8614 && DECL_SELF_REFERENCE_P (decl)
8615 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8616 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8619 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8620 parameters, find the desired type.
8622 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8624 IN_DECL, if non-NULL, is the template declaration we are trying to
8625 instantiate.
8627 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8628 the class we are looking up.
8630 Issue error and warning messages under control of COMPLAIN.
8632 If the template class is really a local class in a template
8633 function, then the FUNCTION_CONTEXT is the function in which it is
8634 being instantiated.
8636 ??? Note that this function is currently called *twice* for each
8637 template-id: the first time from the parser, while creating the
8638 incomplete type (finish_template_type), and the second type during the
8639 real instantiation (instantiate_template_class). This is surely something
8640 that we want to avoid. It also causes some problems with argument
8641 coercion (see convert_nontype_argument for more information on this). */
8643 static tree
8644 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8645 int entering_scope, tsubst_flags_t complain)
8647 tree templ = NULL_TREE, parmlist;
8648 tree t;
8649 spec_entry **slot;
8650 spec_entry *entry;
8651 spec_entry elt;
8652 hashval_t hash;
8654 if (identifier_p (d1))
8656 tree value = innermost_non_namespace_value (d1);
8657 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8658 templ = value;
8659 else
8661 if (context)
8662 push_decl_namespace (context);
8663 templ = lookup_name (d1);
8664 templ = maybe_get_template_decl_from_type_decl (templ);
8665 if (context)
8666 pop_decl_namespace ();
8668 if (templ)
8669 context = DECL_CONTEXT (templ);
8671 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8673 tree type = TREE_TYPE (d1);
8675 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8676 an implicit typename for the second A. Deal with it. */
8677 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8678 type = TREE_TYPE (type);
8680 if (CLASSTYPE_TEMPLATE_INFO (type))
8682 templ = CLASSTYPE_TI_TEMPLATE (type);
8683 d1 = DECL_NAME (templ);
8686 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8687 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8689 templ = TYPE_TI_TEMPLATE (d1);
8690 d1 = DECL_NAME (templ);
8692 else if (DECL_TYPE_TEMPLATE_P (d1))
8694 templ = d1;
8695 d1 = DECL_NAME (templ);
8696 context = DECL_CONTEXT (templ);
8698 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8700 templ = d1;
8701 d1 = DECL_NAME (templ);
8704 /* Issue an error message if we didn't find a template. */
8705 if (! templ)
8707 if (complain & tf_error)
8708 error ("%qT is not a template", d1);
8709 return error_mark_node;
8712 if (TREE_CODE (templ) != TEMPLATE_DECL
8713 /* Make sure it's a user visible template, if it was named by
8714 the user. */
8715 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8716 && !PRIMARY_TEMPLATE_P (templ)))
8718 if (complain & tf_error)
8720 error ("non-template type %qT used as a template", d1);
8721 if (in_decl)
8722 error ("for template declaration %q+D", in_decl);
8724 return error_mark_node;
8727 complain &= ~tf_user;
8729 /* An alias that just changes the name of a template is equivalent to the
8730 other template, so if any of the arguments are pack expansions, strip
8731 the alias to avoid problems with a pack expansion passed to a non-pack
8732 alias template parameter (DR 1430). */
8733 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8734 templ = get_underlying_template (templ);
8736 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8738 tree parm;
8739 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8740 if (arglist2 == error_mark_node
8741 || (!uses_template_parms (arglist2)
8742 && check_instantiated_args (templ, arglist2, complain)))
8743 return error_mark_node;
8745 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8746 return parm;
8748 else
8750 tree template_type = TREE_TYPE (templ);
8751 tree gen_tmpl;
8752 tree type_decl;
8753 tree found = NULL_TREE;
8754 int arg_depth;
8755 int parm_depth;
8756 int is_dependent_type;
8757 int use_partial_inst_tmpl = false;
8759 if (template_type == error_mark_node)
8760 /* An error occurred while building the template TEMPL, and a
8761 diagnostic has most certainly been emitted for that
8762 already. Let's propagate that error. */
8763 return error_mark_node;
8765 gen_tmpl = most_general_template (templ);
8766 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8767 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8768 arg_depth = TMPL_ARGS_DEPTH (arglist);
8770 if (arg_depth == 1 && parm_depth > 1)
8772 /* We've been given an incomplete set of template arguments.
8773 For example, given:
8775 template <class T> struct S1 {
8776 template <class U> struct S2 {};
8777 template <class U> struct S2<U*> {};
8780 we will be called with an ARGLIST of `U*', but the
8781 TEMPLATE will be `template <class T> template
8782 <class U> struct S1<T>::S2'. We must fill in the missing
8783 arguments. */
8784 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8785 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8786 arg_depth = TMPL_ARGS_DEPTH (arglist);
8789 /* Now we should have enough arguments. */
8790 gcc_assert (parm_depth == arg_depth);
8792 /* From here on, we're only interested in the most general
8793 template. */
8795 /* Calculate the BOUND_ARGS. These will be the args that are
8796 actually tsubst'd into the definition to create the
8797 instantiation. */
8798 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8799 complain,
8800 /*require_all_args=*/true,
8801 /*use_default_args=*/true);
8803 if (arglist == error_mark_node)
8804 /* We were unable to bind the arguments. */
8805 return error_mark_node;
8807 /* In the scope of a template class, explicit references to the
8808 template class refer to the type of the template, not any
8809 instantiation of it. For example, in:
8811 template <class T> class C { void f(C<T>); }
8813 the `C<T>' is just the same as `C'. Outside of the
8814 class, however, such a reference is an instantiation. */
8815 if (entering_scope
8816 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8817 || currently_open_class (template_type))
8819 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8821 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8822 return template_type;
8825 /* If we already have this specialization, return it. */
8826 elt.tmpl = gen_tmpl;
8827 elt.args = arglist;
8828 elt.spec = NULL_TREE;
8829 hash = spec_hasher::hash (&elt);
8830 entry = type_specializations->find_with_hash (&elt, hash);
8832 if (entry)
8833 return entry->spec;
8835 /* If the the template's constraints are not satisfied,
8836 then we cannot form a valid type.
8838 Note that the check is deferred until after the hash
8839 lookup. This prevents redundant checks on previously
8840 instantiated specializations. */
8841 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8843 if (complain & tf_error)
8845 error ("template constraint failure");
8846 diagnose_constraints (input_location, gen_tmpl, arglist);
8848 return error_mark_node;
8851 is_dependent_type = uses_template_parms (arglist);
8853 /* If the deduced arguments are invalid, then the binding
8854 failed. */
8855 if (!is_dependent_type
8856 && check_instantiated_args (gen_tmpl,
8857 INNERMOST_TEMPLATE_ARGS (arglist),
8858 complain))
8859 return error_mark_node;
8861 if (!is_dependent_type
8862 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8863 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8864 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8866 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8867 DECL_NAME (gen_tmpl),
8868 /*tag_scope=*/ts_global);
8869 return found;
8872 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8873 complain, in_decl);
8874 if (context == error_mark_node)
8875 return error_mark_node;
8877 if (!context)
8878 context = global_namespace;
8880 /* Create the type. */
8881 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8883 /* The user referred to a specialization of an alias
8884 template represented by GEN_TMPL.
8886 [temp.alias]/2 says:
8888 When a template-id refers to the specialization of an
8889 alias template, it is equivalent to the associated
8890 type obtained by substitution of its
8891 template-arguments for the template-parameters in the
8892 type-id of the alias template. */
8894 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8895 /* Note that the call above (by indirectly calling
8896 register_specialization in tsubst_decl) registers the
8897 TYPE_DECL representing the specialization of the alias
8898 template. So next time someone substitutes ARGLIST for
8899 the template parms into the alias template (GEN_TMPL),
8900 she'll get that TYPE_DECL back. */
8902 if (t == error_mark_node)
8903 return t;
8905 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8907 if (!is_dependent_type)
8909 set_current_access_from_decl (TYPE_NAME (template_type));
8910 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8911 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8912 arglist, complain, in_decl),
8913 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8914 arglist, complain, in_decl),
8915 SCOPED_ENUM_P (template_type), NULL);
8917 if (t == error_mark_node)
8918 return t;
8920 else
8922 /* We don't want to call start_enum for this type, since
8923 the values for the enumeration constants may involve
8924 template parameters. And, no one should be interested
8925 in the enumeration constants for such a type. */
8926 t = cxx_make_type (ENUMERAL_TYPE);
8927 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8929 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8930 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8931 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8933 else if (CLASS_TYPE_P (template_type))
8935 t = make_class_type (TREE_CODE (template_type));
8936 CLASSTYPE_DECLARED_CLASS (t)
8937 = CLASSTYPE_DECLARED_CLASS (template_type);
8938 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8940 /* A local class. Make sure the decl gets registered properly. */
8941 if (context == current_function_decl)
8942 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8944 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8945 /* This instantiation is another name for the primary
8946 template type. Set the TYPE_CANONICAL field
8947 appropriately. */
8948 TYPE_CANONICAL (t) = template_type;
8949 else if (any_template_arguments_need_structural_equality_p (arglist))
8950 /* Some of the template arguments require structural
8951 equality testing, so this template class requires
8952 structural equality testing. */
8953 SET_TYPE_STRUCTURAL_EQUALITY (t);
8955 else
8956 gcc_unreachable ();
8958 /* If we called start_enum or pushtag above, this information
8959 will already be set up. */
8960 if (!TYPE_NAME (t))
8962 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8964 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8965 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8966 DECL_SOURCE_LOCATION (type_decl)
8967 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8969 else
8970 type_decl = TYPE_NAME (t);
8972 if (CLASS_TYPE_P (template_type))
8974 TREE_PRIVATE (type_decl)
8975 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8976 TREE_PROTECTED (type_decl)
8977 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8978 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8980 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8981 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8985 if (OVERLOAD_TYPE_P (t)
8986 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8988 static const char *tags[] = {"abi_tag", "may_alias"};
8990 for (unsigned ix = 0; ix != 2; ix++)
8992 tree attributes
8993 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8995 if (attributes)
8996 TYPE_ATTRIBUTES (t)
8997 = tree_cons (TREE_PURPOSE (attributes),
8998 TREE_VALUE (attributes),
8999 TYPE_ATTRIBUTES (t));
9003 /* Let's consider the explicit specialization of a member
9004 of a class template specialization that is implicitly instantiated,
9005 e.g.:
9006 template<class T>
9007 struct S
9009 template<class U> struct M {}; //#0
9012 template<>
9013 template<>
9014 struct S<int>::M<char> //#1
9016 int i;
9018 [temp.expl.spec]/4 says this is valid.
9020 In this case, when we write:
9021 S<int>::M<char> m;
9023 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9024 the one of #0.
9026 When we encounter #1, we want to store the partial instantiation
9027 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9029 For all cases other than this "explicit specialization of member of a
9030 class template", we just want to store the most general template into
9031 the CLASSTYPE_TI_TEMPLATE of M.
9033 This case of "explicit specialization of member of a class template"
9034 only happens when:
9035 1/ the enclosing class is an instantiation of, and therefore not
9036 the same as, the context of the most general template, and
9037 2/ we aren't looking at the partial instantiation itself, i.e.
9038 the innermost arguments are not the same as the innermost parms of
9039 the most general template.
9041 So it's only when 1/ and 2/ happens that we want to use the partial
9042 instantiation of the member template in lieu of its most general
9043 template. */
9045 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9046 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9047 /* the enclosing class must be an instantiation... */
9048 && CLASS_TYPE_P (context)
9049 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9051 TREE_VEC_LENGTH (arglist)--;
9052 ++processing_template_decl;
9053 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9054 tree partial_inst_args =
9055 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9056 arglist, complain, NULL_TREE);
9057 --processing_template_decl;
9058 TREE_VEC_LENGTH (arglist)++;
9059 if (partial_inst_args == error_mark_node)
9060 return error_mark_node;
9061 use_partial_inst_tmpl =
9062 /*...and we must not be looking at the partial instantiation
9063 itself. */
9064 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9065 partial_inst_args);
9068 if (!use_partial_inst_tmpl)
9069 /* This case is easy; there are no member templates involved. */
9070 found = gen_tmpl;
9071 else
9073 /* This is a full instantiation of a member template. Find
9074 the partial instantiation of which this is an instance. */
9076 /* Temporarily reduce by one the number of levels in the ARGLIST
9077 so as to avoid comparing the last set of arguments. */
9078 TREE_VEC_LENGTH (arglist)--;
9079 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9080 TREE_VEC_LENGTH (arglist)++;
9081 /* FOUND is either a proper class type, or an alias
9082 template specialization. In the later case, it's a
9083 TYPE_DECL, resulting from the substituting of arguments
9084 for parameters in the TYPE_DECL of the alias template
9085 done earlier. So be careful while getting the template
9086 of FOUND. */
9087 found = (TREE_CODE (found) == TEMPLATE_DECL
9088 ? found
9089 : (TREE_CODE (found) == TYPE_DECL
9090 ? DECL_TI_TEMPLATE (found)
9091 : CLASSTYPE_TI_TEMPLATE (found)));
9094 // Build template info for the new specialization.
9095 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9097 elt.spec = t;
9098 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9099 entry = ggc_alloc<spec_entry> ();
9100 *entry = elt;
9101 *slot = entry;
9103 /* Note this use of the partial instantiation so we can check it
9104 later in maybe_process_partial_specialization. */
9105 DECL_TEMPLATE_INSTANTIATIONS (found)
9106 = tree_cons (arglist, t,
9107 DECL_TEMPLATE_INSTANTIATIONS (found));
9109 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9110 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9111 /* Now that the type has been registered on the instantiations
9112 list, we set up the enumerators. Because the enumeration
9113 constants may involve the enumeration type itself, we make
9114 sure to register the type first, and then create the
9115 constants. That way, doing tsubst_expr for the enumeration
9116 constants won't result in recursive calls here; we'll find
9117 the instantiation and exit above. */
9118 tsubst_enum (template_type, t, arglist);
9120 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9121 /* If the type makes use of template parameters, the
9122 code that generates debugging information will crash. */
9123 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9125 /* Possibly limit visibility based on template args. */
9126 TREE_PUBLIC (type_decl) = 1;
9127 determine_visibility (type_decl);
9129 inherit_targ_abi_tags (t);
9131 return t;
9135 /* Wrapper for lookup_template_class_1. */
9137 tree
9138 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9139 int entering_scope, tsubst_flags_t complain)
9141 tree ret;
9142 timevar_push (TV_TEMPLATE_INST);
9143 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9144 entering_scope, complain);
9145 timevar_pop (TV_TEMPLATE_INST);
9146 return ret;
9149 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9151 tree
9152 lookup_template_variable (tree templ, tree arglist)
9154 /* The type of the expression is NULL_TREE since the template-id could refer
9155 to an explicit or partial specialization. */
9156 tree type = NULL_TREE;
9157 if (flag_concepts && variable_concept_p (templ))
9158 /* Except that concepts are always bool. */
9159 type = boolean_type_node;
9160 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9163 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9165 tree
9166 finish_template_variable (tree var, tsubst_flags_t complain)
9168 tree templ = TREE_OPERAND (var, 0);
9169 tree arglist = TREE_OPERAND (var, 1);
9171 /* We never want to return a VAR_DECL for a variable concept, since they
9172 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9173 bool concept_p = flag_concepts && variable_concept_p (templ);
9174 if (concept_p && processing_template_decl)
9175 return var;
9177 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9178 arglist = add_outermost_template_args (tmpl_args, arglist);
9180 templ = most_general_template (templ);
9181 tree parms = DECL_TEMPLATE_PARMS (templ);
9182 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9183 /*req_all*/true,
9184 /*use_default*/true);
9186 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9188 if (complain & tf_error)
9190 error ("use of invalid variable template %qE", var);
9191 diagnose_constraints (location_of (var), templ, arglist);
9193 return error_mark_node;
9196 /* If a template-id refers to a specialization of a variable
9197 concept, then the expression is true if and only if the
9198 concept's constraints are satisfied by the given template
9199 arguments.
9201 NOTE: This is an extension of Concepts Lite TS that
9202 allows constraints to be used in expressions. */
9203 if (concept_p)
9205 tree decl = DECL_TEMPLATE_RESULT (templ);
9206 return evaluate_variable_concept (decl, arglist);
9209 return instantiate_template (templ, arglist, complain);
9212 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9213 TARGS template args, and instantiate it if it's not dependent. */
9215 tree
9216 lookup_and_finish_template_variable (tree templ, tree targs,
9217 tsubst_flags_t complain)
9219 templ = lookup_template_variable (templ, targs);
9220 if (!any_dependent_template_arguments_p (targs))
9222 templ = finish_template_variable (templ, complain);
9223 mark_used (templ);
9226 return convert_from_reference (templ);
9230 struct pair_fn_data
9232 tree_fn_t fn;
9233 tree_fn_t any_fn;
9234 void *data;
9235 /* True when we should also visit template parameters that occur in
9236 non-deduced contexts. */
9237 bool include_nondeduced_p;
9238 hash_set<tree> *visited;
9241 /* Called from for_each_template_parm via walk_tree. */
9243 static tree
9244 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9246 tree t = *tp;
9247 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9248 tree_fn_t fn = pfd->fn;
9249 void *data = pfd->data;
9250 tree result = NULL_TREE;
9252 #define WALK_SUBTREE(NODE) \
9253 do \
9255 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9256 pfd->include_nondeduced_p, \
9257 pfd->any_fn); \
9258 if (result) goto out; \
9260 while (0)
9262 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9263 return t;
9265 if (TYPE_P (t)
9266 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9267 WALK_SUBTREE (TYPE_CONTEXT (t));
9269 switch (TREE_CODE (t))
9271 case RECORD_TYPE:
9272 if (TYPE_PTRMEMFUNC_P (t))
9273 break;
9274 /* Fall through. */
9276 case UNION_TYPE:
9277 case ENUMERAL_TYPE:
9278 if (!TYPE_TEMPLATE_INFO (t))
9279 *walk_subtrees = 0;
9280 else
9281 WALK_SUBTREE (TYPE_TI_ARGS (t));
9282 break;
9284 case INTEGER_TYPE:
9285 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9286 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9287 break;
9289 case METHOD_TYPE:
9290 /* Since we're not going to walk subtrees, we have to do this
9291 explicitly here. */
9292 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9293 /* Fall through. */
9295 case FUNCTION_TYPE:
9296 /* Check the return type. */
9297 WALK_SUBTREE (TREE_TYPE (t));
9299 /* Check the parameter types. Since default arguments are not
9300 instantiated until they are needed, the TYPE_ARG_TYPES may
9301 contain expressions that involve template parameters. But,
9302 no-one should be looking at them yet. And, once they're
9303 instantiated, they don't contain template parameters, so
9304 there's no point in looking at them then, either. */
9306 tree parm;
9308 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9309 WALK_SUBTREE (TREE_VALUE (parm));
9311 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9312 want walk_tree walking into them itself. */
9313 *walk_subtrees = 0;
9316 if (flag_noexcept_type)
9318 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9319 if (spec)
9320 WALK_SUBTREE (TREE_PURPOSE (spec));
9322 break;
9324 case TYPEOF_TYPE:
9325 case UNDERLYING_TYPE:
9326 if (pfd->include_nondeduced_p
9327 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9328 pfd->visited,
9329 pfd->include_nondeduced_p,
9330 pfd->any_fn))
9331 return error_mark_node;
9332 break;
9334 case FUNCTION_DECL:
9335 case VAR_DECL:
9336 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9337 WALK_SUBTREE (DECL_TI_ARGS (t));
9338 /* Fall through. */
9340 case PARM_DECL:
9341 case CONST_DECL:
9342 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9343 WALK_SUBTREE (DECL_INITIAL (t));
9344 if (DECL_CONTEXT (t)
9345 && pfd->include_nondeduced_p)
9346 WALK_SUBTREE (DECL_CONTEXT (t));
9347 break;
9349 case BOUND_TEMPLATE_TEMPLATE_PARM:
9350 /* Record template parameters such as `T' inside `TT<T>'. */
9351 WALK_SUBTREE (TYPE_TI_ARGS (t));
9352 /* Fall through. */
9354 case TEMPLATE_TEMPLATE_PARM:
9355 case TEMPLATE_TYPE_PARM:
9356 case TEMPLATE_PARM_INDEX:
9357 if (fn && (*fn)(t, data))
9358 return t;
9359 else if (!fn)
9360 return t;
9361 break;
9363 case TEMPLATE_DECL:
9364 /* A template template parameter is encountered. */
9365 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9366 WALK_SUBTREE (TREE_TYPE (t));
9368 /* Already substituted template template parameter */
9369 *walk_subtrees = 0;
9370 break;
9372 case TYPENAME_TYPE:
9373 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9374 partial instantiation. */
9375 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9376 break;
9378 case CONSTRUCTOR:
9379 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9380 && pfd->include_nondeduced_p)
9381 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9382 break;
9384 case INDIRECT_REF:
9385 case COMPONENT_REF:
9386 /* If there's no type, then this thing must be some expression
9387 involving template parameters. */
9388 if (!fn && !TREE_TYPE (t))
9389 return error_mark_node;
9390 break;
9392 case MODOP_EXPR:
9393 case CAST_EXPR:
9394 case IMPLICIT_CONV_EXPR:
9395 case REINTERPRET_CAST_EXPR:
9396 case CONST_CAST_EXPR:
9397 case STATIC_CAST_EXPR:
9398 case DYNAMIC_CAST_EXPR:
9399 case ARROW_EXPR:
9400 case DOTSTAR_EXPR:
9401 case TYPEID_EXPR:
9402 case PSEUDO_DTOR_EXPR:
9403 if (!fn)
9404 return error_mark_node;
9405 break;
9407 default:
9408 break;
9411 #undef WALK_SUBTREE
9413 /* We didn't find any template parameters we liked. */
9414 out:
9415 return result;
9418 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9419 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9420 call FN with the parameter and the DATA.
9421 If FN returns nonzero, the iteration is terminated, and
9422 for_each_template_parm returns 1. Otherwise, the iteration
9423 continues. If FN never returns a nonzero value, the value
9424 returned by for_each_template_parm is 0. If FN is NULL, it is
9425 considered to be the function which always returns 1.
9427 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9428 parameters that occur in non-deduced contexts. When false, only
9429 visits those template parameters that can be deduced. */
9431 static tree
9432 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9433 hash_set<tree> *visited,
9434 bool include_nondeduced_p,
9435 tree_fn_t any_fn)
9437 struct pair_fn_data pfd;
9438 tree result;
9440 /* Set up. */
9441 pfd.fn = fn;
9442 pfd.any_fn = any_fn;
9443 pfd.data = data;
9444 pfd.include_nondeduced_p = include_nondeduced_p;
9446 /* Walk the tree. (Conceptually, we would like to walk without
9447 duplicates, but for_each_template_parm_r recursively calls
9448 for_each_template_parm, so we would need to reorganize a fair
9449 bit to use walk_tree_without_duplicates, so we keep our own
9450 visited list.) */
9451 if (visited)
9452 pfd.visited = visited;
9453 else
9454 pfd.visited = new hash_set<tree>;
9455 result = cp_walk_tree (&t,
9456 for_each_template_parm_r,
9457 &pfd,
9458 pfd.visited);
9460 /* Clean up. */
9461 if (!visited)
9463 delete pfd.visited;
9464 pfd.visited = 0;
9467 return result;
9470 /* Returns true if T depends on any template parameter. */
9473 uses_template_parms (tree t)
9475 if (t == NULL_TREE)
9476 return false;
9478 bool dependent_p;
9479 int saved_processing_template_decl;
9481 saved_processing_template_decl = processing_template_decl;
9482 if (!saved_processing_template_decl)
9483 processing_template_decl = 1;
9484 if (TYPE_P (t))
9485 dependent_p = dependent_type_p (t);
9486 else if (TREE_CODE (t) == TREE_VEC)
9487 dependent_p = any_dependent_template_arguments_p (t);
9488 else if (TREE_CODE (t) == TREE_LIST)
9489 dependent_p = (uses_template_parms (TREE_VALUE (t))
9490 || uses_template_parms (TREE_CHAIN (t)));
9491 else if (TREE_CODE (t) == TYPE_DECL)
9492 dependent_p = dependent_type_p (TREE_TYPE (t));
9493 else if (DECL_P (t)
9494 || EXPR_P (t)
9495 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9496 || TREE_CODE (t) == OVERLOAD
9497 || BASELINK_P (t)
9498 || identifier_p (t)
9499 || TREE_CODE (t) == TRAIT_EXPR
9500 || TREE_CODE (t) == CONSTRUCTOR
9501 || CONSTANT_CLASS_P (t))
9502 dependent_p = (type_dependent_expression_p (t)
9503 || value_dependent_expression_p (t));
9504 else
9506 gcc_assert (t == error_mark_node);
9507 dependent_p = false;
9510 processing_template_decl = saved_processing_template_decl;
9512 return dependent_p;
9515 /* Returns true iff current_function_decl is an incompletely instantiated
9516 template. Useful instead of processing_template_decl because the latter
9517 is set to 0 during instantiate_non_dependent_expr. */
9519 bool
9520 in_template_function (void)
9522 tree fn = current_function_decl;
9523 bool ret;
9524 ++processing_template_decl;
9525 ret = (fn && DECL_LANG_SPECIFIC (fn)
9526 && DECL_TEMPLATE_INFO (fn)
9527 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9528 --processing_template_decl;
9529 return ret;
9532 /* Returns true if T depends on any template parameter with level LEVEL. */
9534 bool
9535 uses_template_parms_level (tree t, int level)
9537 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9538 /*include_nondeduced_p=*/true);
9541 /* Returns true if the signature of DECL depends on any template parameter from
9542 its enclosing class. */
9544 bool
9545 uses_outer_template_parms (tree decl)
9547 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9548 if (depth == 0)
9549 return false;
9550 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9551 &depth, NULL, /*include_nondeduced_p=*/true))
9552 return true;
9553 if (PRIMARY_TEMPLATE_P (decl)
9554 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9555 (DECL_TEMPLATE_PARMS (decl)),
9556 template_parm_outer_level,
9557 &depth, NULL, /*include_nondeduced_p=*/true))
9558 return true;
9559 tree ci = get_constraints (decl);
9560 if (ci)
9561 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9562 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9563 &depth, NULL, /*nondeduced*/true))
9564 return true;
9565 return false;
9568 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9569 ill-formed translation unit, i.e. a variable or function that isn't
9570 usable in a constant expression. */
9572 static inline bool
9573 neglectable_inst_p (tree d)
9575 return (DECL_P (d)
9576 && !undeduced_auto_decl (d)
9577 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9578 : decl_maybe_constant_var_p (d)));
9581 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9582 neglectable and instantiated from within an erroneous instantiation. */
9584 static bool
9585 limit_bad_template_recursion (tree decl)
9587 struct tinst_level *lev = current_tinst_level;
9588 int errs = errorcount + sorrycount;
9589 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9590 return false;
9592 for (; lev; lev = lev->next)
9593 if (neglectable_inst_p (lev->decl))
9594 break;
9596 return (lev && errs > lev->errors);
9599 static int tinst_depth;
9600 extern int max_tinst_depth;
9601 int depth_reached;
9603 static GTY(()) struct tinst_level *last_error_tinst_level;
9605 /* We're starting to instantiate D; record the template instantiation context
9606 for diagnostics and to restore it later. */
9608 bool
9609 push_tinst_level (tree d)
9611 return push_tinst_level_loc (d, input_location);
9614 /* We're starting to instantiate D; record the template instantiation context
9615 at LOC for diagnostics and to restore it later. */
9617 bool
9618 push_tinst_level_loc (tree d, location_t loc)
9620 struct tinst_level *new_level;
9622 if (tinst_depth >= max_tinst_depth)
9624 /* Tell error.c not to try to instantiate any templates. */
9625 at_eof = 2;
9626 fatal_error (input_location,
9627 "template instantiation depth exceeds maximum of %d"
9628 " (use -ftemplate-depth= to increase the maximum)",
9629 max_tinst_depth);
9630 return false;
9633 /* If the current instantiation caused problems, don't let it instantiate
9634 anything else. Do allow deduction substitution and decls usable in
9635 constant expressions. */
9636 if (limit_bad_template_recursion (d))
9637 return false;
9639 /* When not -quiet, dump template instantiations other than functions, since
9640 announce_function will take care of those. */
9641 if (!quiet_flag
9642 && TREE_CODE (d) != TREE_LIST
9643 && TREE_CODE (d) != FUNCTION_DECL)
9644 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9646 new_level = ggc_alloc<tinst_level> ();
9647 new_level->decl = d;
9648 new_level->locus = loc;
9649 new_level->errors = errorcount+sorrycount;
9650 new_level->in_system_header_p = in_system_header_at (input_location);
9651 new_level->next = current_tinst_level;
9652 current_tinst_level = new_level;
9654 ++tinst_depth;
9655 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9656 depth_reached = tinst_depth;
9658 return true;
9661 /* We're done instantiating this template; return to the instantiation
9662 context. */
9664 void
9665 pop_tinst_level (void)
9667 /* Restore the filename and line number stashed away when we started
9668 this instantiation. */
9669 input_location = current_tinst_level->locus;
9670 current_tinst_level = current_tinst_level->next;
9671 --tinst_depth;
9674 /* We're instantiating a deferred template; restore the template
9675 instantiation context in which the instantiation was requested, which
9676 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9678 static tree
9679 reopen_tinst_level (struct tinst_level *level)
9681 struct tinst_level *t;
9683 tinst_depth = 0;
9684 for (t = level; t; t = t->next)
9685 ++tinst_depth;
9687 current_tinst_level = level;
9688 pop_tinst_level ();
9689 if (current_tinst_level)
9690 current_tinst_level->errors = errorcount+sorrycount;
9691 return level->decl;
9694 /* Returns the TINST_LEVEL which gives the original instantiation
9695 context. */
9697 struct tinst_level *
9698 outermost_tinst_level (void)
9700 struct tinst_level *level = current_tinst_level;
9701 if (level)
9702 while (level->next)
9703 level = level->next;
9704 return level;
9707 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9708 vector of template arguments, as for tsubst.
9710 Returns an appropriate tsubst'd friend declaration. */
9712 static tree
9713 tsubst_friend_function (tree decl, tree args)
9715 tree new_friend;
9717 if (TREE_CODE (decl) == FUNCTION_DECL
9718 && DECL_TEMPLATE_INSTANTIATION (decl)
9719 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9720 /* This was a friend declared with an explicit template
9721 argument list, e.g.:
9723 friend void f<>(T);
9725 to indicate that f was a template instantiation, not a new
9726 function declaration. Now, we have to figure out what
9727 instantiation of what template. */
9729 tree template_id, arglist, fns;
9730 tree new_args;
9731 tree tmpl;
9732 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9734 /* Friend functions are looked up in the containing namespace scope.
9735 We must enter that scope, to avoid finding member functions of the
9736 current class with same name. */
9737 push_nested_namespace (ns);
9738 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9739 tf_warning_or_error, NULL_TREE,
9740 /*integral_constant_expression_p=*/false);
9741 pop_nested_namespace (ns);
9742 arglist = tsubst (DECL_TI_ARGS (decl), args,
9743 tf_warning_or_error, NULL_TREE);
9744 template_id = lookup_template_function (fns, arglist);
9746 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9747 tmpl = determine_specialization (template_id, new_friend,
9748 &new_args,
9749 /*need_member_template=*/0,
9750 TREE_VEC_LENGTH (args),
9751 tsk_none);
9752 return instantiate_template (tmpl, new_args, tf_error);
9755 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9757 /* The NEW_FRIEND will look like an instantiation, to the
9758 compiler, but is not an instantiation from the point of view of
9759 the language. For example, we might have had:
9761 template <class T> struct S {
9762 template <class U> friend void f(T, U);
9765 Then, in S<int>, template <class U> void f(int, U) is not an
9766 instantiation of anything. */
9767 if (new_friend == error_mark_node)
9768 return error_mark_node;
9770 DECL_USE_TEMPLATE (new_friend) = 0;
9771 if (TREE_CODE (decl) == TEMPLATE_DECL)
9773 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9774 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9775 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9778 /* The mangled name for the NEW_FRIEND is incorrect. The function
9779 is not a template instantiation and should not be mangled like
9780 one. Therefore, we forget the mangling here; we'll recompute it
9781 later if we need it. */
9782 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9784 SET_DECL_RTL (new_friend, NULL);
9785 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9788 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9790 tree old_decl;
9791 tree new_friend_template_info;
9792 tree new_friend_result_template_info;
9793 tree ns;
9794 int new_friend_is_defn;
9796 /* We must save some information from NEW_FRIEND before calling
9797 duplicate decls since that function will free NEW_FRIEND if
9798 possible. */
9799 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9800 new_friend_is_defn =
9801 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9802 (template_for_substitution (new_friend)))
9803 != NULL_TREE);
9804 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9806 /* This declaration is a `primary' template. */
9807 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9809 new_friend_result_template_info
9810 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9812 else
9813 new_friend_result_template_info = NULL_TREE;
9815 /* Inside pushdecl_namespace_level, we will push into the
9816 current namespace. However, the friend function should go
9817 into the namespace of the template. */
9818 ns = decl_namespace_context (new_friend);
9819 push_nested_namespace (ns);
9820 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9821 pop_nested_namespace (ns);
9823 if (old_decl == error_mark_node)
9824 return error_mark_node;
9826 if (old_decl != new_friend)
9828 /* This new friend declaration matched an existing
9829 declaration. For example, given:
9831 template <class T> void f(T);
9832 template <class U> class C {
9833 template <class T> friend void f(T) {}
9836 the friend declaration actually provides the definition
9837 of `f', once C has been instantiated for some type. So,
9838 old_decl will be the out-of-class template declaration,
9839 while new_friend is the in-class definition.
9841 But, if `f' was called before this point, the
9842 instantiation of `f' will have DECL_TI_ARGS corresponding
9843 to `T' but not to `U', references to which might appear
9844 in the definition of `f'. Previously, the most general
9845 template for an instantiation of `f' was the out-of-class
9846 version; now it is the in-class version. Therefore, we
9847 run through all specialization of `f', adding to their
9848 DECL_TI_ARGS appropriately. In particular, they need a
9849 new set of outer arguments, corresponding to the
9850 arguments for this class instantiation.
9852 The same situation can arise with something like this:
9854 friend void f(int);
9855 template <class T> class C {
9856 friend void f(T) {}
9859 when `C<int>' is instantiated. Now, `f(int)' is defined
9860 in the class. */
9862 if (!new_friend_is_defn)
9863 /* On the other hand, if the in-class declaration does
9864 *not* provide a definition, then we don't want to alter
9865 existing definitions. We can just leave everything
9866 alone. */
9868 else
9870 tree new_template = TI_TEMPLATE (new_friend_template_info);
9871 tree new_args = TI_ARGS (new_friend_template_info);
9873 /* Overwrite whatever template info was there before, if
9874 any, with the new template information pertaining to
9875 the declaration. */
9876 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9878 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9880 /* We should have called reregister_specialization in
9881 duplicate_decls. */
9882 gcc_assert (retrieve_specialization (new_template,
9883 new_args, 0)
9884 == old_decl);
9886 /* Instantiate it if the global has already been used. */
9887 if (DECL_ODR_USED (old_decl))
9888 instantiate_decl (old_decl, /*defer_ok=*/true,
9889 /*expl_inst_class_mem_p=*/false);
9891 else
9893 tree t;
9895 /* Indicate that the old function template is a partial
9896 instantiation. */
9897 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9898 = new_friend_result_template_info;
9900 gcc_assert (new_template
9901 == most_general_template (new_template));
9902 gcc_assert (new_template != old_decl);
9904 /* Reassign any specializations already in the hash table
9905 to the new more general template, and add the
9906 additional template args. */
9907 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9908 t != NULL_TREE;
9909 t = TREE_CHAIN (t))
9911 tree spec = TREE_VALUE (t);
9912 spec_entry elt;
9914 elt.tmpl = old_decl;
9915 elt.args = DECL_TI_ARGS (spec);
9916 elt.spec = NULL_TREE;
9918 decl_specializations->remove_elt (&elt);
9920 DECL_TI_ARGS (spec)
9921 = add_outermost_template_args (new_args,
9922 DECL_TI_ARGS (spec));
9924 register_specialization
9925 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9928 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9932 /* The information from NEW_FRIEND has been merged into OLD_DECL
9933 by duplicate_decls. */
9934 new_friend = old_decl;
9937 else
9939 tree context = DECL_CONTEXT (new_friend);
9940 bool dependent_p;
9942 /* In the code
9943 template <class T> class C {
9944 template <class U> friend void C1<U>::f (); // case 1
9945 friend void C2<T>::f (); // case 2
9947 we only need to make sure CONTEXT is a complete type for
9948 case 2. To distinguish between the two cases, we note that
9949 CONTEXT of case 1 remains dependent type after tsubst while
9950 this isn't true for case 2. */
9951 ++processing_template_decl;
9952 dependent_p = dependent_type_p (context);
9953 --processing_template_decl;
9955 if (!dependent_p
9956 && !complete_type_or_else (context, NULL_TREE))
9957 return error_mark_node;
9959 if (COMPLETE_TYPE_P (context))
9961 tree fn = new_friend;
9962 /* do_friend adds the TEMPLATE_DECL for any member friend
9963 template even if it isn't a member template, i.e.
9964 template <class T> friend A<T>::f();
9965 Look through it in that case. */
9966 if (TREE_CODE (fn) == TEMPLATE_DECL
9967 && !PRIMARY_TEMPLATE_P (fn))
9968 fn = DECL_TEMPLATE_RESULT (fn);
9969 /* Check to see that the declaration is really present, and,
9970 possibly obtain an improved declaration. */
9971 fn = check_classfn (context, fn, NULL_TREE);
9973 if (fn)
9974 new_friend = fn;
9978 return new_friend;
9981 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9982 template arguments, as for tsubst.
9984 Returns an appropriate tsubst'd friend type or error_mark_node on
9985 failure. */
9987 static tree
9988 tsubst_friend_class (tree friend_tmpl, tree args)
9990 tree friend_type;
9991 tree tmpl;
9992 tree context;
9994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9996 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9997 return TREE_TYPE (t);
10000 context = CP_DECL_CONTEXT (friend_tmpl);
10002 if (context != global_namespace)
10004 if (TREE_CODE (context) == NAMESPACE_DECL)
10005 push_nested_namespace (context);
10006 else
10007 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10010 /* Look for a class template declaration. We look for hidden names
10011 because two friend declarations of the same template are the
10012 same. For example, in:
10014 struct A {
10015 template <typename> friend class F;
10017 template <typename> struct B {
10018 template <typename> friend class F;
10021 both F templates are the same. */
10022 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10023 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10025 /* But, if we don't find one, it might be because we're in a
10026 situation like this:
10028 template <class T>
10029 struct S {
10030 template <class U>
10031 friend struct S;
10034 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10035 for `S<int>', not the TEMPLATE_DECL. */
10036 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10038 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10039 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10042 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10044 /* The friend template has already been declared. Just
10045 check to see that the declarations match, and install any new
10046 default parameters. We must tsubst the default parameters,
10047 of course. We only need the innermost template parameters
10048 because that is all that redeclare_class_template will look
10049 at. */
10050 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10051 > TMPL_ARGS_DEPTH (args))
10053 tree parms;
10054 location_t saved_input_location;
10055 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10056 args, tf_warning_or_error);
10058 saved_input_location = input_location;
10059 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10060 tree cons = get_constraints (tmpl);
10061 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10062 input_location = saved_input_location;
10066 friend_type = TREE_TYPE (tmpl);
10068 else
10070 /* The friend template has not already been declared. In this
10071 case, the instantiation of the template class will cause the
10072 injection of this template into the global scope. */
10073 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10074 if (tmpl == error_mark_node)
10075 return error_mark_node;
10077 /* The new TMPL is not an instantiation of anything, so we
10078 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10079 the new type because that is supposed to be the corresponding
10080 template decl, i.e., TMPL. */
10081 DECL_USE_TEMPLATE (tmpl) = 0;
10082 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10083 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10084 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10085 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10087 /* Inject this template into the global scope. */
10088 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10091 if (context != global_namespace)
10093 if (TREE_CODE (context) == NAMESPACE_DECL)
10094 pop_nested_namespace (context);
10095 else
10096 pop_nested_class ();
10099 return friend_type;
10102 /* Returns zero if TYPE cannot be completed later due to circularity.
10103 Otherwise returns one. */
10105 static int
10106 can_complete_type_without_circularity (tree type)
10108 if (type == NULL_TREE || type == error_mark_node)
10109 return 0;
10110 else if (COMPLETE_TYPE_P (type))
10111 return 1;
10112 else if (TREE_CODE (type) == ARRAY_TYPE)
10113 return can_complete_type_without_circularity (TREE_TYPE (type));
10114 else if (CLASS_TYPE_P (type)
10115 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10116 return 0;
10117 else
10118 return 1;
10121 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10122 tsubst_flags_t, tree);
10124 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10125 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10127 static tree
10128 tsubst_attribute (tree t, tree *decl_p, tree args,
10129 tsubst_flags_t complain, tree in_decl)
10131 gcc_assert (ATTR_IS_DEPENDENT (t));
10133 tree val = TREE_VALUE (t);
10134 if (val == NULL_TREE)
10135 /* Nothing to do. */;
10136 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10137 && is_attribute_p ("omp declare simd",
10138 get_attribute_name (t)))
10140 tree clauses = TREE_VALUE (val);
10141 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10142 complain, in_decl);
10143 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10144 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10145 tree parms = DECL_ARGUMENTS (*decl_p);
10146 clauses
10147 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10148 if (clauses)
10149 val = build_tree_list (NULL_TREE, clauses);
10150 else
10151 val = NULL_TREE;
10153 /* If the first attribute argument is an identifier, don't
10154 pass it through tsubst. Attributes like mode, format,
10155 cleanup and several target specific attributes expect it
10156 unmodified. */
10157 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10159 tree chain
10160 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10161 /*integral_constant_expression_p=*/false);
10162 if (chain != TREE_CHAIN (val))
10163 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10165 else if (PACK_EXPANSION_P (val))
10167 /* An attribute pack expansion. */
10168 tree purp = TREE_PURPOSE (t);
10169 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10170 if (pack == error_mark_node)
10171 return error_mark_node;
10172 int len = TREE_VEC_LENGTH (pack);
10173 tree list = NULL_TREE;
10174 tree *q = &list;
10175 for (int i = 0; i < len; ++i)
10177 tree elt = TREE_VEC_ELT (pack, i);
10178 *q = build_tree_list (purp, elt);
10179 q = &TREE_CHAIN (*q);
10181 return list;
10183 else
10184 val = tsubst_expr (val, args, complain, in_decl,
10185 /*integral_constant_expression_p=*/false);
10187 if (val != TREE_VALUE (t))
10188 return build_tree_list (TREE_PURPOSE (t), val);
10189 return t;
10192 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10193 unchanged or a new TREE_LIST chain. */
10195 static tree
10196 tsubst_attributes (tree attributes, tree args,
10197 tsubst_flags_t complain, tree in_decl)
10199 tree last_dep = NULL_TREE;
10201 for (tree t = attributes; t; t = TREE_CHAIN (t))
10202 if (ATTR_IS_DEPENDENT (t))
10204 last_dep = t;
10205 attributes = copy_list (attributes);
10206 break;
10209 if (last_dep)
10210 for (tree *p = &attributes; *p; )
10212 tree t = *p;
10213 if (ATTR_IS_DEPENDENT (t))
10215 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10216 if (subst != t)
10218 *p = subst;
10220 p = &TREE_CHAIN (*p);
10221 while (*p);
10222 *p = TREE_CHAIN (t);
10223 continue;
10226 p = &TREE_CHAIN (*p);
10229 return attributes;
10232 /* Apply any attributes which had to be deferred until instantiation
10233 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10234 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10236 static void
10237 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10238 tree args, tsubst_flags_t complain, tree in_decl)
10240 tree last_dep = NULL_TREE;
10241 tree t;
10242 tree *p;
10244 if (attributes == NULL_TREE)
10245 return;
10247 if (DECL_P (*decl_p))
10249 if (TREE_TYPE (*decl_p) == error_mark_node)
10250 return;
10251 p = &DECL_ATTRIBUTES (*decl_p);
10252 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10253 to our attributes parameter. */
10254 gcc_assert (*p == attributes);
10256 else
10258 p = &TYPE_ATTRIBUTES (*decl_p);
10259 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10260 lookup_template_class_1, and should be preserved. */
10261 gcc_assert (*p != attributes);
10262 while (*p)
10263 p = &TREE_CHAIN (*p);
10266 for (t = attributes; t; t = TREE_CHAIN (t))
10267 if (ATTR_IS_DEPENDENT (t))
10269 last_dep = t;
10270 attributes = copy_list (attributes);
10271 break;
10274 *p = attributes;
10275 if (last_dep)
10277 tree late_attrs = NULL_TREE;
10278 tree *q = &late_attrs;
10280 for (; *p; )
10282 t = *p;
10283 if (ATTR_IS_DEPENDENT (t))
10285 *p = TREE_CHAIN (t);
10286 TREE_CHAIN (t) = NULL_TREE;
10287 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10289 q = &TREE_CHAIN (*q);
10290 while (*q);
10292 else
10293 p = &TREE_CHAIN (t);
10296 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10300 /* Perform (or defer) access check for typedefs that were referenced
10301 from within the template TMPL code.
10302 This is a subroutine of instantiate_decl and instantiate_class_template.
10303 TMPL is the template to consider and TARGS is the list of arguments of
10304 that template. */
10306 static void
10307 perform_typedefs_access_check (tree tmpl, tree targs)
10309 location_t saved_location;
10310 unsigned i;
10311 qualified_typedef_usage_t *iter;
10313 if (!tmpl
10314 || (!CLASS_TYPE_P (tmpl)
10315 && TREE_CODE (tmpl) != FUNCTION_DECL))
10316 return;
10318 saved_location = input_location;
10319 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10321 tree type_decl = iter->typedef_decl;
10322 tree type_scope = iter->context;
10324 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10325 continue;
10327 if (uses_template_parms (type_decl))
10328 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10329 if (uses_template_parms (type_scope))
10330 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10332 /* Make access check error messages point to the location
10333 of the use of the typedef. */
10334 input_location = iter->locus;
10335 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10336 type_decl, type_decl,
10337 tf_warning_or_error);
10339 input_location = saved_location;
10342 static tree
10343 instantiate_class_template_1 (tree type)
10345 tree templ, args, pattern, t, member;
10346 tree typedecl;
10347 tree pbinfo;
10348 tree base_list;
10349 unsigned int saved_maximum_field_alignment;
10350 tree fn_context;
10352 if (type == error_mark_node)
10353 return error_mark_node;
10355 if (COMPLETE_OR_OPEN_TYPE_P (type)
10356 || uses_template_parms (type))
10357 return type;
10359 /* Figure out which template is being instantiated. */
10360 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10361 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10363 /* Determine what specialization of the original template to
10364 instantiate. */
10365 t = most_specialized_partial_spec (type, tf_warning_or_error);
10366 if (t == error_mark_node)
10368 TYPE_BEING_DEFINED (type) = 1;
10369 return error_mark_node;
10371 else if (t)
10373 /* This TYPE is actually an instantiation of a partial
10374 specialization. We replace the innermost set of ARGS with
10375 the arguments appropriate for substitution. For example,
10376 given:
10378 template <class T> struct S {};
10379 template <class T> struct S<T*> {};
10381 and supposing that we are instantiating S<int*>, ARGS will
10382 presently be {int*} -- but we need {int}. */
10383 pattern = TREE_TYPE (t);
10384 args = TREE_PURPOSE (t);
10386 else
10388 pattern = TREE_TYPE (templ);
10389 args = CLASSTYPE_TI_ARGS (type);
10392 /* If the template we're instantiating is incomplete, then clearly
10393 there's nothing we can do. */
10394 if (!COMPLETE_TYPE_P (pattern))
10395 return type;
10397 /* If we've recursively instantiated too many templates, stop. */
10398 if (! push_tinst_level (type))
10399 return type;
10401 /* Now we're really doing the instantiation. Mark the type as in
10402 the process of being defined. */
10403 TYPE_BEING_DEFINED (type) = 1;
10405 /* We may be in the middle of deferred access check. Disable
10406 it now. */
10407 push_deferring_access_checks (dk_no_deferred);
10409 int saved_unevaluated_operand = cp_unevaluated_operand;
10410 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10412 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10413 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10414 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10415 fn_context = error_mark_node;
10416 if (!fn_context)
10417 push_to_top_level ();
10418 else
10420 cp_unevaluated_operand = 0;
10421 c_inhibit_evaluation_warnings = 0;
10423 /* Use #pragma pack from the template context. */
10424 saved_maximum_field_alignment = maximum_field_alignment;
10425 maximum_field_alignment = TYPE_PRECISION (pattern);
10427 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10429 /* Set the input location to the most specialized template definition.
10430 This is needed if tsubsting causes an error. */
10431 typedecl = TYPE_MAIN_DECL (pattern);
10432 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10433 DECL_SOURCE_LOCATION (typedecl);
10435 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10436 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10437 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10438 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10439 if (ANON_AGGR_TYPE_P (pattern))
10440 SET_ANON_AGGR_TYPE_P (type);
10441 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10443 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10444 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10445 /* Adjust visibility for template arguments. */
10446 determine_visibility (TYPE_MAIN_DECL (type));
10448 if (CLASS_TYPE_P (type))
10449 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10451 pbinfo = TYPE_BINFO (pattern);
10453 /* We should never instantiate a nested class before its enclosing
10454 class; we need to look up the nested class by name before we can
10455 instantiate it, and that lookup should instantiate the enclosing
10456 class. */
10457 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10458 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10460 base_list = NULL_TREE;
10461 if (BINFO_N_BASE_BINFOS (pbinfo))
10463 tree pbase_binfo;
10464 tree pushed_scope;
10465 int i;
10467 /* We must enter the scope containing the type, as that is where
10468 the accessibility of types named in dependent bases are
10469 looked up from. */
10470 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10472 /* Substitute into each of the bases to determine the actual
10473 basetypes. */
10474 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10476 tree base;
10477 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10478 tree expanded_bases = NULL_TREE;
10479 int idx, len = 1;
10481 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10483 expanded_bases =
10484 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10485 args, tf_error, NULL_TREE);
10486 if (expanded_bases == error_mark_node)
10487 continue;
10489 len = TREE_VEC_LENGTH (expanded_bases);
10492 for (idx = 0; idx < len; idx++)
10494 if (expanded_bases)
10495 /* Extract the already-expanded base class. */
10496 base = TREE_VEC_ELT (expanded_bases, idx);
10497 else
10498 /* Substitute to figure out the base class. */
10499 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10500 NULL_TREE);
10502 if (base == error_mark_node)
10503 continue;
10505 base_list = tree_cons (access, base, base_list);
10506 if (BINFO_VIRTUAL_P (pbase_binfo))
10507 TREE_TYPE (base_list) = integer_type_node;
10511 /* The list is now in reverse order; correct that. */
10512 base_list = nreverse (base_list);
10514 if (pushed_scope)
10515 pop_scope (pushed_scope);
10517 /* Now call xref_basetypes to set up all the base-class
10518 information. */
10519 xref_basetypes (type, base_list);
10521 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10522 (int) ATTR_FLAG_TYPE_IN_PLACE,
10523 args, tf_error, NULL_TREE);
10524 fixup_attribute_variants (type);
10526 /* Now that our base classes are set up, enter the scope of the
10527 class, so that name lookups into base classes, etc. will work
10528 correctly. This is precisely analogous to what we do in
10529 begin_class_definition when defining an ordinary non-template
10530 class, except we also need to push the enclosing classes. */
10531 push_nested_class (type);
10533 /* Now members are processed in the order of declaration. */
10534 for (member = CLASSTYPE_DECL_LIST (pattern);
10535 member; member = TREE_CHAIN (member))
10537 tree t = TREE_VALUE (member);
10539 if (TREE_PURPOSE (member))
10541 if (TYPE_P (t))
10543 /* Build new CLASSTYPE_NESTED_UTDS. */
10545 tree newtag;
10546 bool class_template_p;
10548 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10549 && TYPE_LANG_SPECIFIC (t)
10550 && CLASSTYPE_IS_TEMPLATE (t));
10551 /* If the member is a class template, then -- even after
10552 substitution -- there may be dependent types in the
10553 template argument list for the class. We increment
10554 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10555 that function will assume that no types are dependent
10556 when outside of a template. */
10557 if (class_template_p)
10558 ++processing_template_decl;
10559 newtag = tsubst (t, args, tf_error, NULL_TREE);
10560 if (class_template_p)
10561 --processing_template_decl;
10562 if (newtag == error_mark_node)
10563 continue;
10565 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10567 tree name = TYPE_IDENTIFIER (t);
10569 if (class_template_p)
10570 /* Unfortunately, lookup_template_class sets
10571 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10572 instantiation (i.e., for the type of a member
10573 template class nested within a template class.)
10574 This behavior is required for
10575 maybe_process_partial_specialization to work
10576 correctly, but is not accurate in this case;
10577 the TAG is not an instantiation of anything.
10578 (The corresponding TEMPLATE_DECL is an
10579 instantiation, but the TYPE is not.) */
10580 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10582 /* Now, we call pushtag to put this NEWTAG into the scope of
10583 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10584 pushtag calling push_template_decl. We don't have to do
10585 this for enums because it will already have been done in
10586 tsubst_enum. */
10587 if (name)
10588 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10589 pushtag (name, newtag, /*tag_scope=*/ts_current);
10592 else if (DECL_DECLARES_FUNCTION_P (t))
10594 tree r;
10596 if (TREE_CODE (t) == TEMPLATE_DECL)
10597 ++processing_template_decl;
10598 r = tsubst (t, args, tf_error, NULL_TREE);
10599 if (TREE_CODE (t) == TEMPLATE_DECL)
10600 --processing_template_decl;
10601 set_current_access_from_decl (r);
10602 finish_member_declaration (r);
10603 /* Instantiate members marked with attribute used. */
10604 if (r != error_mark_node && DECL_PRESERVE_P (r))
10605 mark_used (r);
10606 if (TREE_CODE (r) == FUNCTION_DECL
10607 && DECL_OMP_DECLARE_REDUCTION_P (r))
10608 cp_check_omp_declare_reduction (r);
10610 else if (DECL_CLASS_TEMPLATE_P (t)
10611 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10612 /* A closure type for a lambda in a default argument for a
10613 member template. Ignore it; it will be instantiated with
10614 the default argument. */;
10615 else
10617 /* Build new TYPE_FIELDS. */
10618 if (TREE_CODE (t) == STATIC_ASSERT)
10620 tree condition;
10622 ++c_inhibit_evaluation_warnings;
10623 condition =
10624 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10625 tf_warning_or_error, NULL_TREE,
10626 /*integral_constant_expression_p=*/true);
10627 --c_inhibit_evaluation_warnings;
10629 finish_static_assert (condition,
10630 STATIC_ASSERT_MESSAGE (t),
10631 STATIC_ASSERT_SOURCE_LOCATION (t),
10632 /*member_p=*/true);
10634 else if (TREE_CODE (t) != CONST_DECL)
10636 tree r;
10637 tree vec = NULL_TREE;
10638 int len = 1;
10640 /* The file and line for this declaration, to
10641 assist in error message reporting. Since we
10642 called push_tinst_level above, we don't need to
10643 restore these. */
10644 input_location = DECL_SOURCE_LOCATION (t);
10646 if (TREE_CODE (t) == TEMPLATE_DECL)
10647 ++processing_template_decl;
10648 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10649 if (TREE_CODE (t) == TEMPLATE_DECL)
10650 --processing_template_decl;
10652 if (TREE_CODE (r) == TREE_VEC)
10654 /* A capture pack became multiple fields. */
10655 vec = r;
10656 len = TREE_VEC_LENGTH (vec);
10659 for (int i = 0; i < len; ++i)
10661 if (vec)
10662 r = TREE_VEC_ELT (vec, i);
10663 if (VAR_P (r))
10665 /* In [temp.inst]:
10667 [t]he initialization (and any associated
10668 side-effects) of a static data member does
10669 not occur unless the static data member is
10670 itself used in a way that requires the
10671 definition of the static data member to
10672 exist.
10674 Therefore, we do not substitute into the
10675 initialized for the static data member here. */
10676 finish_static_data_member_decl
10678 /*init=*/NULL_TREE,
10679 /*init_const_expr_p=*/false,
10680 /*asmspec_tree=*/NULL_TREE,
10681 /*flags=*/0);
10682 /* Instantiate members marked with attribute used. */
10683 if (r != error_mark_node && DECL_PRESERVE_P (r))
10684 mark_used (r);
10686 else if (TREE_CODE (r) == FIELD_DECL)
10688 /* Determine whether R has a valid type and can be
10689 completed later. If R is invalid, then its type
10690 is replaced by error_mark_node. */
10691 tree rtype = TREE_TYPE (r);
10692 if (can_complete_type_without_circularity (rtype))
10693 complete_type (rtype);
10695 if (!complete_or_array_type_p (rtype))
10697 /* If R's type couldn't be completed and
10698 it isn't a flexible array member (whose
10699 type is incomplete by definition) give
10700 an error. */
10701 cxx_incomplete_type_error (r, rtype);
10702 TREE_TYPE (r) = error_mark_node;
10706 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10707 such a thing will already have been added to the field
10708 list by tsubst_enum in finish_member_declaration in the
10709 CLASSTYPE_NESTED_UTDS case above. */
10710 if (!(TREE_CODE (r) == TYPE_DECL
10711 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10712 && DECL_ARTIFICIAL (r)))
10714 set_current_access_from_decl (r);
10715 finish_member_declaration (r);
10721 else
10723 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10724 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10726 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10728 tree friend_type = t;
10729 bool adjust_processing_template_decl = false;
10731 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10733 /* template <class T> friend class C; */
10734 friend_type = tsubst_friend_class (friend_type, args);
10735 adjust_processing_template_decl = true;
10737 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10739 /* template <class T> friend class C::D; */
10740 friend_type = tsubst (friend_type, args,
10741 tf_warning_or_error, NULL_TREE);
10742 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10743 friend_type = TREE_TYPE (friend_type);
10744 adjust_processing_template_decl = true;
10746 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10747 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10749 /* This could be either
10751 friend class T::C;
10753 when dependent_type_p is false or
10755 template <class U> friend class T::C;
10757 otherwise. */
10758 /* Bump processing_template_decl in case this is something like
10759 template <class T> friend struct A<T>::B. */
10760 ++processing_template_decl;
10761 friend_type = tsubst (friend_type, args,
10762 tf_warning_or_error, NULL_TREE);
10763 if (dependent_type_p (friend_type))
10764 adjust_processing_template_decl = true;
10765 --processing_template_decl;
10767 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10768 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10769 && TYPE_HIDDEN_P (friend_type))
10771 /* friend class C;
10773 where C hasn't been declared yet. Let's lookup name
10774 from namespace scope directly, bypassing any name that
10775 come from dependent base class. */
10776 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10778 /* The call to xref_tag_from_type does injection for friend
10779 classes. */
10780 push_nested_namespace (ns);
10781 friend_type =
10782 xref_tag_from_type (friend_type, NULL_TREE,
10783 /*tag_scope=*/ts_current);
10784 pop_nested_namespace (ns);
10786 else if (uses_template_parms (friend_type))
10787 /* friend class C<T>; */
10788 friend_type = tsubst (friend_type, args,
10789 tf_warning_or_error, NULL_TREE);
10790 /* Otherwise it's
10792 friend class C;
10794 where C is already declared or
10796 friend class C<int>;
10798 We don't have to do anything in these cases. */
10800 if (adjust_processing_template_decl)
10801 /* Trick make_friend_class into realizing that the friend
10802 we're adding is a template, not an ordinary class. It's
10803 important that we use make_friend_class since it will
10804 perform some error-checking and output cross-reference
10805 information. */
10806 ++processing_template_decl;
10808 if (friend_type != error_mark_node)
10809 make_friend_class (type, friend_type, /*complain=*/false);
10811 if (adjust_processing_template_decl)
10812 --processing_template_decl;
10814 else
10816 /* Build new DECL_FRIENDLIST. */
10817 tree r;
10819 /* The file and line for this declaration, to
10820 assist in error message reporting. Since we
10821 called push_tinst_level above, we don't need to
10822 restore these. */
10823 input_location = DECL_SOURCE_LOCATION (t);
10825 if (TREE_CODE (t) == TEMPLATE_DECL)
10827 ++processing_template_decl;
10828 push_deferring_access_checks (dk_no_check);
10831 r = tsubst_friend_function (t, args);
10832 add_friend (type, r, /*complain=*/false);
10833 if (TREE_CODE (t) == TEMPLATE_DECL)
10835 pop_deferring_access_checks ();
10836 --processing_template_decl;
10842 if (fn_context)
10844 /* Restore these before substituting into the lambda capture
10845 initializers. */
10846 cp_unevaluated_operand = saved_unevaluated_operand;
10847 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10850 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10852 tree decl = lambda_function (type);
10853 if (decl)
10855 if (cxx_dialect >= cxx17)
10856 CLASSTYPE_LITERAL_P (type) = true;
10858 if (!DECL_TEMPLATE_INFO (decl)
10859 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10861 /* Set function_depth to avoid garbage collection. */
10862 ++function_depth;
10863 instantiate_decl (decl, /*defer_ok=*/false, false);
10864 --function_depth;
10867 /* We need to instantiate the capture list from the template
10868 after we've instantiated the closure members, but before we
10869 consider adding the conversion op. Also keep any captures
10870 that may have been added during instantiation of the op(). */
10871 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10872 tree tmpl_cap
10873 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10874 args, tf_warning_or_error, NULL_TREE,
10875 false, false);
10877 LAMBDA_EXPR_CAPTURE_LIST (expr)
10878 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10880 maybe_add_lambda_conv_op (type);
10882 else
10883 gcc_assert (errorcount);
10886 /* Set the file and line number information to whatever is given for
10887 the class itself. This puts error messages involving generated
10888 implicit functions at a predictable point, and the same point
10889 that would be used for non-template classes. */
10890 input_location = DECL_SOURCE_LOCATION (typedecl);
10892 unreverse_member_declarations (type);
10893 finish_struct_1 (type);
10894 TYPE_BEING_DEFINED (type) = 0;
10896 /* We don't instantiate default arguments for member functions. 14.7.1:
10898 The implicit instantiation of a class template specialization causes
10899 the implicit instantiation of the declarations, but not of the
10900 definitions or default arguments, of the class member functions,
10901 member classes, static data members and member templates.... */
10903 /* Some typedefs referenced from within the template code need to be access
10904 checked at template instantiation time, i.e now. These types were
10905 added to the template at parsing time. Let's get those and perform
10906 the access checks then. */
10907 perform_typedefs_access_check (pattern, args);
10908 perform_deferred_access_checks (tf_warning_or_error);
10909 pop_nested_class ();
10910 maximum_field_alignment = saved_maximum_field_alignment;
10911 if (!fn_context)
10912 pop_from_top_level ();
10913 pop_deferring_access_checks ();
10914 pop_tinst_level ();
10916 /* The vtable for a template class can be emitted in any translation
10917 unit in which the class is instantiated. When there is no key
10918 method, however, finish_struct_1 will already have added TYPE to
10919 the keyed_classes. */
10920 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10921 vec_safe_push (keyed_classes, type);
10923 return type;
10926 /* Wrapper for instantiate_class_template_1. */
10928 tree
10929 instantiate_class_template (tree type)
10931 tree ret;
10932 timevar_push (TV_TEMPLATE_INST);
10933 ret = instantiate_class_template_1 (type);
10934 timevar_pop (TV_TEMPLATE_INST);
10935 return ret;
10938 static tree
10939 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10941 tree r;
10943 if (!t)
10944 r = t;
10945 else if (TYPE_P (t))
10946 r = tsubst (t, args, complain, in_decl);
10947 else
10949 if (!(complain & tf_warning))
10950 ++c_inhibit_evaluation_warnings;
10951 r = tsubst_expr (t, args, complain, in_decl,
10952 /*integral_constant_expression_p=*/true);
10953 if (!(complain & tf_warning))
10954 --c_inhibit_evaluation_warnings;
10956 return r;
10959 /* Given a function parameter pack TMPL_PARM and some function parameters
10960 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10961 and set *SPEC_P to point at the next point in the list. */
10963 tree
10964 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10966 /* Collect all of the extra "packed" parameters into an
10967 argument pack. */
10968 tree parmvec;
10969 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10970 tree spec_parm = *spec_p;
10971 int i, len;
10973 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10974 if (tmpl_parm
10975 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10976 break;
10978 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10979 parmvec = make_tree_vec (len);
10980 spec_parm = *spec_p;
10981 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10982 TREE_VEC_ELT (parmvec, i) = spec_parm;
10984 /* Build the argument packs. */
10985 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10986 *spec_p = spec_parm;
10988 return argpack;
10991 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10992 NONTYPE_ARGUMENT_PACK. */
10994 static tree
10995 make_fnparm_pack (tree spec_parm)
10997 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11000 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11001 pack expansion with no extra args, 2 if it has extra args, or 0
11002 if it is not a pack expansion. */
11004 static int
11005 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11007 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11008 if (i >= TREE_VEC_LENGTH (vec))
11009 return 0;
11010 tree elt = TREE_VEC_ELT (vec, i);
11011 if (DECL_P (elt))
11012 /* A decl pack is itself an expansion. */
11013 elt = TREE_TYPE (elt);
11014 if (!PACK_EXPANSION_P (elt))
11015 return 0;
11016 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11017 return 2;
11018 return 1;
11022 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11024 static tree
11025 make_argument_pack_select (tree arg_pack, unsigned index)
11027 tree aps = make_node (ARGUMENT_PACK_SELECT);
11029 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11030 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11032 return aps;
11035 /* This is a subroutine of tsubst_pack_expansion.
11037 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11038 mechanism to store the (non complete list of) arguments of the
11039 substitution and return a non substituted pack expansion, in order
11040 to wait for when we have enough arguments to really perform the
11041 substitution. */
11043 static bool
11044 use_pack_expansion_extra_args_p (tree parm_packs,
11045 int arg_pack_len,
11046 bool has_empty_arg)
11048 /* If one pack has an expansion and another pack has a normal
11049 argument or if one pack has an empty argument and an another
11050 one hasn't then tsubst_pack_expansion cannot perform the
11051 substitution and need to fall back on the
11052 PACK_EXPANSION_EXTRA mechanism. */
11053 if (parm_packs == NULL_TREE)
11054 return false;
11055 else if (has_empty_arg)
11056 return true;
11058 bool has_expansion_arg = false;
11059 for (int i = 0 ; i < arg_pack_len; ++i)
11061 bool has_non_expansion_arg = false;
11062 for (tree parm_pack = parm_packs;
11063 parm_pack;
11064 parm_pack = TREE_CHAIN (parm_pack))
11066 tree arg = TREE_VALUE (parm_pack);
11068 int exp = argument_pack_element_is_expansion_p (arg, i);
11069 if (exp == 2)
11070 /* We can't substitute a pack expansion with extra args into
11071 our pattern. */
11072 return true;
11073 else if (exp)
11074 has_expansion_arg = true;
11075 else
11076 has_non_expansion_arg = true;
11079 if (has_expansion_arg && has_non_expansion_arg)
11080 return true;
11082 return false;
11085 /* [temp.variadic]/6 says that:
11087 The instantiation of a pack expansion [...]
11088 produces a list E1,E2, ..., En, where N is the number of elements
11089 in the pack expansion parameters.
11091 This subroutine of tsubst_pack_expansion produces one of these Ei.
11093 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11094 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11095 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11096 INDEX is the index 'i' of the element Ei to produce. ARGS,
11097 COMPLAIN, and IN_DECL are the same parameters as for the
11098 tsubst_pack_expansion function.
11100 The function returns the resulting Ei upon successful completion,
11101 or error_mark_node.
11103 Note that this function possibly modifies the ARGS parameter, so
11104 it's the responsibility of the caller to restore it. */
11106 static tree
11107 gen_elem_of_pack_expansion_instantiation (tree pattern,
11108 tree parm_packs,
11109 unsigned index,
11110 tree args /* This parm gets
11111 modified. */,
11112 tsubst_flags_t complain,
11113 tree in_decl)
11115 tree t;
11116 bool ith_elem_is_expansion = false;
11118 /* For each parameter pack, change the substitution of the parameter
11119 pack to the ith argument in its argument pack, then expand the
11120 pattern. */
11121 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11123 tree parm = TREE_PURPOSE (pack);
11124 tree arg_pack = TREE_VALUE (pack);
11125 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11127 ith_elem_is_expansion |=
11128 argument_pack_element_is_expansion_p (arg_pack, index);
11130 /* Select the Ith argument from the pack. */
11131 if (TREE_CODE (parm) == PARM_DECL
11132 || TREE_CODE (parm) == FIELD_DECL)
11134 if (index == 0)
11136 aps = make_argument_pack_select (arg_pack, index);
11137 if (!mark_used (parm, complain) && !(complain & tf_error))
11138 return error_mark_node;
11139 register_local_specialization (aps, parm);
11141 else
11142 aps = retrieve_local_specialization (parm);
11144 else
11146 int idx, level;
11147 template_parm_level_and_index (parm, &level, &idx);
11149 if (index == 0)
11151 aps = make_argument_pack_select (arg_pack, index);
11152 /* Update the corresponding argument. */
11153 TMPL_ARG (args, level, idx) = aps;
11155 else
11156 /* Re-use the ARGUMENT_PACK_SELECT. */
11157 aps = TMPL_ARG (args, level, idx);
11159 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11162 /* Substitute into the PATTERN with the (possibly altered)
11163 arguments. */
11164 if (pattern == in_decl)
11165 /* Expanding a fixed parameter pack from
11166 coerce_template_parameter_pack. */
11167 t = tsubst_decl (pattern, args, complain);
11168 else if (pattern == error_mark_node)
11169 t = error_mark_node;
11170 else if (constraint_p (pattern))
11172 if (processing_template_decl)
11173 t = tsubst_constraint (pattern, args, complain, in_decl);
11174 else
11175 t = (constraints_satisfied_p (pattern, args)
11176 ? boolean_true_node : boolean_false_node);
11178 else if (!TYPE_P (pattern))
11179 t = tsubst_expr (pattern, args, complain, in_decl,
11180 /*integral_constant_expression_p=*/false);
11181 else
11182 t = tsubst (pattern, args, complain, in_decl);
11184 /* If the Ith argument pack element is a pack expansion, then
11185 the Ith element resulting from the substituting is going to
11186 be a pack expansion as well. */
11187 if (ith_elem_is_expansion)
11188 t = make_pack_expansion (t, complain);
11190 return t;
11193 /* When the unexpanded parameter pack in a fold expression expands to an empty
11194 sequence, the value of the expression is as follows; the program is
11195 ill-formed if the operator is not listed in this table.
11197 && true
11198 || false
11199 , void() */
11201 tree
11202 expand_empty_fold (tree t, tsubst_flags_t complain)
11204 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11205 if (!FOLD_EXPR_MODIFY_P (t))
11206 switch (code)
11208 case TRUTH_ANDIF_EXPR:
11209 return boolean_true_node;
11210 case TRUTH_ORIF_EXPR:
11211 return boolean_false_node;
11212 case COMPOUND_EXPR:
11213 return void_node;
11214 default:
11215 break;
11218 if (complain & tf_error)
11219 error_at (location_of (t),
11220 "fold of empty expansion over %O", code);
11221 return error_mark_node;
11224 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11225 form an expression that combines the two terms using the
11226 operator of T. */
11228 static tree
11229 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11231 tree op = FOLD_EXPR_OP (t);
11232 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11234 // Handle compound assignment operators.
11235 if (FOLD_EXPR_MODIFY_P (t))
11236 return build_x_modify_expr (input_location, left, code, right, complain);
11238 switch (code)
11240 case COMPOUND_EXPR:
11241 return build_x_compound_expr (input_location, left, right, complain);
11242 case DOTSTAR_EXPR:
11243 return build_m_component_ref (left, right, complain);
11244 default:
11245 return build_x_binary_op (input_location, code,
11246 left, TREE_CODE (left),
11247 right, TREE_CODE (right),
11248 /*overload=*/NULL,
11249 complain);
11253 /* Substitute ARGS into the pack of a fold expression T. */
11255 static inline tree
11256 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11258 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11261 /* Substitute ARGS into the pack of a fold expression T. */
11263 static inline tree
11264 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11266 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11269 /* Expand a PACK of arguments into a grouped as left fold.
11270 Given a pack containing elements A0, A1, ..., An and an
11271 operator @, this builds the expression:
11273 ((A0 @ A1) @ A2) ... @ An
11275 Note that PACK must not be empty.
11277 The operator is defined by the original fold expression T. */
11279 static tree
11280 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11282 tree left = TREE_VEC_ELT (pack, 0);
11283 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11285 tree right = TREE_VEC_ELT (pack, i);
11286 left = fold_expression (t, left, right, complain);
11288 return left;
11291 /* Substitute into a unary left fold expression. */
11293 static tree
11294 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11295 tree in_decl)
11297 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11298 if (pack == error_mark_node)
11299 return error_mark_node;
11300 if (PACK_EXPANSION_P (pack))
11302 tree r = copy_node (t);
11303 FOLD_EXPR_PACK (r) = pack;
11304 return r;
11306 if (TREE_VEC_LENGTH (pack) == 0)
11307 return expand_empty_fold (t, complain);
11308 else
11309 return expand_left_fold (t, pack, complain);
11312 /* Substitute into a binary left fold expression.
11314 Do ths by building a single (non-empty) vector of argumnts and
11315 building the expression from those elements. */
11317 static tree
11318 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11319 tree in_decl)
11321 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11322 if (pack == error_mark_node)
11323 return error_mark_node;
11324 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11325 if (init == error_mark_node)
11326 return error_mark_node;
11328 if (PACK_EXPANSION_P (pack))
11330 tree r = copy_node (t);
11331 FOLD_EXPR_PACK (r) = pack;
11332 FOLD_EXPR_INIT (r) = init;
11333 return r;
11336 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11337 TREE_VEC_ELT (vec, 0) = init;
11338 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11339 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11341 return expand_left_fold (t, vec, complain);
11344 /* Expand a PACK of arguments into a grouped as right fold.
11345 Given a pack containing elementns A0, A1, ..., and an
11346 operator @, this builds the expression:
11348 A0@ ... (An-2 @ (An-1 @ An))
11350 Note that PACK must not be empty.
11352 The operator is defined by the original fold expression T. */
11354 tree
11355 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11357 // Build the expression.
11358 int n = TREE_VEC_LENGTH (pack);
11359 tree right = TREE_VEC_ELT (pack, n - 1);
11360 for (--n; n != 0; --n)
11362 tree left = TREE_VEC_ELT (pack, n - 1);
11363 right = fold_expression (t, left, right, complain);
11365 return right;
11368 /* Substitute into a unary right fold expression. */
11370 static tree
11371 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11372 tree in_decl)
11374 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11375 if (pack == error_mark_node)
11376 return error_mark_node;
11377 if (PACK_EXPANSION_P (pack))
11379 tree r = copy_node (t);
11380 FOLD_EXPR_PACK (r) = pack;
11381 return r;
11383 if (TREE_VEC_LENGTH (pack) == 0)
11384 return expand_empty_fold (t, complain);
11385 else
11386 return expand_right_fold (t, pack, complain);
11389 /* Substitute into a binary right fold expression.
11391 Do ths by building a single (non-empty) vector of arguments and
11392 building the expression from those elements. */
11394 static tree
11395 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11396 tree in_decl)
11398 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11399 if (pack == error_mark_node)
11400 return error_mark_node;
11401 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11402 if (init == error_mark_node)
11403 return error_mark_node;
11405 if (PACK_EXPANSION_P (pack))
11407 tree r = copy_node (t);
11408 FOLD_EXPR_PACK (r) = pack;
11409 FOLD_EXPR_INIT (r) = init;
11410 return r;
11413 int n = TREE_VEC_LENGTH (pack);
11414 tree vec = make_tree_vec (n + 1);
11415 for (int i = 0; i < n; ++i)
11416 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11417 TREE_VEC_ELT (vec, n) = init;
11419 return expand_right_fold (t, vec, complain);
11423 /* Substitute ARGS into T, which is an pack expansion
11424 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11425 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11426 (if only a partial substitution could be performed) or
11427 ERROR_MARK_NODE if there was an error. */
11428 tree
11429 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11430 tree in_decl)
11432 tree pattern;
11433 tree pack, packs = NULL_TREE;
11434 bool unsubstituted_packs = false;
11435 int i, len = -1;
11436 tree result;
11437 hash_map<tree, tree> *saved_local_specializations = NULL;
11438 bool need_local_specializations = false;
11439 int levels;
11441 gcc_assert (PACK_EXPANSION_P (t));
11442 pattern = PACK_EXPANSION_PATTERN (t);
11444 /* Add in any args remembered from an earlier partial instantiation. */
11445 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11447 levels = TMPL_ARGS_DEPTH (args);
11449 /* Determine the argument packs that will instantiate the parameter
11450 packs used in the expansion expression. While we're at it,
11451 compute the number of arguments to be expanded and make sure it
11452 is consistent. */
11453 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11454 pack = TREE_CHAIN (pack))
11456 tree parm_pack = TREE_VALUE (pack);
11457 tree arg_pack = NULL_TREE;
11458 tree orig_arg = NULL_TREE;
11459 int level = 0;
11461 if (TREE_CODE (parm_pack) == BASES)
11463 gcc_assert (parm_pack == pattern);
11464 if (BASES_DIRECT (parm_pack))
11465 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11466 args, complain, in_decl, false));
11467 else
11468 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11469 args, complain, in_decl, false));
11471 else if (builtin_pack_call_p (parm_pack))
11473 /* ??? Support use in other patterns. */
11474 gcc_assert (parm_pack == pattern);
11475 return expand_builtin_pack_call (parm_pack, args,
11476 complain, in_decl);
11478 else if (TREE_CODE (parm_pack) == PARM_DECL)
11480 /* We know we have correct local_specializations if this
11481 expansion is at function scope, or if we're dealing with a
11482 local parameter in a requires expression; for the latter,
11483 tsubst_requires_expr set it up appropriately. */
11484 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11485 arg_pack = retrieve_local_specialization (parm_pack);
11486 else
11487 /* We can't rely on local_specializations for a parameter
11488 name used later in a function declaration (such as in a
11489 late-specified return type). Even if it exists, it might
11490 have the wrong value for a recursive call. */
11491 need_local_specializations = true;
11493 if (!arg_pack)
11495 /* This parameter pack was used in an unevaluated context. Just
11496 make a dummy decl, since it's only used for its type. */
11497 arg_pack = tsubst_decl (parm_pack, args, complain);
11498 if (arg_pack && DECL_PACK_P (arg_pack))
11499 /* Partial instantiation of the parm_pack, we can't build
11500 up an argument pack yet. */
11501 arg_pack = NULL_TREE;
11502 else
11503 arg_pack = make_fnparm_pack (arg_pack);
11506 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11507 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11508 else
11510 int idx;
11511 template_parm_level_and_index (parm_pack, &level, &idx);
11513 if (level <= levels)
11514 arg_pack = TMPL_ARG (args, level, idx);
11517 orig_arg = arg_pack;
11518 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11519 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11521 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11522 /* This can only happen if we forget to expand an argument
11523 pack somewhere else. Just return an error, silently. */
11525 result = make_tree_vec (1);
11526 TREE_VEC_ELT (result, 0) = error_mark_node;
11527 return result;
11530 if (arg_pack)
11532 int my_len =
11533 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11535 /* Don't bother trying to do a partial substitution with
11536 incomplete packs; we'll try again after deduction. */
11537 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11538 return t;
11540 if (len < 0)
11541 len = my_len;
11542 else if (len != my_len)
11544 if (!(complain & tf_error))
11545 /* Fail quietly. */;
11546 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11547 error ("mismatched argument pack lengths while expanding %qT",
11548 pattern);
11549 else
11550 error ("mismatched argument pack lengths while expanding %qE",
11551 pattern);
11552 return error_mark_node;
11555 /* Keep track of the parameter packs and their corresponding
11556 argument packs. */
11557 packs = tree_cons (parm_pack, arg_pack, packs);
11558 TREE_TYPE (packs) = orig_arg;
11560 else
11562 /* We can't substitute for this parameter pack. We use a flag as
11563 well as the missing_level counter because function parameter
11564 packs don't have a level. */
11565 gcc_assert (processing_template_decl);
11566 unsubstituted_packs = true;
11570 /* If the expansion is just T..., return the matching argument pack, unless
11571 we need to call convert_from_reference on all the elements. This is an
11572 important optimization; see c++/68422. */
11573 if (!unsubstituted_packs
11574 && TREE_PURPOSE (packs) == pattern)
11576 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11577 /* Types need no adjustment, nor does sizeof..., and if we still have
11578 some pack expansion args we won't do anything yet. */
11579 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11580 || PACK_EXPANSION_SIZEOF_P (t)
11581 || pack_expansion_args_count (args))
11582 return args;
11583 /* Also optimize expression pack expansions if we can tell that the
11584 elements won't have reference type. */
11585 tree type = TREE_TYPE (pattern);
11586 if (type && TREE_CODE (type) != REFERENCE_TYPE
11587 && !PACK_EXPANSION_P (type)
11588 && !WILDCARD_TYPE_P (type))
11589 return args;
11590 /* Otherwise use the normal path so we get convert_from_reference. */
11593 /* We cannot expand this expansion expression, because we don't have
11594 all of the argument packs we need. */
11595 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11597 /* We got some full packs, but we can't substitute them in until we
11598 have values for all the packs. So remember these until then. */
11600 t = make_pack_expansion (pattern, complain);
11601 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11602 return t;
11604 else if (unsubstituted_packs)
11606 /* There were no real arguments, we're just replacing a parameter
11607 pack with another version of itself. Substitute into the
11608 pattern and return a PACK_EXPANSION_*. The caller will need to
11609 deal with that. */
11610 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11611 t = tsubst_expr (pattern, args, complain, in_decl,
11612 /*integral_constant_expression_p=*/false);
11613 else
11614 t = tsubst (pattern, args, complain, in_decl);
11615 t = make_pack_expansion (t, complain);
11616 return t;
11619 gcc_assert (len >= 0);
11621 if (need_local_specializations)
11623 /* We're in a late-specified return type, so create our own local
11624 specializations map; the current map is either NULL or (in the
11625 case of recursive unification) might have bindings that we don't
11626 want to use or alter. */
11627 saved_local_specializations = local_specializations;
11628 local_specializations = new hash_map<tree, tree>;
11631 /* For each argument in each argument pack, substitute into the
11632 pattern. */
11633 result = make_tree_vec (len);
11634 tree elem_args = copy_template_args (args);
11635 for (i = 0; i < len; ++i)
11637 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11639 elem_args, complain,
11640 in_decl);
11641 TREE_VEC_ELT (result, i) = t;
11642 if (t == error_mark_node)
11644 result = error_mark_node;
11645 break;
11649 /* Update ARGS to restore the substitution from parameter packs to
11650 their argument packs. */
11651 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11653 tree parm = TREE_PURPOSE (pack);
11655 if (TREE_CODE (parm) == PARM_DECL
11656 || TREE_CODE (parm) == FIELD_DECL)
11657 register_local_specialization (TREE_TYPE (pack), parm);
11658 else
11660 int idx, level;
11662 if (TREE_VALUE (pack) == NULL_TREE)
11663 continue;
11665 template_parm_level_and_index (parm, &level, &idx);
11667 /* Update the corresponding argument. */
11668 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11669 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11670 TREE_TYPE (pack);
11671 else
11672 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11676 if (need_local_specializations)
11678 delete local_specializations;
11679 local_specializations = saved_local_specializations;
11682 /* If the dependent pack arguments were such that we end up with only a
11683 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11684 if (len == 1 && TREE_CODE (result) == TREE_VEC
11685 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11686 return TREE_VEC_ELT (result, 0);
11688 return result;
11691 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11692 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11693 parameter packs; all parms generated from a function parameter pack will
11694 have the same DECL_PARM_INDEX. */
11696 tree
11697 get_pattern_parm (tree parm, tree tmpl)
11699 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11700 tree patparm;
11702 if (DECL_ARTIFICIAL (parm))
11704 for (patparm = DECL_ARGUMENTS (pattern);
11705 patparm; patparm = DECL_CHAIN (patparm))
11706 if (DECL_ARTIFICIAL (patparm)
11707 && DECL_NAME (parm) == DECL_NAME (patparm))
11708 break;
11710 else
11712 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11713 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11714 gcc_assert (DECL_PARM_INDEX (patparm)
11715 == DECL_PARM_INDEX (parm));
11718 return patparm;
11721 /* Make an argument pack out of the TREE_VEC VEC. */
11723 static tree
11724 make_argument_pack (tree vec)
11726 tree pack;
11727 tree elt = TREE_VEC_ELT (vec, 0);
11728 if (TYPE_P (elt))
11729 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11730 else
11732 pack = make_node (NONTYPE_ARGUMENT_PACK);
11733 TREE_CONSTANT (pack) = 1;
11735 SET_ARGUMENT_PACK_ARGS (pack, vec);
11736 return pack;
11739 /* Return an exact copy of template args T that can be modified
11740 independently. */
11742 static tree
11743 copy_template_args (tree t)
11745 if (t == error_mark_node)
11746 return t;
11748 int len = TREE_VEC_LENGTH (t);
11749 tree new_vec = make_tree_vec (len);
11751 for (int i = 0; i < len; ++i)
11753 tree elt = TREE_VEC_ELT (t, i);
11754 if (elt && TREE_CODE (elt) == TREE_VEC)
11755 elt = copy_template_args (elt);
11756 TREE_VEC_ELT (new_vec, i) = elt;
11759 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11760 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11762 return new_vec;
11765 /* Substitute ARGS into the vector or list of template arguments T. */
11767 static tree
11768 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11770 tree orig_t = t;
11771 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11772 tree *elts;
11774 if (t == error_mark_node)
11775 return error_mark_node;
11777 len = TREE_VEC_LENGTH (t);
11778 elts = XALLOCAVEC (tree, len);
11780 for (i = 0; i < len; i++)
11782 tree orig_arg = TREE_VEC_ELT (t, i);
11783 tree new_arg;
11785 if (TREE_CODE (orig_arg) == TREE_VEC)
11786 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11787 else if (PACK_EXPANSION_P (orig_arg))
11789 /* Substitute into an expansion expression. */
11790 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11792 if (TREE_CODE (new_arg) == TREE_VEC)
11793 /* Add to the expanded length adjustment the number of
11794 expanded arguments. We subtract one from this
11795 measurement, because the argument pack expression
11796 itself is already counted as 1 in
11797 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11798 the argument pack is empty. */
11799 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11801 else if (ARGUMENT_PACK_P (orig_arg))
11803 /* Substitute into each of the arguments. */
11804 new_arg = TYPE_P (orig_arg)
11805 ? cxx_make_type (TREE_CODE (orig_arg))
11806 : make_node (TREE_CODE (orig_arg));
11808 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11809 args, complain, in_decl);
11810 if (pack_args == error_mark_node)
11811 new_arg = error_mark_node;
11812 else
11813 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11815 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11816 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11818 else
11819 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11821 if (new_arg == error_mark_node)
11822 return error_mark_node;
11824 elts[i] = new_arg;
11825 if (new_arg != orig_arg)
11826 need_new = 1;
11829 if (!need_new)
11830 return t;
11832 /* Make space for the expanded arguments coming from template
11833 argument packs. */
11834 t = make_tree_vec (len + expanded_len_adjust);
11835 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11836 arguments for a member template.
11837 In that case each TREE_VEC in ORIG_T represents a level of template
11838 arguments, and ORIG_T won't carry any non defaulted argument count.
11839 It will rather be the nested TREE_VECs that will carry one.
11840 In other words, ORIG_T carries a non defaulted argument count only
11841 if it doesn't contain any nested TREE_VEC. */
11842 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11844 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11845 count += expanded_len_adjust;
11846 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11848 for (i = 0, out = 0; i < len; i++)
11850 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11851 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11852 && TREE_CODE (elts[i]) == TREE_VEC)
11854 int idx;
11856 /* Now expand the template argument pack "in place". */
11857 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11858 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11860 else
11862 TREE_VEC_ELT (t, out) = elts[i];
11863 out++;
11867 return t;
11870 /* Substitute ARGS into one level PARMS of template parameters. */
11872 static tree
11873 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11875 if (parms == error_mark_node)
11876 return error_mark_node;
11878 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11880 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11882 tree tuple = TREE_VEC_ELT (parms, i);
11884 if (tuple == error_mark_node)
11885 continue;
11887 TREE_VEC_ELT (new_vec, i) =
11888 tsubst_template_parm (tuple, args, complain);
11891 return new_vec;
11894 /* Return the result of substituting ARGS into the template parameters
11895 given by PARMS. If there are m levels of ARGS and m + n levels of
11896 PARMS, then the result will contain n levels of PARMS. For
11897 example, if PARMS is `template <class T> template <class U>
11898 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11899 result will be `template <int*, double, class V>'. */
11901 static tree
11902 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11904 tree r = NULL_TREE;
11905 tree* new_parms;
11907 /* When substituting into a template, we must set
11908 PROCESSING_TEMPLATE_DECL as the template parameters may be
11909 dependent if they are based on one-another, and the dependency
11910 predicates are short-circuit outside of templates. */
11911 ++processing_template_decl;
11913 for (new_parms = &r;
11914 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11915 new_parms = &(TREE_CHAIN (*new_parms)),
11916 parms = TREE_CHAIN (parms))
11918 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11919 args, complain);
11920 *new_parms =
11921 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11922 - TMPL_ARGS_DEPTH (args)),
11923 new_vec, NULL_TREE);
11926 --processing_template_decl;
11928 return r;
11931 /* Return the result of substituting ARGS into one template parameter
11932 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11933 parameter and which TREE_PURPOSE is the default argument of the
11934 template parameter. */
11936 static tree
11937 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11939 tree default_value, parm_decl;
11941 if (args == NULL_TREE
11942 || t == NULL_TREE
11943 || t == error_mark_node)
11944 return t;
11946 gcc_assert (TREE_CODE (t) == TREE_LIST);
11948 default_value = TREE_PURPOSE (t);
11949 parm_decl = TREE_VALUE (t);
11951 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11952 if (TREE_CODE (parm_decl) == PARM_DECL
11953 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11954 parm_decl = error_mark_node;
11955 default_value = tsubst_template_arg (default_value, args,
11956 complain, NULL_TREE);
11958 return build_tree_list (default_value, parm_decl);
11961 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11962 type T. If T is not an aggregate or enumeration type, it is
11963 handled as if by tsubst. IN_DECL is as for tsubst. If
11964 ENTERING_SCOPE is nonzero, T is the context for a template which
11965 we are presently tsubst'ing. Return the substituted value. */
11967 static tree
11968 tsubst_aggr_type (tree t,
11969 tree args,
11970 tsubst_flags_t complain,
11971 tree in_decl,
11972 int entering_scope)
11974 if (t == NULL_TREE)
11975 return NULL_TREE;
11977 switch (TREE_CODE (t))
11979 case RECORD_TYPE:
11980 if (TYPE_PTRMEMFUNC_P (t))
11981 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11983 /* Fall through. */
11984 case ENUMERAL_TYPE:
11985 case UNION_TYPE:
11986 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11988 tree argvec;
11989 tree context;
11990 tree r;
11991 int saved_unevaluated_operand;
11992 int saved_inhibit_evaluation_warnings;
11994 /* In "sizeof(X<I>)" we need to evaluate "I". */
11995 saved_unevaluated_operand = cp_unevaluated_operand;
11996 cp_unevaluated_operand = 0;
11997 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11998 c_inhibit_evaluation_warnings = 0;
12000 /* First, determine the context for the type we are looking
12001 up. */
12002 context = TYPE_CONTEXT (t);
12003 if (context && TYPE_P (context))
12005 context = tsubst_aggr_type (context, args, complain,
12006 in_decl, /*entering_scope=*/1);
12007 /* If context is a nested class inside a class template,
12008 it may still need to be instantiated (c++/33959). */
12009 context = complete_type (context);
12012 /* Then, figure out what arguments are appropriate for the
12013 type we are trying to find. For example, given:
12015 template <class T> struct S;
12016 template <class T, class U> void f(T, U) { S<U> su; }
12018 and supposing that we are instantiating f<int, double>,
12019 then our ARGS will be {int, double}, but, when looking up
12020 S we only want {double}. */
12021 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12022 complain, in_decl);
12023 if (argvec == error_mark_node)
12024 r = error_mark_node;
12025 else
12027 r = lookup_template_class (t, argvec, in_decl, context,
12028 entering_scope, complain);
12029 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12032 cp_unevaluated_operand = saved_unevaluated_operand;
12033 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12035 return r;
12037 else
12038 /* This is not a template type, so there's nothing to do. */
12039 return t;
12041 default:
12042 return tsubst (t, args, complain, in_decl);
12046 static GTY((cache)) tree_cache_map *defarg_inst;
12048 /* Substitute into the default argument ARG (a default argument for
12049 FN), which has the indicated TYPE. */
12051 tree
12052 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12053 tsubst_flags_t complain)
12055 tree saved_class_ptr = NULL_TREE;
12056 tree saved_class_ref = NULL_TREE;
12057 int errs = errorcount + sorrycount;
12059 /* This can happen in invalid code. */
12060 if (TREE_CODE (arg) == DEFAULT_ARG)
12061 return arg;
12063 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12064 parm = chain_index (parmnum, parm);
12065 tree parmtype = TREE_TYPE (parm);
12066 if (DECL_BY_REFERENCE (parm))
12067 parmtype = TREE_TYPE (parmtype);
12068 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12070 tree *slot;
12071 if (defarg_inst && (slot = defarg_inst->get (parm)))
12072 return *slot;
12074 /* This default argument came from a template. Instantiate the
12075 default argument here, not in tsubst. In the case of
12076 something like:
12078 template <class T>
12079 struct S {
12080 static T t();
12081 void f(T = t());
12084 we must be careful to do name lookup in the scope of S<T>,
12085 rather than in the current class. */
12086 push_access_scope (fn);
12087 /* The "this" pointer is not valid in a default argument. */
12088 if (cfun)
12090 saved_class_ptr = current_class_ptr;
12091 cp_function_chain->x_current_class_ptr = NULL_TREE;
12092 saved_class_ref = current_class_ref;
12093 cp_function_chain->x_current_class_ref = NULL_TREE;
12096 start_lambda_scope (parm);
12098 push_deferring_access_checks(dk_no_deferred);
12099 /* The default argument expression may cause implicitly defined
12100 member functions to be synthesized, which will result in garbage
12101 collection. We must treat this situation as if we were within
12102 the body of function so as to avoid collecting live data on the
12103 stack. */
12104 ++function_depth;
12105 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12106 complain, NULL_TREE,
12107 /*integral_constant_expression_p=*/false);
12108 --function_depth;
12109 pop_deferring_access_checks();
12111 finish_lambda_scope ();
12113 /* Restore the "this" pointer. */
12114 if (cfun)
12116 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12117 cp_function_chain->x_current_class_ref = saved_class_ref;
12120 if (errorcount+sorrycount > errs
12121 && (complain & tf_warning_or_error))
12122 inform (input_location,
12123 " when instantiating default argument for call to %qD", fn);
12125 /* Make sure the default argument is reasonable. */
12126 arg = check_default_argument (type, arg, complain);
12128 pop_access_scope (fn);
12130 if (arg != error_mark_node && !cp_unevaluated_operand)
12132 if (!defarg_inst)
12133 defarg_inst = tree_cache_map::create_ggc (37);
12134 defarg_inst->put (parm, arg);
12137 return arg;
12140 /* Substitute into all the default arguments for FN. */
12142 static void
12143 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12145 tree arg;
12146 tree tmpl_args;
12148 tmpl_args = DECL_TI_ARGS (fn);
12150 /* If this function is not yet instantiated, we certainly don't need
12151 its default arguments. */
12152 if (uses_template_parms (tmpl_args))
12153 return;
12154 /* Don't do this again for clones. */
12155 if (DECL_CLONED_FUNCTION_P (fn))
12156 return;
12158 int i = 0;
12159 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12160 arg;
12161 arg = TREE_CHAIN (arg), ++i)
12162 if (TREE_PURPOSE (arg))
12163 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12164 TREE_VALUE (arg),
12165 TREE_PURPOSE (arg),
12166 complain);
12169 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12171 static tree
12172 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12173 tree lambda_fntype)
12175 tree gen_tmpl, argvec;
12176 hashval_t hash = 0;
12177 tree in_decl = t;
12179 /* Nobody should be tsubst'ing into non-template functions. */
12180 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12182 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12184 /* If T is not dependent, just return it. */
12185 if (!uses_template_parms (DECL_TI_ARGS (t)))
12186 return t;
12188 /* Calculate the most general template of which R is a
12189 specialization, and the complete set of arguments used to
12190 specialize R. */
12191 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12192 argvec = tsubst_template_args (DECL_TI_ARGS
12193 (DECL_TEMPLATE_RESULT
12194 (DECL_TI_TEMPLATE (t))),
12195 args, complain, in_decl);
12196 if (argvec == error_mark_node)
12197 return error_mark_node;
12199 /* Check to see if we already have this specialization. */
12200 if (!lambda_fntype)
12202 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12203 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12204 return spec;
12207 /* We can see more levels of arguments than parameters if
12208 there was a specialization of a member template, like
12209 this:
12211 template <class T> struct S { template <class U> void f(); }
12212 template <> template <class U> void S<int>::f(U);
12214 Here, we'll be substituting into the specialization,
12215 because that's where we can find the code we actually
12216 want to generate, but we'll have enough arguments for
12217 the most general template.
12219 We also deal with the peculiar case:
12221 template <class T> struct S {
12222 template <class U> friend void f();
12224 template <class U> void f() {}
12225 template S<int>;
12226 template void f<double>();
12228 Here, the ARGS for the instantiation of will be {int,
12229 double}. But, we only need as many ARGS as there are
12230 levels of template parameters in CODE_PATTERN. We are
12231 careful not to get fooled into reducing the ARGS in
12232 situations like:
12234 template <class T> struct S { template <class U> void f(U); }
12235 template <class T> template <> void S<T>::f(int) {}
12237 which we can spot because the pattern will be a
12238 specialization in this case. */
12239 int args_depth = TMPL_ARGS_DEPTH (args);
12240 int parms_depth =
12241 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12243 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12244 args = get_innermost_template_args (args, parms_depth);
12246 else
12248 /* This special case arises when we have something like this:
12250 template <class T> struct S {
12251 friend void f<int>(int, double);
12254 Here, the DECL_TI_TEMPLATE for the friend declaration
12255 will be an IDENTIFIER_NODE. We are being called from
12256 tsubst_friend_function, and we want only to create a
12257 new decl (R) with appropriate types so that we can call
12258 determine_specialization. */
12259 gen_tmpl = NULL_TREE;
12260 argvec = NULL_TREE;
12263 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12264 : NULL_TREE);
12265 tree ctx = closure ? closure : DECL_CONTEXT (t);
12266 bool member = ctx && TYPE_P (ctx);
12268 if (member && !closure)
12269 ctx = tsubst_aggr_type (ctx, args,
12270 complain, t, /*entering_scope=*/1);
12272 tree type = (lambda_fntype ? lambda_fntype
12273 : tsubst (TREE_TYPE (t), args,
12274 complain | tf_fndecl_type, in_decl));
12275 if (type == error_mark_node)
12276 return error_mark_node;
12278 /* If we hit excessive deduction depth, the type is bogus even if
12279 it isn't error_mark_node, so don't build a decl. */
12280 if (excessive_deduction_depth)
12281 return error_mark_node;
12283 /* We do NOT check for matching decls pushed separately at this
12284 point, as they may not represent instantiations of this
12285 template, and in any case are considered separate under the
12286 discrete model. */
12287 tree r = copy_decl (t);
12288 DECL_USE_TEMPLATE (r) = 0;
12289 TREE_TYPE (r) = type;
12290 /* Clear out the mangled name and RTL for the instantiation. */
12291 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12292 SET_DECL_RTL (r, NULL);
12293 /* Leave DECL_INITIAL set on deleted instantiations. */
12294 if (!DECL_DELETED_FN (r))
12295 DECL_INITIAL (r) = NULL_TREE;
12296 DECL_CONTEXT (r) = ctx;
12298 /* OpenMP UDRs have the only argument a reference to the declared
12299 type. We want to diagnose if the declared type is a reference,
12300 which is invalid, but as references to references are usually
12301 quietly merged, diagnose it here. */
12302 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12304 tree argtype
12305 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12306 argtype = tsubst (argtype, args, complain, in_decl);
12307 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12308 error_at (DECL_SOURCE_LOCATION (t),
12309 "reference type %qT in "
12310 "%<#pragma omp declare reduction%>", argtype);
12311 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12312 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12313 argtype);
12316 if (member && DECL_CONV_FN_P (r))
12317 /* Type-conversion operator. Reconstruct the name, in
12318 case it's the name of one of the template's parameters. */
12319 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12321 tree parms = DECL_ARGUMENTS (t);
12322 if (closure)
12323 parms = DECL_CHAIN (parms);
12324 parms = tsubst (parms, args, complain, t);
12325 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12326 DECL_CONTEXT (parm) = r;
12327 if (closure)
12329 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12330 DECL_CHAIN (tparm) = parms;
12331 parms = tparm;
12333 DECL_ARGUMENTS (r) = parms;
12334 DECL_RESULT (r) = NULL_TREE;
12336 TREE_STATIC (r) = 0;
12337 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12338 DECL_EXTERNAL (r) = 1;
12339 /* If this is an instantiation of a function with internal
12340 linkage, we already know what object file linkage will be
12341 assigned to the instantiation. */
12342 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12343 DECL_DEFER_OUTPUT (r) = 0;
12344 DECL_CHAIN (r) = NULL_TREE;
12345 DECL_PENDING_INLINE_INFO (r) = 0;
12346 DECL_PENDING_INLINE_P (r) = 0;
12347 DECL_SAVED_TREE (r) = NULL_TREE;
12348 DECL_STRUCT_FUNCTION (r) = NULL;
12349 TREE_USED (r) = 0;
12350 /* We'll re-clone as appropriate in instantiate_template. */
12351 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12353 /* If we aren't complaining now, return on error before we register
12354 the specialization so that we'll complain eventually. */
12355 if ((complain & tf_error) == 0
12356 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12357 && !grok_op_properties (r, /*complain=*/false))
12358 return error_mark_node;
12360 /* When instantiating a constrained member, substitute
12361 into the constraints to create a new constraint. */
12362 if (tree ci = get_constraints (t))
12363 if (member)
12365 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12366 set_constraints (r, ci);
12369 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12370 this in the special friend case mentioned above where
12371 GEN_TMPL is NULL. */
12372 if (gen_tmpl && !closure)
12374 DECL_TEMPLATE_INFO (r)
12375 = build_template_info (gen_tmpl, argvec);
12376 SET_DECL_IMPLICIT_INSTANTIATION (r);
12378 tree new_r
12379 = register_specialization (r, gen_tmpl, argvec, false, hash);
12380 if (new_r != r)
12381 /* We instantiated this while substituting into
12382 the type earlier (template/friend54.C). */
12383 return new_r;
12385 /* We're not supposed to instantiate default arguments
12386 until they are called, for a template. But, for a
12387 declaration like:
12389 template <class T> void f ()
12390 { extern void g(int i = T()); }
12392 we should do the substitution when the template is
12393 instantiated. We handle the member function case in
12394 instantiate_class_template since the default arguments
12395 might refer to other members of the class. */
12396 if (!member
12397 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12398 && !uses_template_parms (argvec))
12399 tsubst_default_arguments (r, complain);
12401 else
12402 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12404 /* Copy the list of befriending classes. */
12405 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12406 *friends;
12407 friends = &TREE_CHAIN (*friends))
12409 *friends = copy_node (*friends);
12410 TREE_VALUE (*friends)
12411 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12414 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12416 maybe_retrofit_in_chrg (r);
12417 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12418 return error_mark_node;
12419 /* If this is an instantiation of a member template, clone it.
12420 If it isn't, that'll be handled by
12421 clone_constructors_and_destructors. */
12422 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12423 clone_function_decl (r, /*update_methods=*/false);
12425 else if ((complain & tf_error) != 0
12426 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12427 && !grok_op_properties (r, /*complain=*/true))
12428 return error_mark_node;
12430 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12431 SET_DECL_FRIEND_CONTEXT (r,
12432 tsubst (DECL_FRIEND_CONTEXT (t),
12433 args, complain, in_decl));
12435 /* Possibly limit visibility based on template args. */
12436 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12437 if (DECL_VISIBILITY_SPECIFIED (t))
12439 DECL_VISIBILITY_SPECIFIED (r) = 0;
12440 DECL_ATTRIBUTES (r)
12441 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12443 determine_visibility (r);
12444 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12445 && !processing_template_decl)
12446 defaulted_late_check (r);
12448 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12449 args, complain, in_decl);
12450 return r;
12453 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12455 static tree
12456 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12457 tree lambda_fntype)
12459 /* We can get here when processing a member function template,
12460 member class template, or template template parameter. */
12461 tree decl = DECL_TEMPLATE_RESULT (t);
12462 tree in_decl = t;
12463 tree spec;
12464 tree tmpl_args;
12465 tree full_args;
12466 tree r;
12467 hashval_t hash = 0;
12469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12471 /* Template template parameter is treated here. */
12472 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12473 if (new_type == error_mark_node)
12474 r = error_mark_node;
12475 /* If we get a real template back, return it. This can happen in
12476 the context of most_specialized_partial_spec. */
12477 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12478 r = new_type;
12479 else
12480 /* The new TEMPLATE_DECL was built in
12481 reduce_template_parm_level. */
12482 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12483 return r;
12486 if (!lambda_fntype)
12488 /* We might already have an instance of this template.
12489 The ARGS are for the surrounding class type, so the
12490 full args contain the tsubst'd args for the context,
12491 plus the innermost args from the template decl. */
12492 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12493 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12494 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12495 /* Because this is a template, the arguments will still be
12496 dependent, even after substitution. If
12497 PROCESSING_TEMPLATE_DECL is not set, the dependency
12498 predicates will short-circuit. */
12499 ++processing_template_decl;
12500 full_args = tsubst_template_args (tmpl_args, args,
12501 complain, in_decl);
12502 --processing_template_decl;
12503 if (full_args == error_mark_node)
12504 return error_mark_node;
12506 /* If this is a default template template argument,
12507 tsubst might not have changed anything. */
12508 if (full_args == tmpl_args)
12509 return t;
12511 hash = hash_tmpl_and_args (t, full_args);
12512 spec = retrieve_specialization (t, full_args, hash);
12513 if (spec != NULL_TREE)
12514 return spec;
12517 /* Make a new template decl. It will be similar to the
12518 original, but will record the current template arguments.
12519 We also create a new function declaration, which is just
12520 like the old one, but points to this new template, rather
12521 than the old one. */
12522 r = copy_decl (t);
12523 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12524 DECL_CHAIN (r) = NULL_TREE;
12526 // Build new template info linking to the original template decl.
12527 if (!lambda_fntype)
12529 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12530 SET_DECL_IMPLICIT_INSTANTIATION (r);
12532 else
12533 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12535 /* The template parameters for this new template are all the
12536 template parameters for the old template, except the
12537 outermost level of parameters. */
12538 DECL_TEMPLATE_PARMS (r)
12539 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12540 complain);
12542 if (TREE_CODE (decl) == TYPE_DECL
12543 && !TYPE_DECL_ALIAS_P (decl))
12545 tree new_type;
12546 ++processing_template_decl;
12547 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12548 --processing_template_decl;
12549 if (new_type == error_mark_node)
12550 return error_mark_node;
12552 TREE_TYPE (r) = new_type;
12553 /* For a partial specialization, we need to keep pointing to
12554 the primary template. */
12555 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12556 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12557 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12558 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12559 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12561 else
12563 tree new_decl;
12564 ++processing_template_decl;
12565 if (TREE_CODE (decl) == FUNCTION_DECL)
12566 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12567 else
12568 new_decl = tsubst (decl, args, complain, in_decl);
12569 --processing_template_decl;
12570 if (new_decl == error_mark_node)
12571 return error_mark_node;
12573 DECL_TEMPLATE_RESULT (r) = new_decl;
12574 TREE_TYPE (r) = TREE_TYPE (new_decl);
12575 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12576 if (lambda_fntype)
12578 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12579 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12581 else
12583 DECL_TI_TEMPLATE (new_decl) = r;
12584 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12588 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12589 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12591 if (PRIMARY_TEMPLATE_P (t))
12592 DECL_PRIMARY_TEMPLATE (r) = r;
12594 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12595 && !lambda_fntype)
12596 /* Record this non-type partial instantiation. */
12597 register_specialization (r, t,
12598 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12599 false, hash);
12601 return r;
12604 /* True if FN is the op() for a lambda in an uninstantiated template. */
12606 bool
12607 lambda_fn_in_template_p (tree fn)
12609 if (!fn || !LAMBDA_FUNCTION_P (fn))
12610 return false;
12611 tree closure = DECL_CONTEXT (fn);
12612 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12615 /* True if FN is the op() for a lambda regenerated from a lambda in an
12616 uninstantiated template. */
12618 bool
12619 regenerated_lambda_fn_p (tree fn)
12621 return (LAMBDA_FUNCTION_P (fn)
12622 && !DECL_TEMPLATE_INSTANTIATION (fn));
12625 /* We're instantiating a variable from template function TCTX. Return the
12626 corresponding current enclosing scope. This gets complicated because lambda
12627 functions in templates are regenerated rather than instantiated, but generic
12628 lambda functions are subsequently instantiated. */
12630 static tree
12631 enclosing_instantiation_of (tree tctx)
12633 tree fn = current_function_decl;
12634 int lambda_count = 0;
12636 for (; tctx && lambda_fn_in_template_p (tctx);
12637 tctx = decl_function_context (tctx))
12638 ++lambda_count;
12639 for (; fn; fn = decl_function_context (fn))
12641 tree lambda = fn;
12642 int flambda_count = 0;
12643 for (; fn && regenerated_lambda_fn_p (fn);
12644 fn = decl_function_context (fn))
12645 ++flambda_count;
12646 if (DECL_TEMPLATE_INFO (fn)
12647 ? most_general_template (fn) != most_general_template (tctx)
12648 : fn != tctx)
12649 continue;
12650 if (lambda_count)
12652 fn = lambda;
12653 while (flambda_count-- > lambda_count)
12654 fn = decl_function_context (fn);
12656 return fn;
12658 gcc_unreachable ();
12661 /* Substitute the ARGS into the T, which is a _DECL. Return the
12662 result of the substitution. Issue error and warning messages under
12663 control of COMPLAIN. */
12665 static tree
12666 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12668 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12669 location_t saved_loc;
12670 tree r = NULL_TREE;
12671 tree in_decl = t;
12672 hashval_t hash = 0;
12674 /* Set the filename and linenumber to improve error-reporting. */
12675 saved_loc = input_location;
12676 input_location = DECL_SOURCE_LOCATION (t);
12678 switch (TREE_CODE (t))
12680 case TEMPLATE_DECL:
12681 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12682 break;
12684 case FUNCTION_DECL:
12685 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12686 break;
12688 case PARM_DECL:
12690 tree type = NULL_TREE;
12691 int i, len = 1;
12692 tree expanded_types = NULL_TREE;
12693 tree prev_r = NULL_TREE;
12694 tree first_r = NULL_TREE;
12696 if (DECL_PACK_P (t))
12698 /* If there is a local specialization that isn't a
12699 parameter pack, it means that we're doing a "simple"
12700 substitution from inside tsubst_pack_expansion. Just
12701 return the local specialization (which will be a single
12702 parm). */
12703 tree spec = retrieve_local_specialization (t);
12704 if (spec
12705 && TREE_CODE (spec) == PARM_DECL
12706 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12707 RETURN (spec);
12709 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12710 the parameters in this function parameter pack. */
12711 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12712 complain, in_decl);
12713 if (TREE_CODE (expanded_types) == TREE_VEC)
12715 len = TREE_VEC_LENGTH (expanded_types);
12717 /* Zero-length parameter packs are boring. Just substitute
12718 into the chain. */
12719 if (len == 0)
12720 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12721 TREE_CHAIN (t)));
12723 else
12725 /* All we did was update the type. Make a note of that. */
12726 type = expanded_types;
12727 expanded_types = NULL_TREE;
12731 /* Loop through all of the parameters we'll build. When T is
12732 a function parameter pack, LEN is the number of expanded
12733 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12734 r = NULL_TREE;
12735 for (i = 0; i < len; ++i)
12737 prev_r = r;
12738 r = copy_node (t);
12739 if (DECL_TEMPLATE_PARM_P (t))
12740 SET_DECL_TEMPLATE_PARM_P (r);
12742 if (expanded_types)
12743 /* We're on the Ith parameter of the function parameter
12744 pack. */
12746 /* Get the Ith type. */
12747 type = TREE_VEC_ELT (expanded_types, i);
12749 /* Rename the parameter to include the index. */
12750 DECL_NAME (r)
12751 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12753 else if (!type)
12754 /* We're dealing with a normal parameter. */
12755 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12757 type = type_decays_to (type);
12758 TREE_TYPE (r) = type;
12759 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12761 if (DECL_INITIAL (r))
12763 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12764 DECL_INITIAL (r) = TREE_TYPE (r);
12765 else
12766 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12767 complain, in_decl);
12770 DECL_CONTEXT (r) = NULL_TREE;
12772 if (!DECL_TEMPLATE_PARM_P (r))
12773 DECL_ARG_TYPE (r) = type_passed_as (type);
12775 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12776 args, complain, in_decl);
12778 /* Keep track of the first new parameter we
12779 generate. That's what will be returned to the
12780 caller. */
12781 if (!first_r)
12782 first_r = r;
12784 /* Build a proper chain of parameters when substituting
12785 into a function parameter pack. */
12786 if (prev_r)
12787 DECL_CHAIN (prev_r) = r;
12790 /* If cp_unevaluated_operand is set, we're just looking for a
12791 single dummy parameter, so don't keep going. */
12792 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12793 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12794 complain, DECL_CHAIN (t));
12796 /* FIRST_R contains the start of the chain we've built. */
12797 r = first_r;
12799 break;
12801 case FIELD_DECL:
12803 tree type = NULL_TREE;
12804 tree vec = NULL_TREE;
12805 tree expanded_types = NULL_TREE;
12806 int len = 1;
12808 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12810 /* This field is a lambda capture pack. Return a TREE_VEC of
12811 the expanded fields to instantiate_class_template_1 and
12812 store them in the specializations hash table as a
12813 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12814 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12815 complain, in_decl);
12816 if (TREE_CODE (expanded_types) == TREE_VEC)
12818 len = TREE_VEC_LENGTH (expanded_types);
12819 vec = make_tree_vec (len);
12821 else
12823 /* All we did was update the type. Make a note of that. */
12824 type = expanded_types;
12825 expanded_types = NULL_TREE;
12829 for (int i = 0; i < len; ++i)
12831 r = copy_decl (t);
12832 if (expanded_types)
12834 type = TREE_VEC_ELT (expanded_types, i);
12835 DECL_NAME (r)
12836 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12838 else if (!type)
12839 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12841 if (type == error_mark_node)
12842 RETURN (error_mark_node);
12843 TREE_TYPE (r) = type;
12844 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12846 if (DECL_C_BIT_FIELD (r))
12847 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12848 number of bits. */
12849 DECL_BIT_FIELD_REPRESENTATIVE (r)
12850 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12851 complain, in_decl,
12852 /*integral_constant_expression_p=*/true);
12853 if (DECL_INITIAL (t))
12855 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12856 NSDMI in perform_member_init. Still set DECL_INITIAL
12857 so that we know there is one. */
12858 DECL_INITIAL (r) = void_node;
12859 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12860 retrofit_lang_decl (r);
12861 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12863 /* We don't have to set DECL_CONTEXT here; it is set by
12864 finish_member_declaration. */
12865 DECL_CHAIN (r) = NULL_TREE;
12867 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12868 args, complain, in_decl);
12870 if (vec)
12871 TREE_VEC_ELT (vec, i) = r;
12874 if (vec)
12876 r = vec;
12877 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12878 SET_ARGUMENT_PACK_ARGS (pack, vec);
12879 register_specialization (pack, t, args, false, 0);
12882 break;
12884 case USING_DECL:
12885 /* We reach here only for member using decls. We also need to check
12886 uses_template_parms because DECL_DEPENDENT_P is not set for a
12887 using-declaration that designates a member of the current
12888 instantiation (c++/53549). */
12889 if (DECL_DEPENDENT_P (t)
12890 || uses_template_parms (USING_DECL_SCOPE (t)))
12892 tree scope = USING_DECL_SCOPE (t);
12893 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12894 if (PACK_EXPANSION_P (scope))
12896 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12897 int len = TREE_VEC_LENGTH (vec);
12898 r = make_tree_vec (len);
12899 for (int i = 0; i < len; ++i)
12901 tree escope = TREE_VEC_ELT (vec, i);
12902 tree elt = do_class_using_decl (escope, name);
12903 if (!elt)
12905 r = error_mark_node;
12906 break;
12908 else
12910 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12911 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12913 TREE_VEC_ELT (r, i) = elt;
12916 else
12918 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12919 complain, in_decl);
12920 r = do_class_using_decl (inst_scope, name);
12921 if (!r)
12922 r = error_mark_node;
12923 else
12925 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12926 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12930 else
12932 r = copy_node (t);
12933 DECL_CHAIN (r) = NULL_TREE;
12935 break;
12937 case TYPE_DECL:
12938 case VAR_DECL:
12940 tree argvec = NULL_TREE;
12941 tree gen_tmpl = NULL_TREE;
12942 tree spec;
12943 tree tmpl = NULL_TREE;
12944 tree ctx;
12945 tree type = NULL_TREE;
12946 bool local_p;
12948 if (TREE_TYPE (t) == error_mark_node)
12949 RETURN (error_mark_node);
12951 if (TREE_CODE (t) == TYPE_DECL
12952 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12954 /* If this is the canonical decl, we don't have to
12955 mess with instantiations, and often we can't (for
12956 typename, template type parms and such). Note that
12957 TYPE_NAME is not correct for the above test if
12958 we've copied the type for a typedef. */
12959 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12960 if (type == error_mark_node)
12961 RETURN (error_mark_node);
12962 r = TYPE_NAME (type);
12963 break;
12966 /* Check to see if we already have the specialization we
12967 need. */
12968 spec = NULL_TREE;
12969 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12971 /* T is a static data member or namespace-scope entity.
12972 We have to substitute into namespace-scope variables
12973 (not just variable templates) because of cases like:
12975 template <class T> void f() { extern T t; }
12977 where the entity referenced is not known until
12978 instantiation time. */
12979 local_p = false;
12980 ctx = DECL_CONTEXT (t);
12981 if (DECL_CLASS_SCOPE_P (t))
12983 ctx = tsubst_aggr_type (ctx, args,
12984 complain,
12985 in_decl, /*entering_scope=*/1);
12986 /* If CTX is unchanged, then T is in fact the
12987 specialization we want. That situation occurs when
12988 referencing a static data member within in its own
12989 class. We can use pointer equality, rather than
12990 same_type_p, because DECL_CONTEXT is always
12991 canonical... */
12992 if (ctx == DECL_CONTEXT (t)
12993 /* ... unless T is a member template; in which
12994 case our caller can be willing to create a
12995 specialization of that template represented
12996 by T. */
12997 && !(DECL_TI_TEMPLATE (t)
12998 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12999 spec = t;
13002 if (!spec)
13004 tmpl = DECL_TI_TEMPLATE (t);
13005 gen_tmpl = most_general_template (tmpl);
13006 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13007 if (argvec != error_mark_node)
13008 argvec = (coerce_innermost_template_parms
13009 (DECL_TEMPLATE_PARMS (gen_tmpl),
13010 argvec, t, complain,
13011 /*all*/true, /*defarg*/true));
13012 if (argvec == error_mark_node)
13013 RETURN (error_mark_node);
13014 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13015 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13018 else
13020 /* A local variable. */
13021 local_p = true;
13022 /* Subsequent calls to pushdecl will fill this in. */
13023 ctx = NULL_TREE;
13024 /* Unless this is a reference to a static variable from an
13025 enclosing function, in which case we need to fill it in now. */
13026 if (TREE_STATIC (t))
13028 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13029 if (fn != current_function_decl)
13030 ctx = fn;
13032 spec = retrieve_local_specialization (t);
13034 /* If we already have the specialization we need, there is
13035 nothing more to do. */
13036 if (spec)
13038 r = spec;
13039 break;
13042 /* Create a new node for the specialization we need. */
13043 r = copy_decl (t);
13044 if (type == NULL_TREE)
13046 if (is_typedef_decl (t))
13047 type = DECL_ORIGINAL_TYPE (t);
13048 else
13049 type = TREE_TYPE (t);
13050 if (VAR_P (t)
13051 && VAR_HAD_UNKNOWN_BOUND (t)
13052 && type != error_mark_node)
13053 type = strip_array_domain (type);
13054 tree sub_args = args;
13055 if (tree auto_node = type_uses_auto (type))
13057 /* Mask off any template args past the variable's context so we
13058 don't replace the auto with an unrelated argument. */
13059 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13060 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13061 if (extra > 0)
13062 /* This should never happen with the new lambda instantiation
13063 model, but keep the handling just in case. */
13064 gcc_assert (!CHECKING_P),
13065 sub_args = strip_innermost_template_args (args, extra);
13067 type = tsubst (type, sub_args, complain, in_decl);
13069 if (VAR_P (r))
13071 /* Even if the original location is out of scope, the
13072 newly substituted one is not. */
13073 DECL_DEAD_FOR_LOCAL (r) = 0;
13074 DECL_INITIALIZED_P (r) = 0;
13075 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13076 if (type == error_mark_node)
13077 RETURN (error_mark_node);
13078 if (TREE_CODE (type) == FUNCTION_TYPE)
13080 /* It may seem that this case cannot occur, since:
13082 typedef void f();
13083 void g() { f x; }
13085 declares a function, not a variable. However:
13087 typedef void f();
13088 template <typename T> void g() { T t; }
13089 template void g<f>();
13091 is an attempt to declare a variable with function
13092 type. */
13093 error ("variable %qD has function type",
13094 /* R is not yet sufficiently initialized, so we
13095 just use its name. */
13096 DECL_NAME (r));
13097 RETURN (error_mark_node);
13099 type = complete_type (type);
13100 /* Wait until cp_finish_decl to set this again, to handle
13101 circular dependency (template/instantiate6.C). */
13102 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13103 type = check_var_type (DECL_NAME (r), type);
13105 if (DECL_HAS_VALUE_EXPR_P (t))
13107 tree ve = DECL_VALUE_EXPR (t);
13108 ve = tsubst_expr (ve, args, complain, in_decl,
13109 /*constant_expression_p=*/false);
13110 if (REFERENCE_REF_P (ve))
13112 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13113 ve = TREE_OPERAND (ve, 0);
13115 SET_DECL_VALUE_EXPR (r, ve);
13117 if (CP_DECL_THREAD_LOCAL_P (r)
13118 && !processing_template_decl)
13119 set_decl_tls_model (r, decl_default_tls_model (r));
13121 else if (DECL_SELF_REFERENCE_P (t))
13122 SET_DECL_SELF_REFERENCE_P (r);
13123 TREE_TYPE (r) = type;
13124 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13125 DECL_CONTEXT (r) = ctx;
13126 /* Clear out the mangled name and RTL for the instantiation. */
13127 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13128 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13129 SET_DECL_RTL (r, NULL);
13130 /* The initializer must not be expanded until it is required;
13131 see [temp.inst]. */
13132 DECL_INITIAL (r) = NULL_TREE;
13133 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13134 if (VAR_P (r))
13136 SET_DECL_MODE (r, VOIDmode);
13138 /* Possibly limit visibility based on template args. */
13139 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13140 if (DECL_VISIBILITY_SPECIFIED (t))
13142 DECL_VISIBILITY_SPECIFIED (r) = 0;
13143 DECL_ATTRIBUTES (r)
13144 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13146 determine_visibility (r);
13149 if (!local_p)
13151 /* A static data member declaration is always marked
13152 external when it is declared in-class, even if an
13153 initializer is present. We mimic the non-template
13154 processing here. */
13155 DECL_EXTERNAL (r) = 1;
13156 if (DECL_NAMESPACE_SCOPE_P (t))
13157 DECL_NOT_REALLY_EXTERN (r) = 1;
13159 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13160 SET_DECL_IMPLICIT_INSTANTIATION (r);
13161 register_specialization (r, gen_tmpl, argvec, false, hash);
13163 else
13165 if (DECL_LANG_SPECIFIC (r))
13166 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13167 if (!cp_unevaluated_operand)
13168 register_local_specialization (r, t);
13171 DECL_CHAIN (r) = NULL_TREE;
13173 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13174 /*flags=*/0,
13175 args, complain, in_decl);
13177 /* Preserve a typedef that names a type. */
13178 if (is_typedef_decl (r) && type != error_mark_node)
13180 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13181 set_underlying_type (r);
13182 if (TYPE_DECL_ALIAS_P (r))
13183 /* An alias template specialization can be dependent
13184 even if its underlying type is not. */
13185 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13188 layout_decl (r, 0);
13190 break;
13192 default:
13193 gcc_unreachable ();
13195 #undef RETURN
13197 out:
13198 /* Restore the file and line information. */
13199 input_location = saved_loc;
13201 return r;
13204 /* Substitute into the ARG_TYPES of a function type.
13205 If END is a TREE_CHAIN, leave it and any following types
13206 un-substituted. */
13208 static tree
13209 tsubst_arg_types (tree arg_types,
13210 tree args,
13211 tree end,
13212 tsubst_flags_t complain,
13213 tree in_decl)
13215 tree remaining_arg_types;
13216 tree type = NULL_TREE;
13217 int i = 1;
13218 tree expanded_args = NULL_TREE;
13219 tree default_arg;
13221 if (!arg_types || arg_types == void_list_node || arg_types == end)
13222 return arg_types;
13224 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13225 args, end, complain, in_decl);
13226 if (remaining_arg_types == error_mark_node)
13227 return error_mark_node;
13229 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13231 /* For a pack expansion, perform substitution on the
13232 entire expression. Later on, we'll handle the arguments
13233 one-by-one. */
13234 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13235 args, complain, in_decl);
13237 if (TREE_CODE (expanded_args) == TREE_VEC)
13238 /* So that we'll spin through the parameters, one by one. */
13239 i = TREE_VEC_LENGTH (expanded_args);
13240 else
13242 /* We only partially substituted into the parameter
13243 pack. Our type is TYPE_PACK_EXPANSION. */
13244 type = expanded_args;
13245 expanded_args = NULL_TREE;
13249 while (i > 0) {
13250 --i;
13252 if (expanded_args)
13253 type = TREE_VEC_ELT (expanded_args, i);
13254 else if (!type)
13255 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13257 if (type == error_mark_node)
13258 return error_mark_node;
13259 if (VOID_TYPE_P (type))
13261 if (complain & tf_error)
13263 error ("invalid parameter type %qT", type);
13264 if (in_decl)
13265 error ("in declaration %q+D", in_decl);
13267 return error_mark_node;
13269 /* DR 657. */
13270 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13271 return error_mark_node;
13273 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13274 top-level qualifiers as required. */
13275 type = cv_unqualified (type_decays_to (type));
13277 /* We do not substitute into default arguments here. The standard
13278 mandates that they be instantiated only when needed, which is
13279 done in build_over_call. */
13280 default_arg = TREE_PURPOSE (arg_types);
13282 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13283 since the new op() won't have any associated template arguments for us
13284 to refer to later. */
13285 if (lambda_fn_in_template_p (in_decl))
13286 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13287 false/*fn*/, false/*constexpr*/);
13289 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13291 /* We've instantiated a template before its default arguments
13292 have been parsed. This can happen for a nested template
13293 class, and is not an error unless we require the default
13294 argument in a call of this function. */
13295 remaining_arg_types =
13296 tree_cons (default_arg, type, remaining_arg_types);
13297 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13299 else
13300 remaining_arg_types =
13301 hash_tree_cons (default_arg, type, remaining_arg_types);
13304 return remaining_arg_types;
13307 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13308 *not* handle the exception-specification for FNTYPE, because the
13309 initial substitution of explicitly provided template parameters
13310 during argument deduction forbids substitution into the
13311 exception-specification:
13313 [temp.deduct]
13315 All references in the function type of the function template to the
13316 corresponding template parameters are replaced by the specified tem-
13317 plate argument values. If a substitution in a template parameter or
13318 in the function type of the function template results in an invalid
13319 type, type deduction fails. [Note: The equivalent substitution in
13320 exception specifications is done only when the function is instanti-
13321 ated, at which point a program is ill-formed if the substitution
13322 results in an invalid type.] */
13324 static tree
13325 tsubst_function_type (tree t,
13326 tree args,
13327 tsubst_flags_t complain,
13328 tree in_decl)
13330 tree return_type;
13331 tree arg_types = NULL_TREE;
13332 tree fntype;
13334 /* The TYPE_CONTEXT is not used for function/method types. */
13335 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13337 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13338 failure. */
13339 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13341 if (late_return_type_p)
13343 /* Substitute the argument types. */
13344 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13345 complain, in_decl);
13346 if (arg_types == error_mark_node)
13347 return error_mark_node;
13349 tree save_ccp = current_class_ptr;
13350 tree save_ccr = current_class_ref;
13351 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13352 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13353 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13354 if (do_inject)
13356 /* DR 1207: 'this' is in scope in the trailing return type. */
13357 inject_this_parameter (this_type, cp_type_quals (this_type));
13360 /* Substitute the return type. */
13361 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13363 if (do_inject)
13365 current_class_ptr = save_ccp;
13366 current_class_ref = save_ccr;
13369 else
13370 /* Substitute the return type. */
13371 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13373 if (return_type == error_mark_node)
13374 return error_mark_node;
13375 /* DR 486 clarifies that creation of a function type with an
13376 invalid return type is a deduction failure. */
13377 if (TREE_CODE (return_type) == ARRAY_TYPE
13378 || TREE_CODE (return_type) == FUNCTION_TYPE)
13380 if (complain & tf_error)
13382 if (TREE_CODE (return_type) == ARRAY_TYPE)
13383 error ("function returning an array");
13384 else
13385 error ("function returning a function");
13387 return error_mark_node;
13389 /* And DR 657. */
13390 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13391 return error_mark_node;
13393 if (!late_return_type_p)
13395 /* Substitute the argument types. */
13396 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13397 complain, in_decl);
13398 if (arg_types == error_mark_node)
13399 return error_mark_node;
13402 /* Construct a new type node and return it. */
13403 if (TREE_CODE (t) == FUNCTION_TYPE)
13405 fntype = build_function_type (return_type, arg_types);
13406 fntype = apply_memfn_quals (fntype,
13407 type_memfn_quals (t),
13408 type_memfn_rqual (t));
13410 else
13412 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13413 /* Don't pick up extra function qualifiers from the basetype. */
13414 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13415 if (! MAYBE_CLASS_TYPE_P (r))
13417 /* [temp.deduct]
13419 Type deduction may fail for any of the following
13420 reasons:
13422 -- Attempting to create "pointer to member of T" when T
13423 is not a class type. */
13424 if (complain & tf_error)
13425 error ("creating pointer to member function of non-class type %qT",
13427 return error_mark_node;
13430 fntype = build_method_type_directly (r, return_type,
13431 TREE_CHAIN (arg_types));
13432 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13434 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13436 if (late_return_type_p)
13437 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13439 return fntype;
13442 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13443 ARGS into that specification, and return the substituted
13444 specification. If there is no specification, return NULL_TREE. */
13446 static tree
13447 tsubst_exception_specification (tree fntype,
13448 tree args,
13449 tsubst_flags_t complain,
13450 tree in_decl,
13451 bool defer_ok)
13453 tree specs;
13454 tree new_specs;
13456 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13457 new_specs = NULL_TREE;
13458 if (specs && TREE_PURPOSE (specs))
13460 /* A noexcept-specifier. */
13461 tree expr = TREE_PURPOSE (specs);
13462 if (TREE_CODE (expr) == INTEGER_CST)
13463 new_specs = expr;
13464 else if (defer_ok)
13466 /* Defer instantiation of noexcept-specifiers to avoid
13467 excessive instantiations (c++/49107). */
13468 new_specs = make_node (DEFERRED_NOEXCEPT);
13469 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13471 /* We already partially instantiated this member template,
13472 so combine the new args with the old. */
13473 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13474 = DEFERRED_NOEXCEPT_PATTERN (expr);
13475 DEFERRED_NOEXCEPT_ARGS (new_specs)
13476 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13478 else
13480 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13481 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13484 else
13485 new_specs = tsubst_copy_and_build
13486 (expr, args, complain, in_decl, /*function_p=*/false,
13487 /*integral_constant_expression_p=*/true);
13488 new_specs = build_noexcept_spec (new_specs, complain);
13490 else if (specs)
13492 if (! TREE_VALUE (specs))
13493 new_specs = specs;
13494 else
13495 while (specs)
13497 tree spec;
13498 int i, len = 1;
13499 tree expanded_specs = NULL_TREE;
13501 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13503 /* Expand the pack expansion type. */
13504 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13505 args, complain,
13506 in_decl);
13508 if (expanded_specs == error_mark_node)
13509 return error_mark_node;
13510 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13511 len = TREE_VEC_LENGTH (expanded_specs);
13512 else
13514 /* We're substituting into a member template, so
13515 we got a TYPE_PACK_EXPANSION back. Add that
13516 expansion and move on. */
13517 gcc_assert (TREE_CODE (expanded_specs)
13518 == TYPE_PACK_EXPANSION);
13519 new_specs = add_exception_specifier (new_specs,
13520 expanded_specs,
13521 complain);
13522 specs = TREE_CHAIN (specs);
13523 continue;
13527 for (i = 0; i < len; ++i)
13529 if (expanded_specs)
13530 spec = TREE_VEC_ELT (expanded_specs, i);
13531 else
13532 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13533 if (spec == error_mark_node)
13534 return spec;
13535 new_specs = add_exception_specifier (new_specs, spec,
13536 complain);
13539 specs = TREE_CHAIN (specs);
13542 return new_specs;
13545 /* Take the tree structure T and replace template parameters used
13546 therein with the argument vector ARGS. IN_DECL is an associated
13547 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13548 Issue error and warning messages under control of COMPLAIN. Note
13549 that we must be relatively non-tolerant of extensions here, in
13550 order to preserve conformance; if we allow substitutions that
13551 should not be allowed, we may allow argument deductions that should
13552 not succeed, and therefore report ambiguous overload situations
13553 where there are none. In theory, we could allow the substitution,
13554 but indicate that it should have failed, and allow our caller to
13555 make sure that the right thing happens, but we don't try to do this
13556 yet.
13558 This function is used for dealing with types, decls and the like;
13559 for expressions, use tsubst_expr or tsubst_copy. */
13561 tree
13562 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13564 enum tree_code code;
13565 tree type, r = NULL_TREE;
13567 if (t == NULL_TREE || t == error_mark_node
13568 || t == integer_type_node
13569 || t == void_type_node
13570 || t == char_type_node
13571 || t == unknown_type_node
13572 || TREE_CODE (t) == NAMESPACE_DECL
13573 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13574 return t;
13576 if (DECL_P (t))
13577 return tsubst_decl (t, args, complain);
13579 if (args == NULL_TREE)
13580 return t;
13582 code = TREE_CODE (t);
13584 if (code == IDENTIFIER_NODE)
13585 type = IDENTIFIER_TYPE_VALUE (t);
13586 else
13587 type = TREE_TYPE (t);
13589 gcc_assert (type != unknown_type_node);
13591 /* Reuse typedefs. We need to do this to handle dependent attributes,
13592 such as attribute aligned. */
13593 if (TYPE_P (t)
13594 && typedef_variant_p (t))
13596 tree decl = TYPE_NAME (t);
13598 if (alias_template_specialization_p (t))
13600 /* DECL represents an alias template and we want to
13601 instantiate it. */
13602 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13603 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13604 r = instantiate_alias_template (tmpl, gen_args, complain);
13606 else if (DECL_CLASS_SCOPE_P (decl)
13607 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13608 && uses_template_parms (DECL_CONTEXT (decl)))
13610 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13611 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13612 r = retrieve_specialization (tmpl, gen_args, 0);
13614 else if (DECL_FUNCTION_SCOPE_P (decl)
13615 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13616 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13617 r = retrieve_local_specialization (decl);
13618 else
13619 /* The typedef is from a non-template context. */
13620 return t;
13622 if (r)
13624 r = TREE_TYPE (r);
13625 r = cp_build_qualified_type_real
13626 (r, cp_type_quals (t) | cp_type_quals (r),
13627 complain | tf_ignore_bad_quals);
13628 return r;
13630 else
13632 /* We don't have an instantiation yet, so drop the typedef. */
13633 int quals = cp_type_quals (t);
13634 t = DECL_ORIGINAL_TYPE (decl);
13635 t = cp_build_qualified_type_real (t, quals,
13636 complain | tf_ignore_bad_quals);
13640 bool fndecl_type = (complain & tf_fndecl_type);
13641 complain &= ~tf_fndecl_type;
13643 if (type
13644 && code != TYPENAME_TYPE
13645 && code != TEMPLATE_TYPE_PARM
13646 && code != TEMPLATE_PARM_INDEX
13647 && code != IDENTIFIER_NODE
13648 && code != FUNCTION_TYPE
13649 && code != METHOD_TYPE)
13650 type = tsubst (type, args, complain, in_decl);
13651 if (type == error_mark_node)
13652 return error_mark_node;
13654 switch (code)
13656 case RECORD_TYPE:
13657 case UNION_TYPE:
13658 case ENUMERAL_TYPE:
13659 return tsubst_aggr_type (t, args, complain, in_decl,
13660 /*entering_scope=*/0);
13662 case ERROR_MARK:
13663 case IDENTIFIER_NODE:
13664 case VOID_TYPE:
13665 case REAL_TYPE:
13666 case COMPLEX_TYPE:
13667 case VECTOR_TYPE:
13668 case BOOLEAN_TYPE:
13669 case NULLPTR_TYPE:
13670 case LANG_TYPE:
13671 return t;
13673 case INTEGER_TYPE:
13674 if (t == integer_type_node)
13675 return t;
13677 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13678 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13679 return t;
13682 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13684 max = tsubst_expr (omax, args, complain, in_decl,
13685 /*integral_constant_expression_p=*/false);
13687 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13688 needed. */
13689 if (TREE_CODE (max) == NOP_EXPR
13690 && TREE_SIDE_EFFECTS (omax)
13691 && !TREE_TYPE (max))
13692 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13694 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13695 with TREE_SIDE_EFFECTS that indicates this is not an integral
13696 constant expression. */
13697 if (processing_template_decl
13698 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13700 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13701 TREE_SIDE_EFFECTS (max) = 1;
13704 return compute_array_index_type (NULL_TREE, max, complain);
13707 case TEMPLATE_TYPE_PARM:
13708 case TEMPLATE_TEMPLATE_PARM:
13709 case BOUND_TEMPLATE_TEMPLATE_PARM:
13710 case TEMPLATE_PARM_INDEX:
13712 int idx;
13713 int level;
13714 int levels;
13715 tree arg = NULL_TREE;
13717 /* Early in template argument deduction substitution, we don't
13718 want to reduce the level of 'auto', or it will be confused
13719 with a normal template parm in subsequent deduction. */
13720 if (is_auto (t) && (complain & tf_partial))
13721 return t;
13723 r = NULL_TREE;
13725 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13726 template_parm_level_and_index (t, &level, &idx);
13728 levels = TMPL_ARGS_DEPTH (args);
13729 if (level <= levels
13730 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13732 arg = TMPL_ARG (args, level, idx);
13734 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13736 /* See through ARGUMENT_PACK_SELECT arguments. */
13737 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13738 /* If the selected argument is an expansion E, that most
13739 likely means we were called from
13740 gen_elem_of_pack_expansion_instantiation during the
13741 substituting of pack an argument pack (which Ith
13742 element is a pack expansion, where I is
13743 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13744 In this case, the Ith element resulting from this
13745 substituting is going to be a pack expansion, which
13746 pattern is the pattern of E. Let's return the
13747 pattern of E, and
13748 gen_elem_of_pack_expansion_instantiation will
13749 build the resulting pack expansion from it. */
13750 if (PACK_EXPANSION_P (arg))
13752 /* Make sure we aren't throwing away arg info. */
13753 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13754 arg = PACK_EXPANSION_PATTERN (arg);
13759 if (arg == error_mark_node)
13760 return error_mark_node;
13761 else if (arg != NULL_TREE)
13763 if (ARGUMENT_PACK_P (arg))
13764 /* If ARG is an argument pack, we don't actually want to
13765 perform a substitution here, because substitutions
13766 for argument packs are only done
13767 element-by-element. We can get to this point when
13768 substituting the type of a non-type template
13769 parameter pack, when that type actually contains
13770 template parameter packs from an outer template, e.g.,
13772 template<typename... Types> struct A {
13773 template<Types... Values> struct B { };
13774 }; */
13775 return t;
13777 if (code == TEMPLATE_TYPE_PARM)
13779 int quals;
13780 gcc_assert (TYPE_P (arg));
13782 quals = cp_type_quals (arg) | cp_type_quals (t);
13784 return cp_build_qualified_type_real
13785 (arg, quals, complain | tf_ignore_bad_quals);
13787 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13789 /* We are processing a type constructed from a
13790 template template parameter. */
13791 tree argvec = tsubst (TYPE_TI_ARGS (t),
13792 args, complain, in_decl);
13793 if (argvec == error_mark_node)
13794 return error_mark_node;
13796 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13797 || TREE_CODE (arg) == TEMPLATE_DECL
13798 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13800 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13801 /* Consider this code:
13803 template <template <class> class Template>
13804 struct Internal {
13805 template <class Arg> using Bind = Template<Arg>;
13808 template <template <class> class Template, class Arg>
13809 using Instantiate = Template<Arg>; //#0
13811 template <template <class> class Template,
13812 class Argument>
13813 using Bind =
13814 Instantiate<Internal<Template>::template Bind,
13815 Argument>; //#1
13817 When #1 is parsed, the
13818 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13819 parameter `Template' in #0 matches the
13820 UNBOUND_CLASS_TEMPLATE representing the argument
13821 `Internal<Template>::template Bind'; We then want
13822 to assemble the type `Bind<Argument>' that can't
13823 be fully created right now, because
13824 `Internal<Template>' not being complete, the Bind
13825 template cannot be looked up in that context. So
13826 we need to "store" `Bind<Argument>' for later
13827 when the context of Bind becomes complete. Let's
13828 store that in a TYPENAME_TYPE. */
13829 return make_typename_type (TYPE_CONTEXT (arg),
13830 build_nt (TEMPLATE_ID_EXPR,
13831 TYPE_IDENTIFIER (arg),
13832 argvec),
13833 typename_type,
13834 complain);
13836 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13837 are resolving nested-types in the signature of a
13838 member function templates. Otherwise ARG is a
13839 TEMPLATE_DECL and is the real template to be
13840 instantiated. */
13841 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13842 arg = TYPE_NAME (arg);
13844 r = lookup_template_class (arg,
13845 argvec, in_decl,
13846 DECL_CONTEXT (arg),
13847 /*entering_scope=*/0,
13848 complain);
13849 return cp_build_qualified_type_real
13850 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13852 else if (code == TEMPLATE_TEMPLATE_PARM)
13853 return arg;
13854 else
13855 /* TEMPLATE_PARM_INDEX. */
13856 return convert_from_reference (unshare_expr (arg));
13859 if (level == 1)
13860 /* This can happen during the attempted tsubst'ing in
13861 unify. This means that we don't yet have any information
13862 about the template parameter in question. */
13863 return t;
13865 /* If we get here, we must have been looking at a parm for a
13866 more deeply nested template. Make a new version of this
13867 template parameter, but with a lower level. */
13868 switch (code)
13870 case TEMPLATE_TYPE_PARM:
13871 case TEMPLATE_TEMPLATE_PARM:
13872 case BOUND_TEMPLATE_TEMPLATE_PARM:
13873 if (cp_type_quals (t))
13875 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13876 r = cp_build_qualified_type_real
13877 (r, cp_type_quals (t),
13878 complain | (code == TEMPLATE_TYPE_PARM
13879 ? tf_ignore_bad_quals : 0));
13881 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13882 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13883 && (r = (TEMPLATE_PARM_DESCENDANTS
13884 (TEMPLATE_TYPE_PARM_INDEX (t))))
13885 && (r = TREE_TYPE (r))
13886 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13887 /* Break infinite recursion when substituting the constraints
13888 of a constrained placeholder. */;
13889 else
13891 r = copy_type (t);
13892 TEMPLATE_TYPE_PARM_INDEX (r)
13893 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13894 r, levels, args, complain);
13895 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13896 TYPE_MAIN_VARIANT (r) = r;
13897 TYPE_POINTER_TO (r) = NULL_TREE;
13898 TYPE_REFERENCE_TO (r) = NULL_TREE;
13900 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13902 /* Propagate constraints on placeholders. */
13903 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13904 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13905 = tsubst_constraint (constr, args, complain, in_decl);
13906 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13908 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13909 pl = tsubst (pl, args, complain, in_decl);
13910 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13914 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13915 /* We have reduced the level of the template
13916 template parameter, but not the levels of its
13917 template parameters, so canonical_type_parameter
13918 will not be able to find the canonical template
13919 template parameter for this level. Thus, we
13920 require structural equality checking to compare
13921 TEMPLATE_TEMPLATE_PARMs. */
13922 SET_TYPE_STRUCTURAL_EQUALITY (r);
13923 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13924 SET_TYPE_STRUCTURAL_EQUALITY (r);
13925 else
13926 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13928 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13930 tree tinfo = TYPE_TEMPLATE_INFO (t);
13931 /* We might need to substitute into the types of non-type
13932 template parameters. */
13933 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13934 complain, in_decl);
13935 if (tmpl == error_mark_node)
13936 return error_mark_node;
13937 tree argvec = tsubst (TI_ARGS (tinfo), args,
13938 complain, in_decl);
13939 if (argvec == error_mark_node)
13940 return error_mark_node;
13942 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13943 = build_template_info (tmpl, argvec);
13946 break;
13948 case TEMPLATE_PARM_INDEX:
13949 /* OK, now substitute the type of the non-type parameter. We
13950 couldn't do it earlier because it might be an auto parameter,
13951 and we wouldn't need to if we had an argument. */
13952 type = tsubst (type, args, complain, in_decl);
13953 if (type == error_mark_node)
13954 return error_mark_node;
13955 r = reduce_template_parm_level (t, type, levels, args, complain);
13956 break;
13958 default:
13959 gcc_unreachable ();
13962 return r;
13965 case TREE_LIST:
13967 tree purpose, value, chain;
13969 if (t == void_list_node)
13970 return t;
13972 purpose = TREE_PURPOSE (t);
13973 if (purpose)
13975 purpose = tsubst (purpose, args, complain, in_decl);
13976 if (purpose == error_mark_node)
13977 return error_mark_node;
13979 value = TREE_VALUE (t);
13980 if (value)
13982 value = tsubst (value, args, complain, in_decl);
13983 if (value == error_mark_node)
13984 return error_mark_node;
13986 chain = TREE_CHAIN (t);
13987 if (chain && chain != void_type_node)
13989 chain = tsubst (chain, args, complain, in_decl);
13990 if (chain == error_mark_node)
13991 return error_mark_node;
13993 if (purpose == TREE_PURPOSE (t)
13994 && value == TREE_VALUE (t)
13995 && chain == TREE_CHAIN (t))
13996 return t;
13997 return hash_tree_cons (purpose, value, chain);
14000 case TREE_BINFO:
14001 /* We should never be tsubsting a binfo. */
14002 gcc_unreachable ();
14004 case TREE_VEC:
14005 /* A vector of template arguments. */
14006 gcc_assert (!type);
14007 return tsubst_template_args (t, args, complain, in_decl);
14009 case POINTER_TYPE:
14010 case REFERENCE_TYPE:
14012 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14013 return t;
14015 /* [temp.deduct]
14017 Type deduction may fail for any of the following
14018 reasons:
14020 -- Attempting to create a pointer to reference type.
14021 -- Attempting to create a reference to a reference type or
14022 a reference to void.
14024 Core issue 106 says that creating a reference to a reference
14025 during instantiation is no longer a cause for failure. We
14026 only enforce this check in strict C++98 mode. */
14027 if ((TREE_CODE (type) == REFERENCE_TYPE
14028 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14029 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14031 static location_t last_loc;
14033 /* We keep track of the last time we issued this error
14034 message to avoid spewing a ton of messages during a
14035 single bad template instantiation. */
14036 if (complain & tf_error
14037 && last_loc != input_location)
14039 if (VOID_TYPE_P (type))
14040 error ("forming reference to void");
14041 else if (code == POINTER_TYPE)
14042 error ("forming pointer to reference type %qT", type);
14043 else
14044 error ("forming reference to reference type %qT", type);
14045 last_loc = input_location;
14048 return error_mark_node;
14050 else if (TREE_CODE (type) == FUNCTION_TYPE
14051 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14052 || type_memfn_rqual (type) != REF_QUAL_NONE))
14054 if (complain & tf_error)
14056 if (code == POINTER_TYPE)
14057 error ("forming pointer to qualified function type %qT",
14058 type);
14059 else
14060 error ("forming reference to qualified function type %qT",
14061 type);
14063 return error_mark_node;
14065 else if (code == POINTER_TYPE)
14067 r = build_pointer_type (type);
14068 if (TREE_CODE (type) == METHOD_TYPE)
14069 r = build_ptrmemfunc_type (r);
14071 else if (TREE_CODE (type) == REFERENCE_TYPE)
14072 /* In C++0x, during template argument substitution, when there is an
14073 attempt to create a reference to a reference type, reference
14074 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14076 "If a template-argument for a template-parameter T names a type
14077 that is a reference to a type A, an attempt to create the type
14078 'lvalue reference to cv T' creates the type 'lvalue reference to
14079 A,' while an attempt to create the type type rvalue reference to
14080 cv T' creates the type T"
14082 r = cp_build_reference_type
14083 (TREE_TYPE (type),
14084 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14085 else
14086 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14087 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14089 if (r != error_mark_node)
14090 /* Will this ever be needed for TYPE_..._TO values? */
14091 layout_type (r);
14093 return r;
14095 case OFFSET_TYPE:
14097 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14098 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14100 /* [temp.deduct]
14102 Type deduction may fail for any of the following
14103 reasons:
14105 -- Attempting to create "pointer to member of T" when T
14106 is not a class type. */
14107 if (complain & tf_error)
14108 error ("creating pointer to member of non-class type %qT", r);
14109 return error_mark_node;
14111 if (TREE_CODE (type) == REFERENCE_TYPE)
14113 if (complain & tf_error)
14114 error ("creating pointer to member reference type %qT", type);
14115 return error_mark_node;
14117 if (VOID_TYPE_P (type))
14119 if (complain & tf_error)
14120 error ("creating pointer to member of type void");
14121 return error_mark_node;
14123 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14124 if (TREE_CODE (type) == FUNCTION_TYPE)
14126 /* The type of the implicit object parameter gets its
14127 cv-qualifiers from the FUNCTION_TYPE. */
14128 tree memptr;
14129 tree method_type
14130 = build_memfn_type (type, r, type_memfn_quals (type),
14131 type_memfn_rqual (type));
14132 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14133 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14134 complain);
14136 else
14137 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14138 cp_type_quals (t),
14139 complain);
14141 case FUNCTION_TYPE:
14142 case METHOD_TYPE:
14144 tree fntype;
14145 tree specs;
14146 fntype = tsubst_function_type (t, args, complain, in_decl);
14147 if (fntype == error_mark_node)
14148 return error_mark_node;
14150 /* Substitute the exception specification. */
14151 specs = tsubst_exception_specification (t, args, complain, in_decl,
14152 /*defer_ok*/fndecl_type);
14153 if (specs == error_mark_node)
14154 return error_mark_node;
14155 if (specs)
14156 fntype = build_exception_variant (fntype, specs);
14157 return fntype;
14159 case ARRAY_TYPE:
14161 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14162 if (domain == error_mark_node)
14163 return error_mark_node;
14165 /* As an optimization, we avoid regenerating the array type if
14166 it will obviously be the same as T. */
14167 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14168 return t;
14170 /* These checks should match the ones in create_array_type_for_decl.
14172 [temp.deduct]
14174 The deduction may fail for any of the following reasons:
14176 -- Attempting to create an array with an element type that
14177 is void, a function type, or a reference type, or [DR337]
14178 an abstract class type. */
14179 if (VOID_TYPE_P (type)
14180 || TREE_CODE (type) == FUNCTION_TYPE
14181 || (TREE_CODE (type) == ARRAY_TYPE
14182 && TYPE_DOMAIN (type) == NULL_TREE)
14183 || TREE_CODE (type) == REFERENCE_TYPE)
14185 if (complain & tf_error)
14186 error ("creating array of %qT", type);
14187 return error_mark_node;
14190 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14191 return error_mark_node;
14193 r = build_cplus_array_type (type, domain);
14195 if (TYPE_USER_ALIGN (t))
14197 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14198 TYPE_USER_ALIGN (r) = 1;
14201 return r;
14204 case TYPENAME_TYPE:
14206 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14207 in_decl, /*entering_scope=*/1);
14208 if (ctx == error_mark_node)
14209 return error_mark_node;
14211 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14212 complain, in_decl);
14213 if (f == error_mark_node)
14214 return error_mark_node;
14216 if (!MAYBE_CLASS_TYPE_P (ctx))
14218 if (complain & tf_error)
14219 error ("%qT is not a class, struct, or union type", ctx);
14220 return error_mark_node;
14222 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14224 /* Normally, make_typename_type does not require that the CTX
14225 have complete type in order to allow things like:
14227 template <class T> struct S { typename S<T>::X Y; };
14229 But, such constructs have already been resolved by this
14230 point, so here CTX really should have complete type, unless
14231 it's a partial instantiation. */
14232 ctx = complete_type (ctx);
14233 if (!COMPLETE_TYPE_P (ctx))
14235 if (complain & tf_error)
14236 cxx_incomplete_type_error (NULL_TREE, ctx);
14237 return error_mark_node;
14241 f = make_typename_type (ctx, f, typename_type,
14242 complain | tf_keep_type_decl);
14243 if (f == error_mark_node)
14244 return f;
14245 if (TREE_CODE (f) == TYPE_DECL)
14247 complain |= tf_ignore_bad_quals;
14248 f = TREE_TYPE (f);
14251 if (TREE_CODE (f) != TYPENAME_TYPE)
14253 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14255 if (complain & tf_error)
14256 error ("%qT resolves to %qT, which is not an enumeration type",
14257 t, f);
14258 else
14259 return error_mark_node;
14261 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14263 if (complain & tf_error)
14264 error ("%qT resolves to %qT, which is is not a class type",
14265 t, f);
14266 else
14267 return error_mark_node;
14271 return cp_build_qualified_type_real
14272 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14275 case UNBOUND_CLASS_TEMPLATE:
14277 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14278 in_decl, /*entering_scope=*/1);
14279 tree name = TYPE_IDENTIFIER (t);
14280 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14282 if (ctx == error_mark_node || name == error_mark_node)
14283 return error_mark_node;
14285 if (parm_list)
14286 parm_list = tsubst_template_parms (parm_list, args, complain);
14287 return make_unbound_class_template (ctx, name, parm_list, complain);
14290 case TYPEOF_TYPE:
14292 tree type;
14294 ++cp_unevaluated_operand;
14295 ++c_inhibit_evaluation_warnings;
14297 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14298 complain, in_decl,
14299 /*integral_constant_expression_p=*/false);
14301 --cp_unevaluated_operand;
14302 --c_inhibit_evaluation_warnings;
14304 type = finish_typeof (type);
14305 return cp_build_qualified_type_real (type,
14306 cp_type_quals (t)
14307 | cp_type_quals (type),
14308 complain);
14311 case DECLTYPE_TYPE:
14313 tree type;
14315 ++cp_unevaluated_operand;
14316 ++c_inhibit_evaluation_warnings;
14318 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14319 complain|tf_decltype, in_decl,
14320 /*function_p*/false,
14321 /*integral_constant_expression*/false);
14323 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14325 if (type == NULL_TREE)
14327 if (complain & tf_error)
14328 error ("empty initializer in lambda init-capture");
14329 type = error_mark_node;
14331 else if (TREE_CODE (type) == TREE_LIST)
14332 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14335 --cp_unevaluated_operand;
14336 --c_inhibit_evaluation_warnings;
14338 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14339 type = lambda_capture_field_type (type,
14340 DECLTYPE_FOR_INIT_CAPTURE (t),
14341 DECLTYPE_FOR_REF_CAPTURE (t));
14342 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14343 type = lambda_proxy_type (type);
14344 else
14346 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14347 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14348 && EXPR_P (type))
14349 /* In a template ~id could be either a complement expression
14350 or an unqualified-id naming a destructor; if instantiating
14351 it produces an expression, it's not an id-expression or
14352 member access. */
14353 id = false;
14354 type = finish_decltype_type (type, id, complain);
14356 return cp_build_qualified_type_real (type,
14357 cp_type_quals (t)
14358 | cp_type_quals (type),
14359 complain | tf_ignore_bad_quals);
14362 case UNDERLYING_TYPE:
14364 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14365 complain, in_decl);
14366 return finish_underlying_type (type);
14369 case TYPE_ARGUMENT_PACK:
14370 case NONTYPE_ARGUMENT_PACK:
14372 tree r;
14374 if (code == NONTYPE_ARGUMENT_PACK)
14375 r = make_node (code);
14376 else
14377 r = cxx_make_type (code);
14379 tree pack_args = ARGUMENT_PACK_ARGS (t);
14380 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14381 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14383 return r;
14386 case VOID_CST:
14387 case INTEGER_CST:
14388 case REAL_CST:
14389 case STRING_CST:
14390 case PLUS_EXPR:
14391 case MINUS_EXPR:
14392 case NEGATE_EXPR:
14393 case NOP_EXPR:
14394 case INDIRECT_REF:
14395 case ADDR_EXPR:
14396 case CALL_EXPR:
14397 case ARRAY_REF:
14398 case SCOPE_REF:
14399 /* We should use one of the expression tsubsts for these codes. */
14400 gcc_unreachable ();
14402 default:
14403 sorry ("use of %qs in template", get_tree_code_name (code));
14404 return error_mark_node;
14408 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14409 expression on the left-hand side of the "." or "->" operator. A
14410 baselink indicates a function from a base class. Both the
14411 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14412 bases of the template class, rather than the instantiated class.
14413 In addition, lookups that were not ambiguous before may be
14414 ambiguous now. Therefore, we perform the lookup again. */
14416 static tree
14417 tsubst_baselink (tree baselink, tree object_type,
14418 tree args, tsubst_flags_t complain, tree in_decl)
14420 bool qualified = BASELINK_QUALIFIED_P (baselink);
14422 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14423 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14425 tree optype = BASELINK_OPTYPE (baselink);
14426 optype = tsubst (optype, args, complain, in_decl);
14428 tree template_args = NULL_TREE;
14429 bool template_id_p = false;
14430 tree fns = BASELINK_FUNCTIONS (baselink);
14431 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14433 template_id_p = true;
14434 template_args = TREE_OPERAND (fns, 1);
14435 fns = TREE_OPERAND (fns, 0);
14436 if (template_args)
14437 template_args = tsubst_template_args (template_args, args,
14438 complain, in_decl);
14441 tree name = OVL_NAME (fns);
14442 if (IDENTIFIER_CONV_OP_P (name))
14443 name = make_conv_op_name (optype);
14445 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14446 if (!baselink)
14448 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14449 error ("cannot call constructor %<%T::%D%> directly",
14450 qualifying_scope, name);
14451 return error_mark_node;
14454 /* If lookup found a single function, mark it as used at this point.
14455 (If it lookup found multiple functions the one selected later by
14456 overload resolution will be marked as used at that point.) */
14457 if (BASELINK_P (baselink))
14458 fns = BASELINK_FUNCTIONS (baselink);
14459 if (!template_id_p && !really_overloaded_fn (fns)
14460 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14461 return error_mark_node;
14463 if (BASELINK_P (baselink))
14465 /* Add back the template arguments, if present. */
14466 if (template_id_p)
14467 BASELINK_FUNCTIONS (baselink)
14468 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14469 BASELINK_FUNCTIONS (baselink), template_args);
14471 /* Update the conversion operator type. */
14472 BASELINK_OPTYPE (baselink) = optype;
14475 if (!object_type)
14476 object_type = current_class_type;
14478 if (qualified || name == complete_dtor_identifier)
14480 baselink = adjust_result_of_qualified_name_lookup (baselink,
14481 qualifying_scope,
14482 object_type);
14483 if (!qualified)
14484 /* We need to call adjust_result_of_qualified_name_lookup in case the
14485 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14486 so that we still get virtual function binding. */
14487 BASELINK_QUALIFIED_P (baselink) = false;
14490 return baselink;
14493 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14494 true if the qualified-id will be a postfix-expression in-and-of
14495 itself; false if more of the postfix-expression follows the
14496 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14497 of "&". */
14499 static tree
14500 tsubst_qualified_id (tree qualified_id, tree args,
14501 tsubst_flags_t complain, tree in_decl,
14502 bool done, bool address_p)
14504 tree expr;
14505 tree scope;
14506 tree name;
14507 bool is_template;
14508 tree template_args;
14509 location_t loc = UNKNOWN_LOCATION;
14511 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14513 /* Figure out what name to look up. */
14514 name = TREE_OPERAND (qualified_id, 1);
14515 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14517 is_template = true;
14518 loc = EXPR_LOCATION (name);
14519 template_args = TREE_OPERAND (name, 1);
14520 if (template_args)
14521 template_args = tsubst_template_args (template_args, args,
14522 complain, in_decl);
14523 if (template_args == error_mark_node)
14524 return error_mark_node;
14525 name = TREE_OPERAND (name, 0);
14527 else
14529 is_template = false;
14530 template_args = NULL_TREE;
14533 /* Substitute into the qualifying scope. When there are no ARGS, we
14534 are just trying to simplify a non-dependent expression. In that
14535 case the qualifying scope may be dependent, and, in any case,
14536 substituting will not help. */
14537 scope = TREE_OPERAND (qualified_id, 0);
14538 if (args)
14540 scope = tsubst (scope, args, complain, in_decl);
14541 expr = tsubst_copy (name, args, complain, in_decl);
14543 else
14544 expr = name;
14546 if (dependent_scope_p (scope))
14548 if (is_template)
14549 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14550 tree r = build_qualified_name (NULL_TREE, scope, expr,
14551 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14552 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14553 return r;
14556 if (!BASELINK_P (name) && !DECL_P (expr))
14558 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14560 /* A BIT_NOT_EXPR is used to represent a destructor. */
14561 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14563 error ("qualifying type %qT does not match destructor name ~%qT",
14564 scope, TREE_OPERAND (expr, 0));
14565 expr = error_mark_node;
14567 else
14568 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14569 /*is_type_p=*/0, false);
14571 else
14572 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14573 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14574 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14576 if (complain & tf_error)
14578 error ("dependent-name %qE is parsed as a non-type, but "
14579 "instantiation yields a type", qualified_id);
14580 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14582 return error_mark_node;
14586 if (DECL_P (expr))
14588 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14589 scope);
14590 /* Remember that there was a reference to this entity. */
14591 if (!mark_used (expr, complain) && !(complain & tf_error))
14592 return error_mark_node;
14595 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14597 if (complain & tf_error)
14598 qualified_name_lookup_error (scope,
14599 TREE_OPERAND (qualified_id, 1),
14600 expr, input_location);
14601 return error_mark_node;
14604 if (is_template)
14606 if (variable_template_p (expr))
14607 expr = lookup_and_finish_template_variable (expr, template_args,
14608 complain);
14609 else
14610 expr = lookup_template_function (expr, template_args);
14613 if (expr == error_mark_node && complain & tf_error)
14614 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14615 expr, input_location);
14616 else if (TYPE_P (scope))
14618 expr = (adjust_result_of_qualified_name_lookup
14619 (expr, scope, current_nonlambda_class_type ()));
14620 expr = (finish_qualified_id_expr
14621 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14622 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14623 /*template_arg_p=*/false, complain));
14626 /* Expressions do not generally have reference type. */
14627 if (TREE_CODE (expr) != SCOPE_REF
14628 /* However, if we're about to form a pointer-to-member, we just
14629 want the referenced member referenced. */
14630 && TREE_CODE (expr) != OFFSET_REF)
14631 expr = convert_from_reference (expr);
14633 if (REF_PARENTHESIZED_P (qualified_id))
14634 expr = force_paren_expr (expr);
14636 return expr;
14639 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14640 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14641 for tsubst. */
14643 static tree
14644 tsubst_init (tree init, tree decl, tree args,
14645 tsubst_flags_t complain, tree in_decl)
14647 if (!init)
14648 return NULL_TREE;
14650 init = tsubst_expr (init, args, complain, in_decl, false);
14652 if (!init && TREE_TYPE (decl) != error_mark_node)
14654 /* If we had an initializer but it
14655 instantiated to nothing,
14656 value-initialize the object. This will
14657 only occur when the initializer was a
14658 pack expansion where the parameter packs
14659 used in that expansion were of length
14660 zero. */
14661 init = build_value_init (TREE_TYPE (decl),
14662 complain);
14663 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14664 init = get_target_expr_sfinae (init, complain);
14665 if (TREE_CODE (init) == TARGET_EXPR)
14666 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14669 return init;
14672 /* Like tsubst, but deals with expressions. This function just replaces
14673 template parms; to finish processing the resultant expression, use
14674 tsubst_copy_and_build or tsubst_expr. */
14676 static tree
14677 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14679 enum tree_code code;
14680 tree r;
14682 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14683 return t;
14685 code = TREE_CODE (t);
14687 switch (code)
14689 case PARM_DECL:
14690 r = retrieve_local_specialization (t);
14692 if (r == NULL_TREE)
14694 /* We get here for a use of 'this' in an NSDMI. */
14695 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14696 return current_class_ptr;
14698 /* This can happen for a parameter name used later in a function
14699 declaration (such as in a late-specified return type). Just
14700 make a dummy decl, since it's only used for its type. */
14701 gcc_assert (cp_unevaluated_operand != 0);
14702 r = tsubst_decl (t, args, complain);
14703 /* Give it the template pattern as its context; its true context
14704 hasn't been instantiated yet and this is good enough for
14705 mangling. */
14706 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14709 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14710 r = ARGUMENT_PACK_SELECT_ARG (r);
14711 if (!mark_used (r, complain) && !(complain & tf_error))
14712 return error_mark_node;
14713 return r;
14715 case CONST_DECL:
14717 tree enum_type;
14718 tree v;
14720 if (DECL_TEMPLATE_PARM_P (t))
14721 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14722 /* There is no need to substitute into namespace-scope
14723 enumerators. */
14724 if (DECL_NAMESPACE_SCOPE_P (t))
14725 return t;
14726 /* If ARGS is NULL, then T is known to be non-dependent. */
14727 if (args == NULL_TREE)
14728 return scalar_constant_value (t);
14730 /* Unfortunately, we cannot just call lookup_name here.
14731 Consider:
14733 template <int I> int f() {
14734 enum E { a = I };
14735 struct S { void g() { E e = a; } };
14738 When we instantiate f<7>::S::g(), say, lookup_name is not
14739 clever enough to find f<7>::a. */
14740 enum_type
14741 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14742 /*entering_scope=*/0);
14744 for (v = TYPE_VALUES (enum_type);
14745 v != NULL_TREE;
14746 v = TREE_CHAIN (v))
14747 if (TREE_PURPOSE (v) == DECL_NAME (t))
14748 return TREE_VALUE (v);
14750 /* We didn't find the name. That should never happen; if
14751 name-lookup found it during preliminary parsing, we
14752 should find it again here during instantiation. */
14753 gcc_unreachable ();
14755 return t;
14757 case FIELD_DECL:
14758 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14760 /* Check for a local specialization set up by
14761 tsubst_pack_expansion. */
14762 if (tree r = retrieve_local_specialization (t))
14764 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14765 r = ARGUMENT_PACK_SELECT_ARG (r);
14766 return r;
14769 /* When retrieving a capture pack from a generic lambda, remove the
14770 lambda call op's own template argument list from ARGS. Only the
14771 template arguments active for the closure type should be used to
14772 retrieve the pack specialization. */
14773 if (LAMBDA_FUNCTION_P (current_function_decl)
14774 && (template_class_depth (DECL_CONTEXT (t))
14775 != TMPL_ARGS_DEPTH (args)))
14776 args = strip_innermost_template_args (args, 1);
14778 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14779 tsubst_decl put in the hash table. */
14780 return retrieve_specialization (t, args, 0);
14783 if (DECL_CONTEXT (t))
14785 tree ctx;
14787 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14788 /*entering_scope=*/1);
14789 if (ctx != DECL_CONTEXT (t))
14791 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14792 if (!r)
14794 if (complain & tf_error)
14795 error ("using invalid field %qD", t);
14796 return error_mark_node;
14798 return r;
14802 return t;
14804 case VAR_DECL:
14805 case FUNCTION_DECL:
14806 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14807 r = tsubst (t, args, complain, in_decl);
14808 else if (local_variable_p (t)
14809 && uses_template_parms (DECL_CONTEXT (t)))
14811 r = retrieve_local_specialization (t);
14812 if (r == NULL_TREE)
14814 /* First try name lookup to find the instantiation. */
14815 r = lookup_name (DECL_NAME (t));
14816 if (r && !is_capture_proxy (r))
14818 /* Make sure that the one we found is the one we want. */
14819 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14820 if (ctx != DECL_CONTEXT (r))
14821 r = NULL_TREE;
14824 if (r)
14825 /* OK */;
14826 else
14828 /* This can happen for a variable used in a
14829 late-specified return type of a local lambda, or for a
14830 local static or constant. Building a new VAR_DECL
14831 should be OK in all those cases. */
14832 r = tsubst_decl (t, args, complain);
14833 if (local_specializations)
14834 /* Avoid infinite recursion (79640). */
14835 register_local_specialization (r, t);
14836 if (decl_maybe_constant_var_p (r))
14838 /* We can't call cp_finish_decl, so handle the
14839 initializer by hand. */
14840 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14841 complain, in_decl);
14842 if (!processing_template_decl)
14843 init = maybe_constant_init (init);
14844 if (processing_template_decl
14845 ? potential_constant_expression (init)
14846 : reduced_constant_expression_p (init))
14847 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14848 = TREE_CONSTANT (r) = true;
14849 DECL_INITIAL (r) = init;
14850 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14851 TREE_TYPE (r)
14852 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14853 complain, adc_variable_type);
14855 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14856 || decl_constant_var_p (r)
14857 || errorcount || sorrycount);
14858 if (!processing_template_decl
14859 && !TREE_STATIC (r))
14860 r = process_outer_var_ref (r, complain);
14862 /* Remember this for subsequent uses. */
14863 if (local_specializations)
14864 register_local_specialization (r, t);
14867 else
14868 r = t;
14869 if (!mark_used (r, complain))
14870 return error_mark_node;
14871 return r;
14873 case NAMESPACE_DECL:
14874 return t;
14876 case OVERLOAD:
14877 /* An OVERLOAD will always be a non-dependent overload set; an
14878 overload set from function scope will just be represented with an
14879 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14880 gcc_assert (!uses_template_parms (t));
14881 /* We must have marked any lookups as persistent. */
14882 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14883 return t;
14885 case BASELINK:
14886 return tsubst_baselink (t, current_nonlambda_class_type (),
14887 args, complain, in_decl);
14889 case TEMPLATE_DECL:
14890 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14891 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14892 args, complain, in_decl);
14893 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14894 return tsubst (t, args, complain, in_decl);
14895 else if (DECL_CLASS_SCOPE_P (t)
14896 && uses_template_parms (DECL_CONTEXT (t)))
14898 /* Template template argument like the following example need
14899 special treatment:
14901 template <template <class> class TT> struct C {};
14902 template <class T> struct D {
14903 template <class U> struct E {};
14904 C<E> c; // #1
14906 D<int> d; // #2
14908 We are processing the template argument `E' in #1 for
14909 the template instantiation #2. Originally, `E' is a
14910 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14911 have to substitute this with one having context `D<int>'. */
14913 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14914 if (dependent_scope_p (context))
14916 /* When rewriting a constructor into a deduction guide, a
14917 non-dependent name can become dependent, so memtmpl<args>
14918 becomes context::template memtmpl<args>. */
14919 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14920 return build_qualified_name (type, context, DECL_NAME (t),
14921 /*template*/true);
14923 return lookup_field (context, DECL_NAME(t), 0, false);
14925 else
14926 /* Ordinary template template argument. */
14927 return t;
14929 case CAST_EXPR:
14930 case REINTERPRET_CAST_EXPR:
14931 case CONST_CAST_EXPR:
14932 case STATIC_CAST_EXPR:
14933 case DYNAMIC_CAST_EXPR:
14934 case IMPLICIT_CONV_EXPR:
14935 case CONVERT_EXPR:
14936 case NOP_EXPR:
14938 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14939 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14940 return build1 (code, type, op0);
14943 case SIZEOF_EXPR:
14944 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14945 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14947 tree expanded, op = TREE_OPERAND (t, 0);
14948 int len = 0;
14950 if (SIZEOF_EXPR_TYPE_P (t))
14951 op = TREE_TYPE (op);
14953 ++cp_unevaluated_operand;
14954 ++c_inhibit_evaluation_warnings;
14955 /* We only want to compute the number of arguments. */
14956 if (PACK_EXPANSION_P (op))
14957 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14958 else
14959 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14960 args, complain, in_decl);
14961 --cp_unevaluated_operand;
14962 --c_inhibit_evaluation_warnings;
14964 if (TREE_CODE (expanded) == TREE_VEC)
14966 len = TREE_VEC_LENGTH (expanded);
14967 /* Set TREE_USED for the benefit of -Wunused. */
14968 for (int i = 0; i < len; i++)
14969 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14970 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14973 if (expanded == error_mark_node)
14974 return error_mark_node;
14975 else if (PACK_EXPANSION_P (expanded)
14976 || (TREE_CODE (expanded) == TREE_VEC
14977 && pack_expansion_args_count (expanded)))
14980 if (PACK_EXPANSION_P (expanded))
14981 /* OK. */;
14982 else if (TREE_VEC_LENGTH (expanded) == 1)
14983 expanded = TREE_VEC_ELT (expanded, 0);
14984 else
14985 expanded = make_argument_pack (expanded);
14987 if (TYPE_P (expanded))
14988 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14989 complain & tf_error);
14990 else
14991 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14992 complain & tf_error);
14994 else
14995 return build_int_cst (size_type_node, len);
14997 if (SIZEOF_EXPR_TYPE_P (t))
14999 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15000 args, complain, in_decl);
15001 r = build1 (NOP_EXPR, r, error_mark_node);
15002 r = build1 (SIZEOF_EXPR,
15003 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15004 SIZEOF_EXPR_TYPE_P (r) = 1;
15005 return r;
15007 /* Fall through */
15009 case INDIRECT_REF:
15010 case NEGATE_EXPR:
15011 case TRUTH_NOT_EXPR:
15012 case BIT_NOT_EXPR:
15013 case ADDR_EXPR:
15014 case UNARY_PLUS_EXPR: /* Unary + */
15015 case ALIGNOF_EXPR:
15016 case AT_ENCODE_EXPR:
15017 case ARROW_EXPR:
15018 case THROW_EXPR:
15019 case TYPEID_EXPR:
15020 case REALPART_EXPR:
15021 case IMAGPART_EXPR:
15022 case PAREN_EXPR:
15024 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15025 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15026 return build1 (code, type, op0);
15029 case COMPONENT_REF:
15031 tree object;
15032 tree name;
15034 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15035 name = TREE_OPERAND (t, 1);
15036 if (TREE_CODE (name) == BIT_NOT_EXPR)
15038 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15039 complain, in_decl);
15040 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15042 else if (TREE_CODE (name) == SCOPE_REF
15043 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15045 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15046 complain, in_decl);
15047 name = TREE_OPERAND (name, 1);
15048 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15049 complain, in_decl);
15050 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15051 name = build_qualified_name (/*type=*/NULL_TREE,
15052 base, name,
15053 /*template_p=*/false);
15055 else if (BASELINK_P (name))
15056 name = tsubst_baselink (name,
15057 non_reference (TREE_TYPE (object)),
15058 args, complain,
15059 in_decl);
15060 else
15061 name = tsubst_copy (name, args, complain, in_decl);
15062 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15065 case PLUS_EXPR:
15066 case MINUS_EXPR:
15067 case MULT_EXPR:
15068 case TRUNC_DIV_EXPR:
15069 case CEIL_DIV_EXPR:
15070 case FLOOR_DIV_EXPR:
15071 case ROUND_DIV_EXPR:
15072 case EXACT_DIV_EXPR:
15073 case BIT_AND_EXPR:
15074 case BIT_IOR_EXPR:
15075 case BIT_XOR_EXPR:
15076 case TRUNC_MOD_EXPR:
15077 case FLOOR_MOD_EXPR:
15078 case TRUTH_ANDIF_EXPR:
15079 case TRUTH_ORIF_EXPR:
15080 case TRUTH_AND_EXPR:
15081 case TRUTH_OR_EXPR:
15082 case RSHIFT_EXPR:
15083 case LSHIFT_EXPR:
15084 case RROTATE_EXPR:
15085 case LROTATE_EXPR:
15086 case EQ_EXPR:
15087 case NE_EXPR:
15088 case MAX_EXPR:
15089 case MIN_EXPR:
15090 case LE_EXPR:
15091 case GE_EXPR:
15092 case LT_EXPR:
15093 case GT_EXPR:
15094 case COMPOUND_EXPR:
15095 case DOTSTAR_EXPR:
15096 case MEMBER_REF:
15097 case PREDECREMENT_EXPR:
15098 case PREINCREMENT_EXPR:
15099 case POSTDECREMENT_EXPR:
15100 case POSTINCREMENT_EXPR:
15102 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15103 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15104 return build_nt (code, op0, op1);
15107 case SCOPE_REF:
15109 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15110 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15111 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15112 QUALIFIED_NAME_IS_TEMPLATE (t));
15115 case ARRAY_REF:
15117 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15118 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15119 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15122 case CALL_EXPR:
15124 int n = VL_EXP_OPERAND_LENGTH (t);
15125 tree result = build_vl_exp (CALL_EXPR, n);
15126 int i;
15127 for (i = 0; i < n; i++)
15128 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15129 complain, in_decl);
15130 return result;
15133 case COND_EXPR:
15134 case MODOP_EXPR:
15135 case PSEUDO_DTOR_EXPR:
15136 case VEC_PERM_EXPR:
15138 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15139 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15140 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15141 r = build_nt (code, op0, op1, op2);
15142 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15143 return r;
15146 case NEW_EXPR:
15148 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15149 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15150 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15151 r = build_nt (code, op0, op1, op2);
15152 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15153 return r;
15156 case DELETE_EXPR:
15158 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15159 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15160 r = build_nt (code, op0, op1);
15161 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15162 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15163 return r;
15166 case TEMPLATE_ID_EXPR:
15168 /* Substituted template arguments */
15169 tree fn = TREE_OPERAND (t, 0);
15170 tree targs = TREE_OPERAND (t, 1);
15172 fn = tsubst_copy (fn, args, complain, in_decl);
15173 if (targs)
15174 targs = tsubst_template_args (targs, args, complain, in_decl);
15176 return lookup_template_function (fn, targs);
15179 case TREE_LIST:
15181 tree purpose, value, chain;
15183 if (t == void_list_node)
15184 return t;
15186 purpose = TREE_PURPOSE (t);
15187 if (purpose)
15188 purpose = tsubst_copy (purpose, args, complain, in_decl);
15189 value = TREE_VALUE (t);
15190 if (value)
15191 value = tsubst_copy (value, args, complain, in_decl);
15192 chain = TREE_CHAIN (t);
15193 if (chain && chain != void_type_node)
15194 chain = tsubst_copy (chain, args, complain, in_decl);
15195 if (purpose == TREE_PURPOSE (t)
15196 && value == TREE_VALUE (t)
15197 && chain == TREE_CHAIN (t))
15198 return t;
15199 return tree_cons (purpose, value, chain);
15202 case RECORD_TYPE:
15203 case UNION_TYPE:
15204 case ENUMERAL_TYPE:
15205 case INTEGER_TYPE:
15206 case TEMPLATE_TYPE_PARM:
15207 case TEMPLATE_TEMPLATE_PARM:
15208 case BOUND_TEMPLATE_TEMPLATE_PARM:
15209 case TEMPLATE_PARM_INDEX:
15210 case POINTER_TYPE:
15211 case REFERENCE_TYPE:
15212 case OFFSET_TYPE:
15213 case FUNCTION_TYPE:
15214 case METHOD_TYPE:
15215 case ARRAY_TYPE:
15216 case TYPENAME_TYPE:
15217 case UNBOUND_CLASS_TEMPLATE:
15218 case TYPEOF_TYPE:
15219 case DECLTYPE_TYPE:
15220 case TYPE_DECL:
15221 return tsubst (t, args, complain, in_decl);
15223 case USING_DECL:
15224 t = DECL_NAME (t);
15225 /* Fall through. */
15226 case IDENTIFIER_NODE:
15227 if (IDENTIFIER_CONV_OP_P (t))
15229 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15230 return make_conv_op_name (new_type);
15232 else
15233 return t;
15235 case CONSTRUCTOR:
15236 /* This is handled by tsubst_copy_and_build. */
15237 gcc_unreachable ();
15239 case VA_ARG_EXPR:
15241 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15242 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15243 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15246 case CLEANUP_POINT_EXPR:
15247 /* We shouldn't have built any of these during initial template
15248 generation. Instead, they should be built during instantiation
15249 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15250 gcc_unreachable ();
15252 case OFFSET_REF:
15254 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15255 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15256 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15257 r = build2 (code, type, op0, op1);
15258 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15259 if (!mark_used (TREE_OPERAND (r, 1), complain)
15260 && !(complain & tf_error))
15261 return error_mark_node;
15262 return r;
15265 case EXPR_PACK_EXPANSION:
15266 error ("invalid use of pack expansion expression");
15267 return error_mark_node;
15269 case NONTYPE_ARGUMENT_PACK:
15270 error ("use %<...%> to expand argument pack");
15271 return error_mark_node;
15273 case VOID_CST:
15274 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15275 return t;
15277 case INTEGER_CST:
15278 case REAL_CST:
15279 case STRING_CST:
15280 case COMPLEX_CST:
15282 /* Instantiate any typedefs in the type. */
15283 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15284 r = fold_convert (type, t);
15285 gcc_assert (TREE_CODE (r) == code);
15286 return r;
15289 case PTRMEM_CST:
15290 /* These can sometimes show up in a partial instantiation, but never
15291 involve template parms. */
15292 gcc_assert (!uses_template_parms (t));
15293 return t;
15295 case UNARY_LEFT_FOLD_EXPR:
15296 return tsubst_unary_left_fold (t, args, complain, in_decl);
15297 case UNARY_RIGHT_FOLD_EXPR:
15298 return tsubst_unary_right_fold (t, args, complain, in_decl);
15299 case BINARY_LEFT_FOLD_EXPR:
15300 return tsubst_binary_left_fold (t, args, complain, in_decl);
15301 case BINARY_RIGHT_FOLD_EXPR:
15302 return tsubst_binary_right_fold (t, args, complain, in_decl);
15303 case PREDICT_EXPR:
15304 return t;
15306 default:
15307 /* We shouldn't get here, but keep going if !flag_checking. */
15308 if (flag_checking)
15309 gcc_unreachable ();
15310 return t;
15314 /* Helper function for tsubst_omp_clauses, used for instantiation of
15315 OMP_CLAUSE_DECL of clauses. */
15317 static tree
15318 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15319 tree in_decl)
15321 if (decl == NULL_TREE)
15322 return NULL_TREE;
15324 /* Handle an OpenMP array section represented as a TREE_LIST (or
15325 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15326 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15327 TREE_LIST. We can handle it exactly the same as an array section
15328 (purpose, value, and a chain), even though the nomenclature
15329 (low_bound, length, etc) is different. */
15330 if (TREE_CODE (decl) == TREE_LIST)
15332 tree low_bound
15333 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15334 /*integral_constant_expression_p=*/false);
15335 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15336 /*integral_constant_expression_p=*/false);
15337 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15338 in_decl);
15339 if (TREE_PURPOSE (decl) == low_bound
15340 && TREE_VALUE (decl) == length
15341 && TREE_CHAIN (decl) == chain)
15342 return decl;
15343 tree ret = tree_cons (low_bound, length, chain);
15344 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15345 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15346 return ret;
15348 tree ret = tsubst_expr (decl, args, complain, in_decl,
15349 /*integral_constant_expression_p=*/false);
15350 /* Undo convert_from_reference tsubst_expr could have called. */
15351 if (decl
15352 && REFERENCE_REF_P (ret)
15353 && !REFERENCE_REF_P (decl))
15354 ret = TREE_OPERAND (ret, 0);
15355 return ret;
15358 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15360 static tree
15361 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15362 tree args, tsubst_flags_t complain, tree in_decl)
15364 tree new_clauses = NULL_TREE, nc, oc;
15365 tree linear_no_step = NULL_TREE;
15367 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15369 nc = copy_node (oc);
15370 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15371 new_clauses = nc;
15373 switch (OMP_CLAUSE_CODE (nc))
15375 case OMP_CLAUSE_LASTPRIVATE:
15376 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15378 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15379 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15380 in_decl, /*integral_constant_expression_p=*/false);
15381 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15382 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15384 /* FALLTHRU */
15385 case OMP_CLAUSE_PRIVATE:
15386 case OMP_CLAUSE_SHARED:
15387 case OMP_CLAUSE_FIRSTPRIVATE:
15388 case OMP_CLAUSE_COPYIN:
15389 case OMP_CLAUSE_COPYPRIVATE:
15390 case OMP_CLAUSE_UNIFORM:
15391 case OMP_CLAUSE_DEPEND:
15392 case OMP_CLAUSE_FROM:
15393 case OMP_CLAUSE_TO:
15394 case OMP_CLAUSE_MAP:
15395 case OMP_CLAUSE_USE_DEVICE_PTR:
15396 case OMP_CLAUSE_IS_DEVICE_PTR:
15397 OMP_CLAUSE_DECL (nc)
15398 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15399 in_decl);
15400 break;
15401 case OMP_CLAUSE_TILE:
15402 case OMP_CLAUSE_IF:
15403 case OMP_CLAUSE_NUM_THREADS:
15404 case OMP_CLAUSE_SCHEDULE:
15405 case OMP_CLAUSE_COLLAPSE:
15406 case OMP_CLAUSE_FINAL:
15407 case OMP_CLAUSE_DEVICE:
15408 case OMP_CLAUSE_DIST_SCHEDULE:
15409 case OMP_CLAUSE_NUM_TEAMS:
15410 case OMP_CLAUSE_THREAD_LIMIT:
15411 case OMP_CLAUSE_SAFELEN:
15412 case OMP_CLAUSE_SIMDLEN:
15413 case OMP_CLAUSE_NUM_TASKS:
15414 case OMP_CLAUSE_GRAINSIZE:
15415 case OMP_CLAUSE_PRIORITY:
15416 case OMP_CLAUSE_ORDERED:
15417 case OMP_CLAUSE_HINT:
15418 case OMP_CLAUSE_NUM_GANGS:
15419 case OMP_CLAUSE_NUM_WORKERS:
15420 case OMP_CLAUSE_VECTOR_LENGTH:
15421 case OMP_CLAUSE_WORKER:
15422 case OMP_CLAUSE_VECTOR:
15423 case OMP_CLAUSE_ASYNC:
15424 case OMP_CLAUSE_WAIT:
15425 OMP_CLAUSE_OPERAND (nc, 0)
15426 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15427 in_decl, /*integral_constant_expression_p=*/false);
15428 break;
15429 case OMP_CLAUSE_REDUCTION:
15430 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15432 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15433 if (TREE_CODE (placeholder) == SCOPE_REF)
15435 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15436 complain, in_decl);
15437 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15438 = build_qualified_name (NULL_TREE, scope,
15439 TREE_OPERAND (placeholder, 1),
15440 false);
15442 else
15443 gcc_assert (identifier_p (placeholder));
15445 OMP_CLAUSE_DECL (nc)
15446 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15447 in_decl);
15448 break;
15449 case OMP_CLAUSE_GANG:
15450 case OMP_CLAUSE_ALIGNED:
15451 OMP_CLAUSE_DECL (nc)
15452 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15453 in_decl);
15454 OMP_CLAUSE_OPERAND (nc, 1)
15455 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15456 in_decl, /*integral_constant_expression_p=*/false);
15457 break;
15458 case OMP_CLAUSE_LINEAR:
15459 OMP_CLAUSE_DECL (nc)
15460 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15461 in_decl);
15462 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15464 gcc_assert (!linear_no_step);
15465 linear_no_step = nc;
15467 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15468 OMP_CLAUSE_LINEAR_STEP (nc)
15469 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15470 complain, in_decl);
15471 else
15472 OMP_CLAUSE_LINEAR_STEP (nc)
15473 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15474 in_decl,
15475 /*integral_constant_expression_p=*/false);
15476 break;
15477 case OMP_CLAUSE_NOWAIT:
15478 case OMP_CLAUSE_DEFAULT:
15479 case OMP_CLAUSE_UNTIED:
15480 case OMP_CLAUSE_MERGEABLE:
15481 case OMP_CLAUSE_INBRANCH:
15482 case OMP_CLAUSE_NOTINBRANCH:
15483 case OMP_CLAUSE_PROC_BIND:
15484 case OMP_CLAUSE_FOR:
15485 case OMP_CLAUSE_PARALLEL:
15486 case OMP_CLAUSE_SECTIONS:
15487 case OMP_CLAUSE_TASKGROUP:
15488 case OMP_CLAUSE_NOGROUP:
15489 case OMP_CLAUSE_THREADS:
15490 case OMP_CLAUSE_SIMD:
15491 case OMP_CLAUSE_DEFAULTMAP:
15492 case OMP_CLAUSE_INDEPENDENT:
15493 case OMP_CLAUSE_AUTO:
15494 case OMP_CLAUSE_SEQ:
15495 break;
15496 default:
15497 gcc_unreachable ();
15499 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15500 switch (OMP_CLAUSE_CODE (nc))
15502 case OMP_CLAUSE_SHARED:
15503 case OMP_CLAUSE_PRIVATE:
15504 case OMP_CLAUSE_FIRSTPRIVATE:
15505 case OMP_CLAUSE_LASTPRIVATE:
15506 case OMP_CLAUSE_COPYPRIVATE:
15507 case OMP_CLAUSE_LINEAR:
15508 case OMP_CLAUSE_REDUCTION:
15509 case OMP_CLAUSE_USE_DEVICE_PTR:
15510 case OMP_CLAUSE_IS_DEVICE_PTR:
15511 /* tsubst_expr on SCOPE_REF results in returning
15512 finish_non_static_data_member result. Undo that here. */
15513 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15514 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15515 == IDENTIFIER_NODE))
15517 tree t = OMP_CLAUSE_DECL (nc);
15518 tree v = t;
15519 while (v)
15520 switch (TREE_CODE (v))
15522 case COMPONENT_REF:
15523 case MEM_REF:
15524 case INDIRECT_REF:
15525 CASE_CONVERT:
15526 case POINTER_PLUS_EXPR:
15527 v = TREE_OPERAND (v, 0);
15528 continue;
15529 case PARM_DECL:
15530 if (DECL_CONTEXT (v) == current_function_decl
15531 && DECL_ARTIFICIAL (v)
15532 && DECL_NAME (v) == this_identifier)
15533 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15534 /* FALLTHRU */
15535 default:
15536 v = NULL_TREE;
15537 break;
15540 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15541 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15542 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15543 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15544 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15546 tree decl = OMP_CLAUSE_DECL (nc);
15547 if (VAR_P (decl))
15549 retrofit_lang_decl (decl);
15550 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15553 break;
15554 default:
15555 break;
15559 new_clauses = nreverse (new_clauses);
15560 if (ort != C_ORT_OMP_DECLARE_SIMD)
15562 new_clauses = finish_omp_clauses (new_clauses, ort);
15563 if (linear_no_step)
15564 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15565 if (nc == linear_no_step)
15567 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15568 break;
15571 return new_clauses;
15574 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15576 static tree
15577 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15578 tree in_decl)
15580 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15582 tree purpose, value, chain;
15584 if (t == NULL)
15585 return t;
15587 if (TREE_CODE (t) != TREE_LIST)
15588 return tsubst_copy_and_build (t, args, complain, in_decl,
15589 /*function_p=*/false,
15590 /*integral_constant_expression_p=*/false);
15592 if (t == void_list_node)
15593 return t;
15595 purpose = TREE_PURPOSE (t);
15596 if (purpose)
15597 purpose = RECUR (purpose);
15598 value = TREE_VALUE (t);
15599 if (value)
15601 if (TREE_CODE (value) != LABEL_DECL)
15602 value = RECUR (value);
15603 else
15605 value = lookup_label (DECL_NAME (value));
15606 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15607 TREE_USED (value) = 1;
15610 chain = TREE_CHAIN (t);
15611 if (chain && chain != void_type_node)
15612 chain = RECUR (chain);
15613 return tree_cons (purpose, value, chain);
15614 #undef RECUR
15617 /* Used to temporarily communicate the list of #pragma omp parallel
15618 clauses to #pragma omp for instantiation if they are combined
15619 together. */
15621 static tree *omp_parallel_combined_clauses;
15623 /* Substitute one OMP_FOR iterator. */
15625 static void
15626 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15627 tree initv, tree condv, tree incrv, tree *clauses,
15628 tree args, tsubst_flags_t complain, tree in_decl,
15629 bool integral_constant_expression_p)
15631 #define RECUR(NODE) \
15632 tsubst_expr ((NODE), args, complain, in_decl, \
15633 integral_constant_expression_p)
15634 tree decl, init, cond, incr;
15636 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15637 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15639 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15641 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15642 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15645 decl = TREE_OPERAND (init, 0);
15646 init = TREE_OPERAND (init, 1);
15647 tree decl_expr = NULL_TREE;
15648 if (init && TREE_CODE (init) == DECL_EXPR)
15650 /* We need to jump through some hoops to handle declarations in the
15651 init-statement, since we might need to handle auto deduction,
15652 but we need to keep control of initialization. */
15653 decl_expr = init;
15654 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15655 decl = tsubst_decl (decl, args, complain);
15657 else
15659 if (TREE_CODE (decl) == SCOPE_REF)
15661 decl = RECUR (decl);
15662 if (TREE_CODE (decl) == COMPONENT_REF)
15664 tree v = decl;
15665 while (v)
15666 switch (TREE_CODE (v))
15668 case COMPONENT_REF:
15669 case MEM_REF:
15670 case INDIRECT_REF:
15671 CASE_CONVERT:
15672 case POINTER_PLUS_EXPR:
15673 v = TREE_OPERAND (v, 0);
15674 continue;
15675 case PARM_DECL:
15676 if (DECL_CONTEXT (v) == current_function_decl
15677 && DECL_ARTIFICIAL (v)
15678 && DECL_NAME (v) == this_identifier)
15680 decl = TREE_OPERAND (decl, 1);
15681 decl = omp_privatize_field (decl, false);
15683 /* FALLTHRU */
15684 default:
15685 v = NULL_TREE;
15686 break;
15690 else
15691 decl = RECUR (decl);
15693 init = RECUR (init);
15695 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15696 if (auto_node && init)
15697 TREE_TYPE (decl)
15698 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15700 gcc_assert (!type_dependent_expression_p (decl));
15702 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15704 if (decl_expr)
15706 /* Declare the variable, but don't let that initialize it. */
15707 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15708 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15709 RECUR (decl_expr);
15710 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15713 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15714 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15715 if (TREE_CODE (incr) == MODIFY_EXPR)
15717 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15718 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15719 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15720 NOP_EXPR, rhs, complain);
15722 else
15723 incr = RECUR (incr);
15724 TREE_VEC_ELT (declv, i) = decl;
15725 TREE_VEC_ELT (initv, i) = init;
15726 TREE_VEC_ELT (condv, i) = cond;
15727 TREE_VEC_ELT (incrv, i) = incr;
15728 return;
15731 if (decl_expr)
15733 /* Declare and initialize the variable. */
15734 RECUR (decl_expr);
15735 init = NULL_TREE;
15737 else if (init)
15739 tree *pc;
15740 int j;
15741 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15743 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15745 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15746 && OMP_CLAUSE_DECL (*pc) == decl)
15747 break;
15748 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15749 && OMP_CLAUSE_DECL (*pc) == decl)
15751 if (j)
15752 break;
15753 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15754 tree c = *pc;
15755 *pc = OMP_CLAUSE_CHAIN (c);
15756 OMP_CLAUSE_CHAIN (c) = *clauses;
15757 *clauses = c;
15759 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15760 && OMP_CLAUSE_DECL (*pc) == decl)
15762 error ("iteration variable %qD should not be firstprivate",
15763 decl);
15764 *pc = OMP_CLAUSE_CHAIN (*pc);
15766 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15767 && OMP_CLAUSE_DECL (*pc) == decl)
15769 error ("iteration variable %qD should not be reduction",
15770 decl);
15771 *pc = OMP_CLAUSE_CHAIN (*pc);
15773 else
15774 pc = &OMP_CLAUSE_CHAIN (*pc);
15776 if (*pc)
15777 break;
15779 if (*pc == NULL_TREE)
15781 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15782 OMP_CLAUSE_DECL (c) = decl;
15783 c = finish_omp_clauses (c, C_ORT_OMP);
15784 if (c)
15786 OMP_CLAUSE_CHAIN (c) = *clauses;
15787 *clauses = c;
15791 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15792 if (COMPARISON_CLASS_P (cond))
15794 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15795 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15796 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15798 else
15799 cond = RECUR (cond);
15800 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15801 switch (TREE_CODE (incr))
15803 case PREINCREMENT_EXPR:
15804 case PREDECREMENT_EXPR:
15805 case POSTINCREMENT_EXPR:
15806 case POSTDECREMENT_EXPR:
15807 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15808 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15809 break;
15810 case MODIFY_EXPR:
15811 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15812 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15814 tree rhs = TREE_OPERAND (incr, 1);
15815 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15816 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15817 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15818 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15819 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15820 rhs0, rhs1));
15822 else
15823 incr = RECUR (incr);
15824 break;
15825 case MODOP_EXPR:
15826 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15827 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15829 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15830 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15831 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15832 TREE_TYPE (decl), lhs,
15833 RECUR (TREE_OPERAND (incr, 2))));
15835 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15836 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15837 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15839 tree rhs = TREE_OPERAND (incr, 2);
15840 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15841 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15842 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15843 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15844 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15845 rhs0, rhs1));
15847 else
15848 incr = RECUR (incr);
15849 break;
15850 default:
15851 incr = RECUR (incr);
15852 break;
15855 TREE_VEC_ELT (declv, i) = decl;
15856 TREE_VEC_ELT (initv, i) = init;
15857 TREE_VEC_ELT (condv, i) = cond;
15858 TREE_VEC_ELT (incrv, i) = incr;
15859 #undef RECUR
15862 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15863 of OMP_TARGET's body. */
15865 static tree
15866 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15868 *walk_subtrees = 0;
15869 switch (TREE_CODE (*tp))
15871 case OMP_TEAMS:
15872 return *tp;
15873 case BIND_EXPR:
15874 case STATEMENT_LIST:
15875 *walk_subtrees = 1;
15876 break;
15877 default:
15878 break;
15880 return NULL_TREE;
15883 /* Helper function for tsubst_expr. For decomposition declaration
15884 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15885 also the corresponding decls representing the identifiers
15886 of the decomposition declaration. Return DECL if successful
15887 or error_mark_node otherwise, set *FIRST to the first decl
15888 in the list chained through DECL_CHAIN and *CNT to the number
15889 of such decls. */
15891 static tree
15892 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15893 tsubst_flags_t complain, tree in_decl, tree *first,
15894 unsigned int *cnt)
15896 tree decl2, decl3, prev = decl;
15897 *cnt = 0;
15898 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15899 for (decl2 = DECL_CHAIN (pattern_decl);
15900 decl2
15901 && VAR_P (decl2)
15902 && DECL_DECOMPOSITION_P (decl2)
15903 && DECL_NAME (decl2);
15904 decl2 = DECL_CHAIN (decl2))
15906 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15908 gcc_assert (errorcount);
15909 return error_mark_node;
15911 (*cnt)++;
15912 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15913 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15914 tree v = DECL_VALUE_EXPR (decl2);
15915 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15916 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15917 decl3 = tsubst (decl2, args, complain, in_decl);
15918 SET_DECL_VALUE_EXPR (decl2, v);
15919 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15920 if (VAR_P (decl3))
15921 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15922 maybe_push_decl (decl3);
15923 if (error_operand_p (decl3))
15924 decl = error_mark_node;
15925 else if (decl != error_mark_node
15926 && DECL_CHAIN (decl3) != prev)
15928 gcc_assert (errorcount);
15929 decl = error_mark_node;
15931 else
15932 prev = decl3;
15934 *first = prev;
15935 return decl;
15938 /* Like tsubst_copy for expressions, etc. but also does semantic
15939 processing. */
15941 tree
15942 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15943 bool integral_constant_expression_p)
15945 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15946 #define RECUR(NODE) \
15947 tsubst_expr ((NODE), args, complain, in_decl, \
15948 integral_constant_expression_p)
15950 tree stmt, tmp;
15951 tree r;
15952 location_t loc;
15954 if (t == NULL_TREE || t == error_mark_node)
15955 return t;
15957 loc = input_location;
15958 if (EXPR_HAS_LOCATION (t))
15959 input_location = EXPR_LOCATION (t);
15960 if (STATEMENT_CODE_P (TREE_CODE (t)))
15961 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15963 switch (TREE_CODE (t))
15965 case STATEMENT_LIST:
15967 tree_stmt_iterator i;
15968 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15969 RECUR (tsi_stmt (i));
15970 break;
15973 case CTOR_INITIALIZER:
15974 finish_mem_initializers (tsubst_initializer_list
15975 (TREE_OPERAND (t, 0), args));
15976 break;
15978 case RETURN_EXPR:
15979 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15980 break;
15982 case EXPR_STMT:
15983 tmp = RECUR (EXPR_STMT_EXPR (t));
15984 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15985 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15986 else
15987 finish_expr_stmt (tmp);
15988 break;
15990 case USING_STMT:
15991 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15992 /*attribs=*/NULL_TREE);
15993 break;
15995 case DECL_EXPR:
15997 tree decl, pattern_decl;
15998 tree init;
16000 pattern_decl = decl = DECL_EXPR_DECL (t);
16001 if (TREE_CODE (decl) == LABEL_DECL)
16002 finish_label_decl (DECL_NAME (decl));
16003 else if (TREE_CODE (decl) == USING_DECL)
16005 tree scope = USING_DECL_SCOPE (decl);
16006 tree name = DECL_NAME (decl);
16008 scope = tsubst (scope, args, complain, in_decl);
16009 decl = lookup_qualified_name (scope, name,
16010 /*is_type_p=*/false,
16011 /*complain=*/false);
16012 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16013 qualified_name_lookup_error (scope, name, decl, input_location);
16014 else
16015 finish_local_using_decl (decl, scope, name);
16017 else if (DECL_PACK_P (decl))
16019 /* Don't build up decls for a variadic capture proxy, we'll
16020 instantiate the elements directly as needed. */
16021 break;
16023 else if (is_capture_proxy (decl)
16024 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16026 /* We're in tsubst_lambda_expr, we've already inserted a new
16027 capture proxy, so look it up and register it. */
16028 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16029 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16030 gcc_assert (inst != decl && is_capture_proxy (inst));
16031 register_local_specialization (inst, decl);
16032 break;
16034 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16035 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16036 /* Don't copy the old closure; we'll create a new one in
16037 tsubst_lambda_expr. */
16038 break;
16039 else
16041 init = DECL_INITIAL (decl);
16042 decl = tsubst (decl, args, complain, in_decl);
16043 if (decl != error_mark_node)
16045 /* By marking the declaration as instantiated, we avoid
16046 trying to instantiate it. Since instantiate_decl can't
16047 handle local variables, and since we've already done
16048 all that needs to be done, that's the right thing to
16049 do. */
16050 if (VAR_P (decl))
16051 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16052 if (VAR_P (decl)
16053 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16054 /* Anonymous aggregates are a special case. */
16055 finish_anon_union (decl);
16056 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16058 DECL_CONTEXT (decl) = current_function_decl;
16059 if (DECL_NAME (decl) == this_identifier)
16061 tree lam = DECL_CONTEXT (current_function_decl);
16062 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16063 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16065 insert_capture_proxy (decl);
16067 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16068 /* We already did a pushtag. */;
16069 else if (TREE_CODE (decl) == FUNCTION_DECL
16070 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16071 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16073 DECL_CONTEXT (decl) = NULL_TREE;
16074 pushdecl (decl);
16075 DECL_CONTEXT (decl) = current_function_decl;
16076 cp_check_omp_declare_reduction (decl);
16078 else
16080 int const_init = false;
16081 maybe_push_decl (decl);
16082 if (VAR_P (decl)
16083 && DECL_PRETTY_FUNCTION_P (decl))
16085 /* For __PRETTY_FUNCTION__ we have to adjust the
16086 initializer. */
16087 const char *const name
16088 = cxx_printable_name (current_function_decl, 2);
16089 init = cp_fname_init (name, &TREE_TYPE (decl));
16091 else
16092 init = tsubst_init (init, decl, args, complain, in_decl);
16094 if (VAR_P (decl))
16095 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16096 (pattern_decl));
16097 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16098 if (VAR_P (decl)
16099 && DECL_DECOMPOSITION_P (decl)
16100 && TREE_TYPE (pattern_decl) != error_mark_node)
16102 unsigned int cnt;
16103 tree first;
16104 decl = tsubst_decomp_names (decl, pattern_decl, args,
16105 complain, in_decl, &first,
16106 &cnt);
16107 if (decl != error_mark_node)
16108 cp_finish_decomp (decl, first, cnt);
16114 break;
16117 case FOR_STMT:
16118 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16119 RECUR (FOR_INIT_STMT (t));
16120 finish_init_stmt (stmt);
16121 tmp = RECUR (FOR_COND (t));
16122 finish_for_cond (tmp, stmt, false);
16123 tmp = RECUR (FOR_EXPR (t));
16124 finish_for_expr (tmp, stmt);
16125 RECUR (FOR_BODY (t));
16126 finish_for_stmt (stmt);
16127 break;
16129 case RANGE_FOR_STMT:
16131 tree decl, expr;
16132 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16133 decl = RANGE_FOR_DECL (t);
16134 decl = tsubst (decl, args, complain, in_decl);
16135 maybe_push_decl (decl);
16136 expr = RECUR (RANGE_FOR_EXPR (t));
16137 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16139 unsigned int cnt;
16140 tree first;
16141 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16142 complain, in_decl, &first, &cnt);
16143 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16144 RANGE_FOR_IVDEP (t));
16146 else
16147 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16148 RANGE_FOR_IVDEP (t));
16149 RECUR (RANGE_FOR_BODY (t));
16150 finish_for_stmt (stmt);
16152 break;
16154 case WHILE_STMT:
16155 stmt = begin_while_stmt ();
16156 tmp = RECUR (WHILE_COND (t));
16157 finish_while_stmt_cond (tmp, stmt, false);
16158 RECUR (WHILE_BODY (t));
16159 finish_while_stmt (stmt);
16160 break;
16162 case DO_STMT:
16163 stmt = begin_do_stmt ();
16164 RECUR (DO_BODY (t));
16165 finish_do_body (stmt);
16166 tmp = RECUR (DO_COND (t));
16167 finish_do_stmt (tmp, stmt, false);
16168 break;
16170 case IF_STMT:
16171 stmt = begin_if_stmt ();
16172 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16173 tmp = RECUR (IF_COND (t));
16174 tmp = finish_if_stmt_cond (tmp, stmt);
16175 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16176 /* Don't instantiate the THEN_CLAUSE. */;
16177 else
16179 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16180 if (inhibit)
16181 ++c_inhibit_evaluation_warnings;
16182 RECUR (THEN_CLAUSE (t));
16183 if (inhibit)
16184 --c_inhibit_evaluation_warnings;
16186 finish_then_clause (stmt);
16188 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16189 /* Don't instantiate the ELSE_CLAUSE. */;
16190 else if (ELSE_CLAUSE (t))
16192 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16193 begin_else_clause (stmt);
16194 if (inhibit)
16195 ++c_inhibit_evaluation_warnings;
16196 RECUR (ELSE_CLAUSE (t));
16197 if (inhibit)
16198 --c_inhibit_evaluation_warnings;
16199 finish_else_clause (stmt);
16202 finish_if_stmt (stmt);
16203 break;
16205 case BIND_EXPR:
16206 if (BIND_EXPR_BODY_BLOCK (t))
16207 stmt = begin_function_body ();
16208 else
16209 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16210 ? BCS_TRY_BLOCK : 0);
16212 RECUR (BIND_EXPR_BODY (t));
16214 if (BIND_EXPR_BODY_BLOCK (t))
16215 finish_function_body (stmt);
16216 else
16217 finish_compound_stmt (stmt);
16218 break;
16220 case BREAK_STMT:
16221 finish_break_stmt ();
16222 break;
16224 case CONTINUE_STMT:
16225 finish_continue_stmt ();
16226 break;
16228 case SWITCH_STMT:
16229 stmt = begin_switch_stmt ();
16230 tmp = RECUR (SWITCH_STMT_COND (t));
16231 finish_switch_cond (tmp, stmt);
16232 RECUR (SWITCH_STMT_BODY (t));
16233 finish_switch_stmt (stmt);
16234 break;
16236 case CASE_LABEL_EXPR:
16238 tree low = RECUR (CASE_LOW (t));
16239 tree high = RECUR (CASE_HIGH (t));
16240 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16241 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16242 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16243 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16245 break;
16247 case LABEL_EXPR:
16249 tree decl = LABEL_EXPR_LABEL (t);
16250 tree label;
16252 label = finish_label_stmt (DECL_NAME (decl));
16253 if (TREE_CODE (label) == LABEL_DECL)
16254 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16255 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16256 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16258 break;
16260 case GOTO_EXPR:
16261 tmp = GOTO_DESTINATION (t);
16262 if (TREE_CODE (tmp) != LABEL_DECL)
16263 /* Computed goto's must be tsubst'd into. On the other hand,
16264 non-computed gotos must not be; the identifier in question
16265 will have no binding. */
16266 tmp = RECUR (tmp);
16267 else
16268 tmp = DECL_NAME (tmp);
16269 finish_goto_stmt (tmp);
16270 break;
16272 case ASM_EXPR:
16274 tree string = RECUR (ASM_STRING (t));
16275 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16276 complain, in_decl);
16277 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16278 complain, in_decl);
16279 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16280 complain, in_decl);
16281 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16282 complain, in_decl);
16283 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16284 clobbers, labels);
16285 tree asm_expr = tmp;
16286 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16287 asm_expr = TREE_OPERAND (asm_expr, 0);
16288 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16290 break;
16292 case TRY_BLOCK:
16293 if (CLEANUP_P (t))
16295 stmt = begin_try_block ();
16296 RECUR (TRY_STMTS (t));
16297 finish_cleanup_try_block (stmt);
16298 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16300 else
16302 tree compound_stmt = NULL_TREE;
16304 if (FN_TRY_BLOCK_P (t))
16305 stmt = begin_function_try_block (&compound_stmt);
16306 else
16307 stmt = begin_try_block ();
16309 RECUR (TRY_STMTS (t));
16311 if (FN_TRY_BLOCK_P (t))
16312 finish_function_try_block (stmt);
16313 else
16314 finish_try_block (stmt);
16316 RECUR (TRY_HANDLERS (t));
16317 if (FN_TRY_BLOCK_P (t))
16318 finish_function_handler_sequence (stmt, compound_stmt);
16319 else
16320 finish_handler_sequence (stmt);
16322 break;
16324 case HANDLER:
16326 tree decl = HANDLER_PARMS (t);
16328 if (decl)
16330 decl = tsubst (decl, args, complain, in_decl);
16331 /* Prevent instantiate_decl from trying to instantiate
16332 this variable. We've already done all that needs to be
16333 done. */
16334 if (decl != error_mark_node)
16335 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16337 stmt = begin_handler ();
16338 finish_handler_parms (decl, stmt);
16339 RECUR (HANDLER_BODY (t));
16340 finish_handler (stmt);
16342 break;
16344 case TAG_DEFN:
16345 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16346 if (CLASS_TYPE_P (tmp))
16348 /* Local classes are not independent templates; they are
16349 instantiated along with their containing function. And this
16350 way we don't have to deal with pushing out of one local class
16351 to instantiate a member of another local class. */
16352 /* Closures are handled by the LAMBDA_EXPR. */
16353 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16354 complete_type (tmp);
16355 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16356 if ((VAR_P (fld)
16357 || (TREE_CODE (fld) == FUNCTION_DECL
16358 && !DECL_ARTIFICIAL (fld)))
16359 && DECL_TEMPLATE_INSTANTIATION (fld))
16360 instantiate_decl (fld, /*defer_ok=*/false,
16361 /*expl_inst_class=*/false);
16363 break;
16365 case STATIC_ASSERT:
16367 tree condition;
16369 ++c_inhibit_evaluation_warnings;
16370 condition =
16371 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16372 args,
16373 complain, in_decl,
16374 /*integral_constant_expression_p=*/true);
16375 --c_inhibit_evaluation_warnings;
16377 finish_static_assert (condition,
16378 STATIC_ASSERT_MESSAGE (t),
16379 STATIC_ASSERT_SOURCE_LOCATION (t),
16380 /*member_p=*/false);
16382 break;
16384 case OACC_KERNELS:
16385 case OACC_PARALLEL:
16386 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16387 in_decl);
16388 stmt = begin_omp_parallel ();
16389 RECUR (OMP_BODY (t));
16390 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16391 break;
16393 case OMP_PARALLEL:
16394 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16395 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16396 complain, in_decl);
16397 if (OMP_PARALLEL_COMBINED (t))
16398 omp_parallel_combined_clauses = &tmp;
16399 stmt = begin_omp_parallel ();
16400 RECUR (OMP_PARALLEL_BODY (t));
16401 gcc_assert (omp_parallel_combined_clauses == NULL);
16402 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16403 = OMP_PARALLEL_COMBINED (t);
16404 pop_omp_privatization_clauses (r);
16405 break;
16407 case OMP_TASK:
16408 r = push_omp_privatization_clauses (false);
16409 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16410 complain, in_decl);
16411 stmt = begin_omp_task ();
16412 RECUR (OMP_TASK_BODY (t));
16413 finish_omp_task (tmp, stmt);
16414 pop_omp_privatization_clauses (r);
16415 break;
16417 case OMP_FOR:
16418 case OMP_SIMD:
16419 case CILK_SIMD:
16420 case CILK_FOR:
16421 case OMP_DISTRIBUTE:
16422 case OMP_TASKLOOP:
16423 case OACC_LOOP:
16425 tree clauses, body, pre_body;
16426 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16427 tree orig_declv = NULL_TREE;
16428 tree incrv = NULL_TREE;
16429 enum c_omp_region_type ort = C_ORT_OMP;
16430 int i;
16432 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16433 ort = C_ORT_CILK;
16434 else if (TREE_CODE (t) == OACC_LOOP)
16435 ort = C_ORT_ACC;
16437 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16438 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16439 in_decl);
16440 if (OMP_FOR_INIT (t) != NULL_TREE)
16442 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16443 if (OMP_FOR_ORIG_DECLS (t))
16444 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16445 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16446 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16447 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16450 stmt = begin_omp_structured_block ();
16452 pre_body = push_stmt_list ();
16453 RECUR (OMP_FOR_PRE_BODY (t));
16454 pre_body = pop_stmt_list (pre_body);
16456 if (OMP_FOR_INIT (t) != NULL_TREE)
16457 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16458 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16459 incrv, &clauses, args, complain, in_decl,
16460 integral_constant_expression_p);
16461 omp_parallel_combined_clauses = NULL;
16463 body = push_stmt_list ();
16464 RECUR (OMP_FOR_BODY (t));
16465 body = pop_stmt_list (body);
16467 if (OMP_FOR_INIT (t) != NULL_TREE)
16468 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16469 orig_declv, initv, condv, incrv, body, pre_body,
16470 NULL, clauses);
16471 else
16473 t = make_node (TREE_CODE (t));
16474 TREE_TYPE (t) = void_type_node;
16475 OMP_FOR_BODY (t) = body;
16476 OMP_FOR_PRE_BODY (t) = pre_body;
16477 OMP_FOR_CLAUSES (t) = clauses;
16478 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16479 add_stmt (t);
16482 add_stmt (finish_omp_structured_block (stmt));
16483 pop_omp_privatization_clauses (r);
16485 break;
16487 case OMP_SECTIONS:
16488 omp_parallel_combined_clauses = NULL;
16489 /* FALLTHRU */
16490 case OMP_SINGLE:
16491 case OMP_TEAMS:
16492 case OMP_CRITICAL:
16493 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16494 && OMP_TEAMS_COMBINED (t));
16495 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16496 in_decl);
16497 stmt = push_stmt_list ();
16498 RECUR (OMP_BODY (t));
16499 stmt = pop_stmt_list (stmt);
16501 t = copy_node (t);
16502 OMP_BODY (t) = stmt;
16503 OMP_CLAUSES (t) = tmp;
16504 add_stmt (t);
16505 pop_omp_privatization_clauses (r);
16506 break;
16508 case OACC_DATA:
16509 case OMP_TARGET_DATA:
16510 case OMP_TARGET:
16511 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16512 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16513 in_decl);
16514 keep_next_level (true);
16515 stmt = begin_omp_structured_block ();
16517 RECUR (OMP_BODY (t));
16518 stmt = finish_omp_structured_block (stmt);
16520 t = copy_node (t);
16521 OMP_BODY (t) = stmt;
16522 OMP_CLAUSES (t) = tmp;
16523 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16525 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16526 if (teams)
16528 /* For combined target teams, ensure the num_teams and
16529 thread_limit clause expressions are evaluated on the host,
16530 before entering the target construct. */
16531 tree c;
16532 for (c = OMP_TEAMS_CLAUSES (teams);
16533 c; c = OMP_CLAUSE_CHAIN (c))
16534 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16535 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16536 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16538 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16539 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16540 if (expr == error_mark_node)
16541 continue;
16542 tmp = TARGET_EXPR_SLOT (expr);
16543 add_stmt (expr);
16544 OMP_CLAUSE_OPERAND (c, 0) = expr;
16545 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16546 OMP_CLAUSE_FIRSTPRIVATE);
16547 OMP_CLAUSE_DECL (tc) = tmp;
16548 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16549 OMP_TARGET_CLAUSES (t) = tc;
16553 add_stmt (t);
16554 break;
16556 case OACC_DECLARE:
16557 t = copy_node (t);
16558 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16559 complain, in_decl);
16560 OACC_DECLARE_CLAUSES (t) = tmp;
16561 add_stmt (t);
16562 break;
16564 case OMP_TARGET_UPDATE:
16565 case OMP_TARGET_ENTER_DATA:
16566 case OMP_TARGET_EXIT_DATA:
16567 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16568 complain, in_decl);
16569 t = copy_node (t);
16570 OMP_STANDALONE_CLAUSES (t) = tmp;
16571 add_stmt (t);
16572 break;
16574 case OACC_ENTER_DATA:
16575 case OACC_EXIT_DATA:
16576 case OACC_UPDATE:
16577 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16578 complain, in_decl);
16579 t = copy_node (t);
16580 OMP_STANDALONE_CLAUSES (t) = tmp;
16581 add_stmt (t);
16582 break;
16584 case OMP_ORDERED:
16585 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16586 complain, in_decl);
16587 stmt = push_stmt_list ();
16588 RECUR (OMP_BODY (t));
16589 stmt = pop_stmt_list (stmt);
16591 t = copy_node (t);
16592 OMP_BODY (t) = stmt;
16593 OMP_ORDERED_CLAUSES (t) = tmp;
16594 add_stmt (t);
16595 break;
16597 case OMP_SECTION:
16598 case OMP_MASTER:
16599 case OMP_TASKGROUP:
16600 stmt = push_stmt_list ();
16601 RECUR (OMP_BODY (t));
16602 stmt = pop_stmt_list (stmt);
16604 t = copy_node (t);
16605 OMP_BODY (t) = stmt;
16606 add_stmt (t);
16607 break;
16609 case OMP_ATOMIC:
16610 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16611 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16613 tree op1 = TREE_OPERAND (t, 1);
16614 tree rhs1 = NULL_TREE;
16615 tree lhs, rhs;
16616 if (TREE_CODE (op1) == COMPOUND_EXPR)
16618 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16619 op1 = TREE_OPERAND (op1, 1);
16621 lhs = RECUR (TREE_OPERAND (op1, 0));
16622 rhs = RECUR (TREE_OPERAND (op1, 1));
16623 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16624 NULL_TREE, NULL_TREE, rhs1,
16625 OMP_ATOMIC_SEQ_CST (t));
16627 else
16629 tree op1 = TREE_OPERAND (t, 1);
16630 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16631 tree rhs1 = NULL_TREE;
16632 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16633 enum tree_code opcode = NOP_EXPR;
16634 if (code == OMP_ATOMIC_READ)
16636 v = RECUR (TREE_OPERAND (op1, 0));
16637 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16639 else if (code == OMP_ATOMIC_CAPTURE_OLD
16640 || code == OMP_ATOMIC_CAPTURE_NEW)
16642 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16643 v = RECUR (TREE_OPERAND (op1, 0));
16644 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16645 if (TREE_CODE (op11) == COMPOUND_EXPR)
16647 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16648 op11 = TREE_OPERAND (op11, 1);
16650 lhs = RECUR (TREE_OPERAND (op11, 0));
16651 rhs = RECUR (TREE_OPERAND (op11, 1));
16652 opcode = TREE_CODE (op11);
16653 if (opcode == MODIFY_EXPR)
16654 opcode = NOP_EXPR;
16656 else
16658 code = OMP_ATOMIC;
16659 lhs = RECUR (TREE_OPERAND (op1, 0));
16660 rhs = RECUR (TREE_OPERAND (op1, 1));
16662 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16663 OMP_ATOMIC_SEQ_CST (t));
16665 break;
16667 case TRANSACTION_EXPR:
16669 int flags = 0;
16670 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16671 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16673 if (TRANSACTION_EXPR_IS_STMT (t))
16675 tree body = TRANSACTION_EXPR_BODY (t);
16676 tree noex = NULL_TREE;
16677 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16679 noex = MUST_NOT_THROW_COND (body);
16680 if (noex == NULL_TREE)
16681 noex = boolean_true_node;
16682 body = TREE_OPERAND (body, 0);
16684 stmt = begin_transaction_stmt (input_location, NULL, flags);
16685 RECUR (body);
16686 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16688 else
16690 stmt = build_transaction_expr (EXPR_LOCATION (t),
16691 RECUR (TRANSACTION_EXPR_BODY (t)),
16692 flags, NULL_TREE);
16693 RETURN (stmt);
16696 break;
16698 case MUST_NOT_THROW_EXPR:
16700 tree op0 = RECUR (TREE_OPERAND (t, 0));
16701 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16702 RETURN (build_must_not_throw_expr (op0, cond));
16705 case EXPR_PACK_EXPANSION:
16706 error ("invalid use of pack expansion expression");
16707 RETURN (error_mark_node);
16709 case NONTYPE_ARGUMENT_PACK:
16710 error ("use %<...%> to expand argument pack");
16711 RETURN (error_mark_node);
16713 case CILK_SPAWN_STMT:
16714 cfun->calls_cilk_spawn = 1;
16715 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16717 case CILK_SYNC_STMT:
16718 RETURN (build_cilk_sync ());
16720 case COMPOUND_EXPR:
16721 tmp = RECUR (TREE_OPERAND (t, 0));
16722 if (tmp == NULL_TREE)
16723 /* If the first operand was a statement, we're done with it. */
16724 RETURN (RECUR (TREE_OPERAND (t, 1)));
16725 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16726 RECUR (TREE_OPERAND (t, 1)),
16727 complain));
16729 case ANNOTATE_EXPR:
16730 tmp = RECUR (TREE_OPERAND (t, 0));
16731 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16732 TREE_TYPE (tmp), tmp,
16733 RECUR (TREE_OPERAND (t, 1)),
16734 RECUR (TREE_OPERAND (t, 2))));
16736 default:
16737 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16739 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16740 /*function_p=*/false,
16741 integral_constant_expression_p));
16744 RETURN (NULL_TREE);
16745 out:
16746 input_location = loc;
16747 return r;
16748 #undef RECUR
16749 #undef RETURN
16752 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16753 function. For description of the body see comment above
16754 cp_parser_omp_declare_reduction_exprs. */
16756 static void
16757 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16759 if (t == NULL_TREE || t == error_mark_node)
16760 return;
16762 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16764 tree_stmt_iterator tsi;
16765 int i;
16766 tree stmts[7];
16767 memset (stmts, 0, sizeof stmts);
16768 for (i = 0, tsi = tsi_start (t);
16769 i < 7 && !tsi_end_p (tsi);
16770 i++, tsi_next (&tsi))
16771 stmts[i] = tsi_stmt (tsi);
16772 gcc_assert (tsi_end_p (tsi));
16774 if (i >= 3)
16776 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16777 && TREE_CODE (stmts[1]) == DECL_EXPR);
16778 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16779 args, complain, in_decl);
16780 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16781 args, complain, in_decl);
16782 DECL_CONTEXT (omp_out) = current_function_decl;
16783 DECL_CONTEXT (omp_in) = current_function_decl;
16784 keep_next_level (true);
16785 tree block = begin_omp_structured_block ();
16786 tsubst_expr (stmts[2], args, complain, in_decl, false);
16787 block = finish_omp_structured_block (block);
16788 block = maybe_cleanup_point_expr_void (block);
16789 add_decl_expr (omp_out);
16790 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16791 TREE_NO_WARNING (omp_out) = 1;
16792 add_decl_expr (omp_in);
16793 finish_expr_stmt (block);
16795 if (i >= 6)
16797 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16798 && TREE_CODE (stmts[4]) == DECL_EXPR);
16799 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16800 args, complain, in_decl);
16801 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16802 args, complain, in_decl);
16803 DECL_CONTEXT (omp_priv) = current_function_decl;
16804 DECL_CONTEXT (omp_orig) = current_function_decl;
16805 keep_next_level (true);
16806 tree block = begin_omp_structured_block ();
16807 tsubst_expr (stmts[5], args, complain, in_decl, false);
16808 block = finish_omp_structured_block (block);
16809 block = maybe_cleanup_point_expr_void (block);
16810 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16811 add_decl_expr (omp_priv);
16812 add_decl_expr (omp_orig);
16813 finish_expr_stmt (block);
16814 if (i == 7)
16815 add_decl_expr (omp_orig);
16819 /* T is a postfix-expression that is not being used in a function
16820 call. Return the substituted version of T. */
16822 static tree
16823 tsubst_non_call_postfix_expression (tree t, tree args,
16824 tsubst_flags_t complain,
16825 tree in_decl)
16827 if (TREE_CODE (t) == SCOPE_REF)
16828 t = tsubst_qualified_id (t, args, complain, in_decl,
16829 /*done=*/false, /*address_p=*/false);
16830 else
16831 t = tsubst_copy_and_build (t, args, complain, in_decl,
16832 /*function_p=*/false,
16833 /*integral_constant_expression_p=*/false);
16835 return t;
16838 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16839 instantiation context. Instantiating a pack expansion containing a lambda
16840 might result in multiple lambdas all based on the same lambda in the
16841 template. */
16843 tree
16844 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16846 tree oldfn = lambda_function (t);
16847 in_decl = oldfn;
16849 tree r = build_lambda_expr ();
16851 LAMBDA_EXPR_LOCATION (r)
16852 = LAMBDA_EXPR_LOCATION (t);
16853 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16854 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16855 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16857 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16858 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16859 else
16860 record_lambda_scope (r);
16862 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16863 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16865 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16866 cap = TREE_CHAIN (cap))
16868 tree field = TREE_PURPOSE (cap);
16869 if (PACK_EXPANSION_P (field))
16870 field = PACK_EXPANSION_PATTERN (field);
16871 field = tsubst_decl (field, args, complain);
16873 if (field == error_mark_node)
16874 return error_mark_node;
16876 tree init = TREE_VALUE (cap);
16877 if (PACK_EXPANSION_P (init))
16878 init = tsubst_pack_expansion (init, args, complain, in_decl);
16879 else
16880 init = tsubst_copy_and_build (init, args, complain, in_decl,
16881 /*fn*/false, /*constexpr*/false);
16883 if (TREE_CODE (field) == TREE_VEC)
16885 int len = TREE_VEC_LENGTH (field);
16886 gcc_assert (TREE_CODE (init) == TREE_VEC
16887 && TREE_VEC_LENGTH (init) == len);
16888 for (int i = 0; i < len; ++i)
16889 LAMBDA_EXPR_CAPTURE_LIST (r)
16890 = tree_cons (TREE_VEC_ELT (field, i),
16891 TREE_VEC_ELT (init, i),
16892 LAMBDA_EXPR_CAPTURE_LIST (r));
16894 else
16896 LAMBDA_EXPR_CAPTURE_LIST (r)
16897 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16899 if (id_equal (DECL_NAME (field), "__this"))
16900 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16904 tree type = begin_lambda_type (r);
16906 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16907 determine_visibility (TYPE_NAME (type));
16909 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16911 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16912 ? DECL_TI_TEMPLATE (oldfn)
16913 : NULL_TREE);
16915 tree fntype = static_fn_type (oldfn);
16916 if (oldtmpl)
16917 ++processing_template_decl;
16918 fntype = tsubst (fntype, args, complain, in_decl);
16919 if (oldtmpl)
16920 --processing_template_decl;
16922 if (fntype == error_mark_node)
16923 r = error_mark_node;
16924 else
16926 /* Fix the type of 'this'. */
16927 fntype = build_memfn_type (fntype, type,
16928 type_memfn_quals (fntype),
16929 type_memfn_rqual (fntype));
16930 tree fn, tmpl;
16931 if (oldtmpl)
16933 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16934 fn = DECL_TEMPLATE_RESULT (tmpl);
16935 finish_member_declaration (tmpl);
16937 else
16939 tmpl = NULL_TREE;
16940 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16941 finish_member_declaration (fn);
16944 /* Let finish_function set this. */
16945 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16947 bool nested = cfun;
16948 if (nested)
16949 push_function_context ();
16951 local_specialization_stack s (lss_copy);
16953 tree body = start_lambda_function (fn, r);
16955 register_parameter_specializations (oldfn, fn);
16957 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16958 /*constexpr*/false);
16960 finish_lambda_function (body);
16962 if (nested)
16963 pop_function_context ();
16965 /* The capture list was built up in reverse order; fix that now. */
16966 LAMBDA_EXPR_CAPTURE_LIST (r)
16967 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16969 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16971 maybe_add_lambda_conv_op (type);
16974 finish_struct (type, /*attr*/NULL_TREE);
16976 insert_pending_capture_proxies ();
16978 return r;
16981 /* Like tsubst but deals with expressions and performs semantic
16982 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16984 tree
16985 tsubst_copy_and_build (tree t,
16986 tree args,
16987 tsubst_flags_t complain,
16988 tree in_decl,
16989 bool function_p,
16990 bool integral_constant_expression_p)
16992 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16993 #define RECUR(NODE) \
16994 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16995 /*function_p=*/false, \
16996 integral_constant_expression_p)
16998 tree retval, op1;
16999 location_t loc;
17001 if (t == NULL_TREE || t == error_mark_node)
17002 return t;
17004 loc = input_location;
17005 if (EXPR_HAS_LOCATION (t))
17006 input_location = EXPR_LOCATION (t);
17008 /* N3276 decltype magic only applies to calls at the top level or on the
17009 right side of a comma. */
17010 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17011 complain &= ~tf_decltype;
17013 switch (TREE_CODE (t))
17015 case USING_DECL:
17016 t = DECL_NAME (t);
17017 /* Fall through. */
17018 case IDENTIFIER_NODE:
17020 tree decl;
17021 cp_id_kind idk;
17022 bool non_integral_constant_expression_p;
17023 const char *error_msg;
17025 if (IDENTIFIER_CONV_OP_P (t))
17027 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17028 t = make_conv_op_name (new_type);
17031 /* Look up the name. */
17032 decl = lookup_name (t);
17034 /* By convention, expressions use ERROR_MARK_NODE to indicate
17035 failure, not NULL_TREE. */
17036 if (decl == NULL_TREE)
17037 decl = error_mark_node;
17039 decl = finish_id_expression (t, decl, NULL_TREE,
17040 &idk,
17041 integral_constant_expression_p,
17042 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17043 &non_integral_constant_expression_p,
17044 /*template_p=*/false,
17045 /*done=*/true,
17046 /*address_p=*/false,
17047 /*template_arg_p=*/false,
17048 &error_msg,
17049 input_location);
17050 if (error_msg)
17051 error (error_msg);
17052 if (!function_p && identifier_p (decl))
17054 if (complain & tf_error)
17055 unqualified_name_lookup_error (decl);
17056 decl = error_mark_node;
17058 RETURN (decl);
17061 case TEMPLATE_ID_EXPR:
17063 tree object;
17064 tree templ = RECUR (TREE_OPERAND (t, 0));
17065 tree targs = TREE_OPERAND (t, 1);
17067 if (targs)
17068 targs = tsubst_template_args (targs, args, complain, in_decl);
17069 if (targs == error_mark_node)
17070 return error_mark_node;
17072 if (TREE_CODE (templ) == SCOPE_REF)
17074 tree name = TREE_OPERAND (templ, 1);
17075 tree tid = lookup_template_function (name, targs);
17076 TREE_OPERAND (templ, 1) = tid;
17077 return templ;
17080 if (variable_template_p (templ))
17081 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17083 if (TREE_CODE (templ) == COMPONENT_REF)
17085 object = TREE_OPERAND (templ, 0);
17086 templ = TREE_OPERAND (templ, 1);
17088 else
17089 object = NULL_TREE;
17090 templ = lookup_template_function (templ, targs);
17092 if (object)
17093 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17094 object, templ, NULL_TREE));
17095 else
17096 RETURN (baselink_for_fns (templ));
17099 case INDIRECT_REF:
17101 tree r = RECUR (TREE_OPERAND (t, 0));
17103 if (REFERENCE_REF_P (t))
17105 /* A type conversion to reference type will be enclosed in
17106 such an indirect ref, but the substitution of the cast
17107 will have also added such an indirect ref. */
17108 r = convert_from_reference (r);
17110 else
17111 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17112 complain|decltype_flag);
17114 if (TREE_CODE (r) == INDIRECT_REF)
17115 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17117 RETURN (r);
17120 case NOP_EXPR:
17122 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17123 tree op0 = RECUR (TREE_OPERAND (t, 0));
17124 RETURN (build_nop (type, op0));
17127 case IMPLICIT_CONV_EXPR:
17129 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17130 tree expr = RECUR (TREE_OPERAND (t, 0));
17131 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17133 retval = copy_node (t);
17134 TREE_TYPE (retval) = type;
17135 TREE_OPERAND (retval, 0) = expr;
17136 RETURN (retval);
17138 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17139 /* We'll pass this to convert_nontype_argument again, we don't need
17140 to actually perform any conversion here. */
17141 RETURN (expr);
17142 int flags = LOOKUP_IMPLICIT;
17143 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17144 flags = LOOKUP_NORMAL;
17145 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17146 flags));
17149 case CONVERT_EXPR:
17151 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17152 tree op0 = RECUR (TREE_OPERAND (t, 0));
17153 RETURN (build1 (CONVERT_EXPR, type, op0));
17156 case CAST_EXPR:
17157 case REINTERPRET_CAST_EXPR:
17158 case CONST_CAST_EXPR:
17159 case DYNAMIC_CAST_EXPR:
17160 case STATIC_CAST_EXPR:
17162 tree type;
17163 tree op, r = NULL_TREE;
17165 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17166 if (integral_constant_expression_p
17167 && !cast_valid_in_integral_constant_expression_p (type))
17169 if (complain & tf_error)
17170 error ("a cast to a type other than an integral or "
17171 "enumeration type cannot appear in a constant-expression");
17172 RETURN (error_mark_node);
17175 op = RECUR (TREE_OPERAND (t, 0));
17177 warning_sentinel s(warn_useless_cast);
17178 switch (TREE_CODE (t))
17180 case CAST_EXPR:
17181 r = build_functional_cast (type, op, complain);
17182 break;
17183 case REINTERPRET_CAST_EXPR:
17184 r = build_reinterpret_cast (type, op, complain);
17185 break;
17186 case CONST_CAST_EXPR:
17187 r = build_const_cast (type, op, complain);
17188 break;
17189 case DYNAMIC_CAST_EXPR:
17190 r = build_dynamic_cast (type, op, complain);
17191 break;
17192 case STATIC_CAST_EXPR:
17193 r = build_static_cast (type, op, complain);
17194 break;
17195 default:
17196 gcc_unreachable ();
17199 RETURN (r);
17202 case POSTDECREMENT_EXPR:
17203 case POSTINCREMENT_EXPR:
17204 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17205 args, complain, in_decl);
17206 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17207 complain|decltype_flag));
17209 case PREDECREMENT_EXPR:
17210 case PREINCREMENT_EXPR:
17211 case NEGATE_EXPR:
17212 case BIT_NOT_EXPR:
17213 case ABS_EXPR:
17214 case TRUTH_NOT_EXPR:
17215 case UNARY_PLUS_EXPR: /* Unary + */
17216 case REALPART_EXPR:
17217 case IMAGPART_EXPR:
17218 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17219 RECUR (TREE_OPERAND (t, 0)),
17220 complain|decltype_flag));
17222 case FIX_TRUNC_EXPR:
17223 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17224 false, complain));
17226 case ADDR_EXPR:
17227 op1 = TREE_OPERAND (t, 0);
17228 if (TREE_CODE (op1) == LABEL_DECL)
17229 RETURN (finish_label_address_expr (DECL_NAME (op1),
17230 EXPR_LOCATION (op1)));
17231 if (TREE_CODE (op1) == SCOPE_REF)
17232 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17233 /*done=*/true, /*address_p=*/true);
17234 else
17235 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17236 in_decl);
17237 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17238 complain|decltype_flag));
17240 case PLUS_EXPR:
17241 case MINUS_EXPR:
17242 case MULT_EXPR:
17243 case TRUNC_DIV_EXPR:
17244 case CEIL_DIV_EXPR:
17245 case FLOOR_DIV_EXPR:
17246 case ROUND_DIV_EXPR:
17247 case EXACT_DIV_EXPR:
17248 case BIT_AND_EXPR:
17249 case BIT_IOR_EXPR:
17250 case BIT_XOR_EXPR:
17251 case TRUNC_MOD_EXPR:
17252 case FLOOR_MOD_EXPR:
17253 case TRUTH_ANDIF_EXPR:
17254 case TRUTH_ORIF_EXPR:
17255 case TRUTH_AND_EXPR:
17256 case TRUTH_OR_EXPR:
17257 case RSHIFT_EXPR:
17258 case LSHIFT_EXPR:
17259 case RROTATE_EXPR:
17260 case LROTATE_EXPR:
17261 case EQ_EXPR:
17262 case NE_EXPR:
17263 case MAX_EXPR:
17264 case MIN_EXPR:
17265 case LE_EXPR:
17266 case GE_EXPR:
17267 case LT_EXPR:
17268 case GT_EXPR:
17269 case MEMBER_REF:
17270 case DOTSTAR_EXPR:
17272 warning_sentinel s1(warn_type_limits);
17273 warning_sentinel s2(warn_div_by_zero);
17274 warning_sentinel s3(warn_logical_op);
17275 warning_sentinel s4(warn_tautological_compare);
17276 tree op0 = RECUR (TREE_OPERAND (t, 0));
17277 tree op1 = RECUR (TREE_OPERAND (t, 1));
17278 tree r = build_x_binary_op
17279 (input_location, TREE_CODE (t),
17280 op0,
17281 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17282 ? ERROR_MARK
17283 : TREE_CODE (TREE_OPERAND (t, 0))),
17284 op1,
17285 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17286 ? ERROR_MARK
17287 : TREE_CODE (TREE_OPERAND (t, 1))),
17288 /*overload=*/NULL,
17289 complain|decltype_flag);
17290 if (EXPR_P (r) && TREE_NO_WARNING (t))
17291 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17293 RETURN (r);
17296 case POINTER_PLUS_EXPR:
17298 tree op0 = RECUR (TREE_OPERAND (t, 0));
17299 tree op1 = RECUR (TREE_OPERAND (t, 1));
17300 return fold_build_pointer_plus (op0, op1);
17303 case SCOPE_REF:
17304 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17305 /*address_p=*/false));
17306 case ARRAY_REF:
17307 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17308 args, complain, in_decl);
17309 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17310 RECUR (TREE_OPERAND (t, 1)),
17311 complain|decltype_flag));
17313 case ARRAY_NOTATION_REF:
17315 tree start_index, length, stride;
17316 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17317 args, complain, in_decl);
17318 start_index = RECUR (ARRAY_NOTATION_START (t));
17319 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17320 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17321 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17322 length, stride, TREE_TYPE (op1)));
17324 case SIZEOF_EXPR:
17325 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17326 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17327 RETURN (tsubst_copy (t, args, complain, in_decl));
17328 /* Fall through */
17330 case ALIGNOF_EXPR:
17332 tree r;
17334 op1 = TREE_OPERAND (t, 0);
17335 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17336 op1 = TREE_TYPE (op1);
17337 if (!args)
17339 /* When there are no ARGS, we are trying to evaluate a
17340 non-dependent expression from the parser. Trying to do
17341 the substitutions may not work. */
17342 if (!TYPE_P (op1))
17343 op1 = TREE_TYPE (op1);
17345 else
17347 ++cp_unevaluated_operand;
17348 ++c_inhibit_evaluation_warnings;
17349 if (TYPE_P (op1))
17350 op1 = tsubst (op1, args, complain, in_decl);
17351 else
17352 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17353 /*function_p=*/false,
17354 /*integral_constant_expression_p=*/
17355 false);
17356 --cp_unevaluated_operand;
17357 --c_inhibit_evaluation_warnings;
17359 if (TYPE_P (op1))
17360 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17361 complain & tf_error);
17362 else
17363 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17364 complain & tf_error);
17365 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17367 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17369 if (!processing_template_decl && TYPE_P (op1))
17371 r = build_min (SIZEOF_EXPR, size_type_node,
17372 build1 (NOP_EXPR, op1, error_mark_node));
17373 SIZEOF_EXPR_TYPE_P (r) = 1;
17375 else
17376 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17377 TREE_SIDE_EFFECTS (r) = 0;
17378 TREE_READONLY (r) = 1;
17380 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17382 RETURN (r);
17385 case AT_ENCODE_EXPR:
17387 op1 = TREE_OPERAND (t, 0);
17388 ++cp_unevaluated_operand;
17389 ++c_inhibit_evaluation_warnings;
17390 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17391 /*function_p=*/false,
17392 /*integral_constant_expression_p=*/false);
17393 --cp_unevaluated_operand;
17394 --c_inhibit_evaluation_warnings;
17395 RETURN (objc_build_encode_expr (op1));
17398 case NOEXCEPT_EXPR:
17399 op1 = TREE_OPERAND (t, 0);
17400 ++cp_unevaluated_operand;
17401 ++c_inhibit_evaluation_warnings;
17402 ++cp_noexcept_operand;
17403 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17404 /*function_p=*/false,
17405 /*integral_constant_expression_p=*/false);
17406 --cp_unevaluated_operand;
17407 --c_inhibit_evaluation_warnings;
17408 --cp_noexcept_operand;
17409 RETURN (finish_noexcept_expr (op1, complain));
17411 case MODOP_EXPR:
17413 warning_sentinel s(warn_div_by_zero);
17414 tree lhs = RECUR (TREE_OPERAND (t, 0));
17415 tree rhs = RECUR (TREE_OPERAND (t, 2));
17416 tree r = build_x_modify_expr
17417 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17418 complain|decltype_flag);
17419 /* TREE_NO_WARNING must be set if either the expression was
17420 parenthesized or it uses an operator such as >>= rather
17421 than plain assignment. In the former case, it was already
17422 set and must be copied. In the latter case,
17423 build_x_modify_expr sets it and it must not be reset
17424 here. */
17425 if (TREE_NO_WARNING (t))
17426 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17428 RETURN (r);
17431 case ARROW_EXPR:
17432 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17433 args, complain, in_decl);
17434 /* Remember that there was a reference to this entity. */
17435 if (DECL_P (op1)
17436 && !mark_used (op1, complain) && !(complain & tf_error))
17437 RETURN (error_mark_node);
17438 RETURN (build_x_arrow (input_location, op1, complain));
17440 case NEW_EXPR:
17442 tree placement = RECUR (TREE_OPERAND (t, 0));
17443 tree init = RECUR (TREE_OPERAND (t, 3));
17444 vec<tree, va_gc> *placement_vec;
17445 vec<tree, va_gc> *init_vec;
17446 tree ret;
17448 if (placement == NULL_TREE)
17449 placement_vec = NULL;
17450 else
17452 placement_vec = make_tree_vector ();
17453 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17454 vec_safe_push (placement_vec, TREE_VALUE (placement));
17457 /* If there was an initializer in the original tree, but it
17458 instantiated to an empty list, then we should pass a
17459 non-NULL empty vector to tell build_new that it was an
17460 empty initializer() rather than no initializer. This can
17461 only happen when the initializer is a pack expansion whose
17462 parameter packs are of length zero. */
17463 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17464 init_vec = NULL;
17465 else
17467 init_vec = make_tree_vector ();
17468 if (init == void_node)
17469 gcc_assert (init_vec != NULL);
17470 else
17472 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17473 vec_safe_push (init_vec, TREE_VALUE (init));
17477 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17478 tree op2 = RECUR (TREE_OPERAND (t, 2));
17479 ret = build_new (&placement_vec, op1, op2, &init_vec,
17480 NEW_EXPR_USE_GLOBAL (t),
17481 complain);
17483 if (placement_vec != NULL)
17484 release_tree_vector (placement_vec);
17485 if (init_vec != NULL)
17486 release_tree_vector (init_vec);
17488 RETURN (ret);
17491 case DELETE_EXPR:
17493 tree op0 = RECUR (TREE_OPERAND (t, 0));
17494 tree op1 = RECUR (TREE_OPERAND (t, 1));
17495 RETURN (delete_sanity (op0, op1,
17496 DELETE_EXPR_USE_VEC (t),
17497 DELETE_EXPR_USE_GLOBAL (t),
17498 complain));
17501 case COMPOUND_EXPR:
17503 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17504 complain & ~tf_decltype, in_decl,
17505 /*function_p=*/false,
17506 integral_constant_expression_p);
17507 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17508 op0,
17509 RECUR (TREE_OPERAND (t, 1)),
17510 complain|decltype_flag));
17513 case CALL_EXPR:
17515 tree function;
17516 vec<tree, va_gc> *call_args;
17517 unsigned int nargs, i;
17518 bool qualified_p;
17519 bool koenig_p;
17520 tree ret;
17522 function = CALL_EXPR_FN (t);
17523 /* Internal function with no arguments. */
17524 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17525 RETURN (t);
17527 /* When we parsed the expression, we determined whether or
17528 not Koenig lookup should be performed. */
17529 koenig_p = KOENIG_LOOKUP_P (t);
17530 if (function == NULL_TREE)
17532 koenig_p = false;
17533 qualified_p = false;
17535 else if (TREE_CODE (function) == SCOPE_REF)
17537 qualified_p = true;
17538 function = tsubst_qualified_id (function, args, complain, in_decl,
17539 /*done=*/false,
17540 /*address_p=*/false);
17542 else if (koenig_p && identifier_p (function))
17544 /* Do nothing; calling tsubst_copy_and_build on an identifier
17545 would incorrectly perform unqualified lookup again.
17547 Note that we can also have an IDENTIFIER_NODE if the earlier
17548 unqualified lookup found a member function; in that case
17549 koenig_p will be false and we do want to do the lookup
17550 again to find the instantiated member function.
17552 FIXME but doing that causes c++/15272, so we need to stop
17553 using IDENTIFIER_NODE in that situation. */
17554 qualified_p = false;
17556 else
17558 if (TREE_CODE (function) == COMPONENT_REF)
17560 tree op = TREE_OPERAND (function, 1);
17562 qualified_p = (TREE_CODE (op) == SCOPE_REF
17563 || (BASELINK_P (op)
17564 && BASELINK_QUALIFIED_P (op)));
17566 else
17567 qualified_p = false;
17569 if (TREE_CODE (function) == ADDR_EXPR
17570 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17571 /* Avoid error about taking the address of a constructor. */
17572 function = TREE_OPERAND (function, 0);
17574 function = tsubst_copy_and_build (function, args, complain,
17575 in_decl,
17576 !qualified_p,
17577 integral_constant_expression_p);
17579 if (BASELINK_P (function))
17580 qualified_p = true;
17583 nargs = call_expr_nargs (t);
17584 call_args = make_tree_vector ();
17585 for (i = 0; i < nargs; ++i)
17587 tree arg = CALL_EXPR_ARG (t, i);
17589 if (!PACK_EXPANSION_P (arg))
17590 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17591 else
17593 /* Expand the pack expansion and push each entry onto
17594 CALL_ARGS. */
17595 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17596 if (TREE_CODE (arg) == TREE_VEC)
17598 unsigned int len, j;
17600 len = TREE_VEC_LENGTH (arg);
17601 for (j = 0; j < len; ++j)
17603 tree value = TREE_VEC_ELT (arg, j);
17604 if (value != NULL_TREE)
17605 value = convert_from_reference (value);
17606 vec_safe_push (call_args, value);
17609 else
17611 /* A partial substitution. Add one entry. */
17612 vec_safe_push (call_args, arg);
17617 /* We do not perform argument-dependent lookup if normal
17618 lookup finds a non-function, in accordance with the
17619 expected resolution of DR 218. */
17620 if (koenig_p
17621 && ((is_overloaded_fn (function)
17622 /* If lookup found a member function, the Koenig lookup is
17623 not appropriate, even if an unqualified-name was used
17624 to denote the function. */
17625 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17626 || identifier_p (function))
17627 /* Only do this when substitution turns a dependent call
17628 into a non-dependent call. */
17629 && type_dependent_expression_p_push (t)
17630 && !any_type_dependent_arguments_p (call_args))
17631 function = perform_koenig_lookup (function, call_args, tf_none);
17633 if (function != NULL_TREE
17634 && identifier_p (function)
17635 && !any_type_dependent_arguments_p (call_args))
17637 if (koenig_p && (complain & tf_warning_or_error))
17639 /* For backwards compatibility and good diagnostics, try
17640 the unqualified lookup again if we aren't in SFINAE
17641 context. */
17642 tree unq = (tsubst_copy_and_build
17643 (function, args, complain, in_decl, true,
17644 integral_constant_expression_p));
17645 if (unq == error_mark_node)
17647 release_tree_vector (call_args);
17648 RETURN (error_mark_node);
17651 if (unq != function)
17653 /* In a lambda fn, we have to be careful to not
17654 introduce new this captures. Legacy code can't
17655 be using lambdas anyway, so it's ok to be
17656 stricter. */
17657 bool in_lambda = (current_class_type
17658 && LAMBDA_TYPE_P (current_class_type));
17659 char const *const msg
17660 = G_("%qD was not declared in this scope, "
17661 "and no declarations were found by "
17662 "argument-dependent lookup at the point "
17663 "of instantiation");
17665 bool diag = true;
17666 if (in_lambda)
17667 error_at (EXPR_LOC_OR_LOC (t, input_location),
17668 msg, function);
17669 else
17670 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17671 msg, function);
17672 if (diag)
17674 tree fn = unq;
17676 if (INDIRECT_REF_P (fn))
17677 fn = TREE_OPERAND (fn, 0);
17678 if (is_overloaded_fn (fn))
17679 fn = get_first_fn (fn);
17681 if (!DECL_P (fn))
17682 /* Can't say anything more. */;
17683 else if (DECL_CLASS_SCOPE_P (fn))
17685 location_t loc = EXPR_LOC_OR_LOC (t,
17686 input_location);
17687 inform (loc,
17688 "declarations in dependent base %qT are "
17689 "not found by unqualified lookup",
17690 DECL_CLASS_CONTEXT (fn));
17691 if (current_class_ptr)
17692 inform (loc,
17693 "use %<this->%D%> instead", function);
17694 else
17695 inform (loc,
17696 "use %<%T::%D%> instead",
17697 current_class_name, function);
17699 else
17700 inform (DECL_SOURCE_LOCATION (fn),
17701 "%qD declared here, later in the "
17702 "translation unit", fn);
17703 if (in_lambda)
17705 release_tree_vector (call_args);
17706 RETURN (error_mark_node);
17710 function = unq;
17713 if (identifier_p (function))
17715 if (complain & tf_error)
17716 unqualified_name_lookup_error (function);
17717 release_tree_vector (call_args);
17718 RETURN (error_mark_node);
17722 /* Remember that there was a reference to this entity. */
17723 if (function != NULL_TREE
17724 && DECL_P (function)
17725 && !mark_used (function, complain) && !(complain & tf_error))
17727 release_tree_vector (call_args);
17728 RETURN (error_mark_node);
17731 /* Put back tf_decltype for the actual call. */
17732 complain |= decltype_flag;
17734 if (function == NULL_TREE)
17735 switch (CALL_EXPR_IFN (t))
17737 case IFN_LAUNDER:
17738 gcc_assert (nargs == 1);
17739 if (vec_safe_length (call_args) != 1)
17741 error_at (EXPR_LOC_OR_LOC (t, input_location),
17742 "wrong number of arguments to "
17743 "%<__builtin_launder%>");
17744 ret = error_mark_node;
17746 else
17747 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17748 input_location),
17749 (*call_args)[0], complain);
17750 break;
17752 default:
17753 /* Unsupported internal function with arguments. */
17754 gcc_unreachable ();
17756 else if (TREE_CODE (function) == OFFSET_REF)
17757 ret = build_offset_ref_call_from_tree (function, &call_args,
17758 complain);
17759 else if (TREE_CODE (function) == COMPONENT_REF)
17761 tree instance = TREE_OPERAND (function, 0);
17762 tree fn = TREE_OPERAND (function, 1);
17764 if (processing_template_decl
17765 && (type_dependent_expression_p (instance)
17766 || (!BASELINK_P (fn)
17767 && TREE_CODE (fn) != FIELD_DECL)
17768 || type_dependent_expression_p (fn)
17769 || any_type_dependent_arguments_p (call_args)))
17770 ret = build_min_nt_call_vec (function, call_args);
17771 else if (!BASELINK_P (fn))
17772 ret = finish_call_expr (function, &call_args,
17773 /*disallow_virtual=*/false,
17774 /*koenig_p=*/false,
17775 complain);
17776 else
17777 ret = (build_new_method_call
17778 (instance, fn,
17779 &call_args, NULL_TREE,
17780 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17781 /*fn_p=*/NULL,
17782 complain));
17784 else
17785 ret = finish_call_expr (function, &call_args,
17786 /*disallow_virtual=*/qualified_p,
17787 koenig_p,
17788 complain);
17790 release_tree_vector (call_args);
17792 if (ret != error_mark_node)
17794 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17795 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17796 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17797 bool thk = CALL_FROM_THUNK_P (t);
17798 if (op || ord || rev || thk)
17800 function = extract_call_expr (ret);
17801 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17802 CALL_EXPR_ORDERED_ARGS (function) = ord;
17803 CALL_EXPR_REVERSE_ARGS (function) = rev;
17804 if (thk)
17806 CALL_FROM_THUNK_P (function) = true;
17807 /* The thunk location is not interesting. */
17808 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17813 RETURN (ret);
17816 case COND_EXPR:
17818 tree cond = RECUR (TREE_OPERAND (t, 0));
17819 tree folded_cond = fold_non_dependent_expr (cond);
17820 tree exp1, exp2;
17822 if (TREE_CODE (folded_cond) == INTEGER_CST)
17824 if (integer_zerop (folded_cond))
17826 ++c_inhibit_evaluation_warnings;
17827 exp1 = RECUR (TREE_OPERAND (t, 1));
17828 --c_inhibit_evaluation_warnings;
17829 exp2 = RECUR (TREE_OPERAND (t, 2));
17831 else
17833 exp1 = RECUR (TREE_OPERAND (t, 1));
17834 ++c_inhibit_evaluation_warnings;
17835 exp2 = RECUR (TREE_OPERAND (t, 2));
17836 --c_inhibit_evaluation_warnings;
17838 cond = folded_cond;
17840 else
17842 exp1 = RECUR (TREE_OPERAND (t, 1));
17843 exp2 = RECUR (TREE_OPERAND (t, 2));
17846 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17847 cond, exp1, exp2, complain));
17850 case PSEUDO_DTOR_EXPR:
17852 tree op0 = RECUR (TREE_OPERAND (t, 0));
17853 tree op1 = RECUR (TREE_OPERAND (t, 1));
17854 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17855 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17856 input_location));
17859 case TREE_LIST:
17861 tree purpose, value, chain;
17863 if (t == void_list_node)
17864 RETURN (t);
17866 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17867 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17869 /* We have pack expansions, so expand those and
17870 create a new list out of it. */
17871 tree purposevec = NULL_TREE;
17872 tree valuevec = NULL_TREE;
17873 tree chain;
17874 int i, len = -1;
17876 /* Expand the argument expressions. */
17877 if (TREE_PURPOSE (t))
17878 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17879 complain, in_decl);
17880 if (TREE_VALUE (t))
17881 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17882 complain, in_decl);
17884 /* Build the rest of the list. */
17885 chain = TREE_CHAIN (t);
17886 if (chain && chain != void_type_node)
17887 chain = RECUR (chain);
17889 /* Determine the number of arguments. */
17890 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17892 len = TREE_VEC_LENGTH (purposevec);
17893 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17895 else if (TREE_CODE (valuevec) == TREE_VEC)
17896 len = TREE_VEC_LENGTH (valuevec);
17897 else
17899 /* Since we only performed a partial substitution into
17900 the argument pack, we only RETURN (a single list
17901 node. */
17902 if (purposevec == TREE_PURPOSE (t)
17903 && valuevec == TREE_VALUE (t)
17904 && chain == TREE_CHAIN (t))
17905 RETURN (t);
17907 RETURN (tree_cons (purposevec, valuevec, chain));
17910 /* Convert the argument vectors into a TREE_LIST */
17911 i = len;
17912 while (i > 0)
17914 /* Grab the Ith values. */
17915 i--;
17916 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17917 : NULL_TREE;
17918 value
17919 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17920 : NULL_TREE;
17922 /* Build the list (backwards). */
17923 chain = tree_cons (purpose, value, chain);
17926 RETURN (chain);
17929 purpose = TREE_PURPOSE (t);
17930 if (purpose)
17931 purpose = RECUR (purpose);
17932 value = TREE_VALUE (t);
17933 if (value)
17934 value = RECUR (value);
17935 chain = TREE_CHAIN (t);
17936 if (chain && chain != void_type_node)
17937 chain = RECUR (chain);
17938 if (purpose == TREE_PURPOSE (t)
17939 && value == TREE_VALUE (t)
17940 && chain == TREE_CHAIN (t))
17941 RETURN (t);
17942 RETURN (tree_cons (purpose, value, chain));
17945 case COMPONENT_REF:
17947 tree object;
17948 tree object_type;
17949 tree member;
17950 tree r;
17952 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17953 args, complain, in_decl);
17954 /* Remember that there was a reference to this entity. */
17955 if (DECL_P (object)
17956 && !mark_used (object, complain) && !(complain & tf_error))
17957 RETURN (error_mark_node);
17958 object_type = TREE_TYPE (object);
17960 member = TREE_OPERAND (t, 1);
17961 if (BASELINK_P (member))
17962 member = tsubst_baselink (member,
17963 non_reference (TREE_TYPE (object)),
17964 args, complain, in_decl);
17965 else
17966 member = tsubst_copy (member, args, complain, in_decl);
17967 if (member == error_mark_node)
17968 RETURN (error_mark_node);
17970 if (TREE_CODE (member) == FIELD_DECL)
17972 r = finish_non_static_data_member (member, object, NULL_TREE);
17973 if (TREE_CODE (r) == COMPONENT_REF)
17974 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17975 RETURN (r);
17977 else if (type_dependent_expression_p (object))
17978 /* We can't do much here. */;
17979 else if (!CLASS_TYPE_P (object_type))
17981 if (scalarish_type_p (object_type))
17983 tree s = NULL_TREE;
17984 tree dtor = member;
17986 if (TREE_CODE (dtor) == SCOPE_REF)
17988 s = TREE_OPERAND (dtor, 0);
17989 dtor = TREE_OPERAND (dtor, 1);
17991 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17993 dtor = TREE_OPERAND (dtor, 0);
17994 if (TYPE_P (dtor))
17995 RETURN (finish_pseudo_destructor_expr
17996 (object, s, dtor, input_location));
18000 else if (TREE_CODE (member) == SCOPE_REF
18001 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18003 /* Lookup the template functions now that we know what the
18004 scope is. */
18005 tree scope = TREE_OPERAND (member, 0);
18006 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18007 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18008 member = lookup_qualified_name (scope, tmpl,
18009 /*is_type_p=*/false,
18010 /*complain=*/false);
18011 if (BASELINK_P (member))
18013 BASELINK_FUNCTIONS (member)
18014 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18015 args);
18016 member = (adjust_result_of_qualified_name_lookup
18017 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18018 object_type));
18020 else
18022 qualified_name_lookup_error (scope, tmpl, member,
18023 input_location);
18024 RETURN (error_mark_node);
18027 else if (TREE_CODE (member) == SCOPE_REF
18028 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18029 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18031 if (complain & tf_error)
18033 if (TYPE_P (TREE_OPERAND (member, 0)))
18034 error ("%qT is not a class or namespace",
18035 TREE_OPERAND (member, 0));
18036 else
18037 error ("%qD is not a class or namespace",
18038 TREE_OPERAND (member, 0));
18040 RETURN (error_mark_node);
18043 r = finish_class_member_access_expr (object, member,
18044 /*template_p=*/false,
18045 complain);
18046 if (TREE_CODE (r) == COMPONENT_REF)
18047 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18048 RETURN (r);
18051 case THROW_EXPR:
18052 RETURN (build_throw
18053 (RECUR (TREE_OPERAND (t, 0))));
18055 case CONSTRUCTOR:
18057 vec<constructor_elt, va_gc> *n;
18058 constructor_elt *ce;
18059 unsigned HOST_WIDE_INT idx;
18060 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18061 bool process_index_p;
18062 int newlen;
18063 bool need_copy_p = false;
18064 tree r;
18066 if (type == error_mark_node)
18067 RETURN (error_mark_node);
18069 /* digest_init will do the wrong thing if we let it. */
18070 if (type && TYPE_PTRMEMFUNC_P (type))
18071 RETURN (t);
18073 /* We do not want to process the index of aggregate
18074 initializers as they are identifier nodes which will be
18075 looked up by digest_init. */
18076 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18078 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18079 newlen = vec_safe_length (n);
18080 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18082 if (ce->index && process_index_p
18083 /* An identifier index is looked up in the type
18084 being initialized, not the current scope. */
18085 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18086 ce->index = RECUR (ce->index);
18088 if (PACK_EXPANSION_P (ce->value))
18090 /* Substitute into the pack expansion. */
18091 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18092 in_decl);
18094 if (ce->value == error_mark_node
18095 || PACK_EXPANSION_P (ce->value))
18097 else if (TREE_VEC_LENGTH (ce->value) == 1)
18098 /* Just move the argument into place. */
18099 ce->value = TREE_VEC_ELT (ce->value, 0);
18100 else
18102 /* Update the length of the final CONSTRUCTOR
18103 arguments vector, and note that we will need to
18104 copy.*/
18105 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18106 need_copy_p = true;
18109 else
18110 ce->value = RECUR (ce->value);
18113 if (need_copy_p)
18115 vec<constructor_elt, va_gc> *old_n = n;
18117 vec_alloc (n, newlen);
18118 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18120 if (TREE_CODE (ce->value) == TREE_VEC)
18122 int i, len = TREE_VEC_LENGTH (ce->value);
18123 for (i = 0; i < len; ++i)
18124 CONSTRUCTOR_APPEND_ELT (n, 0,
18125 TREE_VEC_ELT (ce->value, i));
18127 else
18128 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18132 r = build_constructor (init_list_type_node, n);
18133 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18135 if (TREE_HAS_CONSTRUCTOR (t))
18137 fcl_t cl = fcl_functional;
18138 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18139 cl = fcl_c99;
18140 RETURN (finish_compound_literal (type, r, complain, cl));
18143 TREE_TYPE (r) = type;
18144 RETURN (r);
18147 case TYPEID_EXPR:
18149 tree operand_0 = TREE_OPERAND (t, 0);
18150 if (TYPE_P (operand_0))
18152 operand_0 = tsubst (operand_0, args, complain, in_decl);
18153 RETURN (get_typeid (operand_0, complain));
18155 else
18157 operand_0 = RECUR (operand_0);
18158 RETURN (build_typeid (operand_0, complain));
18162 case VAR_DECL:
18163 if (!args)
18164 RETURN (t);
18165 else if (DECL_PACK_P (t))
18167 /* We don't build decls for an instantiation of a
18168 variadic capture proxy, we instantiate the elements
18169 when needed. */
18170 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18171 return RECUR (DECL_VALUE_EXPR (t));
18173 /* Fall through */
18175 case PARM_DECL:
18177 tree r = tsubst_copy (t, args, complain, in_decl);
18178 /* ??? We're doing a subset of finish_id_expression here. */
18179 if (VAR_P (r)
18180 && !processing_template_decl
18181 && !cp_unevaluated_operand
18182 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18183 && CP_DECL_THREAD_LOCAL_P (r))
18185 if (tree wrap = get_tls_wrapper_fn (r))
18186 /* Replace an evaluated use of the thread_local variable with
18187 a call to its wrapper. */
18188 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18190 else if (outer_automatic_var_p (r))
18191 r = process_outer_var_ref (r, complain);
18193 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18194 /* If the original type was a reference, we'll be wrapped in
18195 the appropriate INDIRECT_REF. */
18196 r = convert_from_reference (r);
18197 RETURN (r);
18200 case VA_ARG_EXPR:
18202 tree op0 = RECUR (TREE_OPERAND (t, 0));
18203 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18204 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18207 case OFFSETOF_EXPR:
18209 tree object_ptr
18210 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18211 in_decl, /*function_p=*/false,
18212 /*integral_constant_expression_p=*/false);
18213 RETURN (finish_offsetof (object_ptr,
18214 RECUR (TREE_OPERAND (t, 0)),
18215 EXPR_LOCATION (t)));
18218 case ADDRESSOF_EXPR:
18219 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18220 RECUR (TREE_OPERAND (t, 0)), complain));
18222 case TRAIT_EXPR:
18224 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18225 complain, in_decl);
18227 tree type2 = TRAIT_EXPR_TYPE2 (t);
18228 if (type2 && TREE_CODE (type2) == TREE_LIST)
18229 type2 = RECUR (type2);
18230 else if (type2)
18231 type2 = tsubst (type2, args, complain, in_decl);
18233 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18236 case STMT_EXPR:
18238 tree old_stmt_expr = cur_stmt_expr;
18239 tree stmt_expr = begin_stmt_expr ();
18241 cur_stmt_expr = stmt_expr;
18242 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18243 integral_constant_expression_p);
18244 stmt_expr = finish_stmt_expr (stmt_expr, false);
18245 cur_stmt_expr = old_stmt_expr;
18247 /* If the resulting list of expression statement is empty,
18248 fold it further into void_node. */
18249 if (empty_expr_stmt_p (stmt_expr))
18250 stmt_expr = void_node;
18252 RETURN (stmt_expr);
18255 case LAMBDA_EXPR:
18257 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18259 RETURN (build_lambda_object (r));
18262 case TARGET_EXPR:
18263 /* We can get here for a constant initializer of non-dependent type.
18264 FIXME stop folding in cp_parser_initializer_clause. */
18266 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18267 complain);
18268 RETURN (r);
18271 case TRANSACTION_EXPR:
18272 RETURN (tsubst_expr(t, args, complain, in_decl,
18273 integral_constant_expression_p));
18275 case PAREN_EXPR:
18276 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18278 case VEC_PERM_EXPR:
18280 tree op0 = RECUR (TREE_OPERAND (t, 0));
18281 tree op1 = RECUR (TREE_OPERAND (t, 1));
18282 tree op2 = RECUR (TREE_OPERAND (t, 2));
18283 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18284 complain));
18287 case REQUIRES_EXPR:
18288 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18290 default:
18291 /* Handle Objective-C++ constructs, if appropriate. */
18293 tree subst
18294 = objcp_tsubst_copy_and_build (t, args, complain,
18295 in_decl, /*function_p=*/false);
18296 if (subst)
18297 RETURN (subst);
18299 RETURN (tsubst_copy (t, args, complain, in_decl));
18302 #undef RECUR
18303 #undef RETURN
18304 out:
18305 input_location = loc;
18306 return retval;
18309 /* Verify that the instantiated ARGS are valid. For type arguments,
18310 make sure that the type's linkage is ok. For non-type arguments,
18311 make sure they are constants if they are integral or enumerations.
18312 Emit an error under control of COMPLAIN, and return TRUE on error. */
18314 static bool
18315 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18317 if (dependent_template_arg_p (t))
18318 return false;
18319 if (ARGUMENT_PACK_P (t))
18321 tree vec = ARGUMENT_PACK_ARGS (t);
18322 int len = TREE_VEC_LENGTH (vec);
18323 bool result = false;
18324 int i;
18326 for (i = 0; i < len; ++i)
18327 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18328 result = true;
18329 return result;
18331 else if (TYPE_P (t))
18333 /* [basic.link]: A name with no linkage (notably, the name
18334 of a class or enumeration declared in a local scope)
18335 shall not be used to declare an entity with linkage.
18336 This implies that names with no linkage cannot be used as
18337 template arguments
18339 DR 757 relaxes this restriction for C++0x. */
18340 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18341 : no_linkage_check (t, /*relaxed_p=*/false));
18343 if (nt)
18345 /* DR 488 makes use of a type with no linkage cause
18346 type deduction to fail. */
18347 if (complain & tf_error)
18349 if (TYPE_UNNAMED_P (nt))
18350 error ("%qT is/uses unnamed type", t);
18351 else
18352 error ("template argument for %qD uses local type %qT",
18353 tmpl, t);
18355 return true;
18357 /* In order to avoid all sorts of complications, we do not
18358 allow variably-modified types as template arguments. */
18359 else if (variably_modified_type_p (t, NULL_TREE))
18361 if (complain & tf_error)
18362 error ("%qT is a variably modified type", t);
18363 return true;
18366 /* Class template and alias template arguments should be OK. */
18367 else if (DECL_TYPE_TEMPLATE_P (t))
18369 /* A non-type argument of integral or enumerated type must be a
18370 constant. */
18371 else if (TREE_TYPE (t)
18372 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18373 && !REFERENCE_REF_P (t)
18374 && !TREE_CONSTANT (t))
18376 if (complain & tf_error)
18377 error ("integral expression %qE is not constant", t);
18378 return true;
18380 return false;
18383 static bool
18384 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18386 int ix, len = DECL_NTPARMS (tmpl);
18387 bool result = false;
18389 for (ix = 0; ix != len; ix++)
18391 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18392 result = true;
18394 if (result && (complain & tf_error))
18395 error (" trying to instantiate %qD", tmpl);
18396 return result;
18399 /* We're out of SFINAE context now, so generate diagnostics for the access
18400 errors we saw earlier when instantiating D from TMPL and ARGS. */
18402 static void
18403 recheck_decl_substitution (tree d, tree tmpl, tree args)
18405 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18406 tree type = TREE_TYPE (pattern);
18407 location_t loc = input_location;
18409 push_access_scope (d);
18410 push_deferring_access_checks (dk_no_deferred);
18411 input_location = DECL_SOURCE_LOCATION (pattern);
18412 tsubst (type, args, tf_warning_or_error, d);
18413 input_location = loc;
18414 pop_deferring_access_checks ();
18415 pop_access_scope (d);
18418 /* Instantiate the indicated variable, function, or alias template TMPL with
18419 the template arguments in TARG_PTR. */
18421 static tree
18422 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18424 tree targ_ptr = orig_args;
18425 tree fndecl;
18426 tree gen_tmpl;
18427 tree spec;
18428 bool access_ok = true;
18430 if (tmpl == error_mark_node)
18431 return error_mark_node;
18433 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18435 /* If this function is a clone, handle it specially. */
18436 if (DECL_CLONED_FUNCTION_P (tmpl))
18438 tree spec;
18439 tree clone;
18441 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18442 DECL_CLONED_FUNCTION. */
18443 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18444 targ_ptr, complain);
18445 if (spec == error_mark_node)
18446 return error_mark_node;
18448 /* Look for the clone. */
18449 FOR_EACH_CLONE (clone, spec)
18450 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18451 return clone;
18452 /* We should always have found the clone by now. */
18453 gcc_unreachable ();
18454 return NULL_TREE;
18457 if (targ_ptr == error_mark_node)
18458 return error_mark_node;
18460 /* Check to see if we already have this specialization. */
18461 gen_tmpl = most_general_template (tmpl);
18462 if (TMPL_ARGS_DEPTH (targ_ptr)
18463 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18464 /* targ_ptr only has the innermost template args, so add the outer ones
18465 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18466 the case of a non-dependent call within a template definition). */
18467 targ_ptr = (add_outermost_template_args
18468 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18469 targ_ptr));
18471 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18472 but it doesn't seem to be on the hot path. */
18473 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18475 gcc_assert (tmpl == gen_tmpl
18476 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18477 == spec)
18478 || fndecl == NULL_TREE);
18480 if (spec != NULL_TREE)
18482 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18484 if (complain & tf_error)
18485 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18486 return error_mark_node;
18488 return spec;
18491 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18492 complain))
18493 return error_mark_node;
18495 /* We are building a FUNCTION_DECL, during which the access of its
18496 parameters and return types have to be checked. However this
18497 FUNCTION_DECL which is the desired context for access checking
18498 is not built yet. We solve this chicken-and-egg problem by
18499 deferring all checks until we have the FUNCTION_DECL. */
18500 push_deferring_access_checks (dk_deferred);
18502 /* Instantiation of the function happens in the context of the function
18503 template, not the context of the overload resolution we're doing. */
18504 push_to_top_level ();
18505 /* If there are dependent arguments, e.g. because we're doing partial
18506 ordering, make sure processing_template_decl stays set. */
18507 if (uses_template_parms (targ_ptr))
18508 ++processing_template_decl;
18509 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18511 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18512 complain, gen_tmpl, true);
18513 push_nested_class (ctx);
18516 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18518 fndecl = NULL_TREE;
18519 if (VAR_P (pattern))
18521 /* We need to determine if we're using a partial or explicit
18522 specialization now, because the type of the variable could be
18523 different. */
18524 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18525 tree elt = most_specialized_partial_spec (tid, complain);
18526 if (elt == error_mark_node)
18527 pattern = error_mark_node;
18528 else if (elt)
18530 tree partial_tmpl = TREE_VALUE (elt);
18531 tree partial_args = TREE_PURPOSE (elt);
18532 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18533 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18537 /* Substitute template parameters to obtain the specialization. */
18538 if (fndecl == NULL_TREE)
18539 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18540 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18541 pop_nested_class ();
18542 pop_from_top_level ();
18544 if (fndecl == error_mark_node)
18546 pop_deferring_access_checks ();
18547 return error_mark_node;
18550 /* The DECL_TI_TEMPLATE should always be the immediate parent
18551 template, not the most general template. */
18552 DECL_TI_TEMPLATE (fndecl) = tmpl;
18553 DECL_TI_ARGS (fndecl) = targ_ptr;
18555 /* Now we know the specialization, compute access previously
18556 deferred. Do no access control for inheriting constructors,
18557 as we already checked access for the inherited constructor. */
18558 if (!(flag_new_inheriting_ctors
18559 && DECL_INHERITED_CTOR (fndecl)))
18561 push_access_scope (fndecl);
18562 if (!perform_deferred_access_checks (complain))
18563 access_ok = false;
18564 pop_access_scope (fndecl);
18566 pop_deferring_access_checks ();
18568 /* If we've just instantiated the main entry point for a function,
18569 instantiate all the alternate entry points as well. We do this
18570 by cloning the instantiation of the main entry point, not by
18571 instantiating the template clones. */
18572 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18573 clone_function_decl (fndecl, /*update_methods=*/false);
18575 if (!access_ok)
18577 if (!(complain & tf_error))
18579 /* Remember to reinstantiate when we're out of SFINAE so the user
18580 can see the errors. */
18581 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18583 return error_mark_node;
18585 return fndecl;
18588 /* Wrapper for instantiate_template_1. */
18590 tree
18591 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18593 tree ret;
18594 timevar_push (TV_TEMPLATE_INST);
18595 ret = instantiate_template_1 (tmpl, orig_args, complain);
18596 timevar_pop (TV_TEMPLATE_INST);
18597 return ret;
18600 /* Instantiate the alias template TMPL with ARGS. Also push a template
18601 instantiation level, which instantiate_template doesn't do because
18602 functions and variables have sufficient context established by the
18603 callers. */
18605 static tree
18606 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18608 struct pending_template *old_last_pend = last_pending_template;
18609 struct tinst_level *old_error_tinst = last_error_tinst_level;
18610 if (tmpl == error_mark_node || args == error_mark_node)
18611 return error_mark_node;
18612 tree tinst = build_tree_list (tmpl, args);
18613 if (!push_tinst_level (tinst))
18615 ggc_free (tinst);
18616 return error_mark_node;
18619 args =
18620 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18621 args, tmpl, complain,
18622 /*require_all_args=*/true,
18623 /*use_default_args=*/true);
18625 tree r = instantiate_template (tmpl, args, complain);
18626 pop_tinst_level ();
18627 /* We can't free this if a pending_template entry or last_error_tinst_level
18628 is pointing at it. */
18629 if (last_pending_template == old_last_pend
18630 && last_error_tinst_level == old_error_tinst)
18631 ggc_free (tinst);
18633 return r;
18636 /* PARM is a template parameter pack for FN. Returns true iff
18637 PARM is used in a deducible way in the argument list of FN. */
18639 static bool
18640 pack_deducible_p (tree parm, tree fn)
18642 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18643 for (; t; t = TREE_CHAIN (t))
18645 tree type = TREE_VALUE (t);
18646 tree packs;
18647 if (!PACK_EXPANSION_P (type))
18648 continue;
18649 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18650 packs; packs = TREE_CHAIN (packs))
18651 if (template_args_equal (TREE_VALUE (packs), parm))
18653 /* The template parameter pack is used in a function parameter
18654 pack. If this is the end of the parameter list, the
18655 template parameter pack is deducible. */
18656 if (TREE_CHAIN (t) == void_list_node)
18657 return true;
18658 else
18659 /* Otherwise, not. Well, it could be deduced from
18660 a non-pack parameter, but doing so would end up with
18661 a deduction mismatch, so don't bother. */
18662 return false;
18665 /* The template parameter pack isn't used in any function parameter
18666 packs, but it might be used deeper, e.g. tuple<Args...>. */
18667 return true;
18670 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18671 NARGS elements of the arguments that are being used when calling
18672 it. TARGS is a vector into which the deduced template arguments
18673 are placed.
18675 Returns either a FUNCTION_DECL for the matching specialization of FN or
18676 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18677 true, diagnostics will be printed to explain why it failed.
18679 If FN is a conversion operator, or we are trying to produce a specific
18680 specialization, RETURN_TYPE is the return type desired.
18682 The EXPLICIT_TARGS are explicit template arguments provided via a
18683 template-id.
18685 The parameter STRICT is one of:
18687 DEDUCE_CALL:
18688 We are deducing arguments for a function call, as in
18689 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18690 deducing arguments for a call to the result of a conversion
18691 function template, as in [over.call.object].
18693 DEDUCE_CONV:
18694 We are deducing arguments for a conversion function, as in
18695 [temp.deduct.conv].
18697 DEDUCE_EXACT:
18698 We are deducing arguments when doing an explicit instantiation
18699 as in [temp.explicit], when determining an explicit specialization
18700 as in [temp.expl.spec], or when taking the address of a function
18701 template, as in [temp.deduct.funcaddr]. */
18703 tree
18704 fn_type_unification (tree fn,
18705 tree explicit_targs,
18706 tree targs,
18707 const tree *args,
18708 unsigned int nargs,
18709 tree return_type,
18710 unification_kind_t strict,
18711 int flags,
18712 bool explain_p,
18713 bool decltype_p)
18715 tree parms;
18716 tree fntype;
18717 tree decl = NULL_TREE;
18718 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18719 bool ok;
18720 static int deduction_depth;
18721 struct pending_template *old_last_pend = last_pending_template;
18722 struct tinst_level *old_error_tinst = last_error_tinst_level;
18724 tree orig_fn = fn;
18725 if (flag_new_inheriting_ctors)
18726 fn = strip_inheriting_ctors (fn);
18728 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18729 tree tinst;
18730 tree r = error_mark_node;
18732 tree full_targs = targs;
18733 if (TMPL_ARGS_DEPTH (targs)
18734 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18735 full_targs = (add_outermost_template_args
18736 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18737 targs));
18739 if (decltype_p)
18740 complain |= tf_decltype;
18742 /* In C++0x, it's possible to have a function template whose type depends
18743 on itself recursively. This is most obvious with decltype, but can also
18744 occur with enumeration scope (c++/48969). So we need to catch infinite
18745 recursion and reject the substitution at deduction time; this function
18746 will return error_mark_node for any repeated substitution.
18748 This also catches excessive recursion such as when f<N> depends on
18749 f<N-1> across all integers, and returns error_mark_node for all the
18750 substitutions back up to the initial one.
18752 This is, of course, not reentrant. */
18753 if (excessive_deduction_depth)
18754 return error_mark_node;
18755 tinst = build_tree_list (fn, NULL_TREE);
18756 ++deduction_depth;
18758 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18760 fntype = TREE_TYPE (fn);
18761 if (explicit_targs)
18763 /* [temp.deduct]
18765 The specified template arguments must match the template
18766 parameters in kind (i.e., type, nontype, template), and there
18767 must not be more arguments than there are parameters;
18768 otherwise type deduction fails.
18770 Nontype arguments must match the types of the corresponding
18771 nontype template parameters, or must be convertible to the
18772 types of the corresponding nontype parameters as specified in
18773 _temp.arg.nontype_, otherwise type deduction fails.
18775 All references in the function type of the function template
18776 to the corresponding template parameters are replaced by the
18777 specified template argument values. If a substitution in a
18778 template parameter or in the function type of the function
18779 template results in an invalid type, type deduction fails. */
18780 int i, len = TREE_VEC_LENGTH (tparms);
18781 location_t loc = input_location;
18782 bool incomplete = false;
18784 if (explicit_targs == error_mark_node)
18785 goto fail;
18787 if (TMPL_ARGS_DEPTH (explicit_targs)
18788 < TMPL_ARGS_DEPTH (full_targs))
18789 explicit_targs = add_outermost_template_args (full_targs,
18790 explicit_targs);
18792 /* Adjust any explicit template arguments before entering the
18793 substitution context. */
18794 explicit_targs
18795 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18796 complain,
18797 /*require_all_args=*/false,
18798 /*use_default_args=*/false));
18799 if (explicit_targs == error_mark_node)
18800 goto fail;
18802 /* Substitute the explicit args into the function type. This is
18803 necessary so that, for instance, explicitly declared function
18804 arguments can match null pointed constants. If we were given
18805 an incomplete set of explicit args, we must not do semantic
18806 processing during substitution as we could create partial
18807 instantiations. */
18808 for (i = 0; i < len; i++)
18810 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18811 bool parameter_pack = false;
18812 tree targ = TREE_VEC_ELT (explicit_targs, i);
18814 /* Dig out the actual parm. */
18815 if (TREE_CODE (parm) == TYPE_DECL
18816 || TREE_CODE (parm) == TEMPLATE_DECL)
18818 parm = TREE_TYPE (parm);
18819 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18821 else if (TREE_CODE (parm) == PARM_DECL)
18823 parm = DECL_INITIAL (parm);
18824 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18827 if (!parameter_pack && targ == NULL_TREE)
18828 /* No explicit argument for this template parameter. */
18829 incomplete = true;
18831 if (parameter_pack && pack_deducible_p (parm, fn))
18833 /* Mark the argument pack as "incomplete". We could
18834 still deduce more arguments during unification.
18835 We remove this mark in type_unification_real. */
18836 if (targ)
18838 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18839 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18840 = ARGUMENT_PACK_ARGS (targ);
18843 /* We have some incomplete argument packs. */
18844 incomplete = true;
18848 TREE_VALUE (tinst) = explicit_targs;
18849 if (!push_tinst_level (tinst))
18851 excessive_deduction_depth = true;
18852 goto fail;
18854 processing_template_decl += incomplete;
18855 input_location = DECL_SOURCE_LOCATION (fn);
18856 /* Ignore any access checks; we'll see them again in
18857 instantiate_template and they might have the wrong
18858 access path at this point. */
18859 push_deferring_access_checks (dk_deferred);
18860 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18861 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18862 pop_deferring_access_checks ();
18863 input_location = loc;
18864 processing_template_decl -= incomplete;
18865 pop_tinst_level ();
18867 if (fntype == error_mark_node)
18868 goto fail;
18870 /* Place the explicitly specified arguments in TARGS. */
18871 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18872 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18873 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18876 /* Never do unification on the 'this' parameter. */
18877 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18879 if (return_type && strict == DEDUCE_CALL)
18881 /* We're deducing for a call to the result of a template conversion
18882 function. The parms we really want are in return_type. */
18883 if (POINTER_TYPE_P (return_type))
18884 return_type = TREE_TYPE (return_type);
18885 parms = TYPE_ARG_TYPES (return_type);
18887 else if (return_type)
18889 tree *new_args;
18891 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18892 new_args = XALLOCAVEC (tree, nargs + 1);
18893 new_args[0] = return_type;
18894 memcpy (new_args + 1, args, nargs * sizeof (tree));
18895 args = new_args;
18896 ++nargs;
18899 /* We allow incomplete unification without an error message here
18900 because the standard doesn't seem to explicitly prohibit it. Our
18901 callers must be ready to deal with unification failures in any
18902 event. */
18904 TREE_VALUE (tinst) = targs;
18905 /* If we aren't explaining yet, push tinst context so we can see where
18906 any errors (e.g. from class instantiations triggered by instantiation
18907 of default template arguments) come from. If we are explaining, this
18908 context is redundant. */
18909 if (!explain_p && !push_tinst_level (tinst))
18911 excessive_deduction_depth = true;
18912 goto fail;
18915 /* type_unification_real will pass back any access checks from default
18916 template argument substitution. */
18917 vec<deferred_access_check, va_gc> *checks;
18918 checks = NULL;
18920 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18921 full_targs, parms, args, nargs, /*subr=*/0,
18922 strict, flags, &checks, explain_p);
18923 if (!explain_p)
18924 pop_tinst_level ();
18925 if (!ok)
18926 goto fail;
18928 /* Now that we have bindings for all of the template arguments,
18929 ensure that the arguments deduced for the template template
18930 parameters have compatible template parameter lists. We cannot
18931 check this property before we have deduced all template
18932 arguments, because the template parameter types of a template
18933 template parameter might depend on prior template parameters
18934 deduced after the template template parameter. The following
18935 ill-formed example illustrates this issue:
18937 template<typename T, template<T> class C> void f(C<5>, T);
18939 template<int N> struct X {};
18941 void g() {
18942 f(X<5>(), 5l); // error: template argument deduction fails
18945 The template parameter list of 'C' depends on the template type
18946 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18947 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18948 time that we deduce 'C'. */
18949 if (!template_template_parm_bindings_ok_p
18950 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18952 unify_inconsistent_template_template_parameters (explain_p);
18953 goto fail;
18956 /* All is well so far. Now, check:
18958 [temp.deduct]
18960 When all template arguments have been deduced, all uses of
18961 template parameters in nondeduced contexts are replaced with
18962 the corresponding deduced argument values. If the
18963 substitution results in an invalid type, as described above,
18964 type deduction fails. */
18965 TREE_VALUE (tinst) = targs;
18966 if (!push_tinst_level (tinst))
18968 excessive_deduction_depth = true;
18969 goto fail;
18972 /* Also collect access checks from the instantiation. */
18973 reopen_deferring_access_checks (checks);
18975 decl = instantiate_template (fn, targs, complain);
18977 checks = get_deferred_access_checks ();
18978 pop_deferring_access_checks ();
18980 pop_tinst_level ();
18982 if (decl == error_mark_node)
18983 goto fail;
18985 /* Now perform any access checks encountered during substitution. */
18986 push_access_scope (decl);
18987 ok = perform_access_checks (checks, complain);
18988 pop_access_scope (decl);
18989 if (!ok)
18990 goto fail;
18992 /* If we're looking for an exact match, check that what we got
18993 is indeed an exact match. It might not be if some template
18994 parameters are used in non-deduced contexts. But don't check
18995 for an exact match if we have dependent template arguments;
18996 in that case we're doing partial ordering, and we already know
18997 that we have two candidates that will provide the actual type. */
18998 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19000 tree substed = TREE_TYPE (decl);
19001 unsigned int i;
19003 tree sarg
19004 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19005 if (return_type)
19006 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19007 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19008 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19010 unify_type_mismatch (explain_p, args[i],
19011 TREE_VALUE (sarg));
19012 goto fail;
19016 /* After doing deduction with the inherited constructor, actually return an
19017 instantiation of the inheriting constructor. */
19018 if (orig_fn != fn)
19019 decl = instantiate_template (orig_fn, targs, complain);
19021 r = decl;
19023 fail:
19024 --deduction_depth;
19025 if (excessive_deduction_depth)
19027 if (deduction_depth == 0)
19028 /* Reset once we're all the way out. */
19029 excessive_deduction_depth = false;
19032 /* We can't free this if a pending_template entry or last_error_tinst_level
19033 is pointing at it. */
19034 if (last_pending_template == old_last_pend
19035 && last_error_tinst_level == old_error_tinst)
19036 ggc_free (tinst);
19038 return r;
19041 /* Adjust types before performing type deduction, as described in
19042 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19043 sections are symmetric. PARM is the type of a function parameter
19044 or the return type of the conversion function. ARG is the type of
19045 the argument passed to the call, or the type of the value
19046 initialized with the result of the conversion function.
19047 ARG_EXPR is the original argument expression, which may be null. */
19049 static int
19050 maybe_adjust_types_for_deduction (unification_kind_t strict,
19051 tree* parm,
19052 tree* arg,
19053 tree arg_expr)
19055 int result = 0;
19057 switch (strict)
19059 case DEDUCE_CALL:
19060 break;
19062 case DEDUCE_CONV:
19063 /* Swap PARM and ARG throughout the remainder of this
19064 function; the handling is precisely symmetric since PARM
19065 will initialize ARG rather than vice versa. */
19066 std::swap (parm, arg);
19067 break;
19069 case DEDUCE_EXACT:
19070 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19071 too, but here handle it by stripping the reference from PARM
19072 rather than by adding it to ARG. */
19073 if (TREE_CODE (*parm) == REFERENCE_TYPE
19074 && TYPE_REF_IS_RVALUE (*parm)
19075 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19076 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19077 && TREE_CODE (*arg) == REFERENCE_TYPE
19078 && !TYPE_REF_IS_RVALUE (*arg))
19079 *parm = TREE_TYPE (*parm);
19080 /* Nothing else to do in this case. */
19081 return 0;
19083 default:
19084 gcc_unreachable ();
19087 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19089 /* [temp.deduct.call]
19091 If P is not a reference type:
19093 --If A is an array type, the pointer type produced by the
19094 array-to-pointer standard conversion (_conv.array_) is
19095 used in place of A for type deduction; otherwise,
19097 --If A is a function type, the pointer type produced by
19098 the function-to-pointer standard conversion
19099 (_conv.func_) is used in place of A for type deduction;
19100 otherwise,
19102 --If A is a cv-qualified type, the top level
19103 cv-qualifiers of A's type are ignored for type
19104 deduction. */
19105 if (TREE_CODE (*arg) == ARRAY_TYPE)
19106 *arg = build_pointer_type (TREE_TYPE (*arg));
19107 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19108 *arg = build_pointer_type (*arg);
19109 else
19110 *arg = TYPE_MAIN_VARIANT (*arg);
19113 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19114 reference to a cv-unqualified template parameter that does not represent a
19115 template parameter of a class template (during class template argument
19116 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19117 an lvalue, the type "lvalue reference to A" is used in place of A for type
19118 deduction. */
19119 if (TREE_CODE (*parm) == REFERENCE_TYPE
19120 && TYPE_REF_IS_RVALUE (*parm)
19121 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19122 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19123 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19124 && (arg_expr ? lvalue_p (arg_expr)
19125 /* try_one_overload doesn't provide an arg_expr, but
19126 functions are always lvalues. */
19127 : TREE_CODE (*arg) == FUNCTION_TYPE))
19128 *arg = build_reference_type (*arg);
19130 /* [temp.deduct.call]
19132 If P is a cv-qualified type, the top level cv-qualifiers
19133 of P's type are ignored for type deduction. If P is a
19134 reference type, the type referred to by P is used for
19135 type deduction. */
19136 *parm = TYPE_MAIN_VARIANT (*parm);
19137 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19139 *parm = TREE_TYPE (*parm);
19140 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19143 /* DR 322. For conversion deduction, remove a reference type on parm
19144 too (which has been swapped into ARG). */
19145 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19146 *arg = TREE_TYPE (*arg);
19148 return result;
19151 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19152 template which does contain any deducible template parameters; check if
19153 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19154 unify_one_argument. */
19156 static int
19157 check_non_deducible_conversion (tree parm, tree arg, int strict,
19158 int flags, bool explain_p)
19160 tree type;
19162 if (!TYPE_P (arg))
19163 type = TREE_TYPE (arg);
19164 else
19165 type = arg;
19167 if (same_type_p (parm, type))
19168 return unify_success (explain_p);
19170 if (strict == DEDUCE_CONV)
19172 if (can_convert_arg (type, parm, NULL_TREE, flags,
19173 explain_p ? tf_warning_or_error : tf_none))
19174 return unify_success (explain_p);
19176 else if (strict != DEDUCE_EXACT)
19178 if (can_convert_arg (parm, type,
19179 TYPE_P (arg) ? NULL_TREE : arg,
19180 flags, explain_p ? tf_warning_or_error : tf_none))
19181 return unify_success (explain_p);
19184 if (strict == DEDUCE_EXACT)
19185 return unify_type_mismatch (explain_p, parm, arg);
19186 else
19187 return unify_arg_conversion (explain_p, parm, type, arg);
19190 static bool uses_deducible_template_parms (tree type);
19192 /* Returns true iff the expression EXPR is one from which a template
19193 argument can be deduced. In other words, if it's an undecorated
19194 use of a template non-type parameter. */
19196 static bool
19197 deducible_expression (tree expr)
19199 /* Strip implicit conversions. */
19200 while (CONVERT_EXPR_P (expr))
19201 expr = TREE_OPERAND (expr, 0);
19202 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19205 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19206 deducible way; that is, if it has a max value of <PARM> - 1. */
19208 static bool
19209 deducible_array_bound (tree domain)
19211 if (domain == NULL_TREE)
19212 return false;
19214 tree max = TYPE_MAX_VALUE (domain);
19215 if (TREE_CODE (max) != MINUS_EXPR)
19216 return false;
19218 return deducible_expression (TREE_OPERAND (max, 0));
19221 /* Returns true iff the template arguments ARGS use a template parameter
19222 in a deducible way. */
19224 static bool
19225 deducible_template_args (tree args)
19227 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19229 bool deducible;
19230 tree elt = TREE_VEC_ELT (args, i);
19231 if (ARGUMENT_PACK_P (elt))
19232 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19233 else
19235 if (PACK_EXPANSION_P (elt))
19236 elt = PACK_EXPANSION_PATTERN (elt);
19237 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19238 deducible = true;
19239 else if (TYPE_P (elt))
19240 deducible = uses_deducible_template_parms (elt);
19241 else
19242 deducible = deducible_expression (elt);
19244 if (deducible)
19245 return true;
19247 return false;
19250 /* Returns true iff TYPE contains any deducible references to template
19251 parameters, as per 14.8.2.5. */
19253 static bool
19254 uses_deducible_template_parms (tree type)
19256 if (PACK_EXPANSION_P (type))
19257 type = PACK_EXPANSION_PATTERN (type);
19259 /* T
19260 cv-list T
19261 TT<T>
19262 TT<i>
19263 TT<> */
19264 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19265 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19266 return true;
19268 /* T*
19270 T&& */
19271 if (POINTER_TYPE_P (type))
19272 return uses_deducible_template_parms (TREE_TYPE (type));
19274 /* T[integer-constant ]
19275 type [i] */
19276 if (TREE_CODE (type) == ARRAY_TYPE)
19277 return (uses_deducible_template_parms (TREE_TYPE (type))
19278 || deducible_array_bound (TYPE_DOMAIN (type)));
19280 /* T type ::*
19281 type T::*
19282 T T::*
19283 T (type ::*)()
19284 type (T::*)()
19285 type (type ::*)(T)
19286 type (T::*)(T)
19287 T (type ::*)(T)
19288 T (T::*)()
19289 T (T::*)(T) */
19290 if (TYPE_PTRMEM_P (type))
19291 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19292 || (uses_deducible_template_parms
19293 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19295 /* template-name <T> (where template-name refers to a class template)
19296 template-name <i> (where template-name refers to a class template) */
19297 if (CLASS_TYPE_P (type)
19298 && CLASSTYPE_TEMPLATE_INFO (type)
19299 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19300 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19301 (CLASSTYPE_TI_ARGS (type)));
19303 /* type (T)
19305 T(T) */
19306 if (TREE_CODE (type) == FUNCTION_TYPE
19307 || TREE_CODE (type) == METHOD_TYPE)
19309 if (uses_deducible_template_parms (TREE_TYPE (type)))
19310 return true;
19311 tree parm = TYPE_ARG_TYPES (type);
19312 if (TREE_CODE (type) == METHOD_TYPE)
19313 parm = TREE_CHAIN (parm);
19314 for (; parm; parm = TREE_CHAIN (parm))
19315 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19316 return true;
19319 return false;
19322 /* Subroutine of type_unification_real and unify_pack_expansion to
19323 handle unification of a single P/A pair. Parameters are as
19324 for those functions. */
19326 static int
19327 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19328 int subr, unification_kind_t strict,
19329 bool explain_p)
19331 tree arg_expr = NULL_TREE;
19332 int arg_strict;
19334 if (arg == error_mark_node || parm == error_mark_node)
19335 return unify_invalid (explain_p);
19336 if (arg == unknown_type_node)
19337 /* We can't deduce anything from this, but we might get all the
19338 template args from other function args. */
19339 return unify_success (explain_p);
19341 /* Implicit conversions (Clause 4) will be performed on a function
19342 argument to convert it to the type of the corresponding function
19343 parameter if the parameter type contains no template-parameters that
19344 participate in template argument deduction. */
19345 if (strict != DEDUCE_EXACT
19346 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19347 /* For function parameters with no deducible template parameters,
19348 just return. We'll check non-dependent conversions later. */
19349 return unify_success (explain_p);
19351 switch (strict)
19353 case DEDUCE_CALL:
19354 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19355 | UNIFY_ALLOW_MORE_CV_QUAL
19356 | UNIFY_ALLOW_DERIVED);
19357 break;
19359 case DEDUCE_CONV:
19360 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19361 break;
19363 case DEDUCE_EXACT:
19364 arg_strict = UNIFY_ALLOW_NONE;
19365 break;
19367 default:
19368 gcc_unreachable ();
19371 /* We only do these transformations if this is the top-level
19372 parameter_type_list in a call or declaration matching; in other
19373 situations (nested function declarators, template argument lists) we
19374 won't be comparing a type to an expression, and we don't do any type
19375 adjustments. */
19376 if (!subr)
19378 if (!TYPE_P (arg))
19380 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19381 if (type_unknown_p (arg))
19383 /* [temp.deduct.type] A template-argument can be
19384 deduced from a pointer to function or pointer
19385 to member function argument if the set of
19386 overloaded functions does not contain function
19387 templates and at most one of a set of
19388 overloaded functions provides a unique
19389 match. */
19390 resolve_overloaded_unification (tparms, targs, parm,
19391 arg, strict,
19392 arg_strict, explain_p);
19393 /* If a unique match was not found, this is a
19394 non-deduced context, so we still succeed. */
19395 return unify_success (explain_p);
19398 arg_expr = arg;
19399 arg = unlowered_expr_type (arg);
19400 if (arg == error_mark_node)
19401 return unify_invalid (explain_p);
19404 arg_strict |=
19405 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19407 else
19408 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19409 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19410 return unify_template_argument_mismatch (explain_p, parm, arg);
19412 /* For deduction from an init-list we need the actual list. */
19413 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19414 arg = arg_expr;
19415 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19418 /* for_each_template_parm callback that always returns 0. */
19420 static int
19421 zero_r (tree, void *)
19423 return 0;
19426 /* for_each_template_parm any_fn callback to handle deduction of a template
19427 type argument from the type of an array bound. */
19429 static int
19430 array_deduction_r (tree t, void *data)
19432 tree_pair_p d = (tree_pair_p)data;
19433 tree &tparms = d->purpose;
19434 tree &targs = d->value;
19436 if (TREE_CODE (t) == ARRAY_TYPE)
19437 if (tree dom = TYPE_DOMAIN (t))
19438 if (tree max = TYPE_MAX_VALUE (dom))
19440 if (TREE_CODE (max) == MINUS_EXPR)
19441 max = TREE_OPERAND (max, 0);
19442 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19443 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19444 UNIFY_ALLOW_NONE, /*explain*/false);
19447 /* Keep walking. */
19448 return 0;
19451 /* Try to deduce any not-yet-deduced template type arguments from the type of
19452 an array bound. This is handled separately from unify because 14.8.2.5 says
19453 "The type of a type parameter is only deduced from an array bound if it is
19454 not otherwise deduced." */
19456 static void
19457 try_array_deduction (tree tparms, tree targs, tree parm)
19459 tree_pair_s data = { tparms, targs };
19460 hash_set<tree> visited;
19461 for_each_template_parm (parm, zero_r, &data, &visited,
19462 /*nondeduced*/false, array_deduction_r);
19465 /* Most parms like fn_type_unification.
19467 If SUBR is 1, we're being called recursively (to unify the
19468 arguments of a function or method parameter of a function
19469 template).
19471 CHECKS is a pointer to a vector of access checks encountered while
19472 substituting default template arguments. */
19474 static int
19475 type_unification_real (tree tparms,
19476 tree full_targs,
19477 tree xparms,
19478 const tree *xargs,
19479 unsigned int xnargs,
19480 int subr,
19481 unification_kind_t strict,
19482 int flags,
19483 vec<deferred_access_check, va_gc> **checks,
19484 bool explain_p)
19486 tree parm, arg;
19487 int i;
19488 int ntparms = TREE_VEC_LENGTH (tparms);
19489 int saw_undeduced = 0;
19490 tree parms;
19491 const tree *args;
19492 unsigned int nargs;
19493 unsigned int ia;
19495 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19496 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19497 gcc_assert (ntparms > 0);
19499 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19501 /* Reset the number of non-defaulted template arguments contained
19502 in TARGS. */
19503 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19505 again:
19506 parms = xparms;
19507 args = xargs;
19508 nargs = xnargs;
19510 ia = 0;
19511 while (parms && parms != void_list_node
19512 && ia < nargs)
19514 parm = TREE_VALUE (parms);
19516 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19517 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19518 /* For a function parameter pack that occurs at the end of the
19519 parameter-declaration-list, the type A of each remaining
19520 argument of the call is compared with the type P of the
19521 declarator-id of the function parameter pack. */
19522 break;
19524 parms = TREE_CHAIN (parms);
19526 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19527 /* For a function parameter pack that does not occur at the
19528 end of the parameter-declaration-list, the type of the
19529 parameter pack is a non-deduced context. */
19530 continue;
19532 arg = args[ia];
19533 ++ia;
19535 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19536 explain_p))
19537 return 1;
19540 if (parms
19541 && parms != void_list_node
19542 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19544 /* Unify the remaining arguments with the pack expansion type. */
19545 tree argvec;
19546 tree parmvec = make_tree_vec (1);
19548 /* Allocate a TREE_VEC and copy in all of the arguments */
19549 argvec = make_tree_vec (nargs - ia);
19550 for (i = 0; ia < nargs; ++ia, ++i)
19551 TREE_VEC_ELT (argvec, i) = args[ia];
19553 /* Copy the parameter into parmvec. */
19554 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19555 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19556 /*subr=*/subr, explain_p))
19557 return 1;
19559 /* Advance to the end of the list of parameters. */
19560 parms = TREE_CHAIN (parms);
19563 /* Fail if we've reached the end of the parm list, and more args
19564 are present, and the parm list isn't variadic. */
19565 if (ia < nargs && parms == void_list_node)
19566 return unify_too_many_arguments (explain_p, nargs, ia);
19567 /* Fail if parms are left and they don't have default values and
19568 they aren't all deduced as empty packs (c++/57397). This is
19569 consistent with sufficient_parms_p. */
19570 if (parms && parms != void_list_node
19571 && TREE_PURPOSE (parms) == NULL_TREE)
19573 unsigned int count = nargs;
19574 tree p = parms;
19575 bool type_pack_p;
19578 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19579 if (!type_pack_p)
19580 count++;
19581 p = TREE_CHAIN (p);
19583 while (p && p != void_list_node);
19584 if (count != nargs)
19585 return unify_too_few_arguments (explain_p, ia, count,
19586 type_pack_p);
19589 if (!subr)
19591 tsubst_flags_t complain = (explain_p
19592 ? tf_warning_or_error
19593 : tf_none);
19594 bool tried_array_deduction = (cxx_dialect < cxx17);
19596 for (i = 0; i < ntparms; i++)
19598 tree targ = TREE_VEC_ELT (targs, i);
19599 tree tparm = TREE_VEC_ELT (tparms, i);
19601 /* Clear the "incomplete" flags on all argument packs now so that
19602 substituting them into later default arguments works. */
19603 if (targ && ARGUMENT_PACK_P (targ))
19605 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19606 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19609 if (targ || tparm == error_mark_node)
19610 continue;
19611 tparm = TREE_VALUE (tparm);
19613 if (TREE_CODE (tparm) == TYPE_DECL
19614 && !tried_array_deduction)
19616 try_array_deduction (tparms, targs, xparms);
19617 tried_array_deduction = true;
19618 if (TREE_VEC_ELT (targs, i))
19619 continue;
19622 /* If this is an undeduced nontype parameter that depends on
19623 a type parameter, try another pass; its type may have been
19624 deduced from a later argument than the one from which
19625 this parameter can be deduced. */
19626 if (TREE_CODE (tparm) == PARM_DECL
19627 && uses_template_parms (TREE_TYPE (tparm))
19628 && saw_undeduced < 2)
19630 saw_undeduced = 1;
19631 continue;
19634 /* Core issue #226 (C++0x) [temp.deduct]:
19636 If a template argument has not been deduced, its
19637 default template argument, if any, is used.
19639 When we are in C++98 mode, TREE_PURPOSE will either
19640 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19641 to explicitly check cxx_dialect here. */
19642 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19643 /* OK, there is a default argument. Wait until after the
19644 conversion check to do substitution. */
19645 continue;
19647 /* If the type parameter is a parameter pack, then it will
19648 be deduced to an empty parameter pack. */
19649 if (template_parameter_pack_p (tparm))
19651 tree arg;
19653 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19655 arg = make_node (NONTYPE_ARGUMENT_PACK);
19656 TREE_CONSTANT (arg) = 1;
19658 else
19659 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19661 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19663 TREE_VEC_ELT (targs, i) = arg;
19664 continue;
19667 return unify_parameter_deduction_failure (explain_p, tparm);
19670 /* DR 1391: All parameters have args, now check non-dependent parms for
19671 convertibility. */
19672 if (saw_undeduced < 2)
19673 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19674 parms && parms != void_list_node && ia < nargs; )
19676 parm = TREE_VALUE (parms);
19678 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19679 && (!TREE_CHAIN (parms)
19680 || TREE_CHAIN (parms) == void_list_node))
19681 /* For a function parameter pack that occurs at the end of the
19682 parameter-declaration-list, the type A of each remaining
19683 argument of the call is compared with the type P of the
19684 declarator-id of the function parameter pack. */
19685 break;
19687 parms = TREE_CHAIN (parms);
19689 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19690 /* For a function parameter pack that does not occur at the
19691 end of the parameter-declaration-list, the type of the
19692 parameter pack is a non-deduced context. */
19693 continue;
19695 arg = args[ia];
19696 ++ia;
19698 if (uses_template_parms (parm))
19699 continue;
19700 if (check_non_deducible_conversion (parm, arg, strict, flags,
19701 explain_p))
19702 return 1;
19705 /* Now substitute into the default template arguments. */
19706 for (i = 0; i < ntparms; i++)
19708 tree targ = TREE_VEC_ELT (targs, i);
19709 tree tparm = TREE_VEC_ELT (tparms, i);
19711 if (targ || tparm == error_mark_node)
19712 continue;
19713 tree parm = TREE_VALUE (tparm);
19715 if (TREE_CODE (parm) == PARM_DECL
19716 && uses_template_parms (TREE_TYPE (parm))
19717 && saw_undeduced < 2)
19718 continue;
19720 tree arg = TREE_PURPOSE (tparm);
19721 reopen_deferring_access_checks (*checks);
19722 location_t save_loc = input_location;
19723 if (DECL_P (parm))
19724 input_location = DECL_SOURCE_LOCATION (parm);
19725 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19726 if (!uses_template_parms (arg))
19727 arg = convert_template_argument (parm, arg, full_targs, complain,
19728 i, NULL_TREE);
19729 else if (saw_undeduced < 2)
19730 arg = NULL_TREE;
19731 else
19732 arg = error_mark_node;
19733 input_location = save_loc;
19734 *checks = get_deferred_access_checks ();
19735 pop_deferring_access_checks ();
19736 if (arg == error_mark_node)
19737 return 1;
19738 else if (arg)
19740 TREE_VEC_ELT (targs, i) = arg;
19741 /* The position of the first default template argument,
19742 is also the number of non-defaulted arguments in TARGS.
19743 Record that. */
19744 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19745 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19749 if (saw_undeduced++ == 1)
19750 goto again;
19753 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19754 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19756 return unify_success (explain_p);
19759 /* Subroutine of type_unification_real. Args are like the variables
19760 at the call site. ARG is an overloaded function (or template-id);
19761 we try deducing template args from each of the overloads, and if
19762 only one succeeds, we go with that. Modifies TARGS and returns
19763 true on success. */
19765 static bool
19766 resolve_overloaded_unification (tree tparms,
19767 tree targs,
19768 tree parm,
19769 tree arg,
19770 unification_kind_t strict,
19771 int sub_strict,
19772 bool explain_p)
19774 tree tempargs = copy_node (targs);
19775 int good = 0;
19776 tree goodfn = NULL_TREE;
19777 bool addr_p;
19779 if (TREE_CODE (arg) == ADDR_EXPR)
19781 arg = TREE_OPERAND (arg, 0);
19782 addr_p = true;
19784 else
19785 addr_p = false;
19787 if (TREE_CODE (arg) == COMPONENT_REF)
19788 /* Handle `&x' where `x' is some static or non-static member
19789 function name. */
19790 arg = TREE_OPERAND (arg, 1);
19792 if (TREE_CODE (arg) == OFFSET_REF)
19793 arg = TREE_OPERAND (arg, 1);
19795 /* Strip baselink information. */
19796 if (BASELINK_P (arg))
19797 arg = BASELINK_FUNCTIONS (arg);
19799 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19801 /* If we got some explicit template args, we need to plug them into
19802 the affected templates before we try to unify, in case the
19803 explicit args will completely resolve the templates in question. */
19805 int ok = 0;
19806 tree expl_subargs = TREE_OPERAND (arg, 1);
19807 arg = TREE_OPERAND (arg, 0);
19809 for (lkp_iterator iter (arg); iter; ++iter)
19811 tree fn = *iter;
19812 tree subargs, elem;
19814 if (TREE_CODE (fn) != TEMPLATE_DECL)
19815 continue;
19817 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19818 expl_subargs, NULL_TREE, tf_none,
19819 /*require_all_args=*/true,
19820 /*use_default_args=*/true);
19821 if (subargs != error_mark_node
19822 && !any_dependent_template_arguments_p (subargs))
19824 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19825 if (try_one_overload (tparms, targs, tempargs, parm,
19826 elem, strict, sub_strict, addr_p, explain_p)
19827 && (!goodfn || !same_type_p (goodfn, elem)))
19829 goodfn = elem;
19830 ++good;
19833 else if (subargs)
19834 ++ok;
19836 /* If no templates (or more than one) are fully resolved by the
19837 explicit arguments, this template-id is a non-deduced context; it
19838 could still be OK if we deduce all template arguments for the
19839 enclosing call through other arguments. */
19840 if (good != 1)
19841 good = ok;
19843 else if (TREE_CODE (arg) != OVERLOAD
19844 && TREE_CODE (arg) != FUNCTION_DECL)
19845 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19846 -- but the deduction does not succeed because the expression is
19847 not just the function on its own. */
19848 return false;
19849 else
19850 for (lkp_iterator iter (arg); iter; ++iter)
19852 tree fn = *iter;
19853 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19854 strict, sub_strict, addr_p, explain_p)
19855 && (!goodfn || !decls_match (goodfn, fn)))
19857 goodfn = fn;
19858 ++good;
19862 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19863 to function or pointer to member function argument if the set of
19864 overloaded functions does not contain function templates and at most
19865 one of a set of overloaded functions provides a unique match.
19867 So if we found multiple possibilities, we return success but don't
19868 deduce anything. */
19870 if (good == 1)
19872 int i = TREE_VEC_LENGTH (targs);
19873 for (; i--; )
19874 if (TREE_VEC_ELT (tempargs, i))
19876 tree old = TREE_VEC_ELT (targs, i);
19877 tree new_ = TREE_VEC_ELT (tempargs, i);
19878 if (new_ && old && ARGUMENT_PACK_P (old)
19879 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19880 /* Don't forget explicit template arguments in a pack. */
19881 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19882 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19883 TREE_VEC_ELT (targs, i) = new_;
19886 if (good)
19887 return true;
19889 return false;
19892 /* Core DR 115: In contexts where deduction is done and fails, or in
19893 contexts where deduction is not done, if a template argument list is
19894 specified and it, along with any default template arguments, identifies
19895 a single function template specialization, then the template-id is an
19896 lvalue for the function template specialization. */
19898 tree
19899 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19901 tree expr, offset, baselink;
19902 bool addr;
19904 if (!type_unknown_p (orig_expr))
19905 return orig_expr;
19907 expr = orig_expr;
19908 addr = false;
19909 offset = NULL_TREE;
19910 baselink = NULL_TREE;
19912 if (TREE_CODE (expr) == ADDR_EXPR)
19914 expr = TREE_OPERAND (expr, 0);
19915 addr = true;
19917 if (TREE_CODE (expr) == OFFSET_REF)
19919 offset = expr;
19920 expr = TREE_OPERAND (expr, 1);
19922 if (BASELINK_P (expr))
19924 baselink = expr;
19925 expr = BASELINK_FUNCTIONS (expr);
19928 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19930 int good = 0;
19931 tree goodfn = NULL_TREE;
19933 /* If we got some explicit template args, we need to plug them into
19934 the affected templates before we try to unify, in case the
19935 explicit args will completely resolve the templates in question. */
19937 tree expl_subargs = TREE_OPERAND (expr, 1);
19938 tree arg = TREE_OPERAND (expr, 0);
19939 tree badfn = NULL_TREE;
19940 tree badargs = NULL_TREE;
19942 for (lkp_iterator iter (arg); iter; ++iter)
19944 tree fn = *iter;
19945 tree subargs, elem;
19947 if (TREE_CODE (fn) != TEMPLATE_DECL)
19948 continue;
19950 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19951 expl_subargs, NULL_TREE, tf_none,
19952 /*require_all_args=*/true,
19953 /*use_default_args=*/true);
19954 if (subargs != error_mark_node
19955 && !any_dependent_template_arguments_p (subargs))
19957 elem = instantiate_template (fn, subargs, tf_none);
19958 if (elem == error_mark_node)
19960 badfn = fn;
19961 badargs = subargs;
19963 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19965 goodfn = elem;
19966 ++good;
19970 if (good == 1)
19972 mark_used (goodfn);
19973 expr = goodfn;
19974 if (baselink)
19975 expr = build_baselink (BASELINK_BINFO (baselink),
19976 BASELINK_ACCESS_BINFO (baselink),
19977 expr, BASELINK_OPTYPE (baselink));
19978 if (offset)
19980 tree base
19981 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19982 expr = build_offset_ref (base, expr, addr, complain);
19984 if (addr)
19985 expr = cp_build_addr_expr (expr, complain);
19986 return expr;
19988 else if (good == 0 && badargs && (complain & tf_error))
19989 /* There were no good options and at least one bad one, so let the
19990 user know what the problem is. */
19991 instantiate_template (badfn, badargs, complain);
19993 return orig_expr;
19996 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19997 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19998 different overloads deduce different arguments for a given parm.
19999 ADDR_P is true if the expression for which deduction is being
20000 performed was of the form "& fn" rather than simply "fn".
20002 Returns 1 on success. */
20004 static int
20005 try_one_overload (tree tparms,
20006 tree orig_targs,
20007 tree targs,
20008 tree parm,
20009 tree arg,
20010 unification_kind_t strict,
20011 int sub_strict,
20012 bool addr_p,
20013 bool explain_p)
20015 int nargs;
20016 tree tempargs;
20017 int i;
20019 if (arg == error_mark_node)
20020 return 0;
20022 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20023 to function or pointer to member function argument if the set of
20024 overloaded functions does not contain function templates and at most
20025 one of a set of overloaded functions provides a unique match.
20027 So if this is a template, just return success. */
20029 if (uses_template_parms (arg))
20030 return 1;
20032 if (TREE_CODE (arg) == METHOD_TYPE)
20033 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20034 else if (addr_p)
20035 arg = build_pointer_type (arg);
20037 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20039 /* We don't copy orig_targs for this because if we have already deduced
20040 some template args from previous args, unify would complain when we
20041 try to deduce a template parameter for the same argument, even though
20042 there isn't really a conflict. */
20043 nargs = TREE_VEC_LENGTH (targs);
20044 tempargs = make_tree_vec (nargs);
20046 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20047 return 0;
20049 /* First make sure we didn't deduce anything that conflicts with
20050 explicitly specified args. */
20051 for (i = nargs; i--; )
20053 tree elt = TREE_VEC_ELT (tempargs, i);
20054 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20056 if (!elt)
20057 /*NOP*/;
20058 else if (uses_template_parms (elt))
20059 /* Since we're unifying against ourselves, we will fill in
20060 template args used in the function parm list with our own
20061 template parms. Discard them. */
20062 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20063 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20065 /* Check that the argument at each index of the deduced argument pack
20066 is equivalent to the corresponding explicitly specified argument.
20067 We may have deduced more arguments than were explicitly specified,
20068 and that's OK. */
20070 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20071 that's wrong if we deduce the same argument pack from multiple
20072 function arguments: it's only incomplete the first time. */
20074 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20075 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20077 if (TREE_VEC_LENGTH (deduced_pack)
20078 < TREE_VEC_LENGTH (explicit_pack))
20079 return 0;
20081 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20082 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20083 TREE_VEC_ELT (deduced_pack, j)))
20084 return 0;
20086 else if (oldelt && !template_args_equal (oldelt, elt))
20087 return 0;
20090 for (i = nargs; i--; )
20092 tree elt = TREE_VEC_ELT (tempargs, i);
20094 if (elt)
20095 TREE_VEC_ELT (targs, i) = elt;
20098 return 1;
20101 /* PARM is a template class (perhaps with unbound template
20102 parameters). ARG is a fully instantiated type. If ARG can be
20103 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20104 TARGS are as for unify. */
20106 static tree
20107 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20108 bool explain_p)
20110 tree copy_of_targs;
20112 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20113 return NULL_TREE;
20114 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20115 /* Matches anything. */;
20116 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20117 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20118 return NULL_TREE;
20120 /* We need to make a new template argument vector for the call to
20121 unify. If we used TARGS, we'd clutter it up with the result of
20122 the attempted unification, even if this class didn't work out.
20123 We also don't want to commit ourselves to all the unifications
20124 we've already done, since unification is supposed to be done on
20125 an argument-by-argument basis. In other words, consider the
20126 following pathological case:
20128 template <int I, int J, int K>
20129 struct S {};
20131 template <int I, int J>
20132 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20134 template <int I, int J, int K>
20135 void f(S<I, J, K>, S<I, I, I>);
20137 void g() {
20138 S<0, 0, 0> s0;
20139 S<0, 1, 2> s2;
20141 f(s0, s2);
20144 Now, by the time we consider the unification involving `s2', we
20145 already know that we must have `f<0, 0, 0>'. But, even though
20146 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20147 because there are two ways to unify base classes of S<0, 1, 2>
20148 with S<I, I, I>. If we kept the already deduced knowledge, we
20149 would reject the possibility I=1. */
20150 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20152 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20154 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20155 return NULL_TREE;
20156 return arg;
20159 /* If unification failed, we're done. */
20160 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20161 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20162 return NULL_TREE;
20164 return arg;
20167 /* Given a template type PARM and a class type ARG, find the unique
20168 base type in ARG that is an instance of PARM. We do not examine
20169 ARG itself; only its base-classes. If there is not exactly one
20170 appropriate base class, return NULL_TREE. PARM may be the type of
20171 a partial specialization, as well as a plain template type. Used
20172 by unify. */
20174 static enum template_base_result
20175 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20176 bool explain_p, tree *result)
20178 tree rval = NULL_TREE;
20179 tree binfo;
20181 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20183 binfo = TYPE_BINFO (complete_type (arg));
20184 if (!binfo)
20186 /* The type could not be completed. */
20187 *result = NULL_TREE;
20188 return tbr_incomplete_type;
20191 /* Walk in inheritance graph order. The search order is not
20192 important, and this avoids multiple walks of virtual bases. */
20193 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20195 tree r = try_class_unification (tparms, targs, parm,
20196 BINFO_TYPE (binfo), explain_p);
20198 if (r)
20200 /* If there is more than one satisfactory baseclass, then:
20202 [temp.deduct.call]
20204 If they yield more than one possible deduced A, the type
20205 deduction fails.
20207 applies. */
20208 if (rval && !same_type_p (r, rval))
20210 *result = NULL_TREE;
20211 return tbr_ambiguous_baseclass;
20214 rval = r;
20218 *result = rval;
20219 return tbr_success;
20222 /* Returns the level of DECL, which declares a template parameter. */
20224 static int
20225 template_decl_level (tree decl)
20227 switch (TREE_CODE (decl))
20229 case TYPE_DECL:
20230 case TEMPLATE_DECL:
20231 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20233 case PARM_DECL:
20234 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20236 default:
20237 gcc_unreachable ();
20239 return 0;
20242 /* Decide whether ARG can be unified with PARM, considering only the
20243 cv-qualifiers of each type, given STRICT as documented for unify.
20244 Returns nonzero iff the unification is OK on that basis. */
20246 static int
20247 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20249 int arg_quals = cp_type_quals (arg);
20250 int parm_quals = cp_type_quals (parm);
20252 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20253 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20255 /* Although a CVR qualifier is ignored when being applied to a
20256 substituted template parameter ([8.3.2]/1 for example), that
20257 does not allow us to unify "const T" with "int&" because both
20258 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20259 It is ok when we're allowing additional CV qualifiers
20260 at the outer level [14.8.2.1]/3,1st bullet. */
20261 if ((TREE_CODE (arg) == REFERENCE_TYPE
20262 || TREE_CODE (arg) == FUNCTION_TYPE
20263 || TREE_CODE (arg) == METHOD_TYPE)
20264 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20265 return 0;
20267 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20268 && (parm_quals & TYPE_QUAL_RESTRICT))
20269 return 0;
20272 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20273 && (arg_quals & parm_quals) != parm_quals)
20274 return 0;
20276 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20277 && (parm_quals & arg_quals) != arg_quals)
20278 return 0;
20280 return 1;
20283 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20284 void
20285 template_parm_level_and_index (tree parm, int* level, int* index)
20287 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20288 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20289 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20291 *index = TEMPLATE_TYPE_IDX (parm);
20292 *level = TEMPLATE_TYPE_LEVEL (parm);
20294 else
20296 *index = TEMPLATE_PARM_IDX (parm);
20297 *level = TEMPLATE_PARM_LEVEL (parm);
20301 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20302 do { \
20303 if (unify (TP, TA, P, A, S, EP)) \
20304 return 1; \
20305 } while (0)
20307 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20308 expansion at the end of PACKED_PARMS. Returns 0 if the type
20309 deduction succeeds, 1 otherwise. STRICT is the same as in
20310 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20311 function call argument list. We'll need to adjust the arguments to make them
20312 types. SUBR tells us if this is from a recursive call to
20313 type_unification_real, or for comparing two template argument
20314 lists. */
20316 static int
20317 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20318 tree packed_args, unification_kind_t strict,
20319 bool subr, bool explain_p)
20321 tree parm
20322 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20323 tree pattern = PACK_EXPANSION_PATTERN (parm);
20324 tree pack, packs = NULL_TREE;
20325 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20327 /* Add in any args remembered from an earlier partial instantiation. */
20328 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20330 packed_args = expand_template_argument_pack (packed_args);
20332 int len = TREE_VEC_LENGTH (packed_args);
20334 /* Determine the parameter packs we will be deducing from the
20335 pattern, and record their current deductions. */
20336 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20337 pack; pack = TREE_CHAIN (pack))
20339 tree parm_pack = TREE_VALUE (pack);
20340 int idx, level;
20342 /* Determine the index and level of this parameter pack. */
20343 template_parm_level_and_index (parm_pack, &level, &idx);
20345 /* Keep track of the parameter packs and their corresponding
20346 argument packs. */
20347 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20348 TREE_TYPE (packs) = make_tree_vec (len - start);
20351 /* Loop through all of the arguments that have not yet been
20352 unified and unify each with the pattern. */
20353 for (i = start; i < len; i++)
20355 tree parm;
20356 bool any_explicit = false;
20357 tree arg = TREE_VEC_ELT (packed_args, i);
20359 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20360 or the element of its argument pack at the current index if
20361 this argument was explicitly specified. */
20362 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20364 int idx, level;
20365 tree arg, pargs;
20366 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20368 arg = NULL_TREE;
20369 if (TREE_VALUE (pack)
20370 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20371 && (i - start < TREE_VEC_LENGTH (pargs)))
20373 any_explicit = true;
20374 arg = TREE_VEC_ELT (pargs, i - start);
20376 TMPL_ARG (targs, level, idx) = arg;
20379 /* If we had explicit template arguments, substitute them into the
20380 pattern before deduction. */
20381 if (any_explicit)
20383 /* Some arguments might still be unspecified or dependent. */
20384 bool dependent;
20385 ++processing_template_decl;
20386 dependent = any_dependent_template_arguments_p (targs);
20387 if (!dependent)
20388 --processing_template_decl;
20389 parm = tsubst (pattern, targs,
20390 explain_p ? tf_warning_or_error : tf_none,
20391 NULL_TREE);
20392 if (dependent)
20393 --processing_template_decl;
20394 if (parm == error_mark_node)
20395 return 1;
20397 else
20398 parm = pattern;
20400 /* Unify the pattern with the current argument. */
20401 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20402 explain_p))
20403 return 1;
20405 /* For each parameter pack, collect the deduced value. */
20406 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20408 int idx, level;
20409 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20411 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20412 TMPL_ARG (targs, level, idx);
20416 /* Verify that the results of unification with the parameter packs
20417 produce results consistent with what we've seen before, and make
20418 the deduced argument packs available. */
20419 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20421 tree old_pack = TREE_VALUE (pack);
20422 tree new_args = TREE_TYPE (pack);
20423 int i, len = TREE_VEC_LENGTH (new_args);
20424 int idx, level;
20425 bool nondeduced_p = false;
20427 /* By default keep the original deduced argument pack.
20428 If necessary, more specific code is going to update the
20429 resulting deduced argument later down in this function. */
20430 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20431 TMPL_ARG (targs, level, idx) = old_pack;
20433 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20434 actually deduce anything. */
20435 for (i = 0; i < len && !nondeduced_p; ++i)
20436 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20437 nondeduced_p = true;
20438 if (nondeduced_p)
20439 continue;
20441 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20443 /* If we had fewer function args than explicit template args,
20444 just use the explicits. */
20445 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20446 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20447 if (len < explicit_len)
20448 new_args = explicit_args;
20451 if (!old_pack)
20453 tree result;
20454 /* Build the deduced *_ARGUMENT_PACK. */
20455 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20457 result = make_node (NONTYPE_ARGUMENT_PACK);
20458 TREE_CONSTANT (result) = 1;
20460 else
20461 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20463 SET_ARGUMENT_PACK_ARGS (result, new_args);
20465 /* Note the deduced argument packs for this parameter
20466 pack. */
20467 TMPL_ARG (targs, level, idx) = result;
20469 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20470 && (ARGUMENT_PACK_ARGS (old_pack)
20471 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20473 /* We only had the explicitly-provided arguments before, but
20474 now we have a complete set of arguments. */
20475 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20477 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20478 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20479 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20481 else
20483 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20484 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20486 if (!comp_template_args (old_args, new_args,
20487 &bad_old_arg, &bad_new_arg))
20488 /* Inconsistent unification of this parameter pack. */
20489 return unify_parameter_pack_inconsistent (explain_p,
20490 bad_old_arg,
20491 bad_new_arg);
20495 return unify_success (explain_p);
20498 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20499 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20500 parameters and return value are as for unify. */
20502 static int
20503 unify_array_domain (tree tparms, tree targs,
20504 tree parm_dom, tree arg_dom,
20505 bool explain_p)
20507 tree parm_max;
20508 tree arg_max;
20509 bool parm_cst;
20510 bool arg_cst;
20512 /* Our representation of array types uses "N - 1" as the
20513 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20514 not an integer constant. We cannot unify arbitrarily
20515 complex expressions, so we eliminate the MINUS_EXPRs
20516 here. */
20517 parm_max = TYPE_MAX_VALUE (parm_dom);
20518 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20519 if (!parm_cst)
20521 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20522 parm_max = TREE_OPERAND (parm_max, 0);
20524 arg_max = TYPE_MAX_VALUE (arg_dom);
20525 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20526 if (!arg_cst)
20528 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20529 trying to unify the type of a variable with the type
20530 of a template parameter. For example:
20532 template <unsigned int N>
20533 void f (char (&) [N]);
20534 int g();
20535 void h(int i) {
20536 char a[g(i)];
20537 f(a);
20540 Here, the type of the ARG will be "int [g(i)]", and
20541 may be a SAVE_EXPR, etc. */
20542 if (TREE_CODE (arg_max) != MINUS_EXPR)
20543 return unify_vla_arg (explain_p, arg_dom);
20544 arg_max = TREE_OPERAND (arg_max, 0);
20547 /* If only one of the bounds used a MINUS_EXPR, compensate
20548 by adding one to the other bound. */
20549 if (parm_cst && !arg_cst)
20550 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20551 integer_type_node,
20552 parm_max,
20553 integer_one_node);
20554 else if (arg_cst && !parm_cst)
20555 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20556 integer_type_node,
20557 arg_max,
20558 integer_one_node);
20560 return unify (tparms, targs, parm_max, arg_max,
20561 UNIFY_ALLOW_INTEGER, explain_p);
20564 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20566 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20568 static pa_kind_t
20569 pa_kind (tree t)
20571 if (PACK_EXPANSION_P (t))
20572 t = PACK_EXPANSION_PATTERN (t);
20573 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20574 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20575 || DECL_TYPE_TEMPLATE_P (t))
20576 return pa_tmpl;
20577 else if (TYPE_P (t))
20578 return pa_type;
20579 else
20580 return pa_expr;
20583 /* Deduce the value of template parameters. TPARMS is the (innermost)
20584 set of template parameters to a template. TARGS is the bindings
20585 for those template parameters, as determined thus far; TARGS may
20586 include template arguments for outer levels of template parameters
20587 as well. PARM is a parameter to a template function, or a
20588 subcomponent of that parameter; ARG is the corresponding argument.
20589 This function attempts to match PARM with ARG in a manner
20590 consistent with the existing assignments in TARGS. If more values
20591 are deduced, then TARGS is updated.
20593 Returns 0 if the type deduction succeeds, 1 otherwise. The
20594 parameter STRICT is a bitwise or of the following flags:
20596 UNIFY_ALLOW_NONE:
20597 Require an exact match between PARM and ARG.
20598 UNIFY_ALLOW_MORE_CV_QUAL:
20599 Allow the deduced ARG to be more cv-qualified (by qualification
20600 conversion) than ARG.
20601 UNIFY_ALLOW_LESS_CV_QUAL:
20602 Allow the deduced ARG to be less cv-qualified than ARG.
20603 UNIFY_ALLOW_DERIVED:
20604 Allow the deduced ARG to be a template base class of ARG,
20605 or a pointer to a template base class of the type pointed to by
20606 ARG.
20607 UNIFY_ALLOW_INTEGER:
20608 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20609 case for more information.
20610 UNIFY_ALLOW_OUTER_LEVEL:
20611 This is the outermost level of a deduction. Used to determine validity
20612 of qualification conversions. A valid qualification conversion must
20613 have const qualified pointers leading up to the inner type which
20614 requires additional CV quals, except at the outer level, where const
20615 is not required [conv.qual]. It would be normal to set this flag in
20616 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20617 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20618 This is the outermost level of a deduction, and PARM can be more CV
20619 qualified at this point.
20620 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20621 This is the outermost level of a deduction, and PARM can be less CV
20622 qualified at this point. */
20624 static int
20625 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20626 bool explain_p)
20628 int idx;
20629 tree targ;
20630 tree tparm;
20631 int strict_in = strict;
20632 tsubst_flags_t complain = (explain_p
20633 ? tf_warning_or_error
20634 : tf_none);
20636 /* I don't think this will do the right thing with respect to types.
20637 But the only case I've seen it in so far has been array bounds, where
20638 signedness is the only information lost, and I think that will be
20639 okay. */
20640 while (CONVERT_EXPR_P (parm))
20641 parm = TREE_OPERAND (parm, 0);
20643 if (arg == error_mark_node)
20644 return unify_invalid (explain_p);
20645 if (arg == unknown_type_node
20646 || arg == init_list_type_node)
20647 /* We can't deduce anything from this, but we might get all the
20648 template args from other function args. */
20649 return unify_success (explain_p);
20651 if (parm == any_targ_node || arg == any_targ_node)
20652 return unify_success (explain_p);
20654 /* If PARM uses template parameters, then we can't bail out here,
20655 even if ARG == PARM, since we won't record unifications for the
20656 template parameters. We might need them if we're trying to
20657 figure out which of two things is more specialized. */
20658 if (arg == parm && !uses_template_parms (parm))
20659 return unify_success (explain_p);
20661 /* Handle init lists early, so the rest of the function can assume
20662 we're dealing with a type. */
20663 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20665 tree elt, elttype;
20666 unsigned i;
20667 tree orig_parm = parm;
20669 /* Replace T with std::initializer_list<T> for deduction. */
20670 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20671 && flag_deduce_init_list)
20672 parm = listify (parm);
20674 if (!is_std_init_list (parm)
20675 && TREE_CODE (parm) != ARRAY_TYPE)
20676 /* We can only deduce from an initializer list argument if the
20677 parameter is std::initializer_list or an array; otherwise this
20678 is a non-deduced context. */
20679 return unify_success (explain_p);
20681 if (TREE_CODE (parm) == ARRAY_TYPE)
20682 elttype = TREE_TYPE (parm);
20683 else
20685 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20686 /* Deduction is defined in terms of a single type, so just punt
20687 on the (bizarre) std::initializer_list<T...>. */
20688 if (PACK_EXPANSION_P (elttype))
20689 return unify_success (explain_p);
20692 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20694 int elt_strict = strict;
20696 if (elt == error_mark_node)
20697 return unify_invalid (explain_p);
20699 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20701 tree type = TREE_TYPE (elt);
20702 if (type == error_mark_node)
20703 return unify_invalid (explain_p);
20704 /* It should only be possible to get here for a call. */
20705 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20706 elt_strict |= maybe_adjust_types_for_deduction
20707 (DEDUCE_CALL, &elttype, &type, elt);
20708 elt = type;
20711 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20712 explain_p);
20715 if (TREE_CODE (parm) == ARRAY_TYPE
20716 && deducible_array_bound (TYPE_DOMAIN (parm)))
20718 /* Also deduce from the length of the initializer list. */
20719 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20720 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20721 if (idx == error_mark_node)
20722 return unify_invalid (explain_p);
20723 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20724 idx, explain_p);
20727 /* If the std::initializer_list<T> deduction worked, replace the
20728 deduced A with std::initializer_list<A>. */
20729 if (orig_parm != parm)
20731 idx = TEMPLATE_TYPE_IDX (orig_parm);
20732 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20733 targ = listify (targ);
20734 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20736 return unify_success (explain_p);
20739 /* If parm and arg aren't the same kind of thing (template, type, or
20740 expression), fail early. */
20741 if (pa_kind (parm) != pa_kind (arg))
20742 return unify_invalid (explain_p);
20744 /* Immediately reject some pairs that won't unify because of
20745 cv-qualification mismatches. */
20746 if (TREE_CODE (arg) == TREE_CODE (parm)
20747 && TYPE_P (arg)
20748 /* It is the elements of the array which hold the cv quals of an array
20749 type, and the elements might be template type parms. We'll check
20750 when we recurse. */
20751 && TREE_CODE (arg) != ARRAY_TYPE
20752 /* We check the cv-qualifiers when unifying with template type
20753 parameters below. We want to allow ARG `const T' to unify with
20754 PARM `T' for example, when computing which of two templates
20755 is more specialized, for example. */
20756 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20757 && !check_cv_quals_for_unify (strict_in, arg, parm))
20758 return unify_cv_qual_mismatch (explain_p, parm, arg);
20760 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20761 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20762 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20763 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20764 strict &= ~UNIFY_ALLOW_DERIVED;
20765 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20766 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20768 switch (TREE_CODE (parm))
20770 case TYPENAME_TYPE:
20771 case SCOPE_REF:
20772 case UNBOUND_CLASS_TEMPLATE:
20773 /* In a type which contains a nested-name-specifier, template
20774 argument values cannot be deduced for template parameters used
20775 within the nested-name-specifier. */
20776 return unify_success (explain_p);
20778 case TEMPLATE_TYPE_PARM:
20779 case TEMPLATE_TEMPLATE_PARM:
20780 case BOUND_TEMPLATE_TEMPLATE_PARM:
20781 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20782 if (error_operand_p (tparm))
20783 return unify_invalid (explain_p);
20785 if (TEMPLATE_TYPE_LEVEL (parm)
20786 != template_decl_level (tparm))
20787 /* The PARM is not one we're trying to unify. Just check
20788 to see if it matches ARG. */
20790 if (TREE_CODE (arg) == TREE_CODE (parm)
20791 && (is_auto (parm) ? is_auto (arg)
20792 : same_type_p (parm, arg)))
20793 return unify_success (explain_p);
20794 else
20795 return unify_type_mismatch (explain_p, parm, arg);
20797 idx = TEMPLATE_TYPE_IDX (parm);
20798 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20799 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20800 if (error_operand_p (tparm))
20801 return unify_invalid (explain_p);
20803 /* Check for mixed types and values. */
20804 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20805 && TREE_CODE (tparm) != TYPE_DECL)
20806 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20807 && TREE_CODE (tparm) != TEMPLATE_DECL))
20808 gcc_unreachable ();
20810 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20812 if ((strict_in & UNIFY_ALLOW_DERIVED)
20813 && CLASS_TYPE_P (arg))
20815 /* First try to match ARG directly. */
20816 tree t = try_class_unification (tparms, targs, parm, arg,
20817 explain_p);
20818 if (!t)
20820 /* Otherwise, look for a suitable base of ARG, as below. */
20821 enum template_base_result r;
20822 r = get_template_base (tparms, targs, parm, arg,
20823 explain_p, &t);
20824 if (!t)
20825 return unify_no_common_base (explain_p, r, parm, arg);
20826 arg = t;
20829 /* ARG must be constructed from a template class or a template
20830 template parameter. */
20831 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20832 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20833 return unify_template_deduction_failure (explain_p, parm, arg);
20835 /* Deduce arguments T, i from TT<T> or TT<i>. */
20836 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20837 return 1;
20839 arg = TYPE_TI_TEMPLATE (arg);
20841 /* Fall through to deduce template name. */
20844 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20845 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20847 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20849 /* Simple cases: Value already set, does match or doesn't. */
20850 if (targ != NULL_TREE && template_args_equal (targ, arg))
20851 return unify_success (explain_p);
20852 else if (targ)
20853 return unify_inconsistency (explain_p, parm, targ, arg);
20855 else
20857 /* If PARM is `const T' and ARG is only `int', we don't have
20858 a match unless we are allowing additional qualification.
20859 If ARG is `const int' and PARM is just `T' that's OK;
20860 that binds `const int' to `T'. */
20861 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20862 arg, parm))
20863 return unify_cv_qual_mismatch (explain_p, parm, arg);
20865 /* Consider the case where ARG is `const volatile int' and
20866 PARM is `const T'. Then, T should be `volatile int'. */
20867 arg = cp_build_qualified_type_real
20868 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20869 if (arg == error_mark_node)
20870 return unify_invalid (explain_p);
20872 /* Simple cases: Value already set, does match or doesn't. */
20873 if (targ != NULL_TREE && same_type_p (targ, arg))
20874 return unify_success (explain_p);
20875 else if (targ)
20876 return unify_inconsistency (explain_p, parm, targ, arg);
20878 /* Make sure that ARG is not a variable-sized array. (Note
20879 that were talking about variable-sized arrays (like
20880 `int[n]'), rather than arrays of unknown size (like
20881 `int[]').) We'll get very confused by such a type since
20882 the bound of the array is not constant, and therefore
20883 not mangleable. Besides, such types are not allowed in
20884 ISO C++, so we can do as we please here. We do allow
20885 them for 'auto' deduction, since that isn't ABI-exposed. */
20886 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20887 return unify_vla_arg (explain_p, arg);
20889 /* Strip typedefs as in convert_template_argument. */
20890 arg = canonicalize_type_argument (arg, tf_none);
20893 /* If ARG is a parameter pack or an expansion, we cannot unify
20894 against it unless PARM is also a parameter pack. */
20895 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20896 && !template_parameter_pack_p (parm))
20897 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20899 /* If the argument deduction results is a METHOD_TYPE,
20900 then there is a problem.
20901 METHOD_TYPE doesn't map to any real C++ type the result of
20902 the deduction can not be of that type. */
20903 if (TREE_CODE (arg) == METHOD_TYPE)
20904 return unify_method_type_error (explain_p, arg);
20906 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20907 return unify_success (explain_p);
20909 case TEMPLATE_PARM_INDEX:
20910 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20911 if (error_operand_p (tparm))
20912 return unify_invalid (explain_p);
20914 if (TEMPLATE_PARM_LEVEL (parm)
20915 != template_decl_level (tparm))
20917 /* The PARM is not one we're trying to unify. Just check
20918 to see if it matches ARG. */
20919 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20920 && cp_tree_equal (parm, arg));
20921 if (result)
20922 unify_expression_unequal (explain_p, parm, arg);
20923 return result;
20926 idx = TEMPLATE_PARM_IDX (parm);
20927 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20929 if (targ)
20931 if ((strict & UNIFY_ALLOW_INTEGER)
20932 && TREE_TYPE (targ) && TREE_TYPE (arg)
20933 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
20934 /* We're deducing from an array bound, the type doesn't matter. */
20935 arg = fold_convert (TREE_TYPE (targ), arg);
20936 int x = !cp_tree_equal (targ, arg);
20937 if (x)
20938 unify_inconsistency (explain_p, parm, targ, arg);
20939 return x;
20942 /* [temp.deduct.type] If, in the declaration of a function template
20943 with a non-type template-parameter, the non-type
20944 template-parameter is used in an expression in the function
20945 parameter-list and, if the corresponding template-argument is
20946 deduced, the template-argument type shall match the type of the
20947 template-parameter exactly, except that a template-argument
20948 deduced from an array bound may be of any integral type.
20949 The non-type parameter might use already deduced type parameters. */
20950 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20951 if (tree a = type_uses_auto (tparm))
20953 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20954 if (tparm == error_mark_node)
20955 return 1;
20958 if (!TREE_TYPE (arg))
20959 /* Template-parameter dependent expression. Just accept it for now.
20960 It will later be processed in convert_template_argument. */
20962 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20963 non_reference (tparm)))
20964 /* OK */;
20965 else if ((strict & UNIFY_ALLOW_INTEGER)
20966 && CP_INTEGRAL_TYPE_P (tparm))
20967 /* Convert the ARG to the type of PARM; the deduced non-type
20968 template argument must exactly match the types of the
20969 corresponding parameter. */
20970 arg = fold (build_nop (tparm, arg));
20971 else if (uses_template_parms (tparm))
20973 /* We haven't deduced the type of this parameter yet. */
20974 if (cxx_dialect >= cxx17
20975 /* We deduce from array bounds in try_array_deduction. */
20976 && !(strict & UNIFY_ALLOW_INTEGER))
20978 /* Deduce it from the non-type argument. */
20979 tree atype = TREE_TYPE (arg);
20980 RECUR_AND_CHECK_FAILURE (tparms, targs,
20981 tparm, atype,
20982 UNIFY_ALLOW_NONE, explain_p);
20984 else
20985 /* Try again later. */
20986 return unify_success (explain_p);
20988 else
20989 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20991 /* If ARG is a parameter pack or an expansion, we cannot unify
20992 against it unless PARM is also a parameter pack. */
20993 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20994 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20995 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20998 bool removed_attr = false;
20999 arg = strip_typedefs_expr (arg, &removed_attr);
21001 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21002 return unify_success (explain_p);
21004 case PTRMEM_CST:
21006 /* A pointer-to-member constant can be unified only with
21007 another constant. */
21008 if (TREE_CODE (arg) != PTRMEM_CST)
21009 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21011 /* Just unify the class member. It would be useless (and possibly
21012 wrong, depending on the strict flags) to unify also
21013 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21014 arg refer to the same variable, even if through different
21015 classes. For instance:
21017 struct A { int x; };
21018 struct B : A { };
21020 Unification of &A::x and &B::x must succeed. */
21021 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21022 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21025 case POINTER_TYPE:
21027 if (!TYPE_PTR_P (arg))
21028 return unify_type_mismatch (explain_p, parm, arg);
21030 /* [temp.deduct.call]
21032 A can be another pointer or pointer to member type that can
21033 be converted to the deduced A via a qualification
21034 conversion (_conv.qual_).
21036 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21037 This will allow for additional cv-qualification of the
21038 pointed-to types if appropriate. */
21040 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21041 /* The derived-to-base conversion only persists through one
21042 level of pointers. */
21043 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21045 return unify (tparms, targs, TREE_TYPE (parm),
21046 TREE_TYPE (arg), strict, explain_p);
21049 case REFERENCE_TYPE:
21050 if (TREE_CODE (arg) != REFERENCE_TYPE)
21051 return unify_type_mismatch (explain_p, parm, arg);
21052 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21053 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21055 case ARRAY_TYPE:
21056 if (TREE_CODE (arg) != ARRAY_TYPE)
21057 return unify_type_mismatch (explain_p, parm, arg);
21058 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21059 != (TYPE_DOMAIN (arg) == NULL_TREE))
21060 return unify_type_mismatch (explain_p, parm, arg);
21061 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21062 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21063 if (TYPE_DOMAIN (parm) != NULL_TREE)
21064 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21065 TYPE_DOMAIN (arg), explain_p);
21066 return unify_success (explain_p);
21068 case REAL_TYPE:
21069 case COMPLEX_TYPE:
21070 case VECTOR_TYPE:
21071 case INTEGER_TYPE:
21072 case BOOLEAN_TYPE:
21073 case ENUMERAL_TYPE:
21074 case VOID_TYPE:
21075 case NULLPTR_TYPE:
21076 if (TREE_CODE (arg) != TREE_CODE (parm))
21077 return unify_type_mismatch (explain_p, parm, arg);
21079 /* We have already checked cv-qualification at the top of the
21080 function. */
21081 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21082 return unify_type_mismatch (explain_p, parm, arg);
21084 /* As far as unification is concerned, this wins. Later checks
21085 will invalidate it if necessary. */
21086 return unify_success (explain_p);
21088 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21089 /* Type INTEGER_CST can come from ordinary constant template args. */
21090 case INTEGER_CST:
21091 while (CONVERT_EXPR_P (arg))
21092 arg = TREE_OPERAND (arg, 0);
21094 if (TREE_CODE (arg) != INTEGER_CST)
21095 return unify_template_argument_mismatch (explain_p, parm, arg);
21096 return (tree_int_cst_equal (parm, arg)
21097 ? unify_success (explain_p)
21098 : unify_template_argument_mismatch (explain_p, parm, arg));
21100 case TREE_VEC:
21102 int i, len, argslen;
21103 int parm_variadic_p = 0;
21105 if (TREE_CODE (arg) != TREE_VEC)
21106 return unify_template_argument_mismatch (explain_p, parm, arg);
21108 len = TREE_VEC_LENGTH (parm);
21109 argslen = TREE_VEC_LENGTH (arg);
21111 /* Check for pack expansions in the parameters. */
21112 for (i = 0; i < len; ++i)
21114 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21116 if (i == len - 1)
21117 /* We can unify against something with a trailing
21118 parameter pack. */
21119 parm_variadic_p = 1;
21120 else
21121 /* [temp.deduct.type]/9: If the template argument list of
21122 P contains a pack expansion that is not the last
21123 template argument, the entire template argument list
21124 is a non-deduced context. */
21125 return unify_success (explain_p);
21129 /* If we don't have enough arguments to satisfy the parameters
21130 (not counting the pack expression at the end), or we have
21131 too many arguments for a parameter list that doesn't end in
21132 a pack expression, we can't unify. */
21133 if (parm_variadic_p
21134 ? argslen < len - parm_variadic_p
21135 : argslen != len)
21136 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21138 /* Unify all of the parameters that precede the (optional)
21139 pack expression. */
21140 for (i = 0; i < len - parm_variadic_p; ++i)
21142 RECUR_AND_CHECK_FAILURE (tparms, targs,
21143 TREE_VEC_ELT (parm, i),
21144 TREE_VEC_ELT (arg, i),
21145 UNIFY_ALLOW_NONE, explain_p);
21147 if (parm_variadic_p)
21148 return unify_pack_expansion (tparms, targs, parm, arg,
21149 DEDUCE_EXACT,
21150 /*subr=*/true, explain_p);
21151 return unify_success (explain_p);
21154 case RECORD_TYPE:
21155 case UNION_TYPE:
21156 if (TREE_CODE (arg) != TREE_CODE (parm))
21157 return unify_type_mismatch (explain_p, parm, arg);
21159 if (TYPE_PTRMEMFUNC_P (parm))
21161 if (!TYPE_PTRMEMFUNC_P (arg))
21162 return unify_type_mismatch (explain_p, parm, arg);
21164 return unify (tparms, targs,
21165 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21166 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21167 strict, explain_p);
21169 else if (TYPE_PTRMEMFUNC_P (arg))
21170 return unify_type_mismatch (explain_p, parm, arg);
21172 if (CLASSTYPE_TEMPLATE_INFO (parm))
21174 tree t = NULL_TREE;
21176 if (strict_in & UNIFY_ALLOW_DERIVED)
21178 /* First, we try to unify the PARM and ARG directly. */
21179 t = try_class_unification (tparms, targs,
21180 parm, arg, explain_p);
21182 if (!t)
21184 /* Fallback to the special case allowed in
21185 [temp.deduct.call]:
21187 If P is a class, and P has the form
21188 template-id, then A can be a derived class of
21189 the deduced A. Likewise, if P is a pointer to
21190 a class of the form template-id, A can be a
21191 pointer to a derived class pointed to by the
21192 deduced A. */
21193 enum template_base_result r;
21194 r = get_template_base (tparms, targs, parm, arg,
21195 explain_p, &t);
21197 if (!t)
21199 /* Don't give the derived diagnostic if we're
21200 already dealing with the same template. */
21201 bool same_template
21202 = (CLASSTYPE_TEMPLATE_INFO (arg)
21203 && (CLASSTYPE_TI_TEMPLATE (parm)
21204 == CLASSTYPE_TI_TEMPLATE (arg)));
21205 return unify_no_common_base (explain_p && !same_template,
21206 r, parm, arg);
21210 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21211 && (CLASSTYPE_TI_TEMPLATE (parm)
21212 == CLASSTYPE_TI_TEMPLATE (arg)))
21213 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21214 Then, we should unify `int' and `U'. */
21215 t = arg;
21216 else
21217 /* There's no chance of unification succeeding. */
21218 return unify_type_mismatch (explain_p, parm, arg);
21220 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21221 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21223 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21224 return unify_type_mismatch (explain_p, parm, arg);
21225 return unify_success (explain_p);
21227 case METHOD_TYPE:
21228 case FUNCTION_TYPE:
21230 unsigned int nargs;
21231 tree *args;
21232 tree a;
21233 unsigned int i;
21235 if (TREE_CODE (arg) != TREE_CODE (parm))
21236 return unify_type_mismatch (explain_p, parm, arg);
21238 /* CV qualifications for methods can never be deduced, they must
21239 match exactly. We need to check them explicitly here,
21240 because type_unification_real treats them as any other
21241 cv-qualified parameter. */
21242 if (TREE_CODE (parm) == METHOD_TYPE
21243 && (!check_cv_quals_for_unify
21244 (UNIFY_ALLOW_NONE,
21245 class_of_this_parm (arg),
21246 class_of_this_parm (parm))))
21247 return unify_cv_qual_mismatch (explain_p, parm, arg);
21248 if (TREE_CODE (arg) == FUNCTION_TYPE
21249 && type_memfn_quals (parm) != type_memfn_quals (arg))
21250 return unify_cv_qual_mismatch (explain_p, parm, arg);
21251 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21252 return unify_type_mismatch (explain_p, parm, arg);
21254 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21255 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21257 nargs = list_length (TYPE_ARG_TYPES (arg));
21258 args = XALLOCAVEC (tree, nargs);
21259 for (a = TYPE_ARG_TYPES (arg), i = 0;
21260 a != NULL_TREE && a != void_list_node;
21261 a = TREE_CHAIN (a), ++i)
21262 args[i] = TREE_VALUE (a);
21263 nargs = i;
21265 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21266 args, nargs, 1, DEDUCE_EXACT,
21267 LOOKUP_NORMAL, NULL, explain_p))
21268 return 1;
21270 if (flag_noexcept_type)
21272 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21273 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21274 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21275 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21276 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21277 && uses_template_parms (TREE_PURPOSE (pspec)))
21278 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21279 TREE_PURPOSE (aspec),
21280 UNIFY_ALLOW_NONE, explain_p);
21281 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21282 return unify_type_mismatch (explain_p, parm, arg);
21285 return 0;
21288 case OFFSET_TYPE:
21289 /* Unify a pointer to member with a pointer to member function, which
21290 deduces the type of the member as a function type. */
21291 if (TYPE_PTRMEMFUNC_P (arg))
21293 /* Check top-level cv qualifiers */
21294 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21295 return unify_cv_qual_mismatch (explain_p, parm, arg);
21297 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21298 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21299 UNIFY_ALLOW_NONE, explain_p);
21301 /* Determine the type of the function we are unifying against. */
21302 tree fntype = static_fn_type (arg);
21304 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21307 if (TREE_CODE (arg) != OFFSET_TYPE)
21308 return unify_type_mismatch (explain_p, parm, arg);
21309 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21310 TYPE_OFFSET_BASETYPE (arg),
21311 UNIFY_ALLOW_NONE, explain_p);
21312 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21313 strict, explain_p);
21315 case CONST_DECL:
21316 if (DECL_TEMPLATE_PARM_P (parm))
21317 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21318 if (arg != scalar_constant_value (parm))
21319 return unify_template_argument_mismatch (explain_p, parm, arg);
21320 return unify_success (explain_p);
21322 case FIELD_DECL:
21323 case TEMPLATE_DECL:
21324 /* Matched cases are handled by the ARG == PARM test above. */
21325 return unify_template_argument_mismatch (explain_p, parm, arg);
21327 case VAR_DECL:
21328 /* We might get a variable as a non-type template argument in parm if the
21329 corresponding parameter is type-dependent. Make any necessary
21330 adjustments based on whether arg is a reference. */
21331 if (CONSTANT_CLASS_P (arg))
21332 parm = fold_non_dependent_expr (parm);
21333 else if (REFERENCE_REF_P (arg))
21335 tree sub = TREE_OPERAND (arg, 0);
21336 STRIP_NOPS (sub);
21337 if (TREE_CODE (sub) == ADDR_EXPR)
21338 arg = TREE_OPERAND (sub, 0);
21340 /* Now use the normal expression code to check whether they match. */
21341 goto expr;
21343 case TYPE_ARGUMENT_PACK:
21344 case NONTYPE_ARGUMENT_PACK:
21345 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21346 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21348 case TYPEOF_TYPE:
21349 case DECLTYPE_TYPE:
21350 case UNDERLYING_TYPE:
21351 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21352 or UNDERLYING_TYPE nodes. */
21353 return unify_success (explain_p);
21355 case ERROR_MARK:
21356 /* Unification fails if we hit an error node. */
21357 return unify_invalid (explain_p);
21359 case INDIRECT_REF:
21360 if (REFERENCE_REF_P (parm))
21362 bool pexp = PACK_EXPANSION_P (arg);
21363 if (pexp)
21364 arg = PACK_EXPANSION_PATTERN (arg);
21365 if (REFERENCE_REF_P (arg))
21366 arg = TREE_OPERAND (arg, 0);
21367 if (pexp)
21368 arg = make_pack_expansion (arg, complain);
21369 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21370 strict, explain_p);
21372 /* FALLTHRU */
21374 default:
21375 /* An unresolved overload is a nondeduced context. */
21376 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21377 return unify_success (explain_p);
21378 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21379 expr:
21380 /* We must be looking at an expression. This can happen with
21381 something like:
21383 template <int I>
21384 void foo(S<I>, S<I + 2>);
21386 This is a "nondeduced context":
21388 [deduct.type]
21390 The nondeduced contexts are:
21392 --A type that is a template-id in which one or more of
21393 the template-arguments is an expression that references
21394 a template-parameter.
21396 In these cases, we assume deduction succeeded, but don't
21397 actually infer any unifications. */
21399 if (!uses_template_parms (parm)
21400 && !template_args_equal (parm, arg))
21401 return unify_expression_unequal (explain_p, parm, arg);
21402 else
21403 return unify_success (explain_p);
21406 #undef RECUR_AND_CHECK_FAILURE
21408 /* Note that DECL can be defined in this translation unit, if
21409 required. */
21411 static void
21412 mark_definable (tree decl)
21414 tree clone;
21415 DECL_NOT_REALLY_EXTERN (decl) = 1;
21416 FOR_EACH_CLONE (clone, decl)
21417 DECL_NOT_REALLY_EXTERN (clone) = 1;
21420 /* Called if RESULT is explicitly instantiated, or is a member of an
21421 explicitly instantiated class. */
21423 void
21424 mark_decl_instantiated (tree result, int extern_p)
21426 SET_DECL_EXPLICIT_INSTANTIATION (result);
21428 /* If this entity has already been written out, it's too late to
21429 make any modifications. */
21430 if (TREE_ASM_WRITTEN (result))
21431 return;
21433 /* For anonymous namespace we don't need to do anything. */
21434 if (decl_anon_ns_mem_p (result))
21436 gcc_assert (!TREE_PUBLIC (result));
21437 return;
21440 if (TREE_CODE (result) != FUNCTION_DECL)
21441 /* The TREE_PUBLIC flag for function declarations will have been
21442 set correctly by tsubst. */
21443 TREE_PUBLIC (result) = 1;
21445 /* This might have been set by an earlier implicit instantiation. */
21446 DECL_COMDAT (result) = 0;
21448 if (extern_p)
21449 DECL_NOT_REALLY_EXTERN (result) = 0;
21450 else
21452 mark_definable (result);
21453 mark_needed (result);
21454 /* Always make artificials weak. */
21455 if (DECL_ARTIFICIAL (result) && flag_weak)
21456 comdat_linkage (result);
21457 /* For WIN32 we also want to put explicit instantiations in
21458 linkonce sections. */
21459 else if (TREE_PUBLIC (result))
21460 maybe_make_one_only (result);
21463 /* If EXTERN_P, then this function will not be emitted -- unless
21464 followed by an explicit instantiation, at which point its linkage
21465 will be adjusted. If !EXTERN_P, then this function will be
21466 emitted here. In neither circumstance do we want
21467 import_export_decl to adjust the linkage. */
21468 DECL_INTERFACE_KNOWN (result) = 1;
21471 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21472 important template arguments. If any are missing, we check whether
21473 they're important by using error_mark_node for substituting into any
21474 args that were used for partial ordering (the ones between ARGS and END)
21475 and seeing if it bubbles up. */
21477 static bool
21478 check_undeduced_parms (tree targs, tree args, tree end)
21480 bool found = false;
21481 int i;
21482 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21483 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21485 found = true;
21486 TREE_VEC_ELT (targs, i) = error_mark_node;
21488 if (found)
21490 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21491 if (substed == error_mark_node)
21492 return true;
21494 return false;
21497 /* Given two function templates PAT1 and PAT2, return:
21499 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21500 -1 if PAT2 is more specialized than PAT1.
21501 0 if neither is more specialized.
21503 LEN indicates the number of parameters we should consider
21504 (defaulted parameters should not be considered).
21506 The 1998 std underspecified function template partial ordering, and
21507 DR214 addresses the issue. We take pairs of arguments, one from
21508 each of the templates, and deduce them against each other. One of
21509 the templates will be more specialized if all the *other*
21510 template's arguments deduce against its arguments and at least one
21511 of its arguments *does* *not* deduce against the other template's
21512 corresponding argument. Deduction is done as for class templates.
21513 The arguments used in deduction have reference and top level cv
21514 qualifiers removed. Iff both arguments were originally reference
21515 types *and* deduction succeeds in both directions, an lvalue reference
21516 wins against an rvalue reference and otherwise the template
21517 with the more cv-qualified argument wins for that pairing (if
21518 neither is more cv-qualified, they both are equal). Unlike regular
21519 deduction, after all the arguments have been deduced in this way,
21520 we do *not* verify the deduced template argument values can be
21521 substituted into non-deduced contexts.
21523 The logic can be a bit confusing here, because we look at deduce1 and
21524 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21525 can find template arguments for pat1 to make arg1 look like arg2, that
21526 means that arg2 is at least as specialized as arg1. */
21529 more_specialized_fn (tree pat1, tree pat2, int len)
21531 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21532 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21533 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21534 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21535 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21536 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21537 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21538 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21539 tree origs1, origs2;
21540 bool lose1 = false;
21541 bool lose2 = false;
21543 /* Remove the this parameter from non-static member functions. If
21544 one is a non-static member function and the other is not a static
21545 member function, remove the first parameter from that function
21546 also. This situation occurs for operator functions where we
21547 locate both a member function (with this pointer) and non-member
21548 operator (with explicit first operand). */
21549 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21551 len--; /* LEN is the number of significant arguments for DECL1 */
21552 args1 = TREE_CHAIN (args1);
21553 if (!DECL_STATIC_FUNCTION_P (decl2))
21554 args2 = TREE_CHAIN (args2);
21556 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21558 args2 = TREE_CHAIN (args2);
21559 if (!DECL_STATIC_FUNCTION_P (decl1))
21561 len--;
21562 args1 = TREE_CHAIN (args1);
21566 /* If only one is a conversion operator, they are unordered. */
21567 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21568 return 0;
21570 /* Consider the return type for a conversion function */
21571 if (DECL_CONV_FN_P (decl1))
21573 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21574 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21575 len++;
21578 processing_template_decl++;
21580 origs1 = args1;
21581 origs2 = args2;
21583 while (len--
21584 /* Stop when an ellipsis is seen. */
21585 && args1 != NULL_TREE && args2 != NULL_TREE)
21587 tree arg1 = TREE_VALUE (args1);
21588 tree arg2 = TREE_VALUE (args2);
21589 int deduce1, deduce2;
21590 int quals1 = -1;
21591 int quals2 = -1;
21592 int ref1 = 0;
21593 int ref2 = 0;
21595 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21596 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21598 /* When both arguments are pack expansions, we need only
21599 unify the patterns themselves. */
21600 arg1 = PACK_EXPANSION_PATTERN (arg1);
21601 arg2 = PACK_EXPANSION_PATTERN (arg2);
21603 /* This is the last comparison we need to do. */
21604 len = 0;
21607 /* DR 1847: If a particular P contains no template-parameters that
21608 participate in template argument deduction, that P is not used to
21609 determine the ordering. */
21610 if (!uses_deducible_template_parms (arg1)
21611 && !uses_deducible_template_parms (arg2))
21612 goto next;
21614 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21616 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21617 arg1 = TREE_TYPE (arg1);
21618 quals1 = cp_type_quals (arg1);
21621 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21623 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21624 arg2 = TREE_TYPE (arg2);
21625 quals2 = cp_type_quals (arg2);
21628 arg1 = TYPE_MAIN_VARIANT (arg1);
21629 arg2 = TYPE_MAIN_VARIANT (arg2);
21631 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21633 int i, len2 = remaining_arguments (args2);
21634 tree parmvec = make_tree_vec (1);
21635 tree argvec = make_tree_vec (len2);
21636 tree ta = args2;
21638 /* Setup the parameter vector, which contains only ARG1. */
21639 TREE_VEC_ELT (parmvec, 0) = arg1;
21641 /* Setup the argument vector, which contains the remaining
21642 arguments. */
21643 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21644 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21646 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21647 argvec, DEDUCE_EXACT,
21648 /*subr=*/true, /*explain_p=*/false)
21649 == 0);
21651 /* We cannot deduce in the other direction, because ARG1 is
21652 a pack expansion but ARG2 is not. */
21653 deduce2 = 0;
21655 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21657 int i, len1 = remaining_arguments (args1);
21658 tree parmvec = make_tree_vec (1);
21659 tree argvec = make_tree_vec (len1);
21660 tree ta = args1;
21662 /* Setup the parameter vector, which contains only ARG1. */
21663 TREE_VEC_ELT (parmvec, 0) = arg2;
21665 /* Setup the argument vector, which contains the remaining
21666 arguments. */
21667 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21668 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21670 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21671 argvec, DEDUCE_EXACT,
21672 /*subr=*/true, /*explain_p=*/false)
21673 == 0);
21675 /* We cannot deduce in the other direction, because ARG2 is
21676 a pack expansion but ARG1 is not.*/
21677 deduce1 = 0;
21680 else
21682 /* The normal case, where neither argument is a pack
21683 expansion. */
21684 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21685 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21686 == 0);
21687 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21688 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21689 == 0);
21692 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21693 arg2, then arg2 is not as specialized as arg1. */
21694 if (!deduce1)
21695 lose2 = true;
21696 if (!deduce2)
21697 lose1 = true;
21699 /* "If, for a given type, deduction succeeds in both directions
21700 (i.e., the types are identical after the transformations above)
21701 and both P and A were reference types (before being replaced with
21702 the type referred to above):
21703 - if the type from the argument template was an lvalue reference and
21704 the type from the parameter template was not, the argument type is
21705 considered to be more specialized than the other; otherwise,
21706 - if the type from the argument template is more cv-qualified
21707 than the type from the parameter template (as described above),
21708 the argument type is considered to be more specialized than the other;
21709 otherwise,
21710 - neither type is more specialized than the other." */
21712 if (deduce1 && deduce2)
21714 if (ref1 && ref2 && ref1 != ref2)
21716 if (ref1 > ref2)
21717 lose1 = true;
21718 else
21719 lose2 = true;
21721 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21723 if ((quals1 & quals2) == quals2)
21724 lose2 = true;
21725 if ((quals1 & quals2) == quals1)
21726 lose1 = true;
21730 if (lose1 && lose2)
21731 /* We've failed to deduce something in either direction.
21732 These must be unordered. */
21733 break;
21735 next:
21737 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21738 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21739 /* We have already processed all of the arguments in our
21740 handing of the pack expansion type. */
21741 len = 0;
21743 args1 = TREE_CHAIN (args1);
21744 args2 = TREE_CHAIN (args2);
21747 /* "In most cases, all template parameters must have values in order for
21748 deduction to succeed, but for partial ordering purposes a template
21749 parameter may remain without a value provided it is not used in the
21750 types being used for partial ordering."
21752 Thus, if we are missing any of the targs1 we need to substitute into
21753 origs1, then pat2 is not as specialized as pat1. This can happen when
21754 there is a nondeduced context. */
21755 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21756 lose2 = true;
21757 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21758 lose1 = true;
21760 processing_template_decl--;
21762 /* If both deductions succeed, the partial ordering selects the more
21763 constrained template. */
21764 if (!lose1 && !lose2)
21766 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21767 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21768 lose1 = !subsumes_constraints (c1, c2);
21769 lose2 = !subsumes_constraints (c2, c1);
21772 /* All things being equal, if the next argument is a pack expansion
21773 for one function but not for the other, prefer the
21774 non-variadic function. FIXME this is bogus; see c++/41958. */
21775 if (lose1 == lose2
21776 && args1 && TREE_VALUE (args1)
21777 && args2 && TREE_VALUE (args2))
21779 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21780 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21783 if (lose1 == lose2)
21784 return 0;
21785 else if (!lose1)
21786 return 1;
21787 else
21788 return -1;
21791 /* Determine which of two partial specializations of TMPL is more
21792 specialized.
21794 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21795 to the first partial specialization. The TREE_PURPOSE is the
21796 innermost set of template parameters for the partial
21797 specialization. PAT2 is similar, but for the second template.
21799 Return 1 if the first partial specialization is more specialized;
21800 -1 if the second is more specialized; 0 if neither is more
21801 specialized.
21803 See [temp.class.order] for information about determining which of
21804 two templates is more specialized. */
21806 static int
21807 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21809 tree targs;
21810 int winner = 0;
21811 bool any_deductions = false;
21813 tree tmpl1 = TREE_VALUE (pat1);
21814 tree tmpl2 = TREE_VALUE (pat2);
21815 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21816 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21818 /* Just like what happens for functions, if we are ordering between
21819 different template specializations, we may encounter dependent
21820 types in the arguments, and we need our dependency check functions
21821 to behave correctly. */
21822 ++processing_template_decl;
21823 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21824 if (targs)
21826 --winner;
21827 any_deductions = true;
21830 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21831 if (targs)
21833 ++winner;
21834 any_deductions = true;
21836 --processing_template_decl;
21838 /* If both deductions succeed, the partial ordering selects the more
21839 constrained template. */
21840 if (!winner && any_deductions)
21841 return more_constrained (tmpl1, tmpl2);
21843 /* In the case of a tie where at least one of the templates
21844 has a parameter pack at the end, the template with the most
21845 non-packed parameters wins. */
21846 if (winner == 0
21847 && any_deductions
21848 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21849 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21851 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21852 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21853 int len1 = TREE_VEC_LENGTH (args1);
21854 int len2 = TREE_VEC_LENGTH (args2);
21856 /* We don't count the pack expansion at the end. */
21857 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21858 --len1;
21859 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21860 --len2;
21862 if (len1 > len2)
21863 return 1;
21864 else if (len1 < len2)
21865 return -1;
21868 return winner;
21871 /* Return the template arguments that will produce the function signature
21872 DECL from the function template FN, with the explicit template
21873 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21874 also match. Return NULL_TREE if no satisfactory arguments could be
21875 found. */
21877 static tree
21878 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21880 int ntparms = DECL_NTPARMS (fn);
21881 tree targs = make_tree_vec (ntparms);
21882 tree decl_type = TREE_TYPE (decl);
21883 tree decl_arg_types;
21884 tree *args;
21885 unsigned int nargs, ix;
21886 tree arg;
21888 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21890 /* Never do unification on the 'this' parameter. */
21891 decl_arg_types = skip_artificial_parms_for (decl,
21892 TYPE_ARG_TYPES (decl_type));
21894 nargs = list_length (decl_arg_types);
21895 args = XALLOCAVEC (tree, nargs);
21896 for (arg = decl_arg_types, ix = 0;
21897 arg != NULL_TREE && arg != void_list_node;
21898 arg = TREE_CHAIN (arg), ++ix)
21899 args[ix] = TREE_VALUE (arg);
21901 if (fn_type_unification (fn, explicit_args, targs,
21902 args, ix,
21903 (check_rettype || DECL_CONV_FN_P (fn)
21904 ? TREE_TYPE (decl_type) : NULL_TREE),
21905 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21906 /*decltype*/false)
21907 == error_mark_node)
21908 return NULL_TREE;
21910 return targs;
21913 /* Return the innermost template arguments that, when applied to a partial
21914 specialization SPEC_TMPL of TMPL, yield the ARGS.
21916 For example, suppose we have:
21918 template <class T, class U> struct S {};
21919 template <class T> struct S<T*, int> {};
21921 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21922 partial specialization and the ARGS will be {double*, int}. The resulting
21923 vector will be {double}, indicating that `T' is bound to `double'. */
21925 static tree
21926 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21928 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21929 tree spec_args
21930 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21931 int i, ntparms = TREE_VEC_LENGTH (tparms);
21932 tree deduced_args;
21933 tree innermost_deduced_args;
21935 innermost_deduced_args = make_tree_vec (ntparms);
21936 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21938 deduced_args = copy_node (args);
21939 SET_TMPL_ARGS_LEVEL (deduced_args,
21940 TMPL_ARGS_DEPTH (deduced_args),
21941 innermost_deduced_args);
21943 else
21944 deduced_args = innermost_deduced_args;
21946 bool tried_array_deduction = (cxx_dialect < cxx17);
21947 again:
21948 if (unify (tparms, deduced_args,
21949 INNERMOST_TEMPLATE_ARGS (spec_args),
21950 INNERMOST_TEMPLATE_ARGS (args),
21951 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21952 return NULL_TREE;
21954 for (i = 0; i < ntparms; ++i)
21955 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21957 if (!tried_array_deduction)
21959 try_array_deduction (tparms, innermost_deduced_args,
21960 INNERMOST_TEMPLATE_ARGS (spec_args));
21961 tried_array_deduction = true;
21962 if (TREE_VEC_ELT (innermost_deduced_args, i))
21963 goto again;
21965 return NULL_TREE;
21968 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21969 if (!push_tinst_level (tinst))
21971 excessive_deduction_depth = true;
21972 return NULL_TREE;
21975 /* Verify that nondeduced template arguments agree with the type
21976 obtained from argument deduction.
21978 For example:
21980 struct A { typedef int X; };
21981 template <class T, class U> struct C {};
21982 template <class T> struct C<T, typename T::X> {};
21984 Then with the instantiation `C<A, int>', we can deduce that
21985 `T' is `A' but unify () does not check whether `typename T::X'
21986 is `int'. */
21987 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21989 if (spec_args != error_mark_node)
21990 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21991 INNERMOST_TEMPLATE_ARGS (spec_args),
21992 tmpl, tf_none, false, false);
21994 pop_tinst_level ();
21996 if (spec_args == error_mark_node
21997 /* We only need to check the innermost arguments; the other
21998 arguments will always agree. */
21999 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22000 INNERMOST_TEMPLATE_ARGS (args)))
22001 return NULL_TREE;
22003 /* Now that we have bindings for all of the template arguments,
22004 ensure that the arguments deduced for the template template
22005 parameters have compatible template parameter lists. See the use
22006 of template_template_parm_bindings_ok_p in fn_type_unification
22007 for more information. */
22008 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22009 return NULL_TREE;
22011 return deduced_args;
22014 // Compare two function templates T1 and T2 by deducing bindings
22015 // from one against the other. If both deductions succeed, compare
22016 // constraints to see which is more constrained.
22017 static int
22018 more_specialized_inst (tree t1, tree t2)
22020 int fate = 0;
22021 int count = 0;
22023 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22025 --fate;
22026 ++count;
22029 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22031 ++fate;
22032 ++count;
22035 // If both deductions succeed, then one may be more constrained.
22036 if (count == 2 && fate == 0)
22037 fate = more_constrained (t1, t2);
22039 return fate;
22042 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22043 Return the TREE_LIST node with the most specialized template, if
22044 any. If there is no most specialized template, the error_mark_node
22045 is returned.
22047 Note that this function does not look at, or modify, the
22048 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22049 returned is one of the elements of INSTANTIATIONS, callers may
22050 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22051 and retrieve it from the value returned. */
22053 tree
22054 most_specialized_instantiation (tree templates)
22056 tree fn, champ;
22058 ++processing_template_decl;
22060 champ = templates;
22061 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22063 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22064 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22065 if (fate == -1)
22066 champ = fn;
22067 else if (!fate)
22069 /* Equally specialized, move to next function. If there
22070 is no next function, nothing's most specialized. */
22071 fn = TREE_CHAIN (fn);
22072 champ = fn;
22073 if (!fn)
22074 break;
22078 if (champ)
22079 /* Now verify that champ is better than everything earlier in the
22080 instantiation list. */
22081 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22082 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22084 champ = NULL_TREE;
22085 break;
22089 processing_template_decl--;
22091 if (!champ)
22092 return error_mark_node;
22094 return champ;
22097 /* If DECL is a specialization of some template, return the most
22098 general such template. Otherwise, returns NULL_TREE.
22100 For example, given:
22102 template <class T> struct S { template <class U> void f(U); };
22104 if TMPL is `template <class U> void S<int>::f(U)' this will return
22105 the full template. This function will not trace past partial
22106 specializations, however. For example, given in addition:
22108 template <class T> struct S<T*> { template <class U> void f(U); };
22110 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22111 `template <class T> template <class U> S<T*>::f(U)'. */
22113 tree
22114 most_general_template (tree decl)
22116 if (TREE_CODE (decl) != TEMPLATE_DECL)
22118 if (tree tinfo = get_template_info (decl))
22119 decl = TI_TEMPLATE (tinfo);
22120 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22121 template friend, or a FIELD_DECL for a capture pack. */
22122 if (TREE_CODE (decl) != TEMPLATE_DECL)
22123 return NULL_TREE;
22126 /* Look for more and more general templates. */
22127 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22129 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22130 (See cp-tree.h for details.) */
22131 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22132 break;
22134 if (CLASS_TYPE_P (TREE_TYPE (decl))
22135 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22136 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22137 break;
22139 /* Stop if we run into an explicitly specialized class template. */
22140 if (!DECL_NAMESPACE_SCOPE_P (decl)
22141 && DECL_CONTEXT (decl)
22142 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22143 break;
22145 decl = DECL_TI_TEMPLATE (decl);
22148 return decl;
22151 /* Return the most specialized of the template partial specializations
22152 which can produce TARGET, a specialization of some class or variable
22153 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22154 a TEMPLATE_DECL node corresponding to the partial specialization, while
22155 the TREE_PURPOSE is the set of template arguments that must be
22156 substituted into the template pattern in order to generate TARGET.
22158 If the choice of partial specialization is ambiguous, a diagnostic
22159 is issued, and the error_mark_node is returned. If there are no
22160 partial specializations matching TARGET, then NULL_TREE is
22161 returned, indicating that the primary template should be used. */
22163 static tree
22164 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22166 tree list = NULL_TREE;
22167 tree t;
22168 tree champ;
22169 int fate;
22170 bool ambiguous_p;
22171 tree outer_args = NULL_TREE;
22172 tree tmpl, args;
22174 if (TYPE_P (target))
22176 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22177 tmpl = TI_TEMPLATE (tinfo);
22178 args = TI_ARGS (tinfo);
22180 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22182 tmpl = TREE_OPERAND (target, 0);
22183 args = TREE_OPERAND (target, 1);
22185 else if (VAR_P (target))
22187 tree tinfo = DECL_TEMPLATE_INFO (target);
22188 tmpl = TI_TEMPLATE (tinfo);
22189 args = TI_ARGS (tinfo);
22191 else
22192 gcc_unreachable ();
22194 tree main_tmpl = most_general_template (tmpl);
22196 /* For determining which partial specialization to use, only the
22197 innermost args are interesting. */
22198 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22200 outer_args = strip_innermost_template_args (args, 1);
22201 args = INNERMOST_TEMPLATE_ARGS (args);
22204 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22206 tree spec_args;
22207 tree spec_tmpl = TREE_VALUE (t);
22209 if (outer_args)
22211 /* Substitute in the template args from the enclosing class. */
22212 ++processing_template_decl;
22213 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22214 --processing_template_decl;
22217 if (spec_tmpl == error_mark_node)
22218 return error_mark_node;
22220 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22221 if (spec_args)
22223 if (outer_args)
22224 spec_args = add_to_template_args (outer_args, spec_args);
22226 /* Keep the candidate only if the constraints are satisfied,
22227 or if we're not compiling with concepts. */
22228 if (!flag_concepts
22229 || constraints_satisfied_p (spec_tmpl, spec_args))
22231 list = tree_cons (spec_args, TREE_VALUE (t), list);
22232 TREE_TYPE (list) = TREE_TYPE (t);
22237 if (! list)
22238 return NULL_TREE;
22240 ambiguous_p = false;
22241 t = list;
22242 champ = t;
22243 t = TREE_CHAIN (t);
22244 for (; t; t = TREE_CHAIN (t))
22246 fate = more_specialized_partial_spec (tmpl, champ, t);
22247 if (fate == 1)
22249 else
22251 if (fate == 0)
22253 t = TREE_CHAIN (t);
22254 if (! t)
22256 ambiguous_p = true;
22257 break;
22260 champ = t;
22264 if (!ambiguous_p)
22265 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22267 fate = more_specialized_partial_spec (tmpl, champ, t);
22268 if (fate != 1)
22270 ambiguous_p = true;
22271 break;
22275 if (ambiguous_p)
22277 const char *str;
22278 char *spaces = NULL;
22279 if (!(complain & tf_error))
22280 return error_mark_node;
22281 if (TYPE_P (target))
22282 error ("ambiguous template instantiation for %q#T", target);
22283 else
22284 error ("ambiguous template instantiation for %q#D", target);
22285 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22286 for (t = list; t; t = TREE_CHAIN (t))
22288 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22289 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22290 "%s %#qS", spaces ? spaces : str, subst);
22291 spaces = spaces ? spaces : get_spaces (str);
22293 free (spaces);
22294 return error_mark_node;
22297 return champ;
22300 /* Explicitly instantiate DECL. */
22302 void
22303 do_decl_instantiation (tree decl, tree storage)
22305 tree result = NULL_TREE;
22306 int extern_p = 0;
22308 if (!decl || decl == error_mark_node)
22309 /* An error occurred, for which grokdeclarator has already issued
22310 an appropriate message. */
22311 return;
22312 else if (! DECL_LANG_SPECIFIC (decl))
22314 error ("explicit instantiation of non-template %q#D", decl);
22315 return;
22318 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22319 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22321 if (VAR_P (decl) && !var_templ)
22323 /* There is an asymmetry here in the way VAR_DECLs and
22324 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22325 the latter, the DECL we get back will be marked as a
22326 template instantiation, and the appropriate
22327 DECL_TEMPLATE_INFO will be set up. This does not happen for
22328 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22329 should handle VAR_DECLs as it currently handles
22330 FUNCTION_DECLs. */
22331 if (!DECL_CLASS_SCOPE_P (decl))
22333 error ("%qD is not a static data member of a class template", decl);
22334 return;
22336 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22337 if (!result || !VAR_P (result))
22339 error ("no matching template for %qD found", decl);
22340 return;
22342 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22344 error ("type %qT for explicit instantiation %qD does not match "
22345 "declared type %qT", TREE_TYPE (result), decl,
22346 TREE_TYPE (decl));
22347 return;
22350 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22352 error ("explicit instantiation of %q#D", decl);
22353 return;
22355 else
22356 result = decl;
22358 /* Check for various error cases. Note that if the explicit
22359 instantiation is valid the RESULT will currently be marked as an
22360 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22361 until we get here. */
22363 if (DECL_TEMPLATE_SPECIALIZATION (result))
22365 /* DR 259 [temp.spec].
22367 Both an explicit instantiation and a declaration of an explicit
22368 specialization shall not appear in a program unless the explicit
22369 instantiation follows a declaration of the explicit specialization.
22371 For a given set of template parameters, if an explicit
22372 instantiation of a template appears after a declaration of an
22373 explicit specialization for that template, the explicit
22374 instantiation has no effect. */
22375 return;
22377 else if (DECL_EXPLICIT_INSTANTIATION (result))
22379 /* [temp.spec]
22381 No program shall explicitly instantiate any template more
22382 than once.
22384 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22385 the first instantiation was `extern' and the second is not,
22386 and EXTERN_P for the opposite case. */
22387 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22388 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22389 /* If an "extern" explicit instantiation follows an ordinary
22390 explicit instantiation, the template is instantiated. */
22391 if (extern_p)
22392 return;
22394 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22396 error ("no matching template for %qD found", result);
22397 return;
22399 else if (!DECL_TEMPLATE_INFO (result))
22401 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22402 return;
22405 if (storage == NULL_TREE)
22407 else if (storage == ridpointers[(int) RID_EXTERN])
22409 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22410 pedwarn (input_location, OPT_Wpedantic,
22411 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22412 "instantiations");
22413 extern_p = 1;
22415 else
22416 error ("storage class %qD applied to template instantiation", storage);
22418 check_explicit_instantiation_namespace (result);
22419 mark_decl_instantiated (result, extern_p);
22420 if (! extern_p)
22421 instantiate_decl (result, /*defer_ok=*/true,
22422 /*expl_inst_class_mem_p=*/false);
22425 static void
22426 mark_class_instantiated (tree t, int extern_p)
22428 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22429 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22430 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22431 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22432 if (! extern_p)
22434 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22435 rest_of_type_compilation (t, 1);
22439 /* Called from do_type_instantiation through binding_table_foreach to
22440 do recursive instantiation for the type bound in ENTRY. */
22441 static void
22442 bt_instantiate_type_proc (binding_entry entry, void *data)
22444 tree storage = *(tree *) data;
22446 if (MAYBE_CLASS_TYPE_P (entry->type)
22447 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22448 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22451 /* Perform an explicit instantiation of template class T. STORAGE, if
22452 non-null, is the RID for extern, inline or static. COMPLAIN is
22453 nonzero if this is called from the parser, zero if called recursively,
22454 since the standard is unclear (as detailed below). */
22456 void
22457 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22459 int extern_p = 0;
22460 int nomem_p = 0;
22461 int static_p = 0;
22462 int previous_instantiation_extern_p = 0;
22464 if (TREE_CODE (t) == TYPE_DECL)
22465 t = TREE_TYPE (t);
22467 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22469 tree tmpl =
22470 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22471 if (tmpl)
22472 error ("explicit instantiation of non-class template %qD", tmpl);
22473 else
22474 error ("explicit instantiation of non-template type %qT", t);
22475 return;
22478 complete_type (t);
22480 if (!COMPLETE_TYPE_P (t))
22482 if (complain & tf_error)
22483 error ("explicit instantiation of %q#T before definition of template",
22485 return;
22488 if (storage != NULL_TREE)
22490 if (!in_system_header_at (input_location))
22492 if (storage == ridpointers[(int) RID_EXTERN])
22494 if (cxx_dialect == cxx98)
22495 pedwarn (input_location, OPT_Wpedantic,
22496 "ISO C++ 1998 forbids the use of %<extern%> on "
22497 "explicit instantiations");
22499 else
22500 pedwarn (input_location, OPT_Wpedantic,
22501 "ISO C++ forbids the use of %qE"
22502 " on explicit instantiations", storage);
22505 if (storage == ridpointers[(int) RID_INLINE])
22506 nomem_p = 1;
22507 else if (storage == ridpointers[(int) RID_EXTERN])
22508 extern_p = 1;
22509 else if (storage == ridpointers[(int) RID_STATIC])
22510 static_p = 1;
22511 else
22513 error ("storage class %qD applied to template instantiation",
22514 storage);
22515 extern_p = 0;
22519 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22521 /* DR 259 [temp.spec].
22523 Both an explicit instantiation and a declaration of an explicit
22524 specialization shall not appear in a program unless the explicit
22525 instantiation follows a declaration of the explicit specialization.
22527 For a given set of template parameters, if an explicit
22528 instantiation of a template appears after a declaration of an
22529 explicit specialization for that template, the explicit
22530 instantiation has no effect. */
22531 return;
22533 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22535 /* [temp.spec]
22537 No program shall explicitly instantiate any template more
22538 than once.
22540 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22541 instantiation was `extern'. If EXTERN_P then the second is.
22542 These cases are OK. */
22543 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22545 if (!previous_instantiation_extern_p && !extern_p
22546 && (complain & tf_error))
22547 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22549 /* If we've already instantiated the template, just return now. */
22550 if (!CLASSTYPE_INTERFACE_ONLY (t))
22551 return;
22554 check_explicit_instantiation_namespace (TYPE_NAME (t));
22555 mark_class_instantiated (t, extern_p);
22557 if (nomem_p)
22558 return;
22560 /* In contrast to implicit instantiation, where only the
22561 declarations, and not the definitions, of members are
22562 instantiated, we have here:
22564 [temp.explicit]
22566 The explicit instantiation of a class template specialization
22567 implies the instantiation of all of its members not
22568 previously explicitly specialized in the translation unit
22569 containing the explicit instantiation.
22571 Of course, we can't instantiate member template classes, since we
22572 don't have any arguments for them. Note that the standard is
22573 unclear on whether the instantiation of the members are
22574 *explicit* instantiations or not. However, the most natural
22575 interpretation is that it should be an explicit
22576 instantiation. */
22577 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22578 if ((VAR_P (fld)
22579 || (TREE_CODE (fld) == FUNCTION_DECL
22580 && !static_p
22581 && user_provided_p (fld)))
22582 && DECL_TEMPLATE_INSTANTIATION (fld))
22584 mark_decl_instantiated (fld, extern_p);
22585 if (! extern_p)
22586 instantiate_decl (fld, /*defer_ok=*/true,
22587 /*expl_inst_class_mem_p=*/true);
22590 if (CLASSTYPE_NESTED_UTDS (t))
22591 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22592 bt_instantiate_type_proc, &storage);
22595 /* Given a function DECL, which is a specialization of TMPL, modify
22596 DECL to be a re-instantiation of TMPL with the same template
22597 arguments. TMPL should be the template into which tsubst'ing
22598 should occur for DECL, not the most general template.
22600 One reason for doing this is a scenario like this:
22602 template <class T>
22603 void f(const T&, int i);
22605 void g() { f(3, 7); }
22607 template <class T>
22608 void f(const T& t, const int i) { }
22610 Note that when the template is first instantiated, with
22611 instantiate_template, the resulting DECL will have no name for the
22612 first parameter, and the wrong type for the second. So, when we go
22613 to instantiate the DECL, we regenerate it. */
22615 static void
22616 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22618 /* The arguments used to instantiate DECL, from the most general
22619 template. */
22620 tree code_pattern;
22622 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22624 /* Make sure that we can see identifiers, and compute access
22625 correctly. */
22626 push_access_scope (decl);
22628 if (TREE_CODE (decl) == FUNCTION_DECL)
22630 tree decl_parm;
22631 tree pattern_parm;
22632 tree specs;
22633 int args_depth;
22634 int parms_depth;
22636 args_depth = TMPL_ARGS_DEPTH (args);
22637 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22638 if (args_depth > parms_depth)
22639 args = get_innermost_template_args (args, parms_depth);
22641 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22642 args, tf_error, NULL_TREE,
22643 /*defer_ok*/false);
22644 if (specs && specs != error_mark_node)
22645 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22646 specs);
22648 /* Merge parameter declarations. */
22649 decl_parm = skip_artificial_parms_for (decl,
22650 DECL_ARGUMENTS (decl));
22651 pattern_parm
22652 = skip_artificial_parms_for (code_pattern,
22653 DECL_ARGUMENTS (code_pattern));
22654 while (decl_parm && !DECL_PACK_P (pattern_parm))
22656 tree parm_type;
22657 tree attributes;
22659 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22660 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22661 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22662 NULL_TREE);
22663 parm_type = type_decays_to (parm_type);
22664 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22665 TREE_TYPE (decl_parm) = parm_type;
22666 attributes = DECL_ATTRIBUTES (pattern_parm);
22667 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22669 DECL_ATTRIBUTES (decl_parm) = attributes;
22670 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22672 decl_parm = DECL_CHAIN (decl_parm);
22673 pattern_parm = DECL_CHAIN (pattern_parm);
22675 /* Merge any parameters that match with the function parameter
22676 pack. */
22677 if (pattern_parm && DECL_PACK_P (pattern_parm))
22679 int i, len;
22680 tree expanded_types;
22681 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22682 the parameters in this function parameter pack. */
22683 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22684 args, tf_error, NULL_TREE);
22685 len = TREE_VEC_LENGTH (expanded_types);
22686 for (i = 0; i < len; i++)
22688 tree parm_type;
22689 tree attributes;
22691 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22692 /* Rename the parameter to include the index. */
22693 DECL_NAME (decl_parm) =
22694 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22695 parm_type = TREE_VEC_ELT (expanded_types, i);
22696 parm_type = type_decays_to (parm_type);
22697 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22698 TREE_TYPE (decl_parm) = parm_type;
22699 attributes = DECL_ATTRIBUTES (pattern_parm);
22700 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22702 DECL_ATTRIBUTES (decl_parm) = attributes;
22703 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22705 decl_parm = DECL_CHAIN (decl_parm);
22708 /* Merge additional specifiers from the CODE_PATTERN. */
22709 if (DECL_DECLARED_INLINE_P (code_pattern)
22710 && !DECL_DECLARED_INLINE_P (decl))
22711 DECL_DECLARED_INLINE_P (decl) = 1;
22713 else if (VAR_P (decl))
22715 start_lambda_scope (decl);
22716 DECL_INITIAL (decl) =
22717 tsubst_expr (DECL_INITIAL (code_pattern), args,
22718 tf_error, DECL_TI_TEMPLATE (decl),
22719 /*integral_constant_expression_p=*/false);
22720 finish_lambda_scope ();
22721 if (VAR_HAD_UNKNOWN_BOUND (decl))
22722 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22723 tf_error, DECL_TI_TEMPLATE (decl));
22725 else
22726 gcc_unreachable ();
22728 pop_access_scope (decl);
22731 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22732 substituted to get DECL. */
22734 tree
22735 template_for_substitution (tree decl)
22737 tree tmpl = DECL_TI_TEMPLATE (decl);
22739 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22740 for the instantiation. This is not always the most general
22741 template. Consider, for example:
22743 template <class T>
22744 struct S { template <class U> void f();
22745 template <> void f<int>(); };
22747 and an instantiation of S<double>::f<int>. We want TD to be the
22748 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22749 while (/* An instantiation cannot have a definition, so we need a
22750 more general template. */
22751 DECL_TEMPLATE_INSTANTIATION (tmpl)
22752 /* We must also deal with friend templates. Given:
22754 template <class T> struct S {
22755 template <class U> friend void f() {};
22758 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22759 so far as the language is concerned, but that's still
22760 where we get the pattern for the instantiation from. On
22761 other hand, if the definition comes outside the class, say:
22763 template <class T> struct S {
22764 template <class U> friend void f();
22766 template <class U> friend void f() {}
22768 we don't need to look any further. That's what the check for
22769 DECL_INITIAL is for. */
22770 || (TREE_CODE (decl) == FUNCTION_DECL
22771 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22772 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22774 /* The present template, TD, should not be a definition. If it
22775 were a definition, we should be using it! Note that we
22776 cannot restructure the loop to just keep going until we find
22777 a template with a definition, since that might go too far if
22778 a specialization was declared, but not defined. */
22780 /* Fetch the more general template. */
22781 tmpl = DECL_TI_TEMPLATE (tmpl);
22784 return tmpl;
22787 /* Returns true if we need to instantiate this template instance even if we
22788 know we aren't going to emit it. */
22790 bool
22791 always_instantiate_p (tree decl)
22793 /* We always instantiate inline functions so that we can inline them. An
22794 explicit instantiation declaration prohibits implicit instantiation of
22795 non-inline functions. With high levels of optimization, we would
22796 normally inline non-inline functions -- but we're not allowed to do
22797 that for "extern template" functions. Therefore, we check
22798 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22799 return ((TREE_CODE (decl) == FUNCTION_DECL
22800 && (DECL_DECLARED_INLINE_P (decl)
22801 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22802 /* And we need to instantiate static data members so that
22803 their initializers are available in integral constant
22804 expressions. */
22805 || (VAR_P (decl)
22806 && decl_maybe_constant_var_p (decl)));
22809 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22810 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22811 error, true otherwise. */
22813 bool
22814 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22816 tree fntype, spec, noex, clone;
22818 /* Don't instantiate a noexcept-specification from template context. */
22819 if (processing_template_decl)
22820 return true;
22822 if (DECL_CLONED_FUNCTION_P (fn))
22823 fn = DECL_CLONED_FUNCTION (fn);
22824 fntype = TREE_TYPE (fn);
22825 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22827 if (!spec || !TREE_PURPOSE (spec))
22828 return true;
22830 noex = TREE_PURPOSE (spec);
22832 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22834 static hash_set<tree>* fns = new hash_set<tree>;
22835 bool added = false;
22836 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22837 spec = get_defaulted_eh_spec (fn, complain);
22838 else if (!(added = !fns->add (fn)))
22840 /* If hash_set::add returns true, the element was already there. */
22841 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22842 DECL_SOURCE_LOCATION (fn));
22843 error_at (loc,
22844 "exception specification of %qD depends on itself",
22845 fn);
22846 spec = noexcept_false_spec;
22848 else if (push_tinst_level (fn))
22850 push_access_scope (fn);
22851 push_deferring_access_checks (dk_no_deferred);
22852 input_location = DECL_SOURCE_LOCATION (fn);
22853 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22854 DEFERRED_NOEXCEPT_ARGS (noex),
22855 tf_warning_or_error, fn,
22856 /*function_p=*/false,
22857 /*integral_constant_expression_p=*/true);
22858 pop_deferring_access_checks ();
22859 pop_access_scope (fn);
22860 pop_tinst_level ();
22861 spec = build_noexcept_spec (noex, tf_warning_or_error);
22862 if (spec == error_mark_node)
22863 spec = noexcept_false_spec;
22865 else
22866 spec = noexcept_false_spec;
22868 if (added)
22869 fns->remove (fn);
22871 if (spec == error_mark_node)
22872 return false;
22874 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22877 FOR_EACH_CLONE (clone, fn)
22879 if (TREE_TYPE (clone) == fntype)
22880 TREE_TYPE (clone) = TREE_TYPE (fn);
22881 else
22882 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22885 return true;
22888 /* We're starting to process the function INST, an instantiation of PATTERN;
22889 add their parameters to local_specializations. */
22891 static void
22892 register_parameter_specializations (tree pattern, tree inst)
22894 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22895 tree spec_parm = DECL_ARGUMENTS (inst);
22896 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22898 register_local_specialization (spec_parm, tmpl_parm);
22899 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22900 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22902 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22904 if (!DECL_PACK_P (tmpl_parm))
22906 register_local_specialization (spec_parm, tmpl_parm);
22907 spec_parm = DECL_CHAIN (spec_parm);
22909 else
22911 /* Register the (value) argument pack as a specialization of
22912 TMPL_PARM, then move on. */
22913 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22914 register_local_specialization (argpack, tmpl_parm);
22917 gcc_assert (!spec_parm);
22920 /* Produce the definition of D, a _DECL generated from a template. If
22921 DEFER_OK is true, then we don't have to actually do the
22922 instantiation now; we just have to do it sometime. Normally it is
22923 an error if this is an explicit instantiation but D is undefined.
22924 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22925 instantiated class template. */
22927 tree
22928 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22930 tree tmpl = DECL_TI_TEMPLATE (d);
22931 tree gen_args;
22932 tree args;
22933 tree td;
22934 tree code_pattern;
22935 tree spec;
22936 tree gen_tmpl;
22937 bool pattern_defined;
22938 location_t saved_loc = input_location;
22939 int saved_unevaluated_operand = cp_unevaluated_operand;
22940 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22941 bool external_p;
22942 bool deleted_p;
22944 /* This function should only be used to instantiate templates for
22945 functions and static member variables. */
22946 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22948 /* A concept is never instantiated. */
22949 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22951 /* Variables are never deferred; if instantiation is required, they
22952 are instantiated right away. That allows for better code in the
22953 case that an expression refers to the value of the variable --
22954 if the variable has a constant value the referring expression can
22955 take advantage of that fact. */
22956 if (VAR_P (d))
22957 defer_ok = false;
22959 /* Don't instantiate cloned functions. Instead, instantiate the
22960 functions they cloned. */
22961 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22962 d = DECL_CLONED_FUNCTION (d);
22964 if (DECL_TEMPLATE_INSTANTIATED (d)
22965 || (TREE_CODE (d) == FUNCTION_DECL
22966 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22967 || DECL_TEMPLATE_SPECIALIZATION (d))
22968 /* D has already been instantiated or explicitly specialized, so
22969 there's nothing for us to do here.
22971 It might seem reasonable to check whether or not D is an explicit
22972 instantiation, and, if so, stop here. But when an explicit
22973 instantiation is deferred until the end of the compilation,
22974 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22975 the instantiation. */
22976 return d;
22978 /* Check to see whether we know that this template will be
22979 instantiated in some other file, as with "extern template"
22980 extension. */
22981 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22983 /* In general, we do not instantiate such templates. */
22984 if (external_p && !always_instantiate_p (d))
22985 return d;
22987 gen_tmpl = most_general_template (tmpl);
22988 gen_args = DECL_TI_ARGS (d);
22990 if (tmpl != gen_tmpl)
22991 /* We should already have the extra args. */
22992 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22993 == TMPL_ARGS_DEPTH (gen_args));
22994 /* And what's in the hash table should match D. */
22995 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22996 || spec == NULL_TREE);
22998 /* This needs to happen before any tsubsting. */
22999 if (! push_tinst_level (d))
23000 return d;
23002 timevar_push (TV_TEMPLATE_INST);
23004 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23005 for the instantiation. */
23006 td = template_for_substitution (d);
23007 args = gen_args;
23009 if (VAR_P (d))
23011 /* Look up an explicit specialization, if any. */
23012 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23013 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23014 if (elt && elt != error_mark_node)
23016 td = TREE_VALUE (elt);
23017 args = TREE_PURPOSE (elt);
23021 code_pattern = DECL_TEMPLATE_RESULT (td);
23023 /* We should never be trying to instantiate a member of a class
23024 template or partial specialization. */
23025 gcc_assert (d != code_pattern);
23027 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23028 || DECL_TEMPLATE_SPECIALIZATION (td))
23029 /* In the case of a friend template whose definition is provided
23030 outside the class, we may have too many arguments. Drop the
23031 ones we don't need. The same is true for specializations. */
23032 args = get_innermost_template_args
23033 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23035 if (TREE_CODE (d) == FUNCTION_DECL)
23037 deleted_p = DECL_DELETED_FN (code_pattern);
23038 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23039 && DECL_INITIAL (code_pattern) != error_mark_node)
23040 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23041 || deleted_p);
23043 else
23045 deleted_p = false;
23046 if (DECL_CLASS_SCOPE_P (code_pattern))
23047 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23048 || DECL_INLINE_VAR_P (code_pattern));
23049 else
23050 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23053 /* We may be in the middle of deferred access check. Disable it now. */
23054 push_deferring_access_checks (dk_no_deferred);
23056 /* Unless an explicit instantiation directive has already determined
23057 the linkage of D, remember that a definition is available for
23058 this entity. */
23059 if (pattern_defined
23060 && !DECL_INTERFACE_KNOWN (d)
23061 && !DECL_NOT_REALLY_EXTERN (d))
23062 mark_definable (d);
23064 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23065 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23066 input_location = DECL_SOURCE_LOCATION (d);
23068 /* If D is a member of an explicitly instantiated class template,
23069 and no definition is available, treat it like an implicit
23070 instantiation. */
23071 if (!pattern_defined && expl_inst_class_mem_p
23072 && DECL_EXPLICIT_INSTANTIATION (d))
23074 /* Leave linkage flags alone on instantiations with anonymous
23075 visibility. */
23076 if (TREE_PUBLIC (d))
23078 DECL_NOT_REALLY_EXTERN (d) = 0;
23079 DECL_INTERFACE_KNOWN (d) = 0;
23081 SET_DECL_IMPLICIT_INSTANTIATION (d);
23084 /* Defer all other templates, unless we have been explicitly
23085 forbidden from doing so. */
23086 if (/* If there is no definition, we cannot instantiate the
23087 template. */
23088 ! pattern_defined
23089 /* If it's OK to postpone instantiation, do so. */
23090 || defer_ok
23091 /* If this is a static data member that will be defined
23092 elsewhere, we don't want to instantiate the entire data
23093 member, but we do want to instantiate the initializer so that
23094 we can substitute that elsewhere. */
23095 || (external_p && VAR_P (d))
23096 /* Handle here a deleted function too, avoid generating
23097 its body (c++/61080). */
23098 || deleted_p)
23100 /* The definition of the static data member is now required so
23101 we must substitute the initializer. */
23102 if (VAR_P (d)
23103 && !DECL_INITIAL (d)
23104 && DECL_INITIAL (code_pattern))
23106 tree ns;
23107 tree init;
23108 bool const_init = false;
23109 bool enter_context = DECL_CLASS_SCOPE_P (d);
23111 ns = decl_namespace_context (d);
23112 push_nested_namespace (ns);
23113 if (enter_context)
23114 push_nested_class (DECL_CONTEXT (d));
23115 init = tsubst_expr (DECL_INITIAL (code_pattern),
23116 args,
23117 tf_warning_or_error, NULL_TREE,
23118 /*integral_constant_expression_p=*/false);
23119 /* If instantiating the initializer involved instantiating this
23120 again, don't call cp_finish_decl twice. */
23121 if (!DECL_INITIAL (d))
23123 /* Make sure the initializer is still constant, in case of
23124 circular dependency (template/instantiate6.C). */
23125 const_init
23126 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23127 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23128 /*asmspec_tree=*/NULL_TREE,
23129 LOOKUP_ONLYCONVERTING);
23131 if (enter_context)
23132 pop_nested_class ();
23133 pop_nested_namespace (ns);
23136 /* We restore the source position here because it's used by
23137 add_pending_template. */
23138 input_location = saved_loc;
23140 if (at_eof && !pattern_defined
23141 && DECL_EXPLICIT_INSTANTIATION (d)
23142 && DECL_NOT_REALLY_EXTERN (d))
23143 /* [temp.explicit]
23145 The definition of a non-exported function template, a
23146 non-exported member function template, or a non-exported
23147 member function or static data member of a class template
23148 shall be present in every translation unit in which it is
23149 explicitly instantiated. */
23150 permerror (input_location, "explicit instantiation of %qD "
23151 "but no definition available", d);
23153 /* If we're in unevaluated context, we just wanted to get the
23154 constant value; this isn't an odr use, so don't queue
23155 a full instantiation. */
23156 if (cp_unevaluated_operand != 0)
23157 goto out;
23158 /* ??? Historically, we have instantiated inline functions, even
23159 when marked as "extern template". */
23160 if (!(external_p && VAR_P (d)))
23161 add_pending_template (d);
23162 goto out;
23164 /* Tell the repository that D is available in this translation unit
23165 -- and see if it is supposed to be instantiated here. */
23166 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23168 /* In a PCH file, despite the fact that the repository hasn't
23169 requested instantiation in the PCH it is still possible that
23170 an instantiation will be required in a file that includes the
23171 PCH. */
23172 if (pch_file)
23173 add_pending_template (d);
23174 /* Instantiate inline functions so that the inliner can do its
23175 job, even though we'll not be emitting a copy of this
23176 function. */
23177 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23178 goto out;
23181 bool push_to_top, nested;
23182 tree fn_context;
23183 fn_context = decl_function_context (d);
23184 nested = current_function_decl != NULL_TREE;
23185 push_to_top = !(nested && fn_context == current_function_decl);
23187 vec<tree> omp_privatization_save;
23188 if (nested)
23189 save_omp_privatization_clauses (omp_privatization_save);
23191 if (push_to_top)
23192 push_to_top_level ();
23193 else
23195 push_function_context ();
23196 cp_unevaluated_operand = 0;
23197 c_inhibit_evaluation_warnings = 0;
23200 /* Mark D as instantiated so that recursive calls to
23201 instantiate_decl do not try to instantiate it again. */
23202 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23204 /* Regenerate the declaration in case the template has been modified
23205 by a subsequent redeclaration. */
23206 regenerate_decl_from_template (d, td, args);
23208 /* We already set the file and line above. Reset them now in case
23209 they changed as a result of calling regenerate_decl_from_template. */
23210 input_location = DECL_SOURCE_LOCATION (d);
23212 if (VAR_P (d))
23214 tree init;
23215 bool const_init = false;
23217 /* Clear out DECL_RTL; whatever was there before may not be right
23218 since we've reset the type of the declaration. */
23219 SET_DECL_RTL (d, NULL);
23220 DECL_IN_AGGR_P (d) = 0;
23222 /* The initializer is placed in DECL_INITIAL by
23223 regenerate_decl_from_template so we don't need to
23224 push/pop_access_scope again here. Pull it out so that
23225 cp_finish_decl can process it. */
23226 init = DECL_INITIAL (d);
23227 DECL_INITIAL (d) = NULL_TREE;
23228 DECL_INITIALIZED_P (d) = 0;
23230 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23231 initializer. That function will defer actual emission until
23232 we have a chance to determine linkage. */
23233 DECL_EXTERNAL (d) = 0;
23235 /* Enter the scope of D so that access-checking works correctly. */
23236 bool enter_context = DECL_CLASS_SCOPE_P (d);
23237 if (enter_context)
23238 push_nested_class (DECL_CONTEXT (d));
23240 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23241 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23243 if (enter_context)
23244 pop_nested_class ();
23246 if (variable_template_p (gen_tmpl))
23247 note_variable_template_instantiation (d);
23249 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23250 synthesize_method (d);
23251 else if (TREE_CODE (d) == FUNCTION_DECL)
23253 /* Set up the list of local specializations. */
23254 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23255 tree block = NULL_TREE;
23257 /* Set up context. */
23258 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23259 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23260 block = push_stmt_list ();
23261 else
23262 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23264 /* Some typedefs referenced from within the template code need to be
23265 access checked at template instantiation time, i.e now. These
23266 types were added to the template at parsing time. Let's get those
23267 and perform the access checks then. */
23268 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23269 args);
23271 /* Create substitution entries for the parameters. */
23272 register_parameter_specializations (code_pattern, d);
23274 /* Substitute into the body of the function. */
23275 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23276 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23277 tf_warning_or_error, tmpl);
23278 else
23280 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23281 tf_warning_or_error, tmpl,
23282 /*integral_constant_expression_p=*/false);
23284 /* Set the current input_location to the end of the function
23285 so that finish_function knows where we are. */
23286 input_location
23287 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23289 /* Remember if we saw an infinite loop in the template. */
23290 current_function_infinite_loop
23291 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23294 /* Finish the function. */
23295 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23296 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23297 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23298 else
23300 d = finish_function (/*inline_p=*/false);
23301 expand_or_defer_fn (d);
23304 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23305 cp_check_omp_declare_reduction (d);
23308 /* We're not deferring instantiation any more. */
23309 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23311 if (push_to_top)
23312 pop_from_top_level ();
23313 else
23314 pop_function_context ();
23316 if (nested)
23317 restore_omp_privatization_clauses (omp_privatization_save);
23319 out:
23320 pop_deferring_access_checks ();
23321 timevar_pop (TV_TEMPLATE_INST);
23322 pop_tinst_level ();
23323 input_location = saved_loc;
23324 cp_unevaluated_operand = saved_unevaluated_operand;
23325 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23327 return d;
23330 /* Run through the list of templates that we wish we could
23331 instantiate, and instantiate any we can. RETRIES is the
23332 number of times we retry pending template instantiation. */
23334 void
23335 instantiate_pending_templates (int retries)
23337 int reconsider;
23338 location_t saved_loc = input_location;
23340 /* Instantiating templates may trigger vtable generation. This in turn
23341 may require further template instantiations. We place a limit here
23342 to avoid infinite loop. */
23343 if (pending_templates && retries >= max_tinst_depth)
23345 tree decl = pending_templates->tinst->decl;
23347 fatal_error (input_location,
23348 "template instantiation depth exceeds maximum of %d"
23349 " instantiating %q+D, possibly from virtual table generation"
23350 " (use -ftemplate-depth= to increase the maximum)",
23351 max_tinst_depth, decl);
23352 if (TREE_CODE (decl) == FUNCTION_DECL)
23353 /* Pretend that we defined it. */
23354 DECL_INITIAL (decl) = error_mark_node;
23355 return;
23360 struct pending_template **t = &pending_templates;
23361 struct pending_template *last = NULL;
23362 reconsider = 0;
23363 while (*t)
23365 tree instantiation = reopen_tinst_level ((*t)->tinst);
23366 bool complete = false;
23368 if (TYPE_P (instantiation))
23370 if (!COMPLETE_TYPE_P (instantiation))
23372 instantiate_class_template (instantiation);
23373 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23374 for (tree fld = TYPE_FIELDS (instantiation);
23375 fld; fld = TREE_CHAIN (fld))
23376 if ((VAR_P (fld)
23377 || (TREE_CODE (fld) == FUNCTION_DECL
23378 && !DECL_ARTIFICIAL (fld)))
23379 && DECL_TEMPLATE_INSTANTIATION (fld))
23380 instantiate_decl (fld,
23381 /*defer_ok=*/false,
23382 /*expl_inst_class_mem_p=*/false);
23384 if (COMPLETE_TYPE_P (instantiation))
23385 reconsider = 1;
23388 complete = COMPLETE_TYPE_P (instantiation);
23390 else
23392 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23393 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23395 instantiation
23396 = instantiate_decl (instantiation,
23397 /*defer_ok=*/false,
23398 /*expl_inst_class_mem_p=*/false);
23399 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23400 reconsider = 1;
23403 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23404 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23407 if (complete)
23408 /* If INSTANTIATION has been instantiated, then we don't
23409 need to consider it again in the future. */
23410 *t = (*t)->next;
23411 else
23413 last = *t;
23414 t = &(*t)->next;
23416 tinst_depth = 0;
23417 current_tinst_level = NULL;
23419 last_pending_template = last;
23421 while (reconsider);
23423 input_location = saved_loc;
23426 /* Substitute ARGVEC into T, which is a list of initializers for
23427 either base class or a non-static data member. The TREE_PURPOSEs
23428 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23429 instantiate_decl. */
23431 static tree
23432 tsubst_initializer_list (tree t, tree argvec)
23434 tree inits = NULL_TREE;
23436 for (; t; t = TREE_CHAIN (t))
23438 tree decl;
23439 tree init;
23440 tree expanded_bases = NULL_TREE;
23441 tree expanded_arguments = NULL_TREE;
23442 int i, len = 1;
23444 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23446 tree expr;
23447 tree arg;
23449 /* Expand the base class expansion type into separate base
23450 classes. */
23451 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23452 tf_warning_or_error,
23453 NULL_TREE);
23454 if (expanded_bases == error_mark_node)
23455 continue;
23457 /* We'll be building separate TREE_LISTs of arguments for
23458 each base. */
23459 len = TREE_VEC_LENGTH (expanded_bases);
23460 expanded_arguments = make_tree_vec (len);
23461 for (i = 0; i < len; i++)
23462 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23464 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23465 expand each argument in the TREE_VALUE of t. */
23466 expr = make_node (EXPR_PACK_EXPANSION);
23467 PACK_EXPANSION_LOCAL_P (expr) = true;
23468 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23469 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23471 if (TREE_VALUE (t) == void_type_node)
23472 /* VOID_TYPE_NODE is used to indicate
23473 value-initialization. */
23475 for (i = 0; i < len; i++)
23476 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23478 else
23480 /* Substitute parameter packs into each argument in the
23481 TREE_LIST. */
23482 in_base_initializer = 1;
23483 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23485 tree expanded_exprs;
23487 /* Expand the argument. */
23488 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23489 expanded_exprs
23490 = tsubst_pack_expansion (expr, argvec,
23491 tf_warning_or_error,
23492 NULL_TREE);
23493 if (expanded_exprs == error_mark_node)
23494 continue;
23496 /* Prepend each of the expanded expressions to the
23497 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23498 for (i = 0; i < len; i++)
23500 TREE_VEC_ELT (expanded_arguments, i) =
23501 tree_cons (NULL_TREE,
23502 TREE_VEC_ELT (expanded_exprs, i),
23503 TREE_VEC_ELT (expanded_arguments, i));
23506 in_base_initializer = 0;
23508 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23509 since we built them backwards. */
23510 for (i = 0; i < len; i++)
23512 TREE_VEC_ELT (expanded_arguments, i) =
23513 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23518 for (i = 0; i < len; ++i)
23520 if (expanded_bases)
23522 decl = TREE_VEC_ELT (expanded_bases, i);
23523 decl = expand_member_init (decl);
23524 init = TREE_VEC_ELT (expanded_arguments, i);
23526 else
23528 tree tmp;
23529 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23530 tf_warning_or_error, NULL_TREE);
23532 decl = expand_member_init (decl);
23533 if (decl && !DECL_P (decl))
23534 in_base_initializer = 1;
23536 init = TREE_VALUE (t);
23537 tmp = init;
23538 if (init != void_type_node)
23539 init = tsubst_expr (init, argvec,
23540 tf_warning_or_error, NULL_TREE,
23541 /*integral_constant_expression_p=*/false);
23542 if (init == NULL_TREE && tmp != NULL_TREE)
23543 /* If we had an initializer but it instantiated to nothing,
23544 value-initialize the object. This will only occur when
23545 the initializer was a pack expansion where the parameter
23546 packs used in that expansion were of length zero. */
23547 init = void_type_node;
23548 in_base_initializer = 0;
23551 if (decl)
23553 init = build_tree_list (decl, init);
23554 TREE_CHAIN (init) = inits;
23555 inits = init;
23559 return inits;
23562 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23564 static void
23565 set_current_access_from_decl (tree decl)
23567 if (TREE_PRIVATE (decl))
23568 current_access_specifier = access_private_node;
23569 else if (TREE_PROTECTED (decl))
23570 current_access_specifier = access_protected_node;
23571 else
23572 current_access_specifier = access_public_node;
23575 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23576 is the instantiation (which should have been created with
23577 start_enum) and ARGS are the template arguments to use. */
23579 static void
23580 tsubst_enum (tree tag, tree newtag, tree args)
23582 tree e;
23584 if (SCOPED_ENUM_P (newtag))
23585 begin_scope (sk_scoped_enum, newtag);
23587 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23589 tree value;
23590 tree decl;
23592 decl = TREE_VALUE (e);
23593 /* Note that in a template enum, the TREE_VALUE is the
23594 CONST_DECL, not the corresponding INTEGER_CST. */
23595 value = tsubst_expr (DECL_INITIAL (decl),
23596 args, tf_warning_or_error, NULL_TREE,
23597 /*integral_constant_expression_p=*/true);
23599 /* Give this enumeration constant the correct access. */
23600 set_current_access_from_decl (decl);
23602 /* Actually build the enumerator itself. Here we're assuming that
23603 enumerators can't have dependent attributes. */
23604 build_enumerator (DECL_NAME (decl), value, newtag,
23605 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23608 if (SCOPED_ENUM_P (newtag))
23609 finish_scope ();
23611 finish_enum_value_list (newtag);
23612 finish_enum (newtag);
23614 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23615 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23618 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23619 its type -- but without substituting the innermost set of template
23620 arguments. So, innermost set of template parameters will appear in
23621 the type. */
23623 tree
23624 get_mostly_instantiated_function_type (tree decl)
23626 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23627 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23630 /* Return truthvalue if we're processing a template different from
23631 the last one involved in diagnostics. */
23632 bool
23633 problematic_instantiation_changed (void)
23635 return current_tinst_level != last_error_tinst_level;
23638 /* Remember current template involved in diagnostics. */
23639 void
23640 record_last_problematic_instantiation (void)
23642 last_error_tinst_level = current_tinst_level;
23645 struct tinst_level *
23646 current_instantiation (void)
23648 return current_tinst_level;
23651 /* Return TRUE if current_function_decl is being instantiated, false
23652 otherwise. */
23654 bool
23655 instantiating_current_function_p (void)
23657 return (current_instantiation ()
23658 && current_instantiation ()->decl == current_function_decl);
23661 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23662 type. Return false for ok, true for disallowed. Issue error and
23663 inform messages under control of COMPLAIN. */
23665 static bool
23666 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23668 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23669 return false;
23670 else if (POINTER_TYPE_P (type))
23671 return false;
23672 else if (TYPE_PTRMEM_P (type))
23673 return false;
23674 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23675 return false;
23676 else if (TREE_CODE (type) == TYPENAME_TYPE)
23677 return false;
23678 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23679 return false;
23680 else if (TREE_CODE (type) == NULLPTR_TYPE)
23681 return false;
23682 /* A bound template template parm could later be instantiated to have a valid
23683 nontype parm type via an alias template. */
23684 else if (cxx_dialect >= cxx11
23685 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23686 return false;
23688 if (complain & tf_error)
23690 if (type == error_mark_node)
23691 inform (input_location, "invalid template non-type parameter");
23692 else
23693 error ("%q#T is not a valid type for a template non-type parameter",
23694 type);
23696 return true;
23699 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23700 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23702 static bool
23703 dependent_type_p_r (tree type)
23705 tree scope;
23707 /* [temp.dep.type]
23709 A type is dependent if it is:
23711 -- a template parameter. Template template parameters are types
23712 for us (since TYPE_P holds true for them) so we handle
23713 them here. */
23714 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23715 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23716 return true;
23717 /* -- a qualified-id with a nested-name-specifier which contains a
23718 class-name that names a dependent type or whose unqualified-id
23719 names a dependent type. */
23720 if (TREE_CODE (type) == TYPENAME_TYPE)
23721 return true;
23723 /* An alias template specialization can be dependent even if the
23724 resulting type is not. */
23725 if (dependent_alias_template_spec_p (type))
23726 return true;
23728 /* -- a cv-qualified type where the cv-unqualified type is
23729 dependent.
23730 No code is necessary for this bullet; the code below handles
23731 cv-qualified types, and we don't want to strip aliases with
23732 TYPE_MAIN_VARIANT because of DR 1558. */
23733 /* -- a compound type constructed from any dependent type. */
23734 if (TYPE_PTRMEM_P (type))
23735 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23736 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23737 (type)));
23738 else if (TYPE_PTR_P (type)
23739 || TREE_CODE (type) == REFERENCE_TYPE)
23740 return dependent_type_p (TREE_TYPE (type));
23741 else if (TREE_CODE (type) == FUNCTION_TYPE
23742 || TREE_CODE (type) == METHOD_TYPE)
23744 tree arg_type;
23746 if (dependent_type_p (TREE_TYPE (type)))
23747 return true;
23748 for (arg_type = TYPE_ARG_TYPES (type);
23749 arg_type;
23750 arg_type = TREE_CHAIN (arg_type))
23751 if (dependent_type_p (TREE_VALUE (arg_type)))
23752 return true;
23753 if (cxx_dialect >= cxx17)
23754 /* A value-dependent noexcept-specifier makes the type dependent. */
23755 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
23756 if (tree noex = TREE_PURPOSE (spec))
23757 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
23758 affect overload resolution and treating it as dependent breaks
23759 things. */
23760 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
23761 && value_dependent_expression_p (noex))
23762 return true;
23763 return false;
23765 /* -- an array type constructed from any dependent type or whose
23766 size is specified by a constant expression that is
23767 value-dependent.
23769 We checked for type- and value-dependence of the bounds in
23770 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23771 if (TREE_CODE (type) == ARRAY_TYPE)
23773 if (TYPE_DOMAIN (type)
23774 && dependent_type_p (TYPE_DOMAIN (type)))
23775 return true;
23776 return dependent_type_p (TREE_TYPE (type));
23779 /* -- a template-id in which either the template name is a template
23780 parameter ... */
23781 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23782 return true;
23783 /* ... or any of the template arguments is a dependent type or
23784 an expression that is type-dependent or value-dependent. */
23785 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23786 && (any_dependent_template_arguments_p
23787 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23788 return true;
23790 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23791 dependent; if the argument of the `typeof' expression is not
23792 type-dependent, then it should already been have resolved. */
23793 if (TREE_CODE (type) == TYPEOF_TYPE
23794 || TREE_CODE (type) == DECLTYPE_TYPE
23795 || TREE_CODE (type) == UNDERLYING_TYPE)
23796 return true;
23798 /* A template argument pack is dependent if any of its packed
23799 arguments are. */
23800 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23802 tree args = ARGUMENT_PACK_ARGS (type);
23803 int i, len = TREE_VEC_LENGTH (args);
23804 for (i = 0; i < len; ++i)
23805 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23806 return true;
23809 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23810 be template parameters. */
23811 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23812 return true;
23814 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23815 return true;
23817 /* The standard does not specifically mention types that are local
23818 to template functions or local classes, but they should be
23819 considered dependent too. For example:
23821 template <int I> void f() {
23822 enum E { a = I };
23823 S<sizeof (E)> s;
23826 The size of `E' cannot be known until the value of `I' has been
23827 determined. Therefore, `E' must be considered dependent. */
23828 scope = TYPE_CONTEXT (type);
23829 if (scope && TYPE_P (scope))
23830 return dependent_type_p (scope);
23831 /* Don't use type_dependent_expression_p here, as it can lead
23832 to infinite recursion trying to determine whether a lambda
23833 nested in a lambda is dependent (c++/47687). */
23834 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23835 && DECL_LANG_SPECIFIC (scope)
23836 && DECL_TEMPLATE_INFO (scope)
23837 && (any_dependent_template_arguments_p
23838 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23839 return true;
23841 /* Other types are non-dependent. */
23842 return false;
23845 /* Returns TRUE if TYPE is dependent, in the sense of
23846 [temp.dep.type]. Note that a NULL type is considered dependent. */
23848 bool
23849 dependent_type_p (tree type)
23851 /* If there are no template parameters in scope, then there can't be
23852 any dependent types. */
23853 if (!processing_template_decl)
23855 /* If we are not processing a template, then nobody should be
23856 providing us with a dependent type. */
23857 gcc_assert (type);
23858 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23859 return false;
23862 /* If the type is NULL, we have not computed a type for the entity
23863 in question; in that case, the type is dependent. */
23864 if (!type)
23865 return true;
23867 /* Erroneous types can be considered non-dependent. */
23868 if (type == error_mark_node)
23869 return false;
23871 /* Getting here with global_type_node means we improperly called this
23872 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23873 gcc_checking_assert (type != global_type_node);
23875 /* If we have not already computed the appropriate value for TYPE,
23876 do so now. */
23877 if (!TYPE_DEPENDENT_P_VALID (type))
23879 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23880 TYPE_DEPENDENT_P_VALID (type) = 1;
23883 return TYPE_DEPENDENT_P (type);
23886 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23887 lookup. In other words, a dependent type that is not the current
23888 instantiation. */
23890 bool
23891 dependent_scope_p (tree scope)
23893 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23894 && !currently_open_class (scope));
23897 /* T is a SCOPE_REF; return whether we need to consider it
23898 instantiation-dependent so that we can check access at instantiation
23899 time even though we know which member it resolves to. */
23901 static bool
23902 instantiation_dependent_scope_ref_p (tree t)
23904 if (DECL_P (TREE_OPERAND (t, 1))
23905 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23906 && accessible_in_template_p (TREE_OPERAND (t, 0),
23907 TREE_OPERAND (t, 1)))
23908 return false;
23909 else
23910 return true;
23913 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23914 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23915 expression. */
23917 /* Note that this predicate is not appropriate for general expressions;
23918 only constant expressions (that satisfy potential_constant_expression)
23919 can be tested for value dependence. */
23921 bool
23922 value_dependent_expression_p (tree expression)
23924 if (!processing_template_decl || expression == NULL_TREE)
23925 return false;
23927 /* A type-dependent expression is also value-dependent. */
23928 if (type_dependent_expression_p (expression))
23929 return true;
23931 switch (TREE_CODE (expression))
23933 case BASELINK:
23934 /* A dependent member function of the current instantiation. */
23935 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23937 case FUNCTION_DECL:
23938 /* A dependent member function of the current instantiation. */
23939 if (DECL_CLASS_SCOPE_P (expression)
23940 && dependent_type_p (DECL_CONTEXT (expression)))
23941 return true;
23942 break;
23944 case IDENTIFIER_NODE:
23945 /* A name that has not been looked up -- must be dependent. */
23946 return true;
23948 case TEMPLATE_PARM_INDEX:
23949 /* A non-type template parm. */
23950 return true;
23952 case CONST_DECL:
23953 /* A non-type template parm. */
23954 if (DECL_TEMPLATE_PARM_P (expression))
23955 return true;
23956 return value_dependent_expression_p (DECL_INITIAL (expression));
23958 case VAR_DECL:
23959 /* A constant with literal type and is initialized
23960 with an expression that is value-dependent.
23962 Note that a non-dependent parenthesized initializer will have
23963 already been replaced with its constant value, so if we see
23964 a TREE_LIST it must be dependent. */
23965 if (DECL_INITIAL (expression)
23966 && decl_constant_var_p (expression)
23967 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23968 /* cp_finish_decl doesn't fold reference initializers. */
23969 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23970 || value_dependent_expression_p (DECL_INITIAL (expression))))
23971 return true;
23972 if (DECL_HAS_VALUE_EXPR_P (expression))
23974 tree value_expr = DECL_VALUE_EXPR (expression);
23975 if (value_dependent_expression_p (value_expr))
23976 return true;
23978 return false;
23980 case DYNAMIC_CAST_EXPR:
23981 case STATIC_CAST_EXPR:
23982 case CONST_CAST_EXPR:
23983 case REINTERPRET_CAST_EXPR:
23984 case CAST_EXPR:
23985 case IMPLICIT_CONV_EXPR:
23986 /* These expressions are value-dependent if the type to which
23987 the cast occurs is dependent or the expression being casted
23988 is value-dependent. */
23990 tree type = TREE_TYPE (expression);
23992 if (dependent_type_p (type))
23993 return true;
23995 /* A functional cast has a list of operands. */
23996 expression = TREE_OPERAND (expression, 0);
23997 if (!expression)
23999 /* If there are no operands, it must be an expression such
24000 as "int()". This should not happen for aggregate types
24001 because it would form non-constant expressions. */
24002 gcc_assert (cxx_dialect >= cxx11
24003 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24005 return false;
24008 if (TREE_CODE (expression) == TREE_LIST)
24009 return any_value_dependent_elements_p (expression);
24011 return value_dependent_expression_p (expression);
24014 case SIZEOF_EXPR:
24015 if (SIZEOF_EXPR_TYPE_P (expression))
24016 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24017 /* FALLTHRU */
24018 case ALIGNOF_EXPR:
24019 case TYPEID_EXPR:
24020 /* A `sizeof' expression is value-dependent if the operand is
24021 type-dependent or is a pack expansion. */
24022 expression = TREE_OPERAND (expression, 0);
24023 if (PACK_EXPANSION_P (expression))
24024 return true;
24025 else if (TYPE_P (expression))
24026 return dependent_type_p (expression);
24027 return instantiation_dependent_uneval_expression_p (expression);
24029 case AT_ENCODE_EXPR:
24030 /* An 'encode' expression is value-dependent if the operand is
24031 type-dependent. */
24032 expression = TREE_OPERAND (expression, 0);
24033 return dependent_type_p (expression);
24035 case NOEXCEPT_EXPR:
24036 expression = TREE_OPERAND (expression, 0);
24037 return instantiation_dependent_uneval_expression_p (expression);
24039 case SCOPE_REF:
24040 /* All instantiation-dependent expressions should also be considered
24041 value-dependent. */
24042 return instantiation_dependent_scope_ref_p (expression);
24044 case COMPONENT_REF:
24045 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24046 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24048 case NONTYPE_ARGUMENT_PACK:
24049 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24050 is value-dependent. */
24052 tree values = ARGUMENT_PACK_ARGS (expression);
24053 int i, len = TREE_VEC_LENGTH (values);
24055 for (i = 0; i < len; ++i)
24056 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24057 return true;
24059 return false;
24062 case TRAIT_EXPR:
24064 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24066 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24067 return true;
24069 if (!type2)
24070 return false;
24072 if (TREE_CODE (type2) != TREE_LIST)
24073 return dependent_type_p (type2);
24075 for (; type2; type2 = TREE_CHAIN (type2))
24076 if (dependent_type_p (TREE_VALUE (type2)))
24077 return true;
24079 return false;
24082 case MODOP_EXPR:
24083 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24084 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24086 case ARRAY_REF:
24087 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24088 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24090 case ADDR_EXPR:
24092 tree op = TREE_OPERAND (expression, 0);
24093 return (value_dependent_expression_p (op)
24094 || has_value_dependent_address (op));
24097 case REQUIRES_EXPR:
24098 /* Treat all requires-expressions as value-dependent so
24099 we don't try to fold them. */
24100 return true;
24102 case TYPE_REQ:
24103 return dependent_type_p (TREE_OPERAND (expression, 0));
24105 case CALL_EXPR:
24107 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24108 return true;
24109 tree fn = get_callee_fndecl (expression);
24110 int i, nargs;
24111 nargs = call_expr_nargs (expression);
24112 for (i = 0; i < nargs; ++i)
24114 tree op = CALL_EXPR_ARG (expression, i);
24115 /* In a call to a constexpr member function, look through the
24116 implicit ADDR_EXPR on the object argument so that it doesn't
24117 cause the call to be considered value-dependent. We also
24118 look through it in potential_constant_expression. */
24119 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24120 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24121 && TREE_CODE (op) == ADDR_EXPR)
24122 op = TREE_OPERAND (op, 0);
24123 if (value_dependent_expression_p (op))
24124 return true;
24126 return false;
24129 case TEMPLATE_ID_EXPR:
24130 return variable_concept_p (TREE_OPERAND (expression, 0));
24132 case CONSTRUCTOR:
24134 unsigned ix;
24135 tree val;
24136 if (dependent_type_p (TREE_TYPE (expression)))
24137 return true;
24138 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24139 if (value_dependent_expression_p (val))
24140 return true;
24141 return false;
24144 case STMT_EXPR:
24145 /* Treat a GNU statement expression as dependent to avoid crashing
24146 under instantiate_non_dependent_expr; it can't be constant. */
24147 return true;
24149 default:
24150 /* A constant expression is value-dependent if any subexpression is
24151 value-dependent. */
24152 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24154 case tcc_reference:
24155 case tcc_unary:
24156 case tcc_comparison:
24157 case tcc_binary:
24158 case tcc_expression:
24159 case tcc_vl_exp:
24161 int i, len = cp_tree_operand_length (expression);
24163 for (i = 0; i < len; i++)
24165 tree t = TREE_OPERAND (expression, i);
24167 /* In some cases, some of the operands may be missing.
24168 (For example, in the case of PREDECREMENT_EXPR, the
24169 amount to increment by may be missing.) That doesn't
24170 make the expression dependent. */
24171 if (t && value_dependent_expression_p (t))
24172 return true;
24175 break;
24176 default:
24177 break;
24179 break;
24182 /* The expression is not value-dependent. */
24183 return false;
24186 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24187 [temp.dep.expr]. Note that an expression with no type is
24188 considered dependent. Other parts of the compiler arrange for an
24189 expression with type-dependent subexpressions to have no type, so
24190 this function doesn't have to be fully recursive. */
24192 bool
24193 type_dependent_expression_p (tree expression)
24195 if (!processing_template_decl)
24196 return false;
24198 if (expression == NULL_TREE || expression == error_mark_node)
24199 return false;
24201 /* An unresolved name is always dependent. */
24202 if (identifier_p (expression)
24203 || TREE_CODE (expression) == USING_DECL
24204 || TREE_CODE (expression) == WILDCARD_DECL)
24205 return true;
24207 /* A fold expression is type-dependent. */
24208 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24209 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24210 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24211 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24212 return true;
24214 /* Some expression forms are never type-dependent. */
24215 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24216 || TREE_CODE (expression) == SIZEOF_EXPR
24217 || TREE_CODE (expression) == ALIGNOF_EXPR
24218 || TREE_CODE (expression) == AT_ENCODE_EXPR
24219 || TREE_CODE (expression) == NOEXCEPT_EXPR
24220 || TREE_CODE (expression) == TRAIT_EXPR
24221 || TREE_CODE (expression) == TYPEID_EXPR
24222 || TREE_CODE (expression) == DELETE_EXPR
24223 || TREE_CODE (expression) == VEC_DELETE_EXPR
24224 || TREE_CODE (expression) == THROW_EXPR
24225 || TREE_CODE (expression) == REQUIRES_EXPR)
24226 return false;
24228 /* The types of these expressions depends only on the type to which
24229 the cast occurs. */
24230 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24231 || TREE_CODE (expression) == STATIC_CAST_EXPR
24232 || TREE_CODE (expression) == CONST_CAST_EXPR
24233 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24234 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24235 || TREE_CODE (expression) == CAST_EXPR)
24236 return dependent_type_p (TREE_TYPE (expression));
24238 /* The types of these expressions depends only on the type created
24239 by the expression. */
24240 if (TREE_CODE (expression) == NEW_EXPR
24241 || TREE_CODE (expression) == VEC_NEW_EXPR)
24243 /* For NEW_EXPR tree nodes created inside a template, either
24244 the object type itself or a TREE_LIST may appear as the
24245 operand 1. */
24246 tree type = TREE_OPERAND (expression, 1);
24247 if (TREE_CODE (type) == TREE_LIST)
24248 /* This is an array type. We need to check array dimensions
24249 as well. */
24250 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24251 || value_dependent_expression_p
24252 (TREE_OPERAND (TREE_VALUE (type), 1));
24253 else
24254 return dependent_type_p (type);
24257 if (TREE_CODE (expression) == SCOPE_REF)
24259 tree scope = TREE_OPERAND (expression, 0);
24260 tree name = TREE_OPERAND (expression, 1);
24262 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24263 contains an identifier associated by name lookup with one or more
24264 declarations declared with a dependent type, or...a
24265 nested-name-specifier or qualified-id that names a member of an
24266 unknown specialization. */
24267 return (type_dependent_expression_p (name)
24268 || dependent_scope_p (scope));
24271 if (TREE_CODE (expression) == TEMPLATE_DECL
24272 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24273 return uses_outer_template_parms (expression);
24275 if (TREE_CODE (expression) == STMT_EXPR)
24276 expression = stmt_expr_value_expr (expression);
24278 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24280 tree elt;
24281 unsigned i;
24283 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24285 if (type_dependent_expression_p (elt))
24286 return true;
24288 return false;
24291 /* A static data member of the current instantiation with incomplete
24292 array type is type-dependent, as the definition and specializations
24293 can have different bounds. */
24294 if (VAR_P (expression)
24295 && DECL_CLASS_SCOPE_P (expression)
24296 && dependent_type_p (DECL_CONTEXT (expression))
24297 && VAR_HAD_UNKNOWN_BOUND (expression))
24298 return true;
24300 /* An array of unknown bound depending on a variadic parameter, eg:
24302 template<typename... Args>
24303 void foo (Args... args)
24305 int arr[] = { args... };
24308 template<int... vals>
24309 void bar ()
24311 int arr[] = { vals... };
24314 If the array has no length and has an initializer, it must be that
24315 we couldn't determine its length in cp_complete_array_type because
24316 it is dependent. */
24317 if (VAR_P (expression)
24318 && TREE_TYPE (expression) != NULL_TREE
24319 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24320 && !TYPE_DOMAIN (TREE_TYPE (expression))
24321 && DECL_INITIAL (expression))
24322 return true;
24324 /* A function or variable template-id is type-dependent if it has any
24325 dependent template arguments. */
24326 if (VAR_OR_FUNCTION_DECL_P (expression)
24327 && DECL_LANG_SPECIFIC (expression)
24328 && DECL_TEMPLATE_INFO (expression))
24330 /* Consider the innermost template arguments, since those are the ones
24331 that come from the template-id; the template arguments for the
24332 enclosing class do not make it type-dependent unless they are used in
24333 the type of the decl. */
24334 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24335 && (any_dependent_template_arguments_p
24336 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24337 return true;
24340 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24341 type-dependent. Checking this is important for functions with auto return
24342 type, which looks like a dependent type. */
24343 if (TREE_CODE (expression) == FUNCTION_DECL
24344 && !(DECL_CLASS_SCOPE_P (expression)
24345 && dependent_type_p (DECL_CONTEXT (expression)))
24346 && !(DECL_FRIEND_P (expression)
24347 && (!DECL_FRIEND_CONTEXT (expression)
24348 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24349 && !DECL_LOCAL_FUNCTION_P (expression))
24351 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24352 || undeduced_auto_decl (expression));
24353 return false;
24356 /* Always dependent, on the number of arguments if nothing else. */
24357 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24358 return true;
24360 if (TREE_TYPE (expression) == unknown_type_node)
24362 if (TREE_CODE (expression) == ADDR_EXPR)
24363 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24364 if (TREE_CODE (expression) == COMPONENT_REF
24365 || TREE_CODE (expression) == OFFSET_REF)
24367 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24368 return true;
24369 expression = TREE_OPERAND (expression, 1);
24370 if (identifier_p (expression))
24371 return false;
24373 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24374 if (TREE_CODE (expression) == SCOPE_REF)
24375 return false;
24377 if (BASELINK_P (expression))
24379 if (BASELINK_OPTYPE (expression)
24380 && dependent_type_p (BASELINK_OPTYPE (expression)))
24381 return true;
24382 expression = BASELINK_FUNCTIONS (expression);
24385 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24387 if (any_dependent_template_arguments_p
24388 (TREE_OPERAND (expression, 1)))
24389 return true;
24390 expression = TREE_OPERAND (expression, 0);
24391 if (identifier_p (expression))
24392 return true;
24395 gcc_assert (TREE_CODE (expression) == OVERLOAD
24396 || TREE_CODE (expression) == FUNCTION_DECL);
24398 for (lkp_iterator iter (expression); iter; ++iter)
24399 if (type_dependent_expression_p (*iter))
24400 return true;
24402 return false;
24405 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24407 /* Dependent type attributes might not have made it from the decl to
24408 the type yet. */
24409 if (DECL_P (expression)
24410 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24411 return true;
24413 return (dependent_type_p (TREE_TYPE (expression)));
24416 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24417 type-dependent if the expression refers to a member of the current
24418 instantiation and the type of the referenced member is dependent, or the
24419 class member access expression refers to a member of an unknown
24420 specialization.
24422 This function returns true if the OBJECT in such a class member access
24423 expression is of an unknown specialization. */
24425 bool
24426 type_dependent_object_expression_p (tree object)
24428 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24429 dependent. */
24430 if (TREE_CODE (object) == IDENTIFIER_NODE)
24431 return true;
24432 tree scope = TREE_TYPE (object);
24433 return (!scope || dependent_scope_p (scope));
24436 /* walk_tree callback function for instantiation_dependent_expression_p,
24437 below. Returns non-zero if a dependent subexpression is found. */
24439 static tree
24440 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24441 void * /*data*/)
24443 if (TYPE_P (*tp))
24445 /* We don't have to worry about decltype currently because decltype
24446 of an instantiation-dependent expr is a dependent type. This
24447 might change depending on the resolution of DR 1172. */
24448 *walk_subtrees = false;
24449 return NULL_TREE;
24451 enum tree_code code = TREE_CODE (*tp);
24452 switch (code)
24454 /* Don't treat an argument list as dependent just because it has no
24455 TREE_TYPE. */
24456 case TREE_LIST:
24457 case TREE_VEC:
24458 return NULL_TREE;
24460 case TEMPLATE_PARM_INDEX:
24461 return *tp;
24463 /* Handle expressions with type operands. */
24464 case SIZEOF_EXPR:
24465 case ALIGNOF_EXPR:
24466 case TYPEID_EXPR:
24467 case AT_ENCODE_EXPR:
24469 tree op = TREE_OPERAND (*tp, 0);
24470 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24471 op = TREE_TYPE (op);
24472 if (TYPE_P (op))
24474 if (dependent_type_p (op))
24475 return *tp;
24476 else
24478 *walk_subtrees = false;
24479 return NULL_TREE;
24482 break;
24485 case COMPONENT_REF:
24486 if (identifier_p (TREE_OPERAND (*tp, 1)))
24487 /* In a template, finish_class_member_access_expr creates a
24488 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24489 type-dependent, so that we can check access control at
24490 instantiation time (PR 42277). See also Core issue 1273. */
24491 return *tp;
24492 break;
24494 case SCOPE_REF:
24495 if (instantiation_dependent_scope_ref_p (*tp))
24496 return *tp;
24497 else
24498 break;
24500 /* Treat statement-expressions as dependent. */
24501 case BIND_EXPR:
24502 return *tp;
24504 /* Treat requires-expressions as dependent. */
24505 case REQUIRES_EXPR:
24506 return *tp;
24508 case CALL_EXPR:
24509 /* Treat calls to function concepts as dependent. */
24510 if (function_concept_check_p (*tp))
24511 return *tp;
24512 break;
24514 case TEMPLATE_ID_EXPR:
24515 /* And variable concepts. */
24516 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24517 return *tp;
24518 break;
24520 default:
24521 break;
24524 if (type_dependent_expression_p (*tp))
24525 return *tp;
24526 else
24527 return NULL_TREE;
24530 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24531 sense defined by the ABI:
24533 "An expression is instantiation-dependent if it is type-dependent
24534 or value-dependent, or it has a subexpression that is type-dependent
24535 or value-dependent."
24537 Except don't actually check value-dependence for unevaluated expressions,
24538 because in sizeof(i) we don't care about the value of i. Checking
24539 type-dependence will in turn check value-dependence of array bounds/template
24540 arguments as needed. */
24542 bool
24543 instantiation_dependent_uneval_expression_p (tree expression)
24545 tree result;
24547 if (!processing_template_decl)
24548 return false;
24550 if (expression == error_mark_node)
24551 return false;
24553 result = cp_walk_tree_without_duplicates (&expression,
24554 instantiation_dependent_r, NULL);
24555 return result != NULL_TREE;
24558 /* As above, but also check value-dependence of the expression as a whole. */
24560 bool
24561 instantiation_dependent_expression_p (tree expression)
24563 return (instantiation_dependent_uneval_expression_p (expression)
24564 || value_dependent_expression_p (expression));
24567 /* Like type_dependent_expression_p, but it also works while not processing
24568 a template definition, i.e. during substitution or mangling. */
24570 bool
24571 type_dependent_expression_p_push (tree expr)
24573 bool b;
24574 ++processing_template_decl;
24575 b = type_dependent_expression_p (expr);
24576 --processing_template_decl;
24577 return b;
24580 /* Returns TRUE if ARGS contains a type-dependent expression. */
24582 bool
24583 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24585 unsigned int i;
24586 tree arg;
24588 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24590 if (type_dependent_expression_p (arg))
24591 return true;
24593 return false;
24596 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24597 expressions) contains any type-dependent expressions. */
24599 bool
24600 any_type_dependent_elements_p (const_tree list)
24602 for (; list; list = TREE_CHAIN (list))
24603 if (type_dependent_expression_p (TREE_VALUE (list)))
24604 return true;
24606 return false;
24609 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24610 expressions) contains any value-dependent expressions. */
24612 bool
24613 any_value_dependent_elements_p (const_tree list)
24615 for (; list; list = TREE_CHAIN (list))
24616 if (value_dependent_expression_p (TREE_VALUE (list)))
24617 return true;
24619 return false;
24622 /* Returns TRUE if the ARG (a template argument) is dependent. */
24624 bool
24625 dependent_template_arg_p (tree arg)
24627 if (!processing_template_decl)
24628 return false;
24630 /* Assume a template argument that was wrongly written by the user
24631 is dependent. This is consistent with what
24632 any_dependent_template_arguments_p [that calls this function]
24633 does. */
24634 if (!arg || arg == error_mark_node)
24635 return true;
24637 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24638 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24640 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24641 return true;
24642 if (TREE_CODE (arg) == TEMPLATE_DECL)
24644 if (DECL_TEMPLATE_PARM_P (arg))
24645 return true;
24646 /* A member template of a dependent class is not necessarily
24647 type-dependent, but it is a dependent template argument because it
24648 will be a member of an unknown specialization to that template. */
24649 tree scope = CP_DECL_CONTEXT (arg);
24650 return TYPE_P (scope) && dependent_type_p (scope);
24652 else if (ARGUMENT_PACK_P (arg))
24654 tree args = ARGUMENT_PACK_ARGS (arg);
24655 int i, len = TREE_VEC_LENGTH (args);
24656 for (i = 0; i < len; ++i)
24658 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24659 return true;
24662 return false;
24664 else if (TYPE_P (arg))
24665 return dependent_type_p (arg);
24666 else
24667 return (type_dependent_expression_p (arg)
24668 || value_dependent_expression_p (arg));
24671 /* Returns true if ARGS (a collection of template arguments) contains
24672 any types that require structural equality testing. */
24674 bool
24675 any_template_arguments_need_structural_equality_p (tree args)
24677 int i;
24678 int j;
24680 if (!args)
24681 return false;
24682 if (args == error_mark_node)
24683 return true;
24685 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24687 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24688 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24690 tree arg = TREE_VEC_ELT (level, j);
24691 tree packed_args = NULL_TREE;
24692 int k, len = 1;
24694 if (ARGUMENT_PACK_P (arg))
24696 /* Look inside the argument pack. */
24697 packed_args = ARGUMENT_PACK_ARGS (arg);
24698 len = TREE_VEC_LENGTH (packed_args);
24701 for (k = 0; k < len; ++k)
24703 if (packed_args)
24704 arg = TREE_VEC_ELT (packed_args, k);
24706 if (error_operand_p (arg))
24707 return true;
24708 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24709 continue;
24710 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24711 return true;
24712 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24713 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24714 return true;
24719 return false;
24722 /* Returns true if ARGS (a collection of template arguments) contains
24723 any dependent arguments. */
24725 bool
24726 any_dependent_template_arguments_p (const_tree args)
24728 int i;
24729 int j;
24731 if (!args)
24732 return false;
24733 if (args == error_mark_node)
24734 return true;
24736 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24738 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24739 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24740 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24741 return true;
24744 return false;
24747 /* Returns TRUE if the template TMPL is type-dependent. */
24749 bool
24750 dependent_template_p (tree tmpl)
24752 if (TREE_CODE (tmpl) == OVERLOAD)
24754 for (lkp_iterator iter (tmpl); iter; ++iter)
24755 if (dependent_template_p (*iter))
24756 return true;
24757 return false;
24760 /* Template template parameters are dependent. */
24761 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24762 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24763 return true;
24764 /* So are names that have not been looked up. */
24765 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24766 return true;
24767 return false;
24770 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24772 bool
24773 dependent_template_id_p (tree tmpl, tree args)
24775 return (dependent_template_p (tmpl)
24776 || any_dependent_template_arguments_p (args));
24779 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24780 are dependent. */
24782 bool
24783 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24785 int i;
24787 if (!processing_template_decl)
24788 return false;
24790 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24792 tree decl = TREE_VEC_ELT (declv, i);
24793 tree init = TREE_VEC_ELT (initv, i);
24794 tree cond = TREE_VEC_ELT (condv, i);
24795 tree incr = TREE_VEC_ELT (incrv, i);
24797 if (type_dependent_expression_p (decl)
24798 || TREE_CODE (decl) == SCOPE_REF)
24799 return true;
24801 if (init && type_dependent_expression_p (init))
24802 return true;
24804 if (type_dependent_expression_p (cond))
24805 return true;
24807 if (COMPARISON_CLASS_P (cond)
24808 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24809 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24810 return true;
24812 if (TREE_CODE (incr) == MODOP_EXPR)
24814 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24815 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24816 return true;
24818 else if (type_dependent_expression_p (incr))
24819 return true;
24820 else if (TREE_CODE (incr) == MODIFY_EXPR)
24822 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24823 return true;
24824 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24826 tree t = TREE_OPERAND (incr, 1);
24827 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24828 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24829 return true;
24834 return false;
24837 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24838 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24839 no such TYPE can be found. Note that this function peers inside
24840 uninstantiated templates and therefore should be used only in
24841 extremely limited situations. ONLY_CURRENT_P restricts this
24842 peering to the currently open classes hierarchy (which is required
24843 when comparing types). */
24845 tree
24846 resolve_typename_type (tree type, bool only_current_p)
24848 tree scope;
24849 tree name;
24850 tree decl;
24851 int quals;
24852 tree pushed_scope;
24853 tree result;
24855 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24857 scope = TYPE_CONTEXT (type);
24858 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24859 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24860 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24861 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24862 identifier of the TYPENAME_TYPE anymore.
24863 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24864 TYPENAME_TYPE instead, we avoid messing up with a possible
24865 typedef variant case. */
24866 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24868 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24869 it first before we can figure out what NAME refers to. */
24870 if (TREE_CODE (scope) == TYPENAME_TYPE)
24872 if (TYPENAME_IS_RESOLVING_P (scope))
24873 /* Given a class template A with a dependent base with nested type C,
24874 typedef typename A::C::C C will land us here, as trying to resolve
24875 the initial A::C leads to the local C typedef, which leads back to
24876 A::C::C. So we break the recursion now. */
24877 return type;
24878 else
24879 scope = resolve_typename_type (scope, only_current_p);
24881 /* If we don't know what SCOPE refers to, then we cannot resolve the
24882 TYPENAME_TYPE. */
24883 if (!CLASS_TYPE_P (scope))
24884 return type;
24885 /* If this is a typedef, we don't want to look inside (c++/11987). */
24886 if (typedef_variant_p (type))
24887 return type;
24888 /* If SCOPE isn't the template itself, it will not have a valid
24889 TYPE_FIELDS list. */
24890 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24891 /* scope is either the template itself or a compatible instantiation
24892 like X<T>, so look up the name in the original template. */
24893 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24894 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24895 gcc_checking_assert (uses_template_parms (scope));
24896 /* If scope has no fields, it can't be a current instantiation. Check this
24897 before currently_open_class to avoid infinite recursion (71515). */
24898 if (!TYPE_FIELDS (scope))
24899 return type;
24900 /* If the SCOPE is not the current instantiation, there's no reason
24901 to look inside it. */
24902 if (only_current_p && !currently_open_class (scope))
24903 return type;
24904 /* Enter the SCOPE so that name lookup will be resolved as if we
24905 were in the class definition. In particular, SCOPE will no
24906 longer be considered a dependent type. */
24907 pushed_scope = push_scope (scope);
24908 /* Look up the declaration. */
24909 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24910 tf_warning_or_error);
24912 result = NULL_TREE;
24914 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24915 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24916 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24917 if (!decl)
24918 /*nop*/;
24919 else if (identifier_p (fullname)
24920 && TREE_CODE (decl) == TYPE_DECL)
24922 result = TREE_TYPE (decl);
24923 if (result == error_mark_node)
24924 result = NULL_TREE;
24926 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24927 && DECL_CLASS_TEMPLATE_P (decl))
24929 /* Obtain the template and the arguments. */
24930 tree tmpl = TREE_OPERAND (fullname, 0);
24931 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24933 /* We get here with a plain identifier because a previous tentative
24934 parse of the nested-name-specifier as part of a ptr-operator saw
24935 ::template X<A>. The use of ::template is necessary in a
24936 ptr-operator, but wrong in a declarator-id.
24938 [temp.names]: In a qualified-id of a declarator-id, the keyword
24939 template shall not appear at the top level. */
24940 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24941 "keyword %<template%> not allowed in declarator-id");
24942 tmpl = decl;
24944 tree args = TREE_OPERAND (fullname, 1);
24945 /* Instantiate the template. */
24946 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24947 /*entering_scope=*/true,
24948 tf_error | tf_user);
24949 if (result == error_mark_node)
24950 result = NULL_TREE;
24953 /* Leave the SCOPE. */
24954 if (pushed_scope)
24955 pop_scope (pushed_scope);
24957 /* If we failed to resolve it, return the original typename. */
24958 if (!result)
24959 return type;
24961 /* If lookup found a typename type, resolve that too. */
24962 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24964 /* Ill-formed programs can cause infinite recursion here, so we
24965 must catch that. */
24966 TYPENAME_IS_RESOLVING_P (result) = 1;
24967 result = resolve_typename_type (result, only_current_p);
24968 TYPENAME_IS_RESOLVING_P (result) = 0;
24971 /* Qualify the resulting type. */
24972 quals = cp_type_quals (type);
24973 if (quals)
24974 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24976 return result;
24979 /* EXPR is an expression which is not type-dependent. Return a proxy
24980 for EXPR that can be used to compute the types of larger
24981 expressions containing EXPR. */
24983 tree
24984 build_non_dependent_expr (tree expr)
24986 tree inner_expr;
24988 /* When checking, try to get a constant value for all non-dependent
24989 expressions in order to expose bugs in *_dependent_expression_p
24990 and constexpr. This can affect code generation, see PR70704, so
24991 only do this for -fchecking=2. */
24992 if (flag_checking > 1
24993 && cxx_dialect >= cxx11
24994 /* Don't do this during nsdmi parsing as it can lead to
24995 unexpected recursive instantiations. */
24996 && !parsing_nsdmi ()
24997 /* Don't do this during concept expansion either and for
24998 the same reason. */
24999 && !expanding_concept ())
25000 fold_non_dependent_expr (expr);
25002 /* Preserve OVERLOADs; the functions must be available to resolve
25003 types. */
25004 inner_expr = expr;
25005 if (TREE_CODE (inner_expr) == STMT_EXPR)
25006 inner_expr = stmt_expr_value_expr (inner_expr);
25007 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25008 inner_expr = TREE_OPERAND (inner_expr, 0);
25009 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25010 inner_expr = TREE_OPERAND (inner_expr, 1);
25011 if (is_overloaded_fn (inner_expr)
25012 || TREE_CODE (inner_expr) == OFFSET_REF)
25013 return expr;
25014 /* There is no need to return a proxy for a variable. */
25015 if (VAR_P (expr))
25016 return expr;
25017 /* Preserve string constants; conversions from string constants to
25018 "char *" are allowed, even though normally a "const char *"
25019 cannot be used to initialize a "char *". */
25020 if (TREE_CODE (expr) == STRING_CST)
25021 return expr;
25022 /* Preserve void and arithmetic constants, as an optimization -- there is no
25023 reason to create a new node. */
25024 if (TREE_CODE (expr) == VOID_CST
25025 || TREE_CODE (expr) == INTEGER_CST
25026 || TREE_CODE (expr) == REAL_CST)
25027 return expr;
25028 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25029 There is at least one place where we want to know that a
25030 particular expression is a throw-expression: when checking a ?:
25031 expression, there are special rules if the second or third
25032 argument is a throw-expression. */
25033 if (TREE_CODE (expr) == THROW_EXPR)
25034 return expr;
25036 /* Don't wrap an initializer list, we need to be able to look inside. */
25037 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25038 return expr;
25040 /* Don't wrap a dummy object, we need to be able to test for it. */
25041 if (is_dummy_object (expr))
25042 return expr;
25044 if (TREE_CODE (expr) == COND_EXPR)
25045 return build3 (COND_EXPR,
25046 TREE_TYPE (expr),
25047 TREE_OPERAND (expr, 0),
25048 (TREE_OPERAND (expr, 1)
25049 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25050 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25051 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25052 if (TREE_CODE (expr) == COMPOUND_EXPR
25053 && !COMPOUND_EXPR_OVERLOADED (expr))
25054 return build2 (COMPOUND_EXPR,
25055 TREE_TYPE (expr),
25056 TREE_OPERAND (expr, 0),
25057 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25059 /* If the type is unknown, it can't really be non-dependent */
25060 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25062 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25063 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25066 /* ARGS is a vector of expressions as arguments to a function call.
25067 Replace the arguments with equivalent non-dependent expressions.
25068 This modifies ARGS in place. */
25070 void
25071 make_args_non_dependent (vec<tree, va_gc> *args)
25073 unsigned int ix;
25074 tree arg;
25076 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25078 tree newarg = build_non_dependent_expr (arg);
25079 if (newarg != arg)
25080 (*args)[ix] = newarg;
25084 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25085 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25086 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25088 static tree
25089 make_auto_1 (tree name, bool set_canonical)
25091 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25092 TYPE_NAME (au) = build_decl (input_location,
25093 TYPE_DECL, name, au);
25094 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25095 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25096 (0, processing_template_decl + 1, processing_template_decl + 1,
25097 TYPE_NAME (au), NULL_TREE);
25098 if (set_canonical)
25099 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25100 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25101 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25103 return au;
25106 tree
25107 make_decltype_auto (void)
25109 return make_auto_1 (decltype_auto_identifier, true);
25112 tree
25113 make_auto (void)
25115 return make_auto_1 (auto_identifier, true);
25118 /* Return a C++17 deduction placeholder for class template TMPL. */
25120 tree
25121 make_template_placeholder (tree tmpl)
25123 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25124 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25125 return t;
25128 /* True iff T is a C++17 class template deduction placeholder. */
25130 bool
25131 template_placeholder_p (tree t)
25133 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25136 /* Make a "constrained auto" type-specifier. This is an
25137 auto type with constraints that must be associated after
25138 deduction. The constraint is formed from the given
25139 CONC and its optional sequence of arguments, which are
25140 non-null if written as partial-concept-id. */
25142 tree
25143 make_constrained_auto (tree con, tree args)
25145 tree type = make_auto_1 (auto_identifier, false);
25147 /* Build the constraint. */
25148 tree tmpl = DECL_TI_TEMPLATE (con);
25149 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25150 expr = build_concept_check (expr, type, args);
25152 tree constr = normalize_expression (expr);
25153 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25155 /* Our canonical type depends on the constraint. */
25156 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25158 /* Attach the constraint to the type declaration. */
25159 tree decl = TYPE_NAME (type);
25160 return decl;
25163 /* Given type ARG, return std::initializer_list<ARG>. */
25165 static tree
25166 listify (tree arg)
25168 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25170 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25172 gcc_rich_location richloc (input_location);
25173 maybe_add_include_fixit (&richloc, "<initializer_list>");
25174 error_at (&richloc,
25175 "deducing from brace-enclosed initializer list"
25176 " requires %<#include <initializer_list>%>");
25178 return error_mark_node;
25180 tree argvec = make_tree_vec (1);
25181 TREE_VEC_ELT (argvec, 0) = arg;
25183 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25184 NULL_TREE, 0, tf_warning_or_error);
25187 /* Replace auto in TYPE with std::initializer_list<auto>. */
25189 static tree
25190 listify_autos (tree type, tree auto_node)
25192 tree init_auto = listify (auto_node);
25193 tree argvec = make_tree_vec (1);
25194 TREE_VEC_ELT (argvec, 0) = init_auto;
25195 if (processing_template_decl)
25196 argvec = add_to_template_args (current_template_args (), argvec);
25197 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25200 /* Hash traits for hashing possibly constrained 'auto'
25201 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25203 struct auto_hash : default_hash_traits<tree>
25205 static inline hashval_t hash (tree);
25206 static inline bool equal (tree, tree);
25209 /* Hash the 'auto' T. */
25211 inline hashval_t
25212 auto_hash::hash (tree t)
25214 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25215 /* Matching constrained-type-specifiers denote the same template
25216 parameter, so hash the constraint. */
25217 return hash_placeholder_constraint (c);
25218 else
25219 /* But unconstrained autos are all separate, so just hash the pointer. */
25220 return iterative_hash_object (t, 0);
25223 /* Compare two 'auto's. */
25225 inline bool
25226 auto_hash::equal (tree t1, tree t2)
25228 if (t1 == t2)
25229 return true;
25231 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25232 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25234 /* Two unconstrained autos are distinct. */
25235 if (!c1 || !c2)
25236 return false;
25238 return equivalent_placeholder_constraints (c1, c2);
25241 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25242 constrained) auto, add it to the vector. */
25244 static int
25245 extract_autos_r (tree t, void *data)
25247 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25248 if (is_auto (t))
25250 /* All the autos were built with index 0; fix that up now. */
25251 tree *p = hash.find_slot (t, INSERT);
25252 unsigned idx;
25253 if (*p)
25254 /* If this is a repeated constrained-type-specifier, use the index we
25255 chose before. */
25256 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25257 else
25259 /* Otherwise this is new, so use the current count. */
25260 *p = t;
25261 idx = hash.elements () - 1;
25263 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25266 /* Always keep walking. */
25267 return 0;
25270 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25271 says they can appear anywhere in the type. */
25273 static tree
25274 extract_autos (tree type)
25276 hash_set<tree> visited;
25277 hash_table<auto_hash> hash (2);
25279 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25281 tree tree_vec = make_tree_vec (hash.elements());
25282 for (hash_table<auto_hash>::iterator iter = hash.begin();
25283 iter != hash.end(); ++iter)
25285 tree elt = *iter;
25286 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25287 TREE_VEC_ELT (tree_vec, i)
25288 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25291 return tree_vec;
25294 /* The stem for deduction guide names. */
25295 const char *const dguide_base = "__dguide_";
25297 /* Return the name for a deduction guide for class template TMPL. */
25299 tree
25300 dguide_name (tree tmpl)
25302 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25303 tree tname = TYPE_IDENTIFIER (type);
25304 char *buf = (char *) alloca (1 + strlen (dguide_base)
25305 + IDENTIFIER_LENGTH (tname));
25306 memcpy (buf, dguide_base, strlen (dguide_base));
25307 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25308 IDENTIFIER_LENGTH (tname) + 1);
25309 tree dname = get_identifier (buf);
25310 TREE_TYPE (dname) = type;
25311 return dname;
25314 /* True if NAME is the name of a deduction guide. */
25316 bool
25317 dguide_name_p (tree name)
25319 return (TREE_TYPE (name)
25320 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25321 strlen (dguide_base)));
25324 /* True if FN is a deduction guide. */
25326 bool
25327 deduction_guide_p (const_tree fn)
25329 if (DECL_P (fn))
25330 if (tree name = DECL_NAME (fn))
25331 return dguide_name_p (name);
25332 return false;
25335 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25337 bool
25338 copy_guide_p (const_tree fn)
25340 gcc_assert (deduction_guide_p (fn));
25341 if (!DECL_ARTIFICIAL (fn))
25342 return false;
25343 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25344 return (TREE_CHAIN (parms) == void_list_node
25345 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25348 /* True if FN is a guide generated from a constructor template. */
25350 bool
25351 template_guide_p (const_tree fn)
25353 gcc_assert (deduction_guide_p (fn));
25354 if (!DECL_ARTIFICIAL (fn))
25355 return false;
25356 tree tmpl = DECL_TI_TEMPLATE (fn);
25357 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25358 return PRIMARY_TEMPLATE_P (org);
25359 return false;
25362 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25363 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25364 template parameter types. Note that the handling of template template
25365 parameters relies on current_template_parms being set appropriately for the
25366 new template. */
25368 static tree
25369 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25370 tree tsubst_args, tsubst_flags_t complain)
25372 tree oldidx = get_template_parm_index (olddecl);
25374 tree newtype;
25375 if (TREE_CODE (olddecl) == TYPE_DECL
25376 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25378 tree oldtype = TREE_TYPE (olddecl);
25379 newtype = cxx_make_type (TREE_CODE (oldtype));
25380 TYPE_MAIN_VARIANT (newtype) = newtype;
25381 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25382 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25383 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25385 else
25386 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25387 complain, NULL_TREE);
25389 tree newdecl
25390 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25391 DECL_NAME (olddecl), newtype);
25392 SET_DECL_TEMPLATE_PARM_P (newdecl);
25394 tree newidx;
25395 if (TREE_CODE (olddecl) == TYPE_DECL
25396 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25398 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25399 = build_template_parm_index (index, level, level,
25400 newdecl, newtype);
25401 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25402 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25403 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25404 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25406 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25408 DECL_TEMPLATE_RESULT (newdecl)
25409 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25410 DECL_NAME (olddecl), newtype);
25411 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25412 // First create a copy (ttargs) of tsubst_args with an
25413 // additional level for the template template parameter's own
25414 // template parameters (ttparms).
25415 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25416 (DECL_TEMPLATE_PARMS (olddecl)));
25417 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25418 tree ttargs = make_tree_vec (depth + 1);
25419 for (int i = 0; i < depth; ++i)
25420 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25421 TREE_VEC_ELT (ttargs, depth)
25422 = template_parms_level_to_args (ttparms);
25423 // Substitute ttargs into ttparms to fix references to
25424 // other template parameters.
25425 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25426 complain);
25427 // Now substitute again with args based on tparms, to reduce
25428 // the level of the ttparms.
25429 ttargs = current_template_args ();
25430 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25431 complain);
25432 // Finally, tack the adjusted parms onto tparms.
25433 ttparms = tree_cons (size_int (depth), ttparms,
25434 current_template_parms);
25435 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25438 else
25440 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25441 tree newconst
25442 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25443 TREE_CODE (oldconst),
25444 DECL_NAME (oldconst), newtype);
25445 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25446 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25447 SET_DECL_TEMPLATE_PARM_P (newconst);
25448 newidx = build_template_parm_index (index, level, level,
25449 newconst, newtype);
25450 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25451 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25452 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25455 return newdecl;
25458 /* Returns a C++17 class deduction guide template based on the constructor
25459 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25460 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25462 static tree
25463 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25465 tree type, tparms, targs, fparms, fargs, ci;
25466 bool memtmpl = false;
25467 bool explicit_p;
25468 location_t loc;
25469 tree fn_tmpl = NULL_TREE;
25471 if (TYPE_P (ctor))
25473 type = ctor;
25474 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25475 if (copy_p)
25477 type = TREE_TYPE (type);
25478 fparms = tree_cons (NULL_TREE, type, void_list_node);
25480 else
25481 fparms = void_list_node;
25483 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25484 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25485 targs = CLASSTYPE_TI_ARGS (type);
25486 ci = NULL_TREE;
25487 fargs = NULL_TREE;
25488 loc = DECL_SOURCE_LOCATION (ctmpl);
25489 explicit_p = false;
25491 else
25493 ++processing_template_decl;
25495 fn_tmpl
25496 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25497 : DECL_TI_TEMPLATE (ctor));
25498 if (outer_args)
25499 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25500 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25502 type = DECL_CONTEXT (ctor);
25504 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25505 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25506 fully specialized args for the enclosing class. Strip those off, as
25507 the deduction guide won't have those template parameters. */
25508 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25509 TMPL_PARMS_DEPTH (tparms));
25510 /* Discard the 'this' parameter. */
25511 fparms = FUNCTION_ARG_CHAIN (ctor);
25512 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25513 ci = get_constraints (ctor);
25514 loc = DECL_SOURCE_LOCATION (ctor);
25515 explicit_p = DECL_NONCONVERTING_P (ctor);
25517 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25519 memtmpl = true;
25521 /* For a member template constructor, we need to flatten the two
25522 template parameter lists into one, and then adjust the function
25523 signature accordingly. This gets...complicated. */
25524 tree save_parms = current_template_parms;
25526 /* For a member template we should have two levels of parms/args, one
25527 for the class and one for the constructor. We stripped
25528 specialized args for further enclosing classes above. */
25529 const int depth = 2;
25530 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25532 /* Template args for translating references to the two-level template
25533 parameters into references to the one-level template parameters we
25534 are creating. */
25535 tree tsubst_args = copy_node (targs);
25536 TMPL_ARGS_LEVEL (tsubst_args, depth)
25537 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25539 /* Template parms for the constructor template. */
25540 tree ftparms = TREE_VALUE (tparms);
25541 unsigned flen = TREE_VEC_LENGTH (ftparms);
25542 /* Template parms for the class template. */
25543 tparms = TREE_CHAIN (tparms);
25544 tree ctparms = TREE_VALUE (tparms);
25545 unsigned clen = TREE_VEC_LENGTH (ctparms);
25546 /* Template parms for the deduction guide start as a copy of the
25547 template parms for the class. We set current_template_parms for
25548 lookup_template_class_1. */
25549 current_template_parms = tparms = copy_node (tparms);
25550 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25551 for (unsigned i = 0; i < clen; ++i)
25552 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25554 /* Now we need to rewrite the constructor parms to append them to the
25555 class parms. */
25556 for (unsigned i = 0; i < flen; ++i)
25558 unsigned index = i + clen;
25559 unsigned level = 1;
25560 tree oldelt = TREE_VEC_ELT (ftparms, i);
25561 tree olddecl = TREE_VALUE (oldelt);
25562 tree newdecl = rewrite_template_parm (olddecl, index, level,
25563 tsubst_args, complain);
25564 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25565 tsubst_args, complain, ctor);
25566 tree list = build_tree_list (newdef, newdecl);
25567 TEMPLATE_PARM_CONSTRAINTS (list)
25568 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25569 tsubst_args, complain, ctor);
25570 TREE_VEC_ELT (new_vec, index) = list;
25571 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25574 /* Now we have a final set of template parms to substitute into the
25575 function signature. */
25576 targs = template_parms_to_args (tparms);
25577 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25578 complain, ctor);
25579 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25580 if (ci)
25581 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25583 current_template_parms = save_parms;
25585 --processing_template_decl;
25588 if (!memtmpl)
25590 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25591 tparms = copy_node (tparms);
25592 INNERMOST_TEMPLATE_PARMS (tparms)
25593 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25596 tree fntype = build_function_type (type, fparms);
25597 tree ded_fn = build_lang_decl_loc (loc,
25598 FUNCTION_DECL,
25599 dguide_name (type), fntype);
25600 DECL_ARGUMENTS (ded_fn) = fargs;
25601 DECL_ARTIFICIAL (ded_fn) = true;
25602 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25603 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25604 DECL_ARTIFICIAL (ded_tmpl) = true;
25605 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25606 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25607 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25608 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25609 if (DECL_P (ctor))
25610 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25611 if (ci)
25612 set_constraints (ded_tmpl, ci);
25614 return ded_tmpl;
25617 /* Deduce template arguments for the class template placeholder PTYPE for
25618 template TMPL based on the initializer INIT, and return the resulting
25619 type. */
25621 static tree
25622 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25623 tsubst_flags_t complain)
25625 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25627 /* We should have handled this in the caller. */
25628 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25629 return ptype;
25630 if (complain & tf_error)
25631 error ("non-class template %qT used without template arguments", tmpl);
25632 return error_mark_node;
25635 tree type = TREE_TYPE (tmpl);
25637 bool try_list_ctor = false;
25639 vec<tree,va_gc> *args;
25640 if (init == NULL_TREE
25641 || TREE_CODE (init) == TREE_LIST)
25642 args = make_tree_vector_from_list (init);
25643 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25645 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25646 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25648 /* As an exception, the first phase in 16.3.1.7 (considering the
25649 initializer list as a single argument) is omitted if the
25650 initializer list consists of a single expression of type cv U,
25651 where U is a specialization of C or a class derived from a
25652 specialization of C. */
25653 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25654 tree etype = TREE_TYPE (elt);
25656 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25657 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25658 int err = unify (tparms, targs, type, etype,
25659 UNIFY_ALLOW_DERIVED, /*explain*/false);
25660 if (err == 0)
25661 try_list_ctor = false;
25662 ggc_free (targs);
25664 if (try_list_ctor || is_std_init_list (type))
25665 args = make_tree_vector_single (init);
25666 else
25667 args = make_tree_vector_from_ctor (init);
25669 else
25670 args = make_tree_vector_single (init);
25672 tree dname = dguide_name (tmpl);
25673 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25674 /*type*/false, /*complain*/false,
25675 /*hidden*/false);
25676 bool elided = false;
25677 if (cands == error_mark_node)
25678 cands = NULL_TREE;
25680 /* Prune explicit deduction guides in copy-initialization context. */
25681 if (flags & LOOKUP_ONLYCONVERTING)
25683 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25684 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25685 elided = true;
25687 if (elided)
25689 /* Found a nonconverting guide, prune the candidates. */
25690 tree pruned = NULL_TREE;
25691 for (lkp_iterator iter (cands); iter; ++iter)
25692 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25693 pruned = lookup_add (*iter, pruned);
25695 cands = pruned;
25699 tree outer_args = NULL_TREE;
25700 if (DECL_CLASS_SCOPE_P (tmpl)
25701 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25703 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25704 type = TREE_TYPE (most_general_template (tmpl));
25707 bool saw_ctor = false;
25708 // FIXME cache artificial deduction guides
25709 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25711 tree guide = build_deduction_guide (*iter, outer_args, complain);
25712 if ((flags & LOOKUP_ONLYCONVERTING)
25713 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25714 elided = true;
25715 else
25716 cands = lookup_add (guide, cands);
25718 saw_ctor = true;
25721 tree call = error_mark_node;
25723 /* If this is list-initialization and the class has a list constructor, first
25724 try deducing from the list as a single argument, as [over.match.list]. */
25725 tree list_cands = NULL_TREE;
25726 if (try_list_ctor && cands)
25727 for (lkp_iterator iter (cands); iter; ++iter)
25729 tree dg = *iter;
25730 if (is_list_ctor (dg))
25731 list_cands = lookup_add (dg, list_cands);
25733 if (list_cands)
25735 ++cp_unevaluated_operand;
25736 call = build_new_function_call (list_cands, &args, tf_decltype);
25737 --cp_unevaluated_operand;
25739 if (call == error_mark_node)
25741 /* That didn't work, now try treating the list as a sequence of
25742 arguments. */
25743 release_tree_vector (args);
25744 args = make_tree_vector_from_ctor (init);
25748 /* Maybe generate an implicit deduction guide. */
25749 if (call == error_mark_node && args->length () < 2)
25751 tree gtype = NULL_TREE;
25753 if (args->length () == 1)
25754 /* Generate a copy guide. */
25755 gtype = build_reference_type (type);
25756 else if (!saw_ctor)
25757 /* Generate a default guide. */
25758 gtype = type;
25760 if (gtype)
25762 tree guide = build_deduction_guide (gtype, outer_args, complain);
25763 cands = lookup_add (guide, cands);
25767 if (elided && !cands)
25769 error ("cannot deduce template arguments for copy-initialization"
25770 " of %qT, as it has no non-explicit deduction guides or "
25771 "user-declared constructors", type);
25772 return error_mark_node;
25774 else if (!cands && call == error_mark_node)
25776 error ("cannot deduce template arguments of %qT, as it has no viable "
25777 "deduction guides", type);
25778 return error_mark_node;
25781 if (call == error_mark_node)
25783 ++cp_unevaluated_operand;
25784 call = build_new_function_call (cands, &args, tf_decltype);
25785 --cp_unevaluated_operand;
25788 if (call == error_mark_node && (complain & tf_warning_or_error))
25790 error ("class template argument deduction failed:");
25792 ++cp_unevaluated_operand;
25793 call = build_new_function_call (cands, &args, complain | tf_decltype);
25794 --cp_unevaluated_operand;
25796 if (elided)
25797 inform (input_location, "explicit deduction guides not considered "
25798 "for copy-initialization");
25801 release_tree_vector (args);
25803 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25806 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25807 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25809 tree
25810 do_auto_deduction (tree type, tree init, tree auto_node)
25812 return do_auto_deduction (type, init, auto_node,
25813 tf_warning_or_error,
25814 adc_unspecified);
25817 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25818 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25819 The CONTEXT determines the context in which auto deduction is performed
25820 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25821 OUTER_TARGS are used during template argument deduction
25822 (context == adc_unify) to properly substitute the result, and is ignored
25823 in other contexts.
25825 For partial-concept-ids, extra args may be appended to the list of deduced
25826 template arguments prior to determining constraint satisfaction. */
25828 tree
25829 do_auto_deduction (tree type, tree init, tree auto_node,
25830 tsubst_flags_t complain, auto_deduction_context context,
25831 tree outer_targs, int flags)
25833 tree targs;
25835 if (init == error_mark_node)
25836 return error_mark_node;
25838 if (init && type_dependent_expression_p (init)
25839 && context != adc_unify)
25840 /* Defining a subset of type-dependent expressions that we can deduce
25841 from ahead of time isn't worth the trouble. */
25842 return type;
25844 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25845 /* C++17 class template argument deduction. */
25846 return do_class_deduction (type, tmpl, init, flags, complain);
25848 if (TREE_TYPE (init) == NULL_TREE)
25849 /* Nothing we can do with this, even in deduction context. */
25850 return type;
25852 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25853 with either a new invented type template parameter U or, if the
25854 initializer is a braced-init-list (8.5.4), with
25855 std::initializer_list<U>. */
25856 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25858 if (!DIRECT_LIST_INIT_P (init))
25859 type = listify_autos (type, auto_node);
25860 else if (CONSTRUCTOR_NELTS (init) == 1)
25861 init = CONSTRUCTOR_ELT (init, 0)->value;
25862 else
25864 if (complain & tf_warning_or_error)
25866 if (permerror (input_location, "direct-list-initialization of "
25867 "%<auto%> requires exactly one element"))
25868 inform (input_location,
25869 "for deduction to %<std::initializer_list%>, use copy-"
25870 "list-initialization (i.e. add %<=%> before the %<{%>)");
25872 type = listify_autos (type, auto_node);
25876 if (type == error_mark_node)
25877 return error_mark_node;
25879 init = resolve_nondeduced_context (init, complain);
25881 if (context == adc_decomp_type
25882 && auto_node == type
25883 && init != error_mark_node
25884 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25885 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25886 and initializer has array type, deduce cv-qualified array type. */
25887 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25888 complain);
25889 else if (AUTO_IS_DECLTYPE (auto_node))
25891 bool id = (DECL_P (init)
25892 || ((TREE_CODE (init) == COMPONENT_REF
25893 || TREE_CODE (init) == SCOPE_REF)
25894 && !REF_PARENTHESIZED_P (init)));
25895 targs = make_tree_vec (1);
25896 TREE_VEC_ELT (targs, 0)
25897 = finish_decltype_type (init, id, tf_warning_or_error);
25898 if (type != auto_node)
25900 if (complain & tf_error)
25901 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25902 return error_mark_node;
25905 else
25907 tree parms = build_tree_list (NULL_TREE, type);
25908 tree tparms;
25910 if (flag_concepts)
25911 tparms = extract_autos (type);
25912 else
25914 tparms = make_tree_vec (1);
25915 TREE_VEC_ELT (tparms, 0)
25916 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25919 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25920 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25921 DEDUCE_CALL, LOOKUP_NORMAL,
25922 NULL, /*explain_p=*/false);
25923 if (val > 0)
25925 if (processing_template_decl)
25926 /* Try again at instantiation time. */
25927 return type;
25928 if (type && type != error_mark_node
25929 && (complain & tf_error))
25930 /* If type is error_mark_node a diagnostic must have been
25931 emitted by now. Also, having a mention to '<type error>'
25932 in the diagnostic is not really useful to the user. */
25934 if (cfun && auto_node == current_function_auto_return_pattern
25935 && LAMBDA_FUNCTION_P (current_function_decl))
25936 error ("unable to deduce lambda return type from %qE", init);
25937 else
25938 error ("unable to deduce %qT from %qE", type, init);
25939 type_unification_real (tparms, targs, parms, &init, 1, 0,
25940 DEDUCE_CALL, LOOKUP_NORMAL,
25941 NULL, /*explain_p=*/true);
25943 return error_mark_node;
25947 /* Check any placeholder constraints against the deduced type. */
25948 if (flag_concepts && !processing_template_decl)
25949 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25951 /* Use the deduced type to check the associated constraints. If we
25952 have a partial-concept-id, rebuild the argument list so that
25953 we check using the extra arguments. */
25954 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25955 tree cargs = CHECK_CONSTR_ARGS (constr);
25956 if (TREE_VEC_LENGTH (cargs) > 1)
25958 cargs = copy_node (cargs);
25959 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25961 else
25962 cargs = targs;
25963 if (!constraints_satisfied_p (constr, cargs))
25965 if (complain & tf_warning_or_error)
25967 switch (context)
25969 case adc_unspecified:
25970 case adc_unify:
25971 error("placeholder constraints not satisfied");
25972 break;
25973 case adc_variable_type:
25974 case adc_decomp_type:
25975 error ("deduced initializer does not satisfy "
25976 "placeholder constraints");
25977 break;
25978 case adc_return_type:
25979 error ("deduced return type does not satisfy "
25980 "placeholder constraints");
25981 break;
25982 case adc_requirement:
25983 error ("deduced expression type does not satisfy "
25984 "placeholder constraints");
25985 break;
25987 diagnose_constraints (input_location, constr, targs);
25989 return error_mark_node;
25993 if (processing_template_decl && context != adc_unify)
25994 outer_targs = current_template_args ();
25995 targs = add_to_template_args (outer_targs, targs);
25996 return tsubst (type, targs, complain, NULL_TREE);
25999 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26000 result. */
26002 tree
26003 splice_late_return_type (tree type, tree late_return_type)
26005 if (is_auto (type))
26007 if (late_return_type)
26008 return late_return_type;
26010 tree idx = get_template_parm_index (type);
26011 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26012 /* In an abbreviated function template we didn't know we were dealing
26013 with a function template when we saw the auto return type, so update
26014 it to have the correct level. */
26015 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26017 return type;
26020 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26021 'decltype(auto)' or a deduced class template. */
26023 bool
26024 is_auto (const_tree type)
26026 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26027 && (TYPE_IDENTIFIER (type) == auto_identifier
26028 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26029 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26030 return true;
26031 else
26032 return false;
26035 /* for_each_template_parm callback for type_uses_auto. */
26038 is_auto_r (tree tp, void */*data*/)
26040 return is_auto (tp);
26043 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26044 a use of `auto'. Returns NULL_TREE otherwise. */
26046 tree
26047 type_uses_auto (tree type)
26049 if (type == NULL_TREE)
26050 return NULL_TREE;
26051 else if (flag_concepts)
26053 /* The Concepts TS allows multiple autos in one type-specifier; just
26054 return the first one we find, do_auto_deduction will collect all of
26055 them. */
26056 if (uses_template_parms (type))
26057 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26058 /*visited*/NULL, /*nondeduced*/true);
26059 else
26060 return NULL_TREE;
26062 else
26063 return find_type_usage (type, is_auto);
26066 /* For a given template T, return the vector of typedefs referenced
26067 in T for which access check is needed at T instantiation time.
26068 T is either a FUNCTION_DECL or a RECORD_TYPE.
26069 Those typedefs were added to T by the function
26070 append_type_to_template_for_access_check. */
26072 vec<qualified_typedef_usage_t, va_gc> *
26073 get_types_needing_access_check (tree t)
26075 tree ti;
26076 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26078 if (!t || t == error_mark_node)
26079 return NULL;
26081 if (!(ti = get_template_info (t)))
26082 return NULL;
26084 if (CLASS_TYPE_P (t)
26085 || TREE_CODE (t) == FUNCTION_DECL)
26087 if (!TI_TEMPLATE (ti))
26088 return NULL;
26090 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26093 return result;
26096 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26097 tied to T. That list of typedefs will be access checked at
26098 T instantiation time.
26099 T is either a FUNCTION_DECL or a RECORD_TYPE.
26100 TYPE_DECL is a TYPE_DECL node representing a typedef.
26101 SCOPE is the scope through which TYPE_DECL is accessed.
26102 LOCATION is the location of the usage point of TYPE_DECL.
26104 This function is a subroutine of
26105 append_type_to_template_for_access_check. */
26107 static void
26108 append_type_to_template_for_access_check_1 (tree t,
26109 tree type_decl,
26110 tree scope,
26111 location_t location)
26113 qualified_typedef_usage_t typedef_usage;
26114 tree ti;
26116 if (!t || t == error_mark_node)
26117 return;
26119 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26120 || CLASS_TYPE_P (t))
26121 && type_decl
26122 && TREE_CODE (type_decl) == TYPE_DECL
26123 && scope);
26125 if (!(ti = get_template_info (t)))
26126 return;
26128 gcc_assert (TI_TEMPLATE (ti));
26130 typedef_usage.typedef_decl = type_decl;
26131 typedef_usage.context = scope;
26132 typedef_usage.locus = location;
26134 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26137 /* Append TYPE_DECL to the template TEMPL.
26138 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26139 At TEMPL instanciation time, TYPE_DECL will be checked to see
26140 if it can be accessed through SCOPE.
26141 LOCATION is the location of the usage point of TYPE_DECL.
26143 e.g. consider the following code snippet:
26145 class C
26147 typedef int myint;
26150 template<class U> struct S
26152 C::myint mi; // <-- usage point of the typedef C::myint
26155 S<char> s;
26157 At S<char> instantiation time, we need to check the access of C::myint
26158 In other words, we need to check the access of the myint typedef through
26159 the C scope. For that purpose, this function will add the myint typedef
26160 and the scope C through which its being accessed to a list of typedefs
26161 tied to the template S. That list will be walked at template instantiation
26162 time and access check performed on each typedefs it contains.
26163 Note that this particular code snippet should yield an error because
26164 myint is private to C. */
26166 void
26167 append_type_to_template_for_access_check (tree templ,
26168 tree type_decl,
26169 tree scope,
26170 location_t location)
26172 qualified_typedef_usage_t *iter;
26173 unsigned i;
26175 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26177 /* Make sure we don't append the type to the template twice. */
26178 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26179 if (iter->typedef_decl == type_decl && scope == iter->context)
26180 return;
26182 append_type_to_template_for_access_check_1 (templ, type_decl,
26183 scope, location);
26186 /* Convert the generic type parameters in PARM that match the types given in the
26187 range [START_IDX, END_IDX) from the current_template_parms into generic type
26188 packs. */
26190 tree
26191 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26193 tree current = current_template_parms;
26194 int depth = TMPL_PARMS_DEPTH (current);
26195 current = INNERMOST_TEMPLATE_PARMS (current);
26196 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26198 for (int i = 0; i < start_idx; ++i)
26199 TREE_VEC_ELT (replacement, i)
26200 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26202 for (int i = start_idx; i < end_idx; ++i)
26204 /* Create a distinct parameter pack type from the current parm and add it
26205 to the replacement args to tsubst below into the generic function
26206 parameter. */
26208 tree o = TREE_TYPE (TREE_VALUE
26209 (TREE_VEC_ELT (current, i)));
26210 tree t = copy_type (o);
26211 TEMPLATE_TYPE_PARM_INDEX (t)
26212 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26213 o, 0, 0, tf_none);
26214 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26215 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26216 TYPE_MAIN_VARIANT (t) = t;
26217 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26218 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26219 TREE_VEC_ELT (replacement, i) = t;
26220 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26223 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26224 TREE_VEC_ELT (replacement, i)
26225 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26227 /* If there are more levels then build up the replacement with the outer
26228 template parms. */
26229 if (depth > 1)
26230 replacement = add_to_template_args (template_parms_to_args
26231 (TREE_CHAIN (current_template_parms)),
26232 replacement);
26234 return tsubst (parm, replacement, tf_none, NULL_TREE);
26237 /* Entries in the decl_constraint hash table. */
26238 struct GTY((for_user)) constr_entry
26240 tree decl;
26241 tree ci;
26244 /* Hashing function and equality for constraint entries. */
26245 struct constr_hasher : ggc_ptr_hash<constr_entry>
26247 static hashval_t hash (constr_entry *e)
26249 return (hashval_t)DECL_UID (e->decl);
26252 static bool equal (constr_entry *e1, constr_entry *e2)
26254 return e1->decl == e2->decl;
26258 /* A mapping from declarations to constraint information. Note that
26259 both templates and their underlying declarations are mapped to the
26260 same constraint information.
26262 FIXME: This is defined in pt.c because garbage collection
26263 code is not being generated for constraint.cc. */
26265 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26267 /* Returns the template constraints of declaration T. If T is not
26268 constrained, return NULL_TREE. Note that T must be non-null. */
26270 tree
26271 get_constraints (tree t)
26273 if (!flag_concepts)
26274 return NULL_TREE;
26276 gcc_assert (DECL_P (t));
26277 if (TREE_CODE (t) == TEMPLATE_DECL)
26278 t = DECL_TEMPLATE_RESULT (t);
26279 constr_entry elt = { t, NULL_TREE };
26280 constr_entry* found = decl_constraints->find (&elt);
26281 if (found)
26282 return found->ci;
26283 else
26284 return NULL_TREE;
26287 /* Associate the given constraint information CI with the declaration
26288 T. If T is a template, then the constraints are associated with
26289 its underlying declaration. Don't build associations if CI is
26290 NULL_TREE. */
26292 void
26293 set_constraints (tree t, tree ci)
26295 if (!ci)
26296 return;
26297 gcc_assert (t && flag_concepts);
26298 if (TREE_CODE (t) == TEMPLATE_DECL)
26299 t = DECL_TEMPLATE_RESULT (t);
26300 gcc_assert (!get_constraints (t));
26301 constr_entry elt = {t, ci};
26302 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26303 constr_entry* entry = ggc_alloc<constr_entry> ();
26304 *entry = elt;
26305 *slot = entry;
26308 /* Remove the associated constraints of the declaration T. */
26310 void
26311 remove_constraints (tree t)
26313 gcc_assert (DECL_P (t));
26314 if (TREE_CODE (t) == TEMPLATE_DECL)
26315 t = DECL_TEMPLATE_RESULT (t);
26317 constr_entry elt = {t, NULL_TREE};
26318 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26319 if (slot)
26320 decl_constraints->clear_slot (slot);
26323 /* Memoized satisfaction results for declarations. This
26324 maps the pair (constraint_info, arguments) to the result computed
26325 by constraints_satisfied_p. */
26327 struct GTY((for_user)) constraint_sat_entry
26329 tree ci;
26330 tree args;
26331 tree result;
26334 /* Hashing function and equality for constraint entries. */
26336 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26338 static hashval_t hash (constraint_sat_entry *e)
26340 hashval_t val = iterative_hash_object(e->ci, 0);
26341 return iterative_hash_template_arg (e->args, val);
26344 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26346 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26350 /* Memoized satisfaction results for concept checks. */
26352 struct GTY((for_user)) concept_spec_entry
26354 tree tmpl;
26355 tree args;
26356 tree result;
26359 /* Hashing function and equality for constraint entries. */
26361 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26363 static hashval_t hash (concept_spec_entry *e)
26365 return hash_tmpl_and_args (e->tmpl, e->args);
26368 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26370 ++comparing_specializations;
26371 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26372 --comparing_specializations;
26373 return eq;
26377 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26378 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26380 /* Search for a memoized satisfaction result. Returns one of the
26381 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26383 tree
26384 lookup_constraint_satisfaction (tree ci, tree args)
26386 constraint_sat_entry elt = { ci, args, NULL_TREE };
26387 constraint_sat_entry* found = constraint_memos->find (&elt);
26388 if (found)
26389 return found->result;
26390 else
26391 return NULL_TREE;
26394 /* Memoize the result of a satisfication test. Returns the saved result. */
26396 tree
26397 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26399 constraint_sat_entry elt = {ci, args, result};
26400 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26401 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26402 *entry = elt;
26403 *slot = entry;
26404 return result;
26407 /* Search for a memoized satisfaction result for a concept. */
26409 tree
26410 lookup_concept_satisfaction (tree tmpl, tree args)
26412 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26413 concept_spec_entry* found = concept_memos->find (&elt);
26414 if (found)
26415 return found->result;
26416 else
26417 return NULL_TREE;
26420 /* Memoize the result of a concept check. Returns the saved result. */
26422 tree
26423 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26425 concept_spec_entry elt = {tmpl, args, result};
26426 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26427 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26428 *entry = elt;
26429 *slot = entry;
26430 return result;
26433 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26435 /* Returns a prior concept specialization. This returns the substituted
26436 and normalized constraints defined by the concept. */
26438 tree
26439 get_concept_expansion (tree tmpl, tree args)
26441 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26442 concept_spec_entry* found = concept_expansions->find (&elt);
26443 if (found)
26444 return found->result;
26445 else
26446 return NULL_TREE;
26449 /* Save a concept expansion for later. */
26451 tree
26452 save_concept_expansion (tree tmpl, tree args, tree def)
26454 concept_spec_entry elt = {tmpl, args, def};
26455 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26456 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26457 *entry = elt;
26458 *slot = entry;
26459 return def;
26462 static hashval_t
26463 hash_subsumption_args (tree t1, tree t2)
26465 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26466 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26467 int val = 0;
26468 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26469 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26470 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26471 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26472 return val;
26475 /* Compare the constraints of two subsumption entries. The LEFT1 and
26476 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26477 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26479 static bool
26480 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26482 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26483 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26484 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26485 CHECK_CONSTR_ARGS (right1)))
26486 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26487 CHECK_CONSTR_ARGS (right2));
26488 return false;
26491 /* Key/value pair for learning and memoizing subsumption results. This
26492 associates a pair of check constraints (including arguments) with
26493 a boolean value indicating the result. */
26495 struct GTY((for_user)) subsumption_entry
26497 tree t1;
26498 tree t2;
26499 bool result;
26502 /* Hashing function and equality for constraint entries. */
26504 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26506 static hashval_t hash (subsumption_entry *e)
26508 return hash_subsumption_args (e->t1, e->t2);
26511 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26513 ++comparing_specializations;
26514 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26515 --comparing_specializations;
26516 return eq;
26520 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26522 /* Search for a previously cached subsumption result. */
26524 bool*
26525 lookup_subsumption_result (tree t1, tree t2)
26527 subsumption_entry elt = { t1, t2, false };
26528 subsumption_entry* found = subsumption_table->find (&elt);
26529 if (found)
26530 return &found->result;
26531 else
26532 return 0;
26535 /* Save a subsumption result. */
26537 bool
26538 save_subsumption_result (tree t1, tree t2, bool result)
26540 subsumption_entry elt = {t1, t2, result};
26541 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26542 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26543 *entry = elt;
26544 *slot = entry;
26545 return result;
26548 /* Set up the hash table for constraint association. */
26550 void
26551 init_constraint_processing (void)
26553 if (!flag_concepts)
26554 return;
26556 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26557 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26558 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26559 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26560 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26563 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26564 0..N-1. */
26566 void
26567 declare_integer_pack (void)
26569 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26570 build_function_type_list (integer_type_node,
26571 integer_type_node,
26572 NULL_TREE),
26573 NULL_TREE, ECF_CONST);
26574 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26575 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26578 /* Set up the hash tables for template instantiations. */
26580 void
26581 init_template_processing (void)
26583 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26584 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26586 if (cxx_dialect >= cxx11)
26587 declare_integer_pack ();
26590 /* Print stats about the template hash tables for -fstats. */
26592 void
26593 print_template_statistics (void)
26595 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26596 "%f collisions\n", (long) decl_specializations->size (),
26597 (long) decl_specializations->elements (),
26598 decl_specializations->collisions ());
26599 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26600 "%f collisions\n", (long) type_specializations->size (),
26601 (long) type_specializations->elements (),
26602 type_specializations->collisions ());
26605 #include "gt-cp-pt.h"