* cfghooks.c (verify_flow_info): Disable check that all probabilities
[official-gcc.git] / gcc / cp / pt.c
blobba52f3b57a62dc6970f5c732939ca0dbe5dcba3e
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 int 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);
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)
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 error ("base initializer expansion %qT contains no parameter packs", arg);
3774 delete ppd.visited;
3775 return error_mark_node;
3778 if (TREE_VALUE (arg) != void_type_node)
3780 /* Collect the sets of parameter packs used in each of the
3781 initialization arguments. */
3782 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3784 /* Determine which parameter packs will be expanded in this
3785 argument. */
3786 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3787 &ppd, ppd.visited);
3791 delete ppd.visited;
3793 /* Create the pack expansion type for the base type. */
3794 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3795 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3796 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3797 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3799 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3800 they will rarely be compared to anything. */
3801 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3803 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3806 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3807 for_types = true;
3809 /* Build the PACK_EXPANSION_* node. */
3810 result = for_types
3811 ? cxx_make_type (TYPE_PACK_EXPANSION)
3812 : make_node (EXPR_PACK_EXPANSION);
3813 SET_PACK_EXPANSION_PATTERN (result, arg);
3814 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3816 /* Propagate type and const-expression information. */
3817 TREE_TYPE (result) = TREE_TYPE (arg);
3818 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3819 /* Mark this read now, since the expansion might be length 0. */
3820 mark_exp_read (arg);
3822 else
3823 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3824 they will rarely be compared to anything. */
3825 SET_TYPE_STRUCTURAL_EQUALITY (result);
3827 /* Determine which parameter packs will be expanded. */
3828 ppd.parameter_packs = &parameter_packs;
3829 ppd.visited = new hash_set<tree>;
3830 ppd.type_pack_expansion_p = TYPE_P (arg);
3831 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3832 delete ppd.visited;
3834 /* Make sure we found some parameter packs. */
3835 if (parameter_packs == NULL_TREE)
3837 if (TYPE_P (arg))
3838 error ("expansion pattern %qT contains no argument packs", arg);
3839 else
3840 error ("expansion pattern %qE contains no argument packs", arg);
3841 return error_mark_node;
3843 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3845 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3847 return result;
3850 /* Checks T for any "bare" parameter packs, which have not yet been
3851 expanded, and issues an error if any are found. This operation can
3852 only be done on full expressions or types (e.g., an expression
3853 statement, "if" condition, etc.), because we could have expressions like:
3855 foo(f(g(h(args)))...)
3857 where "args" is a parameter pack. check_for_bare_parameter_packs
3858 should not be called for the subexpressions args, h(args),
3859 g(h(args)), or f(g(h(args))), because we would produce erroneous
3860 error messages.
3862 Returns TRUE and emits an error if there were bare parameter packs,
3863 returns FALSE otherwise. */
3864 bool
3865 check_for_bare_parameter_packs (tree t)
3867 tree parameter_packs = NULL_TREE;
3868 struct find_parameter_pack_data ppd;
3870 if (!processing_template_decl || !t || t == error_mark_node)
3871 return false;
3873 /* A lambda might use a parameter pack from the containing context. */
3874 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3875 return false;
3877 if (TREE_CODE (t) == TYPE_DECL)
3878 t = TREE_TYPE (t);
3880 ppd.parameter_packs = &parameter_packs;
3881 ppd.visited = new hash_set<tree>;
3882 ppd.type_pack_expansion_p = false;
3883 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3884 delete ppd.visited;
3886 if (parameter_packs)
3888 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3889 error_at (loc, "parameter packs not expanded with %<...%>:");
3890 while (parameter_packs)
3892 tree pack = TREE_VALUE (parameter_packs);
3893 tree name = NULL_TREE;
3895 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3896 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3897 name = TYPE_NAME (pack);
3898 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3899 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3900 else if (TREE_CODE (pack) == CALL_EXPR)
3901 name = DECL_NAME (CALL_EXPR_FN (pack));
3902 else
3903 name = DECL_NAME (pack);
3905 if (name)
3906 inform (loc, " %qD", name);
3907 else
3908 inform (loc, " <anonymous>");
3910 parameter_packs = TREE_CHAIN (parameter_packs);
3913 return true;
3916 return false;
3919 /* Expand any parameter packs that occur in the template arguments in
3920 ARGS. */
3921 tree
3922 expand_template_argument_pack (tree args)
3924 if (args == error_mark_node)
3925 return error_mark_node;
3927 tree result_args = NULL_TREE;
3928 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3929 int num_result_args = -1;
3930 int non_default_args_count = -1;
3932 /* First, determine if we need to expand anything, and the number of
3933 slots we'll need. */
3934 for (in_arg = 0; in_arg < nargs; ++in_arg)
3936 tree arg = TREE_VEC_ELT (args, in_arg);
3937 if (arg == NULL_TREE)
3938 return args;
3939 if (ARGUMENT_PACK_P (arg))
3941 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3942 if (num_result_args < 0)
3943 num_result_args = in_arg + num_packed;
3944 else
3945 num_result_args += num_packed;
3947 else
3949 if (num_result_args >= 0)
3950 num_result_args++;
3954 /* If no expansion is necessary, we're done. */
3955 if (num_result_args < 0)
3956 return args;
3958 /* Expand arguments. */
3959 result_args = make_tree_vec (num_result_args);
3960 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3961 non_default_args_count =
3962 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3963 for (in_arg = 0; in_arg < nargs; ++in_arg)
3965 tree arg = TREE_VEC_ELT (args, in_arg);
3966 if (ARGUMENT_PACK_P (arg))
3968 tree packed = ARGUMENT_PACK_ARGS (arg);
3969 int i, num_packed = TREE_VEC_LENGTH (packed);
3970 for (i = 0; i < num_packed; ++i, ++out_arg)
3971 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3972 if (non_default_args_count > 0)
3973 non_default_args_count += num_packed - 1;
3975 else
3977 TREE_VEC_ELT (result_args, out_arg) = arg;
3978 ++out_arg;
3981 if (non_default_args_count >= 0)
3982 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3983 return result_args;
3986 /* Checks if DECL shadows a template parameter.
3988 [temp.local]: A template-parameter shall not be redeclared within its
3989 scope (including nested scopes).
3991 Emits an error and returns TRUE if the DECL shadows a parameter,
3992 returns FALSE otherwise. */
3994 bool
3995 check_template_shadow (tree decl)
3997 tree olddecl;
3999 /* If we're not in a template, we can't possibly shadow a template
4000 parameter. */
4001 if (!current_template_parms)
4002 return true;
4004 /* Figure out what we're shadowing. */
4005 decl = OVL_FIRST (decl);
4006 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4008 /* If there's no previous binding for this name, we're not shadowing
4009 anything, let alone a template parameter. */
4010 if (!olddecl)
4011 return true;
4013 /* If we're not shadowing a template parameter, we're done. Note
4014 that OLDDECL might be an OVERLOAD (or perhaps even an
4015 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4016 node. */
4017 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4018 return true;
4020 /* We check for decl != olddecl to avoid bogus errors for using a
4021 name inside a class. We check TPFI to avoid duplicate errors for
4022 inline member templates. */
4023 if (decl == olddecl
4024 || (DECL_TEMPLATE_PARM_P (decl)
4025 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4026 return true;
4028 /* Don't complain about the injected class name, as we've already
4029 complained about the class itself. */
4030 if (DECL_SELF_REFERENCE_P (decl))
4031 return false;
4033 if (DECL_TEMPLATE_PARM_P (decl))
4034 error ("declaration of template parameter %q+D shadows "
4035 "template parameter", decl);
4036 else
4037 error ("declaration of %q+#D shadows template parameter", decl);
4038 inform (DECL_SOURCE_LOCATION (olddecl),
4039 "template parameter %qD declared here", olddecl);
4040 return false;
4043 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4044 ORIG_LEVEL, DECL, and TYPE. */
4046 static tree
4047 build_template_parm_index (int index,
4048 int level,
4049 int orig_level,
4050 tree decl,
4051 tree type)
4053 tree t = make_node (TEMPLATE_PARM_INDEX);
4054 TEMPLATE_PARM_IDX (t) = index;
4055 TEMPLATE_PARM_LEVEL (t) = level;
4056 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4057 TEMPLATE_PARM_DECL (t) = decl;
4058 TREE_TYPE (t) = type;
4059 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4060 TREE_READONLY (t) = TREE_READONLY (decl);
4062 return t;
4065 /* Find the canonical type parameter for the given template type
4066 parameter. Returns the canonical type parameter, which may be TYPE
4067 if no such parameter existed. */
4069 static tree
4070 canonical_type_parameter (tree type)
4072 tree list;
4073 int idx = TEMPLATE_TYPE_IDX (type);
4074 if (!canonical_template_parms)
4075 vec_alloc (canonical_template_parms, idx + 1);
4077 if (canonical_template_parms->length () <= (unsigned) idx)
4078 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4080 list = (*canonical_template_parms)[idx];
4081 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4082 list = TREE_CHAIN (list);
4084 if (list)
4085 return TREE_VALUE (list);
4086 else
4088 (*canonical_template_parms)[idx]
4089 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4090 return type;
4094 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4095 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4096 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4097 new one is created. */
4099 static tree
4100 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4101 tsubst_flags_t complain)
4103 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4104 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4105 != TEMPLATE_PARM_LEVEL (index) - levels)
4106 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4108 tree orig_decl = TEMPLATE_PARM_DECL (index);
4109 tree decl, t;
4111 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4112 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4113 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4114 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4115 DECL_ARTIFICIAL (decl) = 1;
4116 SET_DECL_TEMPLATE_PARM_P (decl);
4118 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4119 TEMPLATE_PARM_LEVEL (index) - levels,
4120 TEMPLATE_PARM_ORIG_LEVEL (index),
4121 decl, type);
4122 TEMPLATE_PARM_DESCENDANTS (index) = t;
4123 TEMPLATE_PARM_PARAMETER_PACK (t)
4124 = TEMPLATE_PARM_PARAMETER_PACK (index);
4126 /* Template template parameters need this. */
4127 if (TREE_CODE (decl) == TEMPLATE_DECL)
4129 DECL_TEMPLATE_RESULT (decl)
4130 = build_decl (DECL_SOURCE_LOCATION (decl),
4131 TYPE_DECL, DECL_NAME (decl), type);
4132 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4133 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4134 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4138 return TEMPLATE_PARM_DESCENDANTS (index);
4141 /* Process information from new template parameter PARM and append it
4142 to the LIST being built. This new parameter is a non-type
4143 parameter iff IS_NON_TYPE is true. This new parameter is a
4144 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4145 is in PARM_LOC. */
4147 tree
4148 process_template_parm (tree list, location_t parm_loc, tree parm,
4149 bool is_non_type, bool is_parameter_pack)
4151 tree decl = 0;
4152 int idx = 0;
4154 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4155 tree defval = TREE_PURPOSE (parm);
4156 tree constr = TREE_TYPE (parm);
4158 if (list)
4160 tree p = tree_last (list);
4162 if (p && TREE_VALUE (p) != error_mark_node)
4164 p = TREE_VALUE (p);
4165 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4166 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4167 else
4168 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4171 ++idx;
4174 if (is_non_type)
4176 parm = TREE_VALUE (parm);
4178 SET_DECL_TEMPLATE_PARM_P (parm);
4180 if (TREE_TYPE (parm) != error_mark_node)
4182 /* [temp.param]
4184 The top-level cv-qualifiers on the template-parameter are
4185 ignored when determining its type. */
4186 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4187 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4188 TREE_TYPE (parm) = error_mark_node;
4189 else if (uses_parameter_packs (TREE_TYPE (parm))
4190 && !is_parameter_pack
4191 /* If we're in a nested template parameter list, the template
4192 template parameter could be a parameter pack. */
4193 && processing_template_parmlist == 1)
4195 /* This template parameter is not a parameter pack, but it
4196 should be. Complain about "bare" parameter packs. */
4197 check_for_bare_parameter_packs (TREE_TYPE (parm));
4199 /* Recover by calling this a parameter pack. */
4200 is_parameter_pack = true;
4204 /* A template parameter is not modifiable. */
4205 TREE_CONSTANT (parm) = 1;
4206 TREE_READONLY (parm) = 1;
4207 decl = build_decl (parm_loc,
4208 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4209 TREE_CONSTANT (decl) = 1;
4210 TREE_READONLY (decl) = 1;
4211 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4212 = build_template_parm_index (idx, processing_template_decl,
4213 processing_template_decl,
4214 decl, TREE_TYPE (parm));
4216 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4217 = is_parameter_pack;
4219 else
4221 tree t;
4222 parm = TREE_VALUE (TREE_VALUE (parm));
4224 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4226 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4227 /* This is for distinguishing between real templates and template
4228 template parameters */
4229 TREE_TYPE (parm) = t;
4230 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4231 decl = parm;
4233 else
4235 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4236 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4237 decl = build_decl (parm_loc,
4238 TYPE_DECL, parm, t);
4241 TYPE_NAME (t) = decl;
4242 TYPE_STUB_DECL (t) = decl;
4243 parm = decl;
4244 TEMPLATE_TYPE_PARM_INDEX (t)
4245 = build_template_parm_index (idx, processing_template_decl,
4246 processing_template_decl,
4247 decl, TREE_TYPE (parm));
4248 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4249 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4251 DECL_ARTIFICIAL (decl) = 1;
4252 SET_DECL_TEMPLATE_PARM_P (decl);
4254 /* Build requirements for the type/template parameter.
4255 This must be done after SET_DECL_TEMPLATE_PARM_P or
4256 process_template_parm could fail. */
4257 tree reqs = finish_shorthand_constraint (parm, constr);
4259 pushdecl (decl);
4261 /* Build the parameter node linking the parameter declaration,
4262 its default argument (if any), and its constraints (if any). */
4263 parm = build_tree_list (defval, parm);
4264 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4266 return chainon (list, parm);
4269 /* The end of a template parameter list has been reached. Process the
4270 tree list into a parameter vector, converting each parameter into a more
4271 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4272 as PARM_DECLs. */
4274 tree
4275 end_template_parm_list (tree parms)
4277 int nparms;
4278 tree parm, next;
4279 tree saved_parmlist = make_tree_vec (list_length (parms));
4281 /* Pop the dummy parameter level and add the real one. */
4282 current_template_parms = TREE_CHAIN (current_template_parms);
4284 current_template_parms
4285 = tree_cons (size_int (processing_template_decl),
4286 saved_parmlist, current_template_parms);
4288 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4290 next = TREE_CHAIN (parm);
4291 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4292 TREE_CHAIN (parm) = NULL_TREE;
4295 --processing_template_parmlist;
4297 return saved_parmlist;
4300 // Explicitly indicate the end of the template parameter list. We assume
4301 // that the current template parameters have been constructed and/or
4302 // managed explicitly, as when creating new template template parameters
4303 // from a shorthand constraint.
4304 void
4305 end_template_parm_list ()
4307 --processing_template_parmlist;
4310 /* end_template_decl is called after a template declaration is seen. */
4312 void
4313 end_template_decl (void)
4315 reset_specialization ();
4317 if (! processing_template_decl)
4318 return;
4320 /* This matches the pushlevel in begin_template_parm_list. */
4321 finish_scope ();
4323 --processing_template_decl;
4324 current_template_parms = TREE_CHAIN (current_template_parms);
4327 /* Takes a TREE_LIST representing a template parameter and convert it
4328 into an argument suitable to be passed to the type substitution
4329 functions. Note that If the TREE_LIST contains an error_mark
4330 node, the returned argument is error_mark_node. */
4332 tree
4333 template_parm_to_arg (tree t)
4336 if (t == NULL_TREE
4337 || TREE_CODE (t) != TREE_LIST)
4338 return t;
4340 if (error_operand_p (TREE_VALUE (t)))
4341 return error_mark_node;
4343 t = TREE_VALUE (t);
4345 if (TREE_CODE (t) == TYPE_DECL
4346 || TREE_CODE (t) == TEMPLATE_DECL)
4348 t = TREE_TYPE (t);
4350 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4352 /* Turn this argument into a TYPE_ARGUMENT_PACK
4353 with a single element, which expands T. */
4354 tree vec = make_tree_vec (1);
4355 if (CHECKING_P)
4356 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4358 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4360 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4361 SET_ARGUMENT_PACK_ARGS (t, vec);
4364 else
4366 t = DECL_INITIAL (t);
4368 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4370 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4371 with a single element, which expands T. */
4372 tree vec = make_tree_vec (1);
4373 if (CHECKING_P)
4374 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4376 t = convert_from_reference (t);
4377 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4379 t = make_node (NONTYPE_ARGUMENT_PACK);
4380 SET_ARGUMENT_PACK_ARGS (t, vec);
4382 else
4383 t = convert_from_reference (t);
4385 return t;
4388 /* Given a single level of template parameters (a TREE_VEC), return it
4389 as a set of template arguments. */
4391 static tree
4392 template_parms_level_to_args (tree parms)
4394 tree a = copy_node (parms);
4395 TREE_TYPE (a) = NULL_TREE;
4396 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4397 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4399 if (CHECKING_P)
4400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4402 return a;
4405 /* Given a set of template parameters, return them as a set of template
4406 arguments. The template parameters are represented as a TREE_VEC, in
4407 the form documented in cp-tree.h for template arguments. */
4409 static tree
4410 template_parms_to_args (tree parms)
4412 tree header;
4413 tree args = NULL_TREE;
4414 int length = TMPL_PARMS_DEPTH (parms);
4415 int l = length;
4417 /* If there is only one level of template parameters, we do not
4418 create a TREE_VEC of TREE_VECs. Instead, we return a single
4419 TREE_VEC containing the arguments. */
4420 if (length > 1)
4421 args = make_tree_vec (length);
4423 for (header = parms; header; header = TREE_CHAIN (header))
4425 tree a = template_parms_level_to_args (TREE_VALUE (header));
4427 if (length > 1)
4428 TREE_VEC_ELT (args, --l) = a;
4429 else
4430 args = a;
4433 return args;
4436 /* Within the declaration of a template, return the currently active
4437 template parameters as an argument TREE_VEC. */
4439 static tree
4440 current_template_args (void)
4442 return template_parms_to_args (current_template_parms);
4445 /* Update the declared TYPE by doing any lookups which were thought to be
4446 dependent, but are not now that we know the SCOPE of the declarator. */
4448 tree
4449 maybe_update_decl_type (tree orig_type, tree scope)
4451 tree type = orig_type;
4453 if (type == NULL_TREE)
4454 return type;
4456 if (TREE_CODE (orig_type) == TYPE_DECL)
4457 type = TREE_TYPE (type);
4459 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4460 && dependent_type_p (type)
4461 /* Don't bother building up the args in this case. */
4462 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4464 /* tsubst in the args corresponding to the template parameters,
4465 including auto if present. Most things will be unchanged, but
4466 make_typename_type and tsubst_qualified_id will resolve
4467 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4468 tree args = current_template_args ();
4469 tree auto_node = type_uses_auto (type);
4470 tree pushed;
4471 if (auto_node)
4473 tree auto_vec = make_tree_vec (1);
4474 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4475 args = add_to_template_args (args, auto_vec);
4477 pushed = push_scope (scope);
4478 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4479 if (pushed)
4480 pop_scope (scope);
4483 if (type == error_mark_node)
4484 return orig_type;
4486 if (TREE_CODE (orig_type) == TYPE_DECL)
4488 if (same_type_p (type, TREE_TYPE (orig_type)))
4489 type = orig_type;
4490 else
4491 type = TYPE_NAME (type);
4493 return type;
4496 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4497 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4498 the new template is a member template. */
4500 tree
4501 build_template_decl (tree decl, tree parms, bool member_template_p)
4503 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4504 DECL_TEMPLATE_PARMS (tmpl) = parms;
4505 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4506 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4507 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4509 return tmpl;
4512 struct template_parm_data
4514 /* The level of the template parameters we are currently
4515 processing. */
4516 int level;
4518 /* The index of the specialization argument we are currently
4519 processing. */
4520 int current_arg;
4522 /* An array whose size is the number of template parameters. The
4523 elements are nonzero if the parameter has been used in any one
4524 of the arguments processed so far. */
4525 int* parms;
4527 /* An array whose size is the number of template arguments. The
4528 elements are nonzero if the argument makes use of template
4529 parameters of this level. */
4530 int* arg_uses_template_parms;
4533 /* Subroutine of push_template_decl used to see if each template
4534 parameter in a partial specialization is used in the explicit
4535 argument list. If T is of the LEVEL given in DATA (which is
4536 treated as a template_parm_data*), then DATA->PARMS is marked
4537 appropriately. */
4539 static int
4540 mark_template_parm (tree t, void* data)
4542 int level;
4543 int idx;
4544 struct template_parm_data* tpd = (struct template_parm_data*) data;
4546 template_parm_level_and_index (t, &level, &idx);
4548 if (level == tpd->level)
4550 tpd->parms[idx] = 1;
4551 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4554 /* In C++17 the type of a non-type argument is a deduced context. */
4555 if (cxx_dialect >= cxx17
4556 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4557 for_each_template_parm (TREE_TYPE (t),
4558 &mark_template_parm,
4559 data,
4560 NULL,
4561 /*include_nondeduced_p=*/false);
4563 /* Return zero so that for_each_template_parm will continue the
4564 traversal of the tree; we want to mark *every* template parm. */
4565 return 0;
4568 /* Process the partial specialization DECL. */
4570 static tree
4571 process_partial_specialization (tree decl)
4573 tree type = TREE_TYPE (decl);
4574 tree tinfo = get_template_info (decl);
4575 tree maintmpl = TI_TEMPLATE (tinfo);
4576 tree specargs = TI_ARGS (tinfo);
4577 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4578 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4579 tree inner_parms;
4580 tree inst;
4581 int nargs = TREE_VEC_LENGTH (inner_args);
4582 int ntparms;
4583 int i;
4584 bool did_error_intro = false;
4585 struct template_parm_data tpd;
4586 struct template_parm_data tpd2;
4588 gcc_assert (current_template_parms);
4590 /* A concept cannot be specialized. */
4591 if (flag_concepts && variable_concept_p (maintmpl))
4593 error ("specialization of variable concept %q#D", maintmpl);
4594 return error_mark_node;
4597 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4598 ntparms = TREE_VEC_LENGTH (inner_parms);
4600 /* We check that each of the template parameters given in the
4601 partial specialization is used in the argument list to the
4602 specialization. For example:
4604 template <class T> struct S;
4605 template <class T> struct S<T*>;
4607 The second declaration is OK because `T*' uses the template
4608 parameter T, whereas
4610 template <class T> struct S<int>;
4612 is no good. Even trickier is:
4614 template <class T>
4615 struct S1
4617 template <class U>
4618 struct S2;
4619 template <class U>
4620 struct S2<T>;
4623 The S2<T> declaration is actually invalid; it is a
4624 full-specialization. Of course,
4626 template <class U>
4627 struct S2<T (*)(U)>;
4629 or some such would have been OK. */
4630 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4631 tpd.parms = XALLOCAVEC (int, ntparms);
4632 memset (tpd.parms, 0, sizeof (int) * ntparms);
4634 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4635 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4636 for (i = 0; i < nargs; ++i)
4638 tpd.current_arg = i;
4639 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4640 &mark_template_parm,
4641 &tpd,
4642 NULL,
4643 /*include_nondeduced_p=*/false);
4645 for (i = 0; i < ntparms; ++i)
4646 if (tpd.parms[i] == 0)
4648 /* One of the template parms was not used in a deduced context in the
4649 specialization. */
4650 if (!did_error_intro)
4652 error ("template parameters not deducible in "
4653 "partial specialization:");
4654 did_error_intro = true;
4657 inform (input_location, " %qD",
4658 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4661 if (did_error_intro)
4662 return error_mark_node;
4664 /* [temp.class.spec]
4666 The argument list of the specialization shall not be identical to
4667 the implicit argument list of the primary template. */
4668 tree main_args
4669 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4670 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4671 && (!flag_concepts
4672 || !strictly_subsumes (current_template_constraints (),
4673 get_constraints (maintmpl))))
4675 if (!flag_concepts)
4676 error ("partial specialization %q+D does not specialize "
4677 "any template arguments", decl);
4678 else
4679 error ("partial specialization %q+D does not specialize any "
4680 "template arguments and is not more constrained than", decl);
4681 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4684 /* A partial specialization that replaces multiple parameters of the
4685 primary template with a pack expansion is less specialized for those
4686 parameters. */
4687 if (nargs < DECL_NTPARMS (maintmpl))
4689 error ("partial specialization is not more specialized than the "
4690 "primary template because it replaces multiple parameters "
4691 "with a pack expansion");
4692 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4693 /* Avoid crash in process_partial_specialization. */
4694 return decl;
4697 /* If we aren't in a dependent class, we can actually try deduction. */
4698 else if (tpd.level == 1
4699 /* FIXME we should be able to handle a partial specialization of a
4700 partial instantiation, but currently we can't (c++/41727). */
4701 && TMPL_ARGS_DEPTH (specargs) == 1
4702 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4704 if (permerror (input_location, "partial specialization %qD is not "
4705 "more specialized than", decl))
4706 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4707 maintmpl);
4710 /* [temp.class.spec]
4712 A partially specialized non-type argument expression shall not
4713 involve template parameters of the partial specialization except
4714 when the argument expression is a simple identifier.
4716 The type of a template parameter corresponding to a specialized
4717 non-type argument shall not be dependent on a parameter of the
4718 specialization.
4720 Also, we verify that pack expansions only occur at the
4721 end of the argument list. */
4722 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4723 tpd2.parms = 0;
4724 for (i = 0; i < nargs; ++i)
4726 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4727 tree arg = TREE_VEC_ELT (inner_args, i);
4728 tree packed_args = NULL_TREE;
4729 int j, len = 1;
4731 if (ARGUMENT_PACK_P (arg))
4733 /* Extract the arguments from the argument pack. We'll be
4734 iterating over these in the following loop. */
4735 packed_args = ARGUMENT_PACK_ARGS (arg);
4736 len = TREE_VEC_LENGTH (packed_args);
4739 for (j = 0; j < len; j++)
4741 if (packed_args)
4742 /* Get the Jth argument in the parameter pack. */
4743 arg = TREE_VEC_ELT (packed_args, j);
4745 if (PACK_EXPANSION_P (arg))
4747 /* Pack expansions must come at the end of the
4748 argument list. */
4749 if ((packed_args && j < len - 1)
4750 || (!packed_args && i < nargs - 1))
4752 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4753 error ("parameter pack argument %qE must be at the "
4754 "end of the template argument list", arg);
4755 else
4756 error ("parameter pack argument %qT must be at the "
4757 "end of the template argument list", arg);
4761 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4762 /* We only care about the pattern. */
4763 arg = PACK_EXPANSION_PATTERN (arg);
4765 if (/* These first two lines are the `non-type' bit. */
4766 !TYPE_P (arg)
4767 && TREE_CODE (arg) != TEMPLATE_DECL
4768 /* This next two lines are the `argument expression is not just a
4769 simple identifier' condition and also the `specialized
4770 non-type argument' bit. */
4771 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4772 && !(REFERENCE_REF_P (arg)
4773 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4775 if ((!packed_args && tpd.arg_uses_template_parms[i])
4776 || (packed_args && uses_template_parms (arg)))
4777 error ("template argument %qE involves template parameter(s)",
4778 arg);
4779 else
4781 /* Look at the corresponding template parameter,
4782 marking which template parameters its type depends
4783 upon. */
4784 tree type = TREE_TYPE (parm);
4786 if (!tpd2.parms)
4788 /* We haven't yet initialized TPD2. Do so now. */
4789 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4790 /* The number of parameters here is the number in the
4791 main template, which, as checked in the assertion
4792 above, is NARGS. */
4793 tpd2.parms = XALLOCAVEC (int, nargs);
4794 tpd2.level =
4795 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4798 /* Mark the template parameters. But this time, we're
4799 looking for the template parameters of the main
4800 template, not in the specialization. */
4801 tpd2.current_arg = i;
4802 tpd2.arg_uses_template_parms[i] = 0;
4803 memset (tpd2.parms, 0, sizeof (int) * nargs);
4804 for_each_template_parm (type,
4805 &mark_template_parm,
4806 &tpd2,
4807 NULL,
4808 /*include_nondeduced_p=*/false);
4810 if (tpd2.arg_uses_template_parms [i])
4812 /* The type depended on some template parameters.
4813 If they are fully specialized in the
4814 specialization, that's OK. */
4815 int j;
4816 int count = 0;
4817 for (j = 0; j < nargs; ++j)
4818 if (tpd2.parms[j] != 0
4819 && tpd.arg_uses_template_parms [j])
4820 ++count;
4821 if (count != 0)
4822 error_n (input_location, count,
4823 "type %qT of template argument %qE depends "
4824 "on a template parameter",
4825 "type %qT of template argument %qE depends "
4826 "on template parameters",
4827 type,
4828 arg);
4835 /* We should only get here once. */
4836 if (TREE_CODE (decl) == TYPE_DECL)
4837 gcc_assert (!COMPLETE_TYPE_P (type));
4839 // Build the template decl.
4840 tree tmpl = build_template_decl (decl, current_template_parms,
4841 DECL_MEMBER_TEMPLATE_P (maintmpl));
4842 TREE_TYPE (tmpl) = type;
4843 DECL_TEMPLATE_RESULT (tmpl) = decl;
4844 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4845 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4846 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4848 /* Give template template parms a DECL_CONTEXT of the template
4849 for which they are a parameter. */
4850 for (i = 0; i < ntparms; ++i)
4852 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4853 if (TREE_CODE (parm) == TEMPLATE_DECL)
4854 DECL_CONTEXT (parm) = tmpl;
4857 if (VAR_P (decl))
4858 /* We didn't register this in check_explicit_specialization so we could
4859 wait until the constraints were set. */
4860 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4861 else
4862 associate_classtype_constraints (type);
4864 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4865 = tree_cons (specargs, tmpl,
4866 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4867 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4869 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4870 inst = TREE_CHAIN (inst))
4872 tree instance = TREE_VALUE (inst);
4873 if (TYPE_P (instance)
4874 ? (COMPLETE_TYPE_P (instance)
4875 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4876 : DECL_TEMPLATE_INSTANTIATION (instance))
4878 tree spec = most_specialized_partial_spec (instance, tf_none);
4879 tree inst_decl = (DECL_P (instance)
4880 ? instance : TYPE_NAME (instance));
4881 if (!spec)
4882 /* OK */;
4883 else if (spec == error_mark_node)
4884 permerror (input_location,
4885 "declaration of %qD ambiguates earlier template "
4886 "instantiation for %qD", decl, inst_decl);
4887 else if (TREE_VALUE (spec) == tmpl)
4888 permerror (input_location,
4889 "partial specialization of %qD after instantiation "
4890 "of %qD", decl, inst_decl);
4894 return decl;
4897 /* PARM is a template parameter of some form; return the corresponding
4898 TEMPLATE_PARM_INDEX. */
4900 static tree
4901 get_template_parm_index (tree parm)
4903 if (TREE_CODE (parm) == PARM_DECL
4904 || TREE_CODE (parm) == CONST_DECL)
4905 parm = DECL_INITIAL (parm);
4906 else if (TREE_CODE (parm) == TYPE_DECL
4907 || TREE_CODE (parm) == TEMPLATE_DECL)
4908 parm = TREE_TYPE (parm);
4909 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4910 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4911 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4912 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4913 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4914 return parm;
4917 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4918 parameter packs used by the template parameter PARM. */
4920 static void
4921 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4923 /* A type parm can't refer to another parm. */
4924 if (TREE_CODE (parm) == TYPE_DECL)
4925 return;
4926 else if (TREE_CODE (parm) == PARM_DECL)
4928 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4929 ppd, ppd->visited);
4930 return;
4933 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4935 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4936 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4937 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4940 /* PARM is a template parameter pack. Return any parameter packs used in
4941 its type or the type of any of its template parameters. If there are
4942 any such packs, it will be instantiated into a fixed template parameter
4943 list by partial instantiation rather than be fully deduced. */
4945 tree
4946 fixed_parameter_pack_p (tree parm)
4948 /* This can only be true in a member template. */
4949 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4950 return NULL_TREE;
4951 /* This can only be true for a parameter pack. */
4952 if (!template_parameter_pack_p (parm))
4953 return NULL_TREE;
4954 /* A type parm can't refer to another parm. */
4955 if (TREE_CODE (parm) == TYPE_DECL)
4956 return NULL_TREE;
4958 tree parameter_packs = NULL_TREE;
4959 struct find_parameter_pack_data ppd;
4960 ppd.parameter_packs = &parameter_packs;
4961 ppd.visited = new hash_set<tree>;
4962 ppd.type_pack_expansion_p = false;
4964 fixed_parameter_pack_p_1 (parm, &ppd);
4966 delete ppd.visited;
4967 return parameter_packs;
4970 /* Check that a template declaration's use of default arguments and
4971 parameter packs is not invalid. Here, PARMS are the template
4972 parameters. IS_PRIMARY is true if DECL is the thing declared by
4973 a primary template. IS_PARTIAL is true if DECL is a partial
4974 specialization.
4976 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4977 declaration (but not a definition); 1 indicates a declaration, 2
4978 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4979 emitted for extraneous default arguments.
4981 Returns TRUE if there were no errors found, FALSE otherwise. */
4983 bool
4984 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4985 bool is_partial, int is_friend_decl)
4987 const char *msg;
4988 int last_level_to_check;
4989 tree parm_level;
4990 bool no_errors = true;
4992 /* [temp.param]
4994 A default template-argument shall not be specified in a
4995 function template declaration or a function template definition, nor
4996 in the template-parameter-list of the definition of a member of a
4997 class template. */
4999 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5000 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5001 /* You can't have a function template declaration in a local
5002 scope, nor you can you define a member of a class template in a
5003 local scope. */
5004 return true;
5006 if ((TREE_CODE (decl) == TYPE_DECL
5007 && TREE_TYPE (decl)
5008 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5009 || (TREE_CODE (decl) == FUNCTION_DECL
5010 && LAMBDA_FUNCTION_P (decl)))
5011 /* A lambda doesn't have an explicit declaration; don't complain
5012 about the parms of the enclosing class. */
5013 return true;
5015 if (current_class_type
5016 && !TYPE_BEING_DEFINED (current_class_type)
5017 && DECL_LANG_SPECIFIC (decl)
5018 && DECL_DECLARES_FUNCTION_P (decl)
5019 /* If this is either a friend defined in the scope of the class
5020 or a member function. */
5021 && (DECL_FUNCTION_MEMBER_P (decl)
5022 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5023 : DECL_FRIEND_CONTEXT (decl)
5024 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5025 : false)
5026 /* And, if it was a member function, it really was defined in
5027 the scope of the class. */
5028 && (!DECL_FUNCTION_MEMBER_P (decl)
5029 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5030 /* We already checked these parameters when the template was
5031 declared, so there's no need to do it again now. This function
5032 was defined in class scope, but we're processing its body now
5033 that the class is complete. */
5034 return true;
5036 /* Core issue 226 (C++0x only): the following only applies to class
5037 templates. */
5038 if (is_primary
5039 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5041 /* [temp.param]
5043 If a template-parameter has a default template-argument, all
5044 subsequent template-parameters shall have a default
5045 template-argument supplied. */
5046 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5048 tree inner_parms = TREE_VALUE (parm_level);
5049 int ntparms = TREE_VEC_LENGTH (inner_parms);
5050 int seen_def_arg_p = 0;
5051 int i;
5053 for (i = 0; i < ntparms; ++i)
5055 tree parm = TREE_VEC_ELT (inner_parms, i);
5057 if (parm == error_mark_node)
5058 continue;
5060 if (TREE_PURPOSE (parm))
5061 seen_def_arg_p = 1;
5062 else if (seen_def_arg_p
5063 && !template_parameter_pack_p (TREE_VALUE (parm)))
5065 error ("no default argument for %qD", TREE_VALUE (parm));
5066 /* For better subsequent error-recovery, we indicate that
5067 there should have been a default argument. */
5068 TREE_PURPOSE (parm) = error_mark_node;
5069 no_errors = false;
5071 else if (!is_partial
5072 && !is_friend_decl
5073 /* Don't complain about an enclosing partial
5074 specialization. */
5075 && parm_level == parms
5076 && TREE_CODE (decl) == TYPE_DECL
5077 && i < ntparms - 1
5078 && template_parameter_pack_p (TREE_VALUE (parm))
5079 /* A fixed parameter pack will be partially
5080 instantiated into a fixed length list. */
5081 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5083 /* A primary class template can only have one
5084 parameter pack, at the end of the template
5085 parameter list. */
5087 error ("parameter pack %q+D must be at the end of the"
5088 " template parameter list", TREE_VALUE (parm));
5090 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5091 = error_mark_node;
5092 no_errors = false;
5098 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5099 || is_partial
5100 || !is_primary
5101 || is_friend_decl)
5102 /* For an ordinary class template, default template arguments are
5103 allowed at the innermost level, e.g.:
5104 template <class T = int>
5105 struct S {};
5106 but, in a partial specialization, they're not allowed even
5107 there, as we have in [temp.class.spec]:
5109 The template parameter list of a specialization shall not
5110 contain default template argument values.
5112 So, for a partial specialization, or for a function template
5113 (in C++98/C++03), we look at all of them. */
5115 else
5116 /* But, for a primary class template that is not a partial
5117 specialization we look at all template parameters except the
5118 innermost ones. */
5119 parms = TREE_CHAIN (parms);
5121 /* Figure out what error message to issue. */
5122 if (is_friend_decl == 2)
5123 msg = G_("default template arguments may not be used in function template "
5124 "friend re-declaration");
5125 else if (is_friend_decl)
5126 msg = G_("default template arguments may not be used in function template "
5127 "friend declarations");
5128 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5129 msg = G_("default template arguments may not be used in function templates "
5130 "without -std=c++11 or -std=gnu++11");
5131 else if (is_partial)
5132 msg = G_("default template arguments may not be used in "
5133 "partial specializations");
5134 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5135 msg = G_("default argument for template parameter for class enclosing %qD");
5136 else
5137 /* Per [temp.param]/9, "A default template-argument shall not be
5138 specified in the template-parameter-lists of the definition of
5139 a member of a class template that appears outside of the member's
5140 class.", thus if we aren't handling a member of a class template
5141 there is no need to examine the parameters. */
5142 return true;
5144 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5145 /* If we're inside a class definition, there's no need to
5146 examine the parameters to the class itself. On the one
5147 hand, they will be checked when the class is defined, and,
5148 on the other, default arguments are valid in things like:
5149 template <class T = double>
5150 struct S { template <class U> void f(U); };
5151 Here the default argument for `S' has no bearing on the
5152 declaration of `f'. */
5153 last_level_to_check = template_class_depth (current_class_type) + 1;
5154 else
5155 /* Check everything. */
5156 last_level_to_check = 0;
5158 for (parm_level = parms;
5159 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5160 parm_level = TREE_CHAIN (parm_level))
5162 tree inner_parms = TREE_VALUE (parm_level);
5163 int i;
5164 int ntparms;
5166 ntparms = TREE_VEC_LENGTH (inner_parms);
5167 for (i = 0; i < ntparms; ++i)
5169 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5170 continue;
5172 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5174 if (msg)
5176 no_errors = false;
5177 if (is_friend_decl == 2)
5178 return no_errors;
5180 error (msg, decl);
5181 msg = 0;
5184 /* Clear out the default argument so that we are not
5185 confused later. */
5186 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5190 /* At this point, if we're still interested in issuing messages,
5191 they must apply to classes surrounding the object declared. */
5192 if (msg)
5193 msg = G_("default argument for template parameter for class "
5194 "enclosing %qD");
5197 return no_errors;
5200 /* Worker for push_template_decl_real, called via
5201 for_each_template_parm. DATA is really an int, indicating the
5202 level of the parameters we are interested in. If T is a template
5203 parameter of that level, return nonzero. */
5205 static int
5206 template_parm_this_level_p (tree t, void* data)
5208 int this_level = *(int *)data;
5209 int level;
5211 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5212 level = TEMPLATE_PARM_LEVEL (t);
5213 else
5214 level = TEMPLATE_TYPE_LEVEL (t);
5215 return level == this_level;
5218 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5219 DATA is really an int, indicating the innermost outer level of parameters.
5220 If T is a template parameter of that level or further out, return
5221 nonzero. */
5223 static int
5224 template_parm_outer_level (tree t, void *data)
5226 int this_level = *(int *)data;
5227 int level;
5229 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5230 level = TEMPLATE_PARM_LEVEL (t);
5231 else
5232 level = TEMPLATE_TYPE_LEVEL (t);
5233 return level <= this_level;
5236 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5237 parameters given by current_template_args, or reuses a
5238 previously existing one, if appropriate. Returns the DECL, or an
5239 equivalent one, if it is replaced via a call to duplicate_decls.
5241 If IS_FRIEND is true, DECL is a friend declaration. */
5243 tree
5244 push_template_decl_real (tree decl, bool is_friend)
5246 tree tmpl;
5247 tree args;
5248 tree info;
5249 tree ctx;
5250 bool is_primary;
5251 bool is_partial;
5252 int new_template_p = 0;
5253 /* True if the template is a member template, in the sense of
5254 [temp.mem]. */
5255 bool member_template_p = false;
5257 if (decl == error_mark_node || !current_template_parms)
5258 return error_mark_node;
5260 /* See if this is a partial specialization. */
5261 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5262 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5263 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5264 || (VAR_P (decl)
5265 && DECL_LANG_SPECIFIC (decl)
5266 && DECL_TEMPLATE_SPECIALIZATION (decl)
5267 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5269 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5270 is_friend = true;
5272 if (is_friend)
5273 /* For a friend, we want the context of the friend function, not
5274 the type of which it is a friend. */
5275 ctx = CP_DECL_CONTEXT (decl);
5276 else if (CP_DECL_CONTEXT (decl)
5277 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5278 /* In the case of a virtual function, we want the class in which
5279 it is defined. */
5280 ctx = CP_DECL_CONTEXT (decl);
5281 else
5282 /* Otherwise, if we're currently defining some class, the DECL
5283 is assumed to be a member of the class. */
5284 ctx = current_scope ();
5286 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5287 ctx = NULL_TREE;
5289 if (!DECL_CONTEXT (decl))
5290 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5292 /* See if this is a primary template. */
5293 if (is_friend && ctx
5294 && uses_template_parms_level (ctx, processing_template_decl))
5295 /* A friend template that specifies a class context, i.e.
5296 template <typename T> friend void A<T>::f();
5297 is not primary. */
5298 is_primary = false;
5299 else if (TREE_CODE (decl) == TYPE_DECL
5300 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5301 is_primary = false;
5302 else
5303 is_primary = template_parm_scope_p ();
5305 if (is_primary)
5307 warning (OPT_Wtemplates, "template %qD declared", decl);
5309 if (DECL_CLASS_SCOPE_P (decl))
5310 member_template_p = true;
5311 if (TREE_CODE (decl) == TYPE_DECL
5312 && anon_aggrname_p (DECL_NAME (decl)))
5314 error ("template class without a name");
5315 return error_mark_node;
5317 else if (TREE_CODE (decl) == FUNCTION_DECL)
5319 if (member_template_p)
5321 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5322 error ("member template %qD may not have virt-specifiers", decl);
5324 if (DECL_DESTRUCTOR_P (decl))
5326 /* [temp.mem]
5328 A destructor shall not be a member template. */
5329 error ("destructor %qD declared as member template", decl);
5330 return error_mark_node;
5332 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5333 && (!prototype_p (TREE_TYPE (decl))
5334 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5335 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5336 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5337 == void_list_node)))
5339 /* [basic.stc.dynamic.allocation]
5341 An allocation function can be a function
5342 template. ... Template allocation functions shall
5343 have two or more parameters. */
5344 error ("invalid template declaration of %qD", decl);
5345 return error_mark_node;
5348 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5349 && CLASS_TYPE_P (TREE_TYPE (decl)))
5351 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5352 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5353 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5355 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5356 if (TREE_CODE (t) == TYPE_DECL)
5357 t = TREE_TYPE (t);
5358 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5359 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5362 else if (TREE_CODE (decl) == TYPE_DECL
5363 && TYPE_DECL_ALIAS_P (decl))
5364 /* alias-declaration */
5365 gcc_assert (!DECL_ARTIFICIAL (decl));
5366 else if (VAR_P (decl))
5367 /* C++14 variable template. */;
5368 else
5370 error ("template declaration of %q#D", decl);
5371 return error_mark_node;
5375 /* Check to see that the rules regarding the use of default
5376 arguments are not being violated. */
5377 check_default_tmpl_args (decl, current_template_parms,
5378 is_primary, is_partial, /*is_friend_decl=*/0);
5380 /* Ensure that there are no parameter packs in the type of this
5381 declaration that have not been expanded. */
5382 if (TREE_CODE (decl) == FUNCTION_DECL)
5384 /* Check each of the arguments individually to see if there are
5385 any bare parameter packs. */
5386 tree type = TREE_TYPE (decl);
5387 tree arg = DECL_ARGUMENTS (decl);
5388 tree argtype = TYPE_ARG_TYPES (type);
5390 while (arg && argtype)
5392 if (!DECL_PACK_P (arg)
5393 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5395 /* This is a PARM_DECL that contains unexpanded parameter
5396 packs. We have already complained about this in the
5397 check_for_bare_parameter_packs call, so just replace
5398 these types with ERROR_MARK_NODE. */
5399 TREE_TYPE (arg) = error_mark_node;
5400 TREE_VALUE (argtype) = error_mark_node;
5403 arg = DECL_CHAIN (arg);
5404 argtype = TREE_CHAIN (argtype);
5407 /* Check for bare parameter packs in the return type and the
5408 exception specifiers. */
5409 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5410 /* Errors were already issued, set return type to int
5411 as the frontend doesn't expect error_mark_node as
5412 the return type. */
5413 TREE_TYPE (type) = integer_type_node;
5414 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5415 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5417 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5418 && TYPE_DECL_ALIAS_P (decl))
5419 ? DECL_ORIGINAL_TYPE (decl)
5420 : TREE_TYPE (decl)))
5422 TREE_TYPE (decl) = error_mark_node;
5423 return error_mark_node;
5426 if (is_partial)
5427 return process_partial_specialization (decl);
5429 args = current_template_args ();
5431 if (!ctx
5432 || TREE_CODE (ctx) == FUNCTION_DECL
5433 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5434 || (TREE_CODE (decl) == TYPE_DECL
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5436 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5438 if (DECL_LANG_SPECIFIC (decl)
5439 && DECL_TEMPLATE_INFO (decl)
5440 && DECL_TI_TEMPLATE (decl))
5441 tmpl = DECL_TI_TEMPLATE (decl);
5442 /* If DECL is a TYPE_DECL for a class-template, then there won't
5443 be DECL_LANG_SPECIFIC. The information equivalent to
5444 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5445 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5446 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5447 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5449 /* Since a template declaration already existed for this
5450 class-type, we must be redeclaring it here. Make sure
5451 that the redeclaration is valid. */
5452 redeclare_class_template (TREE_TYPE (decl),
5453 current_template_parms,
5454 current_template_constraints ());
5455 /* We don't need to create a new TEMPLATE_DECL; just use the
5456 one we already had. */
5457 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5459 else
5461 tmpl = build_template_decl (decl, current_template_parms,
5462 member_template_p);
5463 new_template_p = 1;
5465 if (DECL_LANG_SPECIFIC (decl)
5466 && DECL_TEMPLATE_SPECIALIZATION (decl))
5468 /* A specialization of a member template of a template
5469 class. */
5470 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5471 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5472 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5476 else
5478 tree a, t, current, parms;
5479 int i;
5480 tree tinfo = get_template_info (decl);
5482 if (!tinfo)
5484 error ("template definition of non-template %q#D", decl);
5485 return error_mark_node;
5488 tmpl = TI_TEMPLATE (tinfo);
5490 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5491 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5492 && DECL_TEMPLATE_SPECIALIZATION (decl)
5493 && DECL_MEMBER_TEMPLATE_P (tmpl))
5495 tree new_tmpl;
5497 /* The declaration is a specialization of a member
5498 template, declared outside the class. Therefore, the
5499 innermost template arguments will be NULL, so we
5500 replace them with the arguments determined by the
5501 earlier call to check_explicit_specialization. */
5502 args = DECL_TI_ARGS (decl);
5504 new_tmpl
5505 = build_template_decl (decl, current_template_parms,
5506 member_template_p);
5507 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5508 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5509 DECL_TI_TEMPLATE (decl) = new_tmpl;
5510 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5511 DECL_TEMPLATE_INFO (new_tmpl)
5512 = build_template_info (tmpl, args);
5514 register_specialization (new_tmpl,
5515 most_general_template (tmpl),
5516 args,
5517 is_friend, 0);
5518 return decl;
5521 /* Make sure the template headers we got make sense. */
5523 parms = DECL_TEMPLATE_PARMS (tmpl);
5524 i = TMPL_PARMS_DEPTH (parms);
5525 if (TMPL_ARGS_DEPTH (args) != i)
5527 error ("expected %d levels of template parms for %q#D, got %d",
5528 i, decl, TMPL_ARGS_DEPTH (args));
5529 DECL_INTERFACE_KNOWN (decl) = 1;
5530 return error_mark_node;
5532 else
5533 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5535 a = TMPL_ARGS_LEVEL (args, i);
5536 t = INNERMOST_TEMPLATE_PARMS (parms);
5538 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5540 if (current == decl)
5541 error ("got %d template parameters for %q#D",
5542 TREE_VEC_LENGTH (a), decl);
5543 else
5544 error ("got %d template parameters for %q#T",
5545 TREE_VEC_LENGTH (a), current);
5546 error (" but %d required", TREE_VEC_LENGTH (t));
5547 /* Avoid crash in import_export_decl. */
5548 DECL_INTERFACE_KNOWN (decl) = 1;
5549 return error_mark_node;
5552 if (current == decl)
5553 current = ctx;
5554 else if (current == NULL_TREE)
5555 /* Can happen in erroneous input. */
5556 break;
5557 else
5558 current = get_containing_scope (current);
5561 /* Check that the parms are used in the appropriate qualifying scopes
5562 in the declarator. */
5563 if (!comp_template_args
5564 (TI_ARGS (tinfo),
5565 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5567 error ("template arguments to %qD do not match original"
5568 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5569 if (!uses_template_parms (TI_ARGS (tinfo)))
5570 inform (input_location, "use %<template<>%> for"
5571 " an explicit specialization");
5572 /* Avoid crash in import_export_decl. */
5573 DECL_INTERFACE_KNOWN (decl) = 1;
5574 return error_mark_node;
5578 DECL_TEMPLATE_RESULT (tmpl) = decl;
5579 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5581 /* Push template declarations for global functions and types. Note
5582 that we do not try to push a global template friend declared in a
5583 template class; such a thing may well depend on the template
5584 parameters of the class. */
5585 if (new_template_p && !ctx
5586 && !(is_friend && template_class_depth (current_class_type) > 0))
5588 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5589 if (tmpl == error_mark_node)
5590 return error_mark_node;
5592 /* Hide template friend classes that haven't been declared yet. */
5593 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5595 DECL_ANTICIPATED (tmpl) = 1;
5596 DECL_FRIEND_P (tmpl) = 1;
5600 if (is_primary)
5602 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5604 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5606 /* Give template template parms a DECL_CONTEXT of the template
5607 for which they are a parameter. */
5608 parms = INNERMOST_TEMPLATE_PARMS (parms);
5609 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5611 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5612 if (TREE_CODE (parm) == TEMPLATE_DECL)
5613 DECL_CONTEXT (parm) = tmpl;
5616 if (TREE_CODE (decl) == TYPE_DECL
5617 && TYPE_DECL_ALIAS_P (decl)
5618 && complex_alias_template_p (tmpl))
5619 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5622 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5623 back to its most general template. If TMPL is a specialization,
5624 ARGS may only have the innermost set of arguments. Add the missing
5625 argument levels if necessary. */
5626 if (DECL_TEMPLATE_INFO (tmpl))
5627 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5629 info = build_template_info (tmpl, args);
5631 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5632 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5633 else
5635 if (is_primary)
5636 retrofit_lang_decl (decl);
5637 if (DECL_LANG_SPECIFIC (decl))
5638 DECL_TEMPLATE_INFO (decl) = info;
5641 if (flag_implicit_templates
5642 && !is_friend
5643 && TREE_PUBLIC (decl)
5644 && VAR_OR_FUNCTION_DECL_P (decl))
5645 /* Set DECL_COMDAT on template instantiations; if we force
5646 them to be emitted by explicit instantiation or -frepo,
5647 mark_needed will tell cgraph to do the right thing. */
5648 DECL_COMDAT (decl) = true;
5650 return DECL_TEMPLATE_RESULT (tmpl);
5653 tree
5654 push_template_decl (tree decl)
5656 return push_template_decl_real (decl, false);
5659 /* FN is an inheriting constructor that inherits from the constructor
5660 template INHERITED; turn FN into a constructor template with a matching
5661 template header. */
5663 tree
5664 add_inherited_template_parms (tree fn, tree inherited)
5666 tree inner_parms
5667 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5668 inner_parms = copy_node (inner_parms);
5669 tree parms
5670 = tree_cons (size_int (processing_template_decl + 1),
5671 inner_parms, current_template_parms);
5672 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5673 tree args = template_parms_to_args (parms);
5674 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5675 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5676 DECL_TEMPLATE_RESULT (tmpl) = fn;
5677 DECL_ARTIFICIAL (tmpl) = true;
5678 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5679 return tmpl;
5682 /* Called when a class template TYPE is redeclared with the indicated
5683 template PARMS, e.g.:
5685 template <class T> struct S;
5686 template <class T> struct S {}; */
5688 bool
5689 redeclare_class_template (tree type, tree parms, tree cons)
5691 tree tmpl;
5692 tree tmpl_parms;
5693 int i;
5695 if (!TYPE_TEMPLATE_INFO (type))
5697 error ("%qT is not a template type", type);
5698 return false;
5701 tmpl = TYPE_TI_TEMPLATE (type);
5702 if (!PRIMARY_TEMPLATE_P (tmpl))
5703 /* The type is nested in some template class. Nothing to worry
5704 about here; there are no new template parameters for the nested
5705 type. */
5706 return true;
5708 if (!parms)
5710 error ("template specifiers not specified in declaration of %qD",
5711 tmpl);
5712 return false;
5715 parms = INNERMOST_TEMPLATE_PARMS (parms);
5716 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5718 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5720 error_n (input_location, TREE_VEC_LENGTH (parms),
5721 "redeclared with %d template parameter",
5722 "redeclared with %d template parameters",
5723 TREE_VEC_LENGTH (parms));
5724 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5725 "previous declaration %qD used %d template parameter",
5726 "previous declaration %qD used %d template parameters",
5727 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5728 return false;
5731 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5733 tree tmpl_parm;
5734 tree parm;
5735 tree tmpl_default;
5736 tree parm_default;
5738 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5739 || TREE_VEC_ELT (parms, i) == error_mark_node)
5740 continue;
5742 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5743 if (error_operand_p (tmpl_parm))
5744 return false;
5746 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5747 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5748 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5750 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5751 TEMPLATE_DECL. */
5752 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5753 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5754 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5755 || (TREE_CODE (tmpl_parm) != PARM_DECL
5756 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5757 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5758 || (TREE_CODE (tmpl_parm) == PARM_DECL
5759 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5760 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5762 error ("template parameter %q+#D", tmpl_parm);
5763 error ("redeclared here as %q#D", parm);
5764 return false;
5767 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5769 /* We have in [temp.param]:
5771 A template-parameter may not be given default arguments
5772 by two different declarations in the same scope. */
5773 error_at (input_location, "redefinition of default argument for %q#D", parm);
5774 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5775 "original definition appeared here");
5776 return false;
5779 if (parm_default != NULL_TREE)
5780 /* Update the previous template parameters (which are the ones
5781 that will really count) with the new default value. */
5782 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5783 else if (tmpl_default != NULL_TREE)
5784 /* Update the new parameters, too; they'll be used as the
5785 parameters for any members. */
5786 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5788 /* Give each template template parm in this redeclaration a
5789 DECL_CONTEXT of the template for which they are a parameter. */
5790 if (TREE_CODE (parm) == TEMPLATE_DECL)
5792 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5793 DECL_CONTEXT (parm) = tmpl;
5796 if (TREE_CODE (parm) == TYPE_DECL)
5797 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5800 // Cannot redeclare a class template with a different set of constraints.
5801 if (!equivalent_constraints (get_constraints (tmpl), cons))
5803 error_at (input_location, "redeclaration %q#D with different "
5804 "constraints", tmpl);
5805 inform (DECL_SOURCE_LOCATION (tmpl),
5806 "original declaration appeared here");
5809 return true;
5812 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5813 to be used when the caller has already checked
5814 (processing_template_decl
5815 && !instantiation_dependent_expression_p (expr)
5816 && potential_constant_expression (expr))
5817 and cleared processing_template_decl. */
5819 tree
5820 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5822 return tsubst_copy_and_build (expr,
5823 /*args=*/NULL_TREE,
5824 complain,
5825 /*in_decl=*/NULL_TREE,
5826 /*function_p=*/false,
5827 /*integral_constant_expression_p=*/true);
5830 /* Simplify EXPR if it is a non-dependent expression. Returns the
5831 (possibly simplified) expression. */
5833 tree
5834 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5836 if (expr == NULL_TREE)
5837 return NULL_TREE;
5839 /* If we're in a template, but EXPR isn't value dependent, simplify
5840 it. We're supposed to treat:
5842 template <typename T> void f(T[1 + 1]);
5843 template <typename T> void f(T[2]);
5845 as two declarations of the same function, for example. */
5846 if (processing_template_decl
5847 && is_nondependent_constant_expression (expr))
5849 processing_template_decl_sentinel s;
5850 expr = instantiate_non_dependent_expr_internal (expr, complain);
5852 return expr;
5855 tree
5856 instantiate_non_dependent_expr (tree expr)
5858 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5861 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5862 an uninstantiated expression. */
5864 tree
5865 instantiate_non_dependent_or_null (tree expr)
5867 if (expr == NULL_TREE)
5868 return NULL_TREE;
5869 if (processing_template_decl)
5871 if (!is_nondependent_constant_expression (expr))
5872 expr = NULL_TREE;
5873 else
5875 processing_template_decl_sentinel s;
5876 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5879 return expr;
5882 /* True iff T is a specialization of a variable template. */
5884 bool
5885 variable_template_specialization_p (tree t)
5887 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5888 return false;
5889 tree tmpl = DECL_TI_TEMPLATE (t);
5890 return variable_template_p (tmpl);
5893 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5894 template declaration, or a TYPE_DECL for an alias declaration. */
5896 bool
5897 alias_type_or_template_p (tree t)
5899 if (t == NULL_TREE)
5900 return false;
5901 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5902 || (TYPE_P (t)
5903 && TYPE_NAME (t)
5904 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5905 || DECL_ALIAS_TEMPLATE_P (t));
5908 /* Return TRUE iff T is a specialization of an alias template. */
5910 bool
5911 alias_template_specialization_p (const_tree t)
5913 /* It's an alias template specialization if it's an alias and its
5914 TYPE_NAME is a specialization of a primary template. */
5915 if (TYPE_ALIAS_P (t))
5916 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5917 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5919 return false;
5922 /* An alias template is complex from a SFINAE perspective if a template-id
5923 using that alias can be ill-formed when the expansion is not, as with
5924 the void_t template. We determine this by checking whether the
5925 expansion for the alias template uses all its template parameters. */
5927 struct uses_all_template_parms_data
5929 int level;
5930 bool *seen;
5933 static int
5934 uses_all_template_parms_r (tree t, void *data_)
5936 struct uses_all_template_parms_data &data
5937 = *(struct uses_all_template_parms_data*)data_;
5938 tree idx = get_template_parm_index (t);
5940 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5941 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5942 return 0;
5945 static bool
5946 complex_alias_template_p (const_tree tmpl)
5948 struct uses_all_template_parms_data data;
5949 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5950 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5951 data.level = TMPL_PARMS_DEPTH (parms);
5952 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5953 data.seen = XALLOCAVEC (bool, len);
5954 for (int i = 0; i < len; ++i)
5955 data.seen[i] = false;
5957 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5958 for (int i = 0; i < len; ++i)
5959 if (!data.seen[i])
5960 return true;
5961 return false;
5964 /* Return TRUE iff T is a specialization of a complex alias template with
5965 dependent template-arguments. */
5967 bool
5968 dependent_alias_template_spec_p (const_tree t)
5970 if (!alias_template_specialization_p (t))
5971 return false;
5973 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5974 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5975 return false;
5977 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5978 if (!any_dependent_template_arguments_p (args))
5979 return false;
5981 return true;
5984 /* Return the number of innermost template parameters in TMPL. */
5986 static int
5987 num_innermost_template_parms (tree tmpl)
5989 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5990 return TREE_VEC_LENGTH (parms);
5993 /* Return either TMPL or another template that it is equivalent to under DR
5994 1286: An alias that just changes the name of a template is equivalent to
5995 the other template. */
5997 static tree
5998 get_underlying_template (tree tmpl)
6000 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6001 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6003 /* Determine if the alias is equivalent to an underlying template. */
6004 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6005 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6006 if (!tinfo)
6007 break;
6009 tree underlying = TI_TEMPLATE (tinfo);
6010 if (!PRIMARY_TEMPLATE_P (underlying)
6011 || (num_innermost_template_parms (tmpl)
6012 != num_innermost_template_parms (underlying)))
6013 break;
6015 tree alias_args = INNERMOST_TEMPLATE_ARGS
6016 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6017 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6018 break;
6020 /* Alias is equivalent. Strip it and repeat. */
6021 tmpl = underlying;
6024 return tmpl;
6027 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6028 must be a reference-to-function or a pointer-to-function type, as specified
6029 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6030 and check that the resulting function has external linkage. */
6032 static tree
6033 convert_nontype_argument_function (tree type, tree expr,
6034 tsubst_flags_t complain)
6036 tree fns = expr;
6037 tree fn, fn_no_ptr;
6038 linkage_kind linkage;
6040 fn = instantiate_type (type, fns, tf_none);
6041 if (fn == error_mark_node)
6042 return error_mark_node;
6044 if (value_dependent_expression_p (fn))
6045 goto accept;
6047 fn_no_ptr = strip_fnptr_conv (fn);
6048 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6049 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6050 if (BASELINK_P (fn_no_ptr))
6051 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6053 /* [temp.arg.nontype]/1
6055 A template-argument for a non-type, non-template template-parameter
6056 shall be one of:
6057 [...]
6058 -- the address of an object or function with external [C++11: or
6059 internal] linkage. */
6061 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6063 if (complain & tf_error)
6065 error ("%qE is not a valid template argument for type %qT",
6066 expr, type);
6067 if (TYPE_PTR_P (type))
6068 inform (input_location, "it must be the address of a function "
6069 "with external linkage");
6070 else
6071 inform (input_location, "it must be the name of a function with "
6072 "external linkage");
6074 return NULL_TREE;
6077 linkage = decl_linkage (fn_no_ptr);
6078 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6080 if (complain & tf_error)
6082 if (cxx_dialect >= cxx11)
6083 error ("%qE is not a valid template argument for type %qT "
6084 "because %qD has no linkage",
6085 expr, type, fn_no_ptr);
6086 else
6087 error ("%qE is not a valid template argument for type %qT "
6088 "because %qD does not have external linkage",
6089 expr, type, fn_no_ptr);
6091 return NULL_TREE;
6094 accept:
6095 if (TREE_CODE (type) == REFERENCE_TYPE)
6096 fn = build_address (fn);
6097 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6098 fn = build_nop (type, fn);
6100 return fn;
6103 /* Subroutine of convert_nontype_argument.
6104 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6105 Emit an error otherwise. */
6107 static bool
6108 check_valid_ptrmem_cst_expr (tree type, tree expr,
6109 tsubst_flags_t complain)
6111 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6112 tree orig_expr = expr;
6113 STRIP_NOPS (expr);
6114 if (null_ptr_cst_p (expr))
6115 return true;
6116 if (TREE_CODE (expr) == PTRMEM_CST
6117 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6118 PTRMEM_CST_CLASS (expr)))
6119 return true;
6120 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6121 return true;
6122 if (processing_template_decl
6123 && TREE_CODE (expr) == ADDR_EXPR
6124 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6125 return true;
6126 if (complain & tf_error)
6128 error_at (loc, "%qE is not a valid template argument for type %qT",
6129 orig_expr, type);
6130 if (TREE_CODE (expr) != PTRMEM_CST)
6131 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6132 else
6133 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6135 return false;
6138 /* Returns TRUE iff the address of OP is value-dependent.
6140 14.6.2.4 [temp.dep.temp]:
6141 A non-integral non-type template-argument is dependent if its type is
6142 dependent or it has either of the following forms
6143 qualified-id
6144 & qualified-id
6145 and contains a nested-name-specifier which specifies a class-name that
6146 names a dependent type.
6148 We generalize this to just say that the address of a member of a
6149 dependent class is value-dependent; the above doesn't cover the
6150 address of a static data member named with an unqualified-id. */
6152 static bool
6153 has_value_dependent_address (tree op)
6155 /* We could use get_inner_reference here, but there's no need;
6156 this is only relevant for template non-type arguments, which
6157 can only be expressed as &id-expression. */
6158 if (DECL_P (op))
6160 tree ctx = CP_DECL_CONTEXT (op);
6161 if (TYPE_P (ctx) && dependent_type_p (ctx))
6162 return true;
6165 return false;
6168 /* The next set of functions are used for providing helpful explanatory
6169 diagnostics for failed overload resolution. Their messages should be
6170 indented by two spaces for consistency with the messages in
6171 call.c */
6173 static int
6174 unify_success (bool /*explain_p*/)
6176 return 0;
6179 /* Other failure functions should call this one, to provide a single function
6180 for setting a breakpoint on. */
6182 static int
6183 unify_invalid (bool /*explain_p*/)
6185 return 1;
6188 static int
6189 unify_parameter_deduction_failure (bool explain_p, tree parm)
6191 if (explain_p)
6192 inform (input_location,
6193 " couldn't deduce template parameter %qD", parm);
6194 return unify_invalid (explain_p);
6197 static int
6198 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6200 if (explain_p)
6201 inform (input_location,
6202 " types %qT and %qT have incompatible cv-qualifiers",
6203 parm, arg);
6204 return unify_invalid (explain_p);
6207 static int
6208 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6210 if (explain_p)
6211 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6212 return unify_invalid (explain_p);
6215 static int
6216 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6218 if (explain_p)
6219 inform (input_location,
6220 " template parameter %qD is not a parameter pack, but "
6221 "argument %qD is",
6222 parm, arg);
6223 return unify_invalid (explain_p);
6226 static int
6227 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6229 if (explain_p)
6230 inform (input_location,
6231 " template argument %qE does not match "
6232 "pointer-to-member constant %qE",
6233 arg, parm);
6234 return unify_invalid (explain_p);
6237 static int
6238 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6240 if (explain_p)
6241 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6242 return unify_invalid (explain_p);
6245 static int
6246 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6248 if (explain_p)
6249 inform (input_location,
6250 " inconsistent parameter pack deduction with %qT and %qT",
6251 old_arg, new_arg);
6252 return unify_invalid (explain_p);
6255 static int
6256 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6258 if (explain_p)
6260 if (TYPE_P (parm))
6261 inform (input_location,
6262 " deduced conflicting types for parameter %qT (%qT and %qT)",
6263 parm, first, second);
6264 else
6265 inform (input_location,
6266 " deduced conflicting values for non-type parameter "
6267 "%qE (%qE and %qE)", parm, first, second);
6269 return unify_invalid (explain_p);
6272 static int
6273 unify_vla_arg (bool explain_p, tree arg)
6275 if (explain_p)
6276 inform (input_location,
6277 " variable-sized array type %qT is not "
6278 "a valid template argument",
6279 arg);
6280 return unify_invalid (explain_p);
6283 static int
6284 unify_method_type_error (bool explain_p, tree arg)
6286 if (explain_p)
6287 inform (input_location,
6288 " member function type %qT is not a valid template argument",
6289 arg);
6290 return unify_invalid (explain_p);
6293 static int
6294 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6296 if (explain_p)
6298 if (least_p)
6299 inform_n (input_location, wanted,
6300 " candidate expects at least %d argument, %d provided",
6301 " candidate expects at least %d arguments, %d provided",
6302 wanted, have);
6303 else
6304 inform_n (input_location, wanted,
6305 " candidate expects %d argument, %d provided",
6306 " candidate expects %d arguments, %d provided",
6307 wanted, have);
6309 return unify_invalid (explain_p);
6312 static int
6313 unify_too_many_arguments (bool explain_p, int have, int wanted)
6315 return unify_arity (explain_p, have, wanted);
6318 static int
6319 unify_too_few_arguments (bool explain_p, int have, int wanted,
6320 bool least_p = false)
6322 return unify_arity (explain_p, have, wanted, least_p);
6325 static int
6326 unify_arg_conversion (bool explain_p, tree to_type,
6327 tree from_type, tree arg)
6329 if (explain_p)
6330 inform (EXPR_LOC_OR_LOC (arg, input_location),
6331 " cannot convert %qE (type %qT) to type %qT",
6332 arg, from_type, to_type);
6333 return unify_invalid (explain_p);
6336 static int
6337 unify_no_common_base (bool explain_p, enum template_base_result r,
6338 tree parm, tree arg)
6340 if (explain_p)
6341 switch (r)
6343 case tbr_ambiguous_baseclass:
6344 inform (input_location, " %qT is an ambiguous base class of %qT",
6345 parm, arg);
6346 break;
6347 default:
6348 inform (input_location, " %qT is not derived from %qT", arg, parm);
6349 break;
6351 return unify_invalid (explain_p);
6354 static int
6355 unify_inconsistent_template_template_parameters (bool explain_p)
6357 if (explain_p)
6358 inform (input_location,
6359 " template parameters of a template template argument are "
6360 "inconsistent with other deduced template arguments");
6361 return unify_invalid (explain_p);
6364 static int
6365 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6367 if (explain_p)
6368 inform (input_location,
6369 " can't deduce a template for %qT from non-template type %qT",
6370 parm, arg);
6371 return unify_invalid (explain_p);
6374 static int
6375 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6377 if (explain_p)
6378 inform (input_location,
6379 " template argument %qE does not match %qE", arg, parm);
6380 return unify_invalid (explain_p);
6383 /* Attempt to convert the non-type template parameter EXPR to the
6384 indicated TYPE. If the conversion is successful, return the
6385 converted value. If the conversion is unsuccessful, return
6386 NULL_TREE if we issued an error message, or error_mark_node if we
6387 did not. We issue error messages for out-and-out bad template
6388 parameters, but not simply because the conversion failed, since we
6389 might be just trying to do argument deduction. Both TYPE and EXPR
6390 must be non-dependent.
6392 The conversion follows the special rules described in
6393 [temp.arg.nontype], and it is much more strict than an implicit
6394 conversion.
6396 This function is called twice for each template argument (see
6397 lookup_template_class for a more accurate description of this
6398 problem). This means that we need to handle expressions which
6399 are not valid in a C++ source, but can be created from the
6400 first call (for instance, casts to perform conversions). These
6401 hacks can go away after we fix the double coercion problem. */
6403 static tree
6404 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6406 tree expr_type;
6407 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6408 tree orig_expr = expr;
6410 /* Detect immediately string literals as invalid non-type argument.
6411 This special-case is not needed for correctness (we would easily
6412 catch this later), but only to provide better diagnostic for this
6413 common user mistake. As suggested by DR 100, we do not mention
6414 linkage issues in the diagnostic as this is not the point. */
6415 /* FIXME we're making this OK. */
6416 if (TREE_CODE (expr) == STRING_CST)
6418 if (complain & tf_error)
6419 error ("%qE is not a valid template argument for type %qT "
6420 "because string literals can never be used in this context",
6421 expr, type);
6422 return NULL_TREE;
6425 /* Add the ADDR_EXPR now for the benefit of
6426 value_dependent_expression_p. */
6427 if (TYPE_PTROBV_P (type)
6428 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6430 expr = decay_conversion (expr, complain);
6431 if (expr == error_mark_node)
6432 return error_mark_node;
6435 /* If we are in a template, EXPR may be non-dependent, but still
6436 have a syntactic, rather than semantic, form. For example, EXPR
6437 might be a SCOPE_REF, rather than the VAR_DECL to which the
6438 SCOPE_REF refers. Preserving the qualifying scope is necessary
6439 so that access checking can be performed when the template is
6440 instantiated -- but here we need the resolved form so that we can
6441 convert the argument. */
6442 bool non_dep = false;
6443 if (TYPE_REF_OBJ_P (type)
6444 && has_value_dependent_address (expr))
6445 /* If we want the address and it's value-dependent, don't fold. */;
6446 else if (processing_template_decl
6447 && is_nondependent_constant_expression (expr))
6448 non_dep = true;
6449 if (error_operand_p (expr))
6450 return error_mark_node;
6451 expr_type = TREE_TYPE (expr);
6453 /* If the argument is non-dependent, perform any conversions in
6454 non-dependent context as well. */
6455 processing_template_decl_sentinel s (non_dep);
6456 if (non_dep)
6457 expr = instantiate_non_dependent_expr_internal (expr, complain);
6459 if (value_dependent_expression_p (expr))
6460 expr = canonicalize_expr_argument (expr, complain);
6462 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6463 to a non-type argument of "nullptr". */
6464 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6465 expr = fold_simple (convert (type, expr));
6467 /* In C++11, integral or enumeration non-type template arguments can be
6468 arbitrary constant expressions. Pointer and pointer to
6469 member arguments can be general constant expressions that evaluate
6470 to a null value, but otherwise still need to be of a specific form. */
6471 if (cxx_dialect >= cxx11)
6473 if (TREE_CODE (expr) == PTRMEM_CST)
6474 /* A PTRMEM_CST is already constant, and a valid template
6475 argument for a parameter of pointer to member type, we just want
6476 to leave it in that form rather than lower it to a
6477 CONSTRUCTOR. */;
6478 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6479 || cxx_dialect >= cxx17)
6481 /* C++17: A template-argument for a non-type template-parameter shall
6482 be a converted constant expression (8.20) of the type of the
6483 template-parameter. */
6484 expr = build_converted_constant_expr (type, expr, complain);
6485 if (expr == error_mark_node)
6486 return error_mark_node;
6487 expr = maybe_constant_value (expr);
6488 expr = convert_from_reference (expr);
6490 else if (TYPE_PTR_OR_PTRMEM_P (type))
6492 tree folded = maybe_constant_value (expr);
6493 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6494 : null_member_pointer_value_p (folded))
6495 expr = folded;
6499 if (TREE_CODE (type) == REFERENCE_TYPE)
6500 expr = mark_lvalue_use (expr);
6501 else
6502 expr = mark_rvalue_use (expr);
6504 /* HACK: Due to double coercion, we can get a
6505 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6506 which is the tree that we built on the first call (see
6507 below when coercing to reference to object or to reference to
6508 function). We just strip everything and get to the arg.
6509 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6510 for examples. */
6511 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6513 tree probe_type, probe = expr;
6514 if (REFERENCE_REF_P (probe))
6515 probe = TREE_OPERAND (probe, 0);
6516 probe_type = TREE_TYPE (probe);
6517 if (TREE_CODE (probe) == NOP_EXPR)
6519 /* ??? Maybe we could use convert_from_reference here, but we
6520 would need to relax its constraints because the NOP_EXPR
6521 could actually change the type to something more cv-qualified,
6522 and this is not folded by convert_from_reference. */
6523 tree addr = TREE_OPERAND (probe, 0);
6524 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6525 && TREE_CODE (addr) == ADDR_EXPR
6526 && TYPE_PTR_P (TREE_TYPE (addr))
6527 && (same_type_ignoring_top_level_qualifiers_p
6528 (TREE_TYPE (probe_type),
6529 TREE_TYPE (TREE_TYPE (addr)))))
6531 expr = TREE_OPERAND (addr, 0);
6532 expr_type = TREE_TYPE (probe_type);
6537 /* [temp.arg.nontype]/5, bullet 1
6539 For a non-type template-parameter of integral or enumeration type,
6540 integral promotions (_conv.prom_) and integral conversions
6541 (_conv.integral_) are applied. */
6542 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6544 if (cxx_dialect < cxx11)
6546 tree t = build_converted_constant_expr (type, expr, complain);
6547 t = maybe_constant_value (t);
6548 if (t != error_mark_node)
6549 expr = t;
6552 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6553 return error_mark_node;
6555 /* Notice that there are constant expressions like '4 % 0' which
6556 do not fold into integer constants. */
6557 if (TREE_CODE (expr) != INTEGER_CST
6558 && !value_dependent_expression_p (expr))
6560 if (complain & tf_error)
6562 int errs = errorcount, warns = warningcount + werrorcount;
6563 if (!require_potential_constant_expression (expr))
6564 expr = error_mark_node;
6565 else
6566 expr = cxx_constant_value (expr);
6567 if (errorcount > errs || warningcount + werrorcount > warns)
6568 inform (loc, "in template argument for type %qT ", type);
6569 if (expr == error_mark_node)
6570 return NULL_TREE;
6571 /* else cxx_constant_value complained but gave us
6572 a real constant, so go ahead. */
6573 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6575 else
6576 return NULL_TREE;
6579 /* Avoid typedef problems. */
6580 if (TREE_TYPE (expr) != type)
6581 expr = fold_convert (type, expr);
6583 /* [temp.arg.nontype]/5, bullet 2
6585 For a non-type template-parameter of type pointer to object,
6586 qualification conversions (_conv.qual_) and the array-to-pointer
6587 conversion (_conv.array_) are applied. */
6588 else if (TYPE_PTROBV_P (type))
6590 tree decayed = expr;
6592 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6593 decay_conversion or an explicit cast. If it's a problematic cast,
6594 we'll complain about it below. */
6595 if (TREE_CODE (expr) == NOP_EXPR)
6597 tree probe = expr;
6598 STRIP_NOPS (probe);
6599 if (TREE_CODE (probe) == ADDR_EXPR
6600 && TYPE_PTR_P (TREE_TYPE (probe)))
6602 expr = probe;
6603 expr_type = TREE_TYPE (expr);
6607 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6609 A template-argument for a non-type, non-template template-parameter
6610 shall be one of: [...]
6612 -- the name of a non-type template-parameter;
6613 -- the address of an object or function with external linkage, [...]
6614 expressed as "& id-expression" where the & is optional if the name
6615 refers to a function or array, or if the corresponding
6616 template-parameter is a reference.
6618 Here, we do not care about functions, as they are invalid anyway
6619 for a parameter of type pointer-to-object. */
6621 if (value_dependent_expression_p (expr))
6622 /* Non-type template parameters are OK. */
6624 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6625 /* Null pointer values are OK in C++11. */;
6626 else if (TREE_CODE (expr) != ADDR_EXPR)
6628 if (VAR_P (expr))
6630 if (complain & tf_error)
6631 error ("%qD is not a valid template argument "
6632 "because %qD is a variable, not the address of "
6633 "a variable", orig_expr, expr);
6634 return NULL_TREE;
6636 if (POINTER_TYPE_P (expr_type))
6638 if (complain & tf_error)
6639 error ("%qE is not a valid template argument for %qT "
6640 "because it is not the address of a variable",
6641 orig_expr, type);
6642 return NULL_TREE;
6644 /* Other values, like integer constants, might be valid
6645 non-type arguments of some other type. */
6646 return error_mark_node;
6648 else
6650 tree decl = TREE_OPERAND (expr, 0);
6652 if (!VAR_P (decl))
6654 if (complain & tf_error)
6655 error ("%qE is not a valid template argument of type %qT "
6656 "because %qE is not a variable", orig_expr, type, decl);
6657 return NULL_TREE;
6659 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6661 if (complain & tf_error)
6662 error ("%qE is not a valid template argument of type %qT "
6663 "because %qD does not have external linkage",
6664 orig_expr, type, decl);
6665 return NULL_TREE;
6667 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6668 && decl_linkage (decl) == lk_none)
6670 if (complain & tf_error)
6671 error ("%qE is not a valid template argument of type %qT "
6672 "because %qD has no linkage", orig_expr, type, decl);
6673 return NULL_TREE;
6675 /* C++17: For a non-type template-parameter of reference or pointer
6676 type, the value of the constant expression shall not refer to (or
6677 for a pointer type, shall not be the address of):
6678 * a subobject (4.5),
6679 * a temporary object (15.2),
6680 * a string literal (5.13.5),
6681 * the result of a typeid expression (8.2.8), or
6682 * a predefined __func__ variable (11.4.1). */
6683 else if (DECL_ARTIFICIAL (decl))
6685 if (complain & tf_error)
6686 error ("the address of %qD is not a valid template argument",
6687 decl);
6688 return NULL_TREE;
6690 else if (!same_type_ignoring_top_level_qualifiers_p
6691 (strip_array_types (TREE_TYPE (type)),
6692 strip_array_types (TREE_TYPE (decl))))
6694 if (complain & tf_error)
6695 error ("the address of the %qT subobject of %qD is not a "
6696 "valid template argument", TREE_TYPE (type), decl);
6697 return NULL_TREE;
6699 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6701 if (complain & tf_error)
6702 error ("the address of %qD is not a valid template argument "
6703 "because it does not have static storage duration",
6704 decl);
6705 return NULL_TREE;
6709 expr = decayed;
6711 expr = perform_qualification_conversions (type, expr);
6712 if (expr == error_mark_node)
6713 return error_mark_node;
6715 /* [temp.arg.nontype]/5, bullet 3
6717 For a non-type template-parameter of type reference to object, no
6718 conversions apply. The type referred to by the reference may be more
6719 cv-qualified than the (otherwise identical) type of the
6720 template-argument. The template-parameter is bound directly to the
6721 template-argument, which must be an lvalue. */
6722 else if (TYPE_REF_OBJ_P (type))
6724 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6725 expr_type))
6726 return error_mark_node;
6728 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6730 if (complain & tf_error)
6731 error ("%qE is not a valid template argument for type %qT "
6732 "because of conflicts in cv-qualification", expr, type);
6733 return NULL_TREE;
6736 if (!lvalue_p (expr))
6738 if (complain & tf_error)
6739 error ("%qE is not a valid template argument for type %qT "
6740 "because it is not an lvalue", expr, type);
6741 return NULL_TREE;
6744 /* [temp.arg.nontype]/1
6746 A template-argument for a non-type, non-template template-parameter
6747 shall be one of: [...]
6749 -- the address of an object or function with external linkage. */
6750 if (INDIRECT_REF_P (expr)
6751 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6753 expr = TREE_OPERAND (expr, 0);
6754 if (DECL_P (expr))
6756 if (complain & tf_error)
6757 error ("%q#D is not a valid template argument for type %qT "
6758 "because a reference variable does not have a constant "
6759 "address", expr, type);
6760 return NULL_TREE;
6764 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6765 && value_dependent_expression_p (expr))
6766 /* OK, dependent reference. We don't want to ask whether a DECL is
6767 itself value-dependent, since what we want here is its address. */;
6768 else
6770 if (!DECL_P (expr))
6772 if (complain & tf_error)
6773 error ("%qE is not a valid template argument for type %qT "
6774 "because it is not an object with linkage",
6775 expr, type);
6776 return NULL_TREE;
6779 /* DR 1155 allows internal linkage in C++11 and up. */
6780 linkage_kind linkage = decl_linkage (expr);
6781 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6783 if (complain & tf_error)
6784 error ("%qE is not a valid template argument for type %qT "
6785 "because object %qD does not have linkage",
6786 expr, type, expr);
6787 return NULL_TREE;
6790 expr = build_address (expr);
6793 if (!same_type_p (type, TREE_TYPE (expr)))
6794 expr = build_nop (type, expr);
6796 /* [temp.arg.nontype]/5, bullet 4
6798 For a non-type template-parameter of type pointer to function, only
6799 the function-to-pointer conversion (_conv.func_) is applied. If the
6800 template-argument represents a set of overloaded functions (or a
6801 pointer to such), the matching function is selected from the set
6802 (_over.over_). */
6803 else if (TYPE_PTRFN_P (type))
6805 /* If the argument is a template-id, we might not have enough
6806 context information to decay the pointer. */
6807 if (!type_unknown_p (expr_type))
6809 expr = decay_conversion (expr, complain);
6810 if (expr == error_mark_node)
6811 return error_mark_node;
6814 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6815 /* Null pointer values are OK in C++11. */
6816 return perform_qualification_conversions (type, expr);
6818 expr = convert_nontype_argument_function (type, expr, complain);
6819 if (!expr || expr == error_mark_node)
6820 return expr;
6822 /* [temp.arg.nontype]/5, bullet 5
6824 For a non-type template-parameter of type reference to function, no
6825 conversions apply. If the template-argument represents a set of
6826 overloaded functions, the matching function is selected from the set
6827 (_over.over_). */
6828 else if (TYPE_REFFN_P (type))
6830 if (TREE_CODE (expr) == ADDR_EXPR)
6832 if (complain & tf_error)
6834 error ("%qE is not a valid template argument for type %qT "
6835 "because it is a pointer", expr, type);
6836 inform (input_location, "try using %qE instead",
6837 TREE_OPERAND (expr, 0));
6839 return NULL_TREE;
6842 expr = convert_nontype_argument_function (type, expr, complain);
6843 if (!expr || expr == error_mark_node)
6844 return expr;
6846 /* [temp.arg.nontype]/5, bullet 6
6848 For a non-type template-parameter of type pointer to member function,
6849 no conversions apply. If the template-argument represents a set of
6850 overloaded member functions, the matching member function is selected
6851 from the set (_over.over_). */
6852 else if (TYPE_PTRMEMFUNC_P (type))
6854 expr = instantiate_type (type, expr, tf_none);
6855 if (expr == error_mark_node)
6856 return error_mark_node;
6858 /* [temp.arg.nontype] bullet 1 says the pointer to member
6859 expression must be a pointer-to-member constant. */
6860 if (!value_dependent_expression_p (expr)
6861 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6862 return NULL_TREE;
6864 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6865 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6866 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6867 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6869 /* [temp.arg.nontype]/5, bullet 7
6871 For a non-type template-parameter of type pointer to data member,
6872 qualification conversions (_conv.qual_) are applied. */
6873 else if (TYPE_PTRDATAMEM_P (type))
6875 /* [temp.arg.nontype] bullet 1 says the pointer to member
6876 expression must be a pointer-to-member constant. */
6877 if (!value_dependent_expression_p (expr)
6878 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6879 return NULL_TREE;
6881 expr = perform_qualification_conversions (type, expr);
6882 if (expr == error_mark_node)
6883 return expr;
6885 else if (NULLPTR_TYPE_P (type))
6887 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6889 if (complain & tf_error)
6890 error ("%qE is not a valid template argument for type %qT "
6891 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6892 return NULL_TREE;
6894 return expr;
6896 /* A template non-type parameter must be one of the above. */
6897 else
6898 gcc_unreachable ();
6900 /* Sanity check: did we actually convert the argument to the
6901 right type? */
6902 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6903 (type, TREE_TYPE (expr)));
6904 return convert_from_reference (expr);
6907 /* Subroutine of coerce_template_template_parms, which returns 1 if
6908 PARM_PARM and ARG_PARM match using the rule for the template
6909 parameters of template template parameters. Both PARM and ARG are
6910 template parameters; the rest of the arguments are the same as for
6911 coerce_template_template_parms.
6913 static int
6914 coerce_template_template_parm (tree parm,
6915 tree arg,
6916 tsubst_flags_t complain,
6917 tree in_decl,
6918 tree outer_args)
6920 if (arg == NULL_TREE || error_operand_p (arg)
6921 || parm == NULL_TREE || error_operand_p (parm))
6922 return 0;
6924 if (TREE_CODE (arg) != TREE_CODE (parm))
6925 return 0;
6927 switch (TREE_CODE (parm))
6929 case TEMPLATE_DECL:
6930 /* We encounter instantiations of templates like
6931 template <template <template <class> class> class TT>
6932 class C; */
6934 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6935 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6937 if (!coerce_template_template_parms
6938 (parmparm, argparm, complain, in_decl, outer_args))
6939 return 0;
6941 /* Fall through. */
6943 case TYPE_DECL:
6944 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6945 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6946 /* Argument is a parameter pack but parameter is not. */
6947 return 0;
6948 break;
6950 case PARM_DECL:
6951 /* The tsubst call is used to handle cases such as
6953 template <int> class C {};
6954 template <class T, template <T> class TT> class D {};
6955 D<int, C> d;
6957 i.e. the parameter list of TT depends on earlier parameters. */
6958 if (!uses_template_parms (TREE_TYPE (arg)))
6960 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6961 if (!uses_template_parms (t)
6962 && !same_type_p (t, TREE_TYPE (arg)))
6963 return 0;
6966 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6967 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6968 /* Argument is a parameter pack but parameter is not. */
6969 return 0;
6971 break;
6973 default:
6974 gcc_unreachable ();
6977 return 1;
6980 /* Coerce template argument list ARGLIST for use with template
6981 template-parameter TEMPL. */
6983 static tree
6984 coerce_template_args_for_ttp (tree templ, tree arglist,
6985 tsubst_flags_t complain)
6987 /* Consider an example where a template template parameter declared as
6989 template <class T, class U = std::allocator<T> > class TT
6991 The template parameter level of T and U are one level larger than
6992 of TT. To proper process the default argument of U, say when an
6993 instantiation `TT<int>' is seen, we need to build the full
6994 arguments containing {int} as the innermost level. Outer levels,
6995 available when not appearing as default template argument, can be
6996 obtained from the arguments of the enclosing template.
6998 Suppose that TT is later substituted with std::vector. The above
6999 instantiation is `TT<int, std::allocator<T> >' with TT at
7000 level 1, and T at level 2, while the template arguments at level 1
7001 becomes {std::vector} and the inner level 2 is {int}. */
7003 tree outer = DECL_CONTEXT (templ);
7004 if (outer)
7006 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7007 /* We want arguments for the partial specialization, not arguments for
7008 the primary template. */
7009 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7010 else
7011 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7013 else if (current_template_parms)
7015 /* This is an argument of the current template, so we haven't set
7016 DECL_CONTEXT yet. */
7017 tree relevant_template_parms;
7019 /* Parameter levels that are greater than the level of the given
7020 template template parm are irrelevant. */
7021 relevant_template_parms = current_template_parms;
7022 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7023 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7024 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7026 outer = template_parms_to_args (relevant_template_parms);
7029 if (outer)
7030 arglist = add_to_template_args (outer, arglist);
7032 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7033 return coerce_template_parms (parmlist, arglist, templ,
7034 complain,
7035 /*require_all_args=*/true,
7036 /*use_default_args=*/true);
7039 /* A cache of template template parameters with match-all default
7040 arguments. */
7041 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7042 static void
7043 store_defaulted_ttp (tree v, tree t)
7045 if (!defaulted_ttp_cache)
7046 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7047 defaulted_ttp_cache->put (v, t);
7049 static tree
7050 lookup_defaulted_ttp (tree v)
7052 if (defaulted_ttp_cache)
7053 if (tree *p = defaulted_ttp_cache->get (v))
7054 return *p;
7055 return NULL_TREE;
7058 /* T is a bound template template-parameter. Copy its arguments into default
7059 arguments of the template template-parameter's template parameters. */
7061 static tree
7062 add_defaults_to_ttp (tree otmpl)
7064 if (tree c = lookup_defaulted_ttp (otmpl))
7065 return c;
7067 tree ntmpl = copy_node (otmpl);
7069 tree ntype = copy_node (TREE_TYPE (otmpl));
7070 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7071 TYPE_MAIN_VARIANT (ntype) = ntype;
7072 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7073 TYPE_NAME (ntype) = ntmpl;
7074 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7076 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7077 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7078 TEMPLATE_PARM_DECL (idx) = ntmpl;
7079 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7081 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7082 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7083 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7084 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7085 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7087 tree o = TREE_VEC_ELT (vec, i);
7088 if (!template_parameter_pack_p (TREE_VALUE (o)))
7090 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7091 TREE_PURPOSE (n) = any_targ_node;
7095 store_defaulted_ttp (otmpl, ntmpl);
7096 return ntmpl;
7099 /* ARG is a bound potential template template-argument, and PARGS is a list
7100 of arguments for the corresponding template template-parameter. Adjust
7101 PARGS as appropriate for application to ARG's template, and if ARG is a
7102 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7103 arguments to the template template parameter. */
7105 static tree
7106 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7108 ++processing_template_decl;
7109 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7110 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7112 /* When comparing two template template-parameters in partial ordering,
7113 rewrite the one currently being used as an argument to have default
7114 arguments for all parameters. */
7115 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7116 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7117 if (pargs != error_mark_node)
7118 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7119 TYPE_TI_ARGS (arg));
7121 else
7123 tree aparms
7124 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7125 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7126 /*require_all*/true,
7127 /*use_default*/true);
7129 --processing_template_decl;
7130 return pargs;
7133 /* Subroutine of unify for the case when PARM is a
7134 BOUND_TEMPLATE_TEMPLATE_PARM. */
7136 static int
7137 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7138 bool explain_p)
7140 tree parmvec = TYPE_TI_ARGS (parm);
7141 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7143 /* The template template parm might be variadic and the argument
7144 not, so flatten both argument lists. */
7145 parmvec = expand_template_argument_pack (parmvec);
7146 argvec = expand_template_argument_pack (argvec);
7148 if (flag_new_ttp)
7150 /* In keeping with P0522R0, adjust P's template arguments
7151 to apply to A's template; then flatten it again. */
7152 tree nparmvec = parmvec;
7153 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7154 nparmvec = expand_template_argument_pack (nparmvec);
7156 if (unify (tparms, targs, nparmvec, argvec,
7157 UNIFY_ALLOW_NONE, explain_p))
7158 return 1;
7160 /* If the P0522 adjustment eliminated a pack expansion, deduce
7161 empty packs. */
7162 if (flag_new_ttp
7163 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7164 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7165 DEDUCE_EXACT, /*sub*/true, explain_p))
7166 return 1;
7168 else
7170 /* Deduce arguments T, i from TT<T> or TT<i>.
7171 We check each element of PARMVEC and ARGVEC individually
7172 rather than the whole TREE_VEC since they can have
7173 different number of elements, which is allowed under N2555. */
7175 int len = TREE_VEC_LENGTH (parmvec);
7177 /* Check if the parameters end in a pack, making them
7178 variadic. */
7179 int parm_variadic_p = 0;
7180 if (len > 0
7181 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7182 parm_variadic_p = 1;
7184 for (int i = 0; i < len - parm_variadic_p; ++i)
7185 /* If the template argument list of P contains a pack
7186 expansion that is not the last template argument, the
7187 entire template argument list is a non-deduced
7188 context. */
7189 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7190 return unify_success (explain_p);
7192 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7193 return unify_too_few_arguments (explain_p,
7194 TREE_VEC_LENGTH (argvec), len);
7196 for (int i = 0; i < len - parm_variadic_p; ++i)
7197 if (unify (tparms, targs,
7198 TREE_VEC_ELT (parmvec, i),
7199 TREE_VEC_ELT (argvec, i),
7200 UNIFY_ALLOW_NONE, explain_p))
7201 return 1;
7203 if (parm_variadic_p
7204 && unify_pack_expansion (tparms, targs,
7205 parmvec, argvec,
7206 DEDUCE_EXACT,
7207 /*subr=*/true, explain_p))
7208 return 1;
7211 return 0;
7214 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7215 template template parameters. Both PARM_PARMS and ARG_PARMS are
7216 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7217 or PARM_DECL.
7219 Consider the example:
7220 template <class T> class A;
7221 template<template <class U> class TT> class B;
7223 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7224 the parameters to A, and OUTER_ARGS contains A. */
7226 static int
7227 coerce_template_template_parms (tree parm_parms,
7228 tree arg_parms,
7229 tsubst_flags_t complain,
7230 tree in_decl,
7231 tree outer_args)
7233 int nparms, nargs, i;
7234 tree parm, arg;
7235 int variadic_p = 0;
7237 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7238 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7240 nparms = TREE_VEC_LENGTH (parm_parms);
7241 nargs = TREE_VEC_LENGTH (arg_parms);
7243 if (flag_new_ttp)
7245 /* P0522R0: A template template-parameter P is at least as specialized as
7246 a template template-argument A if, given the following rewrite to two
7247 function templates, the function template corresponding to P is at
7248 least as specialized as the function template corresponding to A
7249 according to the partial ordering rules for function templates
7250 ([temp.func.order]). Given an invented class template X with the
7251 template parameter list of A (including default arguments):
7253 * Each of the two function templates has the same template parameters,
7254 respectively, as P or A.
7256 * Each function template has a single function parameter whose type is
7257 a specialization of X with template arguments corresponding to the
7258 template parameters from the respective function template where, for
7259 each template parameter PP in the template parameter list of the
7260 function template, a corresponding template argument AA is formed. If
7261 PP declares a parameter pack, then AA is the pack expansion
7262 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7264 If the rewrite produces an invalid type, then P is not at least as
7265 specialized as A. */
7267 /* So coerce P's args to apply to A's parms, and then deduce between A's
7268 args and the converted args. If that succeeds, A is at least as
7269 specialized as P, so they match.*/
7270 tree pargs = template_parms_level_to_args (parm_parms);
7271 ++processing_template_decl;
7272 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7273 /*require_all*/true, /*use_default*/true);
7274 --processing_template_decl;
7275 if (pargs != error_mark_node)
7277 tree targs = make_tree_vec (nargs);
7278 tree aargs = template_parms_level_to_args (arg_parms);
7279 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7280 /*explain*/false))
7281 return 1;
7285 /* Determine whether we have a parameter pack at the end of the
7286 template template parameter's template parameter list. */
7287 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7289 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7291 if (error_operand_p (parm))
7292 return 0;
7294 switch (TREE_CODE (parm))
7296 case TEMPLATE_DECL:
7297 case TYPE_DECL:
7298 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7299 variadic_p = 1;
7300 break;
7302 case PARM_DECL:
7303 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7304 variadic_p = 1;
7305 break;
7307 default:
7308 gcc_unreachable ();
7312 if (nargs != nparms
7313 && !(variadic_p && nargs >= nparms - 1))
7314 return 0;
7316 /* Check all of the template parameters except the parameter pack at
7317 the end (if any). */
7318 for (i = 0; i < nparms - variadic_p; ++i)
7320 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7321 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7322 continue;
7324 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7325 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7327 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7328 outer_args))
7329 return 0;
7333 if (variadic_p)
7335 /* Check each of the template parameters in the template
7336 argument against the template parameter pack at the end of
7337 the template template parameter. */
7338 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7339 return 0;
7341 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7343 for (; i < nargs; ++i)
7345 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7346 continue;
7348 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7350 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7351 outer_args))
7352 return 0;
7356 return 1;
7359 /* Verifies that the deduced template arguments (in TARGS) for the
7360 template template parameters (in TPARMS) represent valid bindings,
7361 by comparing the template parameter list of each template argument
7362 to the template parameter list of its corresponding template
7363 template parameter, in accordance with DR150. This
7364 routine can only be called after all template arguments have been
7365 deduced. It will return TRUE if all of the template template
7366 parameter bindings are okay, FALSE otherwise. */
7367 bool
7368 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7370 int i, ntparms = TREE_VEC_LENGTH (tparms);
7371 bool ret = true;
7373 /* We're dealing with template parms in this process. */
7374 ++processing_template_decl;
7376 targs = INNERMOST_TEMPLATE_ARGS (targs);
7378 for (i = 0; i < ntparms; ++i)
7380 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7381 tree targ = TREE_VEC_ELT (targs, i);
7383 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7385 tree packed_args = NULL_TREE;
7386 int idx, len = 1;
7388 if (ARGUMENT_PACK_P (targ))
7390 /* Look inside the argument pack. */
7391 packed_args = ARGUMENT_PACK_ARGS (targ);
7392 len = TREE_VEC_LENGTH (packed_args);
7395 for (idx = 0; idx < len; ++idx)
7397 tree targ_parms = NULL_TREE;
7399 if (packed_args)
7400 /* Extract the next argument from the argument
7401 pack. */
7402 targ = TREE_VEC_ELT (packed_args, idx);
7404 if (PACK_EXPANSION_P (targ))
7405 /* Look at the pattern of the pack expansion. */
7406 targ = PACK_EXPANSION_PATTERN (targ);
7408 /* Extract the template parameters from the template
7409 argument. */
7410 if (TREE_CODE (targ) == TEMPLATE_DECL)
7411 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7412 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7413 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7415 /* Verify that we can coerce the template template
7416 parameters from the template argument to the template
7417 parameter. This requires an exact match. */
7418 if (targ_parms
7419 && !coerce_template_template_parms
7420 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7421 targ_parms,
7422 tf_none,
7423 tparm,
7424 targs))
7426 ret = false;
7427 goto out;
7433 out:
7435 --processing_template_decl;
7436 return ret;
7439 /* Since type attributes aren't mangled, we need to strip them from
7440 template type arguments. */
7442 static tree
7443 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7445 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7446 return arg;
7447 bool removed_attributes = false;
7448 tree canon = strip_typedefs (arg, &removed_attributes);
7449 if (removed_attributes
7450 && (complain & tf_warning))
7451 warning (OPT_Wignored_attributes,
7452 "ignoring attributes on template argument %qT", arg);
7453 return canon;
7456 /* And from inside dependent non-type arguments like sizeof(Type). */
7458 static tree
7459 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7461 if (!arg || arg == error_mark_node)
7462 return arg;
7463 bool removed_attributes = false;
7464 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7465 if (removed_attributes
7466 && (complain & tf_warning))
7467 warning (OPT_Wignored_attributes,
7468 "ignoring attributes in template argument %qE", arg);
7469 return canon;
7472 // A template declaration can be substituted for a constrained
7473 // template template parameter only when the argument is more
7474 // constrained than the parameter.
7475 static bool
7476 is_compatible_template_arg (tree parm, tree arg)
7478 tree parm_cons = get_constraints (parm);
7480 /* For now, allow constrained template template arguments
7481 and unconstrained template template parameters. */
7482 if (parm_cons == NULL_TREE)
7483 return true;
7485 tree arg_cons = get_constraints (arg);
7487 // If the template parameter is constrained, we need to rewrite its
7488 // constraints in terms of the ARG's template parameters. This ensures
7489 // that all of the template parameter types will have the same depth.
7491 // Note that this is only valid when coerce_template_template_parm is
7492 // true for the innermost template parameters of PARM and ARG. In other
7493 // words, because coercion is successful, this conversion will be valid.
7494 if (parm_cons)
7496 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7497 parm_cons = tsubst_constraint_info (parm_cons,
7498 INNERMOST_TEMPLATE_ARGS (args),
7499 tf_none, NULL_TREE);
7500 if (parm_cons == error_mark_node)
7501 return false;
7504 return subsumes (parm_cons, arg_cons);
7507 // Convert a placeholder argument into a binding to the original
7508 // parameter. The original parameter is saved as the TREE_TYPE of
7509 // ARG.
7510 static inline tree
7511 convert_wildcard_argument (tree parm, tree arg)
7513 TREE_TYPE (arg) = parm;
7514 return arg;
7517 /* Convert the indicated template ARG as necessary to match the
7518 indicated template PARM. Returns the converted ARG, or
7519 error_mark_node if the conversion was unsuccessful. Error and
7520 warning messages are issued under control of COMPLAIN. This
7521 conversion is for the Ith parameter in the parameter list. ARGS is
7522 the full set of template arguments deduced so far. */
7524 static tree
7525 convert_template_argument (tree parm,
7526 tree arg,
7527 tree args,
7528 tsubst_flags_t complain,
7529 int i,
7530 tree in_decl)
7532 tree orig_arg;
7533 tree val;
7534 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7536 if (parm == error_mark_node)
7537 return error_mark_node;
7539 /* Trivially convert placeholders. */
7540 if (TREE_CODE (arg) == WILDCARD_DECL)
7541 return convert_wildcard_argument (parm, arg);
7543 if (arg == any_targ_node)
7544 return arg;
7546 if (TREE_CODE (arg) == TREE_LIST
7547 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7549 /* The template argument was the name of some
7550 member function. That's usually
7551 invalid, but static members are OK. In any
7552 case, grab the underlying fields/functions
7553 and issue an error later if required. */
7554 orig_arg = TREE_VALUE (arg);
7555 TREE_TYPE (arg) = unknown_type_node;
7558 orig_arg = arg;
7560 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7561 requires_type = (TREE_CODE (parm) == TYPE_DECL
7562 || requires_tmpl_type);
7564 /* When determining whether an argument pack expansion is a template,
7565 look at the pattern. */
7566 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7567 arg = PACK_EXPANSION_PATTERN (arg);
7569 /* Deal with an injected-class-name used as a template template arg. */
7570 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7572 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7573 if (TREE_CODE (t) == TEMPLATE_DECL)
7575 if (cxx_dialect >= cxx11)
7576 /* OK under DR 1004. */;
7577 else if (complain & tf_warning_or_error)
7578 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7579 " used as template template argument", TYPE_NAME (arg));
7580 else if (flag_pedantic_errors)
7581 t = arg;
7583 arg = t;
7587 is_tmpl_type =
7588 ((TREE_CODE (arg) == TEMPLATE_DECL
7589 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7590 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7591 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7592 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7594 if (is_tmpl_type
7595 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7596 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7597 arg = TYPE_STUB_DECL (arg);
7599 is_type = TYPE_P (arg) || is_tmpl_type;
7601 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7602 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7604 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7606 if (complain & tf_error)
7607 error ("invalid use of destructor %qE as a type", orig_arg);
7608 return error_mark_node;
7611 permerror (input_location,
7612 "to refer to a type member of a template parameter, "
7613 "use %<typename %E%>", orig_arg);
7615 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7616 TREE_OPERAND (arg, 1),
7617 typename_type,
7618 complain);
7619 arg = orig_arg;
7620 is_type = 1;
7622 if (is_type != requires_type)
7624 if (in_decl)
7626 if (complain & tf_error)
7628 error ("type/value mismatch at argument %d in template "
7629 "parameter list for %qD",
7630 i + 1, in_decl);
7631 if (is_type)
7632 inform (input_location,
7633 " expected a constant of type %qT, got %qT",
7634 TREE_TYPE (parm),
7635 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7636 else if (requires_tmpl_type)
7637 inform (input_location,
7638 " expected a class template, got %qE", orig_arg);
7639 else
7640 inform (input_location,
7641 " expected a type, got %qE", orig_arg);
7644 return error_mark_node;
7646 if (is_tmpl_type ^ requires_tmpl_type)
7648 if (in_decl && (complain & tf_error))
7650 error ("type/value mismatch at argument %d in template "
7651 "parameter list for %qD",
7652 i + 1, in_decl);
7653 if (is_tmpl_type)
7654 inform (input_location,
7655 " expected a type, got %qT", DECL_NAME (arg));
7656 else
7657 inform (input_location,
7658 " expected a class template, got %qT", orig_arg);
7660 return error_mark_node;
7663 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7664 /* We already did the appropriate conversion when packing args. */
7665 val = orig_arg;
7666 else if (is_type)
7668 if (requires_tmpl_type)
7670 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7671 /* The number of argument required is not known yet.
7672 Just accept it for now. */
7673 val = orig_arg;
7674 else
7676 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7677 tree argparm;
7679 /* Strip alias templates that are equivalent to another
7680 template. */
7681 arg = get_underlying_template (arg);
7682 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7684 if (coerce_template_template_parms (parmparm, argparm,
7685 complain, in_decl,
7686 args))
7688 val = arg;
7690 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7691 TEMPLATE_DECL. */
7692 if (val != error_mark_node)
7694 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7695 val = TREE_TYPE (val);
7696 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7697 val = make_pack_expansion (val);
7700 else
7702 if (in_decl && (complain & tf_error))
7704 error ("type/value mismatch at argument %d in "
7705 "template parameter list for %qD",
7706 i + 1, in_decl);
7707 inform (input_location,
7708 " expected a template of type %qD, got %qT",
7709 parm, orig_arg);
7712 val = error_mark_node;
7715 // Check that the constraints are compatible before allowing the
7716 // substitution.
7717 if (val != error_mark_node)
7718 if (!is_compatible_template_arg (parm, arg))
7720 if (in_decl && (complain & tf_error))
7722 error ("constraint mismatch at argument %d in "
7723 "template parameter list for %qD",
7724 i + 1, in_decl);
7725 inform (input_location, " expected %qD but got %qD",
7726 parm, arg);
7728 val = error_mark_node;
7732 else
7733 val = orig_arg;
7734 /* We only form one instance of each template specialization.
7735 Therefore, if we use a non-canonical variant (i.e., a
7736 typedef), any future messages referring to the type will use
7737 the typedef, which is confusing if those future uses do not
7738 themselves also use the typedef. */
7739 if (TYPE_P (val))
7740 val = canonicalize_type_argument (val, complain);
7742 else
7744 tree t = TREE_TYPE (parm);
7746 if (tree a = type_uses_auto (t))
7748 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7749 if (t == error_mark_node)
7750 return error_mark_node;
7752 else
7753 t = tsubst (t, args, complain, in_decl);
7755 if (invalid_nontype_parm_type_p (t, complain))
7756 return error_mark_node;
7758 if (!type_dependent_expression_p (orig_arg)
7759 && !uses_template_parms (t))
7760 /* We used to call digest_init here. However, digest_init
7761 will report errors, which we don't want when complain
7762 is zero. More importantly, digest_init will try too
7763 hard to convert things: for example, `0' should not be
7764 converted to pointer type at this point according to
7765 the standard. Accepting this is not merely an
7766 extension, since deciding whether or not these
7767 conversions can occur is part of determining which
7768 function template to call, or whether a given explicit
7769 argument specification is valid. */
7770 val = convert_nontype_argument (t, orig_arg, complain);
7771 else
7772 val = canonicalize_expr_argument (orig_arg, complain);
7774 if (val == NULL_TREE)
7775 val = error_mark_node;
7776 else if (val == error_mark_node && (complain & tf_error))
7777 error ("could not convert template argument %qE from %qT to %qT",
7778 orig_arg, TREE_TYPE (orig_arg), t);
7780 if (INDIRECT_REF_P (val))
7782 /* Reject template arguments that are references to built-in
7783 functions with no library fallbacks. */
7784 const_tree inner = TREE_OPERAND (val, 0);
7785 const_tree innertype = TREE_TYPE (inner);
7786 if (innertype
7787 && TREE_CODE (innertype) == REFERENCE_TYPE
7788 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7789 && 0 < TREE_OPERAND_LENGTH (inner)
7790 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7791 return error_mark_node;
7794 if (TREE_CODE (val) == SCOPE_REF)
7796 /* Strip typedefs from the SCOPE_REF. */
7797 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7798 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7799 complain);
7800 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7801 QUALIFIED_NAME_IS_TEMPLATE (val));
7805 return val;
7808 /* Coerces the remaining template arguments in INNER_ARGS (from
7809 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7810 Returns the coerced argument pack. PARM_IDX is the position of this
7811 parameter in the template parameter list. ARGS is the original
7812 template argument list. */
7813 static tree
7814 coerce_template_parameter_pack (tree parms,
7815 int parm_idx,
7816 tree args,
7817 tree inner_args,
7818 int arg_idx,
7819 tree new_args,
7820 int* lost,
7821 tree in_decl,
7822 tsubst_flags_t complain)
7824 tree parm = TREE_VEC_ELT (parms, parm_idx);
7825 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7826 tree packed_args;
7827 tree argument_pack;
7828 tree packed_parms = NULL_TREE;
7830 if (arg_idx > nargs)
7831 arg_idx = nargs;
7833 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7835 /* When the template parameter is a non-type template parameter pack
7836 or template template parameter pack whose type or template
7837 parameters use parameter packs, we know exactly how many arguments
7838 we are looking for. Build a vector of the instantiated decls for
7839 these template parameters in PACKED_PARMS. */
7840 /* We can't use make_pack_expansion here because it would interpret a
7841 _DECL as a use rather than a declaration. */
7842 tree decl = TREE_VALUE (parm);
7843 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7844 SET_PACK_EXPANSION_PATTERN (exp, decl);
7845 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7846 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7848 TREE_VEC_LENGTH (args)--;
7849 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7850 TREE_VEC_LENGTH (args)++;
7852 if (packed_parms == error_mark_node)
7853 return error_mark_node;
7855 /* If we're doing a partial instantiation of a member template,
7856 verify that all of the types used for the non-type
7857 template parameter pack are, in fact, valid for non-type
7858 template parameters. */
7859 if (arg_idx < nargs
7860 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7862 int j, len = TREE_VEC_LENGTH (packed_parms);
7863 for (j = 0; j < len; ++j)
7865 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7866 if (invalid_nontype_parm_type_p (t, complain))
7867 return error_mark_node;
7869 /* We don't know how many args we have yet, just
7870 use the unconverted ones for now. */
7871 return NULL_TREE;
7874 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7876 /* Check if we have a placeholder pack, which indicates we're
7877 in the context of a introduction list. In that case we want
7878 to match this pack to the single placeholder. */
7879 else if (arg_idx < nargs
7880 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7881 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7883 nargs = arg_idx + 1;
7884 packed_args = make_tree_vec (1);
7886 else
7887 packed_args = make_tree_vec (nargs - arg_idx);
7889 /* Convert the remaining arguments, which will be a part of the
7890 parameter pack "parm". */
7891 int first_pack_arg = arg_idx;
7892 for (; arg_idx < nargs; ++arg_idx)
7894 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7895 tree actual_parm = TREE_VALUE (parm);
7896 int pack_idx = arg_idx - first_pack_arg;
7898 if (packed_parms)
7900 /* Once we've packed as many args as we have types, stop. */
7901 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7902 break;
7903 else if (PACK_EXPANSION_P (arg))
7904 /* We don't know how many args we have yet, just
7905 use the unconverted ones for now. */
7906 return NULL_TREE;
7907 else
7908 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7911 if (arg == error_mark_node)
7913 if (complain & tf_error)
7914 error ("template argument %d is invalid", arg_idx + 1);
7916 else
7917 arg = convert_template_argument (actual_parm,
7918 arg, new_args, complain, parm_idx,
7919 in_decl);
7920 if (arg == error_mark_node)
7921 (*lost)++;
7922 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7925 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7926 && TREE_VEC_LENGTH (packed_args) > 0)
7928 if (complain & tf_error)
7929 error ("wrong number of template arguments (%d, should be %d)",
7930 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7931 return error_mark_node;
7934 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7935 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7936 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7937 else
7939 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7940 TREE_CONSTANT (argument_pack) = 1;
7943 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7944 if (CHECKING_P)
7945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7946 TREE_VEC_LENGTH (packed_args));
7947 return argument_pack;
7950 /* Returns the number of pack expansions in the template argument vector
7951 ARGS. */
7953 static int
7954 pack_expansion_args_count (tree args)
7956 int i;
7957 int count = 0;
7958 if (args)
7959 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7961 tree elt = TREE_VEC_ELT (args, i);
7962 if (elt && PACK_EXPANSION_P (elt))
7963 ++count;
7965 return count;
7968 /* Convert all template arguments to their appropriate types, and
7969 return a vector containing the innermost resulting template
7970 arguments. If any error occurs, return error_mark_node. Error and
7971 warning messages are issued under control of COMPLAIN.
7973 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7974 for arguments not specified in ARGS. Otherwise, if
7975 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7976 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7977 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7978 ARGS. */
7980 static tree
7981 coerce_template_parms (tree parms,
7982 tree args,
7983 tree in_decl,
7984 tsubst_flags_t complain,
7985 bool require_all_args,
7986 bool use_default_args)
7988 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7989 tree orig_inner_args;
7990 tree inner_args;
7991 tree new_args;
7992 tree new_inner_args;
7993 int saved_unevaluated_operand;
7994 int saved_inhibit_evaluation_warnings;
7996 /* When used as a boolean value, indicates whether this is a
7997 variadic template parameter list. Since it's an int, we can also
7998 subtract it from nparms to get the number of non-variadic
7999 parameters. */
8000 int variadic_p = 0;
8001 int variadic_args_p = 0;
8002 int post_variadic_parms = 0;
8004 /* Likewise for parameters with default arguments. */
8005 int default_p = 0;
8007 if (args == error_mark_node)
8008 return error_mark_node;
8010 nparms = TREE_VEC_LENGTH (parms);
8012 /* Determine if there are any parameter packs or default arguments. */
8013 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8015 tree parm = TREE_VEC_ELT (parms, parm_idx);
8016 if (variadic_p)
8017 ++post_variadic_parms;
8018 if (template_parameter_pack_p (TREE_VALUE (parm)))
8019 ++variadic_p;
8020 if (TREE_PURPOSE (parm))
8021 ++default_p;
8024 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8025 /* If there are no parameters that follow a parameter pack, we need to
8026 expand any argument packs so that we can deduce a parameter pack from
8027 some non-packed args followed by an argument pack, as in variadic85.C.
8028 If there are such parameters, we need to leave argument packs intact
8029 so the arguments are assigned properly. This can happen when dealing
8030 with a nested class inside a partial specialization of a class
8031 template, as in variadic92.C, or when deducing a template parameter pack
8032 from a sub-declarator, as in variadic114.C. */
8033 if (!post_variadic_parms)
8034 inner_args = expand_template_argument_pack (inner_args);
8036 /* Count any pack expansion args. */
8037 variadic_args_p = pack_expansion_args_count (inner_args);
8039 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8040 if ((nargs - variadic_args_p > nparms && !variadic_p)
8041 || (nargs < nparms - variadic_p
8042 && require_all_args
8043 && !variadic_args_p
8044 && (!use_default_args
8045 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8046 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8048 if (complain & tf_error)
8050 if (variadic_p || default_p)
8052 nparms -= variadic_p + default_p;
8053 error ("wrong number of template arguments "
8054 "(%d, should be at least %d)", nargs, nparms);
8056 else
8057 error ("wrong number of template arguments "
8058 "(%d, should be %d)", nargs, nparms);
8060 if (in_decl)
8061 inform (DECL_SOURCE_LOCATION (in_decl),
8062 "provided for %qD", in_decl);
8065 return error_mark_node;
8067 /* We can't pass a pack expansion to a non-pack parameter of an alias
8068 template (DR 1430). */
8069 else if (in_decl
8070 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8071 || concept_template_p (in_decl))
8072 && variadic_args_p
8073 && nargs - variadic_args_p < nparms - variadic_p)
8075 if (complain & tf_error)
8077 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8079 tree arg = TREE_VEC_ELT (inner_args, i);
8080 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8082 if (PACK_EXPANSION_P (arg)
8083 && !template_parameter_pack_p (parm))
8085 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8086 error_at (location_of (arg),
8087 "pack expansion argument for non-pack parameter "
8088 "%qD of alias template %qD", parm, in_decl);
8089 else
8090 error_at (location_of (arg),
8091 "pack expansion argument for non-pack parameter "
8092 "%qD of concept %qD", parm, in_decl);
8093 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8094 goto found;
8097 gcc_unreachable ();
8098 found:;
8100 return error_mark_node;
8103 /* We need to evaluate the template arguments, even though this
8104 template-id may be nested within a "sizeof". */
8105 saved_unevaluated_operand = cp_unevaluated_operand;
8106 cp_unevaluated_operand = 0;
8107 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8108 c_inhibit_evaluation_warnings = 0;
8109 new_inner_args = make_tree_vec (nparms);
8110 new_args = add_outermost_template_args (args, new_inner_args);
8111 int pack_adjust = 0;
8112 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8114 tree arg;
8115 tree parm;
8117 /* Get the Ith template parameter. */
8118 parm = TREE_VEC_ELT (parms, parm_idx);
8120 if (parm == error_mark_node)
8122 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8123 continue;
8126 /* Calculate the next argument. */
8127 if (arg_idx < nargs)
8128 arg = TREE_VEC_ELT (inner_args, arg_idx);
8129 else
8130 arg = NULL_TREE;
8132 if (template_parameter_pack_p (TREE_VALUE (parm))
8133 && !(arg && ARGUMENT_PACK_P (arg)))
8135 /* Some arguments will be placed in the
8136 template parameter pack PARM. */
8137 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8138 inner_args, arg_idx,
8139 new_args, &lost,
8140 in_decl, complain);
8142 if (arg == NULL_TREE)
8144 /* We don't know how many args we have yet, just use the
8145 unconverted (and still packed) ones for now. */
8146 new_inner_args = orig_inner_args;
8147 arg_idx = nargs;
8148 break;
8151 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8153 /* Store this argument. */
8154 if (arg == error_mark_node)
8156 lost++;
8157 /* We are done with all of the arguments. */
8158 arg_idx = nargs;
8160 else
8162 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8163 arg_idx += pack_adjust;
8166 continue;
8168 else if (arg)
8170 if (PACK_EXPANSION_P (arg))
8172 /* "If every valid specialization of a variadic template
8173 requires an empty template parameter pack, the template is
8174 ill-formed, no diagnostic required." So check that the
8175 pattern works with this parameter. */
8176 tree pattern = PACK_EXPANSION_PATTERN (arg);
8177 tree conv = convert_template_argument (TREE_VALUE (parm),
8178 pattern, new_args,
8179 complain, parm_idx,
8180 in_decl);
8181 if (conv == error_mark_node)
8183 if (complain & tf_error)
8184 inform (input_location, "so any instantiation with a "
8185 "non-empty parameter pack would be ill-formed");
8186 ++lost;
8188 else if (TYPE_P (conv) && !TYPE_P (pattern))
8189 /* Recover from missing typename. */
8190 TREE_VEC_ELT (inner_args, arg_idx)
8191 = make_pack_expansion (conv);
8193 /* We don't know how many args we have yet, just
8194 use the unconverted ones for now. */
8195 new_inner_args = inner_args;
8196 arg_idx = nargs;
8197 break;
8200 else if (require_all_args)
8202 /* There must be a default arg in this case. */
8203 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8204 complain, in_decl);
8205 /* The position of the first default template argument,
8206 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8207 Record that. */
8208 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8209 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8210 arg_idx - pack_adjust);
8212 else
8213 break;
8215 if (arg == error_mark_node)
8217 if (complain & tf_error)
8218 error ("template argument %d is invalid", arg_idx + 1);
8220 else if (!arg)
8221 /* This only occurs if there was an error in the template
8222 parameter list itself (which we would already have
8223 reported) that we are trying to recover from, e.g., a class
8224 template with a parameter list such as
8225 template<typename..., typename>. */
8226 ++lost;
8227 else
8228 arg = convert_template_argument (TREE_VALUE (parm),
8229 arg, new_args, complain,
8230 parm_idx, in_decl);
8232 if (arg == error_mark_node)
8233 lost++;
8234 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8236 cp_unevaluated_operand = saved_unevaluated_operand;
8237 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8239 if (variadic_p && arg_idx < nargs)
8241 if (complain & tf_error)
8243 error ("wrong number of template arguments "
8244 "(%d, should be %d)", nargs, arg_idx);
8245 if (in_decl)
8246 error ("provided for %q+D", in_decl);
8248 return error_mark_node;
8251 if (lost)
8252 return error_mark_node;
8254 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8255 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8256 TREE_VEC_LENGTH (new_inner_args));
8258 return new_inner_args;
8261 /* Convert all template arguments to their appropriate types, and
8262 return a vector containing the innermost resulting template
8263 arguments. If any error occurs, return error_mark_node. Error and
8264 warning messages are not issued.
8266 Note that no function argument deduction is performed, and default
8267 arguments are used to fill in unspecified arguments. */
8268 tree
8269 coerce_template_parms (tree parms, tree args, tree in_decl)
8271 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8274 /* Convert all template arguments to their appropriate type, and
8275 instantiate default arguments as needed. This returns a vector
8276 containing the innermost resulting template arguments, or
8277 error_mark_node if unsuccessful. */
8278 tree
8279 coerce_template_parms (tree parms, tree args, tree in_decl,
8280 tsubst_flags_t complain)
8282 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8285 /* Like coerce_template_parms. If PARMS represents all template
8286 parameters levels, this function returns a vector of vectors
8287 representing all the resulting argument levels. Note that in this
8288 case, only the innermost arguments are coerced because the
8289 outermost ones are supposed to have been coerced already.
8291 Otherwise, if PARMS represents only (the innermost) vector of
8292 parameters, this function returns a vector containing just the
8293 innermost resulting arguments. */
8295 static tree
8296 coerce_innermost_template_parms (tree parms,
8297 tree args,
8298 tree in_decl,
8299 tsubst_flags_t complain,
8300 bool require_all_args,
8301 bool use_default_args)
8303 int parms_depth = TMPL_PARMS_DEPTH (parms);
8304 int args_depth = TMPL_ARGS_DEPTH (args);
8305 tree coerced_args;
8307 if (parms_depth > 1)
8309 coerced_args = make_tree_vec (parms_depth);
8310 tree level;
8311 int cur_depth;
8313 for (level = parms, cur_depth = parms_depth;
8314 parms_depth > 0 && level != NULL_TREE;
8315 level = TREE_CHAIN (level), --cur_depth)
8317 tree l;
8318 if (cur_depth == args_depth)
8319 l = coerce_template_parms (TREE_VALUE (level),
8320 args, in_decl, complain,
8321 require_all_args,
8322 use_default_args);
8323 else
8324 l = TMPL_ARGS_LEVEL (args, cur_depth);
8326 if (l == error_mark_node)
8327 return error_mark_node;
8329 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8332 else
8333 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8334 args, in_decl, complain,
8335 require_all_args,
8336 use_default_args);
8337 return coerced_args;
8340 /* Returns 1 if template args OT and NT are equivalent. */
8343 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8345 if (nt == ot)
8346 return 1;
8347 if (nt == NULL_TREE || ot == NULL_TREE)
8348 return false;
8349 if (nt == any_targ_node || ot == any_targ_node)
8350 return true;
8352 if (TREE_CODE (nt) == TREE_VEC)
8353 /* For member templates */
8354 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8355 else if (PACK_EXPANSION_P (ot))
8356 return (PACK_EXPANSION_P (nt)
8357 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8358 PACK_EXPANSION_PATTERN (nt))
8359 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8360 PACK_EXPANSION_EXTRA_ARGS (nt)));
8361 else if (ARGUMENT_PACK_P (ot))
8363 int i, len;
8364 tree opack, npack;
8366 if (!ARGUMENT_PACK_P (nt))
8367 return 0;
8369 opack = ARGUMENT_PACK_ARGS (ot);
8370 npack = ARGUMENT_PACK_ARGS (nt);
8371 len = TREE_VEC_LENGTH (opack);
8372 if (TREE_VEC_LENGTH (npack) != len)
8373 return 0;
8374 for (i = 0; i < len; ++i)
8375 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8376 TREE_VEC_ELT (npack, i)))
8377 return 0;
8378 return 1;
8380 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8381 gcc_unreachable ();
8382 else if (TYPE_P (nt))
8384 if (!TYPE_P (ot))
8385 return false;
8386 /* Don't treat an alias template specialization with dependent
8387 arguments as equivalent to its underlying type when used as a
8388 template argument; we need them to be distinct so that we
8389 substitute into the specialization arguments at instantiation
8390 time. And aliases can't be equivalent without being ==, so
8391 we don't need to look any deeper.
8393 During partial ordering, however, we need to treat them normally so
8394 that we can order uses of the same alias with different
8395 cv-qualification (79960). */
8396 if (!partial_order
8397 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8398 return false;
8399 else
8400 return same_type_p (ot, nt);
8402 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8403 return 0;
8404 else
8406 /* Try to treat a template non-type argument that has been converted
8407 to the parameter type as equivalent to one that hasn't yet. */
8408 for (enum tree_code code1 = TREE_CODE (ot);
8409 CONVERT_EXPR_CODE_P (code1)
8410 || code1 == NON_LVALUE_EXPR;
8411 code1 = TREE_CODE (ot))
8412 ot = TREE_OPERAND (ot, 0);
8413 for (enum tree_code code2 = TREE_CODE (nt);
8414 CONVERT_EXPR_CODE_P (code2)
8415 || code2 == NON_LVALUE_EXPR;
8416 code2 = TREE_CODE (nt))
8417 nt = TREE_OPERAND (nt, 0);
8419 return cp_tree_equal (ot, nt);
8423 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8424 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8425 NEWARG_PTR with the offending arguments if they are non-NULL. */
8428 comp_template_args (tree oldargs, tree newargs,
8429 tree *oldarg_ptr, tree *newarg_ptr,
8430 bool partial_order)
8432 int i;
8434 if (oldargs == newargs)
8435 return 1;
8437 if (!oldargs || !newargs)
8438 return 0;
8440 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8441 return 0;
8443 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8445 tree nt = TREE_VEC_ELT (newargs, i);
8446 tree ot = TREE_VEC_ELT (oldargs, i);
8448 if (! template_args_equal (ot, nt, partial_order))
8450 if (oldarg_ptr != NULL)
8451 *oldarg_ptr = ot;
8452 if (newarg_ptr != NULL)
8453 *newarg_ptr = nt;
8454 return 0;
8457 return 1;
8460 inline bool
8461 comp_template_args_porder (tree oargs, tree nargs)
8463 return comp_template_args (oargs, nargs, NULL, NULL, true);
8466 static void
8467 add_pending_template (tree d)
8469 tree ti = (TYPE_P (d)
8470 ? CLASSTYPE_TEMPLATE_INFO (d)
8471 : DECL_TEMPLATE_INFO (d));
8472 struct pending_template *pt;
8473 int level;
8475 if (TI_PENDING_TEMPLATE_FLAG (ti))
8476 return;
8478 /* We are called both from instantiate_decl, where we've already had a
8479 tinst_level pushed, and instantiate_template, where we haven't.
8480 Compensate. */
8481 level = !current_tinst_level || current_tinst_level->decl != d;
8483 if (level)
8484 push_tinst_level (d);
8486 pt = ggc_alloc<pending_template> ();
8487 pt->next = NULL;
8488 pt->tinst = current_tinst_level;
8489 if (last_pending_template)
8490 last_pending_template->next = pt;
8491 else
8492 pending_templates = pt;
8494 last_pending_template = pt;
8496 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8498 if (level)
8499 pop_tinst_level ();
8503 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8504 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8505 documentation for TEMPLATE_ID_EXPR. */
8507 tree
8508 lookup_template_function (tree fns, tree arglist)
8510 tree type;
8512 if (fns == error_mark_node || arglist == error_mark_node)
8513 return error_mark_node;
8515 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8517 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8519 error ("%q#D is not a function template", fns);
8520 return error_mark_node;
8523 if (BASELINK_P (fns))
8525 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8526 unknown_type_node,
8527 BASELINK_FUNCTIONS (fns),
8528 arglist);
8529 return fns;
8532 type = TREE_TYPE (fns);
8533 if (TREE_CODE (fns) == OVERLOAD || !type)
8534 type = unknown_type_node;
8536 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8539 /* Within the scope of a template class S<T>, the name S gets bound
8540 (in build_self_reference) to a TYPE_DECL for the class, not a
8541 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8542 or one of its enclosing classes, and that type is a template,
8543 return the associated TEMPLATE_DECL. Otherwise, the original
8544 DECL is returned.
8546 Also handle the case when DECL is a TREE_LIST of ambiguous
8547 injected-class-names from different bases. */
8549 tree
8550 maybe_get_template_decl_from_type_decl (tree decl)
8552 if (decl == NULL_TREE)
8553 return decl;
8555 /* DR 176: A lookup that finds an injected-class-name (10.2
8556 [class.member.lookup]) can result in an ambiguity in certain cases
8557 (for example, if it is found in more than one base class). If all of
8558 the injected-class-names that are found refer to specializations of
8559 the same class template, and if the name is followed by a
8560 template-argument-list, the reference refers to the class template
8561 itself and not a specialization thereof, and is not ambiguous. */
8562 if (TREE_CODE (decl) == TREE_LIST)
8564 tree t, tmpl = NULL_TREE;
8565 for (t = decl; t; t = TREE_CHAIN (t))
8567 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8568 if (!tmpl)
8569 tmpl = elt;
8570 else if (tmpl != elt)
8571 break;
8573 if (tmpl && t == NULL_TREE)
8574 return tmpl;
8575 else
8576 return decl;
8579 return (decl != NULL_TREE
8580 && DECL_SELF_REFERENCE_P (decl)
8581 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8582 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8585 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8586 parameters, find the desired type.
8588 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8590 IN_DECL, if non-NULL, is the template declaration we are trying to
8591 instantiate.
8593 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8594 the class we are looking up.
8596 Issue error and warning messages under control of COMPLAIN.
8598 If the template class is really a local class in a template
8599 function, then the FUNCTION_CONTEXT is the function in which it is
8600 being instantiated.
8602 ??? Note that this function is currently called *twice* for each
8603 template-id: the first time from the parser, while creating the
8604 incomplete type (finish_template_type), and the second type during the
8605 real instantiation (instantiate_template_class). This is surely something
8606 that we want to avoid. It also causes some problems with argument
8607 coercion (see convert_nontype_argument for more information on this). */
8609 static tree
8610 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8611 int entering_scope, tsubst_flags_t complain)
8613 tree templ = NULL_TREE, parmlist;
8614 tree t;
8615 spec_entry **slot;
8616 spec_entry *entry;
8617 spec_entry elt;
8618 hashval_t hash;
8620 if (identifier_p (d1))
8622 tree value = innermost_non_namespace_value (d1);
8623 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8624 templ = value;
8625 else
8627 if (context)
8628 push_decl_namespace (context);
8629 templ = lookup_name (d1);
8630 templ = maybe_get_template_decl_from_type_decl (templ);
8631 if (context)
8632 pop_decl_namespace ();
8634 if (templ)
8635 context = DECL_CONTEXT (templ);
8637 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8639 tree type = TREE_TYPE (d1);
8641 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8642 an implicit typename for the second A. Deal with it. */
8643 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8644 type = TREE_TYPE (type);
8646 if (CLASSTYPE_TEMPLATE_INFO (type))
8648 templ = CLASSTYPE_TI_TEMPLATE (type);
8649 d1 = DECL_NAME (templ);
8652 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8653 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8655 templ = TYPE_TI_TEMPLATE (d1);
8656 d1 = DECL_NAME (templ);
8658 else if (DECL_TYPE_TEMPLATE_P (d1))
8660 templ = d1;
8661 d1 = DECL_NAME (templ);
8662 context = DECL_CONTEXT (templ);
8664 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8666 templ = d1;
8667 d1 = DECL_NAME (templ);
8670 /* Issue an error message if we didn't find a template. */
8671 if (! templ)
8673 if (complain & tf_error)
8674 error ("%qT is not a template", d1);
8675 return error_mark_node;
8678 if (TREE_CODE (templ) != TEMPLATE_DECL
8679 /* Make sure it's a user visible template, if it was named by
8680 the user. */
8681 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8682 && !PRIMARY_TEMPLATE_P (templ)))
8684 if (complain & tf_error)
8686 error ("non-template type %qT used as a template", d1);
8687 if (in_decl)
8688 error ("for template declaration %q+D", in_decl);
8690 return error_mark_node;
8693 complain &= ~tf_user;
8695 /* An alias that just changes the name of a template is equivalent to the
8696 other template, so if any of the arguments are pack expansions, strip
8697 the alias to avoid problems with a pack expansion passed to a non-pack
8698 alias template parameter (DR 1430). */
8699 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8700 templ = get_underlying_template (templ);
8702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8704 tree parm;
8705 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8706 if (arglist2 == error_mark_node
8707 || (!uses_template_parms (arglist2)
8708 && check_instantiated_args (templ, arglist2, complain)))
8709 return error_mark_node;
8711 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8712 return parm;
8714 else
8716 tree template_type = TREE_TYPE (templ);
8717 tree gen_tmpl;
8718 tree type_decl;
8719 tree found = NULL_TREE;
8720 int arg_depth;
8721 int parm_depth;
8722 int is_dependent_type;
8723 int use_partial_inst_tmpl = false;
8725 if (template_type == error_mark_node)
8726 /* An error occurred while building the template TEMPL, and a
8727 diagnostic has most certainly been emitted for that
8728 already. Let's propagate that error. */
8729 return error_mark_node;
8731 gen_tmpl = most_general_template (templ);
8732 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8733 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8734 arg_depth = TMPL_ARGS_DEPTH (arglist);
8736 if (arg_depth == 1 && parm_depth > 1)
8738 /* We've been given an incomplete set of template arguments.
8739 For example, given:
8741 template <class T> struct S1 {
8742 template <class U> struct S2 {};
8743 template <class U> struct S2<U*> {};
8746 we will be called with an ARGLIST of `U*', but the
8747 TEMPLATE will be `template <class T> template
8748 <class U> struct S1<T>::S2'. We must fill in the missing
8749 arguments. */
8750 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8751 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8752 arg_depth = TMPL_ARGS_DEPTH (arglist);
8755 /* Now we should have enough arguments. */
8756 gcc_assert (parm_depth == arg_depth);
8758 /* From here on, we're only interested in the most general
8759 template. */
8761 /* Calculate the BOUND_ARGS. These will be the args that are
8762 actually tsubst'd into the definition to create the
8763 instantiation. */
8764 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8765 complain,
8766 /*require_all_args=*/true,
8767 /*use_default_args=*/true);
8769 if (arglist == error_mark_node)
8770 /* We were unable to bind the arguments. */
8771 return error_mark_node;
8773 /* In the scope of a template class, explicit references to the
8774 template class refer to the type of the template, not any
8775 instantiation of it. For example, in:
8777 template <class T> class C { void f(C<T>); }
8779 the `C<T>' is just the same as `C'. Outside of the
8780 class, however, such a reference is an instantiation. */
8781 if (entering_scope
8782 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8783 || currently_open_class (template_type))
8785 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8787 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8788 return template_type;
8791 /* If we already have this specialization, return it. */
8792 elt.tmpl = gen_tmpl;
8793 elt.args = arglist;
8794 elt.spec = NULL_TREE;
8795 hash = spec_hasher::hash (&elt);
8796 entry = type_specializations->find_with_hash (&elt, hash);
8798 if (entry)
8799 return entry->spec;
8801 /* If the the template's constraints are not satisfied,
8802 then we cannot form a valid type.
8804 Note that the check is deferred until after the hash
8805 lookup. This prevents redundant checks on previously
8806 instantiated specializations. */
8807 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8809 if (complain & tf_error)
8811 error ("template constraint failure");
8812 diagnose_constraints (input_location, gen_tmpl, arglist);
8814 return error_mark_node;
8817 is_dependent_type = uses_template_parms (arglist);
8819 /* If the deduced arguments are invalid, then the binding
8820 failed. */
8821 if (!is_dependent_type
8822 && check_instantiated_args (gen_tmpl,
8823 INNERMOST_TEMPLATE_ARGS (arglist),
8824 complain))
8825 return error_mark_node;
8827 if (!is_dependent_type
8828 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8829 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8830 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8832 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8833 DECL_NAME (gen_tmpl),
8834 /*tag_scope=*/ts_global);
8835 return found;
8838 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8839 complain, in_decl);
8840 if (context == error_mark_node)
8841 return error_mark_node;
8843 if (!context)
8844 context = global_namespace;
8846 /* Create the type. */
8847 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8849 /* The user referred to a specialization of an alias
8850 template represented by GEN_TMPL.
8852 [temp.alias]/2 says:
8854 When a template-id refers to the specialization of an
8855 alias template, it is equivalent to the associated
8856 type obtained by substitution of its
8857 template-arguments for the template-parameters in the
8858 type-id of the alias template. */
8860 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8861 /* Note that the call above (by indirectly calling
8862 register_specialization in tsubst_decl) registers the
8863 TYPE_DECL representing the specialization of the alias
8864 template. So next time someone substitutes ARGLIST for
8865 the template parms into the alias template (GEN_TMPL),
8866 she'll get that TYPE_DECL back. */
8868 if (t == error_mark_node)
8869 return t;
8871 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8873 if (!is_dependent_type)
8875 set_current_access_from_decl (TYPE_NAME (template_type));
8876 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8877 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8878 arglist, complain, in_decl),
8879 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8880 arglist, complain, in_decl),
8881 SCOPED_ENUM_P (template_type), NULL);
8883 if (t == error_mark_node)
8884 return t;
8886 else
8888 /* We don't want to call start_enum for this type, since
8889 the values for the enumeration constants may involve
8890 template parameters. And, no one should be interested
8891 in the enumeration constants for such a type. */
8892 t = cxx_make_type (ENUMERAL_TYPE);
8893 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8895 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8896 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8897 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8899 else if (CLASS_TYPE_P (template_type))
8901 t = make_class_type (TREE_CODE (template_type));
8902 CLASSTYPE_DECLARED_CLASS (t)
8903 = CLASSTYPE_DECLARED_CLASS (template_type);
8904 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8906 /* A local class. Make sure the decl gets registered properly. */
8907 if (context == current_function_decl)
8908 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8910 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8911 /* This instantiation is another name for the primary
8912 template type. Set the TYPE_CANONICAL field
8913 appropriately. */
8914 TYPE_CANONICAL (t) = template_type;
8915 else if (any_template_arguments_need_structural_equality_p (arglist))
8916 /* Some of the template arguments require structural
8917 equality testing, so this template class requires
8918 structural equality testing. */
8919 SET_TYPE_STRUCTURAL_EQUALITY (t);
8921 else
8922 gcc_unreachable ();
8924 /* If we called start_enum or pushtag above, this information
8925 will already be set up. */
8926 if (!TYPE_NAME (t))
8928 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8930 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8931 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8932 DECL_SOURCE_LOCATION (type_decl)
8933 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8935 else
8936 type_decl = TYPE_NAME (t);
8938 if (CLASS_TYPE_P (template_type))
8940 TREE_PRIVATE (type_decl)
8941 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8942 TREE_PROTECTED (type_decl)
8943 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8944 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8946 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8947 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8951 if (OVERLOAD_TYPE_P (t)
8952 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8954 static const char *tags[] = {"abi_tag", "may_alias"};
8956 for (unsigned ix = 0; ix != 2; ix++)
8958 tree attributes
8959 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8961 if (attributes)
8962 TYPE_ATTRIBUTES (t)
8963 = tree_cons (TREE_PURPOSE (attributes),
8964 TREE_VALUE (attributes),
8965 TYPE_ATTRIBUTES (t));
8969 /* Let's consider the explicit specialization of a member
8970 of a class template specialization that is implicitly instantiated,
8971 e.g.:
8972 template<class T>
8973 struct S
8975 template<class U> struct M {}; //#0
8978 template<>
8979 template<>
8980 struct S<int>::M<char> //#1
8982 int i;
8984 [temp.expl.spec]/4 says this is valid.
8986 In this case, when we write:
8987 S<int>::M<char> m;
8989 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8990 the one of #0.
8992 When we encounter #1, we want to store the partial instantiation
8993 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8995 For all cases other than this "explicit specialization of member of a
8996 class template", we just want to store the most general template into
8997 the CLASSTYPE_TI_TEMPLATE of M.
8999 This case of "explicit specialization of member of a class template"
9000 only happens when:
9001 1/ the enclosing class is an instantiation of, and therefore not
9002 the same as, the context of the most general template, and
9003 2/ we aren't looking at the partial instantiation itself, i.e.
9004 the innermost arguments are not the same as the innermost parms of
9005 the most general template.
9007 So it's only when 1/ and 2/ happens that we want to use the partial
9008 instantiation of the member template in lieu of its most general
9009 template. */
9011 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9012 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9013 /* the enclosing class must be an instantiation... */
9014 && CLASS_TYPE_P (context)
9015 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9017 TREE_VEC_LENGTH (arglist)--;
9018 ++processing_template_decl;
9019 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9020 tree partial_inst_args =
9021 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9022 arglist, complain, NULL_TREE);
9023 --processing_template_decl;
9024 TREE_VEC_LENGTH (arglist)++;
9025 if (partial_inst_args == error_mark_node)
9026 return error_mark_node;
9027 use_partial_inst_tmpl =
9028 /*...and we must not be looking at the partial instantiation
9029 itself. */
9030 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9031 partial_inst_args);
9034 if (!use_partial_inst_tmpl)
9035 /* This case is easy; there are no member templates involved. */
9036 found = gen_tmpl;
9037 else
9039 /* This is a full instantiation of a member template. Find
9040 the partial instantiation of which this is an instance. */
9042 /* Temporarily reduce by one the number of levels in the ARGLIST
9043 so as to avoid comparing the last set of arguments. */
9044 TREE_VEC_LENGTH (arglist)--;
9045 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9046 TREE_VEC_LENGTH (arglist)++;
9047 /* FOUND is either a proper class type, or an alias
9048 template specialization. In the later case, it's a
9049 TYPE_DECL, resulting from the substituting of arguments
9050 for parameters in the TYPE_DECL of the alias template
9051 done earlier. So be careful while getting the template
9052 of FOUND. */
9053 found = (TREE_CODE (found) == TEMPLATE_DECL
9054 ? found
9055 : (TREE_CODE (found) == TYPE_DECL
9056 ? DECL_TI_TEMPLATE (found)
9057 : CLASSTYPE_TI_TEMPLATE (found)));
9060 // Build template info for the new specialization.
9061 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9063 elt.spec = t;
9064 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9065 entry = ggc_alloc<spec_entry> ();
9066 *entry = elt;
9067 *slot = entry;
9069 /* Note this use of the partial instantiation so we can check it
9070 later in maybe_process_partial_specialization. */
9071 DECL_TEMPLATE_INSTANTIATIONS (found)
9072 = tree_cons (arglist, t,
9073 DECL_TEMPLATE_INSTANTIATIONS (found));
9075 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9076 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9077 /* Now that the type has been registered on the instantiations
9078 list, we set up the enumerators. Because the enumeration
9079 constants may involve the enumeration type itself, we make
9080 sure to register the type first, and then create the
9081 constants. That way, doing tsubst_expr for the enumeration
9082 constants won't result in recursive calls here; we'll find
9083 the instantiation and exit above. */
9084 tsubst_enum (template_type, t, arglist);
9086 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9087 /* If the type makes use of template parameters, the
9088 code that generates debugging information will crash. */
9089 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9091 /* Possibly limit visibility based on template args. */
9092 TREE_PUBLIC (type_decl) = 1;
9093 determine_visibility (type_decl);
9095 inherit_targ_abi_tags (t);
9097 return t;
9101 /* Wrapper for lookup_template_class_1. */
9103 tree
9104 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9105 int entering_scope, tsubst_flags_t complain)
9107 tree ret;
9108 timevar_push (TV_TEMPLATE_INST);
9109 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9110 entering_scope, complain);
9111 timevar_pop (TV_TEMPLATE_INST);
9112 return ret;
9115 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9117 tree
9118 lookup_template_variable (tree templ, tree arglist)
9120 /* The type of the expression is NULL_TREE since the template-id could refer
9121 to an explicit or partial specialization. */
9122 tree type = NULL_TREE;
9123 if (flag_concepts && variable_concept_p (templ))
9124 /* Except that concepts are always bool. */
9125 type = boolean_type_node;
9126 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9129 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9131 tree
9132 finish_template_variable (tree var, tsubst_flags_t complain)
9134 tree templ = TREE_OPERAND (var, 0);
9135 tree arglist = TREE_OPERAND (var, 1);
9137 /* We never want to return a VAR_DECL for a variable concept, since they
9138 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9139 bool concept_p = flag_concepts && variable_concept_p (templ);
9140 if (concept_p && processing_template_decl)
9141 return var;
9143 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9144 arglist = add_outermost_template_args (tmpl_args, arglist);
9146 templ = most_general_template (templ);
9147 tree parms = DECL_TEMPLATE_PARMS (templ);
9148 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9149 /*req_all*/true,
9150 /*use_default*/true);
9152 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9154 if (complain & tf_error)
9156 error ("use of invalid variable template %qE", var);
9157 diagnose_constraints (location_of (var), templ, arglist);
9159 return error_mark_node;
9162 /* If a template-id refers to a specialization of a variable
9163 concept, then the expression is true if and only if the
9164 concept's constraints are satisfied by the given template
9165 arguments.
9167 NOTE: This is an extension of Concepts Lite TS that
9168 allows constraints to be used in expressions. */
9169 if (concept_p)
9171 tree decl = DECL_TEMPLATE_RESULT (templ);
9172 return evaluate_variable_concept (decl, arglist);
9175 return instantiate_template (templ, arglist, complain);
9178 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9179 TARGS template args, and instantiate it if it's not dependent. */
9181 tree
9182 lookup_and_finish_template_variable (tree templ, tree targs,
9183 tsubst_flags_t complain)
9185 templ = lookup_template_variable (templ, targs);
9186 if (!any_dependent_template_arguments_p (targs))
9188 templ = finish_template_variable (templ, complain);
9189 mark_used (templ);
9192 return convert_from_reference (templ);
9196 struct pair_fn_data
9198 tree_fn_t fn;
9199 tree_fn_t any_fn;
9200 void *data;
9201 /* True when we should also visit template parameters that occur in
9202 non-deduced contexts. */
9203 bool include_nondeduced_p;
9204 hash_set<tree> *visited;
9207 /* Called from for_each_template_parm via walk_tree. */
9209 static tree
9210 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9212 tree t = *tp;
9213 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9214 tree_fn_t fn = pfd->fn;
9215 void *data = pfd->data;
9216 tree result = NULL_TREE;
9218 #define WALK_SUBTREE(NODE) \
9219 do \
9221 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9222 pfd->include_nondeduced_p, \
9223 pfd->any_fn); \
9224 if (result) goto out; \
9226 while (0)
9228 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9229 return t;
9231 if (TYPE_P (t)
9232 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9233 WALK_SUBTREE (TYPE_CONTEXT (t));
9235 switch (TREE_CODE (t))
9237 case RECORD_TYPE:
9238 if (TYPE_PTRMEMFUNC_P (t))
9239 break;
9240 /* Fall through. */
9242 case UNION_TYPE:
9243 case ENUMERAL_TYPE:
9244 if (!TYPE_TEMPLATE_INFO (t))
9245 *walk_subtrees = 0;
9246 else
9247 WALK_SUBTREE (TYPE_TI_ARGS (t));
9248 break;
9250 case INTEGER_TYPE:
9251 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9252 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9253 break;
9255 case METHOD_TYPE:
9256 /* Since we're not going to walk subtrees, we have to do this
9257 explicitly here. */
9258 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9259 /* Fall through. */
9261 case FUNCTION_TYPE:
9262 /* Check the return type. */
9263 WALK_SUBTREE (TREE_TYPE (t));
9265 /* Check the parameter types. Since default arguments are not
9266 instantiated until they are needed, the TYPE_ARG_TYPES may
9267 contain expressions that involve template parameters. But,
9268 no-one should be looking at them yet. And, once they're
9269 instantiated, they don't contain template parameters, so
9270 there's no point in looking at them then, either. */
9272 tree parm;
9274 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9275 WALK_SUBTREE (TREE_VALUE (parm));
9277 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9278 want walk_tree walking into them itself. */
9279 *walk_subtrees = 0;
9282 if (flag_noexcept_type)
9284 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9285 if (spec)
9286 WALK_SUBTREE (TREE_PURPOSE (spec));
9288 break;
9290 case TYPEOF_TYPE:
9291 case UNDERLYING_TYPE:
9292 if (pfd->include_nondeduced_p
9293 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9294 pfd->visited,
9295 pfd->include_nondeduced_p,
9296 pfd->any_fn))
9297 return error_mark_node;
9298 break;
9300 case FUNCTION_DECL:
9301 case VAR_DECL:
9302 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9303 WALK_SUBTREE (DECL_TI_ARGS (t));
9304 /* Fall through. */
9306 case PARM_DECL:
9307 case CONST_DECL:
9308 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9309 WALK_SUBTREE (DECL_INITIAL (t));
9310 if (DECL_CONTEXT (t)
9311 && pfd->include_nondeduced_p)
9312 WALK_SUBTREE (DECL_CONTEXT (t));
9313 break;
9315 case BOUND_TEMPLATE_TEMPLATE_PARM:
9316 /* Record template parameters such as `T' inside `TT<T>'. */
9317 WALK_SUBTREE (TYPE_TI_ARGS (t));
9318 /* Fall through. */
9320 case TEMPLATE_TEMPLATE_PARM:
9321 case TEMPLATE_TYPE_PARM:
9322 case TEMPLATE_PARM_INDEX:
9323 if (fn && (*fn)(t, data))
9324 return t;
9325 else if (!fn)
9326 return t;
9327 break;
9329 case TEMPLATE_DECL:
9330 /* A template template parameter is encountered. */
9331 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9332 WALK_SUBTREE (TREE_TYPE (t));
9334 /* Already substituted template template parameter */
9335 *walk_subtrees = 0;
9336 break;
9338 case TYPENAME_TYPE:
9339 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9340 partial instantiation. */
9341 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9342 break;
9344 case CONSTRUCTOR:
9345 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9346 && pfd->include_nondeduced_p)
9347 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9348 break;
9350 case INDIRECT_REF:
9351 case COMPONENT_REF:
9352 /* If there's no type, then this thing must be some expression
9353 involving template parameters. */
9354 if (!fn && !TREE_TYPE (t))
9355 return error_mark_node;
9356 break;
9358 case MODOP_EXPR:
9359 case CAST_EXPR:
9360 case IMPLICIT_CONV_EXPR:
9361 case REINTERPRET_CAST_EXPR:
9362 case CONST_CAST_EXPR:
9363 case STATIC_CAST_EXPR:
9364 case DYNAMIC_CAST_EXPR:
9365 case ARROW_EXPR:
9366 case DOTSTAR_EXPR:
9367 case TYPEID_EXPR:
9368 case PSEUDO_DTOR_EXPR:
9369 if (!fn)
9370 return error_mark_node;
9371 break;
9373 default:
9374 break;
9377 #undef WALK_SUBTREE
9379 /* We didn't find any template parameters we liked. */
9380 out:
9381 return result;
9384 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9385 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9386 call FN with the parameter and the DATA.
9387 If FN returns nonzero, the iteration is terminated, and
9388 for_each_template_parm returns 1. Otherwise, the iteration
9389 continues. If FN never returns a nonzero value, the value
9390 returned by for_each_template_parm is 0. If FN is NULL, it is
9391 considered to be the function which always returns 1.
9393 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9394 parameters that occur in non-deduced contexts. When false, only
9395 visits those template parameters that can be deduced. */
9397 static tree
9398 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9399 hash_set<tree> *visited,
9400 bool include_nondeduced_p,
9401 tree_fn_t any_fn)
9403 struct pair_fn_data pfd;
9404 tree result;
9406 /* Set up. */
9407 pfd.fn = fn;
9408 pfd.any_fn = any_fn;
9409 pfd.data = data;
9410 pfd.include_nondeduced_p = include_nondeduced_p;
9412 /* Walk the tree. (Conceptually, we would like to walk without
9413 duplicates, but for_each_template_parm_r recursively calls
9414 for_each_template_parm, so we would need to reorganize a fair
9415 bit to use walk_tree_without_duplicates, so we keep our own
9416 visited list.) */
9417 if (visited)
9418 pfd.visited = visited;
9419 else
9420 pfd.visited = new hash_set<tree>;
9421 result = cp_walk_tree (&t,
9422 for_each_template_parm_r,
9423 &pfd,
9424 pfd.visited);
9426 /* Clean up. */
9427 if (!visited)
9429 delete pfd.visited;
9430 pfd.visited = 0;
9433 return result;
9436 /* Returns true if T depends on any template parameter. */
9439 uses_template_parms (tree t)
9441 if (t == NULL_TREE)
9442 return false;
9444 bool dependent_p;
9445 int saved_processing_template_decl;
9447 saved_processing_template_decl = processing_template_decl;
9448 if (!saved_processing_template_decl)
9449 processing_template_decl = 1;
9450 if (TYPE_P (t))
9451 dependent_p = dependent_type_p (t);
9452 else if (TREE_CODE (t) == TREE_VEC)
9453 dependent_p = any_dependent_template_arguments_p (t);
9454 else if (TREE_CODE (t) == TREE_LIST)
9455 dependent_p = (uses_template_parms (TREE_VALUE (t))
9456 || uses_template_parms (TREE_CHAIN (t)));
9457 else if (TREE_CODE (t) == TYPE_DECL)
9458 dependent_p = dependent_type_p (TREE_TYPE (t));
9459 else if (DECL_P (t)
9460 || EXPR_P (t)
9461 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9462 || TREE_CODE (t) == OVERLOAD
9463 || BASELINK_P (t)
9464 || identifier_p (t)
9465 || TREE_CODE (t) == TRAIT_EXPR
9466 || TREE_CODE (t) == CONSTRUCTOR
9467 || CONSTANT_CLASS_P (t))
9468 dependent_p = (type_dependent_expression_p (t)
9469 || value_dependent_expression_p (t));
9470 else
9472 gcc_assert (t == error_mark_node);
9473 dependent_p = false;
9476 processing_template_decl = saved_processing_template_decl;
9478 return dependent_p;
9481 /* Returns true iff current_function_decl is an incompletely instantiated
9482 template. Useful instead of processing_template_decl because the latter
9483 is set to 0 during instantiate_non_dependent_expr. */
9485 bool
9486 in_template_function (void)
9488 tree fn = current_function_decl;
9489 bool ret;
9490 ++processing_template_decl;
9491 ret = (fn && DECL_LANG_SPECIFIC (fn)
9492 && DECL_TEMPLATE_INFO (fn)
9493 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9494 --processing_template_decl;
9495 return ret;
9498 /* Returns true iff we are currently within a template other than a
9499 default-capturing generic lambda, so we don't need to worry about semantic
9500 processing. */
9502 bool
9503 processing_nonlambda_template (void)
9505 return processing_template_decl && !need_generic_capture ();
9508 /* Returns true if T depends on any template parameter with level LEVEL. */
9510 bool
9511 uses_template_parms_level (tree t, int level)
9513 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9514 /*include_nondeduced_p=*/true);
9517 /* Returns true if the signature of DECL depends on any template parameter from
9518 its enclosing class. */
9520 bool
9521 uses_outer_template_parms (tree decl)
9523 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9524 if (depth == 0)
9525 return false;
9526 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9527 &depth, NULL, /*include_nondeduced_p=*/true))
9528 return true;
9529 if (PRIMARY_TEMPLATE_P (decl)
9530 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9531 (DECL_TEMPLATE_PARMS (decl)),
9532 template_parm_outer_level,
9533 &depth, NULL, /*include_nondeduced_p=*/true))
9534 return true;
9535 tree ci = get_constraints (decl);
9536 if (ci)
9537 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9538 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9539 &depth, NULL, /*nondeduced*/true))
9540 return true;
9541 return false;
9544 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9545 ill-formed translation unit, i.e. a variable or function that isn't
9546 usable in a constant expression. */
9548 static inline bool
9549 neglectable_inst_p (tree d)
9551 return (DECL_P (d)
9552 && !undeduced_auto_decl (d)
9553 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9554 : decl_maybe_constant_var_p (d)));
9557 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9558 neglectable and instantiated from within an erroneous instantiation. */
9560 static bool
9561 limit_bad_template_recursion (tree decl)
9563 struct tinst_level *lev = current_tinst_level;
9564 int errs = errorcount + sorrycount;
9565 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9566 return false;
9568 for (; lev; lev = lev->next)
9569 if (neglectable_inst_p (lev->decl))
9570 break;
9572 return (lev && errs > lev->errors);
9575 static int tinst_depth;
9576 extern int max_tinst_depth;
9577 int depth_reached;
9579 static GTY(()) struct tinst_level *last_error_tinst_level;
9581 /* We're starting to instantiate D; record the template instantiation context
9582 for diagnostics and to restore it later. */
9584 bool
9585 push_tinst_level (tree d)
9587 return push_tinst_level_loc (d, input_location);
9590 /* We're starting to instantiate D; record the template instantiation context
9591 at LOC for diagnostics and to restore it later. */
9593 bool
9594 push_tinst_level_loc (tree d, location_t loc)
9596 struct tinst_level *new_level;
9598 if (tinst_depth >= max_tinst_depth)
9600 /* Tell error.c not to try to instantiate any templates. */
9601 at_eof = 2;
9602 fatal_error (input_location,
9603 "template instantiation depth exceeds maximum of %d"
9604 " (use -ftemplate-depth= to increase the maximum)",
9605 max_tinst_depth);
9606 return false;
9609 /* If the current instantiation caused problems, don't let it instantiate
9610 anything else. Do allow deduction substitution and decls usable in
9611 constant expressions. */
9612 if (limit_bad_template_recursion (d))
9613 return false;
9615 /* When not -quiet, dump template instantiations other than functions, since
9616 announce_function will take care of those. */
9617 if (!quiet_flag
9618 && TREE_CODE (d) != TREE_LIST
9619 && TREE_CODE (d) != FUNCTION_DECL)
9620 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9622 new_level = ggc_alloc<tinst_level> ();
9623 new_level->decl = d;
9624 new_level->locus = loc;
9625 new_level->errors = errorcount+sorrycount;
9626 new_level->in_system_header_p = in_system_header_at (input_location);
9627 new_level->next = current_tinst_level;
9628 current_tinst_level = new_level;
9630 ++tinst_depth;
9631 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9632 depth_reached = tinst_depth;
9634 return true;
9637 /* We're done instantiating this template; return to the instantiation
9638 context. */
9640 void
9641 pop_tinst_level (void)
9643 /* Restore the filename and line number stashed away when we started
9644 this instantiation. */
9645 input_location = current_tinst_level->locus;
9646 current_tinst_level = current_tinst_level->next;
9647 --tinst_depth;
9650 /* We're instantiating a deferred template; restore the template
9651 instantiation context in which the instantiation was requested, which
9652 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9654 static tree
9655 reopen_tinst_level (struct tinst_level *level)
9657 struct tinst_level *t;
9659 tinst_depth = 0;
9660 for (t = level; t; t = t->next)
9661 ++tinst_depth;
9663 current_tinst_level = level;
9664 pop_tinst_level ();
9665 if (current_tinst_level)
9666 current_tinst_level->errors = errorcount+sorrycount;
9667 return level->decl;
9670 /* Returns the TINST_LEVEL which gives the original instantiation
9671 context. */
9673 struct tinst_level *
9674 outermost_tinst_level (void)
9676 struct tinst_level *level = current_tinst_level;
9677 if (level)
9678 while (level->next)
9679 level = level->next;
9680 return level;
9683 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9684 vector of template arguments, as for tsubst.
9686 Returns an appropriate tsubst'd friend declaration. */
9688 static tree
9689 tsubst_friend_function (tree decl, tree args)
9691 tree new_friend;
9693 if (TREE_CODE (decl) == FUNCTION_DECL
9694 && DECL_TEMPLATE_INSTANTIATION (decl)
9695 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9696 /* This was a friend declared with an explicit template
9697 argument list, e.g.:
9699 friend void f<>(T);
9701 to indicate that f was a template instantiation, not a new
9702 function declaration. Now, we have to figure out what
9703 instantiation of what template. */
9705 tree template_id, arglist, fns;
9706 tree new_args;
9707 tree tmpl;
9708 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9710 /* Friend functions are looked up in the containing namespace scope.
9711 We must enter that scope, to avoid finding member functions of the
9712 current class with same name. */
9713 push_nested_namespace (ns);
9714 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9715 tf_warning_or_error, NULL_TREE,
9716 /*integral_constant_expression_p=*/false);
9717 pop_nested_namespace (ns);
9718 arglist = tsubst (DECL_TI_ARGS (decl), args,
9719 tf_warning_or_error, NULL_TREE);
9720 template_id = lookup_template_function (fns, arglist);
9722 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9723 tmpl = determine_specialization (template_id, new_friend,
9724 &new_args,
9725 /*need_member_template=*/0,
9726 TREE_VEC_LENGTH (args),
9727 tsk_none);
9728 return instantiate_template (tmpl, new_args, tf_error);
9731 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9733 /* The NEW_FRIEND will look like an instantiation, to the
9734 compiler, but is not an instantiation from the point of view of
9735 the language. For example, we might have had:
9737 template <class T> struct S {
9738 template <class U> friend void f(T, U);
9741 Then, in S<int>, template <class U> void f(int, U) is not an
9742 instantiation of anything. */
9743 if (new_friend == error_mark_node)
9744 return error_mark_node;
9746 DECL_USE_TEMPLATE (new_friend) = 0;
9747 if (TREE_CODE (decl) == TEMPLATE_DECL)
9749 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9750 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9751 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9754 /* The mangled name for the NEW_FRIEND is incorrect. The function
9755 is not a template instantiation and should not be mangled like
9756 one. Therefore, we forget the mangling here; we'll recompute it
9757 later if we need it. */
9758 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9760 SET_DECL_RTL (new_friend, NULL);
9761 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9764 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9766 tree old_decl;
9767 tree new_friend_template_info;
9768 tree new_friend_result_template_info;
9769 tree ns;
9770 int new_friend_is_defn;
9772 /* We must save some information from NEW_FRIEND before calling
9773 duplicate decls since that function will free NEW_FRIEND if
9774 possible. */
9775 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9776 new_friend_is_defn =
9777 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9778 (template_for_substitution (new_friend)))
9779 != NULL_TREE);
9780 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9782 /* This declaration is a `primary' template. */
9783 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9785 new_friend_result_template_info
9786 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9788 else
9789 new_friend_result_template_info = NULL_TREE;
9791 /* Inside pushdecl_namespace_level, we will push into the
9792 current namespace. However, the friend function should go
9793 into the namespace of the template. */
9794 ns = decl_namespace_context (new_friend);
9795 push_nested_namespace (ns);
9796 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9797 pop_nested_namespace (ns);
9799 if (old_decl == error_mark_node)
9800 return error_mark_node;
9802 if (old_decl != new_friend)
9804 /* This new friend declaration matched an existing
9805 declaration. For example, given:
9807 template <class T> void f(T);
9808 template <class U> class C {
9809 template <class T> friend void f(T) {}
9812 the friend declaration actually provides the definition
9813 of `f', once C has been instantiated for some type. So,
9814 old_decl will be the out-of-class template declaration,
9815 while new_friend is the in-class definition.
9817 But, if `f' was called before this point, the
9818 instantiation of `f' will have DECL_TI_ARGS corresponding
9819 to `T' but not to `U', references to which might appear
9820 in the definition of `f'. Previously, the most general
9821 template for an instantiation of `f' was the out-of-class
9822 version; now it is the in-class version. Therefore, we
9823 run through all specialization of `f', adding to their
9824 DECL_TI_ARGS appropriately. In particular, they need a
9825 new set of outer arguments, corresponding to the
9826 arguments for this class instantiation.
9828 The same situation can arise with something like this:
9830 friend void f(int);
9831 template <class T> class C {
9832 friend void f(T) {}
9835 when `C<int>' is instantiated. Now, `f(int)' is defined
9836 in the class. */
9838 if (!new_friend_is_defn)
9839 /* On the other hand, if the in-class declaration does
9840 *not* provide a definition, then we don't want to alter
9841 existing definitions. We can just leave everything
9842 alone. */
9844 else
9846 tree new_template = TI_TEMPLATE (new_friend_template_info);
9847 tree new_args = TI_ARGS (new_friend_template_info);
9849 /* Overwrite whatever template info was there before, if
9850 any, with the new template information pertaining to
9851 the declaration. */
9852 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9854 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9856 /* We should have called reregister_specialization in
9857 duplicate_decls. */
9858 gcc_assert (retrieve_specialization (new_template,
9859 new_args, 0)
9860 == old_decl);
9862 /* Instantiate it if the global has already been used. */
9863 if (DECL_ODR_USED (old_decl))
9864 instantiate_decl (old_decl, /*defer_ok=*/true,
9865 /*expl_inst_class_mem_p=*/false);
9867 else
9869 tree t;
9871 /* Indicate that the old function template is a partial
9872 instantiation. */
9873 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9874 = new_friend_result_template_info;
9876 gcc_assert (new_template
9877 == most_general_template (new_template));
9878 gcc_assert (new_template != old_decl);
9880 /* Reassign any specializations already in the hash table
9881 to the new more general template, and add the
9882 additional template args. */
9883 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9884 t != NULL_TREE;
9885 t = TREE_CHAIN (t))
9887 tree spec = TREE_VALUE (t);
9888 spec_entry elt;
9890 elt.tmpl = old_decl;
9891 elt.args = DECL_TI_ARGS (spec);
9892 elt.spec = NULL_TREE;
9894 decl_specializations->remove_elt (&elt);
9896 DECL_TI_ARGS (spec)
9897 = add_outermost_template_args (new_args,
9898 DECL_TI_ARGS (spec));
9900 register_specialization
9901 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9904 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9908 /* The information from NEW_FRIEND has been merged into OLD_DECL
9909 by duplicate_decls. */
9910 new_friend = old_decl;
9913 else
9915 tree context = DECL_CONTEXT (new_friend);
9916 bool dependent_p;
9918 /* In the code
9919 template <class T> class C {
9920 template <class U> friend void C1<U>::f (); // case 1
9921 friend void C2<T>::f (); // case 2
9923 we only need to make sure CONTEXT is a complete type for
9924 case 2. To distinguish between the two cases, we note that
9925 CONTEXT of case 1 remains dependent type after tsubst while
9926 this isn't true for case 2. */
9927 ++processing_template_decl;
9928 dependent_p = dependent_type_p (context);
9929 --processing_template_decl;
9931 if (!dependent_p
9932 && !complete_type_or_else (context, NULL_TREE))
9933 return error_mark_node;
9935 if (COMPLETE_TYPE_P (context))
9937 tree fn = new_friend;
9938 /* do_friend adds the TEMPLATE_DECL for any member friend
9939 template even if it isn't a member template, i.e.
9940 template <class T> friend A<T>::f();
9941 Look through it in that case. */
9942 if (TREE_CODE (fn) == TEMPLATE_DECL
9943 && !PRIMARY_TEMPLATE_P (fn))
9944 fn = DECL_TEMPLATE_RESULT (fn);
9945 /* Check to see that the declaration is really present, and,
9946 possibly obtain an improved declaration. */
9947 fn = check_classfn (context, fn, NULL_TREE);
9949 if (fn)
9950 new_friend = fn;
9954 return new_friend;
9957 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9958 template arguments, as for tsubst.
9960 Returns an appropriate tsubst'd friend type or error_mark_node on
9961 failure. */
9963 static tree
9964 tsubst_friend_class (tree friend_tmpl, tree args)
9966 tree friend_type;
9967 tree tmpl;
9968 tree context;
9970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9972 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9973 return TREE_TYPE (t);
9976 context = CP_DECL_CONTEXT (friend_tmpl);
9978 if (context != global_namespace)
9980 if (TREE_CODE (context) == NAMESPACE_DECL)
9981 push_nested_namespace (context);
9982 else
9983 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9986 /* Look for a class template declaration. We look for hidden names
9987 because two friend declarations of the same template are the
9988 same. For example, in:
9990 struct A {
9991 template <typename> friend class F;
9993 template <typename> struct B {
9994 template <typename> friend class F;
9997 both F templates are the same. */
9998 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9999 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10001 /* But, if we don't find one, it might be because we're in a
10002 situation like this:
10004 template <class T>
10005 struct S {
10006 template <class U>
10007 friend struct S;
10010 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10011 for `S<int>', not the TEMPLATE_DECL. */
10012 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10014 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10015 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10018 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10020 /* The friend template has already been declared. Just
10021 check to see that the declarations match, and install any new
10022 default parameters. We must tsubst the default parameters,
10023 of course. We only need the innermost template parameters
10024 because that is all that redeclare_class_template will look
10025 at. */
10026 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10027 > TMPL_ARGS_DEPTH (args))
10029 tree parms;
10030 location_t saved_input_location;
10031 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10032 args, tf_warning_or_error);
10034 saved_input_location = input_location;
10035 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10036 tree cons = get_constraints (tmpl);
10037 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10038 input_location = saved_input_location;
10042 friend_type = TREE_TYPE (tmpl);
10044 else
10046 /* The friend template has not already been declared. In this
10047 case, the instantiation of the template class will cause the
10048 injection of this template into the global scope. */
10049 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10050 if (tmpl == error_mark_node)
10051 return error_mark_node;
10053 /* The new TMPL is not an instantiation of anything, so we
10054 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10055 the new type because that is supposed to be the corresponding
10056 template decl, i.e., TMPL. */
10057 DECL_USE_TEMPLATE (tmpl) = 0;
10058 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10059 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10060 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10061 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10063 /* Inject this template into the global scope. */
10064 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10067 if (context != global_namespace)
10069 if (TREE_CODE (context) == NAMESPACE_DECL)
10070 pop_nested_namespace (context);
10071 else
10072 pop_nested_class ();
10075 return friend_type;
10078 /* Returns zero if TYPE cannot be completed later due to circularity.
10079 Otherwise returns one. */
10081 static int
10082 can_complete_type_without_circularity (tree type)
10084 if (type == NULL_TREE || type == error_mark_node)
10085 return 0;
10086 else if (COMPLETE_TYPE_P (type))
10087 return 1;
10088 else if (TREE_CODE (type) == ARRAY_TYPE)
10089 return can_complete_type_without_circularity (TREE_TYPE (type));
10090 else if (CLASS_TYPE_P (type)
10091 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10092 return 0;
10093 else
10094 return 1;
10097 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10098 tsubst_flags_t, tree);
10100 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10101 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10103 static tree
10104 tsubst_attribute (tree t, tree *decl_p, tree args,
10105 tsubst_flags_t complain, tree in_decl)
10107 gcc_assert (ATTR_IS_DEPENDENT (t));
10109 tree val = TREE_VALUE (t);
10110 if (val == NULL_TREE)
10111 /* Nothing to do. */;
10112 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10113 && is_attribute_p ("omp declare simd",
10114 get_attribute_name (t)))
10116 tree clauses = TREE_VALUE (val);
10117 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10118 complain, in_decl);
10119 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10120 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10121 tree parms = DECL_ARGUMENTS (*decl_p);
10122 clauses
10123 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10124 if (clauses)
10125 val = build_tree_list (NULL_TREE, clauses);
10126 else
10127 val = NULL_TREE;
10129 /* If the first attribute argument is an identifier, don't
10130 pass it through tsubst. Attributes like mode, format,
10131 cleanup and several target specific attributes expect it
10132 unmodified. */
10133 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10135 tree chain
10136 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10137 /*integral_constant_expression_p=*/false);
10138 if (chain != TREE_CHAIN (val))
10139 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10141 else if (PACK_EXPANSION_P (val))
10143 /* An attribute pack expansion. */
10144 tree purp = TREE_PURPOSE (t);
10145 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10146 if (pack == error_mark_node)
10147 return error_mark_node;
10148 int len = TREE_VEC_LENGTH (pack);
10149 tree list = NULL_TREE;
10150 tree *q = &list;
10151 for (int i = 0; i < len; ++i)
10153 tree elt = TREE_VEC_ELT (pack, i);
10154 *q = build_tree_list (purp, elt);
10155 q = &TREE_CHAIN (*q);
10157 return list;
10159 else
10160 val = tsubst_expr (val, args, complain, in_decl,
10161 /*integral_constant_expression_p=*/false);
10163 if (val != TREE_VALUE (t))
10164 return build_tree_list (TREE_PURPOSE (t), val);
10165 return t;
10168 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10169 unchanged or a new TREE_LIST chain. */
10171 static tree
10172 tsubst_attributes (tree attributes, tree args,
10173 tsubst_flags_t complain, tree in_decl)
10175 tree last_dep = NULL_TREE;
10177 for (tree t = attributes; t; t = TREE_CHAIN (t))
10178 if (ATTR_IS_DEPENDENT (t))
10180 last_dep = t;
10181 attributes = copy_list (attributes);
10182 break;
10185 if (last_dep)
10186 for (tree *p = &attributes; *p; )
10188 tree t = *p;
10189 if (ATTR_IS_DEPENDENT (t))
10191 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10192 if (subst != t)
10194 *p = subst;
10196 p = &TREE_CHAIN (*p);
10197 while (*p);
10198 *p = TREE_CHAIN (t);
10199 continue;
10202 p = &TREE_CHAIN (*p);
10205 return attributes;
10208 /* Apply any attributes which had to be deferred until instantiation
10209 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10210 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10212 static void
10213 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10214 tree args, tsubst_flags_t complain, tree in_decl)
10216 tree last_dep = NULL_TREE;
10217 tree t;
10218 tree *p;
10220 if (attributes == NULL_TREE)
10221 return;
10223 if (DECL_P (*decl_p))
10225 if (TREE_TYPE (*decl_p) == error_mark_node)
10226 return;
10227 p = &DECL_ATTRIBUTES (*decl_p);
10228 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10229 to our attributes parameter. */
10230 gcc_assert (*p == attributes);
10232 else
10234 p = &TYPE_ATTRIBUTES (*decl_p);
10235 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10236 lookup_template_class_1, and should be preserved. */
10237 gcc_assert (*p != attributes);
10238 while (*p)
10239 p = &TREE_CHAIN (*p);
10242 for (t = attributes; t; t = TREE_CHAIN (t))
10243 if (ATTR_IS_DEPENDENT (t))
10245 last_dep = t;
10246 attributes = copy_list (attributes);
10247 break;
10250 *p = attributes;
10251 if (last_dep)
10253 tree late_attrs = NULL_TREE;
10254 tree *q = &late_attrs;
10256 for (; *p; )
10258 t = *p;
10259 if (ATTR_IS_DEPENDENT (t))
10261 *p = TREE_CHAIN (t);
10262 TREE_CHAIN (t) = NULL_TREE;
10263 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10265 q = &TREE_CHAIN (*q);
10266 while (*q);
10268 else
10269 p = &TREE_CHAIN (t);
10272 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10276 /* Perform (or defer) access check for typedefs that were referenced
10277 from within the template TMPL code.
10278 This is a subroutine of instantiate_decl and instantiate_class_template.
10279 TMPL is the template to consider and TARGS is the list of arguments of
10280 that template. */
10282 static void
10283 perform_typedefs_access_check (tree tmpl, tree targs)
10285 location_t saved_location;
10286 unsigned i;
10287 qualified_typedef_usage_t *iter;
10289 if (!tmpl
10290 || (!CLASS_TYPE_P (tmpl)
10291 && TREE_CODE (tmpl) != FUNCTION_DECL))
10292 return;
10294 saved_location = input_location;
10295 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10297 tree type_decl = iter->typedef_decl;
10298 tree type_scope = iter->context;
10300 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10301 continue;
10303 if (uses_template_parms (type_decl))
10304 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10305 if (uses_template_parms (type_scope))
10306 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10308 /* Make access check error messages point to the location
10309 of the use of the typedef. */
10310 input_location = iter->locus;
10311 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10312 type_decl, type_decl,
10313 tf_warning_or_error);
10315 input_location = saved_location;
10318 static tree
10319 instantiate_class_template_1 (tree type)
10321 tree templ, args, pattern, t, member;
10322 tree typedecl;
10323 tree pbinfo;
10324 tree base_list;
10325 unsigned int saved_maximum_field_alignment;
10326 tree fn_context;
10328 if (type == error_mark_node)
10329 return error_mark_node;
10331 if (COMPLETE_OR_OPEN_TYPE_P (type)
10332 || uses_template_parms (type))
10333 return type;
10335 /* Figure out which template is being instantiated. */
10336 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10337 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10339 /* Determine what specialization of the original template to
10340 instantiate. */
10341 t = most_specialized_partial_spec (type, tf_warning_or_error);
10342 if (t == error_mark_node)
10344 TYPE_BEING_DEFINED (type) = 1;
10345 return error_mark_node;
10347 else if (t)
10349 /* This TYPE is actually an instantiation of a partial
10350 specialization. We replace the innermost set of ARGS with
10351 the arguments appropriate for substitution. For example,
10352 given:
10354 template <class T> struct S {};
10355 template <class T> struct S<T*> {};
10357 and supposing that we are instantiating S<int*>, ARGS will
10358 presently be {int*} -- but we need {int}. */
10359 pattern = TREE_TYPE (t);
10360 args = TREE_PURPOSE (t);
10362 else
10364 pattern = TREE_TYPE (templ);
10365 args = CLASSTYPE_TI_ARGS (type);
10368 /* If the template we're instantiating is incomplete, then clearly
10369 there's nothing we can do. */
10370 if (!COMPLETE_TYPE_P (pattern))
10371 return type;
10373 /* If we've recursively instantiated too many templates, stop. */
10374 if (! push_tinst_level (type))
10375 return type;
10377 /* Now we're really doing the instantiation. Mark the type as in
10378 the process of being defined. */
10379 TYPE_BEING_DEFINED (type) = 1;
10381 /* We may be in the middle of deferred access check. Disable
10382 it now. */
10383 push_deferring_access_checks (dk_no_deferred);
10385 int saved_unevaluated_operand = cp_unevaluated_operand;
10386 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10388 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10389 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10390 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10391 fn_context = error_mark_node;
10392 if (!fn_context)
10393 push_to_top_level ();
10394 else
10396 cp_unevaluated_operand = 0;
10397 c_inhibit_evaluation_warnings = 0;
10399 /* Use #pragma pack from the template context. */
10400 saved_maximum_field_alignment = maximum_field_alignment;
10401 maximum_field_alignment = TYPE_PRECISION (pattern);
10403 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10405 /* Set the input location to the most specialized template definition.
10406 This is needed if tsubsting causes an error. */
10407 typedecl = TYPE_MAIN_DECL (pattern);
10408 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10409 DECL_SOURCE_LOCATION (typedecl);
10411 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10412 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10413 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10414 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10415 if (ANON_AGGR_TYPE_P (pattern))
10416 SET_ANON_AGGR_TYPE_P (type);
10417 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10419 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10420 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10421 /* Adjust visibility for template arguments. */
10422 determine_visibility (TYPE_MAIN_DECL (type));
10424 if (CLASS_TYPE_P (type))
10425 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10427 pbinfo = TYPE_BINFO (pattern);
10429 /* We should never instantiate a nested class before its enclosing
10430 class; we need to look up the nested class by name before we can
10431 instantiate it, and that lookup should instantiate the enclosing
10432 class. */
10433 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10434 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10436 base_list = NULL_TREE;
10437 if (BINFO_N_BASE_BINFOS (pbinfo))
10439 tree pbase_binfo;
10440 tree pushed_scope;
10441 int i;
10443 /* We must enter the scope containing the type, as that is where
10444 the accessibility of types named in dependent bases are
10445 looked up from. */
10446 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10448 /* Substitute into each of the bases to determine the actual
10449 basetypes. */
10450 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10452 tree base;
10453 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10454 tree expanded_bases = NULL_TREE;
10455 int idx, len = 1;
10457 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10459 expanded_bases =
10460 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10461 args, tf_error, NULL_TREE);
10462 if (expanded_bases == error_mark_node)
10463 continue;
10465 len = TREE_VEC_LENGTH (expanded_bases);
10468 for (idx = 0; idx < len; idx++)
10470 if (expanded_bases)
10471 /* Extract the already-expanded base class. */
10472 base = TREE_VEC_ELT (expanded_bases, idx);
10473 else
10474 /* Substitute to figure out the base class. */
10475 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10476 NULL_TREE);
10478 if (base == error_mark_node)
10479 continue;
10481 base_list = tree_cons (access, base, base_list);
10482 if (BINFO_VIRTUAL_P (pbase_binfo))
10483 TREE_TYPE (base_list) = integer_type_node;
10487 /* The list is now in reverse order; correct that. */
10488 base_list = nreverse (base_list);
10490 if (pushed_scope)
10491 pop_scope (pushed_scope);
10493 /* Now call xref_basetypes to set up all the base-class
10494 information. */
10495 xref_basetypes (type, base_list);
10497 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10498 (int) ATTR_FLAG_TYPE_IN_PLACE,
10499 args, tf_error, NULL_TREE);
10500 fixup_attribute_variants (type);
10502 /* Now that our base classes are set up, enter the scope of the
10503 class, so that name lookups into base classes, etc. will work
10504 correctly. This is precisely analogous to what we do in
10505 begin_class_definition when defining an ordinary non-template
10506 class, except we also need to push the enclosing classes. */
10507 push_nested_class (type);
10509 /* Now members are processed in the order of declaration. */
10510 for (member = CLASSTYPE_DECL_LIST (pattern);
10511 member; member = TREE_CHAIN (member))
10513 tree t = TREE_VALUE (member);
10515 if (TREE_PURPOSE (member))
10517 if (TYPE_P (t))
10519 /* Build new CLASSTYPE_NESTED_UTDS. */
10521 tree newtag;
10522 bool class_template_p;
10524 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10525 && TYPE_LANG_SPECIFIC (t)
10526 && CLASSTYPE_IS_TEMPLATE (t));
10527 /* If the member is a class template, then -- even after
10528 substitution -- there may be dependent types in the
10529 template argument list for the class. We increment
10530 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10531 that function will assume that no types are dependent
10532 when outside of a template. */
10533 if (class_template_p)
10534 ++processing_template_decl;
10535 newtag = tsubst (t, args, tf_error, NULL_TREE);
10536 if (class_template_p)
10537 --processing_template_decl;
10538 if (newtag == error_mark_node)
10539 continue;
10541 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10543 tree name = TYPE_IDENTIFIER (t);
10545 if (class_template_p)
10546 /* Unfortunately, lookup_template_class sets
10547 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10548 instantiation (i.e., for the type of a member
10549 template class nested within a template class.)
10550 This behavior is required for
10551 maybe_process_partial_specialization to work
10552 correctly, but is not accurate in this case;
10553 the TAG is not an instantiation of anything.
10554 (The corresponding TEMPLATE_DECL is an
10555 instantiation, but the TYPE is not.) */
10556 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10558 /* Now, we call pushtag to put this NEWTAG into the scope of
10559 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10560 pushtag calling push_template_decl. We don't have to do
10561 this for enums because it will already have been done in
10562 tsubst_enum. */
10563 if (name)
10564 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10565 pushtag (name, newtag, /*tag_scope=*/ts_current);
10568 else if (DECL_DECLARES_FUNCTION_P (t))
10570 tree r;
10572 if (TREE_CODE (t) == TEMPLATE_DECL)
10573 ++processing_template_decl;
10574 r = tsubst (t, args, tf_error, NULL_TREE);
10575 if (TREE_CODE (t) == TEMPLATE_DECL)
10576 --processing_template_decl;
10577 set_current_access_from_decl (r);
10578 finish_member_declaration (r);
10579 /* Instantiate members marked with attribute used. */
10580 if (r != error_mark_node && DECL_PRESERVE_P (r))
10581 mark_used (r);
10582 if (TREE_CODE (r) == FUNCTION_DECL
10583 && DECL_OMP_DECLARE_REDUCTION_P (r))
10584 cp_check_omp_declare_reduction (r);
10586 else if (DECL_CLASS_TEMPLATE_P (t)
10587 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10588 /* A closure type for a lambda in a default argument for a
10589 member template. Ignore it; it will be instantiated with
10590 the default argument. */;
10591 else
10593 /* Build new TYPE_FIELDS. */
10594 if (TREE_CODE (t) == STATIC_ASSERT)
10596 tree condition;
10598 ++c_inhibit_evaluation_warnings;
10599 condition =
10600 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10601 tf_warning_or_error, NULL_TREE,
10602 /*integral_constant_expression_p=*/true);
10603 --c_inhibit_evaluation_warnings;
10605 finish_static_assert (condition,
10606 STATIC_ASSERT_MESSAGE (t),
10607 STATIC_ASSERT_SOURCE_LOCATION (t),
10608 /*member_p=*/true);
10610 else if (TREE_CODE (t) != CONST_DECL)
10612 tree r;
10613 tree vec = NULL_TREE;
10614 int len = 1;
10616 /* The file and line for this declaration, to
10617 assist in error message reporting. Since we
10618 called push_tinst_level above, we don't need to
10619 restore these. */
10620 input_location = DECL_SOURCE_LOCATION (t);
10622 if (TREE_CODE (t) == TEMPLATE_DECL)
10623 ++processing_template_decl;
10624 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10625 if (TREE_CODE (t) == TEMPLATE_DECL)
10626 --processing_template_decl;
10628 if (TREE_CODE (r) == TREE_VEC)
10630 /* A capture pack became multiple fields. */
10631 vec = r;
10632 len = TREE_VEC_LENGTH (vec);
10635 for (int i = 0; i < len; ++i)
10637 if (vec)
10638 r = TREE_VEC_ELT (vec, i);
10639 if (VAR_P (r))
10641 /* In [temp.inst]:
10643 [t]he initialization (and any associated
10644 side-effects) of a static data member does
10645 not occur unless the static data member is
10646 itself used in a way that requires the
10647 definition of the static data member to
10648 exist.
10650 Therefore, we do not substitute into the
10651 initialized for the static data member here. */
10652 finish_static_data_member_decl
10654 /*init=*/NULL_TREE,
10655 /*init_const_expr_p=*/false,
10656 /*asmspec_tree=*/NULL_TREE,
10657 /*flags=*/0);
10658 /* Instantiate members marked with attribute used. */
10659 if (r != error_mark_node && DECL_PRESERVE_P (r))
10660 mark_used (r);
10662 else if (TREE_CODE (r) == FIELD_DECL)
10664 /* Determine whether R has a valid type and can be
10665 completed later. If R is invalid, then its type
10666 is replaced by error_mark_node. */
10667 tree rtype = TREE_TYPE (r);
10668 if (can_complete_type_without_circularity (rtype))
10669 complete_type (rtype);
10671 if (!complete_or_array_type_p (rtype))
10673 /* If R's type couldn't be completed and
10674 it isn't a flexible array member (whose
10675 type is incomplete by definition) give
10676 an error. */
10677 cxx_incomplete_type_error (r, rtype);
10678 TREE_TYPE (r) = error_mark_node;
10682 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10683 such a thing will already have been added to the field
10684 list by tsubst_enum in finish_member_declaration in the
10685 CLASSTYPE_NESTED_UTDS case above. */
10686 if (!(TREE_CODE (r) == TYPE_DECL
10687 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10688 && DECL_ARTIFICIAL (r)))
10690 set_current_access_from_decl (r);
10691 finish_member_declaration (r);
10697 else
10699 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10700 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10702 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10704 tree friend_type = t;
10705 bool adjust_processing_template_decl = false;
10707 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10709 /* template <class T> friend class C; */
10710 friend_type = tsubst_friend_class (friend_type, args);
10711 adjust_processing_template_decl = true;
10713 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10715 /* template <class T> friend class C::D; */
10716 friend_type = tsubst (friend_type, args,
10717 tf_warning_or_error, NULL_TREE);
10718 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10719 friend_type = TREE_TYPE (friend_type);
10720 adjust_processing_template_decl = true;
10722 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10723 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10725 /* This could be either
10727 friend class T::C;
10729 when dependent_type_p is false or
10731 template <class U> friend class T::C;
10733 otherwise. */
10734 /* Bump processing_template_decl in case this is something like
10735 template <class T> friend struct A<T>::B. */
10736 ++processing_template_decl;
10737 friend_type = tsubst (friend_type, args,
10738 tf_warning_or_error, NULL_TREE);
10739 if (dependent_type_p (friend_type))
10740 adjust_processing_template_decl = true;
10741 --processing_template_decl;
10743 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10744 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10745 && TYPE_HIDDEN_P (friend_type))
10747 /* friend class C;
10749 where C hasn't been declared yet. Let's lookup name
10750 from namespace scope directly, bypassing any name that
10751 come from dependent base class. */
10752 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10754 /* The call to xref_tag_from_type does injection for friend
10755 classes. */
10756 push_nested_namespace (ns);
10757 friend_type =
10758 xref_tag_from_type (friend_type, NULL_TREE,
10759 /*tag_scope=*/ts_current);
10760 pop_nested_namespace (ns);
10762 else if (uses_template_parms (friend_type))
10763 /* friend class C<T>; */
10764 friend_type = tsubst (friend_type, args,
10765 tf_warning_or_error, NULL_TREE);
10766 /* Otherwise it's
10768 friend class C;
10770 where C is already declared or
10772 friend class C<int>;
10774 We don't have to do anything in these cases. */
10776 if (adjust_processing_template_decl)
10777 /* Trick make_friend_class into realizing that the friend
10778 we're adding is a template, not an ordinary class. It's
10779 important that we use make_friend_class since it will
10780 perform some error-checking and output cross-reference
10781 information. */
10782 ++processing_template_decl;
10784 if (friend_type != error_mark_node)
10785 make_friend_class (type, friend_type, /*complain=*/false);
10787 if (adjust_processing_template_decl)
10788 --processing_template_decl;
10790 else
10792 /* Build new DECL_FRIENDLIST. */
10793 tree r;
10795 /* The file and line for this declaration, to
10796 assist in error message reporting. Since we
10797 called push_tinst_level above, we don't need to
10798 restore these. */
10799 input_location = DECL_SOURCE_LOCATION (t);
10801 if (TREE_CODE (t) == TEMPLATE_DECL)
10803 ++processing_template_decl;
10804 push_deferring_access_checks (dk_no_check);
10807 r = tsubst_friend_function (t, args);
10808 add_friend (type, r, /*complain=*/false);
10809 if (TREE_CODE (t) == TEMPLATE_DECL)
10811 pop_deferring_access_checks ();
10812 --processing_template_decl;
10818 if (fn_context)
10820 /* Restore these before substituting into the lambda capture
10821 initializers. */
10822 cp_unevaluated_operand = saved_unevaluated_operand;
10823 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10826 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10828 tree decl = lambda_function (type);
10829 if (decl)
10831 if (cxx_dialect >= cxx17)
10832 CLASSTYPE_LITERAL_P (type) = true;
10834 if (!DECL_TEMPLATE_INFO (decl)
10835 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10837 /* Set function_depth to avoid garbage collection. */
10838 ++function_depth;
10839 instantiate_decl (decl, /*defer_ok=*/false, false);
10840 --function_depth;
10843 /* We need to instantiate the capture list from the template
10844 after we've instantiated the closure members, but before we
10845 consider adding the conversion op. Also keep any captures
10846 that may have been added during instantiation of the op(). */
10847 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10848 tree tmpl_cap
10849 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10850 args, tf_warning_or_error, NULL_TREE,
10851 false, false);
10853 LAMBDA_EXPR_CAPTURE_LIST (expr)
10854 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10856 maybe_add_lambda_conv_op (type);
10858 else
10859 gcc_assert (errorcount);
10862 /* Set the file and line number information to whatever is given for
10863 the class itself. This puts error messages involving generated
10864 implicit functions at a predictable point, and the same point
10865 that would be used for non-template classes. */
10866 input_location = DECL_SOURCE_LOCATION (typedecl);
10868 unreverse_member_declarations (type);
10869 finish_struct_1 (type);
10870 TYPE_BEING_DEFINED (type) = 0;
10872 /* We don't instantiate default arguments for member functions. 14.7.1:
10874 The implicit instantiation of a class template specialization causes
10875 the implicit instantiation of the declarations, but not of the
10876 definitions or default arguments, of the class member functions,
10877 member classes, static data members and member templates.... */
10879 /* Some typedefs referenced from within the template code need to be access
10880 checked at template instantiation time, i.e now. These types were
10881 added to the template at parsing time. Let's get those and perform
10882 the access checks then. */
10883 perform_typedefs_access_check (pattern, args);
10884 perform_deferred_access_checks (tf_warning_or_error);
10885 pop_nested_class ();
10886 maximum_field_alignment = saved_maximum_field_alignment;
10887 if (!fn_context)
10888 pop_from_top_level ();
10889 pop_deferring_access_checks ();
10890 pop_tinst_level ();
10892 /* The vtable for a template class can be emitted in any translation
10893 unit in which the class is instantiated. When there is no key
10894 method, however, finish_struct_1 will already have added TYPE to
10895 the keyed_classes. */
10896 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10897 vec_safe_push (keyed_classes, type);
10899 return type;
10902 /* Wrapper for instantiate_class_template_1. */
10904 tree
10905 instantiate_class_template (tree type)
10907 tree ret;
10908 timevar_push (TV_TEMPLATE_INST);
10909 ret = instantiate_class_template_1 (type);
10910 timevar_pop (TV_TEMPLATE_INST);
10911 return ret;
10914 static tree
10915 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10917 tree r;
10919 if (!t)
10920 r = t;
10921 else if (TYPE_P (t))
10922 r = tsubst (t, args, complain, in_decl);
10923 else
10925 if (!(complain & tf_warning))
10926 ++c_inhibit_evaluation_warnings;
10927 r = tsubst_expr (t, args, complain, in_decl,
10928 /*integral_constant_expression_p=*/true);
10929 if (!(complain & tf_warning))
10930 --c_inhibit_evaluation_warnings;
10932 return r;
10935 /* Given a function parameter pack TMPL_PARM and some function parameters
10936 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10937 and set *SPEC_P to point at the next point in the list. */
10939 tree
10940 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10942 /* Collect all of the extra "packed" parameters into an
10943 argument pack. */
10944 tree parmvec;
10945 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10946 tree spec_parm = *spec_p;
10947 int i, len;
10949 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10950 if (tmpl_parm
10951 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10952 break;
10954 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10955 parmvec = make_tree_vec (len);
10956 spec_parm = *spec_p;
10957 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10958 TREE_VEC_ELT (parmvec, i) = spec_parm;
10960 /* Build the argument packs. */
10961 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10962 *spec_p = spec_parm;
10964 return argpack;
10967 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10968 NONTYPE_ARGUMENT_PACK. */
10970 static tree
10971 make_fnparm_pack (tree spec_parm)
10973 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10976 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10977 pack expansion with no extra args, 2 if it has extra args, or 0
10978 if it is not a pack expansion. */
10980 static int
10981 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10983 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10984 if (i >= TREE_VEC_LENGTH (vec))
10985 return 0;
10986 tree elt = TREE_VEC_ELT (vec, i);
10987 if (DECL_P (elt))
10988 /* A decl pack is itself an expansion. */
10989 elt = TREE_TYPE (elt);
10990 if (!PACK_EXPANSION_P (elt))
10991 return 0;
10992 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10993 return 2;
10994 return 1;
10998 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11000 static tree
11001 make_argument_pack_select (tree arg_pack, unsigned index)
11003 tree aps = make_node (ARGUMENT_PACK_SELECT);
11005 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11006 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11008 return aps;
11011 /* This is a subroutine of tsubst_pack_expansion.
11013 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11014 mechanism to store the (non complete list of) arguments of the
11015 substitution and return a non substituted pack expansion, in order
11016 to wait for when we have enough arguments to really perform the
11017 substitution. */
11019 static bool
11020 use_pack_expansion_extra_args_p (tree parm_packs,
11021 int arg_pack_len,
11022 bool has_empty_arg)
11024 /* If one pack has an expansion and another pack has a normal
11025 argument or if one pack has an empty argument and an another
11026 one hasn't then tsubst_pack_expansion cannot perform the
11027 substitution and need to fall back on the
11028 PACK_EXPANSION_EXTRA mechanism. */
11029 if (parm_packs == NULL_TREE)
11030 return false;
11031 else if (has_empty_arg)
11032 return true;
11034 bool has_expansion_arg = false;
11035 for (int i = 0 ; i < arg_pack_len; ++i)
11037 bool has_non_expansion_arg = false;
11038 for (tree parm_pack = parm_packs;
11039 parm_pack;
11040 parm_pack = TREE_CHAIN (parm_pack))
11042 tree arg = TREE_VALUE (parm_pack);
11044 int exp = argument_pack_element_is_expansion_p (arg, i);
11045 if (exp == 2)
11046 /* We can't substitute a pack expansion with extra args into
11047 our pattern. */
11048 return true;
11049 else if (exp)
11050 has_expansion_arg = true;
11051 else
11052 has_non_expansion_arg = true;
11055 if (has_expansion_arg && has_non_expansion_arg)
11056 return true;
11058 return false;
11061 /* [temp.variadic]/6 says that:
11063 The instantiation of a pack expansion [...]
11064 produces a list E1,E2, ..., En, where N is the number of elements
11065 in the pack expansion parameters.
11067 This subroutine of tsubst_pack_expansion produces one of these Ei.
11069 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11070 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11071 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11072 INDEX is the index 'i' of the element Ei to produce. ARGS,
11073 COMPLAIN, and IN_DECL are the same parameters as for the
11074 tsubst_pack_expansion function.
11076 The function returns the resulting Ei upon successful completion,
11077 or error_mark_node.
11079 Note that this function possibly modifies the ARGS parameter, so
11080 it's the responsibility of the caller to restore it. */
11082 static tree
11083 gen_elem_of_pack_expansion_instantiation (tree pattern,
11084 tree parm_packs,
11085 unsigned index,
11086 tree args /* This parm gets
11087 modified. */,
11088 tsubst_flags_t complain,
11089 tree in_decl)
11091 tree t;
11092 bool ith_elem_is_expansion = false;
11094 /* For each parameter pack, change the substitution of the parameter
11095 pack to the ith argument in its argument pack, then expand the
11096 pattern. */
11097 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11099 tree parm = TREE_PURPOSE (pack);
11100 tree arg_pack = TREE_VALUE (pack);
11101 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11103 ith_elem_is_expansion |=
11104 argument_pack_element_is_expansion_p (arg_pack, index);
11106 /* Select the Ith argument from the pack. */
11107 if (TREE_CODE (parm) == PARM_DECL
11108 || TREE_CODE (parm) == FIELD_DECL)
11110 if (index == 0)
11112 aps = make_argument_pack_select (arg_pack, index);
11113 if (!mark_used (parm, complain) && !(complain & tf_error))
11114 return error_mark_node;
11115 register_local_specialization (aps, parm);
11117 else
11118 aps = retrieve_local_specialization (parm);
11120 else
11122 int idx, level;
11123 template_parm_level_and_index (parm, &level, &idx);
11125 if (index == 0)
11127 aps = make_argument_pack_select (arg_pack, index);
11128 /* Update the corresponding argument. */
11129 TMPL_ARG (args, level, idx) = aps;
11131 else
11132 /* Re-use the ARGUMENT_PACK_SELECT. */
11133 aps = TMPL_ARG (args, level, idx);
11135 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11138 /* Substitute into the PATTERN with the (possibly altered)
11139 arguments. */
11140 if (pattern == in_decl)
11141 /* Expanding a fixed parameter pack from
11142 coerce_template_parameter_pack. */
11143 t = tsubst_decl (pattern, args, complain);
11144 else if (pattern == error_mark_node)
11145 t = error_mark_node;
11146 else if (constraint_p (pattern))
11148 if (processing_template_decl)
11149 t = tsubst_constraint (pattern, args, complain, in_decl);
11150 else
11151 t = (constraints_satisfied_p (pattern, args)
11152 ? boolean_true_node : boolean_false_node);
11154 else if (!TYPE_P (pattern))
11155 t = tsubst_expr (pattern, args, complain, in_decl,
11156 /*integral_constant_expression_p=*/false);
11157 else
11158 t = tsubst (pattern, args, complain, in_decl);
11160 /* If the Ith argument pack element is a pack expansion, then
11161 the Ith element resulting from the substituting is going to
11162 be a pack expansion as well. */
11163 if (ith_elem_is_expansion)
11164 t = make_pack_expansion (t);
11166 return t;
11169 /* When the unexpanded parameter pack in a fold expression expands to an empty
11170 sequence, the value of the expression is as follows; the program is
11171 ill-formed if the operator is not listed in this table.
11173 && true
11174 || false
11175 , void() */
11177 tree
11178 expand_empty_fold (tree t, tsubst_flags_t complain)
11180 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11181 if (!FOLD_EXPR_MODIFY_P (t))
11182 switch (code)
11184 case TRUTH_ANDIF_EXPR:
11185 return boolean_true_node;
11186 case TRUTH_ORIF_EXPR:
11187 return boolean_false_node;
11188 case COMPOUND_EXPR:
11189 return void_node;
11190 default:
11191 break;
11194 if (complain & tf_error)
11195 error_at (location_of (t),
11196 "fold of empty expansion over %O", code);
11197 return error_mark_node;
11200 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11201 form an expression that combines the two terms using the
11202 operator of T. */
11204 static tree
11205 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11207 tree op = FOLD_EXPR_OP (t);
11208 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11210 // Handle compound assignment operators.
11211 if (FOLD_EXPR_MODIFY_P (t))
11212 return build_x_modify_expr (input_location, left, code, right, complain);
11214 switch (code)
11216 case COMPOUND_EXPR:
11217 return build_x_compound_expr (input_location, left, right, complain);
11218 case DOTSTAR_EXPR:
11219 return build_m_component_ref (left, right, complain);
11220 default:
11221 return build_x_binary_op (input_location, code,
11222 left, TREE_CODE (left),
11223 right, TREE_CODE (right),
11224 /*overload=*/NULL,
11225 complain);
11229 /* Substitute ARGS into the pack of a fold expression T. */
11231 static inline tree
11232 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11234 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11237 /* Substitute ARGS into the pack of a fold expression T. */
11239 static inline tree
11240 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11242 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11245 /* Expand a PACK of arguments into a grouped as left fold.
11246 Given a pack containing elements A0, A1, ..., An and an
11247 operator @, this builds the expression:
11249 ((A0 @ A1) @ A2) ... @ An
11251 Note that PACK must not be empty.
11253 The operator is defined by the original fold expression T. */
11255 static tree
11256 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11258 tree left = TREE_VEC_ELT (pack, 0);
11259 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11261 tree right = TREE_VEC_ELT (pack, i);
11262 left = fold_expression (t, left, right, complain);
11264 return left;
11267 /* Substitute into a unary left fold expression. */
11269 static tree
11270 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11271 tree in_decl)
11273 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11274 if (pack == error_mark_node)
11275 return error_mark_node;
11276 if (PACK_EXPANSION_P (pack))
11278 tree r = copy_node (t);
11279 FOLD_EXPR_PACK (r) = pack;
11280 return r;
11282 if (TREE_VEC_LENGTH (pack) == 0)
11283 return expand_empty_fold (t, complain);
11284 else
11285 return expand_left_fold (t, pack, complain);
11288 /* Substitute into a binary left fold expression.
11290 Do ths by building a single (non-empty) vector of argumnts and
11291 building the expression from those elements. */
11293 static tree
11294 tsubst_binary_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 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11301 if (init == error_mark_node)
11302 return error_mark_node;
11304 if (PACK_EXPANSION_P (pack))
11306 tree r = copy_node (t);
11307 FOLD_EXPR_PACK (r) = pack;
11308 FOLD_EXPR_INIT (r) = init;
11309 return r;
11312 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11313 TREE_VEC_ELT (vec, 0) = init;
11314 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11315 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11317 return expand_left_fold (t, vec, complain);
11320 /* Expand a PACK of arguments into a grouped as right fold.
11321 Given a pack containing elementns A0, A1, ..., and an
11322 operator @, this builds the expression:
11324 A0@ ... (An-2 @ (An-1 @ An))
11326 Note that PACK must not be empty.
11328 The operator is defined by the original fold expression T. */
11330 tree
11331 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11333 // Build the expression.
11334 int n = TREE_VEC_LENGTH (pack);
11335 tree right = TREE_VEC_ELT (pack, n - 1);
11336 for (--n; n != 0; --n)
11338 tree left = TREE_VEC_ELT (pack, n - 1);
11339 right = fold_expression (t, left, right, complain);
11341 return right;
11344 /* Substitute into a unary right fold expression. */
11346 static tree
11347 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11348 tree in_decl)
11350 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11351 if (pack == error_mark_node)
11352 return error_mark_node;
11353 if (PACK_EXPANSION_P (pack))
11355 tree r = copy_node (t);
11356 FOLD_EXPR_PACK (r) = pack;
11357 return r;
11359 if (TREE_VEC_LENGTH (pack) == 0)
11360 return expand_empty_fold (t, complain);
11361 else
11362 return expand_right_fold (t, pack, complain);
11365 /* Substitute into a binary right fold expression.
11367 Do ths by building a single (non-empty) vector of arguments and
11368 building the expression from those elements. */
11370 static tree
11371 tsubst_binary_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 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11378 if (init == error_mark_node)
11379 return error_mark_node;
11381 if (PACK_EXPANSION_P (pack))
11383 tree r = copy_node (t);
11384 FOLD_EXPR_PACK (r) = pack;
11385 FOLD_EXPR_INIT (r) = init;
11386 return r;
11389 int n = TREE_VEC_LENGTH (pack);
11390 tree vec = make_tree_vec (n + 1);
11391 for (int i = 0; i < n; ++i)
11392 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11393 TREE_VEC_ELT (vec, n) = init;
11395 return expand_right_fold (t, vec, complain);
11399 /* Substitute ARGS into T, which is an pack expansion
11400 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11401 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11402 (if only a partial substitution could be performed) or
11403 ERROR_MARK_NODE if there was an error. */
11404 tree
11405 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11406 tree in_decl)
11408 tree pattern;
11409 tree pack, packs = NULL_TREE;
11410 bool unsubstituted_packs = false;
11411 int i, len = -1;
11412 tree result;
11413 hash_map<tree, tree> *saved_local_specializations = NULL;
11414 bool need_local_specializations = false;
11415 int levels;
11417 gcc_assert (PACK_EXPANSION_P (t));
11418 pattern = PACK_EXPANSION_PATTERN (t);
11420 /* Add in any args remembered from an earlier partial instantiation. */
11421 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11423 levels = TMPL_ARGS_DEPTH (args);
11425 /* Determine the argument packs that will instantiate the parameter
11426 packs used in the expansion expression. While we're at it,
11427 compute the number of arguments to be expanded and make sure it
11428 is consistent. */
11429 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11430 pack = TREE_CHAIN (pack))
11432 tree parm_pack = TREE_VALUE (pack);
11433 tree arg_pack = NULL_TREE;
11434 tree orig_arg = NULL_TREE;
11435 int level = 0;
11437 if (TREE_CODE (parm_pack) == BASES)
11439 gcc_assert (parm_pack == pattern);
11440 if (BASES_DIRECT (parm_pack))
11441 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11442 args, complain, in_decl, false));
11443 else
11444 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11445 args, complain, in_decl, false));
11447 else if (builtin_pack_call_p (parm_pack))
11449 /* ??? Support use in other patterns. */
11450 gcc_assert (parm_pack == pattern);
11451 return expand_builtin_pack_call (parm_pack, args,
11452 complain, in_decl);
11454 else if (TREE_CODE (parm_pack) == PARM_DECL)
11456 /* We know we have correct local_specializations if this
11457 expansion is at function scope, or if we're dealing with a
11458 local parameter in a requires expression; for the latter,
11459 tsubst_requires_expr set it up appropriately. */
11460 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11461 arg_pack = retrieve_local_specialization (parm_pack);
11462 else
11463 /* We can't rely on local_specializations for a parameter
11464 name used later in a function declaration (such as in a
11465 late-specified return type). Even if it exists, it might
11466 have the wrong value for a recursive call. */
11467 need_local_specializations = true;
11469 if (!arg_pack)
11471 /* This parameter pack was used in an unevaluated context. Just
11472 make a dummy decl, since it's only used for its type. */
11473 arg_pack = tsubst_decl (parm_pack, args, complain);
11474 if (arg_pack && DECL_PACK_P (arg_pack))
11475 /* Partial instantiation of the parm_pack, we can't build
11476 up an argument pack yet. */
11477 arg_pack = NULL_TREE;
11478 else
11479 arg_pack = make_fnparm_pack (arg_pack);
11482 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11483 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11484 else
11486 int idx;
11487 template_parm_level_and_index (parm_pack, &level, &idx);
11489 if (level <= levels)
11490 arg_pack = TMPL_ARG (args, level, idx);
11493 orig_arg = arg_pack;
11494 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11495 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11497 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11498 /* This can only happen if we forget to expand an argument
11499 pack somewhere else. Just return an error, silently. */
11501 result = make_tree_vec (1);
11502 TREE_VEC_ELT (result, 0) = error_mark_node;
11503 return result;
11506 if (arg_pack)
11508 int my_len =
11509 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11511 /* Don't bother trying to do a partial substitution with
11512 incomplete packs; we'll try again after deduction. */
11513 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11514 return t;
11516 if (len < 0)
11517 len = my_len;
11518 else if (len != my_len)
11520 if (!(complain & tf_error))
11521 /* Fail quietly. */;
11522 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11523 error ("mismatched argument pack lengths while expanding %qT",
11524 pattern);
11525 else
11526 error ("mismatched argument pack lengths while expanding %qE",
11527 pattern);
11528 return error_mark_node;
11531 /* Keep track of the parameter packs and their corresponding
11532 argument packs. */
11533 packs = tree_cons (parm_pack, arg_pack, packs);
11534 TREE_TYPE (packs) = orig_arg;
11536 else
11538 /* We can't substitute for this parameter pack. We use a flag as
11539 well as the missing_level counter because function parameter
11540 packs don't have a level. */
11541 gcc_assert (processing_template_decl);
11542 unsubstituted_packs = true;
11546 /* If the expansion is just T..., return the matching argument pack, unless
11547 we need to call convert_from_reference on all the elements. This is an
11548 important optimization; see c++/68422. */
11549 if (!unsubstituted_packs
11550 && TREE_PURPOSE (packs) == pattern)
11552 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11553 /* Types need no adjustment, nor does sizeof..., and if we still have
11554 some pack expansion args we won't do anything yet. */
11555 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11556 || PACK_EXPANSION_SIZEOF_P (t)
11557 || pack_expansion_args_count (args))
11558 return args;
11559 /* Also optimize expression pack expansions if we can tell that the
11560 elements won't have reference type. */
11561 tree type = TREE_TYPE (pattern);
11562 if (type && TREE_CODE (type) != REFERENCE_TYPE
11563 && !PACK_EXPANSION_P (type)
11564 && !WILDCARD_TYPE_P (type))
11565 return args;
11566 /* Otherwise use the normal path so we get convert_from_reference. */
11569 /* We cannot expand this expansion expression, because we don't have
11570 all of the argument packs we need. */
11571 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11573 /* We got some full packs, but we can't substitute them in until we
11574 have values for all the packs. So remember these until then. */
11576 t = make_pack_expansion (pattern);
11577 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11578 return t;
11580 else if (unsubstituted_packs)
11582 /* There were no real arguments, we're just replacing a parameter
11583 pack with another version of itself. Substitute into the
11584 pattern and return a PACK_EXPANSION_*. The caller will need to
11585 deal with that. */
11586 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11587 t = tsubst_expr (pattern, args, complain, in_decl,
11588 /*integral_constant_expression_p=*/false);
11589 else
11590 t = tsubst (pattern, args, complain, in_decl);
11591 t = make_pack_expansion (t);
11592 return t;
11595 gcc_assert (len >= 0);
11597 if (need_local_specializations)
11599 /* We're in a late-specified return type, so create our own local
11600 specializations map; the current map is either NULL or (in the
11601 case of recursive unification) might have bindings that we don't
11602 want to use or alter. */
11603 saved_local_specializations = local_specializations;
11604 local_specializations = new hash_map<tree, tree>;
11607 /* For each argument in each argument pack, substitute into the
11608 pattern. */
11609 result = make_tree_vec (len);
11610 tree elem_args = copy_template_args (args);
11611 for (i = 0; i < len; ++i)
11613 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11615 elem_args, complain,
11616 in_decl);
11617 TREE_VEC_ELT (result, i) = t;
11618 if (t == error_mark_node)
11620 result = error_mark_node;
11621 break;
11625 /* Update ARGS to restore the substitution from parameter packs to
11626 their argument packs. */
11627 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11629 tree parm = TREE_PURPOSE (pack);
11631 if (TREE_CODE (parm) == PARM_DECL
11632 || TREE_CODE (parm) == FIELD_DECL)
11633 register_local_specialization (TREE_TYPE (pack), parm);
11634 else
11636 int idx, level;
11638 if (TREE_VALUE (pack) == NULL_TREE)
11639 continue;
11641 template_parm_level_and_index (parm, &level, &idx);
11643 /* Update the corresponding argument. */
11644 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11645 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11646 TREE_TYPE (pack);
11647 else
11648 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11652 if (need_local_specializations)
11654 delete local_specializations;
11655 local_specializations = saved_local_specializations;
11658 /* If the dependent pack arguments were such that we end up with only a
11659 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11660 if (len == 1 && TREE_CODE (result) == TREE_VEC
11661 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11662 return TREE_VEC_ELT (result, 0);
11664 return result;
11667 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11668 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11669 parameter packs; all parms generated from a function parameter pack will
11670 have the same DECL_PARM_INDEX. */
11672 tree
11673 get_pattern_parm (tree parm, tree tmpl)
11675 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11676 tree patparm;
11678 if (DECL_ARTIFICIAL (parm))
11680 for (patparm = DECL_ARGUMENTS (pattern);
11681 patparm; patparm = DECL_CHAIN (patparm))
11682 if (DECL_ARTIFICIAL (patparm)
11683 && DECL_NAME (parm) == DECL_NAME (patparm))
11684 break;
11686 else
11688 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11689 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11690 gcc_assert (DECL_PARM_INDEX (patparm)
11691 == DECL_PARM_INDEX (parm));
11694 return patparm;
11697 /* Make an argument pack out of the TREE_VEC VEC. */
11699 static tree
11700 make_argument_pack (tree vec)
11702 tree pack;
11703 tree elt = TREE_VEC_ELT (vec, 0);
11704 if (TYPE_P (elt))
11705 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11706 else
11708 pack = make_node (NONTYPE_ARGUMENT_PACK);
11709 TREE_CONSTANT (pack) = 1;
11711 SET_ARGUMENT_PACK_ARGS (pack, vec);
11712 return pack;
11715 /* Return an exact copy of template args T that can be modified
11716 independently. */
11718 static tree
11719 copy_template_args (tree t)
11721 if (t == error_mark_node)
11722 return t;
11724 int len = TREE_VEC_LENGTH (t);
11725 tree new_vec = make_tree_vec (len);
11727 for (int i = 0; i < len; ++i)
11729 tree elt = TREE_VEC_ELT (t, i);
11730 if (elt && TREE_CODE (elt) == TREE_VEC)
11731 elt = copy_template_args (elt);
11732 TREE_VEC_ELT (new_vec, i) = elt;
11735 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11736 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11738 return new_vec;
11741 /* Substitute ARGS into the vector or list of template arguments T. */
11743 static tree
11744 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11746 tree orig_t = t;
11747 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11748 tree *elts;
11750 if (t == error_mark_node)
11751 return error_mark_node;
11753 len = TREE_VEC_LENGTH (t);
11754 elts = XALLOCAVEC (tree, len);
11756 for (i = 0; i < len; i++)
11758 tree orig_arg = TREE_VEC_ELT (t, i);
11759 tree new_arg;
11761 if (TREE_CODE (orig_arg) == TREE_VEC)
11762 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11763 else if (PACK_EXPANSION_P (orig_arg))
11765 /* Substitute into an expansion expression. */
11766 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11768 if (TREE_CODE (new_arg) == TREE_VEC)
11769 /* Add to the expanded length adjustment the number of
11770 expanded arguments. We subtract one from this
11771 measurement, because the argument pack expression
11772 itself is already counted as 1 in
11773 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11774 the argument pack is empty. */
11775 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11777 else if (ARGUMENT_PACK_P (orig_arg))
11779 /* Substitute into each of the arguments. */
11780 new_arg = TYPE_P (orig_arg)
11781 ? cxx_make_type (TREE_CODE (orig_arg))
11782 : make_node (TREE_CODE (orig_arg));
11784 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11785 args, complain, in_decl);
11786 if (pack_args == error_mark_node)
11787 new_arg = error_mark_node;
11788 else
11789 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11791 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11792 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11794 else
11795 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11797 if (new_arg == error_mark_node)
11798 return error_mark_node;
11800 elts[i] = new_arg;
11801 if (new_arg != orig_arg)
11802 need_new = 1;
11805 if (!need_new)
11806 return t;
11808 /* Make space for the expanded arguments coming from template
11809 argument packs. */
11810 t = make_tree_vec (len + expanded_len_adjust);
11811 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11812 arguments for a member template.
11813 In that case each TREE_VEC in ORIG_T represents a level of template
11814 arguments, and ORIG_T won't carry any non defaulted argument count.
11815 It will rather be the nested TREE_VECs that will carry one.
11816 In other words, ORIG_T carries a non defaulted argument count only
11817 if it doesn't contain any nested TREE_VEC. */
11818 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11820 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11821 count += expanded_len_adjust;
11822 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11824 for (i = 0, out = 0; i < len; i++)
11826 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11827 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11828 && TREE_CODE (elts[i]) == TREE_VEC)
11830 int idx;
11832 /* Now expand the template argument pack "in place". */
11833 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11834 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11836 else
11838 TREE_VEC_ELT (t, out) = elts[i];
11839 out++;
11843 return t;
11846 /* Substitute ARGS into one level PARMS of template parameters. */
11848 static tree
11849 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11851 if (parms == error_mark_node)
11852 return error_mark_node;
11854 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11856 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11858 tree tuple = TREE_VEC_ELT (parms, i);
11860 if (tuple == error_mark_node)
11861 continue;
11863 TREE_VEC_ELT (new_vec, i) =
11864 tsubst_template_parm (tuple, args, complain);
11867 return new_vec;
11870 /* Return the result of substituting ARGS into the template parameters
11871 given by PARMS. If there are m levels of ARGS and m + n levels of
11872 PARMS, then the result will contain n levels of PARMS. For
11873 example, if PARMS is `template <class T> template <class U>
11874 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11875 result will be `template <int*, double, class V>'. */
11877 static tree
11878 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11880 tree r = NULL_TREE;
11881 tree* new_parms;
11883 /* When substituting into a template, we must set
11884 PROCESSING_TEMPLATE_DECL as the template parameters may be
11885 dependent if they are based on one-another, and the dependency
11886 predicates are short-circuit outside of templates. */
11887 ++processing_template_decl;
11889 for (new_parms = &r;
11890 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11891 new_parms = &(TREE_CHAIN (*new_parms)),
11892 parms = TREE_CHAIN (parms))
11894 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11895 args, complain);
11896 *new_parms =
11897 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11898 - TMPL_ARGS_DEPTH (args)),
11899 new_vec, NULL_TREE);
11902 --processing_template_decl;
11904 return r;
11907 /* Return the result of substituting ARGS into one template parameter
11908 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11909 parameter and which TREE_PURPOSE is the default argument of the
11910 template parameter. */
11912 static tree
11913 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11915 tree default_value, parm_decl;
11917 if (args == NULL_TREE
11918 || t == NULL_TREE
11919 || t == error_mark_node)
11920 return t;
11922 gcc_assert (TREE_CODE (t) == TREE_LIST);
11924 default_value = TREE_PURPOSE (t);
11925 parm_decl = TREE_VALUE (t);
11927 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11928 if (TREE_CODE (parm_decl) == PARM_DECL
11929 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11930 parm_decl = error_mark_node;
11931 default_value = tsubst_template_arg (default_value, args,
11932 complain, NULL_TREE);
11934 return build_tree_list (default_value, parm_decl);
11937 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11938 type T. If T is not an aggregate or enumeration type, it is
11939 handled as if by tsubst. IN_DECL is as for tsubst. If
11940 ENTERING_SCOPE is nonzero, T is the context for a template which
11941 we are presently tsubst'ing. Return the substituted value. */
11943 static tree
11944 tsubst_aggr_type (tree t,
11945 tree args,
11946 tsubst_flags_t complain,
11947 tree in_decl,
11948 int entering_scope)
11950 if (t == NULL_TREE)
11951 return NULL_TREE;
11953 switch (TREE_CODE (t))
11955 case RECORD_TYPE:
11956 if (TYPE_PTRMEMFUNC_P (t))
11957 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11959 /* Fall through. */
11960 case ENUMERAL_TYPE:
11961 case UNION_TYPE:
11962 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11964 tree argvec;
11965 tree context;
11966 tree r;
11967 int saved_unevaluated_operand;
11968 int saved_inhibit_evaluation_warnings;
11970 /* In "sizeof(X<I>)" we need to evaluate "I". */
11971 saved_unevaluated_operand = cp_unevaluated_operand;
11972 cp_unevaluated_operand = 0;
11973 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11974 c_inhibit_evaluation_warnings = 0;
11976 /* First, determine the context for the type we are looking
11977 up. */
11978 context = TYPE_CONTEXT (t);
11979 if (context && TYPE_P (context))
11981 context = tsubst_aggr_type (context, args, complain,
11982 in_decl, /*entering_scope=*/1);
11983 /* If context is a nested class inside a class template,
11984 it may still need to be instantiated (c++/33959). */
11985 context = complete_type (context);
11988 /* Then, figure out what arguments are appropriate for the
11989 type we are trying to find. For example, given:
11991 template <class T> struct S;
11992 template <class T, class U> void f(T, U) { S<U> su; }
11994 and supposing that we are instantiating f<int, double>,
11995 then our ARGS will be {int, double}, but, when looking up
11996 S we only want {double}. */
11997 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11998 complain, in_decl);
11999 if (argvec == error_mark_node)
12000 r = error_mark_node;
12001 else
12003 r = lookup_template_class (t, argvec, in_decl, context,
12004 entering_scope, complain);
12005 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12008 cp_unevaluated_operand = saved_unevaluated_operand;
12009 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12011 return r;
12013 else
12014 /* This is not a template type, so there's nothing to do. */
12015 return t;
12017 default:
12018 return tsubst (t, args, complain, in_decl);
12022 static GTY(()) hash_map<tree, tree> *defarg_inst;
12024 /* Substitute into the default argument ARG (a default argument for
12025 FN), which has the indicated TYPE. */
12027 tree
12028 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12029 tsubst_flags_t complain)
12031 tree saved_class_ptr = NULL_TREE;
12032 tree saved_class_ref = NULL_TREE;
12033 int errs = errorcount + sorrycount;
12035 /* This can happen in invalid code. */
12036 if (TREE_CODE (arg) == DEFAULT_ARG)
12037 return arg;
12039 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12040 parm = chain_index (parmnum, parm);
12041 tree parmtype = TREE_TYPE (parm);
12042 if (DECL_BY_REFERENCE (parm))
12043 parmtype = TREE_TYPE (parmtype);
12044 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12046 tree *slot;
12047 if (defarg_inst && (slot = defarg_inst->get (parm)))
12048 return *slot;
12050 /* This default argument came from a template. Instantiate the
12051 default argument here, not in tsubst. In the case of
12052 something like:
12054 template <class T>
12055 struct S {
12056 static T t();
12057 void f(T = t());
12060 we must be careful to do name lookup in the scope of S<T>,
12061 rather than in the current class. */
12062 push_access_scope (fn);
12063 /* The "this" pointer is not valid in a default argument. */
12064 if (cfun)
12066 saved_class_ptr = current_class_ptr;
12067 cp_function_chain->x_current_class_ptr = NULL_TREE;
12068 saved_class_ref = current_class_ref;
12069 cp_function_chain->x_current_class_ref = NULL_TREE;
12072 start_lambda_scope (parm);
12074 push_deferring_access_checks(dk_no_deferred);
12075 /* The default argument expression may cause implicitly defined
12076 member functions to be synthesized, which will result in garbage
12077 collection. We must treat this situation as if we were within
12078 the body of function so as to avoid collecting live data on the
12079 stack. */
12080 ++function_depth;
12081 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12082 complain, NULL_TREE,
12083 /*integral_constant_expression_p=*/false);
12084 --function_depth;
12085 pop_deferring_access_checks();
12087 finish_lambda_scope ();
12089 /* Restore the "this" pointer. */
12090 if (cfun)
12092 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12093 cp_function_chain->x_current_class_ref = saved_class_ref;
12096 if (errorcount+sorrycount > errs
12097 && (complain & tf_warning_or_error))
12098 inform (input_location,
12099 " when instantiating default argument for call to %qD", fn);
12101 /* Make sure the default argument is reasonable. */
12102 arg = check_default_argument (type, arg, complain);
12104 pop_access_scope (fn);
12106 if (arg != error_mark_node && !cp_unevaluated_operand)
12108 if (!defarg_inst)
12109 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12110 defarg_inst->put (parm, arg);
12113 return arg;
12116 /* Substitute into all the default arguments for FN. */
12118 static void
12119 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12121 tree arg;
12122 tree tmpl_args;
12124 tmpl_args = DECL_TI_ARGS (fn);
12126 /* If this function is not yet instantiated, we certainly don't need
12127 its default arguments. */
12128 if (uses_template_parms (tmpl_args))
12129 return;
12130 /* Don't do this again for clones. */
12131 if (DECL_CLONED_FUNCTION_P (fn))
12132 return;
12134 int i = 0;
12135 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12136 arg;
12137 arg = TREE_CHAIN (arg), ++i)
12138 if (TREE_PURPOSE (arg))
12139 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12140 TREE_VALUE (arg),
12141 TREE_PURPOSE (arg),
12142 complain);
12145 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12147 static tree
12148 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12149 tree lambda_fntype)
12151 tree gen_tmpl, argvec;
12152 hashval_t hash = 0;
12153 tree in_decl = t;
12155 /* Nobody should be tsubst'ing into non-template functions. */
12156 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12158 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12160 /* If T is not dependent, just return it. */
12161 if (!uses_template_parms (DECL_TI_ARGS (t)))
12162 return t;
12164 /* Calculate the most general template of which R is a
12165 specialization, and the complete set of arguments used to
12166 specialize R. */
12167 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12168 argvec = tsubst_template_args (DECL_TI_ARGS
12169 (DECL_TEMPLATE_RESULT
12170 (DECL_TI_TEMPLATE (t))),
12171 args, complain, in_decl);
12172 if (argvec == error_mark_node)
12173 return error_mark_node;
12175 /* Check to see if we already have this specialization. */
12176 if (!lambda_fntype)
12178 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12179 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12180 return spec;
12183 /* We can see more levels of arguments than parameters if
12184 there was a specialization of a member template, like
12185 this:
12187 template <class T> struct S { template <class U> void f(); }
12188 template <> template <class U> void S<int>::f(U);
12190 Here, we'll be substituting into the specialization,
12191 because that's where we can find the code we actually
12192 want to generate, but we'll have enough arguments for
12193 the most general template.
12195 We also deal with the peculiar case:
12197 template <class T> struct S {
12198 template <class U> friend void f();
12200 template <class U> void f() {}
12201 template S<int>;
12202 template void f<double>();
12204 Here, the ARGS for the instantiation of will be {int,
12205 double}. But, we only need as many ARGS as there are
12206 levels of template parameters in CODE_PATTERN. We are
12207 careful not to get fooled into reducing the ARGS in
12208 situations like:
12210 template <class T> struct S { template <class U> void f(U); }
12211 template <class T> template <> void S<T>::f(int) {}
12213 which we can spot because the pattern will be a
12214 specialization in this case. */
12215 int args_depth = TMPL_ARGS_DEPTH (args);
12216 int parms_depth =
12217 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12219 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12220 args = get_innermost_template_args (args, parms_depth);
12222 else
12224 /* This special case arises when we have something like this:
12226 template <class T> struct S {
12227 friend void f<int>(int, double);
12230 Here, the DECL_TI_TEMPLATE for the friend declaration
12231 will be an IDENTIFIER_NODE. We are being called from
12232 tsubst_friend_function, and we want only to create a
12233 new decl (R) with appropriate types so that we can call
12234 determine_specialization. */
12235 gen_tmpl = NULL_TREE;
12236 argvec = NULL_TREE;
12239 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12240 : NULL_TREE);
12241 tree ctx = closure ? closure : DECL_CONTEXT (t);
12242 bool member = ctx && TYPE_P (ctx);
12244 if (member && !closure)
12245 ctx = tsubst_aggr_type (ctx, args,
12246 complain, t, /*entering_scope=*/1);
12248 tree type = (lambda_fntype ? lambda_fntype
12249 : tsubst (TREE_TYPE (t), args,
12250 complain | tf_fndecl_type, in_decl));
12251 if (type == error_mark_node)
12252 return error_mark_node;
12254 /* If we hit excessive deduction depth, the type is bogus even if
12255 it isn't error_mark_node, so don't build a decl. */
12256 if (excessive_deduction_depth)
12257 return error_mark_node;
12259 /* We do NOT check for matching decls pushed separately at this
12260 point, as they may not represent instantiations of this
12261 template, and in any case are considered separate under the
12262 discrete model. */
12263 tree r = copy_decl (t);
12264 DECL_USE_TEMPLATE (r) = 0;
12265 TREE_TYPE (r) = type;
12266 /* Clear out the mangled name and RTL for the instantiation. */
12267 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12268 SET_DECL_RTL (r, NULL);
12269 /* Leave DECL_INITIAL set on deleted instantiations. */
12270 if (!DECL_DELETED_FN (r))
12271 DECL_INITIAL (r) = NULL_TREE;
12272 DECL_CONTEXT (r) = ctx;
12274 /* OpenMP UDRs have the only argument a reference to the declared
12275 type. We want to diagnose if the declared type is a reference,
12276 which is invalid, but as references to references are usually
12277 quietly merged, diagnose it here. */
12278 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12280 tree argtype
12281 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12282 argtype = tsubst (argtype, args, complain, in_decl);
12283 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12284 error_at (DECL_SOURCE_LOCATION (t),
12285 "reference type %qT in "
12286 "%<#pragma omp declare reduction%>", argtype);
12287 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12288 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12289 argtype);
12292 if (member && DECL_CONV_FN_P (r))
12293 /* Type-conversion operator. Reconstruct the name, in
12294 case it's the name of one of the template's parameters. */
12295 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12297 tree parms = DECL_ARGUMENTS (t);
12298 if (closure)
12299 parms = DECL_CHAIN (parms);
12300 parms = tsubst (parms, args, complain, t);
12301 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12302 DECL_CONTEXT (parm) = r;
12303 if (closure)
12305 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12306 DECL_CHAIN (tparm) = parms;
12307 parms = tparm;
12309 DECL_ARGUMENTS (r) = parms;
12310 DECL_RESULT (r) = NULL_TREE;
12312 TREE_STATIC (r) = 0;
12313 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12314 DECL_EXTERNAL (r) = 1;
12315 /* If this is an instantiation of a function with internal
12316 linkage, we already know what object file linkage will be
12317 assigned to the instantiation. */
12318 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12319 DECL_DEFER_OUTPUT (r) = 0;
12320 DECL_CHAIN (r) = NULL_TREE;
12321 DECL_PENDING_INLINE_INFO (r) = 0;
12322 DECL_PENDING_INLINE_P (r) = 0;
12323 DECL_SAVED_TREE (r) = NULL_TREE;
12324 DECL_STRUCT_FUNCTION (r) = NULL;
12325 TREE_USED (r) = 0;
12326 /* We'll re-clone as appropriate in instantiate_template. */
12327 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12329 /* If we aren't complaining now, return on error before we register
12330 the specialization so that we'll complain eventually. */
12331 if ((complain & tf_error) == 0
12332 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12333 && !grok_op_properties (r, /*complain=*/false))
12334 return error_mark_node;
12336 /* When instantiating a constrained member, substitute
12337 into the constraints to create a new constraint. */
12338 if (tree ci = get_constraints (t))
12339 if (member)
12341 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12342 set_constraints (r, ci);
12345 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12346 this in the special friend case mentioned above where
12347 GEN_TMPL is NULL. */
12348 if (gen_tmpl && !closure)
12350 DECL_TEMPLATE_INFO (r)
12351 = build_template_info (gen_tmpl, argvec);
12352 SET_DECL_IMPLICIT_INSTANTIATION (r);
12354 tree new_r
12355 = register_specialization (r, gen_tmpl, argvec, false, hash);
12356 if (new_r != r)
12357 /* We instantiated this while substituting into
12358 the type earlier (template/friend54.C). */
12359 return new_r;
12361 /* We're not supposed to instantiate default arguments
12362 until they are called, for a template. But, for a
12363 declaration like:
12365 template <class T> void f ()
12366 { extern void g(int i = T()); }
12368 we should do the substitution when the template is
12369 instantiated. We handle the member function case in
12370 instantiate_class_template since the default arguments
12371 might refer to other members of the class. */
12372 if (!member
12373 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12374 && !uses_template_parms (argvec))
12375 tsubst_default_arguments (r, complain);
12377 else
12378 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12380 /* Copy the list of befriending classes. */
12381 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12382 *friends;
12383 friends = &TREE_CHAIN (*friends))
12385 *friends = copy_node (*friends);
12386 TREE_VALUE (*friends)
12387 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12390 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12392 maybe_retrofit_in_chrg (r);
12393 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12394 return error_mark_node;
12395 /* If this is an instantiation of a member template, clone it.
12396 If it isn't, that'll be handled by
12397 clone_constructors_and_destructors. */
12398 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12399 clone_function_decl (r, /*update_methods=*/false);
12401 else if ((complain & tf_error) != 0
12402 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12403 && !grok_op_properties (r, /*complain=*/true))
12404 return error_mark_node;
12406 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12407 SET_DECL_FRIEND_CONTEXT (r,
12408 tsubst (DECL_FRIEND_CONTEXT (t),
12409 args, complain, in_decl));
12411 /* Possibly limit visibility based on template args. */
12412 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12413 if (DECL_VISIBILITY_SPECIFIED (t))
12415 DECL_VISIBILITY_SPECIFIED (r) = 0;
12416 DECL_ATTRIBUTES (r)
12417 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12419 determine_visibility (r);
12420 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12421 && !processing_template_decl)
12422 defaulted_late_check (r);
12424 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12425 args, complain, in_decl);
12426 return r;
12429 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12431 static tree
12432 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12433 tree lambda_fntype)
12435 /* We can get here when processing a member function template,
12436 member class template, or template template parameter. */
12437 tree decl = DECL_TEMPLATE_RESULT (t);
12438 tree in_decl = t;
12439 tree spec;
12440 tree tmpl_args;
12441 tree full_args;
12442 tree r;
12443 hashval_t hash = 0;
12445 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12447 /* Template template parameter is treated here. */
12448 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12449 if (new_type == error_mark_node)
12450 r = error_mark_node;
12451 /* If we get a real template back, return it. This can happen in
12452 the context of most_specialized_partial_spec. */
12453 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12454 r = new_type;
12455 else
12456 /* The new TEMPLATE_DECL was built in
12457 reduce_template_parm_level. */
12458 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12459 return r;
12462 if (!lambda_fntype)
12464 /* We might already have an instance of this template.
12465 The ARGS are for the surrounding class type, so the
12466 full args contain the tsubst'd args for the context,
12467 plus the innermost args from the template decl. */
12468 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12469 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12470 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12471 /* Because this is a template, the arguments will still be
12472 dependent, even after substitution. If
12473 PROCESSING_TEMPLATE_DECL is not set, the dependency
12474 predicates will short-circuit. */
12475 ++processing_template_decl;
12476 full_args = tsubst_template_args (tmpl_args, args,
12477 complain, in_decl);
12478 --processing_template_decl;
12479 if (full_args == error_mark_node)
12480 return error_mark_node;
12482 /* If this is a default template template argument,
12483 tsubst might not have changed anything. */
12484 if (full_args == tmpl_args)
12485 return t;
12487 hash = hash_tmpl_and_args (t, full_args);
12488 spec = retrieve_specialization (t, full_args, hash);
12489 if (spec != NULL_TREE)
12490 return spec;
12493 /* Make a new template decl. It will be similar to the
12494 original, but will record the current template arguments.
12495 We also create a new function declaration, which is just
12496 like the old one, but points to this new template, rather
12497 than the old one. */
12498 r = copy_decl (t);
12499 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12500 DECL_CHAIN (r) = NULL_TREE;
12502 // Build new template info linking to the original template decl.
12503 if (!lambda_fntype)
12505 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12506 SET_DECL_IMPLICIT_INSTANTIATION (r);
12508 else
12509 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12511 /* The template parameters for this new template are all the
12512 template parameters for the old template, except the
12513 outermost level of parameters. */
12514 DECL_TEMPLATE_PARMS (r)
12515 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12516 complain);
12518 if (TREE_CODE (decl) == TYPE_DECL
12519 && !TYPE_DECL_ALIAS_P (decl))
12521 tree new_type;
12522 ++processing_template_decl;
12523 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12524 --processing_template_decl;
12525 if (new_type == error_mark_node)
12526 return error_mark_node;
12528 TREE_TYPE (r) = new_type;
12529 /* For a partial specialization, we need to keep pointing to
12530 the primary template. */
12531 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12532 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12533 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12534 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12535 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12537 else
12539 tree new_decl;
12540 ++processing_template_decl;
12541 if (TREE_CODE (decl) == FUNCTION_DECL)
12542 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12543 else
12544 new_decl = tsubst (decl, args, complain, in_decl);
12545 --processing_template_decl;
12546 if (new_decl == error_mark_node)
12547 return error_mark_node;
12549 DECL_TEMPLATE_RESULT (r) = new_decl;
12550 TREE_TYPE (r) = TREE_TYPE (new_decl);
12551 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12552 if (lambda_fntype)
12554 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12555 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12557 else
12559 DECL_TI_TEMPLATE (new_decl) = r;
12560 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12564 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12565 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12567 if (PRIMARY_TEMPLATE_P (t))
12568 DECL_PRIMARY_TEMPLATE (r) = r;
12570 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12571 && !lambda_fntype)
12572 /* Record this non-type partial instantiation. */
12573 register_specialization (r, t,
12574 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12575 false, hash);
12577 return r;
12580 /* True if FN is the op() for a lambda in an uninstantiated template. */
12582 bool
12583 lambda_fn_in_template_p (tree fn)
12585 if (!fn || !LAMBDA_FUNCTION_P (fn))
12586 return false;
12587 tree closure = DECL_CONTEXT (fn);
12588 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12591 /* True if FN is the op() for a lambda regenerated from a lambda in an
12592 uninstantiated template. */
12594 bool
12595 regenerated_lambda_fn_p (tree fn)
12597 return (LAMBDA_FUNCTION_P (fn)
12598 && !DECL_TEMPLATE_INSTANTIATION (fn));
12601 /* We're instantiating a variable from template function TCTX. Return the
12602 corresponding current enclosing scope. This gets complicated because lambda
12603 functions in templates are regenerated rather than instantiated, but generic
12604 lambda functions are subsequently instantiated. */
12606 static tree
12607 enclosing_instantiation_of (tree tctx)
12609 tree fn = current_function_decl;
12610 int lambda_count = 0;
12612 for (; tctx && lambda_fn_in_template_p (tctx);
12613 tctx = decl_function_context (tctx))
12614 ++lambda_count;
12615 for (; fn; fn = decl_function_context (fn))
12617 tree lambda = fn;
12618 int flambda_count = 0;
12619 for (; fn && regenerated_lambda_fn_p (fn);
12620 fn = decl_function_context (fn))
12621 ++flambda_count;
12622 if (DECL_TEMPLATE_INFO (fn)
12623 ? most_general_template (fn) != most_general_template (tctx)
12624 : fn != tctx)
12625 continue;
12626 if (lambda_count)
12628 fn = lambda;
12629 while (flambda_count-- > lambda_count)
12630 fn = decl_function_context (fn);
12632 return fn;
12634 gcc_unreachable ();
12637 /* Substitute the ARGS into the T, which is a _DECL. Return the
12638 result of the substitution. Issue error and warning messages under
12639 control of COMPLAIN. */
12641 static tree
12642 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12644 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12645 location_t saved_loc;
12646 tree r = NULL_TREE;
12647 tree in_decl = t;
12648 hashval_t hash = 0;
12650 /* Set the filename and linenumber to improve error-reporting. */
12651 saved_loc = input_location;
12652 input_location = DECL_SOURCE_LOCATION (t);
12654 switch (TREE_CODE (t))
12656 case TEMPLATE_DECL:
12657 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12658 break;
12660 case FUNCTION_DECL:
12661 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12662 break;
12664 case PARM_DECL:
12666 tree type = NULL_TREE;
12667 int i, len = 1;
12668 tree expanded_types = NULL_TREE;
12669 tree prev_r = NULL_TREE;
12670 tree first_r = NULL_TREE;
12672 if (DECL_PACK_P (t))
12674 /* If there is a local specialization that isn't a
12675 parameter pack, it means that we're doing a "simple"
12676 substitution from inside tsubst_pack_expansion. Just
12677 return the local specialization (which will be a single
12678 parm). */
12679 tree spec = retrieve_local_specialization (t);
12680 if (spec
12681 && TREE_CODE (spec) == PARM_DECL
12682 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12683 RETURN (spec);
12685 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12686 the parameters in this function parameter pack. */
12687 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12688 complain, in_decl);
12689 if (TREE_CODE (expanded_types) == TREE_VEC)
12691 len = TREE_VEC_LENGTH (expanded_types);
12693 /* Zero-length parameter packs are boring. Just substitute
12694 into the chain. */
12695 if (len == 0)
12696 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12697 TREE_CHAIN (t)));
12699 else
12701 /* All we did was update the type. Make a note of that. */
12702 type = expanded_types;
12703 expanded_types = NULL_TREE;
12707 /* Loop through all of the parameters we'll build. When T is
12708 a function parameter pack, LEN is the number of expanded
12709 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12710 r = NULL_TREE;
12711 for (i = 0; i < len; ++i)
12713 prev_r = r;
12714 r = copy_node (t);
12715 if (DECL_TEMPLATE_PARM_P (t))
12716 SET_DECL_TEMPLATE_PARM_P (r);
12718 if (expanded_types)
12719 /* We're on the Ith parameter of the function parameter
12720 pack. */
12722 /* Get the Ith type. */
12723 type = TREE_VEC_ELT (expanded_types, i);
12725 /* Rename the parameter to include the index. */
12726 DECL_NAME (r)
12727 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12729 else if (!type)
12730 /* We're dealing with a normal parameter. */
12731 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12733 type = type_decays_to (type);
12734 TREE_TYPE (r) = type;
12735 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12737 if (DECL_INITIAL (r))
12739 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12740 DECL_INITIAL (r) = TREE_TYPE (r);
12741 else
12742 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12743 complain, in_decl);
12746 DECL_CONTEXT (r) = NULL_TREE;
12748 if (!DECL_TEMPLATE_PARM_P (r))
12749 DECL_ARG_TYPE (r) = type_passed_as (type);
12751 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12752 args, complain, in_decl);
12754 /* Keep track of the first new parameter we
12755 generate. That's what will be returned to the
12756 caller. */
12757 if (!first_r)
12758 first_r = r;
12760 /* Build a proper chain of parameters when substituting
12761 into a function parameter pack. */
12762 if (prev_r)
12763 DECL_CHAIN (prev_r) = r;
12766 /* If cp_unevaluated_operand is set, we're just looking for a
12767 single dummy parameter, so don't keep going. */
12768 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12769 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12770 complain, DECL_CHAIN (t));
12772 /* FIRST_R contains the start of the chain we've built. */
12773 r = first_r;
12775 break;
12777 case FIELD_DECL:
12779 tree type = NULL_TREE;
12780 tree vec = NULL_TREE;
12781 tree expanded_types = NULL_TREE;
12782 int len = 1;
12784 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12786 /* This field is a lambda capture pack. Return a TREE_VEC of
12787 the expanded fields to instantiate_class_template_1 and
12788 store them in the specializations hash table as a
12789 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12790 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12791 complain, in_decl);
12792 if (TREE_CODE (expanded_types) == TREE_VEC)
12794 len = TREE_VEC_LENGTH (expanded_types);
12795 vec = make_tree_vec (len);
12797 else
12799 /* All we did was update the type. Make a note of that. */
12800 type = expanded_types;
12801 expanded_types = NULL_TREE;
12805 for (int i = 0; i < len; ++i)
12807 r = copy_decl (t);
12808 if (expanded_types)
12810 type = TREE_VEC_ELT (expanded_types, i);
12811 DECL_NAME (r)
12812 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12814 else if (!type)
12815 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12817 if (type == error_mark_node)
12818 RETURN (error_mark_node);
12819 TREE_TYPE (r) = type;
12820 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12822 if (DECL_C_BIT_FIELD (r))
12823 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12824 number of bits. */
12825 DECL_BIT_FIELD_REPRESENTATIVE (r)
12826 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12827 complain, in_decl,
12828 /*integral_constant_expression_p=*/true);
12829 if (DECL_INITIAL (t))
12831 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12832 NSDMI in perform_member_init. Still set DECL_INITIAL
12833 so that we know there is one. */
12834 DECL_INITIAL (r) = void_node;
12835 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12836 retrofit_lang_decl (r);
12837 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12839 /* We don't have to set DECL_CONTEXT here; it is set by
12840 finish_member_declaration. */
12841 DECL_CHAIN (r) = NULL_TREE;
12843 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12844 args, complain, in_decl);
12846 if (vec)
12847 TREE_VEC_ELT (vec, i) = r;
12850 if (vec)
12852 r = vec;
12853 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12854 SET_ARGUMENT_PACK_ARGS (pack, vec);
12855 register_specialization (pack, t, args, false, 0);
12858 break;
12860 case USING_DECL:
12861 /* We reach here only for member using decls. We also need to check
12862 uses_template_parms because DECL_DEPENDENT_P is not set for a
12863 using-declaration that designates a member of the current
12864 instantiation (c++/53549). */
12865 if (DECL_DEPENDENT_P (t)
12866 || uses_template_parms (USING_DECL_SCOPE (t)))
12868 tree scope = USING_DECL_SCOPE (t);
12869 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12870 if (PACK_EXPANSION_P (scope))
12872 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12873 int len = TREE_VEC_LENGTH (vec);
12874 r = make_tree_vec (len);
12875 for (int i = 0; i < len; ++i)
12877 tree escope = TREE_VEC_ELT (vec, i);
12878 tree elt = do_class_using_decl (escope, name);
12879 if (!elt)
12881 r = error_mark_node;
12882 break;
12884 else
12886 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12887 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12889 TREE_VEC_ELT (r, i) = elt;
12892 else
12894 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12895 complain, in_decl);
12896 r = do_class_using_decl (inst_scope, name);
12897 if (!r)
12898 r = error_mark_node;
12899 else
12901 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12902 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12906 else
12908 r = copy_node (t);
12909 DECL_CHAIN (r) = NULL_TREE;
12911 break;
12913 case TYPE_DECL:
12914 case VAR_DECL:
12916 tree argvec = NULL_TREE;
12917 tree gen_tmpl = NULL_TREE;
12918 tree spec;
12919 tree tmpl = NULL_TREE;
12920 tree ctx;
12921 tree type = NULL_TREE;
12922 bool local_p;
12924 if (TREE_TYPE (t) == error_mark_node)
12925 RETURN (error_mark_node);
12927 if (TREE_CODE (t) == TYPE_DECL
12928 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12930 /* If this is the canonical decl, we don't have to
12931 mess with instantiations, and often we can't (for
12932 typename, template type parms and such). Note that
12933 TYPE_NAME is not correct for the above test if
12934 we've copied the type for a typedef. */
12935 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12936 if (type == error_mark_node)
12937 RETURN (error_mark_node);
12938 r = TYPE_NAME (type);
12939 break;
12942 /* Check to see if we already have the specialization we
12943 need. */
12944 spec = NULL_TREE;
12945 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12947 /* T is a static data member or namespace-scope entity.
12948 We have to substitute into namespace-scope variables
12949 (not just variable templates) because of cases like:
12951 template <class T> void f() { extern T t; }
12953 where the entity referenced is not known until
12954 instantiation time. */
12955 local_p = false;
12956 ctx = DECL_CONTEXT (t);
12957 if (DECL_CLASS_SCOPE_P (t))
12959 ctx = tsubst_aggr_type (ctx, args,
12960 complain,
12961 in_decl, /*entering_scope=*/1);
12962 /* If CTX is unchanged, then T is in fact the
12963 specialization we want. That situation occurs when
12964 referencing a static data member within in its own
12965 class. We can use pointer equality, rather than
12966 same_type_p, because DECL_CONTEXT is always
12967 canonical... */
12968 if (ctx == DECL_CONTEXT (t)
12969 /* ... unless T is a member template; in which
12970 case our caller can be willing to create a
12971 specialization of that template represented
12972 by T. */
12973 && !(DECL_TI_TEMPLATE (t)
12974 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12975 spec = t;
12978 if (!spec)
12980 tmpl = DECL_TI_TEMPLATE (t);
12981 gen_tmpl = most_general_template (tmpl);
12982 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12983 if (argvec != error_mark_node)
12984 argvec = (coerce_innermost_template_parms
12985 (DECL_TEMPLATE_PARMS (gen_tmpl),
12986 argvec, t, complain,
12987 /*all*/true, /*defarg*/true));
12988 if (argvec == error_mark_node)
12989 RETURN (error_mark_node);
12990 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12991 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12994 else
12996 /* A local variable. */
12997 local_p = true;
12998 /* Subsequent calls to pushdecl will fill this in. */
12999 ctx = NULL_TREE;
13000 /* Unless this is a reference to a static variable from an
13001 enclosing function, in which case we need to fill it in now. */
13002 if (TREE_STATIC (t))
13004 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13005 if (fn != current_function_decl)
13006 ctx = fn;
13008 spec = retrieve_local_specialization (t);
13010 /* If we already have the specialization we need, there is
13011 nothing more to do. */
13012 if (spec)
13014 r = spec;
13015 break;
13018 /* Create a new node for the specialization we need. */
13019 r = copy_decl (t);
13020 if (type == NULL_TREE)
13022 if (is_typedef_decl (t))
13023 type = DECL_ORIGINAL_TYPE (t);
13024 else
13025 type = TREE_TYPE (t);
13026 if (VAR_P (t)
13027 && VAR_HAD_UNKNOWN_BOUND (t)
13028 && type != error_mark_node)
13029 type = strip_array_domain (type);
13030 tree sub_args = args;
13031 if (tree auto_node = type_uses_auto (type))
13033 /* Mask off any template args past the variable's context so we
13034 don't replace the auto with an unrelated argument. */
13035 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13036 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13037 if (extra > 0)
13038 /* This should never happen with the new lambda instantiation
13039 model, but keep the handling just in case. */
13040 gcc_assert (!CHECKING_P),
13041 sub_args = strip_innermost_template_args (args, extra);
13043 type = tsubst (type, sub_args, complain, in_decl);
13045 if (VAR_P (r))
13047 /* Even if the original location is out of scope, the
13048 newly substituted one is not. */
13049 DECL_DEAD_FOR_LOCAL (r) = 0;
13050 DECL_INITIALIZED_P (r) = 0;
13051 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13052 if (type == error_mark_node)
13053 RETURN (error_mark_node);
13054 if (TREE_CODE (type) == FUNCTION_TYPE)
13056 /* It may seem that this case cannot occur, since:
13058 typedef void f();
13059 void g() { f x; }
13061 declares a function, not a variable. However:
13063 typedef void f();
13064 template <typename T> void g() { T t; }
13065 template void g<f>();
13067 is an attempt to declare a variable with function
13068 type. */
13069 error ("variable %qD has function type",
13070 /* R is not yet sufficiently initialized, so we
13071 just use its name. */
13072 DECL_NAME (r));
13073 RETURN (error_mark_node);
13075 type = complete_type (type);
13076 /* Wait until cp_finish_decl to set this again, to handle
13077 circular dependency (template/instantiate6.C). */
13078 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13079 type = check_var_type (DECL_NAME (r), type);
13081 if (DECL_HAS_VALUE_EXPR_P (t))
13083 tree ve = DECL_VALUE_EXPR (t);
13084 ve = tsubst_expr (ve, args, complain, in_decl,
13085 /*constant_expression_p=*/false);
13086 if (REFERENCE_REF_P (ve))
13088 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13089 ve = TREE_OPERAND (ve, 0);
13091 SET_DECL_VALUE_EXPR (r, ve);
13093 if (CP_DECL_THREAD_LOCAL_P (r)
13094 && !processing_template_decl)
13095 set_decl_tls_model (r, decl_default_tls_model (r));
13097 else if (DECL_SELF_REFERENCE_P (t))
13098 SET_DECL_SELF_REFERENCE_P (r);
13099 TREE_TYPE (r) = type;
13100 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13101 DECL_CONTEXT (r) = ctx;
13102 /* Clear out the mangled name and RTL for the instantiation. */
13103 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13104 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13105 SET_DECL_RTL (r, NULL);
13106 /* The initializer must not be expanded until it is required;
13107 see [temp.inst]. */
13108 DECL_INITIAL (r) = NULL_TREE;
13109 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13110 if (VAR_P (r))
13112 SET_DECL_MODE (r, VOIDmode);
13114 /* Possibly limit visibility based on template args. */
13115 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13116 if (DECL_VISIBILITY_SPECIFIED (t))
13118 DECL_VISIBILITY_SPECIFIED (r) = 0;
13119 DECL_ATTRIBUTES (r)
13120 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13122 determine_visibility (r);
13125 if (!local_p)
13127 /* A static data member declaration is always marked
13128 external when it is declared in-class, even if an
13129 initializer is present. We mimic the non-template
13130 processing here. */
13131 DECL_EXTERNAL (r) = 1;
13132 if (DECL_NAMESPACE_SCOPE_P (t))
13133 DECL_NOT_REALLY_EXTERN (r) = 1;
13135 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13136 SET_DECL_IMPLICIT_INSTANTIATION (r);
13137 register_specialization (r, gen_tmpl, argvec, false, hash);
13139 else
13141 if (DECL_LANG_SPECIFIC (r))
13142 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13143 if (!cp_unevaluated_operand)
13144 register_local_specialization (r, t);
13147 DECL_CHAIN (r) = NULL_TREE;
13149 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13150 /*flags=*/0,
13151 args, complain, in_decl);
13153 /* Preserve a typedef that names a type. */
13154 if (is_typedef_decl (r) && type != error_mark_node)
13156 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13157 set_underlying_type (r);
13158 if (TYPE_DECL_ALIAS_P (r))
13159 /* An alias template specialization can be dependent
13160 even if its underlying type is not. */
13161 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13164 layout_decl (r, 0);
13166 break;
13168 default:
13169 gcc_unreachable ();
13171 #undef RETURN
13173 out:
13174 /* Restore the file and line information. */
13175 input_location = saved_loc;
13177 return r;
13180 /* Substitute into the ARG_TYPES of a function type.
13181 If END is a TREE_CHAIN, leave it and any following types
13182 un-substituted. */
13184 static tree
13185 tsubst_arg_types (tree arg_types,
13186 tree args,
13187 tree end,
13188 tsubst_flags_t complain,
13189 tree in_decl)
13191 tree remaining_arg_types;
13192 tree type = NULL_TREE;
13193 int i = 1;
13194 tree expanded_args = NULL_TREE;
13195 tree default_arg;
13197 if (!arg_types || arg_types == void_list_node || arg_types == end)
13198 return arg_types;
13200 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13201 args, end, complain, in_decl);
13202 if (remaining_arg_types == error_mark_node)
13203 return error_mark_node;
13205 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13207 /* For a pack expansion, perform substitution on the
13208 entire expression. Later on, we'll handle the arguments
13209 one-by-one. */
13210 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13211 args, complain, in_decl);
13213 if (TREE_CODE (expanded_args) == TREE_VEC)
13214 /* So that we'll spin through the parameters, one by one. */
13215 i = TREE_VEC_LENGTH (expanded_args);
13216 else
13218 /* We only partially substituted into the parameter
13219 pack. Our type is TYPE_PACK_EXPANSION. */
13220 type = expanded_args;
13221 expanded_args = NULL_TREE;
13225 while (i > 0) {
13226 --i;
13228 if (expanded_args)
13229 type = TREE_VEC_ELT (expanded_args, i);
13230 else if (!type)
13231 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13233 if (type == error_mark_node)
13234 return error_mark_node;
13235 if (VOID_TYPE_P (type))
13237 if (complain & tf_error)
13239 error ("invalid parameter type %qT", type);
13240 if (in_decl)
13241 error ("in declaration %q+D", in_decl);
13243 return error_mark_node;
13245 /* DR 657. */
13246 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13247 return error_mark_node;
13249 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13250 top-level qualifiers as required. */
13251 type = cv_unqualified (type_decays_to (type));
13253 /* We do not substitute into default arguments here. The standard
13254 mandates that they be instantiated only when needed, which is
13255 done in build_over_call. */
13256 default_arg = TREE_PURPOSE (arg_types);
13258 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13259 since the new op() won't have any associated template arguments for us
13260 to refer to later. */
13261 if (lambda_fn_in_template_p (in_decl))
13262 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13263 false/*fn*/, false/*constexpr*/);
13265 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13267 /* We've instantiated a template before its default arguments
13268 have been parsed. This can happen for a nested template
13269 class, and is not an error unless we require the default
13270 argument in a call of this function. */
13271 remaining_arg_types =
13272 tree_cons (default_arg, type, remaining_arg_types);
13273 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13275 else
13276 remaining_arg_types =
13277 hash_tree_cons (default_arg, type, remaining_arg_types);
13280 return remaining_arg_types;
13283 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13284 *not* handle the exception-specification for FNTYPE, because the
13285 initial substitution of explicitly provided template parameters
13286 during argument deduction forbids substitution into the
13287 exception-specification:
13289 [temp.deduct]
13291 All references in the function type of the function template to the
13292 corresponding template parameters are replaced by the specified tem-
13293 plate argument values. If a substitution in a template parameter or
13294 in the function type of the function template results in an invalid
13295 type, type deduction fails. [Note: The equivalent substitution in
13296 exception specifications is done only when the function is instanti-
13297 ated, at which point a program is ill-formed if the substitution
13298 results in an invalid type.] */
13300 static tree
13301 tsubst_function_type (tree t,
13302 tree args,
13303 tsubst_flags_t complain,
13304 tree in_decl)
13306 tree return_type;
13307 tree arg_types = NULL_TREE;
13308 tree fntype;
13310 /* The TYPE_CONTEXT is not used for function/method types. */
13311 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13313 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13314 failure. */
13315 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13317 if (late_return_type_p)
13319 /* Substitute the argument types. */
13320 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13321 complain, in_decl);
13322 if (arg_types == error_mark_node)
13323 return error_mark_node;
13325 tree save_ccp = current_class_ptr;
13326 tree save_ccr = current_class_ref;
13327 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13328 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13329 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13330 if (do_inject)
13332 /* DR 1207: 'this' is in scope in the trailing return type. */
13333 inject_this_parameter (this_type, cp_type_quals (this_type));
13336 /* Substitute the return type. */
13337 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13339 if (do_inject)
13341 current_class_ptr = save_ccp;
13342 current_class_ref = save_ccr;
13345 else
13346 /* Substitute the return type. */
13347 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13349 if (return_type == error_mark_node)
13350 return error_mark_node;
13351 /* DR 486 clarifies that creation of a function type with an
13352 invalid return type is a deduction failure. */
13353 if (TREE_CODE (return_type) == ARRAY_TYPE
13354 || TREE_CODE (return_type) == FUNCTION_TYPE)
13356 if (complain & tf_error)
13358 if (TREE_CODE (return_type) == ARRAY_TYPE)
13359 error ("function returning an array");
13360 else
13361 error ("function returning a function");
13363 return error_mark_node;
13365 /* And DR 657. */
13366 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13367 return error_mark_node;
13369 if (!late_return_type_p)
13371 /* Substitute the argument types. */
13372 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13373 complain, in_decl);
13374 if (arg_types == error_mark_node)
13375 return error_mark_node;
13378 /* Construct a new type node and return it. */
13379 if (TREE_CODE (t) == FUNCTION_TYPE)
13381 fntype = build_function_type (return_type, arg_types);
13382 fntype = apply_memfn_quals (fntype,
13383 type_memfn_quals (t),
13384 type_memfn_rqual (t));
13386 else
13388 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13389 /* Don't pick up extra function qualifiers from the basetype. */
13390 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13391 if (! MAYBE_CLASS_TYPE_P (r))
13393 /* [temp.deduct]
13395 Type deduction may fail for any of the following
13396 reasons:
13398 -- Attempting to create "pointer to member of T" when T
13399 is not a class type. */
13400 if (complain & tf_error)
13401 error ("creating pointer to member function of non-class type %qT",
13403 return error_mark_node;
13406 fntype = build_method_type_directly (r, return_type,
13407 TREE_CHAIN (arg_types));
13408 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13410 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13412 if (late_return_type_p)
13413 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13415 return fntype;
13418 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13419 ARGS into that specification, and return the substituted
13420 specification. If there is no specification, return NULL_TREE. */
13422 static tree
13423 tsubst_exception_specification (tree fntype,
13424 tree args,
13425 tsubst_flags_t complain,
13426 tree in_decl,
13427 bool defer_ok)
13429 tree specs;
13430 tree new_specs;
13432 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13433 new_specs = NULL_TREE;
13434 if (specs && TREE_PURPOSE (specs))
13436 /* A noexcept-specifier. */
13437 tree expr = TREE_PURPOSE (specs);
13438 if (TREE_CODE (expr) == INTEGER_CST)
13439 new_specs = expr;
13440 else if (defer_ok)
13442 /* Defer instantiation of noexcept-specifiers to avoid
13443 excessive instantiations (c++/49107). */
13444 new_specs = make_node (DEFERRED_NOEXCEPT);
13445 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13447 /* We already partially instantiated this member template,
13448 so combine the new args with the old. */
13449 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13450 = DEFERRED_NOEXCEPT_PATTERN (expr);
13451 DEFERRED_NOEXCEPT_ARGS (new_specs)
13452 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13454 else
13456 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13457 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13460 else
13461 new_specs = tsubst_copy_and_build
13462 (expr, args, complain, in_decl, /*function_p=*/false,
13463 /*integral_constant_expression_p=*/true);
13464 new_specs = build_noexcept_spec (new_specs, complain);
13466 else if (specs)
13468 if (! TREE_VALUE (specs))
13469 new_specs = specs;
13470 else
13471 while (specs)
13473 tree spec;
13474 int i, len = 1;
13475 tree expanded_specs = NULL_TREE;
13477 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13479 /* Expand the pack expansion type. */
13480 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13481 args, complain,
13482 in_decl);
13484 if (expanded_specs == error_mark_node)
13485 return error_mark_node;
13486 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13487 len = TREE_VEC_LENGTH (expanded_specs);
13488 else
13490 /* We're substituting into a member template, so
13491 we got a TYPE_PACK_EXPANSION back. Add that
13492 expansion and move on. */
13493 gcc_assert (TREE_CODE (expanded_specs)
13494 == TYPE_PACK_EXPANSION);
13495 new_specs = add_exception_specifier (new_specs,
13496 expanded_specs,
13497 complain);
13498 specs = TREE_CHAIN (specs);
13499 continue;
13503 for (i = 0; i < len; ++i)
13505 if (expanded_specs)
13506 spec = TREE_VEC_ELT (expanded_specs, i);
13507 else
13508 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13509 if (spec == error_mark_node)
13510 return spec;
13511 new_specs = add_exception_specifier (new_specs, spec,
13512 complain);
13515 specs = TREE_CHAIN (specs);
13518 return new_specs;
13521 /* Take the tree structure T and replace template parameters used
13522 therein with the argument vector ARGS. IN_DECL is an associated
13523 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13524 Issue error and warning messages under control of COMPLAIN. Note
13525 that we must be relatively non-tolerant of extensions here, in
13526 order to preserve conformance; if we allow substitutions that
13527 should not be allowed, we may allow argument deductions that should
13528 not succeed, and therefore report ambiguous overload situations
13529 where there are none. In theory, we could allow the substitution,
13530 but indicate that it should have failed, and allow our caller to
13531 make sure that the right thing happens, but we don't try to do this
13532 yet.
13534 This function is used for dealing with types, decls and the like;
13535 for expressions, use tsubst_expr or tsubst_copy. */
13537 tree
13538 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13540 enum tree_code code;
13541 tree type, r = NULL_TREE;
13543 if (t == NULL_TREE || t == error_mark_node
13544 || t == integer_type_node
13545 || t == void_type_node
13546 || t == char_type_node
13547 || t == unknown_type_node
13548 || TREE_CODE (t) == NAMESPACE_DECL
13549 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13550 return t;
13552 if (DECL_P (t))
13553 return tsubst_decl (t, args, complain);
13555 if (args == NULL_TREE)
13556 return t;
13558 code = TREE_CODE (t);
13560 if (code == IDENTIFIER_NODE)
13561 type = IDENTIFIER_TYPE_VALUE (t);
13562 else
13563 type = TREE_TYPE (t);
13565 gcc_assert (type != unknown_type_node);
13567 /* Reuse typedefs. We need to do this to handle dependent attributes,
13568 such as attribute aligned. */
13569 if (TYPE_P (t)
13570 && typedef_variant_p (t))
13572 tree decl = TYPE_NAME (t);
13574 if (alias_template_specialization_p (t))
13576 /* DECL represents an alias template and we want to
13577 instantiate it. */
13578 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13579 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13580 r = instantiate_alias_template (tmpl, gen_args, complain);
13582 else if (DECL_CLASS_SCOPE_P (decl)
13583 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13584 && uses_template_parms (DECL_CONTEXT (decl)))
13586 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13587 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13588 r = retrieve_specialization (tmpl, gen_args, 0);
13590 else if (DECL_FUNCTION_SCOPE_P (decl)
13591 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13592 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13593 r = retrieve_local_specialization (decl);
13594 else
13595 /* The typedef is from a non-template context. */
13596 return t;
13598 if (r)
13600 r = TREE_TYPE (r);
13601 r = cp_build_qualified_type_real
13602 (r, cp_type_quals (t) | cp_type_quals (r),
13603 complain | tf_ignore_bad_quals);
13604 return r;
13606 else
13608 /* We don't have an instantiation yet, so drop the typedef. */
13609 int quals = cp_type_quals (t);
13610 t = DECL_ORIGINAL_TYPE (decl);
13611 t = cp_build_qualified_type_real (t, quals,
13612 complain | tf_ignore_bad_quals);
13616 bool fndecl_type = (complain & tf_fndecl_type);
13617 complain &= ~tf_fndecl_type;
13619 if (type
13620 && code != TYPENAME_TYPE
13621 && code != TEMPLATE_TYPE_PARM
13622 && code != TEMPLATE_PARM_INDEX
13623 && code != IDENTIFIER_NODE
13624 && code != FUNCTION_TYPE
13625 && code != METHOD_TYPE)
13626 type = tsubst (type, args, complain, in_decl);
13627 if (type == error_mark_node)
13628 return error_mark_node;
13630 switch (code)
13632 case RECORD_TYPE:
13633 case UNION_TYPE:
13634 case ENUMERAL_TYPE:
13635 return tsubst_aggr_type (t, args, complain, in_decl,
13636 /*entering_scope=*/0);
13638 case ERROR_MARK:
13639 case IDENTIFIER_NODE:
13640 case VOID_TYPE:
13641 case REAL_TYPE:
13642 case COMPLEX_TYPE:
13643 case VECTOR_TYPE:
13644 case BOOLEAN_TYPE:
13645 case NULLPTR_TYPE:
13646 case LANG_TYPE:
13647 return t;
13649 case INTEGER_TYPE:
13650 if (t == integer_type_node)
13651 return t;
13653 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13654 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13655 return t;
13658 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13660 max = tsubst_expr (omax, args, complain, in_decl,
13661 /*integral_constant_expression_p=*/false);
13663 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13664 needed. */
13665 if (TREE_CODE (max) == NOP_EXPR
13666 && TREE_SIDE_EFFECTS (omax)
13667 && !TREE_TYPE (max))
13668 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13670 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13671 with TREE_SIDE_EFFECTS that indicates this is not an integral
13672 constant expression. */
13673 if (processing_template_decl
13674 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13676 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13677 TREE_SIDE_EFFECTS (max) = 1;
13680 return compute_array_index_type (NULL_TREE, max, complain);
13683 case TEMPLATE_TYPE_PARM:
13684 case TEMPLATE_TEMPLATE_PARM:
13685 case BOUND_TEMPLATE_TEMPLATE_PARM:
13686 case TEMPLATE_PARM_INDEX:
13688 int idx;
13689 int level;
13690 int levels;
13691 tree arg = NULL_TREE;
13693 /* Early in template argument deduction substitution, we don't
13694 want to reduce the level of 'auto', or it will be confused
13695 with a normal template parm in subsequent deduction. */
13696 if (is_auto (t) && (complain & tf_partial))
13697 return t;
13699 r = NULL_TREE;
13701 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13702 template_parm_level_and_index (t, &level, &idx);
13704 levels = TMPL_ARGS_DEPTH (args);
13705 if (level <= levels
13706 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13708 arg = TMPL_ARG (args, level, idx);
13710 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13712 /* See through ARGUMENT_PACK_SELECT arguments. */
13713 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13714 /* If the selected argument is an expansion E, that most
13715 likely means we were called from
13716 gen_elem_of_pack_expansion_instantiation during the
13717 substituting of pack an argument pack (which Ith
13718 element is a pack expansion, where I is
13719 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13720 In this case, the Ith element resulting from this
13721 substituting is going to be a pack expansion, which
13722 pattern is the pattern of E. Let's return the
13723 pattern of E, and
13724 gen_elem_of_pack_expansion_instantiation will
13725 build the resulting pack expansion from it. */
13726 if (PACK_EXPANSION_P (arg))
13728 /* Make sure we aren't throwing away arg info. */
13729 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13730 arg = PACK_EXPANSION_PATTERN (arg);
13735 if (arg == error_mark_node)
13736 return error_mark_node;
13737 else if (arg != NULL_TREE)
13739 if (ARGUMENT_PACK_P (arg))
13740 /* If ARG is an argument pack, we don't actually want to
13741 perform a substitution here, because substitutions
13742 for argument packs are only done
13743 element-by-element. We can get to this point when
13744 substituting the type of a non-type template
13745 parameter pack, when that type actually contains
13746 template parameter packs from an outer template, e.g.,
13748 template<typename... Types> struct A {
13749 template<Types... Values> struct B { };
13750 }; */
13751 return t;
13753 if (code == TEMPLATE_TYPE_PARM)
13755 int quals;
13756 gcc_assert (TYPE_P (arg));
13758 quals = cp_type_quals (arg) | cp_type_quals (t);
13760 return cp_build_qualified_type_real
13761 (arg, quals, complain | tf_ignore_bad_quals);
13763 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13765 /* We are processing a type constructed from a
13766 template template parameter. */
13767 tree argvec = tsubst (TYPE_TI_ARGS (t),
13768 args, complain, in_decl);
13769 if (argvec == error_mark_node)
13770 return error_mark_node;
13772 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13773 || TREE_CODE (arg) == TEMPLATE_DECL
13774 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13776 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13777 /* Consider this code:
13779 template <template <class> class Template>
13780 struct Internal {
13781 template <class Arg> using Bind = Template<Arg>;
13784 template <template <class> class Template, class Arg>
13785 using Instantiate = Template<Arg>; //#0
13787 template <template <class> class Template,
13788 class Argument>
13789 using Bind =
13790 Instantiate<Internal<Template>::template Bind,
13791 Argument>; //#1
13793 When #1 is parsed, the
13794 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13795 parameter `Template' in #0 matches the
13796 UNBOUND_CLASS_TEMPLATE representing the argument
13797 `Internal<Template>::template Bind'; We then want
13798 to assemble the type `Bind<Argument>' that can't
13799 be fully created right now, because
13800 `Internal<Template>' not being complete, the Bind
13801 template cannot be looked up in that context. So
13802 we need to "store" `Bind<Argument>' for later
13803 when the context of Bind becomes complete. Let's
13804 store that in a TYPENAME_TYPE. */
13805 return make_typename_type (TYPE_CONTEXT (arg),
13806 build_nt (TEMPLATE_ID_EXPR,
13807 TYPE_IDENTIFIER (arg),
13808 argvec),
13809 typename_type,
13810 complain);
13812 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13813 are resolving nested-types in the signature of a
13814 member function templates. Otherwise ARG is a
13815 TEMPLATE_DECL and is the real template to be
13816 instantiated. */
13817 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13818 arg = TYPE_NAME (arg);
13820 r = lookup_template_class (arg,
13821 argvec, in_decl,
13822 DECL_CONTEXT (arg),
13823 /*entering_scope=*/0,
13824 complain);
13825 return cp_build_qualified_type_real
13826 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13828 else if (code == TEMPLATE_TEMPLATE_PARM)
13829 return arg;
13830 else
13831 /* TEMPLATE_PARM_INDEX. */
13832 return convert_from_reference (unshare_expr (arg));
13835 if (level == 1)
13836 /* This can happen during the attempted tsubst'ing in
13837 unify. This means that we don't yet have any information
13838 about the template parameter in question. */
13839 return t;
13841 /* If we get here, we must have been looking at a parm for a
13842 more deeply nested template. Make a new version of this
13843 template parameter, but with a lower level. */
13844 switch (code)
13846 case TEMPLATE_TYPE_PARM:
13847 case TEMPLATE_TEMPLATE_PARM:
13848 case BOUND_TEMPLATE_TEMPLATE_PARM:
13849 if (cp_type_quals (t))
13851 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13852 r = cp_build_qualified_type_real
13853 (r, cp_type_quals (t),
13854 complain | (code == TEMPLATE_TYPE_PARM
13855 ? tf_ignore_bad_quals : 0));
13857 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13858 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13859 && (r = (TEMPLATE_PARM_DESCENDANTS
13860 (TEMPLATE_TYPE_PARM_INDEX (t))))
13861 && (r = TREE_TYPE (r))
13862 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13863 /* Break infinite recursion when substituting the constraints
13864 of a constrained placeholder. */;
13865 else
13867 r = copy_type (t);
13868 TEMPLATE_TYPE_PARM_INDEX (r)
13869 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13870 r, levels, args, complain);
13871 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13872 TYPE_MAIN_VARIANT (r) = r;
13873 TYPE_POINTER_TO (r) = NULL_TREE;
13874 TYPE_REFERENCE_TO (r) = NULL_TREE;
13876 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13878 /* Propagate constraints on placeholders. */
13879 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13880 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13881 = tsubst_constraint (constr, args, complain, in_decl);
13882 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13884 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13885 pl = tsubst (pl, args, complain, in_decl);
13886 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13890 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13891 /* We have reduced the level of the template
13892 template parameter, but not the levels of its
13893 template parameters, so canonical_type_parameter
13894 will not be able to find the canonical template
13895 template parameter for this level. Thus, we
13896 require structural equality checking to compare
13897 TEMPLATE_TEMPLATE_PARMs. */
13898 SET_TYPE_STRUCTURAL_EQUALITY (r);
13899 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13900 SET_TYPE_STRUCTURAL_EQUALITY (r);
13901 else
13902 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13904 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13906 tree tinfo = TYPE_TEMPLATE_INFO (t);
13907 /* We might need to substitute into the types of non-type
13908 template parameters. */
13909 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13910 complain, in_decl);
13911 if (tmpl == error_mark_node)
13912 return error_mark_node;
13913 tree argvec = tsubst (TI_ARGS (tinfo), args,
13914 complain, in_decl);
13915 if (argvec == error_mark_node)
13916 return error_mark_node;
13918 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13919 = build_template_info (tmpl, argvec);
13922 break;
13924 case TEMPLATE_PARM_INDEX:
13925 /* OK, now substitute the type of the non-type parameter. We
13926 couldn't do it earlier because it might be an auto parameter,
13927 and we wouldn't need to if we had an argument. */
13928 type = tsubst (type, args, complain, in_decl);
13929 if (type == error_mark_node)
13930 return error_mark_node;
13931 r = reduce_template_parm_level (t, type, levels, args, complain);
13932 break;
13934 default:
13935 gcc_unreachable ();
13938 return r;
13941 case TREE_LIST:
13943 tree purpose, value, chain;
13945 if (t == void_list_node)
13946 return t;
13948 purpose = TREE_PURPOSE (t);
13949 if (purpose)
13951 purpose = tsubst (purpose, args, complain, in_decl);
13952 if (purpose == error_mark_node)
13953 return error_mark_node;
13955 value = TREE_VALUE (t);
13956 if (value)
13958 value = tsubst (value, args, complain, in_decl);
13959 if (value == error_mark_node)
13960 return error_mark_node;
13962 chain = TREE_CHAIN (t);
13963 if (chain && chain != void_type_node)
13965 chain = tsubst (chain, args, complain, in_decl);
13966 if (chain == error_mark_node)
13967 return error_mark_node;
13969 if (purpose == TREE_PURPOSE (t)
13970 && value == TREE_VALUE (t)
13971 && chain == TREE_CHAIN (t))
13972 return t;
13973 return hash_tree_cons (purpose, value, chain);
13976 case TREE_BINFO:
13977 /* We should never be tsubsting a binfo. */
13978 gcc_unreachable ();
13980 case TREE_VEC:
13981 /* A vector of template arguments. */
13982 gcc_assert (!type);
13983 return tsubst_template_args (t, args, complain, in_decl);
13985 case POINTER_TYPE:
13986 case REFERENCE_TYPE:
13988 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13989 return t;
13991 /* [temp.deduct]
13993 Type deduction may fail for any of the following
13994 reasons:
13996 -- Attempting to create a pointer to reference type.
13997 -- Attempting to create a reference to a reference type or
13998 a reference to void.
14000 Core issue 106 says that creating a reference to a reference
14001 during instantiation is no longer a cause for failure. We
14002 only enforce this check in strict C++98 mode. */
14003 if ((TREE_CODE (type) == REFERENCE_TYPE
14004 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14005 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14007 static location_t last_loc;
14009 /* We keep track of the last time we issued this error
14010 message to avoid spewing a ton of messages during a
14011 single bad template instantiation. */
14012 if (complain & tf_error
14013 && last_loc != input_location)
14015 if (VOID_TYPE_P (type))
14016 error ("forming reference to void");
14017 else if (code == POINTER_TYPE)
14018 error ("forming pointer to reference type %qT", type);
14019 else
14020 error ("forming reference to reference type %qT", type);
14021 last_loc = input_location;
14024 return error_mark_node;
14026 else if (TREE_CODE (type) == FUNCTION_TYPE
14027 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14028 || type_memfn_rqual (type) != REF_QUAL_NONE))
14030 if (complain & tf_error)
14032 if (code == POINTER_TYPE)
14033 error ("forming pointer to qualified function type %qT",
14034 type);
14035 else
14036 error ("forming reference to qualified function type %qT",
14037 type);
14039 return error_mark_node;
14041 else if (code == POINTER_TYPE)
14043 r = build_pointer_type (type);
14044 if (TREE_CODE (type) == METHOD_TYPE)
14045 r = build_ptrmemfunc_type (r);
14047 else if (TREE_CODE (type) == REFERENCE_TYPE)
14048 /* In C++0x, during template argument substitution, when there is an
14049 attempt to create a reference to a reference type, reference
14050 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14052 "If a template-argument for a template-parameter T names a type
14053 that is a reference to a type A, an attempt to create the type
14054 'lvalue reference to cv T' creates the type 'lvalue reference to
14055 A,' while an attempt to create the type type rvalue reference to
14056 cv T' creates the type T"
14058 r = cp_build_reference_type
14059 (TREE_TYPE (type),
14060 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14061 else
14062 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14063 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14065 if (r != error_mark_node)
14066 /* Will this ever be needed for TYPE_..._TO values? */
14067 layout_type (r);
14069 return r;
14071 case OFFSET_TYPE:
14073 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14074 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14076 /* [temp.deduct]
14078 Type deduction may fail for any of the following
14079 reasons:
14081 -- Attempting to create "pointer to member of T" when T
14082 is not a class type. */
14083 if (complain & tf_error)
14084 error ("creating pointer to member of non-class type %qT", r);
14085 return error_mark_node;
14087 if (TREE_CODE (type) == REFERENCE_TYPE)
14089 if (complain & tf_error)
14090 error ("creating pointer to member reference type %qT", type);
14091 return error_mark_node;
14093 if (VOID_TYPE_P (type))
14095 if (complain & tf_error)
14096 error ("creating pointer to member of type void");
14097 return error_mark_node;
14099 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14100 if (TREE_CODE (type) == FUNCTION_TYPE)
14102 /* The type of the implicit object parameter gets its
14103 cv-qualifiers from the FUNCTION_TYPE. */
14104 tree memptr;
14105 tree method_type
14106 = build_memfn_type (type, r, type_memfn_quals (type),
14107 type_memfn_rqual (type));
14108 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14109 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14110 complain);
14112 else
14113 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14114 cp_type_quals (t),
14115 complain);
14117 case FUNCTION_TYPE:
14118 case METHOD_TYPE:
14120 tree fntype;
14121 tree specs;
14122 fntype = tsubst_function_type (t, args, complain, in_decl);
14123 if (fntype == error_mark_node)
14124 return error_mark_node;
14126 /* Substitute the exception specification. */
14127 specs = tsubst_exception_specification (t, args, complain, in_decl,
14128 /*defer_ok*/fndecl_type);
14129 if (specs == error_mark_node)
14130 return error_mark_node;
14131 if (specs)
14132 fntype = build_exception_variant (fntype, specs);
14133 return fntype;
14135 case ARRAY_TYPE:
14137 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14138 if (domain == error_mark_node)
14139 return error_mark_node;
14141 /* As an optimization, we avoid regenerating the array type if
14142 it will obviously be the same as T. */
14143 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14144 return t;
14146 /* These checks should match the ones in create_array_type_for_decl.
14148 [temp.deduct]
14150 The deduction may fail for any of the following reasons:
14152 -- Attempting to create an array with an element type that
14153 is void, a function type, or a reference type, or [DR337]
14154 an abstract class type. */
14155 if (VOID_TYPE_P (type)
14156 || TREE_CODE (type) == FUNCTION_TYPE
14157 || (TREE_CODE (type) == ARRAY_TYPE
14158 && TYPE_DOMAIN (type) == NULL_TREE)
14159 || TREE_CODE (type) == REFERENCE_TYPE)
14161 if (complain & tf_error)
14162 error ("creating array of %qT", type);
14163 return error_mark_node;
14166 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14167 return error_mark_node;
14169 r = build_cplus_array_type (type, domain);
14171 if (TYPE_USER_ALIGN (t))
14173 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14174 TYPE_USER_ALIGN (r) = 1;
14177 return r;
14180 case TYPENAME_TYPE:
14182 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14183 in_decl, /*entering_scope=*/1);
14184 if (ctx == error_mark_node)
14185 return error_mark_node;
14187 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14188 complain, in_decl);
14189 if (f == error_mark_node)
14190 return error_mark_node;
14192 if (!MAYBE_CLASS_TYPE_P (ctx))
14194 if (complain & tf_error)
14195 error ("%qT is not a class, struct, or union type", ctx);
14196 return error_mark_node;
14198 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14200 /* Normally, make_typename_type does not require that the CTX
14201 have complete type in order to allow things like:
14203 template <class T> struct S { typename S<T>::X Y; };
14205 But, such constructs have already been resolved by this
14206 point, so here CTX really should have complete type, unless
14207 it's a partial instantiation. */
14208 ctx = complete_type (ctx);
14209 if (!COMPLETE_TYPE_P (ctx))
14211 if (complain & tf_error)
14212 cxx_incomplete_type_error (NULL_TREE, ctx);
14213 return error_mark_node;
14217 f = make_typename_type (ctx, f, typename_type,
14218 complain | tf_keep_type_decl);
14219 if (f == error_mark_node)
14220 return f;
14221 if (TREE_CODE (f) == TYPE_DECL)
14223 complain |= tf_ignore_bad_quals;
14224 f = TREE_TYPE (f);
14227 if (TREE_CODE (f) != TYPENAME_TYPE)
14229 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14231 if (complain & tf_error)
14232 error ("%qT resolves to %qT, which is not an enumeration type",
14233 t, f);
14234 else
14235 return error_mark_node;
14237 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14239 if (complain & tf_error)
14240 error ("%qT resolves to %qT, which is is not a class type",
14241 t, f);
14242 else
14243 return error_mark_node;
14247 return cp_build_qualified_type_real
14248 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14251 case UNBOUND_CLASS_TEMPLATE:
14253 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14254 in_decl, /*entering_scope=*/1);
14255 tree name = TYPE_IDENTIFIER (t);
14256 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14258 if (ctx == error_mark_node || name == error_mark_node)
14259 return error_mark_node;
14261 if (parm_list)
14262 parm_list = tsubst_template_parms (parm_list, args, complain);
14263 return make_unbound_class_template (ctx, name, parm_list, complain);
14266 case TYPEOF_TYPE:
14268 tree type;
14270 ++cp_unevaluated_operand;
14271 ++c_inhibit_evaluation_warnings;
14273 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14274 complain, in_decl,
14275 /*integral_constant_expression_p=*/false);
14277 --cp_unevaluated_operand;
14278 --c_inhibit_evaluation_warnings;
14280 type = finish_typeof (type);
14281 return cp_build_qualified_type_real (type,
14282 cp_type_quals (t)
14283 | cp_type_quals (type),
14284 complain);
14287 case DECLTYPE_TYPE:
14289 tree type;
14291 ++cp_unevaluated_operand;
14292 ++c_inhibit_evaluation_warnings;
14294 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14295 complain|tf_decltype, in_decl,
14296 /*function_p*/false,
14297 /*integral_constant_expression*/false);
14299 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14301 if (type == NULL_TREE)
14303 if (complain & tf_error)
14304 error ("empty initializer in lambda init-capture");
14305 type = error_mark_node;
14307 else if (TREE_CODE (type) == TREE_LIST)
14308 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14311 --cp_unevaluated_operand;
14312 --c_inhibit_evaluation_warnings;
14314 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14315 type = lambda_capture_field_type (type,
14316 DECLTYPE_FOR_INIT_CAPTURE (t),
14317 DECLTYPE_FOR_REF_CAPTURE (t));
14318 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14319 type = lambda_proxy_type (type);
14320 else
14322 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14323 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14324 && EXPR_P (type))
14325 /* In a template ~id could be either a complement expression
14326 or an unqualified-id naming a destructor; if instantiating
14327 it produces an expression, it's not an id-expression or
14328 member access. */
14329 id = false;
14330 type = finish_decltype_type (type, id, complain);
14332 return cp_build_qualified_type_real (type,
14333 cp_type_quals (t)
14334 | cp_type_quals (type),
14335 complain | tf_ignore_bad_quals);
14338 case UNDERLYING_TYPE:
14340 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14341 complain, in_decl);
14342 return finish_underlying_type (type);
14345 case TYPE_ARGUMENT_PACK:
14346 case NONTYPE_ARGUMENT_PACK:
14348 tree r;
14350 if (code == NONTYPE_ARGUMENT_PACK)
14351 r = make_node (code);
14352 else
14353 r = cxx_make_type (code);
14355 tree pack_args = ARGUMENT_PACK_ARGS (t);
14356 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14357 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14359 return r;
14362 case VOID_CST:
14363 case INTEGER_CST:
14364 case REAL_CST:
14365 case STRING_CST:
14366 case PLUS_EXPR:
14367 case MINUS_EXPR:
14368 case NEGATE_EXPR:
14369 case NOP_EXPR:
14370 case INDIRECT_REF:
14371 case ADDR_EXPR:
14372 case CALL_EXPR:
14373 case ARRAY_REF:
14374 case SCOPE_REF:
14375 /* We should use one of the expression tsubsts for these codes. */
14376 gcc_unreachable ();
14378 default:
14379 sorry ("use of %qs in template", get_tree_code_name (code));
14380 return error_mark_node;
14384 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14385 expression on the left-hand side of the "." or "->" operator. A
14386 baselink indicates a function from a base class. Both the
14387 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14388 bases of the template class, rather than the instantiated class.
14389 In addition, lookups that were not ambiguous before may be
14390 ambiguous now. Therefore, we perform the lookup again. */
14392 static tree
14393 tsubst_baselink (tree baselink, tree object_type,
14394 tree args, tsubst_flags_t complain, tree in_decl)
14396 bool qualified = BASELINK_QUALIFIED_P (baselink);
14398 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14399 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14401 tree optype = BASELINK_OPTYPE (baselink);
14402 optype = tsubst (optype, args, complain, in_decl);
14404 tree template_args = NULL_TREE;
14405 bool template_id_p = false;
14406 tree fns = BASELINK_FUNCTIONS (baselink);
14407 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14409 template_id_p = true;
14410 template_args = TREE_OPERAND (fns, 1);
14411 fns = TREE_OPERAND (fns, 0);
14412 if (template_args)
14413 template_args = tsubst_template_args (template_args, args,
14414 complain, in_decl);
14417 tree name = OVL_NAME (fns);
14418 if (IDENTIFIER_CONV_OP_P (name))
14419 name = make_conv_op_name (optype);
14421 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14422 if (!baselink)
14424 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14425 error ("cannot call constructor %<%T::%D%> directly",
14426 qualifying_scope, name);
14427 return error_mark_node;
14430 /* If lookup found a single function, mark it as used at this point.
14431 (If it lookup found multiple functions the one selected later by
14432 overload resolution will be marked as used at that point.) */
14433 if (BASELINK_P (baselink))
14434 fns = BASELINK_FUNCTIONS (baselink);
14435 if (!template_id_p && !really_overloaded_fn (fns)
14436 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14437 return error_mark_node;
14439 if (BASELINK_P (baselink))
14441 /* Add back the template arguments, if present. */
14442 if (template_id_p)
14443 BASELINK_FUNCTIONS (baselink)
14444 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14445 BASELINK_FUNCTIONS (baselink), template_args);
14447 /* Update the conversion operator type. */
14448 BASELINK_OPTYPE (baselink) = optype;
14451 if (!object_type)
14452 object_type = current_class_type;
14454 if (qualified || name == complete_dtor_identifier)
14456 baselink = adjust_result_of_qualified_name_lookup (baselink,
14457 qualifying_scope,
14458 object_type);
14459 if (!qualified)
14460 /* We need to call adjust_result_of_qualified_name_lookup in case the
14461 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14462 so that we still get virtual function binding. */
14463 BASELINK_QUALIFIED_P (baselink) = false;
14466 return baselink;
14469 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14470 true if the qualified-id will be a postfix-expression in-and-of
14471 itself; false if more of the postfix-expression follows the
14472 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14473 of "&". */
14475 static tree
14476 tsubst_qualified_id (tree qualified_id, tree args,
14477 tsubst_flags_t complain, tree in_decl,
14478 bool done, bool address_p)
14480 tree expr;
14481 tree scope;
14482 tree name;
14483 bool is_template;
14484 tree template_args;
14485 location_t loc = UNKNOWN_LOCATION;
14487 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14489 /* Figure out what name to look up. */
14490 name = TREE_OPERAND (qualified_id, 1);
14491 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14493 is_template = true;
14494 loc = EXPR_LOCATION (name);
14495 template_args = TREE_OPERAND (name, 1);
14496 if (template_args)
14497 template_args = tsubst_template_args (template_args, args,
14498 complain, in_decl);
14499 if (template_args == error_mark_node)
14500 return error_mark_node;
14501 name = TREE_OPERAND (name, 0);
14503 else
14505 is_template = false;
14506 template_args = NULL_TREE;
14509 /* Substitute into the qualifying scope. When there are no ARGS, we
14510 are just trying to simplify a non-dependent expression. In that
14511 case the qualifying scope may be dependent, and, in any case,
14512 substituting will not help. */
14513 scope = TREE_OPERAND (qualified_id, 0);
14514 if (args)
14516 scope = tsubst (scope, args, complain, in_decl);
14517 expr = tsubst_copy (name, args, complain, in_decl);
14519 else
14520 expr = name;
14522 if (dependent_scope_p (scope))
14524 if (is_template)
14525 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14526 tree r = build_qualified_name (NULL_TREE, scope, expr,
14527 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14528 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14529 return r;
14532 if (!BASELINK_P (name) && !DECL_P (expr))
14534 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14536 /* A BIT_NOT_EXPR is used to represent a destructor. */
14537 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14539 error ("qualifying type %qT does not match destructor name ~%qT",
14540 scope, TREE_OPERAND (expr, 0));
14541 expr = error_mark_node;
14543 else
14544 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14545 /*is_type_p=*/0, false);
14547 else
14548 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14549 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14550 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14552 if (complain & tf_error)
14554 error ("dependent-name %qE is parsed as a non-type, but "
14555 "instantiation yields a type", qualified_id);
14556 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14558 return error_mark_node;
14562 if (DECL_P (expr))
14564 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14565 scope);
14566 /* Remember that there was a reference to this entity. */
14567 if (!mark_used (expr, complain) && !(complain & tf_error))
14568 return error_mark_node;
14571 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14573 if (complain & tf_error)
14574 qualified_name_lookup_error (scope,
14575 TREE_OPERAND (qualified_id, 1),
14576 expr, input_location);
14577 return error_mark_node;
14580 if (is_template)
14582 if (variable_template_p (expr))
14583 expr = lookup_and_finish_template_variable (expr, template_args,
14584 complain);
14585 else
14586 expr = lookup_template_function (expr, template_args);
14589 if (expr == error_mark_node && complain & tf_error)
14590 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14591 expr, input_location);
14592 else if (TYPE_P (scope))
14594 expr = (adjust_result_of_qualified_name_lookup
14595 (expr, scope, current_nonlambda_class_type ()));
14596 expr = (finish_qualified_id_expr
14597 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14598 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14599 /*template_arg_p=*/false, complain));
14602 /* Expressions do not generally have reference type. */
14603 if (TREE_CODE (expr) != SCOPE_REF
14604 /* However, if we're about to form a pointer-to-member, we just
14605 want the referenced member referenced. */
14606 && TREE_CODE (expr) != OFFSET_REF)
14607 expr = convert_from_reference (expr);
14609 if (REF_PARENTHESIZED_P (qualified_id))
14610 expr = force_paren_expr (expr);
14612 return expr;
14615 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14616 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14617 for tsubst. */
14619 static tree
14620 tsubst_init (tree init, tree decl, tree args,
14621 tsubst_flags_t complain, tree in_decl)
14623 if (!init)
14624 return NULL_TREE;
14626 init = tsubst_expr (init, args, complain, in_decl, false);
14628 if (!init && TREE_TYPE (decl) != error_mark_node)
14630 /* If we had an initializer but it
14631 instantiated to nothing,
14632 value-initialize the object. This will
14633 only occur when the initializer was a
14634 pack expansion where the parameter packs
14635 used in that expansion were of length
14636 zero. */
14637 init = build_value_init (TREE_TYPE (decl),
14638 complain);
14639 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14640 init = get_target_expr_sfinae (init, complain);
14641 if (TREE_CODE (init) == TARGET_EXPR)
14642 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14645 return init;
14648 /* Like tsubst, but deals with expressions. This function just replaces
14649 template parms; to finish processing the resultant expression, use
14650 tsubst_copy_and_build or tsubst_expr. */
14652 static tree
14653 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14655 enum tree_code code;
14656 tree r;
14658 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14659 return t;
14661 code = TREE_CODE (t);
14663 switch (code)
14665 case PARM_DECL:
14666 r = retrieve_local_specialization (t);
14668 if (r == NULL_TREE)
14670 /* We get here for a use of 'this' in an NSDMI. */
14671 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14672 return current_class_ptr;
14674 /* This can happen for a parameter name used later in a function
14675 declaration (such as in a late-specified return type). Just
14676 make a dummy decl, since it's only used for its type. */
14677 gcc_assert (cp_unevaluated_operand != 0);
14678 r = tsubst_decl (t, args, complain);
14679 /* Give it the template pattern as its context; its true context
14680 hasn't been instantiated yet and this is good enough for
14681 mangling. */
14682 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14685 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14686 r = ARGUMENT_PACK_SELECT_ARG (r);
14687 if (!mark_used (r, complain) && !(complain & tf_error))
14688 return error_mark_node;
14689 return r;
14691 case CONST_DECL:
14693 tree enum_type;
14694 tree v;
14696 if (DECL_TEMPLATE_PARM_P (t))
14697 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14698 /* There is no need to substitute into namespace-scope
14699 enumerators. */
14700 if (DECL_NAMESPACE_SCOPE_P (t))
14701 return t;
14702 /* If ARGS is NULL, then T is known to be non-dependent. */
14703 if (args == NULL_TREE)
14704 return scalar_constant_value (t);
14706 /* Unfortunately, we cannot just call lookup_name here.
14707 Consider:
14709 template <int I> int f() {
14710 enum E { a = I };
14711 struct S { void g() { E e = a; } };
14714 When we instantiate f<7>::S::g(), say, lookup_name is not
14715 clever enough to find f<7>::a. */
14716 enum_type
14717 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14718 /*entering_scope=*/0);
14720 for (v = TYPE_VALUES (enum_type);
14721 v != NULL_TREE;
14722 v = TREE_CHAIN (v))
14723 if (TREE_PURPOSE (v) == DECL_NAME (t))
14724 return TREE_VALUE (v);
14726 /* We didn't find the name. That should never happen; if
14727 name-lookup found it during preliminary parsing, we
14728 should find it again here during instantiation. */
14729 gcc_unreachable ();
14731 return t;
14733 case FIELD_DECL:
14734 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14736 /* Check for a local specialization set up by
14737 tsubst_pack_expansion. */
14738 if (tree r = retrieve_local_specialization (t))
14740 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14741 r = ARGUMENT_PACK_SELECT_ARG (r);
14742 return r;
14745 /* When retrieving a capture pack from a generic lambda, remove the
14746 lambda call op's own template argument list from ARGS. Only the
14747 template arguments active for the closure type should be used to
14748 retrieve the pack specialization. */
14749 if (LAMBDA_FUNCTION_P (current_function_decl)
14750 && (template_class_depth (DECL_CONTEXT (t))
14751 != TMPL_ARGS_DEPTH (args)))
14752 args = strip_innermost_template_args (args, 1);
14754 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14755 tsubst_decl put in the hash table. */
14756 return retrieve_specialization (t, args, 0);
14759 if (DECL_CONTEXT (t))
14761 tree ctx;
14763 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14764 /*entering_scope=*/1);
14765 if (ctx != DECL_CONTEXT (t))
14767 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14768 if (!r)
14770 if (complain & tf_error)
14771 error ("using invalid field %qD", t);
14772 return error_mark_node;
14774 return r;
14778 return t;
14780 case VAR_DECL:
14781 case FUNCTION_DECL:
14782 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14783 r = tsubst (t, args, complain, in_decl);
14784 else if (local_variable_p (t)
14785 && uses_template_parms (DECL_CONTEXT (t)))
14787 r = retrieve_local_specialization (t);
14788 if (r == NULL_TREE)
14790 /* First try name lookup to find the instantiation. */
14791 r = lookup_name (DECL_NAME (t));
14792 if (r && !is_capture_proxy (r))
14794 /* Make sure that the one we found is the one we want. */
14795 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14796 if (ctx != DECL_CONTEXT (r))
14797 r = NULL_TREE;
14800 if (r)
14801 /* OK */;
14802 else
14804 /* This can happen for a variable used in a
14805 late-specified return type of a local lambda, or for a
14806 local static or constant. Building a new VAR_DECL
14807 should be OK in all those cases. */
14808 r = tsubst_decl (t, args, complain);
14809 if (local_specializations)
14810 /* Avoid infinite recursion (79640). */
14811 register_local_specialization (r, t);
14812 if (decl_maybe_constant_var_p (r))
14814 /* We can't call cp_finish_decl, so handle the
14815 initializer by hand. */
14816 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14817 complain, in_decl);
14818 if (!processing_template_decl)
14819 init = maybe_constant_init (init);
14820 if (processing_template_decl
14821 ? potential_constant_expression (init)
14822 : reduced_constant_expression_p (init))
14823 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14824 = TREE_CONSTANT (r) = true;
14825 DECL_INITIAL (r) = init;
14826 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14827 TREE_TYPE (r)
14828 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14829 complain, adc_variable_type);
14831 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14832 || decl_constant_var_p (r)
14833 || errorcount || sorrycount);
14834 if (!processing_template_decl
14835 && !TREE_STATIC (r))
14836 r = process_outer_var_ref (r, complain);
14838 /* Remember this for subsequent uses. */
14839 if (local_specializations)
14840 register_local_specialization (r, t);
14843 else
14844 r = t;
14845 if (!mark_used (r, complain))
14846 return error_mark_node;
14847 return r;
14849 case NAMESPACE_DECL:
14850 return t;
14852 case OVERLOAD:
14853 /* An OVERLOAD will always be a non-dependent overload set; an
14854 overload set from function scope will just be represented with an
14855 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14856 gcc_assert (!uses_template_parms (t));
14857 /* We must have marked any lookups as persistent. */
14858 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14859 return t;
14861 case BASELINK:
14862 return tsubst_baselink (t, current_nonlambda_class_type (),
14863 args, complain, in_decl);
14865 case TEMPLATE_DECL:
14866 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14867 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14868 args, complain, in_decl);
14869 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14870 return tsubst (t, args, complain, in_decl);
14871 else if (DECL_CLASS_SCOPE_P (t)
14872 && uses_template_parms (DECL_CONTEXT (t)))
14874 /* Template template argument like the following example need
14875 special treatment:
14877 template <template <class> class TT> struct C {};
14878 template <class T> struct D {
14879 template <class U> struct E {};
14880 C<E> c; // #1
14882 D<int> d; // #2
14884 We are processing the template argument `E' in #1 for
14885 the template instantiation #2. Originally, `E' is a
14886 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14887 have to substitute this with one having context `D<int>'. */
14889 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14890 if (dependent_scope_p (context))
14892 /* When rewriting a constructor into a deduction guide, a
14893 non-dependent name can become dependent, so memtmpl<args>
14894 becomes context::template memtmpl<args>. */
14895 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14896 return build_qualified_name (type, context, DECL_NAME (t),
14897 /*template*/true);
14899 return lookup_field (context, DECL_NAME(t), 0, false);
14901 else
14902 /* Ordinary template template argument. */
14903 return t;
14905 case CAST_EXPR:
14906 case REINTERPRET_CAST_EXPR:
14907 case CONST_CAST_EXPR:
14908 case STATIC_CAST_EXPR:
14909 case DYNAMIC_CAST_EXPR:
14910 case IMPLICIT_CONV_EXPR:
14911 case CONVERT_EXPR:
14912 case NOP_EXPR:
14914 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14915 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14916 return build1 (code, type, op0);
14919 case SIZEOF_EXPR:
14920 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14921 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14923 tree expanded, op = TREE_OPERAND (t, 0);
14924 int len = 0;
14926 if (SIZEOF_EXPR_TYPE_P (t))
14927 op = TREE_TYPE (op);
14929 ++cp_unevaluated_operand;
14930 ++c_inhibit_evaluation_warnings;
14931 /* We only want to compute the number of arguments. */
14932 if (PACK_EXPANSION_P (op))
14933 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14934 else
14935 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14936 args, complain, in_decl);
14937 --cp_unevaluated_operand;
14938 --c_inhibit_evaluation_warnings;
14940 if (TREE_CODE (expanded) == TREE_VEC)
14942 len = TREE_VEC_LENGTH (expanded);
14943 /* Set TREE_USED for the benefit of -Wunused. */
14944 for (int i = 0; i < len; i++)
14945 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14946 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14949 if (expanded == error_mark_node)
14950 return error_mark_node;
14951 else if (PACK_EXPANSION_P (expanded)
14952 || (TREE_CODE (expanded) == TREE_VEC
14953 && pack_expansion_args_count (expanded)))
14956 if (PACK_EXPANSION_P (expanded))
14957 /* OK. */;
14958 else if (TREE_VEC_LENGTH (expanded) == 1)
14959 expanded = TREE_VEC_ELT (expanded, 0);
14960 else
14961 expanded = make_argument_pack (expanded);
14963 if (TYPE_P (expanded))
14964 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14965 complain & tf_error);
14966 else
14967 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14968 complain & tf_error);
14970 else
14971 return build_int_cst (size_type_node, len);
14973 if (SIZEOF_EXPR_TYPE_P (t))
14975 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14976 args, complain, in_decl);
14977 r = build1 (NOP_EXPR, r, error_mark_node);
14978 r = build1 (SIZEOF_EXPR,
14979 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14980 SIZEOF_EXPR_TYPE_P (r) = 1;
14981 return r;
14983 /* Fall through */
14985 case INDIRECT_REF:
14986 case NEGATE_EXPR:
14987 case TRUTH_NOT_EXPR:
14988 case BIT_NOT_EXPR:
14989 case ADDR_EXPR:
14990 case UNARY_PLUS_EXPR: /* Unary + */
14991 case ALIGNOF_EXPR:
14992 case AT_ENCODE_EXPR:
14993 case ARROW_EXPR:
14994 case THROW_EXPR:
14995 case TYPEID_EXPR:
14996 case REALPART_EXPR:
14997 case IMAGPART_EXPR:
14998 case PAREN_EXPR:
15000 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15001 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15002 return build1 (code, type, op0);
15005 case COMPONENT_REF:
15007 tree object;
15008 tree name;
15010 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15011 name = TREE_OPERAND (t, 1);
15012 if (TREE_CODE (name) == BIT_NOT_EXPR)
15014 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15015 complain, in_decl);
15016 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15018 else if (TREE_CODE (name) == SCOPE_REF
15019 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15021 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15022 complain, in_decl);
15023 name = TREE_OPERAND (name, 1);
15024 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15025 complain, in_decl);
15026 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15027 name = build_qualified_name (/*type=*/NULL_TREE,
15028 base, name,
15029 /*template_p=*/false);
15031 else if (BASELINK_P (name))
15032 name = tsubst_baselink (name,
15033 non_reference (TREE_TYPE (object)),
15034 args, complain,
15035 in_decl);
15036 else
15037 name = tsubst_copy (name, args, complain, in_decl);
15038 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15041 case PLUS_EXPR:
15042 case MINUS_EXPR:
15043 case MULT_EXPR:
15044 case TRUNC_DIV_EXPR:
15045 case CEIL_DIV_EXPR:
15046 case FLOOR_DIV_EXPR:
15047 case ROUND_DIV_EXPR:
15048 case EXACT_DIV_EXPR:
15049 case BIT_AND_EXPR:
15050 case BIT_IOR_EXPR:
15051 case BIT_XOR_EXPR:
15052 case TRUNC_MOD_EXPR:
15053 case FLOOR_MOD_EXPR:
15054 case TRUTH_ANDIF_EXPR:
15055 case TRUTH_ORIF_EXPR:
15056 case TRUTH_AND_EXPR:
15057 case TRUTH_OR_EXPR:
15058 case RSHIFT_EXPR:
15059 case LSHIFT_EXPR:
15060 case RROTATE_EXPR:
15061 case LROTATE_EXPR:
15062 case EQ_EXPR:
15063 case NE_EXPR:
15064 case MAX_EXPR:
15065 case MIN_EXPR:
15066 case LE_EXPR:
15067 case GE_EXPR:
15068 case LT_EXPR:
15069 case GT_EXPR:
15070 case COMPOUND_EXPR:
15071 case DOTSTAR_EXPR:
15072 case MEMBER_REF:
15073 case PREDECREMENT_EXPR:
15074 case PREINCREMENT_EXPR:
15075 case POSTDECREMENT_EXPR:
15076 case POSTINCREMENT_EXPR:
15078 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15079 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15080 return build_nt (code, op0, op1);
15083 case SCOPE_REF:
15085 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15086 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15087 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15088 QUALIFIED_NAME_IS_TEMPLATE (t));
15091 case ARRAY_REF:
15093 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15094 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15095 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15098 case CALL_EXPR:
15100 int n = VL_EXP_OPERAND_LENGTH (t);
15101 tree result = build_vl_exp (CALL_EXPR, n);
15102 int i;
15103 for (i = 0; i < n; i++)
15104 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15105 complain, in_decl);
15106 return result;
15109 case COND_EXPR:
15110 case MODOP_EXPR:
15111 case PSEUDO_DTOR_EXPR:
15112 case VEC_PERM_EXPR:
15114 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15115 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15116 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15117 r = build_nt (code, op0, op1, op2);
15118 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15119 return r;
15122 case NEW_EXPR:
15124 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15125 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15126 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15127 r = build_nt (code, op0, op1, op2);
15128 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15129 return r;
15132 case DELETE_EXPR:
15134 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15135 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15136 r = build_nt (code, op0, op1);
15137 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15138 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15139 return r;
15142 case TEMPLATE_ID_EXPR:
15144 /* Substituted template arguments */
15145 tree fn = TREE_OPERAND (t, 0);
15146 tree targs = TREE_OPERAND (t, 1);
15148 fn = tsubst_copy (fn, args, complain, in_decl);
15149 if (targs)
15150 targs = tsubst_template_args (targs, args, complain, in_decl);
15152 return lookup_template_function (fn, targs);
15155 case TREE_LIST:
15157 tree purpose, value, chain;
15159 if (t == void_list_node)
15160 return t;
15162 purpose = TREE_PURPOSE (t);
15163 if (purpose)
15164 purpose = tsubst_copy (purpose, args, complain, in_decl);
15165 value = TREE_VALUE (t);
15166 if (value)
15167 value = tsubst_copy (value, args, complain, in_decl);
15168 chain = TREE_CHAIN (t);
15169 if (chain && chain != void_type_node)
15170 chain = tsubst_copy (chain, args, complain, in_decl);
15171 if (purpose == TREE_PURPOSE (t)
15172 && value == TREE_VALUE (t)
15173 && chain == TREE_CHAIN (t))
15174 return t;
15175 return tree_cons (purpose, value, chain);
15178 case RECORD_TYPE:
15179 case UNION_TYPE:
15180 case ENUMERAL_TYPE:
15181 case INTEGER_TYPE:
15182 case TEMPLATE_TYPE_PARM:
15183 case TEMPLATE_TEMPLATE_PARM:
15184 case BOUND_TEMPLATE_TEMPLATE_PARM:
15185 case TEMPLATE_PARM_INDEX:
15186 case POINTER_TYPE:
15187 case REFERENCE_TYPE:
15188 case OFFSET_TYPE:
15189 case FUNCTION_TYPE:
15190 case METHOD_TYPE:
15191 case ARRAY_TYPE:
15192 case TYPENAME_TYPE:
15193 case UNBOUND_CLASS_TEMPLATE:
15194 case TYPEOF_TYPE:
15195 case DECLTYPE_TYPE:
15196 case TYPE_DECL:
15197 return tsubst (t, args, complain, in_decl);
15199 case USING_DECL:
15200 t = DECL_NAME (t);
15201 /* Fall through. */
15202 case IDENTIFIER_NODE:
15203 if (IDENTIFIER_CONV_OP_P (t))
15205 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15206 return make_conv_op_name (new_type);
15208 else
15209 return t;
15211 case CONSTRUCTOR:
15212 /* This is handled by tsubst_copy_and_build. */
15213 gcc_unreachable ();
15215 case VA_ARG_EXPR:
15217 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15218 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15219 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15222 case CLEANUP_POINT_EXPR:
15223 /* We shouldn't have built any of these during initial template
15224 generation. Instead, they should be built during instantiation
15225 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15226 gcc_unreachable ();
15228 case OFFSET_REF:
15230 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15231 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15232 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15233 r = build2 (code, type, op0, op1);
15234 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15235 if (!mark_used (TREE_OPERAND (r, 1), complain)
15236 && !(complain & tf_error))
15237 return error_mark_node;
15238 return r;
15241 case EXPR_PACK_EXPANSION:
15242 error ("invalid use of pack expansion expression");
15243 return error_mark_node;
15245 case NONTYPE_ARGUMENT_PACK:
15246 error ("use %<...%> to expand argument pack");
15247 return error_mark_node;
15249 case VOID_CST:
15250 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15251 return t;
15253 case INTEGER_CST:
15254 case REAL_CST:
15255 case STRING_CST:
15256 case COMPLEX_CST:
15258 /* Instantiate any typedefs in the type. */
15259 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15260 r = fold_convert (type, t);
15261 gcc_assert (TREE_CODE (r) == code);
15262 return r;
15265 case PTRMEM_CST:
15266 /* These can sometimes show up in a partial instantiation, but never
15267 involve template parms. */
15268 gcc_assert (!uses_template_parms (t));
15269 return t;
15271 case UNARY_LEFT_FOLD_EXPR:
15272 return tsubst_unary_left_fold (t, args, complain, in_decl);
15273 case UNARY_RIGHT_FOLD_EXPR:
15274 return tsubst_unary_right_fold (t, args, complain, in_decl);
15275 case BINARY_LEFT_FOLD_EXPR:
15276 return tsubst_binary_left_fold (t, args, complain, in_decl);
15277 case BINARY_RIGHT_FOLD_EXPR:
15278 return tsubst_binary_right_fold (t, args, complain, in_decl);
15279 case PREDICT_EXPR:
15280 return t;
15282 default:
15283 /* We shouldn't get here, but keep going if !flag_checking. */
15284 if (flag_checking)
15285 gcc_unreachable ();
15286 return t;
15290 /* Helper function for tsubst_omp_clauses, used for instantiation of
15291 OMP_CLAUSE_DECL of clauses. */
15293 static tree
15294 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15295 tree in_decl)
15297 if (decl == NULL_TREE)
15298 return NULL_TREE;
15300 /* Handle an OpenMP array section represented as a TREE_LIST (or
15301 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15302 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15303 TREE_LIST. We can handle it exactly the same as an array section
15304 (purpose, value, and a chain), even though the nomenclature
15305 (low_bound, length, etc) is different. */
15306 if (TREE_CODE (decl) == TREE_LIST)
15308 tree low_bound
15309 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15310 /*integral_constant_expression_p=*/false);
15311 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15312 /*integral_constant_expression_p=*/false);
15313 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15314 in_decl);
15315 if (TREE_PURPOSE (decl) == low_bound
15316 && TREE_VALUE (decl) == length
15317 && TREE_CHAIN (decl) == chain)
15318 return decl;
15319 tree ret = tree_cons (low_bound, length, chain);
15320 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15321 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15322 return ret;
15324 tree ret = tsubst_expr (decl, args, complain, in_decl,
15325 /*integral_constant_expression_p=*/false);
15326 /* Undo convert_from_reference tsubst_expr could have called. */
15327 if (decl
15328 && REFERENCE_REF_P (ret)
15329 && !REFERENCE_REF_P (decl))
15330 ret = TREE_OPERAND (ret, 0);
15331 return ret;
15334 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15336 static tree
15337 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15338 tree args, tsubst_flags_t complain, tree in_decl)
15340 tree new_clauses = NULL_TREE, nc, oc;
15341 tree linear_no_step = NULL_TREE;
15343 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15345 nc = copy_node (oc);
15346 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15347 new_clauses = nc;
15349 switch (OMP_CLAUSE_CODE (nc))
15351 case OMP_CLAUSE_LASTPRIVATE:
15352 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15354 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15355 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15356 in_decl, /*integral_constant_expression_p=*/false);
15357 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15358 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15360 /* FALLTHRU */
15361 case OMP_CLAUSE_PRIVATE:
15362 case OMP_CLAUSE_SHARED:
15363 case OMP_CLAUSE_FIRSTPRIVATE:
15364 case OMP_CLAUSE_COPYIN:
15365 case OMP_CLAUSE_COPYPRIVATE:
15366 case OMP_CLAUSE_UNIFORM:
15367 case OMP_CLAUSE_DEPEND:
15368 case OMP_CLAUSE_FROM:
15369 case OMP_CLAUSE_TO:
15370 case OMP_CLAUSE_MAP:
15371 case OMP_CLAUSE_USE_DEVICE_PTR:
15372 case OMP_CLAUSE_IS_DEVICE_PTR:
15373 OMP_CLAUSE_DECL (nc)
15374 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15375 in_decl);
15376 break;
15377 case OMP_CLAUSE_TILE:
15378 case OMP_CLAUSE_IF:
15379 case OMP_CLAUSE_NUM_THREADS:
15380 case OMP_CLAUSE_SCHEDULE:
15381 case OMP_CLAUSE_COLLAPSE:
15382 case OMP_CLAUSE_FINAL:
15383 case OMP_CLAUSE_DEVICE:
15384 case OMP_CLAUSE_DIST_SCHEDULE:
15385 case OMP_CLAUSE_NUM_TEAMS:
15386 case OMP_CLAUSE_THREAD_LIMIT:
15387 case OMP_CLAUSE_SAFELEN:
15388 case OMP_CLAUSE_SIMDLEN:
15389 case OMP_CLAUSE_NUM_TASKS:
15390 case OMP_CLAUSE_GRAINSIZE:
15391 case OMP_CLAUSE_PRIORITY:
15392 case OMP_CLAUSE_ORDERED:
15393 case OMP_CLAUSE_HINT:
15394 case OMP_CLAUSE_NUM_GANGS:
15395 case OMP_CLAUSE_NUM_WORKERS:
15396 case OMP_CLAUSE_VECTOR_LENGTH:
15397 case OMP_CLAUSE_WORKER:
15398 case OMP_CLAUSE_VECTOR:
15399 case OMP_CLAUSE_ASYNC:
15400 case OMP_CLAUSE_WAIT:
15401 OMP_CLAUSE_OPERAND (nc, 0)
15402 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15403 in_decl, /*integral_constant_expression_p=*/false);
15404 break;
15405 case OMP_CLAUSE_REDUCTION:
15406 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15408 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15409 if (TREE_CODE (placeholder) == SCOPE_REF)
15411 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15412 complain, in_decl);
15413 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15414 = build_qualified_name (NULL_TREE, scope,
15415 TREE_OPERAND (placeholder, 1),
15416 false);
15418 else
15419 gcc_assert (identifier_p (placeholder));
15421 OMP_CLAUSE_DECL (nc)
15422 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15423 in_decl);
15424 break;
15425 case OMP_CLAUSE_GANG:
15426 case OMP_CLAUSE_ALIGNED:
15427 OMP_CLAUSE_DECL (nc)
15428 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15429 in_decl);
15430 OMP_CLAUSE_OPERAND (nc, 1)
15431 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15432 in_decl, /*integral_constant_expression_p=*/false);
15433 break;
15434 case OMP_CLAUSE_LINEAR:
15435 OMP_CLAUSE_DECL (nc)
15436 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15437 in_decl);
15438 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15440 gcc_assert (!linear_no_step);
15441 linear_no_step = nc;
15443 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15444 OMP_CLAUSE_LINEAR_STEP (nc)
15445 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15446 complain, in_decl);
15447 else
15448 OMP_CLAUSE_LINEAR_STEP (nc)
15449 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15450 in_decl,
15451 /*integral_constant_expression_p=*/false);
15452 break;
15453 case OMP_CLAUSE_NOWAIT:
15454 case OMP_CLAUSE_DEFAULT:
15455 case OMP_CLAUSE_UNTIED:
15456 case OMP_CLAUSE_MERGEABLE:
15457 case OMP_CLAUSE_INBRANCH:
15458 case OMP_CLAUSE_NOTINBRANCH:
15459 case OMP_CLAUSE_PROC_BIND:
15460 case OMP_CLAUSE_FOR:
15461 case OMP_CLAUSE_PARALLEL:
15462 case OMP_CLAUSE_SECTIONS:
15463 case OMP_CLAUSE_TASKGROUP:
15464 case OMP_CLAUSE_NOGROUP:
15465 case OMP_CLAUSE_THREADS:
15466 case OMP_CLAUSE_SIMD:
15467 case OMP_CLAUSE_DEFAULTMAP:
15468 case OMP_CLAUSE_INDEPENDENT:
15469 case OMP_CLAUSE_AUTO:
15470 case OMP_CLAUSE_SEQ:
15471 break;
15472 default:
15473 gcc_unreachable ();
15475 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15476 switch (OMP_CLAUSE_CODE (nc))
15478 case OMP_CLAUSE_SHARED:
15479 case OMP_CLAUSE_PRIVATE:
15480 case OMP_CLAUSE_FIRSTPRIVATE:
15481 case OMP_CLAUSE_LASTPRIVATE:
15482 case OMP_CLAUSE_COPYPRIVATE:
15483 case OMP_CLAUSE_LINEAR:
15484 case OMP_CLAUSE_REDUCTION:
15485 case OMP_CLAUSE_USE_DEVICE_PTR:
15486 case OMP_CLAUSE_IS_DEVICE_PTR:
15487 /* tsubst_expr on SCOPE_REF results in returning
15488 finish_non_static_data_member result. Undo that here. */
15489 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15490 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15491 == IDENTIFIER_NODE))
15493 tree t = OMP_CLAUSE_DECL (nc);
15494 tree v = t;
15495 while (v)
15496 switch (TREE_CODE (v))
15498 case COMPONENT_REF:
15499 case MEM_REF:
15500 case INDIRECT_REF:
15501 CASE_CONVERT:
15502 case POINTER_PLUS_EXPR:
15503 v = TREE_OPERAND (v, 0);
15504 continue;
15505 case PARM_DECL:
15506 if (DECL_CONTEXT (v) == current_function_decl
15507 && DECL_ARTIFICIAL (v)
15508 && DECL_NAME (v) == this_identifier)
15509 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15510 /* FALLTHRU */
15511 default:
15512 v = NULL_TREE;
15513 break;
15516 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15517 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15518 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15519 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15520 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15522 tree decl = OMP_CLAUSE_DECL (nc);
15523 if (VAR_P (decl))
15525 retrofit_lang_decl (decl);
15526 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15529 break;
15530 default:
15531 break;
15535 new_clauses = nreverse (new_clauses);
15536 if (ort != C_ORT_OMP_DECLARE_SIMD)
15538 new_clauses = finish_omp_clauses (new_clauses, ort);
15539 if (linear_no_step)
15540 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15541 if (nc == linear_no_step)
15543 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15544 break;
15547 return new_clauses;
15550 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15552 static tree
15553 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15554 tree in_decl)
15556 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15558 tree purpose, value, chain;
15560 if (t == NULL)
15561 return t;
15563 if (TREE_CODE (t) != TREE_LIST)
15564 return tsubst_copy_and_build (t, args, complain, in_decl,
15565 /*function_p=*/false,
15566 /*integral_constant_expression_p=*/false);
15568 if (t == void_list_node)
15569 return t;
15571 purpose = TREE_PURPOSE (t);
15572 if (purpose)
15573 purpose = RECUR (purpose);
15574 value = TREE_VALUE (t);
15575 if (value)
15577 if (TREE_CODE (value) != LABEL_DECL)
15578 value = RECUR (value);
15579 else
15581 value = lookup_label (DECL_NAME (value));
15582 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15583 TREE_USED (value) = 1;
15586 chain = TREE_CHAIN (t);
15587 if (chain && chain != void_type_node)
15588 chain = RECUR (chain);
15589 return tree_cons (purpose, value, chain);
15590 #undef RECUR
15593 /* Used to temporarily communicate the list of #pragma omp parallel
15594 clauses to #pragma omp for instantiation if they are combined
15595 together. */
15597 static tree *omp_parallel_combined_clauses;
15599 /* Substitute one OMP_FOR iterator. */
15601 static void
15602 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15603 tree initv, tree condv, tree incrv, tree *clauses,
15604 tree args, tsubst_flags_t complain, tree in_decl,
15605 bool integral_constant_expression_p)
15607 #define RECUR(NODE) \
15608 tsubst_expr ((NODE), args, complain, in_decl, \
15609 integral_constant_expression_p)
15610 tree decl, init, cond, incr;
15612 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15613 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15615 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15617 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15618 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15621 decl = TREE_OPERAND (init, 0);
15622 init = TREE_OPERAND (init, 1);
15623 tree decl_expr = NULL_TREE;
15624 if (init && TREE_CODE (init) == DECL_EXPR)
15626 /* We need to jump through some hoops to handle declarations in the
15627 init-statement, since we might need to handle auto deduction,
15628 but we need to keep control of initialization. */
15629 decl_expr = init;
15630 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15631 decl = tsubst_decl (decl, args, complain);
15633 else
15635 if (TREE_CODE (decl) == SCOPE_REF)
15637 decl = RECUR (decl);
15638 if (TREE_CODE (decl) == COMPONENT_REF)
15640 tree v = decl;
15641 while (v)
15642 switch (TREE_CODE (v))
15644 case COMPONENT_REF:
15645 case MEM_REF:
15646 case INDIRECT_REF:
15647 CASE_CONVERT:
15648 case POINTER_PLUS_EXPR:
15649 v = TREE_OPERAND (v, 0);
15650 continue;
15651 case PARM_DECL:
15652 if (DECL_CONTEXT (v) == current_function_decl
15653 && DECL_ARTIFICIAL (v)
15654 && DECL_NAME (v) == this_identifier)
15656 decl = TREE_OPERAND (decl, 1);
15657 decl = omp_privatize_field (decl, false);
15659 /* FALLTHRU */
15660 default:
15661 v = NULL_TREE;
15662 break;
15666 else
15667 decl = RECUR (decl);
15669 init = RECUR (init);
15671 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15672 if (auto_node && init)
15673 TREE_TYPE (decl)
15674 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15676 gcc_assert (!type_dependent_expression_p (decl));
15678 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15680 if (decl_expr)
15682 /* Declare the variable, but don't let that initialize it. */
15683 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15684 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15685 RECUR (decl_expr);
15686 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15689 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15690 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15691 if (TREE_CODE (incr) == MODIFY_EXPR)
15693 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15694 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15695 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15696 NOP_EXPR, rhs, complain);
15698 else
15699 incr = RECUR (incr);
15700 TREE_VEC_ELT (declv, i) = decl;
15701 TREE_VEC_ELT (initv, i) = init;
15702 TREE_VEC_ELT (condv, i) = cond;
15703 TREE_VEC_ELT (incrv, i) = incr;
15704 return;
15707 if (decl_expr)
15709 /* Declare and initialize the variable. */
15710 RECUR (decl_expr);
15711 init = NULL_TREE;
15713 else if (init)
15715 tree *pc;
15716 int j;
15717 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15719 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15721 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15722 && OMP_CLAUSE_DECL (*pc) == decl)
15723 break;
15724 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15725 && OMP_CLAUSE_DECL (*pc) == decl)
15727 if (j)
15728 break;
15729 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15730 tree c = *pc;
15731 *pc = OMP_CLAUSE_CHAIN (c);
15732 OMP_CLAUSE_CHAIN (c) = *clauses;
15733 *clauses = c;
15735 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15736 && OMP_CLAUSE_DECL (*pc) == decl)
15738 error ("iteration variable %qD should not be firstprivate",
15739 decl);
15740 *pc = OMP_CLAUSE_CHAIN (*pc);
15742 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15743 && OMP_CLAUSE_DECL (*pc) == decl)
15745 error ("iteration variable %qD should not be reduction",
15746 decl);
15747 *pc = OMP_CLAUSE_CHAIN (*pc);
15749 else
15750 pc = &OMP_CLAUSE_CHAIN (*pc);
15752 if (*pc)
15753 break;
15755 if (*pc == NULL_TREE)
15757 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15758 OMP_CLAUSE_DECL (c) = decl;
15759 c = finish_omp_clauses (c, C_ORT_OMP);
15760 if (c)
15762 OMP_CLAUSE_CHAIN (c) = *clauses;
15763 *clauses = c;
15767 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15768 if (COMPARISON_CLASS_P (cond))
15770 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15771 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15772 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15774 else
15775 cond = RECUR (cond);
15776 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15777 switch (TREE_CODE (incr))
15779 case PREINCREMENT_EXPR:
15780 case PREDECREMENT_EXPR:
15781 case POSTINCREMENT_EXPR:
15782 case POSTDECREMENT_EXPR:
15783 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15784 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15785 break;
15786 case MODIFY_EXPR:
15787 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15788 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15790 tree rhs = TREE_OPERAND (incr, 1);
15791 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15792 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15793 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15794 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15795 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15796 rhs0, rhs1));
15798 else
15799 incr = RECUR (incr);
15800 break;
15801 case MODOP_EXPR:
15802 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15803 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15805 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15806 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15807 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15808 TREE_TYPE (decl), lhs,
15809 RECUR (TREE_OPERAND (incr, 2))));
15811 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15812 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15813 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15815 tree rhs = TREE_OPERAND (incr, 2);
15816 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15817 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15818 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15819 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15820 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15821 rhs0, rhs1));
15823 else
15824 incr = RECUR (incr);
15825 break;
15826 default:
15827 incr = RECUR (incr);
15828 break;
15831 TREE_VEC_ELT (declv, i) = decl;
15832 TREE_VEC_ELT (initv, i) = init;
15833 TREE_VEC_ELT (condv, i) = cond;
15834 TREE_VEC_ELT (incrv, i) = incr;
15835 #undef RECUR
15838 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15839 of OMP_TARGET's body. */
15841 static tree
15842 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15844 *walk_subtrees = 0;
15845 switch (TREE_CODE (*tp))
15847 case OMP_TEAMS:
15848 return *tp;
15849 case BIND_EXPR:
15850 case STATEMENT_LIST:
15851 *walk_subtrees = 1;
15852 break;
15853 default:
15854 break;
15856 return NULL_TREE;
15859 /* Helper function for tsubst_expr. For decomposition declaration
15860 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15861 also the corresponding decls representing the identifiers
15862 of the decomposition declaration. Return DECL if successful
15863 or error_mark_node otherwise, set *FIRST to the first decl
15864 in the list chained through DECL_CHAIN and *CNT to the number
15865 of such decls. */
15867 static tree
15868 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15869 tsubst_flags_t complain, tree in_decl, tree *first,
15870 unsigned int *cnt)
15872 tree decl2, decl3, prev = decl;
15873 *cnt = 0;
15874 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15875 for (decl2 = DECL_CHAIN (pattern_decl);
15876 decl2
15877 && VAR_P (decl2)
15878 && DECL_DECOMPOSITION_P (decl2)
15879 && DECL_NAME (decl2);
15880 decl2 = DECL_CHAIN (decl2))
15882 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15884 gcc_assert (errorcount);
15885 return error_mark_node;
15887 (*cnt)++;
15888 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15889 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15890 tree v = DECL_VALUE_EXPR (decl2);
15891 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15892 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15893 decl3 = tsubst (decl2, args, complain, in_decl);
15894 SET_DECL_VALUE_EXPR (decl2, v);
15895 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15896 if (VAR_P (decl3))
15897 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15898 maybe_push_decl (decl3);
15899 if (error_operand_p (decl3))
15900 decl = error_mark_node;
15901 else if (decl != error_mark_node
15902 && DECL_CHAIN (decl3) != prev)
15904 gcc_assert (errorcount);
15905 decl = error_mark_node;
15907 else
15908 prev = decl3;
15910 *first = prev;
15911 return decl;
15914 /* Like tsubst_copy for expressions, etc. but also does semantic
15915 processing. */
15917 tree
15918 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15919 bool integral_constant_expression_p)
15921 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15922 #define RECUR(NODE) \
15923 tsubst_expr ((NODE), args, complain, in_decl, \
15924 integral_constant_expression_p)
15926 tree stmt, tmp;
15927 tree r;
15928 location_t loc;
15930 if (t == NULL_TREE || t == error_mark_node)
15931 return t;
15933 loc = input_location;
15934 if (EXPR_HAS_LOCATION (t))
15935 input_location = EXPR_LOCATION (t);
15936 if (STATEMENT_CODE_P (TREE_CODE (t)))
15937 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15939 switch (TREE_CODE (t))
15941 case STATEMENT_LIST:
15943 tree_stmt_iterator i;
15944 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15945 RECUR (tsi_stmt (i));
15946 break;
15949 case CTOR_INITIALIZER:
15950 finish_mem_initializers (tsubst_initializer_list
15951 (TREE_OPERAND (t, 0), args));
15952 break;
15954 case RETURN_EXPR:
15955 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15956 break;
15958 case EXPR_STMT:
15959 tmp = RECUR (EXPR_STMT_EXPR (t));
15960 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15961 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15962 else
15963 finish_expr_stmt (tmp);
15964 break;
15966 case USING_STMT:
15967 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15968 /*attribs=*/NULL_TREE);
15969 break;
15971 case DECL_EXPR:
15973 tree decl, pattern_decl;
15974 tree init;
15976 pattern_decl = decl = DECL_EXPR_DECL (t);
15977 if (TREE_CODE (decl) == LABEL_DECL)
15978 finish_label_decl (DECL_NAME (decl));
15979 else if (TREE_CODE (decl) == USING_DECL)
15981 tree scope = USING_DECL_SCOPE (decl);
15982 tree name = DECL_NAME (decl);
15984 scope = tsubst (scope, args, complain, in_decl);
15985 decl = lookup_qualified_name (scope, name,
15986 /*is_type_p=*/false,
15987 /*complain=*/false);
15988 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15989 qualified_name_lookup_error (scope, name, decl, input_location);
15990 else
15991 finish_local_using_decl (decl, scope, name);
15993 else if (DECL_PACK_P (decl))
15995 /* Don't build up decls for a variadic capture proxy, we'll
15996 instantiate the elements directly as needed. */
15997 break;
15999 else if (is_capture_proxy (decl)
16000 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16002 /* We're in tsubst_lambda_expr, we've already inserted a new
16003 capture proxy, so look it up and register it. */
16004 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16005 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16006 gcc_assert (inst != decl && is_capture_proxy (inst));
16007 register_local_specialization (inst, decl);
16008 break;
16010 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16011 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16012 /* Don't copy the old closure; we'll create a new one in
16013 tsubst_lambda_expr. */
16014 break;
16015 else
16017 init = DECL_INITIAL (decl);
16018 decl = tsubst (decl, args, complain, in_decl);
16019 if (decl != error_mark_node)
16021 /* By marking the declaration as instantiated, we avoid
16022 trying to instantiate it. Since instantiate_decl can't
16023 handle local variables, and since we've already done
16024 all that needs to be done, that's the right thing to
16025 do. */
16026 if (VAR_P (decl))
16027 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16028 if (VAR_P (decl)
16029 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16030 /* Anonymous aggregates are a special case. */
16031 finish_anon_union (decl);
16032 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16034 DECL_CONTEXT (decl) = current_function_decl;
16035 if (DECL_NAME (decl) == this_identifier)
16037 tree lam = DECL_CONTEXT (current_function_decl);
16038 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16039 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16041 insert_capture_proxy (decl);
16043 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16044 /* We already did a pushtag. */;
16045 else if (TREE_CODE (decl) == FUNCTION_DECL
16046 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16047 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16049 DECL_CONTEXT (decl) = NULL_TREE;
16050 pushdecl (decl);
16051 DECL_CONTEXT (decl) = current_function_decl;
16052 cp_check_omp_declare_reduction (decl);
16054 else
16056 int const_init = false;
16057 maybe_push_decl (decl);
16058 if (VAR_P (decl)
16059 && DECL_PRETTY_FUNCTION_P (decl))
16061 /* For __PRETTY_FUNCTION__ we have to adjust the
16062 initializer. */
16063 const char *const name
16064 = cxx_printable_name (current_function_decl, 2);
16065 init = cp_fname_init (name, &TREE_TYPE (decl));
16067 else
16068 init = tsubst_init (init, decl, args, complain, in_decl);
16070 if (VAR_P (decl))
16071 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16072 (pattern_decl));
16073 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16074 if (VAR_P (decl)
16075 && DECL_DECOMPOSITION_P (decl)
16076 && TREE_TYPE (pattern_decl) != error_mark_node)
16078 unsigned int cnt;
16079 tree first;
16080 decl = tsubst_decomp_names (decl, pattern_decl, args,
16081 complain, in_decl, &first,
16082 &cnt);
16083 if (decl != error_mark_node)
16084 cp_finish_decomp (decl, first, cnt);
16090 break;
16093 case FOR_STMT:
16094 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16095 RECUR (FOR_INIT_STMT (t));
16096 finish_init_stmt (stmt);
16097 tmp = RECUR (FOR_COND (t));
16098 finish_for_cond (tmp, stmt, false);
16099 tmp = RECUR (FOR_EXPR (t));
16100 finish_for_expr (tmp, stmt);
16101 RECUR (FOR_BODY (t));
16102 finish_for_stmt (stmt);
16103 break;
16105 case RANGE_FOR_STMT:
16107 tree decl, expr;
16108 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16109 decl = RANGE_FOR_DECL (t);
16110 decl = tsubst (decl, args, complain, in_decl);
16111 maybe_push_decl (decl);
16112 expr = RECUR (RANGE_FOR_EXPR (t));
16113 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16115 unsigned int cnt;
16116 tree first;
16117 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16118 complain, in_decl, &first, &cnt);
16119 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16120 RANGE_FOR_IVDEP (t));
16122 else
16123 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16124 RANGE_FOR_IVDEP (t));
16125 RECUR (RANGE_FOR_BODY (t));
16126 finish_for_stmt (stmt);
16128 break;
16130 case WHILE_STMT:
16131 stmt = begin_while_stmt ();
16132 tmp = RECUR (WHILE_COND (t));
16133 finish_while_stmt_cond (tmp, stmt, false);
16134 RECUR (WHILE_BODY (t));
16135 finish_while_stmt (stmt);
16136 break;
16138 case DO_STMT:
16139 stmt = begin_do_stmt ();
16140 RECUR (DO_BODY (t));
16141 finish_do_body (stmt);
16142 tmp = RECUR (DO_COND (t));
16143 finish_do_stmt (tmp, stmt, false);
16144 break;
16146 case IF_STMT:
16147 stmt = begin_if_stmt ();
16148 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16149 tmp = RECUR (IF_COND (t));
16150 tmp = finish_if_stmt_cond (tmp, stmt);
16151 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16152 /* Don't instantiate the THEN_CLAUSE. */;
16153 else
16155 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16156 if (inhibit)
16157 ++c_inhibit_evaluation_warnings;
16158 RECUR (THEN_CLAUSE (t));
16159 if (inhibit)
16160 --c_inhibit_evaluation_warnings;
16162 finish_then_clause (stmt);
16164 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16165 /* Don't instantiate the ELSE_CLAUSE. */;
16166 else if (ELSE_CLAUSE (t))
16168 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16169 begin_else_clause (stmt);
16170 if (inhibit)
16171 ++c_inhibit_evaluation_warnings;
16172 RECUR (ELSE_CLAUSE (t));
16173 if (inhibit)
16174 --c_inhibit_evaluation_warnings;
16175 finish_else_clause (stmt);
16178 finish_if_stmt (stmt);
16179 break;
16181 case BIND_EXPR:
16182 if (BIND_EXPR_BODY_BLOCK (t))
16183 stmt = begin_function_body ();
16184 else
16185 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16186 ? BCS_TRY_BLOCK : 0);
16188 RECUR (BIND_EXPR_BODY (t));
16190 if (BIND_EXPR_BODY_BLOCK (t))
16191 finish_function_body (stmt);
16192 else
16193 finish_compound_stmt (stmt);
16194 break;
16196 case BREAK_STMT:
16197 finish_break_stmt ();
16198 break;
16200 case CONTINUE_STMT:
16201 finish_continue_stmt ();
16202 break;
16204 case SWITCH_STMT:
16205 stmt = begin_switch_stmt ();
16206 tmp = RECUR (SWITCH_STMT_COND (t));
16207 finish_switch_cond (tmp, stmt);
16208 RECUR (SWITCH_STMT_BODY (t));
16209 finish_switch_stmt (stmt);
16210 break;
16212 case CASE_LABEL_EXPR:
16214 tree low = RECUR (CASE_LOW (t));
16215 tree high = RECUR (CASE_HIGH (t));
16216 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16217 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16218 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16219 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16221 break;
16223 case LABEL_EXPR:
16225 tree decl = LABEL_EXPR_LABEL (t);
16226 tree label;
16228 label = finish_label_stmt (DECL_NAME (decl));
16229 if (TREE_CODE (label) == LABEL_DECL)
16230 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16231 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16232 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16234 break;
16236 case GOTO_EXPR:
16237 tmp = GOTO_DESTINATION (t);
16238 if (TREE_CODE (tmp) != LABEL_DECL)
16239 /* Computed goto's must be tsubst'd into. On the other hand,
16240 non-computed gotos must not be; the identifier in question
16241 will have no binding. */
16242 tmp = RECUR (tmp);
16243 else
16244 tmp = DECL_NAME (tmp);
16245 finish_goto_stmt (tmp);
16246 break;
16248 case ASM_EXPR:
16250 tree string = RECUR (ASM_STRING (t));
16251 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16252 complain, in_decl);
16253 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16254 complain, in_decl);
16255 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16256 complain, in_decl);
16257 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16258 complain, in_decl);
16259 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16260 clobbers, labels);
16261 tree asm_expr = tmp;
16262 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16263 asm_expr = TREE_OPERAND (asm_expr, 0);
16264 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16266 break;
16268 case TRY_BLOCK:
16269 if (CLEANUP_P (t))
16271 stmt = begin_try_block ();
16272 RECUR (TRY_STMTS (t));
16273 finish_cleanup_try_block (stmt);
16274 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16276 else
16278 tree compound_stmt = NULL_TREE;
16280 if (FN_TRY_BLOCK_P (t))
16281 stmt = begin_function_try_block (&compound_stmt);
16282 else
16283 stmt = begin_try_block ();
16285 RECUR (TRY_STMTS (t));
16287 if (FN_TRY_BLOCK_P (t))
16288 finish_function_try_block (stmt);
16289 else
16290 finish_try_block (stmt);
16292 RECUR (TRY_HANDLERS (t));
16293 if (FN_TRY_BLOCK_P (t))
16294 finish_function_handler_sequence (stmt, compound_stmt);
16295 else
16296 finish_handler_sequence (stmt);
16298 break;
16300 case HANDLER:
16302 tree decl = HANDLER_PARMS (t);
16304 if (decl)
16306 decl = tsubst (decl, args, complain, in_decl);
16307 /* Prevent instantiate_decl from trying to instantiate
16308 this variable. We've already done all that needs to be
16309 done. */
16310 if (decl != error_mark_node)
16311 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16313 stmt = begin_handler ();
16314 finish_handler_parms (decl, stmt);
16315 RECUR (HANDLER_BODY (t));
16316 finish_handler (stmt);
16318 break;
16320 case TAG_DEFN:
16321 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16322 if (CLASS_TYPE_P (tmp))
16324 /* Local classes are not independent templates; they are
16325 instantiated along with their containing function. And this
16326 way we don't have to deal with pushing out of one local class
16327 to instantiate a member of another local class. */
16328 /* Closures are handled by the LAMBDA_EXPR. */
16329 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16330 complete_type (tmp);
16331 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16332 if ((VAR_P (fld)
16333 || (TREE_CODE (fld) == FUNCTION_DECL
16334 && !DECL_ARTIFICIAL (fld)))
16335 && DECL_TEMPLATE_INSTANTIATION (fld))
16336 instantiate_decl (fld, /*defer_ok=*/false,
16337 /*expl_inst_class=*/false);
16339 break;
16341 case STATIC_ASSERT:
16343 tree condition;
16345 ++c_inhibit_evaluation_warnings;
16346 condition =
16347 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16348 args,
16349 complain, in_decl,
16350 /*integral_constant_expression_p=*/true);
16351 --c_inhibit_evaluation_warnings;
16353 finish_static_assert (condition,
16354 STATIC_ASSERT_MESSAGE (t),
16355 STATIC_ASSERT_SOURCE_LOCATION (t),
16356 /*member_p=*/false);
16358 break;
16360 case OACC_KERNELS:
16361 case OACC_PARALLEL:
16362 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16363 in_decl);
16364 stmt = begin_omp_parallel ();
16365 RECUR (OMP_BODY (t));
16366 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16367 break;
16369 case OMP_PARALLEL:
16370 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16371 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16372 complain, in_decl);
16373 if (OMP_PARALLEL_COMBINED (t))
16374 omp_parallel_combined_clauses = &tmp;
16375 stmt = begin_omp_parallel ();
16376 RECUR (OMP_PARALLEL_BODY (t));
16377 gcc_assert (omp_parallel_combined_clauses == NULL);
16378 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16379 = OMP_PARALLEL_COMBINED (t);
16380 pop_omp_privatization_clauses (r);
16381 break;
16383 case OMP_TASK:
16384 r = push_omp_privatization_clauses (false);
16385 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16386 complain, in_decl);
16387 stmt = begin_omp_task ();
16388 RECUR (OMP_TASK_BODY (t));
16389 finish_omp_task (tmp, stmt);
16390 pop_omp_privatization_clauses (r);
16391 break;
16393 case OMP_FOR:
16394 case OMP_SIMD:
16395 case CILK_SIMD:
16396 case CILK_FOR:
16397 case OMP_DISTRIBUTE:
16398 case OMP_TASKLOOP:
16399 case OACC_LOOP:
16401 tree clauses, body, pre_body;
16402 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16403 tree orig_declv = NULL_TREE;
16404 tree incrv = NULL_TREE;
16405 enum c_omp_region_type ort = C_ORT_OMP;
16406 int i;
16408 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16409 ort = C_ORT_CILK;
16410 else if (TREE_CODE (t) == OACC_LOOP)
16411 ort = C_ORT_ACC;
16413 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16414 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16415 in_decl);
16416 if (OMP_FOR_INIT (t) != NULL_TREE)
16418 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16419 if (OMP_FOR_ORIG_DECLS (t))
16420 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16421 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16422 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16423 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16426 stmt = begin_omp_structured_block ();
16428 pre_body = push_stmt_list ();
16429 RECUR (OMP_FOR_PRE_BODY (t));
16430 pre_body = pop_stmt_list (pre_body);
16432 if (OMP_FOR_INIT (t) != NULL_TREE)
16433 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16434 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16435 incrv, &clauses, args, complain, in_decl,
16436 integral_constant_expression_p);
16437 omp_parallel_combined_clauses = NULL;
16439 body = push_stmt_list ();
16440 RECUR (OMP_FOR_BODY (t));
16441 body = pop_stmt_list (body);
16443 if (OMP_FOR_INIT (t) != NULL_TREE)
16444 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16445 orig_declv, initv, condv, incrv, body, pre_body,
16446 NULL, clauses);
16447 else
16449 t = make_node (TREE_CODE (t));
16450 TREE_TYPE (t) = void_type_node;
16451 OMP_FOR_BODY (t) = body;
16452 OMP_FOR_PRE_BODY (t) = pre_body;
16453 OMP_FOR_CLAUSES (t) = clauses;
16454 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16455 add_stmt (t);
16458 add_stmt (finish_omp_structured_block (stmt));
16459 pop_omp_privatization_clauses (r);
16461 break;
16463 case OMP_SECTIONS:
16464 omp_parallel_combined_clauses = NULL;
16465 /* FALLTHRU */
16466 case OMP_SINGLE:
16467 case OMP_TEAMS:
16468 case OMP_CRITICAL:
16469 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16470 && OMP_TEAMS_COMBINED (t));
16471 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16472 in_decl);
16473 stmt = push_stmt_list ();
16474 RECUR (OMP_BODY (t));
16475 stmt = pop_stmt_list (stmt);
16477 t = copy_node (t);
16478 OMP_BODY (t) = stmt;
16479 OMP_CLAUSES (t) = tmp;
16480 add_stmt (t);
16481 pop_omp_privatization_clauses (r);
16482 break;
16484 case OACC_DATA:
16485 case OMP_TARGET_DATA:
16486 case OMP_TARGET:
16487 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16488 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16489 in_decl);
16490 keep_next_level (true);
16491 stmt = begin_omp_structured_block ();
16493 RECUR (OMP_BODY (t));
16494 stmt = finish_omp_structured_block (stmt);
16496 t = copy_node (t);
16497 OMP_BODY (t) = stmt;
16498 OMP_CLAUSES (t) = tmp;
16499 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16501 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16502 if (teams)
16504 /* For combined target teams, ensure the num_teams and
16505 thread_limit clause expressions are evaluated on the host,
16506 before entering the target construct. */
16507 tree c;
16508 for (c = OMP_TEAMS_CLAUSES (teams);
16509 c; c = OMP_CLAUSE_CHAIN (c))
16510 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16511 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16512 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16514 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16515 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16516 if (expr == error_mark_node)
16517 continue;
16518 tmp = TARGET_EXPR_SLOT (expr);
16519 add_stmt (expr);
16520 OMP_CLAUSE_OPERAND (c, 0) = expr;
16521 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16522 OMP_CLAUSE_FIRSTPRIVATE);
16523 OMP_CLAUSE_DECL (tc) = tmp;
16524 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16525 OMP_TARGET_CLAUSES (t) = tc;
16529 add_stmt (t);
16530 break;
16532 case OACC_DECLARE:
16533 t = copy_node (t);
16534 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16535 complain, in_decl);
16536 OACC_DECLARE_CLAUSES (t) = tmp;
16537 add_stmt (t);
16538 break;
16540 case OMP_TARGET_UPDATE:
16541 case OMP_TARGET_ENTER_DATA:
16542 case OMP_TARGET_EXIT_DATA:
16543 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16544 complain, in_decl);
16545 t = copy_node (t);
16546 OMP_STANDALONE_CLAUSES (t) = tmp;
16547 add_stmt (t);
16548 break;
16550 case OACC_ENTER_DATA:
16551 case OACC_EXIT_DATA:
16552 case OACC_UPDATE:
16553 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16554 complain, in_decl);
16555 t = copy_node (t);
16556 OMP_STANDALONE_CLAUSES (t) = tmp;
16557 add_stmt (t);
16558 break;
16560 case OMP_ORDERED:
16561 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16562 complain, in_decl);
16563 stmt = push_stmt_list ();
16564 RECUR (OMP_BODY (t));
16565 stmt = pop_stmt_list (stmt);
16567 t = copy_node (t);
16568 OMP_BODY (t) = stmt;
16569 OMP_ORDERED_CLAUSES (t) = tmp;
16570 add_stmt (t);
16571 break;
16573 case OMP_SECTION:
16574 case OMP_MASTER:
16575 case OMP_TASKGROUP:
16576 stmt = push_stmt_list ();
16577 RECUR (OMP_BODY (t));
16578 stmt = pop_stmt_list (stmt);
16580 t = copy_node (t);
16581 OMP_BODY (t) = stmt;
16582 add_stmt (t);
16583 break;
16585 case OMP_ATOMIC:
16586 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16587 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16589 tree op1 = TREE_OPERAND (t, 1);
16590 tree rhs1 = NULL_TREE;
16591 tree lhs, rhs;
16592 if (TREE_CODE (op1) == COMPOUND_EXPR)
16594 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16595 op1 = TREE_OPERAND (op1, 1);
16597 lhs = RECUR (TREE_OPERAND (op1, 0));
16598 rhs = RECUR (TREE_OPERAND (op1, 1));
16599 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16600 NULL_TREE, NULL_TREE, rhs1,
16601 OMP_ATOMIC_SEQ_CST (t));
16603 else
16605 tree op1 = TREE_OPERAND (t, 1);
16606 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16607 tree rhs1 = NULL_TREE;
16608 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16609 enum tree_code opcode = NOP_EXPR;
16610 if (code == OMP_ATOMIC_READ)
16612 v = RECUR (TREE_OPERAND (op1, 0));
16613 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16615 else if (code == OMP_ATOMIC_CAPTURE_OLD
16616 || code == OMP_ATOMIC_CAPTURE_NEW)
16618 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16619 v = RECUR (TREE_OPERAND (op1, 0));
16620 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16621 if (TREE_CODE (op11) == COMPOUND_EXPR)
16623 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16624 op11 = TREE_OPERAND (op11, 1);
16626 lhs = RECUR (TREE_OPERAND (op11, 0));
16627 rhs = RECUR (TREE_OPERAND (op11, 1));
16628 opcode = TREE_CODE (op11);
16629 if (opcode == MODIFY_EXPR)
16630 opcode = NOP_EXPR;
16632 else
16634 code = OMP_ATOMIC;
16635 lhs = RECUR (TREE_OPERAND (op1, 0));
16636 rhs = RECUR (TREE_OPERAND (op1, 1));
16638 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16639 OMP_ATOMIC_SEQ_CST (t));
16641 break;
16643 case TRANSACTION_EXPR:
16645 int flags = 0;
16646 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16647 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16649 if (TRANSACTION_EXPR_IS_STMT (t))
16651 tree body = TRANSACTION_EXPR_BODY (t);
16652 tree noex = NULL_TREE;
16653 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16655 noex = MUST_NOT_THROW_COND (body);
16656 if (noex == NULL_TREE)
16657 noex = boolean_true_node;
16658 body = TREE_OPERAND (body, 0);
16660 stmt = begin_transaction_stmt (input_location, NULL, flags);
16661 RECUR (body);
16662 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16664 else
16666 stmt = build_transaction_expr (EXPR_LOCATION (t),
16667 RECUR (TRANSACTION_EXPR_BODY (t)),
16668 flags, NULL_TREE);
16669 RETURN (stmt);
16672 break;
16674 case MUST_NOT_THROW_EXPR:
16676 tree op0 = RECUR (TREE_OPERAND (t, 0));
16677 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16678 RETURN (build_must_not_throw_expr (op0, cond));
16681 case EXPR_PACK_EXPANSION:
16682 error ("invalid use of pack expansion expression");
16683 RETURN (error_mark_node);
16685 case NONTYPE_ARGUMENT_PACK:
16686 error ("use %<...%> to expand argument pack");
16687 RETURN (error_mark_node);
16689 case CILK_SPAWN_STMT:
16690 cfun->calls_cilk_spawn = 1;
16691 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16693 case CILK_SYNC_STMT:
16694 RETURN (build_cilk_sync ());
16696 case COMPOUND_EXPR:
16697 tmp = RECUR (TREE_OPERAND (t, 0));
16698 if (tmp == NULL_TREE)
16699 /* If the first operand was a statement, we're done with it. */
16700 RETURN (RECUR (TREE_OPERAND (t, 1)));
16701 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16702 RECUR (TREE_OPERAND (t, 1)),
16703 complain));
16705 case ANNOTATE_EXPR:
16706 tmp = RECUR (TREE_OPERAND (t, 0));
16707 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16708 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16710 default:
16711 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16713 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16714 /*function_p=*/false,
16715 integral_constant_expression_p));
16718 RETURN (NULL_TREE);
16719 out:
16720 input_location = loc;
16721 return r;
16722 #undef RECUR
16723 #undef RETURN
16726 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16727 function. For description of the body see comment above
16728 cp_parser_omp_declare_reduction_exprs. */
16730 static void
16731 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16733 if (t == NULL_TREE || t == error_mark_node)
16734 return;
16736 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16738 tree_stmt_iterator tsi;
16739 int i;
16740 tree stmts[7];
16741 memset (stmts, 0, sizeof stmts);
16742 for (i = 0, tsi = tsi_start (t);
16743 i < 7 && !tsi_end_p (tsi);
16744 i++, tsi_next (&tsi))
16745 stmts[i] = tsi_stmt (tsi);
16746 gcc_assert (tsi_end_p (tsi));
16748 if (i >= 3)
16750 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16751 && TREE_CODE (stmts[1]) == DECL_EXPR);
16752 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16753 args, complain, in_decl);
16754 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16755 args, complain, in_decl);
16756 DECL_CONTEXT (omp_out) = current_function_decl;
16757 DECL_CONTEXT (omp_in) = current_function_decl;
16758 keep_next_level (true);
16759 tree block = begin_omp_structured_block ();
16760 tsubst_expr (stmts[2], args, complain, in_decl, false);
16761 block = finish_omp_structured_block (block);
16762 block = maybe_cleanup_point_expr_void (block);
16763 add_decl_expr (omp_out);
16764 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16765 TREE_NO_WARNING (omp_out) = 1;
16766 add_decl_expr (omp_in);
16767 finish_expr_stmt (block);
16769 if (i >= 6)
16771 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16772 && TREE_CODE (stmts[4]) == DECL_EXPR);
16773 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16774 args, complain, in_decl);
16775 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16776 args, complain, in_decl);
16777 DECL_CONTEXT (omp_priv) = current_function_decl;
16778 DECL_CONTEXT (omp_orig) = current_function_decl;
16779 keep_next_level (true);
16780 tree block = begin_omp_structured_block ();
16781 tsubst_expr (stmts[5], args, complain, in_decl, false);
16782 block = finish_omp_structured_block (block);
16783 block = maybe_cleanup_point_expr_void (block);
16784 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16785 add_decl_expr (omp_priv);
16786 add_decl_expr (omp_orig);
16787 finish_expr_stmt (block);
16788 if (i == 7)
16789 add_decl_expr (omp_orig);
16793 /* T is a postfix-expression that is not being used in a function
16794 call. Return the substituted version of T. */
16796 static tree
16797 tsubst_non_call_postfix_expression (tree t, tree args,
16798 tsubst_flags_t complain,
16799 tree in_decl)
16801 if (TREE_CODE (t) == SCOPE_REF)
16802 t = tsubst_qualified_id (t, args, complain, in_decl,
16803 /*done=*/false, /*address_p=*/false);
16804 else
16805 t = tsubst_copy_and_build (t, args, complain, in_decl,
16806 /*function_p=*/false,
16807 /*integral_constant_expression_p=*/false);
16809 return t;
16812 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16813 instantiation context. Instantiating a pack expansion containing a lambda
16814 might result in multiple lambdas all based on the same lambda in the
16815 template. */
16817 tree
16818 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16820 tree oldfn = lambda_function (t);
16821 in_decl = oldfn;
16823 tree r = build_lambda_expr ();
16825 LAMBDA_EXPR_LOCATION (r)
16826 = LAMBDA_EXPR_LOCATION (t);
16827 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16828 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16829 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16831 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16832 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16833 else
16834 record_lambda_scope (r);
16836 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16837 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16839 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16840 cap = TREE_CHAIN (cap))
16842 tree field = TREE_PURPOSE (cap);
16843 if (PACK_EXPANSION_P (field))
16844 field = PACK_EXPANSION_PATTERN (field);
16845 field = tsubst_decl (field, args, complain);
16847 if (field == error_mark_node)
16848 return error_mark_node;
16850 tree init = TREE_VALUE (cap);
16851 if (PACK_EXPANSION_P (init))
16852 init = tsubst_pack_expansion (init, args, complain, in_decl);
16853 else
16854 init = tsubst_copy_and_build (init, args, complain, in_decl,
16855 /*fn*/false, /*constexpr*/false);
16857 if (TREE_CODE (field) == TREE_VEC)
16859 int len = TREE_VEC_LENGTH (field);
16860 gcc_assert (TREE_CODE (init) == TREE_VEC
16861 && TREE_VEC_LENGTH (init) == len);
16862 for (int i = 0; i < len; ++i)
16863 LAMBDA_EXPR_CAPTURE_LIST (r)
16864 = tree_cons (TREE_VEC_ELT (field, i),
16865 TREE_VEC_ELT (init, i),
16866 LAMBDA_EXPR_CAPTURE_LIST (r));
16868 else
16870 LAMBDA_EXPR_CAPTURE_LIST (r)
16871 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16873 if (id_equal (DECL_NAME (field), "__this"))
16874 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16878 tree type = begin_lambda_type (r);
16880 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16881 determine_visibility (TYPE_NAME (type));
16883 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16885 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16886 ? DECL_TI_TEMPLATE (oldfn)
16887 : NULL_TREE);
16889 tree fntype = static_fn_type (oldfn);
16890 if (oldtmpl)
16891 ++processing_template_decl;
16892 fntype = tsubst (fntype, args, complain, in_decl);
16893 if (oldtmpl)
16894 --processing_template_decl;
16896 if (fntype == error_mark_node)
16897 r = error_mark_node;
16898 else
16900 /* Fix the type of 'this'. */
16901 fntype = build_memfn_type (fntype, type,
16902 type_memfn_quals (fntype),
16903 type_memfn_rqual (fntype));
16904 tree fn, tmpl;
16905 if (oldtmpl)
16907 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16908 fn = DECL_TEMPLATE_RESULT (tmpl);
16909 finish_member_declaration (tmpl);
16911 else
16913 tmpl = NULL_TREE;
16914 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16915 finish_member_declaration (fn);
16918 /* Let finish_function set this. */
16919 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16921 bool nested = cfun;
16922 if (nested)
16923 push_function_context ();
16925 local_specialization_stack s (lss_copy);
16927 tree body = start_lambda_function (fn, r);
16929 register_parameter_specializations (oldfn, fn);
16931 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16932 /*constexpr*/false);
16934 finish_lambda_function (body);
16936 if (nested)
16937 pop_function_context ();
16939 /* The capture list was built up in reverse order; fix that now. */
16940 LAMBDA_EXPR_CAPTURE_LIST (r)
16941 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16943 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16945 maybe_add_lambda_conv_op (type);
16948 finish_struct (type, /*attr*/NULL_TREE);
16950 insert_pending_capture_proxies ();
16952 return r;
16955 /* Like tsubst but deals with expressions and performs semantic
16956 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16958 tree
16959 tsubst_copy_and_build (tree t,
16960 tree args,
16961 tsubst_flags_t complain,
16962 tree in_decl,
16963 bool function_p,
16964 bool integral_constant_expression_p)
16966 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16967 #define RECUR(NODE) \
16968 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16969 /*function_p=*/false, \
16970 integral_constant_expression_p)
16972 tree retval, op1;
16973 location_t loc;
16975 if (t == NULL_TREE || t == error_mark_node)
16976 return t;
16978 loc = input_location;
16979 if (EXPR_HAS_LOCATION (t))
16980 input_location = EXPR_LOCATION (t);
16982 /* N3276 decltype magic only applies to calls at the top level or on the
16983 right side of a comma. */
16984 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16985 complain &= ~tf_decltype;
16987 switch (TREE_CODE (t))
16989 case USING_DECL:
16990 t = DECL_NAME (t);
16991 /* Fall through. */
16992 case IDENTIFIER_NODE:
16994 tree decl;
16995 cp_id_kind idk;
16996 bool non_integral_constant_expression_p;
16997 const char *error_msg;
16999 if (IDENTIFIER_CONV_OP_P (t))
17001 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17002 t = make_conv_op_name (new_type);
17005 /* Look up the name. */
17006 decl = lookup_name (t);
17008 /* By convention, expressions use ERROR_MARK_NODE to indicate
17009 failure, not NULL_TREE. */
17010 if (decl == NULL_TREE)
17011 decl = error_mark_node;
17013 decl = finish_id_expression (t, decl, NULL_TREE,
17014 &idk,
17015 integral_constant_expression_p,
17016 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17017 &non_integral_constant_expression_p,
17018 /*template_p=*/false,
17019 /*done=*/true,
17020 /*address_p=*/false,
17021 /*template_arg_p=*/false,
17022 &error_msg,
17023 input_location);
17024 if (error_msg)
17025 error (error_msg);
17026 if (!function_p && identifier_p (decl))
17028 if (complain & tf_error)
17029 unqualified_name_lookup_error (decl);
17030 decl = error_mark_node;
17032 RETURN (decl);
17035 case TEMPLATE_ID_EXPR:
17037 tree object;
17038 tree templ = RECUR (TREE_OPERAND (t, 0));
17039 tree targs = TREE_OPERAND (t, 1);
17041 if (targs)
17042 targs = tsubst_template_args (targs, args, complain, in_decl);
17043 if (targs == error_mark_node)
17044 return error_mark_node;
17046 if (TREE_CODE (templ) == SCOPE_REF)
17048 tree name = TREE_OPERAND (templ, 1);
17049 tree tid = lookup_template_function (name, targs);
17050 TREE_OPERAND (templ, 1) = tid;
17051 return templ;
17054 if (variable_template_p (templ))
17055 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17057 if (TREE_CODE (templ) == COMPONENT_REF)
17059 object = TREE_OPERAND (templ, 0);
17060 templ = TREE_OPERAND (templ, 1);
17062 else
17063 object = NULL_TREE;
17064 templ = lookup_template_function (templ, targs);
17066 if (object)
17067 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17068 object, templ, NULL_TREE));
17069 else
17070 RETURN (baselink_for_fns (templ));
17073 case INDIRECT_REF:
17075 tree r = RECUR (TREE_OPERAND (t, 0));
17077 if (REFERENCE_REF_P (t))
17079 /* A type conversion to reference type will be enclosed in
17080 such an indirect ref, but the substitution of the cast
17081 will have also added such an indirect ref. */
17082 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
17083 r = convert_from_reference (r);
17085 else
17086 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17087 complain|decltype_flag);
17089 if (TREE_CODE (r) == INDIRECT_REF)
17090 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17092 RETURN (r);
17095 case NOP_EXPR:
17097 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17098 tree op0 = RECUR (TREE_OPERAND (t, 0));
17099 RETURN (build_nop (type, op0));
17102 case IMPLICIT_CONV_EXPR:
17104 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17105 tree expr = RECUR (TREE_OPERAND (t, 0));
17106 int flags = LOOKUP_IMPLICIT;
17107 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17108 flags = LOOKUP_NORMAL;
17109 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17110 flags));
17113 case CONVERT_EXPR:
17115 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17116 tree op0 = RECUR (TREE_OPERAND (t, 0));
17117 RETURN (build1 (CONVERT_EXPR, type, op0));
17120 case CAST_EXPR:
17121 case REINTERPRET_CAST_EXPR:
17122 case CONST_CAST_EXPR:
17123 case DYNAMIC_CAST_EXPR:
17124 case STATIC_CAST_EXPR:
17126 tree type;
17127 tree op, r = NULL_TREE;
17129 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17130 if (integral_constant_expression_p
17131 && !cast_valid_in_integral_constant_expression_p (type))
17133 if (complain & tf_error)
17134 error ("a cast to a type other than an integral or "
17135 "enumeration type cannot appear in a constant-expression");
17136 RETURN (error_mark_node);
17139 op = RECUR (TREE_OPERAND (t, 0));
17141 warning_sentinel s(warn_useless_cast);
17142 switch (TREE_CODE (t))
17144 case CAST_EXPR:
17145 r = build_functional_cast (type, op, complain);
17146 break;
17147 case REINTERPRET_CAST_EXPR:
17148 r = build_reinterpret_cast (type, op, complain);
17149 break;
17150 case CONST_CAST_EXPR:
17151 r = build_const_cast (type, op, complain);
17152 break;
17153 case DYNAMIC_CAST_EXPR:
17154 r = build_dynamic_cast (type, op, complain);
17155 break;
17156 case STATIC_CAST_EXPR:
17157 r = build_static_cast (type, op, complain);
17158 break;
17159 default:
17160 gcc_unreachable ();
17163 RETURN (r);
17166 case POSTDECREMENT_EXPR:
17167 case POSTINCREMENT_EXPR:
17168 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17169 args, complain, in_decl);
17170 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17171 complain|decltype_flag));
17173 case PREDECREMENT_EXPR:
17174 case PREINCREMENT_EXPR:
17175 case NEGATE_EXPR:
17176 case BIT_NOT_EXPR:
17177 case ABS_EXPR:
17178 case TRUTH_NOT_EXPR:
17179 case UNARY_PLUS_EXPR: /* Unary + */
17180 case REALPART_EXPR:
17181 case IMAGPART_EXPR:
17182 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17183 RECUR (TREE_OPERAND (t, 0)),
17184 complain|decltype_flag));
17186 case FIX_TRUNC_EXPR:
17187 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17188 false, complain));
17190 case ADDR_EXPR:
17191 op1 = TREE_OPERAND (t, 0);
17192 if (TREE_CODE (op1) == LABEL_DECL)
17193 RETURN (finish_label_address_expr (DECL_NAME (op1),
17194 EXPR_LOCATION (op1)));
17195 if (TREE_CODE (op1) == SCOPE_REF)
17196 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17197 /*done=*/true, /*address_p=*/true);
17198 else
17199 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17200 in_decl);
17201 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17202 complain|decltype_flag));
17204 case PLUS_EXPR:
17205 case MINUS_EXPR:
17206 case MULT_EXPR:
17207 case TRUNC_DIV_EXPR:
17208 case CEIL_DIV_EXPR:
17209 case FLOOR_DIV_EXPR:
17210 case ROUND_DIV_EXPR:
17211 case EXACT_DIV_EXPR:
17212 case BIT_AND_EXPR:
17213 case BIT_IOR_EXPR:
17214 case BIT_XOR_EXPR:
17215 case TRUNC_MOD_EXPR:
17216 case FLOOR_MOD_EXPR:
17217 case TRUTH_ANDIF_EXPR:
17218 case TRUTH_ORIF_EXPR:
17219 case TRUTH_AND_EXPR:
17220 case TRUTH_OR_EXPR:
17221 case RSHIFT_EXPR:
17222 case LSHIFT_EXPR:
17223 case RROTATE_EXPR:
17224 case LROTATE_EXPR:
17225 case EQ_EXPR:
17226 case NE_EXPR:
17227 case MAX_EXPR:
17228 case MIN_EXPR:
17229 case LE_EXPR:
17230 case GE_EXPR:
17231 case LT_EXPR:
17232 case GT_EXPR:
17233 case MEMBER_REF:
17234 case DOTSTAR_EXPR:
17236 warning_sentinel s1(warn_type_limits);
17237 warning_sentinel s2(warn_div_by_zero);
17238 warning_sentinel s3(warn_logical_op);
17239 warning_sentinel s4(warn_tautological_compare);
17240 tree op0 = RECUR (TREE_OPERAND (t, 0));
17241 tree op1 = RECUR (TREE_OPERAND (t, 1));
17242 tree r = build_x_binary_op
17243 (input_location, TREE_CODE (t),
17244 op0,
17245 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17246 ? ERROR_MARK
17247 : TREE_CODE (TREE_OPERAND (t, 0))),
17248 op1,
17249 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17250 ? ERROR_MARK
17251 : TREE_CODE (TREE_OPERAND (t, 1))),
17252 /*overload=*/NULL,
17253 complain|decltype_flag);
17254 if (EXPR_P (r) && TREE_NO_WARNING (t))
17255 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17257 RETURN (r);
17260 case POINTER_PLUS_EXPR:
17262 tree op0 = RECUR (TREE_OPERAND (t, 0));
17263 tree op1 = RECUR (TREE_OPERAND (t, 1));
17264 return fold_build_pointer_plus (op0, op1);
17267 case SCOPE_REF:
17268 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17269 /*address_p=*/false));
17270 case ARRAY_REF:
17271 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17272 args, complain, in_decl);
17273 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17274 RECUR (TREE_OPERAND (t, 1)),
17275 complain|decltype_flag));
17277 case ARRAY_NOTATION_REF:
17279 tree start_index, length, stride;
17280 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17281 args, complain, in_decl);
17282 start_index = RECUR (ARRAY_NOTATION_START (t));
17283 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17284 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17285 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17286 length, stride, TREE_TYPE (op1)));
17288 case SIZEOF_EXPR:
17289 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17290 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17291 RETURN (tsubst_copy (t, args, complain, in_decl));
17292 /* Fall through */
17294 case ALIGNOF_EXPR:
17296 tree r;
17298 op1 = TREE_OPERAND (t, 0);
17299 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17300 op1 = TREE_TYPE (op1);
17301 if (!args)
17303 /* When there are no ARGS, we are trying to evaluate a
17304 non-dependent expression from the parser. Trying to do
17305 the substitutions may not work. */
17306 if (!TYPE_P (op1))
17307 op1 = TREE_TYPE (op1);
17309 else
17311 ++cp_unevaluated_operand;
17312 ++c_inhibit_evaluation_warnings;
17313 if (TYPE_P (op1))
17314 op1 = tsubst (op1, args, complain, in_decl);
17315 else
17316 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17317 /*function_p=*/false,
17318 /*integral_constant_expression_p=*/
17319 false);
17320 --cp_unevaluated_operand;
17321 --c_inhibit_evaluation_warnings;
17323 if (TYPE_P (op1))
17324 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17325 complain & tf_error);
17326 else
17327 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17328 complain & tf_error);
17329 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17331 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17333 if (!processing_template_decl && TYPE_P (op1))
17335 r = build_min (SIZEOF_EXPR, size_type_node,
17336 build1 (NOP_EXPR, op1, error_mark_node));
17337 SIZEOF_EXPR_TYPE_P (r) = 1;
17339 else
17340 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17341 TREE_SIDE_EFFECTS (r) = 0;
17342 TREE_READONLY (r) = 1;
17344 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17346 RETURN (r);
17349 case AT_ENCODE_EXPR:
17351 op1 = TREE_OPERAND (t, 0);
17352 ++cp_unevaluated_operand;
17353 ++c_inhibit_evaluation_warnings;
17354 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17355 /*function_p=*/false,
17356 /*integral_constant_expression_p=*/false);
17357 --cp_unevaluated_operand;
17358 --c_inhibit_evaluation_warnings;
17359 RETURN (objc_build_encode_expr (op1));
17362 case NOEXCEPT_EXPR:
17363 op1 = TREE_OPERAND (t, 0);
17364 ++cp_unevaluated_operand;
17365 ++c_inhibit_evaluation_warnings;
17366 ++cp_noexcept_operand;
17367 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17368 /*function_p=*/false,
17369 /*integral_constant_expression_p=*/false);
17370 --cp_unevaluated_operand;
17371 --c_inhibit_evaluation_warnings;
17372 --cp_noexcept_operand;
17373 RETURN (finish_noexcept_expr (op1, complain));
17375 case MODOP_EXPR:
17377 warning_sentinel s(warn_div_by_zero);
17378 tree lhs = RECUR (TREE_OPERAND (t, 0));
17379 tree rhs = RECUR (TREE_OPERAND (t, 2));
17380 tree r = build_x_modify_expr
17381 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17382 complain|decltype_flag);
17383 /* TREE_NO_WARNING must be set if either the expression was
17384 parenthesized or it uses an operator such as >>= rather
17385 than plain assignment. In the former case, it was already
17386 set and must be copied. In the latter case,
17387 build_x_modify_expr sets it and it must not be reset
17388 here. */
17389 if (TREE_NO_WARNING (t))
17390 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17392 RETURN (r);
17395 case ARROW_EXPR:
17396 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17397 args, complain, in_decl);
17398 /* Remember that there was a reference to this entity. */
17399 if (DECL_P (op1)
17400 && !mark_used (op1, complain) && !(complain & tf_error))
17401 RETURN (error_mark_node);
17402 RETURN (build_x_arrow (input_location, op1, complain));
17404 case NEW_EXPR:
17406 tree placement = RECUR (TREE_OPERAND (t, 0));
17407 tree init = RECUR (TREE_OPERAND (t, 3));
17408 vec<tree, va_gc> *placement_vec;
17409 vec<tree, va_gc> *init_vec;
17410 tree ret;
17412 if (placement == NULL_TREE)
17413 placement_vec = NULL;
17414 else
17416 placement_vec = make_tree_vector ();
17417 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17418 vec_safe_push (placement_vec, TREE_VALUE (placement));
17421 /* If there was an initializer in the original tree, but it
17422 instantiated to an empty list, then we should pass a
17423 non-NULL empty vector to tell build_new that it was an
17424 empty initializer() rather than no initializer. This can
17425 only happen when the initializer is a pack expansion whose
17426 parameter packs are of length zero. */
17427 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17428 init_vec = NULL;
17429 else
17431 init_vec = make_tree_vector ();
17432 if (init == void_node)
17433 gcc_assert (init_vec != NULL);
17434 else
17436 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17437 vec_safe_push (init_vec, TREE_VALUE (init));
17441 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17442 tree op2 = RECUR (TREE_OPERAND (t, 2));
17443 ret = build_new (&placement_vec, op1, op2, &init_vec,
17444 NEW_EXPR_USE_GLOBAL (t),
17445 complain);
17447 if (placement_vec != NULL)
17448 release_tree_vector (placement_vec);
17449 if (init_vec != NULL)
17450 release_tree_vector (init_vec);
17452 RETURN (ret);
17455 case DELETE_EXPR:
17457 tree op0 = RECUR (TREE_OPERAND (t, 0));
17458 tree op1 = RECUR (TREE_OPERAND (t, 1));
17459 RETURN (delete_sanity (op0, op1,
17460 DELETE_EXPR_USE_VEC (t),
17461 DELETE_EXPR_USE_GLOBAL (t),
17462 complain));
17465 case COMPOUND_EXPR:
17467 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17468 complain & ~tf_decltype, in_decl,
17469 /*function_p=*/false,
17470 integral_constant_expression_p);
17471 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17472 op0,
17473 RECUR (TREE_OPERAND (t, 1)),
17474 complain|decltype_flag));
17477 case CALL_EXPR:
17479 tree function;
17480 vec<tree, va_gc> *call_args;
17481 unsigned int nargs, i;
17482 bool qualified_p;
17483 bool koenig_p;
17484 tree ret;
17486 function = CALL_EXPR_FN (t);
17487 /* Internal function with no arguments. */
17488 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17489 RETURN (t);
17491 /* When we parsed the expression, we determined whether or
17492 not Koenig lookup should be performed. */
17493 koenig_p = KOENIG_LOOKUP_P (t);
17494 if (function == NULL_TREE)
17496 koenig_p = false;
17497 qualified_p = false;
17499 else if (TREE_CODE (function) == SCOPE_REF)
17501 qualified_p = true;
17502 function = tsubst_qualified_id (function, args, complain, in_decl,
17503 /*done=*/false,
17504 /*address_p=*/false);
17506 else if (koenig_p && identifier_p (function))
17508 /* Do nothing; calling tsubst_copy_and_build on an identifier
17509 would incorrectly perform unqualified lookup again.
17511 Note that we can also have an IDENTIFIER_NODE if the earlier
17512 unqualified lookup found a member function; in that case
17513 koenig_p will be false and we do want to do the lookup
17514 again to find the instantiated member function.
17516 FIXME but doing that causes c++/15272, so we need to stop
17517 using IDENTIFIER_NODE in that situation. */
17518 qualified_p = false;
17520 else
17522 if (TREE_CODE (function) == COMPONENT_REF)
17524 tree op = TREE_OPERAND (function, 1);
17526 qualified_p = (TREE_CODE (op) == SCOPE_REF
17527 || (BASELINK_P (op)
17528 && BASELINK_QUALIFIED_P (op)));
17530 else
17531 qualified_p = false;
17533 if (TREE_CODE (function) == ADDR_EXPR
17534 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17535 /* Avoid error about taking the address of a constructor. */
17536 function = TREE_OPERAND (function, 0);
17538 function = tsubst_copy_and_build (function, args, complain,
17539 in_decl,
17540 !qualified_p,
17541 integral_constant_expression_p);
17543 if (BASELINK_P (function))
17544 qualified_p = true;
17547 nargs = call_expr_nargs (t);
17548 call_args = make_tree_vector ();
17549 for (i = 0; i < nargs; ++i)
17551 tree arg = CALL_EXPR_ARG (t, i);
17553 if (!PACK_EXPANSION_P (arg))
17554 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17555 else
17557 /* Expand the pack expansion and push each entry onto
17558 CALL_ARGS. */
17559 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17560 if (TREE_CODE (arg) == TREE_VEC)
17562 unsigned int len, j;
17564 len = TREE_VEC_LENGTH (arg);
17565 for (j = 0; j < len; ++j)
17567 tree value = TREE_VEC_ELT (arg, j);
17568 if (value != NULL_TREE)
17569 value = convert_from_reference (value);
17570 vec_safe_push (call_args, value);
17573 else
17575 /* A partial substitution. Add one entry. */
17576 vec_safe_push (call_args, arg);
17581 /* We do not perform argument-dependent lookup if normal
17582 lookup finds a non-function, in accordance with the
17583 expected resolution of DR 218. */
17584 if (koenig_p
17585 && ((is_overloaded_fn (function)
17586 /* If lookup found a member function, the Koenig lookup is
17587 not appropriate, even if an unqualified-name was used
17588 to denote the function. */
17589 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17590 || identifier_p (function))
17591 /* Only do this when substitution turns a dependent call
17592 into a non-dependent call. */
17593 && type_dependent_expression_p_push (t)
17594 && !any_type_dependent_arguments_p (call_args))
17595 function = perform_koenig_lookup (function, call_args, tf_none);
17597 if (function != NULL_TREE
17598 && identifier_p (function)
17599 && !any_type_dependent_arguments_p (call_args))
17601 if (koenig_p && (complain & tf_warning_or_error))
17603 /* For backwards compatibility and good diagnostics, try
17604 the unqualified lookup again if we aren't in SFINAE
17605 context. */
17606 tree unq = (tsubst_copy_and_build
17607 (function, args, complain, in_decl, true,
17608 integral_constant_expression_p));
17609 if (unq == error_mark_node)
17611 release_tree_vector (call_args);
17612 RETURN (error_mark_node);
17615 if (unq != function)
17617 /* In a lambda fn, we have to be careful to not
17618 introduce new this captures. Legacy code can't
17619 be using lambdas anyway, so it's ok to be
17620 stricter. */
17621 bool in_lambda = (current_class_type
17622 && LAMBDA_TYPE_P (current_class_type));
17623 char const *const msg
17624 = G_("%qD was not declared in this scope, "
17625 "and no declarations were found by "
17626 "argument-dependent lookup at the point "
17627 "of instantiation");
17629 bool diag = true;
17630 if (in_lambda)
17631 error_at (EXPR_LOC_OR_LOC (t, input_location),
17632 msg, function);
17633 else
17634 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17635 msg, function);
17636 if (diag)
17638 tree fn = unq;
17640 if (INDIRECT_REF_P (fn))
17641 fn = TREE_OPERAND (fn, 0);
17642 if (is_overloaded_fn (fn))
17643 fn = get_first_fn (fn);
17645 if (!DECL_P (fn))
17646 /* Can't say anything more. */;
17647 else if (DECL_CLASS_SCOPE_P (fn))
17649 location_t loc = EXPR_LOC_OR_LOC (t,
17650 input_location);
17651 inform (loc,
17652 "declarations in dependent base %qT are "
17653 "not found by unqualified lookup",
17654 DECL_CLASS_CONTEXT (fn));
17655 if (current_class_ptr)
17656 inform (loc,
17657 "use %<this->%D%> instead", function);
17658 else
17659 inform (loc,
17660 "use %<%T::%D%> instead",
17661 current_class_name, function);
17663 else
17664 inform (DECL_SOURCE_LOCATION (fn),
17665 "%qD declared here, later in the "
17666 "translation unit", fn);
17667 if (in_lambda)
17669 release_tree_vector (call_args);
17670 RETURN (error_mark_node);
17674 function = unq;
17677 if (identifier_p (function))
17679 if (complain & tf_error)
17680 unqualified_name_lookup_error (function);
17681 release_tree_vector (call_args);
17682 RETURN (error_mark_node);
17686 /* Remember that there was a reference to this entity. */
17687 if (function != NULL_TREE
17688 && DECL_P (function)
17689 && !mark_used (function, complain) && !(complain & tf_error))
17691 release_tree_vector (call_args);
17692 RETURN (error_mark_node);
17695 /* Put back tf_decltype for the actual call. */
17696 complain |= decltype_flag;
17698 if (function == NULL_TREE)
17699 switch (CALL_EXPR_IFN (t))
17701 case IFN_LAUNDER:
17702 gcc_assert (nargs == 1);
17703 if (vec_safe_length (call_args) != 1)
17705 error_at (EXPR_LOC_OR_LOC (t, input_location),
17706 "wrong number of arguments to "
17707 "%<__builtin_launder%>");
17708 ret = error_mark_node;
17710 else
17711 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17712 input_location),
17713 (*call_args)[0], complain);
17714 break;
17716 default:
17717 /* Unsupported internal function with arguments. */
17718 gcc_unreachable ();
17720 else if (TREE_CODE (function) == OFFSET_REF)
17721 ret = build_offset_ref_call_from_tree (function, &call_args,
17722 complain);
17723 else if (TREE_CODE (function) == COMPONENT_REF)
17725 tree instance = TREE_OPERAND (function, 0);
17726 tree fn = TREE_OPERAND (function, 1);
17728 if (processing_template_decl
17729 && (type_dependent_expression_p (instance)
17730 || (!BASELINK_P (fn)
17731 && TREE_CODE (fn) != FIELD_DECL)
17732 || type_dependent_expression_p (fn)
17733 || any_type_dependent_arguments_p (call_args)))
17734 ret = build_min_nt_call_vec (function, call_args);
17735 else if (!BASELINK_P (fn))
17736 ret = finish_call_expr (function, &call_args,
17737 /*disallow_virtual=*/false,
17738 /*koenig_p=*/false,
17739 complain);
17740 else
17741 ret = (build_new_method_call
17742 (instance, fn,
17743 &call_args, NULL_TREE,
17744 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17745 /*fn_p=*/NULL,
17746 complain));
17748 else
17749 ret = finish_call_expr (function, &call_args,
17750 /*disallow_virtual=*/qualified_p,
17751 koenig_p,
17752 complain);
17754 release_tree_vector (call_args);
17756 if (ret != error_mark_node)
17758 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17759 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17760 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17761 bool thk = CALL_FROM_THUNK_P (t);
17762 if (op || ord || rev || thk)
17764 function = extract_call_expr (ret);
17765 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17766 CALL_EXPR_ORDERED_ARGS (function) = ord;
17767 CALL_EXPR_REVERSE_ARGS (function) = rev;
17768 if (thk)
17770 CALL_FROM_THUNK_P (function) = true;
17771 /* The thunk location is not interesting. */
17772 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17777 RETURN (ret);
17780 case COND_EXPR:
17782 tree cond = RECUR (TREE_OPERAND (t, 0));
17783 tree folded_cond = fold_non_dependent_expr (cond);
17784 tree exp1, exp2;
17786 if (TREE_CODE (folded_cond) == INTEGER_CST)
17788 if (integer_zerop (folded_cond))
17790 ++c_inhibit_evaluation_warnings;
17791 exp1 = RECUR (TREE_OPERAND (t, 1));
17792 --c_inhibit_evaluation_warnings;
17793 exp2 = RECUR (TREE_OPERAND (t, 2));
17795 else
17797 exp1 = RECUR (TREE_OPERAND (t, 1));
17798 ++c_inhibit_evaluation_warnings;
17799 exp2 = RECUR (TREE_OPERAND (t, 2));
17800 --c_inhibit_evaluation_warnings;
17802 cond = folded_cond;
17804 else
17806 exp1 = RECUR (TREE_OPERAND (t, 1));
17807 exp2 = RECUR (TREE_OPERAND (t, 2));
17810 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17811 cond, exp1, exp2, complain));
17814 case PSEUDO_DTOR_EXPR:
17816 tree op0 = RECUR (TREE_OPERAND (t, 0));
17817 tree op1 = RECUR (TREE_OPERAND (t, 1));
17818 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17819 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17820 input_location));
17823 case TREE_LIST:
17825 tree purpose, value, chain;
17827 if (t == void_list_node)
17828 RETURN (t);
17830 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17831 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17833 /* We have pack expansions, so expand those and
17834 create a new list out of it. */
17835 tree purposevec = NULL_TREE;
17836 tree valuevec = NULL_TREE;
17837 tree chain;
17838 int i, len = -1;
17840 /* Expand the argument expressions. */
17841 if (TREE_PURPOSE (t))
17842 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17843 complain, in_decl);
17844 if (TREE_VALUE (t))
17845 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17846 complain, in_decl);
17848 /* Build the rest of the list. */
17849 chain = TREE_CHAIN (t);
17850 if (chain && chain != void_type_node)
17851 chain = RECUR (chain);
17853 /* Determine the number of arguments. */
17854 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17856 len = TREE_VEC_LENGTH (purposevec);
17857 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17859 else if (TREE_CODE (valuevec) == TREE_VEC)
17860 len = TREE_VEC_LENGTH (valuevec);
17861 else
17863 /* Since we only performed a partial substitution into
17864 the argument pack, we only RETURN (a single list
17865 node. */
17866 if (purposevec == TREE_PURPOSE (t)
17867 && valuevec == TREE_VALUE (t)
17868 && chain == TREE_CHAIN (t))
17869 RETURN (t);
17871 RETURN (tree_cons (purposevec, valuevec, chain));
17874 /* Convert the argument vectors into a TREE_LIST */
17875 i = len;
17876 while (i > 0)
17878 /* Grab the Ith values. */
17879 i--;
17880 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17881 : NULL_TREE;
17882 value
17883 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17884 : NULL_TREE;
17886 /* Build the list (backwards). */
17887 chain = tree_cons (purpose, value, chain);
17890 RETURN (chain);
17893 purpose = TREE_PURPOSE (t);
17894 if (purpose)
17895 purpose = RECUR (purpose);
17896 value = TREE_VALUE (t);
17897 if (value)
17898 value = RECUR (value);
17899 chain = TREE_CHAIN (t);
17900 if (chain && chain != void_type_node)
17901 chain = RECUR (chain);
17902 if (purpose == TREE_PURPOSE (t)
17903 && value == TREE_VALUE (t)
17904 && chain == TREE_CHAIN (t))
17905 RETURN (t);
17906 RETURN (tree_cons (purpose, value, chain));
17909 case COMPONENT_REF:
17911 tree object;
17912 tree object_type;
17913 tree member;
17914 tree r;
17916 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17917 args, complain, in_decl);
17918 /* Remember that there was a reference to this entity. */
17919 if (DECL_P (object)
17920 && !mark_used (object, complain) && !(complain & tf_error))
17921 RETURN (error_mark_node);
17922 object_type = TREE_TYPE (object);
17924 member = TREE_OPERAND (t, 1);
17925 if (BASELINK_P (member))
17926 member = tsubst_baselink (member,
17927 non_reference (TREE_TYPE (object)),
17928 args, complain, in_decl);
17929 else
17930 member = tsubst_copy (member, args, complain, in_decl);
17931 if (member == error_mark_node)
17932 RETURN (error_mark_node);
17934 if (TREE_CODE (member) == FIELD_DECL)
17936 r = finish_non_static_data_member (member, object, NULL_TREE);
17937 if (TREE_CODE (r) == COMPONENT_REF)
17938 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17939 RETURN (r);
17941 else if (type_dependent_expression_p (object))
17942 /* We can't do much here. */;
17943 else if (!CLASS_TYPE_P (object_type))
17945 if (scalarish_type_p (object_type))
17947 tree s = NULL_TREE;
17948 tree dtor = member;
17950 if (TREE_CODE (dtor) == SCOPE_REF)
17952 s = TREE_OPERAND (dtor, 0);
17953 dtor = TREE_OPERAND (dtor, 1);
17955 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17957 dtor = TREE_OPERAND (dtor, 0);
17958 if (TYPE_P (dtor))
17959 RETURN (finish_pseudo_destructor_expr
17960 (object, s, dtor, input_location));
17964 else if (TREE_CODE (member) == SCOPE_REF
17965 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17967 /* Lookup the template functions now that we know what the
17968 scope is. */
17969 tree scope = TREE_OPERAND (member, 0);
17970 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17971 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17972 member = lookup_qualified_name (scope, tmpl,
17973 /*is_type_p=*/false,
17974 /*complain=*/false);
17975 if (BASELINK_P (member))
17977 BASELINK_FUNCTIONS (member)
17978 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17979 args);
17980 member = (adjust_result_of_qualified_name_lookup
17981 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17982 object_type));
17984 else
17986 qualified_name_lookup_error (scope, tmpl, member,
17987 input_location);
17988 RETURN (error_mark_node);
17991 else if (TREE_CODE (member) == SCOPE_REF
17992 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17993 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17995 if (complain & tf_error)
17997 if (TYPE_P (TREE_OPERAND (member, 0)))
17998 error ("%qT is not a class or namespace",
17999 TREE_OPERAND (member, 0));
18000 else
18001 error ("%qD is not a class or namespace",
18002 TREE_OPERAND (member, 0));
18004 RETURN (error_mark_node);
18007 r = finish_class_member_access_expr (object, member,
18008 /*template_p=*/false,
18009 complain);
18010 if (TREE_CODE (r) == COMPONENT_REF)
18011 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18012 RETURN (r);
18015 case THROW_EXPR:
18016 RETURN (build_throw
18017 (RECUR (TREE_OPERAND (t, 0))));
18019 case CONSTRUCTOR:
18021 vec<constructor_elt, va_gc> *n;
18022 constructor_elt *ce;
18023 unsigned HOST_WIDE_INT idx;
18024 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18025 bool process_index_p;
18026 int newlen;
18027 bool need_copy_p = false;
18028 tree r;
18030 if (type == error_mark_node)
18031 RETURN (error_mark_node);
18033 /* digest_init will do the wrong thing if we let it. */
18034 if (type && TYPE_PTRMEMFUNC_P (type))
18035 RETURN (t);
18037 /* We do not want to process the index of aggregate
18038 initializers as they are identifier nodes which will be
18039 looked up by digest_init. */
18040 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18042 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18043 newlen = vec_safe_length (n);
18044 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18046 if (ce->index && process_index_p
18047 /* An identifier index is looked up in the type
18048 being initialized, not the current scope. */
18049 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18050 ce->index = RECUR (ce->index);
18052 if (PACK_EXPANSION_P (ce->value))
18054 /* Substitute into the pack expansion. */
18055 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18056 in_decl);
18058 if (ce->value == error_mark_node
18059 || PACK_EXPANSION_P (ce->value))
18061 else if (TREE_VEC_LENGTH (ce->value) == 1)
18062 /* Just move the argument into place. */
18063 ce->value = TREE_VEC_ELT (ce->value, 0);
18064 else
18066 /* Update the length of the final CONSTRUCTOR
18067 arguments vector, and note that we will need to
18068 copy.*/
18069 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18070 need_copy_p = true;
18073 else
18074 ce->value = RECUR (ce->value);
18077 if (need_copy_p)
18079 vec<constructor_elt, va_gc> *old_n = n;
18081 vec_alloc (n, newlen);
18082 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18084 if (TREE_CODE (ce->value) == TREE_VEC)
18086 int i, len = TREE_VEC_LENGTH (ce->value);
18087 for (i = 0; i < len; ++i)
18088 CONSTRUCTOR_APPEND_ELT (n, 0,
18089 TREE_VEC_ELT (ce->value, i));
18091 else
18092 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18096 r = build_constructor (init_list_type_node, n);
18097 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18099 if (TREE_HAS_CONSTRUCTOR (t))
18101 fcl_t cl = fcl_functional;
18102 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18103 cl = fcl_c99;
18104 RETURN (finish_compound_literal (type, r, complain, cl));
18107 TREE_TYPE (r) = type;
18108 RETURN (r);
18111 case TYPEID_EXPR:
18113 tree operand_0 = TREE_OPERAND (t, 0);
18114 if (TYPE_P (operand_0))
18116 operand_0 = tsubst (operand_0, args, complain, in_decl);
18117 RETURN (get_typeid (operand_0, complain));
18119 else
18121 operand_0 = RECUR (operand_0);
18122 RETURN (build_typeid (operand_0, complain));
18126 case VAR_DECL:
18127 if (!args)
18128 RETURN (t);
18129 else if (DECL_PACK_P (t))
18131 /* We don't build decls for an instantiation of a
18132 variadic capture proxy, we instantiate the elements
18133 when needed. */
18134 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18135 return RECUR (DECL_VALUE_EXPR (t));
18137 /* Fall through */
18139 case PARM_DECL:
18141 tree r = tsubst_copy (t, args, complain, in_decl);
18142 /* ??? We're doing a subset of finish_id_expression here. */
18143 if (VAR_P (r)
18144 && !processing_template_decl
18145 && !cp_unevaluated_operand
18146 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18147 && CP_DECL_THREAD_LOCAL_P (r))
18149 if (tree wrap = get_tls_wrapper_fn (r))
18150 /* Replace an evaluated use of the thread_local variable with
18151 a call to its wrapper. */
18152 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18154 else if (outer_automatic_var_p (r))
18155 r = process_outer_var_ref (r, complain);
18157 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18158 /* If the original type was a reference, we'll be wrapped in
18159 the appropriate INDIRECT_REF. */
18160 r = convert_from_reference (r);
18161 RETURN (r);
18164 case VA_ARG_EXPR:
18166 tree op0 = RECUR (TREE_OPERAND (t, 0));
18167 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18168 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18171 case OFFSETOF_EXPR:
18173 tree object_ptr
18174 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18175 in_decl, /*function_p=*/false,
18176 /*integral_constant_expression_p=*/false);
18177 RETURN (finish_offsetof (object_ptr,
18178 RECUR (TREE_OPERAND (t, 0)),
18179 EXPR_LOCATION (t)));
18182 case ADDRESSOF_EXPR:
18183 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18184 RECUR (TREE_OPERAND (t, 0)), complain));
18186 case TRAIT_EXPR:
18188 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18189 complain, in_decl);
18191 tree type2 = TRAIT_EXPR_TYPE2 (t);
18192 if (type2 && TREE_CODE (type2) == TREE_LIST)
18193 type2 = RECUR (type2);
18194 else if (type2)
18195 type2 = tsubst (type2, args, complain, in_decl);
18197 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18200 case STMT_EXPR:
18202 tree old_stmt_expr = cur_stmt_expr;
18203 tree stmt_expr = begin_stmt_expr ();
18205 cur_stmt_expr = stmt_expr;
18206 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18207 integral_constant_expression_p);
18208 stmt_expr = finish_stmt_expr (stmt_expr, false);
18209 cur_stmt_expr = old_stmt_expr;
18211 /* If the resulting list of expression statement is empty,
18212 fold it further into void_node. */
18213 if (empty_expr_stmt_p (stmt_expr))
18214 stmt_expr = void_node;
18216 RETURN (stmt_expr);
18219 case LAMBDA_EXPR:
18221 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18223 RETURN (build_lambda_object (r));
18226 case TARGET_EXPR:
18227 /* We can get here for a constant initializer of non-dependent type.
18228 FIXME stop folding in cp_parser_initializer_clause. */
18230 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18231 complain);
18232 RETURN (r);
18235 case TRANSACTION_EXPR:
18236 RETURN (tsubst_expr(t, args, complain, in_decl,
18237 integral_constant_expression_p));
18239 case PAREN_EXPR:
18240 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18242 case VEC_PERM_EXPR:
18244 tree op0 = RECUR (TREE_OPERAND (t, 0));
18245 tree op1 = RECUR (TREE_OPERAND (t, 1));
18246 tree op2 = RECUR (TREE_OPERAND (t, 2));
18247 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18248 complain));
18251 case REQUIRES_EXPR:
18252 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18254 default:
18255 /* Handle Objective-C++ constructs, if appropriate. */
18257 tree subst
18258 = objcp_tsubst_copy_and_build (t, args, complain,
18259 in_decl, /*function_p=*/false);
18260 if (subst)
18261 RETURN (subst);
18263 RETURN (tsubst_copy (t, args, complain, in_decl));
18266 #undef RECUR
18267 #undef RETURN
18268 out:
18269 input_location = loc;
18270 return retval;
18273 /* Verify that the instantiated ARGS are valid. For type arguments,
18274 make sure that the type's linkage is ok. For non-type arguments,
18275 make sure they are constants if they are integral or enumerations.
18276 Emit an error under control of COMPLAIN, and return TRUE on error. */
18278 static bool
18279 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18281 if (dependent_template_arg_p (t))
18282 return false;
18283 if (ARGUMENT_PACK_P (t))
18285 tree vec = ARGUMENT_PACK_ARGS (t);
18286 int len = TREE_VEC_LENGTH (vec);
18287 bool result = false;
18288 int i;
18290 for (i = 0; i < len; ++i)
18291 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18292 result = true;
18293 return result;
18295 else if (TYPE_P (t))
18297 /* [basic.link]: A name with no linkage (notably, the name
18298 of a class or enumeration declared in a local scope)
18299 shall not be used to declare an entity with linkage.
18300 This implies that names with no linkage cannot be used as
18301 template arguments
18303 DR 757 relaxes this restriction for C++0x. */
18304 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18305 : no_linkage_check (t, /*relaxed_p=*/false));
18307 if (nt)
18309 /* DR 488 makes use of a type with no linkage cause
18310 type deduction to fail. */
18311 if (complain & tf_error)
18313 if (TYPE_UNNAMED_P (nt))
18314 error ("%qT is/uses unnamed type", t);
18315 else
18316 error ("template argument for %qD uses local type %qT",
18317 tmpl, t);
18319 return true;
18321 /* In order to avoid all sorts of complications, we do not
18322 allow variably-modified types as template arguments. */
18323 else if (variably_modified_type_p (t, NULL_TREE))
18325 if (complain & tf_error)
18326 error ("%qT is a variably modified type", t);
18327 return true;
18330 /* Class template and alias template arguments should be OK. */
18331 else if (DECL_TYPE_TEMPLATE_P (t))
18333 /* A non-type argument of integral or enumerated type must be a
18334 constant. */
18335 else if (TREE_TYPE (t)
18336 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18337 && !REFERENCE_REF_P (t)
18338 && !TREE_CONSTANT (t))
18340 if (complain & tf_error)
18341 error ("integral expression %qE is not constant", t);
18342 return true;
18344 return false;
18347 static bool
18348 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18350 int ix, len = DECL_NTPARMS (tmpl);
18351 bool result = false;
18353 for (ix = 0; ix != len; ix++)
18355 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18356 result = true;
18358 if (result && (complain & tf_error))
18359 error (" trying to instantiate %qD", tmpl);
18360 return result;
18363 /* We're out of SFINAE context now, so generate diagnostics for the access
18364 errors we saw earlier when instantiating D from TMPL and ARGS. */
18366 static void
18367 recheck_decl_substitution (tree d, tree tmpl, tree args)
18369 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18370 tree type = TREE_TYPE (pattern);
18371 location_t loc = input_location;
18373 push_access_scope (d);
18374 push_deferring_access_checks (dk_no_deferred);
18375 input_location = DECL_SOURCE_LOCATION (pattern);
18376 tsubst (type, args, tf_warning_or_error, d);
18377 input_location = loc;
18378 pop_deferring_access_checks ();
18379 pop_access_scope (d);
18382 /* Instantiate the indicated variable, function, or alias template TMPL with
18383 the template arguments in TARG_PTR. */
18385 static tree
18386 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18388 tree targ_ptr = orig_args;
18389 tree fndecl;
18390 tree gen_tmpl;
18391 tree spec;
18392 bool access_ok = true;
18394 if (tmpl == error_mark_node)
18395 return error_mark_node;
18397 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18399 /* If this function is a clone, handle it specially. */
18400 if (DECL_CLONED_FUNCTION_P (tmpl))
18402 tree spec;
18403 tree clone;
18405 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18406 DECL_CLONED_FUNCTION. */
18407 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18408 targ_ptr, complain);
18409 if (spec == error_mark_node)
18410 return error_mark_node;
18412 /* Look for the clone. */
18413 FOR_EACH_CLONE (clone, spec)
18414 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18415 return clone;
18416 /* We should always have found the clone by now. */
18417 gcc_unreachable ();
18418 return NULL_TREE;
18421 if (targ_ptr == error_mark_node)
18422 return error_mark_node;
18424 /* Check to see if we already have this specialization. */
18425 gen_tmpl = most_general_template (tmpl);
18426 if (TMPL_ARGS_DEPTH (targ_ptr)
18427 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18428 /* targ_ptr only has the innermost template args, so add the outer ones
18429 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18430 the case of a non-dependent call within a template definition). */
18431 targ_ptr = (add_outermost_template_args
18432 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18433 targ_ptr));
18435 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18436 but it doesn't seem to be on the hot path. */
18437 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18439 gcc_assert (tmpl == gen_tmpl
18440 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18441 == spec)
18442 || fndecl == NULL_TREE);
18444 if (spec != NULL_TREE)
18446 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18448 if (complain & tf_error)
18449 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18450 return error_mark_node;
18452 return spec;
18455 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18456 complain))
18457 return error_mark_node;
18459 /* We are building a FUNCTION_DECL, during which the access of its
18460 parameters and return types have to be checked. However this
18461 FUNCTION_DECL which is the desired context for access checking
18462 is not built yet. We solve this chicken-and-egg problem by
18463 deferring all checks until we have the FUNCTION_DECL. */
18464 push_deferring_access_checks (dk_deferred);
18466 /* Instantiation of the function happens in the context of the function
18467 template, not the context of the overload resolution we're doing. */
18468 push_to_top_level ();
18469 /* If there are dependent arguments, e.g. because we're doing partial
18470 ordering, make sure processing_template_decl stays set. */
18471 if (uses_template_parms (targ_ptr))
18472 ++processing_template_decl;
18473 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18475 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18476 complain, gen_tmpl, true);
18477 push_nested_class (ctx);
18480 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18482 fndecl = NULL_TREE;
18483 if (VAR_P (pattern))
18485 /* We need to determine if we're using a partial or explicit
18486 specialization now, because the type of the variable could be
18487 different. */
18488 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18489 tree elt = most_specialized_partial_spec (tid, complain);
18490 if (elt == error_mark_node)
18491 pattern = error_mark_node;
18492 else if (elt)
18494 tree partial_tmpl = TREE_VALUE (elt);
18495 tree partial_args = TREE_PURPOSE (elt);
18496 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18497 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18501 /* Substitute template parameters to obtain the specialization. */
18502 if (fndecl == NULL_TREE)
18503 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18504 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18505 pop_nested_class ();
18506 pop_from_top_level ();
18508 if (fndecl == error_mark_node)
18510 pop_deferring_access_checks ();
18511 return error_mark_node;
18514 /* The DECL_TI_TEMPLATE should always be the immediate parent
18515 template, not the most general template. */
18516 DECL_TI_TEMPLATE (fndecl) = tmpl;
18517 DECL_TI_ARGS (fndecl) = targ_ptr;
18519 /* Now we know the specialization, compute access previously
18520 deferred. Do no access control for inheriting constructors,
18521 as we already checked access for the inherited constructor. */
18522 if (!(flag_new_inheriting_ctors
18523 && DECL_INHERITED_CTOR (fndecl)))
18525 push_access_scope (fndecl);
18526 if (!perform_deferred_access_checks (complain))
18527 access_ok = false;
18528 pop_access_scope (fndecl);
18530 pop_deferring_access_checks ();
18532 /* If we've just instantiated the main entry point for a function,
18533 instantiate all the alternate entry points as well. We do this
18534 by cloning the instantiation of the main entry point, not by
18535 instantiating the template clones. */
18536 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18537 clone_function_decl (fndecl, /*update_methods=*/false);
18539 if (!access_ok)
18541 if (!(complain & tf_error))
18543 /* Remember to reinstantiate when we're out of SFINAE so the user
18544 can see the errors. */
18545 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18547 return error_mark_node;
18549 return fndecl;
18552 /* Wrapper for instantiate_template_1. */
18554 tree
18555 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18557 tree ret;
18558 timevar_push (TV_TEMPLATE_INST);
18559 ret = instantiate_template_1 (tmpl, orig_args, complain);
18560 timevar_pop (TV_TEMPLATE_INST);
18561 return ret;
18564 /* Instantiate the alias template TMPL with ARGS. Also push a template
18565 instantiation level, which instantiate_template doesn't do because
18566 functions and variables have sufficient context established by the
18567 callers. */
18569 static tree
18570 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18572 struct pending_template *old_last_pend = last_pending_template;
18573 struct tinst_level *old_error_tinst = last_error_tinst_level;
18574 if (tmpl == error_mark_node || args == error_mark_node)
18575 return error_mark_node;
18576 tree tinst = build_tree_list (tmpl, args);
18577 if (!push_tinst_level (tinst))
18579 ggc_free (tinst);
18580 return error_mark_node;
18583 args =
18584 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18585 args, tmpl, complain,
18586 /*require_all_args=*/true,
18587 /*use_default_args=*/true);
18589 tree r = instantiate_template (tmpl, args, complain);
18590 pop_tinst_level ();
18591 /* We can't free this if a pending_template entry or last_error_tinst_level
18592 is pointing at it. */
18593 if (last_pending_template == old_last_pend
18594 && last_error_tinst_level == old_error_tinst)
18595 ggc_free (tinst);
18597 return r;
18600 /* PARM is a template parameter pack for FN. Returns true iff
18601 PARM is used in a deducible way in the argument list of FN. */
18603 static bool
18604 pack_deducible_p (tree parm, tree fn)
18606 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18607 for (; t; t = TREE_CHAIN (t))
18609 tree type = TREE_VALUE (t);
18610 tree packs;
18611 if (!PACK_EXPANSION_P (type))
18612 continue;
18613 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18614 packs; packs = TREE_CHAIN (packs))
18615 if (template_args_equal (TREE_VALUE (packs), parm))
18617 /* The template parameter pack is used in a function parameter
18618 pack. If this is the end of the parameter list, the
18619 template parameter pack is deducible. */
18620 if (TREE_CHAIN (t) == void_list_node)
18621 return true;
18622 else
18623 /* Otherwise, not. Well, it could be deduced from
18624 a non-pack parameter, but doing so would end up with
18625 a deduction mismatch, so don't bother. */
18626 return false;
18629 /* The template parameter pack isn't used in any function parameter
18630 packs, but it might be used deeper, e.g. tuple<Args...>. */
18631 return true;
18634 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18635 NARGS elements of the arguments that are being used when calling
18636 it. TARGS is a vector into which the deduced template arguments
18637 are placed.
18639 Returns either a FUNCTION_DECL for the matching specialization of FN or
18640 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18641 true, diagnostics will be printed to explain why it failed.
18643 If FN is a conversion operator, or we are trying to produce a specific
18644 specialization, RETURN_TYPE is the return type desired.
18646 The EXPLICIT_TARGS are explicit template arguments provided via a
18647 template-id.
18649 The parameter STRICT is one of:
18651 DEDUCE_CALL:
18652 We are deducing arguments for a function call, as in
18653 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18654 deducing arguments for a call to the result of a conversion
18655 function template, as in [over.call.object].
18657 DEDUCE_CONV:
18658 We are deducing arguments for a conversion function, as in
18659 [temp.deduct.conv].
18661 DEDUCE_EXACT:
18662 We are deducing arguments when doing an explicit instantiation
18663 as in [temp.explicit], when determining an explicit specialization
18664 as in [temp.expl.spec], or when taking the address of a function
18665 template, as in [temp.deduct.funcaddr]. */
18667 tree
18668 fn_type_unification (tree fn,
18669 tree explicit_targs,
18670 tree targs,
18671 const tree *args,
18672 unsigned int nargs,
18673 tree return_type,
18674 unification_kind_t strict,
18675 int flags,
18676 bool explain_p,
18677 bool decltype_p)
18679 tree parms;
18680 tree fntype;
18681 tree decl = NULL_TREE;
18682 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18683 bool ok;
18684 static int deduction_depth;
18685 struct pending_template *old_last_pend = last_pending_template;
18686 struct tinst_level *old_error_tinst = last_error_tinst_level;
18688 tree orig_fn = fn;
18689 if (flag_new_inheriting_ctors)
18690 fn = strip_inheriting_ctors (fn);
18692 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18693 tree tinst;
18694 tree r = error_mark_node;
18696 tree full_targs = targs;
18697 if (TMPL_ARGS_DEPTH (targs)
18698 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18699 full_targs = (add_outermost_template_args
18700 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18701 targs));
18703 if (decltype_p)
18704 complain |= tf_decltype;
18706 /* In C++0x, it's possible to have a function template whose type depends
18707 on itself recursively. This is most obvious with decltype, but can also
18708 occur with enumeration scope (c++/48969). So we need to catch infinite
18709 recursion and reject the substitution at deduction time; this function
18710 will return error_mark_node for any repeated substitution.
18712 This also catches excessive recursion such as when f<N> depends on
18713 f<N-1> across all integers, and returns error_mark_node for all the
18714 substitutions back up to the initial one.
18716 This is, of course, not reentrant. */
18717 if (excessive_deduction_depth)
18718 return error_mark_node;
18719 tinst = build_tree_list (fn, NULL_TREE);
18720 ++deduction_depth;
18722 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18724 fntype = TREE_TYPE (fn);
18725 if (explicit_targs)
18727 /* [temp.deduct]
18729 The specified template arguments must match the template
18730 parameters in kind (i.e., type, nontype, template), and there
18731 must not be more arguments than there are parameters;
18732 otherwise type deduction fails.
18734 Nontype arguments must match the types of the corresponding
18735 nontype template parameters, or must be convertible to the
18736 types of the corresponding nontype parameters as specified in
18737 _temp.arg.nontype_, otherwise type deduction fails.
18739 All references in the function type of the function template
18740 to the corresponding template parameters are replaced by the
18741 specified template argument values. If a substitution in a
18742 template parameter or in the function type of the function
18743 template results in an invalid type, type deduction fails. */
18744 int i, len = TREE_VEC_LENGTH (tparms);
18745 location_t loc = input_location;
18746 bool incomplete = false;
18748 if (explicit_targs == error_mark_node)
18749 goto fail;
18751 if (TMPL_ARGS_DEPTH (explicit_targs)
18752 < TMPL_ARGS_DEPTH (full_targs))
18753 explicit_targs = add_outermost_template_args (full_targs,
18754 explicit_targs);
18756 /* Adjust any explicit template arguments before entering the
18757 substitution context. */
18758 explicit_targs
18759 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18760 complain,
18761 /*require_all_args=*/false,
18762 /*use_default_args=*/false));
18763 if (explicit_targs == error_mark_node)
18764 goto fail;
18766 /* Substitute the explicit args into the function type. This is
18767 necessary so that, for instance, explicitly declared function
18768 arguments can match null pointed constants. If we were given
18769 an incomplete set of explicit args, we must not do semantic
18770 processing during substitution as we could create partial
18771 instantiations. */
18772 for (i = 0; i < len; i++)
18774 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18775 bool parameter_pack = false;
18776 tree targ = TREE_VEC_ELT (explicit_targs, i);
18778 /* Dig out the actual parm. */
18779 if (TREE_CODE (parm) == TYPE_DECL
18780 || TREE_CODE (parm) == TEMPLATE_DECL)
18782 parm = TREE_TYPE (parm);
18783 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18785 else if (TREE_CODE (parm) == PARM_DECL)
18787 parm = DECL_INITIAL (parm);
18788 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18791 if (!parameter_pack && targ == NULL_TREE)
18792 /* No explicit argument for this template parameter. */
18793 incomplete = true;
18795 if (parameter_pack && pack_deducible_p (parm, fn))
18797 /* Mark the argument pack as "incomplete". We could
18798 still deduce more arguments during unification.
18799 We remove this mark in type_unification_real. */
18800 if (targ)
18802 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18803 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18804 = ARGUMENT_PACK_ARGS (targ);
18807 /* We have some incomplete argument packs. */
18808 incomplete = true;
18812 TREE_VALUE (tinst) = explicit_targs;
18813 if (!push_tinst_level (tinst))
18815 excessive_deduction_depth = true;
18816 goto fail;
18818 processing_template_decl += incomplete;
18819 input_location = DECL_SOURCE_LOCATION (fn);
18820 /* Ignore any access checks; we'll see them again in
18821 instantiate_template and they might have the wrong
18822 access path at this point. */
18823 push_deferring_access_checks (dk_deferred);
18824 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18825 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18826 pop_deferring_access_checks ();
18827 input_location = loc;
18828 processing_template_decl -= incomplete;
18829 pop_tinst_level ();
18831 if (fntype == error_mark_node)
18832 goto fail;
18834 /* Place the explicitly specified arguments in TARGS. */
18835 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18836 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18837 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18840 /* Never do unification on the 'this' parameter. */
18841 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18843 if (return_type && strict == DEDUCE_CALL)
18845 /* We're deducing for a call to the result of a template conversion
18846 function. The parms we really want are in return_type. */
18847 if (POINTER_TYPE_P (return_type))
18848 return_type = TREE_TYPE (return_type);
18849 parms = TYPE_ARG_TYPES (return_type);
18851 else if (return_type)
18853 tree *new_args;
18855 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18856 new_args = XALLOCAVEC (tree, nargs + 1);
18857 new_args[0] = return_type;
18858 memcpy (new_args + 1, args, nargs * sizeof (tree));
18859 args = new_args;
18860 ++nargs;
18863 /* We allow incomplete unification without an error message here
18864 because the standard doesn't seem to explicitly prohibit it. Our
18865 callers must be ready to deal with unification failures in any
18866 event. */
18868 TREE_VALUE (tinst) = targs;
18869 /* If we aren't explaining yet, push tinst context so we can see where
18870 any errors (e.g. from class instantiations triggered by instantiation
18871 of default template arguments) come from. If we are explaining, this
18872 context is redundant. */
18873 if (!explain_p && !push_tinst_level (tinst))
18875 excessive_deduction_depth = true;
18876 goto fail;
18879 /* type_unification_real will pass back any access checks from default
18880 template argument substitution. */
18881 vec<deferred_access_check, va_gc> *checks;
18882 checks = NULL;
18884 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18885 full_targs, parms, args, nargs, /*subr=*/0,
18886 strict, flags, &checks, explain_p);
18887 if (!explain_p)
18888 pop_tinst_level ();
18889 if (!ok)
18890 goto fail;
18892 /* Now that we have bindings for all of the template arguments,
18893 ensure that the arguments deduced for the template template
18894 parameters have compatible template parameter lists. We cannot
18895 check this property before we have deduced all template
18896 arguments, because the template parameter types of a template
18897 template parameter might depend on prior template parameters
18898 deduced after the template template parameter. The following
18899 ill-formed example illustrates this issue:
18901 template<typename T, template<T> class C> void f(C<5>, T);
18903 template<int N> struct X {};
18905 void g() {
18906 f(X<5>(), 5l); // error: template argument deduction fails
18909 The template parameter list of 'C' depends on the template type
18910 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18911 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18912 time that we deduce 'C'. */
18913 if (!template_template_parm_bindings_ok_p
18914 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18916 unify_inconsistent_template_template_parameters (explain_p);
18917 goto fail;
18920 /* All is well so far. Now, check:
18922 [temp.deduct]
18924 When all template arguments have been deduced, all uses of
18925 template parameters in nondeduced contexts are replaced with
18926 the corresponding deduced argument values. If the
18927 substitution results in an invalid type, as described above,
18928 type deduction fails. */
18929 TREE_VALUE (tinst) = targs;
18930 if (!push_tinst_level (tinst))
18932 excessive_deduction_depth = true;
18933 goto fail;
18936 /* Also collect access checks from the instantiation. */
18937 reopen_deferring_access_checks (checks);
18939 decl = instantiate_template (fn, targs, complain);
18941 checks = get_deferred_access_checks ();
18942 pop_deferring_access_checks ();
18944 pop_tinst_level ();
18946 if (decl == error_mark_node)
18947 goto fail;
18949 /* Now perform any access checks encountered during substitution. */
18950 push_access_scope (decl);
18951 ok = perform_access_checks (checks, complain);
18952 pop_access_scope (decl);
18953 if (!ok)
18954 goto fail;
18956 /* If we're looking for an exact match, check that what we got
18957 is indeed an exact match. It might not be if some template
18958 parameters are used in non-deduced contexts. But don't check
18959 for an exact match if we have dependent template arguments;
18960 in that case we're doing partial ordering, and we already know
18961 that we have two candidates that will provide the actual type. */
18962 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18964 tree substed = TREE_TYPE (decl);
18965 unsigned int i;
18967 tree sarg
18968 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18969 if (return_type)
18970 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18971 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18972 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18974 unify_type_mismatch (explain_p, args[i],
18975 TREE_VALUE (sarg));
18976 goto fail;
18980 /* After doing deduction with the inherited constructor, actually return an
18981 instantiation of the inheriting constructor. */
18982 if (orig_fn != fn)
18983 decl = instantiate_template (orig_fn, targs, complain);
18985 r = decl;
18987 fail:
18988 --deduction_depth;
18989 if (excessive_deduction_depth)
18991 if (deduction_depth == 0)
18992 /* Reset once we're all the way out. */
18993 excessive_deduction_depth = false;
18996 /* We can't free this if a pending_template entry or last_error_tinst_level
18997 is pointing at it. */
18998 if (last_pending_template == old_last_pend
18999 && last_error_tinst_level == old_error_tinst)
19000 ggc_free (tinst);
19002 return r;
19005 /* Adjust types before performing type deduction, as described in
19006 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19007 sections are symmetric. PARM is the type of a function parameter
19008 or the return type of the conversion function. ARG is the type of
19009 the argument passed to the call, or the type of the value
19010 initialized with the result of the conversion function.
19011 ARG_EXPR is the original argument expression, which may be null. */
19013 static int
19014 maybe_adjust_types_for_deduction (unification_kind_t strict,
19015 tree* parm,
19016 tree* arg,
19017 tree arg_expr)
19019 int result = 0;
19021 switch (strict)
19023 case DEDUCE_CALL:
19024 break;
19026 case DEDUCE_CONV:
19027 /* Swap PARM and ARG throughout the remainder of this
19028 function; the handling is precisely symmetric since PARM
19029 will initialize ARG rather than vice versa. */
19030 std::swap (parm, arg);
19031 break;
19033 case DEDUCE_EXACT:
19034 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19035 too, but here handle it by stripping the reference from PARM
19036 rather than by adding it to ARG. */
19037 if (TREE_CODE (*parm) == REFERENCE_TYPE
19038 && TYPE_REF_IS_RVALUE (*parm)
19039 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19040 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19041 && TREE_CODE (*arg) == REFERENCE_TYPE
19042 && !TYPE_REF_IS_RVALUE (*arg))
19043 *parm = TREE_TYPE (*parm);
19044 /* Nothing else to do in this case. */
19045 return 0;
19047 default:
19048 gcc_unreachable ();
19051 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19053 /* [temp.deduct.call]
19055 If P is not a reference type:
19057 --If A is an array type, the pointer type produced by the
19058 array-to-pointer standard conversion (_conv.array_) is
19059 used in place of A for type deduction; otherwise,
19061 --If A is a function type, the pointer type produced by
19062 the function-to-pointer standard conversion
19063 (_conv.func_) is used in place of A for type deduction;
19064 otherwise,
19066 --If A is a cv-qualified type, the top level
19067 cv-qualifiers of A's type are ignored for type
19068 deduction. */
19069 if (TREE_CODE (*arg) == ARRAY_TYPE)
19070 *arg = build_pointer_type (TREE_TYPE (*arg));
19071 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19072 *arg = build_pointer_type (*arg);
19073 else
19074 *arg = TYPE_MAIN_VARIANT (*arg);
19077 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19078 reference to a cv-unqualified template parameter that does not represent a
19079 template parameter of a class template (during class template argument
19080 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19081 an lvalue, the type "lvalue reference to A" is used in place of A for type
19082 deduction. */
19083 if (TREE_CODE (*parm) == REFERENCE_TYPE
19084 && TYPE_REF_IS_RVALUE (*parm)
19085 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19086 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19087 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19088 && (arg_expr ? lvalue_p (arg_expr)
19089 /* try_one_overload doesn't provide an arg_expr, but
19090 functions are always lvalues. */
19091 : TREE_CODE (*arg) == FUNCTION_TYPE))
19092 *arg = build_reference_type (*arg);
19094 /* [temp.deduct.call]
19096 If P is a cv-qualified type, the top level cv-qualifiers
19097 of P's type are ignored for type deduction. If P is a
19098 reference type, the type referred to by P is used for
19099 type deduction. */
19100 *parm = TYPE_MAIN_VARIANT (*parm);
19101 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19103 *parm = TREE_TYPE (*parm);
19104 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19107 /* DR 322. For conversion deduction, remove a reference type on parm
19108 too (which has been swapped into ARG). */
19109 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19110 *arg = TREE_TYPE (*arg);
19112 return result;
19115 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19116 template which does contain any deducible template parameters; check if
19117 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19118 unify_one_argument. */
19120 static int
19121 check_non_deducible_conversion (tree parm, tree arg, int strict,
19122 int flags, bool explain_p)
19124 tree type;
19126 if (!TYPE_P (arg))
19127 type = TREE_TYPE (arg);
19128 else
19129 type = arg;
19131 if (same_type_p (parm, type))
19132 return unify_success (explain_p);
19134 if (strict == DEDUCE_CONV)
19136 if (can_convert_arg (type, parm, NULL_TREE, flags,
19137 explain_p ? tf_warning_or_error : tf_none))
19138 return unify_success (explain_p);
19140 else if (strict != DEDUCE_EXACT)
19142 if (can_convert_arg (parm, type,
19143 TYPE_P (arg) ? NULL_TREE : arg,
19144 flags, explain_p ? tf_warning_or_error : tf_none))
19145 return unify_success (explain_p);
19148 if (strict == DEDUCE_EXACT)
19149 return unify_type_mismatch (explain_p, parm, arg);
19150 else
19151 return unify_arg_conversion (explain_p, parm, type, arg);
19154 static bool uses_deducible_template_parms (tree type);
19156 /* Returns true iff the expression EXPR is one from which a template
19157 argument can be deduced. In other words, if it's an undecorated
19158 use of a template non-type parameter. */
19160 static bool
19161 deducible_expression (tree expr)
19163 /* Strip implicit conversions. */
19164 while (CONVERT_EXPR_P (expr))
19165 expr = TREE_OPERAND (expr, 0);
19166 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19169 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19170 deducible way; that is, if it has a max value of <PARM> - 1. */
19172 static bool
19173 deducible_array_bound (tree domain)
19175 if (domain == NULL_TREE)
19176 return false;
19178 tree max = TYPE_MAX_VALUE (domain);
19179 if (TREE_CODE (max) != MINUS_EXPR)
19180 return false;
19182 return deducible_expression (TREE_OPERAND (max, 0));
19185 /* Returns true iff the template arguments ARGS use a template parameter
19186 in a deducible way. */
19188 static bool
19189 deducible_template_args (tree args)
19191 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19193 bool deducible;
19194 tree elt = TREE_VEC_ELT (args, i);
19195 if (ARGUMENT_PACK_P (elt))
19196 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19197 else
19199 if (PACK_EXPANSION_P (elt))
19200 elt = PACK_EXPANSION_PATTERN (elt);
19201 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19202 deducible = true;
19203 else if (TYPE_P (elt))
19204 deducible = uses_deducible_template_parms (elt);
19205 else
19206 deducible = deducible_expression (elt);
19208 if (deducible)
19209 return true;
19211 return false;
19214 /* Returns true iff TYPE contains any deducible references to template
19215 parameters, as per 14.8.2.5. */
19217 static bool
19218 uses_deducible_template_parms (tree type)
19220 if (PACK_EXPANSION_P (type))
19221 type = PACK_EXPANSION_PATTERN (type);
19223 /* T
19224 cv-list T
19225 TT<T>
19226 TT<i>
19227 TT<> */
19228 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19229 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19230 return true;
19232 /* T*
19234 T&& */
19235 if (POINTER_TYPE_P (type))
19236 return uses_deducible_template_parms (TREE_TYPE (type));
19238 /* T[integer-constant ]
19239 type [i] */
19240 if (TREE_CODE (type) == ARRAY_TYPE)
19241 return (uses_deducible_template_parms (TREE_TYPE (type))
19242 || deducible_array_bound (TYPE_DOMAIN (type)));
19244 /* T type ::*
19245 type T::*
19246 T T::*
19247 T (type ::*)()
19248 type (T::*)()
19249 type (type ::*)(T)
19250 type (T::*)(T)
19251 T (type ::*)(T)
19252 T (T::*)()
19253 T (T::*)(T) */
19254 if (TYPE_PTRMEM_P (type))
19255 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19256 || (uses_deducible_template_parms
19257 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19259 /* template-name <T> (where template-name refers to a class template)
19260 template-name <i> (where template-name refers to a class template) */
19261 if (CLASS_TYPE_P (type)
19262 && CLASSTYPE_TEMPLATE_INFO (type)
19263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19264 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19265 (CLASSTYPE_TI_ARGS (type)));
19267 /* type (T)
19269 T(T) */
19270 if (TREE_CODE (type) == FUNCTION_TYPE
19271 || TREE_CODE (type) == METHOD_TYPE)
19273 if (uses_deducible_template_parms (TREE_TYPE (type)))
19274 return true;
19275 tree parm = TYPE_ARG_TYPES (type);
19276 if (TREE_CODE (type) == METHOD_TYPE)
19277 parm = TREE_CHAIN (parm);
19278 for (; parm; parm = TREE_CHAIN (parm))
19279 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19280 return true;
19283 return false;
19286 /* Subroutine of type_unification_real and unify_pack_expansion to
19287 handle unification of a single P/A pair. Parameters are as
19288 for those functions. */
19290 static int
19291 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19292 int subr, unification_kind_t strict,
19293 bool explain_p)
19295 tree arg_expr = NULL_TREE;
19296 int arg_strict;
19298 if (arg == error_mark_node || parm == error_mark_node)
19299 return unify_invalid (explain_p);
19300 if (arg == unknown_type_node)
19301 /* We can't deduce anything from this, but we might get all the
19302 template args from other function args. */
19303 return unify_success (explain_p);
19305 /* Implicit conversions (Clause 4) will be performed on a function
19306 argument to convert it to the type of the corresponding function
19307 parameter if the parameter type contains no template-parameters that
19308 participate in template argument deduction. */
19309 if (strict != DEDUCE_EXACT
19310 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19311 /* For function parameters with no deducible template parameters,
19312 just return. We'll check non-dependent conversions later. */
19313 return unify_success (explain_p);
19315 switch (strict)
19317 case DEDUCE_CALL:
19318 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19319 | UNIFY_ALLOW_MORE_CV_QUAL
19320 | UNIFY_ALLOW_DERIVED);
19321 break;
19323 case DEDUCE_CONV:
19324 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19325 break;
19327 case DEDUCE_EXACT:
19328 arg_strict = UNIFY_ALLOW_NONE;
19329 break;
19331 default:
19332 gcc_unreachable ();
19335 /* We only do these transformations if this is the top-level
19336 parameter_type_list in a call or declaration matching; in other
19337 situations (nested function declarators, template argument lists) we
19338 won't be comparing a type to an expression, and we don't do any type
19339 adjustments. */
19340 if (!subr)
19342 if (!TYPE_P (arg))
19344 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19345 if (type_unknown_p (arg))
19347 /* [temp.deduct.type] A template-argument can be
19348 deduced from a pointer to function or pointer
19349 to member function argument if the set of
19350 overloaded functions does not contain function
19351 templates and at most one of a set of
19352 overloaded functions provides a unique
19353 match. */
19354 resolve_overloaded_unification (tparms, targs, parm,
19355 arg, strict,
19356 arg_strict, explain_p);
19357 /* If a unique match was not found, this is a
19358 non-deduced context, so we still succeed. */
19359 return unify_success (explain_p);
19362 arg_expr = arg;
19363 arg = unlowered_expr_type (arg);
19364 if (arg == error_mark_node)
19365 return unify_invalid (explain_p);
19368 arg_strict |=
19369 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19371 else
19372 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19373 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19374 return unify_template_argument_mismatch (explain_p, parm, arg);
19376 /* For deduction from an init-list we need the actual list. */
19377 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19378 arg = arg_expr;
19379 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19382 /* for_each_template_parm callback that always returns 0. */
19384 static int
19385 zero_r (tree, void *)
19387 return 0;
19390 /* for_each_template_parm any_fn callback to handle deduction of a template
19391 type argument from the type of an array bound. */
19393 static int
19394 array_deduction_r (tree t, void *data)
19396 tree_pair_p d = (tree_pair_p)data;
19397 tree &tparms = d->purpose;
19398 tree &targs = d->value;
19400 if (TREE_CODE (t) == ARRAY_TYPE)
19401 if (tree dom = TYPE_DOMAIN (t))
19402 if (tree max = TYPE_MAX_VALUE (dom))
19404 if (TREE_CODE (max) == MINUS_EXPR)
19405 max = TREE_OPERAND (max, 0);
19406 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19407 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19408 UNIFY_ALLOW_NONE, /*explain*/false);
19411 /* Keep walking. */
19412 return 0;
19415 /* Try to deduce any not-yet-deduced template type arguments from the type of
19416 an array bound. This is handled separately from unify because 14.8.2.5 says
19417 "The type of a type parameter is only deduced from an array bound if it is
19418 not otherwise deduced." */
19420 static void
19421 try_array_deduction (tree tparms, tree targs, tree parm)
19423 tree_pair_s data = { tparms, targs };
19424 hash_set<tree> visited;
19425 for_each_template_parm (parm, zero_r, &data, &visited,
19426 /*nondeduced*/false, array_deduction_r);
19429 /* Most parms like fn_type_unification.
19431 If SUBR is 1, we're being called recursively (to unify the
19432 arguments of a function or method parameter of a function
19433 template).
19435 CHECKS is a pointer to a vector of access checks encountered while
19436 substituting default template arguments. */
19438 static int
19439 type_unification_real (tree tparms,
19440 tree full_targs,
19441 tree xparms,
19442 const tree *xargs,
19443 unsigned int xnargs,
19444 int subr,
19445 unification_kind_t strict,
19446 int flags,
19447 vec<deferred_access_check, va_gc> **checks,
19448 bool explain_p)
19450 tree parm, arg;
19451 int i;
19452 int ntparms = TREE_VEC_LENGTH (tparms);
19453 int saw_undeduced = 0;
19454 tree parms;
19455 const tree *args;
19456 unsigned int nargs;
19457 unsigned int ia;
19459 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19460 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19461 gcc_assert (ntparms > 0);
19463 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19465 /* Reset the number of non-defaulted template arguments contained
19466 in TARGS. */
19467 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19469 again:
19470 parms = xparms;
19471 args = xargs;
19472 nargs = xnargs;
19474 ia = 0;
19475 while (parms && parms != void_list_node
19476 && ia < nargs)
19478 parm = TREE_VALUE (parms);
19480 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19481 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19482 /* For a function parameter pack that occurs at the end of the
19483 parameter-declaration-list, the type A of each remaining
19484 argument of the call is compared with the type P of the
19485 declarator-id of the function parameter pack. */
19486 break;
19488 parms = TREE_CHAIN (parms);
19490 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19491 /* For a function parameter pack that does not occur at the
19492 end of the parameter-declaration-list, the type of the
19493 parameter pack is a non-deduced context. */
19494 continue;
19496 arg = args[ia];
19497 ++ia;
19499 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19500 explain_p))
19501 return 1;
19504 if (parms
19505 && parms != void_list_node
19506 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19508 /* Unify the remaining arguments with the pack expansion type. */
19509 tree argvec;
19510 tree parmvec = make_tree_vec (1);
19512 /* Allocate a TREE_VEC and copy in all of the arguments */
19513 argvec = make_tree_vec (nargs - ia);
19514 for (i = 0; ia < nargs; ++ia, ++i)
19515 TREE_VEC_ELT (argvec, i) = args[ia];
19517 /* Copy the parameter into parmvec. */
19518 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19519 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19520 /*subr=*/subr, explain_p))
19521 return 1;
19523 /* Advance to the end of the list of parameters. */
19524 parms = TREE_CHAIN (parms);
19527 /* Fail if we've reached the end of the parm list, and more args
19528 are present, and the parm list isn't variadic. */
19529 if (ia < nargs && parms == void_list_node)
19530 return unify_too_many_arguments (explain_p, nargs, ia);
19531 /* Fail if parms are left and they don't have default values and
19532 they aren't all deduced as empty packs (c++/57397). This is
19533 consistent with sufficient_parms_p. */
19534 if (parms && parms != void_list_node
19535 && TREE_PURPOSE (parms) == NULL_TREE)
19537 unsigned int count = nargs;
19538 tree p = parms;
19539 bool type_pack_p;
19542 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19543 if (!type_pack_p)
19544 count++;
19545 p = TREE_CHAIN (p);
19547 while (p && p != void_list_node);
19548 if (count != nargs)
19549 return unify_too_few_arguments (explain_p, ia, count,
19550 type_pack_p);
19553 if (!subr)
19555 tsubst_flags_t complain = (explain_p
19556 ? tf_warning_or_error
19557 : tf_none);
19558 bool tried_array_deduction = (cxx_dialect < cxx17);
19560 for (i = 0; i < ntparms; i++)
19562 tree targ = TREE_VEC_ELT (targs, i);
19563 tree tparm = TREE_VEC_ELT (tparms, i);
19565 /* Clear the "incomplete" flags on all argument packs now so that
19566 substituting them into later default arguments works. */
19567 if (targ && ARGUMENT_PACK_P (targ))
19569 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19570 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19573 if (targ || tparm == error_mark_node)
19574 continue;
19575 tparm = TREE_VALUE (tparm);
19577 if (TREE_CODE (tparm) == TYPE_DECL
19578 && !tried_array_deduction)
19580 try_array_deduction (tparms, targs, xparms);
19581 tried_array_deduction = true;
19582 if (TREE_VEC_ELT (targs, i))
19583 continue;
19586 /* If this is an undeduced nontype parameter that depends on
19587 a type parameter, try another pass; its type may have been
19588 deduced from a later argument than the one from which
19589 this parameter can be deduced. */
19590 if (TREE_CODE (tparm) == PARM_DECL
19591 && uses_template_parms (TREE_TYPE (tparm))
19592 && saw_undeduced < 2)
19594 saw_undeduced = 1;
19595 continue;
19598 /* Core issue #226 (C++0x) [temp.deduct]:
19600 If a template argument has not been deduced, its
19601 default template argument, if any, is used.
19603 When we are in C++98 mode, TREE_PURPOSE will either
19604 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19605 to explicitly check cxx_dialect here. */
19606 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19607 /* OK, there is a default argument. Wait until after the
19608 conversion check to do substitution. */
19609 continue;
19611 /* If the type parameter is a parameter pack, then it will
19612 be deduced to an empty parameter pack. */
19613 if (template_parameter_pack_p (tparm))
19615 tree arg;
19617 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19619 arg = make_node (NONTYPE_ARGUMENT_PACK);
19620 TREE_CONSTANT (arg) = 1;
19622 else
19623 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19625 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19627 TREE_VEC_ELT (targs, i) = arg;
19628 continue;
19631 return unify_parameter_deduction_failure (explain_p, tparm);
19634 /* DR 1391: All parameters have args, now check non-dependent parms for
19635 convertibility. */
19636 if (saw_undeduced < 2)
19637 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19638 parms && parms != void_list_node && ia < nargs; )
19640 parm = TREE_VALUE (parms);
19642 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19643 && (!TREE_CHAIN (parms)
19644 || TREE_CHAIN (parms) == void_list_node))
19645 /* For a function parameter pack that occurs at the end of the
19646 parameter-declaration-list, the type A of each remaining
19647 argument of the call is compared with the type P of the
19648 declarator-id of the function parameter pack. */
19649 break;
19651 parms = TREE_CHAIN (parms);
19653 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19654 /* For a function parameter pack that does not occur at the
19655 end of the parameter-declaration-list, the type of the
19656 parameter pack is a non-deduced context. */
19657 continue;
19659 arg = args[ia];
19660 ++ia;
19662 if (uses_template_parms (parm))
19663 continue;
19664 if (check_non_deducible_conversion (parm, arg, strict, flags,
19665 explain_p))
19666 return 1;
19669 /* Now substitute into the default template arguments. */
19670 for (i = 0; i < ntparms; i++)
19672 tree targ = TREE_VEC_ELT (targs, i);
19673 tree tparm = TREE_VEC_ELT (tparms, i);
19675 if (targ || tparm == error_mark_node)
19676 continue;
19677 tree parm = TREE_VALUE (tparm);
19679 if (TREE_CODE (parm) == PARM_DECL
19680 && uses_template_parms (TREE_TYPE (parm))
19681 && saw_undeduced < 2)
19682 continue;
19684 tree arg = TREE_PURPOSE (tparm);
19685 reopen_deferring_access_checks (*checks);
19686 location_t save_loc = input_location;
19687 if (DECL_P (parm))
19688 input_location = DECL_SOURCE_LOCATION (parm);
19689 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19690 if (!uses_template_parms (arg))
19691 arg = convert_template_argument (parm, arg, full_targs, complain,
19692 i, NULL_TREE);
19693 else if (saw_undeduced < 2)
19694 arg = NULL_TREE;
19695 else
19696 arg = error_mark_node;
19697 input_location = save_loc;
19698 *checks = get_deferred_access_checks ();
19699 pop_deferring_access_checks ();
19700 if (arg == error_mark_node)
19701 return 1;
19702 else if (arg)
19704 TREE_VEC_ELT (targs, i) = arg;
19705 /* The position of the first default template argument,
19706 is also the number of non-defaulted arguments in TARGS.
19707 Record that. */
19708 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19713 if (saw_undeduced++ == 1)
19714 goto again;
19717 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19718 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19720 return unify_success (explain_p);
19723 /* Subroutine of type_unification_real. Args are like the variables
19724 at the call site. ARG is an overloaded function (or template-id);
19725 we try deducing template args from each of the overloads, and if
19726 only one succeeds, we go with that. Modifies TARGS and returns
19727 true on success. */
19729 static bool
19730 resolve_overloaded_unification (tree tparms,
19731 tree targs,
19732 tree parm,
19733 tree arg,
19734 unification_kind_t strict,
19735 int sub_strict,
19736 bool explain_p)
19738 tree tempargs = copy_node (targs);
19739 int good = 0;
19740 tree goodfn = NULL_TREE;
19741 bool addr_p;
19743 if (TREE_CODE (arg) == ADDR_EXPR)
19745 arg = TREE_OPERAND (arg, 0);
19746 addr_p = true;
19748 else
19749 addr_p = false;
19751 if (TREE_CODE (arg) == COMPONENT_REF)
19752 /* Handle `&x' where `x' is some static or non-static member
19753 function name. */
19754 arg = TREE_OPERAND (arg, 1);
19756 if (TREE_CODE (arg) == OFFSET_REF)
19757 arg = TREE_OPERAND (arg, 1);
19759 /* Strip baselink information. */
19760 if (BASELINK_P (arg))
19761 arg = BASELINK_FUNCTIONS (arg);
19763 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19765 /* If we got some explicit template args, we need to plug them into
19766 the affected templates before we try to unify, in case the
19767 explicit args will completely resolve the templates in question. */
19769 int ok = 0;
19770 tree expl_subargs = TREE_OPERAND (arg, 1);
19771 arg = TREE_OPERAND (arg, 0);
19773 for (lkp_iterator iter (arg); iter; ++iter)
19775 tree fn = *iter;
19776 tree subargs, elem;
19778 if (TREE_CODE (fn) != TEMPLATE_DECL)
19779 continue;
19781 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19782 expl_subargs, NULL_TREE, tf_none,
19783 /*require_all_args=*/true,
19784 /*use_default_args=*/true);
19785 if (subargs != error_mark_node
19786 && !any_dependent_template_arguments_p (subargs))
19788 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19789 if (try_one_overload (tparms, targs, tempargs, parm,
19790 elem, strict, sub_strict, addr_p, explain_p)
19791 && (!goodfn || !same_type_p (goodfn, elem)))
19793 goodfn = elem;
19794 ++good;
19797 else if (subargs)
19798 ++ok;
19800 /* If no templates (or more than one) are fully resolved by the
19801 explicit arguments, this template-id is a non-deduced context; it
19802 could still be OK if we deduce all template arguments for the
19803 enclosing call through other arguments. */
19804 if (good != 1)
19805 good = ok;
19807 else if (TREE_CODE (arg) != OVERLOAD
19808 && TREE_CODE (arg) != FUNCTION_DECL)
19809 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19810 -- but the deduction does not succeed because the expression is
19811 not just the function on its own. */
19812 return false;
19813 else
19814 for (lkp_iterator iter (arg); iter; ++iter)
19816 tree fn = *iter;
19817 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19818 strict, sub_strict, addr_p, explain_p)
19819 && (!goodfn || !decls_match (goodfn, fn)))
19821 goodfn = fn;
19822 ++good;
19826 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19827 to function or pointer to member function argument if the set of
19828 overloaded functions does not contain function templates and at most
19829 one of a set of overloaded functions provides a unique match.
19831 So if we found multiple possibilities, we return success but don't
19832 deduce anything. */
19834 if (good == 1)
19836 int i = TREE_VEC_LENGTH (targs);
19837 for (; i--; )
19838 if (TREE_VEC_ELT (tempargs, i))
19840 tree old = TREE_VEC_ELT (targs, i);
19841 tree new_ = TREE_VEC_ELT (tempargs, i);
19842 if (new_ && old && ARGUMENT_PACK_P (old)
19843 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19844 /* Don't forget explicit template arguments in a pack. */
19845 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19846 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19847 TREE_VEC_ELT (targs, i) = new_;
19850 if (good)
19851 return true;
19853 return false;
19856 /* Core DR 115: In contexts where deduction is done and fails, or in
19857 contexts where deduction is not done, if a template argument list is
19858 specified and it, along with any default template arguments, identifies
19859 a single function template specialization, then the template-id is an
19860 lvalue for the function template specialization. */
19862 tree
19863 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19865 tree expr, offset, baselink;
19866 bool addr;
19868 if (!type_unknown_p (orig_expr))
19869 return orig_expr;
19871 expr = orig_expr;
19872 addr = false;
19873 offset = NULL_TREE;
19874 baselink = NULL_TREE;
19876 if (TREE_CODE (expr) == ADDR_EXPR)
19878 expr = TREE_OPERAND (expr, 0);
19879 addr = true;
19881 if (TREE_CODE (expr) == OFFSET_REF)
19883 offset = expr;
19884 expr = TREE_OPERAND (expr, 1);
19886 if (BASELINK_P (expr))
19888 baselink = expr;
19889 expr = BASELINK_FUNCTIONS (expr);
19892 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19894 int good = 0;
19895 tree goodfn = NULL_TREE;
19897 /* If we got some explicit template args, we need to plug them into
19898 the affected templates before we try to unify, in case the
19899 explicit args will completely resolve the templates in question. */
19901 tree expl_subargs = TREE_OPERAND (expr, 1);
19902 tree arg = TREE_OPERAND (expr, 0);
19903 tree badfn = NULL_TREE;
19904 tree badargs = NULL_TREE;
19906 for (lkp_iterator iter (arg); iter; ++iter)
19908 tree fn = *iter;
19909 tree subargs, elem;
19911 if (TREE_CODE (fn) != TEMPLATE_DECL)
19912 continue;
19914 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19915 expl_subargs, NULL_TREE, tf_none,
19916 /*require_all_args=*/true,
19917 /*use_default_args=*/true);
19918 if (subargs != error_mark_node
19919 && !any_dependent_template_arguments_p (subargs))
19921 elem = instantiate_template (fn, subargs, tf_none);
19922 if (elem == error_mark_node)
19924 badfn = fn;
19925 badargs = subargs;
19927 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19929 goodfn = elem;
19930 ++good;
19934 if (good == 1)
19936 mark_used (goodfn);
19937 expr = goodfn;
19938 if (baselink)
19939 expr = build_baselink (BASELINK_BINFO (baselink),
19940 BASELINK_ACCESS_BINFO (baselink),
19941 expr, BASELINK_OPTYPE (baselink));
19942 if (offset)
19944 tree base
19945 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19946 expr = build_offset_ref (base, expr, addr, complain);
19948 if (addr)
19949 expr = cp_build_addr_expr (expr, complain);
19950 return expr;
19952 else if (good == 0 && badargs && (complain & tf_error))
19953 /* There were no good options and at least one bad one, so let the
19954 user know what the problem is. */
19955 instantiate_template (badfn, badargs, complain);
19957 return orig_expr;
19960 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19961 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19962 different overloads deduce different arguments for a given parm.
19963 ADDR_P is true if the expression for which deduction is being
19964 performed was of the form "& fn" rather than simply "fn".
19966 Returns 1 on success. */
19968 static int
19969 try_one_overload (tree tparms,
19970 tree orig_targs,
19971 tree targs,
19972 tree parm,
19973 tree arg,
19974 unification_kind_t strict,
19975 int sub_strict,
19976 bool addr_p,
19977 bool explain_p)
19979 int nargs;
19980 tree tempargs;
19981 int i;
19983 if (arg == error_mark_node)
19984 return 0;
19986 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19987 to function or pointer to member function argument if the set of
19988 overloaded functions does not contain function templates and at most
19989 one of a set of overloaded functions provides a unique match.
19991 So if this is a template, just return success. */
19993 if (uses_template_parms (arg))
19994 return 1;
19996 if (TREE_CODE (arg) == METHOD_TYPE)
19997 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19998 else if (addr_p)
19999 arg = build_pointer_type (arg);
20001 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20003 /* We don't copy orig_targs for this because if we have already deduced
20004 some template args from previous args, unify would complain when we
20005 try to deduce a template parameter for the same argument, even though
20006 there isn't really a conflict. */
20007 nargs = TREE_VEC_LENGTH (targs);
20008 tempargs = make_tree_vec (nargs);
20010 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20011 return 0;
20013 /* First make sure we didn't deduce anything that conflicts with
20014 explicitly specified args. */
20015 for (i = nargs; i--; )
20017 tree elt = TREE_VEC_ELT (tempargs, i);
20018 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20020 if (!elt)
20021 /*NOP*/;
20022 else if (uses_template_parms (elt))
20023 /* Since we're unifying against ourselves, we will fill in
20024 template args used in the function parm list with our own
20025 template parms. Discard them. */
20026 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20027 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20029 /* Check that the argument at each index of the deduced argument pack
20030 is equivalent to the corresponding explicitly specified argument.
20031 We may have deduced more arguments than were explicitly specified,
20032 and that's OK. */
20034 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20035 that's wrong if we deduce the same argument pack from multiple
20036 function arguments: it's only incomplete the first time. */
20038 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20039 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20041 if (TREE_VEC_LENGTH (deduced_pack)
20042 < TREE_VEC_LENGTH (explicit_pack))
20043 return 0;
20045 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20046 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20047 TREE_VEC_ELT (deduced_pack, j)))
20048 return 0;
20050 else if (oldelt && !template_args_equal (oldelt, elt))
20051 return 0;
20054 for (i = nargs; i--; )
20056 tree elt = TREE_VEC_ELT (tempargs, i);
20058 if (elt)
20059 TREE_VEC_ELT (targs, i) = elt;
20062 return 1;
20065 /* PARM is a template class (perhaps with unbound template
20066 parameters). ARG is a fully instantiated type. If ARG can be
20067 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20068 TARGS are as for unify. */
20070 static tree
20071 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20072 bool explain_p)
20074 tree copy_of_targs;
20076 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20077 return NULL_TREE;
20078 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20079 /* Matches anything. */;
20080 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20081 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20082 return NULL_TREE;
20084 /* We need to make a new template argument vector for the call to
20085 unify. If we used TARGS, we'd clutter it up with the result of
20086 the attempted unification, even if this class didn't work out.
20087 We also don't want to commit ourselves to all the unifications
20088 we've already done, since unification is supposed to be done on
20089 an argument-by-argument basis. In other words, consider the
20090 following pathological case:
20092 template <int I, int J, int K>
20093 struct S {};
20095 template <int I, int J>
20096 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20098 template <int I, int J, int K>
20099 void f(S<I, J, K>, S<I, I, I>);
20101 void g() {
20102 S<0, 0, 0> s0;
20103 S<0, 1, 2> s2;
20105 f(s0, s2);
20108 Now, by the time we consider the unification involving `s2', we
20109 already know that we must have `f<0, 0, 0>'. But, even though
20110 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20111 because there are two ways to unify base classes of S<0, 1, 2>
20112 with S<I, I, I>. If we kept the already deduced knowledge, we
20113 would reject the possibility I=1. */
20114 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20116 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20118 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20119 return NULL_TREE;
20120 return arg;
20123 /* If unification failed, we're done. */
20124 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20125 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20126 return NULL_TREE;
20128 return arg;
20131 /* Given a template type PARM and a class type ARG, find the unique
20132 base type in ARG that is an instance of PARM. We do not examine
20133 ARG itself; only its base-classes. If there is not exactly one
20134 appropriate base class, return NULL_TREE. PARM may be the type of
20135 a partial specialization, as well as a plain template type. Used
20136 by unify. */
20138 static enum template_base_result
20139 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20140 bool explain_p, tree *result)
20142 tree rval = NULL_TREE;
20143 tree binfo;
20145 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20147 binfo = TYPE_BINFO (complete_type (arg));
20148 if (!binfo)
20150 /* The type could not be completed. */
20151 *result = NULL_TREE;
20152 return tbr_incomplete_type;
20155 /* Walk in inheritance graph order. The search order is not
20156 important, and this avoids multiple walks of virtual bases. */
20157 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20159 tree r = try_class_unification (tparms, targs, parm,
20160 BINFO_TYPE (binfo), explain_p);
20162 if (r)
20164 /* If there is more than one satisfactory baseclass, then:
20166 [temp.deduct.call]
20168 If they yield more than one possible deduced A, the type
20169 deduction fails.
20171 applies. */
20172 if (rval && !same_type_p (r, rval))
20174 *result = NULL_TREE;
20175 return tbr_ambiguous_baseclass;
20178 rval = r;
20182 *result = rval;
20183 return tbr_success;
20186 /* Returns the level of DECL, which declares a template parameter. */
20188 static int
20189 template_decl_level (tree decl)
20191 switch (TREE_CODE (decl))
20193 case TYPE_DECL:
20194 case TEMPLATE_DECL:
20195 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20197 case PARM_DECL:
20198 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20200 default:
20201 gcc_unreachable ();
20203 return 0;
20206 /* Decide whether ARG can be unified with PARM, considering only the
20207 cv-qualifiers of each type, given STRICT as documented for unify.
20208 Returns nonzero iff the unification is OK on that basis. */
20210 static int
20211 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20213 int arg_quals = cp_type_quals (arg);
20214 int parm_quals = cp_type_quals (parm);
20216 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20217 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20219 /* Although a CVR qualifier is ignored when being applied to a
20220 substituted template parameter ([8.3.2]/1 for example), that
20221 does not allow us to unify "const T" with "int&" because both
20222 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20223 It is ok when we're allowing additional CV qualifiers
20224 at the outer level [14.8.2.1]/3,1st bullet. */
20225 if ((TREE_CODE (arg) == REFERENCE_TYPE
20226 || TREE_CODE (arg) == FUNCTION_TYPE
20227 || TREE_CODE (arg) == METHOD_TYPE)
20228 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20229 return 0;
20231 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20232 && (parm_quals & TYPE_QUAL_RESTRICT))
20233 return 0;
20236 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20237 && (arg_quals & parm_quals) != parm_quals)
20238 return 0;
20240 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20241 && (parm_quals & arg_quals) != arg_quals)
20242 return 0;
20244 return 1;
20247 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20248 void
20249 template_parm_level_and_index (tree parm, int* level, int* index)
20251 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20252 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20253 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20255 *index = TEMPLATE_TYPE_IDX (parm);
20256 *level = TEMPLATE_TYPE_LEVEL (parm);
20258 else
20260 *index = TEMPLATE_PARM_IDX (parm);
20261 *level = TEMPLATE_PARM_LEVEL (parm);
20265 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20266 do { \
20267 if (unify (TP, TA, P, A, S, EP)) \
20268 return 1; \
20269 } while (0)
20271 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20272 expansion at the end of PACKED_PARMS. Returns 0 if the type
20273 deduction succeeds, 1 otherwise. STRICT is the same as in
20274 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20275 function call argument list. We'll need to adjust the arguments to make them
20276 types. SUBR tells us if this is from a recursive call to
20277 type_unification_real, or for comparing two template argument
20278 lists. */
20280 static int
20281 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20282 tree packed_args, unification_kind_t strict,
20283 bool subr, bool explain_p)
20285 tree parm
20286 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20287 tree pattern = PACK_EXPANSION_PATTERN (parm);
20288 tree pack, packs = NULL_TREE;
20289 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20291 /* Add in any args remembered from an earlier partial instantiation. */
20292 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20294 packed_args = expand_template_argument_pack (packed_args);
20296 int len = TREE_VEC_LENGTH (packed_args);
20298 /* Determine the parameter packs we will be deducing from the
20299 pattern, and record their current deductions. */
20300 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20301 pack; pack = TREE_CHAIN (pack))
20303 tree parm_pack = TREE_VALUE (pack);
20304 int idx, level;
20306 /* Determine the index and level of this parameter pack. */
20307 template_parm_level_and_index (parm_pack, &level, &idx);
20309 /* Keep track of the parameter packs and their corresponding
20310 argument packs. */
20311 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20312 TREE_TYPE (packs) = make_tree_vec (len - start);
20315 /* Loop through all of the arguments that have not yet been
20316 unified and unify each with the pattern. */
20317 for (i = start; i < len; i++)
20319 tree parm;
20320 bool any_explicit = false;
20321 tree arg = TREE_VEC_ELT (packed_args, i);
20323 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20324 or the element of its argument pack at the current index if
20325 this argument was explicitly specified. */
20326 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20328 int idx, level;
20329 tree arg, pargs;
20330 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20332 arg = NULL_TREE;
20333 if (TREE_VALUE (pack)
20334 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20335 && (i - start < TREE_VEC_LENGTH (pargs)))
20337 any_explicit = true;
20338 arg = TREE_VEC_ELT (pargs, i - start);
20340 TMPL_ARG (targs, level, idx) = arg;
20343 /* If we had explicit template arguments, substitute them into the
20344 pattern before deduction. */
20345 if (any_explicit)
20347 /* Some arguments might still be unspecified or dependent. */
20348 bool dependent;
20349 ++processing_template_decl;
20350 dependent = any_dependent_template_arguments_p (targs);
20351 if (!dependent)
20352 --processing_template_decl;
20353 parm = tsubst (pattern, targs,
20354 explain_p ? tf_warning_or_error : tf_none,
20355 NULL_TREE);
20356 if (dependent)
20357 --processing_template_decl;
20358 if (parm == error_mark_node)
20359 return 1;
20361 else
20362 parm = pattern;
20364 /* Unify the pattern with the current argument. */
20365 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20366 explain_p))
20367 return 1;
20369 /* For each parameter pack, collect the deduced value. */
20370 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20372 int idx, level;
20373 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20375 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20376 TMPL_ARG (targs, level, idx);
20380 /* Verify that the results of unification with the parameter packs
20381 produce results consistent with what we've seen before, and make
20382 the deduced argument packs available. */
20383 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20385 tree old_pack = TREE_VALUE (pack);
20386 tree new_args = TREE_TYPE (pack);
20387 int i, len = TREE_VEC_LENGTH (new_args);
20388 int idx, level;
20389 bool nondeduced_p = false;
20391 /* By default keep the original deduced argument pack.
20392 If necessary, more specific code is going to update the
20393 resulting deduced argument later down in this function. */
20394 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20395 TMPL_ARG (targs, level, idx) = old_pack;
20397 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20398 actually deduce anything. */
20399 for (i = 0; i < len && !nondeduced_p; ++i)
20400 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20401 nondeduced_p = true;
20402 if (nondeduced_p)
20403 continue;
20405 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20407 /* If we had fewer function args than explicit template args,
20408 just use the explicits. */
20409 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20410 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20411 if (len < explicit_len)
20412 new_args = explicit_args;
20415 if (!old_pack)
20417 tree result;
20418 /* Build the deduced *_ARGUMENT_PACK. */
20419 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20421 result = make_node (NONTYPE_ARGUMENT_PACK);
20422 TREE_CONSTANT (result) = 1;
20424 else
20425 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20427 SET_ARGUMENT_PACK_ARGS (result, new_args);
20429 /* Note the deduced argument packs for this parameter
20430 pack. */
20431 TMPL_ARG (targs, level, idx) = result;
20433 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20434 && (ARGUMENT_PACK_ARGS (old_pack)
20435 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20437 /* We only had the explicitly-provided arguments before, but
20438 now we have a complete set of arguments. */
20439 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20441 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20442 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20443 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20445 else
20447 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20448 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20450 if (!comp_template_args (old_args, new_args,
20451 &bad_old_arg, &bad_new_arg))
20452 /* Inconsistent unification of this parameter pack. */
20453 return unify_parameter_pack_inconsistent (explain_p,
20454 bad_old_arg,
20455 bad_new_arg);
20459 return unify_success (explain_p);
20462 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20463 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20464 parameters and return value are as for unify. */
20466 static int
20467 unify_array_domain (tree tparms, tree targs,
20468 tree parm_dom, tree arg_dom,
20469 bool explain_p)
20471 tree parm_max;
20472 tree arg_max;
20473 bool parm_cst;
20474 bool arg_cst;
20476 /* Our representation of array types uses "N - 1" as the
20477 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20478 not an integer constant. We cannot unify arbitrarily
20479 complex expressions, so we eliminate the MINUS_EXPRs
20480 here. */
20481 parm_max = TYPE_MAX_VALUE (parm_dom);
20482 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20483 if (!parm_cst)
20485 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20486 parm_max = TREE_OPERAND (parm_max, 0);
20488 arg_max = TYPE_MAX_VALUE (arg_dom);
20489 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20490 if (!arg_cst)
20492 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20493 trying to unify the type of a variable with the type
20494 of a template parameter. For example:
20496 template <unsigned int N>
20497 void f (char (&) [N]);
20498 int g();
20499 void h(int i) {
20500 char a[g(i)];
20501 f(a);
20504 Here, the type of the ARG will be "int [g(i)]", and
20505 may be a SAVE_EXPR, etc. */
20506 if (TREE_CODE (arg_max) != MINUS_EXPR)
20507 return unify_vla_arg (explain_p, arg_dom);
20508 arg_max = TREE_OPERAND (arg_max, 0);
20511 /* If only one of the bounds used a MINUS_EXPR, compensate
20512 by adding one to the other bound. */
20513 if (parm_cst && !arg_cst)
20514 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20515 integer_type_node,
20516 parm_max,
20517 integer_one_node);
20518 else if (arg_cst && !parm_cst)
20519 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20520 integer_type_node,
20521 arg_max,
20522 integer_one_node);
20524 return unify (tparms, targs, parm_max, arg_max,
20525 UNIFY_ALLOW_INTEGER, explain_p);
20528 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20530 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20532 static pa_kind_t
20533 pa_kind (tree t)
20535 if (PACK_EXPANSION_P (t))
20536 t = PACK_EXPANSION_PATTERN (t);
20537 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20538 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20539 || DECL_TYPE_TEMPLATE_P (t))
20540 return pa_tmpl;
20541 else if (TYPE_P (t))
20542 return pa_type;
20543 else
20544 return pa_expr;
20547 /* Deduce the value of template parameters. TPARMS is the (innermost)
20548 set of template parameters to a template. TARGS is the bindings
20549 for those template parameters, as determined thus far; TARGS may
20550 include template arguments for outer levels of template parameters
20551 as well. PARM is a parameter to a template function, or a
20552 subcomponent of that parameter; ARG is the corresponding argument.
20553 This function attempts to match PARM with ARG in a manner
20554 consistent with the existing assignments in TARGS. If more values
20555 are deduced, then TARGS is updated.
20557 Returns 0 if the type deduction succeeds, 1 otherwise. The
20558 parameter STRICT is a bitwise or of the following flags:
20560 UNIFY_ALLOW_NONE:
20561 Require an exact match between PARM and ARG.
20562 UNIFY_ALLOW_MORE_CV_QUAL:
20563 Allow the deduced ARG to be more cv-qualified (by qualification
20564 conversion) than ARG.
20565 UNIFY_ALLOW_LESS_CV_QUAL:
20566 Allow the deduced ARG to be less cv-qualified than ARG.
20567 UNIFY_ALLOW_DERIVED:
20568 Allow the deduced ARG to be a template base class of ARG,
20569 or a pointer to a template base class of the type pointed to by
20570 ARG.
20571 UNIFY_ALLOW_INTEGER:
20572 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20573 case for more information.
20574 UNIFY_ALLOW_OUTER_LEVEL:
20575 This is the outermost level of a deduction. Used to determine validity
20576 of qualification conversions. A valid qualification conversion must
20577 have const qualified pointers leading up to the inner type which
20578 requires additional CV quals, except at the outer level, where const
20579 is not required [conv.qual]. It would be normal to set this flag in
20580 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20581 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20582 This is the outermost level of a deduction, and PARM can be more CV
20583 qualified at this point.
20584 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20585 This is the outermost level of a deduction, and PARM can be less CV
20586 qualified at this point. */
20588 static int
20589 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20590 bool explain_p)
20592 int idx;
20593 tree targ;
20594 tree tparm;
20595 int strict_in = strict;
20596 tsubst_flags_t complain = (explain_p
20597 ? tf_warning_or_error
20598 : tf_none);
20600 /* I don't think this will do the right thing with respect to types.
20601 But the only case I've seen it in so far has been array bounds, where
20602 signedness is the only information lost, and I think that will be
20603 okay. */
20604 while (CONVERT_EXPR_P (parm))
20605 parm = TREE_OPERAND (parm, 0);
20607 if (arg == error_mark_node)
20608 return unify_invalid (explain_p);
20609 if (arg == unknown_type_node
20610 || arg == init_list_type_node)
20611 /* We can't deduce anything from this, but we might get all the
20612 template args from other function args. */
20613 return unify_success (explain_p);
20615 if (parm == any_targ_node || arg == any_targ_node)
20616 return unify_success (explain_p);
20618 /* If PARM uses template parameters, then we can't bail out here,
20619 even if ARG == PARM, since we won't record unifications for the
20620 template parameters. We might need them if we're trying to
20621 figure out which of two things is more specialized. */
20622 if (arg == parm && !uses_template_parms (parm))
20623 return unify_success (explain_p);
20625 /* Handle init lists early, so the rest of the function can assume
20626 we're dealing with a type. */
20627 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20629 tree elt, elttype;
20630 unsigned i;
20631 tree orig_parm = parm;
20633 /* Replace T with std::initializer_list<T> for deduction. */
20634 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20635 && flag_deduce_init_list)
20636 parm = listify (parm);
20638 if (!is_std_init_list (parm)
20639 && TREE_CODE (parm) != ARRAY_TYPE)
20640 /* We can only deduce from an initializer list argument if the
20641 parameter is std::initializer_list or an array; otherwise this
20642 is a non-deduced context. */
20643 return unify_success (explain_p);
20645 if (TREE_CODE (parm) == ARRAY_TYPE)
20646 elttype = TREE_TYPE (parm);
20647 else
20649 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20650 /* Deduction is defined in terms of a single type, so just punt
20651 on the (bizarre) std::initializer_list<T...>. */
20652 if (PACK_EXPANSION_P (elttype))
20653 return unify_success (explain_p);
20656 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20658 int elt_strict = strict;
20660 if (elt == error_mark_node)
20661 return unify_invalid (explain_p);
20663 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20665 tree type = TREE_TYPE (elt);
20666 if (type == error_mark_node)
20667 return unify_invalid (explain_p);
20668 /* It should only be possible to get here for a call. */
20669 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20670 elt_strict |= maybe_adjust_types_for_deduction
20671 (DEDUCE_CALL, &elttype, &type, elt);
20672 elt = type;
20675 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20676 explain_p);
20679 if (TREE_CODE (parm) == ARRAY_TYPE
20680 && deducible_array_bound (TYPE_DOMAIN (parm)))
20682 /* Also deduce from the length of the initializer list. */
20683 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20684 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20685 if (idx == error_mark_node)
20686 return unify_invalid (explain_p);
20687 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20688 idx, explain_p);
20691 /* If the std::initializer_list<T> deduction worked, replace the
20692 deduced A with std::initializer_list<A>. */
20693 if (orig_parm != parm)
20695 idx = TEMPLATE_TYPE_IDX (orig_parm);
20696 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20697 targ = listify (targ);
20698 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20700 return unify_success (explain_p);
20703 /* If parm and arg aren't the same kind of thing (template, type, or
20704 expression), fail early. */
20705 if (pa_kind (parm) != pa_kind (arg))
20706 return unify_invalid (explain_p);
20708 /* Immediately reject some pairs that won't unify because of
20709 cv-qualification mismatches. */
20710 if (TREE_CODE (arg) == TREE_CODE (parm)
20711 && TYPE_P (arg)
20712 /* It is the elements of the array which hold the cv quals of an array
20713 type, and the elements might be template type parms. We'll check
20714 when we recurse. */
20715 && TREE_CODE (arg) != ARRAY_TYPE
20716 /* We check the cv-qualifiers when unifying with template type
20717 parameters below. We want to allow ARG `const T' to unify with
20718 PARM `T' for example, when computing which of two templates
20719 is more specialized, for example. */
20720 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20721 && !check_cv_quals_for_unify (strict_in, arg, parm))
20722 return unify_cv_qual_mismatch (explain_p, parm, arg);
20724 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20725 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20726 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20727 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20728 strict &= ~UNIFY_ALLOW_DERIVED;
20729 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20730 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20732 switch (TREE_CODE (parm))
20734 case TYPENAME_TYPE:
20735 case SCOPE_REF:
20736 case UNBOUND_CLASS_TEMPLATE:
20737 /* In a type which contains a nested-name-specifier, template
20738 argument values cannot be deduced for template parameters used
20739 within the nested-name-specifier. */
20740 return unify_success (explain_p);
20742 case TEMPLATE_TYPE_PARM:
20743 case TEMPLATE_TEMPLATE_PARM:
20744 case BOUND_TEMPLATE_TEMPLATE_PARM:
20745 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20746 if (error_operand_p (tparm))
20747 return unify_invalid (explain_p);
20749 if (TEMPLATE_TYPE_LEVEL (parm)
20750 != template_decl_level (tparm))
20751 /* The PARM is not one we're trying to unify. Just check
20752 to see if it matches ARG. */
20754 if (TREE_CODE (arg) == TREE_CODE (parm)
20755 && (is_auto (parm) ? is_auto (arg)
20756 : same_type_p (parm, arg)))
20757 return unify_success (explain_p);
20758 else
20759 return unify_type_mismatch (explain_p, parm, arg);
20761 idx = TEMPLATE_TYPE_IDX (parm);
20762 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20763 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20764 if (error_operand_p (tparm))
20765 return unify_invalid (explain_p);
20767 /* Check for mixed types and values. */
20768 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20769 && TREE_CODE (tparm) != TYPE_DECL)
20770 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20771 && TREE_CODE (tparm) != TEMPLATE_DECL))
20772 gcc_unreachable ();
20774 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20776 if ((strict_in & UNIFY_ALLOW_DERIVED)
20777 && CLASS_TYPE_P (arg))
20779 /* First try to match ARG directly. */
20780 tree t = try_class_unification (tparms, targs, parm, arg,
20781 explain_p);
20782 if (!t)
20784 /* Otherwise, look for a suitable base of ARG, as below. */
20785 enum template_base_result r;
20786 r = get_template_base (tparms, targs, parm, arg,
20787 explain_p, &t);
20788 if (!t)
20789 return unify_no_common_base (explain_p, r, parm, arg);
20790 arg = t;
20793 /* ARG must be constructed from a template class or a template
20794 template parameter. */
20795 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20796 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20797 return unify_template_deduction_failure (explain_p, parm, arg);
20799 /* Deduce arguments T, i from TT<T> or TT<i>. */
20800 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20801 return 1;
20803 arg = TYPE_TI_TEMPLATE (arg);
20805 /* Fall through to deduce template name. */
20808 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20809 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20811 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20813 /* Simple cases: Value already set, does match or doesn't. */
20814 if (targ != NULL_TREE && template_args_equal (targ, arg))
20815 return unify_success (explain_p);
20816 else if (targ)
20817 return unify_inconsistency (explain_p, parm, targ, arg);
20819 else
20821 /* If PARM is `const T' and ARG is only `int', we don't have
20822 a match unless we are allowing additional qualification.
20823 If ARG is `const int' and PARM is just `T' that's OK;
20824 that binds `const int' to `T'. */
20825 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20826 arg, parm))
20827 return unify_cv_qual_mismatch (explain_p, parm, arg);
20829 /* Consider the case where ARG is `const volatile int' and
20830 PARM is `const T'. Then, T should be `volatile int'. */
20831 arg = cp_build_qualified_type_real
20832 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20833 if (arg == error_mark_node)
20834 return unify_invalid (explain_p);
20836 /* Simple cases: Value already set, does match or doesn't. */
20837 if (targ != NULL_TREE && same_type_p (targ, arg))
20838 return unify_success (explain_p);
20839 else if (targ)
20840 return unify_inconsistency (explain_p, parm, targ, arg);
20842 /* Make sure that ARG is not a variable-sized array. (Note
20843 that were talking about variable-sized arrays (like
20844 `int[n]'), rather than arrays of unknown size (like
20845 `int[]').) We'll get very confused by such a type since
20846 the bound of the array is not constant, and therefore
20847 not mangleable. Besides, such types are not allowed in
20848 ISO C++, so we can do as we please here. We do allow
20849 them for 'auto' deduction, since that isn't ABI-exposed. */
20850 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20851 return unify_vla_arg (explain_p, arg);
20853 /* Strip typedefs as in convert_template_argument. */
20854 arg = canonicalize_type_argument (arg, tf_none);
20857 /* If ARG is a parameter pack or an expansion, we cannot unify
20858 against it unless PARM is also a parameter pack. */
20859 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20860 && !template_parameter_pack_p (parm))
20861 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20863 /* If the argument deduction results is a METHOD_TYPE,
20864 then there is a problem.
20865 METHOD_TYPE doesn't map to any real C++ type the result of
20866 the deduction can not be of that type. */
20867 if (TREE_CODE (arg) == METHOD_TYPE)
20868 return unify_method_type_error (explain_p, arg);
20870 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20871 return unify_success (explain_p);
20873 case TEMPLATE_PARM_INDEX:
20874 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20875 if (error_operand_p (tparm))
20876 return unify_invalid (explain_p);
20878 if (TEMPLATE_PARM_LEVEL (parm)
20879 != template_decl_level (tparm))
20881 /* The PARM is not one we're trying to unify. Just check
20882 to see if it matches ARG. */
20883 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20884 && cp_tree_equal (parm, arg));
20885 if (result)
20886 unify_expression_unequal (explain_p, parm, arg);
20887 return result;
20890 idx = TEMPLATE_PARM_IDX (parm);
20891 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20893 if (targ)
20895 int x = !cp_tree_equal (targ, arg);
20896 if (x)
20897 unify_inconsistency (explain_p, parm, targ, arg);
20898 return x;
20901 /* [temp.deduct.type] If, in the declaration of a function template
20902 with a non-type template-parameter, the non-type
20903 template-parameter is used in an expression in the function
20904 parameter-list and, if the corresponding template-argument is
20905 deduced, the template-argument type shall match the type of the
20906 template-parameter exactly, except that a template-argument
20907 deduced from an array bound may be of any integral type.
20908 The non-type parameter might use already deduced type parameters. */
20909 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20910 if (tree a = type_uses_auto (tparm))
20912 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20913 if (tparm == error_mark_node)
20914 return 1;
20917 if (!TREE_TYPE (arg))
20918 /* Template-parameter dependent expression. Just accept it for now.
20919 It will later be processed in convert_template_argument. */
20921 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20922 non_reference (tparm)))
20923 /* OK */;
20924 else if ((strict & UNIFY_ALLOW_INTEGER)
20925 && CP_INTEGRAL_TYPE_P (tparm))
20926 /* Convert the ARG to the type of PARM; the deduced non-type
20927 template argument must exactly match the types of the
20928 corresponding parameter. */
20929 arg = fold (build_nop (tparm, arg));
20930 else if (uses_template_parms (tparm))
20932 /* We haven't deduced the type of this parameter yet. */
20933 if (cxx_dialect >= cxx17
20934 /* We deduce from array bounds in try_array_deduction. */
20935 && !(strict & UNIFY_ALLOW_INTEGER))
20937 /* Deduce it from the non-type argument. */
20938 tree atype = TREE_TYPE (arg);
20939 RECUR_AND_CHECK_FAILURE (tparms, targs,
20940 tparm, atype,
20941 UNIFY_ALLOW_NONE, explain_p);
20943 else
20944 /* Try again later. */
20945 return unify_success (explain_p);
20947 else
20948 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20950 /* If ARG is a parameter pack or an expansion, we cannot unify
20951 against it unless PARM is also a parameter pack. */
20952 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20953 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20954 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20957 bool removed_attr = false;
20958 arg = strip_typedefs_expr (arg, &removed_attr);
20960 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20961 return unify_success (explain_p);
20963 case PTRMEM_CST:
20965 /* A pointer-to-member constant can be unified only with
20966 another constant. */
20967 if (TREE_CODE (arg) != PTRMEM_CST)
20968 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20970 /* Just unify the class member. It would be useless (and possibly
20971 wrong, depending on the strict flags) to unify also
20972 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20973 arg refer to the same variable, even if through different
20974 classes. For instance:
20976 struct A { int x; };
20977 struct B : A { };
20979 Unification of &A::x and &B::x must succeed. */
20980 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20981 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20984 case POINTER_TYPE:
20986 if (!TYPE_PTR_P (arg))
20987 return unify_type_mismatch (explain_p, parm, arg);
20989 /* [temp.deduct.call]
20991 A can be another pointer or pointer to member type that can
20992 be converted to the deduced A via a qualification
20993 conversion (_conv.qual_).
20995 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20996 This will allow for additional cv-qualification of the
20997 pointed-to types if appropriate. */
20999 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21000 /* The derived-to-base conversion only persists through one
21001 level of pointers. */
21002 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21004 return unify (tparms, targs, TREE_TYPE (parm),
21005 TREE_TYPE (arg), strict, explain_p);
21008 case REFERENCE_TYPE:
21009 if (TREE_CODE (arg) != REFERENCE_TYPE)
21010 return unify_type_mismatch (explain_p, parm, arg);
21011 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21012 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21014 case ARRAY_TYPE:
21015 if (TREE_CODE (arg) != ARRAY_TYPE)
21016 return unify_type_mismatch (explain_p, parm, arg);
21017 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21018 != (TYPE_DOMAIN (arg) == NULL_TREE))
21019 return unify_type_mismatch (explain_p, parm, arg);
21020 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21021 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21022 if (TYPE_DOMAIN (parm) != NULL_TREE)
21023 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21024 TYPE_DOMAIN (arg), explain_p);
21025 return unify_success (explain_p);
21027 case REAL_TYPE:
21028 case COMPLEX_TYPE:
21029 case VECTOR_TYPE:
21030 case INTEGER_TYPE:
21031 case BOOLEAN_TYPE:
21032 case ENUMERAL_TYPE:
21033 case VOID_TYPE:
21034 case NULLPTR_TYPE:
21035 if (TREE_CODE (arg) != TREE_CODE (parm))
21036 return unify_type_mismatch (explain_p, parm, arg);
21038 /* We have already checked cv-qualification at the top of the
21039 function. */
21040 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21041 return unify_type_mismatch (explain_p, parm, arg);
21043 /* As far as unification is concerned, this wins. Later checks
21044 will invalidate it if necessary. */
21045 return unify_success (explain_p);
21047 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21048 /* Type INTEGER_CST can come from ordinary constant template args. */
21049 case INTEGER_CST:
21050 while (CONVERT_EXPR_P (arg))
21051 arg = TREE_OPERAND (arg, 0);
21053 if (TREE_CODE (arg) != INTEGER_CST)
21054 return unify_template_argument_mismatch (explain_p, parm, arg);
21055 return (tree_int_cst_equal (parm, arg)
21056 ? unify_success (explain_p)
21057 : unify_template_argument_mismatch (explain_p, parm, arg));
21059 case TREE_VEC:
21061 int i, len, argslen;
21062 int parm_variadic_p = 0;
21064 if (TREE_CODE (arg) != TREE_VEC)
21065 return unify_template_argument_mismatch (explain_p, parm, arg);
21067 len = TREE_VEC_LENGTH (parm);
21068 argslen = TREE_VEC_LENGTH (arg);
21070 /* Check for pack expansions in the parameters. */
21071 for (i = 0; i < len; ++i)
21073 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21075 if (i == len - 1)
21076 /* We can unify against something with a trailing
21077 parameter pack. */
21078 parm_variadic_p = 1;
21079 else
21080 /* [temp.deduct.type]/9: If the template argument list of
21081 P contains a pack expansion that is not the last
21082 template argument, the entire template argument list
21083 is a non-deduced context. */
21084 return unify_success (explain_p);
21088 /* If we don't have enough arguments to satisfy the parameters
21089 (not counting the pack expression at the end), or we have
21090 too many arguments for a parameter list that doesn't end in
21091 a pack expression, we can't unify. */
21092 if (parm_variadic_p
21093 ? argslen < len - parm_variadic_p
21094 : argslen != len)
21095 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21097 /* Unify all of the parameters that precede the (optional)
21098 pack expression. */
21099 for (i = 0; i < len - parm_variadic_p; ++i)
21101 RECUR_AND_CHECK_FAILURE (tparms, targs,
21102 TREE_VEC_ELT (parm, i),
21103 TREE_VEC_ELT (arg, i),
21104 UNIFY_ALLOW_NONE, explain_p);
21106 if (parm_variadic_p)
21107 return unify_pack_expansion (tparms, targs, parm, arg,
21108 DEDUCE_EXACT,
21109 /*subr=*/true, explain_p);
21110 return unify_success (explain_p);
21113 case RECORD_TYPE:
21114 case UNION_TYPE:
21115 if (TREE_CODE (arg) != TREE_CODE (parm))
21116 return unify_type_mismatch (explain_p, parm, arg);
21118 if (TYPE_PTRMEMFUNC_P (parm))
21120 if (!TYPE_PTRMEMFUNC_P (arg))
21121 return unify_type_mismatch (explain_p, parm, arg);
21123 return unify (tparms, targs,
21124 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21125 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21126 strict, explain_p);
21128 else if (TYPE_PTRMEMFUNC_P (arg))
21129 return unify_type_mismatch (explain_p, parm, arg);
21131 if (CLASSTYPE_TEMPLATE_INFO (parm))
21133 tree t = NULL_TREE;
21135 if (strict_in & UNIFY_ALLOW_DERIVED)
21137 /* First, we try to unify the PARM and ARG directly. */
21138 t = try_class_unification (tparms, targs,
21139 parm, arg, explain_p);
21141 if (!t)
21143 /* Fallback to the special case allowed in
21144 [temp.deduct.call]:
21146 If P is a class, and P has the form
21147 template-id, then A can be a derived class of
21148 the deduced A. Likewise, if P is a pointer to
21149 a class of the form template-id, A can be a
21150 pointer to a derived class pointed to by the
21151 deduced A. */
21152 enum template_base_result r;
21153 r = get_template_base (tparms, targs, parm, arg,
21154 explain_p, &t);
21156 if (!t)
21158 /* Don't give the derived diagnostic if we're
21159 already dealing with the same template. */
21160 bool same_template
21161 = (CLASSTYPE_TEMPLATE_INFO (arg)
21162 && (CLASSTYPE_TI_TEMPLATE (parm)
21163 == CLASSTYPE_TI_TEMPLATE (arg)));
21164 return unify_no_common_base (explain_p && !same_template,
21165 r, parm, arg);
21169 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21170 && (CLASSTYPE_TI_TEMPLATE (parm)
21171 == CLASSTYPE_TI_TEMPLATE (arg)))
21172 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21173 Then, we should unify `int' and `U'. */
21174 t = arg;
21175 else
21176 /* There's no chance of unification succeeding. */
21177 return unify_type_mismatch (explain_p, parm, arg);
21179 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21180 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21182 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21183 return unify_type_mismatch (explain_p, parm, arg);
21184 return unify_success (explain_p);
21186 case METHOD_TYPE:
21187 case FUNCTION_TYPE:
21189 unsigned int nargs;
21190 tree *args;
21191 tree a;
21192 unsigned int i;
21194 if (TREE_CODE (arg) != TREE_CODE (parm))
21195 return unify_type_mismatch (explain_p, parm, arg);
21197 /* CV qualifications for methods can never be deduced, they must
21198 match exactly. We need to check them explicitly here,
21199 because type_unification_real treats them as any other
21200 cv-qualified parameter. */
21201 if (TREE_CODE (parm) == METHOD_TYPE
21202 && (!check_cv_quals_for_unify
21203 (UNIFY_ALLOW_NONE,
21204 class_of_this_parm (arg),
21205 class_of_this_parm (parm))))
21206 return unify_cv_qual_mismatch (explain_p, parm, arg);
21207 if (TREE_CODE (arg) == FUNCTION_TYPE
21208 && type_memfn_quals (parm) != type_memfn_quals (arg))
21209 return unify_cv_qual_mismatch (explain_p, parm, arg);
21210 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21211 return unify_type_mismatch (explain_p, parm, arg);
21213 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21214 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21216 nargs = list_length (TYPE_ARG_TYPES (arg));
21217 args = XALLOCAVEC (tree, nargs);
21218 for (a = TYPE_ARG_TYPES (arg), i = 0;
21219 a != NULL_TREE && a != void_list_node;
21220 a = TREE_CHAIN (a), ++i)
21221 args[i] = TREE_VALUE (a);
21222 nargs = i;
21224 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21225 args, nargs, 1, DEDUCE_EXACT,
21226 LOOKUP_NORMAL, NULL, explain_p))
21227 return 1;
21229 if (flag_noexcept_type)
21231 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21232 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21233 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21234 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21235 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21236 && uses_template_parms (TREE_PURPOSE (pspec)))
21237 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21238 TREE_PURPOSE (aspec),
21239 UNIFY_ALLOW_NONE, explain_p);
21240 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21241 return unify_type_mismatch (explain_p, parm, arg);
21244 return 0;
21247 case OFFSET_TYPE:
21248 /* Unify a pointer to member with a pointer to member function, which
21249 deduces the type of the member as a function type. */
21250 if (TYPE_PTRMEMFUNC_P (arg))
21252 /* Check top-level cv qualifiers */
21253 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21254 return unify_cv_qual_mismatch (explain_p, parm, arg);
21256 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21257 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21258 UNIFY_ALLOW_NONE, explain_p);
21260 /* Determine the type of the function we are unifying against. */
21261 tree fntype = static_fn_type (arg);
21263 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21266 if (TREE_CODE (arg) != OFFSET_TYPE)
21267 return unify_type_mismatch (explain_p, parm, arg);
21268 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21269 TYPE_OFFSET_BASETYPE (arg),
21270 UNIFY_ALLOW_NONE, explain_p);
21271 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21272 strict, explain_p);
21274 case CONST_DECL:
21275 if (DECL_TEMPLATE_PARM_P (parm))
21276 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21277 if (arg != scalar_constant_value (parm))
21278 return unify_template_argument_mismatch (explain_p, parm, arg);
21279 return unify_success (explain_p);
21281 case FIELD_DECL:
21282 case TEMPLATE_DECL:
21283 /* Matched cases are handled by the ARG == PARM test above. */
21284 return unify_template_argument_mismatch (explain_p, parm, arg);
21286 case VAR_DECL:
21287 /* We might get a variable as a non-type template argument in parm if the
21288 corresponding parameter is type-dependent. Make any necessary
21289 adjustments based on whether arg is a reference. */
21290 if (CONSTANT_CLASS_P (arg))
21291 parm = fold_non_dependent_expr (parm);
21292 else if (REFERENCE_REF_P (arg))
21294 tree sub = TREE_OPERAND (arg, 0);
21295 STRIP_NOPS (sub);
21296 if (TREE_CODE (sub) == ADDR_EXPR)
21297 arg = TREE_OPERAND (sub, 0);
21299 /* Now use the normal expression code to check whether they match. */
21300 goto expr;
21302 case TYPE_ARGUMENT_PACK:
21303 case NONTYPE_ARGUMENT_PACK:
21304 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21305 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21307 case TYPEOF_TYPE:
21308 case DECLTYPE_TYPE:
21309 case UNDERLYING_TYPE:
21310 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21311 or UNDERLYING_TYPE nodes. */
21312 return unify_success (explain_p);
21314 case ERROR_MARK:
21315 /* Unification fails if we hit an error node. */
21316 return unify_invalid (explain_p);
21318 case INDIRECT_REF:
21319 if (REFERENCE_REF_P (parm))
21321 bool pexp = PACK_EXPANSION_P (arg);
21322 if (pexp)
21323 arg = PACK_EXPANSION_PATTERN (arg);
21324 if (REFERENCE_REF_P (arg))
21325 arg = TREE_OPERAND (arg, 0);
21326 if (pexp)
21327 arg = make_pack_expansion (arg);
21328 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21329 strict, explain_p);
21331 /* FALLTHRU */
21333 default:
21334 /* An unresolved overload is a nondeduced context. */
21335 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21336 return unify_success (explain_p);
21337 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21338 expr:
21339 /* We must be looking at an expression. This can happen with
21340 something like:
21342 template <int I>
21343 void foo(S<I>, S<I + 2>);
21345 This is a "nondeduced context":
21347 [deduct.type]
21349 The nondeduced contexts are:
21351 --A type that is a template-id in which one or more of
21352 the template-arguments is an expression that references
21353 a template-parameter.
21355 In these cases, we assume deduction succeeded, but don't
21356 actually infer any unifications. */
21358 if (!uses_template_parms (parm)
21359 && !template_args_equal (parm, arg))
21360 return unify_expression_unequal (explain_p, parm, arg);
21361 else
21362 return unify_success (explain_p);
21365 #undef RECUR_AND_CHECK_FAILURE
21367 /* Note that DECL can be defined in this translation unit, if
21368 required. */
21370 static void
21371 mark_definable (tree decl)
21373 tree clone;
21374 DECL_NOT_REALLY_EXTERN (decl) = 1;
21375 FOR_EACH_CLONE (clone, decl)
21376 DECL_NOT_REALLY_EXTERN (clone) = 1;
21379 /* Called if RESULT is explicitly instantiated, or is a member of an
21380 explicitly instantiated class. */
21382 void
21383 mark_decl_instantiated (tree result, int extern_p)
21385 SET_DECL_EXPLICIT_INSTANTIATION (result);
21387 /* If this entity has already been written out, it's too late to
21388 make any modifications. */
21389 if (TREE_ASM_WRITTEN (result))
21390 return;
21392 /* For anonymous namespace we don't need to do anything. */
21393 if (decl_anon_ns_mem_p (result))
21395 gcc_assert (!TREE_PUBLIC (result));
21396 return;
21399 if (TREE_CODE (result) != FUNCTION_DECL)
21400 /* The TREE_PUBLIC flag for function declarations will have been
21401 set correctly by tsubst. */
21402 TREE_PUBLIC (result) = 1;
21404 /* This might have been set by an earlier implicit instantiation. */
21405 DECL_COMDAT (result) = 0;
21407 if (extern_p)
21408 DECL_NOT_REALLY_EXTERN (result) = 0;
21409 else
21411 mark_definable (result);
21412 mark_needed (result);
21413 /* Always make artificials weak. */
21414 if (DECL_ARTIFICIAL (result) && flag_weak)
21415 comdat_linkage (result);
21416 /* For WIN32 we also want to put explicit instantiations in
21417 linkonce sections. */
21418 else if (TREE_PUBLIC (result))
21419 maybe_make_one_only (result);
21422 /* If EXTERN_P, then this function will not be emitted -- unless
21423 followed by an explicit instantiation, at which point its linkage
21424 will be adjusted. If !EXTERN_P, then this function will be
21425 emitted here. In neither circumstance do we want
21426 import_export_decl to adjust the linkage. */
21427 DECL_INTERFACE_KNOWN (result) = 1;
21430 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21431 important template arguments. If any are missing, we check whether
21432 they're important by using error_mark_node for substituting into any
21433 args that were used for partial ordering (the ones between ARGS and END)
21434 and seeing if it bubbles up. */
21436 static bool
21437 check_undeduced_parms (tree targs, tree args, tree end)
21439 bool found = false;
21440 int i;
21441 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21442 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21444 found = true;
21445 TREE_VEC_ELT (targs, i) = error_mark_node;
21447 if (found)
21449 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21450 if (substed == error_mark_node)
21451 return true;
21453 return false;
21456 /* Given two function templates PAT1 and PAT2, return:
21458 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21459 -1 if PAT2 is more specialized than PAT1.
21460 0 if neither is more specialized.
21462 LEN indicates the number of parameters we should consider
21463 (defaulted parameters should not be considered).
21465 The 1998 std underspecified function template partial ordering, and
21466 DR214 addresses the issue. We take pairs of arguments, one from
21467 each of the templates, and deduce them against each other. One of
21468 the templates will be more specialized if all the *other*
21469 template's arguments deduce against its arguments and at least one
21470 of its arguments *does* *not* deduce against the other template's
21471 corresponding argument. Deduction is done as for class templates.
21472 The arguments used in deduction have reference and top level cv
21473 qualifiers removed. Iff both arguments were originally reference
21474 types *and* deduction succeeds in both directions, an lvalue reference
21475 wins against an rvalue reference and otherwise the template
21476 with the more cv-qualified argument wins for that pairing (if
21477 neither is more cv-qualified, they both are equal). Unlike regular
21478 deduction, after all the arguments have been deduced in this way,
21479 we do *not* verify the deduced template argument values can be
21480 substituted into non-deduced contexts.
21482 The logic can be a bit confusing here, because we look at deduce1 and
21483 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21484 can find template arguments for pat1 to make arg1 look like arg2, that
21485 means that arg2 is at least as specialized as arg1. */
21488 more_specialized_fn (tree pat1, tree pat2, int len)
21490 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21491 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21492 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21493 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21494 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21495 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21496 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21497 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21498 tree origs1, origs2;
21499 bool lose1 = false;
21500 bool lose2 = false;
21502 /* Remove the this parameter from non-static member functions. If
21503 one is a non-static member function and the other is not a static
21504 member function, remove the first parameter from that function
21505 also. This situation occurs for operator functions where we
21506 locate both a member function (with this pointer) and non-member
21507 operator (with explicit first operand). */
21508 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21510 len--; /* LEN is the number of significant arguments for DECL1 */
21511 args1 = TREE_CHAIN (args1);
21512 if (!DECL_STATIC_FUNCTION_P (decl2))
21513 args2 = TREE_CHAIN (args2);
21515 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21517 args2 = TREE_CHAIN (args2);
21518 if (!DECL_STATIC_FUNCTION_P (decl1))
21520 len--;
21521 args1 = TREE_CHAIN (args1);
21525 /* If only one is a conversion operator, they are unordered. */
21526 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21527 return 0;
21529 /* Consider the return type for a conversion function */
21530 if (DECL_CONV_FN_P (decl1))
21532 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21533 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21534 len++;
21537 processing_template_decl++;
21539 origs1 = args1;
21540 origs2 = args2;
21542 while (len--
21543 /* Stop when an ellipsis is seen. */
21544 && args1 != NULL_TREE && args2 != NULL_TREE)
21546 tree arg1 = TREE_VALUE (args1);
21547 tree arg2 = TREE_VALUE (args2);
21548 int deduce1, deduce2;
21549 int quals1 = -1;
21550 int quals2 = -1;
21551 int ref1 = 0;
21552 int ref2 = 0;
21554 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21555 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21557 /* When both arguments are pack expansions, we need only
21558 unify the patterns themselves. */
21559 arg1 = PACK_EXPANSION_PATTERN (arg1);
21560 arg2 = PACK_EXPANSION_PATTERN (arg2);
21562 /* This is the last comparison we need to do. */
21563 len = 0;
21566 /* DR 1847: If a particular P contains no template-parameters that
21567 participate in template argument deduction, that P is not used to
21568 determine the ordering. */
21569 if (!uses_deducible_template_parms (arg1)
21570 && !uses_deducible_template_parms (arg2))
21571 goto next;
21573 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21575 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21576 arg1 = TREE_TYPE (arg1);
21577 quals1 = cp_type_quals (arg1);
21580 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21582 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21583 arg2 = TREE_TYPE (arg2);
21584 quals2 = cp_type_quals (arg2);
21587 arg1 = TYPE_MAIN_VARIANT (arg1);
21588 arg2 = TYPE_MAIN_VARIANT (arg2);
21590 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21592 int i, len2 = remaining_arguments (args2);
21593 tree parmvec = make_tree_vec (1);
21594 tree argvec = make_tree_vec (len2);
21595 tree ta = args2;
21597 /* Setup the parameter vector, which contains only ARG1. */
21598 TREE_VEC_ELT (parmvec, 0) = arg1;
21600 /* Setup the argument vector, which contains the remaining
21601 arguments. */
21602 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21603 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21605 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21606 argvec, DEDUCE_EXACT,
21607 /*subr=*/true, /*explain_p=*/false)
21608 == 0);
21610 /* We cannot deduce in the other direction, because ARG1 is
21611 a pack expansion but ARG2 is not. */
21612 deduce2 = 0;
21614 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21616 int i, len1 = remaining_arguments (args1);
21617 tree parmvec = make_tree_vec (1);
21618 tree argvec = make_tree_vec (len1);
21619 tree ta = args1;
21621 /* Setup the parameter vector, which contains only ARG1. */
21622 TREE_VEC_ELT (parmvec, 0) = arg2;
21624 /* Setup the argument vector, which contains the remaining
21625 arguments. */
21626 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21627 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21629 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21630 argvec, DEDUCE_EXACT,
21631 /*subr=*/true, /*explain_p=*/false)
21632 == 0);
21634 /* We cannot deduce in the other direction, because ARG2 is
21635 a pack expansion but ARG1 is not.*/
21636 deduce1 = 0;
21639 else
21641 /* The normal case, where neither argument is a pack
21642 expansion. */
21643 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21644 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21645 == 0);
21646 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21647 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21648 == 0);
21651 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21652 arg2, then arg2 is not as specialized as arg1. */
21653 if (!deduce1)
21654 lose2 = true;
21655 if (!deduce2)
21656 lose1 = true;
21658 /* "If, for a given type, deduction succeeds in both directions
21659 (i.e., the types are identical after the transformations above)
21660 and both P and A were reference types (before being replaced with
21661 the type referred to above):
21662 - if the type from the argument template was an lvalue reference and
21663 the type from the parameter template was not, the argument type is
21664 considered to be more specialized than the other; otherwise,
21665 - if the type from the argument template is more cv-qualified
21666 than the type from the parameter template (as described above),
21667 the argument type is considered to be more specialized than the other;
21668 otherwise,
21669 - neither type is more specialized than the other." */
21671 if (deduce1 && deduce2)
21673 if (ref1 && ref2 && ref1 != ref2)
21675 if (ref1 > ref2)
21676 lose1 = true;
21677 else
21678 lose2 = true;
21680 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21682 if ((quals1 & quals2) == quals2)
21683 lose2 = true;
21684 if ((quals1 & quals2) == quals1)
21685 lose1 = true;
21689 if (lose1 && lose2)
21690 /* We've failed to deduce something in either direction.
21691 These must be unordered. */
21692 break;
21694 next:
21696 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21697 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21698 /* We have already processed all of the arguments in our
21699 handing of the pack expansion type. */
21700 len = 0;
21702 args1 = TREE_CHAIN (args1);
21703 args2 = TREE_CHAIN (args2);
21706 /* "In most cases, all template parameters must have values in order for
21707 deduction to succeed, but for partial ordering purposes a template
21708 parameter may remain without a value provided it is not used in the
21709 types being used for partial ordering."
21711 Thus, if we are missing any of the targs1 we need to substitute into
21712 origs1, then pat2 is not as specialized as pat1. This can happen when
21713 there is a nondeduced context. */
21714 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21715 lose2 = true;
21716 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21717 lose1 = true;
21719 processing_template_decl--;
21721 /* If both deductions succeed, the partial ordering selects the more
21722 constrained template. */
21723 if (!lose1 && !lose2)
21725 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21726 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21727 lose1 = !subsumes_constraints (c1, c2);
21728 lose2 = !subsumes_constraints (c2, c1);
21731 /* All things being equal, if the next argument is a pack expansion
21732 for one function but not for the other, prefer the
21733 non-variadic function. FIXME this is bogus; see c++/41958. */
21734 if (lose1 == lose2
21735 && args1 && TREE_VALUE (args1)
21736 && args2 && TREE_VALUE (args2))
21738 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21739 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21742 if (lose1 == lose2)
21743 return 0;
21744 else if (!lose1)
21745 return 1;
21746 else
21747 return -1;
21750 /* Determine which of two partial specializations of TMPL is more
21751 specialized.
21753 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21754 to the first partial specialization. The TREE_PURPOSE is the
21755 innermost set of template parameters for the partial
21756 specialization. PAT2 is similar, but for the second template.
21758 Return 1 if the first partial specialization is more specialized;
21759 -1 if the second is more specialized; 0 if neither is more
21760 specialized.
21762 See [temp.class.order] for information about determining which of
21763 two templates is more specialized. */
21765 static int
21766 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21768 tree targs;
21769 int winner = 0;
21770 bool any_deductions = false;
21772 tree tmpl1 = TREE_VALUE (pat1);
21773 tree tmpl2 = TREE_VALUE (pat2);
21774 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21775 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21777 /* Just like what happens for functions, if we are ordering between
21778 different template specializations, we may encounter dependent
21779 types in the arguments, and we need our dependency check functions
21780 to behave correctly. */
21781 ++processing_template_decl;
21782 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21783 if (targs)
21785 --winner;
21786 any_deductions = true;
21789 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21790 if (targs)
21792 ++winner;
21793 any_deductions = true;
21795 --processing_template_decl;
21797 /* If both deductions succeed, the partial ordering selects the more
21798 constrained template. */
21799 if (!winner && any_deductions)
21800 return more_constrained (tmpl1, tmpl2);
21802 /* In the case of a tie where at least one of the templates
21803 has a parameter pack at the end, the template with the most
21804 non-packed parameters wins. */
21805 if (winner == 0
21806 && any_deductions
21807 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21808 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21810 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21811 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21812 int len1 = TREE_VEC_LENGTH (args1);
21813 int len2 = TREE_VEC_LENGTH (args2);
21815 /* We don't count the pack expansion at the end. */
21816 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21817 --len1;
21818 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21819 --len2;
21821 if (len1 > len2)
21822 return 1;
21823 else if (len1 < len2)
21824 return -1;
21827 return winner;
21830 /* Return the template arguments that will produce the function signature
21831 DECL from the function template FN, with the explicit template
21832 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21833 also match. Return NULL_TREE if no satisfactory arguments could be
21834 found. */
21836 static tree
21837 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21839 int ntparms = DECL_NTPARMS (fn);
21840 tree targs = make_tree_vec (ntparms);
21841 tree decl_type = TREE_TYPE (decl);
21842 tree decl_arg_types;
21843 tree *args;
21844 unsigned int nargs, ix;
21845 tree arg;
21847 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21849 /* Never do unification on the 'this' parameter. */
21850 decl_arg_types = skip_artificial_parms_for (decl,
21851 TYPE_ARG_TYPES (decl_type));
21853 nargs = list_length (decl_arg_types);
21854 args = XALLOCAVEC (tree, nargs);
21855 for (arg = decl_arg_types, ix = 0;
21856 arg != NULL_TREE && arg != void_list_node;
21857 arg = TREE_CHAIN (arg), ++ix)
21858 args[ix] = TREE_VALUE (arg);
21860 if (fn_type_unification (fn, explicit_args, targs,
21861 args, ix,
21862 (check_rettype || DECL_CONV_FN_P (fn)
21863 ? TREE_TYPE (decl_type) : NULL_TREE),
21864 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21865 /*decltype*/false)
21866 == error_mark_node)
21867 return NULL_TREE;
21869 return targs;
21872 /* Return the innermost template arguments that, when applied to a partial
21873 specialization SPEC_TMPL of TMPL, yield the ARGS.
21875 For example, suppose we have:
21877 template <class T, class U> struct S {};
21878 template <class T> struct S<T*, int> {};
21880 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21881 partial specialization and the ARGS will be {double*, int}. The resulting
21882 vector will be {double}, indicating that `T' is bound to `double'. */
21884 static tree
21885 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21887 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21888 tree spec_args
21889 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21890 int i, ntparms = TREE_VEC_LENGTH (tparms);
21891 tree deduced_args;
21892 tree innermost_deduced_args;
21894 innermost_deduced_args = make_tree_vec (ntparms);
21895 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21897 deduced_args = copy_node (args);
21898 SET_TMPL_ARGS_LEVEL (deduced_args,
21899 TMPL_ARGS_DEPTH (deduced_args),
21900 innermost_deduced_args);
21902 else
21903 deduced_args = innermost_deduced_args;
21905 bool tried_array_deduction = (cxx_dialect < cxx17);
21906 again:
21907 if (unify (tparms, deduced_args,
21908 INNERMOST_TEMPLATE_ARGS (spec_args),
21909 INNERMOST_TEMPLATE_ARGS (args),
21910 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21911 return NULL_TREE;
21913 for (i = 0; i < ntparms; ++i)
21914 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21916 if (!tried_array_deduction)
21918 try_array_deduction (tparms, innermost_deduced_args,
21919 INNERMOST_TEMPLATE_ARGS (spec_args));
21920 tried_array_deduction = true;
21921 if (TREE_VEC_ELT (innermost_deduced_args, i))
21922 goto again;
21924 return NULL_TREE;
21927 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21928 if (!push_tinst_level (tinst))
21930 excessive_deduction_depth = true;
21931 return NULL_TREE;
21934 /* Verify that nondeduced template arguments agree with the type
21935 obtained from argument deduction.
21937 For example:
21939 struct A { typedef int X; };
21940 template <class T, class U> struct C {};
21941 template <class T> struct C<T, typename T::X> {};
21943 Then with the instantiation `C<A, int>', we can deduce that
21944 `T' is `A' but unify () does not check whether `typename T::X'
21945 is `int'. */
21946 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21948 if (spec_args != error_mark_node)
21949 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21950 INNERMOST_TEMPLATE_ARGS (spec_args),
21951 tmpl, tf_none, false, false);
21953 pop_tinst_level ();
21955 if (spec_args == error_mark_node
21956 /* We only need to check the innermost arguments; the other
21957 arguments will always agree. */
21958 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21959 INNERMOST_TEMPLATE_ARGS (args)))
21960 return NULL_TREE;
21962 /* Now that we have bindings for all of the template arguments,
21963 ensure that the arguments deduced for the template template
21964 parameters have compatible template parameter lists. See the use
21965 of template_template_parm_bindings_ok_p in fn_type_unification
21966 for more information. */
21967 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21968 return NULL_TREE;
21970 return deduced_args;
21973 // Compare two function templates T1 and T2 by deducing bindings
21974 // from one against the other. If both deductions succeed, compare
21975 // constraints to see which is more constrained.
21976 static int
21977 more_specialized_inst (tree t1, tree t2)
21979 int fate = 0;
21980 int count = 0;
21982 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21984 --fate;
21985 ++count;
21988 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21990 ++fate;
21991 ++count;
21994 // If both deductions succeed, then one may be more constrained.
21995 if (count == 2 && fate == 0)
21996 fate = more_constrained (t1, t2);
21998 return fate;
22001 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22002 Return the TREE_LIST node with the most specialized template, if
22003 any. If there is no most specialized template, the error_mark_node
22004 is returned.
22006 Note that this function does not look at, or modify, the
22007 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22008 returned is one of the elements of INSTANTIATIONS, callers may
22009 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22010 and retrieve it from the value returned. */
22012 tree
22013 most_specialized_instantiation (tree templates)
22015 tree fn, champ;
22017 ++processing_template_decl;
22019 champ = templates;
22020 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22022 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22023 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22024 if (fate == -1)
22025 champ = fn;
22026 else if (!fate)
22028 /* Equally specialized, move to next function. If there
22029 is no next function, nothing's most specialized. */
22030 fn = TREE_CHAIN (fn);
22031 champ = fn;
22032 if (!fn)
22033 break;
22037 if (champ)
22038 /* Now verify that champ is better than everything earlier in the
22039 instantiation list. */
22040 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22041 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22043 champ = NULL_TREE;
22044 break;
22048 processing_template_decl--;
22050 if (!champ)
22051 return error_mark_node;
22053 return champ;
22056 /* If DECL is a specialization of some template, return the most
22057 general such template. Otherwise, returns NULL_TREE.
22059 For example, given:
22061 template <class T> struct S { template <class U> void f(U); };
22063 if TMPL is `template <class U> void S<int>::f(U)' this will return
22064 the full template. This function will not trace past partial
22065 specializations, however. For example, given in addition:
22067 template <class T> struct S<T*> { template <class U> void f(U); };
22069 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22070 `template <class T> template <class U> S<T*>::f(U)'. */
22072 tree
22073 most_general_template (tree decl)
22075 if (TREE_CODE (decl) != TEMPLATE_DECL)
22077 if (tree tinfo = get_template_info (decl))
22078 decl = TI_TEMPLATE (tinfo);
22079 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22080 template friend, or a FIELD_DECL for a capture pack. */
22081 if (TREE_CODE (decl) != TEMPLATE_DECL)
22082 return NULL_TREE;
22085 /* Look for more and more general templates. */
22086 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22088 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22089 (See cp-tree.h for details.) */
22090 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22091 break;
22093 if (CLASS_TYPE_P (TREE_TYPE (decl))
22094 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22095 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22096 break;
22098 /* Stop if we run into an explicitly specialized class template. */
22099 if (!DECL_NAMESPACE_SCOPE_P (decl)
22100 && DECL_CONTEXT (decl)
22101 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22102 break;
22104 decl = DECL_TI_TEMPLATE (decl);
22107 return decl;
22110 /* Return the most specialized of the template partial specializations
22111 which can produce TARGET, a specialization of some class or variable
22112 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22113 a TEMPLATE_DECL node corresponding to the partial specialization, while
22114 the TREE_PURPOSE is the set of template arguments that must be
22115 substituted into the template pattern in order to generate TARGET.
22117 If the choice of partial specialization is ambiguous, a diagnostic
22118 is issued, and the error_mark_node is returned. If there are no
22119 partial specializations matching TARGET, then NULL_TREE is
22120 returned, indicating that the primary template should be used. */
22122 static tree
22123 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22125 tree list = NULL_TREE;
22126 tree t;
22127 tree champ;
22128 int fate;
22129 bool ambiguous_p;
22130 tree outer_args = NULL_TREE;
22131 tree tmpl, args;
22133 if (TYPE_P (target))
22135 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22136 tmpl = TI_TEMPLATE (tinfo);
22137 args = TI_ARGS (tinfo);
22139 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22141 tmpl = TREE_OPERAND (target, 0);
22142 args = TREE_OPERAND (target, 1);
22144 else if (VAR_P (target))
22146 tree tinfo = DECL_TEMPLATE_INFO (target);
22147 tmpl = TI_TEMPLATE (tinfo);
22148 args = TI_ARGS (tinfo);
22150 else
22151 gcc_unreachable ();
22153 tree main_tmpl = most_general_template (tmpl);
22155 /* For determining which partial specialization to use, only the
22156 innermost args are interesting. */
22157 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22159 outer_args = strip_innermost_template_args (args, 1);
22160 args = INNERMOST_TEMPLATE_ARGS (args);
22163 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22165 tree spec_args;
22166 tree spec_tmpl = TREE_VALUE (t);
22168 if (outer_args)
22170 /* Substitute in the template args from the enclosing class. */
22171 ++processing_template_decl;
22172 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22173 --processing_template_decl;
22176 if (spec_tmpl == error_mark_node)
22177 return error_mark_node;
22179 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22180 if (spec_args)
22182 if (outer_args)
22183 spec_args = add_to_template_args (outer_args, spec_args);
22185 /* Keep the candidate only if the constraints are satisfied,
22186 or if we're not compiling with concepts. */
22187 if (!flag_concepts
22188 || constraints_satisfied_p (spec_tmpl, spec_args))
22190 list = tree_cons (spec_args, TREE_VALUE (t), list);
22191 TREE_TYPE (list) = TREE_TYPE (t);
22196 if (! list)
22197 return NULL_TREE;
22199 ambiguous_p = false;
22200 t = list;
22201 champ = t;
22202 t = TREE_CHAIN (t);
22203 for (; t; t = TREE_CHAIN (t))
22205 fate = more_specialized_partial_spec (tmpl, champ, t);
22206 if (fate == 1)
22208 else
22210 if (fate == 0)
22212 t = TREE_CHAIN (t);
22213 if (! t)
22215 ambiguous_p = true;
22216 break;
22219 champ = t;
22223 if (!ambiguous_p)
22224 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22226 fate = more_specialized_partial_spec (tmpl, champ, t);
22227 if (fate != 1)
22229 ambiguous_p = true;
22230 break;
22234 if (ambiguous_p)
22236 const char *str;
22237 char *spaces = NULL;
22238 if (!(complain & tf_error))
22239 return error_mark_node;
22240 if (TYPE_P (target))
22241 error ("ambiguous template instantiation for %q#T", target);
22242 else
22243 error ("ambiguous template instantiation for %q#D", target);
22244 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22245 for (t = list; t; t = TREE_CHAIN (t))
22247 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22248 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22249 "%s %#qS", spaces ? spaces : str, subst);
22250 spaces = spaces ? spaces : get_spaces (str);
22252 free (spaces);
22253 return error_mark_node;
22256 return champ;
22259 /* Explicitly instantiate DECL. */
22261 void
22262 do_decl_instantiation (tree decl, tree storage)
22264 tree result = NULL_TREE;
22265 int extern_p = 0;
22267 if (!decl || decl == error_mark_node)
22268 /* An error occurred, for which grokdeclarator has already issued
22269 an appropriate message. */
22270 return;
22271 else if (! DECL_LANG_SPECIFIC (decl))
22273 error ("explicit instantiation of non-template %q#D", decl);
22274 return;
22277 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22278 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22280 if (VAR_P (decl) && !var_templ)
22282 /* There is an asymmetry here in the way VAR_DECLs and
22283 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22284 the latter, the DECL we get back will be marked as a
22285 template instantiation, and the appropriate
22286 DECL_TEMPLATE_INFO will be set up. This does not happen for
22287 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22288 should handle VAR_DECLs as it currently handles
22289 FUNCTION_DECLs. */
22290 if (!DECL_CLASS_SCOPE_P (decl))
22292 error ("%qD is not a static data member of a class template", decl);
22293 return;
22295 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22296 if (!result || !VAR_P (result))
22298 error ("no matching template for %qD found", decl);
22299 return;
22301 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22303 error ("type %qT for explicit instantiation %qD does not match "
22304 "declared type %qT", TREE_TYPE (result), decl,
22305 TREE_TYPE (decl));
22306 return;
22309 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22311 error ("explicit instantiation of %q#D", decl);
22312 return;
22314 else
22315 result = decl;
22317 /* Check for various error cases. Note that if the explicit
22318 instantiation is valid the RESULT will currently be marked as an
22319 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22320 until we get here. */
22322 if (DECL_TEMPLATE_SPECIALIZATION (result))
22324 /* DR 259 [temp.spec].
22326 Both an explicit instantiation and a declaration of an explicit
22327 specialization shall not appear in a program unless the explicit
22328 instantiation follows a declaration of the explicit specialization.
22330 For a given set of template parameters, if an explicit
22331 instantiation of a template appears after a declaration of an
22332 explicit specialization for that template, the explicit
22333 instantiation has no effect. */
22334 return;
22336 else if (DECL_EXPLICIT_INSTANTIATION (result))
22338 /* [temp.spec]
22340 No program shall explicitly instantiate any template more
22341 than once.
22343 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22344 the first instantiation was `extern' and the second is not,
22345 and EXTERN_P for the opposite case. */
22346 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22347 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22348 /* If an "extern" explicit instantiation follows an ordinary
22349 explicit instantiation, the template is instantiated. */
22350 if (extern_p)
22351 return;
22353 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22355 error ("no matching template for %qD found", result);
22356 return;
22358 else if (!DECL_TEMPLATE_INFO (result))
22360 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22361 return;
22364 if (storage == NULL_TREE)
22366 else if (storage == ridpointers[(int) RID_EXTERN])
22368 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22369 pedwarn (input_location, OPT_Wpedantic,
22370 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22371 "instantiations");
22372 extern_p = 1;
22374 else
22375 error ("storage class %qD applied to template instantiation", storage);
22377 check_explicit_instantiation_namespace (result);
22378 mark_decl_instantiated (result, extern_p);
22379 if (! extern_p)
22380 instantiate_decl (result, /*defer_ok=*/true,
22381 /*expl_inst_class_mem_p=*/false);
22384 static void
22385 mark_class_instantiated (tree t, int extern_p)
22387 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22388 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22389 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22390 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22391 if (! extern_p)
22393 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22394 rest_of_type_compilation (t, 1);
22398 /* Called from do_type_instantiation through binding_table_foreach to
22399 do recursive instantiation for the type bound in ENTRY. */
22400 static void
22401 bt_instantiate_type_proc (binding_entry entry, void *data)
22403 tree storage = *(tree *) data;
22405 if (MAYBE_CLASS_TYPE_P (entry->type)
22406 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22407 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22410 /* Perform an explicit instantiation of template class T. STORAGE, if
22411 non-null, is the RID for extern, inline or static. COMPLAIN is
22412 nonzero if this is called from the parser, zero if called recursively,
22413 since the standard is unclear (as detailed below). */
22415 void
22416 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22418 int extern_p = 0;
22419 int nomem_p = 0;
22420 int static_p = 0;
22421 int previous_instantiation_extern_p = 0;
22423 if (TREE_CODE (t) == TYPE_DECL)
22424 t = TREE_TYPE (t);
22426 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22428 tree tmpl =
22429 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22430 if (tmpl)
22431 error ("explicit instantiation of non-class template %qD", tmpl);
22432 else
22433 error ("explicit instantiation of non-template type %qT", t);
22434 return;
22437 complete_type (t);
22439 if (!COMPLETE_TYPE_P (t))
22441 if (complain & tf_error)
22442 error ("explicit instantiation of %q#T before definition of template",
22444 return;
22447 if (storage != NULL_TREE)
22449 if (!in_system_header_at (input_location))
22451 if (storage == ridpointers[(int) RID_EXTERN])
22453 if (cxx_dialect == cxx98)
22454 pedwarn (input_location, OPT_Wpedantic,
22455 "ISO C++ 1998 forbids the use of %<extern%> on "
22456 "explicit instantiations");
22458 else
22459 pedwarn (input_location, OPT_Wpedantic,
22460 "ISO C++ forbids the use of %qE"
22461 " on explicit instantiations", storage);
22464 if (storage == ridpointers[(int) RID_INLINE])
22465 nomem_p = 1;
22466 else if (storage == ridpointers[(int) RID_EXTERN])
22467 extern_p = 1;
22468 else if (storage == ridpointers[(int) RID_STATIC])
22469 static_p = 1;
22470 else
22472 error ("storage class %qD applied to template instantiation",
22473 storage);
22474 extern_p = 0;
22478 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22480 /* DR 259 [temp.spec].
22482 Both an explicit instantiation and a declaration of an explicit
22483 specialization shall not appear in a program unless the explicit
22484 instantiation follows a declaration of the explicit specialization.
22486 For a given set of template parameters, if an explicit
22487 instantiation of a template appears after a declaration of an
22488 explicit specialization for that template, the explicit
22489 instantiation has no effect. */
22490 return;
22492 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22494 /* [temp.spec]
22496 No program shall explicitly instantiate any template more
22497 than once.
22499 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22500 instantiation was `extern'. If EXTERN_P then the second is.
22501 These cases are OK. */
22502 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22504 if (!previous_instantiation_extern_p && !extern_p
22505 && (complain & tf_error))
22506 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22508 /* If we've already instantiated the template, just return now. */
22509 if (!CLASSTYPE_INTERFACE_ONLY (t))
22510 return;
22513 check_explicit_instantiation_namespace (TYPE_NAME (t));
22514 mark_class_instantiated (t, extern_p);
22516 if (nomem_p)
22517 return;
22519 /* In contrast to implicit instantiation, where only the
22520 declarations, and not the definitions, of members are
22521 instantiated, we have here:
22523 [temp.explicit]
22525 The explicit instantiation of a class template specialization
22526 implies the instantiation of all of its members not
22527 previously explicitly specialized in the translation unit
22528 containing the explicit instantiation.
22530 Of course, we can't instantiate member template classes, since we
22531 don't have any arguments for them. Note that the standard is
22532 unclear on whether the instantiation of the members are
22533 *explicit* instantiations or not. However, the most natural
22534 interpretation is that it should be an explicit
22535 instantiation. */
22536 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22537 if ((VAR_P (fld)
22538 || (TREE_CODE (fld) == FUNCTION_DECL
22539 && !static_p
22540 && user_provided_p (fld)))
22541 && DECL_TEMPLATE_INSTANTIATION (fld))
22543 mark_decl_instantiated (fld, extern_p);
22544 if (! extern_p)
22545 instantiate_decl (fld, /*defer_ok=*/true,
22546 /*expl_inst_class_mem_p=*/true);
22549 if (CLASSTYPE_NESTED_UTDS (t))
22550 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22551 bt_instantiate_type_proc, &storage);
22554 /* Given a function DECL, which is a specialization of TMPL, modify
22555 DECL to be a re-instantiation of TMPL with the same template
22556 arguments. TMPL should be the template into which tsubst'ing
22557 should occur for DECL, not the most general template.
22559 One reason for doing this is a scenario like this:
22561 template <class T>
22562 void f(const T&, int i);
22564 void g() { f(3, 7); }
22566 template <class T>
22567 void f(const T& t, const int i) { }
22569 Note that when the template is first instantiated, with
22570 instantiate_template, the resulting DECL will have no name for the
22571 first parameter, and the wrong type for the second. So, when we go
22572 to instantiate the DECL, we regenerate it. */
22574 static void
22575 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22577 /* The arguments used to instantiate DECL, from the most general
22578 template. */
22579 tree code_pattern;
22581 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22583 /* Make sure that we can see identifiers, and compute access
22584 correctly. */
22585 push_access_scope (decl);
22587 if (TREE_CODE (decl) == FUNCTION_DECL)
22589 tree decl_parm;
22590 tree pattern_parm;
22591 tree specs;
22592 int args_depth;
22593 int parms_depth;
22595 args_depth = TMPL_ARGS_DEPTH (args);
22596 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22597 if (args_depth > parms_depth)
22598 args = get_innermost_template_args (args, parms_depth);
22600 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22601 args, tf_error, NULL_TREE,
22602 /*defer_ok*/false);
22603 if (specs && specs != error_mark_node)
22604 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22605 specs);
22607 /* Merge parameter declarations. */
22608 decl_parm = skip_artificial_parms_for (decl,
22609 DECL_ARGUMENTS (decl));
22610 pattern_parm
22611 = skip_artificial_parms_for (code_pattern,
22612 DECL_ARGUMENTS (code_pattern));
22613 while (decl_parm && !DECL_PACK_P (pattern_parm))
22615 tree parm_type;
22616 tree attributes;
22618 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22619 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22620 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22621 NULL_TREE);
22622 parm_type = type_decays_to (parm_type);
22623 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22624 TREE_TYPE (decl_parm) = parm_type;
22625 attributes = DECL_ATTRIBUTES (pattern_parm);
22626 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22628 DECL_ATTRIBUTES (decl_parm) = attributes;
22629 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22631 decl_parm = DECL_CHAIN (decl_parm);
22632 pattern_parm = DECL_CHAIN (pattern_parm);
22634 /* Merge any parameters that match with the function parameter
22635 pack. */
22636 if (pattern_parm && DECL_PACK_P (pattern_parm))
22638 int i, len;
22639 tree expanded_types;
22640 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22641 the parameters in this function parameter pack. */
22642 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22643 args, tf_error, NULL_TREE);
22644 len = TREE_VEC_LENGTH (expanded_types);
22645 for (i = 0; i < len; i++)
22647 tree parm_type;
22648 tree attributes;
22650 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22651 /* Rename the parameter to include the index. */
22652 DECL_NAME (decl_parm) =
22653 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22654 parm_type = TREE_VEC_ELT (expanded_types, i);
22655 parm_type = type_decays_to (parm_type);
22656 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22657 TREE_TYPE (decl_parm) = parm_type;
22658 attributes = DECL_ATTRIBUTES (pattern_parm);
22659 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22661 DECL_ATTRIBUTES (decl_parm) = attributes;
22662 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22664 decl_parm = DECL_CHAIN (decl_parm);
22667 /* Merge additional specifiers from the CODE_PATTERN. */
22668 if (DECL_DECLARED_INLINE_P (code_pattern)
22669 && !DECL_DECLARED_INLINE_P (decl))
22670 DECL_DECLARED_INLINE_P (decl) = 1;
22672 else if (VAR_P (decl))
22674 start_lambda_scope (decl);
22675 DECL_INITIAL (decl) =
22676 tsubst_expr (DECL_INITIAL (code_pattern), args,
22677 tf_error, DECL_TI_TEMPLATE (decl),
22678 /*integral_constant_expression_p=*/false);
22679 finish_lambda_scope ();
22680 if (VAR_HAD_UNKNOWN_BOUND (decl))
22681 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22682 tf_error, DECL_TI_TEMPLATE (decl));
22684 else
22685 gcc_unreachable ();
22687 pop_access_scope (decl);
22690 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22691 substituted to get DECL. */
22693 tree
22694 template_for_substitution (tree decl)
22696 tree tmpl = DECL_TI_TEMPLATE (decl);
22698 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22699 for the instantiation. This is not always the most general
22700 template. Consider, for example:
22702 template <class T>
22703 struct S { template <class U> void f();
22704 template <> void f<int>(); };
22706 and an instantiation of S<double>::f<int>. We want TD to be the
22707 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22708 while (/* An instantiation cannot have a definition, so we need a
22709 more general template. */
22710 DECL_TEMPLATE_INSTANTIATION (tmpl)
22711 /* We must also deal with friend templates. Given:
22713 template <class T> struct S {
22714 template <class U> friend void f() {};
22717 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22718 so far as the language is concerned, but that's still
22719 where we get the pattern for the instantiation from. On
22720 other hand, if the definition comes outside the class, say:
22722 template <class T> struct S {
22723 template <class U> friend void f();
22725 template <class U> friend void f() {}
22727 we don't need to look any further. That's what the check for
22728 DECL_INITIAL is for. */
22729 || (TREE_CODE (decl) == FUNCTION_DECL
22730 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22731 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22733 /* The present template, TD, should not be a definition. If it
22734 were a definition, we should be using it! Note that we
22735 cannot restructure the loop to just keep going until we find
22736 a template with a definition, since that might go too far if
22737 a specialization was declared, but not defined. */
22739 /* Fetch the more general template. */
22740 tmpl = DECL_TI_TEMPLATE (tmpl);
22743 return tmpl;
22746 /* Returns true if we need to instantiate this template instance even if we
22747 know we aren't going to emit it. */
22749 bool
22750 always_instantiate_p (tree decl)
22752 /* We always instantiate inline functions so that we can inline them. An
22753 explicit instantiation declaration prohibits implicit instantiation of
22754 non-inline functions. With high levels of optimization, we would
22755 normally inline non-inline functions -- but we're not allowed to do
22756 that for "extern template" functions. Therefore, we check
22757 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22758 return ((TREE_CODE (decl) == FUNCTION_DECL
22759 && (DECL_DECLARED_INLINE_P (decl)
22760 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22761 /* And we need to instantiate static data members so that
22762 their initializers are available in integral constant
22763 expressions. */
22764 || (VAR_P (decl)
22765 && decl_maybe_constant_var_p (decl)));
22768 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22769 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22770 error, true otherwise. */
22772 bool
22773 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22775 tree fntype, spec, noex, clone;
22777 /* Don't instantiate a noexcept-specification from template context. */
22778 if (processing_template_decl)
22779 return true;
22781 if (DECL_CLONED_FUNCTION_P (fn))
22782 fn = DECL_CLONED_FUNCTION (fn);
22783 fntype = TREE_TYPE (fn);
22784 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22786 if (!spec || !TREE_PURPOSE (spec))
22787 return true;
22789 noex = TREE_PURPOSE (spec);
22791 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22793 static hash_set<tree>* fns = new hash_set<tree>;
22794 bool added = false;
22795 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22796 spec = get_defaulted_eh_spec (fn, complain);
22797 else if (!(added = !fns->add (fn)))
22799 /* If hash_set::add returns true, the element was already there. */
22800 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22801 DECL_SOURCE_LOCATION (fn));
22802 error_at (loc,
22803 "exception specification of %qD depends on itself",
22804 fn);
22805 spec = noexcept_false_spec;
22807 else if (push_tinst_level (fn))
22809 push_access_scope (fn);
22810 push_deferring_access_checks (dk_no_deferred);
22811 input_location = DECL_SOURCE_LOCATION (fn);
22812 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22813 DEFERRED_NOEXCEPT_ARGS (noex),
22814 tf_warning_or_error, fn,
22815 /*function_p=*/false,
22816 /*integral_constant_expression_p=*/true);
22817 pop_deferring_access_checks ();
22818 pop_access_scope (fn);
22819 pop_tinst_level ();
22820 spec = build_noexcept_spec (noex, tf_warning_or_error);
22821 if (spec == error_mark_node)
22822 spec = noexcept_false_spec;
22824 else
22825 spec = noexcept_false_spec;
22827 if (added)
22828 fns->remove (fn);
22830 if (spec == error_mark_node)
22831 return false;
22833 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22836 FOR_EACH_CLONE (clone, fn)
22838 if (TREE_TYPE (clone) == fntype)
22839 TREE_TYPE (clone) = TREE_TYPE (fn);
22840 else
22841 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22844 return true;
22847 /* We're starting to process the function INST, an instantiation of PATTERN;
22848 add their parameters to local_specializations. */
22850 static void
22851 register_parameter_specializations (tree pattern, tree inst)
22853 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22854 tree spec_parm = DECL_ARGUMENTS (inst);
22855 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22857 register_local_specialization (spec_parm, tmpl_parm);
22858 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22859 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22861 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22863 if (!DECL_PACK_P (tmpl_parm))
22865 register_local_specialization (spec_parm, tmpl_parm);
22866 spec_parm = DECL_CHAIN (spec_parm);
22868 else
22870 /* Register the (value) argument pack as a specialization of
22871 TMPL_PARM, then move on. */
22872 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22873 register_local_specialization (argpack, tmpl_parm);
22876 gcc_assert (!spec_parm);
22879 /* Produce the definition of D, a _DECL generated from a template. If
22880 DEFER_OK is true, then we don't have to actually do the
22881 instantiation now; we just have to do it sometime. Normally it is
22882 an error if this is an explicit instantiation but D is undefined.
22883 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22884 instantiated class template. */
22886 tree
22887 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22889 tree tmpl = DECL_TI_TEMPLATE (d);
22890 tree gen_args;
22891 tree args;
22892 tree td;
22893 tree code_pattern;
22894 tree spec;
22895 tree gen_tmpl;
22896 bool pattern_defined;
22897 location_t saved_loc = input_location;
22898 int saved_unevaluated_operand = cp_unevaluated_operand;
22899 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22900 bool external_p;
22901 bool deleted_p;
22903 /* This function should only be used to instantiate templates for
22904 functions and static member variables. */
22905 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22907 /* A concept is never instantiated. */
22908 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22910 /* Variables are never deferred; if instantiation is required, they
22911 are instantiated right away. That allows for better code in the
22912 case that an expression refers to the value of the variable --
22913 if the variable has a constant value the referring expression can
22914 take advantage of that fact. */
22915 if (VAR_P (d))
22916 defer_ok = false;
22918 /* Don't instantiate cloned functions. Instead, instantiate the
22919 functions they cloned. */
22920 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22921 d = DECL_CLONED_FUNCTION (d);
22923 if (DECL_TEMPLATE_INSTANTIATED (d)
22924 || (TREE_CODE (d) == FUNCTION_DECL
22925 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22926 || DECL_TEMPLATE_SPECIALIZATION (d))
22927 /* D has already been instantiated or explicitly specialized, so
22928 there's nothing for us to do here.
22930 It might seem reasonable to check whether or not D is an explicit
22931 instantiation, and, if so, stop here. But when an explicit
22932 instantiation is deferred until the end of the compilation,
22933 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22934 the instantiation. */
22935 return d;
22937 /* Check to see whether we know that this template will be
22938 instantiated in some other file, as with "extern template"
22939 extension. */
22940 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22942 /* In general, we do not instantiate such templates. */
22943 if (external_p && !always_instantiate_p (d))
22944 return d;
22946 gen_tmpl = most_general_template (tmpl);
22947 gen_args = DECL_TI_ARGS (d);
22949 if (tmpl != gen_tmpl)
22950 /* We should already have the extra args. */
22951 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22952 == TMPL_ARGS_DEPTH (gen_args));
22953 /* And what's in the hash table should match D. */
22954 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22955 || spec == NULL_TREE);
22957 /* This needs to happen before any tsubsting. */
22958 if (! push_tinst_level (d))
22959 return d;
22961 timevar_push (TV_TEMPLATE_INST);
22963 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22964 for the instantiation. */
22965 td = template_for_substitution (d);
22966 args = gen_args;
22968 if (VAR_P (d))
22970 /* Look up an explicit specialization, if any. */
22971 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22972 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22973 if (elt && elt != error_mark_node)
22975 td = TREE_VALUE (elt);
22976 args = TREE_PURPOSE (elt);
22980 code_pattern = DECL_TEMPLATE_RESULT (td);
22982 /* We should never be trying to instantiate a member of a class
22983 template or partial specialization. */
22984 gcc_assert (d != code_pattern);
22986 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22987 || DECL_TEMPLATE_SPECIALIZATION (td))
22988 /* In the case of a friend template whose definition is provided
22989 outside the class, we may have too many arguments. Drop the
22990 ones we don't need. The same is true for specializations. */
22991 args = get_innermost_template_args
22992 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22994 if (TREE_CODE (d) == FUNCTION_DECL)
22996 deleted_p = DECL_DELETED_FN (code_pattern);
22997 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22998 && DECL_INITIAL (code_pattern) != error_mark_node)
22999 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23000 || deleted_p);
23002 else
23004 deleted_p = false;
23005 if (DECL_CLASS_SCOPE_P (code_pattern))
23006 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23007 || DECL_INLINE_VAR_P (code_pattern));
23008 else
23009 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23012 /* We may be in the middle of deferred access check. Disable it now. */
23013 push_deferring_access_checks (dk_no_deferred);
23015 /* Unless an explicit instantiation directive has already determined
23016 the linkage of D, remember that a definition is available for
23017 this entity. */
23018 if (pattern_defined
23019 && !DECL_INTERFACE_KNOWN (d)
23020 && !DECL_NOT_REALLY_EXTERN (d))
23021 mark_definable (d);
23023 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23024 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23025 input_location = DECL_SOURCE_LOCATION (d);
23027 /* If D is a member of an explicitly instantiated class template,
23028 and no definition is available, treat it like an implicit
23029 instantiation. */
23030 if (!pattern_defined && expl_inst_class_mem_p
23031 && DECL_EXPLICIT_INSTANTIATION (d))
23033 /* Leave linkage flags alone on instantiations with anonymous
23034 visibility. */
23035 if (TREE_PUBLIC (d))
23037 DECL_NOT_REALLY_EXTERN (d) = 0;
23038 DECL_INTERFACE_KNOWN (d) = 0;
23040 SET_DECL_IMPLICIT_INSTANTIATION (d);
23043 /* Defer all other templates, unless we have been explicitly
23044 forbidden from doing so. */
23045 if (/* If there is no definition, we cannot instantiate the
23046 template. */
23047 ! pattern_defined
23048 /* If it's OK to postpone instantiation, do so. */
23049 || defer_ok
23050 /* If this is a static data member that will be defined
23051 elsewhere, we don't want to instantiate the entire data
23052 member, but we do want to instantiate the initializer so that
23053 we can substitute that elsewhere. */
23054 || (external_p && VAR_P (d))
23055 /* Handle here a deleted function too, avoid generating
23056 its body (c++/61080). */
23057 || deleted_p)
23059 /* The definition of the static data member is now required so
23060 we must substitute the initializer. */
23061 if (VAR_P (d)
23062 && !DECL_INITIAL (d)
23063 && DECL_INITIAL (code_pattern))
23065 tree ns;
23066 tree init;
23067 bool const_init = false;
23068 bool enter_context = DECL_CLASS_SCOPE_P (d);
23070 ns = decl_namespace_context (d);
23071 push_nested_namespace (ns);
23072 if (enter_context)
23073 push_nested_class (DECL_CONTEXT (d));
23074 init = tsubst_expr (DECL_INITIAL (code_pattern),
23075 args,
23076 tf_warning_or_error, NULL_TREE,
23077 /*integral_constant_expression_p=*/false);
23078 /* If instantiating the initializer involved instantiating this
23079 again, don't call cp_finish_decl twice. */
23080 if (!DECL_INITIAL (d))
23082 /* Make sure the initializer is still constant, in case of
23083 circular dependency (template/instantiate6.C). */
23084 const_init
23085 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23086 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23087 /*asmspec_tree=*/NULL_TREE,
23088 LOOKUP_ONLYCONVERTING);
23090 if (enter_context)
23091 pop_nested_class ();
23092 pop_nested_namespace (ns);
23095 /* We restore the source position here because it's used by
23096 add_pending_template. */
23097 input_location = saved_loc;
23099 if (at_eof && !pattern_defined
23100 && DECL_EXPLICIT_INSTANTIATION (d)
23101 && DECL_NOT_REALLY_EXTERN (d))
23102 /* [temp.explicit]
23104 The definition of a non-exported function template, a
23105 non-exported member function template, or a non-exported
23106 member function or static data member of a class template
23107 shall be present in every translation unit in which it is
23108 explicitly instantiated. */
23109 permerror (input_location, "explicit instantiation of %qD "
23110 "but no definition available", d);
23112 /* If we're in unevaluated context, we just wanted to get the
23113 constant value; this isn't an odr use, so don't queue
23114 a full instantiation. */
23115 if (cp_unevaluated_operand != 0)
23116 goto out;
23117 /* ??? Historically, we have instantiated inline functions, even
23118 when marked as "extern template". */
23119 if (!(external_p && VAR_P (d)))
23120 add_pending_template (d);
23121 goto out;
23123 /* Tell the repository that D is available in this translation unit
23124 -- and see if it is supposed to be instantiated here. */
23125 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23127 /* In a PCH file, despite the fact that the repository hasn't
23128 requested instantiation in the PCH it is still possible that
23129 an instantiation will be required in a file that includes the
23130 PCH. */
23131 if (pch_file)
23132 add_pending_template (d);
23133 /* Instantiate inline functions so that the inliner can do its
23134 job, even though we'll not be emitting a copy of this
23135 function. */
23136 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23137 goto out;
23140 bool push_to_top, nested;
23141 tree fn_context;
23142 fn_context = decl_function_context (d);
23143 nested = current_function_decl != NULL_TREE;
23144 push_to_top = !(nested && fn_context == current_function_decl);
23146 vec<tree> omp_privatization_save;
23147 if (nested)
23148 save_omp_privatization_clauses (omp_privatization_save);
23150 if (push_to_top)
23151 push_to_top_level ();
23152 else
23154 push_function_context ();
23155 cp_unevaluated_operand = 0;
23156 c_inhibit_evaluation_warnings = 0;
23159 /* Mark D as instantiated so that recursive calls to
23160 instantiate_decl do not try to instantiate it again. */
23161 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23163 /* Regenerate the declaration in case the template has been modified
23164 by a subsequent redeclaration. */
23165 regenerate_decl_from_template (d, td, args);
23167 /* We already set the file and line above. Reset them now in case
23168 they changed as a result of calling regenerate_decl_from_template. */
23169 input_location = DECL_SOURCE_LOCATION (d);
23171 if (VAR_P (d))
23173 tree init;
23174 bool const_init = false;
23176 /* Clear out DECL_RTL; whatever was there before may not be right
23177 since we've reset the type of the declaration. */
23178 SET_DECL_RTL (d, NULL);
23179 DECL_IN_AGGR_P (d) = 0;
23181 /* The initializer is placed in DECL_INITIAL by
23182 regenerate_decl_from_template so we don't need to
23183 push/pop_access_scope again here. Pull it out so that
23184 cp_finish_decl can process it. */
23185 init = DECL_INITIAL (d);
23186 DECL_INITIAL (d) = NULL_TREE;
23187 DECL_INITIALIZED_P (d) = 0;
23189 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23190 initializer. That function will defer actual emission until
23191 we have a chance to determine linkage. */
23192 DECL_EXTERNAL (d) = 0;
23194 /* Enter the scope of D so that access-checking works correctly. */
23195 bool enter_context = DECL_CLASS_SCOPE_P (d);
23196 if (enter_context)
23197 push_nested_class (DECL_CONTEXT (d));
23199 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23200 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23202 if (enter_context)
23203 pop_nested_class ();
23205 if (variable_template_p (gen_tmpl))
23206 note_variable_template_instantiation (d);
23208 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23209 synthesize_method (d);
23210 else if (TREE_CODE (d) == FUNCTION_DECL)
23212 /* Set up the list of local specializations. */
23213 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23214 tree block = NULL_TREE;
23216 /* Set up context. */
23217 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23218 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23219 block = push_stmt_list ();
23220 else
23221 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23223 /* Some typedefs referenced from within the template code need to be
23224 access checked at template instantiation time, i.e now. These
23225 types were added to the template at parsing time. Let's get those
23226 and perform the access checks then. */
23227 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23228 args);
23230 /* Create substitution entries for the parameters. */
23231 register_parameter_specializations (code_pattern, d);
23233 /* Substitute into the body of the function. */
23234 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23235 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23236 tf_warning_or_error, tmpl);
23237 else
23239 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23240 tf_warning_or_error, tmpl,
23241 /*integral_constant_expression_p=*/false);
23243 /* Set the current input_location to the end of the function
23244 so that finish_function knows where we are. */
23245 input_location
23246 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23248 /* Remember if we saw an infinite loop in the template. */
23249 current_function_infinite_loop
23250 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23253 /* Finish the function. */
23254 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23255 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23256 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23257 else
23259 d = finish_function (/*inline_p=*/false);
23260 expand_or_defer_fn (d);
23263 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23264 cp_check_omp_declare_reduction (d);
23267 /* We're not deferring instantiation any more. */
23268 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23270 if (push_to_top)
23271 pop_from_top_level ();
23272 else
23273 pop_function_context ();
23275 if (nested)
23276 restore_omp_privatization_clauses (omp_privatization_save);
23278 out:
23279 pop_deferring_access_checks ();
23280 timevar_pop (TV_TEMPLATE_INST);
23281 pop_tinst_level ();
23282 input_location = saved_loc;
23283 cp_unevaluated_operand = saved_unevaluated_operand;
23284 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23286 return d;
23289 /* Run through the list of templates that we wish we could
23290 instantiate, and instantiate any we can. RETRIES is the
23291 number of times we retry pending template instantiation. */
23293 void
23294 instantiate_pending_templates (int retries)
23296 int reconsider;
23297 location_t saved_loc = input_location;
23299 /* Instantiating templates may trigger vtable generation. This in turn
23300 may require further template instantiations. We place a limit here
23301 to avoid infinite loop. */
23302 if (pending_templates && retries >= max_tinst_depth)
23304 tree decl = pending_templates->tinst->decl;
23306 fatal_error (input_location,
23307 "template instantiation depth exceeds maximum of %d"
23308 " instantiating %q+D, possibly from virtual table generation"
23309 " (use -ftemplate-depth= to increase the maximum)",
23310 max_tinst_depth, decl);
23311 if (TREE_CODE (decl) == FUNCTION_DECL)
23312 /* Pretend that we defined it. */
23313 DECL_INITIAL (decl) = error_mark_node;
23314 return;
23319 struct pending_template **t = &pending_templates;
23320 struct pending_template *last = NULL;
23321 reconsider = 0;
23322 while (*t)
23324 tree instantiation = reopen_tinst_level ((*t)->tinst);
23325 bool complete = false;
23327 if (TYPE_P (instantiation))
23329 if (!COMPLETE_TYPE_P (instantiation))
23331 instantiate_class_template (instantiation);
23332 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23333 for (tree fld = TYPE_FIELDS (instantiation);
23334 fld; fld = TREE_CHAIN (fld))
23335 if ((VAR_P (fld)
23336 || (TREE_CODE (fld) == FUNCTION_DECL
23337 && !DECL_ARTIFICIAL (fld)))
23338 && DECL_TEMPLATE_INSTANTIATION (fld))
23339 instantiate_decl (fld,
23340 /*defer_ok=*/false,
23341 /*expl_inst_class_mem_p=*/false);
23343 if (COMPLETE_TYPE_P (instantiation))
23344 reconsider = 1;
23347 complete = COMPLETE_TYPE_P (instantiation);
23349 else
23351 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23352 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23354 instantiation
23355 = instantiate_decl (instantiation,
23356 /*defer_ok=*/false,
23357 /*expl_inst_class_mem_p=*/false);
23358 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23359 reconsider = 1;
23362 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23363 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23366 if (complete)
23367 /* If INSTANTIATION has been instantiated, then we don't
23368 need to consider it again in the future. */
23369 *t = (*t)->next;
23370 else
23372 last = *t;
23373 t = &(*t)->next;
23375 tinst_depth = 0;
23376 current_tinst_level = NULL;
23378 last_pending_template = last;
23380 while (reconsider);
23382 input_location = saved_loc;
23385 /* Substitute ARGVEC into T, which is a list of initializers for
23386 either base class or a non-static data member. The TREE_PURPOSEs
23387 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23388 instantiate_decl. */
23390 static tree
23391 tsubst_initializer_list (tree t, tree argvec)
23393 tree inits = NULL_TREE;
23395 for (; t; t = TREE_CHAIN (t))
23397 tree decl;
23398 tree init;
23399 tree expanded_bases = NULL_TREE;
23400 tree expanded_arguments = NULL_TREE;
23401 int i, len = 1;
23403 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23405 tree expr;
23406 tree arg;
23408 /* Expand the base class expansion type into separate base
23409 classes. */
23410 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23411 tf_warning_or_error,
23412 NULL_TREE);
23413 if (expanded_bases == error_mark_node)
23414 continue;
23416 /* We'll be building separate TREE_LISTs of arguments for
23417 each base. */
23418 len = TREE_VEC_LENGTH (expanded_bases);
23419 expanded_arguments = make_tree_vec (len);
23420 for (i = 0; i < len; i++)
23421 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23423 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23424 expand each argument in the TREE_VALUE of t. */
23425 expr = make_node (EXPR_PACK_EXPANSION);
23426 PACK_EXPANSION_LOCAL_P (expr) = true;
23427 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23428 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23430 if (TREE_VALUE (t) == void_type_node)
23431 /* VOID_TYPE_NODE is used to indicate
23432 value-initialization. */
23434 for (i = 0; i < len; i++)
23435 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23437 else
23439 /* Substitute parameter packs into each argument in the
23440 TREE_LIST. */
23441 in_base_initializer = 1;
23442 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23444 tree expanded_exprs;
23446 /* Expand the argument. */
23447 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23448 expanded_exprs
23449 = tsubst_pack_expansion (expr, argvec,
23450 tf_warning_or_error,
23451 NULL_TREE);
23452 if (expanded_exprs == error_mark_node)
23453 continue;
23455 /* Prepend each of the expanded expressions to the
23456 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23457 for (i = 0; i < len; i++)
23459 TREE_VEC_ELT (expanded_arguments, i) =
23460 tree_cons (NULL_TREE,
23461 TREE_VEC_ELT (expanded_exprs, i),
23462 TREE_VEC_ELT (expanded_arguments, i));
23465 in_base_initializer = 0;
23467 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23468 since we built them backwards. */
23469 for (i = 0; i < len; i++)
23471 TREE_VEC_ELT (expanded_arguments, i) =
23472 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23477 for (i = 0; i < len; ++i)
23479 if (expanded_bases)
23481 decl = TREE_VEC_ELT (expanded_bases, i);
23482 decl = expand_member_init (decl);
23483 init = TREE_VEC_ELT (expanded_arguments, i);
23485 else
23487 tree tmp;
23488 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23489 tf_warning_or_error, NULL_TREE);
23491 decl = expand_member_init (decl);
23492 if (decl && !DECL_P (decl))
23493 in_base_initializer = 1;
23495 init = TREE_VALUE (t);
23496 tmp = init;
23497 if (init != void_type_node)
23498 init = tsubst_expr (init, argvec,
23499 tf_warning_or_error, NULL_TREE,
23500 /*integral_constant_expression_p=*/false);
23501 if (init == NULL_TREE && tmp != NULL_TREE)
23502 /* If we had an initializer but it instantiated to nothing,
23503 value-initialize the object. This will only occur when
23504 the initializer was a pack expansion where the parameter
23505 packs used in that expansion were of length zero. */
23506 init = void_type_node;
23507 in_base_initializer = 0;
23510 if (decl)
23512 init = build_tree_list (decl, init);
23513 TREE_CHAIN (init) = inits;
23514 inits = init;
23518 return inits;
23521 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23523 static void
23524 set_current_access_from_decl (tree decl)
23526 if (TREE_PRIVATE (decl))
23527 current_access_specifier = access_private_node;
23528 else if (TREE_PROTECTED (decl))
23529 current_access_specifier = access_protected_node;
23530 else
23531 current_access_specifier = access_public_node;
23534 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23535 is the instantiation (which should have been created with
23536 start_enum) and ARGS are the template arguments to use. */
23538 static void
23539 tsubst_enum (tree tag, tree newtag, tree args)
23541 tree e;
23543 if (SCOPED_ENUM_P (newtag))
23544 begin_scope (sk_scoped_enum, newtag);
23546 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23548 tree value;
23549 tree decl;
23551 decl = TREE_VALUE (e);
23552 /* Note that in a template enum, the TREE_VALUE is the
23553 CONST_DECL, not the corresponding INTEGER_CST. */
23554 value = tsubst_expr (DECL_INITIAL (decl),
23555 args, tf_warning_or_error, NULL_TREE,
23556 /*integral_constant_expression_p=*/true);
23558 /* Give this enumeration constant the correct access. */
23559 set_current_access_from_decl (decl);
23561 /* Actually build the enumerator itself. Here we're assuming that
23562 enumerators can't have dependent attributes. */
23563 build_enumerator (DECL_NAME (decl), value, newtag,
23564 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23567 if (SCOPED_ENUM_P (newtag))
23568 finish_scope ();
23570 finish_enum_value_list (newtag);
23571 finish_enum (newtag);
23573 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23574 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23577 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23578 its type -- but without substituting the innermost set of template
23579 arguments. So, innermost set of template parameters will appear in
23580 the type. */
23582 tree
23583 get_mostly_instantiated_function_type (tree decl)
23585 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23586 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23589 /* Return truthvalue if we're processing a template different from
23590 the last one involved in diagnostics. */
23591 bool
23592 problematic_instantiation_changed (void)
23594 return current_tinst_level != last_error_tinst_level;
23597 /* Remember current template involved in diagnostics. */
23598 void
23599 record_last_problematic_instantiation (void)
23601 last_error_tinst_level = current_tinst_level;
23604 struct tinst_level *
23605 current_instantiation (void)
23607 return current_tinst_level;
23610 /* Return TRUE if current_function_decl is being instantiated, false
23611 otherwise. */
23613 bool
23614 instantiating_current_function_p (void)
23616 return (current_instantiation ()
23617 && current_instantiation ()->decl == current_function_decl);
23620 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23621 type. Return zero for ok, nonzero for disallowed. Issue error and
23622 warning messages under control of COMPLAIN. */
23624 static int
23625 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23627 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23628 return 0;
23629 else if (POINTER_TYPE_P (type))
23630 return 0;
23631 else if (TYPE_PTRMEM_P (type))
23632 return 0;
23633 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23634 return 0;
23635 else if (TREE_CODE (type) == TYPENAME_TYPE)
23636 return 0;
23637 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23638 return 0;
23639 else if (TREE_CODE (type) == NULLPTR_TYPE)
23640 return 0;
23641 /* A bound template template parm could later be instantiated to have a valid
23642 nontype parm type via an alias template. */
23643 else if (cxx_dialect >= cxx11
23644 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23645 return 0;
23647 if (complain & tf_error)
23649 if (type == error_mark_node)
23650 inform (input_location, "invalid template non-type parameter");
23651 else
23652 error ("%q#T is not a valid type for a template non-type parameter",
23653 type);
23655 return 1;
23658 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23659 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23661 static bool
23662 dependent_type_p_r (tree type)
23664 tree scope;
23666 /* [temp.dep.type]
23668 A type is dependent if it is:
23670 -- a template parameter. Template template parameters are types
23671 for us (since TYPE_P holds true for them) so we handle
23672 them here. */
23673 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23674 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23675 return true;
23676 /* -- a qualified-id with a nested-name-specifier which contains a
23677 class-name that names a dependent type or whose unqualified-id
23678 names a dependent type. */
23679 if (TREE_CODE (type) == TYPENAME_TYPE)
23680 return true;
23682 /* An alias template specialization can be dependent even if the
23683 resulting type is not. */
23684 if (dependent_alias_template_spec_p (type))
23685 return true;
23687 /* -- a cv-qualified type where the cv-unqualified type is
23688 dependent.
23689 No code is necessary for this bullet; the code below handles
23690 cv-qualified types, and we don't want to strip aliases with
23691 TYPE_MAIN_VARIANT because of DR 1558. */
23692 /* -- a compound type constructed from any dependent type. */
23693 if (TYPE_PTRMEM_P (type))
23694 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23695 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23696 (type)));
23697 else if (TYPE_PTR_P (type)
23698 || TREE_CODE (type) == REFERENCE_TYPE)
23699 return dependent_type_p (TREE_TYPE (type));
23700 else if (TREE_CODE (type) == FUNCTION_TYPE
23701 || TREE_CODE (type) == METHOD_TYPE)
23703 tree arg_type;
23705 if (dependent_type_p (TREE_TYPE (type)))
23706 return true;
23707 for (arg_type = TYPE_ARG_TYPES (type);
23708 arg_type;
23709 arg_type = TREE_CHAIN (arg_type))
23710 if (dependent_type_p (TREE_VALUE (arg_type)))
23711 return true;
23712 if (cxx_dialect >= cxx17)
23714 /* A value-dependent noexcept-specifier makes the type dependent. */
23715 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23716 if (spec && TREE_PURPOSE (spec)
23717 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23718 return true;
23720 return false;
23722 /* -- an array type constructed from any dependent type or whose
23723 size is specified by a constant expression that is
23724 value-dependent.
23726 We checked for type- and value-dependence of the bounds in
23727 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23728 if (TREE_CODE (type) == ARRAY_TYPE)
23730 if (TYPE_DOMAIN (type)
23731 && dependent_type_p (TYPE_DOMAIN (type)))
23732 return true;
23733 return dependent_type_p (TREE_TYPE (type));
23736 /* -- a template-id in which either the template name is a template
23737 parameter ... */
23738 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23739 return true;
23740 /* ... or any of the template arguments is a dependent type or
23741 an expression that is type-dependent or value-dependent. */
23742 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23743 && (any_dependent_template_arguments_p
23744 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23745 return true;
23747 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23748 dependent; if the argument of the `typeof' expression is not
23749 type-dependent, then it should already been have resolved. */
23750 if (TREE_CODE (type) == TYPEOF_TYPE
23751 || TREE_CODE (type) == DECLTYPE_TYPE
23752 || TREE_CODE (type) == UNDERLYING_TYPE)
23753 return true;
23755 /* A template argument pack is dependent if any of its packed
23756 arguments are. */
23757 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23759 tree args = ARGUMENT_PACK_ARGS (type);
23760 int i, len = TREE_VEC_LENGTH (args);
23761 for (i = 0; i < len; ++i)
23762 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23763 return true;
23766 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23767 be template parameters. */
23768 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23769 return true;
23771 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23772 return true;
23774 /* The standard does not specifically mention types that are local
23775 to template functions or local classes, but they should be
23776 considered dependent too. For example:
23778 template <int I> void f() {
23779 enum E { a = I };
23780 S<sizeof (E)> s;
23783 The size of `E' cannot be known until the value of `I' has been
23784 determined. Therefore, `E' must be considered dependent. */
23785 scope = TYPE_CONTEXT (type);
23786 if (scope && TYPE_P (scope))
23787 return dependent_type_p (scope);
23788 /* Don't use type_dependent_expression_p here, as it can lead
23789 to infinite recursion trying to determine whether a lambda
23790 nested in a lambda is dependent (c++/47687). */
23791 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23792 && DECL_LANG_SPECIFIC (scope)
23793 && DECL_TEMPLATE_INFO (scope)
23794 && (any_dependent_template_arguments_p
23795 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23796 return true;
23798 /* Other types are non-dependent. */
23799 return false;
23802 /* Returns TRUE if TYPE is dependent, in the sense of
23803 [temp.dep.type]. Note that a NULL type is considered dependent. */
23805 bool
23806 dependent_type_p (tree type)
23808 /* If there are no template parameters in scope, then there can't be
23809 any dependent types. */
23810 if (!processing_template_decl)
23812 /* If we are not processing a template, then nobody should be
23813 providing us with a dependent type. */
23814 gcc_assert (type);
23815 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23816 return false;
23819 /* If the type is NULL, we have not computed a type for the entity
23820 in question; in that case, the type is dependent. */
23821 if (!type)
23822 return true;
23824 /* Erroneous types can be considered non-dependent. */
23825 if (type == error_mark_node)
23826 return false;
23828 /* Getting here with global_type_node means we improperly called this
23829 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23830 gcc_checking_assert (type != global_type_node);
23832 /* If we have not already computed the appropriate value for TYPE,
23833 do so now. */
23834 if (!TYPE_DEPENDENT_P_VALID (type))
23836 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23837 TYPE_DEPENDENT_P_VALID (type) = 1;
23840 return TYPE_DEPENDENT_P (type);
23843 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23844 lookup. In other words, a dependent type that is not the current
23845 instantiation. */
23847 bool
23848 dependent_scope_p (tree scope)
23850 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23851 && !currently_open_class (scope));
23854 /* T is a SCOPE_REF; return whether we need to consider it
23855 instantiation-dependent so that we can check access at instantiation
23856 time even though we know which member it resolves to. */
23858 static bool
23859 instantiation_dependent_scope_ref_p (tree t)
23861 if (DECL_P (TREE_OPERAND (t, 1))
23862 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23863 && accessible_in_template_p (TREE_OPERAND (t, 0),
23864 TREE_OPERAND (t, 1)))
23865 return false;
23866 else
23867 return true;
23870 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23871 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23872 expression. */
23874 /* Note that this predicate is not appropriate for general expressions;
23875 only constant expressions (that satisfy potential_constant_expression)
23876 can be tested for value dependence. */
23878 bool
23879 value_dependent_expression_p (tree expression)
23881 if (!processing_template_decl || expression == NULL_TREE)
23882 return false;
23884 /* A name declared with a dependent type. */
23885 if (DECL_P (expression) && type_dependent_expression_p (expression))
23886 return true;
23888 switch (TREE_CODE (expression))
23890 case BASELINK:
23891 /* A dependent member function of the current instantiation. */
23892 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23894 case FUNCTION_DECL:
23895 /* A dependent member function of the current instantiation. */
23896 if (DECL_CLASS_SCOPE_P (expression)
23897 && dependent_type_p (DECL_CONTEXT (expression)))
23898 return true;
23899 break;
23901 case IDENTIFIER_NODE:
23902 /* A name that has not been looked up -- must be dependent. */
23903 return true;
23905 case TEMPLATE_PARM_INDEX:
23906 /* A non-type template parm. */
23907 return true;
23909 case CONST_DECL:
23910 /* A non-type template parm. */
23911 if (DECL_TEMPLATE_PARM_P (expression))
23912 return true;
23913 return value_dependent_expression_p (DECL_INITIAL (expression));
23915 case VAR_DECL:
23916 /* A constant with literal type and is initialized
23917 with an expression that is value-dependent.
23919 Note that a non-dependent parenthesized initializer will have
23920 already been replaced with its constant value, so if we see
23921 a TREE_LIST it must be dependent. */
23922 if (DECL_INITIAL (expression)
23923 && decl_constant_var_p (expression)
23924 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23925 /* cp_finish_decl doesn't fold reference initializers. */
23926 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23927 || type_dependent_expression_p (DECL_INITIAL (expression))
23928 || value_dependent_expression_p (DECL_INITIAL (expression))))
23929 return true;
23930 if (DECL_HAS_VALUE_EXPR_P (expression))
23932 tree value_expr = DECL_VALUE_EXPR (expression);
23933 if (type_dependent_expression_p (value_expr))
23934 return true;
23936 return false;
23938 case DYNAMIC_CAST_EXPR:
23939 case STATIC_CAST_EXPR:
23940 case CONST_CAST_EXPR:
23941 case REINTERPRET_CAST_EXPR:
23942 case CAST_EXPR:
23943 /* These expressions are value-dependent if the type to which
23944 the cast occurs is dependent or the expression being casted
23945 is value-dependent. */
23947 tree type = TREE_TYPE (expression);
23949 if (dependent_type_p (type))
23950 return true;
23952 /* A functional cast has a list of operands. */
23953 expression = TREE_OPERAND (expression, 0);
23954 if (!expression)
23956 /* If there are no operands, it must be an expression such
23957 as "int()". This should not happen for aggregate types
23958 because it would form non-constant expressions. */
23959 gcc_assert (cxx_dialect >= cxx11
23960 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23962 return false;
23965 if (TREE_CODE (expression) == TREE_LIST)
23966 return any_value_dependent_elements_p (expression);
23968 return value_dependent_expression_p (expression);
23971 case SIZEOF_EXPR:
23972 if (SIZEOF_EXPR_TYPE_P (expression))
23973 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23974 /* FALLTHRU */
23975 case ALIGNOF_EXPR:
23976 case TYPEID_EXPR:
23977 /* A `sizeof' expression is value-dependent if the operand is
23978 type-dependent or is a pack expansion. */
23979 expression = TREE_OPERAND (expression, 0);
23980 if (PACK_EXPANSION_P (expression))
23981 return true;
23982 else if (TYPE_P (expression))
23983 return dependent_type_p (expression);
23984 return instantiation_dependent_uneval_expression_p (expression);
23986 case AT_ENCODE_EXPR:
23987 /* An 'encode' expression is value-dependent if the operand is
23988 type-dependent. */
23989 expression = TREE_OPERAND (expression, 0);
23990 return dependent_type_p (expression);
23992 case NOEXCEPT_EXPR:
23993 expression = TREE_OPERAND (expression, 0);
23994 return instantiation_dependent_uneval_expression_p (expression);
23996 case SCOPE_REF:
23997 /* All instantiation-dependent expressions should also be considered
23998 value-dependent. */
23999 return instantiation_dependent_scope_ref_p (expression);
24001 case COMPONENT_REF:
24002 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24003 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24005 case NONTYPE_ARGUMENT_PACK:
24006 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24007 is value-dependent. */
24009 tree values = ARGUMENT_PACK_ARGS (expression);
24010 int i, len = TREE_VEC_LENGTH (values);
24012 for (i = 0; i < len; ++i)
24013 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24014 return true;
24016 return false;
24019 case TRAIT_EXPR:
24021 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24022 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
24023 || (type2 ? dependent_type_p (type2) : false));
24026 case MODOP_EXPR:
24027 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24028 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24030 case ARRAY_REF:
24031 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24032 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24034 case ADDR_EXPR:
24036 tree op = TREE_OPERAND (expression, 0);
24037 return (value_dependent_expression_p (op)
24038 || has_value_dependent_address (op));
24041 case REQUIRES_EXPR:
24042 /* Treat all requires-expressions as value-dependent so
24043 we don't try to fold them. */
24044 return true;
24046 case TYPE_REQ:
24047 return dependent_type_p (TREE_OPERAND (expression, 0));
24049 case CALL_EXPR:
24051 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24052 return true;
24053 tree fn = get_callee_fndecl (expression);
24054 int i, nargs;
24055 nargs = call_expr_nargs (expression);
24056 for (i = 0; i < nargs; ++i)
24058 tree op = CALL_EXPR_ARG (expression, i);
24059 /* In a call to a constexpr member function, look through the
24060 implicit ADDR_EXPR on the object argument so that it doesn't
24061 cause the call to be considered value-dependent. We also
24062 look through it in potential_constant_expression. */
24063 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24064 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24065 && TREE_CODE (op) == ADDR_EXPR)
24066 op = TREE_OPERAND (op, 0);
24067 if (value_dependent_expression_p (op))
24068 return true;
24070 return false;
24073 case TEMPLATE_ID_EXPR:
24074 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24075 type-dependent. */
24076 return type_dependent_expression_p (expression)
24077 || variable_concept_p (TREE_OPERAND (expression, 0));
24079 case CONSTRUCTOR:
24081 unsigned ix;
24082 tree val;
24083 if (dependent_type_p (TREE_TYPE (expression)))
24084 return true;
24085 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24086 if (value_dependent_expression_p (val))
24087 return true;
24088 return false;
24091 case STMT_EXPR:
24092 /* Treat a GNU statement expression as dependent to avoid crashing
24093 under instantiate_non_dependent_expr; it can't be constant. */
24094 return true;
24096 default:
24097 /* A constant expression is value-dependent if any subexpression is
24098 value-dependent. */
24099 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24101 case tcc_reference:
24102 case tcc_unary:
24103 case tcc_comparison:
24104 case tcc_binary:
24105 case tcc_expression:
24106 case tcc_vl_exp:
24108 int i, len = cp_tree_operand_length (expression);
24110 for (i = 0; i < len; i++)
24112 tree t = TREE_OPERAND (expression, i);
24114 /* In some cases, some of the operands may be missing.
24115 (For example, in the case of PREDECREMENT_EXPR, the
24116 amount to increment by may be missing.) That doesn't
24117 make the expression dependent. */
24118 if (t && value_dependent_expression_p (t))
24119 return true;
24122 break;
24123 default:
24124 break;
24126 break;
24129 /* The expression is not value-dependent. */
24130 return false;
24133 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24134 [temp.dep.expr]. Note that an expression with no type is
24135 considered dependent. Other parts of the compiler arrange for an
24136 expression with type-dependent subexpressions to have no type, so
24137 this function doesn't have to be fully recursive. */
24139 bool
24140 type_dependent_expression_p (tree expression)
24142 if (!processing_template_decl)
24143 return false;
24145 if (expression == NULL_TREE || expression == error_mark_node)
24146 return false;
24148 /* An unresolved name is always dependent. */
24149 if (identifier_p (expression)
24150 || TREE_CODE (expression) == USING_DECL
24151 || TREE_CODE (expression) == WILDCARD_DECL)
24152 return true;
24154 /* A fold expression is type-dependent. */
24155 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24156 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24157 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24158 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24159 return true;
24161 /* Some expression forms are never type-dependent. */
24162 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24163 || TREE_CODE (expression) == SIZEOF_EXPR
24164 || TREE_CODE (expression) == ALIGNOF_EXPR
24165 || TREE_CODE (expression) == AT_ENCODE_EXPR
24166 || TREE_CODE (expression) == NOEXCEPT_EXPR
24167 || TREE_CODE (expression) == TRAIT_EXPR
24168 || TREE_CODE (expression) == TYPEID_EXPR
24169 || TREE_CODE (expression) == DELETE_EXPR
24170 || TREE_CODE (expression) == VEC_DELETE_EXPR
24171 || TREE_CODE (expression) == THROW_EXPR
24172 || TREE_CODE (expression) == REQUIRES_EXPR)
24173 return false;
24175 /* The types of these expressions depends only on the type to which
24176 the cast occurs. */
24177 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24178 || TREE_CODE (expression) == STATIC_CAST_EXPR
24179 || TREE_CODE (expression) == CONST_CAST_EXPR
24180 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24181 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24182 || TREE_CODE (expression) == CAST_EXPR)
24183 return dependent_type_p (TREE_TYPE (expression));
24185 /* The types of these expressions depends only on the type created
24186 by the expression. */
24187 if (TREE_CODE (expression) == NEW_EXPR
24188 || TREE_CODE (expression) == VEC_NEW_EXPR)
24190 /* For NEW_EXPR tree nodes created inside a template, either
24191 the object type itself or a TREE_LIST may appear as the
24192 operand 1. */
24193 tree type = TREE_OPERAND (expression, 1);
24194 if (TREE_CODE (type) == TREE_LIST)
24195 /* This is an array type. We need to check array dimensions
24196 as well. */
24197 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24198 || value_dependent_expression_p
24199 (TREE_OPERAND (TREE_VALUE (type), 1));
24200 else
24201 return dependent_type_p (type);
24204 if (TREE_CODE (expression) == SCOPE_REF)
24206 tree scope = TREE_OPERAND (expression, 0);
24207 tree name = TREE_OPERAND (expression, 1);
24209 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24210 contains an identifier associated by name lookup with one or more
24211 declarations declared with a dependent type, or...a
24212 nested-name-specifier or qualified-id that names a member of an
24213 unknown specialization. */
24214 return (type_dependent_expression_p (name)
24215 || dependent_scope_p (scope));
24218 if (TREE_CODE (expression) == TEMPLATE_DECL
24219 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24220 return uses_outer_template_parms (expression);
24222 if (TREE_CODE (expression) == STMT_EXPR)
24223 expression = stmt_expr_value_expr (expression);
24225 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24227 tree elt;
24228 unsigned i;
24230 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24232 if (type_dependent_expression_p (elt))
24233 return true;
24235 return false;
24238 /* A static data member of the current instantiation with incomplete
24239 array type is type-dependent, as the definition and specializations
24240 can have different bounds. */
24241 if (VAR_P (expression)
24242 && DECL_CLASS_SCOPE_P (expression)
24243 && dependent_type_p (DECL_CONTEXT (expression))
24244 && VAR_HAD_UNKNOWN_BOUND (expression))
24245 return true;
24247 /* An array of unknown bound depending on a variadic parameter, eg:
24249 template<typename... Args>
24250 void foo (Args... args)
24252 int arr[] = { args... };
24255 template<int... vals>
24256 void bar ()
24258 int arr[] = { vals... };
24261 If the array has no length and has an initializer, it must be that
24262 we couldn't determine its length in cp_complete_array_type because
24263 it is dependent. */
24264 if (VAR_P (expression)
24265 && TREE_TYPE (expression) != NULL_TREE
24266 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24267 && !TYPE_DOMAIN (TREE_TYPE (expression))
24268 && DECL_INITIAL (expression))
24269 return true;
24271 /* A function or variable template-id is type-dependent if it has any
24272 dependent template arguments. */
24273 if (VAR_OR_FUNCTION_DECL_P (expression)
24274 && DECL_LANG_SPECIFIC (expression)
24275 && DECL_TEMPLATE_INFO (expression))
24277 /* Consider the innermost template arguments, since those are the ones
24278 that come from the template-id; the template arguments for the
24279 enclosing class do not make it type-dependent unless they are used in
24280 the type of the decl. */
24281 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24282 && (any_dependent_template_arguments_p
24283 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24284 return true;
24287 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24288 type-dependent. Checking this is important for functions with auto return
24289 type, which looks like a dependent type. */
24290 if (TREE_CODE (expression) == FUNCTION_DECL
24291 && !(DECL_CLASS_SCOPE_P (expression)
24292 && dependent_type_p (DECL_CONTEXT (expression)))
24293 && !(DECL_FRIEND_P (expression)
24294 && (!DECL_FRIEND_CONTEXT (expression)
24295 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24296 && !DECL_LOCAL_FUNCTION_P (expression))
24298 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24299 || undeduced_auto_decl (expression));
24300 return false;
24303 /* Always dependent, on the number of arguments if nothing else. */
24304 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24305 return true;
24307 if (TREE_TYPE (expression) == unknown_type_node)
24309 if (TREE_CODE (expression) == ADDR_EXPR)
24310 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24311 if (TREE_CODE (expression) == COMPONENT_REF
24312 || TREE_CODE (expression) == OFFSET_REF)
24314 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24315 return true;
24316 expression = TREE_OPERAND (expression, 1);
24317 if (identifier_p (expression))
24318 return false;
24320 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24321 if (TREE_CODE (expression) == SCOPE_REF)
24322 return false;
24324 if (BASELINK_P (expression))
24326 if (BASELINK_OPTYPE (expression)
24327 && dependent_type_p (BASELINK_OPTYPE (expression)))
24328 return true;
24329 expression = BASELINK_FUNCTIONS (expression);
24332 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24334 if (any_dependent_template_arguments_p
24335 (TREE_OPERAND (expression, 1)))
24336 return true;
24337 expression = TREE_OPERAND (expression, 0);
24338 if (identifier_p (expression))
24339 return true;
24342 gcc_assert (TREE_CODE (expression) == OVERLOAD
24343 || TREE_CODE (expression) == FUNCTION_DECL);
24345 for (lkp_iterator iter (expression); iter; ++iter)
24346 if (type_dependent_expression_p (*iter))
24347 return true;
24349 return false;
24352 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24354 /* Dependent type attributes might not have made it from the decl to
24355 the type yet. */
24356 if (DECL_P (expression)
24357 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24358 return true;
24360 return (dependent_type_p (TREE_TYPE (expression)));
24363 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24364 type-dependent if the expression refers to a member of the current
24365 instantiation and the type of the referenced member is dependent, or the
24366 class member access expression refers to a member of an unknown
24367 specialization.
24369 This function returns true if the OBJECT in such a class member access
24370 expression is of an unknown specialization. */
24372 bool
24373 type_dependent_object_expression_p (tree object)
24375 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24376 dependent. */
24377 if (TREE_CODE (object) == IDENTIFIER_NODE)
24378 return true;
24379 tree scope = TREE_TYPE (object);
24380 return (!scope || dependent_scope_p (scope));
24383 /* walk_tree callback function for instantiation_dependent_expression_p,
24384 below. Returns non-zero if a dependent subexpression is found. */
24386 static tree
24387 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24388 void * /*data*/)
24390 if (TYPE_P (*tp))
24392 /* We don't have to worry about decltype currently because decltype
24393 of an instantiation-dependent expr is a dependent type. This
24394 might change depending on the resolution of DR 1172. */
24395 *walk_subtrees = false;
24396 return NULL_TREE;
24398 enum tree_code code = TREE_CODE (*tp);
24399 switch (code)
24401 /* Don't treat an argument list as dependent just because it has no
24402 TREE_TYPE. */
24403 case TREE_LIST:
24404 case TREE_VEC:
24405 return NULL_TREE;
24407 case TEMPLATE_PARM_INDEX:
24408 return *tp;
24410 /* Handle expressions with type operands. */
24411 case SIZEOF_EXPR:
24412 case ALIGNOF_EXPR:
24413 case TYPEID_EXPR:
24414 case AT_ENCODE_EXPR:
24416 tree op = TREE_OPERAND (*tp, 0);
24417 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24418 op = TREE_TYPE (op);
24419 if (TYPE_P (op))
24421 if (dependent_type_p (op))
24422 return *tp;
24423 else
24425 *walk_subtrees = false;
24426 return NULL_TREE;
24429 break;
24432 case COMPONENT_REF:
24433 if (identifier_p (TREE_OPERAND (*tp, 1)))
24434 /* In a template, finish_class_member_access_expr creates a
24435 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24436 type-dependent, so that we can check access control at
24437 instantiation time (PR 42277). See also Core issue 1273. */
24438 return *tp;
24439 break;
24441 case SCOPE_REF:
24442 if (instantiation_dependent_scope_ref_p (*tp))
24443 return *tp;
24444 else
24445 break;
24447 /* Treat statement-expressions as dependent. */
24448 case BIND_EXPR:
24449 return *tp;
24451 /* Treat requires-expressions as dependent. */
24452 case REQUIRES_EXPR:
24453 return *tp;
24455 case CALL_EXPR:
24456 /* Treat calls to function concepts as dependent. */
24457 if (function_concept_check_p (*tp))
24458 return *tp;
24459 break;
24461 case TEMPLATE_ID_EXPR:
24462 /* And variable concepts. */
24463 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24464 return *tp;
24465 break;
24467 default:
24468 break;
24471 if (type_dependent_expression_p (*tp))
24472 return *tp;
24473 else
24474 return NULL_TREE;
24477 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24478 sense defined by the ABI:
24480 "An expression is instantiation-dependent if it is type-dependent
24481 or value-dependent, or it has a subexpression that is type-dependent
24482 or value-dependent."
24484 Except don't actually check value-dependence for unevaluated expressions,
24485 because in sizeof(i) we don't care about the value of i. Checking
24486 type-dependence will in turn check value-dependence of array bounds/template
24487 arguments as needed. */
24489 bool
24490 instantiation_dependent_uneval_expression_p (tree expression)
24492 tree result;
24494 if (!processing_template_decl)
24495 return false;
24497 if (expression == error_mark_node)
24498 return false;
24500 result = cp_walk_tree_without_duplicates (&expression,
24501 instantiation_dependent_r, NULL);
24502 return result != NULL_TREE;
24505 /* As above, but also check value-dependence of the expression as a whole. */
24507 bool
24508 instantiation_dependent_expression_p (tree expression)
24510 return (instantiation_dependent_uneval_expression_p (expression)
24511 || value_dependent_expression_p (expression));
24514 /* Like type_dependent_expression_p, but it also works while not processing
24515 a template definition, i.e. during substitution or mangling. */
24517 bool
24518 type_dependent_expression_p_push (tree expr)
24520 bool b;
24521 ++processing_template_decl;
24522 b = type_dependent_expression_p (expr);
24523 --processing_template_decl;
24524 return b;
24527 /* Returns TRUE if ARGS contains a type-dependent expression. */
24529 bool
24530 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24532 unsigned int i;
24533 tree arg;
24535 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24537 if (type_dependent_expression_p (arg))
24538 return true;
24540 return false;
24543 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24544 expressions) contains any type-dependent expressions. */
24546 bool
24547 any_type_dependent_elements_p (const_tree list)
24549 for (; list; list = TREE_CHAIN (list))
24550 if (type_dependent_expression_p (TREE_VALUE (list)))
24551 return true;
24553 return false;
24556 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24557 expressions) contains any value-dependent expressions. */
24559 bool
24560 any_value_dependent_elements_p (const_tree list)
24562 for (; list; list = TREE_CHAIN (list))
24563 if (value_dependent_expression_p (TREE_VALUE (list)))
24564 return true;
24566 return false;
24569 /* Returns TRUE if the ARG (a template argument) is dependent. */
24571 bool
24572 dependent_template_arg_p (tree arg)
24574 if (!processing_template_decl)
24575 return false;
24577 /* Assume a template argument that was wrongly written by the user
24578 is dependent. This is consistent with what
24579 any_dependent_template_arguments_p [that calls this function]
24580 does. */
24581 if (!arg || arg == error_mark_node)
24582 return true;
24584 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24585 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24587 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24588 return true;
24589 if (TREE_CODE (arg) == TEMPLATE_DECL)
24591 if (DECL_TEMPLATE_PARM_P (arg))
24592 return true;
24593 /* A member template of a dependent class is not necessarily
24594 type-dependent, but it is a dependent template argument because it
24595 will be a member of an unknown specialization to that template. */
24596 tree scope = CP_DECL_CONTEXT (arg);
24597 return TYPE_P (scope) && dependent_type_p (scope);
24599 else if (ARGUMENT_PACK_P (arg))
24601 tree args = ARGUMENT_PACK_ARGS (arg);
24602 int i, len = TREE_VEC_LENGTH (args);
24603 for (i = 0; i < len; ++i)
24605 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24606 return true;
24609 return false;
24611 else if (TYPE_P (arg))
24612 return dependent_type_p (arg);
24613 else
24614 return (type_dependent_expression_p (arg)
24615 || value_dependent_expression_p (arg));
24618 /* Returns true if ARGS (a collection of template arguments) contains
24619 any types that require structural equality testing. */
24621 bool
24622 any_template_arguments_need_structural_equality_p (tree args)
24624 int i;
24625 int j;
24627 if (!args)
24628 return false;
24629 if (args == error_mark_node)
24630 return true;
24632 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24634 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24635 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24637 tree arg = TREE_VEC_ELT (level, j);
24638 tree packed_args = NULL_TREE;
24639 int k, len = 1;
24641 if (ARGUMENT_PACK_P (arg))
24643 /* Look inside the argument pack. */
24644 packed_args = ARGUMENT_PACK_ARGS (arg);
24645 len = TREE_VEC_LENGTH (packed_args);
24648 for (k = 0; k < len; ++k)
24650 if (packed_args)
24651 arg = TREE_VEC_ELT (packed_args, k);
24653 if (error_operand_p (arg))
24654 return true;
24655 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24656 continue;
24657 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24658 return true;
24659 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24660 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24661 return true;
24666 return false;
24669 /* Returns true if ARGS (a collection of template arguments) contains
24670 any dependent arguments. */
24672 bool
24673 any_dependent_template_arguments_p (const_tree args)
24675 int i;
24676 int j;
24678 if (!args)
24679 return false;
24680 if (args == error_mark_node)
24681 return true;
24683 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24685 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24686 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24687 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24688 return true;
24691 return false;
24694 /* Returns TRUE if the template TMPL is type-dependent. */
24696 bool
24697 dependent_template_p (tree tmpl)
24699 if (TREE_CODE (tmpl) == OVERLOAD)
24701 for (lkp_iterator iter (tmpl); iter; ++iter)
24702 if (dependent_template_p (*iter))
24703 return true;
24704 return false;
24707 /* Template template parameters are dependent. */
24708 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24709 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24710 return true;
24711 /* So are names that have not been looked up. */
24712 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24713 return true;
24714 return false;
24717 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24719 bool
24720 dependent_template_id_p (tree tmpl, tree args)
24722 return (dependent_template_p (tmpl)
24723 || any_dependent_template_arguments_p (args));
24726 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24727 are dependent. */
24729 bool
24730 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24732 int i;
24734 if (!processing_template_decl)
24735 return false;
24737 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24739 tree decl = TREE_VEC_ELT (declv, i);
24740 tree init = TREE_VEC_ELT (initv, i);
24741 tree cond = TREE_VEC_ELT (condv, i);
24742 tree incr = TREE_VEC_ELT (incrv, i);
24744 if (type_dependent_expression_p (decl)
24745 || TREE_CODE (decl) == SCOPE_REF)
24746 return true;
24748 if (init && type_dependent_expression_p (init))
24749 return true;
24751 if (type_dependent_expression_p (cond))
24752 return true;
24754 if (COMPARISON_CLASS_P (cond)
24755 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24756 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24757 return true;
24759 if (TREE_CODE (incr) == MODOP_EXPR)
24761 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24762 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24763 return true;
24765 else if (type_dependent_expression_p (incr))
24766 return true;
24767 else if (TREE_CODE (incr) == MODIFY_EXPR)
24769 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24770 return true;
24771 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24773 tree t = TREE_OPERAND (incr, 1);
24774 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24775 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24776 return true;
24781 return false;
24784 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24785 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24786 no such TYPE can be found. Note that this function peers inside
24787 uninstantiated templates and therefore should be used only in
24788 extremely limited situations. ONLY_CURRENT_P restricts this
24789 peering to the currently open classes hierarchy (which is required
24790 when comparing types). */
24792 tree
24793 resolve_typename_type (tree type, bool only_current_p)
24795 tree scope;
24796 tree name;
24797 tree decl;
24798 int quals;
24799 tree pushed_scope;
24800 tree result;
24802 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24804 scope = TYPE_CONTEXT (type);
24805 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24806 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24807 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24808 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24809 identifier of the TYPENAME_TYPE anymore.
24810 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24811 TYPENAME_TYPE instead, we avoid messing up with a possible
24812 typedef variant case. */
24813 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24815 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24816 it first before we can figure out what NAME refers to. */
24817 if (TREE_CODE (scope) == TYPENAME_TYPE)
24819 if (TYPENAME_IS_RESOLVING_P (scope))
24820 /* Given a class template A with a dependent base with nested type C,
24821 typedef typename A::C::C C will land us here, as trying to resolve
24822 the initial A::C leads to the local C typedef, which leads back to
24823 A::C::C. So we break the recursion now. */
24824 return type;
24825 else
24826 scope = resolve_typename_type (scope, only_current_p);
24828 /* If we don't know what SCOPE refers to, then we cannot resolve the
24829 TYPENAME_TYPE. */
24830 if (!CLASS_TYPE_P (scope))
24831 return type;
24832 /* If this is a typedef, we don't want to look inside (c++/11987). */
24833 if (typedef_variant_p (type))
24834 return type;
24835 /* If SCOPE isn't the template itself, it will not have a valid
24836 TYPE_FIELDS list. */
24837 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24838 /* scope is either the template itself or a compatible instantiation
24839 like X<T>, so look up the name in the original template. */
24840 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24841 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24842 gcc_checking_assert (uses_template_parms (scope));
24843 /* If scope has no fields, it can't be a current instantiation. Check this
24844 before currently_open_class to avoid infinite recursion (71515). */
24845 if (!TYPE_FIELDS (scope))
24846 return type;
24847 /* If the SCOPE is not the current instantiation, there's no reason
24848 to look inside it. */
24849 if (only_current_p && !currently_open_class (scope))
24850 return type;
24851 /* Enter the SCOPE so that name lookup will be resolved as if we
24852 were in the class definition. In particular, SCOPE will no
24853 longer be considered a dependent type. */
24854 pushed_scope = push_scope (scope);
24855 /* Look up the declaration. */
24856 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24857 tf_warning_or_error);
24859 result = NULL_TREE;
24861 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24862 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24863 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24864 if (!decl)
24865 /*nop*/;
24866 else if (identifier_p (fullname)
24867 && TREE_CODE (decl) == TYPE_DECL)
24869 result = TREE_TYPE (decl);
24870 if (result == error_mark_node)
24871 result = NULL_TREE;
24873 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24874 && DECL_CLASS_TEMPLATE_P (decl))
24876 /* Obtain the template and the arguments. */
24877 tree tmpl = TREE_OPERAND (fullname, 0);
24878 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24880 /* We get here with a plain identifier because a previous tentative
24881 parse of the nested-name-specifier as part of a ptr-operator saw
24882 ::template X<A>. The use of ::template is necessary in a
24883 ptr-operator, but wrong in a declarator-id.
24885 [temp.names]: In a qualified-id of a declarator-id, the keyword
24886 template shall not appear at the top level. */
24887 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24888 "keyword %<template%> not allowed in declarator-id");
24889 tmpl = decl;
24891 tree args = TREE_OPERAND (fullname, 1);
24892 /* Instantiate the template. */
24893 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24894 /*entering_scope=*/true,
24895 tf_error | tf_user);
24896 if (result == error_mark_node)
24897 result = NULL_TREE;
24900 /* Leave the SCOPE. */
24901 if (pushed_scope)
24902 pop_scope (pushed_scope);
24904 /* If we failed to resolve it, return the original typename. */
24905 if (!result)
24906 return type;
24908 /* If lookup found a typename type, resolve that too. */
24909 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24911 /* Ill-formed programs can cause infinite recursion here, so we
24912 must catch that. */
24913 TYPENAME_IS_RESOLVING_P (result) = 1;
24914 result = resolve_typename_type (result, only_current_p);
24915 TYPENAME_IS_RESOLVING_P (result) = 0;
24918 /* Qualify the resulting type. */
24919 quals = cp_type_quals (type);
24920 if (quals)
24921 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24923 return result;
24926 /* EXPR is an expression which is not type-dependent. Return a proxy
24927 for EXPR that can be used to compute the types of larger
24928 expressions containing EXPR. */
24930 tree
24931 build_non_dependent_expr (tree expr)
24933 tree inner_expr;
24935 /* When checking, try to get a constant value for all non-dependent
24936 expressions in order to expose bugs in *_dependent_expression_p
24937 and constexpr. This can affect code generation, see PR70704, so
24938 only do this for -fchecking=2. */
24939 if (flag_checking > 1
24940 && cxx_dialect >= cxx11
24941 /* Don't do this during nsdmi parsing as it can lead to
24942 unexpected recursive instantiations. */
24943 && !parsing_nsdmi ()
24944 /* Don't do this during concept expansion either and for
24945 the same reason. */
24946 && !expanding_concept ())
24947 fold_non_dependent_expr (expr);
24949 /* Preserve OVERLOADs; the functions must be available to resolve
24950 types. */
24951 inner_expr = expr;
24952 if (TREE_CODE (inner_expr) == STMT_EXPR)
24953 inner_expr = stmt_expr_value_expr (inner_expr);
24954 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24955 inner_expr = TREE_OPERAND (inner_expr, 0);
24956 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24957 inner_expr = TREE_OPERAND (inner_expr, 1);
24958 if (is_overloaded_fn (inner_expr)
24959 || TREE_CODE (inner_expr) == OFFSET_REF)
24960 return expr;
24961 /* There is no need to return a proxy for a variable. */
24962 if (VAR_P (expr))
24963 return expr;
24964 /* Preserve string constants; conversions from string constants to
24965 "char *" are allowed, even though normally a "const char *"
24966 cannot be used to initialize a "char *". */
24967 if (TREE_CODE (expr) == STRING_CST)
24968 return expr;
24969 /* Preserve void and arithmetic constants, as an optimization -- there is no
24970 reason to create a new node. */
24971 if (TREE_CODE (expr) == VOID_CST
24972 || TREE_CODE (expr) == INTEGER_CST
24973 || TREE_CODE (expr) == REAL_CST)
24974 return expr;
24975 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24976 There is at least one place where we want to know that a
24977 particular expression is a throw-expression: when checking a ?:
24978 expression, there are special rules if the second or third
24979 argument is a throw-expression. */
24980 if (TREE_CODE (expr) == THROW_EXPR)
24981 return expr;
24983 /* Don't wrap an initializer list, we need to be able to look inside. */
24984 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24985 return expr;
24987 /* Don't wrap a dummy object, we need to be able to test for it. */
24988 if (is_dummy_object (expr))
24989 return expr;
24991 if (TREE_CODE (expr) == COND_EXPR)
24992 return build3 (COND_EXPR,
24993 TREE_TYPE (expr),
24994 TREE_OPERAND (expr, 0),
24995 (TREE_OPERAND (expr, 1)
24996 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24997 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24998 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24999 if (TREE_CODE (expr) == COMPOUND_EXPR
25000 && !COMPOUND_EXPR_OVERLOADED (expr))
25001 return build2 (COMPOUND_EXPR,
25002 TREE_TYPE (expr),
25003 TREE_OPERAND (expr, 0),
25004 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25006 /* If the type is unknown, it can't really be non-dependent */
25007 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25009 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25010 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25013 /* ARGS is a vector of expressions as arguments to a function call.
25014 Replace the arguments with equivalent non-dependent expressions.
25015 This modifies ARGS in place. */
25017 void
25018 make_args_non_dependent (vec<tree, va_gc> *args)
25020 unsigned int ix;
25021 tree arg;
25023 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25025 tree newarg = build_non_dependent_expr (arg);
25026 if (newarg != arg)
25027 (*args)[ix] = newarg;
25031 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25032 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25033 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25035 static tree
25036 make_auto_1 (tree name, bool set_canonical)
25038 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25039 TYPE_NAME (au) = build_decl (input_location,
25040 TYPE_DECL, name, au);
25041 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25042 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25043 (0, processing_template_decl + 1, processing_template_decl + 1,
25044 TYPE_NAME (au), NULL_TREE);
25045 if (set_canonical)
25046 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25047 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25048 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25050 return au;
25053 tree
25054 make_decltype_auto (void)
25056 return make_auto_1 (decltype_auto_identifier, true);
25059 tree
25060 make_auto (void)
25062 return make_auto_1 (auto_identifier, true);
25065 /* Return a C++17 deduction placeholder for class template TMPL. */
25067 tree
25068 make_template_placeholder (tree tmpl)
25070 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25071 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25072 return t;
25075 /* True iff T is a C++17 class template deduction placeholder. */
25077 bool
25078 template_placeholder_p (tree t)
25080 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25083 /* Make a "constrained auto" type-specifier. This is an
25084 auto type with constraints that must be associated after
25085 deduction. The constraint is formed from the given
25086 CONC and its optional sequence of arguments, which are
25087 non-null if written as partial-concept-id. */
25089 tree
25090 make_constrained_auto (tree con, tree args)
25092 tree type = make_auto_1 (auto_identifier, false);
25094 /* Build the constraint. */
25095 tree tmpl = DECL_TI_TEMPLATE (con);
25096 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25097 expr = build_concept_check (expr, type, args);
25099 tree constr = normalize_expression (expr);
25100 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25102 /* Our canonical type depends on the constraint. */
25103 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25105 /* Attach the constraint to the type declaration. */
25106 tree decl = TYPE_NAME (type);
25107 return decl;
25110 /* Given type ARG, return std::initializer_list<ARG>. */
25112 static tree
25113 listify (tree arg)
25115 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25117 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25119 gcc_rich_location richloc (input_location);
25120 maybe_add_include_fixit (&richloc, "<initializer_list>");
25121 error_at_rich_loc (&richloc,
25122 "deducing from brace-enclosed initializer list"
25123 " requires #include <initializer_list>");
25125 return error_mark_node;
25127 tree argvec = make_tree_vec (1);
25128 TREE_VEC_ELT (argvec, 0) = arg;
25130 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25131 NULL_TREE, 0, tf_warning_or_error);
25134 /* Replace auto in TYPE with std::initializer_list<auto>. */
25136 static tree
25137 listify_autos (tree type, tree auto_node)
25139 tree init_auto = listify (auto_node);
25140 tree argvec = make_tree_vec (1);
25141 TREE_VEC_ELT (argvec, 0) = init_auto;
25142 if (processing_template_decl)
25143 argvec = add_to_template_args (current_template_args (), argvec);
25144 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25147 /* Hash traits for hashing possibly constrained 'auto'
25148 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25150 struct auto_hash : default_hash_traits<tree>
25152 static inline hashval_t hash (tree);
25153 static inline bool equal (tree, tree);
25156 /* Hash the 'auto' T. */
25158 inline hashval_t
25159 auto_hash::hash (tree t)
25161 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25162 /* Matching constrained-type-specifiers denote the same template
25163 parameter, so hash the constraint. */
25164 return hash_placeholder_constraint (c);
25165 else
25166 /* But unconstrained autos are all separate, so just hash the pointer. */
25167 return iterative_hash_object (t, 0);
25170 /* Compare two 'auto's. */
25172 inline bool
25173 auto_hash::equal (tree t1, tree t2)
25175 if (t1 == t2)
25176 return true;
25178 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25179 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25181 /* Two unconstrained autos are distinct. */
25182 if (!c1 || !c2)
25183 return false;
25185 return equivalent_placeholder_constraints (c1, c2);
25188 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25189 constrained) auto, add it to the vector. */
25191 static int
25192 extract_autos_r (tree t, void *data)
25194 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25195 if (is_auto (t))
25197 /* All the autos were built with index 0; fix that up now. */
25198 tree *p = hash.find_slot (t, INSERT);
25199 unsigned idx;
25200 if (*p)
25201 /* If this is a repeated constrained-type-specifier, use the index we
25202 chose before. */
25203 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25204 else
25206 /* Otherwise this is new, so use the current count. */
25207 *p = t;
25208 idx = hash.elements () - 1;
25210 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25213 /* Always keep walking. */
25214 return 0;
25217 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25218 says they can appear anywhere in the type. */
25220 static tree
25221 extract_autos (tree type)
25223 hash_set<tree> visited;
25224 hash_table<auto_hash> hash (2);
25226 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25228 tree tree_vec = make_tree_vec (hash.elements());
25229 for (hash_table<auto_hash>::iterator iter = hash.begin();
25230 iter != hash.end(); ++iter)
25232 tree elt = *iter;
25233 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25234 TREE_VEC_ELT (tree_vec, i)
25235 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25238 return tree_vec;
25241 /* The stem for deduction guide names. */
25242 const char *const dguide_base = "__dguide_";
25244 /* Return the name for a deduction guide for class template TMPL. */
25246 tree
25247 dguide_name (tree tmpl)
25249 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25250 tree tname = TYPE_IDENTIFIER (type);
25251 char *buf = (char *) alloca (1 + strlen (dguide_base)
25252 + IDENTIFIER_LENGTH (tname));
25253 memcpy (buf, dguide_base, strlen (dguide_base));
25254 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25255 IDENTIFIER_LENGTH (tname) + 1);
25256 tree dname = get_identifier (buf);
25257 TREE_TYPE (dname) = type;
25258 return dname;
25261 /* True if NAME is the name of a deduction guide. */
25263 bool
25264 dguide_name_p (tree name)
25266 return (TREE_TYPE (name)
25267 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25268 strlen (dguide_base)));
25271 /* True if FN is a deduction guide. */
25273 bool
25274 deduction_guide_p (const_tree fn)
25276 if (DECL_P (fn))
25277 if (tree name = DECL_NAME (fn))
25278 return dguide_name_p (name);
25279 return false;
25282 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25284 bool
25285 copy_guide_p (const_tree fn)
25287 gcc_assert (deduction_guide_p (fn));
25288 if (!DECL_ARTIFICIAL (fn))
25289 return false;
25290 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25291 return (TREE_CHAIN (parms) == void_list_node
25292 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25295 /* True if FN is a guide generated from a constructor template. */
25297 bool
25298 template_guide_p (const_tree fn)
25300 gcc_assert (deduction_guide_p (fn));
25301 if (!DECL_ARTIFICIAL (fn))
25302 return false;
25303 tree tmpl = DECL_TI_TEMPLATE (fn);
25304 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25305 return PRIMARY_TEMPLATE_P (org);
25306 return false;
25309 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25310 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25311 template parameter types. Note that the handling of template template
25312 parameters relies on current_template_parms being set appropriately for the
25313 new template. */
25315 static tree
25316 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25317 tree tsubst_args, tsubst_flags_t complain)
25319 tree oldidx = get_template_parm_index (olddecl);
25321 tree newtype;
25322 if (TREE_CODE (olddecl) == TYPE_DECL
25323 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25325 tree oldtype = TREE_TYPE (olddecl);
25326 newtype = cxx_make_type (TREE_CODE (oldtype));
25327 TYPE_MAIN_VARIANT (newtype) = newtype;
25328 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25329 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25330 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25332 else
25333 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25334 complain, NULL_TREE);
25336 tree newdecl
25337 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25338 DECL_NAME (olddecl), newtype);
25339 SET_DECL_TEMPLATE_PARM_P (newdecl);
25341 tree newidx;
25342 if (TREE_CODE (olddecl) == TYPE_DECL
25343 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25345 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25346 = build_template_parm_index (index, level, level,
25347 newdecl, newtype);
25348 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25349 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25350 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25351 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25353 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25355 DECL_TEMPLATE_RESULT (newdecl)
25356 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25357 DECL_NAME (olddecl), newtype);
25358 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25359 // First create a copy (ttargs) of tsubst_args with an
25360 // additional level for the template template parameter's own
25361 // template parameters (ttparms).
25362 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25363 (DECL_TEMPLATE_PARMS (olddecl)));
25364 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25365 tree ttargs = make_tree_vec (depth + 1);
25366 for (int i = 0; i < depth; ++i)
25367 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25368 TREE_VEC_ELT (ttargs, depth)
25369 = template_parms_level_to_args (ttparms);
25370 // Substitute ttargs into ttparms to fix references to
25371 // other template parameters.
25372 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25373 complain);
25374 // Now substitute again with args based on tparms, to reduce
25375 // the level of the ttparms.
25376 ttargs = current_template_args ();
25377 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25378 complain);
25379 // Finally, tack the adjusted parms onto tparms.
25380 ttparms = tree_cons (size_int (depth), ttparms,
25381 current_template_parms);
25382 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25385 else
25387 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25388 tree newconst
25389 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25390 TREE_CODE (oldconst),
25391 DECL_NAME (oldconst), newtype);
25392 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25393 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25394 SET_DECL_TEMPLATE_PARM_P (newconst);
25395 newidx = build_template_parm_index (index, level, level,
25396 newconst, newtype);
25397 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25398 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25399 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25402 return newdecl;
25405 /* Returns a C++17 class deduction guide template based on the constructor
25406 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25407 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25409 static tree
25410 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25412 tree type, tparms, targs, fparms, fargs, ci;
25413 bool memtmpl = false;
25414 bool explicit_p;
25415 location_t loc;
25416 tree fn_tmpl = NULL_TREE;
25418 if (TYPE_P (ctor))
25420 type = ctor;
25421 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25422 if (copy_p)
25424 type = TREE_TYPE (type);
25425 fparms = tree_cons (NULL_TREE, type, void_list_node);
25427 else
25428 fparms = void_list_node;
25430 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25431 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25432 targs = CLASSTYPE_TI_ARGS (type);
25433 ci = NULL_TREE;
25434 fargs = NULL_TREE;
25435 loc = DECL_SOURCE_LOCATION (ctmpl);
25436 explicit_p = false;
25438 else
25440 ++processing_template_decl;
25442 fn_tmpl
25443 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25444 : DECL_TI_TEMPLATE (ctor));
25445 if (outer_args)
25446 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25447 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25449 type = DECL_CONTEXT (ctor);
25451 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25452 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25453 fully specialized args for the enclosing class. Strip those off, as
25454 the deduction guide won't have those template parameters. */
25455 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25456 TMPL_PARMS_DEPTH (tparms));
25457 /* Discard the 'this' parameter. */
25458 fparms = FUNCTION_ARG_CHAIN (ctor);
25459 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25460 ci = get_constraints (ctor);
25461 loc = DECL_SOURCE_LOCATION (ctor);
25462 explicit_p = DECL_NONCONVERTING_P (ctor);
25464 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25466 memtmpl = true;
25468 /* For a member template constructor, we need to flatten the two
25469 template parameter lists into one, and then adjust the function
25470 signature accordingly. This gets...complicated. */
25471 tree save_parms = current_template_parms;
25473 /* For a member template we should have two levels of parms/args, one
25474 for the class and one for the constructor. We stripped
25475 specialized args for further enclosing classes above. */
25476 const int depth = 2;
25477 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25479 /* Template args for translating references to the two-level template
25480 parameters into references to the one-level template parameters we
25481 are creating. */
25482 tree tsubst_args = copy_node (targs);
25483 TMPL_ARGS_LEVEL (tsubst_args, depth)
25484 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25486 /* Template parms for the constructor template. */
25487 tree ftparms = TREE_VALUE (tparms);
25488 unsigned flen = TREE_VEC_LENGTH (ftparms);
25489 /* Template parms for the class template. */
25490 tparms = TREE_CHAIN (tparms);
25491 tree ctparms = TREE_VALUE (tparms);
25492 unsigned clen = TREE_VEC_LENGTH (ctparms);
25493 /* Template parms for the deduction guide start as a copy of the
25494 template parms for the class. We set current_template_parms for
25495 lookup_template_class_1. */
25496 current_template_parms = tparms = copy_node (tparms);
25497 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25498 for (unsigned i = 0; i < clen; ++i)
25499 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25501 /* Now we need to rewrite the constructor parms to append them to the
25502 class parms. */
25503 for (unsigned i = 0; i < flen; ++i)
25505 unsigned index = i + clen;
25506 unsigned level = 1;
25507 tree oldelt = TREE_VEC_ELT (ftparms, i);
25508 tree olddecl = TREE_VALUE (oldelt);
25509 tree newdecl = rewrite_template_parm (olddecl, index, level,
25510 tsubst_args, complain);
25511 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25512 tsubst_args, complain, ctor);
25513 tree list = build_tree_list (newdef, newdecl);
25514 TEMPLATE_PARM_CONSTRAINTS (list)
25515 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25516 tsubst_args, complain, ctor);
25517 TREE_VEC_ELT (new_vec, index) = list;
25518 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25521 /* Now we have a final set of template parms to substitute into the
25522 function signature. */
25523 targs = template_parms_to_args (tparms);
25524 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25525 complain, ctor);
25526 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25527 if (ci)
25528 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25530 current_template_parms = save_parms;
25532 --processing_template_decl;
25535 if (!memtmpl)
25537 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25538 tparms = copy_node (tparms);
25539 INNERMOST_TEMPLATE_PARMS (tparms)
25540 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25543 tree fntype = build_function_type (type, fparms);
25544 tree ded_fn = build_lang_decl_loc (loc,
25545 FUNCTION_DECL,
25546 dguide_name (type), fntype);
25547 DECL_ARGUMENTS (ded_fn) = fargs;
25548 DECL_ARTIFICIAL (ded_fn) = true;
25549 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25550 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25551 DECL_ARTIFICIAL (ded_tmpl) = true;
25552 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25553 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25554 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25555 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25556 if (DECL_P (ctor))
25557 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25558 if (ci)
25559 set_constraints (ded_tmpl, ci);
25561 return ded_tmpl;
25564 /* Deduce template arguments for the class template placeholder PTYPE for
25565 template TMPL based on the initializer INIT, and return the resulting
25566 type. */
25568 static tree
25569 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25570 tsubst_flags_t complain)
25572 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25574 /* We should have handled this in the caller. */
25575 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25576 return ptype;
25577 if (complain & tf_error)
25578 error ("non-class template %qT used without template arguments", tmpl);
25579 return error_mark_node;
25582 tree type = TREE_TYPE (tmpl);
25584 bool try_list_ctor = false;
25586 vec<tree,va_gc> *args;
25587 if (init == NULL_TREE
25588 || TREE_CODE (init) == TREE_LIST)
25589 args = make_tree_vector_from_list (init);
25590 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25592 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25593 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25595 /* As an exception, the first phase in 16.3.1.7 (considering the
25596 initializer list as a single argument) is omitted if the
25597 initializer list consists of a single expression of type cv U,
25598 where U is a specialization of C or a class derived from a
25599 specialization of C. */
25600 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25601 tree etype = TREE_TYPE (elt);
25603 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25604 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25605 int err = unify (tparms, targs, type, etype,
25606 UNIFY_ALLOW_DERIVED, /*explain*/false);
25607 if (err == 0)
25608 try_list_ctor = false;
25609 ggc_free (targs);
25611 if (try_list_ctor || is_std_init_list (type))
25612 args = make_tree_vector_single (init);
25613 else
25614 args = make_tree_vector_from_ctor (init);
25616 else
25617 args = make_tree_vector_single (init);
25619 tree dname = dguide_name (tmpl);
25620 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25621 /*type*/false, /*complain*/false,
25622 /*hidden*/false);
25623 bool elided = false;
25624 if (cands == error_mark_node)
25625 cands = NULL_TREE;
25627 /* Prune explicit deduction guides in copy-initialization context. */
25628 if (flags & LOOKUP_ONLYCONVERTING)
25630 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25631 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25632 elided = true;
25634 if (elided)
25636 /* Found a nonconverting guide, prune the candidates. */
25637 tree pruned = NULL_TREE;
25638 for (lkp_iterator iter (cands); iter; ++iter)
25639 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25640 pruned = lookup_add (*iter, pruned);
25642 cands = pruned;
25646 tree outer_args = NULL_TREE;
25647 if (DECL_CLASS_SCOPE_P (tmpl)
25648 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25650 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25651 type = TREE_TYPE (most_general_template (tmpl));
25654 bool saw_ctor = false;
25655 // FIXME cache artificial deduction guides
25656 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25658 tree guide = build_deduction_guide (*iter, outer_args, complain);
25659 if ((flags & LOOKUP_ONLYCONVERTING)
25660 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25661 elided = true;
25662 else
25663 cands = lookup_add (guide, cands);
25665 saw_ctor = true;
25668 tree call = error_mark_node;
25670 /* If this is list-initialization and the class has a list constructor, first
25671 try deducing from the list as a single argument, as [over.match.list]. */
25672 tree list_cands = NULL_TREE;
25673 if (try_list_ctor && cands)
25674 for (lkp_iterator iter (cands); iter; ++iter)
25676 tree dg = *iter;
25677 if (is_list_ctor (dg))
25678 list_cands = lookup_add (dg, list_cands);
25680 if (list_cands)
25682 ++cp_unevaluated_operand;
25683 call = build_new_function_call (list_cands, &args, tf_decltype);
25684 --cp_unevaluated_operand;
25686 if (call == error_mark_node)
25688 /* That didn't work, now try treating the list as a sequence of
25689 arguments. */
25690 release_tree_vector (args);
25691 args = make_tree_vector_from_ctor (init);
25695 /* Maybe generate an implicit deduction guide. */
25696 if (call == error_mark_node && args->length () < 2)
25698 tree gtype = NULL_TREE;
25700 if (args->length () == 1)
25701 /* Generate a copy guide. */
25702 gtype = build_reference_type (type);
25703 else if (!saw_ctor)
25704 /* Generate a default guide. */
25705 gtype = type;
25707 if (gtype)
25709 tree guide = build_deduction_guide (gtype, outer_args, complain);
25710 cands = lookup_add (guide, cands);
25714 if (elided && !cands)
25716 error ("cannot deduce template arguments for copy-initialization"
25717 " of %qT, as it has no non-explicit deduction guides or "
25718 "user-declared constructors", type);
25719 return error_mark_node;
25721 else if (!cands && call == error_mark_node)
25723 error ("cannot deduce template arguments of %qT, as it has no viable "
25724 "deduction guides", type);
25725 return error_mark_node;
25728 if (call == error_mark_node)
25730 ++cp_unevaluated_operand;
25731 call = build_new_function_call (cands, &args, tf_decltype);
25732 --cp_unevaluated_operand;
25735 if (call == error_mark_node && (complain & tf_warning_or_error))
25737 error ("class template argument deduction failed:");
25739 ++cp_unevaluated_operand;
25740 call = build_new_function_call (cands, &args, complain | tf_decltype);
25741 --cp_unevaluated_operand;
25743 if (elided)
25744 inform (input_location, "explicit deduction guides not considered "
25745 "for copy-initialization");
25748 release_tree_vector (args);
25750 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25753 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25754 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25756 tree
25757 do_auto_deduction (tree type, tree init, tree auto_node)
25759 return do_auto_deduction (type, init, auto_node,
25760 tf_warning_or_error,
25761 adc_unspecified);
25764 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25765 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25766 The CONTEXT determines the context in which auto deduction is performed
25767 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25768 OUTER_TARGS are used during template argument deduction
25769 (context == adc_unify) to properly substitute the result, and is ignored
25770 in other contexts.
25772 For partial-concept-ids, extra args may be appended to the list of deduced
25773 template arguments prior to determining constraint satisfaction. */
25775 tree
25776 do_auto_deduction (tree type, tree init, tree auto_node,
25777 tsubst_flags_t complain, auto_deduction_context context,
25778 tree outer_targs, int flags)
25780 tree targs;
25782 if (init == error_mark_node)
25783 return error_mark_node;
25785 if (init && type_dependent_expression_p (init)
25786 && context != adc_unify)
25787 /* Defining a subset of type-dependent expressions that we can deduce
25788 from ahead of time isn't worth the trouble. */
25789 return type;
25791 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25792 /* C++17 class template argument deduction. */
25793 return do_class_deduction (type, tmpl, init, flags, complain);
25795 if (TREE_TYPE (init) == NULL_TREE)
25796 /* Nothing we can do with this, even in deduction context. */
25797 return type;
25799 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25800 with either a new invented type template parameter U or, if the
25801 initializer is a braced-init-list (8.5.4), with
25802 std::initializer_list<U>. */
25803 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25805 if (!DIRECT_LIST_INIT_P (init))
25806 type = listify_autos (type, auto_node);
25807 else if (CONSTRUCTOR_NELTS (init) == 1)
25808 init = CONSTRUCTOR_ELT (init, 0)->value;
25809 else
25811 if (complain & tf_warning_or_error)
25813 if (permerror (input_location, "direct-list-initialization of "
25814 "%<auto%> requires exactly one element"))
25815 inform (input_location,
25816 "for deduction to %<std::initializer_list%>, use copy-"
25817 "list-initialization (i.e. add %<=%> before the %<{%>)");
25819 type = listify_autos (type, auto_node);
25823 if (type == error_mark_node)
25824 return error_mark_node;
25826 init = resolve_nondeduced_context (init, complain);
25828 if (context == adc_decomp_type
25829 && auto_node == type
25830 && init != error_mark_node
25831 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25832 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25833 and initializer has array type, deduce cv-qualified array type. */
25834 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25835 complain);
25836 else if (AUTO_IS_DECLTYPE (auto_node))
25838 bool id = (DECL_P (init)
25839 || ((TREE_CODE (init) == COMPONENT_REF
25840 || TREE_CODE (init) == SCOPE_REF)
25841 && !REF_PARENTHESIZED_P (init)));
25842 targs = make_tree_vec (1);
25843 TREE_VEC_ELT (targs, 0)
25844 = finish_decltype_type (init, id, tf_warning_or_error);
25845 if (type != auto_node)
25847 if (complain & tf_error)
25848 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25849 return error_mark_node;
25852 else
25854 tree parms = build_tree_list (NULL_TREE, type);
25855 tree tparms;
25857 if (flag_concepts)
25858 tparms = extract_autos (type);
25859 else
25861 tparms = make_tree_vec (1);
25862 TREE_VEC_ELT (tparms, 0)
25863 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25866 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25867 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25868 DEDUCE_CALL, LOOKUP_NORMAL,
25869 NULL, /*explain_p=*/false);
25870 if (val > 0)
25872 if (processing_template_decl)
25873 /* Try again at instantiation time. */
25874 return type;
25875 if (type && type != error_mark_node
25876 && (complain & tf_error))
25877 /* If type is error_mark_node a diagnostic must have been
25878 emitted by now. Also, having a mention to '<type error>'
25879 in the diagnostic is not really useful to the user. */
25881 if (cfun && auto_node == current_function_auto_return_pattern
25882 && LAMBDA_FUNCTION_P (current_function_decl))
25883 error ("unable to deduce lambda return type from %qE", init);
25884 else
25885 error ("unable to deduce %qT from %qE", type, init);
25886 type_unification_real (tparms, targs, parms, &init, 1, 0,
25887 DEDUCE_CALL, LOOKUP_NORMAL,
25888 NULL, /*explain_p=*/true);
25890 return error_mark_node;
25894 /* Check any placeholder constraints against the deduced type. */
25895 if (flag_concepts && !processing_template_decl)
25896 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25898 /* Use the deduced type to check the associated constraints. If we
25899 have a partial-concept-id, rebuild the argument list so that
25900 we check using the extra arguments. */
25901 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25902 tree cargs = CHECK_CONSTR_ARGS (constr);
25903 if (TREE_VEC_LENGTH (cargs) > 1)
25905 cargs = copy_node (cargs);
25906 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25908 else
25909 cargs = targs;
25910 if (!constraints_satisfied_p (constr, cargs))
25912 if (complain & tf_warning_or_error)
25914 switch (context)
25916 case adc_unspecified:
25917 case adc_unify:
25918 error("placeholder constraints not satisfied");
25919 break;
25920 case adc_variable_type:
25921 case adc_decomp_type:
25922 error ("deduced initializer does not satisfy "
25923 "placeholder constraints");
25924 break;
25925 case adc_return_type:
25926 error ("deduced return type does not satisfy "
25927 "placeholder constraints");
25928 break;
25929 case adc_requirement:
25930 error ("deduced expression type does not satisfy "
25931 "placeholder constraints");
25932 break;
25934 diagnose_constraints (input_location, constr, targs);
25936 return error_mark_node;
25940 if (processing_template_decl && context != adc_unify)
25941 outer_targs = current_template_args ();
25942 targs = add_to_template_args (outer_targs, targs);
25943 return tsubst (type, targs, complain, NULL_TREE);
25946 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25947 result. */
25949 tree
25950 splice_late_return_type (tree type, tree late_return_type)
25952 if (is_auto (type))
25954 if (late_return_type)
25955 return late_return_type;
25957 tree idx = get_template_parm_index (type);
25958 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25959 /* In an abbreviated function template we didn't know we were dealing
25960 with a function template when we saw the auto return type, so update
25961 it to have the correct level. */
25962 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25964 return type;
25967 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25968 'decltype(auto)' or a deduced class template. */
25970 bool
25971 is_auto (const_tree type)
25973 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25974 && (TYPE_IDENTIFIER (type) == auto_identifier
25975 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25976 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25977 return true;
25978 else
25979 return false;
25982 /* for_each_template_parm callback for type_uses_auto. */
25985 is_auto_r (tree tp, void */*data*/)
25987 return is_auto (tp);
25990 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25991 a use of `auto'. Returns NULL_TREE otherwise. */
25993 tree
25994 type_uses_auto (tree type)
25996 if (type == NULL_TREE)
25997 return NULL_TREE;
25998 else if (flag_concepts)
26000 /* The Concepts TS allows multiple autos in one type-specifier; just
26001 return the first one we find, do_auto_deduction will collect all of
26002 them. */
26003 if (uses_template_parms (type))
26004 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26005 /*visited*/NULL, /*nondeduced*/true);
26006 else
26007 return NULL_TREE;
26009 else
26010 return find_type_usage (type, is_auto);
26013 /* For a given template T, return the vector of typedefs referenced
26014 in T for which access check is needed at T instantiation time.
26015 T is either a FUNCTION_DECL or a RECORD_TYPE.
26016 Those typedefs were added to T by the function
26017 append_type_to_template_for_access_check. */
26019 vec<qualified_typedef_usage_t, va_gc> *
26020 get_types_needing_access_check (tree t)
26022 tree ti;
26023 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26025 if (!t || t == error_mark_node)
26026 return NULL;
26028 if (!(ti = get_template_info (t)))
26029 return NULL;
26031 if (CLASS_TYPE_P (t)
26032 || TREE_CODE (t) == FUNCTION_DECL)
26034 if (!TI_TEMPLATE (ti))
26035 return NULL;
26037 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26040 return result;
26043 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26044 tied to T. That list of typedefs will be access checked at
26045 T instantiation time.
26046 T is either a FUNCTION_DECL or a RECORD_TYPE.
26047 TYPE_DECL is a TYPE_DECL node representing a typedef.
26048 SCOPE is the scope through which TYPE_DECL is accessed.
26049 LOCATION is the location of the usage point of TYPE_DECL.
26051 This function is a subroutine of
26052 append_type_to_template_for_access_check. */
26054 static void
26055 append_type_to_template_for_access_check_1 (tree t,
26056 tree type_decl,
26057 tree scope,
26058 location_t location)
26060 qualified_typedef_usage_t typedef_usage;
26061 tree ti;
26063 if (!t || t == error_mark_node)
26064 return;
26066 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26067 || CLASS_TYPE_P (t))
26068 && type_decl
26069 && TREE_CODE (type_decl) == TYPE_DECL
26070 && scope);
26072 if (!(ti = get_template_info (t)))
26073 return;
26075 gcc_assert (TI_TEMPLATE (ti));
26077 typedef_usage.typedef_decl = type_decl;
26078 typedef_usage.context = scope;
26079 typedef_usage.locus = location;
26081 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26084 /* Append TYPE_DECL to the template TEMPL.
26085 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26086 At TEMPL instanciation time, TYPE_DECL will be checked to see
26087 if it can be accessed through SCOPE.
26088 LOCATION is the location of the usage point of TYPE_DECL.
26090 e.g. consider the following code snippet:
26092 class C
26094 typedef int myint;
26097 template<class U> struct S
26099 C::myint mi; // <-- usage point of the typedef C::myint
26102 S<char> s;
26104 At S<char> instantiation time, we need to check the access of C::myint
26105 In other words, we need to check the access of the myint typedef through
26106 the C scope. For that purpose, this function will add the myint typedef
26107 and the scope C through which its being accessed to a list of typedefs
26108 tied to the template S. That list will be walked at template instantiation
26109 time and access check performed on each typedefs it contains.
26110 Note that this particular code snippet should yield an error because
26111 myint is private to C. */
26113 void
26114 append_type_to_template_for_access_check (tree templ,
26115 tree type_decl,
26116 tree scope,
26117 location_t location)
26119 qualified_typedef_usage_t *iter;
26120 unsigned i;
26122 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26124 /* Make sure we don't append the type to the template twice. */
26125 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26126 if (iter->typedef_decl == type_decl && scope == iter->context)
26127 return;
26129 append_type_to_template_for_access_check_1 (templ, type_decl,
26130 scope, location);
26133 /* Convert the generic type parameters in PARM that match the types given in the
26134 range [START_IDX, END_IDX) from the current_template_parms into generic type
26135 packs. */
26137 tree
26138 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26140 tree current = current_template_parms;
26141 int depth = TMPL_PARMS_DEPTH (current);
26142 current = INNERMOST_TEMPLATE_PARMS (current);
26143 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26145 for (int i = 0; i < start_idx; ++i)
26146 TREE_VEC_ELT (replacement, i)
26147 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26149 for (int i = start_idx; i < end_idx; ++i)
26151 /* Create a distinct parameter pack type from the current parm and add it
26152 to the replacement args to tsubst below into the generic function
26153 parameter. */
26155 tree o = TREE_TYPE (TREE_VALUE
26156 (TREE_VEC_ELT (current, i)));
26157 tree t = copy_type (o);
26158 TEMPLATE_TYPE_PARM_INDEX (t)
26159 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26160 o, 0, 0, tf_none);
26161 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26162 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26163 TYPE_MAIN_VARIANT (t) = t;
26164 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26165 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26166 TREE_VEC_ELT (replacement, i) = t;
26167 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26170 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26171 TREE_VEC_ELT (replacement, i)
26172 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26174 /* If there are more levels then build up the replacement with the outer
26175 template parms. */
26176 if (depth > 1)
26177 replacement = add_to_template_args (template_parms_to_args
26178 (TREE_CHAIN (current_template_parms)),
26179 replacement);
26181 return tsubst (parm, replacement, tf_none, NULL_TREE);
26184 /* Entries in the decl_constraint hash table. */
26185 struct GTY((for_user)) constr_entry
26187 tree decl;
26188 tree ci;
26191 /* Hashing function and equality for constraint entries. */
26192 struct constr_hasher : ggc_ptr_hash<constr_entry>
26194 static hashval_t hash (constr_entry *e)
26196 return (hashval_t)DECL_UID (e->decl);
26199 static bool equal (constr_entry *e1, constr_entry *e2)
26201 return e1->decl == e2->decl;
26205 /* A mapping from declarations to constraint information. Note that
26206 both templates and their underlying declarations are mapped to the
26207 same constraint information.
26209 FIXME: This is defined in pt.c because garbage collection
26210 code is not being generated for constraint.cc. */
26212 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26214 /* Returns the template constraints of declaration T. If T is not
26215 constrained, return NULL_TREE. Note that T must be non-null. */
26217 tree
26218 get_constraints (tree t)
26220 if (!flag_concepts)
26221 return NULL_TREE;
26223 gcc_assert (DECL_P (t));
26224 if (TREE_CODE (t) == TEMPLATE_DECL)
26225 t = DECL_TEMPLATE_RESULT (t);
26226 constr_entry elt = { t, NULL_TREE };
26227 constr_entry* found = decl_constraints->find (&elt);
26228 if (found)
26229 return found->ci;
26230 else
26231 return NULL_TREE;
26234 /* Associate the given constraint information CI with the declaration
26235 T. If T is a template, then the constraints are associated with
26236 its underlying declaration. Don't build associations if CI is
26237 NULL_TREE. */
26239 void
26240 set_constraints (tree t, tree ci)
26242 if (!ci)
26243 return;
26244 gcc_assert (t && flag_concepts);
26245 if (TREE_CODE (t) == TEMPLATE_DECL)
26246 t = DECL_TEMPLATE_RESULT (t);
26247 gcc_assert (!get_constraints (t));
26248 constr_entry elt = {t, ci};
26249 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26250 constr_entry* entry = ggc_alloc<constr_entry> ();
26251 *entry = elt;
26252 *slot = entry;
26255 /* Remove the associated constraints of the declaration T. */
26257 void
26258 remove_constraints (tree t)
26260 gcc_assert (DECL_P (t));
26261 if (TREE_CODE (t) == TEMPLATE_DECL)
26262 t = DECL_TEMPLATE_RESULT (t);
26264 constr_entry elt = {t, NULL_TREE};
26265 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26266 if (slot)
26267 decl_constraints->clear_slot (slot);
26270 /* Memoized satisfaction results for declarations. This
26271 maps the pair (constraint_info, arguments) to the result computed
26272 by constraints_satisfied_p. */
26274 struct GTY((for_user)) constraint_sat_entry
26276 tree ci;
26277 tree args;
26278 tree result;
26281 /* Hashing function and equality for constraint entries. */
26283 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26285 static hashval_t hash (constraint_sat_entry *e)
26287 hashval_t val = iterative_hash_object(e->ci, 0);
26288 return iterative_hash_template_arg (e->args, val);
26291 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26293 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26297 /* Memoized satisfaction results for concept checks. */
26299 struct GTY((for_user)) concept_spec_entry
26301 tree tmpl;
26302 tree args;
26303 tree result;
26306 /* Hashing function and equality for constraint entries. */
26308 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26310 static hashval_t hash (concept_spec_entry *e)
26312 return hash_tmpl_and_args (e->tmpl, e->args);
26315 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26317 ++comparing_specializations;
26318 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26319 --comparing_specializations;
26320 return eq;
26324 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26325 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26327 /* Search for a memoized satisfaction result. Returns one of the
26328 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26330 tree
26331 lookup_constraint_satisfaction (tree ci, tree args)
26333 constraint_sat_entry elt = { ci, args, NULL_TREE };
26334 constraint_sat_entry* found = constraint_memos->find (&elt);
26335 if (found)
26336 return found->result;
26337 else
26338 return NULL_TREE;
26341 /* Memoize the result of a satisfication test. Returns the saved result. */
26343 tree
26344 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26346 constraint_sat_entry elt = {ci, args, result};
26347 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26348 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26349 *entry = elt;
26350 *slot = entry;
26351 return result;
26354 /* Search for a memoized satisfaction result for a concept. */
26356 tree
26357 lookup_concept_satisfaction (tree tmpl, tree args)
26359 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26360 concept_spec_entry* found = concept_memos->find (&elt);
26361 if (found)
26362 return found->result;
26363 else
26364 return NULL_TREE;
26367 /* Memoize the result of a concept check. Returns the saved result. */
26369 tree
26370 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26372 concept_spec_entry elt = {tmpl, args, result};
26373 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26374 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26375 *entry = elt;
26376 *slot = entry;
26377 return result;
26380 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26382 /* Returns a prior concept specialization. This returns the substituted
26383 and normalized constraints defined by the concept. */
26385 tree
26386 get_concept_expansion (tree tmpl, tree args)
26388 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26389 concept_spec_entry* found = concept_expansions->find (&elt);
26390 if (found)
26391 return found->result;
26392 else
26393 return NULL_TREE;
26396 /* Save a concept expansion for later. */
26398 tree
26399 save_concept_expansion (tree tmpl, tree args, tree def)
26401 concept_spec_entry elt = {tmpl, args, def};
26402 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26403 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26404 *entry = elt;
26405 *slot = entry;
26406 return def;
26409 static hashval_t
26410 hash_subsumption_args (tree t1, tree t2)
26412 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26413 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26414 int val = 0;
26415 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26416 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26417 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26418 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26419 return val;
26422 /* Compare the constraints of two subsumption entries. The LEFT1 and
26423 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26424 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26426 static bool
26427 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26429 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26430 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26431 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26432 CHECK_CONSTR_ARGS (right1)))
26433 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26434 CHECK_CONSTR_ARGS (right2));
26435 return false;
26438 /* Key/value pair for learning and memoizing subsumption results. This
26439 associates a pair of check constraints (including arguments) with
26440 a boolean value indicating the result. */
26442 struct GTY((for_user)) subsumption_entry
26444 tree t1;
26445 tree t2;
26446 bool result;
26449 /* Hashing function and equality for constraint entries. */
26451 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26453 static hashval_t hash (subsumption_entry *e)
26455 return hash_subsumption_args (e->t1, e->t2);
26458 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26460 ++comparing_specializations;
26461 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26462 --comparing_specializations;
26463 return eq;
26467 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26469 /* Search for a previously cached subsumption result. */
26471 bool*
26472 lookup_subsumption_result (tree t1, tree t2)
26474 subsumption_entry elt = { t1, t2, false };
26475 subsumption_entry* found = subsumption_table->find (&elt);
26476 if (found)
26477 return &found->result;
26478 else
26479 return 0;
26482 /* Save a subsumption result. */
26484 bool
26485 save_subsumption_result (tree t1, tree t2, bool result)
26487 subsumption_entry elt = {t1, t2, result};
26488 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26489 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26490 *entry = elt;
26491 *slot = entry;
26492 return result;
26495 /* Set up the hash table for constraint association. */
26497 void
26498 init_constraint_processing (void)
26500 if (!flag_concepts)
26501 return;
26503 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26504 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26505 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26506 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26507 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26510 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26511 0..N-1. */
26513 void
26514 declare_integer_pack (void)
26516 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26517 build_function_type_list (integer_type_node,
26518 integer_type_node,
26519 NULL_TREE),
26520 NULL_TREE, ECF_CONST);
26521 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26522 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26525 /* Set up the hash tables for template instantiations. */
26527 void
26528 init_template_processing (void)
26530 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26531 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26533 if (cxx_dialect >= cxx11)
26534 declare_integer_pack ();
26537 /* Print stats about the template hash tables for -fstats. */
26539 void
26540 print_template_statistics (void)
26542 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26543 "%f collisions\n", (long) decl_specializations->size (),
26544 (long) decl_specializations->elements (),
26545 decl_specializations->collisions ());
26546 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26547 "%f collisions\n", (long) type_specializations->size (),
26548 (long) type_specializations->elements (),
26549 type_specializations->collisions ());
26552 #include "gt-cp-pt.h"