Instantiate default arguments/member initializers once.
[official-gcc.git] / gcc / cp / pt.c
blobeec89dbce5b19dda91dcdfd6a4b5d1deeb278e2c
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 ()
81 : saved (local_specializations)
83 local_specializations = new hash_map<tree, tree>;
86 local_specialization_stack::~local_specialization_stack ()
88 delete local_specializations;
89 local_specializations = saved;
92 /* True if we've recursed into fn_type_unification too many times. */
93 static bool excessive_deduction_depth;
95 struct GTY((for_user)) spec_entry
97 tree tmpl;
98 tree args;
99 tree spec;
102 struct spec_hasher : ggc_ptr_hash<spec_entry>
104 static hashval_t hash (spec_entry *);
105 static bool equal (spec_entry *, spec_entry *);
108 static GTY (()) hash_table<spec_hasher> *decl_specializations;
110 static GTY (()) hash_table<spec_hasher> *type_specializations;
112 /* Contains canonical template parameter types. The vector is indexed by
113 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
114 TREE_LIST, whose TREE_VALUEs contain the canonical template
115 parameters of various types and levels. */
116 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
118 #define UNIFY_ALLOW_NONE 0
119 #define UNIFY_ALLOW_MORE_CV_QUAL 1
120 #define UNIFY_ALLOW_LESS_CV_QUAL 2
121 #define UNIFY_ALLOW_DERIVED 4
122 #define UNIFY_ALLOW_INTEGER 8
123 #define UNIFY_ALLOW_OUTER_LEVEL 16
124 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
125 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
127 enum template_base_result {
128 tbr_incomplete_type,
129 tbr_ambiguous_baseclass,
130 tbr_success
133 static void push_access_scope (tree);
134 static void pop_access_scope (tree);
135 static bool resolve_overloaded_unification (tree, tree, tree, tree,
136 unification_kind_t, int,
137 bool);
138 static int try_one_overload (tree, tree, tree, tree, tree,
139 unification_kind_t, int, bool, bool);
140 static int unify (tree, tree, tree, tree, int, bool);
141 static void add_pending_template (tree);
142 static tree reopen_tinst_level (struct tinst_level *);
143 static tree tsubst_initializer_list (tree, tree);
144 static tree get_partial_spec_bindings (tree, tree, tree);
145 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
146 bool, bool);
147 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
148 bool, bool);
149 static void tsubst_enum (tree, tree, tree);
150 static tree add_to_template_args (tree, tree);
151 static tree add_outermost_template_args (tree, tree);
152 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
153 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
154 tree);
155 static int type_unification_real (tree, tree, tree, const tree *,
156 unsigned int, int, unification_kind_t, int,
157 vec<deferred_access_check, va_gc> **,
158 bool);
159 static void note_template_header (int);
160 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
161 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
162 static tree convert_template_argument (tree, tree, tree,
163 tsubst_flags_t, int, tree);
164 static tree for_each_template_parm (tree, tree_fn_t, void*,
165 hash_set<tree> *, bool, tree_fn_t = NULL);
166 static tree expand_template_argument_pack (tree);
167 static tree build_template_parm_index (int, int, int, tree, tree);
168 static bool inline_needs_template_parms (tree, bool);
169 static void push_inline_template_parms_recursive (tree, int);
170 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
171 static int mark_template_parm (tree, void *);
172 static int template_parm_this_level_p (tree, void *);
173 static tree tsubst_friend_function (tree, tree);
174 static tree tsubst_friend_class (tree, tree);
175 static int can_complete_type_without_circularity (tree);
176 static tree get_bindings (tree, tree, tree, bool);
177 static int template_decl_level (tree);
178 static int check_cv_quals_for_unify (int, tree, tree);
179 static void template_parm_level_and_index (tree, int*, int*);
180 static int unify_pack_expansion (tree, tree, tree,
181 tree, unification_kind_t, bool, bool);
182 static tree copy_template_args (tree);
183 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
185 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
186 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
187 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
188 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
190 static bool check_specialization_scope (void);
191 static tree process_partial_specialization (tree);
192 static void set_current_access_from_decl (tree);
193 static enum template_base_result get_template_base (tree, tree, tree, tree,
194 bool , tree *);
195 static tree try_class_unification (tree, tree, tree, tree, bool);
196 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
197 tree, tree);
198 static bool template_template_parm_bindings_ok_p (tree, tree);
199 static void tsubst_default_arguments (tree, tsubst_flags_t);
200 static tree for_each_template_parm_r (tree *, int *, void *);
201 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
202 static void copy_default_args_to_explicit_spec (tree);
203 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
204 static bool dependent_template_arg_p (tree);
205 static bool any_template_arguments_need_structural_equality_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t);
209 static void perform_typedefs_access_check (tree tmpl, tree targs);
210 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
211 location_t);
212 static tree listify (tree);
213 static tree listify_autos (tree, tree);
214 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
215 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
216 static bool complex_alias_template_p (const_tree tmpl);
217 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
218 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 static tree make_argument_pack (tree);
221 /* Make the current scope suitable for access checking when we are
222 processing T. T can be FUNCTION_DECL for instantiated function
223 template, VAR_DECL for static member variable, or TYPE_DECL for
224 alias template (needed by instantiate_decl). */
226 static void
227 push_access_scope (tree t)
229 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
230 || TREE_CODE (t) == TYPE_DECL);
232 if (DECL_FRIEND_CONTEXT (t))
233 push_nested_class (DECL_FRIEND_CONTEXT (t));
234 else if (DECL_CLASS_SCOPE_P (t))
235 push_nested_class (DECL_CONTEXT (t));
236 else
237 push_to_top_level ();
239 if (TREE_CODE (t) == FUNCTION_DECL)
241 saved_access_scope = tree_cons
242 (NULL_TREE, current_function_decl, saved_access_scope);
243 current_function_decl = t;
247 /* Restore the scope set up by push_access_scope. T is the node we
248 are processing. */
250 static void
251 pop_access_scope (tree t)
253 if (TREE_CODE (t) == FUNCTION_DECL)
255 current_function_decl = TREE_VALUE (saved_access_scope);
256 saved_access_scope = TREE_CHAIN (saved_access_scope);
259 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
260 pop_nested_class ();
261 else
262 pop_from_top_level ();
265 /* Do any processing required when DECL (a member template
266 declaration) is finished. Returns the TEMPLATE_DECL corresponding
267 to DECL, unless it is a specialization, in which case the DECL
268 itself is returned. */
270 tree
271 finish_member_template_decl (tree decl)
273 if (decl == error_mark_node)
274 return error_mark_node;
276 gcc_assert (DECL_P (decl));
278 if (TREE_CODE (decl) == TYPE_DECL)
280 tree type;
282 type = TREE_TYPE (decl);
283 if (type == error_mark_node)
284 return error_mark_node;
285 if (MAYBE_CLASS_TYPE_P (type)
286 && CLASSTYPE_TEMPLATE_INFO (type)
287 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
289 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
290 check_member_template (tmpl);
291 return tmpl;
293 return NULL_TREE;
295 else if (TREE_CODE (decl) == FIELD_DECL)
296 error ("data member %qD cannot be a member template", decl);
297 else if (DECL_TEMPLATE_INFO (decl))
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
301 check_member_template (DECL_TI_TEMPLATE (decl));
302 return DECL_TI_TEMPLATE (decl);
304 else
305 return decl;
307 else
308 error ("invalid member template declaration %qD", decl);
310 return error_mark_node;
313 /* Create a template info node. */
315 tree
316 build_template_info (tree template_decl, tree template_args)
318 tree result = make_node (TEMPLATE_INFO);
319 TI_TEMPLATE (result) = template_decl;
320 TI_ARGS (result) = template_args;
321 return result;
324 /* Return the template info node corresponding to T, whatever T is. */
326 tree
327 get_template_info (const_tree t)
329 tree tinfo = NULL_TREE;
331 if (!t || t == error_mark_node)
332 return NULL;
334 if (TREE_CODE (t) == NAMESPACE_DECL
335 || TREE_CODE (t) == PARM_DECL)
336 return NULL;
338 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
339 tinfo = DECL_TEMPLATE_INFO (t);
341 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
342 t = TREE_TYPE (t);
344 if (OVERLOAD_TYPE_P (t))
345 tinfo = TYPE_TEMPLATE_INFO (t);
346 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
347 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
349 return tinfo;
352 /* Returns the template nesting level of the indicated class TYPE.
354 For example, in:
355 template <class T>
356 struct A
358 template <class U>
359 struct B {};
362 A<T>::B<U> has depth two, while A<T> has depth one.
363 Both A<T>::B<int> and A<int>::B<U> have depth one, if
364 they are instantiations, not specializations.
366 This function is guaranteed to return 0 if passed NULL_TREE so
367 that, for example, `template_class_depth (current_class_type)' is
368 always safe. */
371 template_class_depth (tree type)
373 int depth;
375 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
377 tree tinfo = get_template_info (type);
379 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
380 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
381 ++depth;
383 if (DECL_P (type))
384 type = CP_DECL_CONTEXT (type);
385 else if (LAMBDA_TYPE_P (type))
386 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
387 else
388 type = CP_TYPE_CONTEXT (type);
391 return depth;
394 /* Subroutine of maybe_begin_member_template_processing.
395 Returns true if processing DECL needs us to push template parms. */
397 static bool
398 inline_needs_template_parms (tree decl, bool nsdmi)
400 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
401 return false;
403 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
404 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
407 /* Subroutine of maybe_begin_member_template_processing.
408 Push the template parms in PARMS, starting from LEVELS steps into the
409 chain, and ending at the beginning, since template parms are listed
410 innermost first. */
412 static void
413 push_inline_template_parms_recursive (tree parmlist, int levels)
415 tree parms = TREE_VALUE (parmlist);
416 int i;
418 if (levels > 1)
419 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
421 ++processing_template_decl;
422 current_template_parms
423 = tree_cons (size_int (processing_template_decl),
424 parms, current_template_parms);
425 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
427 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
428 NULL);
429 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
431 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
433 if (error_operand_p (parm))
434 continue;
436 gcc_assert (DECL_P (parm));
438 switch (TREE_CODE (parm))
440 case TYPE_DECL:
441 case TEMPLATE_DECL:
442 pushdecl (parm);
443 break;
445 case PARM_DECL:
446 /* Push the CONST_DECL. */
447 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
448 break;
450 default:
451 gcc_unreachable ();
456 /* Restore the template parameter context for a member template, a
457 friend template defined in a class definition, or a non-template
458 member of template class. */
460 void
461 maybe_begin_member_template_processing (tree decl)
463 tree parms;
464 int levels = 0;
465 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
467 if (nsdmi)
469 tree ctx = DECL_CONTEXT (decl);
470 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
471 /* Disregard full specializations (c++/60999). */
472 && uses_template_parms (ctx)
473 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
476 if (inline_needs_template_parms (decl, nsdmi))
478 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
479 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
481 if (DECL_TEMPLATE_SPECIALIZATION (decl))
483 --levels;
484 parms = TREE_CHAIN (parms);
487 push_inline_template_parms_recursive (parms, levels);
490 /* Remember how many levels of template parameters we pushed so that
491 we can pop them later. */
492 inline_parm_levels.safe_push (levels);
495 /* Undo the effects of maybe_begin_member_template_processing. */
497 void
498 maybe_end_member_template_processing (void)
500 int i;
501 int last;
503 if (inline_parm_levels.length () == 0)
504 return;
506 last = inline_parm_levels.pop ();
507 for (i = 0; i < last; ++i)
509 --processing_template_decl;
510 current_template_parms = TREE_CHAIN (current_template_parms);
511 poplevel (0, 0, 0);
515 /* Return a new template argument vector which contains all of ARGS,
516 but has as its innermost set of arguments the EXTRA_ARGS. */
518 static tree
519 add_to_template_args (tree args, tree extra_args)
521 tree new_args;
522 int extra_depth;
523 int i;
524 int j;
526 if (args == NULL_TREE || extra_args == error_mark_node)
527 return extra_args;
529 extra_depth = TMPL_ARGS_DEPTH (extra_args);
530 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
532 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
533 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
535 for (j = 1; j <= extra_depth; ++j, ++i)
536 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
538 return new_args;
541 /* Like add_to_template_args, but only the outermost ARGS are added to
542 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
543 (EXTRA_ARGS) levels are added. This function is used to combine
544 the template arguments from a partial instantiation with the
545 template arguments used to attain the full instantiation from the
546 partial instantiation. */
548 static tree
549 add_outermost_template_args (tree args, tree extra_args)
551 tree new_args;
553 /* If there are more levels of EXTRA_ARGS than there are ARGS,
554 something very fishy is going on. */
555 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
557 /* If *all* the new arguments will be the EXTRA_ARGS, just return
558 them. */
559 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
560 return extra_args;
562 /* For the moment, we make ARGS look like it contains fewer levels. */
563 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
565 new_args = add_to_template_args (args, extra_args);
567 /* Now, we restore ARGS to its full dimensions. */
568 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
570 return new_args;
573 /* Return the N levels of innermost template arguments from the ARGS. */
575 tree
576 get_innermost_template_args (tree args, int n)
578 tree new_args;
579 int extra_levels;
580 int i;
582 gcc_assert (n >= 0);
584 /* If N is 1, just return the innermost set of template arguments. */
585 if (n == 1)
586 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
588 /* If we're not removing anything, just return the arguments we were
589 given. */
590 extra_levels = TMPL_ARGS_DEPTH (args) - n;
591 gcc_assert (extra_levels >= 0);
592 if (extra_levels == 0)
593 return args;
595 /* Make a new set of arguments, not containing the outer arguments. */
596 new_args = make_tree_vec (n);
597 for (i = 1; i <= n; ++i)
598 SET_TMPL_ARGS_LEVEL (new_args, i,
599 TMPL_ARGS_LEVEL (args, i + extra_levels));
601 return new_args;
604 /* The inverse of get_innermost_template_args: Return all but the innermost
605 EXTRA_LEVELS levels of template arguments from the ARGS. */
607 static tree
608 strip_innermost_template_args (tree args, int extra_levels)
610 tree new_args;
611 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
612 int i;
614 gcc_assert (n >= 0);
616 /* If N is 1, just return the outermost set of template arguments. */
617 if (n == 1)
618 return TMPL_ARGS_LEVEL (args, 1);
620 /* If we're not removing anything, just return the arguments we were
621 given. */
622 gcc_assert (extra_levels >= 0);
623 if (extra_levels == 0)
624 return args;
626 /* Make a new set of arguments, not containing the inner arguments. */
627 new_args = make_tree_vec (n);
628 for (i = 1; i <= n; ++i)
629 SET_TMPL_ARGS_LEVEL (new_args, i,
630 TMPL_ARGS_LEVEL (args, i));
632 return new_args;
635 /* We've got a template header coming up; push to a new level for storing
636 the parms. */
638 void
639 begin_template_parm_list (void)
641 /* We use a non-tag-transparent scope here, which causes pushtag to
642 put tags in this scope, rather than in the enclosing class or
643 namespace scope. This is the right thing, since we want
644 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
645 global template class, push_template_decl handles putting the
646 TEMPLATE_DECL into top-level scope. For a nested template class,
647 e.g.:
649 template <class T> struct S1 {
650 template <class T> struct S2 {};
653 pushtag contains special code to insert the TEMPLATE_DECL for S2
654 at the right scope. */
655 begin_scope (sk_template_parms, NULL);
656 ++processing_template_decl;
657 ++processing_template_parmlist;
658 note_template_header (0);
660 /* Add a dummy parameter level while we process the parameter list. */
661 current_template_parms
662 = tree_cons (size_int (processing_template_decl),
663 make_tree_vec (0),
664 current_template_parms);
667 /* This routine is called when a specialization is declared. If it is
668 invalid to declare a specialization here, an error is reported and
669 false is returned, otherwise this routine will return true. */
671 static bool
672 check_specialization_scope (void)
674 tree scope = current_scope ();
676 /* [temp.expl.spec]
678 An explicit specialization shall be declared in the namespace of
679 which the template is a member, or, for member templates, in the
680 namespace of which the enclosing class or enclosing class
681 template is a member. An explicit specialization of a member
682 function, member class or static data member of a class template
683 shall be declared in the namespace of which the class template
684 is a member. */
685 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
687 error ("explicit specialization in non-namespace scope %qD", scope);
688 return false;
691 /* [temp.expl.spec]
693 In an explicit specialization declaration for a member of a class
694 template or a member template that appears in namespace scope,
695 the member template and some of its enclosing class templates may
696 remain unspecialized, except that the declaration shall not
697 explicitly specialize a class member template if its enclosing
698 class templates are not explicitly specialized as well. */
699 if (current_template_parms)
701 error ("enclosing class templates are not explicitly specialized");
702 return false;
705 return true;
708 /* We've just seen template <>. */
710 bool
711 begin_specialization (void)
713 begin_scope (sk_template_spec, NULL);
714 note_template_header (1);
715 return check_specialization_scope ();
718 /* Called at then end of processing a declaration preceded by
719 template<>. */
721 void
722 end_specialization (void)
724 finish_scope ();
725 reset_specialization ();
728 /* Any template <>'s that we have seen thus far are not referring to a
729 function specialization. */
731 void
732 reset_specialization (void)
734 processing_specialization = 0;
735 template_header_count = 0;
738 /* We've just seen a template header. If SPECIALIZATION is nonzero,
739 it was of the form template <>. */
741 static void
742 note_template_header (int specialization)
744 processing_specialization = specialization;
745 template_header_count++;
748 /* We're beginning an explicit instantiation. */
750 void
751 begin_explicit_instantiation (void)
753 gcc_assert (!processing_explicit_instantiation);
754 processing_explicit_instantiation = true;
758 void
759 end_explicit_instantiation (void)
761 gcc_assert (processing_explicit_instantiation);
762 processing_explicit_instantiation = false;
765 /* An explicit specialization or partial specialization of TMPL is being
766 declared. Check that the namespace in which the specialization is
767 occurring is permissible. Returns false iff it is invalid to
768 specialize TMPL in the current namespace. */
770 static bool
771 check_specialization_namespace (tree tmpl)
773 tree tpl_ns = decl_namespace_context (tmpl);
775 /* [tmpl.expl.spec]
777 An explicit specialization shall be declared in a namespace enclosing the
778 specialized template. An explicit specialization whose declarator-id is
779 not qualified shall be declared in the nearest enclosing namespace of the
780 template, or, if the namespace is inline (7.3.1), any namespace from its
781 enclosing namespace set. */
782 if (current_scope() != DECL_CONTEXT (tmpl)
783 && !at_namespace_scope_p ())
785 error ("specialization of %qD must appear at namespace scope", tmpl);
786 return false;
789 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
790 /* Same or enclosing namespace. */
791 return true;
792 else
794 permerror (input_location,
795 "specialization of %qD in different namespace", tmpl);
796 inform (DECL_SOURCE_LOCATION (tmpl),
797 " from definition of %q#D", tmpl);
798 return false;
802 /* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
805 static void
806 check_explicit_instantiation_namespace (tree spec)
808 tree ns;
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns = decl_namespace_context (spec);
813 if (!is_nested_namespace (current_namespace, ns))
814 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec, current_namespace, ns);
819 // Returns the type of a template specialization only if that
820 // specialization needs to be defined. Otherwise (e.g., if the type has
821 // already been defined), the function returns NULL_TREE.
822 static tree
823 maybe_new_partial_specialization (tree type)
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
828 // template<typename T>
829 // struct S;
831 // template<typename T>
832 // struct S<T*>;
834 // Here, S<T*> is an implicit instantiation of S whose type
835 // is incomplete.
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
837 return type;
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
842 // template<typename T>
843 // requires Integral<T>
844 // struct S<T*>;
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
857 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 tree args = CLASSTYPE_TI_ARGS (type);
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms)
863 return NULL_TREE;
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
867 return NULL_TREE;
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr = current_template_constraints ();
873 if (type == TREE_TYPE (tmpl))
875 tree main_constr = get_constraints (tmpl);
876 if (equivalent_constraints (type_constr, main_constr))
877 return NULL_TREE;
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
883 while (specs)
885 tree spec_tmpl = TREE_VALUE (specs);
886 tree spec_args = TREE_PURPOSE (specs);
887 tree spec_constr = get_constraints (spec_tmpl);
888 if (comp_template_args (args, spec_args)
889 && equivalent_constraints (type_constr, spec_constr))
890 return NULL_TREE;
891 specs = TREE_CHAIN (specs);
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t = make_class_type (TREE_CODE (type));
897 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
898 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
905 // Build the corresponding type decl.
906 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
907 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
908 DECL_SOURCE_LOCATION (d) = input_location;
910 return t;
913 return NULL_TREE;
916 /* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
919 tree
920 maybe_process_partial_specialization (tree type)
922 tree context;
924 if (type == error_mark_node)
925 return error_mark_node;
927 /* A lambda that appears in specialization context is not itself a
928 specialization. */
929 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
930 return type;
932 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
934 error ("name of class shadows template template parameter %qD",
935 TYPE_NAME (type));
936 return error_mark_node;
939 context = TYPE_CONTEXT (type);
941 if (TYPE_ALIAS_P (type))
943 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
945 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
946 error ("specialization of alias template %qD",
947 TI_TEMPLATE (tinfo));
948 else
949 error ("explicit specialization of non-template %qT", type);
950 return error_mark_node;
952 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
954 /* This is for ordinary explicit specialization and partial
955 specialization of a template class such as:
957 template <> class C<int>;
961 template <class T> class C<T*>;
963 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
965 if (tree t = maybe_new_partial_specialization (type))
967 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
968 && !at_namespace_scope_p ())
969 return error_mark_node;
970 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
971 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
972 if (processing_template_decl)
974 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
975 if (decl == error_mark_node)
976 return error_mark_node;
977 return TREE_TYPE (decl);
980 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
981 error ("specialization of %qT after instantiation", type);
982 else if (errorcount && !processing_specialization
983 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
984 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
985 /* Trying to define a specialization either without a template<> header
986 or in an inappropriate place. We've already given an error, so just
987 bail now so we don't actually define the specialization. */
988 return error_mark_node;
990 else if (CLASS_TYPE_P (type)
991 && !CLASSTYPE_USE_TEMPLATE (type)
992 && CLASSTYPE_TEMPLATE_INFO (type)
993 && context && CLASS_TYPE_P (context)
994 && CLASSTYPE_TEMPLATE_INFO (context))
996 /* This is for an explicit specialization of member class
997 template according to [temp.expl.spec/18]:
999 template <> template <class U> class C<int>::D;
1001 The context `C<int>' must be an implicit instantiation.
1002 Otherwise this is just a member class template declared
1003 earlier like:
1005 template <> class C<int> { template <class U> class D; };
1006 template <> template <class U> class C<int>::D;
1008 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1009 while in the second case, `C<int>::D' is a primary template
1010 and `C<T>::D' may not exist. */
1012 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1013 && !COMPLETE_TYPE_P (type))
1015 tree t;
1016 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1018 if (current_namespace
1019 != decl_namespace_context (tmpl))
1021 permerror (input_location,
1022 "specializing %q#T in different namespace", type);
1023 permerror (DECL_SOURCE_LOCATION (tmpl),
1024 " from definition of %q#D", tmpl);
1027 /* Check for invalid specialization after instantiation:
1029 template <> template <> class C<int>::D<int>;
1030 template <> template <class U> class C<int>::D; */
1032 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1033 t; t = TREE_CHAIN (t))
1035 tree inst = TREE_VALUE (t);
1036 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1037 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1039 /* We already have a full specialization of this partial
1040 instantiation, or a full specialization has been
1041 looked up but not instantiated. Reassign it to the
1042 new member specialization template. */
1043 spec_entry elt;
1044 spec_entry *entry;
1046 elt.tmpl = most_general_template (tmpl);
1047 elt.args = CLASSTYPE_TI_ARGS (inst);
1048 elt.spec = inst;
1050 type_specializations->remove_elt (&elt);
1052 elt.tmpl = tmpl;
1053 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1055 spec_entry **slot
1056 = type_specializations->find_slot (&elt, INSERT);
1057 entry = ggc_alloc<spec_entry> ();
1058 *entry = elt;
1059 *slot = entry;
1061 else
1062 /* But if we've had an implicit instantiation, that's a
1063 problem ([temp.expl.spec]/6). */
1064 error ("specialization %qT after instantiation %qT",
1065 type, inst);
1068 /* Mark TYPE as a specialization. And as a result, we only
1069 have one level of template argument for the innermost
1070 class template. */
1071 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1072 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1073 CLASSTYPE_TI_ARGS (type)
1074 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1077 else if (processing_specialization)
1079 /* Someday C++0x may allow for enum template specialization. */
1080 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1081 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1082 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1083 "of %qD not allowed by ISO C++", type);
1084 else
1086 error ("explicit specialization of non-template %qT", type);
1087 return error_mark_node;
1091 return type;
1094 /* Returns nonzero if we can optimize the retrieval of specializations
1095 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1096 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1098 static inline bool
1099 optimize_specialization_lookup_p (tree tmpl)
1101 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1102 && DECL_CLASS_SCOPE_P (tmpl)
1103 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1104 parameter. */
1105 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1106 /* The optimized lookup depends on the fact that the
1107 template arguments for the member function template apply
1108 purely to the containing class, which is not true if the
1109 containing class is an explicit or partial
1110 specialization. */
1111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1112 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1113 && !DECL_CONV_FN_P (tmpl)
1114 /* It is possible to have a template that is not a member
1115 template and is not a member of a template class:
1117 template <typename T>
1118 struct S { friend A::f(); };
1120 Here, the friend function is a template, but the context does
1121 not have template information. The optimized lookup relies
1122 on having ARGS be the template arguments for both the class
1123 and the function template. */
1124 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1127 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1128 gone through coerce_template_parms by now. */
1130 static void
1131 verify_unstripped_args (tree args)
1133 ++processing_template_decl;
1134 if (!any_dependent_template_arguments_p (args))
1136 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1137 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1139 tree arg = TREE_VEC_ELT (inner, i);
1140 if (TREE_CODE (arg) == TEMPLATE_DECL)
1141 /* OK */;
1142 else if (TYPE_P (arg))
1143 gcc_assert (strip_typedefs (arg, NULL) == arg);
1144 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1145 /* Allow typedefs on the type of a non-type argument, since a
1146 parameter can have them. */;
1147 else
1148 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1151 --processing_template_decl;
1154 /* Retrieve the specialization (in the sense of [temp.spec] - a
1155 specialization is either an instantiation or an explicit
1156 specialization) of TMPL for the given template ARGS. If there is
1157 no such specialization, return NULL_TREE. The ARGS are a vector of
1158 arguments, or a vector of vectors of arguments, in the case of
1159 templates with more than one level of parameters.
1161 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1162 then we search for a partial specialization matching ARGS. This
1163 parameter is ignored if TMPL is not a class template.
1165 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1166 result is a NONTYPE_ARGUMENT_PACK. */
1168 static tree
1169 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1171 if (tmpl == NULL_TREE)
1172 return NULL_TREE;
1174 if (args == error_mark_node)
1175 return NULL_TREE;
1177 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1178 || TREE_CODE (tmpl) == FIELD_DECL);
1180 /* There should be as many levels of arguments as there are
1181 levels of parameters. */
1182 gcc_assert (TMPL_ARGS_DEPTH (args)
1183 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1185 : template_class_depth (DECL_CONTEXT (tmpl))));
1187 if (flag_checking)
1188 verify_unstripped_args (args);
1190 if (optimize_specialization_lookup_p (tmpl))
1192 /* The template arguments actually apply to the containing
1193 class. Find the class specialization with those
1194 arguments. */
1195 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1196 tree class_specialization
1197 = retrieve_specialization (class_template, args, 0);
1198 if (!class_specialization)
1199 return NULL_TREE;
1201 /* Find the instance of TMPL. */
1202 tree fns = lookup_fnfields_slot (class_specialization, DECL_NAME (tmpl));
1203 for (ovl_iterator iter (fns); iter; ++iter)
1205 tree fn = *iter;
1206 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1207 /* using-declarations can add base methods to the method vec,
1208 and we don't want those here. */
1209 && DECL_CONTEXT (fn) == class_specialization)
1210 return fn;
1212 return NULL_TREE;
1214 else
1216 spec_entry *found;
1217 spec_entry elt;
1218 hash_table<spec_hasher> *specializations;
1220 elt.tmpl = tmpl;
1221 elt.args = args;
1222 elt.spec = NULL_TREE;
1224 if (DECL_CLASS_TEMPLATE_P (tmpl))
1225 specializations = type_specializations;
1226 else
1227 specializations = decl_specializations;
1229 if (hash == 0)
1230 hash = spec_hasher::hash (&elt);
1231 found = specializations->find_with_hash (&elt, hash);
1232 if (found)
1233 return found->spec;
1236 return NULL_TREE;
1239 /* Like retrieve_specialization, but for local declarations. */
1241 tree
1242 retrieve_local_specialization (tree tmpl)
1244 if (local_specializations == NULL)
1245 return NULL_TREE;
1247 tree *slot = local_specializations->get (tmpl);
1248 return slot ? *slot : NULL_TREE;
1251 /* Returns nonzero iff DECL is a specialization of TMPL. */
1254 is_specialization_of (tree decl, tree tmpl)
1256 tree t;
1258 if (TREE_CODE (decl) == FUNCTION_DECL)
1260 for (t = decl;
1261 t != NULL_TREE;
1262 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1263 if (t == tmpl)
1264 return 1;
1266 else
1268 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1270 for (t = TREE_TYPE (decl);
1271 t != NULL_TREE;
1272 t = CLASSTYPE_USE_TEMPLATE (t)
1273 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1274 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1275 return 1;
1278 return 0;
1281 /* Returns nonzero iff DECL is a specialization of friend declaration
1282 FRIEND_DECL according to [temp.friend]. */
1284 bool
1285 is_specialization_of_friend (tree decl, tree friend_decl)
1287 bool need_template = true;
1288 int template_depth;
1290 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1291 || TREE_CODE (decl) == TYPE_DECL);
1293 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1294 of a template class, we want to check if DECL is a specialization
1295 if this. */
1296 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1297 && DECL_TEMPLATE_INFO (friend_decl)
1298 && !DECL_USE_TEMPLATE (friend_decl))
1300 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1301 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1302 need_template = false;
1304 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1305 && !PRIMARY_TEMPLATE_P (friend_decl))
1306 need_template = false;
1308 /* There is nothing to do if this is not a template friend. */
1309 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1310 return false;
1312 if (is_specialization_of (decl, friend_decl))
1313 return true;
1315 /* [temp.friend/6]
1316 A member of a class template may be declared to be a friend of a
1317 non-template class. In this case, the corresponding member of
1318 every specialization of the class template is a friend of the
1319 class granting friendship.
1321 For example, given a template friend declaration
1323 template <class T> friend void A<T>::f();
1325 the member function below is considered a friend
1327 template <> struct A<int> {
1328 void f();
1331 For this type of template friend, TEMPLATE_DEPTH below will be
1332 nonzero. To determine if DECL is a friend of FRIEND, we first
1333 check if the enclosing class is a specialization of another. */
1335 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1336 if (template_depth
1337 && DECL_CLASS_SCOPE_P (decl)
1338 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1339 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1341 /* Next, we check the members themselves. In order to handle
1342 a few tricky cases, such as when FRIEND_DECL's are
1344 template <class T> friend void A<T>::g(T t);
1345 template <class T> template <T t> friend void A<T>::h();
1347 and DECL's are
1349 void A<int>::g(int);
1350 template <int> void A<int>::h();
1352 we need to figure out ARGS, the template arguments from
1353 the context of DECL. This is required for template substitution
1354 of `T' in the function parameter of `g' and template parameter
1355 of `h' in the above examples. Here ARGS corresponds to `int'. */
1357 tree context = DECL_CONTEXT (decl);
1358 tree args = NULL_TREE;
1359 int current_depth = 0;
1361 while (current_depth < template_depth)
1363 if (CLASSTYPE_TEMPLATE_INFO (context))
1365 if (current_depth == 0)
1366 args = TYPE_TI_ARGS (context);
1367 else
1368 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1369 current_depth++;
1371 context = TYPE_CONTEXT (context);
1374 if (TREE_CODE (decl) == FUNCTION_DECL)
1376 bool is_template;
1377 tree friend_type;
1378 tree decl_type;
1379 tree friend_args_type;
1380 tree decl_args_type;
1382 /* Make sure that both DECL and FRIEND_DECL are templates or
1383 non-templates. */
1384 is_template = DECL_TEMPLATE_INFO (decl)
1385 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1386 if (need_template ^ is_template)
1387 return false;
1388 else if (is_template)
1390 /* If both are templates, check template parameter list. */
1391 tree friend_parms
1392 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1393 args, tf_none);
1394 if (!comp_template_parms
1395 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1396 friend_parms))
1397 return false;
1399 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1401 else
1402 decl_type = TREE_TYPE (decl);
1404 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1405 tf_none, NULL_TREE);
1406 if (friend_type == error_mark_node)
1407 return false;
1409 /* Check if return types match. */
1410 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1411 return false;
1413 /* Check if function parameter types match, ignoring the
1414 `this' parameter. */
1415 friend_args_type = TYPE_ARG_TYPES (friend_type);
1416 decl_args_type = TYPE_ARG_TYPES (decl_type);
1417 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1418 friend_args_type = TREE_CHAIN (friend_args_type);
1419 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1420 decl_args_type = TREE_CHAIN (decl_args_type);
1422 return compparms (decl_args_type, friend_args_type);
1424 else
1426 /* DECL is a TYPE_DECL */
1427 bool is_template;
1428 tree decl_type = TREE_TYPE (decl);
1430 /* Make sure that both DECL and FRIEND_DECL are templates or
1431 non-templates. */
1432 is_template
1433 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1434 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1436 if (need_template ^ is_template)
1437 return false;
1438 else if (is_template)
1440 tree friend_parms;
1441 /* If both are templates, check the name of the two
1442 TEMPLATE_DECL's first because is_friend didn't. */
1443 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1444 != DECL_NAME (friend_decl))
1445 return false;
1447 /* Now check template parameter list. */
1448 friend_parms
1449 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1450 args, tf_none);
1451 return comp_template_parms
1452 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1453 friend_parms);
1455 else
1456 return (DECL_NAME (decl)
1457 == DECL_NAME (friend_decl));
1460 return false;
1463 /* Register the specialization SPEC as a specialization of TMPL with
1464 the indicated ARGS. IS_FRIEND indicates whether the specialization
1465 is actually just a friend declaration. Returns SPEC, or an
1466 equivalent prior declaration, if available.
1468 We also store instantiations of field packs in the hash table, even
1469 though they are not themselves templates, to make lookup easier. */
1471 static tree
1472 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1473 hashval_t hash)
1475 tree fn;
1476 spec_entry **slot = NULL;
1477 spec_entry elt;
1479 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1480 || (TREE_CODE (tmpl) == FIELD_DECL
1481 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1483 if (TREE_CODE (spec) == FUNCTION_DECL
1484 && uses_template_parms (DECL_TI_ARGS (spec)))
1485 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1486 register it; we want the corresponding TEMPLATE_DECL instead.
1487 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1488 the more obvious `uses_template_parms (spec)' to avoid problems
1489 with default function arguments. In particular, given
1490 something like this:
1492 template <class T> void f(T t1, T t = T())
1494 the default argument expression is not substituted for in an
1495 instantiation unless and until it is actually needed. */
1496 return spec;
1498 if (optimize_specialization_lookup_p (tmpl))
1499 /* We don't put these specializations in the hash table, but we might
1500 want to give an error about a mismatch. */
1501 fn = retrieve_specialization (tmpl, args, 0);
1502 else
1504 elt.tmpl = tmpl;
1505 elt.args = args;
1506 elt.spec = spec;
1508 if (hash == 0)
1509 hash = spec_hasher::hash (&elt);
1511 slot =
1512 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1513 if (*slot)
1514 fn = ((spec_entry *) *slot)->spec;
1515 else
1516 fn = NULL_TREE;
1519 /* We can sometimes try to re-register a specialization that we've
1520 already got. In particular, regenerate_decl_from_template calls
1521 duplicate_decls which will update the specialization list. But,
1522 we'll still get called again here anyhow. It's more convenient
1523 to simply allow this than to try to prevent it. */
1524 if (fn == spec)
1525 return spec;
1526 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1528 if (DECL_TEMPLATE_INSTANTIATION (fn))
1530 if (DECL_ODR_USED (fn)
1531 || DECL_EXPLICIT_INSTANTIATION (fn))
1533 error ("specialization of %qD after instantiation",
1534 fn);
1535 return error_mark_node;
1537 else
1539 tree clone;
1540 /* This situation should occur only if the first
1541 specialization is an implicit instantiation, the
1542 second is an explicit specialization, and the
1543 implicit instantiation has not yet been used. That
1544 situation can occur if we have implicitly
1545 instantiated a member function and then specialized
1546 it later.
1548 We can also wind up here if a friend declaration that
1549 looked like an instantiation turns out to be a
1550 specialization:
1552 template <class T> void foo(T);
1553 class S { friend void foo<>(int) };
1554 template <> void foo(int);
1556 We transform the existing DECL in place so that any
1557 pointers to it become pointers to the updated
1558 declaration.
1560 If there was a definition for the template, but not
1561 for the specialization, we want this to look as if
1562 there were no definition, and vice versa. */
1563 DECL_INITIAL (fn) = NULL_TREE;
1564 duplicate_decls (spec, fn, is_friend);
1565 /* The call to duplicate_decls will have applied
1566 [temp.expl.spec]:
1568 An explicit specialization of a function template
1569 is inline only if it is explicitly declared to be,
1570 and independently of whether its function template
1573 to the primary function; now copy the inline bits to
1574 the various clones. */
1575 FOR_EACH_CLONE (clone, fn)
1577 DECL_DECLARED_INLINE_P (clone)
1578 = DECL_DECLARED_INLINE_P (fn);
1579 DECL_SOURCE_LOCATION (clone)
1580 = DECL_SOURCE_LOCATION (fn);
1581 DECL_DELETED_FN (clone)
1582 = DECL_DELETED_FN (fn);
1584 check_specialization_namespace (tmpl);
1586 return fn;
1589 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1591 tree dd = duplicate_decls (spec, fn, is_friend);
1592 if (dd == error_mark_node)
1593 /* We've already complained in duplicate_decls. */
1594 return error_mark_node;
1596 if (dd == NULL_TREE && DECL_INITIAL (spec))
1597 /* Dup decl failed, but this is a new definition. Set the
1598 line number so any errors match this new
1599 definition. */
1600 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1602 return fn;
1605 else if (fn)
1606 return duplicate_decls (spec, fn, is_friend);
1608 /* A specialization must be declared in the same namespace as the
1609 template it is specializing. */
1610 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1611 && !check_specialization_namespace (tmpl))
1612 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1614 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1616 spec_entry *entry = ggc_alloc<spec_entry> ();
1617 gcc_assert (tmpl && args && spec);
1618 *entry = elt;
1619 *slot = entry;
1620 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1621 && PRIMARY_TEMPLATE_P (tmpl)
1622 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1623 || variable_template_p (tmpl))
1624 /* If TMPL is a forward declaration of a template function, keep a list
1625 of all specializations in case we need to reassign them to a friend
1626 template later in tsubst_friend_function.
1628 Also keep a list of all variable template instantiations so that
1629 process_partial_specialization can check whether a later partial
1630 specialization would have used it. */
1631 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1632 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1635 return spec;
1638 /* Returns true iff two spec_entry nodes are equivalent. */
1640 int comparing_specializations;
1642 bool
1643 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1645 int equal;
1647 ++comparing_specializations;
1648 equal = (e1->tmpl == e2->tmpl
1649 && comp_template_args (e1->args, e2->args));
1650 if (equal && flag_concepts
1651 /* tmpl could be a FIELD_DECL for a capture pack. */
1652 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1653 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1654 && uses_template_parms (e1->args))
1656 /* Partial specializations of a variable template can be distinguished by
1657 constraints. */
1658 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1659 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1660 equal = equivalent_constraints (c1, c2);
1662 --comparing_specializations;
1664 return equal;
1667 /* Returns a hash for a template TMPL and template arguments ARGS. */
1669 static hashval_t
1670 hash_tmpl_and_args (tree tmpl, tree args)
1672 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1673 return iterative_hash_template_arg (args, val);
1676 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1677 ignoring SPEC. */
1679 hashval_t
1680 spec_hasher::hash (spec_entry *e)
1682 return hash_tmpl_and_args (e->tmpl, e->args);
1685 /* Recursively calculate a hash value for a template argument ARG, for use
1686 in the hash tables of template specializations. */
1688 hashval_t
1689 iterative_hash_template_arg (tree arg, hashval_t val)
1691 unsigned HOST_WIDE_INT i;
1692 enum tree_code code;
1693 char tclass;
1695 if (arg == NULL_TREE)
1696 return iterative_hash_object (arg, val);
1698 if (!TYPE_P (arg))
1699 STRIP_NOPS (arg);
1701 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1702 gcc_unreachable ();
1704 code = TREE_CODE (arg);
1705 tclass = TREE_CODE_CLASS (code);
1707 val = iterative_hash_object (code, val);
1709 switch (code)
1711 case ERROR_MARK:
1712 return val;
1714 case IDENTIFIER_NODE:
1715 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1717 case TREE_VEC:
1719 int i, len = TREE_VEC_LENGTH (arg);
1720 for (i = 0; i < len; ++i)
1721 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1722 return val;
1725 case TYPE_PACK_EXPANSION:
1726 case EXPR_PACK_EXPANSION:
1727 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1728 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1730 case TYPE_ARGUMENT_PACK:
1731 case NONTYPE_ARGUMENT_PACK:
1732 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1734 case TREE_LIST:
1735 for (; arg; arg = TREE_CHAIN (arg))
1736 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1737 return val;
1739 case OVERLOAD:
1740 for (lkp_iterator iter (arg); iter; ++iter)
1741 val = iterative_hash_template_arg (*iter, val);
1742 return val;
1744 case CONSTRUCTOR:
1746 tree field, value;
1747 iterative_hash_template_arg (TREE_TYPE (arg), val);
1748 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1750 val = iterative_hash_template_arg (field, val);
1751 val = iterative_hash_template_arg (value, val);
1753 return val;
1756 case PARM_DECL:
1757 if (!DECL_ARTIFICIAL (arg))
1759 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1760 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1762 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1764 case TARGET_EXPR:
1765 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1767 case PTRMEM_CST:
1768 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1769 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1771 case TEMPLATE_PARM_INDEX:
1772 val = iterative_hash_template_arg
1773 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1774 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1775 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1777 case TRAIT_EXPR:
1778 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1779 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1780 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1782 case BASELINK:
1783 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1784 val);
1785 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1786 val);
1788 case MODOP_EXPR:
1789 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1790 code = TREE_CODE (TREE_OPERAND (arg, 1));
1791 val = iterative_hash_object (code, val);
1792 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1794 case LAMBDA_EXPR:
1795 /* A lambda can't appear in a template arg, but don't crash on
1796 erroneous input. */
1797 gcc_assert (seen_error ());
1798 return val;
1800 case CAST_EXPR:
1801 case IMPLICIT_CONV_EXPR:
1802 case STATIC_CAST_EXPR:
1803 case REINTERPRET_CAST_EXPR:
1804 case CONST_CAST_EXPR:
1805 case DYNAMIC_CAST_EXPR:
1806 case NEW_EXPR:
1807 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1808 /* Now hash operands as usual. */
1809 break;
1811 default:
1812 break;
1815 switch (tclass)
1817 case tcc_type:
1818 if (alias_template_specialization_p (arg))
1820 // We want an alias specialization that survived strip_typedefs
1821 // to hash differently from its TYPE_CANONICAL, to avoid hash
1822 // collisions that compare as different in template_args_equal.
1823 // These could be dependent specializations that strip_typedefs
1824 // left alone, or untouched specializations because
1825 // coerce_template_parms returns the unconverted template
1826 // arguments if it sees incomplete argument packs.
1827 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1828 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1830 if (TYPE_CANONICAL (arg))
1831 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1832 val);
1833 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1834 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1835 /* Otherwise just compare the types during lookup. */
1836 return val;
1838 case tcc_declaration:
1839 case tcc_constant:
1840 return iterative_hash_expr (arg, val);
1842 default:
1843 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1845 unsigned n = cp_tree_operand_length (arg);
1846 for (i = 0; i < n; ++i)
1847 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1848 return val;
1851 gcc_unreachable ();
1852 return 0;
1855 /* Unregister the specialization SPEC as a specialization of TMPL.
1856 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1857 if the SPEC was listed as a specialization of TMPL.
1859 Note that SPEC has been ggc_freed, so we can't look inside it. */
1861 bool
1862 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1864 spec_entry *entry;
1865 spec_entry elt;
1867 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1868 elt.args = TI_ARGS (tinfo);
1869 elt.spec = NULL_TREE;
1871 entry = decl_specializations->find (&elt);
1872 if (entry != NULL)
1874 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1875 gcc_assert (new_spec != NULL_TREE);
1876 entry->spec = new_spec;
1877 return 1;
1880 return 0;
1883 /* Like register_specialization, but for local declarations. We are
1884 registering SPEC, an instantiation of TMPL. */
1886 void
1887 register_local_specialization (tree spec, tree tmpl)
1889 local_specializations->put (tmpl, spec);
1892 /* TYPE is a class type. Returns true if TYPE is an explicitly
1893 specialized class. */
1895 bool
1896 explicit_class_specialization_p (tree type)
1898 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1899 return false;
1900 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1903 /* Print the list of functions at FNS, going through all the overloads
1904 for each element of the list. Alternatively, FNS can not be a
1905 TREE_LIST, in which case it will be printed together with all the
1906 overloads.
1908 MORE and *STR should respectively be FALSE and NULL when the function
1909 is called from the outside. They are used internally on recursive
1910 calls. print_candidates manages the two parameters and leaves NULL
1911 in *STR when it ends. */
1913 static void
1914 print_candidates_1 (tree fns, char **str, bool more = false)
1916 if (TREE_CODE (fns) == TREE_LIST)
1917 for (; fns; fns = TREE_CHAIN (fns))
1918 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1919 else
1920 for (lkp_iterator iter (fns); iter;)
1922 tree cand = *iter;
1923 ++iter;
1925 const char *pfx = *str;
1926 if (!pfx)
1928 if (more || iter)
1929 pfx = _("candidates are:");
1930 else
1931 pfx = _("candidate is:");
1932 *str = get_spaces (pfx);
1934 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1938 /* Print the list of candidate FNS in an error message. FNS can also
1939 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1941 void
1942 print_candidates (tree fns)
1944 char *str = NULL;
1945 print_candidates_1 (fns, &str);
1946 free (str);
1949 /* Get a (possibly) constrained template declaration for the
1950 purpose of ordering candidates. */
1951 static tree
1952 get_template_for_ordering (tree list)
1954 gcc_assert (TREE_CODE (list) == TREE_LIST);
1955 tree f = TREE_VALUE (list);
1956 if (tree ti = DECL_TEMPLATE_INFO (f))
1957 return TI_TEMPLATE (ti);
1958 return f;
1961 /* Among candidates having the same signature, return the
1962 most constrained or NULL_TREE if there is no best candidate.
1963 If the signatures of candidates vary (e.g., template
1964 specialization vs. member function), then there can be no
1965 most constrained.
1967 Note that we don't compare constraints on the functions
1968 themselves, but rather those of their templates. */
1969 static tree
1970 most_constrained_function (tree candidates)
1972 // Try to find the best candidate in a first pass.
1973 tree champ = candidates;
1974 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1976 int winner = more_constrained (get_template_for_ordering (champ),
1977 get_template_for_ordering (c));
1978 if (winner == -1)
1979 champ = c; // The candidate is more constrained
1980 else if (winner == 0)
1981 return NULL_TREE; // Neither is more constrained
1984 // Verify that the champ is better than previous candidates.
1985 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1986 if (!more_constrained (get_template_for_ordering (champ),
1987 get_template_for_ordering (c)))
1988 return NULL_TREE;
1991 return champ;
1995 /* Returns the template (one of the functions given by TEMPLATE_ID)
1996 which can be specialized to match the indicated DECL with the
1997 explicit template args given in TEMPLATE_ID. The DECL may be
1998 NULL_TREE if none is available. In that case, the functions in
1999 TEMPLATE_ID are non-members.
2001 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2002 specialization of a member template.
2004 The TEMPLATE_COUNT is the number of references to qualifying
2005 template classes that appeared in the name of the function. See
2006 check_explicit_specialization for a more accurate description.
2008 TSK indicates what kind of template declaration (if any) is being
2009 declared. TSK_TEMPLATE indicates that the declaration given by
2010 DECL, though a FUNCTION_DECL, has template parameters, and is
2011 therefore a template function.
2013 The template args (those explicitly specified and those deduced)
2014 are output in a newly created vector *TARGS_OUT.
2016 If it is impossible to determine the result, an error message is
2017 issued. The error_mark_node is returned to indicate failure. */
2019 static tree
2020 determine_specialization (tree template_id,
2021 tree decl,
2022 tree* targs_out,
2023 int need_member_template,
2024 int template_count,
2025 tmpl_spec_kind tsk)
2027 tree fns;
2028 tree targs;
2029 tree explicit_targs;
2030 tree candidates = NULL_TREE;
2032 /* A TREE_LIST of templates of which DECL may be a specialization.
2033 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2034 corresponding TREE_PURPOSE is the set of template arguments that,
2035 when used to instantiate the template, would produce a function
2036 with the signature of DECL. */
2037 tree templates = NULL_TREE;
2038 int header_count;
2039 cp_binding_level *b;
2041 *targs_out = NULL_TREE;
2043 if (template_id == error_mark_node || decl == error_mark_node)
2044 return error_mark_node;
2046 /* We shouldn't be specializing a member template of an
2047 unspecialized class template; we already gave an error in
2048 check_specialization_scope, now avoid crashing. */
2049 if (template_count && DECL_CLASS_SCOPE_P (decl)
2050 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2052 gcc_assert (errorcount);
2053 return error_mark_node;
2056 fns = TREE_OPERAND (template_id, 0);
2057 explicit_targs = TREE_OPERAND (template_id, 1);
2059 if (fns == error_mark_node)
2060 return error_mark_node;
2062 /* Check for baselinks. */
2063 if (BASELINK_P (fns))
2064 fns = BASELINK_FUNCTIONS (fns);
2066 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2068 error ("%qD is not a function template", fns);
2069 return error_mark_node;
2071 else if (VAR_P (decl) && !variable_template_p (fns))
2073 error ("%qD is not a variable template", fns);
2074 return error_mark_node;
2077 /* Count the number of template headers specified for this
2078 specialization. */
2079 header_count = 0;
2080 for (b = current_binding_level;
2081 b->kind == sk_template_parms;
2082 b = b->level_chain)
2083 ++header_count;
2085 tree orig_fns = fns;
2087 if (variable_template_p (fns))
2089 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2090 targs = coerce_template_parms (parms, explicit_targs, fns,
2091 tf_warning_or_error,
2092 /*req_all*/true, /*use_defarg*/true);
2093 if (targs != error_mark_node)
2094 templates = tree_cons (targs, fns, templates);
2096 else for (lkp_iterator iter (fns); iter; ++iter)
2098 tree fn = *iter;
2100 if (TREE_CODE (fn) == TEMPLATE_DECL)
2102 tree decl_arg_types;
2103 tree fn_arg_types;
2104 tree insttype;
2106 /* In case of explicit specialization, we need to check if
2107 the number of template headers appearing in the specialization
2108 is correct. This is usually done in check_explicit_specialization,
2109 but the check done there cannot be exhaustive when specializing
2110 member functions. Consider the following code:
2112 template <> void A<int>::f(int);
2113 template <> template <> void A<int>::f(int);
2115 Assuming that A<int> is not itself an explicit specialization
2116 already, the first line specializes "f" which is a non-template
2117 member function, whilst the second line specializes "f" which
2118 is a template member function. So both lines are syntactically
2119 correct, and check_explicit_specialization does not reject
2120 them.
2122 Here, we can do better, as we are matching the specialization
2123 against the declarations. We count the number of template
2124 headers, and we check if they match TEMPLATE_COUNT + 1
2125 (TEMPLATE_COUNT is the number of qualifying template classes,
2126 plus there must be another header for the member template
2127 itself).
2129 Notice that if header_count is zero, this is not a
2130 specialization but rather a template instantiation, so there
2131 is no check we can perform here. */
2132 if (header_count && header_count != template_count + 1)
2133 continue;
2135 /* Check that the number of template arguments at the
2136 innermost level for DECL is the same as for FN. */
2137 if (current_binding_level->kind == sk_template_parms
2138 && !current_binding_level->explicit_spec_p
2139 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2140 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2141 (current_template_parms))))
2142 continue;
2144 /* DECL might be a specialization of FN. */
2145 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2146 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2148 /* For a non-static member function, we need to make sure
2149 that the const qualification is the same. Since
2150 get_bindings does not try to merge the "this" parameter,
2151 we must do the comparison explicitly. */
2152 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2153 && !same_type_p (TREE_VALUE (fn_arg_types),
2154 TREE_VALUE (decl_arg_types)))
2155 continue;
2157 /* Skip the "this" parameter and, for constructors of
2158 classes with virtual bases, the VTT parameter. A
2159 full specialization of a constructor will have a VTT
2160 parameter, but a template never will. */
2161 decl_arg_types
2162 = skip_artificial_parms_for (decl, decl_arg_types);
2163 fn_arg_types
2164 = skip_artificial_parms_for (fn, fn_arg_types);
2166 /* Function templates cannot be specializations; there are
2167 no partial specializations of functions. Therefore, if
2168 the type of DECL does not match FN, there is no
2169 match.
2171 Note that it should never be the case that we have both
2172 candidates added here, and for regular member functions
2173 below. */
2174 if (tsk == tsk_template)
2176 if (compparms (fn_arg_types, decl_arg_types))
2177 candidates = tree_cons (NULL_TREE, fn, candidates);
2178 continue;
2181 /* See whether this function might be a specialization of this
2182 template. Suppress access control because we might be trying
2183 to make this specialization a friend, and we have already done
2184 access control for the declaration of the specialization. */
2185 push_deferring_access_checks (dk_no_check);
2186 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2187 pop_deferring_access_checks ();
2189 if (!targs)
2190 /* We cannot deduce template arguments that when used to
2191 specialize TMPL will produce DECL. */
2192 continue;
2194 /* Remove, from the set of candidates, all those functions
2195 whose constraints are not satisfied. */
2196 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2197 continue;
2199 // Then, try to form the new function type.
2200 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2201 if (insttype == error_mark_node)
2202 continue;
2203 fn_arg_types
2204 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2205 if (!compparms (fn_arg_types, decl_arg_types))
2206 continue;
2208 /* Save this template, and the arguments deduced. */
2209 templates = tree_cons (targs, fn, templates);
2211 else if (need_member_template)
2212 /* FN is an ordinary member function, and we need a
2213 specialization of a member template. */
2215 else if (TREE_CODE (fn) != FUNCTION_DECL)
2216 /* We can get IDENTIFIER_NODEs here in certain erroneous
2217 cases. */
2219 else if (!DECL_FUNCTION_MEMBER_P (fn))
2220 /* This is just an ordinary non-member function. Nothing can
2221 be a specialization of that. */
2223 else if (DECL_ARTIFICIAL (fn))
2224 /* Cannot specialize functions that are created implicitly. */
2226 else
2228 tree decl_arg_types;
2230 /* This is an ordinary member function. However, since
2231 we're here, we can assume its enclosing class is a
2232 template class. For example,
2234 template <typename T> struct S { void f(); };
2235 template <> void S<int>::f() {}
2237 Here, S<int>::f is a non-template, but S<int> is a
2238 template class. If FN has the same type as DECL, we
2239 might be in business. */
2241 if (!DECL_TEMPLATE_INFO (fn))
2242 /* Its enclosing class is an explicit specialization
2243 of a template class. This is not a candidate. */
2244 continue;
2246 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2247 TREE_TYPE (TREE_TYPE (fn))))
2248 /* The return types differ. */
2249 continue;
2251 /* Adjust the type of DECL in case FN is a static member. */
2252 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2253 if (DECL_STATIC_FUNCTION_P (fn)
2254 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2255 decl_arg_types = TREE_CHAIN (decl_arg_types);
2257 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2258 decl_arg_types))
2259 continue;
2261 // If the deduced arguments do not satisfy the constraints,
2262 // this is not a candidate.
2263 if (flag_concepts && !constraints_satisfied_p (fn))
2264 continue;
2266 // Add the candidate.
2267 candidates = tree_cons (NULL_TREE, fn, candidates);
2271 if (templates && TREE_CHAIN (templates))
2273 /* We have:
2275 [temp.expl.spec]
2277 It is possible for a specialization with a given function
2278 signature to be instantiated from more than one function
2279 template. In such cases, explicit specification of the
2280 template arguments must be used to uniquely identify the
2281 function template specialization being specialized.
2283 Note that here, there's no suggestion that we're supposed to
2284 determine which of the candidate templates is most
2285 specialized. However, we, also have:
2287 [temp.func.order]
2289 Partial ordering of overloaded function template
2290 declarations is used in the following contexts to select
2291 the function template to which a function template
2292 specialization refers:
2294 -- when an explicit specialization refers to a function
2295 template.
2297 So, we do use the partial ordering rules, at least for now.
2298 This extension can only serve to make invalid programs valid,
2299 so it's safe. And, there is strong anecdotal evidence that
2300 the committee intended the partial ordering rules to apply;
2301 the EDG front end has that behavior, and John Spicer claims
2302 that the committee simply forgot to delete the wording in
2303 [temp.expl.spec]. */
2304 tree tmpl = most_specialized_instantiation (templates);
2305 if (tmpl != error_mark_node)
2307 templates = tmpl;
2308 TREE_CHAIN (templates) = NULL_TREE;
2312 // Concepts allows multiple declarations of member functions
2313 // with the same signature. Like above, we need to rely on
2314 // on the partial ordering of those candidates to determine which
2315 // is the best.
2316 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2318 if (tree cand = most_constrained_function (candidates))
2320 candidates = cand;
2321 TREE_CHAIN (cand) = NULL_TREE;
2325 if (templates == NULL_TREE && candidates == NULL_TREE)
2327 error ("template-id %qD for %q+D does not match any template "
2328 "declaration", template_id, decl);
2329 if (header_count && header_count != template_count + 1)
2330 inform (input_location, "saw %d %<template<>%>, need %d for "
2331 "specializing a member function template",
2332 header_count, template_count + 1);
2333 else
2334 print_candidates (orig_fns);
2335 return error_mark_node;
2337 else if ((templates && TREE_CHAIN (templates))
2338 || (candidates && TREE_CHAIN (candidates))
2339 || (templates && candidates))
2341 error ("ambiguous template specialization %qD for %q+D",
2342 template_id, decl);
2343 candidates = chainon (candidates, templates);
2344 print_candidates (candidates);
2345 return error_mark_node;
2348 /* We have one, and exactly one, match. */
2349 if (candidates)
2351 tree fn = TREE_VALUE (candidates);
2352 *targs_out = copy_node (DECL_TI_ARGS (fn));
2354 // Propagate the candidate's constraints to the declaration.
2355 set_constraints (decl, get_constraints (fn));
2357 /* DECL is a re-declaration or partial instantiation of a template
2358 function. */
2359 if (TREE_CODE (fn) == TEMPLATE_DECL)
2360 return fn;
2361 /* It was a specialization of an ordinary member function in a
2362 template class. */
2363 return DECL_TI_TEMPLATE (fn);
2366 /* It was a specialization of a template. */
2367 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2368 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2370 *targs_out = copy_node (targs);
2371 SET_TMPL_ARGS_LEVEL (*targs_out,
2372 TMPL_ARGS_DEPTH (*targs_out),
2373 TREE_PURPOSE (templates));
2375 else
2376 *targs_out = TREE_PURPOSE (templates);
2377 return TREE_VALUE (templates);
2380 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2381 but with the default argument values filled in from those in the
2382 TMPL_TYPES. */
2384 static tree
2385 copy_default_args_to_explicit_spec_1 (tree spec_types,
2386 tree tmpl_types)
2388 tree new_spec_types;
2390 if (!spec_types)
2391 return NULL_TREE;
2393 if (spec_types == void_list_node)
2394 return void_list_node;
2396 /* Substitute into the rest of the list. */
2397 new_spec_types =
2398 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2399 TREE_CHAIN (tmpl_types));
2401 /* Add the default argument for this parameter. */
2402 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2403 TREE_VALUE (spec_types),
2404 new_spec_types);
2407 /* DECL is an explicit specialization. Replicate default arguments
2408 from the template it specializes. (That way, code like:
2410 template <class T> void f(T = 3);
2411 template <> void f(double);
2412 void g () { f (); }
2414 works, as required.) An alternative approach would be to look up
2415 the correct default arguments at the call-site, but this approach
2416 is consistent with how implicit instantiations are handled. */
2418 static void
2419 copy_default_args_to_explicit_spec (tree decl)
2421 tree tmpl;
2422 tree spec_types;
2423 tree tmpl_types;
2424 tree new_spec_types;
2425 tree old_type;
2426 tree new_type;
2427 tree t;
2428 tree object_type = NULL_TREE;
2429 tree in_charge = NULL_TREE;
2430 tree vtt = NULL_TREE;
2432 /* See if there's anything we need to do. */
2433 tmpl = DECL_TI_TEMPLATE (decl);
2434 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2435 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2436 if (TREE_PURPOSE (t))
2437 break;
2438 if (!t)
2439 return;
2441 old_type = TREE_TYPE (decl);
2442 spec_types = TYPE_ARG_TYPES (old_type);
2444 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2446 /* Remove the this pointer, but remember the object's type for
2447 CV quals. */
2448 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2449 spec_types = TREE_CHAIN (spec_types);
2450 tmpl_types = TREE_CHAIN (tmpl_types);
2452 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2454 /* DECL may contain more parameters than TMPL due to the extra
2455 in-charge parameter in constructors and destructors. */
2456 in_charge = spec_types;
2457 spec_types = TREE_CHAIN (spec_types);
2459 if (DECL_HAS_VTT_PARM_P (decl))
2461 vtt = spec_types;
2462 spec_types = TREE_CHAIN (spec_types);
2466 /* Compute the merged default arguments. */
2467 new_spec_types =
2468 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2470 /* Compute the new FUNCTION_TYPE. */
2471 if (object_type)
2473 if (vtt)
2474 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2475 TREE_VALUE (vtt),
2476 new_spec_types);
2478 if (in_charge)
2479 /* Put the in-charge parameter back. */
2480 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2481 TREE_VALUE (in_charge),
2482 new_spec_types);
2484 new_type = build_method_type_directly (object_type,
2485 TREE_TYPE (old_type),
2486 new_spec_types);
2488 else
2489 new_type = build_function_type (TREE_TYPE (old_type),
2490 new_spec_types);
2491 new_type = cp_build_type_attribute_variant (new_type,
2492 TYPE_ATTRIBUTES (old_type));
2493 new_type = build_exception_variant (new_type,
2494 TYPE_RAISES_EXCEPTIONS (old_type));
2496 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2497 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2499 TREE_TYPE (decl) = new_type;
2502 /* Return the number of template headers we expect to see for a definition
2503 or specialization of CTYPE or one of its non-template members. */
2506 num_template_headers_for_class (tree ctype)
2508 int num_templates = 0;
2510 while (ctype && CLASS_TYPE_P (ctype))
2512 /* You're supposed to have one `template <...>' for every
2513 template class, but you don't need one for a full
2514 specialization. For example:
2516 template <class T> struct S{};
2517 template <> struct S<int> { void f(); };
2518 void S<int>::f () {}
2520 is correct; there shouldn't be a `template <>' for the
2521 definition of `S<int>::f'. */
2522 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2523 /* If CTYPE does not have template information of any
2524 kind, then it is not a template, nor is it nested
2525 within a template. */
2526 break;
2527 if (explicit_class_specialization_p (ctype))
2528 break;
2529 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2530 ++num_templates;
2532 ctype = TYPE_CONTEXT (ctype);
2535 return num_templates;
2538 /* Do a simple sanity check on the template headers that precede the
2539 variable declaration DECL. */
2541 void
2542 check_template_variable (tree decl)
2544 tree ctx = CP_DECL_CONTEXT (decl);
2545 int wanted = num_template_headers_for_class (ctx);
2546 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2547 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2549 if (cxx_dialect < cxx14)
2550 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2551 "variable templates only available with "
2552 "-std=c++14 or -std=gnu++14");
2554 // Namespace-scope variable templates should have a template header.
2555 ++wanted;
2557 if (template_header_count > wanted)
2559 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2560 "too many template headers for %qD "
2561 "(should be %d)",
2562 decl, wanted);
2563 if (warned && CLASS_TYPE_P (ctx)
2564 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2565 inform (DECL_SOURCE_LOCATION (decl),
2566 "members of an explicitly specialized class are defined "
2567 "without a template header");
2571 /* An explicit specialization whose declarator-id or class-head-name is not
2572 qualified shall be declared in the nearest enclosing namespace of the
2573 template, or, if the namespace is inline (7.3.1), any namespace from its
2574 enclosing namespace set.
2576 If the name declared in the explicit instantiation is an unqualified name,
2577 the explicit instantiation shall appear in the namespace where its template
2578 is declared or, if that namespace is inline (7.3.1), any namespace from its
2579 enclosing namespace set. */
2581 void
2582 check_unqualified_spec_or_inst (tree t, location_t loc)
2584 tree tmpl = most_general_template (t);
2585 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2586 && !is_nested_namespace (current_namespace,
2587 CP_DECL_CONTEXT (tmpl), true))
2589 if (processing_specialization)
2590 permerror (loc, "explicit specialization of %qD outside its "
2591 "namespace must use a nested-name-specifier", tmpl);
2592 else if (processing_explicit_instantiation
2593 && cxx_dialect >= cxx11)
2594 /* This was allowed in C++98, so only pedwarn. */
2595 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2596 "outside its namespace must use a nested-name-"
2597 "specifier", tmpl);
2601 /* Check to see if the function just declared, as indicated in
2602 DECLARATOR, and in DECL, is a specialization of a function
2603 template. We may also discover that the declaration is an explicit
2604 instantiation at this point.
2606 Returns DECL, or an equivalent declaration that should be used
2607 instead if all goes well. Issues an error message if something is
2608 amiss. Returns error_mark_node if the error is not easily
2609 recoverable.
2611 FLAGS is a bitmask consisting of the following flags:
2613 2: The function has a definition.
2614 4: The function is a friend.
2616 The TEMPLATE_COUNT is the number of references to qualifying
2617 template classes that appeared in the name of the function. For
2618 example, in
2620 template <class T> struct S { void f(); };
2621 void S<int>::f();
2623 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2624 classes are not counted in the TEMPLATE_COUNT, so that in
2626 template <class T> struct S {};
2627 template <> struct S<int> { void f(); }
2628 template <> void S<int>::f();
2630 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2631 invalid; there should be no template <>.)
2633 If the function is a specialization, it is marked as such via
2634 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2635 is set up correctly, and it is added to the list of specializations
2636 for that template. */
2638 tree
2639 check_explicit_specialization (tree declarator,
2640 tree decl,
2641 int template_count,
2642 int flags)
2644 int have_def = flags & 2;
2645 int is_friend = flags & 4;
2646 bool is_concept = flags & 8;
2647 int specialization = 0;
2648 int explicit_instantiation = 0;
2649 int member_specialization = 0;
2650 tree ctype = DECL_CLASS_CONTEXT (decl);
2651 tree dname = DECL_NAME (decl);
2652 tmpl_spec_kind tsk;
2654 if (is_friend)
2656 if (!processing_specialization)
2657 tsk = tsk_none;
2658 else
2659 tsk = tsk_excessive_parms;
2661 else
2662 tsk = current_tmpl_spec_kind (template_count);
2664 switch (tsk)
2666 case tsk_none:
2667 if (processing_specialization && !VAR_P (decl))
2669 specialization = 1;
2670 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2672 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2674 if (is_friend)
2675 /* This could be something like:
2677 template <class T> void f(T);
2678 class S { friend void f<>(int); } */
2679 specialization = 1;
2680 else
2682 /* This case handles bogus declarations like template <>
2683 template <class T> void f<int>(); */
2685 error ("template-id %qD in declaration of primary template",
2686 declarator);
2687 return decl;
2690 break;
2692 case tsk_invalid_member_spec:
2693 /* The error has already been reported in
2694 check_specialization_scope. */
2695 return error_mark_node;
2697 case tsk_invalid_expl_inst:
2698 error ("template parameter list used in explicit instantiation");
2700 /* Fall through. */
2702 case tsk_expl_inst:
2703 if (have_def)
2704 error ("definition provided for explicit instantiation");
2706 explicit_instantiation = 1;
2707 break;
2709 case tsk_excessive_parms:
2710 case tsk_insufficient_parms:
2711 if (tsk == tsk_excessive_parms)
2712 error ("too many template parameter lists in declaration of %qD",
2713 decl);
2714 else if (template_header_count)
2715 error("too few template parameter lists in declaration of %qD", decl);
2716 else
2717 error("explicit specialization of %qD must be introduced by "
2718 "%<template <>%>", decl);
2720 /* Fall through. */
2721 case tsk_expl_spec:
2722 if (is_concept)
2723 error ("explicit specialization declared %<concept%>");
2725 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2726 /* In cases like template<> constexpr bool v = true;
2727 We'll give an error in check_template_variable. */
2728 break;
2730 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2731 if (ctype)
2732 member_specialization = 1;
2733 else
2734 specialization = 1;
2735 break;
2737 case tsk_template:
2738 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2740 /* This case handles bogus declarations like template <>
2741 template <class T> void f<int>(); */
2743 if (!uses_template_parms (declarator))
2744 error ("template-id %qD in declaration of primary template",
2745 declarator);
2746 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2748 /* Partial specialization of variable template. */
2749 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2750 specialization = 1;
2751 goto ok;
2753 else if (cxx_dialect < cxx14)
2754 error ("non-type partial specialization %qD "
2755 "is not allowed", declarator);
2756 else
2757 error ("non-class, non-variable partial specialization %qD "
2758 "is not allowed", declarator);
2759 return decl;
2760 ok:;
2763 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2764 /* This is a specialization of a member template, without
2765 specialization the containing class. Something like:
2767 template <class T> struct S {
2768 template <class U> void f (U);
2770 template <> template <class U> void S<int>::f(U) {}
2772 That's a specialization -- but of the entire template. */
2773 specialization = 1;
2774 break;
2776 default:
2777 gcc_unreachable ();
2780 if ((specialization || member_specialization)
2781 /* This doesn't apply to variable templates. */
2782 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2783 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2785 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2786 for (; t; t = TREE_CHAIN (t))
2787 if (TREE_PURPOSE (t))
2789 permerror (input_location,
2790 "default argument specified in explicit specialization");
2791 break;
2795 if (specialization || member_specialization || explicit_instantiation)
2797 tree tmpl = NULL_TREE;
2798 tree targs = NULL_TREE;
2799 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2801 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2802 if (!was_template_id)
2804 tree fns;
2806 gcc_assert (identifier_p (declarator));
2807 if (ctype)
2808 fns = dname;
2809 else
2811 /* If there is no class context, the explicit instantiation
2812 must be at namespace scope. */
2813 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2815 /* Find the namespace binding, using the declaration
2816 context. */
2817 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2818 false, true);
2819 if (fns == error_mark_node)
2820 /* If lookup fails, look for a friend declaration so we can
2821 give a better diagnostic. */
2822 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2823 /*type*/false, /*complain*/true,
2824 /*hidden*/true);
2826 if (fns == error_mark_node || !is_overloaded_fn (fns))
2828 error ("%qD is not a template function", dname);
2829 fns = error_mark_node;
2833 declarator = lookup_template_function (fns, NULL_TREE);
2836 if (declarator == error_mark_node)
2837 return error_mark_node;
2839 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2841 if (!explicit_instantiation)
2842 /* A specialization in class scope. This is invalid,
2843 but the error will already have been flagged by
2844 check_specialization_scope. */
2845 return error_mark_node;
2846 else
2848 /* It's not valid to write an explicit instantiation in
2849 class scope, e.g.:
2851 class C { template void f(); }
2853 This case is caught by the parser. However, on
2854 something like:
2856 template class C { void f(); };
2858 (which is invalid) we can get here. The error will be
2859 issued later. */
2863 return decl;
2865 else if (ctype != NULL_TREE
2866 && (identifier_p (TREE_OPERAND (declarator, 0))))
2868 // We'll match variable templates in start_decl.
2869 if (VAR_P (decl))
2870 return decl;
2872 /* Find the list of functions in ctype that have the same
2873 name as the declared function. */
2874 tree name = TREE_OPERAND (declarator, 0);
2876 if (constructor_name_p (name, ctype))
2878 if (DECL_CONSTRUCTOR_P (decl)
2879 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2880 : !CLASSTYPE_DESTRUCTOR (ctype))
2882 /* From [temp.expl.spec]:
2884 If such an explicit specialization for the member
2885 of a class template names an implicitly-declared
2886 special member function (clause _special_), the
2887 program is ill-formed.
2889 Similar language is found in [temp.explicit]. */
2890 error ("specialization of implicitly-declared special member function");
2891 return error_mark_node;
2894 name = DECL_NAME (decl);
2897 /* For a type-conversion operator, We might be looking for
2898 `operator int' which will be a specialization of
2899 `operator T'. Grab all the conversion operators, and
2900 then select from them. */
2901 tree fns = lookup_fnfields_slot_nolazy (ctype,
2902 IDENTIFIER_CONV_OP_P (name)
2903 ? conv_op_identifier : name);
2905 if (fns == NULL_TREE)
2907 error ("no member function %qD declared in %qT", name, ctype);
2908 return error_mark_node;
2910 else
2911 TREE_OPERAND (declarator, 0) = fns;
2914 /* Figure out what exactly is being specialized at this point.
2915 Note that for an explicit instantiation, even one for a
2916 member function, we cannot tell a priori whether the
2917 instantiation is for a member template, or just a member
2918 function of a template class. Even if a member template is
2919 being instantiated, the member template arguments may be
2920 elided if they can be deduced from the rest of the
2921 declaration. */
2922 tmpl = determine_specialization (declarator, decl,
2923 &targs,
2924 member_specialization,
2925 template_count,
2926 tsk);
2928 if (!tmpl || tmpl == error_mark_node)
2929 /* We couldn't figure out what this declaration was
2930 specializing. */
2931 return error_mark_node;
2932 else
2934 if (TREE_CODE (decl) == FUNCTION_DECL
2935 && DECL_HIDDEN_FRIEND_P (tmpl))
2937 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2938 "friend declaration %qD is not visible to "
2939 "explicit specialization", tmpl))
2940 inform (DECL_SOURCE_LOCATION (tmpl),
2941 "friend declaration here");
2943 else if (!ctype && !is_friend
2944 && CP_DECL_CONTEXT (decl) == current_namespace)
2945 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2947 tree gen_tmpl = most_general_template (tmpl);
2949 if (explicit_instantiation)
2951 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2952 is done by do_decl_instantiation later. */
2954 int arg_depth = TMPL_ARGS_DEPTH (targs);
2955 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2957 if (arg_depth > parm_depth)
2959 /* If TMPL is not the most general template (for
2960 example, if TMPL is a friend template that is
2961 injected into namespace scope), then there will
2962 be too many levels of TARGS. Remove some of them
2963 here. */
2964 int i;
2965 tree new_targs;
2967 new_targs = make_tree_vec (parm_depth);
2968 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2969 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2970 = TREE_VEC_ELT (targs, i);
2971 targs = new_targs;
2974 return instantiate_template (tmpl, targs, tf_error);
2977 /* If we thought that the DECL was a member function, but it
2978 turns out to be specializing a static member function,
2979 make DECL a static member function as well. */
2980 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2981 && DECL_STATIC_FUNCTION_P (tmpl)
2982 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2983 revert_static_member_fn (decl);
2985 /* If this is a specialization of a member template of a
2986 template class, we want to return the TEMPLATE_DECL, not
2987 the specialization of it. */
2988 if (tsk == tsk_template && !was_template_id)
2990 tree result = DECL_TEMPLATE_RESULT (tmpl);
2991 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2992 DECL_INITIAL (result) = NULL_TREE;
2993 if (have_def)
2995 tree parm;
2996 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2997 DECL_SOURCE_LOCATION (result)
2998 = DECL_SOURCE_LOCATION (decl);
2999 /* We want to use the argument list specified in the
3000 definition, not in the original declaration. */
3001 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3002 for (parm = DECL_ARGUMENTS (result); parm;
3003 parm = DECL_CHAIN (parm))
3004 DECL_CONTEXT (parm) = result;
3006 return register_specialization (tmpl, gen_tmpl, targs,
3007 is_friend, 0);
3010 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3011 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3013 if (was_template_id)
3014 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3016 /* Inherit default function arguments from the template
3017 DECL is specializing. */
3018 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3019 copy_default_args_to_explicit_spec (decl);
3021 /* This specialization has the same protection as the
3022 template it specializes. */
3023 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3024 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3026 /* 7.1.1-1 [dcl.stc]
3028 A storage-class-specifier shall not be specified in an
3029 explicit specialization...
3031 The parser rejects these, so unless action is taken here,
3032 explicit function specializations will always appear with
3033 global linkage.
3035 The action recommended by the C++ CWG in response to C++
3036 defect report 605 is to make the storage class and linkage
3037 of the explicit specialization match the templated function:
3039 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3041 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3043 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3044 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3046 /* A concept cannot be specialized. */
3047 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3049 error ("explicit specialization of function concept %qD",
3050 gen_tmpl);
3051 return error_mark_node;
3054 /* This specialization has the same linkage and visibility as
3055 the function template it specializes. */
3056 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3057 if (! TREE_PUBLIC (decl))
3059 DECL_INTERFACE_KNOWN (decl) = 1;
3060 DECL_NOT_REALLY_EXTERN (decl) = 1;
3062 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3063 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3065 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3066 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3070 /* If DECL is a friend declaration, declared using an
3071 unqualified name, the namespace associated with DECL may
3072 have been set incorrectly. For example, in:
3074 template <typename T> void f(T);
3075 namespace N {
3076 struct S { friend void f<int>(int); }
3079 we will have set the DECL_CONTEXT for the friend
3080 declaration to N, rather than to the global namespace. */
3081 if (DECL_NAMESPACE_SCOPE_P (decl))
3082 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3084 if (is_friend && !have_def)
3085 /* This is not really a declaration of a specialization.
3086 It's just the name of an instantiation. But, it's not
3087 a request for an instantiation, either. */
3088 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3089 else if (TREE_CODE (decl) == FUNCTION_DECL)
3090 /* A specialization is not necessarily COMDAT. */
3091 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3092 && DECL_DECLARED_INLINE_P (decl));
3093 else if (VAR_P (decl))
3094 DECL_COMDAT (decl) = false;
3096 /* If this is a full specialization, register it so that we can find
3097 it again. Partial specializations will be registered in
3098 process_partial_specialization. */
3099 if (!processing_template_decl)
3100 decl = register_specialization (decl, gen_tmpl, targs,
3101 is_friend, 0);
3103 /* A 'structor should already have clones. */
3104 gcc_assert (decl == error_mark_node
3105 || variable_template_p (tmpl)
3106 || !(DECL_CONSTRUCTOR_P (decl)
3107 || DECL_DESTRUCTOR_P (decl))
3108 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3112 return decl;
3115 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3116 parameters. These are represented in the same format used for
3117 DECL_TEMPLATE_PARMS. */
3120 comp_template_parms (const_tree parms1, const_tree parms2)
3122 const_tree p1;
3123 const_tree p2;
3125 if (parms1 == parms2)
3126 return 1;
3128 for (p1 = parms1, p2 = parms2;
3129 p1 != NULL_TREE && p2 != NULL_TREE;
3130 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3132 tree t1 = TREE_VALUE (p1);
3133 tree t2 = TREE_VALUE (p2);
3134 int i;
3136 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3137 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3139 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3140 return 0;
3142 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3144 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3145 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3147 /* If either of the template parameters are invalid, assume
3148 they match for the sake of error recovery. */
3149 if (error_operand_p (parm1) || error_operand_p (parm2))
3150 return 1;
3152 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3153 return 0;
3155 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3156 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3157 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3158 continue;
3159 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3160 return 0;
3164 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3165 /* One set of parameters has more parameters lists than the
3166 other. */
3167 return 0;
3169 return 1;
3172 /* Determine whether PARM is a parameter pack. */
3174 bool
3175 template_parameter_pack_p (const_tree parm)
3177 /* Determine if we have a non-type template parameter pack. */
3178 if (TREE_CODE (parm) == PARM_DECL)
3179 return (DECL_TEMPLATE_PARM_P (parm)
3180 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3181 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3182 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3184 /* If this is a list of template parameters, we could get a
3185 TYPE_DECL or a TEMPLATE_DECL. */
3186 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3187 parm = TREE_TYPE (parm);
3189 /* Otherwise it must be a type template parameter. */
3190 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3191 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3192 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3195 /* Determine if T is a function parameter pack. */
3197 bool
3198 function_parameter_pack_p (const_tree t)
3200 if (t && TREE_CODE (t) == PARM_DECL)
3201 return DECL_PACK_P (t);
3202 return false;
3205 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3206 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3208 tree
3209 get_function_template_decl (const_tree primary_func_tmpl_inst)
3211 if (! primary_func_tmpl_inst
3212 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3213 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3214 return NULL;
3216 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3219 /* Return true iff the function parameter PARAM_DECL was expanded
3220 from the function parameter pack PACK. */
3222 bool
3223 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3225 if (DECL_ARTIFICIAL (param_decl)
3226 || !function_parameter_pack_p (pack))
3227 return false;
3229 /* The parameter pack and its pack arguments have the same
3230 DECL_PARM_INDEX. */
3231 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3234 /* Determine whether ARGS describes a variadic template args list,
3235 i.e., one that is terminated by a template argument pack. */
3237 static bool
3238 template_args_variadic_p (tree args)
3240 int nargs;
3241 tree last_parm;
3243 if (args == NULL_TREE)
3244 return false;
3246 args = INNERMOST_TEMPLATE_ARGS (args);
3247 nargs = TREE_VEC_LENGTH (args);
3249 if (nargs == 0)
3250 return false;
3252 last_parm = TREE_VEC_ELT (args, nargs - 1);
3254 return ARGUMENT_PACK_P (last_parm);
3257 /* Generate a new name for the parameter pack name NAME (an
3258 IDENTIFIER_NODE) that incorporates its */
3260 static tree
3261 make_ith_pack_parameter_name (tree name, int i)
3263 /* Munge the name to include the parameter index. */
3264 #define NUMBUF_LEN 128
3265 char numbuf[NUMBUF_LEN];
3266 char* newname;
3267 int newname_len;
3269 if (name == NULL_TREE)
3270 return name;
3271 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3272 newname_len = IDENTIFIER_LENGTH (name)
3273 + strlen (numbuf) + 2;
3274 newname = (char*)alloca (newname_len);
3275 snprintf (newname, newname_len,
3276 "%s#%i", IDENTIFIER_POINTER (name), i);
3277 return get_identifier (newname);
3280 /* Return true if T is a primary function, class or alias template
3281 instantiation. */
3283 bool
3284 primary_template_instantiation_p (const_tree t)
3286 if (!t)
3287 return false;
3289 if (TREE_CODE (t) == FUNCTION_DECL)
3290 return DECL_LANG_SPECIFIC (t)
3291 && DECL_TEMPLATE_INSTANTIATION (t)
3292 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3293 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3294 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3295 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3296 else if (alias_template_specialization_p (t))
3297 return true;
3298 return false;
3301 /* Return true if PARM is a template template parameter. */
3303 bool
3304 template_template_parameter_p (const_tree parm)
3306 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3309 /* Return true iff PARM is a DECL representing a type template
3310 parameter. */
3312 bool
3313 template_type_parameter_p (const_tree parm)
3315 return (parm
3316 && (TREE_CODE (parm) == TYPE_DECL
3317 || TREE_CODE (parm) == TEMPLATE_DECL)
3318 && DECL_TEMPLATE_PARM_P (parm));
3321 /* Return the template parameters of T if T is a
3322 primary template instantiation, NULL otherwise. */
3324 tree
3325 get_primary_template_innermost_parameters (const_tree t)
3327 tree parms = NULL, template_info = NULL;
3329 if ((template_info = get_template_info (t))
3330 && primary_template_instantiation_p (t))
3331 parms = INNERMOST_TEMPLATE_PARMS
3332 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3334 return parms;
3337 /* Return the template parameters of the LEVELth level from the full list
3338 of template parameters PARMS. */
3340 tree
3341 get_template_parms_at_level (tree parms, int level)
3343 tree p;
3344 if (!parms
3345 || TREE_CODE (parms) != TREE_LIST
3346 || level > TMPL_PARMS_DEPTH (parms))
3347 return NULL_TREE;
3349 for (p = parms; p; p = TREE_CHAIN (p))
3350 if (TMPL_PARMS_DEPTH (p) == level)
3351 return p;
3353 return NULL_TREE;
3356 /* Returns the template arguments of T if T is a template instantiation,
3357 NULL otherwise. */
3359 tree
3360 get_template_innermost_arguments (const_tree t)
3362 tree args = NULL, template_info = NULL;
3364 if ((template_info = get_template_info (t))
3365 && TI_ARGS (template_info))
3366 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3368 return args;
3371 /* Return the argument pack elements of T if T is a template argument pack,
3372 NULL otherwise. */
3374 tree
3375 get_template_argument_pack_elems (const_tree t)
3377 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3378 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3379 return NULL;
3381 return ARGUMENT_PACK_ARGS (t);
3384 /* True iff FN is a function representing a built-in variadic parameter
3385 pack. */
3387 bool
3388 builtin_pack_fn_p (tree fn)
3390 if (!fn
3391 || TREE_CODE (fn) != FUNCTION_DECL
3392 || !DECL_IS_BUILTIN (fn))
3393 return false;
3395 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3396 return true;
3398 return false;
3401 /* True iff CALL is a call to a function representing a built-in variadic
3402 parameter pack. */
3404 static bool
3405 builtin_pack_call_p (tree call)
3407 if (TREE_CODE (call) != CALL_EXPR)
3408 return false;
3409 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3412 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3414 static tree
3415 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3416 tree in_decl)
3418 tree ohi = CALL_EXPR_ARG (call, 0);
3419 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3420 false/*fn*/, true/*int_cst*/);
3422 if (value_dependent_expression_p (hi))
3424 if (hi != ohi)
3426 call = copy_node (call);
3427 CALL_EXPR_ARG (call, 0) = hi;
3429 tree ex = make_pack_expansion (call);
3430 tree vec = make_tree_vec (1);
3431 TREE_VEC_ELT (vec, 0) = ex;
3432 return vec;
3434 else
3436 hi = cxx_constant_value (hi);
3437 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3439 /* Calculate the largest value of len that won't make the size of the vec
3440 overflow an int. The compiler will exceed resource limits long before
3441 this, but it seems a decent place to diagnose. */
3442 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3444 if (len < 0 || len > max)
3446 if ((complain & tf_error)
3447 && hi != error_mark_node)
3448 error ("argument to __integer_pack must be between 0 and %d", max);
3449 return error_mark_node;
3452 tree vec = make_tree_vec (len);
3454 for (int i = 0; i < len; ++i)
3455 TREE_VEC_ELT (vec, i) = size_int (i);
3457 return vec;
3461 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3462 CALL. */
3464 static tree
3465 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3466 tree in_decl)
3468 if (!builtin_pack_call_p (call))
3469 return NULL_TREE;
3471 tree fn = CALL_EXPR_FN (call);
3473 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3474 return expand_integer_pack (call, args, complain, in_decl);
3476 return NULL_TREE;
3479 /* Structure used to track the progress of find_parameter_packs_r. */
3480 struct find_parameter_pack_data
3482 /* TREE_LIST that will contain all of the parameter packs found by
3483 the traversal. */
3484 tree* parameter_packs;
3486 /* Set of AST nodes that have been visited by the traversal. */
3487 hash_set<tree> *visited;
3489 /* True iff we're making a type pack expansion. */
3490 bool type_pack_expansion_p;
3493 /* Identifies all of the argument packs that occur in a template
3494 argument and appends them to the TREE_LIST inside DATA, which is a
3495 find_parameter_pack_data structure. This is a subroutine of
3496 make_pack_expansion and uses_parameter_packs. */
3497 static tree
3498 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3500 tree t = *tp;
3501 struct find_parameter_pack_data* ppd =
3502 (struct find_parameter_pack_data*)data;
3503 bool parameter_pack_p = false;
3505 /* Handle type aliases/typedefs. */
3506 if (TYPE_ALIAS_P (t))
3508 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3509 cp_walk_tree (&TI_ARGS (tinfo),
3510 &find_parameter_packs_r,
3511 ppd, ppd->visited);
3512 *walk_subtrees = 0;
3513 return NULL_TREE;
3516 /* Identify whether this is a parameter pack or not. */
3517 switch (TREE_CODE (t))
3519 case TEMPLATE_PARM_INDEX:
3520 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3521 parameter_pack_p = true;
3522 break;
3524 case TEMPLATE_TYPE_PARM:
3525 t = TYPE_MAIN_VARIANT (t);
3526 /* FALLTHRU */
3527 case TEMPLATE_TEMPLATE_PARM:
3528 /* If the placeholder appears in the decl-specifier-seq of a function
3529 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3530 is a pack expansion, the invented template parameter is a template
3531 parameter pack. */
3532 if (ppd->type_pack_expansion_p && is_auto (t))
3533 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3534 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3535 parameter_pack_p = true;
3536 break;
3538 case FIELD_DECL:
3539 case PARM_DECL:
3540 if (DECL_PACK_P (t))
3542 /* We don't want to walk into the type of a PARM_DECL,
3543 because we don't want to see the type parameter pack. */
3544 *walk_subtrees = 0;
3545 parameter_pack_p = true;
3547 break;
3549 /* Look through a lambda capture proxy to the field pack. */
3550 case VAR_DECL:
3551 if (DECL_HAS_VALUE_EXPR_P (t))
3553 tree v = DECL_VALUE_EXPR (t);
3554 cp_walk_tree (&v,
3555 &find_parameter_packs_r,
3556 ppd, ppd->visited);
3557 *walk_subtrees = 0;
3559 else if (variable_template_specialization_p (t))
3561 cp_walk_tree (&DECL_TI_ARGS (t),
3562 find_parameter_packs_r,
3563 ppd, ppd->visited);
3564 *walk_subtrees = 0;
3566 break;
3568 case CALL_EXPR:
3569 if (builtin_pack_call_p (t))
3570 parameter_pack_p = true;
3571 break;
3573 case BASES:
3574 parameter_pack_p = true;
3575 break;
3576 default:
3577 /* Not a parameter pack. */
3578 break;
3581 if (parameter_pack_p)
3583 /* Add this parameter pack to the list. */
3584 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3587 if (TYPE_P (t))
3588 cp_walk_tree (&TYPE_CONTEXT (t),
3589 &find_parameter_packs_r, ppd, ppd->visited);
3591 /* This switch statement will return immediately if we don't find a
3592 parameter pack. */
3593 switch (TREE_CODE (t))
3595 case TEMPLATE_PARM_INDEX:
3596 return NULL_TREE;
3598 case BOUND_TEMPLATE_TEMPLATE_PARM:
3599 /* Check the template itself. */
3600 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3601 &find_parameter_packs_r, ppd, ppd->visited);
3602 /* Check the template arguments. */
3603 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3604 ppd->visited);
3605 *walk_subtrees = 0;
3606 return NULL_TREE;
3608 case TEMPLATE_TYPE_PARM:
3609 case TEMPLATE_TEMPLATE_PARM:
3610 return NULL_TREE;
3612 case PARM_DECL:
3613 return NULL_TREE;
3615 case RECORD_TYPE:
3616 if (TYPE_PTRMEMFUNC_P (t))
3617 return NULL_TREE;
3618 /* Fall through. */
3620 case UNION_TYPE:
3621 case ENUMERAL_TYPE:
3622 if (TYPE_TEMPLATE_INFO (t))
3623 cp_walk_tree (&TYPE_TI_ARGS (t),
3624 &find_parameter_packs_r, ppd, ppd->visited);
3626 *walk_subtrees = 0;
3627 return NULL_TREE;
3629 case TEMPLATE_DECL:
3630 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3631 return NULL_TREE;
3632 gcc_fallthrough();
3634 case CONSTRUCTOR:
3635 cp_walk_tree (&TREE_TYPE (t),
3636 &find_parameter_packs_r, ppd, ppd->visited);
3637 return NULL_TREE;
3639 case TYPENAME_TYPE:
3640 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3641 ppd, ppd->visited);
3642 *walk_subtrees = 0;
3643 return NULL_TREE;
3645 case TYPE_PACK_EXPANSION:
3646 case EXPR_PACK_EXPANSION:
3647 *walk_subtrees = 0;
3648 return NULL_TREE;
3650 case INTEGER_TYPE:
3651 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3652 ppd, ppd->visited);
3653 *walk_subtrees = 0;
3654 return NULL_TREE;
3656 case IDENTIFIER_NODE:
3657 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3658 ppd->visited);
3659 *walk_subtrees = 0;
3660 return NULL_TREE;
3662 case DECLTYPE_TYPE:
3664 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3665 type_pack_expansion_p to false so that any placeholders
3666 within the expression don't get marked as parameter packs. */
3667 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3668 ppd->type_pack_expansion_p = false;
3669 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3670 ppd, ppd->visited);
3671 ppd->type_pack_expansion_p = type_pack_expansion_p;
3672 *walk_subtrees = 0;
3673 return NULL_TREE;
3676 default:
3677 return NULL_TREE;
3680 return NULL_TREE;
3683 /* Determines if the expression or type T uses any parameter packs. */
3684 bool
3685 uses_parameter_packs (tree t)
3687 tree parameter_packs = NULL_TREE;
3688 struct find_parameter_pack_data ppd;
3689 ppd.parameter_packs = &parameter_packs;
3690 ppd.visited = new hash_set<tree>;
3691 ppd.type_pack_expansion_p = false;
3692 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3693 delete ppd.visited;
3694 return parameter_packs != NULL_TREE;
3697 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3698 representation a base-class initializer into a parameter pack
3699 expansion. If all goes well, the resulting node will be an
3700 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3701 respectively. */
3702 tree
3703 make_pack_expansion (tree arg)
3705 tree result;
3706 tree parameter_packs = NULL_TREE;
3707 bool for_types = false;
3708 struct find_parameter_pack_data ppd;
3710 if (!arg || arg == error_mark_node)
3711 return arg;
3713 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3715 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3716 class initializer. In this case, the TREE_PURPOSE will be a
3717 _TYPE node (representing the base class expansion we're
3718 initializing) and the TREE_VALUE will be a TREE_LIST
3719 containing the initialization arguments.
3721 The resulting expansion looks somewhat different from most
3722 expansions. Rather than returning just one _EXPANSION, we
3723 return a TREE_LIST whose TREE_PURPOSE is a
3724 TYPE_PACK_EXPANSION containing the bases that will be
3725 initialized. The TREE_VALUE will be identical to the
3726 original TREE_VALUE, which is a list of arguments that will
3727 be passed to each base. We do not introduce any new pack
3728 expansion nodes into the TREE_VALUE (although it is possible
3729 that some already exist), because the TREE_PURPOSE and
3730 TREE_VALUE all need to be expanded together with the same
3731 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3732 resulting TREE_PURPOSE will mention the parameter packs in
3733 both the bases and the arguments to the bases. */
3734 tree purpose;
3735 tree value;
3736 tree parameter_packs = NULL_TREE;
3738 /* Determine which parameter packs will be used by the base
3739 class expansion. */
3740 ppd.visited = new hash_set<tree>;
3741 ppd.parameter_packs = &parameter_packs;
3742 ppd.type_pack_expansion_p = true;
3743 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3744 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3745 &ppd, ppd.visited);
3747 if (parameter_packs == NULL_TREE)
3749 error ("base initializer expansion %qT contains no parameter packs", arg);
3750 delete ppd.visited;
3751 return error_mark_node;
3754 if (TREE_VALUE (arg) != void_type_node)
3756 /* Collect the sets of parameter packs used in each of the
3757 initialization arguments. */
3758 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3760 /* Determine which parameter packs will be expanded in this
3761 argument. */
3762 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3763 &ppd, ppd.visited);
3767 delete ppd.visited;
3769 /* Create the pack expansion type for the base type. */
3770 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3771 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3772 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3774 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3775 they will rarely be compared to anything. */
3776 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3778 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3781 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3782 for_types = true;
3784 /* Build the PACK_EXPANSION_* node. */
3785 result = for_types
3786 ? cxx_make_type (TYPE_PACK_EXPANSION)
3787 : make_node (EXPR_PACK_EXPANSION);
3788 SET_PACK_EXPANSION_PATTERN (result, arg);
3789 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3791 /* Propagate type and const-expression information. */
3792 TREE_TYPE (result) = TREE_TYPE (arg);
3793 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3794 /* Mark this read now, since the expansion might be length 0. */
3795 mark_exp_read (arg);
3797 else
3798 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3799 they will rarely be compared to anything. */
3800 SET_TYPE_STRUCTURAL_EQUALITY (result);
3802 /* Determine which parameter packs will be expanded. */
3803 ppd.parameter_packs = &parameter_packs;
3804 ppd.visited = new hash_set<tree>;
3805 ppd.type_pack_expansion_p = TYPE_P (arg);
3806 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3807 delete ppd.visited;
3809 /* Make sure we found some parameter packs. */
3810 if (parameter_packs == NULL_TREE)
3812 if (TYPE_P (arg))
3813 error ("expansion pattern %qT contains no argument packs", arg);
3814 else
3815 error ("expansion pattern %qE contains no argument packs", arg);
3816 return error_mark_node;
3818 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3820 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3822 return result;
3825 /* Checks T for any "bare" parameter packs, which have not yet been
3826 expanded, and issues an error if any are found. This operation can
3827 only be done on full expressions or types (e.g., an expression
3828 statement, "if" condition, etc.), because we could have expressions like:
3830 foo(f(g(h(args)))...)
3832 where "args" is a parameter pack. check_for_bare_parameter_packs
3833 should not be called for the subexpressions args, h(args),
3834 g(h(args)), or f(g(h(args))), because we would produce erroneous
3835 error messages.
3837 Returns TRUE and emits an error if there were bare parameter packs,
3838 returns FALSE otherwise. */
3839 bool
3840 check_for_bare_parameter_packs (tree t)
3842 tree parameter_packs = NULL_TREE;
3843 struct find_parameter_pack_data ppd;
3845 if (!processing_template_decl || !t || t == error_mark_node)
3846 return false;
3848 if (TREE_CODE (t) == TYPE_DECL)
3849 t = TREE_TYPE (t);
3851 ppd.parameter_packs = &parameter_packs;
3852 ppd.visited = new hash_set<tree>;
3853 ppd.type_pack_expansion_p = false;
3854 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3855 delete ppd.visited;
3857 if (parameter_packs)
3859 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3860 error_at (loc, "parameter packs not expanded with %<...%>:");
3861 while (parameter_packs)
3863 tree pack = TREE_VALUE (parameter_packs);
3864 tree name = NULL_TREE;
3866 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3867 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3868 name = TYPE_NAME (pack);
3869 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3870 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3871 else if (TREE_CODE (pack) == CALL_EXPR)
3872 name = DECL_NAME (CALL_EXPR_FN (pack));
3873 else
3874 name = DECL_NAME (pack);
3876 if (name)
3877 inform (loc, " %qD", name);
3878 else
3879 inform (loc, " <anonymous>");
3881 parameter_packs = TREE_CHAIN (parameter_packs);
3884 return true;
3887 return false;
3890 /* Expand any parameter packs that occur in the template arguments in
3891 ARGS. */
3892 tree
3893 expand_template_argument_pack (tree args)
3895 if (args == error_mark_node)
3896 return error_mark_node;
3898 tree result_args = NULL_TREE;
3899 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3900 int num_result_args = -1;
3901 int non_default_args_count = -1;
3903 /* First, determine if we need to expand anything, and the number of
3904 slots we'll need. */
3905 for (in_arg = 0; in_arg < nargs; ++in_arg)
3907 tree arg = TREE_VEC_ELT (args, in_arg);
3908 if (arg == NULL_TREE)
3909 return args;
3910 if (ARGUMENT_PACK_P (arg))
3912 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3913 if (num_result_args < 0)
3914 num_result_args = in_arg + num_packed;
3915 else
3916 num_result_args += num_packed;
3918 else
3920 if (num_result_args >= 0)
3921 num_result_args++;
3925 /* If no expansion is necessary, we're done. */
3926 if (num_result_args < 0)
3927 return args;
3929 /* Expand arguments. */
3930 result_args = make_tree_vec (num_result_args);
3931 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3932 non_default_args_count =
3933 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3934 for (in_arg = 0; in_arg < nargs; ++in_arg)
3936 tree arg = TREE_VEC_ELT (args, in_arg);
3937 if (ARGUMENT_PACK_P (arg))
3939 tree packed = ARGUMENT_PACK_ARGS (arg);
3940 int i, num_packed = TREE_VEC_LENGTH (packed);
3941 for (i = 0; i < num_packed; ++i, ++out_arg)
3942 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3943 if (non_default_args_count > 0)
3944 non_default_args_count += num_packed - 1;
3946 else
3948 TREE_VEC_ELT (result_args, out_arg) = arg;
3949 ++out_arg;
3952 if (non_default_args_count >= 0)
3953 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3954 return result_args;
3957 /* Checks if DECL shadows a template parameter.
3959 [temp.local]: A template-parameter shall not be redeclared within its
3960 scope (including nested scopes).
3962 Emits an error and returns TRUE if the DECL shadows a parameter,
3963 returns FALSE otherwise. */
3965 bool
3966 check_template_shadow (tree decl)
3968 tree olddecl;
3970 /* If we're not in a template, we can't possibly shadow a template
3971 parameter. */
3972 if (!current_template_parms)
3973 return true;
3975 /* Figure out what we're shadowing. */
3976 decl = OVL_FIRST (decl);
3977 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3979 /* If there's no previous binding for this name, we're not shadowing
3980 anything, let alone a template parameter. */
3981 if (!olddecl)
3982 return true;
3984 /* If we're not shadowing a template parameter, we're done. Note
3985 that OLDDECL might be an OVERLOAD (or perhaps even an
3986 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3987 node. */
3988 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3989 return true;
3991 /* We check for decl != olddecl to avoid bogus errors for using a
3992 name inside a class. We check TPFI to avoid duplicate errors for
3993 inline member templates. */
3994 if (decl == olddecl
3995 || (DECL_TEMPLATE_PARM_P (decl)
3996 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3997 return true;
3999 /* Don't complain about the injected class name, as we've already
4000 complained about the class itself. */
4001 if (DECL_SELF_REFERENCE_P (decl))
4002 return false;
4004 if (DECL_TEMPLATE_PARM_P (decl))
4005 error ("declaration of template parameter %q+D shadows "
4006 "template parameter", decl);
4007 else
4008 error ("declaration of %q+#D shadows template parameter", decl);
4009 inform (DECL_SOURCE_LOCATION (olddecl),
4010 "template parameter %qD declared here", olddecl);
4011 return false;
4014 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4015 ORIG_LEVEL, DECL, and TYPE. */
4017 static tree
4018 build_template_parm_index (int index,
4019 int level,
4020 int orig_level,
4021 tree decl,
4022 tree type)
4024 tree t = make_node (TEMPLATE_PARM_INDEX);
4025 TEMPLATE_PARM_IDX (t) = index;
4026 TEMPLATE_PARM_LEVEL (t) = level;
4027 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4028 TEMPLATE_PARM_DECL (t) = decl;
4029 TREE_TYPE (t) = type;
4030 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4031 TREE_READONLY (t) = TREE_READONLY (decl);
4033 return t;
4036 /* Find the canonical type parameter for the given template type
4037 parameter. Returns the canonical type parameter, which may be TYPE
4038 if no such parameter existed. */
4040 static tree
4041 canonical_type_parameter (tree type)
4043 tree list;
4044 int idx = TEMPLATE_TYPE_IDX (type);
4045 if (!canonical_template_parms)
4046 vec_alloc (canonical_template_parms, idx + 1);
4048 if (canonical_template_parms->length () <= (unsigned) idx)
4049 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4051 list = (*canonical_template_parms)[idx];
4052 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4053 list = TREE_CHAIN (list);
4055 if (list)
4056 return TREE_VALUE (list);
4057 else
4059 (*canonical_template_parms)[idx]
4060 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4061 return type;
4065 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4066 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4067 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4068 new one is created. */
4070 static tree
4071 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4072 tsubst_flags_t complain)
4074 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4075 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4076 != TEMPLATE_PARM_LEVEL (index) - levels)
4077 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4079 tree orig_decl = TEMPLATE_PARM_DECL (index);
4080 tree decl, t;
4082 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4083 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4084 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4085 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4086 DECL_ARTIFICIAL (decl) = 1;
4087 SET_DECL_TEMPLATE_PARM_P (decl);
4089 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4090 TEMPLATE_PARM_LEVEL (index) - levels,
4091 TEMPLATE_PARM_ORIG_LEVEL (index),
4092 decl, type);
4093 TEMPLATE_PARM_DESCENDANTS (index) = t;
4094 TEMPLATE_PARM_PARAMETER_PACK (t)
4095 = TEMPLATE_PARM_PARAMETER_PACK (index);
4097 /* Template template parameters need this. */
4098 if (TREE_CODE (decl) == TEMPLATE_DECL)
4100 DECL_TEMPLATE_RESULT (decl)
4101 = build_decl (DECL_SOURCE_LOCATION (decl),
4102 TYPE_DECL, DECL_NAME (decl), type);
4103 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4104 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4105 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4109 return TEMPLATE_PARM_DESCENDANTS (index);
4112 /* Process information from new template parameter PARM and append it
4113 to the LIST being built. This new parameter is a non-type
4114 parameter iff IS_NON_TYPE is true. This new parameter is a
4115 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4116 is in PARM_LOC. */
4118 tree
4119 process_template_parm (tree list, location_t parm_loc, tree parm,
4120 bool is_non_type, bool is_parameter_pack)
4122 tree decl = 0;
4123 int idx = 0;
4125 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4126 tree defval = TREE_PURPOSE (parm);
4127 tree constr = TREE_TYPE (parm);
4129 if (list)
4131 tree p = tree_last (list);
4133 if (p && TREE_VALUE (p) != error_mark_node)
4135 p = TREE_VALUE (p);
4136 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4137 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4138 else
4139 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4142 ++idx;
4145 if (is_non_type)
4147 parm = TREE_VALUE (parm);
4149 SET_DECL_TEMPLATE_PARM_P (parm);
4151 if (TREE_TYPE (parm) != error_mark_node)
4153 /* [temp.param]
4155 The top-level cv-qualifiers on the template-parameter are
4156 ignored when determining its type. */
4157 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4158 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4159 TREE_TYPE (parm) = error_mark_node;
4160 else if (uses_parameter_packs (TREE_TYPE (parm))
4161 && !is_parameter_pack
4162 /* If we're in a nested template parameter list, the template
4163 template parameter could be a parameter pack. */
4164 && processing_template_parmlist == 1)
4166 /* This template parameter is not a parameter pack, but it
4167 should be. Complain about "bare" parameter packs. */
4168 check_for_bare_parameter_packs (TREE_TYPE (parm));
4170 /* Recover by calling this a parameter pack. */
4171 is_parameter_pack = true;
4175 /* A template parameter is not modifiable. */
4176 TREE_CONSTANT (parm) = 1;
4177 TREE_READONLY (parm) = 1;
4178 decl = build_decl (parm_loc,
4179 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4180 TREE_CONSTANT (decl) = 1;
4181 TREE_READONLY (decl) = 1;
4182 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4183 = build_template_parm_index (idx, processing_template_decl,
4184 processing_template_decl,
4185 decl, TREE_TYPE (parm));
4187 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4188 = is_parameter_pack;
4190 else
4192 tree t;
4193 parm = TREE_VALUE (TREE_VALUE (parm));
4195 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4197 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4198 /* This is for distinguishing between real templates and template
4199 template parameters */
4200 TREE_TYPE (parm) = t;
4201 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4202 decl = parm;
4204 else
4206 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4207 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4208 decl = build_decl (parm_loc,
4209 TYPE_DECL, parm, t);
4212 TYPE_NAME (t) = decl;
4213 TYPE_STUB_DECL (t) = decl;
4214 parm = decl;
4215 TEMPLATE_TYPE_PARM_INDEX (t)
4216 = build_template_parm_index (idx, processing_template_decl,
4217 processing_template_decl,
4218 decl, TREE_TYPE (parm));
4219 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4220 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4222 DECL_ARTIFICIAL (decl) = 1;
4223 SET_DECL_TEMPLATE_PARM_P (decl);
4225 /* Build requirements for the type/template parameter.
4226 This must be done after SET_DECL_TEMPLATE_PARM_P or
4227 process_template_parm could fail. */
4228 tree reqs = finish_shorthand_constraint (parm, constr);
4230 pushdecl (decl);
4232 /* Build the parameter node linking the parameter declaration,
4233 its default argument (if any), and its constraints (if any). */
4234 parm = build_tree_list (defval, parm);
4235 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4237 return chainon (list, parm);
4240 /* The end of a template parameter list has been reached. Process the
4241 tree list into a parameter vector, converting each parameter into a more
4242 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4243 as PARM_DECLs. */
4245 tree
4246 end_template_parm_list (tree parms)
4248 int nparms;
4249 tree parm, next;
4250 tree saved_parmlist = make_tree_vec (list_length (parms));
4252 /* Pop the dummy parameter level and add the real one. */
4253 current_template_parms = TREE_CHAIN (current_template_parms);
4255 current_template_parms
4256 = tree_cons (size_int (processing_template_decl),
4257 saved_parmlist, current_template_parms);
4259 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4261 next = TREE_CHAIN (parm);
4262 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4263 TREE_CHAIN (parm) = NULL_TREE;
4266 --processing_template_parmlist;
4268 return saved_parmlist;
4271 // Explicitly indicate the end of the template parameter list. We assume
4272 // that the current template parameters have been constructed and/or
4273 // managed explicitly, as when creating new template template parameters
4274 // from a shorthand constraint.
4275 void
4276 end_template_parm_list ()
4278 --processing_template_parmlist;
4281 /* end_template_decl is called after a template declaration is seen. */
4283 void
4284 end_template_decl (void)
4286 reset_specialization ();
4288 if (! processing_template_decl)
4289 return;
4291 /* This matches the pushlevel in begin_template_parm_list. */
4292 finish_scope ();
4294 --processing_template_decl;
4295 current_template_parms = TREE_CHAIN (current_template_parms);
4298 /* Takes a TREE_LIST representing a template parameter and convert it
4299 into an argument suitable to be passed to the type substitution
4300 functions. Note that If the TREE_LIST contains an error_mark
4301 node, the returned argument is error_mark_node. */
4303 tree
4304 template_parm_to_arg (tree t)
4307 if (t == NULL_TREE
4308 || TREE_CODE (t) != TREE_LIST)
4309 return t;
4311 if (error_operand_p (TREE_VALUE (t)))
4312 return error_mark_node;
4314 t = TREE_VALUE (t);
4316 if (TREE_CODE (t) == TYPE_DECL
4317 || TREE_CODE (t) == TEMPLATE_DECL)
4319 t = TREE_TYPE (t);
4321 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4323 /* Turn this argument into a TYPE_ARGUMENT_PACK
4324 with a single element, which expands T. */
4325 tree vec = make_tree_vec (1);
4326 if (CHECKING_P)
4327 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4329 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4331 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4332 SET_ARGUMENT_PACK_ARGS (t, vec);
4335 else
4337 t = DECL_INITIAL (t);
4339 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4341 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4342 with a single element, which expands T. */
4343 tree vec = make_tree_vec (1);
4344 if (CHECKING_P)
4345 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4347 t = convert_from_reference (t);
4348 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4350 t = make_node (NONTYPE_ARGUMENT_PACK);
4351 SET_ARGUMENT_PACK_ARGS (t, vec);
4353 else
4354 t = convert_from_reference (t);
4356 return t;
4359 /* Given a single level of template parameters (a TREE_VEC), return it
4360 as a set of template arguments. */
4362 static tree
4363 template_parms_level_to_args (tree parms)
4365 tree a = copy_node (parms);
4366 TREE_TYPE (a) = NULL_TREE;
4367 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4368 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4370 if (CHECKING_P)
4371 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4373 return a;
4376 /* Given a set of template parameters, return them as a set of template
4377 arguments. The template parameters are represented as a TREE_VEC, in
4378 the form documented in cp-tree.h for template arguments. */
4380 static tree
4381 template_parms_to_args (tree parms)
4383 tree header;
4384 tree args = NULL_TREE;
4385 int length = TMPL_PARMS_DEPTH (parms);
4386 int l = length;
4388 /* If there is only one level of template parameters, we do not
4389 create a TREE_VEC of TREE_VECs. Instead, we return a single
4390 TREE_VEC containing the arguments. */
4391 if (length > 1)
4392 args = make_tree_vec (length);
4394 for (header = parms; header; header = TREE_CHAIN (header))
4396 tree a = template_parms_level_to_args (TREE_VALUE (header));
4398 if (length > 1)
4399 TREE_VEC_ELT (args, --l) = a;
4400 else
4401 args = a;
4404 return args;
4407 /* Within the declaration of a template, return the currently active
4408 template parameters as an argument TREE_VEC. */
4410 static tree
4411 current_template_args (void)
4413 return template_parms_to_args (current_template_parms);
4416 /* Update the declared TYPE by doing any lookups which were thought to be
4417 dependent, but are not now that we know the SCOPE of the declarator. */
4419 tree
4420 maybe_update_decl_type (tree orig_type, tree scope)
4422 tree type = orig_type;
4424 if (type == NULL_TREE)
4425 return type;
4427 if (TREE_CODE (orig_type) == TYPE_DECL)
4428 type = TREE_TYPE (type);
4430 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4431 && dependent_type_p (type)
4432 /* Don't bother building up the args in this case. */
4433 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4435 /* tsubst in the args corresponding to the template parameters,
4436 including auto if present. Most things will be unchanged, but
4437 make_typename_type and tsubst_qualified_id will resolve
4438 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4439 tree args = current_template_args ();
4440 tree auto_node = type_uses_auto (type);
4441 tree pushed;
4442 if (auto_node)
4444 tree auto_vec = make_tree_vec (1);
4445 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4446 args = add_to_template_args (args, auto_vec);
4448 pushed = push_scope (scope);
4449 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4450 if (pushed)
4451 pop_scope (scope);
4454 if (type == error_mark_node)
4455 return orig_type;
4457 if (TREE_CODE (orig_type) == TYPE_DECL)
4459 if (same_type_p (type, TREE_TYPE (orig_type)))
4460 type = orig_type;
4461 else
4462 type = TYPE_NAME (type);
4464 return type;
4467 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4468 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4469 the new template is a member template. */
4471 tree
4472 build_template_decl (tree decl, tree parms, bool member_template_p)
4474 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4475 DECL_TEMPLATE_PARMS (tmpl) = parms;
4476 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4477 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4478 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4480 return tmpl;
4483 struct template_parm_data
4485 /* The level of the template parameters we are currently
4486 processing. */
4487 int level;
4489 /* The index of the specialization argument we are currently
4490 processing. */
4491 int current_arg;
4493 /* An array whose size is the number of template parameters. The
4494 elements are nonzero if the parameter has been used in any one
4495 of the arguments processed so far. */
4496 int* parms;
4498 /* An array whose size is the number of template arguments. The
4499 elements are nonzero if the argument makes use of template
4500 parameters of this level. */
4501 int* arg_uses_template_parms;
4504 /* Subroutine of push_template_decl used to see if each template
4505 parameter in a partial specialization is used in the explicit
4506 argument list. If T is of the LEVEL given in DATA (which is
4507 treated as a template_parm_data*), then DATA->PARMS is marked
4508 appropriately. */
4510 static int
4511 mark_template_parm (tree t, void* data)
4513 int level;
4514 int idx;
4515 struct template_parm_data* tpd = (struct template_parm_data*) data;
4517 template_parm_level_and_index (t, &level, &idx);
4519 if (level == tpd->level)
4521 tpd->parms[idx] = 1;
4522 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4525 /* In C++17 the type of a non-type argument is a deduced context. */
4526 if (cxx_dialect >= cxx1z
4527 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4528 for_each_template_parm (TREE_TYPE (t),
4529 &mark_template_parm,
4530 data,
4531 NULL,
4532 /*include_nondeduced_p=*/false);
4534 /* Return zero so that for_each_template_parm will continue the
4535 traversal of the tree; we want to mark *every* template parm. */
4536 return 0;
4539 /* Process the partial specialization DECL. */
4541 static tree
4542 process_partial_specialization (tree decl)
4544 tree type = TREE_TYPE (decl);
4545 tree tinfo = get_template_info (decl);
4546 tree maintmpl = TI_TEMPLATE (tinfo);
4547 tree specargs = TI_ARGS (tinfo);
4548 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4549 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4550 tree inner_parms;
4551 tree inst;
4552 int nargs = TREE_VEC_LENGTH (inner_args);
4553 int ntparms;
4554 int i;
4555 bool did_error_intro = false;
4556 struct template_parm_data tpd;
4557 struct template_parm_data tpd2;
4559 gcc_assert (current_template_parms);
4561 /* A concept cannot be specialized. */
4562 if (flag_concepts && variable_concept_p (maintmpl))
4564 error ("specialization of variable concept %q#D", maintmpl);
4565 return error_mark_node;
4568 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4569 ntparms = TREE_VEC_LENGTH (inner_parms);
4571 /* We check that each of the template parameters given in the
4572 partial specialization is used in the argument list to the
4573 specialization. For example:
4575 template <class T> struct S;
4576 template <class T> struct S<T*>;
4578 The second declaration is OK because `T*' uses the template
4579 parameter T, whereas
4581 template <class T> struct S<int>;
4583 is no good. Even trickier is:
4585 template <class T>
4586 struct S1
4588 template <class U>
4589 struct S2;
4590 template <class U>
4591 struct S2<T>;
4594 The S2<T> declaration is actually invalid; it is a
4595 full-specialization. Of course,
4597 template <class U>
4598 struct S2<T (*)(U)>;
4600 or some such would have been OK. */
4601 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4602 tpd.parms = XALLOCAVEC (int, ntparms);
4603 memset (tpd.parms, 0, sizeof (int) * ntparms);
4605 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4606 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4607 for (i = 0; i < nargs; ++i)
4609 tpd.current_arg = i;
4610 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4611 &mark_template_parm,
4612 &tpd,
4613 NULL,
4614 /*include_nondeduced_p=*/false);
4616 for (i = 0; i < ntparms; ++i)
4617 if (tpd.parms[i] == 0)
4619 /* One of the template parms was not used in a deduced context in the
4620 specialization. */
4621 if (!did_error_intro)
4623 error ("template parameters not deducible in "
4624 "partial specialization:");
4625 did_error_intro = true;
4628 inform (input_location, " %qD",
4629 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4632 if (did_error_intro)
4633 return error_mark_node;
4635 /* [temp.class.spec]
4637 The argument list of the specialization shall not be identical to
4638 the implicit argument list of the primary template. */
4639 tree main_args
4640 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4641 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4642 && (!flag_concepts
4643 || !strictly_subsumes (current_template_constraints (),
4644 get_constraints (maintmpl))))
4646 if (!flag_concepts)
4647 error ("partial specialization %q+D does not specialize "
4648 "any template arguments", decl);
4649 else
4650 error ("partial specialization %q+D does not specialize any "
4651 "template arguments and is not more constrained than", decl);
4652 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4655 /* A partial specialization that replaces multiple parameters of the
4656 primary template with a pack expansion is less specialized for those
4657 parameters. */
4658 if (nargs < DECL_NTPARMS (maintmpl))
4660 error ("partial specialization is not more specialized than the "
4661 "primary template because it replaces multiple parameters "
4662 "with a pack expansion");
4663 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4664 /* Avoid crash in process_partial_specialization. */
4665 return decl;
4668 /* If we aren't in a dependent class, we can actually try deduction. */
4669 else if (tpd.level == 1
4670 /* FIXME we should be able to handle a partial specialization of a
4671 partial instantiation, but currently we can't (c++/41727). */
4672 && TMPL_ARGS_DEPTH (specargs) == 1
4673 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4675 if (permerror (input_location, "partial specialization %qD is not "
4676 "more specialized than", decl))
4677 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4678 maintmpl);
4681 /* [temp.class.spec]
4683 A partially specialized non-type argument expression shall not
4684 involve template parameters of the partial specialization except
4685 when the argument expression is a simple identifier.
4687 The type of a template parameter corresponding to a specialized
4688 non-type argument shall not be dependent on a parameter of the
4689 specialization.
4691 Also, we verify that pack expansions only occur at the
4692 end of the argument list. */
4693 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4694 tpd2.parms = 0;
4695 for (i = 0; i < nargs; ++i)
4697 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4698 tree arg = TREE_VEC_ELT (inner_args, i);
4699 tree packed_args = NULL_TREE;
4700 int j, len = 1;
4702 if (ARGUMENT_PACK_P (arg))
4704 /* Extract the arguments from the argument pack. We'll be
4705 iterating over these in the following loop. */
4706 packed_args = ARGUMENT_PACK_ARGS (arg);
4707 len = TREE_VEC_LENGTH (packed_args);
4710 for (j = 0; j < len; j++)
4712 if (packed_args)
4713 /* Get the Jth argument in the parameter pack. */
4714 arg = TREE_VEC_ELT (packed_args, j);
4716 if (PACK_EXPANSION_P (arg))
4718 /* Pack expansions must come at the end of the
4719 argument list. */
4720 if ((packed_args && j < len - 1)
4721 || (!packed_args && i < nargs - 1))
4723 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4724 error ("parameter pack argument %qE must be at the "
4725 "end of the template argument list", arg);
4726 else
4727 error ("parameter pack argument %qT must be at the "
4728 "end of the template argument list", arg);
4732 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4733 /* We only care about the pattern. */
4734 arg = PACK_EXPANSION_PATTERN (arg);
4736 if (/* These first two lines are the `non-type' bit. */
4737 !TYPE_P (arg)
4738 && TREE_CODE (arg) != TEMPLATE_DECL
4739 /* This next two lines are the `argument expression is not just a
4740 simple identifier' condition and also the `specialized
4741 non-type argument' bit. */
4742 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4743 && !(REFERENCE_REF_P (arg)
4744 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4746 if ((!packed_args && tpd.arg_uses_template_parms[i])
4747 || (packed_args && uses_template_parms (arg)))
4748 error ("template argument %qE involves template parameter(s)",
4749 arg);
4750 else
4752 /* Look at the corresponding template parameter,
4753 marking which template parameters its type depends
4754 upon. */
4755 tree type = TREE_TYPE (parm);
4757 if (!tpd2.parms)
4759 /* We haven't yet initialized TPD2. Do so now. */
4760 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4761 /* The number of parameters here is the number in the
4762 main template, which, as checked in the assertion
4763 above, is NARGS. */
4764 tpd2.parms = XALLOCAVEC (int, nargs);
4765 tpd2.level =
4766 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4769 /* Mark the template parameters. But this time, we're
4770 looking for the template parameters of the main
4771 template, not in the specialization. */
4772 tpd2.current_arg = i;
4773 tpd2.arg_uses_template_parms[i] = 0;
4774 memset (tpd2.parms, 0, sizeof (int) * nargs);
4775 for_each_template_parm (type,
4776 &mark_template_parm,
4777 &tpd2,
4778 NULL,
4779 /*include_nondeduced_p=*/false);
4781 if (tpd2.arg_uses_template_parms [i])
4783 /* The type depended on some template parameters.
4784 If they are fully specialized in the
4785 specialization, that's OK. */
4786 int j;
4787 int count = 0;
4788 for (j = 0; j < nargs; ++j)
4789 if (tpd2.parms[j] != 0
4790 && tpd.arg_uses_template_parms [j])
4791 ++count;
4792 if (count != 0)
4793 error_n (input_location, count,
4794 "type %qT of template argument %qE depends "
4795 "on a template parameter",
4796 "type %qT of template argument %qE depends "
4797 "on template parameters",
4798 type,
4799 arg);
4806 /* We should only get here once. */
4807 if (TREE_CODE (decl) == TYPE_DECL)
4808 gcc_assert (!COMPLETE_TYPE_P (type));
4810 // Build the template decl.
4811 tree tmpl = build_template_decl (decl, current_template_parms,
4812 DECL_MEMBER_TEMPLATE_P (maintmpl));
4813 TREE_TYPE (tmpl) = type;
4814 DECL_TEMPLATE_RESULT (tmpl) = decl;
4815 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4816 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4817 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4819 /* Give template template parms a DECL_CONTEXT of the template
4820 for which they are a parameter. */
4821 for (i = 0; i < ntparms; ++i)
4823 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4824 if (TREE_CODE (parm) == TEMPLATE_DECL)
4825 DECL_CONTEXT (parm) = tmpl;
4828 if (VAR_P (decl))
4829 /* We didn't register this in check_explicit_specialization so we could
4830 wait until the constraints were set. */
4831 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4832 else
4833 associate_classtype_constraints (type);
4835 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4836 = tree_cons (specargs, tmpl,
4837 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4838 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4840 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4841 inst = TREE_CHAIN (inst))
4843 tree instance = TREE_VALUE (inst);
4844 if (TYPE_P (instance)
4845 ? (COMPLETE_TYPE_P (instance)
4846 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4847 : DECL_TEMPLATE_INSTANTIATION (instance))
4849 tree spec = most_specialized_partial_spec (instance, tf_none);
4850 tree inst_decl = (DECL_P (instance)
4851 ? instance : TYPE_NAME (instance));
4852 if (!spec)
4853 /* OK */;
4854 else if (spec == error_mark_node)
4855 permerror (input_location,
4856 "declaration of %qD ambiguates earlier template "
4857 "instantiation for %qD", decl, inst_decl);
4858 else if (TREE_VALUE (spec) == tmpl)
4859 permerror (input_location,
4860 "partial specialization of %qD after instantiation "
4861 "of %qD", decl, inst_decl);
4865 return decl;
4868 /* PARM is a template parameter of some form; return the corresponding
4869 TEMPLATE_PARM_INDEX. */
4871 static tree
4872 get_template_parm_index (tree parm)
4874 if (TREE_CODE (parm) == PARM_DECL
4875 || TREE_CODE (parm) == CONST_DECL)
4876 parm = DECL_INITIAL (parm);
4877 else if (TREE_CODE (parm) == TYPE_DECL
4878 || TREE_CODE (parm) == TEMPLATE_DECL)
4879 parm = TREE_TYPE (parm);
4880 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4881 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4882 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4883 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4884 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4885 return parm;
4888 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4889 parameter packs used by the template parameter PARM. */
4891 static void
4892 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4894 /* A type parm can't refer to another parm. */
4895 if (TREE_CODE (parm) == TYPE_DECL)
4896 return;
4897 else if (TREE_CODE (parm) == PARM_DECL)
4899 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4900 ppd, ppd->visited);
4901 return;
4904 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4906 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4907 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4908 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4911 /* PARM is a template parameter pack. Return any parameter packs used in
4912 its type or the type of any of its template parameters. If there are
4913 any such packs, it will be instantiated into a fixed template parameter
4914 list by partial instantiation rather than be fully deduced. */
4916 tree
4917 fixed_parameter_pack_p (tree parm)
4919 /* This can only be true in a member template. */
4920 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4921 return NULL_TREE;
4922 /* This can only be true for a parameter pack. */
4923 if (!template_parameter_pack_p (parm))
4924 return NULL_TREE;
4925 /* A type parm can't refer to another parm. */
4926 if (TREE_CODE (parm) == TYPE_DECL)
4927 return NULL_TREE;
4929 tree parameter_packs = NULL_TREE;
4930 struct find_parameter_pack_data ppd;
4931 ppd.parameter_packs = &parameter_packs;
4932 ppd.visited = new hash_set<tree>;
4933 ppd.type_pack_expansion_p = false;
4935 fixed_parameter_pack_p_1 (parm, &ppd);
4937 delete ppd.visited;
4938 return parameter_packs;
4941 /* Check that a template declaration's use of default arguments and
4942 parameter packs is not invalid. Here, PARMS are the template
4943 parameters. IS_PRIMARY is true if DECL is the thing declared by
4944 a primary template. IS_PARTIAL is true if DECL is a partial
4945 specialization.
4947 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4948 declaration (but not a definition); 1 indicates a declaration, 2
4949 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4950 emitted for extraneous default arguments.
4952 Returns TRUE if there were no errors found, FALSE otherwise. */
4954 bool
4955 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4956 bool is_partial, int is_friend_decl)
4958 const char *msg;
4959 int last_level_to_check;
4960 tree parm_level;
4961 bool no_errors = true;
4963 /* [temp.param]
4965 A default template-argument shall not be specified in a
4966 function template declaration or a function template definition, nor
4967 in the template-parameter-list of the definition of a member of a
4968 class template. */
4970 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4971 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4972 /* You can't have a function template declaration in a local
4973 scope, nor you can you define a member of a class template in a
4974 local scope. */
4975 return true;
4977 if ((TREE_CODE (decl) == TYPE_DECL
4978 && TREE_TYPE (decl)
4979 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4980 || (TREE_CODE (decl) == FUNCTION_DECL
4981 && LAMBDA_FUNCTION_P (decl)))
4982 /* A lambda doesn't have an explicit declaration; don't complain
4983 about the parms of the enclosing class. */
4984 return true;
4986 if (current_class_type
4987 && !TYPE_BEING_DEFINED (current_class_type)
4988 && DECL_LANG_SPECIFIC (decl)
4989 && DECL_DECLARES_FUNCTION_P (decl)
4990 /* If this is either a friend defined in the scope of the class
4991 or a member function. */
4992 && (DECL_FUNCTION_MEMBER_P (decl)
4993 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4994 : DECL_FRIEND_CONTEXT (decl)
4995 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4996 : false)
4997 /* And, if it was a member function, it really was defined in
4998 the scope of the class. */
4999 && (!DECL_FUNCTION_MEMBER_P (decl)
5000 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5001 /* We already checked these parameters when the template was
5002 declared, so there's no need to do it again now. This function
5003 was defined in class scope, but we're processing its body now
5004 that the class is complete. */
5005 return true;
5007 /* Core issue 226 (C++0x only): the following only applies to class
5008 templates. */
5009 if (is_primary
5010 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5012 /* [temp.param]
5014 If a template-parameter has a default template-argument, all
5015 subsequent template-parameters shall have a default
5016 template-argument supplied. */
5017 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5019 tree inner_parms = TREE_VALUE (parm_level);
5020 int ntparms = TREE_VEC_LENGTH (inner_parms);
5021 int seen_def_arg_p = 0;
5022 int i;
5024 for (i = 0; i < ntparms; ++i)
5026 tree parm = TREE_VEC_ELT (inner_parms, i);
5028 if (parm == error_mark_node)
5029 continue;
5031 if (TREE_PURPOSE (parm))
5032 seen_def_arg_p = 1;
5033 else if (seen_def_arg_p
5034 && !template_parameter_pack_p (TREE_VALUE (parm)))
5036 error ("no default argument for %qD", TREE_VALUE (parm));
5037 /* For better subsequent error-recovery, we indicate that
5038 there should have been a default argument. */
5039 TREE_PURPOSE (parm) = error_mark_node;
5040 no_errors = false;
5042 else if (!is_partial
5043 && !is_friend_decl
5044 /* Don't complain about an enclosing partial
5045 specialization. */
5046 && parm_level == parms
5047 && TREE_CODE (decl) == TYPE_DECL
5048 && i < ntparms - 1
5049 && template_parameter_pack_p (TREE_VALUE (parm))
5050 /* A fixed parameter pack will be partially
5051 instantiated into a fixed length list. */
5052 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5054 /* A primary class template can only have one
5055 parameter pack, at the end of the template
5056 parameter list. */
5058 error ("parameter pack %q+D must be at the end of the"
5059 " template parameter list", TREE_VALUE (parm));
5061 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5062 = error_mark_node;
5063 no_errors = false;
5069 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5070 || is_partial
5071 || !is_primary
5072 || is_friend_decl)
5073 /* For an ordinary class template, default template arguments are
5074 allowed at the innermost level, e.g.:
5075 template <class T = int>
5076 struct S {};
5077 but, in a partial specialization, they're not allowed even
5078 there, as we have in [temp.class.spec]:
5080 The template parameter list of a specialization shall not
5081 contain default template argument values.
5083 So, for a partial specialization, or for a function template
5084 (in C++98/C++03), we look at all of them. */
5086 else
5087 /* But, for a primary class template that is not a partial
5088 specialization we look at all template parameters except the
5089 innermost ones. */
5090 parms = TREE_CHAIN (parms);
5092 /* Figure out what error message to issue. */
5093 if (is_friend_decl == 2)
5094 msg = G_("default template arguments may not be used in function template "
5095 "friend re-declaration");
5096 else if (is_friend_decl)
5097 msg = G_("default template arguments may not be used in function template "
5098 "friend declarations");
5099 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5100 msg = G_("default template arguments may not be used in function templates "
5101 "without -std=c++11 or -std=gnu++11");
5102 else if (is_partial)
5103 msg = G_("default template arguments may not be used in "
5104 "partial specializations");
5105 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5106 msg = G_("default argument for template parameter for class enclosing %qD");
5107 else
5108 /* Per [temp.param]/9, "A default template-argument shall not be
5109 specified in the template-parameter-lists of the definition of
5110 a member of a class template that appears outside of the member's
5111 class.", thus if we aren't handling a member of a class template
5112 there is no need to examine the parameters. */
5113 return true;
5115 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5116 /* If we're inside a class definition, there's no need to
5117 examine the parameters to the class itself. On the one
5118 hand, they will be checked when the class is defined, and,
5119 on the other, default arguments are valid in things like:
5120 template <class T = double>
5121 struct S { template <class U> void f(U); };
5122 Here the default argument for `S' has no bearing on the
5123 declaration of `f'. */
5124 last_level_to_check = template_class_depth (current_class_type) + 1;
5125 else
5126 /* Check everything. */
5127 last_level_to_check = 0;
5129 for (parm_level = parms;
5130 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5131 parm_level = TREE_CHAIN (parm_level))
5133 tree inner_parms = TREE_VALUE (parm_level);
5134 int i;
5135 int ntparms;
5137 ntparms = TREE_VEC_LENGTH (inner_parms);
5138 for (i = 0; i < ntparms; ++i)
5140 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5141 continue;
5143 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5145 if (msg)
5147 no_errors = false;
5148 if (is_friend_decl == 2)
5149 return no_errors;
5151 error (msg, decl);
5152 msg = 0;
5155 /* Clear out the default argument so that we are not
5156 confused later. */
5157 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5161 /* At this point, if we're still interested in issuing messages,
5162 they must apply to classes surrounding the object declared. */
5163 if (msg)
5164 msg = G_("default argument for template parameter for class "
5165 "enclosing %qD");
5168 return no_errors;
5171 /* Worker for push_template_decl_real, called via
5172 for_each_template_parm. DATA is really an int, indicating the
5173 level of the parameters we are interested in. If T is a template
5174 parameter of that level, return nonzero. */
5176 static int
5177 template_parm_this_level_p (tree t, void* data)
5179 int this_level = *(int *)data;
5180 int level;
5182 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5183 level = TEMPLATE_PARM_LEVEL (t);
5184 else
5185 level = TEMPLATE_TYPE_LEVEL (t);
5186 return level == this_level;
5189 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5190 DATA is really an int, indicating the innermost outer level of parameters.
5191 If T is a template parameter of that level or further out, return
5192 nonzero. */
5194 static int
5195 template_parm_outer_level (tree t, void *data)
5197 int this_level = *(int *)data;
5198 int level;
5200 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5201 level = TEMPLATE_PARM_LEVEL (t);
5202 else
5203 level = TEMPLATE_TYPE_LEVEL (t);
5204 return level <= this_level;
5207 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5208 parameters given by current_template_args, or reuses a
5209 previously existing one, if appropriate. Returns the DECL, or an
5210 equivalent one, if it is replaced via a call to duplicate_decls.
5212 If IS_FRIEND is true, DECL is a friend declaration. */
5214 tree
5215 push_template_decl_real (tree decl, bool is_friend)
5217 tree tmpl;
5218 tree args;
5219 tree info;
5220 tree ctx;
5221 bool is_primary;
5222 bool is_partial;
5223 int new_template_p = 0;
5224 /* True if the template is a member template, in the sense of
5225 [temp.mem]. */
5226 bool member_template_p = false;
5228 if (decl == error_mark_node || !current_template_parms)
5229 return error_mark_node;
5231 /* See if this is a partial specialization. */
5232 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5233 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5234 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5235 || (VAR_P (decl)
5236 && DECL_LANG_SPECIFIC (decl)
5237 && DECL_TEMPLATE_SPECIALIZATION (decl)
5238 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5240 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5241 is_friend = true;
5243 if (is_friend)
5244 /* For a friend, we want the context of the friend function, not
5245 the type of which it is a friend. */
5246 ctx = CP_DECL_CONTEXT (decl);
5247 else if (CP_DECL_CONTEXT (decl)
5248 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5249 /* In the case of a virtual function, we want the class in which
5250 it is defined. */
5251 ctx = CP_DECL_CONTEXT (decl);
5252 else
5253 /* Otherwise, if we're currently defining some class, the DECL
5254 is assumed to be a member of the class. */
5255 ctx = current_scope ();
5257 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5258 ctx = NULL_TREE;
5260 if (!DECL_CONTEXT (decl))
5261 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5263 /* See if this is a primary template. */
5264 if (is_friend && ctx
5265 && uses_template_parms_level (ctx, processing_template_decl))
5266 /* A friend template that specifies a class context, i.e.
5267 template <typename T> friend void A<T>::f();
5268 is not primary. */
5269 is_primary = false;
5270 else if (TREE_CODE (decl) == TYPE_DECL
5271 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5272 is_primary = false;
5273 else
5274 is_primary = template_parm_scope_p ();
5276 if (is_primary)
5278 warning (OPT_Wtemplates, "template %qD declared", decl);
5280 if (DECL_CLASS_SCOPE_P (decl))
5281 member_template_p = true;
5282 if (TREE_CODE (decl) == TYPE_DECL
5283 && anon_aggrname_p (DECL_NAME (decl)))
5285 error ("template class without a name");
5286 return error_mark_node;
5288 else if (TREE_CODE (decl) == FUNCTION_DECL)
5290 if (member_template_p)
5292 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5293 error ("member template %qD may not have virt-specifiers", decl);
5295 if (DECL_DESTRUCTOR_P (decl))
5297 /* [temp.mem]
5299 A destructor shall not be a member template. */
5300 error ("destructor %qD declared as member template", decl);
5301 return error_mark_node;
5303 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5304 && (!prototype_p (TREE_TYPE (decl))
5305 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5306 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5307 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5308 == void_list_node)))
5310 /* [basic.stc.dynamic.allocation]
5312 An allocation function can be a function
5313 template. ... Template allocation functions shall
5314 have two or more parameters. */
5315 error ("invalid template declaration of %qD", decl);
5316 return error_mark_node;
5319 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5320 && CLASS_TYPE_P (TREE_TYPE (decl)))
5322 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5323 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5324 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5326 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5327 if (TREE_CODE (t) == TYPE_DECL)
5328 t = TREE_TYPE (t);
5329 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5330 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5333 else if (TREE_CODE (decl) == TYPE_DECL
5334 && TYPE_DECL_ALIAS_P (decl))
5335 /* alias-declaration */
5336 gcc_assert (!DECL_ARTIFICIAL (decl));
5337 else if (VAR_P (decl))
5338 /* C++14 variable template. */;
5339 else
5341 error ("template declaration of %q#D", decl);
5342 return error_mark_node;
5346 /* Check to see that the rules regarding the use of default
5347 arguments are not being violated. */
5348 check_default_tmpl_args (decl, current_template_parms,
5349 is_primary, is_partial, /*is_friend_decl=*/0);
5351 /* Ensure that there are no parameter packs in the type of this
5352 declaration that have not been expanded. */
5353 if (TREE_CODE (decl) == FUNCTION_DECL)
5355 /* Check each of the arguments individually to see if there are
5356 any bare parameter packs. */
5357 tree type = TREE_TYPE (decl);
5358 tree arg = DECL_ARGUMENTS (decl);
5359 tree argtype = TYPE_ARG_TYPES (type);
5361 while (arg && argtype)
5363 if (!DECL_PACK_P (arg)
5364 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5366 /* This is a PARM_DECL that contains unexpanded parameter
5367 packs. We have already complained about this in the
5368 check_for_bare_parameter_packs call, so just replace
5369 these types with ERROR_MARK_NODE. */
5370 TREE_TYPE (arg) = error_mark_node;
5371 TREE_VALUE (argtype) = error_mark_node;
5374 arg = DECL_CHAIN (arg);
5375 argtype = TREE_CHAIN (argtype);
5378 /* Check for bare parameter packs in the return type and the
5379 exception specifiers. */
5380 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5381 /* Errors were already issued, set return type to int
5382 as the frontend doesn't expect error_mark_node as
5383 the return type. */
5384 TREE_TYPE (type) = integer_type_node;
5385 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5386 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5388 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5389 && TYPE_DECL_ALIAS_P (decl))
5390 ? DECL_ORIGINAL_TYPE (decl)
5391 : TREE_TYPE (decl)))
5393 TREE_TYPE (decl) = error_mark_node;
5394 return error_mark_node;
5397 if (is_partial)
5398 return process_partial_specialization (decl);
5400 args = current_template_args ();
5402 if (!ctx
5403 || TREE_CODE (ctx) == FUNCTION_DECL
5404 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5405 || (TREE_CODE (decl) == TYPE_DECL
5406 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5407 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5409 if (DECL_LANG_SPECIFIC (decl)
5410 && DECL_TEMPLATE_INFO (decl)
5411 && DECL_TI_TEMPLATE (decl))
5412 tmpl = DECL_TI_TEMPLATE (decl);
5413 /* If DECL is a TYPE_DECL for a class-template, then there won't
5414 be DECL_LANG_SPECIFIC. The information equivalent to
5415 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5416 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5417 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5418 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5420 /* Since a template declaration already existed for this
5421 class-type, we must be redeclaring it here. Make sure
5422 that the redeclaration is valid. */
5423 redeclare_class_template (TREE_TYPE (decl),
5424 current_template_parms,
5425 current_template_constraints ());
5426 /* We don't need to create a new TEMPLATE_DECL; just use the
5427 one we already had. */
5428 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5430 else
5432 tmpl = build_template_decl (decl, current_template_parms,
5433 member_template_p);
5434 new_template_p = 1;
5436 if (DECL_LANG_SPECIFIC (decl)
5437 && DECL_TEMPLATE_SPECIALIZATION (decl))
5439 /* A specialization of a member template of a template
5440 class. */
5441 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5442 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5443 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5447 else
5449 tree a, t, current, parms;
5450 int i;
5451 tree tinfo = get_template_info (decl);
5453 if (!tinfo)
5455 error ("template definition of non-template %q#D", decl);
5456 return error_mark_node;
5459 tmpl = TI_TEMPLATE (tinfo);
5461 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5462 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5463 && DECL_TEMPLATE_SPECIALIZATION (decl)
5464 && DECL_MEMBER_TEMPLATE_P (tmpl))
5466 tree new_tmpl;
5468 /* The declaration is a specialization of a member
5469 template, declared outside the class. Therefore, the
5470 innermost template arguments will be NULL, so we
5471 replace them with the arguments determined by the
5472 earlier call to check_explicit_specialization. */
5473 args = DECL_TI_ARGS (decl);
5475 new_tmpl
5476 = build_template_decl (decl, current_template_parms,
5477 member_template_p);
5478 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5479 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5480 DECL_TI_TEMPLATE (decl) = new_tmpl;
5481 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5482 DECL_TEMPLATE_INFO (new_tmpl)
5483 = build_template_info (tmpl, args);
5485 register_specialization (new_tmpl,
5486 most_general_template (tmpl),
5487 args,
5488 is_friend, 0);
5489 return decl;
5492 /* Make sure the template headers we got make sense. */
5494 parms = DECL_TEMPLATE_PARMS (tmpl);
5495 i = TMPL_PARMS_DEPTH (parms);
5496 if (TMPL_ARGS_DEPTH (args) != i)
5498 error ("expected %d levels of template parms for %q#D, got %d",
5499 i, decl, TMPL_ARGS_DEPTH (args));
5500 DECL_INTERFACE_KNOWN (decl) = 1;
5501 return error_mark_node;
5503 else
5504 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5506 a = TMPL_ARGS_LEVEL (args, i);
5507 t = INNERMOST_TEMPLATE_PARMS (parms);
5509 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5511 if (current == decl)
5512 error ("got %d template parameters for %q#D",
5513 TREE_VEC_LENGTH (a), decl);
5514 else
5515 error ("got %d template parameters for %q#T",
5516 TREE_VEC_LENGTH (a), current);
5517 error (" but %d required", TREE_VEC_LENGTH (t));
5518 /* Avoid crash in import_export_decl. */
5519 DECL_INTERFACE_KNOWN (decl) = 1;
5520 return error_mark_node;
5523 if (current == decl)
5524 current = ctx;
5525 else if (current == NULL_TREE)
5526 /* Can happen in erroneous input. */
5527 break;
5528 else
5529 current = get_containing_scope (current);
5532 /* Check that the parms are used in the appropriate qualifying scopes
5533 in the declarator. */
5534 if (!comp_template_args
5535 (TI_ARGS (tinfo),
5536 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5538 error ("\
5539 template arguments to %qD do not match original template %qD",
5540 decl, DECL_TEMPLATE_RESULT (tmpl));
5541 if (!uses_template_parms (TI_ARGS (tinfo)))
5542 inform (input_location, "use template<> for an explicit specialization");
5543 /* Avoid crash in import_export_decl. */
5544 DECL_INTERFACE_KNOWN (decl) = 1;
5545 return error_mark_node;
5549 DECL_TEMPLATE_RESULT (tmpl) = decl;
5550 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5552 /* Push template declarations for global functions and types. Note
5553 that we do not try to push a global template friend declared in a
5554 template class; such a thing may well depend on the template
5555 parameters of the class. */
5556 if (new_template_p && !ctx
5557 && !(is_friend && template_class_depth (current_class_type) > 0))
5559 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5560 if (tmpl == error_mark_node)
5561 return error_mark_node;
5563 /* Hide template friend classes that haven't been declared yet. */
5564 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5566 DECL_ANTICIPATED (tmpl) = 1;
5567 DECL_FRIEND_P (tmpl) = 1;
5571 if (is_primary)
5573 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5574 int i;
5576 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5577 if (DECL_CONV_FN_P (tmpl))
5579 int depth = TMPL_PARMS_DEPTH (parms);
5581 /* It is a conversion operator. See if the type converted to
5582 depends on innermost template operands. */
5584 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5585 depth))
5586 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5589 /* Give template template parms a DECL_CONTEXT of the template
5590 for which they are a parameter. */
5591 parms = INNERMOST_TEMPLATE_PARMS (parms);
5592 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5594 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5595 if (TREE_CODE (parm) == TEMPLATE_DECL)
5596 DECL_CONTEXT (parm) = tmpl;
5599 if (TREE_CODE (decl) == TYPE_DECL
5600 && TYPE_DECL_ALIAS_P (decl)
5601 && complex_alias_template_p (tmpl))
5602 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5605 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5606 back to its most general template. If TMPL is a specialization,
5607 ARGS may only have the innermost set of arguments. Add the missing
5608 argument levels if necessary. */
5609 if (DECL_TEMPLATE_INFO (tmpl))
5610 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5612 info = build_template_info (tmpl, args);
5614 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5615 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5616 else
5618 if (is_primary)
5619 retrofit_lang_decl (decl);
5620 if (DECL_LANG_SPECIFIC (decl))
5621 DECL_TEMPLATE_INFO (decl) = info;
5624 if (flag_implicit_templates
5625 && !is_friend
5626 && TREE_PUBLIC (decl)
5627 && VAR_OR_FUNCTION_DECL_P (decl))
5628 /* Set DECL_COMDAT on template instantiations; if we force
5629 them to be emitted by explicit instantiation or -frepo,
5630 mark_needed will tell cgraph to do the right thing. */
5631 DECL_COMDAT (decl) = true;
5633 return DECL_TEMPLATE_RESULT (tmpl);
5636 tree
5637 push_template_decl (tree decl)
5639 return push_template_decl_real (decl, false);
5642 /* FN is an inheriting constructor that inherits from the constructor
5643 template INHERITED; turn FN into a constructor template with a matching
5644 template header. */
5646 tree
5647 add_inherited_template_parms (tree fn, tree inherited)
5649 tree inner_parms
5650 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5651 inner_parms = copy_node (inner_parms);
5652 tree parms
5653 = tree_cons (size_int (processing_template_decl + 1),
5654 inner_parms, current_template_parms);
5655 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5656 tree args = template_parms_to_args (parms);
5657 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5658 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5659 DECL_TEMPLATE_RESULT (tmpl) = fn;
5660 DECL_ARTIFICIAL (tmpl) = true;
5661 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5662 return tmpl;
5665 /* Called when a class template TYPE is redeclared with the indicated
5666 template PARMS, e.g.:
5668 template <class T> struct S;
5669 template <class T> struct S {}; */
5671 bool
5672 redeclare_class_template (tree type, tree parms, tree cons)
5674 tree tmpl;
5675 tree tmpl_parms;
5676 int i;
5678 if (!TYPE_TEMPLATE_INFO (type))
5680 error ("%qT is not a template type", type);
5681 return false;
5684 tmpl = TYPE_TI_TEMPLATE (type);
5685 if (!PRIMARY_TEMPLATE_P (tmpl))
5686 /* The type is nested in some template class. Nothing to worry
5687 about here; there are no new template parameters for the nested
5688 type. */
5689 return true;
5691 if (!parms)
5693 error ("template specifiers not specified in declaration of %qD",
5694 tmpl);
5695 return false;
5698 parms = INNERMOST_TEMPLATE_PARMS (parms);
5699 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5701 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5703 error_n (input_location, TREE_VEC_LENGTH (parms),
5704 "redeclared with %d template parameter",
5705 "redeclared with %d template parameters",
5706 TREE_VEC_LENGTH (parms));
5707 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5708 "previous declaration %qD used %d template parameter",
5709 "previous declaration %qD used %d template parameters",
5710 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5711 return false;
5714 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5716 tree tmpl_parm;
5717 tree parm;
5718 tree tmpl_default;
5719 tree parm_default;
5721 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5722 || TREE_VEC_ELT (parms, i) == error_mark_node)
5723 continue;
5725 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5726 if (error_operand_p (tmpl_parm))
5727 return false;
5729 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5730 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5731 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5733 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5734 TEMPLATE_DECL. */
5735 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5736 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5737 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5738 || (TREE_CODE (tmpl_parm) != PARM_DECL
5739 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5740 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5741 || (TREE_CODE (tmpl_parm) == PARM_DECL
5742 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5743 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5745 error ("template parameter %q+#D", tmpl_parm);
5746 error ("redeclared here as %q#D", parm);
5747 return false;
5750 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5752 /* We have in [temp.param]:
5754 A template-parameter may not be given default arguments
5755 by two different declarations in the same scope. */
5756 error_at (input_location, "redefinition of default argument for %q#D", parm);
5757 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5758 "original definition appeared here");
5759 return false;
5762 if (parm_default != NULL_TREE)
5763 /* Update the previous template parameters (which are the ones
5764 that will really count) with the new default value. */
5765 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5766 else if (tmpl_default != NULL_TREE)
5767 /* Update the new parameters, too; they'll be used as the
5768 parameters for any members. */
5769 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5771 /* Give each template template parm in this redeclaration a
5772 DECL_CONTEXT of the template for which they are a parameter. */
5773 if (TREE_CODE (parm) == TEMPLATE_DECL)
5775 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5776 DECL_CONTEXT (parm) = tmpl;
5779 if (TREE_CODE (parm) == TYPE_DECL)
5780 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5783 // Cannot redeclare a class template with a different set of constraints.
5784 if (!equivalent_constraints (get_constraints (tmpl), cons))
5786 error_at (input_location, "redeclaration %q#D with different "
5787 "constraints", tmpl);
5788 inform (DECL_SOURCE_LOCATION (tmpl),
5789 "original declaration appeared here");
5792 return true;
5795 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5796 to be used when the caller has already checked
5797 (processing_template_decl
5798 && !instantiation_dependent_expression_p (expr)
5799 && potential_constant_expression (expr))
5800 and cleared processing_template_decl. */
5802 tree
5803 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5805 return tsubst_copy_and_build (expr,
5806 /*args=*/NULL_TREE,
5807 complain,
5808 /*in_decl=*/NULL_TREE,
5809 /*function_p=*/false,
5810 /*integral_constant_expression_p=*/true);
5813 /* Simplify EXPR if it is a non-dependent expression. Returns the
5814 (possibly simplified) expression. */
5816 tree
5817 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5819 if (expr == NULL_TREE)
5820 return NULL_TREE;
5822 /* If we're in a template, but EXPR isn't value dependent, simplify
5823 it. We're supposed to treat:
5825 template <typename T> void f(T[1 + 1]);
5826 template <typename T> void f(T[2]);
5828 as two declarations of the same function, for example. */
5829 if (processing_template_decl
5830 && potential_nondependent_constant_expression (expr))
5832 processing_template_decl_sentinel s;
5833 expr = instantiate_non_dependent_expr_internal (expr, complain);
5835 return expr;
5838 tree
5839 instantiate_non_dependent_expr (tree expr)
5841 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5844 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5845 an uninstantiated expression. */
5847 tree
5848 instantiate_non_dependent_or_null (tree expr)
5850 if (expr == NULL_TREE)
5851 return NULL_TREE;
5852 if (processing_template_decl)
5854 if (!potential_nondependent_constant_expression (expr))
5855 expr = NULL_TREE;
5856 else
5858 processing_template_decl_sentinel s;
5859 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5862 return expr;
5865 /* True iff T is a specialization of a variable template. */
5867 bool
5868 variable_template_specialization_p (tree t)
5870 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5871 return false;
5872 tree tmpl = DECL_TI_TEMPLATE (t);
5873 return variable_template_p (tmpl);
5876 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5877 template declaration, or a TYPE_DECL for an alias declaration. */
5879 bool
5880 alias_type_or_template_p (tree t)
5882 if (t == NULL_TREE)
5883 return false;
5884 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5885 || (TYPE_P (t)
5886 && TYPE_NAME (t)
5887 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5888 || DECL_ALIAS_TEMPLATE_P (t));
5891 /* Return TRUE iff T is a specialization of an alias template. */
5893 bool
5894 alias_template_specialization_p (const_tree t)
5896 /* It's an alias template specialization if it's an alias and its
5897 TYPE_NAME is a specialization of a primary template. */
5898 if (TYPE_ALIAS_P (t))
5899 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5900 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5902 return false;
5905 /* An alias template is complex from a SFINAE perspective if a template-id
5906 using that alias can be ill-formed when the expansion is not, as with
5907 the void_t template. We determine this by checking whether the
5908 expansion for the alias template uses all its template parameters. */
5910 struct uses_all_template_parms_data
5912 int level;
5913 bool *seen;
5916 static int
5917 uses_all_template_parms_r (tree t, void *data_)
5919 struct uses_all_template_parms_data &data
5920 = *(struct uses_all_template_parms_data*)data_;
5921 tree idx = get_template_parm_index (t);
5923 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5924 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5925 return 0;
5928 static bool
5929 complex_alias_template_p (const_tree tmpl)
5931 struct uses_all_template_parms_data data;
5932 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5933 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5934 data.level = TMPL_PARMS_DEPTH (parms);
5935 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5936 data.seen = XALLOCAVEC (bool, len);
5937 for (int i = 0; i < len; ++i)
5938 data.seen[i] = false;
5940 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5941 for (int i = 0; i < len; ++i)
5942 if (!data.seen[i])
5943 return true;
5944 return false;
5947 /* Return TRUE iff T is a specialization of a complex alias template with
5948 dependent template-arguments. */
5950 bool
5951 dependent_alias_template_spec_p (const_tree t)
5953 if (!alias_template_specialization_p (t))
5954 return false;
5956 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5957 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5958 return false;
5960 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5961 if (!any_dependent_template_arguments_p (args))
5962 return false;
5964 return true;
5967 /* Return the number of innermost template parameters in TMPL. */
5969 static int
5970 num_innermost_template_parms (tree tmpl)
5972 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5973 return TREE_VEC_LENGTH (parms);
5976 /* Return either TMPL or another template that it is equivalent to under DR
5977 1286: An alias that just changes the name of a template is equivalent to
5978 the other template. */
5980 static tree
5981 get_underlying_template (tree tmpl)
5983 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5984 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5986 /* Determine if the alias is equivalent to an underlying template. */
5987 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5988 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5989 if (!tinfo)
5990 break;
5992 tree underlying = TI_TEMPLATE (tinfo);
5993 if (!PRIMARY_TEMPLATE_P (underlying)
5994 || (num_innermost_template_parms (tmpl)
5995 != num_innermost_template_parms (underlying)))
5996 break;
5998 tree alias_args = INNERMOST_TEMPLATE_ARGS
5999 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6000 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6001 break;
6003 /* Alias is equivalent. Strip it and repeat. */
6004 tmpl = underlying;
6007 return tmpl;
6010 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6011 must be a reference-to-function or a pointer-to-function type, as specified
6012 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6013 and check that the resulting function has external linkage. */
6015 static tree
6016 convert_nontype_argument_function (tree type, tree expr,
6017 tsubst_flags_t complain)
6019 tree fns = expr;
6020 tree fn, fn_no_ptr;
6021 linkage_kind linkage;
6023 fn = instantiate_type (type, fns, tf_none);
6024 if (fn == error_mark_node)
6025 return error_mark_node;
6027 if (value_dependent_expression_p (fn))
6028 goto accept;
6030 fn_no_ptr = strip_fnptr_conv (fn);
6031 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6032 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6033 if (BASELINK_P (fn_no_ptr))
6034 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6036 /* [temp.arg.nontype]/1
6038 A template-argument for a non-type, non-template template-parameter
6039 shall be one of:
6040 [...]
6041 -- the address of an object or function with external [C++11: or
6042 internal] linkage. */
6044 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6046 if (complain & tf_error)
6048 error ("%qE is not a valid template argument for type %qT",
6049 expr, type);
6050 if (TYPE_PTR_P (type))
6051 inform (input_location, "it must be the address of a function "
6052 "with external linkage");
6053 else
6054 inform (input_location, "it must be the name of a function with "
6055 "external linkage");
6057 return NULL_TREE;
6060 linkage = decl_linkage (fn_no_ptr);
6061 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6063 if (complain & tf_error)
6065 if (cxx_dialect >= cxx11)
6066 error ("%qE is not a valid template argument for type %qT "
6067 "because %qD has no linkage",
6068 expr, type, fn_no_ptr);
6069 else
6070 error ("%qE is not a valid template argument for type %qT "
6071 "because %qD does not have external linkage",
6072 expr, type, fn_no_ptr);
6074 return NULL_TREE;
6077 accept:
6078 if (TREE_CODE (type) == REFERENCE_TYPE)
6079 fn = build_address (fn);
6080 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6081 fn = build_nop (type, fn);
6083 return fn;
6086 /* Subroutine of convert_nontype_argument.
6087 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6088 Emit an error otherwise. */
6090 static bool
6091 check_valid_ptrmem_cst_expr (tree type, tree expr,
6092 tsubst_flags_t complain)
6094 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6095 tree orig_expr = expr;
6096 STRIP_NOPS (expr);
6097 if (null_ptr_cst_p (expr))
6098 return true;
6099 if (TREE_CODE (expr) == PTRMEM_CST
6100 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6101 PTRMEM_CST_CLASS (expr)))
6102 return true;
6103 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6104 return true;
6105 if (processing_template_decl
6106 && TREE_CODE (expr) == ADDR_EXPR
6107 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6108 return true;
6109 if (complain & tf_error)
6111 error_at (loc, "%qE is not a valid template argument for type %qT",
6112 orig_expr, type);
6113 if (TREE_CODE (expr) != PTRMEM_CST)
6114 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6115 else
6116 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6118 return false;
6121 /* Returns TRUE iff the address of OP is value-dependent.
6123 14.6.2.4 [temp.dep.temp]:
6124 A non-integral non-type template-argument is dependent if its type is
6125 dependent or it has either of the following forms
6126 qualified-id
6127 & qualified-id
6128 and contains a nested-name-specifier which specifies a class-name that
6129 names a dependent type.
6131 We generalize this to just say that the address of a member of a
6132 dependent class is value-dependent; the above doesn't cover the
6133 address of a static data member named with an unqualified-id. */
6135 static bool
6136 has_value_dependent_address (tree op)
6138 /* We could use get_inner_reference here, but there's no need;
6139 this is only relevant for template non-type arguments, which
6140 can only be expressed as &id-expression. */
6141 if (DECL_P (op))
6143 tree ctx = CP_DECL_CONTEXT (op);
6144 if (TYPE_P (ctx) && dependent_type_p (ctx))
6145 return true;
6148 return false;
6151 /* The next set of functions are used for providing helpful explanatory
6152 diagnostics for failed overload resolution. Their messages should be
6153 indented by two spaces for consistency with the messages in
6154 call.c */
6156 static int
6157 unify_success (bool /*explain_p*/)
6159 return 0;
6162 /* Other failure functions should call this one, to provide a single function
6163 for setting a breakpoint on. */
6165 static int
6166 unify_invalid (bool /*explain_p*/)
6168 return 1;
6171 static int
6172 unify_parameter_deduction_failure (bool explain_p, tree parm)
6174 if (explain_p)
6175 inform (input_location,
6176 " couldn't deduce template parameter %qD", parm);
6177 return unify_invalid (explain_p);
6180 static int
6181 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6183 if (explain_p)
6184 inform (input_location,
6185 " types %qT and %qT have incompatible cv-qualifiers",
6186 parm, arg);
6187 return unify_invalid (explain_p);
6190 static int
6191 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6193 if (explain_p)
6194 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6195 return unify_invalid (explain_p);
6198 static int
6199 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6201 if (explain_p)
6202 inform (input_location,
6203 " template parameter %qD is not a parameter pack, but "
6204 "argument %qD is",
6205 parm, arg);
6206 return unify_invalid (explain_p);
6209 static int
6210 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6212 if (explain_p)
6213 inform (input_location,
6214 " template argument %qE does not match "
6215 "pointer-to-member constant %qE",
6216 arg, parm);
6217 return unify_invalid (explain_p);
6220 static int
6221 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6223 if (explain_p)
6224 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6225 return unify_invalid (explain_p);
6228 static int
6229 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6231 if (explain_p)
6232 inform (input_location,
6233 " inconsistent parameter pack deduction with %qT and %qT",
6234 old_arg, new_arg);
6235 return unify_invalid (explain_p);
6238 static int
6239 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6241 if (explain_p)
6243 if (TYPE_P (parm))
6244 inform (input_location,
6245 " deduced conflicting types for parameter %qT (%qT and %qT)",
6246 parm, first, second);
6247 else
6248 inform (input_location,
6249 " deduced conflicting values for non-type parameter "
6250 "%qE (%qE and %qE)", parm, first, second);
6252 return unify_invalid (explain_p);
6255 static int
6256 unify_vla_arg (bool explain_p, tree arg)
6258 if (explain_p)
6259 inform (input_location,
6260 " variable-sized array type %qT is not "
6261 "a valid template argument",
6262 arg);
6263 return unify_invalid (explain_p);
6266 static int
6267 unify_method_type_error (bool explain_p, tree arg)
6269 if (explain_p)
6270 inform (input_location,
6271 " member function type %qT is not a valid template argument",
6272 arg);
6273 return unify_invalid (explain_p);
6276 static int
6277 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6279 if (explain_p)
6281 if (least_p)
6282 inform_n (input_location, wanted,
6283 " candidate expects at least %d argument, %d provided",
6284 " candidate expects at least %d arguments, %d provided",
6285 wanted, have);
6286 else
6287 inform_n (input_location, wanted,
6288 " candidate expects %d argument, %d provided",
6289 " candidate expects %d arguments, %d provided",
6290 wanted, have);
6292 return unify_invalid (explain_p);
6295 static int
6296 unify_too_many_arguments (bool explain_p, int have, int wanted)
6298 return unify_arity (explain_p, have, wanted);
6301 static int
6302 unify_too_few_arguments (bool explain_p, int have, int wanted,
6303 bool least_p = false)
6305 return unify_arity (explain_p, have, wanted, least_p);
6308 static int
6309 unify_arg_conversion (bool explain_p, tree to_type,
6310 tree from_type, tree arg)
6312 if (explain_p)
6313 inform (EXPR_LOC_OR_LOC (arg, input_location),
6314 " cannot convert %qE (type %qT) to type %qT",
6315 arg, from_type, to_type);
6316 return unify_invalid (explain_p);
6319 static int
6320 unify_no_common_base (bool explain_p, enum template_base_result r,
6321 tree parm, tree arg)
6323 if (explain_p)
6324 switch (r)
6326 case tbr_ambiguous_baseclass:
6327 inform (input_location, " %qT is an ambiguous base class of %qT",
6328 parm, arg);
6329 break;
6330 default:
6331 inform (input_location, " %qT is not derived from %qT", arg, parm);
6332 break;
6334 return unify_invalid (explain_p);
6337 static int
6338 unify_inconsistent_template_template_parameters (bool explain_p)
6340 if (explain_p)
6341 inform (input_location,
6342 " template parameters of a template template argument are "
6343 "inconsistent with other deduced template arguments");
6344 return unify_invalid (explain_p);
6347 static int
6348 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6350 if (explain_p)
6351 inform (input_location,
6352 " can't deduce a template for %qT from non-template type %qT",
6353 parm, arg);
6354 return unify_invalid (explain_p);
6357 static int
6358 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6360 if (explain_p)
6361 inform (input_location,
6362 " template argument %qE does not match %qE", arg, parm);
6363 return unify_invalid (explain_p);
6366 static int
6367 unify_overload_resolution_failure (bool explain_p, tree arg)
6369 if (explain_p)
6370 inform (input_location,
6371 " could not resolve address from overloaded function %qE",
6372 arg);
6373 return unify_invalid (explain_p);
6376 /* Attempt to convert the non-type template parameter EXPR to the
6377 indicated TYPE. If the conversion is successful, return the
6378 converted value. If the conversion is unsuccessful, return
6379 NULL_TREE if we issued an error message, or error_mark_node if we
6380 did not. We issue error messages for out-and-out bad template
6381 parameters, but not simply because the conversion failed, since we
6382 might be just trying to do argument deduction. Both TYPE and EXPR
6383 must be non-dependent.
6385 The conversion follows the special rules described in
6386 [temp.arg.nontype], and it is much more strict than an implicit
6387 conversion.
6389 This function is called twice for each template argument (see
6390 lookup_template_class for a more accurate description of this
6391 problem). This means that we need to handle expressions which
6392 are not valid in a C++ source, but can be created from the
6393 first call (for instance, casts to perform conversions). These
6394 hacks can go away after we fix the double coercion problem. */
6396 static tree
6397 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6399 tree expr_type;
6400 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6401 tree orig_expr = expr;
6403 /* Detect immediately string literals as invalid non-type argument.
6404 This special-case is not needed for correctness (we would easily
6405 catch this later), but only to provide better diagnostic for this
6406 common user mistake. As suggested by DR 100, we do not mention
6407 linkage issues in the diagnostic as this is not the point. */
6408 /* FIXME we're making this OK. */
6409 if (TREE_CODE (expr) == STRING_CST)
6411 if (complain & tf_error)
6412 error ("%qE is not a valid template argument for type %qT "
6413 "because string literals can never be used in this context",
6414 expr, type);
6415 return NULL_TREE;
6418 /* Add the ADDR_EXPR now for the benefit of
6419 value_dependent_expression_p. */
6420 if (TYPE_PTROBV_P (type)
6421 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6423 expr = decay_conversion (expr, complain);
6424 if (expr == error_mark_node)
6425 return error_mark_node;
6428 /* If we are in a template, EXPR may be non-dependent, but still
6429 have a syntactic, rather than semantic, form. For example, EXPR
6430 might be a SCOPE_REF, rather than the VAR_DECL to which the
6431 SCOPE_REF refers. Preserving the qualifying scope is necessary
6432 so that access checking can be performed when the template is
6433 instantiated -- but here we need the resolved form so that we can
6434 convert the argument. */
6435 bool non_dep = false;
6436 if (TYPE_REF_OBJ_P (type)
6437 && has_value_dependent_address (expr))
6438 /* If we want the address and it's value-dependent, don't fold. */;
6439 else if (processing_template_decl
6440 && potential_nondependent_constant_expression (expr))
6441 non_dep = true;
6442 if (error_operand_p (expr))
6443 return error_mark_node;
6444 expr_type = TREE_TYPE (expr);
6445 if (TREE_CODE (type) == REFERENCE_TYPE)
6446 expr = mark_lvalue_use (expr);
6447 else
6448 expr = mark_rvalue_use (expr);
6450 /* If the argument is non-dependent, perform any conversions in
6451 non-dependent context as well. */
6452 processing_template_decl_sentinel s (non_dep);
6453 if (non_dep)
6454 expr = instantiate_non_dependent_expr_internal (expr, complain);
6456 if (value_dependent_expression_p (expr))
6457 expr = canonicalize_expr_argument (expr, complain);
6459 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6460 to a non-type argument of "nullptr". */
6461 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6462 expr = fold_simple (convert (type, expr));
6464 /* In C++11, integral or enumeration non-type template arguments can be
6465 arbitrary constant expressions. Pointer and pointer to
6466 member arguments can be general constant expressions that evaluate
6467 to a null value, but otherwise still need to be of a specific form. */
6468 if (cxx_dialect >= cxx11)
6470 if (TREE_CODE (expr) == PTRMEM_CST)
6471 /* A PTRMEM_CST is already constant, and a valid template
6472 argument for a parameter of pointer to member type, we just want
6473 to leave it in that form rather than lower it to a
6474 CONSTRUCTOR. */;
6475 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6476 || cxx_dialect >= cxx1z)
6478 /* C++17: A template-argument for a non-type template-parameter shall
6479 be a converted constant expression (8.20) of the type of the
6480 template-parameter. */
6481 expr = build_converted_constant_expr (type, expr, complain);
6482 if (expr == error_mark_node)
6483 return error_mark_node;
6484 expr = maybe_constant_value (expr);
6485 expr = convert_from_reference (expr);
6487 else if (TYPE_PTR_OR_PTRMEM_P (type))
6489 tree folded = maybe_constant_value (expr);
6490 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6491 : null_member_pointer_value_p (folded))
6492 expr = folded;
6496 /* HACK: Due to double coercion, we can get a
6497 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6498 which is the tree that we built on the first call (see
6499 below when coercing to reference to object or to reference to
6500 function). We just strip everything and get to the arg.
6501 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6502 for examples. */
6503 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6505 tree probe_type, probe = expr;
6506 if (REFERENCE_REF_P (probe))
6507 probe = TREE_OPERAND (probe, 0);
6508 probe_type = TREE_TYPE (probe);
6509 if (TREE_CODE (probe) == NOP_EXPR)
6511 /* ??? Maybe we could use convert_from_reference here, but we
6512 would need to relax its constraints because the NOP_EXPR
6513 could actually change the type to something more cv-qualified,
6514 and this is not folded by convert_from_reference. */
6515 tree addr = TREE_OPERAND (probe, 0);
6516 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6517 && TREE_CODE (addr) == ADDR_EXPR
6518 && TYPE_PTR_P (TREE_TYPE (addr))
6519 && (same_type_ignoring_top_level_qualifiers_p
6520 (TREE_TYPE (probe_type),
6521 TREE_TYPE (TREE_TYPE (addr)))))
6523 expr = TREE_OPERAND (addr, 0);
6524 expr_type = TREE_TYPE (probe_type);
6529 /* [temp.arg.nontype]/5, bullet 1
6531 For a non-type template-parameter of integral or enumeration type,
6532 integral promotions (_conv.prom_) and integral conversions
6533 (_conv.integral_) are applied. */
6534 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6536 if (cxx_dialect < cxx11)
6538 tree t = build_converted_constant_expr (type, expr, complain);
6539 t = maybe_constant_value (t);
6540 if (t != error_mark_node)
6541 expr = t;
6544 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6545 return error_mark_node;
6547 /* Notice that there are constant expressions like '4 % 0' which
6548 do not fold into integer constants. */
6549 if (TREE_CODE (expr) != INTEGER_CST
6550 && !value_dependent_expression_p (expr))
6552 if (complain & tf_error)
6554 int errs = errorcount, warns = warningcount + werrorcount;
6555 if (!require_potential_constant_expression (expr))
6556 expr = error_mark_node;
6557 else
6558 expr = cxx_constant_value (expr);
6559 if (errorcount > errs || warningcount + werrorcount > warns)
6560 inform (loc, "in template argument for type %qT ", type);
6561 if (expr == error_mark_node)
6562 return NULL_TREE;
6563 /* else cxx_constant_value complained but gave us
6564 a real constant, so go ahead. */
6565 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6567 else
6568 return NULL_TREE;
6571 /* Avoid typedef problems. */
6572 if (TREE_TYPE (expr) != type)
6573 expr = fold_convert (type, expr);
6575 /* [temp.arg.nontype]/5, bullet 2
6577 For a non-type template-parameter of type pointer to object,
6578 qualification conversions (_conv.qual_) and the array-to-pointer
6579 conversion (_conv.array_) are applied. */
6580 else if (TYPE_PTROBV_P (type))
6582 tree decayed = expr;
6584 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6585 decay_conversion or an explicit cast. If it's a problematic cast,
6586 we'll complain about it below. */
6587 if (TREE_CODE (expr) == NOP_EXPR)
6589 tree probe = expr;
6590 STRIP_NOPS (probe);
6591 if (TREE_CODE (probe) == ADDR_EXPR
6592 && TYPE_PTR_P (TREE_TYPE (probe)))
6594 expr = probe;
6595 expr_type = TREE_TYPE (expr);
6599 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6601 A template-argument for a non-type, non-template template-parameter
6602 shall be one of: [...]
6604 -- the name of a non-type template-parameter;
6605 -- the address of an object or function with external linkage, [...]
6606 expressed as "& id-expression" where the & is optional if the name
6607 refers to a function or array, or if the corresponding
6608 template-parameter is a reference.
6610 Here, we do not care about functions, as they are invalid anyway
6611 for a parameter of type pointer-to-object. */
6613 if (value_dependent_expression_p (expr))
6614 /* Non-type template parameters are OK. */
6616 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6617 /* Null pointer values are OK in C++11. */;
6618 else if (TREE_CODE (expr) != ADDR_EXPR)
6620 if (VAR_P (expr))
6622 if (complain & tf_error)
6623 error ("%qD is not a valid template argument "
6624 "because %qD is a variable, not the address of "
6625 "a variable", orig_expr, expr);
6626 return NULL_TREE;
6628 if (POINTER_TYPE_P (expr_type))
6630 if (complain & tf_error)
6631 error ("%qE is not a valid template argument for %qT "
6632 "because it is not the address of a variable",
6633 orig_expr, type);
6634 return NULL_TREE;
6636 /* Other values, like integer constants, might be valid
6637 non-type arguments of some other type. */
6638 return error_mark_node;
6640 else
6642 tree decl = TREE_OPERAND (expr, 0);
6644 if (!VAR_P (decl))
6646 if (complain & tf_error)
6647 error ("%qE is not a valid template argument of type %qT "
6648 "because %qE is not a variable", orig_expr, type, decl);
6649 return NULL_TREE;
6651 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6653 if (complain & tf_error)
6654 error ("%qE is not a valid template argument of type %qT "
6655 "because %qD does not have external linkage",
6656 orig_expr, type, decl);
6657 return NULL_TREE;
6659 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6660 && decl_linkage (decl) == lk_none)
6662 if (complain & tf_error)
6663 error ("%qE is not a valid template argument of type %qT "
6664 "because %qD has no linkage", orig_expr, type, decl);
6665 return NULL_TREE;
6667 /* C++17: For a non-type template-parameter of reference or pointer
6668 type, the value of the constant expression shall not refer to (or
6669 for a pointer type, shall not be the address of):
6670 * a subobject (4.5),
6671 * a temporary object (15.2),
6672 * a string literal (5.13.5),
6673 * the result of a typeid expression (8.2.8), or
6674 * a predefined __func__ variable (11.4.1). */
6675 else if (DECL_ARTIFICIAL (decl))
6677 if (complain & tf_error)
6678 error ("the address of %qD is not a valid template argument",
6679 decl);
6680 return NULL_TREE;
6682 else if (!same_type_ignoring_top_level_qualifiers_p
6683 (strip_array_types (TREE_TYPE (type)),
6684 strip_array_types (TREE_TYPE (decl))))
6686 if (complain & tf_error)
6687 error ("the address of the %qT subobject of %qD is not a "
6688 "valid template argument", TREE_TYPE (type), decl);
6689 return NULL_TREE;
6691 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6693 if (complain & tf_error)
6694 error ("the address of %qD is not a valid template argument "
6695 "because it does not have static storage duration",
6696 decl);
6697 return NULL_TREE;
6701 expr = decayed;
6703 expr = perform_qualification_conversions (type, expr);
6704 if (expr == error_mark_node)
6705 return error_mark_node;
6707 /* [temp.arg.nontype]/5, bullet 3
6709 For a non-type template-parameter of type reference to object, no
6710 conversions apply. The type referred to by the reference may be more
6711 cv-qualified than the (otherwise identical) type of the
6712 template-argument. The template-parameter is bound directly to the
6713 template-argument, which must be an lvalue. */
6714 else if (TYPE_REF_OBJ_P (type))
6716 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6717 expr_type))
6718 return error_mark_node;
6720 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6722 if (complain & tf_error)
6723 error ("%qE is not a valid template argument for type %qT "
6724 "because of conflicts in cv-qualification", expr, type);
6725 return NULL_TREE;
6728 if (!lvalue_p (expr))
6730 if (complain & tf_error)
6731 error ("%qE is not a valid template argument for type %qT "
6732 "because it is not an lvalue", expr, type);
6733 return NULL_TREE;
6736 /* [temp.arg.nontype]/1
6738 A template-argument for a non-type, non-template template-parameter
6739 shall be one of: [...]
6741 -- the address of an object or function with external linkage. */
6742 if (INDIRECT_REF_P (expr)
6743 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6745 expr = TREE_OPERAND (expr, 0);
6746 if (DECL_P (expr))
6748 if (complain & tf_error)
6749 error ("%q#D is not a valid template argument for type %qT "
6750 "because a reference variable does not have a constant "
6751 "address", expr, type);
6752 return NULL_TREE;
6756 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6757 && value_dependent_expression_p (expr))
6758 /* OK, dependent reference. We don't want to ask whether a DECL is
6759 itself value-dependent, since what we want here is its address. */;
6760 else
6762 if (!DECL_P (expr))
6764 if (complain & tf_error)
6765 error ("%qE is not a valid template argument for type %qT "
6766 "because it is not an object with linkage",
6767 expr, type);
6768 return NULL_TREE;
6771 /* DR 1155 allows internal linkage in C++11 and up. */
6772 linkage_kind linkage = decl_linkage (expr);
6773 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6775 if (complain & tf_error)
6776 error ("%qE is not a valid template argument for type %qT "
6777 "because object %qD does not have linkage",
6778 expr, type, expr);
6779 return NULL_TREE;
6782 expr = build_address (expr);
6785 if (!same_type_p (type, TREE_TYPE (expr)))
6786 expr = build_nop (type, expr);
6788 /* [temp.arg.nontype]/5, bullet 4
6790 For a non-type template-parameter of type pointer to function, only
6791 the function-to-pointer conversion (_conv.func_) is applied. If the
6792 template-argument represents a set of overloaded functions (or a
6793 pointer to such), the matching function is selected from the set
6794 (_over.over_). */
6795 else if (TYPE_PTRFN_P (type))
6797 /* If the argument is a template-id, we might not have enough
6798 context information to decay the pointer. */
6799 if (!type_unknown_p (expr_type))
6801 expr = decay_conversion (expr, complain);
6802 if (expr == error_mark_node)
6803 return error_mark_node;
6806 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6807 /* Null pointer values are OK in C++11. */
6808 return perform_qualification_conversions (type, expr);
6810 expr = convert_nontype_argument_function (type, expr, complain);
6811 if (!expr || expr == error_mark_node)
6812 return expr;
6814 /* [temp.arg.nontype]/5, bullet 5
6816 For a non-type template-parameter of type reference to function, no
6817 conversions apply. If the template-argument represents a set of
6818 overloaded functions, the matching function is selected from the set
6819 (_over.over_). */
6820 else if (TYPE_REFFN_P (type))
6822 if (TREE_CODE (expr) == ADDR_EXPR)
6824 if (complain & tf_error)
6826 error ("%qE is not a valid template argument for type %qT "
6827 "because it is a pointer", expr, type);
6828 inform (input_location, "try using %qE instead",
6829 TREE_OPERAND (expr, 0));
6831 return NULL_TREE;
6834 expr = convert_nontype_argument_function (type, expr, complain);
6835 if (!expr || expr == error_mark_node)
6836 return expr;
6838 /* [temp.arg.nontype]/5, bullet 6
6840 For a non-type template-parameter of type pointer to member function,
6841 no conversions apply. If the template-argument represents a set of
6842 overloaded member functions, the matching member function is selected
6843 from the set (_over.over_). */
6844 else if (TYPE_PTRMEMFUNC_P (type))
6846 expr = instantiate_type (type, expr, tf_none);
6847 if (expr == error_mark_node)
6848 return error_mark_node;
6850 /* [temp.arg.nontype] bullet 1 says the pointer to member
6851 expression must be a pointer-to-member constant. */
6852 if (!value_dependent_expression_p (expr)
6853 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6854 return NULL_TREE;
6856 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6857 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6858 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6859 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6861 /* [temp.arg.nontype]/5, bullet 7
6863 For a non-type template-parameter of type pointer to data member,
6864 qualification conversions (_conv.qual_) are applied. */
6865 else if (TYPE_PTRDATAMEM_P (type))
6867 /* [temp.arg.nontype] bullet 1 says the pointer to member
6868 expression must be a pointer-to-member constant. */
6869 if (!value_dependent_expression_p (expr)
6870 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6871 return NULL_TREE;
6873 expr = perform_qualification_conversions (type, expr);
6874 if (expr == error_mark_node)
6875 return expr;
6877 else if (NULLPTR_TYPE_P (type))
6879 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6881 if (complain & tf_error)
6882 error ("%qE is not a valid template argument for type %qT "
6883 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6884 return NULL_TREE;
6886 return expr;
6888 /* A template non-type parameter must be one of the above. */
6889 else
6890 gcc_unreachable ();
6892 /* Sanity check: did we actually convert the argument to the
6893 right type? */
6894 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6895 (type, TREE_TYPE (expr)));
6896 return convert_from_reference (expr);
6899 /* Subroutine of coerce_template_template_parms, which returns 1 if
6900 PARM_PARM and ARG_PARM match using the rule for the template
6901 parameters of template template parameters. Both PARM and ARG are
6902 template parameters; the rest of the arguments are the same as for
6903 coerce_template_template_parms.
6905 static int
6906 coerce_template_template_parm (tree parm,
6907 tree arg,
6908 tsubst_flags_t complain,
6909 tree in_decl,
6910 tree outer_args)
6912 if (arg == NULL_TREE || error_operand_p (arg)
6913 || parm == NULL_TREE || error_operand_p (parm))
6914 return 0;
6916 if (TREE_CODE (arg) != TREE_CODE (parm))
6917 return 0;
6919 switch (TREE_CODE (parm))
6921 case TEMPLATE_DECL:
6922 /* We encounter instantiations of templates like
6923 template <template <template <class> class> class TT>
6924 class C; */
6926 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6927 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6929 if (!coerce_template_template_parms
6930 (parmparm, argparm, complain, in_decl, outer_args))
6931 return 0;
6933 /* Fall through. */
6935 case TYPE_DECL:
6936 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6937 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6938 /* Argument is a parameter pack but parameter is not. */
6939 return 0;
6940 break;
6942 case PARM_DECL:
6943 /* The tsubst call is used to handle cases such as
6945 template <int> class C {};
6946 template <class T, template <T> class TT> class D {};
6947 D<int, C> d;
6949 i.e. the parameter list of TT depends on earlier parameters. */
6950 if (!uses_template_parms (TREE_TYPE (arg)))
6952 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6953 if (!uses_template_parms (t)
6954 && !same_type_p (t, TREE_TYPE (arg)))
6955 return 0;
6958 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6959 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6960 /* Argument is a parameter pack but parameter is not. */
6961 return 0;
6963 break;
6965 default:
6966 gcc_unreachable ();
6969 return 1;
6972 /* Coerce template argument list ARGLIST for use with template
6973 template-parameter TEMPL. */
6975 static tree
6976 coerce_template_args_for_ttp (tree templ, tree arglist,
6977 tsubst_flags_t complain)
6979 /* Consider an example where a template template parameter declared as
6981 template <class T, class U = std::allocator<T> > class TT
6983 The template parameter level of T and U are one level larger than
6984 of TT. To proper process the default argument of U, say when an
6985 instantiation `TT<int>' is seen, we need to build the full
6986 arguments containing {int} as the innermost level. Outer levels,
6987 available when not appearing as default template argument, can be
6988 obtained from the arguments of the enclosing template.
6990 Suppose that TT is later substituted with std::vector. The above
6991 instantiation is `TT<int, std::allocator<T> >' with TT at
6992 level 1, and T at level 2, while the template arguments at level 1
6993 becomes {std::vector} and the inner level 2 is {int}. */
6995 tree outer = DECL_CONTEXT (templ);
6996 if (outer)
6998 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6999 /* We want arguments for the partial specialization, not arguments for
7000 the primary template. */
7001 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7002 else
7003 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7005 else if (current_template_parms)
7007 /* This is an argument of the current template, so we haven't set
7008 DECL_CONTEXT yet. */
7009 tree relevant_template_parms;
7011 /* Parameter levels that are greater than the level of the given
7012 template template parm are irrelevant. */
7013 relevant_template_parms = current_template_parms;
7014 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7015 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7016 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7018 outer = template_parms_to_args (relevant_template_parms);
7021 if (outer)
7022 arglist = add_to_template_args (outer, arglist);
7024 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7025 return coerce_template_parms (parmlist, arglist, templ,
7026 complain,
7027 /*require_all_args=*/true,
7028 /*use_default_args=*/true);
7031 /* A cache of template template parameters with match-all default
7032 arguments. */
7033 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7034 static void
7035 store_defaulted_ttp (tree v, tree t)
7037 if (!defaulted_ttp_cache)
7038 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7039 defaulted_ttp_cache->put (v, t);
7041 static tree
7042 lookup_defaulted_ttp (tree v)
7044 if (defaulted_ttp_cache)
7045 if (tree *p = defaulted_ttp_cache->get (v))
7046 return *p;
7047 return NULL_TREE;
7050 /* T is a bound template template-parameter. Copy its arguments into default
7051 arguments of the template template-parameter's template parameters. */
7053 static tree
7054 add_defaults_to_ttp (tree otmpl)
7056 if (tree c = lookup_defaulted_ttp (otmpl))
7057 return c;
7059 tree ntmpl = copy_node (otmpl);
7061 tree ntype = copy_node (TREE_TYPE (otmpl));
7062 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7063 TYPE_MAIN_VARIANT (ntype) = ntype;
7064 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7065 TYPE_NAME (ntype) = ntmpl;
7066 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7068 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7069 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7070 TEMPLATE_PARM_DECL (idx) = ntmpl;
7071 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7073 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7074 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7075 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7076 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7077 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7079 tree o = TREE_VEC_ELT (vec, i);
7080 if (!template_parameter_pack_p (TREE_VALUE (o)))
7082 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7083 TREE_PURPOSE (n) = any_targ_node;
7087 store_defaulted_ttp (otmpl, ntmpl);
7088 return ntmpl;
7091 /* ARG is a bound potential template template-argument, and PARGS is a list
7092 of arguments for the corresponding template template-parameter. Adjust
7093 PARGS as appropriate for application to ARG's template, and if ARG is a
7094 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7095 arguments to the template template parameter. */
7097 static tree
7098 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7100 ++processing_template_decl;
7101 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7104 /* When comparing two template template-parameters in partial ordering,
7105 rewrite the one currently being used as an argument to have default
7106 arguments for all parameters. */
7107 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7108 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7109 if (pargs != error_mark_node)
7110 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7111 TYPE_TI_ARGS (arg));
7113 else
7115 tree aparms
7116 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7117 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7118 /*require_all*/true,
7119 /*use_default*/true);
7121 --processing_template_decl;
7122 return pargs;
7125 /* Subroutine of unify for the case when PARM is a
7126 BOUND_TEMPLATE_TEMPLATE_PARM. */
7128 static int
7129 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7130 bool explain_p)
7132 tree parmvec = TYPE_TI_ARGS (parm);
7133 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7135 /* The template template parm might be variadic and the argument
7136 not, so flatten both argument lists. */
7137 parmvec = expand_template_argument_pack (parmvec);
7138 argvec = expand_template_argument_pack (argvec);
7140 if (flag_new_ttp)
7142 /* In keeping with P0522R0, adjust P's template arguments
7143 to apply to A's template; then flatten it again. */
7144 tree nparmvec = parmvec;
7145 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7146 nparmvec = expand_template_argument_pack (nparmvec);
7148 if (unify (tparms, targs, nparmvec, argvec,
7149 UNIFY_ALLOW_NONE, explain_p))
7150 return 1;
7152 /* If the P0522 adjustment eliminated a pack expansion, deduce
7153 empty packs. */
7154 if (flag_new_ttp
7155 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7156 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7157 DEDUCE_EXACT, /*sub*/true, explain_p))
7158 return 1;
7160 else
7162 /* Deduce arguments T, i from TT<T> or TT<i>.
7163 We check each element of PARMVEC and ARGVEC individually
7164 rather than the whole TREE_VEC since they can have
7165 different number of elements, which is allowed under N2555. */
7167 int len = TREE_VEC_LENGTH (parmvec);
7169 /* Check if the parameters end in a pack, making them
7170 variadic. */
7171 int parm_variadic_p = 0;
7172 if (len > 0
7173 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7174 parm_variadic_p = 1;
7176 for (int i = 0; i < len - parm_variadic_p; ++i)
7177 /* If the template argument list of P contains a pack
7178 expansion that is not the last template argument, the
7179 entire template argument list is a non-deduced
7180 context. */
7181 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7182 return unify_success (explain_p);
7184 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7185 return unify_too_few_arguments (explain_p,
7186 TREE_VEC_LENGTH (argvec), len);
7188 for (int i = 0; i < len - parm_variadic_p; ++i)
7189 if (unify (tparms, targs,
7190 TREE_VEC_ELT (parmvec, i),
7191 TREE_VEC_ELT (argvec, i),
7192 UNIFY_ALLOW_NONE, explain_p))
7193 return 1;
7195 if (parm_variadic_p
7196 && unify_pack_expansion (tparms, targs,
7197 parmvec, argvec,
7198 DEDUCE_EXACT,
7199 /*subr=*/true, explain_p))
7200 return 1;
7203 return 0;
7206 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7207 template template parameters. Both PARM_PARMS and ARG_PARMS are
7208 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7209 or PARM_DECL.
7211 Consider the example:
7212 template <class T> class A;
7213 template<template <class U> class TT> class B;
7215 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7216 the parameters to A, and OUTER_ARGS contains A. */
7218 static int
7219 coerce_template_template_parms (tree parm_parms,
7220 tree arg_parms,
7221 tsubst_flags_t complain,
7222 tree in_decl,
7223 tree outer_args)
7225 int nparms, nargs, i;
7226 tree parm, arg;
7227 int variadic_p = 0;
7229 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7230 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7232 nparms = TREE_VEC_LENGTH (parm_parms);
7233 nargs = TREE_VEC_LENGTH (arg_parms);
7235 if (flag_new_ttp)
7237 /* P0522R0: A template template-parameter P is at least as specialized as
7238 a template template-argument A if, given the following rewrite to two
7239 function templates, the function template corresponding to P is at
7240 least as specialized as the function template corresponding to A
7241 according to the partial ordering rules for function templates
7242 ([temp.func.order]). Given an invented class template X with the
7243 template parameter list of A (including default arguments):
7245 * Each of the two function templates has the same template parameters,
7246 respectively, as P or A.
7248 * Each function template has a single function parameter whose type is
7249 a specialization of X with template arguments corresponding to the
7250 template parameters from the respective function template where, for
7251 each template parameter PP in the template parameter list of the
7252 function template, a corresponding template argument AA is formed. If
7253 PP declares a parameter pack, then AA is the pack expansion
7254 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7256 If the rewrite produces an invalid type, then P is not at least as
7257 specialized as A. */
7259 /* So coerce P's args to apply to A's parms, and then deduce between A's
7260 args and the converted args. If that succeeds, A is at least as
7261 specialized as P, so they match.*/
7262 tree pargs = template_parms_level_to_args (parm_parms);
7263 ++processing_template_decl;
7264 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7265 /*require_all*/true, /*use_default*/true);
7266 --processing_template_decl;
7267 if (pargs != error_mark_node)
7269 tree targs = make_tree_vec (nargs);
7270 tree aargs = template_parms_level_to_args (arg_parms);
7271 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7272 /*explain*/false))
7273 return 1;
7277 /* Determine whether we have a parameter pack at the end of the
7278 template template parameter's template parameter list. */
7279 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7281 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7283 if (error_operand_p (parm))
7284 return 0;
7286 switch (TREE_CODE (parm))
7288 case TEMPLATE_DECL:
7289 case TYPE_DECL:
7290 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7291 variadic_p = 1;
7292 break;
7294 case PARM_DECL:
7295 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7296 variadic_p = 1;
7297 break;
7299 default:
7300 gcc_unreachable ();
7304 if (nargs != nparms
7305 && !(variadic_p && nargs >= nparms - 1))
7306 return 0;
7308 /* Check all of the template parameters except the parameter pack at
7309 the end (if any). */
7310 for (i = 0; i < nparms - variadic_p; ++i)
7312 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7313 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7314 continue;
7316 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7317 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7319 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7320 outer_args))
7321 return 0;
7325 if (variadic_p)
7327 /* Check each of the template parameters in the template
7328 argument against the template parameter pack at the end of
7329 the template template parameter. */
7330 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7331 return 0;
7333 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7335 for (; i < nargs; ++i)
7337 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7338 continue;
7340 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7342 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7343 outer_args))
7344 return 0;
7348 return 1;
7351 /* Verifies that the deduced template arguments (in TARGS) for the
7352 template template parameters (in TPARMS) represent valid bindings,
7353 by comparing the template parameter list of each template argument
7354 to the template parameter list of its corresponding template
7355 template parameter, in accordance with DR150. This
7356 routine can only be called after all template arguments have been
7357 deduced. It will return TRUE if all of the template template
7358 parameter bindings are okay, FALSE otherwise. */
7359 bool
7360 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7362 int i, ntparms = TREE_VEC_LENGTH (tparms);
7363 bool ret = true;
7365 /* We're dealing with template parms in this process. */
7366 ++processing_template_decl;
7368 targs = INNERMOST_TEMPLATE_ARGS (targs);
7370 for (i = 0; i < ntparms; ++i)
7372 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7373 tree targ = TREE_VEC_ELT (targs, i);
7375 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7377 tree packed_args = NULL_TREE;
7378 int idx, len = 1;
7380 if (ARGUMENT_PACK_P (targ))
7382 /* Look inside the argument pack. */
7383 packed_args = ARGUMENT_PACK_ARGS (targ);
7384 len = TREE_VEC_LENGTH (packed_args);
7387 for (idx = 0; idx < len; ++idx)
7389 tree targ_parms = NULL_TREE;
7391 if (packed_args)
7392 /* Extract the next argument from the argument
7393 pack. */
7394 targ = TREE_VEC_ELT (packed_args, idx);
7396 if (PACK_EXPANSION_P (targ))
7397 /* Look at the pattern of the pack expansion. */
7398 targ = PACK_EXPANSION_PATTERN (targ);
7400 /* Extract the template parameters from the template
7401 argument. */
7402 if (TREE_CODE (targ) == TEMPLATE_DECL)
7403 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7404 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7405 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7407 /* Verify that we can coerce the template template
7408 parameters from the template argument to the template
7409 parameter. This requires an exact match. */
7410 if (targ_parms
7411 && !coerce_template_template_parms
7412 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7413 targ_parms,
7414 tf_none,
7415 tparm,
7416 targs))
7418 ret = false;
7419 goto out;
7425 out:
7427 --processing_template_decl;
7428 return ret;
7431 /* Since type attributes aren't mangled, we need to strip them from
7432 template type arguments. */
7434 static tree
7435 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7437 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7438 return arg;
7439 bool removed_attributes = false;
7440 tree canon = strip_typedefs (arg, &removed_attributes);
7441 if (removed_attributes
7442 && (complain & tf_warning))
7443 warning (OPT_Wignored_attributes,
7444 "ignoring attributes on template argument %qT", arg);
7445 return canon;
7448 /* And from inside dependent non-type arguments like sizeof(Type). */
7450 static tree
7451 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7453 if (!arg || arg == error_mark_node)
7454 return arg;
7455 bool removed_attributes = false;
7456 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7457 if (removed_attributes
7458 && (complain & tf_warning))
7459 warning (OPT_Wignored_attributes,
7460 "ignoring attributes in template argument %qE", arg);
7461 return canon;
7464 // A template declaration can be substituted for a constrained
7465 // template template parameter only when the argument is more
7466 // constrained than the parameter.
7467 static bool
7468 is_compatible_template_arg (tree parm, tree arg)
7470 tree parm_cons = get_constraints (parm);
7472 /* For now, allow constrained template template arguments
7473 and unconstrained template template parameters. */
7474 if (parm_cons == NULL_TREE)
7475 return true;
7477 tree arg_cons = get_constraints (arg);
7479 // If the template parameter is constrained, we need to rewrite its
7480 // constraints in terms of the ARG's template parameters. This ensures
7481 // that all of the template parameter types will have the same depth.
7483 // Note that this is only valid when coerce_template_template_parm is
7484 // true for the innermost template parameters of PARM and ARG. In other
7485 // words, because coercion is successful, this conversion will be valid.
7486 if (parm_cons)
7488 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7489 parm_cons = tsubst_constraint_info (parm_cons,
7490 INNERMOST_TEMPLATE_ARGS (args),
7491 tf_none, NULL_TREE);
7492 if (parm_cons == error_mark_node)
7493 return false;
7496 return subsumes (parm_cons, arg_cons);
7499 // Convert a placeholder argument into a binding to the original
7500 // parameter. The original parameter is saved as the TREE_TYPE of
7501 // ARG.
7502 static inline tree
7503 convert_wildcard_argument (tree parm, tree arg)
7505 TREE_TYPE (arg) = parm;
7506 return arg;
7509 /* Convert the indicated template ARG as necessary to match the
7510 indicated template PARM. Returns the converted ARG, or
7511 error_mark_node if the conversion was unsuccessful. Error and
7512 warning messages are issued under control of COMPLAIN. This
7513 conversion is for the Ith parameter in the parameter list. ARGS is
7514 the full set of template arguments deduced so far. */
7516 static tree
7517 convert_template_argument (tree parm,
7518 tree arg,
7519 tree args,
7520 tsubst_flags_t complain,
7521 int i,
7522 tree in_decl)
7524 tree orig_arg;
7525 tree val;
7526 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7528 if (parm == error_mark_node)
7529 return error_mark_node;
7531 /* Trivially convert placeholders. */
7532 if (TREE_CODE (arg) == WILDCARD_DECL)
7533 return convert_wildcard_argument (parm, arg);
7535 if (arg == any_targ_node)
7536 return arg;
7538 if (TREE_CODE (arg) == TREE_LIST
7539 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7541 /* The template argument was the name of some
7542 member function. That's usually
7543 invalid, but static members are OK. In any
7544 case, grab the underlying fields/functions
7545 and issue an error later if required. */
7546 orig_arg = TREE_VALUE (arg);
7547 TREE_TYPE (arg) = unknown_type_node;
7550 orig_arg = arg;
7552 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7553 requires_type = (TREE_CODE (parm) == TYPE_DECL
7554 || requires_tmpl_type);
7556 /* When determining whether an argument pack expansion is a template,
7557 look at the pattern. */
7558 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7559 arg = PACK_EXPANSION_PATTERN (arg);
7561 /* Deal with an injected-class-name used as a template template arg. */
7562 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7564 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7565 if (TREE_CODE (t) == TEMPLATE_DECL)
7567 if (cxx_dialect >= cxx11)
7568 /* OK under DR 1004. */;
7569 else if (complain & tf_warning_or_error)
7570 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7571 " used as template template argument", TYPE_NAME (arg));
7572 else if (flag_pedantic_errors)
7573 t = arg;
7575 arg = t;
7579 is_tmpl_type =
7580 ((TREE_CODE (arg) == TEMPLATE_DECL
7581 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7582 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7583 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7584 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7586 if (is_tmpl_type
7587 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7588 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7589 arg = TYPE_STUB_DECL (arg);
7591 is_type = TYPE_P (arg) || is_tmpl_type;
7593 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7594 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7596 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7598 if (complain & tf_error)
7599 error ("invalid use of destructor %qE as a type", orig_arg);
7600 return error_mark_node;
7603 permerror (input_location,
7604 "to refer to a type member of a template parameter, "
7605 "use %<typename %E%>", orig_arg);
7607 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7608 TREE_OPERAND (arg, 1),
7609 typename_type,
7610 complain);
7611 arg = orig_arg;
7612 is_type = 1;
7614 if (is_type != requires_type)
7616 if (in_decl)
7618 if (complain & tf_error)
7620 error ("type/value mismatch at argument %d in template "
7621 "parameter list for %qD",
7622 i + 1, in_decl);
7623 if (is_type)
7624 inform (input_location,
7625 " expected a constant of type %qT, got %qT",
7626 TREE_TYPE (parm),
7627 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7628 else if (requires_tmpl_type)
7629 inform (input_location,
7630 " expected a class template, got %qE", orig_arg);
7631 else
7632 inform (input_location,
7633 " expected a type, got %qE", orig_arg);
7636 return error_mark_node;
7638 if (is_tmpl_type ^ requires_tmpl_type)
7640 if (in_decl && (complain & tf_error))
7642 error ("type/value mismatch at argument %d in template "
7643 "parameter list for %qD",
7644 i + 1, in_decl);
7645 if (is_tmpl_type)
7646 inform (input_location,
7647 " expected a type, got %qT", DECL_NAME (arg));
7648 else
7649 inform (input_location,
7650 " expected a class template, got %qT", orig_arg);
7652 return error_mark_node;
7655 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7656 /* We already did the appropriate conversion when packing args. */
7657 val = orig_arg;
7658 else if (is_type)
7660 if (requires_tmpl_type)
7662 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7663 /* The number of argument required is not known yet.
7664 Just accept it for now. */
7665 val = orig_arg;
7666 else
7668 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7669 tree argparm;
7671 /* Strip alias templates that are equivalent to another
7672 template. */
7673 arg = get_underlying_template (arg);
7674 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7676 if (coerce_template_template_parms (parmparm, argparm,
7677 complain, in_decl,
7678 args))
7680 val = arg;
7682 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7683 TEMPLATE_DECL. */
7684 if (val != error_mark_node)
7686 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7687 val = TREE_TYPE (val);
7688 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7689 val = make_pack_expansion (val);
7692 else
7694 if (in_decl && (complain & tf_error))
7696 error ("type/value mismatch at argument %d in "
7697 "template parameter list for %qD",
7698 i + 1, in_decl);
7699 inform (input_location,
7700 " expected a template of type %qD, got %qT",
7701 parm, orig_arg);
7704 val = error_mark_node;
7707 // Check that the constraints are compatible before allowing the
7708 // substitution.
7709 if (val != error_mark_node)
7710 if (!is_compatible_template_arg (parm, arg))
7712 if (in_decl && (complain & tf_error))
7714 error ("constraint mismatch at argument %d in "
7715 "template parameter list for %qD",
7716 i + 1, in_decl);
7717 inform (input_location, " expected %qD but got %qD",
7718 parm, arg);
7720 val = error_mark_node;
7724 else
7725 val = orig_arg;
7726 /* We only form one instance of each template specialization.
7727 Therefore, if we use a non-canonical variant (i.e., a
7728 typedef), any future messages referring to the type will use
7729 the typedef, which is confusing if those future uses do not
7730 themselves also use the typedef. */
7731 if (TYPE_P (val))
7732 val = canonicalize_type_argument (val, complain);
7734 else
7736 tree t = TREE_TYPE (parm);
7738 if (tree a = type_uses_auto (t))
7740 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7741 if (t == error_mark_node)
7742 return error_mark_node;
7744 else
7745 t = tsubst (t, args, complain, in_decl);
7747 if (invalid_nontype_parm_type_p (t, complain))
7748 return error_mark_node;
7750 if (!type_dependent_expression_p (orig_arg)
7751 && !uses_template_parms (t))
7752 /* We used to call digest_init here. However, digest_init
7753 will report errors, which we don't want when complain
7754 is zero. More importantly, digest_init will try too
7755 hard to convert things: for example, `0' should not be
7756 converted to pointer type at this point according to
7757 the standard. Accepting this is not merely an
7758 extension, since deciding whether or not these
7759 conversions can occur is part of determining which
7760 function template to call, or whether a given explicit
7761 argument specification is valid. */
7762 val = convert_nontype_argument (t, orig_arg, complain);
7763 else
7764 val = canonicalize_expr_argument (orig_arg, complain);
7766 if (val == NULL_TREE)
7767 val = error_mark_node;
7768 else if (val == error_mark_node && (complain & tf_error))
7769 error ("could not convert template argument %qE from %qT to %qT",
7770 orig_arg, TREE_TYPE (orig_arg), t);
7772 if (INDIRECT_REF_P (val))
7774 /* Reject template arguments that are references to built-in
7775 functions with no library fallbacks. */
7776 const_tree inner = TREE_OPERAND (val, 0);
7777 const_tree innertype = TREE_TYPE (inner);
7778 if (innertype
7779 && TREE_CODE (innertype) == REFERENCE_TYPE
7780 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7781 && 0 < TREE_OPERAND_LENGTH (inner)
7782 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7783 return error_mark_node;
7786 if (TREE_CODE (val) == SCOPE_REF)
7788 /* Strip typedefs from the SCOPE_REF. */
7789 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7790 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7791 complain);
7792 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7793 QUALIFIED_NAME_IS_TEMPLATE (val));
7797 return val;
7800 /* Coerces the remaining template arguments in INNER_ARGS (from
7801 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7802 Returns the coerced argument pack. PARM_IDX is the position of this
7803 parameter in the template parameter list. ARGS is the original
7804 template argument list. */
7805 static tree
7806 coerce_template_parameter_pack (tree parms,
7807 int parm_idx,
7808 tree args,
7809 tree inner_args,
7810 int arg_idx,
7811 tree new_args,
7812 int* lost,
7813 tree in_decl,
7814 tsubst_flags_t complain)
7816 tree parm = TREE_VEC_ELT (parms, parm_idx);
7817 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7818 tree packed_args;
7819 tree argument_pack;
7820 tree packed_parms = NULL_TREE;
7822 if (arg_idx > nargs)
7823 arg_idx = nargs;
7825 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7827 /* When the template parameter is a non-type template parameter pack
7828 or template template parameter pack whose type or template
7829 parameters use parameter packs, we know exactly how many arguments
7830 we are looking for. Build a vector of the instantiated decls for
7831 these template parameters in PACKED_PARMS. */
7832 /* We can't use make_pack_expansion here because it would interpret a
7833 _DECL as a use rather than a declaration. */
7834 tree decl = TREE_VALUE (parm);
7835 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7836 SET_PACK_EXPANSION_PATTERN (exp, decl);
7837 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7838 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7840 TREE_VEC_LENGTH (args)--;
7841 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7842 TREE_VEC_LENGTH (args)++;
7844 if (packed_parms == error_mark_node)
7845 return error_mark_node;
7847 /* If we're doing a partial instantiation of a member template,
7848 verify that all of the types used for the non-type
7849 template parameter pack are, in fact, valid for non-type
7850 template parameters. */
7851 if (arg_idx < nargs
7852 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7854 int j, len = TREE_VEC_LENGTH (packed_parms);
7855 for (j = 0; j < len; ++j)
7857 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7858 if (invalid_nontype_parm_type_p (t, complain))
7859 return error_mark_node;
7861 /* We don't know how many args we have yet, just
7862 use the unconverted ones for now. */
7863 return NULL_TREE;
7866 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7868 /* Check if we have a placeholder pack, which indicates we're
7869 in the context of a introduction list. In that case we want
7870 to match this pack to the single placeholder. */
7871 else if (arg_idx < nargs
7872 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7873 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7875 nargs = arg_idx + 1;
7876 packed_args = make_tree_vec (1);
7878 else
7879 packed_args = make_tree_vec (nargs - arg_idx);
7881 /* Convert the remaining arguments, which will be a part of the
7882 parameter pack "parm". */
7883 int first_pack_arg = arg_idx;
7884 for (; arg_idx < nargs; ++arg_idx)
7886 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7887 tree actual_parm = TREE_VALUE (parm);
7888 int pack_idx = arg_idx - first_pack_arg;
7890 if (packed_parms)
7892 /* Once we've packed as many args as we have types, stop. */
7893 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7894 break;
7895 else if (PACK_EXPANSION_P (arg))
7896 /* We don't know how many args we have yet, just
7897 use the unconverted ones for now. */
7898 return NULL_TREE;
7899 else
7900 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7903 if (arg == error_mark_node)
7905 if (complain & tf_error)
7906 error ("template argument %d is invalid", arg_idx + 1);
7908 else
7909 arg = convert_template_argument (actual_parm,
7910 arg, new_args, complain, parm_idx,
7911 in_decl);
7912 if (arg == error_mark_node)
7913 (*lost)++;
7914 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7917 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7918 && TREE_VEC_LENGTH (packed_args) > 0)
7920 if (complain & tf_error)
7921 error ("wrong number of template arguments (%d, should be %d)",
7922 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7923 return error_mark_node;
7926 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7927 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7928 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7929 else
7931 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7932 TREE_CONSTANT (argument_pack) = 1;
7935 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7936 if (CHECKING_P)
7937 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7938 TREE_VEC_LENGTH (packed_args));
7939 return argument_pack;
7942 /* Returns the number of pack expansions in the template argument vector
7943 ARGS. */
7945 static int
7946 pack_expansion_args_count (tree args)
7948 int i;
7949 int count = 0;
7950 if (args)
7951 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7953 tree elt = TREE_VEC_ELT (args, i);
7954 if (elt && PACK_EXPANSION_P (elt))
7955 ++count;
7957 return count;
7960 /* Convert all template arguments to their appropriate types, and
7961 return a vector containing the innermost resulting template
7962 arguments. If any error occurs, return error_mark_node. Error and
7963 warning messages are issued under control of COMPLAIN.
7965 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7966 for arguments not specified in ARGS. Otherwise, if
7967 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7968 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7969 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7970 ARGS. */
7972 static tree
7973 coerce_template_parms (tree parms,
7974 tree args,
7975 tree in_decl,
7976 tsubst_flags_t complain,
7977 bool require_all_args,
7978 bool use_default_args)
7980 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7981 tree orig_inner_args;
7982 tree inner_args;
7983 tree new_args;
7984 tree new_inner_args;
7985 int saved_unevaluated_operand;
7986 int saved_inhibit_evaluation_warnings;
7988 /* When used as a boolean value, indicates whether this is a
7989 variadic template parameter list. Since it's an int, we can also
7990 subtract it from nparms to get the number of non-variadic
7991 parameters. */
7992 int variadic_p = 0;
7993 int variadic_args_p = 0;
7994 int post_variadic_parms = 0;
7996 /* Likewise for parameters with default arguments. */
7997 int default_p = 0;
7999 if (args == error_mark_node)
8000 return error_mark_node;
8002 nparms = TREE_VEC_LENGTH (parms);
8004 /* Determine if there are any parameter packs or default arguments. */
8005 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8007 tree parm = TREE_VEC_ELT (parms, parm_idx);
8008 if (variadic_p)
8009 ++post_variadic_parms;
8010 if (template_parameter_pack_p (TREE_VALUE (parm)))
8011 ++variadic_p;
8012 if (TREE_PURPOSE (parm))
8013 ++default_p;
8016 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8017 /* If there are no parameters that follow a parameter pack, we need to
8018 expand any argument packs so that we can deduce a parameter pack from
8019 some non-packed args followed by an argument pack, as in variadic85.C.
8020 If there are such parameters, we need to leave argument packs intact
8021 so the arguments are assigned properly. This can happen when dealing
8022 with a nested class inside a partial specialization of a class
8023 template, as in variadic92.C, or when deducing a template parameter pack
8024 from a sub-declarator, as in variadic114.C. */
8025 if (!post_variadic_parms)
8026 inner_args = expand_template_argument_pack (inner_args);
8028 /* Count any pack expansion args. */
8029 variadic_args_p = pack_expansion_args_count (inner_args);
8031 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8032 if ((nargs - variadic_args_p > nparms && !variadic_p)
8033 || (nargs < nparms - variadic_p
8034 && require_all_args
8035 && !variadic_args_p
8036 && (!use_default_args
8037 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8038 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8040 if (complain & tf_error)
8042 if (variadic_p || default_p)
8044 nparms -= variadic_p + default_p;
8045 error ("wrong number of template arguments "
8046 "(%d, should be at least %d)", nargs, nparms);
8048 else
8049 error ("wrong number of template arguments "
8050 "(%d, should be %d)", nargs, nparms);
8052 if (in_decl)
8053 inform (DECL_SOURCE_LOCATION (in_decl),
8054 "provided for %qD", in_decl);
8057 return error_mark_node;
8059 /* We can't pass a pack expansion to a non-pack parameter of an alias
8060 template (DR 1430). */
8061 else if (in_decl
8062 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8063 || concept_template_p (in_decl))
8064 && variadic_args_p
8065 && nargs - variadic_args_p < nparms - variadic_p)
8067 if (complain & tf_error)
8069 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8071 tree arg = TREE_VEC_ELT (inner_args, i);
8072 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8074 if (PACK_EXPANSION_P (arg)
8075 && !template_parameter_pack_p (parm))
8077 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8078 error_at (location_of (arg),
8079 "pack expansion argument for non-pack parameter "
8080 "%qD of alias template %qD", parm, in_decl);
8081 else
8082 error_at (location_of (arg),
8083 "pack expansion argument for non-pack parameter "
8084 "%qD of concept %qD", parm, in_decl);
8085 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8086 goto found;
8089 gcc_unreachable ();
8090 found:;
8092 return error_mark_node;
8095 /* We need to evaluate the template arguments, even though this
8096 template-id may be nested within a "sizeof". */
8097 saved_unevaluated_operand = cp_unevaluated_operand;
8098 cp_unevaluated_operand = 0;
8099 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8100 c_inhibit_evaluation_warnings = 0;
8101 new_inner_args = make_tree_vec (nparms);
8102 new_args = add_outermost_template_args (args, new_inner_args);
8103 int pack_adjust = 0;
8104 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8106 tree arg;
8107 tree parm;
8109 /* Get the Ith template parameter. */
8110 parm = TREE_VEC_ELT (parms, parm_idx);
8112 if (parm == error_mark_node)
8114 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8115 continue;
8118 /* Calculate the next argument. */
8119 if (arg_idx < nargs)
8120 arg = TREE_VEC_ELT (inner_args, arg_idx);
8121 else
8122 arg = NULL_TREE;
8124 if (template_parameter_pack_p (TREE_VALUE (parm))
8125 && !(arg && ARGUMENT_PACK_P (arg)))
8127 /* Some arguments will be placed in the
8128 template parameter pack PARM. */
8129 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8130 inner_args, arg_idx,
8131 new_args, &lost,
8132 in_decl, complain);
8134 if (arg == NULL_TREE)
8136 /* We don't know how many args we have yet, just use the
8137 unconverted (and still packed) ones for now. */
8138 new_inner_args = orig_inner_args;
8139 arg_idx = nargs;
8140 break;
8143 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8145 /* Store this argument. */
8146 if (arg == error_mark_node)
8148 lost++;
8149 /* We are done with all of the arguments. */
8150 arg_idx = nargs;
8152 else
8154 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8155 arg_idx += pack_adjust;
8158 continue;
8160 else if (arg)
8162 if (PACK_EXPANSION_P (arg))
8164 /* "If every valid specialization of a variadic template
8165 requires an empty template parameter pack, the template is
8166 ill-formed, no diagnostic required." So check that the
8167 pattern works with this parameter. */
8168 tree pattern = PACK_EXPANSION_PATTERN (arg);
8169 tree conv = convert_template_argument (TREE_VALUE (parm),
8170 pattern, new_args,
8171 complain, parm_idx,
8172 in_decl);
8173 if (conv == error_mark_node)
8175 if (complain & tf_error)
8176 inform (input_location, "so any instantiation with a "
8177 "non-empty parameter pack would be ill-formed");
8178 ++lost;
8180 else if (TYPE_P (conv) && !TYPE_P (pattern))
8181 /* Recover from missing typename. */
8182 TREE_VEC_ELT (inner_args, arg_idx)
8183 = make_pack_expansion (conv);
8185 /* We don't know how many args we have yet, just
8186 use the unconverted ones for now. */
8187 new_inner_args = inner_args;
8188 arg_idx = nargs;
8189 break;
8192 else if (require_all_args)
8194 /* There must be a default arg in this case. */
8195 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8196 complain, in_decl);
8197 /* The position of the first default template argument,
8198 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8199 Record that. */
8200 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8201 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8202 arg_idx - pack_adjust);
8204 else
8205 break;
8207 if (arg == error_mark_node)
8209 if (complain & tf_error)
8210 error ("template argument %d is invalid", arg_idx + 1);
8212 else if (!arg)
8213 /* This only occurs if there was an error in the template
8214 parameter list itself (which we would already have
8215 reported) that we are trying to recover from, e.g., a class
8216 template with a parameter list such as
8217 template<typename..., typename>. */
8218 ++lost;
8219 else
8220 arg = convert_template_argument (TREE_VALUE (parm),
8221 arg, new_args, complain,
8222 parm_idx, in_decl);
8224 if (arg == error_mark_node)
8225 lost++;
8226 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8228 cp_unevaluated_operand = saved_unevaluated_operand;
8229 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8231 if (variadic_p && arg_idx < nargs)
8233 if (complain & tf_error)
8235 error ("wrong number of template arguments "
8236 "(%d, should be %d)", nargs, arg_idx);
8237 if (in_decl)
8238 error ("provided for %q+D", in_decl);
8240 return error_mark_node;
8243 if (lost)
8244 return error_mark_node;
8246 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8247 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8248 TREE_VEC_LENGTH (new_inner_args));
8250 return new_inner_args;
8253 /* Convert all template arguments to their appropriate types, and
8254 return a vector containing the innermost resulting template
8255 arguments. If any error occurs, return error_mark_node. Error and
8256 warning messages are not issued.
8258 Note that no function argument deduction is performed, and default
8259 arguments are used to fill in unspecified arguments. */
8260 tree
8261 coerce_template_parms (tree parms, tree args, tree in_decl)
8263 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8266 /* Convert all template arguments to their appropriate type, and
8267 instantiate default arguments as needed. This returns a vector
8268 containing the innermost resulting template arguments, or
8269 error_mark_node if unsuccessful. */
8270 tree
8271 coerce_template_parms (tree parms, tree args, tree in_decl,
8272 tsubst_flags_t complain)
8274 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8277 /* Like coerce_template_parms. If PARMS represents all template
8278 parameters levels, this function returns a vector of vectors
8279 representing all the resulting argument levels. Note that in this
8280 case, only the innermost arguments are coerced because the
8281 outermost ones are supposed to have been coerced already.
8283 Otherwise, if PARMS represents only (the innermost) vector of
8284 parameters, this function returns a vector containing just the
8285 innermost resulting arguments. */
8287 static tree
8288 coerce_innermost_template_parms (tree parms,
8289 tree args,
8290 tree in_decl,
8291 tsubst_flags_t complain,
8292 bool require_all_args,
8293 bool use_default_args)
8295 int parms_depth = TMPL_PARMS_DEPTH (parms);
8296 int args_depth = TMPL_ARGS_DEPTH (args);
8297 tree coerced_args;
8299 if (parms_depth > 1)
8301 coerced_args = make_tree_vec (parms_depth);
8302 tree level;
8303 int cur_depth;
8305 for (level = parms, cur_depth = parms_depth;
8306 parms_depth > 0 && level != NULL_TREE;
8307 level = TREE_CHAIN (level), --cur_depth)
8309 tree l;
8310 if (cur_depth == args_depth)
8311 l = coerce_template_parms (TREE_VALUE (level),
8312 args, in_decl, complain,
8313 require_all_args,
8314 use_default_args);
8315 else
8316 l = TMPL_ARGS_LEVEL (args, cur_depth);
8318 if (l == error_mark_node)
8319 return error_mark_node;
8321 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8324 else
8325 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8326 args, in_decl, complain,
8327 require_all_args,
8328 use_default_args);
8329 return coerced_args;
8332 /* Returns 1 if template args OT and NT are equivalent. */
8335 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8337 if (nt == ot)
8338 return 1;
8339 if (nt == NULL_TREE || ot == NULL_TREE)
8340 return false;
8341 if (nt == any_targ_node || ot == any_targ_node)
8342 return true;
8344 if (TREE_CODE (nt) == TREE_VEC)
8345 /* For member templates */
8346 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8347 else if (PACK_EXPANSION_P (ot))
8348 return (PACK_EXPANSION_P (nt)
8349 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8350 PACK_EXPANSION_PATTERN (nt))
8351 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8352 PACK_EXPANSION_EXTRA_ARGS (nt)));
8353 else if (ARGUMENT_PACK_P (ot))
8355 int i, len;
8356 tree opack, npack;
8358 if (!ARGUMENT_PACK_P (nt))
8359 return 0;
8361 opack = ARGUMENT_PACK_ARGS (ot);
8362 npack = ARGUMENT_PACK_ARGS (nt);
8363 len = TREE_VEC_LENGTH (opack);
8364 if (TREE_VEC_LENGTH (npack) != len)
8365 return 0;
8366 for (i = 0; i < len; ++i)
8367 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8368 TREE_VEC_ELT (npack, i)))
8369 return 0;
8370 return 1;
8372 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8373 gcc_unreachable ();
8374 else if (TYPE_P (nt))
8376 if (!TYPE_P (ot))
8377 return false;
8378 /* Don't treat an alias template specialization with dependent
8379 arguments as equivalent to its underlying type when used as a
8380 template argument; we need them to be distinct so that we
8381 substitute into the specialization arguments at instantiation
8382 time. And aliases can't be equivalent without being ==, so
8383 we don't need to look any deeper.
8385 During partial ordering, however, we need to treat them normally so
8386 that we can order uses of the same alias with different
8387 cv-qualification (79960). */
8388 if (!partial_order
8389 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8390 return false;
8391 else
8392 return same_type_p (ot, nt);
8394 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8395 return 0;
8396 else
8398 /* Try to treat a template non-type argument that has been converted
8399 to the parameter type as equivalent to one that hasn't yet. */
8400 for (enum tree_code code1 = TREE_CODE (ot);
8401 CONVERT_EXPR_CODE_P (code1)
8402 || code1 == NON_LVALUE_EXPR;
8403 code1 = TREE_CODE (ot))
8404 ot = TREE_OPERAND (ot, 0);
8405 for (enum tree_code code2 = TREE_CODE (nt);
8406 CONVERT_EXPR_CODE_P (code2)
8407 || code2 == NON_LVALUE_EXPR;
8408 code2 = TREE_CODE (nt))
8409 nt = TREE_OPERAND (nt, 0);
8411 return cp_tree_equal (ot, nt);
8415 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8416 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8417 NEWARG_PTR with the offending arguments if they are non-NULL. */
8420 comp_template_args (tree oldargs, tree newargs,
8421 tree *oldarg_ptr, tree *newarg_ptr,
8422 bool partial_order)
8424 int i;
8426 if (oldargs == newargs)
8427 return 1;
8429 if (!oldargs || !newargs)
8430 return 0;
8432 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8433 return 0;
8435 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8437 tree nt = TREE_VEC_ELT (newargs, i);
8438 tree ot = TREE_VEC_ELT (oldargs, i);
8440 if (! template_args_equal (ot, nt, partial_order))
8442 if (oldarg_ptr != NULL)
8443 *oldarg_ptr = ot;
8444 if (newarg_ptr != NULL)
8445 *newarg_ptr = nt;
8446 return 0;
8449 return 1;
8452 inline bool
8453 comp_template_args_porder (tree oargs, tree nargs)
8455 return comp_template_args (oargs, nargs, NULL, NULL, true);
8458 static void
8459 add_pending_template (tree d)
8461 tree ti = (TYPE_P (d)
8462 ? CLASSTYPE_TEMPLATE_INFO (d)
8463 : DECL_TEMPLATE_INFO (d));
8464 struct pending_template *pt;
8465 int level;
8467 if (TI_PENDING_TEMPLATE_FLAG (ti))
8468 return;
8470 /* We are called both from instantiate_decl, where we've already had a
8471 tinst_level pushed, and instantiate_template, where we haven't.
8472 Compensate. */
8473 level = !current_tinst_level || current_tinst_level->decl != d;
8475 if (level)
8476 push_tinst_level (d);
8478 pt = ggc_alloc<pending_template> ();
8479 pt->next = NULL;
8480 pt->tinst = current_tinst_level;
8481 if (last_pending_template)
8482 last_pending_template->next = pt;
8483 else
8484 pending_templates = pt;
8486 last_pending_template = pt;
8488 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8490 if (level)
8491 pop_tinst_level ();
8495 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8496 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8497 documentation for TEMPLATE_ID_EXPR. */
8499 tree
8500 lookup_template_function (tree fns, tree arglist)
8502 tree type;
8504 if (fns == error_mark_node || arglist == error_mark_node)
8505 return error_mark_node;
8507 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8509 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8511 error ("%q#D is not a function template", fns);
8512 return error_mark_node;
8515 if (BASELINK_P (fns))
8517 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8518 unknown_type_node,
8519 BASELINK_FUNCTIONS (fns),
8520 arglist);
8521 return fns;
8524 type = TREE_TYPE (fns);
8525 if (TREE_CODE (fns) == OVERLOAD || !type)
8526 type = unknown_type_node;
8528 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8531 /* Within the scope of a template class S<T>, the name S gets bound
8532 (in build_self_reference) to a TYPE_DECL for the class, not a
8533 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8534 or one of its enclosing classes, and that type is a template,
8535 return the associated TEMPLATE_DECL. Otherwise, the original
8536 DECL is returned.
8538 Also handle the case when DECL is a TREE_LIST of ambiguous
8539 injected-class-names from different bases. */
8541 tree
8542 maybe_get_template_decl_from_type_decl (tree decl)
8544 if (decl == NULL_TREE)
8545 return decl;
8547 /* DR 176: A lookup that finds an injected-class-name (10.2
8548 [class.member.lookup]) can result in an ambiguity in certain cases
8549 (for example, if it is found in more than one base class). If all of
8550 the injected-class-names that are found refer to specializations of
8551 the same class template, and if the name is followed by a
8552 template-argument-list, the reference refers to the class template
8553 itself and not a specialization thereof, and is not ambiguous. */
8554 if (TREE_CODE (decl) == TREE_LIST)
8556 tree t, tmpl = NULL_TREE;
8557 for (t = decl; t; t = TREE_CHAIN (t))
8559 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8560 if (!tmpl)
8561 tmpl = elt;
8562 else if (tmpl != elt)
8563 break;
8565 if (tmpl && t == NULL_TREE)
8566 return tmpl;
8567 else
8568 return decl;
8571 return (decl != NULL_TREE
8572 && DECL_SELF_REFERENCE_P (decl)
8573 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8574 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8577 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8578 parameters, find the desired type.
8580 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8582 IN_DECL, if non-NULL, is the template declaration we are trying to
8583 instantiate.
8585 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8586 the class we are looking up.
8588 Issue error and warning messages under control of COMPLAIN.
8590 If the template class is really a local class in a template
8591 function, then the FUNCTION_CONTEXT is the function in which it is
8592 being instantiated.
8594 ??? Note that this function is currently called *twice* for each
8595 template-id: the first time from the parser, while creating the
8596 incomplete type (finish_template_type), and the second type during the
8597 real instantiation (instantiate_template_class). This is surely something
8598 that we want to avoid. It also causes some problems with argument
8599 coercion (see convert_nontype_argument for more information on this). */
8601 static tree
8602 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8603 int entering_scope, tsubst_flags_t complain)
8605 tree templ = NULL_TREE, parmlist;
8606 tree t;
8607 spec_entry **slot;
8608 spec_entry *entry;
8609 spec_entry elt;
8610 hashval_t hash;
8612 if (identifier_p (d1))
8614 tree value = innermost_non_namespace_value (d1);
8615 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8616 templ = value;
8617 else
8619 if (context)
8620 push_decl_namespace (context);
8621 templ = lookup_name (d1);
8622 templ = maybe_get_template_decl_from_type_decl (templ);
8623 if (context)
8624 pop_decl_namespace ();
8626 if (templ)
8627 context = DECL_CONTEXT (templ);
8629 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8631 tree type = TREE_TYPE (d1);
8633 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8634 an implicit typename for the second A. Deal with it. */
8635 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8636 type = TREE_TYPE (type);
8638 if (CLASSTYPE_TEMPLATE_INFO (type))
8640 templ = CLASSTYPE_TI_TEMPLATE (type);
8641 d1 = DECL_NAME (templ);
8644 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8645 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8647 templ = TYPE_TI_TEMPLATE (d1);
8648 d1 = DECL_NAME (templ);
8650 else if (DECL_TYPE_TEMPLATE_P (d1))
8652 templ = d1;
8653 d1 = DECL_NAME (templ);
8654 context = DECL_CONTEXT (templ);
8656 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8658 templ = d1;
8659 d1 = DECL_NAME (templ);
8662 /* Issue an error message if we didn't find a template. */
8663 if (! templ)
8665 if (complain & tf_error)
8666 error ("%qT is not a template", d1);
8667 return error_mark_node;
8670 if (TREE_CODE (templ) != TEMPLATE_DECL
8671 /* Make sure it's a user visible template, if it was named by
8672 the user. */
8673 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8674 && !PRIMARY_TEMPLATE_P (templ)))
8676 if (complain & tf_error)
8678 error ("non-template type %qT used as a template", d1);
8679 if (in_decl)
8680 error ("for template declaration %q+D", in_decl);
8682 return error_mark_node;
8685 complain &= ~tf_user;
8687 /* An alias that just changes the name of a template is equivalent to the
8688 other template, so if any of the arguments are pack expansions, strip
8689 the alias to avoid problems with a pack expansion passed to a non-pack
8690 alias template parameter (DR 1430). */
8691 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8692 templ = get_underlying_template (templ);
8694 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8696 tree parm;
8697 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8698 if (arglist2 == error_mark_node
8699 || (!uses_template_parms (arglist2)
8700 && check_instantiated_args (templ, arglist2, complain)))
8701 return error_mark_node;
8703 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8704 return parm;
8706 else
8708 tree template_type = TREE_TYPE (templ);
8709 tree gen_tmpl;
8710 tree type_decl;
8711 tree found = NULL_TREE;
8712 int arg_depth;
8713 int parm_depth;
8714 int is_dependent_type;
8715 int use_partial_inst_tmpl = false;
8717 if (template_type == error_mark_node)
8718 /* An error occurred while building the template TEMPL, and a
8719 diagnostic has most certainly been emitted for that
8720 already. Let's propagate that error. */
8721 return error_mark_node;
8723 gen_tmpl = most_general_template (templ);
8724 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8725 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8726 arg_depth = TMPL_ARGS_DEPTH (arglist);
8728 if (arg_depth == 1 && parm_depth > 1)
8730 /* We've been given an incomplete set of template arguments.
8731 For example, given:
8733 template <class T> struct S1 {
8734 template <class U> struct S2 {};
8735 template <class U> struct S2<U*> {};
8738 we will be called with an ARGLIST of `U*', but the
8739 TEMPLATE will be `template <class T> template
8740 <class U> struct S1<T>::S2'. We must fill in the missing
8741 arguments. */
8742 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8743 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8744 arg_depth = TMPL_ARGS_DEPTH (arglist);
8747 /* Now we should have enough arguments. */
8748 gcc_assert (parm_depth == arg_depth);
8750 /* From here on, we're only interested in the most general
8751 template. */
8753 /* Calculate the BOUND_ARGS. These will be the args that are
8754 actually tsubst'd into the definition to create the
8755 instantiation. */
8756 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8757 complain,
8758 /*require_all_args=*/true,
8759 /*use_default_args=*/true);
8761 if (arglist == error_mark_node)
8762 /* We were unable to bind the arguments. */
8763 return error_mark_node;
8765 /* In the scope of a template class, explicit references to the
8766 template class refer to the type of the template, not any
8767 instantiation of it. For example, in:
8769 template <class T> class C { void f(C<T>); }
8771 the `C<T>' is just the same as `C'. Outside of the
8772 class, however, such a reference is an instantiation. */
8773 if (entering_scope
8774 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8775 || currently_open_class (template_type))
8777 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8779 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8780 return template_type;
8783 /* If we already have this specialization, return it. */
8784 elt.tmpl = gen_tmpl;
8785 elt.args = arglist;
8786 elt.spec = NULL_TREE;
8787 hash = spec_hasher::hash (&elt);
8788 entry = type_specializations->find_with_hash (&elt, hash);
8790 if (entry)
8791 return entry->spec;
8793 /* If the the template's constraints are not satisfied,
8794 then we cannot form a valid type.
8796 Note that the check is deferred until after the hash
8797 lookup. This prevents redundant checks on previously
8798 instantiated specializations. */
8799 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8801 if (complain & tf_error)
8803 error ("template constraint failure");
8804 diagnose_constraints (input_location, gen_tmpl, arglist);
8806 return error_mark_node;
8809 is_dependent_type = uses_template_parms (arglist);
8811 /* If the deduced arguments are invalid, then the binding
8812 failed. */
8813 if (!is_dependent_type
8814 && check_instantiated_args (gen_tmpl,
8815 INNERMOST_TEMPLATE_ARGS (arglist),
8816 complain))
8817 return error_mark_node;
8819 if (!is_dependent_type
8820 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8821 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8822 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8824 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8825 DECL_NAME (gen_tmpl),
8826 /*tag_scope=*/ts_global);
8827 return found;
8830 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8831 complain, in_decl);
8832 if (context == error_mark_node)
8833 return error_mark_node;
8835 if (!context)
8836 context = global_namespace;
8838 /* Create the type. */
8839 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8841 /* The user referred to a specialization of an alias
8842 template represented by GEN_TMPL.
8844 [temp.alias]/2 says:
8846 When a template-id refers to the specialization of an
8847 alias template, it is equivalent to the associated
8848 type obtained by substitution of its
8849 template-arguments for the template-parameters in the
8850 type-id of the alias template. */
8852 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8853 /* Note that the call above (by indirectly calling
8854 register_specialization in tsubst_decl) registers the
8855 TYPE_DECL representing the specialization of the alias
8856 template. So next time someone substitutes ARGLIST for
8857 the template parms into the alias template (GEN_TMPL),
8858 she'll get that TYPE_DECL back. */
8860 if (t == error_mark_node)
8861 return t;
8863 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8865 if (!is_dependent_type)
8867 set_current_access_from_decl (TYPE_NAME (template_type));
8868 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8869 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8870 arglist, complain, in_decl),
8871 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8872 arglist, complain, in_decl),
8873 SCOPED_ENUM_P (template_type), NULL);
8875 if (t == error_mark_node)
8876 return t;
8878 else
8880 /* We don't want to call start_enum for this type, since
8881 the values for the enumeration constants may involve
8882 template parameters. And, no one should be interested
8883 in the enumeration constants for such a type. */
8884 t = cxx_make_type (ENUMERAL_TYPE);
8885 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8887 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8888 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8889 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8891 else if (CLASS_TYPE_P (template_type))
8893 t = make_class_type (TREE_CODE (template_type));
8894 CLASSTYPE_DECLARED_CLASS (t)
8895 = CLASSTYPE_DECLARED_CLASS (template_type);
8896 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8898 /* A local class. Make sure the decl gets registered properly. */
8899 if (context == current_function_decl)
8900 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8902 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8903 /* This instantiation is another name for the primary
8904 template type. Set the TYPE_CANONICAL field
8905 appropriately. */
8906 TYPE_CANONICAL (t) = template_type;
8907 else if (any_template_arguments_need_structural_equality_p (arglist))
8908 /* Some of the template arguments require structural
8909 equality testing, so this template class requires
8910 structural equality testing. */
8911 SET_TYPE_STRUCTURAL_EQUALITY (t);
8913 else
8914 gcc_unreachable ();
8916 /* If we called start_enum or pushtag above, this information
8917 will already be set up. */
8918 if (!TYPE_NAME (t))
8920 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8922 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8923 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8924 DECL_SOURCE_LOCATION (type_decl)
8925 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8927 else
8928 type_decl = TYPE_NAME (t);
8930 if (CLASS_TYPE_P (template_type))
8932 TREE_PRIVATE (type_decl)
8933 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8934 TREE_PROTECTED (type_decl)
8935 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8936 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8938 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8939 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8943 if (OVERLOAD_TYPE_P (t)
8944 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8946 static const char *tags[] = {"abi_tag", "may_alias"};
8948 for (unsigned ix = 0; ix != 2; ix++)
8950 tree attributes
8951 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8953 if (attributes)
8954 TYPE_ATTRIBUTES (t)
8955 = tree_cons (TREE_PURPOSE (attributes),
8956 TREE_VALUE (attributes),
8957 TYPE_ATTRIBUTES (t));
8961 /* Let's consider the explicit specialization of a member
8962 of a class template specialization that is implicitly instantiated,
8963 e.g.:
8964 template<class T>
8965 struct S
8967 template<class U> struct M {}; //#0
8970 template<>
8971 template<>
8972 struct S<int>::M<char> //#1
8974 int i;
8976 [temp.expl.spec]/4 says this is valid.
8978 In this case, when we write:
8979 S<int>::M<char> m;
8981 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8982 the one of #0.
8984 When we encounter #1, we want to store the partial instantiation
8985 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8987 For all cases other than this "explicit specialization of member of a
8988 class template", we just want to store the most general template into
8989 the CLASSTYPE_TI_TEMPLATE of M.
8991 This case of "explicit specialization of member of a class template"
8992 only happens when:
8993 1/ the enclosing class is an instantiation of, and therefore not
8994 the same as, the context of the most general template, and
8995 2/ we aren't looking at the partial instantiation itself, i.e.
8996 the innermost arguments are not the same as the innermost parms of
8997 the most general template.
8999 So it's only when 1/ and 2/ happens that we want to use the partial
9000 instantiation of the member template in lieu of its most general
9001 template. */
9003 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9004 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9005 /* the enclosing class must be an instantiation... */
9006 && CLASS_TYPE_P (context)
9007 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9009 TREE_VEC_LENGTH (arglist)--;
9010 ++processing_template_decl;
9011 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9012 tree partial_inst_args =
9013 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9014 arglist, complain, NULL_TREE);
9015 --processing_template_decl;
9016 TREE_VEC_LENGTH (arglist)++;
9017 if (partial_inst_args == error_mark_node)
9018 return error_mark_node;
9019 use_partial_inst_tmpl =
9020 /*...and we must not be looking at the partial instantiation
9021 itself. */
9022 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9023 partial_inst_args);
9026 if (!use_partial_inst_tmpl)
9027 /* This case is easy; there are no member templates involved. */
9028 found = gen_tmpl;
9029 else
9031 /* This is a full instantiation of a member template. Find
9032 the partial instantiation of which this is an instance. */
9034 /* Temporarily reduce by one the number of levels in the ARGLIST
9035 so as to avoid comparing the last set of arguments. */
9036 TREE_VEC_LENGTH (arglist)--;
9037 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9038 TREE_VEC_LENGTH (arglist)++;
9039 /* FOUND is either a proper class type, or an alias
9040 template specialization. In the later case, it's a
9041 TYPE_DECL, resulting from the substituting of arguments
9042 for parameters in the TYPE_DECL of the alias template
9043 done earlier. So be careful while getting the template
9044 of FOUND. */
9045 found = (TREE_CODE (found) == TEMPLATE_DECL
9046 ? found
9047 : (TREE_CODE (found) == TYPE_DECL
9048 ? DECL_TI_TEMPLATE (found)
9049 : CLASSTYPE_TI_TEMPLATE (found)));
9052 // Build template info for the new specialization.
9053 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9055 elt.spec = t;
9056 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9057 entry = ggc_alloc<spec_entry> ();
9058 *entry = elt;
9059 *slot = entry;
9061 /* Note this use of the partial instantiation so we can check it
9062 later in maybe_process_partial_specialization. */
9063 DECL_TEMPLATE_INSTANTIATIONS (found)
9064 = tree_cons (arglist, t,
9065 DECL_TEMPLATE_INSTANTIATIONS (found));
9067 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9068 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9069 /* Now that the type has been registered on the instantiations
9070 list, we set up the enumerators. Because the enumeration
9071 constants may involve the enumeration type itself, we make
9072 sure to register the type first, and then create the
9073 constants. That way, doing tsubst_expr for the enumeration
9074 constants won't result in recursive calls here; we'll find
9075 the instantiation and exit above. */
9076 tsubst_enum (template_type, t, arglist);
9078 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9079 /* If the type makes use of template parameters, the
9080 code that generates debugging information will crash. */
9081 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9083 /* Possibly limit visibility based on template args. */
9084 TREE_PUBLIC (type_decl) = 1;
9085 determine_visibility (type_decl);
9087 inherit_targ_abi_tags (t);
9089 return t;
9093 /* Wrapper for lookup_template_class_1. */
9095 tree
9096 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9097 int entering_scope, tsubst_flags_t complain)
9099 tree ret;
9100 timevar_push (TV_TEMPLATE_INST);
9101 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9102 entering_scope, complain);
9103 timevar_pop (TV_TEMPLATE_INST);
9104 return ret;
9107 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9109 tree
9110 lookup_template_variable (tree templ, tree arglist)
9112 /* The type of the expression is NULL_TREE since the template-id could refer
9113 to an explicit or partial specialization. */
9114 tree type = NULL_TREE;
9115 if (flag_concepts && variable_concept_p (templ))
9116 /* Except that concepts are always bool. */
9117 type = boolean_type_node;
9118 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9121 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9123 tree
9124 finish_template_variable (tree var, tsubst_flags_t complain)
9126 tree templ = TREE_OPERAND (var, 0);
9127 tree arglist = TREE_OPERAND (var, 1);
9129 /* We never want to return a VAR_DECL for a variable concept, since they
9130 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9131 bool concept_p = flag_concepts && variable_concept_p (templ);
9132 if (concept_p && processing_template_decl)
9133 return var;
9135 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9136 arglist = add_outermost_template_args (tmpl_args, arglist);
9138 templ = most_general_template (templ);
9139 tree parms = DECL_TEMPLATE_PARMS (templ);
9140 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9141 /*req_all*/true,
9142 /*use_default*/true);
9144 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9146 if (complain & tf_error)
9148 error ("use of invalid variable template %qE", var);
9149 diagnose_constraints (location_of (var), templ, arglist);
9151 return error_mark_node;
9154 /* If a template-id refers to a specialization of a variable
9155 concept, then the expression is true if and only if the
9156 concept's constraints are satisfied by the given template
9157 arguments.
9159 NOTE: This is an extension of Concepts Lite TS that
9160 allows constraints to be used in expressions. */
9161 if (concept_p)
9163 tree decl = DECL_TEMPLATE_RESULT (templ);
9164 return evaluate_variable_concept (decl, arglist);
9167 return instantiate_template (templ, arglist, complain);
9170 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9171 TARGS template args, and instantiate it if it's not dependent. */
9173 tree
9174 lookup_and_finish_template_variable (tree templ, tree targs,
9175 tsubst_flags_t complain)
9177 templ = lookup_template_variable (templ, targs);
9178 if (!any_dependent_template_arguments_p (targs))
9180 templ = finish_template_variable (templ, complain);
9181 mark_used (templ);
9184 return convert_from_reference (templ);
9188 struct pair_fn_data
9190 tree_fn_t fn;
9191 tree_fn_t any_fn;
9192 void *data;
9193 /* True when we should also visit template parameters that occur in
9194 non-deduced contexts. */
9195 bool include_nondeduced_p;
9196 hash_set<tree> *visited;
9199 /* Called from for_each_template_parm via walk_tree. */
9201 static tree
9202 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9204 tree t = *tp;
9205 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9206 tree_fn_t fn = pfd->fn;
9207 void *data = pfd->data;
9208 tree result = NULL_TREE;
9210 #define WALK_SUBTREE(NODE) \
9211 do \
9213 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9214 pfd->include_nondeduced_p, \
9215 pfd->any_fn); \
9216 if (result) goto out; \
9218 while (0)
9220 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9221 return t;
9223 if (TYPE_P (t)
9224 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9225 WALK_SUBTREE (TYPE_CONTEXT (t));
9227 switch (TREE_CODE (t))
9229 case RECORD_TYPE:
9230 if (TYPE_PTRMEMFUNC_P (t))
9231 break;
9232 /* Fall through. */
9234 case UNION_TYPE:
9235 case ENUMERAL_TYPE:
9236 if (!TYPE_TEMPLATE_INFO (t))
9237 *walk_subtrees = 0;
9238 else
9239 WALK_SUBTREE (TYPE_TI_ARGS (t));
9240 break;
9242 case INTEGER_TYPE:
9243 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9244 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9245 break;
9247 case METHOD_TYPE:
9248 /* Since we're not going to walk subtrees, we have to do this
9249 explicitly here. */
9250 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9251 /* Fall through. */
9253 case FUNCTION_TYPE:
9254 /* Check the return type. */
9255 WALK_SUBTREE (TREE_TYPE (t));
9257 /* Check the parameter types. Since default arguments are not
9258 instantiated until they are needed, the TYPE_ARG_TYPES may
9259 contain expressions that involve template parameters. But,
9260 no-one should be looking at them yet. And, once they're
9261 instantiated, they don't contain template parameters, so
9262 there's no point in looking at them then, either. */
9264 tree parm;
9266 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9267 WALK_SUBTREE (TREE_VALUE (parm));
9269 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9270 want walk_tree walking into them itself. */
9271 *walk_subtrees = 0;
9274 if (flag_noexcept_type)
9276 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9277 if (spec)
9278 WALK_SUBTREE (TREE_PURPOSE (spec));
9280 break;
9282 case TYPEOF_TYPE:
9283 case UNDERLYING_TYPE:
9284 if (pfd->include_nondeduced_p
9285 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9286 pfd->visited,
9287 pfd->include_nondeduced_p,
9288 pfd->any_fn))
9289 return error_mark_node;
9290 break;
9292 case FUNCTION_DECL:
9293 case VAR_DECL:
9294 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9295 WALK_SUBTREE (DECL_TI_ARGS (t));
9296 /* Fall through. */
9298 case PARM_DECL:
9299 case CONST_DECL:
9300 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9301 WALK_SUBTREE (DECL_INITIAL (t));
9302 if (DECL_CONTEXT (t)
9303 && pfd->include_nondeduced_p)
9304 WALK_SUBTREE (DECL_CONTEXT (t));
9305 break;
9307 case BOUND_TEMPLATE_TEMPLATE_PARM:
9308 /* Record template parameters such as `T' inside `TT<T>'. */
9309 WALK_SUBTREE (TYPE_TI_ARGS (t));
9310 /* Fall through. */
9312 case TEMPLATE_TEMPLATE_PARM:
9313 case TEMPLATE_TYPE_PARM:
9314 case TEMPLATE_PARM_INDEX:
9315 if (fn && (*fn)(t, data))
9316 return t;
9317 else if (!fn)
9318 return t;
9319 break;
9321 case TEMPLATE_DECL:
9322 /* A template template parameter is encountered. */
9323 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9324 WALK_SUBTREE (TREE_TYPE (t));
9326 /* Already substituted template template parameter */
9327 *walk_subtrees = 0;
9328 break;
9330 case TYPENAME_TYPE:
9331 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9332 partial instantiation. */
9333 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9334 break;
9336 case CONSTRUCTOR:
9337 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9338 && pfd->include_nondeduced_p)
9339 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9340 break;
9342 case INDIRECT_REF:
9343 case COMPONENT_REF:
9344 /* If there's no type, then this thing must be some expression
9345 involving template parameters. */
9346 if (!fn && !TREE_TYPE (t))
9347 return error_mark_node;
9348 break;
9350 case MODOP_EXPR:
9351 case CAST_EXPR:
9352 case IMPLICIT_CONV_EXPR:
9353 case REINTERPRET_CAST_EXPR:
9354 case CONST_CAST_EXPR:
9355 case STATIC_CAST_EXPR:
9356 case DYNAMIC_CAST_EXPR:
9357 case ARROW_EXPR:
9358 case DOTSTAR_EXPR:
9359 case TYPEID_EXPR:
9360 case PSEUDO_DTOR_EXPR:
9361 if (!fn)
9362 return error_mark_node;
9363 break;
9365 default:
9366 break;
9369 #undef WALK_SUBTREE
9371 /* We didn't find any template parameters we liked. */
9372 out:
9373 return result;
9376 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9377 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9378 call FN with the parameter and the DATA.
9379 If FN returns nonzero, the iteration is terminated, and
9380 for_each_template_parm returns 1. Otherwise, the iteration
9381 continues. If FN never returns a nonzero value, the value
9382 returned by for_each_template_parm is 0. If FN is NULL, it is
9383 considered to be the function which always returns 1.
9385 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9386 parameters that occur in non-deduced contexts. When false, only
9387 visits those template parameters that can be deduced. */
9389 static tree
9390 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9391 hash_set<tree> *visited,
9392 bool include_nondeduced_p,
9393 tree_fn_t any_fn)
9395 struct pair_fn_data pfd;
9396 tree result;
9398 /* Set up. */
9399 pfd.fn = fn;
9400 pfd.any_fn = any_fn;
9401 pfd.data = data;
9402 pfd.include_nondeduced_p = include_nondeduced_p;
9404 /* Walk the tree. (Conceptually, we would like to walk without
9405 duplicates, but for_each_template_parm_r recursively calls
9406 for_each_template_parm, so we would need to reorganize a fair
9407 bit to use walk_tree_without_duplicates, so we keep our own
9408 visited list.) */
9409 if (visited)
9410 pfd.visited = visited;
9411 else
9412 pfd.visited = new hash_set<tree>;
9413 result = cp_walk_tree (&t,
9414 for_each_template_parm_r,
9415 &pfd,
9416 pfd.visited);
9418 /* Clean up. */
9419 if (!visited)
9421 delete pfd.visited;
9422 pfd.visited = 0;
9425 return result;
9428 /* Returns true if T depends on any template parameter. */
9431 uses_template_parms (tree t)
9433 if (t == NULL_TREE)
9434 return false;
9436 bool dependent_p;
9437 int saved_processing_template_decl;
9439 saved_processing_template_decl = processing_template_decl;
9440 if (!saved_processing_template_decl)
9441 processing_template_decl = 1;
9442 if (TYPE_P (t))
9443 dependent_p = dependent_type_p (t);
9444 else if (TREE_CODE (t) == TREE_VEC)
9445 dependent_p = any_dependent_template_arguments_p (t);
9446 else if (TREE_CODE (t) == TREE_LIST)
9447 dependent_p = (uses_template_parms (TREE_VALUE (t))
9448 || uses_template_parms (TREE_CHAIN (t)));
9449 else if (TREE_CODE (t) == TYPE_DECL)
9450 dependent_p = dependent_type_p (TREE_TYPE (t));
9451 else if (DECL_P (t)
9452 || EXPR_P (t)
9453 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9454 || TREE_CODE (t) == OVERLOAD
9455 || BASELINK_P (t)
9456 || identifier_p (t)
9457 || TREE_CODE (t) == TRAIT_EXPR
9458 || TREE_CODE (t) == CONSTRUCTOR
9459 || CONSTANT_CLASS_P (t))
9460 dependent_p = (type_dependent_expression_p (t)
9461 || value_dependent_expression_p (t));
9462 else
9464 gcc_assert (t == error_mark_node);
9465 dependent_p = false;
9468 processing_template_decl = saved_processing_template_decl;
9470 return dependent_p;
9473 /* Returns true iff current_function_decl is an incompletely instantiated
9474 template. Useful instead of processing_template_decl because the latter
9475 is set to 0 during instantiate_non_dependent_expr. */
9477 bool
9478 in_template_function (void)
9480 tree fn = current_function_decl;
9481 bool ret;
9482 ++processing_template_decl;
9483 ret = (fn && DECL_LANG_SPECIFIC (fn)
9484 && DECL_TEMPLATE_INFO (fn)
9485 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9486 --processing_template_decl;
9487 return ret;
9490 /* Returns true if T depends on any template parameter with level LEVEL. */
9492 bool
9493 uses_template_parms_level (tree t, int level)
9495 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9496 /*include_nondeduced_p=*/true);
9499 /* Returns true if the signature of DECL depends on any template parameter from
9500 its enclosing class. */
9502 bool
9503 uses_outer_template_parms (tree decl)
9505 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9506 if (depth == 0)
9507 return false;
9508 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9509 &depth, NULL, /*include_nondeduced_p=*/true))
9510 return true;
9511 if (PRIMARY_TEMPLATE_P (decl)
9512 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9513 (DECL_TEMPLATE_PARMS (decl)),
9514 template_parm_outer_level,
9515 &depth, NULL, /*include_nondeduced_p=*/true))
9516 return true;
9517 tree ci = get_constraints (decl);
9518 if (ci)
9519 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9520 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9521 &depth, NULL, /*nondeduced*/true))
9522 return true;
9523 return false;
9526 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9527 ill-formed translation unit, i.e. a variable or function that isn't
9528 usable in a constant expression. */
9530 static inline bool
9531 neglectable_inst_p (tree d)
9533 return (DECL_P (d)
9534 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9535 : decl_maybe_constant_var_p (d)));
9538 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9539 neglectable and instantiated from within an erroneous instantiation. */
9541 static bool
9542 limit_bad_template_recursion (tree decl)
9544 struct tinst_level *lev = current_tinst_level;
9545 int errs = errorcount + sorrycount;
9546 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9547 return false;
9549 for (; lev; lev = lev->next)
9550 if (neglectable_inst_p (lev->decl))
9551 break;
9553 return (lev && errs > lev->errors);
9556 static int tinst_depth;
9557 extern int max_tinst_depth;
9558 int depth_reached;
9560 static GTY(()) struct tinst_level *last_error_tinst_level;
9562 /* We're starting to instantiate D; record the template instantiation context
9563 for diagnostics and to restore it later. */
9565 bool
9566 push_tinst_level (tree d)
9568 return push_tinst_level_loc (d, input_location);
9571 /* We're starting to instantiate D; record the template instantiation context
9572 at LOC for diagnostics and to restore it later. */
9574 bool
9575 push_tinst_level_loc (tree d, location_t loc)
9577 struct tinst_level *new_level;
9579 if (tinst_depth >= max_tinst_depth)
9581 /* Tell error.c not to try to instantiate any templates. */
9582 at_eof = 2;
9583 fatal_error (input_location,
9584 "template instantiation depth exceeds maximum of %d"
9585 " (use -ftemplate-depth= to increase the maximum)",
9586 max_tinst_depth);
9587 return false;
9590 /* If the current instantiation caused problems, don't let it instantiate
9591 anything else. Do allow deduction substitution and decls usable in
9592 constant expressions. */
9593 if (limit_bad_template_recursion (d))
9594 return false;
9596 /* When not -quiet, dump template instantiations other than functions, since
9597 announce_function will take care of those. */
9598 if (!quiet_flag
9599 && TREE_CODE (d) != TREE_LIST
9600 && TREE_CODE (d) != FUNCTION_DECL)
9601 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9603 new_level = ggc_alloc<tinst_level> ();
9604 new_level->decl = d;
9605 new_level->locus = loc;
9606 new_level->errors = errorcount+sorrycount;
9607 new_level->in_system_header_p = in_system_header_at (input_location);
9608 new_level->next = current_tinst_level;
9609 current_tinst_level = new_level;
9611 ++tinst_depth;
9612 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9613 depth_reached = tinst_depth;
9615 return true;
9618 /* We're done instantiating this template; return to the instantiation
9619 context. */
9621 void
9622 pop_tinst_level (void)
9624 /* Restore the filename and line number stashed away when we started
9625 this instantiation. */
9626 input_location = current_tinst_level->locus;
9627 current_tinst_level = current_tinst_level->next;
9628 --tinst_depth;
9631 /* We're instantiating a deferred template; restore the template
9632 instantiation context in which the instantiation was requested, which
9633 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9635 static tree
9636 reopen_tinst_level (struct tinst_level *level)
9638 struct tinst_level *t;
9640 tinst_depth = 0;
9641 for (t = level; t; t = t->next)
9642 ++tinst_depth;
9644 current_tinst_level = level;
9645 pop_tinst_level ();
9646 if (current_tinst_level)
9647 current_tinst_level->errors = errorcount+sorrycount;
9648 return level->decl;
9651 /* Returns the TINST_LEVEL which gives the original instantiation
9652 context. */
9654 struct tinst_level *
9655 outermost_tinst_level (void)
9657 struct tinst_level *level = current_tinst_level;
9658 if (level)
9659 while (level->next)
9660 level = level->next;
9661 return level;
9664 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9665 vector of template arguments, as for tsubst.
9667 Returns an appropriate tsubst'd friend declaration. */
9669 static tree
9670 tsubst_friend_function (tree decl, tree args)
9672 tree new_friend;
9674 if (TREE_CODE (decl) == FUNCTION_DECL
9675 && DECL_TEMPLATE_INSTANTIATION (decl)
9676 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9677 /* This was a friend declared with an explicit template
9678 argument list, e.g.:
9680 friend void f<>(T);
9682 to indicate that f was a template instantiation, not a new
9683 function declaration. Now, we have to figure out what
9684 instantiation of what template. */
9686 tree template_id, arglist, fns;
9687 tree new_args;
9688 tree tmpl;
9689 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9691 /* Friend functions are looked up in the containing namespace scope.
9692 We must enter that scope, to avoid finding member functions of the
9693 current class with same name. */
9694 push_nested_namespace (ns);
9695 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9696 tf_warning_or_error, NULL_TREE,
9697 /*integral_constant_expression_p=*/false);
9698 pop_nested_namespace (ns);
9699 arglist = tsubst (DECL_TI_ARGS (decl), args,
9700 tf_warning_or_error, NULL_TREE);
9701 template_id = lookup_template_function (fns, arglist);
9703 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9704 tmpl = determine_specialization (template_id, new_friend,
9705 &new_args,
9706 /*need_member_template=*/0,
9707 TREE_VEC_LENGTH (args),
9708 tsk_none);
9709 return instantiate_template (tmpl, new_args, tf_error);
9712 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9714 /* The NEW_FRIEND will look like an instantiation, to the
9715 compiler, but is not an instantiation from the point of view of
9716 the language. For example, we might have had:
9718 template <class T> struct S {
9719 template <class U> friend void f(T, U);
9722 Then, in S<int>, template <class U> void f(int, U) is not an
9723 instantiation of anything. */
9724 if (new_friend == error_mark_node)
9725 return error_mark_node;
9727 DECL_USE_TEMPLATE (new_friend) = 0;
9728 if (TREE_CODE (decl) == TEMPLATE_DECL)
9730 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9731 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9732 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9735 /* The mangled name for the NEW_FRIEND is incorrect. The function
9736 is not a template instantiation and should not be mangled like
9737 one. Therefore, we forget the mangling here; we'll recompute it
9738 later if we need it. */
9739 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9741 SET_DECL_RTL (new_friend, NULL);
9742 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9745 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9747 tree old_decl;
9748 tree new_friend_template_info;
9749 tree new_friend_result_template_info;
9750 tree ns;
9751 int new_friend_is_defn;
9753 /* We must save some information from NEW_FRIEND before calling
9754 duplicate decls since that function will free NEW_FRIEND if
9755 possible. */
9756 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9757 new_friend_is_defn =
9758 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9759 (template_for_substitution (new_friend)))
9760 != NULL_TREE);
9761 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9763 /* This declaration is a `primary' template. */
9764 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9766 new_friend_result_template_info
9767 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9769 else
9770 new_friend_result_template_info = NULL_TREE;
9772 /* Inside pushdecl_namespace_level, we will push into the
9773 current namespace. However, the friend function should go
9774 into the namespace of the template. */
9775 ns = decl_namespace_context (new_friend);
9776 push_nested_namespace (ns);
9777 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9778 pop_nested_namespace (ns);
9780 if (old_decl == error_mark_node)
9781 return error_mark_node;
9783 if (old_decl != new_friend)
9785 /* This new friend declaration matched an existing
9786 declaration. For example, given:
9788 template <class T> void f(T);
9789 template <class U> class C {
9790 template <class T> friend void f(T) {}
9793 the friend declaration actually provides the definition
9794 of `f', once C has been instantiated for some type. So,
9795 old_decl will be the out-of-class template declaration,
9796 while new_friend is the in-class definition.
9798 But, if `f' was called before this point, the
9799 instantiation of `f' will have DECL_TI_ARGS corresponding
9800 to `T' but not to `U', references to which might appear
9801 in the definition of `f'. Previously, the most general
9802 template for an instantiation of `f' was the out-of-class
9803 version; now it is the in-class version. Therefore, we
9804 run through all specialization of `f', adding to their
9805 DECL_TI_ARGS appropriately. In particular, they need a
9806 new set of outer arguments, corresponding to the
9807 arguments for this class instantiation.
9809 The same situation can arise with something like this:
9811 friend void f(int);
9812 template <class T> class C {
9813 friend void f(T) {}
9816 when `C<int>' is instantiated. Now, `f(int)' is defined
9817 in the class. */
9819 if (!new_friend_is_defn)
9820 /* On the other hand, if the in-class declaration does
9821 *not* provide a definition, then we don't want to alter
9822 existing definitions. We can just leave everything
9823 alone. */
9825 else
9827 tree new_template = TI_TEMPLATE (new_friend_template_info);
9828 tree new_args = TI_ARGS (new_friend_template_info);
9830 /* Overwrite whatever template info was there before, if
9831 any, with the new template information pertaining to
9832 the declaration. */
9833 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9835 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9837 /* We should have called reregister_specialization in
9838 duplicate_decls. */
9839 gcc_assert (retrieve_specialization (new_template,
9840 new_args, 0)
9841 == old_decl);
9843 /* Instantiate it if the global has already been used. */
9844 if (DECL_ODR_USED (old_decl))
9845 instantiate_decl (old_decl, /*defer_ok=*/true,
9846 /*expl_inst_class_mem_p=*/false);
9848 else
9850 tree t;
9852 /* Indicate that the old function template is a partial
9853 instantiation. */
9854 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9855 = new_friend_result_template_info;
9857 gcc_assert (new_template
9858 == most_general_template (new_template));
9859 gcc_assert (new_template != old_decl);
9861 /* Reassign any specializations already in the hash table
9862 to the new more general template, and add the
9863 additional template args. */
9864 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9865 t != NULL_TREE;
9866 t = TREE_CHAIN (t))
9868 tree spec = TREE_VALUE (t);
9869 spec_entry elt;
9871 elt.tmpl = old_decl;
9872 elt.args = DECL_TI_ARGS (spec);
9873 elt.spec = NULL_TREE;
9875 decl_specializations->remove_elt (&elt);
9877 DECL_TI_ARGS (spec)
9878 = add_outermost_template_args (new_args,
9879 DECL_TI_ARGS (spec));
9881 register_specialization
9882 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9885 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9889 /* The information from NEW_FRIEND has been merged into OLD_DECL
9890 by duplicate_decls. */
9891 new_friend = old_decl;
9894 else
9896 tree context = DECL_CONTEXT (new_friend);
9897 bool dependent_p;
9899 /* In the code
9900 template <class T> class C {
9901 template <class U> friend void C1<U>::f (); // case 1
9902 friend void C2<T>::f (); // case 2
9904 we only need to make sure CONTEXT is a complete type for
9905 case 2. To distinguish between the two cases, we note that
9906 CONTEXT of case 1 remains dependent type after tsubst while
9907 this isn't true for case 2. */
9908 ++processing_template_decl;
9909 dependent_p = dependent_type_p (context);
9910 --processing_template_decl;
9912 if (!dependent_p
9913 && !complete_type_or_else (context, NULL_TREE))
9914 return error_mark_node;
9916 if (COMPLETE_TYPE_P (context))
9918 tree fn = new_friend;
9919 /* do_friend adds the TEMPLATE_DECL for any member friend
9920 template even if it isn't a member template, i.e.
9921 template <class T> friend A<T>::f();
9922 Look through it in that case. */
9923 if (TREE_CODE (fn) == TEMPLATE_DECL
9924 && !PRIMARY_TEMPLATE_P (fn))
9925 fn = DECL_TEMPLATE_RESULT (fn);
9926 /* Check to see that the declaration is really present, and,
9927 possibly obtain an improved declaration. */
9928 fn = check_classfn (context, fn, NULL_TREE);
9930 if (fn)
9931 new_friend = fn;
9935 return new_friend;
9938 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9939 template arguments, as for tsubst.
9941 Returns an appropriate tsubst'd friend type or error_mark_node on
9942 failure. */
9944 static tree
9945 tsubst_friend_class (tree friend_tmpl, tree args)
9947 tree friend_type;
9948 tree tmpl;
9949 tree context;
9951 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9953 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9954 return TREE_TYPE (t);
9957 context = CP_DECL_CONTEXT (friend_tmpl);
9959 if (context != global_namespace)
9961 if (TREE_CODE (context) == NAMESPACE_DECL)
9962 push_nested_namespace (context);
9963 else
9964 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9967 /* Look for a class template declaration. We look for hidden names
9968 because two friend declarations of the same template are the
9969 same. For example, in:
9971 struct A {
9972 template <typename> friend class F;
9974 template <typename> struct B {
9975 template <typename> friend class F;
9978 both F templates are the same. */
9979 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9980 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9982 /* But, if we don't find one, it might be because we're in a
9983 situation like this:
9985 template <class T>
9986 struct S {
9987 template <class U>
9988 friend struct S;
9991 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9992 for `S<int>', not the TEMPLATE_DECL. */
9993 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9995 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9996 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9999 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10001 /* The friend template has already been declared. Just
10002 check to see that the declarations match, and install any new
10003 default parameters. We must tsubst the default parameters,
10004 of course. We only need the innermost template parameters
10005 because that is all that redeclare_class_template will look
10006 at. */
10007 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10008 > TMPL_ARGS_DEPTH (args))
10010 tree parms;
10011 location_t saved_input_location;
10012 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10013 args, tf_warning_or_error);
10015 saved_input_location = input_location;
10016 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10017 tree cons = get_constraints (tmpl);
10018 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10019 input_location = saved_input_location;
10023 friend_type = TREE_TYPE (tmpl);
10025 else
10027 /* The friend template has not already been declared. In this
10028 case, the instantiation of the template class will cause the
10029 injection of this template into the global scope. */
10030 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10031 if (tmpl == error_mark_node)
10032 return error_mark_node;
10034 /* The new TMPL is not an instantiation of anything, so we
10035 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10036 the new type because that is supposed to be the corresponding
10037 template decl, i.e., TMPL. */
10038 DECL_USE_TEMPLATE (tmpl) = 0;
10039 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10040 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10041 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10042 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10044 /* Inject this template into the global scope. */
10045 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10048 if (context != global_namespace)
10050 if (TREE_CODE (context) == NAMESPACE_DECL)
10051 pop_nested_namespace (context);
10052 else
10053 pop_nested_class ();
10056 return friend_type;
10059 /* Returns zero if TYPE cannot be completed later due to circularity.
10060 Otherwise returns one. */
10062 static int
10063 can_complete_type_without_circularity (tree type)
10065 if (type == NULL_TREE || type == error_mark_node)
10066 return 0;
10067 else if (COMPLETE_TYPE_P (type))
10068 return 1;
10069 else if (TREE_CODE (type) == ARRAY_TYPE)
10070 return can_complete_type_without_circularity (TREE_TYPE (type));
10071 else if (CLASS_TYPE_P (type)
10072 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10073 return 0;
10074 else
10075 return 1;
10078 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10079 tsubst_flags_t, tree);
10081 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10082 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10084 static tree
10085 tsubst_attribute (tree t, tree *decl_p, tree args,
10086 tsubst_flags_t complain, tree in_decl)
10088 gcc_assert (ATTR_IS_DEPENDENT (t));
10090 tree val = TREE_VALUE (t);
10091 if (val == NULL_TREE)
10092 /* Nothing to do. */;
10093 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10094 && is_attribute_p ("omp declare simd",
10095 get_attribute_name (t)))
10097 tree clauses = TREE_VALUE (val);
10098 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10099 complain, in_decl);
10100 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10101 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10102 tree parms = DECL_ARGUMENTS (*decl_p);
10103 clauses
10104 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10105 if (clauses)
10106 val = build_tree_list (NULL_TREE, clauses);
10107 else
10108 val = NULL_TREE;
10110 /* If the first attribute argument is an identifier, don't
10111 pass it through tsubst. Attributes like mode, format,
10112 cleanup and several target specific attributes expect it
10113 unmodified. */
10114 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10116 tree chain
10117 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10118 /*integral_constant_expression_p=*/false);
10119 if (chain != TREE_CHAIN (val))
10120 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10122 else if (PACK_EXPANSION_P (val))
10124 /* An attribute pack expansion. */
10125 tree purp = TREE_PURPOSE (t);
10126 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10127 if (pack == error_mark_node)
10128 return error_mark_node;
10129 int len = TREE_VEC_LENGTH (pack);
10130 tree list = NULL_TREE;
10131 tree *q = &list;
10132 for (int i = 0; i < len; ++i)
10134 tree elt = TREE_VEC_ELT (pack, i);
10135 *q = build_tree_list (purp, elt);
10136 q = &TREE_CHAIN (*q);
10138 return list;
10140 else
10141 val = tsubst_expr (val, args, complain, in_decl,
10142 /*integral_constant_expression_p=*/false);
10144 if (val != TREE_VALUE (t))
10145 return build_tree_list (TREE_PURPOSE (t), val);
10146 return t;
10149 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10150 unchanged or a new TREE_LIST chain. */
10152 static tree
10153 tsubst_attributes (tree attributes, tree args,
10154 tsubst_flags_t complain, tree in_decl)
10156 tree last_dep = NULL_TREE;
10158 for (tree t = attributes; t; t = TREE_CHAIN (t))
10159 if (ATTR_IS_DEPENDENT (t))
10161 last_dep = t;
10162 attributes = copy_list (attributes);
10163 break;
10166 if (last_dep)
10167 for (tree *p = &attributes; *p; )
10169 tree t = *p;
10170 if (ATTR_IS_DEPENDENT (t))
10172 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10173 if (subst != t)
10175 *p = subst;
10177 p = &TREE_CHAIN (*p);
10178 while (*p);
10179 *p = TREE_CHAIN (t);
10180 continue;
10183 p = &TREE_CHAIN (*p);
10186 return attributes;
10189 /* Apply any attributes which had to be deferred until instantiation
10190 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10191 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10193 static void
10194 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10195 tree args, tsubst_flags_t complain, tree in_decl)
10197 tree last_dep = NULL_TREE;
10198 tree t;
10199 tree *p;
10201 if (attributes == NULL_TREE)
10202 return;
10204 if (DECL_P (*decl_p))
10206 if (TREE_TYPE (*decl_p) == error_mark_node)
10207 return;
10208 p = &DECL_ATTRIBUTES (*decl_p);
10209 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10210 to our attributes parameter. */
10211 gcc_assert (*p == attributes);
10213 else
10215 p = &TYPE_ATTRIBUTES (*decl_p);
10216 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10217 lookup_template_class_1, and should be preserved. */
10218 gcc_assert (*p != attributes);
10219 while (*p)
10220 p = &TREE_CHAIN (*p);
10223 for (t = attributes; t; t = TREE_CHAIN (t))
10224 if (ATTR_IS_DEPENDENT (t))
10226 last_dep = t;
10227 attributes = copy_list (attributes);
10228 break;
10231 *p = attributes;
10232 if (last_dep)
10234 tree late_attrs = NULL_TREE;
10235 tree *q = &late_attrs;
10237 for (; *p; )
10239 t = *p;
10240 if (ATTR_IS_DEPENDENT (t))
10242 *p = TREE_CHAIN (t);
10243 TREE_CHAIN (t) = NULL_TREE;
10244 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10246 q = &TREE_CHAIN (*q);
10247 while (*q);
10249 else
10250 p = &TREE_CHAIN (t);
10253 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10257 /* Perform (or defer) access check for typedefs that were referenced
10258 from within the template TMPL code.
10259 This is a subroutine of instantiate_decl and instantiate_class_template.
10260 TMPL is the template to consider and TARGS is the list of arguments of
10261 that template. */
10263 static void
10264 perform_typedefs_access_check (tree tmpl, tree targs)
10266 location_t saved_location;
10267 unsigned i;
10268 qualified_typedef_usage_t *iter;
10270 if (!tmpl
10271 || (!CLASS_TYPE_P (tmpl)
10272 && TREE_CODE (tmpl) != FUNCTION_DECL))
10273 return;
10275 saved_location = input_location;
10276 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10278 tree type_decl = iter->typedef_decl;
10279 tree type_scope = iter->context;
10281 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10282 continue;
10284 if (uses_template_parms (type_decl))
10285 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10286 if (uses_template_parms (type_scope))
10287 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10289 /* Make access check error messages point to the location
10290 of the use of the typedef. */
10291 input_location = iter->locus;
10292 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10293 type_decl, type_decl,
10294 tf_warning_or_error);
10296 input_location = saved_location;
10299 static tree
10300 instantiate_class_template_1 (tree type)
10302 tree templ, args, pattern, t, member;
10303 tree typedecl;
10304 tree pbinfo;
10305 tree base_list;
10306 unsigned int saved_maximum_field_alignment;
10307 tree fn_context;
10309 if (type == error_mark_node)
10310 return error_mark_node;
10312 if (COMPLETE_OR_OPEN_TYPE_P (type)
10313 || uses_template_parms (type))
10314 return type;
10316 /* Figure out which template is being instantiated. */
10317 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10318 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10320 /* Determine what specialization of the original template to
10321 instantiate. */
10322 t = most_specialized_partial_spec (type, tf_warning_or_error);
10323 if (t == error_mark_node)
10325 TYPE_BEING_DEFINED (type) = 1;
10326 return error_mark_node;
10328 else if (t)
10330 /* This TYPE is actually an instantiation of a partial
10331 specialization. We replace the innermost set of ARGS with
10332 the arguments appropriate for substitution. For example,
10333 given:
10335 template <class T> struct S {};
10336 template <class T> struct S<T*> {};
10338 and supposing that we are instantiating S<int*>, ARGS will
10339 presently be {int*} -- but we need {int}. */
10340 pattern = TREE_TYPE (t);
10341 args = TREE_PURPOSE (t);
10343 else
10345 pattern = TREE_TYPE (templ);
10346 args = CLASSTYPE_TI_ARGS (type);
10349 /* If the template we're instantiating is incomplete, then clearly
10350 there's nothing we can do. */
10351 if (!COMPLETE_TYPE_P (pattern))
10352 return type;
10354 /* If we've recursively instantiated too many templates, stop. */
10355 if (! push_tinst_level (type))
10356 return type;
10358 /* Now we're really doing the instantiation. Mark the type as in
10359 the process of being defined. */
10360 TYPE_BEING_DEFINED (type) = 1;
10362 /* We may be in the middle of deferred access check. Disable
10363 it now. */
10364 push_deferring_access_checks (dk_no_deferred);
10366 int saved_unevaluated_operand = cp_unevaluated_operand;
10367 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10369 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10370 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10371 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10372 fn_context = error_mark_node;
10373 if (!fn_context)
10374 push_to_top_level ();
10375 else
10377 cp_unevaluated_operand = 0;
10378 c_inhibit_evaluation_warnings = 0;
10380 /* Use #pragma pack from the template context. */
10381 saved_maximum_field_alignment = maximum_field_alignment;
10382 maximum_field_alignment = TYPE_PRECISION (pattern);
10384 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10386 /* Set the input location to the most specialized template definition.
10387 This is needed if tsubsting causes an error. */
10388 typedecl = TYPE_MAIN_DECL (pattern);
10389 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10390 DECL_SOURCE_LOCATION (typedecl);
10392 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10393 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10394 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10395 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10396 if (ANON_AGGR_TYPE_P (pattern))
10397 SET_ANON_AGGR_TYPE_P (type);
10398 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10400 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10401 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10402 /* Adjust visibility for template arguments. */
10403 determine_visibility (TYPE_MAIN_DECL (type));
10405 if (CLASS_TYPE_P (type))
10406 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10408 pbinfo = TYPE_BINFO (pattern);
10410 /* We should never instantiate a nested class before its enclosing
10411 class; we need to look up the nested class by name before we can
10412 instantiate it, and that lookup should instantiate the enclosing
10413 class. */
10414 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10415 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10417 base_list = NULL_TREE;
10418 if (BINFO_N_BASE_BINFOS (pbinfo))
10420 tree pbase_binfo;
10421 tree pushed_scope;
10422 int i;
10424 /* We must enter the scope containing the type, as that is where
10425 the accessibility of types named in dependent bases are
10426 looked up from. */
10427 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10429 /* Substitute into each of the bases to determine the actual
10430 basetypes. */
10431 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10433 tree base;
10434 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10435 tree expanded_bases = NULL_TREE;
10436 int idx, len = 1;
10438 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10440 expanded_bases =
10441 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10442 args, tf_error, NULL_TREE);
10443 if (expanded_bases == error_mark_node)
10444 continue;
10446 len = TREE_VEC_LENGTH (expanded_bases);
10449 for (idx = 0; idx < len; idx++)
10451 if (expanded_bases)
10452 /* Extract the already-expanded base class. */
10453 base = TREE_VEC_ELT (expanded_bases, idx);
10454 else
10455 /* Substitute to figure out the base class. */
10456 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10457 NULL_TREE);
10459 if (base == error_mark_node)
10460 continue;
10462 base_list = tree_cons (access, base, base_list);
10463 if (BINFO_VIRTUAL_P (pbase_binfo))
10464 TREE_TYPE (base_list) = integer_type_node;
10468 /* The list is now in reverse order; correct that. */
10469 base_list = nreverse (base_list);
10471 if (pushed_scope)
10472 pop_scope (pushed_scope);
10474 /* Now call xref_basetypes to set up all the base-class
10475 information. */
10476 xref_basetypes (type, base_list);
10478 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10479 (int) ATTR_FLAG_TYPE_IN_PLACE,
10480 args, tf_error, NULL_TREE);
10481 fixup_attribute_variants (type);
10483 /* Now that our base classes are set up, enter the scope of the
10484 class, so that name lookups into base classes, etc. will work
10485 correctly. This is precisely analogous to what we do in
10486 begin_class_definition when defining an ordinary non-template
10487 class, except we also need to push the enclosing classes. */
10488 push_nested_class (type);
10490 /* Now members are processed in the order of declaration. */
10491 for (member = CLASSTYPE_DECL_LIST (pattern);
10492 member; member = TREE_CHAIN (member))
10494 tree t = TREE_VALUE (member);
10496 if (TREE_PURPOSE (member))
10498 if (TYPE_P (t))
10500 /* Build new CLASSTYPE_NESTED_UTDS. */
10502 tree newtag;
10503 bool class_template_p;
10505 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10506 && TYPE_LANG_SPECIFIC (t)
10507 && CLASSTYPE_IS_TEMPLATE (t));
10508 /* If the member is a class template, then -- even after
10509 substitution -- there may be dependent types in the
10510 template argument list for the class. We increment
10511 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10512 that function will assume that no types are dependent
10513 when outside of a template. */
10514 if (class_template_p)
10515 ++processing_template_decl;
10516 newtag = tsubst (t, args, tf_error, NULL_TREE);
10517 if (class_template_p)
10518 --processing_template_decl;
10519 if (newtag == error_mark_node)
10520 continue;
10522 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10524 tree name = TYPE_IDENTIFIER (t);
10526 if (class_template_p)
10527 /* Unfortunately, lookup_template_class sets
10528 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10529 instantiation (i.e., for the type of a member
10530 template class nested within a template class.)
10531 This behavior is required for
10532 maybe_process_partial_specialization to work
10533 correctly, but is not accurate in this case;
10534 the TAG is not an instantiation of anything.
10535 (The corresponding TEMPLATE_DECL is an
10536 instantiation, but the TYPE is not.) */
10537 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10539 /* Now, we call pushtag to put this NEWTAG into the scope of
10540 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10541 pushtag calling push_template_decl. We don't have to do
10542 this for enums because it will already have been done in
10543 tsubst_enum. */
10544 if (name)
10545 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10546 pushtag (name, newtag, /*tag_scope=*/ts_current);
10549 else if (DECL_DECLARES_FUNCTION_P (t))
10551 tree r;
10553 if (TREE_CODE (t) == TEMPLATE_DECL)
10554 ++processing_template_decl;
10555 r = tsubst (t, args, tf_error, NULL_TREE);
10556 if (TREE_CODE (t) == TEMPLATE_DECL)
10557 --processing_template_decl;
10558 set_current_access_from_decl (r);
10559 finish_member_declaration (r);
10560 /* Instantiate members marked with attribute used. */
10561 if (r != error_mark_node && DECL_PRESERVE_P (r))
10562 mark_used (r);
10563 if (TREE_CODE (r) == FUNCTION_DECL
10564 && DECL_OMP_DECLARE_REDUCTION_P (r))
10565 cp_check_omp_declare_reduction (r);
10567 else if (DECL_CLASS_TEMPLATE_P (t)
10568 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10569 /* A closure type for a lambda in a default argument for a
10570 member template. Ignore it; it will be instantiated with
10571 the default argument. */;
10572 else
10574 /* Build new TYPE_FIELDS. */
10575 if (TREE_CODE (t) == STATIC_ASSERT)
10577 tree condition;
10579 ++c_inhibit_evaluation_warnings;
10580 condition =
10581 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10582 tf_warning_or_error, NULL_TREE,
10583 /*integral_constant_expression_p=*/true);
10584 --c_inhibit_evaluation_warnings;
10586 finish_static_assert (condition,
10587 STATIC_ASSERT_MESSAGE (t),
10588 STATIC_ASSERT_SOURCE_LOCATION (t),
10589 /*member_p=*/true);
10591 else if (TREE_CODE (t) != CONST_DECL)
10593 tree r;
10594 tree vec = NULL_TREE;
10595 int len = 1;
10597 /* The file and line for this declaration, to
10598 assist in error message reporting. Since we
10599 called push_tinst_level above, we don't need to
10600 restore these. */
10601 input_location = DECL_SOURCE_LOCATION (t);
10603 if (TREE_CODE (t) == TEMPLATE_DECL)
10604 ++processing_template_decl;
10605 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10606 if (TREE_CODE (t) == TEMPLATE_DECL)
10607 --processing_template_decl;
10609 if (TREE_CODE (r) == TREE_VEC)
10611 /* A capture pack became multiple fields. */
10612 vec = r;
10613 len = TREE_VEC_LENGTH (vec);
10616 for (int i = 0; i < len; ++i)
10618 if (vec)
10619 r = TREE_VEC_ELT (vec, i);
10620 if (VAR_P (r))
10622 /* In [temp.inst]:
10624 [t]he initialization (and any associated
10625 side-effects) of a static data member does
10626 not occur unless the static data member is
10627 itself used in a way that requires the
10628 definition of the static data member to
10629 exist.
10631 Therefore, we do not substitute into the
10632 initialized for the static data member here. */
10633 finish_static_data_member_decl
10635 /*init=*/NULL_TREE,
10636 /*init_const_expr_p=*/false,
10637 /*asmspec_tree=*/NULL_TREE,
10638 /*flags=*/0);
10639 /* Instantiate members marked with attribute used. */
10640 if (r != error_mark_node && DECL_PRESERVE_P (r))
10641 mark_used (r);
10643 else if (TREE_CODE (r) == FIELD_DECL)
10645 /* Determine whether R has a valid type and can be
10646 completed later. If R is invalid, then its type
10647 is replaced by error_mark_node. */
10648 tree rtype = TREE_TYPE (r);
10649 if (can_complete_type_without_circularity (rtype))
10650 complete_type (rtype);
10652 if (!complete_or_array_type_p (rtype))
10654 /* If R's type couldn't be completed and
10655 it isn't a flexible array member (whose
10656 type is incomplete by definition) give
10657 an error. */
10658 cxx_incomplete_type_error (r, rtype);
10659 TREE_TYPE (r) = error_mark_node;
10663 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10664 such a thing will already have been added to the field
10665 list by tsubst_enum in finish_member_declaration in the
10666 CLASSTYPE_NESTED_UTDS case above. */
10667 if (!(TREE_CODE (r) == TYPE_DECL
10668 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10669 && DECL_ARTIFICIAL (r)))
10671 set_current_access_from_decl (r);
10672 finish_member_declaration (r);
10678 else
10680 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10681 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10683 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10685 tree friend_type = t;
10686 bool adjust_processing_template_decl = false;
10688 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10690 /* template <class T> friend class C; */
10691 friend_type = tsubst_friend_class (friend_type, args);
10692 adjust_processing_template_decl = true;
10694 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10696 /* template <class T> friend class C::D; */
10697 friend_type = tsubst (friend_type, args,
10698 tf_warning_or_error, NULL_TREE);
10699 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10700 friend_type = TREE_TYPE (friend_type);
10701 adjust_processing_template_decl = true;
10703 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10704 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10706 /* This could be either
10708 friend class T::C;
10710 when dependent_type_p is false or
10712 template <class U> friend class T::C;
10714 otherwise. */
10715 /* Bump processing_template_decl in case this is something like
10716 template <class T> friend struct A<T>::B. */
10717 ++processing_template_decl;
10718 friend_type = tsubst (friend_type, args,
10719 tf_warning_or_error, NULL_TREE);
10720 if (dependent_type_p (friend_type))
10721 adjust_processing_template_decl = true;
10722 --processing_template_decl;
10724 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10725 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10726 && TYPE_HIDDEN_P (friend_type))
10728 /* friend class C;
10730 where C hasn't been declared yet. Let's lookup name
10731 from namespace scope directly, bypassing any name that
10732 come from dependent base class. */
10733 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10735 /* The call to xref_tag_from_type does injection for friend
10736 classes. */
10737 push_nested_namespace (ns);
10738 friend_type =
10739 xref_tag_from_type (friend_type, NULL_TREE,
10740 /*tag_scope=*/ts_current);
10741 pop_nested_namespace (ns);
10743 else if (uses_template_parms (friend_type))
10744 /* friend class C<T>; */
10745 friend_type = tsubst (friend_type, args,
10746 tf_warning_or_error, NULL_TREE);
10747 /* Otherwise it's
10749 friend class C;
10751 where C is already declared or
10753 friend class C<int>;
10755 We don't have to do anything in these cases. */
10757 if (adjust_processing_template_decl)
10758 /* Trick make_friend_class into realizing that the friend
10759 we're adding is a template, not an ordinary class. It's
10760 important that we use make_friend_class since it will
10761 perform some error-checking and output cross-reference
10762 information. */
10763 ++processing_template_decl;
10765 if (friend_type != error_mark_node)
10766 make_friend_class (type, friend_type, /*complain=*/false);
10768 if (adjust_processing_template_decl)
10769 --processing_template_decl;
10771 else
10773 /* Build new DECL_FRIENDLIST. */
10774 tree r;
10776 /* The file and line for this declaration, to
10777 assist in error message reporting. Since we
10778 called push_tinst_level above, we don't need to
10779 restore these. */
10780 input_location = DECL_SOURCE_LOCATION (t);
10782 if (TREE_CODE (t) == TEMPLATE_DECL)
10784 ++processing_template_decl;
10785 push_deferring_access_checks (dk_no_check);
10788 r = tsubst_friend_function (t, args);
10789 add_friend (type, r, /*complain=*/false);
10790 if (TREE_CODE (t) == TEMPLATE_DECL)
10792 pop_deferring_access_checks ();
10793 --processing_template_decl;
10799 if (fn_context)
10801 /* Restore these before substituting into the lambda capture
10802 initializers. */
10803 cp_unevaluated_operand = saved_unevaluated_operand;
10804 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10807 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10809 tree decl = lambda_function (type);
10810 if (decl)
10812 if (cxx_dialect >= cxx1z)
10813 CLASSTYPE_LITERAL_P (type) = true;
10815 if (!DECL_TEMPLATE_INFO (decl)
10816 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10818 /* Set function_depth to avoid garbage collection. */
10819 ++function_depth;
10820 instantiate_decl (decl, /*defer_ok=*/false, false);
10821 --function_depth;
10824 /* We need to instantiate the capture list from the template
10825 after we've instantiated the closure members, but before we
10826 consider adding the conversion op. Also keep any captures
10827 that may have been added during instantiation of the op(). */
10828 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10829 tree tmpl_cap
10830 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10831 args, tf_warning_or_error, NULL_TREE,
10832 false, false);
10834 LAMBDA_EXPR_CAPTURE_LIST (expr)
10835 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10837 maybe_add_lambda_conv_op (type);
10839 else
10840 gcc_assert (errorcount);
10843 /* Set the file and line number information to whatever is given for
10844 the class itself. This puts error messages involving generated
10845 implicit functions at a predictable point, and the same point
10846 that would be used for non-template classes. */
10847 input_location = DECL_SOURCE_LOCATION (typedecl);
10849 unreverse_member_declarations (type);
10850 finish_struct_1 (type);
10851 TYPE_BEING_DEFINED (type) = 0;
10853 /* We don't instantiate default arguments for member functions. 14.7.1:
10855 The implicit instantiation of a class template specialization causes
10856 the implicit instantiation of the declarations, but not of the
10857 definitions or default arguments, of the class member functions,
10858 member classes, static data members and member templates.... */
10860 /* Some typedefs referenced from within the template code need to be access
10861 checked at template instantiation time, i.e now. These types were
10862 added to the template at parsing time. Let's get those and perform
10863 the access checks then. */
10864 perform_typedefs_access_check (pattern, args);
10865 perform_deferred_access_checks (tf_warning_or_error);
10866 pop_nested_class ();
10867 maximum_field_alignment = saved_maximum_field_alignment;
10868 if (!fn_context)
10869 pop_from_top_level ();
10870 pop_deferring_access_checks ();
10871 pop_tinst_level ();
10873 /* The vtable for a template class can be emitted in any translation
10874 unit in which the class is instantiated. When there is no key
10875 method, however, finish_struct_1 will already have added TYPE to
10876 the keyed_classes. */
10877 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10878 vec_safe_push (keyed_classes, type);
10880 return type;
10883 /* Wrapper for instantiate_class_template_1. */
10885 tree
10886 instantiate_class_template (tree type)
10888 tree ret;
10889 timevar_push (TV_TEMPLATE_INST);
10890 ret = instantiate_class_template_1 (type);
10891 timevar_pop (TV_TEMPLATE_INST);
10892 return ret;
10895 static tree
10896 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10898 tree r;
10900 if (!t)
10901 r = t;
10902 else if (TYPE_P (t))
10903 r = tsubst (t, args, complain, in_decl);
10904 else
10906 if (!(complain & tf_warning))
10907 ++c_inhibit_evaluation_warnings;
10908 r = tsubst_expr (t, args, complain, in_decl,
10909 /*integral_constant_expression_p=*/true);
10910 if (!(complain & tf_warning))
10911 --c_inhibit_evaluation_warnings;
10913 return r;
10916 /* Given a function parameter pack TMPL_PARM and some function parameters
10917 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10918 and set *SPEC_P to point at the next point in the list. */
10920 tree
10921 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10923 /* Collect all of the extra "packed" parameters into an
10924 argument pack. */
10925 tree parmvec;
10926 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10927 tree spec_parm = *spec_p;
10928 int i, len;
10930 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10931 if (tmpl_parm
10932 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10933 break;
10935 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10936 parmvec = make_tree_vec (len);
10937 spec_parm = *spec_p;
10938 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10939 TREE_VEC_ELT (parmvec, i) = spec_parm;
10941 /* Build the argument packs. */
10942 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10943 *spec_p = spec_parm;
10945 return argpack;
10948 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10949 NONTYPE_ARGUMENT_PACK. */
10951 static tree
10952 make_fnparm_pack (tree spec_parm)
10954 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10957 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10958 pack expansion with no extra args, 2 if it has extra args, or 0
10959 if it is not a pack expansion. */
10961 static int
10962 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10964 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10965 if (i >= TREE_VEC_LENGTH (vec))
10966 return 0;
10967 tree elt = TREE_VEC_ELT (vec, i);
10968 if (DECL_P (elt))
10969 /* A decl pack is itself an expansion. */
10970 elt = TREE_TYPE (elt);
10971 if (!PACK_EXPANSION_P (elt))
10972 return 0;
10973 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10974 return 2;
10975 return 1;
10979 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10981 static tree
10982 make_argument_pack_select (tree arg_pack, unsigned index)
10984 tree aps = make_node (ARGUMENT_PACK_SELECT);
10986 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10987 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10989 return aps;
10992 /* This is a subroutine of tsubst_pack_expansion.
10994 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10995 mechanism to store the (non complete list of) arguments of the
10996 substitution and return a non substituted pack expansion, in order
10997 to wait for when we have enough arguments to really perform the
10998 substitution. */
11000 static bool
11001 use_pack_expansion_extra_args_p (tree parm_packs,
11002 int arg_pack_len,
11003 bool has_empty_arg)
11005 /* If one pack has an expansion and another pack has a normal
11006 argument or if one pack has an empty argument and an another
11007 one hasn't then tsubst_pack_expansion cannot perform the
11008 substitution and need to fall back on the
11009 PACK_EXPANSION_EXTRA mechanism. */
11010 if (parm_packs == NULL_TREE)
11011 return false;
11012 else if (has_empty_arg)
11013 return true;
11015 bool has_expansion_arg = false;
11016 for (int i = 0 ; i < arg_pack_len; ++i)
11018 bool has_non_expansion_arg = false;
11019 for (tree parm_pack = parm_packs;
11020 parm_pack;
11021 parm_pack = TREE_CHAIN (parm_pack))
11023 tree arg = TREE_VALUE (parm_pack);
11025 int exp = argument_pack_element_is_expansion_p (arg, i);
11026 if (exp == 2)
11027 /* We can't substitute a pack expansion with extra args into
11028 our pattern. */
11029 return true;
11030 else if (exp)
11031 has_expansion_arg = true;
11032 else
11033 has_non_expansion_arg = true;
11036 if (has_expansion_arg && has_non_expansion_arg)
11037 return true;
11039 return false;
11042 /* [temp.variadic]/6 says that:
11044 The instantiation of a pack expansion [...]
11045 produces a list E1,E2, ..., En, where N is the number of elements
11046 in the pack expansion parameters.
11048 This subroutine of tsubst_pack_expansion produces one of these Ei.
11050 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11051 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11052 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11053 INDEX is the index 'i' of the element Ei to produce. ARGS,
11054 COMPLAIN, and IN_DECL are the same parameters as for the
11055 tsubst_pack_expansion function.
11057 The function returns the resulting Ei upon successful completion,
11058 or error_mark_node.
11060 Note that this function possibly modifies the ARGS parameter, so
11061 it's the responsibility of the caller to restore it. */
11063 static tree
11064 gen_elem_of_pack_expansion_instantiation (tree pattern,
11065 tree parm_packs,
11066 unsigned index,
11067 tree args /* This parm gets
11068 modified. */,
11069 tsubst_flags_t complain,
11070 tree in_decl)
11072 tree t;
11073 bool ith_elem_is_expansion = false;
11075 /* For each parameter pack, change the substitution of the parameter
11076 pack to the ith argument in its argument pack, then expand the
11077 pattern. */
11078 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11080 tree parm = TREE_PURPOSE (pack);
11081 tree arg_pack = TREE_VALUE (pack);
11082 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11084 ith_elem_is_expansion |=
11085 argument_pack_element_is_expansion_p (arg_pack, index);
11087 /* Select the Ith argument from the pack. */
11088 if (TREE_CODE (parm) == PARM_DECL
11089 || TREE_CODE (parm) == FIELD_DECL)
11091 if (index == 0)
11093 aps = make_argument_pack_select (arg_pack, index);
11094 if (!mark_used (parm, complain) && !(complain & tf_error))
11095 return error_mark_node;
11096 register_local_specialization (aps, parm);
11098 else
11099 aps = retrieve_local_specialization (parm);
11101 else
11103 int idx, level;
11104 template_parm_level_and_index (parm, &level, &idx);
11106 if (index == 0)
11108 aps = make_argument_pack_select (arg_pack, index);
11109 /* Update the corresponding argument. */
11110 TMPL_ARG (args, level, idx) = aps;
11112 else
11113 /* Re-use the ARGUMENT_PACK_SELECT. */
11114 aps = TMPL_ARG (args, level, idx);
11116 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11119 /* Substitute into the PATTERN with the (possibly altered)
11120 arguments. */
11121 if (pattern == in_decl)
11122 /* Expanding a fixed parameter pack from
11123 coerce_template_parameter_pack. */
11124 t = tsubst_decl (pattern, args, complain);
11125 else if (pattern == error_mark_node)
11126 t = error_mark_node;
11127 else if (constraint_p (pattern))
11129 if (processing_template_decl)
11130 t = tsubst_constraint (pattern, args, complain, in_decl);
11131 else
11132 t = (constraints_satisfied_p (pattern, args)
11133 ? boolean_true_node : boolean_false_node);
11135 else if (!TYPE_P (pattern))
11136 t = tsubst_expr (pattern, args, complain, in_decl,
11137 /*integral_constant_expression_p=*/false);
11138 else
11139 t = tsubst (pattern, args, complain, in_decl);
11141 /* If the Ith argument pack element is a pack expansion, then
11142 the Ith element resulting from the substituting is going to
11143 be a pack expansion as well. */
11144 if (ith_elem_is_expansion)
11145 t = make_pack_expansion (t);
11147 return t;
11150 /* When the unexpanded parameter pack in a fold expression expands to an empty
11151 sequence, the value of the expression is as follows; the program is
11152 ill-formed if the operator is not listed in this table.
11154 && true
11155 || false
11156 , void() */
11158 tree
11159 expand_empty_fold (tree t, tsubst_flags_t complain)
11161 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11162 if (!FOLD_EXPR_MODIFY_P (t))
11163 switch (code)
11165 case TRUTH_ANDIF_EXPR:
11166 return boolean_true_node;
11167 case TRUTH_ORIF_EXPR:
11168 return boolean_false_node;
11169 case COMPOUND_EXPR:
11170 return void_node;
11171 default:
11172 break;
11175 if (complain & tf_error)
11176 error_at (location_of (t),
11177 "fold of empty expansion over %O", code);
11178 return error_mark_node;
11181 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11182 form an expression that combines the two terms using the
11183 operator of T. */
11185 static tree
11186 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11188 tree op = FOLD_EXPR_OP (t);
11189 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11191 // Handle compound assignment operators.
11192 if (FOLD_EXPR_MODIFY_P (t))
11193 return build_x_modify_expr (input_location, left, code, right, complain);
11195 switch (code)
11197 case COMPOUND_EXPR:
11198 return build_x_compound_expr (input_location, left, right, complain);
11199 case DOTSTAR_EXPR:
11200 return build_m_component_ref (left, right, complain);
11201 default:
11202 return build_x_binary_op (input_location, code,
11203 left, TREE_CODE (left),
11204 right, TREE_CODE (right),
11205 /*overload=*/NULL,
11206 complain);
11210 /* Substitute ARGS into the pack of a fold expression T. */
11212 static inline tree
11213 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11215 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11218 /* Substitute ARGS into the pack of a fold expression T. */
11220 static inline tree
11221 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11223 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11226 /* Expand a PACK of arguments into a grouped as left fold.
11227 Given a pack containing elements A0, A1, ..., An and an
11228 operator @, this builds the expression:
11230 ((A0 @ A1) @ A2) ... @ An
11232 Note that PACK must not be empty.
11234 The operator is defined by the original fold expression T. */
11236 static tree
11237 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11239 tree left = TREE_VEC_ELT (pack, 0);
11240 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11242 tree right = TREE_VEC_ELT (pack, i);
11243 left = fold_expression (t, left, right, complain);
11245 return left;
11248 /* Substitute into a unary left fold expression. */
11250 static tree
11251 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11252 tree in_decl)
11254 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11255 if (pack == error_mark_node)
11256 return error_mark_node;
11257 if (PACK_EXPANSION_P (pack))
11259 tree r = copy_node (t);
11260 FOLD_EXPR_PACK (r) = pack;
11261 return r;
11263 if (TREE_VEC_LENGTH (pack) == 0)
11264 return expand_empty_fold (t, complain);
11265 else
11266 return expand_left_fold (t, pack, complain);
11269 /* Substitute into a binary left fold expression.
11271 Do ths by building a single (non-empty) vector of argumnts and
11272 building the expression from those elements. */
11274 static tree
11275 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11276 tree in_decl)
11278 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11279 if (pack == error_mark_node)
11280 return error_mark_node;
11281 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11282 if (init == error_mark_node)
11283 return error_mark_node;
11285 if (PACK_EXPANSION_P (pack))
11287 tree r = copy_node (t);
11288 FOLD_EXPR_PACK (r) = pack;
11289 FOLD_EXPR_INIT (r) = init;
11290 return r;
11293 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11294 TREE_VEC_ELT (vec, 0) = init;
11295 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11296 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11298 return expand_left_fold (t, vec, complain);
11301 /* Expand a PACK of arguments into a grouped as right fold.
11302 Given a pack containing elementns A0, A1, ..., and an
11303 operator @, this builds the expression:
11305 A0@ ... (An-2 @ (An-1 @ An))
11307 Note that PACK must not be empty.
11309 The operator is defined by the original fold expression T. */
11311 tree
11312 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11314 // Build the expression.
11315 int n = TREE_VEC_LENGTH (pack);
11316 tree right = TREE_VEC_ELT (pack, n - 1);
11317 for (--n; n != 0; --n)
11319 tree left = TREE_VEC_ELT (pack, n - 1);
11320 right = fold_expression (t, left, right, complain);
11322 return right;
11325 /* Substitute into a unary right fold expression. */
11327 static tree
11328 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11329 tree in_decl)
11331 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11332 if (pack == error_mark_node)
11333 return error_mark_node;
11334 if (PACK_EXPANSION_P (pack))
11336 tree r = copy_node (t);
11337 FOLD_EXPR_PACK (r) = pack;
11338 return r;
11340 if (TREE_VEC_LENGTH (pack) == 0)
11341 return expand_empty_fold (t, complain);
11342 else
11343 return expand_right_fold (t, pack, complain);
11346 /* Substitute into a binary right fold expression.
11348 Do ths by building a single (non-empty) vector of arguments and
11349 building the expression from those elements. */
11351 static tree
11352 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11353 tree in_decl)
11355 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11356 if (pack == error_mark_node)
11357 return error_mark_node;
11358 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11359 if (init == error_mark_node)
11360 return error_mark_node;
11362 if (PACK_EXPANSION_P (pack))
11364 tree r = copy_node (t);
11365 FOLD_EXPR_PACK (r) = pack;
11366 FOLD_EXPR_INIT (r) = init;
11367 return r;
11370 int n = TREE_VEC_LENGTH (pack);
11371 tree vec = make_tree_vec (n + 1);
11372 for (int i = 0; i < n; ++i)
11373 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11374 TREE_VEC_ELT (vec, n) = init;
11376 return expand_right_fold (t, vec, complain);
11380 /* Substitute ARGS into T, which is an pack expansion
11381 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11382 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11383 (if only a partial substitution could be performed) or
11384 ERROR_MARK_NODE if there was an error. */
11385 tree
11386 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11387 tree in_decl)
11389 tree pattern;
11390 tree pack, packs = NULL_TREE;
11391 bool unsubstituted_packs = false;
11392 int i, len = -1;
11393 tree result;
11394 hash_map<tree, tree> *saved_local_specializations = NULL;
11395 bool need_local_specializations = false;
11396 int levels;
11398 gcc_assert (PACK_EXPANSION_P (t));
11399 pattern = PACK_EXPANSION_PATTERN (t);
11401 /* Add in any args remembered from an earlier partial instantiation. */
11402 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11404 levels = TMPL_ARGS_DEPTH (args);
11406 /* Determine the argument packs that will instantiate the parameter
11407 packs used in the expansion expression. While we're at it,
11408 compute the number of arguments to be expanded and make sure it
11409 is consistent. */
11410 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11411 pack = TREE_CHAIN (pack))
11413 tree parm_pack = TREE_VALUE (pack);
11414 tree arg_pack = NULL_TREE;
11415 tree orig_arg = NULL_TREE;
11416 int level = 0;
11418 if (TREE_CODE (parm_pack) == BASES)
11420 gcc_assert (parm_pack == pattern);
11421 if (BASES_DIRECT (parm_pack))
11422 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11423 args, complain, in_decl, false));
11424 else
11425 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11426 args, complain, in_decl, false));
11428 else if (builtin_pack_call_p (parm_pack))
11430 /* ??? Support use in other patterns. */
11431 gcc_assert (parm_pack == pattern);
11432 return expand_builtin_pack_call (parm_pack, args,
11433 complain, in_decl);
11435 else if (TREE_CODE (parm_pack) == PARM_DECL)
11437 /* We know we have correct local_specializations if this
11438 expansion is at function scope, or if we're dealing with a
11439 local parameter in a requires expression; for the latter,
11440 tsubst_requires_expr set it up appropriately. */
11441 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11442 arg_pack = retrieve_local_specialization (parm_pack);
11443 else
11444 /* We can't rely on local_specializations for a parameter
11445 name used later in a function declaration (such as in a
11446 late-specified return type). Even if it exists, it might
11447 have the wrong value for a recursive call. */
11448 need_local_specializations = true;
11450 if (!arg_pack)
11452 /* This parameter pack was used in an unevaluated context. Just
11453 make a dummy decl, since it's only used for its type. */
11454 arg_pack = tsubst_decl (parm_pack, args, complain);
11455 if (arg_pack && DECL_PACK_P (arg_pack))
11456 /* Partial instantiation of the parm_pack, we can't build
11457 up an argument pack yet. */
11458 arg_pack = NULL_TREE;
11459 else
11460 arg_pack = make_fnparm_pack (arg_pack);
11463 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11464 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11465 else
11467 int idx;
11468 template_parm_level_and_index (parm_pack, &level, &idx);
11470 if (level <= levels)
11471 arg_pack = TMPL_ARG (args, level, idx);
11474 orig_arg = arg_pack;
11475 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11476 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11478 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11479 /* This can only happen if we forget to expand an argument
11480 pack somewhere else. Just return an error, silently. */
11482 result = make_tree_vec (1);
11483 TREE_VEC_ELT (result, 0) = error_mark_node;
11484 return result;
11487 if (arg_pack)
11489 int my_len =
11490 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11492 /* Don't bother trying to do a partial substitution with
11493 incomplete packs; we'll try again after deduction. */
11494 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11495 return t;
11497 if (len < 0)
11498 len = my_len;
11499 else if (len != my_len)
11501 if (!(complain & tf_error))
11502 /* Fail quietly. */;
11503 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11504 error ("mismatched argument pack lengths while expanding %qT",
11505 pattern);
11506 else
11507 error ("mismatched argument pack lengths while expanding %qE",
11508 pattern);
11509 return error_mark_node;
11512 /* Keep track of the parameter packs and their corresponding
11513 argument packs. */
11514 packs = tree_cons (parm_pack, arg_pack, packs);
11515 TREE_TYPE (packs) = orig_arg;
11517 else
11519 /* We can't substitute for this parameter pack. We use a flag as
11520 well as the missing_level counter because function parameter
11521 packs don't have a level. */
11522 gcc_assert (processing_template_decl);
11523 unsubstituted_packs = true;
11527 /* If the expansion is just T..., return the matching argument pack, unless
11528 we need to call convert_from_reference on all the elements. This is an
11529 important optimization; see c++/68422. */
11530 if (!unsubstituted_packs
11531 && TREE_PURPOSE (packs) == pattern)
11533 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11534 /* Types need no adjustment, nor does sizeof..., and if we still have
11535 some pack expansion args we won't do anything yet. */
11536 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11537 || PACK_EXPANSION_SIZEOF_P (t)
11538 || pack_expansion_args_count (args))
11539 return args;
11540 /* Also optimize expression pack expansions if we can tell that the
11541 elements won't have reference type. */
11542 tree type = TREE_TYPE (pattern);
11543 if (type && TREE_CODE (type) != REFERENCE_TYPE
11544 && !PACK_EXPANSION_P (type)
11545 && !WILDCARD_TYPE_P (type))
11546 return args;
11547 /* Otherwise use the normal path so we get convert_from_reference. */
11550 /* We cannot expand this expansion expression, because we don't have
11551 all of the argument packs we need. */
11552 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11554 /* We got some full packs, but we can't substitute them in until we
11555 have values for all the packs. So remember these until then. */
11557 t = make_pack_expansion (pattern);
11558 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11559 return t;
11561 else if (unsubstituted_packs)
11563 /* There were no real arguments, we're just replacing a parameter
11564 pack with another version of itself. Substitute into the
11565 pattern and return a PACK_EXPANSION_*. The caller will need to
11566 deal with that. */
11567 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11568 t = tsubst_expr (pattern, args, complain, in_decl,
11569 /*integral_constant_expression_p=*/false);
11570 else
11571 t = tsubst (pattern, args, complain, in_decl);
11572 t = make_pack_expansion (t);
11573 return t;
11576 gcc_assert (len >= 0);
11578 if (need_local_specializations)
11580 /* We're in a late-specified return type, so create our own local
11581 specializations map; the current map is either NULL or (in the
11582 case of recursive unification) might have bindings that we don't
11583 want to use or alter. */
11584 saved_local_specializations = local_specializations;
11585 local_specializations = new hash_map<tree, tree>;
11588 /* For each argument in each argument pack, substitute into the
11589 pattern. */
11590 result = make_tree_vec (len);
11591 tree elem_args = copy_template_args (args);
11592 for (i = 0; i < len; ++i)
11594 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11596 elem_args, complain,
11597 in_decl);
11598 TREE_VEC_ELT (result, i) = t;
11599 if (t == error_mark_node)
11601 result = error_mark_node;
11602 break;
11606 /* Update ARGS to restore the substitution from parameter packs to
11607 their argument packs. */
11608 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11610 tree parm = TREE_PURPOSE (pack);
11612 if (TREE_CODE (parm) == PARM_DECL
11613 || TREE_CODE (parm) == FIELD_DECL)
11614 register_local_specialization (TREE_TYPE (pack), parm);
11615 else
11617 int idx, level;
11619 if (TREE_VALUE (pack) == NULL_TREE)
11620 continue;
11622 template_parm_level_and_index (parm, &level, &idx);
11624 /* Update the corresponding argument. */
11625 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11626 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11627 TREE_TYPE (pack);
11628 else
11629 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11633 if (need_local_specializations)
11635 delete local_specializations;
11636 local_specializations = saved_local_specializations;
11639 /* If the dependent pack arguments were such that we end up with only a
11640 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11641 if (len == 1 && TREE_CODE (result) == TREE_VEC
11642 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11643 return TREE_VEC_ELT (result, 0);
11645 return result;
11648 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11649 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11650 parameter packs; all parms generated from a function parameter pack will
11651 have the same DECL_PARM_INDEX. */
11653 tree
11654 get_pattern_parm (tree parm, tree tmpl)
11656 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11657 tree patparm;
11659 if (DECL_ARTIFICIAL (parm))
11661 for (patparm = DECL_ARGUMENTS (pattern);
11662 patparm; patparm = DECL_CHAIN (patparm))
11663 if (DECL_ARTIFICIAL (patparm)
11664 && DECL_NAME (parm) == DECL_NAME (patparm))
11665 break;
11667 else
11669 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11670 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11671 gcc_assert (DECL_PARM_INDEX (patparm)
11672 == DECL_PARM_INDEX (parm));
11675 return patparm;
11678 /* Make an argument pack out of the TREE_VEC VEC. */
11680 static tree
11681 make_argument_pack (tree vec)
11683 tree pack;
11684 tree elt = TREE_VEC_ELT (vec, 0);
11685 if (TYPE_P (elt))
11686 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11687 else
11689 pack = make_node (NONTYPE_ARGUMENT_PACK);
11690 TREE_CONSTANT (pack) = 1;
11692 SET_ARGUMENT_PACK_ARGS (pack, vec);
11693 return pack;
11696 /* Return an exact copy of template args T that can be modified
11697 independently. */
11699 static tree
11700 copy_template_args (tree t)
11702 if (t == error_mark_node)
11703 return t;
11705 int len = TREE_VEC_LENGTH (t);
11706 tree new_vec = make_tree_vec (len);
11708 for (int i = 0; i < len; ++i)
11710 tree elt = TREE_VEC_ELT (t, i);
11711 if (elt && TREE_CODE (elt) == TREE_VEC)
11712 elt = copy_template_args (elt);
11713 TREE_VEC_ELT (new_vec, i) = elt;
11716 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11717 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11719 return new_vec;
11722 /* Substitute ARGS into the vector or list of template arguments T. */
11724 static tree
11725 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11727 tree orig_t = t;
11728 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11729 tree *elts;
11731 if (t == error_mark_node)
11732 return error_mark_node;
11734 len = TREE_VEC_LENGTH (t);
11735 elts = XALLOCAVEC (tree, len);
11737 for (i = 0; i < len; i++)
11739 tree orig_arg = TREE_VEC_ELT (t, i);
11740 tree new_arg;
11742 if (TREE_CODE (orig_arg) == TREE_VEC)
11743 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11744 else if (PACK_EXPANSION_P (orig_arg))
11746 /* Substitute into an expansion expression. */
11747 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11749 if (TREE_CODE (new_arg) == TREE_VEC)
11750 /* Add to the expanded length adjustment the number of
11751 expanded arguments. We subtract one from this
11752 measurement, because the argument pack expression
11753 itself is already counted as 1 in
11754 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11755 the argument pack is empty. */
11756 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11758 else if (ARGUMENT_PACK_P (orig_arg))
11760 /* Substitute into each of the arguments. */
11761 new_arg = TYPE_P (orig_arg)
11762 ? cxx_make_type (TREE_CODE (orig_arg))
11763 : make_node (TREE_CODE (orig_arg));
11765 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11766 args, complain, in_decl);
11767 if (pack_args == error_mark_node)
11768 new_arg = error_mark_node;
11769 else
11770 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11772 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11773 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11775 else
11776 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11778 if (new_arg == error_mark_node)
11779 return error_mark_node;
11781 elts[i] = new_arg;
11782 if (new_arg != orig_arg)
11783 need_new = 1;
11786 if (!need_new)
11787 return t;
11789 /* Make space for the expanded arguments coming from template
11790 argument packs. */
11791 t = make_tree_vec (len + expanded_len_adjust);
11792 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11793 arguments for a member template.
11794 In that case each TREE_VEC in ORIG_T represents a level of template
11795 arguments, and ORIG_T won't carry any non defaulted argument count.
11796 It will rather be the nested TREE_VECs that will carry one.
11797 In other words, ORIG_T carries a non defaulted argument count only
11798 if it doesn't contain any nested TREE_VEC. */
11799 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11801 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11802 count += expanded_len_adjust;
11803 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11805 for (i = 0, out = 0; i < len; i++)
11807 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11808 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11809 && TREE_CODE (elts[i]) == TREE_VEC)
11811 int idx;
11813 /* Now expand the template argument pack "in place". */
11814 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11815 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11817 else
11819 TREE_VEC_ELT (t, out) = elts[i];
11820 out++;
11824 return t;
11827 /* Substitute ARGS into one level PARMS of template parameters. */
11829 static tree
11830 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11832 if (parms == error_mark_node)
11833 return error_mark_node;
11835 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11837 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11839 tree tuple = TREE_VEC_ELT (parms, i);
11841 if (tuple == error_mark_node)
11842 continue;
11844 TREE_VEC_ELT (new_vec, i) =
11845 tsubst_template_parm (tuple, args, complain);
11848 return new_vec;
11851 /* Return the result of substituting ARGS into the template parameters
11852 given by PARMS. If there are m levels of ARGS and m + n levels of
11853 PARMS, then the result will contain n levels of PARMS. For
11854 example, if PARMS is `template <class T> template <class U>
11855 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11856 result will be `template <int*, double, class V>'. */
11858 static tree
11859 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11861 tree r = NULL_TREE;
11862 tree* new_parms;
11864 /* When substituting into a template, we must set
11865 PROCESSING_TEMPLATE_DECL as the template parameters may be
11866 dependent if they are based on one-another, and the dependency
11867 predicates are short-circuit outside of templates. */
11868 ++processing_template_decl;
11870 for (new_parms = &r;
11871 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11872 new_parms = &(TREE_CHAIN (*new_parms)),
11873 parms = TREE_CHAIN (parms))
11875 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11876 args, complain);
11877 *new_parms =
11878 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11879 - TMPL_ARGS_DEPTH (args)),
11880 new_vec, NULL_TREE);
11883 --processing_template_decl;
11885 return r;
11888 /* Return the result of substituting ARGS into one template parameter
11889 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11890 parameter and which TREE_PURPOSE is the default argument of the
11891 template parameter. */
11893 static tree
11894 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11896 tree default_value, parm_decl;
11898 if (args == NULL_TREE
11899 || t == NULL_TREE
11900 || t == error_mark_node)
11901 return t;
11903 gcc_assert (TREE_CODE (t) == TREE_LIST);
11905 default_value = TREE_PURPOSE (t);
11906 parm_decl = TREE_VALUE (t);
11908 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11909 if (TREE_CODE (parm_decl) == PARM_DECL
11910 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11911 parm_decl = error_mark_node;
11912 default_value = tsubst_template_arg (default_value, args,
11913 complain, NULL_TREE);
11915 return build_tree_list (default_value, parm_decl);
11918 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11919 type T. If T is not an aggregate or enumeration type, it is
11920 handled as if by tsubst. IN_DECL is as for tsubst. If
11921 ENTERING_SCOPE is nonzero, T is the context for a template which
11922 we are presently tsubst'ing. Return the substituted value. */
11924 static tree
11925 tsubst_aggr_type (tree t,
11926 tree args,
11927 tsubst_flags_t complain,
11928 tree in_decl,
11929 int entering_scope)
11931 if (t == NULL_TREE)
11932 return NULL_TREE;
11934 switch (TREE_CODE (t))
11936 case RECORD_TYPE:
11937 if (TYPE_PTRMEMFUNC_P (t))
11938 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11940 /* Fall through. */
11941 case ENUMERAL_TYPE:
11942 case UNION_TYPE:
11943 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11945 tree argvec;
11946 tree context;
11947 tree r;
11948 int saved_unevaluated_operand;
11949 int saved_inhibit_evaluation_warnings;
11951 /* In "sizeof(X<I>)" we need to evaluate "I". */
11952 saved_unevaluated_operand = cp_unevaluated_operand;
11953 cp_unevaluated_operand = 0;
11954 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11955 c_inhibit_evaluation_warnings = 0;
11957 /* First, determine the context for the type we are looking
11958 up. */
11959 context = TYPE_CONTEXT (t);
11960 if (context && TYPE_P (context))
11962 context = tsubst_aggr_type (context, args, complain,
11963 in_decl, /*entering_scope=*/1);
11964 /* If context is a nested class inside a class template,
11965 it may still need to be instantiated (c++/33959). */
11966 context = complete_type (context);
11969 /* Then, figure out what arguments are appropriate for the
11970 type we are trying to find. For example, given:
11972 template <class T> struct S;
11973 template <class T, class U> void f(T, U) { S<U> su; }
11975 and supposing that we are instantiating f<int, double>,
11976 then our ARGS will be {int, double}, but, when looking up
11977 S we only want {double}. */
11978 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11979 complain, in_decl);
11980 if (argvec == error_mark_node)
11981 r = error_mark_node;
11982 else
11984 r = lookup_template_class (t, argvec, in_decl, context,
11985 entering_scope, complain);
11986 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11989 cp_unevaluated_operand = saved_unevaluated_operand;
11990 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11992 return r;
11994 else
11995 /* This is not a template type, so there's nothing to do. */
11996 return t;
11998 default:
11999 return tsubst (t, args, complain, in_decl);
12003 static GTY(()) hash_map<tree, tree> *defarg_inst;
12005 /* Substitute into the default argument ARG (a default argument for
12006 FN), which has the indicated TYPE. */
12008 tree
12009 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12010 tsubst_flags_t complain)
12012 tree saved_class_ptr = NULL_TREE;
12013 tree saved_class_ref = NULL_TREE;
12014 int errs = errorcount + sorrycount;
12016 /* This can happen in invalid code. */
12017 if (TREE_CODE (arg) == DEFAULT_ARG)
12018 return arg;
12020 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12021 parm = chain_index (parmnum, parm);
12022 tree parmtype = TREE_TYPE (parm);
12023 if (DECL_BY_REFERENCE (parm))
12024 parmtype = TREE_TYPE (parmtype);
12025 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12027 tree *slot;
12028 if (defarg_inst && (slot = defarg_inst->get (parm)))
12029 return *slot;
12031 /* This default argument came from a template. Instantiate the
12032 default argument here, not in tsubst. In the case of
12033 something like:
12035 template <class T>
12036 struct S {
12037 static T t();
12038 void f(T = t());
12041 we must be careful to do name lookup in the scope of S<T>,
12042 rather than in the current class. */
12043 push_access_scope (fn);
12044 /* The "this" pointer is not valid in a default argument. */
12045 if (cfun)
12047 saved_class_ptr = current_class_ptr;
12048 cp_function_chain->x_current_class_ptr = NULL_TREE;
12049 saved_class_ref = current_class_ref;
12050 cp_function_chain->x_current_class_ref = NULL_TREE;
12053 push_deferring_access_checks(dk_no_deferred);
12054 /* The default argument expression may cause implicitly defined
12055 member functions to be synthesized, which will result in garbage
12056 collection. We must treat this situation as if we were within
12057 the body of function so as to avoid collecting live data on the
12058 stack. */
12059 ++function_depth;
12060 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12061 complain, NULL_TREE,
12062 /*integral_constant_expression_p=*/false);
12063 --function_depth;
12064 pop_deferring_access_checks();
12066 /* Restore the "this" pointer. */
12067 if (cfun)
12069 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12070 cp_function_chain->x_current_class_ref = saved_class_ref;
12073 if (errorcount+sorrycount > errs
12074 && (complain & tf_warning_or_error))
12075 inform (input_location,
12076 " when instantiating default argument for call to %qD", fn);
12078 /* Make sure the default argument is reasonable. */
12079 arg = check_default_argument (type, arg, complain);
12081 pop_access_scope (fn);
12083 if (arg != error_mark_node && !cp_unevaluated_operand)
12085 if (!defarg_inst)
12086 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12087 defarg_inst->put (parm, arg);
12090 return arg;
12093 /* Substitute into all the default arguments for FN. */
12095 static void
12096 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12098 tree arg;
12099 tree tmpl_args;
12101 tmpl_args = DECL_TI_ARGS (fn);
12103 /* If this function is not yet instantiated, we certainly don't need
12104 its default arguments. */
12105 if (uses_template_parms (tmpl_args))
12106 return;
12107 /* Don't do this again for clones. */
12108 if (DECL_CLONED_FUNCTION_P (fn))
12109 return;
12111 int i = 0;
12112 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12113 arg;
12114 arg = TREE_CHAIN (arg), ++i)
12115 if (TREE_PURPOSE (arg))
12116 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12117 TREE_VALUE (arg),
12118 TREE_PURPOSE (arg),
12119 complain);
12122 /* Substitute the ARGS into the T, which is a _DECL. Return the
12123 result of the substitution. Issue error and warning messages under
12124 control of COMPLAIN. */
12126 static tree
12127 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12129 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12130 location_t saved_loc;
12131 tree r = NULL_TREE;
12132 tree in_decl = t;
12133 hashval_t hash = 0;
12135 /* Set the filename and linenumber to improve error-reporting. */
12136 saved_loc = input_location;
12137 input_location = DECL_SOURCE_LOCATION (t);
12139 switch (TREE_CODE (t))
12141 case TEMPLATE_DECL:
12143 /* We can get here when processing a member function template,
12144 member class template, or template template parameter. */
12145 tree decl = DECL_TEMPLATE_RESULT (t);
12146 tree spec;
12147 tree tmpl_args;
12148 tree full_args;
12150 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12152 /* Template template parameter is treated here. */
12153 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12154 if (new_type == error_mark_node)
12155 r = error_mark_node;
12156 /* If we get a real template back, return it. This can happen in
12157 the context of most_specialized_partial_spec. */
12158 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12159 r = new_type;
12160 else
12161 /* The new TEMPLATE_DECL was built in
12162 reduce_template_parm_level. */
12163 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12164 break;
12167 /* We might already have an instance of this template.
12168 The ARGS are for the surrounding class type, so the
12169 full args contain the tsubst'd args for the context,
12170 plus the innermost args from the template decl. */
12171 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12172 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12173 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12174 /* Because this is a template, the arguments will still be
12175 dependent, even after substitution. If
12176 PROCESSING_TEMPLATE_DECL is not set, the dependency
12177 predicates will short-circuit. */
12178 ++processing_template_decl;
12179 full_args = tsubst_template_args (tmpl_args, args,
12180 complain, in_decl);
12181 --processing_template_decl;
12182 if (full_args == error_mark_node)
12183 RETURN (error_mark_node);
12185 /* If this is a default template template argument,
12186 tsubst might not have changed anything. */
12187 if (full_args == tmpl_args)
12188 RETURN (t);
12190 hash = hash_tmpl_and_args (t, full_args);
12191 spec = retrieve_specialization (t, full_args, hash);
12192 if (spec != NULL_TREE)
12194 r = spec;
12195 break;
12198 /* Make a new template decl. It will be similar to the
12199 original, but will record the current template arguments.
12200 We also create a new function declaration, which is just
12201 like the old one, but points to this new template, rather
12202 than the old one. */
12203 r = copy_decl (t);
12204 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12205 DECL_CHAIN (r) = NULL_TREE;
12207 // Build new template info linking to the original template decl.
12208 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12210 if (TREE_CODE (decl) == TYPE_DECL
12211 && !TYPE_DECL_ALIAS_P (decl))
12213 tree new_type;
12214 ++processing_template_decl;
12215 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12216 --processing_template_decl;
12217 if (new_type == error_mark_node)
12218 RETURN (error_mark_node);
12220 TREE_TYPE (r) = new_type;
12221 /* For a partial specialization, we need to keep pointing to
12222 the primary template. */
12223 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12224 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12225 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12226 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12227 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12229 else
12231 tree new_decl;
12232 ++processing_template_decl;
12233 new_decl = tsubst (decl, args, complain, in_decl);
12234 --processing_template_decl;
12235 if (new_decl == error_mark_node)
12236 RETURN (error_mark_node);
12238 DECL_TEMPLATE_RESULT (r) = new_decl;
12239 DECL_TI_TEMPLATE (new_decl) = r;
12240 TREE_TYPE (r) = TREE_TYPE (new_decl);
12241 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12242 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12245 SET_DECL_IMPLICIT_INSTANTIATION (r);
12246 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12247 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12249 /* The template parameters for this new template are all the
12250 template parameters for the old template, except the
12251 outermost level of parameters. */
12252 DECL_TEMPLATE_PARMS (r)
12253 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12254 complain);
12256 if (PRIMARY_TEMPLATE_P (t))
12257 DECL_PRIMARY_TEMPLATE (r) = r;
12259 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12260 /* Record this non-type partial instantiation. */
12261 register_specialization (r, t,
12262 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12263 false, hash);
12265 break;
12267 case FUNCTION_DECL:
12269 tree gen_tmpl, argvec;
12271 /* Nobody should be tsubst'ing into non-template functions. */
12272 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12274 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12276 /* If T is not dependent, just return it. */
12277 if (!uses_template_parms (DECL_TI_ARGS (t)))
12278 RETURN (t);
12280 /* Calculate the most general template of which R is a
12281 specialization, and the complete set of arguments used to
12282 specialize R. */
12283 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12284 argvec = tsubst_template_args (DECL_TI_ARGS
12285 (DECL_TEMPLATE_RESULT
12286 (DECL_TI_TEMPLATE (t))),
12287 args, complain, in_decl);
12288 if (argvec == error_mark_node)
12289 RETURN (error_mark_node);
12291 /* Check to see if we already have this specialization. */
12292 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12293 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12295 r = spec;
12296 break;
12299 /* We can see more levels of arguments than parameters if
12300 there was a specialization of a member template, like
12301 this:
12303 template <class T> struct S { template <class U> void f(); }
12304 template <> template <class U> void S<int>::f(U);
12306 Here, we'll be substituting into the specialization,
12307 because that's where we can find the code we actually
12308 want to generate, but we'll have enough arguments for
12309 the most general template.
12311 We also deal with the peculiar case:
12313 template <class T> struct S {
12314 template <class U> friend void f();
12316 template <class U> void f() {}
12317 template S<int>;
12318 template void f<double>();
12320 Here, the ARGS for the instantiation of will be {int,
12321 double}. But, we only need as many ARGS as there are
12322 levels of template parameters in CODE_PATTERN. We are
12323 careful not to get fooled into reducing the ARGS in
12324 situations like:
12326 template <class T> struct S { template <class U> void f(U); }
12327 template <class T> template <> void S<T>::f(int) {}
12329 which we can spot because the pattern will be a
12330 specialization in this case. */
12331 int args_depth = TMPL_ARGS_DEPTH (args);
12332 int parms_depth =
12333 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12335 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12336 args = get_innermost_template_args (args, parms_depth);
12338 else
12340 /* This special case arises when we have something like this:
12342 template <class T> struct S {
12343 friend void f<int>(int, double);
12346 Here, the DECL_TI_TEMPLATE for the friend declaration
12347 will be an IDENTIFIER_NODE. We are being called from
12348 tsubst_friend_function, and we want only to create a
12349 new decl (R) with appropriate types so that we can call
12350 determine_specialization. */
12351 gen_tmpl = NULL_TREE;
12352 argvec = NULL_TREE;
12355 tree ctx = DECL_CONTEXT (t);
12356 bool member = ctx && TYPE_P (ctx);
12358 if (member)
12359 ctx = tsubst_aggr_type (ctx, args,
12360 complain, t, /*entering_scope=*/1);
12362 tree type = tsubst (TREE_TYPE (t), args,
12363 complain | tf_fndecl_type, in_decl);
12364 if (type == error_mark_node)
12365 RETURN (error_mark_node);
12367 /* If we hit excessive deduction depth, the type is bogus even if
12368 it isn't error_mark_node, so don't build a decl. */
12369 if (excessive_deduction_depth)
12370 RETURN (error_mark_node);
12372 /* We do NOT check for matching decls pushed separately at this
12373 point, as they may not represent instantiations of this
12374 template, and in any case are considered separate under the
12375 discrete model. */
12376 r = copy_decl (t);
12377 DECL_USE_TEMPLATE (r) = 0;
12378 TREE_TYPE (r) = type;
12379 /* Clear out the mangled name and RTL for the instantiation. */
12380 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12381 SET_DECL_RTL (r, NULL);
12382 /* Leave DECL_INITIAL set on deleted instantiations. */
12383 if (!DECL_DELETED_FN (r))
12384 DECL_INITIAL (r) = NULL_TREE;
12385 DECL_CONTEXT (r) = ctx;
12387 /* OpenMP UDRs have the only argument a reference to the declared
12388 type. We want to diagnose if the declared type is a reference,
12389 which is invalid, but as references to references are usually
12390 quietly merged, diagnose it here. */
12391 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12393 tree argtype
12394 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12395 argtype = tsubst (argtype, args, complain, in_decl);
12396 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12397 error_at (DECL_SOURCE_LOCATION (t),
12398 "reference type %qT in "
12399 "%<#pragma omp declare reduction%>", argtype);
12400 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12401 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12402 argtype);
12405 if (member && DECL_CONV_FN_P (r))
12406 /* Type-conversion operator. Reconstruct the name, in
12407 case it's the name of one of the template's parameters. */
12408 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12410 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12411 complain, t);
12412 DECL_RESULT (r) = NULL_TREE;
12414 TREE_STATIC (r) = 0;
12415 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12416 DECL_EXTERNAL (r) = 1;
12417 /* If this is an instantiation of a function with internal
12418 linkage, we already know what object file linkage will be
12419 assigned to the instantiation. */
12420 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12421 DECL_DEFER_OUTPUT (r) = 0;
12422 DECL_CHAIN (r) = NULL_TREE;
12423 DECL_PENDING_INLINE_INFO (r) = 0;
12424 DECL_PENDING_INLINE_P (r) = 0;
12425 DECL_SAVED_TREE (r) = NULL_TREE;
12426 DECL_STRUCT_FUNCTION (r) = NULL;
12427 TREE_USED (r) = 0;
12428 /* We'll re-clone as appropriate in instantiate_template. */
12429 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12431 /* If we aren't complaining now, return on error before we register
12432 the specialization so that we'll complain eventually. */
12433 if ((complain & tf_error) == 0
12434 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12435 && !grok_op_properties (r, /*complain=*/false))
12436 RETURN (error_mark_node);
12438 /* When instantiating a constrained member, substitute
12439 into the constraints to create a new constraint. */
12440 if (tree ci = get_constraints (t))
12441 if (member)
12443 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12444 set_constraints (r, ci);
12447 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12448 this in the special friend case mentioned above where
12449 GEN_TMPL is NULL. */
12450 if (gen_tmpl)
12452 DECL_TEMPLATE_INFO (r)
12453 = build_template_info (gen_tmpl, argvec);
12454 SET_DECL_IMPLICIT_INSTANTIATION (r);
12456 tree new_r
12457 = register_specialization (r, gen_tmpl, argvec, false, hash);
12458 if (new_r != r)
12459 /* We instantiated this while substituting into
12460 the type earlier (template/friend54.C). */
12461 RETURN (new_r);
12463 /* We're not supposed to instantiate default arguments
12464 until they are called, for a template. But, for a
12465 declaration like:
12467 template <class T> void f ()
12468 { extern void g(int i = T()); }
12470 we should do the substitution when the template is
12471 instantiated. We handle the member function case in
12472 instantiate_class_template since the default arguments
12473 might refer to other members of the class. */
12474 if (!member
12475 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12476 && !uses_template_parms (argvec))
12477 tsubst_default_arguments (r, complain);
12479 else
12480 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12482 /* Copy the list of befriending classes. */
12483 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12484 *friends;
12485 friends = &TREE_CHAIN (*friends))
12487 *friends = copy_node (*friends);
12488 TREE_VALUE (*friends)
12489 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12492 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12494 maybe_retrofit_in_chrg (r);
12495 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12496 RETURN (error_mark_node);
12497 /* If this is an instantiation of a member template, clone it.
12498 If it isn't, that'll be handled by
12499 clone_constructors_and_destructors. */
12500 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12501 clone_function_decl (r, /*update_methods=*/false);
12503 else if ((complain & tf_error) != 0
12504 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12505 && !grok_op_properties (r, /*complain=*/true))
12506 RETURN (error_mark_node);
12508 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12509 SET_DECL_FRIEND_CONTEXT (r,
12510 tsubst (DECL_FRIEND_CONTEXT (t),
12511 args, complain, in_decl));
12513 /* Possibly limit visibility based on template args. */
12514 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12515 if (DECL_VISIBILITY_SPECIFIED (t))
12517 DECL_VISIBILITY_SPECIFIED (r) = 0;
12518 DECL_ATTRIBUTES (r)
12519 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12521 determine_visibility (r);
12522 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12523 && !processing_template_decl)
12524 defaulted_late_check (r);
12526 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12527 args, complain, in_decl);
12529 break;
12531 case PARM_DECL:
12533 tree type = NULL_TREE;
12534 int i, len = 1;
12535 tree expanded_types = NULL_TREE;
12536 tree prev_r = NULL_TREE;
12537 tree first_r = NULL_TREE;
12539 if (DECL_PACK_P (t))
12541 /* If there is a local specialization that isn't a
12542 parameter pack, it means that we're doing a "simple"
12543 substitution from inside tsubst_pack_expansion. Just
12544 return the local specialization (which will be a single
12545 parm). */
12546 tree spec = retrieve_local_specialization (t);
12547 if (spec
12548 && TREE_CODE (spec) == PARM_DECL
12549 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12550 RETURN (spec);
12552 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12553 the parameters in this function parameter pack. */
12554 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12555 complain, in_decl);
12556 if (TREE_CODE (expanded_types) == TREE_VEC)
12558 len = TREE_VEC_LENGTH (expanded_types);
12560 /* Zero-length parameter packs are boring. Just substitute
12561 into the chain. */
12562 if (len == 0)
12563 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12564 TREE_CHAIN (t)));
12566 else
12568 /* All we did was update the type. Make a note of that. */
12569 type = expanded_types;
12570 expanded_types = NULL_TREE;
12574 /* Loop through all of the parameters we'll build. When T is
12575 a function parameter pack, LEN is the number of expanded
12576 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12577 r = NULL_TREE;
12578 for (i = 0; i < len; ++i)
12580 prev_r = r;
12581 r = copy_node (t);
12582 if (DECL_TEMPLATE_PARM_P (t))
12583 SET_DECL_TEMPLATE_PARM_P (r);
12585 if (expanded_types)
12586 /* We're on the Ith parameter of the function parameter
12587 pack. */
12589 /* Get the Ith type. */
12590 type = TREE_VEC_ELT (expanded_types, i);
12592 /* Rename the parameter to include the index. */
12593 DECL_NAME (r)
12594 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12596 else if (!type)
12597 /* We're dealing with a normal parameter. */
12598 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12600 type = type_decays_to (type);
12601 TREE_TYPE (r) = type;
12602 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12604 if (DECL_INITIAL (r))
12606 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12607 DECL_INITIAL (r) = TREE_TYPE (r);
12608 else
12609 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12610 complain, in_decl);
12613 DECL_CONTEXT (r) = NULL_TREE;
12615 if (!DECL_TEMPLATE_PARM_P (r))
12616 DECL_ARG_TYPE (r) = type_passed_as (type);
12618 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12619 args, complain, in_decl);
12621 /* Keep track of the first new parameter we
12622 generate. That's what will be returned to the
12623 caller. */
12624 if (!first_r)
12625 first_r = r;
12627 /* Build a proper chain of parameters when substituting
12628 into a function parameter pack. */
12629 if (prev_r)
12630 DECL_CHAIN (prev_r) = r;
12633 /* If cp_unevaluated_operand is set, we're just looking for a
12634 single dummy parameter, so don't keep going. */
12635 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12636 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12637 complain, DECL_CHAIN (t));
12639 /* FIRST_R contains the start of the chain we've built. */
12640 r = first_r;
12642 break;
12644 case FIELD_DECL:
12646 tree type = NULL_TREE;
12647 tree vec = NULL_TREE;
12648 tree expanded_types = NULL_TREE;
12649 int len = 1;
12651 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12653 /* This field is a lambda capture pack. Return a TREE_VEC of
12654 the expanded fields to instantiate_class_template_1 and
12655 store them in the specializations hash table as a
12656 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12657 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12658 complain, in_decl);
12659 if (TREE_CODE (expanded_types) == TREE_VEC)
12661 len = TREE_VEC_LENGTH (expanded_types);
12662 vec = make_tree_vec (len);
12664 else
12666 /* All we did was update the type. Make a note of that. */
12667 type = expanded_types;
12668 expanded_types = NULL_TREE;
12672 for (int i = 0; i < len; ++i)
12674 r = copy_decl (t);
12675 if (expanded_types)
12677 type = TREE_VEC_ELT (expanded_types, i);
12678 DECL_NAME (r)
12679 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12681 else if (!type)
12682 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12684 if (type == error_mark_node)
12685 RETURN (error_mark_node);
12686 TREE_TYPE (r) = type;
12687 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12689 if (DECL_C_BIT_FIELD (r))
12690 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12691 non-bit-fields DECL_INITIAL is a non-static data member
12692 initializer, which gets deferred instantiation. */
12693 DECL_INITIAL (r)
12694 = tsubst_expr (DECL_INITIAL (t), args,
12695 complain, in_decl,
12696 /*integral_constant_expression_p=*/true);
12697 else if (DECL_INITIAL (t))
12699 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12700 NSDMI in perform_member_init. Still set DECL_INITIAL
12701 so that we know there is one. */
12702 DECL_INITIAL (r) = void_node;
12703 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12704 retrofit_lang_decl (r);
12705 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12707 /* We don't have to set DECL_CONTEXT here; it is set by
12708 finish_member_declaration. */
12709 DECL_CHAIN (r) = NULL_TREE;
12711 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12712 args, complain, in_decl);
12714 if (vec)
12715 TREE_VEC_ELT (vec, i) = r;
12718 if (vec)
12720 r = vec;
12721 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12722 SET_ARGUMENT_PACK_ARGS (pack, vec);
12723 register_specialization (pack, t, args, false, 0);
12726 break;
12728 case USING_DECL:
12729 /* We reach here only for member using decls. We also need to check
12730 uses_template_parms because DECL_DEPENDENT_P is not set for a
12731 using-declaration that designates a member of the current
12732 instantiation (c++/53549). */
12733 if (DECL_DEPENDENT_P (t)
12734 || uses_template_parms (USING_DECL_SCOPE (t)))
12736 tree scope = USING_DECL_SCOPE (t);
12737 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12738 if (PACK_EXPANSION_P (scope))
12740 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12741 int len = TREE_VEC_LENGTH (vec);
12742 r = make_tree_vec (len);
12743 for (int i = 0; i < len; ++i)
12745 tree escope = TREE_VEC_ELT (vec, i);
12746 tree elt = do_class_using_decl (escope, name);
12747 if (!elt)
12749 r = error_mark_node;
12750 break;
12752 else
12754 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12755 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12757 TREE_VEC_ELT (r, i) = elt;
12760 else
12762 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12763 complain, in_decl);
12764 r = do_class_using_decl (inst_scope, name);
12765 if (!r)
12766 r = error_mark_node;
12767 else
12769 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12770 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12774 else
12776 r = copy_node (t);
12777 DECL_CHAIN (r) = NULL_TREE;
12779 break;
12781 case TYPE_DECL:
12782 case VAR_DECL:
12784 tree argvec = NULL_TREE;
12785 tree gen_tmpl = NULL_TREE;
12786 tree spec;
12787 tree tmpl = NULL_TREE;
12788 tree ctx;
12789 tree type = NULL_TREE;
12790 bool local_p;
12792 if (TREE_TYPE (t) == error_mark_node)
12793 RETURN (error_mark_node);
12795 if (TREE_CODE (t) == TYPE_DECL
12796 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12798 /* If this is the canonical decl, we don't have to
12799 mess with instantiations, and often we can't (for
12800 typename, template type parms and such). Note that
12801 TYPE_NAME is not correct for the above test if
12802 we've copied the type for a typedef. */
12803 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12804 if (type == error_mark_node)
12805 RETURN (error_mark_node);
12806 r = TYPE_NAME (type);
12807 break;
12810 /* Check to see if we already have the specialization we
12811 need. */
12812 spec = NULL_TREE;
12813 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12815 /* T is a static data member or namespace-scope entity.
12816 We have to substitute into namespace-scope variables
12817 (not just variable templates) because of cases like:
12819 template <class T> void f() { extern T t; }
12821 where the entity referenced is not known until
12822 instantiation time. */
12823 local_p = false;
12824 ctx = DECL_CONTEXT (t);
12825 if (DECL_CLASS_SCOPE_P (t))
12827 ctx = tsubst_aggr_type (ctx, args,
12828 complain,
12829 in_decl, /*entering_scope=*/1);
12830 /* If CTX is unchanged, then T is in fact the
12831 specialization we want. That situation occurs when
12832 referencing a static data member within in its own
12833 class. We can use pointer equality, rather than
12834 same_type_p, because DECL_CONTEXT is always
12835 canonical... */
12836 if (ctx == DECL_CONTEXT (t)
12837 /* ... unless T is a member template; in which
12838 case our caller can be willing to create a
12839 specialization of that template represented
12840 by T. */
12841 && !(DECL_TI_TEMPLATE (t)
12842 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12843 spec = t;
12846 if (!spec)
12848 tmpl = DECL_TI_TEMPLATE (t);
12849 gen_tmpl = most_general_template (tmpl);
12850 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12851 if (argvec != error_mark_node)
12852 argvec = (coerce_innermost_template_parms
12853 (DECL_TEMPLATE_PARMS (gen_tmpl),
12854 argvec, t, complain,
12855 /*all*/true, /*defarg*/true));
12856 if (argvec == error_mark_node)
12857 RETURN (error_mark_node);
12858 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12859 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12862 else
12864 /* A local variable. */
12865 local_p = true;
12866 /* Subsequent calls to pushdecl will fill this in. */
12867 ctx = NULL_TREE;
12868 /* Unless this is a reference to a static variable from an
12869 enclosing function, in which case we need to fill it in now. */
12870 if (TREE_STATIC (t))
12872 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12873 if (fn != current_function_decl)
12874 ctx = fn;
12876 spec = retrieve_local_specialization (t);
12878 /* If we already have the specialization we need, there is
12879 nothing more to do. */
12880 if (spec)
12882 r = spec;
12883 break;
12886 /* Create a new node for the specialization we need. */
12887 r = copy_decl (t);
12888 if (type == NULL_TREE)
12890 if (is_typedef_decl (t))
12891 type = DECL_ORIGINAL_TYPE (t);
12892 else
12893 type = TREE_TYPE (t);
12894 if (VAR_P (t)
12895 && VAR_HAD_UNKNOWN_BOUND (t)
12896 && type != error_mark_node)
12897 type = strip_array_domain (type);
12898 tree auto_node = type_uses_auto (type);
12899 int len = TREE_VEC_LENGTH (args);
12900 if (auto_node)
12901 /* Mask off any template args past the variable's context so we
12902 don't replace the auto with an unrelated argument. */
12903 TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
12904 type = tsubst (type, args, complain, in_decl);
12905 if (auto_node)
12906 TREE_VEC_LENGTH (args) = len;
12908 if (VAR_P (r))
12910 /* Even if the original location is out of scope, the
12911 newly substituted one is not. */
12912 DECL_DEAD_FOR_LOCAL (r) = 0;
12913 DECL_INITIALIZED_P (r) = 0;
12914 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12915 if (type == error_mark_node)
12916 RETURN (error_mark_node);
12917 if (TREE_CODE (type) == FUNCTION_TYPE)
12919 /* It may seem that this case cannot occur, since:
12921 typedef void f();
12922 void g() { f x; }
12924 declares a function, not a variable. However:
12926 typedef void f();
12927 template <typename T> void g() { T t; }
12928 template void g<f>();
12930 is an attempt to declare a variable with function
12931 type. */
12932 error ("variable %qD has function type",
12933 /* R is not yet sufficiently initialized, so we
12934 just use its name. */
12935 DECL_NAME (r));
12936 RETURN (error_mark_node);
12938 type = complete_type (type);
12939 /* Wait until cp_finish_decl to set this again, to handle
12940 circular dependency (template/instantiate6.C). */
12941 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12942 type = check_var_type (DECL_NAME (r), type);
12944 if (DECL_HAS_VALUE_EXPR_P (t))
12946 tree ve = DECL_VALUE_EXPR (t);
12947 ve = tsubst_expr (ve, args, complain, in_decl,
12948 /*constant_expression_p=*/false);
12949 if (REFERENCE_REF_P (ve))
12951 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12952 ve = TREE_OPERAND (ve, 0);
12954 SET_DECL_VALUE_EXPR (r, ve);
12956 if (CP_DECL_THREAD_LOCAL_P (r)
12957 && !processing_template_decl)
12958 set_decl_tls_model (r, decl_default_tls_model (r));
12960 else if (DECL_SELF_REFERENCE_P (t))
12961 SET_DECL_SELF_REFERENCE_P (r);
12962 TREE_TYPE (r) = type;
12963 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12964 DECL_CONTEXT (r) = ctx;
12965 /* Clear out the mangled name and RTL for the instantiation. */
12966 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12967 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12968 SET_DECL_RTL (r, NULL);
12969 /* The initializer must not be expanded until it is required;
12970 see [temp.inst]. */
12971 DECL_INITIAL (r) = NULL_TREE;
12972 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12973 if (VAR_P (r))
12975 SET_DECL_MODE (r, VOIDmode);
12977 /* Possibly limit visibility based on template args. */
12978 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12979 if (DECL_VISIBILITY_SPECIFIED (t))
12981 DECL_VISIBILITY_SPECIFIED (r) = 0;
12982 DECL_ATTRIBUTES (r)
12983 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12985 determine_visibility (r);
12988 if (!local_p)
12990 /* A static data member declaration is always marked
12991 external when it is declared in-class, even if an
12992 initializer is present. We mimic the non-template
12993 processing here. */
12994 DECL_EXTERNAL (r) = 1;
12995 if (DECL_NAMESPACE_SCOPE_P (t))
12996 DECL_NOT_REALLY_EXTERN (r) = 1;
12998 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12999 SET_DECL_IMPLICIT_INSTANTIATION (r);
13000 register_specialization (r, gen_tmpl, argvec, false, hash);
13002 else
13004 if (DECL_LANG_SPECIFIC (r))
13005 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13006 if (!cp_unevaluated_operand)
13007 register_local_specialization (r, t);
13010 DECL_CHAIN (r) = NULL_TREE;
13012 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13013 /*flags=*/0,
13014 args, complain, in_decl);
13016 /* Preserve a typedef that names a type. */
13017 if (is_typedef_decl (r) && type != error_mark_node)
13019 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13020 set_underlying_type (r);
13021 if (TYPE_DECL_ALIAS_P (r))
13022 /* An alias template specialization can be dependent
13023 even if its underlying type is not. */
13024 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13027 layout_decl (r, 0);
13029 break;
13031 default:
13032 gcc_unreachable ();
13034 #undef RETURN
13036 out:
13037 /* Restore the file and line information. */
13038 input_location = saved_loc;
13040 return r;
13043 /* Substitute into the ARG_TYPES of a function type.
13044 If END is a TREE_CHAIN, leave it and any following types
13045 un-substituted. */
13047 static tree
13048 tsubst_arg_types (tree arg_types,
13049 tree args,
13050 tree end,
13051 tsubst_flags_t complain,
13052 tree in_decl)
13054 tree remaining_arg_types;
13055 tree type = NULL_TREE;
13056 int i = 1;
13057 tree expanded_args = NULL_TREE;
13058 tree default_arg;
13060 if (!arg_types || arg_types == void_list_node || arg_types == end)
13061 return arg_types;
13063 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13064 args, end, complain, in_decl);
13065 if (remaining_arg_types == error_mark_node)
13066 return error_mark_node;
13068 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13070 /* For a pack expansion, perform substitution on the
13071 entire expression. Later on, we'll handle the arguments
13072 one-by-one. */
13073 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13074 args, complain, in_decl);
13076 if (TREE_CODE (expanded_args) == TREE_VEC)
13077 /* So that we'll spin through the parameters, one by one. */
13078 i = TREE_VEC_LENGTH (expanded_args);
13079 else
13081 /* We only partially substituted into the parameter
13082 pack. Our type is TYPE_PACK_EXPANSION. */
13083 type = expanded_args;
13084 expanded_args = NULL_TREE;
13088 while (i > 0) {
13089 --i;
13091 if (expanded_args)
13092 type = TREE_VEC_ELT (expanded_args, i);
13093 else if (!type)
13094 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13096 if (type == error_mark_node)
13097 return error_mark_node;
13098 if (VOID_TYPE_P (type))
13100 if (complain & tf_error)
13102 error ("invalid parameter type %qT", type);
13103 if (in_decl)
13104 error ("in declaration %q+D", in_decl);
13106 return error_mark_node;
13108 /* DR 657. */
13109 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13110 return error_mark_node;
13112 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13113 top-level qualifiers as required. */
13114 type = cv_unqualified (type_decays_to (type));
13116 /* We do not substitute into default arguments here. The standard
13117 mandates that they be instantiated only when needed, which is
13118 done in build_over_call. */
13119 default_arg = TREE_PURPOSE (arg_types);
13121 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13123 /* We've instantiated a template before its default arguments
13124 have been parsed. This can happen for a nested template
13125 class, and is not an error unless we require the default
13126 argument in a call of this function. */
13127 remaining_arg_types =
13128 tree_cons (default_arg, type, remaining_arg_types);
13129 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13131 else
13132 remaining_arg_types =
13133 hash_tree_cons (default_arg, type, remaining_arg_types);
13136 return remaining_arg_types;
13139 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13140 *not* handle the exception-specification for FNTYPE, because the
13141 initial substitution of explicitly provided template parameters
13142 during argument deduction forbids substitution into the
13143 exception-specification:
13145 [temp.deduct]
13147 All references in the function type of the function template to the
13148 corresponding template parameters are replaced by the specified tem-
13149 plate argument values. If a substitution in a template parameter or
13150 in the function type of the function template results in an invalid
13151 type, type deduction fails. [Note: The equivalent substitution in
13152 exception specifications is done only when the function is instanti-
13153 ated, at which point a program is ill-formed if the substitution
13154 results in an invalid type.] */
13156 static tree
13157 tsubst_function_type (tree t,
13158 tree args,
13159 tsubst_flags_t complain,
13160 tree in_decl)
13162 tree return_type;
13163 tree arg_types = NULL_TREE;
13164 tree fntype;
13166 /* The TYPE_CONTEXT is not used for function/method types. */
13167 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13169 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13170 failure. */
13171 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13173 if (late_return_type_p)
13175 /* Substitute the argument types. */
13176 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13177 complain, in_decl);
13178 if (arg_types == error_mark_node)
13179 return error_mark_node;
13181 tree save_ccp = current_class_ptr;
13182 tree save_ccr = current_class_ref;
13183 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13184 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13185 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13186 if (do_inject)
13188 /* DR 1207: 'this' is in scope in the trailing return type. */
13189 inject_this_parameter (this_type, cp_type_quals (this_type));
13192 /* Substitute the return type. */
13193 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13195 if (do_inject)
13197 current_class_ptr = save_ccp;
13198 current_class_ref = save_ccr;
13201 else
13202 /* Substitute the return type. */
13203 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13205 if (return_type == error_mark_node)
13206 return error_mark_node;
13207 /* DR 486 clarifies that creation of a function type with an
13208 invalid return type is a deduction failure. */
13209 if (TREE_CODE (return_type) == ARRAY_TYPE
13210 || TREE_CODE (return_type) == FUNCTION_TYPE)
13212 if (complain & tf_error)
13214 if (TREE_CODE (return_type) == ARRAY_TYPE)
13215 error ("function returning an array");
13216 else
13217 error ("function returning a function");
13219 return error_mark_node;
13221 /* And DR 657. */
13222 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13223 return error_mark_node;
13225 if (!late_return_type_p)
13227 /* Substitute the argument types. */
13228 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13229 complain, in_decl);
13230 if (arg_types == error_mark_node)
13231 return error_mark_node;
13234 /* Construct a new type node and return it. */
13235 if (TREE_CODE (t) == FUNCTION_TYPE)
13237 fntype = build_function_type (return_type, arg_types);
13238 fntype = apply_memfn_quals (fntype,
13239 type_memfn_quals (t),
13240 type_memfn_rqual (t));
13242 else
13244 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13245 /* Don't pick up extra function qualifiers from the basetype. */
13246 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13247 if (! MAYBE_CLASS_TYPE_P (r))
13249 /* [temp.deduct]
13251 Type deduction may fail for any of the following
13252 reasons:
13254 -- Attempting to create "pointer to member of T" when T
13255 is not a class type. */
13256 if (complain & tf_error)
13257 error ("creating pointer to member function of non-class type %qT",
13259 return error_mark_node;
13262 fntype = build_method_type_directly (r, return_type,
13263 TREE_CHAIN (arg_types));
13264 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13266 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13268 if (late_return_type_p)
13269 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13271 return fntype;
13274 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13275 ARGS into that specification, and return the substituted
13276 specification. If there is no specification, return NULL_TREE. */
13278 static tree
13279 tsubst_exception_specification (tree fntype,
13280 tree args,
13281 tsubst_flags_t complain,
13282 tree in_decl,
13283 bool defer_ok)
13285 tree specs;
13286 tree new_specs;
13288 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13289 new_specs = NULL_TREE;
13290 if (specs && TREE_PURPOSE (specs))
13292 /* A noexcept-specifier. */
13293 tree expr = TREE_PURPOSE (specs);
13294 if (TREE_CODE (expr) == INTEGER_CST)
13295 new_specs = expr;
13296 else if (defer_ok)
13298 /* Defer instantiation of noexcept-specifiers to avoid
13299 excessive instantiations (c++/49107). */
13300 new_specs = make_node (DEFERRED_NOEXCEPT);
13301 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13303 /* We already partially instantiated this member template,
13304 so combine the new args with the old. */
13305 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13306 = DEFERRED_NOEXCEPT_PATTERN (expr);
13307 DEFERRED_NOEXCEPT_ARGS (new_specs)
13308 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13310 else
13312 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13313 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13316 else
13317 new_specs = tsubst_copy_and_build
13318 (expr, args, complain, in_decl, /*function_p=*/false,
13319 /*integral_constant_expression_p=*/true);
13320 new_specs = build_noexcept_spec (new_specs, complain);
13322 else if (specs)
13324 if (! TREE_VALUE (specs))
13325 new_specs = specs;
13326 else
13327 while (specs)
13329 tree spec;
13330 int i, len = 1;
13331 tree expanded_specs = NULL_TREE;
13333 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13335 /* Expand the pack expansion type. */
13336 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13337 args, complain,
13338 in_decl);
13340 if (expanded_specs == error_mark_node)
13341 return error_mark_node;
13342 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13343 len = TREE_VEC_LENGTH (expanded_specs);
13344 else
13346 /* We're substituting into a member template, so
13347 we got a TYPE_PACK_EXPANSION back. Add that
13348 expansion and move on. */
13349 gcc_assert (TREE_CODE (expanded_specs)
13350 == TYPE_PACK_EXPANSION);
13351 new_specs = add_exception_specifier (new_specs,
13352 expanded_specs,
13353 complain);
13354 specs = TREE_CHAIN (specs);
13355 continue;
13359 for (i = 0; i < len; ++i)
13361 if (expanded_specs)
13362 spec = TREE_VEC_ELT (expanded_specs, i);
13363 else
13364 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13365 if (spec == error_mark_node)
13366 return spec;
13367 new_specs = add_exception_specifier (new_specs, spec,
13368 complain);
13371 specs = TREE_CHAIN (specs);
13374 return new_specs;
13377 /* Take the tree structure T and replace template parameters used
13378 therein with the argument vector ARGS. IN_DECL is an associated
13379 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13380 Issue error and warning messages under control of COMPLAIN. Note
13381 that we must be relatively non-tolerant of extensions here, in
13382 order to preserve conformance; if we allow substitutions that
13383 should not be allowed, we may allow argument deductions that should
13384 not succeed, and therefore report ambiguous overload situations
13385 where there are none. In theory, we could allow the substitution,
13386 but indicate that it should have failed, and allow our caller to
13387 make sure that the right thing happens, but we don't try to do this
13388 yet.
13390 This function is used for dealing with types, decls and the like;
13391 for expressions, use tsubst_expr or tsubst_copy. */
13393 tree
13394 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13396 enum tree_code code;
13397 tree type, r = NULL_TREE;
13399 if (t == NULL_TREE || t == error_mark_node
13400 || t == integer_type_node
13401 || t == void_type_node
13402 || t == char_type_node
13403 || t == unknown_type_node
13404 || TREE_CODE (t) == NAMESPACE_DECL
13405 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13406 return t;
13408 if (DECL_P (t))
13409 return tsubst_decl (t, args, complain);
13411 if (args == NULL_TREE)
13412 return t;
13414 code = TREE_CODE (t);
13416 if (code == IDENTIFIER_NODE)
13417 type = IDENTIFIER_TYPE_VALUE (t);
13418 else
13419 type = TREE_TYPE (t);
13421 gcc_assert (type != unknown_type_node);
13423 /* Reuse typedefs. We need to do this to handle dependent attributes,
13424 such as attribute aligned. */
13425 if (TYPE_P (t)
13426 && typedef_variant_p (t))
13428 tree decl = TYPE_NAME (t);
13430 if (alias_template_specialization_p (t))
13432 /* DECL represents an alias template and we want to
13433 instantiate it. */
13434 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13435 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13436 r = instantiate_alias_template (tmpl, gen_args, complain);
13438 else if (DECL_CLASS_SCOPE_P (decl)
13439 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13440 && uses_template_parms (DECL_CONTEXT (decl)))
13442 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13443 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13444 r = retrieve_specialization (tmpl, gen_args, 0);
13446 else if (DECL_FUNCTION_SCOPE_P (decl)
13447 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13448 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13449 r = retrieve_local_specialization (decl);
13450 else
13451 /* The typedef is from a non-template context. */
13452 return t;
13454 if (r)
13456 r = TREE_TYPE (r);
13457 r = cp_build_qualified_type_real
13458 (r, cp_type_quals (t) | cp_type_quals (r),
13459 complain | tf_ignore_bad_quals);
13460 return r;
13462 else
13464 /* We don't have an instantiation yet, so drop the typedef. */
13465 int quals = cp_type_quals (t);
13466 t = DECL_ORIGINAL_TYPE (decl);
13467 t = cp_build_qualified_type_real (t, quals,
13468 complain | tf_ignore_bad_quals);
13472 bool fndecl_type = (complain & tf_fndecl_type);
13473 complain &= ~tf_fndecl_type;
13475 if (type
13476 && code != TYPENAME_TYPE
13477 && code != TEMPLATE_TYPE_PARM
13478 && code != TEMPLATE_PARM_INDEX
13479 && code != IDENTIFIER_NODE
13480 && code != FUNCTION_TYPE
13481 && code != METHOD_TYPE)
13482 type = tsubst (type, args, complain, in_decl);
13483 if (type == error_mark_node)
13484 return error_mark_node;
13486 switch (code)
13488 case RECORD_TYPE:
13489 case UNION_TYPE:
13490 case ENUMERAL_TYPE:
13491 return tsubst_aggr_type (t, args, complain, in_decl,
13492 /*entering_scope=*/0);
13494 case ERROR_MARK:
13495 case IDENTIFIER_NODE:
13496 case VOID_TYPE:
13497 case REAL_TYPE:
13498 case COMPLEX_TYPE:
13499 case VECTOR_TYPE:
13500 case BOOLEAN_TYPE:
13501 case NULLPTR_TYPE:
13502 case LANG_TYPE:
13503 return t;
13505 case INTEGER_TYPE:
13506 if (t == integer_type_node)
13507 return t;
13509 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13510 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13511 return t;
13514 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13516 max = tsubst_expr (omax, args, complain, in_decl,
13517 /*integral_constant_expression_p=*/false);
13519 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13520 needed. */
13521 if (TREE_CODE (max) == NOP_EXPR
13522 && TREE_SIDE_EFFECTS (omax)
13523 && !TREE_TYPE (max))
13524 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13526 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13527 with TREE_SIDE_EFFECTS that indicates this is not an integral
13528 constant expression. */
13529 if (processing_template_decl
13530 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13532 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13533 TREE_SIDE_EFFECTS (max) = 1;
13536 return compute_array_index_type (NULL_TREE, max, complain);
13539 case TEMPLATE_TYPE_PARM:
13540 case TEMPLATE_TEMPLATE_PARM:
13541 case BOUND_TEMPLATE_TEMPLATE_PARM:
13542 case TEMPLATE_PARM_INDEX:
13544 int idx;
13545 int level;
13546 int levels;
13547 tree arg = NULL_TREE;
13549 /* Early in template argument deduction substitution, we don't
13550 want to reduce the level of 'auto', or it will be confused
13551 with a normal template parm in subsequent deduction. */
13552 if (is_auto (t) && (complain & tf_partial))
13553 return t;
13555 r = NULL_TREE;
13557 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13558 template_parm_level_and_index (t, &level, &idx);
13560 levels = TMPL_ARGS_DEPTH (args);
13561 if (level <= levels
13562 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13564 arg = TMPL_ARG (args, level, idx);
13566 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13568 /* See through ARGUMENT_PACK_SELECT arguments. */
13569 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13570 /* If the selected argument is an expansion E, that most
13571 likely means we were called from
13572 gen_elem_of_pack_expansion_instantiation during the
13573 substituting of pack an argument pack (which Ith
13574 element is a pack expansion, where I is
13575 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13576 In this case, the Ith element resulting from this
13577 substituting is going to be a pack expansion, which
13578 pattern is the pattern of E. Let's return the
13579 pattern of E, and
13580 gen_elem_of_pack_expansion_instantiation will
13581 build the resulting pack expansion from it. */
13582 if (PACK_EXPANSION_P (arg))
13584 /* Make sure we aren't throwing away arg info. */
13585 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13586 arg = PACK_EXPANSION_PATTERN (arg);
13591 if (arg == error_mark_node)
13592 return error_mark_node;
13593 else if (arg != NULL_TREE)
13595 if (ARGUMENT_PACK_P (arg))
13596 /* If ARG is an argument pack, we don't actually want to
13597 perform a substitution here, because substitutions
13598 for argument packs are only done
13599 element-by-element. We can get to this point when
13600 substituting the type of a non-type template
13601 parameter pack, when that type actually contains
13602 template parameter packs from an outer template, e.g.,
13604 template<typename... Types> struct A {
13605 template<Types... Values> struct B { };
13606 }; */
13607 return t;
13609 if (code == TEMPLATE_TYPE_PARM)
13611 int quals;
13612 gcc_assert (TYPE_P (arg));
13614 quals = cp_type_quals (arg) | cp_type_quals (t);
13616 return cp_build_qualified_type_real
13617 (arg, quals, complain | tf_ignore_bad_quals);
13619 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13621 /* We are processing a type constructed from a
13622 template template parameter. */
13623 tree argvec = tsubst (TYPE_TI_ARGS (t),
13624 args, complain, in_decl);
13625 if (argvec == error_mark_node)
13626 return error_mark_node;
13628 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13629 || TREE_CODE (arg) == TEMPLATE_DECL
13630 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13632 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13633 /* Consider this code:
13635 template <template <class> class Template>
13636 struct Internal {
13637 template <class Arg> using Bind = Template<Arg>;
13640 template <template <class> class Template, class Arg>
13641 using Instantiate = Template<Arg>; //#0
13643 template <template <class> class Template,
13644 class Argument>
13645 using Bind =
13646 Instantiate<Internal<Template>::template Bind,
13647 Argument>; //#1
13649 When #1 is parsed, the
13650 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13651 parameter `Template' in #0 matches the
13652 UNBOUND_CLASS_TEMPLATE representing the argument
13653 `Internal<Template>::template Bind'; We then want
13654 to assemble the type `Bind<Argument>' that can't
13655 be fully created right now, because
13656 `Internal<Template>' not being complete, the Bind
13657 template cannot be looked up in that context. So
13658 we need to "store" `Bind<Argument>' for later
13659 when the context of Bind becomes complete. Let's
13660 store that in a TYPENAME_TYPE. */
13661 return make_typename_type (TYPE_CONTEXT (arg),
13662 build_nt (TEMPLATE_ID_EXPR,
13663 TYPE_IDENTIFIER (arg),
13664 argvec),
13665 typename_type,
13666 complain);
13668 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13669 are resolving nested-types in the signature of a
13670 member function templates. Otherwise ARG is a
13671 TEMPLATE_DECL and is the real template to be
13672 instantiated. */
13673 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13674 arg = TYPE_NAME (arg);
13676 r = lookup_template_class (arg,
13677 argvec, in_decl,
13678 DECL_CONTEXT (arg),
13679 /*entering_scope=*/0,
13680 complain);
13681 return cp_build_qualified_type_real
13682 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13684 else if (code == TEMPLATE_TEMPLATE_PARM)
13685 return arg;
13686 else
13687 /* TEMPLATE_PARM_INDEX. */
13688 return convert_from_reference (unshare_expr (arg));
13691 if (level == 1)
13692 /* This can happen during the attempted tsubst'ing in
13693 unify. This means that we don't yet have any information
13694 about the template parameter in question. */
13695 return t;
13697 /* If we get here, we must have been looking at a parm for a
13698 more deeply nested template. Make a new version of this
13699 template parameter, but with a lower level. */
13700 switch (code)
13702 case TEMPLATE_TYPE_PARM:
13703 case TEMPLATE_TEMPLATE_PARM:
13704 case BOUND_TEMPLATE_TEMPLATE_PARM:
13705 if (cp_type_quals (t))
13707 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13708 r = cp_build_qualified_type_real
13709 (r, cp_type_quals (t),
13710 complain | (code == TEMPLATE_TYPE_PARM
13711 ? tf_ignore_bad_quals : 0));
13713 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13714 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13715 && (r = (TEMPLATE_PARM_DESCENDANTS
13716 (TEMPLATE_TYPE_PARM_INDEX (t))))
13717 && (r = TREE_TYPE (r))
13718 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13719 /* Break infinite recursion when substituting the constraints
13720 of a constrained placeholder. */;
13721 else
13723 r = copy_type (t);
13724 TEMPLATE_TYPE_PARM_INDEX (r)
13725 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13726 r, levels, args, complain);
13727 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13728 TYPE_MAIN_VARIANT (r) = r;
13729 TYPE_POINTER_TO (r) = NULL_TREE;
13730 TYPE_REFERENCE_TO (r) = NULL_TREE;
13732 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13734 /* Propagate constraints on placeholders. */
13735 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13736 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13737 = tsubst_constraint (constr, args, complain, in_decl);
13738 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13740 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13741 pl = tsubst (pl, args, complain, in_decl);
13742 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13746 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13747 /* We have reduced the level of the template
13748 template parameter, but not the levels of its
13749 template parameters, so canonical_type_parameter
13750 will not be able to find the canonical template
13751 template parameter for this level. Thus, we
13752 require structural equality checking to compare
13753 TEMPLATE_TEMPLATE_PARMs. */
13754 SET_TYPE_STRUCTURAL_EQUALITY (r);
13755 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13756 SET_TYPE_STRUCTURAL_EQUALITY (r);
13757 else
13758 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13760 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13762 tree tinfo = TYPE_TEMPLATE_INFO (t);
13763 /* We might need to substitute into the types of non-type
13764 template parameters. */
13765 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13766 complain, in_decl);
13767 if (tmpl == error_mark_node)
13768 return error_mark_node;
13769 tree argvec = tsubst (TI_ARGS (tinfo), args,
13770 complain, in_decl);
13771 if (argvec == error_mark_node)
13772 return error_mark_node;
13774 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13775 = build_template_info (tmpl, argvec);
13778 break;
13780 case TEMPLATE_PARM_INDEX:
13781 /* OK, now substitute the type of the non-type parameter. We
13782 couldn't do it earlier because it might be an auto parameter,
13783 and we wouldn't need to if we had an argument. */
13784 type = tsubst (type, args, complain, in_decl);
13785 r = reduce_template_parm_level (t, type, levels, args, complain);
13786 break;
13788 default:
13789 gcc_unreachable ();
13792 return r;
13795 case TREE_LIST:
13797 tree purpose, value, chain;
13799 if (t == void_list_node)
13800 return t;
13802 purpose = TREE_PURPOSE (t);
13803 if (purpose)
13805 purpose = tsubst (purpose, args, complain, in_decl);
13806 if (purpose == error_mark_node)
13807 return error_mark_node;
13809 value = TREE_VALUE (t);
13810 if (value)
13812 value = tsubst (value, args, complain, in_decl);
13813 if (value == error_mark_node)
13814 return error_mark_node;
13816 chain = TREE_CHAIN (t);
13817 if (chain && chain != void_type_node)
13819 chain = tsubst (chain, args, complain, in_decl);
13820 if (chain == error_mark_node)
13821 return error_mark_node;
13823 if (purpose == TREE_PURPOSE (t)
13824 && value == TREE_VALUE (t)
13825 && chain == TREE_CHAIN (t))
13826 return t;
13827 return hash_tree_cons (purpose, value, chain);
13830 case TREE_BINFO:
13831 /* We should never be tsubsting a binfo. */
13832 gcc_unreachable ();
13834 case TREE_VEC:
13835 /* A vector of template arguments. */
13836 gcc_assert (!type);
13837 return tsubst_template_args (t, args, complain, in_decl);
13839 case POINTER_TYPE:
13840 case REFERENCE_TYPE:
13842 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13843 return t;
13845 /* [temp.deduct]
13847 Type deduction may fail for any of the following
13848 reasons:
13850 -- Attempting to create a pointer to reference type.
13851 -- Attempting to create a reference to a reference type or
13852 a reference to void.
13854 Core issue 106 says that creating a reference to a reference
13855 during instantiation is no longer a cause for failure. We
13856 only enforce this check in strict C++98 mode. */
13857 if ((TREE_CODE (type) == REFERENCE_TYPE
13858 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13859 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13861 static location_t last_loc;
13863 /* We keep track of the last time we issued this error
13864 message to avoid spewing a ton of messages during a
13865 single bad template instantiation. */
13866 if (complain & tf_error
13867 && last_loc != input_location)
13869 if (VOID_TYPE_P (type))
13870 error ("forming reference to void");
13871 else if (code == POINTER_TYPE)
13872 error ("forming pointer to reference type %qT", type);
13873 else
13874 error ("forming reference to reference type %qT", type);
13875 last_loc = input_location;
13878 return error_mark_node;
13880 else if (TREE_CODE (type) == FUNCTION_TYPE
13881 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13882 || type_memfn_rqual (type) != REF_QUAL_NONE))
13884 if (complain & tf_error)
13886 if (code == POINTER_TYPE)
13887 error ("forming pointer to qualified function type %qT",
13888 type);
13889 else
13890 error ("forming reference to qualified function type %qT",
13891 type);
13893 return error_mark_node;
13895 else if (code == POINTER_TYPE)
13897 r = build_pointer_type (type);
13898 if (TREE_CODE (type) == METHOD_TYPE)
13899 r = build_ptrmemfunc_type (r);
13901 else if (TREE_CODE (type) == REFERENCE_TYPE)
13902 /* In C++0x, during template argument substitution, when there is an
13903 attempt to create a reference to a reference type, reference
13904 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13906 "If a template-argument for a template-parameter T names a type
13907 that is a reference to a type A, an attempt to create the type
13908 'lvalue reference to cv T' creates the type 'lvalue reference to
13909 A,' while an attempt to create the type type rvalue reference to
13910 cv T' creates the type T"
13912 r = cp_build_reference_type
13913 (TREE_TYPE (type),
13914 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13915 else
13916 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13917 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13919 if (r != error_mark_node)
13920 /* Will this ever be needed for TYPE_..._TO values? */
13921 layout_type (r);
13923 return r;
13925 case OFFSET_TYPE:
13927 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13928 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13930 /* [temp.deduct]
13932 Type deduction may fail for any of the following
13933 reasons:
13935 -- Attempting to create "pointer to member of T" when T
13936 is not a class type. */
13937 if (complain & tf_error)
13938 error ("creating pointer to member of non-class type %qT", r);
13939 return error_mark_node;
13941 if (TREE_CODE (type) == REFERENCE_TYPE)
13943 if (complain & tf_error)
13944 error ("creating pointer to member reference type %qT", type);
13945 return error_mark_node;
13947 if (VOID_TYPE_P (type))
13949 if (complain & tf_error)
13950 error ("creating pointer to member of type void");
13951 return error_mark_node;
13953 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13954 if (TREE_CODE (type) == FUNCTION_TYPE)
13956 /* The type of the implicit object parameter gets its
13957 cv-qualifiers from the FUNCTION_TYPE. */
13958 tree memptr;
13959 tree method_type
13960 = build_memfn_type (type, r, type_memfn_quals (type),
13961 type_memfn_rqual (type));
13962 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13963 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13964 complain);
13966 else
13967 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13968 cp_type_quals (t),
13969 complain);
13971 case FUNCTION_TYPE:
13972 case METHOD_TYPE:
13974 tree fntype;
13975 tree specs;
13976 fntype = tsubst_function_type (t, args, complain, in_decl);
13977 if (fntype == error_mark_node)
13978 return error_mark_node;
13980 /* Substitute the exception specification. */
13981 specs = tsubst_exception_specification (t, args, complain, in_decl,
13982 /*defer_ok*/fndecl_type);
13983 if (specs == error_mark_node)
13984 return error_mark_node;
13985 if (specs)
13986 fntype = build_exception_variant (fntype, specs);
13987 return fntype;
13989 case ARRAY_TYPE:
13991 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13992 if (domain == error_mark_node)
13993 return error_mark_node;
13995 /* As an optimization, we avoid regenerating the array type if
13996 it will obviously be the same as T. */
13997 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13998 return t;
14000 /* These checks should match the ones in create_array_type_for_decl.
14002 [temp.deduct]
14004 The deduction may fail for any of the following reasons:
14006 -- Attempting to create an array with an element type that
14007 is void, a function type, or a reference type, or [DR337]
14008 an abstract class type. */
14009 if (VOID_TYPE_P (type)
14010 || TREE_CODE (type) == FUNCTION_TYPE
14011 || (TREE_CODE (type) == ARRAY_TYPE
14012 && TYPE_DOMAIN (type) == NULL_TREE)
14013 || TREE_CODE (type) == REFERENCE_TYPE)
14015 if (complain & tf_error)
14016 error ("creating array of %qT", type);
14017 return error_mark_node;
14020 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14021 return error_mark_node;
14023 r = build_cplus_array_type (type, domain);
14025 if (TYPE_USER_ALIGN (t))
14027 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14028 TYPE_USER_ALIGN (r) = 1;
14031 return r;
14034 case TYPENAME_TYPE:
14036 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14037 in_decl, /*entering_scope=*/1);
14038 if (ctx == error_mark_node)
14039 return error_mark_node;
14041 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14042 complain, in_decl);
14043 if (f == error_mark_node)
14044 return error_mark_node;
14046 if (!MAYBE_CLASS_TYPE_P (ctx))
14048 if (complain & tf_error)
14049 error ("%qT is not a class, struct, or union type", ctx);
14050 return error_mark_node;
14052 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14054 /* Normally, make_typename_type does not require that the CTX
14055 have complete type in order to allow things like:
14057 template <class T> struct S { typename S<T>::X Y; };
14059 But, such constructs have already been resolved by this
14060 point, so here CTX really should have complete type, unless
14061 it's a partial instantiation. */
14062 ctx = complete_type (ctx);
14063 if (!COMPLETE_TYPE_P (ctx))
14065 if (complain & tf_error)
14066 cxx_incomplete_type_error (NULL_TREE, ctx);
14067 return error_mark_node;
14071 f = make_typename_type (ctx, f, typename_type,
14072 complain | tf_keep_type_decl);
14073 if (f == error_mark_node)
14074 return f;
14075 if (TREE_CODE (f) == TYPE_DECL)
14077 complain |= tf_ignore_bad_quals;
14078 f = TREE_TYPE (f);
14081 if (TREE_CODE (f) != TYPENAME_TYPE)
14083 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14085 if (complain & tf_error)
14086 error ("%qT resolves to %qT, which is not an enumeration type",
14087 t, f);
14088 else
14089 return error_mark_node;
14091 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14093 if (complain & tf_error)
14094 error ("%qT resolves to %qT, which is is not a class type",
14095 t, f);
14096 else
14097 return error_mark_node;
14101 return cp_build_qualified_type_real
14102 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14105 case UNBOUND_CLASS_TEMPLATE:
14107 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14108 in_decl, /*entering_scope=*/1);
14109 tree name = TYPE_IDENTIFIER (t);
14110 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14112 if (ctx == error_mark_node || name == error_mark_node)
14113 return error_mark_node;
14115 if (parm_list)
14116 parm_list = tsubst_template_parms (parm_list, args, complain);
14117 return make_unbound_class_template (ctx, name, parm_list, complain);
14120 case TYPEOF_TYPE:
14122 tree type;
14124 ++cp_unevaluated_operand;
14125 ++c_inhibit_evaluation_warnings;
14127 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14128 complain, in_decl,
14129 /*integral_constant_expression_p=*/false);
14131 --cp_unevaluated_operand;
14132 --c_inhibit_evaluation_warnings;
14134 type = finish_typeof (type);
14135 return cp_build_qualified_type_real (type,
14136 cp_type_quals (t)
14137 | cp_type_quals (type),
14138 complain);
14141 case DECLTYPE_TYPE:
14143 tree type;
14145 ++cp_unevaluated_operand;
14146 ++c_inhibit_evaluation_warnings;
14148 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14149 complain|tf_decltype, in_decl,
14150 /*function_p*/false,
14151 /*integral_constant_expression*/false);
14153 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14155 if (type == NULL_TREE)
14157 if (complain & tf_error)
14158 error ("empty initializer in lambda init-capture");
14159 type = error_mark_node;
14161 else if (TREE_CODE (type) == TREE_LIST)
14162 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14165 --cp_unevaluated_operand;
14166 --c_inhibit_evaluation_warnings;
14168 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14169 type = lambda_capture_field_type (type,
14170 DECLTYPE_FOR_INIT_CAPTURE (t),
14171 DECLTYPE_FOR_REF_CAPTURE (t));
14172 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14173 type = lambda_proxy_type (type);
14174 else
14176 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14177 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14178 && EXPR_P (type))
14179 /* In a template ~id could be either a complement expression
14180 or an unqualified-id naming a destructor; if instantiating
14181 it produces an expression, it's not an id-expression or
14182 member access. */
14183 id = false;
14184 type = finish_decltype_type (type, id, complain);
14186 return cp_build_qualified_type_real (type,
14187 cp_type_quals (t)
14188 | cp_type_quals (type),
14189 complain | tf_ignore_bad_quals);
14192 case UNDERLYING_TYPE:
14194 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14195 complain, in_decl);
14196 return finish_underlying_type (type);
14199 case TYPE_ARGUMENT_PACK:
14200 case NONTYPE_ARGUMENT_PACK:
14202 tree r;
14204 if (code == NONTYPE_ARGUMENT_PACK)
14205 r = make_node (code);
14206 else
14207 r = cxx_make_type (code);
14209 tree pack_args = ARGUMENT_PACK_ARGS (t);
14210 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14211 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14213 return r;
14216 case VOID_CST:
14217 case INTEGER_CST:
14218 case REAL_CST:
14219 case STRING_CST:
14220 case PLUS_EXPR:
14221 case MINUS_EXPR:
14222 case NEGATE_EXPR:
14223 case NOP_EXPR:
14224 case INDIRECT_REF:
14225 case ADDR_EXPR:
14226 case CALL_EXPR:
14227 case ARRAY_REF:
14228 case SCOPE_REF:
14229 /* We should use one of the expression tsubsts for these codes. */
14230 gcc_unreachable ();
14232 default:
14233 sorry ("use of %qs in template", get_tree_code_name (code));
14234 return error_mark_node;
14238 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14239 expression on the left-hand side of the "." or "->" operator. A
14240 baselink indicates a function from a base class. Both the
14241 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14242 bases of the template class, rather than the instantiated class.
14243 In addition, lookups that were not ambiguous before may be
14244 ambiguous now. Therefore, we perform the lookup again. */
14246 static tree
14247 tsubst_baselink (tree baselink, tree object_type,
14248 tree args, tsubst_flags_t complain, tree in_decl)
14250 bool qualified = BASELINK_QUALIFIED_P (baselink);
14252 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14253 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14255 tree optype = BASELINK_OPTYPE (baselink);
14256 optype = tsubst (optype, args, complain, in_decl);
14258 tree template_args = NULL_TREE;
14259 bool template_id_p = false;
14260 tree fns = BASELINK_FUNCTIONS (baselink);
14261 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14263 template_id_p = true;
14264 template_args = TREE_OPERAND (fns, 1);
14265 fns = TREE_OPERAND (fns, 0);
14266 if (template_args)
14267 template_args = tsubst_template_args (template_args, args,
14268 complain, in_decl);
14271 tree name = OVL_NAME (fns);
14272 if (IDENTIFIER_CONV_OP_P (name))
14273 name = make_conv_op_name (optype);
14275 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14276 if (!baselink)
14278 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14279 error ("cannot call constructor %<%T::%D%> directly",
14280 qualifying_scope, name);
14281 return error_mark_node;
14284 /* If lookup found a single function, mark it as used at this point.
14285 (If it lookup found multiple functions the one selected later by
14286 overload resolution will be marked as used at that point.) */
14287 if (BASELINK_P (baselink))
14288 fns = BASELINK_FUNCTIONS (baselink);
14289 if (!template_id_p && !really_overloaded_fn (fns)
14290 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14291 return error_mark_node;
14293 if (BASELINK_P (baselink))
14295 /* Add back the template arguments, if present. */
14296 if (template_id_p)
14297 BASELINK_FUNCTIONS (baselink)
14298 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14299 BASELINK_FUNCTIONS (baselink), template_args);
14301 /* Update the conversion operator type. */
14302 BASELINK_OPTYPE (baselink) = optype;
14305 if (!object_type)
14306 object_type = current_class_type;
14308 if (qualified || name == complete_dtor_identifier)
14310 baselink = adjust_result_of_qualified_name_lookup (baselink,
14311 qualifying_scope,
14312 object_type);
14313 if (!qualified)
14314 /* We need to call adjust_result_of_qualified_name_lookup in case the
14315 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14316 so that we still get virtual function binding. */
14317 BASELINK_QUALIFIED_P (baselink) = false;
14320 return baselink;
14323 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14324 true if the qualified-id will be a postfix-expression in-and-of
14325 itself; false if more of the postfix-expression follows the
14326 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14327 of "&". */
14329 static tree
14330 tsubst_qualified_id (tree qualified_id, tree args,
14331 tsubst_flags_t complain, tree in_decl,
14332 bool done, bool address_p)
14334 tree expr;
14335 tree scope;
14336 tree name;
14337 bool is_template;
14338 tree template_args;
14339 location_t loc = UNKNOWN_LOCATION;
14341 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14343 /* Figure out what name to look up. */
14344 name = TREE_OPERAND (qualified_id, 1);
14345 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14347 is_template = true;
14348 loc = EXPR_LOCATION (name);
14349 template_args = TREE_OPERAND (name, 1);
14350 if (template_args)
14351 template_args = tsubst_template_args (template_args, args,
14352 complain, in_decl);
14353 if (template_args == error_mark_node)
14354 return error_mark_node;
14355 name = TREE_OPERAND (name, 0);
14357 else
14359 is_template = false;
14360 template_args = NULL_TREE;
14363 /* Substitute into the qualifying scope. When there are no ARGS, we
14364 are just trying to simplify a non-dependent expression. In that
14365 case the qualifying scope may be dependent, and, in any case,
14366 substituting will not help. */
14367 scope = TREE_OPERAND (qualified_id, 0);
14368 if (args)
14370 scope = tsubst (scope, args, complain, in_decl);
14371 expr = tsubst_copy (name, args, complain, in_decl);
14373 else
14374 expr = name;
14376 if (dependent_scope_p (scope))
14378 if (is_template)
14379 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14380 tree r = build_qualified_name (NULL_TREE, scope, expr,
14381 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14382 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14383 return r;
14386 if (!BASELINK_P (name) && !DECL_P (expr))
14388 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14390 /* A BIT_NOT_EXPR is used to represent a destructor. */
14391 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14393 error ("qualifying type %qT does not match destructor name ~%qT",
14394 scope, TREE_OPERAND (expr, 0));
14395 expr = error_mark_node;
14397 else
14398 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14399 /*is_type_p=*/0, false);
14401 else
14402 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14403 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14404 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14406 if (complain & tf_error)
14408 error ("dependent-name %qE is parsed as a non-type, but "
14409 "instantiation yields a type", qualified_id);
14410 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14412 return error_mark_node;
14416 if (DECL_P (expr))
14418 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14419 scope);
14420 /* Remember that there was a reference to this entity. */
14421 if (!mark_used (expr, complain) && !(complain & tf_error))
14422 return error_mark_node;
14425 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14427 if (complain & tf_error)
14428 qualified_name_lookup_error (scope,
14429 TREE_OPERAND (qualified_id, 1),
14430 expr, input_location);
14431 return error_mark_node;
14434 if (is_template)
14436 if (variable_template_p (expr))
14437 expr = lookup_and_finish_template_variable (expr, template_args,
14438 complain);
14439 else
14440 expr = lookup_template_function (expr, template_args);
14443 if (expr == error_mark_node && complain & tf_error)
14444 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14445 expr, input_location);
14446 else if (TYPE_P (scope))
14448 expr = (adjust_result_of_qualified_name_lookup
14449 (expr, scope, current_nonlambda_class_type ()));
14450 expr = (finish_qualified_id_expr
14451 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14452 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14453 /*template_arg_p=*/false, complain));
14456 /* Expressions do not generally have reference type. */
14457 if (TREE_CODE (expr) != SCOPE_REF
14458 /* However, if we're about to form a pointer-to-member, we just
14459 want the referenced member referenced. */
14460 && TREE_CODE (expr) != OFFSET_REF)
14461 expr = convert_from_reference (expr);
14463 if (REF_PARENTHESIZED_P (qualified_id))
14464 expr = force_paren_expr (expr);
14466 return expr;
14469 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14470 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14471 for tsubst. */
14473 static tree
14474 tsubst_init (tree init, tree decl, tree args,
14475 tsubst_flags_t complain, tree in_decl)
14477 if (!init)
14478 return NULL_TREE;
14480 init = tsubst_expr (init, args, complain, in_decl, false);
14482 if (!init && TREE_TYPE (decl) != error_mark_node)
14484 /* If we had an initializer but it
14485 instantiated to nothing,
14486 value-initialize the object. This will
14487 only occur when the initializer was a
14488 pack expansion where the parameter packs
14489 used in that expansion were of length
14490 zero. */
14491 init = build_value_init (TREE_TYPE (decl),
14492 complain);
14493 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14494 init = get_target_expr_sfinae (init, complain);
14495 if (TREE_CODE (init) == TARGET_EXPR)
14496 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14499 return init;
14502 /* Like tsubst, but deals with expressions. This function just replaces
14503 template parms; to finish processing the resultant expression, use
14504 tsubst_copy_and_build or tsubst_expr. */
14506 static tree
14507 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14509 enum tree_code code;
14510 tree r;
14512 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14513 return t;
14515 code = TREE_CODE (t);
14517 switch (code)
14519 case PARM_DECL:
14520 r = retrieve_local_specialization (t);
14522 if (r == NULL_TREE)
14524 /* We get here for a use of 'this' in an NSDMI. */
14525 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14526 return current_class_ptr;
14528 /* This can happen for a parameter name used later in a function
14529 declaration (such as in a late-specified return type). Just
14530 make a dummy decl, since it's only used for its type. */
14531 gcc_assert (cp_unevaluated_operand != 0);
14532 r = tsubst_decl (t, args, complain);
14533 /* Give it the template pattern as its context; its true context
14534 hasn't been instantiated yet and this is good enough for
14535 mangling. */
14536 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14539 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14540 r = ARGUMENT_PACK_SELECT_ARG (r);
14541 if (!mark_used (r, complain) && !(complain & tf_error))
14542 return error_mark_node;
14543 return r;
14545 case CONST_DECL:
14547 tree enum_type;
14548 tree v;
14550 if (DECL_TEMPLATE_PARM_P (t))
14551 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14552 /* There is no need to substitute into namespace-scope
14553 enumerators. */
14554 if (DECL_NAMESPACE_SCOPE_P (t))
14555 return t;
14556 /* If ARGS is NULL, then T is known to be non-dependent. */
14557 if (args == NULL_TREE)
14558 return scalar_constant_value (t);
14560 /* Unfortunately, we cannot just call lookup_name here.
14561 Consider:
14563 template <int I> int f() {
14564 enum E { a = I };
14565 struct S { void g() { E e = a; } };
14568 When we instantiate f<7>::S::g(), say, lookup_name is not
14569 clever enough to find f<7>::a. */
14570 enum_type
14571 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14572 /*entering_scope=*/0);
14574 for (v = TYPE_VALUES (enum_type);
14575 v != NULL_TREE;
14576 v = TREE_CHAIN (v))
14577 if (TREE_PURPOSE (v) == DECL_NAME (t))
14578 return TREE_VALUE (v);
14580 /* We didn't find the name. That should never happen; if
14581 name-lookup found it during preliminary parsing, we
14582 should find it again here during instantiation. */
14583 gcc_unreachable ();
14585 return t;
14587 case FIELD_DECL:
14588 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14590 /* Check for a local specialization set up by
14591 tsubst_pack_expansion. */
14592 if (tree r = retrieve_local_specialization (t))
14594 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14595 r = ARGUMENT_PACK_SELECT_ARG (r);
14596 return r;
14599 /* When retrieving a capture pack from a generic lambda, remove the
14600 lambda call op's own template argument list from ARGS. Only the
14601 template arguments active for the closure type should be used to
14602 retrieve the pack specialization. */
14603 if (LAMBDA_FUNCTION_P (current_function_decl)
14604 && (template_class_depth (DECL_CONTEXT (t))
14605 != TMPL_ARGS_DEPTH (args)))
14606 args = strip_innermost_template_args (args, 1);
14608 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14609 tsubst_decl put in the hash table. */
14610 return retrieve_specialization (t, args, 0);
14613 if (DECL_CONTEXT (t))
14615 tree ctx;
14617 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14618 /*entering_scope=*/1);
14619 if (ctx != DECL_CONTEXT (t))
14621 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14622 if (!r)
14624 if (complain & tf_error)
14625 error ("using invalid field %qD", t);
14626 return error_mark_node;
14628 return r;
14632 return t;
14634 case VAR_DECL:
14635 case FUNCTION_DECL:
14636 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14637 r = tsubst (t, args, complain, in_decl);
14638 else if (local_variable_p (t)
14639 && uses_template_parms (DECL_CONTEXT (t)))
14641 r = retrieve_local_specialization (t);
14642 if (r == NULL_TREE)
14644 /* First try name lookup to find the instantiation. */
14645 r = lookup_name (DECL_NAME (t));
14646 if (r && !is_capture_proxy (r))
14648 /* Make sure that the one we found is the one we want. */
14649 tree ctx = DECL_CONTEXT (t);
14650 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14651 ctx = tsubst (ctx, args, complain, in_decl);
14652 if (ctx != DECL_CONTEXT (r))
14653 r = NULL_TREE;
14656 if (r)
14657 /* OK */;
14658 else
14660 /* This can happen for a variable used in a
14661 late-specified return type of a local lambda, or for a
14662 local static or constant. Building a new VAR_DECL
14663 should be OK in all those cases. */
14664 r = tsubst_decl (t, args, complain);
14665 if (local_specializations)
14666 /* Avoid infinite recursion (79640). */
14667 register_local_specialization (r, t);
14668 if (decl_maybe_constant_var_p (r))
14670 /* We can't call cp_finish_decl, so handle the
14671 initializer by hand. */
14672 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14673 complain, in_decl);
14674 if (!processing_template_decl)
14675 init = maybe_constant_init (init);
14676 if (processing_template_decl
14677 ? potential_constant_expression (init)
14678 : reduced_constant_expression_p (init))
14679 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14680 = TREE_CONSTANT (r) = true;
14681 DECL_INITIAL (r) = init;
14682 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14683 TREE_TYPE (r)
14684 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14685 complain, adc_variable_type);
14687 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14688 || decl_constant_var_p (r)
14689 || errorcount || sorrycount);
14690 if (!processing_template_decl
14691 && !TREE_STATIC (r))
14692 r = process_outer_var_ref (r, complain);
14694 /* Remember this for subsequent uses. */
14695 if (local_specializations)
14696 register_local_specialization (r, t);
14699 else
14700 r = t;
14701 if (!mark_used (r, complain))
14702 return error_mark_node;
14703 return r;
14705 case NAMESPACE_DECL:
14706 return t;
14708 case OVERLOAD:
14709 /* An OVERLOAD will always be a non-dependent overload set; an
14710 overload set from function scope will just be represented with an
14711 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14712 gcc_assert (!uses_template_parms (t));
14713 /* We must have marked any lookups as persistent. */
14714 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14715 return t;
14717 case BASELINK:
14718 return tsubst_baselink (t, current_nonlambda_class_type (),
14719 args, complain, in_decl);
14721 case TEMPLATE_DECL:
14722 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14723 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14724 args, complain, in_decl);
14725 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14726 return tsubst (t, args, complain, in_decl);
14727 else if (DECL_CLASS_SCOPE_P (t)
14728 && uses_template_parms (DECL_CONTEXT (t)))
14730 /* Template template argument like the following example need
14731 special treatment:
14733 template <template <class> class TT> struct C {};
14734 template <class T> struct D {
14735 template <class U> struct E {};
14736 C<E> c; // #1
14738 D<int> d; // #2
14740 We are processing the template argument `E' in #1 for
14741 the template instantiation #2. Originally, `E' is a
14742 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14743 have to substitute this with one having context `D<int>'. */
14745 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14746 if (dependent_scope_p (context))
14748 /* When rewriting a constructor into a deduction guide, a
14749 non-dependent name can become dependent, so memtmpl<args>
14750 becomes context::template memtmpl<args>. */
14751 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14752 return build_qualified_name (type, context, DECL_NAME (t),
14753 /*template*/true);
14755 return lookup_field (context, DECL_NAME(t), 0, false);
14757 else
14758 /* Ordinary template template argument. */
14759 return t;
14761 case CAST_EXPR:
14762 case REINTERPRET_CAST_EXPR:
14763 case CONST_CAST_EXPR:
14764 case STATIC_CAST_EXPR:
14765 case DYNAMIC_CAST_EXPR:
14766 case IMPLICIT_CONV_EXPR:
14767 case CONVERT_EXPR:
14768 case NOP_EXPR:
14770 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14771 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14772 return build1 (code, type, op0);
14775 case SIZEOF_EXPR:
14776 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14777 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14779 tree expanded, op = TREE_OPERAND (t, 0);
14780 int len = 0;
14782 if (SIZEOF_EXPR_TYPE_P (t))
14783 op = TREE_TYPE (op);
14785 ++cp_unevaluated_operand;
14786 ++c_inhibit_evaluation_warnings;
14787 /* We only want to compute the number of arguments. */
14788 if (PACK_EXPANSION_P (op))
14789 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14790 else
14791 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14792 args, complain, in_decl);
14793 --cp_unevaluated_operand;
14794 --c_inhibit_evaluation_warnings;
14796 if (TREE_CODE (expanded) == TREE_VEC)
14798 len = TREE_VEC_LENGTH (expanded);
14799 /* Set TREE_USED for the benefit of -Wunused. */
14800 for (int i = 0; i < len; i++)
14801 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14802 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14805 if (expanded == error_mark_node)
14806 return error_mark_node;
14807 else if (PACK_EXPANSION_P (expanded)
14808 || (TREE_CODE (expanded) == TREE_VEC
14809 && pack_expansion_args_count (expanded)))
14812 if (PACK_EXPANSION_P (expanded))
14813 /* OK. */;
14814 else if (TREE_VEC_LENGTH (expanded) == 1)
14815 expanded = TREE_VEC_ELT (expanded, 0);
14816 else
14817 expanded = make_argument_pack (expanded);
14819 if (TYPE_P (expanded))
14820 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14821 complain & tf_error);
14822 else
14823 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14824 complain & tf_error);
14826 else
14827 return build_int_cst (size_type_node, len);
14829 if (SIZEOF_EXPR_TYPE_P (t))
14831 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14832 args, complain, in_decl);
14833 r = build1 (NOP_EXPR, r, error_mark_node);
14834 r = build1 (SIZEOF_EXPR,
14835 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14836 SIZEOF_EXPR_TYPE_P (r) = 1;
14837 return r;
14839 /* Fall through */
14841 case INDIRECT_REF:
14842 case NEGATE_EXPR:
14843 case TRUTH_NOT_EXPR:
14844 case BIT_NOT_EXPR:
14845 case ADDR_EXPR:
14846 case UNARY_PLUS_EXPR: /* Unary + */
14847 case ALIGNOF_EXPR:
14848 case AT_ENCODE_EXPR:
14849 case ARROW_EXPR:
14850 case THROW_EXPR:
14851 case TYPEID_EXPR:
14852 case REALPART_EXPR:
14853 case IMAGPART_EXPR:
14854 case PAREN_EXPR:
14856 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14857 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14858 return build1 (code, type, op0);
14861 case COMPONENT_REF:
14863 tree object;
14864 tree name;
14866 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14867 name = TREE_OPERAND (t, 1);
14868 if (TREE_CODE (name) == BIT_NOT_EXPR)
14870 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14871 complain, in_decl);
14872 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14874 else if (TREE_CODE (name) == SCOPE_REF
14875 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14877 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14878 complain, in_decl);
14879 name = TREE_OPERAND (name, 1);
14880 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14881 complain, in_decl);
14882 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14883 name = build_qualified_name (/*type=*/NULL_TREE,
14884 base, name,
14885 /*template_p=*/false);
14887 else if (BASELINK_P (name))
14888 name = tsubst_baselink (name,
14889 non_reference (TREE_TYPE (object)),
14890 args, complain,
14891 in_decl);
14892 else
14893 name = tsubst_copy (name, args, complain, in_decl);
14894 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14897 case PLUS_EXPR:
14898 case MINUS_EXPR:
14899 case MULT_EXPR:
14900 case TRUNC_DIV_EXPR:
14901 case CEIL_DIV_EXPR:
14902 case FLOOR_DIV_EXPR:
14903 case ROUND_DIV_EXPR:
14904 case EXACT_DIV_EXPR:
14905 case BIT_AND_EXPR:
14906 case BIT_IOR_EXPR:
14907 case BIT_XOR_EXPR:
14908 case TRUNC_MOD_EXPR:
14909 case FLOOR_MOD_EXPR:
14910 case TRUTH_ANDIF_EXPR:
14911 case TRUTH_ORIF_EXPR:
14912 case TRUTH_AND_EXPR:
14913 case TRUTH_OR_EXPR:
14914 case RSHIFT_EXPR:
14915 case LSHIFT_EXPR:
14916 case RROTATE_EXPR:
14917 case LROTATE_EXPR:
14918 case EQ_EXPR:
14919 case NE_EXPR:
14920 case MAX_EXPR:
14921 case MIN_EXPR:
14922 case LE_EXPR:
14923 case GE_EXPR:
14924 case LT_EXPR:
14925 case GT_EXPR:
14926 case COMPOUND_EXPR:
14927 case DOTSTAR_EXPR:
14928 case MEMBER_REF:
14929 case PREDECREMENT_EXPR:
14930 case PREINCREMENT_EXPR:
14931 case POSTDECREMENT_EXPR:
14932 case POSTINCREMENT_EXPR:
14934 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14935 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14936 return build_nt (code, op0, op1);
14939 case SCOPE_REF:
14941 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14942 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14943 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14944 QUALIFIED_NAME_IS_TEMPLATE (t));
14947 case ARRAY_REF:
14949 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14950 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14951 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14954 case CALL_EXPR:
14956 int n = VL_EXP_OPERAND_LENGTH (t);
14957 tree result = build_vl_exp (CALL_EXPR, n);
14958 int i;
14959 for (i = 0; i < n; i++)
14960 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14961 complain, in_decl);
14962 return result;
14965 case COND_EXPR:
14966 case MODOP_EXPR:
14967 case PSEUDO_DTOR_EXPR:
14968 case VEC_PERM_EXPR:
14970 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14971 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14972 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14973 r = build_nt (code, op0, op1, op2);
14974 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14975 return r;
14978 case NEW_EXPR:
14980 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14981 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14982 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14983 r = build_nt (code, op0, op1, op2);
14984 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14985 return r;
14988 case DELETE_EXPR:
14990 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14991 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14992 r = build_nt (code, op0, op1);
14993 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14994 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14995 return r;
14998 case TEMPLATE_ID_EXPR:
15000 /* Substituted template arguments */
15001 tree fn = TREE_OPERAND (t, 0);
15002 tree targs = TREE_OPERAND (t, 1);
15004 fn = tsubst_copy (fn, args, complain, in_decl);
15005 if (targs)
15006 targs = tsubst_template_args (targs, args, complain, in_decl);
15008 return lookup_template_function (fn, targs);
15011 case TREE_LIST:
15013 tree purpose, value, chain;
15015 if (t == void_list_node)
15016 return t;
15018 purpose = TREE_PURPOSE (t);
15019 if (purpose)
15020 purpose = tsubst_copy (purpose, args, complain, in_decl);
15021 value = TREE_VALUE (t);
15022 if (value)
15023 value = tsubst_copy (value, args, complain, in_decl);
15024 chain = TREE_CHAIN (t);
15025 if (chain && chain != void_type_node)
15026 chain = tsubst_copy (chain, args, complain, in_decl);
15027 if (purpose == TREE_PURPOSE (t)
15028 && value == TREE_VALUE (t)
15029 && chain == TREE_CHAIN (t))
15030 return t;
15031 return tree_cons (purpose, value, chain);
15034 case RECORD_TYPE:
15035 case UNION_TYPE:
15036 case ENUMERAL_TYPE:
15037 case INTEGER_TYPE:
15038 case TEMPLATE_TYPE_PARM:
15039 case TEMPLATE_TEMPLATE_PARM:
15040 case BOUND_TEMPLATE_TEMPLATE_PARM:
15041 case TEMPLATE_PARM_INDEX:
15042 case POINTER_TYPE:
15043 case REFERENCE_TYPE:
15044 case OFFSET_TYPE:
15045 case FUNCTION_TYPE:
15046 case METHOD_TYPE:
15047 case ARRAY_TYPE:
15048 case TYPENAME_TYPE:
15049 case UNBOUND_CLASS_TEMPLATE:
15050 case TYPEOF_TYPE:
15051 case DECLTYPE_TYPE:
15052 case TYPE_DECL:
15053 return tsubst (t, args, complain, in_decl);
15055 case USING_DECL:
15056 t = DECL_NAME (t);
15057 /* Fall through. */
15058 case IDENTIFIER_NODE:
15059 if (IDENTIFIER_CONV_OP_P (t))
15061 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15062 return make_conv_op_name (new_type);
15064 else
15065 return t;
15067 case CONSTRUCTOR:
15068 /* This is handled by tsubst_copy_and_build. */
15069 gcc_unreachable ();
15071 case VA_ARG_EXPR:
15073 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15074 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15075 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15078 case CLEANUP_POINT_EXPR:
15079 /* We shouldn't have built any of these during initial template
15080 generation. Instead, they should be built during instantiation
15081 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15082 gcc_unreachable ();
15084 case OFFSET_REF:
15086 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15087 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15088 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15089 r = build2 (code, type, op0, op1);
15090 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15091 if (!mark_used (TREE_OPERAND (r, 1), complain)
15092 && !(complain & tf_error))
15093 return error_mark_node;
15094 return r;
15097 case EXPR_PACK_EXPANSION:
15098 error ("invalid use of pack expansion expression");
15099 return error_mark_node;
15101 case NONTYPE_ARGUMENT_PACK:
15102 error ("use %<...%> to expand argument pack");
15103 return error_mark_node;
15105 case VOID_CST:
15106 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15107 return t;
15109 case INTEGER_CST:
15110 case REAL_CST:
15111 case STRING_CST:
15112 case COMPLEX_CST:
15114 /* Instantiate any typedefs in the type. */
15115 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15116 r = fold_convert (type, t);
15117 gcc_assert (TREE_CODE (r) == code);
15118 return r;
15121 case PTRMEM_CST:
15122 /* These can sometimes show up in a partial instantiation, but never
15123 involve template parms. */
15124 gcc_assert (!uses_template_parms (t));
15125 return t;
15127 case UNARY_LEFT_FOLD_EXPR:
15128 return tsubst_unary_left_fold (t, args, complain, in_decl);
15129 case UNARY_RIGHT_FOLD_EXPR:
15130 return tsubst_unary_right_fold (t, args, complain, in_decl);
15131 case BINARY_LEFT_FOLD_EXPR:
15132 return tsubst_binary_left_fold (t, args, complain, in_decl);
15133 case BINARY_RIGHT_FOLD_EXPR:
15134 return tsubst_binary_right_fold (t, args, complain, in_decl);
15135 case PREDICT_EXPR:
15136 return t;
15138 default:
15139 /* We shouldn't get here, but keep going if !flag_checking. */
15140 if (flag_checking)
15141 gcc_unreachable ();
15142 return t;
15146 /* Helper function for tsubst_omp_clauses, used for instantiation of
15147 OMP_CLAUSE_DECL of clauses. */
15149 static tree
15150 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15151 tree in_decl)
15153 if (decl == NULL_TREE)
15154 return NULL_TREE;
15156 /* Handle an OpenMP array section represented as a TREE_LIST (or
15157 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15158 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15159 TREE_LIST. We can handle it exactly the same as an array section
15160 (purpose, value, and a chain), even though the nomenclature
15161 (low_bound, length, etc) is different. */
15162 if (TREE_CODE (decl) == TREE_LIST)
15164 tree low_bound
15165 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15166 /*integral_constant_expression_p=*/false);
15167 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15168 /*integral_constant_expression_p=*/false);
15169 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15170 in_decl);
15171 if (TREE_PURPOSE (decl) == low_bound
15172 && TREE_VALUE (decl) == length
15173 && TREE_CHAIN (decl) == chain)
15174 return decl;
15175 tree ret = tree_cons (low_bound, length, chain);
15176 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15177 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15178 return ret;
15180 tree ret = tsubst_expr (decl, args, complain, in_decl,
15181 /*integral_constant_expression_p=*/false);
15182 /* Undo convert_from_reference tsubst_expr could have called. */
15183 if (decl
15184 && REFERENCE_REF_P (ret)
15185 && !REFERENCE_REF_P (decl))
15186 ret = TREE_OPERAND (ret, 0);
15187 return ret;
15190 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15192 static tree
15193 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15194 tree args, tsubst_flags_t complain, tree in_decl)
15196 tree new_clauses = NULL_TREE, nc, oc;
15197 tree linear_no_step = NULL_TREE;
15199 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15201 nc = copy_node (oc);
15202 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15203 new_clauses = nc;
15205 switch (OMP_CLAUSE_CODE (nc))
15207 case OMP_CLAUSE_LASTPRIVATE:
15208 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15210 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15211 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15212 in_decl, /*integral_constant_expression_p=*/false);
15213 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15214 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15216 /* FALLTHRU */
15217 case OMP_CLAUSE_PRIVATE:
15218 case OMP_CLAUSE_SHARED:
15219 case OMP_CLAUSE_FIRSTPRIVATE:
15220 case OMP_CLAUSE_COPYIN:
15221 case OMP_CLAUSE_COPYPRIVATE:
15222 case OMP_CLAUSE_UNIFORM:
15223 case OMP_CLAUSE_DEPEND:
15224 case OMP_CLAUSE_FROM:
15225 case OMP_CLAUSE_TO:
15226 case OMP_CLAUSE_MAP:
15227 case OMP_CLAUSE_USE_DEVICE_PTR:
15228 case OMP_CLAUSE_IS_DEVICE_PTR:
15229 OMP_CLAUSE_DECL (nc)
15230 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15231 in_decl);
15232 break;
15233 case OMP_CLAUSE_TILE:
15234 case OMP_CLAUSE_IF:
15235 case OMP_CLAUSE_NUM_THREADS:
15236 case OMP_CLAUSE_SCHEDULE:
15237 case OMP_CLAUSE_COLLAPSE:
15238 case OMP_CLAUSE_FINAL:
15239 case OMP_CLAUSE_DEVICE:
15240 case OMP_CLAUSE_DIST_SCHEDULE:
15241 case OMP_CLAUSE_NUM_TEAMS:
15242 case OMP_CLAUSE_THREAD_LIMIT:
15243 case OMP_CLAUSE_SAFELEN:
15244 case OMP_CLAUSE_SIMDLEN:
15245 case OMP_CLAUSE_NUM_TASKS:
15246 case OMP_CLAUSE_GRAINSIZE:
15247 case OMP_CLAUSE_PRIORITY:
15248 case OMP_CLAUSE_ORDERED:
15249 case OMP_CLAUSE_HINT:
15250 case OMP_CLAUSE_NUM_GANGS:
15251 case OMP_CLAUSE_NUM_WORKERS:
15252 case OMP_CLAUSE_VECTOR_LENGTH:
15253 case OMP_CLAUSE_WORKER:
15254 case OMP_CLAUSE_VECTOR:
15255 case OMP_CLAUSE_ASYNC:
15256 case OMP_CLAUSE_WAIT:
15257 OMP_CLAUSE_OPERAND (nc, 0)
15258 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15259 in_decl, /*integral_constant_expression_p=*/false);
15260 break;
15261 case OMP_CLAUSE_REDUCTION:
15262 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15264 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15265 if (TREE_CODE (placeholder) == SCOPE_REF)
15267 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15268 complain, in_decl);
15269 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15270 = build_qualified_name (NULL_TREE, scope,
15271 TREE_OPERAND (placeholder, 1),
15272 false);
15274 else
15275 gcc_assert (identifier_p (placeholder));
15277 OMP_CLAUSE_DECL (nc)
15278 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15279 in_decl);
15280 break;
15281 case OMP_CLAUSE_GANG:
15282 case OMP_CLAUSE_ALIGNED:
15283 OMP_CLAUSE_DECL (nc)
15284 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15285 in_decl);
15286 OMP_CLAUSE_OPERAND (nc, 1)
15287 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15288 in_decl, /*integral_constant_expression_p=*/false);
15289 break;
15290 case OMP_CLAUSE_LINEAR:
15291 OMP_CLAUSE_DECL (nc)
15292 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15293 in_decl);
15294 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15296 gcc_assert (!linear_no_step);
15297 linear_no_step = nc;
15299 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15300 OMP_CLAUSE_LINEAR_STEP (nc)
15301 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15302 complain, in_decl);
15303 else
15304 OMP_CLAUSE_LINEAR_STEP (nc)
15305 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15306 in_decl,
15307 /*integral_constant_expression_p=*/false);
15308 break;
15309 case OMP_CLAUSE_NOWAIT:
15310 case OMP_CLAUSE_DEFAULT:
15311 case OMP_CLAUSE_UNTIED:
15312 case OMP_CLAUSE_MERGEABLE:
15313 case OMP_CLAUSE_INBRANCH:
15314 case OMP_CLAUSE_NOTINBRANCH:
15315 case OMP_CLAUSE_PROC_BIND:
15316 case OMP_CLAUSE_FOR:
15317 case OMP_CLAUSE_PARALLEL:
15318 case OMP_CLAUSE_SECTIONS:
15319 case OMP_CLAUSE_TASKGROUP:
15320 case OMP_CLAUSE_NOGROUP:
15321 case OMP_CLAUSE_THREADS:
15322 case OMP_CLAUSE_SIMD:
15323 case OMP_CLAUSE_DEFAULTMAP:
15324 case OMP_CLAUSE_INDEPENDENT:
15325 case OMP_CLAUSE_AUTO:
15326 case OMP_CLAUSE_SEQ:
15327 break;
15328 default:
15329 gcc_unreachable ();
15331 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15332 switch (OMP_CLAUSE_CODE (nc))
15334 case OMP_CLAUSE_SHARED:
15335 case OMP_CLAUSE_PRIVATE:
15336 case OMP_CLAUSE_FIRSTPRIVATE:
15337 case OMP_CLAUSE_LASTPRIVATE:
15338 case OMP_CLAUSE_COPYPRIVATE:
15339 case OMP_CLAUSE_LINEAR:
15340 case OMP_CLAUSE_REDUCTION:
15341 case OMP_CLAUSE_USE_DEVICE_PTR:
15342 case OMP_CLAUSE_IS_DEVICE_PTR:
15343 /* tsubst_expr on SCOPE_REF results in returning
15344 finish_non_static_data_member result. Undo that here. */
15345 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15346 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15347 == IDENTIFIER_NODE))
15349 tree t = OMP_CLAUSE_DECL (nc);
15350 tree v = t;
15351 while (v)
15352 switch (TREE_CODE (v))
15354 case COMPONENT_REF:
15355 case MEM_REF:
15356 case INDIRECT_REF:
15357 CASE_CONVERT:
15358 case POINTER_PLUS_EXPR:
15359 v = TREE_OPERAND (v, 0);
15360 continue;
15361 case PARM_DECL:
15362 if (DECL_CONTEXT (v) == current_function_decl
15363 && DECL_ARTIFICIAL (v)
15364 && DECL_NAME (v) == this_identifier)
15365 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15366 /* FALLTHRU */
15367 default:
15368 v = NULL_TREE;
15369 break;
15372 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15373 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15374 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15375 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15376 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15378 tree decl = OMP_CLAUSE_DECL (nc);
15379 if (VAR_P (decl))
15381 retrofit_lang_decl (decl);
15382 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15385 break;
15386 default:
15387 break;
15391 new_clauses = nreverse (new_clauses);
15392 if (ort != C_ORT_OMP_DECLARE_SIMD)
15394 new_clauses = finish_omp_clauses (new_clauses, ort);
15395 if (linear_no_step)
15396 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15397 if (nc == linear_no_step)
15399 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15400 break;
15403 return new_clauses;
15406 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15408 static tree
15409 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15410 tree in_decl)
15412 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15414 tree purpose, value, chain;
15416 if (t == NULL)
15417 return t;
15419 if (TREE_CODE (t) != TREE_LIST)
15420 return tsubst_copy_and_build (t, args, complain, in_decl,
15421 /*function_p=*/false,
15422 /*integral_constant_expression_p=*/false);
15424 if (t == void_list_node)
15425 return t;
15427 purpose = TREE_PURPOSE (t);
15428 if (purpose)
15429 purpose = RECUR (purpose);
15430 value = TREE_VALUE (t);
15431 if (value)
15433 if (TREE_CODE (value) != LABEL_DECL)
15434 value = RECUR (value);
15435 else
15437 value = lookup_label (DECL_NAME (value));
15438 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15439 TREE_USED (value) = 1;
15442 chain = TREE_CHAIN (t);
15443 if (chain && chain != void_type_node)
15444 chain = RECUR (chain);
15445 return tree_cons (purpose, value, chain);
15446 #undef RECUR
15449 /* Used to temporarily communicate the list of #pragma omp parallel
15450 clauses to #pragma omp for instantiation if they are combined
15451 together. */
15453 static tree *omp_parallel_combined_clauses;
15455 /* Substitute one OMP_FOR iterator. */
15457 static void
15458 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15459 tree initv, tree condv, tree incrv, tree *clauses,
15460 tree args, tsubst_flags_t complain, tree in_decl,
15461 bool integral_constant_expression_p)
15463 #define RECUR(NODE) \
15464 tsubst_expr ((NODE), args, complain, in_decl, \
15465 integral_constant_expression_p)
15466 tree decl, init, cond, incr;
15468 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15469 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15471 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15473 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15474 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15477 decl = TREE_OPERAND (init, 0);
15478 init = TREE_OPERAND (init, 1);
15479 tree decl_expr = NULL_TREE;
15480 if (init && TREE_CODE (init) == DECL_EXPR)
15482 /* We need to jump through some hoops to handle declarations in the
15483 init-statement, since we might need to handle auto deduction,
15484 but we need to keep control of initialization. */
15485 decl_expr = init;
15486 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15487 decl = tsubst_decl (decl, args, complain);
15489 else
15491 if (TREE_CODE (decl) == SCOPE_REF)
15493 decl = RECUR (decl);
15494 if (TREE_CODE (decl) == COMPONENT_REF)
15496 tree v = decl;
15497 while (v)
15498 switch (TREE_CODE (v))
15500 case COMPONENT_REF:
15501 case MEM_REF:
15502 case INDIRECT_REF:
15503 CASE_CONVERT:
15504 case POINTER_PLUS_EXPR:
15505 v = TREE_OPERAND (v, 0);
15506 continue;
15507 case PARM_DECL:
15508 if (DECL_CONTEXT (v) == current_function_decl
15509 && DECL_ARTIFICIAL (v)
15510 && DECL_NAME (v) == this_identifier)
15512 decl = TREE_OPERAND (decl, 1);
15513 decl = omp_privatize_field (decl, false);
15515 /* FALLTHRU */
15516 default:
15517 v = NULL_TREE;
15518 break;
15522 else
15523 decl = RECUR (decl);
15525 init = RECUR (init);
15527 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15528 if (auto_node && init)
15529 TREE_TYPE (decl)
15530 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15532 gcc_assert (!type_dependent_expression_p (decl));
15534 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15536 if (decl_expr)
15538 /* Declare the variable, but don't let that initialize it. */
15539 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15540 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15541 RECUR (decl_expr);
15542 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15545 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15546 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15547 if (TREE_CODE (incr) == MODIFY_EXPR)
15549 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15550 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15551 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15552 NOP_EXPR, rhs, complain);
15554 else
15555 incr = RECUR (incr);
15556 TREE_VEC_ELT (declv, i) = decl;
15557 TREE_VEC_ELT (initv, i) = init;
15558 TREE_VEC_ELT (condv, i) = cond;
15559 TREE_VEC_ELT (incrv, i) = incr;
15560 return;
15563 if (decl_expr)
15565 /* Declare and initialize the variable. */
15566 RECUR (decl_expr);
15567 init = NULL_TREE;
15569 else if (init)
15571 tree *pc;
15572 int j;
15573 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15575 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15577 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15578 && OMP_CLAUSE_DECL (*pc) == decl)
15579 break;
15580 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15581 && OMP_CLAUSE_DECL (*pc) == decl)
15583 if (j)
15584 break;
15585 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15586 tree c = *pc;
15587 *pc = OMP_CLAUSE_CHAIN (c);
15588 OMP_CLAUSE_CHAIN (c) = *clauses;
15589 *clauses = c;
15591 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15592 && OMP_CLAUSE_DECL (*pc) == decl)
15594 error ("iteration variable %qD should not be firstprivate",
15595 decl);
15596 *pc = OMP_CLAUSE_CHAIN (*pc);
15598 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15599 && OMP_CLAUSE_DECL (*pc) == decl)
15601 error ("iteration variable %qD should not be reduction",
15602 decl);
15603 *pc = OMP_CLAUSE_CHAIN (*pc);
15605 else
15606 pc = &OMP_CLAUSE_CHAIN (*pc);
15608 if (*pc)
15609 break;
15611 if (*pc == NULL_TREE)
15613 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15614 OMP_CLAUSE_DECL (c) = decl;
15615 c = finish_omp_clauses (c, C_ORT_OMP);
15616 if (c)
15618 OMP_CLAUSE_CHAIN (c) = *clauses;
15619 *clauses = c;
15623 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15624 if (COMPARISON_CLASS_P (cond))
15626 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15627 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15628 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15630 else
15631 cond = RECUR (cond);
15632 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15633 switch (TREE_CODE (incr))
15635 case PREINCREMENT_EXPR:
15636 case PREDECREMENT_EXPR:
15637 case POSTINCREMENT_EXPR:
15638 case POSTDECREMENT_EXPR:
15639 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15640 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15641 break;
15642 case MODIFY_EXPR:
15643 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15644 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15646 tree rhs = TREE_OPERAND (incr, 1);
15647 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15648 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15649 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15650 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15651 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15652 rhs0, rhs1));
15654 else
15655 incr = RECUR (incr);
15656 break;
15657 case MODOP_EXPR:
15658 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15659 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15661 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15662 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15663 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15664 TREE_TYPE (decl), lhs,
15665 RECUR (TREE_OPERAND (incr, 2))));
15667 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15668 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15669 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15671 tree rhs = TREE_OPERAND (incr, 2);
15672 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15673 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15674 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15675 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15676 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15677 rhs0, rhs1));
15679 else
15680 incr = RECUR (incr);
15681 break;
15682 default:
15683 incr = RECUR (incr);
15684 break;
15687 TREE_VEC_ELT (declv, i) = decl;
15688 TREE_VEC_ELT (initv, i) = init;
15689 TREE_VEC_ELT (condv, i) = cond;
15690 TREE_VEC_ELT (incrv, i) = incr;
15691 #undef RECUR
15694 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15695 of OMP_TARGET's body. */
15697 static tree
15698 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15700 *walk_subtrees = 0;
15701 switch (TREE_CODE (*tp))
15703 case OMP_TEAMS:
15704 return *tp;
15705 case BIND_EXPR:
15706 case STATEMENT_LIST:
15707 *walk_subtrees = 1;
15708 break;
15709 default:
15710 break;
15712 return NULL_TREE;
15715 /* Helper function for tsubst_expr. For decomposition declaration
15716 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15717 also the corresponding decls representing the identifiers
15718 of the decomposition declaration. Return DECL if successful
15719 or error_mark_node otherwise, set *FIRST to the first decl
15720 in the list chained through DECL_CHAIN and *CNT to the number
15721 of such decls. */
15723 static tree
15724 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15725 tsubst_flags_t complain, tree in_decl, tree *first,
15726 unsigned int *cnt)
15728 tree decl2, decl3, prev = decl;
15729 *cnt = 0;
15730 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15731 for (decl2 = DECL_CHAIN (pattern_decl);
15732 decl2
15733 && VAR_P (decl2)
15734 && DECL_DECOMPOSITION_P (decl2)
15735 && DECL_NAME (decl2);
15736 decl2 = DECL_CHAIN (decl2))
15738 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15740 gcc_assert (errorcount);
15741 return error_mark_node;
15743 (*cnt)++;
15744 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15745 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15746 tree v = DECL_VALUE_EXPR (decl2);
15747 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15748 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15749 decl3 = tsubst (decl2, args, complain, in_decl);
15750 SET_DECL_VALUE_EXPR (decl2, v);
15751 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15752 if (VAR_P (decl3))
15753 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15754 maybe_push_decl (decl3);
15755 if (error_operand_p (decl3))
15756 decl = error_mark_node;
15757 else if (decl != error_mark_node
15758 && DECL_CHAIN (decl3) != prev)
15760 gcc_assert (errorcount);
15761 decl = error_mark_node;
15763 else
15764 prev = decl3;
15766 *first = prev;
15767 return decl;
15770 /* Like tsubst_copy for expressions, etc. but also does semantic
15771 processing. */
15773 tree
15774 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15775 bool integral_constant_expression_p)
15777 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15778 #define RECUR(NODE) \
15779 tsubst_expr ((NODE), args, complain, in_decl, \
15780 integral_constant_expression_p)
15782 tree stmt, tmp;
15783 tree r;
15784 location_t loc;
15786 if (t == NULL_TREE || t == error_mark_node)
15787 return t;
15789 loc = input_location;
15790 if (EXPR_HAS_LOCATION (t))
15791 input_location = EXPR_LOCATION (t);
15792 if (STATEMENT_CODE_P (TREE_CODE (t)))
15793 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15795 switch (TREE_CODE (t))
15797 case STATEMENT_LIST:
15799 tree_stmt_iterator i;
15800 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15801 RECUR (tsi_stmt (i));
15802 break;
15805 case CTOR_INITIALIZER:
15806 finish_mem_initializers (tsubst_initializer_list
15807 (TREE_OPERAND (t, 0), args));
15808 break;
15810 case RETURN_EXPR:
15811 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15812 break;
15814 case EXPR_STMT:
15815 tmp = RECUR (EXPR_STMT_EXPR (t));
15816 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15817 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15818 else
15819 finish_expr_stmt (tmp);
15820 break;
15822 case USING_STMT:
15823 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15824 /*attribs=*/NULL_TREE);
15825 break;
15827 case DECL_EXPR:
15829 tree decl, pattern_decl;
15830 tree init;
15832 pattern_decl = decl = DECL_EXPR_DECL (t);
15833 if (TREE_CODE (decl) == LABEL_DECL)
15834 finish_label_decl (DECL_NAME (decl));
15835 else if (TREE_CODE (decl) == USING_DECL)
15837 tree scope = USING_DECL_SCOPE (decl);
15838 tree name = DECL_NAME (decl);
15840 scope = tsubst (scope, args, complain, in_decl);
15841 decl = lookup_qualified_name (scope, name,
15842 /*is_type_p=*/false,
15843 /*complain=*/false);
15844 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15845 qualified_name_lookup_error (scope, name, decl, input_location);
15846 else
15847 finish_local_using_decl (decl, scope, name);
15849 else if (DECL_PACK_P (decl))
15851 /* Don't build up decls for a variadic capture proxy, we'll
15852 instantiate the elements directly as needed. */
15853 break;
15855 else
15857 init = DECL_INITIAL (decl);
15858 decl = tsubst (decl, args, complain, in_decl);
15859 if (decl != error_mark_node)
15861 /* By marking the declaration as instantiated, we avoid
15862 trying to instantiate it. Since instantiate_decl can't
15863 handle local variables, and since we've already done
15864 all that needs to be done, that's the right thing to
15865 do. */
15866 if (VAR_P (decl))
15867 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15868 if (VAR_P (decl)
15869 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15870 /* Anonymous aggregates are a special case. */
15871 finish_anon_union (decl);
15872 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15874 DECL_CONTEXT (decl) = current_function_decl;
15875 if (DECL_NAME (decl) == this_identifier)
15877 tree lam = DECL_CONTEXT (current_function_decl);
15878 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15879 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15881 insert_capture_proxy (decl);
15883 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15884 /* We already did a pushtag. */;
15885 else if (TREE_CODE (decl) == FUNCTION_DECL
15886 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15887 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15889 DECL_CONTEXT (decl) = NULL_TREE;
15890 pushdecl (decl);
15891 DECL_CONTEXT (decl) = current_function_decl;
15892 cp_check_omp_declare_reduction (decl);
15894 else
15896 int const_init = false;
15897 maybe_push_decl (decl);
15898 if (VAR_P (decl)
15899 && DECL_PRETTY_FUNCTION_P (decl))
15901 /* For __PRETTY_FUNCTION__ we have to adjust the
15902 initializer. */
15903 const char *const name
15904 = cxx_printable_name (current_function_decl, 2);
15905 init = cp_fname_init (name, &TREE_TYPE (decl));
15907 else
15908 init = tsubst_init (init, decl, args, complain, in_decl);
15910 if (VAR_P (decl))
15911 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15912 (pattern_decl));
15913 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15914 if (VAR_P (decl)
15915 && DECL_DECOMPOSITION_P (decl)
15916 && TREE_TYPE (pattern_decl) != error_mark_node)
15918 unsigned int cnt;
15919 tree first;
15920 decl = tsubst_decomp_names (decl, pattern_decl, args,
15921 complain, in_decl, &first,
15922 &cnt);
15923 if (decl != error_mark_node)
15924 cp_finish_decomp (decl, first, cnt);
15930 break;
15933 case FOR_STMT:
15934 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15935 RECUR (FOR_INIT_STMT (t));
15936 finish_init_stmt (stmt);
15937 tmp = RECUR (FOR_COND (t));
15938 finish_for_cond (tmp, stmt, false);
15939 tmp = RECUR (FOR_EXPR (t));
15940 finish_for_expr (tmp, stmt);
15941 RECUR (FOR_BODY (t));
15942 finish_for_stmt (stmt);
15943 break;
15945 case RANGE_FOR_STMT:
15947 tree decl, expr;
15948 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15949 decl = RANGE_FOR_DECL (t);
15950 decl = tsubst (decl, args, complain, in_decl);
15951 maybe_push_decl (decl);
15952 expr = RECUR (RANGE_FOR_EXPR (t));
15953 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15955 unsigned int cnt;
15956 tree first;
15957 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15958 complain, in_decl, &first, &cnt);
15959 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15960 RANGE_FOR_IVDEP (t));
15962 else
15963 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15964 RANGE_FOR_IVDEP (t));
15965 RECUR (RANGE_FOR_BODY (t));
15966 finish_for_stmt (stmt);
15968 break;
15970 case WHILE_STMT:
15971 stmt = begin_while_stmt ();
15972 tmp = RECUR (WHILE_COND (t));
15973 finish_while_stmt_cond (tmp, stmt, false);
15974 RECUR (WHILE_BODY (t));
15975 finish_while_stmt (stmt);
15976 break;
15978 case DO_STMT:
15979 stmt = begin_do_stmt ();
15980 RECUR (DO_BODY (t));
15981 finish_do_body (stmt);
15982 tmp = RECUR (DO_COND (t));
15983 finish_do_stmt (tmp, stmt, false);
15984 break;
15986 case IF_STMT:
15987 stmt = begin_if_stmt ();
15988 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15989 tmp = RECUR (IF_COND (t));
15990 tmp = finish_if_stmt_cond (tmp, stmt);
15991 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15992 /* Don't instantiate the THEN_CLAUSE. */;
15993 else
15995 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15996 if (inhibit)
15997 ++c_inhibit_evaluation_warnings;
15998 RECUR (THEN_CLAUSE (t));
15999 if (inhibit)
16000 --c_inhibit_evaluation_warnings;
16002 finish_then_clause (stmt);
16004 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16005 /* Don't instantiate the ELSE_CLAUSE. */;
16006 else if (ELSE_CLAUSE (t))
16008 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16009 begin_else_clause (stmt);
16010 if (inhibit)
16011 ++c_inhibit_evaluation_warnings;
16012 RECUR (ELSE_CLAUSE (t));
16013 if (inhibit)
16014 --c_inhibit_evaluation_warnings;
16015 finish_else_clause (stmt);
16018 finish_if_stmt (stmt);
16019 break;
16021 case BIND_EXPR:
16022 if (BIND_EXPR_BODY_BLOCK (t))
16023 stmt = begin_function_body ();
16024 else
16025 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16026 ? BCS_TRY_BLOCK : 0);
16028 RECUR (BIND_EXPR_BODY (t));
16030 if (BIND_EXPR_BODY_BLOCK (t))
16031 finish_function_body (stmt);
16032 else
16033 finish_compound_stmt (stmt);
16034 break;
16036 case BREAK_STMT:
16037 finish_break_stmt ();
16038 break;
16040 case CONTINUE_STMT:
16041 finish_continue_stmt ();
16042 break;
16044 case SWITCH_STMT:
16045 stmt = begin_switch_stmt ();
16046 tmp = RECUR (SWITCH_STMT_COND (t));
16047 finish_switch_cond (tmp, stmt);
16048 RECUR (SWITCH_STMT_BODY (t));
16049 finish_switch_stmt (stmt);
16050 break;
16052 case CASE_LABEL_EXPR:
16054 tree low = RECUR (CASE_LOW (t));
16055 tree high = RECUR (CASE_HIGH (t));
16056 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16057 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16058 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16059 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16061 break;
16063 case LABEL_EXPR:
16065 tree decl = LABEL_EXPR_LABEL (t);
16066 tree label;
16068 label = finish_label_stmt (DECL_NAME (decl));
16069 if (TREE_CODE (label) == LABEL_DECL)
16070 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16071 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16072 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16074 break;
16076 case GOTO_EXPR:
16077 tmp = GOTO_DESTINATION (t);
16078 if (TREE_CODE (tmp) != LABEL_DECL)
16079 /* Computed goto's must be tsubst'd into. On the other hand,
16080 non-computed gotos must not be; the identifier in question
16081 will have no binding. */
16082 tmp = RECUR (tmp);
16083 else
16084 tmp = DECL_NAME (tmp);
16085 finish_goto_stmt (tmp);
16086 break;
16088 case ASM_EXPR:
16090 tree string = RECUR (ASM_STRING (t));
16091 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16092 complain, in_decl);
16093 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16094 complain, in_decl);
16095 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16096 complain, in_decl);
16097 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16098 complain, in_decl);
16099 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16100 clobbers, labels);
16101 tree asm_expr = tmp;
16102 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16103 asm_expr = TREE_OPERAND (asm_expr, 0);
16104 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16106 break;
16108 case TRY_BLOCK:
16109 if (CLEANUP_P (t))
16111 stmt = begin_try_block ();
16112 RECUR (TRY_STMTS (t));
16113 finish_cleanup_try_block (stmt);
16114 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16116 else
16118 tree compound_stmt = NULL_TREE;
16120 if (FN_TRY_BLOCK_P (t))
16121 stmt = begin_function_try_block (&compound_stmt);
16122 else
16123 stmt = begin_try_block ();
16125 RECUR (TRY_STMTS (t));
16127 if (FN_TRY_BLOCK_P (t))
16128 finish_function_try_block (stmt);
16129 else
16130 finish_try_block (stmt);
16132 RECUR (TRY_HANDLERS (t));
16133 if (FN_TRY_BLOCK_P (t))
16134 finish_function_handler_sequence (stmt, compound_stmt);
16135 else
16136 finish_handler_sequence (stmt);
16138 break;
16140 case HANDLER:
16142 tree decl = HANDLER_PARMS (t);
16144 if (decl)
16146 decl = tsubst (decl, args, complain, in_decl);
16147 /* Prevent instantiate_decl from trying to instantiate
16148 this variable. We've already done all that needs to be
16149 done. */
16150 if (decl != error_mark_node)
16151 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16153 stmt = begin_handler ();
16154 finish_handler_parms (decl, stmt);
16155 RECUR (HANDLER_BODY (t));
16156 finish_handler (stmt);
16158 break;
16160 case TAG_DEFN:
16161 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16162 if (CLASS_TYPE_P (tmp))
16164 /* Local classes are not independent templates; they are
16165 instantiated along with their containing function. And this
16166 way we don't have to deal with pushing out of one local class
16167 to instantiate a member of another local class. */
16168 /* Closures are handled by the LAMBDA_EXPR. */
16169 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16170 complete_type (tmp);
16171 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16172 if ((VAR_P (fld)
16173 || (TREE_CODE (fld) == FUNCTION_DECL
16174 && !DECL_ARTIFICIAL (fld)))
16175 && DECL_TEMPLATE_INSTANTIATION (fld))
16176 instantiate_decl (fld, /*defer_ok=*/false,
16177 /*expl_inst_class=*/false);
16179 break;
16181 case STATIC_ASSERT:
16183 tree condition;
16185 ++c_inhibit_evaluation_warnings;
16186 condition =
16187 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16188 args,
16189 complain, in_decl,
16190 /*integral_constant_expression_p=*/true);
16191 --c_inhibit_evaluation_warnings;
16193 finish_static_assert (condition,
16194 STATIC_ASSERT_MESSAGE (t),
16195 STATIC_ASSERT_SOURCE_LOCATION (t),
16196 /*member_p=*/false);
16198 break;
16200 case OACC_KERNELS:
16201 case OACC_PARALLEL:
16202 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16203 in_decl);
16204 stmt = begin_omp_parallel ();
16205 RECUR (OMP_BODY (t));
16206 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16207 break;
16209 case OMP_PARALLEL:
16210 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16211 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16212 complain, in_decl);
16213 if (OMP_PARALLEL_COMBINED (t))
16214 omp_parallel_combined_clauses = &tmp;
16215 stmt = begin_omp_parallel ();
16216 RECUR (OMP_PARALLEL_BODY (t));
16217 gcc_assert (omp_parallel_combined_clauses == NULL);
16218 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16219 = OMP_PARALLEL_COMBINED (t);
16220 pop_omp_privatization_clauses (r);
16221 break;
16223 case OMP_TASK:
16224 r = push_omp_privatization_clauses (false);
16225 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16226 complain, in_decl);
16227 stmt = begin_omp_task ();
16228 RECUR (OMP_TASK_BODY (t));
16229 finish_omp_task (tmp, stmt);
16230 pop_omp_privatization_clauses (r);
16231 break;
16233 case OMP_FOR:
16234 case OMP_SIMD:
16235 case CILK_SIMD:
16236 case CILK_FOR:
16237 case OMP_DISTRIBUTE:
16238 case OMP_TASKLOOP:
16239 case OACC_LOOP:
16241 tree clauses, body, pre_body;
16242 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16243 tree orig_declv = NULL_TREE;
16244 tree incrv = NULL_TREE;
16245 enum c_omp_region_type ort = C_ORT_OMP;
16246 int i;
16248 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16249 ort = C_ORT_CILK;
16250 else if (TREE_CODE (t) == OACC_LOOP)
16251 ort = C_ORT_ACC;
16253 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16254 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16255 in_decl);
16256 if (OMP_FOR_INIT (t) != NULL_TREE)
16258 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16259 if (OMP_FOR_ORIG_DECLS (t))
16260 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16261 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16262 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16263 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16266 stmt = begin_omp_structured_block ();
16268 pre_body = push_stmt_list ();
16269 RECUR (OMP_FOR_PRE_BODY (t));
16270 pre_body = pop_stmt_list (pre_body);
16272 if (OMP_FOR_INIT (t) != NULL_TREE)
16273 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16274 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16275 incrv, &clauses, args, complain, in_decl,
16276 integral_constant_expression_p);
16277 omp_parallel_combined_clauses = NULL;
16279 body = push_stmt_list ();
16280 RECUR (OMP_FOR_BODY (t));
16281 body = pop_stmt_list (body);
16283 if (OMP_FOR_INIT (t) != NULL_TREE)
16284 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16285 orig_declv, initv, condv, incrv, body, pre_body,
16286 NULL, clauses);
16287 else
16289 t = make_node (TREE_CODE (t));
16290 TREE_TYPE (t) = void_type_node;
16291 OMP_FOR_BODY (t) = body;
16292 OMP_FOR_PRE_BODY (t) = pre_body;
16293 OMP_FOR_CLAUSES (t) = clauses;
16294 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16295 add_stmt (t);
16298 add_stmt (finish_omp_structured_block (stmt));
16299 pop_omp_privatization_clauses (r);
16301 break;
16303 case OMP_SECTIONS:
16304 omp_parallel_combined_clauses = NULL;
16305 /* FALLTHRU */
16306 case OMP_SINGLE:
16307 case OMP_TEAMS:
16308 case OMP_CRITICAL:
16309 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16310 && OMP_TEAMS_COMBINED (t));
16311 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16312 in_decl);
16313 stmt = push_stmt_list ();
16314 RECUR (OMP_BODY (t));
16315 stmt = pop_stmt_list (stmt);
16317 t = copy_node (t);
16318 OMP_BODY (t) = stmt;
16319 OMP_CLAUSES (t) = tmp;
16320 add_stmt (t);
16321 pop_omp_privatization_clauses (r);
16322 break;
16324 case OACC_DATA:
16325 case OMP_TARGET_DATA:
16326 case OMP_TARGET:
16327 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16328 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16329 in_decl);
16330 keep_next_level (true);
16331 stmt = begin_omp_structured_block ();
16333 RECUR (OMP_BODY (t));
16334 stmt = finish_omp_structured_block (stmt);
16336 t = copy_node (t);
16337 OMP_BODY (t) = stmt;
16338 OMP_CLAUSES (t) = tmp;
16339 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16341 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16342 if (teams)
16344 /* For combined target teams, ensure the num_teams and
16345 thread_limit clause expressions are evaluated on the host,
16346 before entering the target construct. */
16347 tree c;
16348 for (c = OMP_TEAMS_CLAUSES (teams);
16349 c; c = OMP_CLAUSE_CHAIN (c))
16350 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16351 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16352 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16354 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16355 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16356 if (expr == error_mark_node)
16357 continue;
16358 tmp = TARGET_EXPR_SLOT (expr);
16359 add_stmt (expr);
16360 OMP_CLAUSE_OPERAND (c, 0) = expr;
16361 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16362 OMP_CLAUSE_FIRSTPRIVATE);
16363 OMP_CLAUSE_DECL (tc) = tmp;
16364 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16365 OMP_TARGET_CLAUSES (t) = tc;
16369 add_stmt (t);
16370 break;
16372 case OACC_DECLARE:
16373 t = copy_node (t);
16374 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16375 complain, in_decl);
16376 OACC_DECLARE_CLAUSES (t) = tmp;
16377 add_stmt (t);
16378 break;
16380 case OMP_TARGET_UPDATE:
16381 case OMP_TARGET_ENTER_DATA:
16382 case OMP_TARGET_EXIT_DATA:
16383 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16384 complain, in_decl);
16385 t = copy_node (t);
16386 OMP_STANDALONE_CLAUSES (t) = tmp;
16387 add_stmt (t);
16388 break;
16390 case OACC_ENTER_DATA:
16391 case OACC_EXIT_DATA:
16392 case OACC_UPDATE:
16393 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16394 complain, in_decl);
16395 t = copy_node (t);
16396 OMP_STANDALONE_CLAUSES (t) = tmp;
16397 add_stmt (t);
16398 break;
16400 case OMP_ORDERED:
16401 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16402 complain, in_decl);
16403 stmt = push_stmt_list ();
16404 RECUR (OMP_BODY (t));
16405 stmt = pop_stmt_list (stmt);
16407 t = copy_node (t);
16408 OMP_BODY (t) = stmt;
16409 OMP_ORDERED_CLAUSES (t) = tmp;
16410 add_stmt (t);
16411 break;
16413 case OMP_SECTION:
16414 case OMP_MASTER:
16415 case OMP_TASKGROUP:
16416 stmt = push_stmt_list ();
16417 RECUR (OMP_BODY (t));
16418 stmt = pop_stmt_list (stmt);
16420 t = copy_node (t);
16421 OMP_BODY (t) = stmt;
16422 add_stmt (t);
16423 break;
16425 case OMP_ATOMIC:
16426 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16427 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16429 tree op1 = TREE_OPERAND (t, 1);
16430 tree rhs1 = NULL_TREE;
16431 tree lhs, rhs;
16432 if (TREE_CODE (op1) == COMPOUND_EXPR)
16434 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16435 op1 = TREE_OPERAND (op1, 1);
16437 lhs = RECUR (TREE_OPERAND (op1, 0));
16438 rhs = RECUR (TREE_OPERAND (op1, 1));
16439 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16440 NULL_TREE, NULL_TREE, rhs1,
16441 OMP_ATOMIC_SEQ_CST (t));
16443 else
16445 tree op1 = TREE_OPERAND (t, 1);
16446 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16447 tree rhs1 = NULL_TREE;
16448 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16449 enum tree_code opcode = NOP_EXPR;
16450 if (code == OMP_ATOMIC_READ)
16452 v = RECUR (TREE_OPERAND (op1, 0));
16453 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16455 else if (code == OMP_ATOMIC_CAPTURE_OLD
16456 || code == OMP_ATOMIC_CAPTURE_NEW)
16458 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16459 v = RECUR (TREE_OPERAND (op1, 0));
16460 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16461 if (TREE_CODE (op11) == COMPOUND_EXPR)
16463 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16464 op11 = TREE_OPERAND (op11, 1);
16466 lhs = RECUR (TREE_OPERAND (op11, 0));
16467 rhs = RECUR (TREE_OPERAND (op11, 1));
16468 opcode = TREE_CODE (op11);
16469 if (opcode == MODIFY_EXPR)
16470 opcode = NOP_EXPR;
16472 else
16474 code = OMP_ATOMIC;
16475 lhs = RECUR (TREE_OPERAND (op1, 0));
16476 rhs = RECUR (TREE_OPERAND (op1, 1));
16478 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16479 OMP_ATOMIC_SEQ_CST (t));
16481 break;
16483 case TRANSACTION_EXPR:
16485 int flags = 0;
16486 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16487 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16489 if (TRANSACTION_EXPR_IS_STMT (t))
16491 tree body = TRANSACTION_EXPR_BODY (t);
16492 tree noex = NULL_TREE;
16493 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16495 noex = MUST_NOT_THROW_COND (body);
16496 if (noex == NULL_TREE)
16497 noex = boolean_true_node;
16498 body = TREE_OPERAND (body, 0);
16500 stmt = begin_transaction_stmt (input_location, NULL, flags);
16501 RECUR (body);
16502 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16504 else
16506 stmt = build_transaction_expr (EXPR_LOCATION (t),
16507 RECUR (TRANSACTION_EXPR_BODY (t)),
16508 flags, NULL_TREE);
16509 RETURN (stmt);
16512 break;
16514 case MUST_NOT_THROW_EXPR:
16516 tree op0 = RECUR (TREE_OPERAND (t, 0));
16517 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16518 RETURN (build_must_not_throw_expr (op0, cond));
16521 case EXPR_PACK_EXPANSION:
16522 error ("invalid use of pack expansion expression");
16523 RETURN (error_mark_node);
16525 case NONTYPE_ARGUMENT_PACK:
16526 error ("use %<...%> to expand argument pack");
16527 RETURN (error_mark_node);
16529 case CILK_SPAWN_STMT:
16530 cfun->calls_cilk_spawn = 1;
16531 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16533 case CILK_SYNC_STMT:
16534 RETURN (build_cilk_sync ());
16536 case COMPOUND_EXPR:
16537 tmp = RECUR (TREE_OPERAND (t, 0));
16538 if (tmp == NULL_TREE)
16539 /* If the first operand was a statement, we're done with it. */
16540 RETURN (RECUR (TREE_OPERAND (t, 1)));
16541 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16542 RECUR (TREE_OPERAND (t, 1)),
16543 complain));
16545 case ANNOTATE_EXPR:
16546 tmp = RECUR (TREE_OPERAND (t, 0));
16547 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16548 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16550 default:
16551 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16553 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16554 /*function_p=*/false,
16555 integral_constant_expression_p));
16558 RETURN (NULL_TREE);
16559 out:
16560 input_location = loc;
16561 return r;
16562 #undef RECUR
16563 #undef RETURN
16566 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16567 function. For description of the body see comment above
16568 cp_parser_omp_declare_reduction_exprs. */
16570 static void
16571 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16573 if (t == NULL_TREE || t == error_mark_node)
16574 return;
16576 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16578 tree_stmt_iterator tsi;
16579 int i;
16580 tree stmts[7];
16581 memset (stmts, 0, sizeof stmts);
16582 for (i = 0, tsi = tsi_start (t);
16583 i < 7 && !tsi_end_p (tsi);
16584 i++, tsi_next (&tsi))
16585 stmts[i] = tsi_stmt (tsi);
16586 gcc_assert (tsi_end_p (tsi));
16588 if (i >= 3)
16590 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16591 && TREE_CODE (stmts[1]) == DECL_EXPR);
16592 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16593 args, complain, in_decl);
16594 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16595 args, complain, in_decl);
16596 DECL_CONTEXT (omp_out) = current_function_decl;
16597 DECL_CONTEXT (omp_in) = current_function_decl;
16598 keep_next_level (true);
16599 tree block = begin_omp_structured_block ();
16600 tsubst_expr (stmts[2], args, complain, in_decl, false);
16601 block = finish_omp_structured_block (block);
16602 block = maybe_cleanup_point_expr_void (block);
16603 add_decl_expr (omp_out);
16604 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16605 TREE_NO_WARNING (omp_out) = 1;
16606 add_decl_expr (omp_in);
16607 finish_expr_stmt (block);
16609 if (i >= 6)
16611 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16612 && TREE_CODE (stmts[4]) == DECL_EXPR);
16613 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16614 args, complain, in_decl);
16615 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16616 args, complain, in_decl);
16617 DECL_CONTEXT (omp_priv) = current_function_decl;
16618 DECL_CONTEXT (omp_orig) = current_function_decl;
16619 keep_next_level (true);
16620 tree block = begin_omp_structured_block ();
16621 tsubst_expr (stmts[5], args, complain, in_decl, false);
16622 block = finish_omp_structured_block (block);
16623 block = maybe_cleanup_point_expr_void (block);
16624 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16625 add_decl_expr (omp_priv);
16626 add_decl_expr (omp_orig);
16627 finish_expr_stmt (block);
16628 if (i == 7)
16629 add_decl_expr (omp_orig);
16633 /* T is a postfix-expression that is not being used in a function
16634 call. Return the substituted version of T. */
16636 static tree
16637 tsubst_non_call_postfix_expression (tree t, tree args,
16638 tsubst_flags_t complain,
16639 tree in_decl)
16641 if (TREE_CODE (t) == SCOPE_REF)
16642 t = tsubst_qualified_id (t, args, complain, in_decl,
16643 /*done=*/false, /*address_p=*/false);
16644 else
16645 t = tsubst_copy_and_build (t, args, complain, in_decl,
16646 /*function_p=*/false,
16647 /*integral_constant_expression_p=*/false);
16649 return t;
16652 /* Like tsubst but deals with expressions and performs semantic
16653 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16655 tree
16656 tsubst_copy_and_build (tree t,
16657 tree args,
16658 tsubst_flags_t complain,
16659 tree in_decl,
16660 bool function_p,
16661 bool integral_constant_expression_p)
16663 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16664 #define RECUR(NODE) \
16665 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16666 /*function_p=*/false, \
16667 integral_constant_expression_p)
16669 tree retval, op1;
16670 location_t loc;
16672 if (t == NULL_TREE || t == error_mark_node)
16673 return t;
16675 loc = input_location;
16676 if (EXPR_HAS_LOCATION (t))
16677 input_location = EXPR_LOCATION (t);
16679 /* N3276 decltype magic only applies to calls at the top level or on the
16680 right side of a comma. */
16681 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16682 complain &= ~tf_decltype;
16684 switch (TREE_CODE (t))
16686 case USING_DECL:
16687 t = DECL_NAME (t);
16688 /* Fall through. */
16689 case IDENTIFIER_NODE:
16691 tree decl;
16692 cp_id_kind idk;
16693 bool non_integral_constant_expression_p;
16694 const char *error_msg;
16696 if (IDENTIFIER_CONV_OP_P (t))
16698 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16699 t = make_conv_op_name (new_type);
16702 /* Look up the name. */
16703 decl = lookup_name (t);
16705 /* By convention, expressions use ERROR_MARK_NODE to indicate
16706 failure, not NULL_TREE. */
16707 if (decl == NULL_TREE)
16708 decl = error_mark_node;
16710 decl = finish_id_expression (t, decl, NULL_TREE,
16711 &idk,
16712 integral_constant_expression_p,
16713 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16714 &non_integral_constant_expression_p,
16715 /*template_p=*/false,
16716 /*done=*/true,
16717 /*address_p=*/false,
16718 /*template_arg_p=*/false,
16719 &error_msg,
16720 input_location);
16721 if (error_msg)
16722 error (error_msg);
16723 if (!function_p && identifier_p (decl))
16725 if (complain & tf_error)
16726 unqualified_name_lookup_error (decl);
16727 decl = error_mark_node;
16729 RETURN (decl);
16732 case TEMPLATE_ID_EXPR:
16734 tree object;
16735 tree templ = RECUR (TREE_OPERAND (t, 0));
16736 tree targs = TREE_OPERAND (t, 1);
16738 if (targs)
16739 targs = tsubst_template_args (targs, args, complain, in_decl);
16740 if (targs == error_mark_node)
16741 return error_mark_node;
16743 if (TREE_CODE (templ) == SCOPE_REF)
16745 tree name = TREE_OPERAND (templ, 1);
16746 tree tid = lookup_template_function (name, targs);
16747 TREE_OPERAND (templ, 1) = tid;
16748 return templ;
16751 if (variable_template_p (templ))
16752 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16754 if (TREE_CODE (templ) == COMPONENT_REF)
16756 object = TREE_OPERAND (templ, 0);
16757 templ = TREE_OPERAND (templ, 1);
16759 else
16760 object = NULL_TREE;
16761 templ = lookup_template_function (templ, targs);
16763 if (object)
16764 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16765 object, templ, NULL_TREE));
16766 else
16767 RETURN (baselink_for_fns (templ));
16770 case INDIRECT_REF:
16772 tree r = RECUR (TREE_OPERAND (t, 0));
16774 if (REFERENCE_REF_P (t))
16776 /* A type conversion to reference type will be enclosed in
16777 such an indirect ref, but the substitution of the cast
16778 will have also added such an indirect ref. */
16779 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16780 r = convert_from_reference (r);
16782 else
16783 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16784 complain|decltype_flag);
16786 if (TREE_CODE (r) == INDIRECT_REF)
16787 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16789 RETURN (r);
16792 case NOP_EXPR:
16794 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16795 tree op0 = RECUR (TREE_OPERAND (t, 0));
16796 RETURN (build_nop (type, op0));
16799 case IMPLICIT_CONV_EXPR:
16801 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16802 tree expr = RECUR (TREE_OPERAND (t, 0));
16803 int flags = LOOKUP_IMPLICIT;
16804 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16805 flags = LOOKUP_NORMAL;
16806 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16807 flags));
16810 case CONVERT_EXPR:
16812 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16813 tree op0 = RECUR (TREE_OPERAND (t, 0));
16814 RETURN (build1 (CONVERT_EXPR, type, op0));
16817 case CAST_EXPR:
16818 case REINTERPRET_CAST_EXPR:
16819 case CONST_CAST_EXPR:
16820 case DYNAMIC_CAST_EXPR:
16821 case STATIC_CAST_EXPR:
16823 tree type;
16824 tree op, r = NULL_TREE;
16826 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16827 if (integral_constant_expression_p
16828 && !cast_valid_in_integral_constant_expression_p (type))
16830 if (complain & tf_error)
16831 error ("a cast to a type other than an integral or "
16832 "enumeration type cannot appear in a constant-expression");
16833 RETURN (error_mark_node);
16836 op = RECUR (TREE_OPERAND (t, 0));
16838 warning_sentinel s(warn_useless_cast);
16839 switch (TREE_CODE (t))
16841 case CAST_EXPR:
16842 r = build_functional_cast (type, op, complain);
16843 break;
16844 case REINTERPRET_CAST_EXPR:
16845 r = build_reinterpret_cast (type, op, complain);
16846 break;
16847 case CONST_CAST_EXPR:
16848 r = build_const_cast (type, op, complain);
16849 break;
16850 case DYNAMIC_CAST_EXPR:
16851 r = build_dynamic_cast (type, op, complain);
16852 break;
16853 case STATIC_CAST_EXPR:
16854 r = build_static_cast (type, op, complain);
16855 break;
16856 default:
16857 gcc_unreachable ();
16860 RETURN (r);
16863 case POSTDECREMENT_EXPR:
16864 case POSTINCREMENT_EXPR:
16865 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16866 args, complain, in_decl);
16867 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16868 complain|decltype_flag));
16870 case PREDECREMENT_EXPR:
16871 case PREINCREMENT_EXPR:
16872 case NEGATE_EXPR:
16873 case BIT_NOT_EXPR:
16874 case ABS_EXPR:
16875 case TRUTH_NOT_EXPR:
16876 case UNARY_PLUS_EXPR: /* Unary + */
16877 case REALPART_EXPR:
16878 case IMAGPART_EXPR:
16879 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16880 RECUR (TREE_OPERAND (t, 0)),
16881 complain|decltype_flag));
16883 case FIX_TRUNC_EXPR:
16884 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16885 false, complain));
16887 case ADDR_EXPR:
16888 op1 = TREE_OPERAND (t, 0);
16889 if (TREE_CODE (op1) == LABEL_DECL)
16890 RETURN (finish_label_address_expr (DECL_NAME (op1),
16891 EXPR_LOCATION (op1)));
16892 if (TREE_CODE (op1) == SCOPE_REF)
16893 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16894 /*done=*/true, /*address_p=*/true);
16895 else
16896 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16897 in_decl);
16898 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16899 complain|decltype_flag));
16901 case PLUS_EXPR:
16902 case MINUS_EXPR:
16903 case MULT_EXPR:
16904 case TRUNC_DIV_EXPR:
16905 case CEIL_DIV_EXPR:
16906 case FLOOR_DIV_EXPR:
16907 case ROUND_DIV_EXPR:
16908 case EXACT_DIV_EXPR:
16909 case BIT_AND_EXPR:
16910 case BIT_IOR_EXPR:
16911 case BIT_XOR_EXPR:
16912 case TRUNC_MOD_EXPR:
16913 case FLOOR_MOD_EXPR:
16914 case TRUTH_ANDIF_EXPR:
16915 case TRUTH_ORIF_EXPR:
16916 case TRUTH_AND_EXPR:
16917 case TRUTH_OR_EXPR:
16918 case RSHIFT_EXPR:
16919 case LSHIFT_EXPR:
16920 case RROTATE_EXPR:
16921 case LROTATE_EXPR:
16922 case EQ_EXPR:
16923 case NE_EXPR:
16924 case MAX_EXPR:
16925 case MIN_EXPR:
16926 case LE_EXPR:
16927 case GE_EXPR:
16928 case LT_EXPR:
16929 case GT_EXPR:
16930 case MEMBER_REF:
16931 case DOTSTAR_EXPR:
16933 warning_sentinel s1(warn_type_limits);
16934 warning_sentinel s2(warn_div_by_zero);
16935 warning_sentinel s3(warn_logical_op);
16936 warning_sentinel s4(warn_tautological_compare);
16937 tree op0 = RECUR (TREE_OPERAND (t, 0));
16938 tree op1 = RECUR (TREE_OPERAND (t, 1));
16939 tree r = build_x_binary_op
16940 (input_location, TREE_CODE (t),
16941 op0,
16942 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16943 ? ERROR_MARK
16944 : TREE_CODE (TREE_OPERAND (t, 0))),
16945 op1,
16946 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16947 ? ERROR_MARK
16948 : TREE_CODE (TREE_OPERAND (t, 1))),
16949 /*overload=*/NULL,
16950 complain|decltype_flag);
16951 if (EXPR_P (r) && TREE_NO_WARNING (t))
16952 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16954 RETURN (r);
16957 case POINTER_PLUS_EXPR:
16959 tree op0 = RECUR (TREE_OPERAND (t, 0));
16960 tree op1 = RECUR (TREE_OPERAND (t, 1));
16961 return fold_build_pointer_plus (op0, op1);
16964 case SCOPE_REF:
16965 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16966 /*address_p=*/false));
16967 case ARRAY_REF:
16968 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16969 args, complain, in_decl);
16970 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16971 RECUR (TREE_OPERAND (t, 1)),
16972 complain|decltype_flag));
16974 case ARRAY_NOTATION_REF:
16976 tree start_index, length, stride;
16977 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16978 args, complain, in_decl);
16979 start_index = RECUR (ARRAY_NOTATION_START (t));
16980 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16981 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16982 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16983 length, stride, TREE_TYPE (op1)));
16985 case SIZEOF_EXPR:
16986 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16987 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16988 RETURN (tsubst_copy (t, args, complain, in_decl));
16989 /* Fall through */
16991 case ALIGNOF_EXPR:
16993 tree r;
16995 op1 = TREE_OPERAND (t, 0);
16996 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16997 op1 = TREE_TYPE (op1);
16998 if (!args)
17000 /* When there are no ARGS, we are trying to evaluate a
17001 non-dependent expression from the parser. Trying to do
17002 the substitutions may not work. */
17003 if (!TYPE_P (op1))
17004 op1 = TREE_TYPE (op1);
17006 else
17008 ++cp_unevaluated_operand;
17009 ++c_inhibit_evaluation_warnings;
17010 if (TYPE_P (op1))
17011 op1 = tsubst (op1, args, complain, in_decl);
17012 else
17013 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17014 /*function_p=*/false,
17015 /*integral_constant_expression_p=*/
17016 false);
17017 --cp_unevaluated_operand;
17018 --c_inhibit_evaluation_warnings;
17020 if (TYPE_P (op1))
17021 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17022 complain & tf_error);
17023 else
17024 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17025 complain & tf_error);
17026 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17028 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17030 if (!processing_template_decl && TYPE_P (op1))
17032 r = build_min (SIZEOF_EXPR, size_type_node,
17033 build1 (NOP_EXPR, op1, error_mark_node));
17034 SIZEOF_EXPR_TYPE_P (r) = 1;
17036 else
17037 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17038 TREE_SIDE_EFFECTS (r) = 0;
17039 TREE_READONLY (r) = 1;
17041 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17043 RETURN (r);
17046 case AT_ENCODE_EXPR:
17048 op1 = TREE_OPERAND (t, 0);
17049 ++cp_unevaluated_operand;
17050 ++c_inhibit_evaluation_warnings;
17051 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17052 /*function_p=*/false,
17053 /*integral_constant_expression_p=*/false);
17054 --cp_unevaluated_operand;
17055 --c_inhibit_evaluation_warnings;
17056 RETURN (objc_build_encode_expr (op1));
17059 case NOEXCEPT_EXPR:
17060 op1 = TREE_OPERAND (t, 0);
17061 ++cp_unevaluated_operand;
17062 ++c_inhibit_evaluation_warnings;
17063 ++cp_noexcept_operand;
17064 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17065 /*function_p=*/false,
17066 /*integral_constant_expression_p=*/false);
17067 --cp_unevaluated_operand;
17068 --c_inhibit_evaluation_warnings;
17069 --cp_noexcept_operand;
17070 RETURN (finish_noexcept_expr (op1, complain));
17072 case MODOP_EXPR:
17074 warning_sentinel s(warn_div_by_zero);
17075 tree lhs = RECUR (TREE_OPERAND (t, 0));
17076 tree rhs = RECUR (TREE_OPERAND (t, 2));
17077 tree r = build_x_modify_expr
17078 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17079 complain|decltype_flag);
17080 /* TREE_NO_WARNING must be set if either the expression was
17081 parenthesized or it uses an operator such as >>= rather
17082 than plain assignment. In the former case, it was already
17083 set and must be copied. In the latter case,
17084 build_x_modify_expr sets it and it must not be reset
17085 here. */
17086 if (TREE_NO_WARNING (t))
17087 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17089 RETURN (r);
17092 case ARROW_EXPR:
17093 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17094 args, complain, in_decl);
17095 /* Remember that there was a reference to this entity. */
17096 if (DECL_P (op1)
17097 && !mark_used (op1, complain) && !(complain & tf_error))
17098 RETURN (error_mark_node);
17099 RETURN (build_x_arrow (input_location, op1, complain));
17101 case NEW_EXPR:
17103 tree placement = RECUR (TREE_OPERAND (t, 0));
17104 tree init = RECUR (TREE_OPERAND (t, 3));
17105 vec<tree, va_gc> *placement_vec;
17106 vec<tree, va_gc> *init_vec;
17107 tree ret;
17109 if (placement == NULL_TREE)
17110 placement_vec = NULL;
17111 else
17113 placement_vec = make_tree_vector ();
17114 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17115 vec_safe_push (placement_vec, TREE_VALUE (placement));
17118 /* If there was an initializer in the original tree, but it
17119 instantiated to an empty list, then we should pass a
17120 non-NULL empty vector to tell build_new that it was an
17121 empty initializer() rather than no initializer. This can
17122 only happen when the initializer is a pack expansion whose
17123 parameter packs are of length zero. */
17124 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17125 init_vec = NULL;
17126 else
17128 init_vec = make_tree_vector ();
17129 if (init == void_node)
17130 gcc_assert (init_vec != NULL);
17131 else
17133 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17134 vec_safe_push (init_vec, TREE_VALUE (init));
17138 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17139 tree op2 = RECUR (TREE_OPERAND (t, 2));
17140 ret = build_new (&placement_vec, op1, op2, &init_vec,
17141 NEW_EXPR_USE_GLOBAL (t),
17142 complain);
17144 if (placement_vec != NULL)
17145 release_tree_vector (placement_vec);
17146 if (init_vec != NULL)
17147 release_tree_vector (init_vec);
17149 RETURN (ret);
17152 case DELETE_EXPR:
17154 tree op0 = RECUR (TREE_OPERAND (t, 0));
17155 tree op1 = RECUR (TREE_OPERAND (t, 1));
17156 RETURN (delete_sanity (op0, op1,
17157 DELETE_EXPR_USE_VEC (t),
17158 DELETE_EXPR_USE_GLOBAL (t),
17159 complain));
17162 case COMPOUND_EXPR:
17164 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17165 complain & ~tf_decltype, in_decl,
17166 /*function_p=*/false,
17167 integral_constant_expression_p);
17168 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17169 op0,
17170 RECUR (TREE_OPERAND (t, 1)),
17171 complain|decltype_flag));
17174 case CALL_EXPR:
17176 tree function;
17177 vec<tree, va_gc> *call_args;
17178 unsigned int nargs, i;
17179 bool qualified_p;
17180 bool koenig_p;
17181 tree ret;
17183 function = CALL_EXPR_FN (t);
17184 /* Internal function with no arguments. */
17185 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17186 RETURN (t);
17188 /* When we parsed the expression, we determined whether or
17189 not Koenig lookup should be performed. */
17190 koenig_p = KOENIG_LOOKUP_P (t);
17191 if (function == NULL_TREE)
17193 koenig_p = false;
17194 qualified_p = false;
17196 else if (TREE_CODE (function) == SCOPE_REF)
17198 qualified_p = true;
17199 function = tsubst_qualified_id (function, args, complain, in_decl,
17200 /*done=*/false,
17201 /*address_p=*/false);
17203 else if (koenig_p && identifier_p (function))
17205 /* Do nothing; calling tsubst_copy_and_build on an identifier
17206 would incorrectly perform unqualified lookup again.
17208 Note that we can also have an IDENTIFIER_NODE if the earlier
17209 unqualified lookup found a member function; in that case
17210 koenig_p will be false and we do want to do the lookup
17211 again to find the instantiated member function.
17213 FIXME but doing that causes c++/15272, so we need to stop
17214 using IDENTIFIER_NODE in that situation. */
17215 qualified_p = false;
17217 else
17219 if (TREE_CODE (function) == COMPONENT_REF)
17221 tree op = TREE_OPERAND (function, 1);
17223 qualified_p = (TREE_CODE (op) == SCOPE_REF
17224 || (BASELINK_P (op)
17225 && BASELINK_QUALIFIED_P (op)));
17227 else
17228 qualified_p = false;
17230 if (TREE_CODE (function) == ADDR_EXPR
17231 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17232 /* Avoid error about taking the address of a constructor. */
17233 function = TREE_OPERAND (function, 0);
17235 function = tsubst_copy_and_build (function, args, complain,
17236 in_decl,
17237 !qualified_p,
17238 integral_constant_expression_p);
17240 if (BASELINK_P (function))
17241 qualified_p = true;
17244 nargs = call_expr_nargs (t);
17245 call_args = make_tree_vector ();
17246 for (i = 0; i < nargs; ++i)
17248 tree arg = CALL_EXPR_ARG (t, i);
17250 if (!PACK_EXPANSION_P (arg))
17251 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17252 else
17254 /* Expand the pack expansion and push each entry onto
17255 CALL_ARGS. */
17256 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17257 if (TREE_CODE (arg) == TREE_VEC)
17259 unsigned int len, j;
17261 len = TREE_VEC_LENGTH (arg);
17262 for (j = 0; j < len; ++j)
17264 tree value = TREE_VEC_ELT (arg, j);
17265 if (value != NULL_TREE)
17266 value = convert_from_reference (value);
17267 vec_safe_push (call_args, value);
17270 else
17272 /* A partial substitution. Add one entry. */
17273 vec_safe_push (call_args, arg);
17278 /* We do not perform argument-dependent lookup if normal
17279 lookup finds a non-function, in accordance with the
17280 expected resolution of DR 218. */
17281 if (koenig_p
17282 && ((is_overloaded_fn (function)
17283 /* If lookup found a member function, the Koenig lookup is
17284 not appropriate, even if an unqualified-name was used
17285 to denote the function. */
17286 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17287 || identifier_p (function))
17288 /* Only do this when substitution turns a dependent call
17289 into a non-dependent call. */
17290 && type_dependent_expression_p_push (t)
17291 && !any_type_dependent_arguments_p (call_args))
17292 function = perform_koenig_lookup (function, call_args, tf_none);
17294 if (function != NULL_TREE
17295 && identifier_p (function)
17296 && !any_type_dependent_arguments_p (call_args))
17298 if (koenig_p && (complain & tf_warning_or_error))
17300 /* For backwards compatibility and good diagnostics, try
17301 the unqualified lookup again if we aren't in SFINAE
17302 context. */
17303 tree unq = (tsubst_copy_and_build
17304 (function, args, complain, in_decl, true,
17305 integral_constant_expression_p));
17306 if (unq == error_mark_node)
17308 release_tree_vector (call_args);
17309 RETURN (error_mark_node);
17312 if (unq != function)
17314 /* In a lambda fn, we have to be careful to not
17315 introduce new this captures. Legacy code can't
17316 be using lambdas anyway, so it's ok to be
17317 stricter. */
17318 bool in_lambda = (current_class_type
17319 && LAMBDA_TYPE_P (current_class_type));
17320 char const *const msg
17321 = G_("%qD was not declared in this scope, "
17322 "and no declarations were found by "
17323 "argument-dependent lookup at the point "
17324 "of instantiation");
17326 bool diag = true;
17327 if (in_lambda)
17328 error_at (EXPR_LOC_OR_LOC (t, input_location),
17329 msg, function);
17330 else
17331 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17332 msg, function);
17333 if (diag)
17335 tree fn = unq;
17337 if (INDIRECT_REF_P (fn))
17338 fn = TREE_OPERAND (fn, 0);
17339 if (is_overloaded_fn (fn))
17340 fn = get_first_fn (fn);
17342 if (!DECL_P (fn))
17343 /* Can't say anything more. */;
17344 else if (DECL_CLASS_SCOPE_P (fn))
17346 location_t loc = EXPR_LOC_OR_LOC (t,
17347 input_location);
17348 inform (loc,
17349 "declarations in dependent base %qT are "
17350 "not found by unqualified lookup",
17351 DECL_CLASS_CONTEXT (fn));
17352 if (current_class_ptr)
17353 inform (loc,
17354 "use %<this->%D%> instead", function);
17355 else
17356 inform (loc,
17357 "use %<%T::%D%> instead",
17358 current_class_name, function);
17360 else
17361 inform (DECL_SOURCE_LOCATION (fn),
17362 "%qD declared here, later in the "
17363 "translation unit", fn);
17364 if (in_lambda)
17366 release_tree_vector (call_args);
17367 RETURN (error_mark_node);
17371 function = unq;
17374 if (identifier_p (function))
17376 if (complain & tf_error)
17377 unqualified_name_lookup_error (function);
17378 release_tree_vector (call_args);
17379 RETURN (error_mark_node);
17383 /* Remember that there was a reference to this entity. */
17384 if (function != NULL_TREE
17385 && DECL_P (function)
17386 && !mark_used (function, complain) && !(complain & tf_error))
17388 release_tree_vector (call_args);
17389 RETURN (error_mark_node);
17392 /* Put back tf_decltype for the actual call. */
17393 complain |= decltype_flag;
17395 if (function == NULL_TREE)
17396 switch (CALL_EXPR_IFN (t))
17398 case IFN_LAUNDER:
17399 gcc_assert (nargs == 1);
17400 if (vec_safe_length (call_args) != 1)
17402 error_at (EXPR_LOC_OR_LOC (t, input_location),
17403 "wrong number of arguments to "
17404 "%<__builtin_launder%>");
17405 ret = error_mark_node;
17407 else
17408 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17409 input_location),
17410 (*call_args)[0], complain);
17411 break;
17413 default:
17414 /* Unsupported internal function with arguments. */
17415 gcc_unreachable ();
17417 else if (TREE_CODE (function) == OFFSET_REF)
17418 ret = build_offset_ref_call_from_tree (function, &call_args,
17419 complain);
17420 else if (TREE_CODE (function) == COMPONENT_REF)
17422 tree instance = TREE_OPERAND (function, 0);
17423 tree fn = TREE_OPERAND (function, 1);
17425 if (processing_template_decl
17426 && (type_dependent_expression_p (instance)
17427 || (!BASELINK_P (fn)
17428 && TREE_CODE (fn) != FIELD_DECL)
17429 || type_dependent_expression_p (fn)
17430 || any_type_dependent_arguments_p (call_args)))
17431 ret = build_min_nt_call_vec (function, call_args);
17432 else if (!BASELINK_P (fn))
17433 ret = finish_call_expr (function, &call_args,
17434 /*disallow_virtual=*/false,
17435 /*koenig_p=*/false,
17436 complain);
17437 else
17438 ret = (build_new_method_call
17439 (instance, fn,
17440 &call_args, NULL_TREE,
17441 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17442 /*fn_p=*/NULL,
17443 complain));
17445 else
17446 ret = finish_call_expr (function, &call_args,
17447 /*disallow_virtual=*/qualified_p,
17448 koenig_p,
17449 complain);
17451 release_tree_vector (call_args);
17453 if (ret != error_mark_node)
17455 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17456 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17457 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17458 bool thk = CALL_FROM_THUNK_P (t);
17459 if (op || ord || rev || thk)
17461 function = extract_call_expr (ret);
17462 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17463 CALL_EXPR_ORDERED_ARGS (function) = ord;
17464 CALL_EXPR_REVERSE_ARGS (function) = rev;
17465 if (thk)
17467 CALL_FROM_THUNK_P (function) = true;
17468 /* The thunk location is not interesting. */
17469 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17474 RETURN (ret);
17477 case COND_EXPR:
17479 tree cond = RECUR (TREE_OPERAND (t, 0));
17480 tree folded_cond = fold_non_dependent_expr (cond);
17481 tree exp1, exp2;
17483 if (TREE_CODE (folded_cond) == INTEGER_CST)
17485 if (integer_zerop (folded_cond))
17487 ++c_inhibit_evaluation_warnings;
17488 exp1 = RECUR (TREE_OPERAND (t, 1));
17489 --c_inhibit_evaluation_warnings;
17490 exp2 = RECUR (TREE_OPERAND (t, 2));
17492 else
17494 exp1 = RECUR (TREE_OPERAND (t, 1));
17495 ++c_inhibit_evaluation_warnings;
17496 exp2 = RECUR (TREE_OPERAND (t, 2));
17497 --c_inhibit_evaluation_warnings;
17499 cond = folded_cond;
17501 else
17503 exp1 = RECUR (TREE_OPERAND (t, 1));
17504 exp2 = RECUR (TREE_OPERAND (t, 2));
17507 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17508 cond, exp1, exp2, complain));
17511 case PSEUDO_DTOR_EXPR:
17513 tree op0 = RECUR (TREE_OPERAND (t, 0));
17514 tree op1 = RECUR (TREE_OPERAND (t, 1));
17515 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17516 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17517 input_location));
17520 case TREE_LIST:
17522 tree purpose, value, chain;
17524 if (t == void_list_node)
17525 RETURN (t);
17527 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17528 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17530 /* We have pack expansions, so expand those and
17531 create a new list out of it. */
17532 tree purposevec = NULL_TREE;
17533 tree valuevec = NULL_TREE;
17534 tree chain;
17535 int i, len = -1;
17537 /* Expand the argument expressions. */
17538 if (TREE_PURPOSE (t))
17539 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17540 complain, in_decl);
17541 if (TREE_VALUE (t))
17542 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17543 complain, in_decl);
17545 /* Build the rest of the list. */
17546 chain = TREE_CHAIN (t);
17547 if (chain && chain != void_type_node)
17548 chain = RECUR (chain);
17550 /* Determine the number of arguments. */
17551 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17553 len = TREE_VEC_LENGTH (purposevec);
17554 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17556 else if (TREE_CODE (valuevec) == TREE_VEC)
17557 len = TREE_VEC_LENGTH (valuevec);
17558 else
17560 /* Since we only performed a partial substitution into
17561 the argument pack, we only RETURN (a single list
17562 node. */
17563 if (purposevec == TREE_PURPOSE (t)
17564 && valuevec == TREE_VALUE (t)
17565 && chain == TREE_CHAIN (t))
17566 RETURN (t);
17568 RETURN (tree_cons (purposevec, valuevec, chain));
17571 /* Convert the argument vectors into a TREE_LIST */
17572 i = len;
17573 while (i > 0)
17575 /* Grab the Ith values. */
17576 i--;
17577 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17578 : NULL_TREE;
17579 value
17580 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17581 : NULL_TREE;
17583 /* Build the list (backwards). */
17584 chain = tree_cons (purpose, value, chain);
17587 RETURN (chain);
17590 purpose = TREE_PURPOSE (t);
17591 if (purpose)
17592 purpose = RECUR (purpose);
17593 value = TREE_VALUE (t);
17594 if (value)
17595 value = RECUR (value);
17596 chain = TREE_CHAIN (t);
17597 if (chain && chain != void_type_node)
17598 chain = RECUR (chain);
17599 if (purpose == TREE_PURPOSE (t)
17600 && value == TREE_VALUE (t)
17601 && chain == TREE_CHAIN (t))
17602 RETURN (t);
17603 RETURN (tree_cons (purpose, value, chain));
17606 case COMPONENT_REF:
17608 tree object;
17609 tree object_type;
17610 tree member;
17611 tree r;
17613 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17614 args, complain, in_decl);
17615 /* Remember that there was a reference to this entity. */
17616 if (DECL_P (object)
17617 && !mark_used (object, complain) && !(complain & tf_error))
17618 RETURN (error_mark_node);
17619 object_type = TREE_TYPE (object);
17621 member = TREE_OPERAND (t, 1);
17622 if (BASELINK_P (member))
17623 member = tsubst_baselink (member,
17624 non_reference (TREE_TYPE (object)),
17625 args, complain, in_decl);
17626 else
17627 member = tsubst_copy (member, args, complain, in_decl);
17628 if (member == error_mark_node)
17629 RETURN (error_mark_node);
17631 if (TREE_CODE (member) == FIELD_DECL)
17633 r = finish_non_static_data_member (member, object, NULL_TREE);
17634 if (TREE_CODE (r) == COMPONENT_REF)
17635 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17636 RETURN (r);
17638 else if (type_dependent_expression_p (object))
17639 /* We can't do much here. */;
17640 else if (!CLASS_TYPE_P (object_type))
17642 if (scalarish_type_p (object_type))
17644 tree s = NULL_TREE;
17645 tree dtor = member;
17647 if (TREE_CODE (dtor) == SCOPE_REF)
17649 s = TREE_OPERAND (dtor, 0);
17650 dtor = TREE_OPERAND (dtor, 1);
17652 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17654 dtor = TREE_OPERAND (dtor, 0);
17655 if (TYPE_P (dtor))
17656 RETURN (finish_pseudo_destructor_expr
17657 (object, s, dtor, input_location));
17661 else if (TREE_CODE (member) == SCOPE_REF
17662 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17664 /* Lookup the template functions now that we know what the
17665 scope is. */
17666 tree scope = TREE_OPERAND (member, 0);
17667 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17668 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17669 member = lookup_qualified_name (scope, tmpl,
17670 /*is_type_p=*/false,
17671 /*complain=*/false);
17672 if (BASELINK_P (member))
17674 BASELINK_FUNCTIONS (member)
17675 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17676 args);
17677 member = (adjust_result_of_qualified_name_lookup
17678 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17679 object_type));
17681 else
17683 qualified_name_lookup_error (scope, tmpl, member,
17684 input_location);
17685 RETURN (error_mark_node);
17688 else if (TREE_CODE (member) == SCOPE_REF
17689 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17690 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17692 if (complain & tf_error)
17694 if (TYPE_P (TREE_OPERAND (member, 0)))
17695 error ("%qT is not a class or namespace",
17696 TREE_OPERAND (member, 0));
17697 else
17698 error ("%qD is not a class or namespace",
17699 TREE_OPERAND (member, 0));
17701 RETURN (error_mark_node);
17704 r = finish_class_member_access_expr (object, member,
17705 /*template_p=*/false,
17706 complain);
17707 if (TREE_CODE (r) == COMPONENT_REF)
17708 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17709 RETURN (r);
17712 case THROW_EXPR:
17713 RETURN (build_throw
17714 (RECUR (TREE_OPERAND (t, 0))));
17716 case CONSTRUCTOR:
17718 vec<constructor_elt, va_gc> *n;
17719 constructor_elt *ce;
17720 unsigned HOST_WIDE_INT idx;
17721 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17722 bool process_index_p;
17723 int newlen;
17724 bool need_copy_p = false;
17725 tree r;
17727 if (type == error_mark_node)
17728 RETURN (error_mark_node);
17730 /* digest_init will do the wrong thing if we let it. */
17731 if (type && TYPE_PTRMEMFUNC_P (type))
17732 RETURN (t);
17734 /* We do not want to process the index of aggregate
17735 initializers as they are identifier nodes which will be
17736 looked up by digest_init. */
17737 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17739 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17740 newlen = vec_safe_length (n);
17741 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17743 if (ce->index && process_index_p
17744 /* An identifier index is looked up in the type
17745 being initialized, not the current scope. */
17746 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17747 ce->index = RECUR (ce->index);
17749 if (PACK_EXPANSION_P (ce->value))
17751 /* Substitute into the pack expansion. */
17752 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17753 in_decl);
17755 if (ce->value == error_mark_node
17756 || PACK_EXPANSION_P (ce->value))
17758 else if (TREE_VEC_LENGTH (ce->value) == 1)
17759 /* Just move the argument into place. */
17760 ce->value = TREE_VEC_ELT (ce->value, 0);
17761 else
17763 /* Update the length of the final CONSTRUCTOR
17764 arguments vector, and note that we will need to
17765 copy.*/
17766 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17767 need_copy_p = true;
17770 else
17771 ce->value = RECUR (ce->value);
17774 if (need_copy_p)
17776 vec<constructor_elt, va_gc> *old_n = n;
17778 vec_alloc (n, newlen);
17779 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17781 if (TREE_CODE (ce->value) == TREE_VEC)
17783 int i, len = TREE_VEC_LENGTH (ce->value);
17784 for (i = 0; i < len; ++i)
17785 CONSTRUCTOR_APPEND_ELT (n, 0,
17786 TREE_VEC_ELT (ce->value, i));
17788 else
17789 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17793 r = build_constructor (init_list_type_node, n);
17794 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17796 if (TREE_HAS_CONSTRUCTOR (t))
17798 fcl_t cl = fcl_functional;
17799 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17800 cl = fcl_c99;
17801 RETURN (finish_compound_literal (type, r, complain, cl));
17804 TREE_TYPE (r) = type;
17805 RETURN (r);
17808 case TYPEID_EXPR:
17810 tree operand_0 = TREE_OPERAND (t, 0);
17811 if (TYPE_P (operand_0))
17813 operand_0 = tsubst (operand_0, args, complain, in_decl);
17814 RETURN (get_typeid (operand_0, complain));
17816 else
17818 operand_0 = RECUR (operand_0);
17819 RETURN (build_typeid (operand_0, complain));
17823 case VAR_DECL:
17824 if (!args)
17825 RETURN (t);
17826 else if (DECL_PACK_P (t))
17828 /* We don't build decls for an instantiation of a
17829 variadic capture proxy, we instantiate the elements
17830 when needed. */
17831 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17832 return RECUR (DECL_VALUE_EXPR (t));
17834 /* Fall through */
17836 case PARM_DECL:
17838 tree r = tsubst_copy (t, args, complain, in_decl);
17839 /* ??? We're doing a subset of finish_id_expression here. */
17840 if (VAR_P (r)
17841 && !processing_template_decl
17842 && !cp_unevaluated_operand
17843 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17844 && CP_DECL_THREAD_LOCAL_P (r))
17846 if (tree wrap = get_tls_wrapper_fn (r))
17847 /* Replace an evaluated use of the thread_local variable with
17848 a call to its wrapper. */
17849 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17851 else if (outer_automatic_var_p (r))
17853 r = process_outer_var_ref (r, complain);
17854 if (is_capture_proxy (r))
17855 register_local_specialization (r, t);
17858 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17859 /* If the original type was a reference, we'll be wrapped in
17860 the appropriate INDIRECT_REF. */
17861 r = convert_from_reference (r);
17862 RETURN (r);
17865 case VA_ARG_EXPR:
17867 tree op0 = RECUR (TREE_OPERAND (t, 0));
17868 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17869 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17872 case OFFSETOF_EXPR:
17874 tree object_ptr
17875 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17876 in_decl, /*function_p=*/false,
17877 /*integral_constant_expression_p=*/false);
17878 RETURN (finish_offsetof (object_ptr,
17879 RECUR (TREE_OPERAND (t, 0)),
17880 EXPR_LOCATION (t)));
17883 case ADDRESSOF_EXPR:
17884 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17885 RECUR (TREE_OPERAND (t, 0)), complain));
17887 case TRAIT_EXPR:
17889 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17890 complain, in_decl);
17892 tree type2 = TRAIT_EXPR_TYPE2 (t);
17893 if (type2 && TREE_CODE (type2) == TREE_LIST)
17894 type2 = RECUR (type2);
17895 else if (type2)
17896 type2 = tsubst (type2, args, complain, in_decl);
17898 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17901 case STMT_EXPR:
17903 tree old_stmt_expr = cur_stmt_expr;
17904 tree stmt_expr = begin_stmt_expr ();
17906 cur_stmt_expr = stmt_expr;
17907 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17908 integral_constant_expression_p);
17909 stmt_expr = finish_stmt_expr (stmt_expr, false);
17910 cur_stmt_expr = old_stmt_expr;
17912 /* If the resulting list of expression statement is empty,
17913 fold it further into void_node. */
17914 if (empty_expr_stmt_p (stmt_expr))
17915 stmt_expr = void_node;
17917 RETURN (stmt_expr);
17920 case LAMBDA_EXPR:
17922 tree r = build_lambda_expr ();
17924 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17925 LAMBDA_EXPR_CLOSURE (r) = type;
17926 CLASSTYPE_LAMBDA_EXPR (type) = r;
17928 LAMBDA_EXPR_LOCATION (r)
17929 = LAMBDA_EXPR_LOCATION (t);
17930 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17931 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17932 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17933 LAMBDA_EXPR_DISCRIMINATOR (r)
17934 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17935 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17936 if (!scope)
17937 /* No substitution needed. */;
17938 else if (VAR_OR_FUNCTION_DECL_P (scope))
17939 /* For a function or variable scope, we want to use tsubst so that we
17940 don't complain about referring to an auto before deduction. */
17941 scope = tsubst (scope, args, complain, in_decl);
17942 else if (TREE_CODE (scope) == PARM_DECL)
17944 /* Look up the parameter we want directly, as tsubst_copy
17945 doesn't do what we need. */
17946 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17947 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17948 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17949 parm = DECL_CHAIN (parm);
17950 scope = parm;
17951 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17952 if (DECL_CONTEXT (scope) == NULL_TREE)
17953 DECL_CONTEXT (scope) = fn;
17955 else if (TREE_CODE (scope) == FIELD_DECL)
17956 /* For a field, use tsubst_copy so that we look up the existing field
17957 rather than build a new one. */
17958 scope = RECUR (scope);
17959 else
17960 gcc_unreachable ();
17961 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17963 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17964 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17966 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17967 determine_visibility (TYPE_NAME (type));
17968 /* Now that we know visibility, instantiate the type so we have a
17969 declaration of the op() for later calls to lambda_function. */
17970 complete_type (type);
17972 if (tree fn = lambda_function (type))
17973 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17975 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17977 insert_pending_capture_proxies ();
17979 RETURN (build_lambda_object (r));
17982 case TARGET_EXPR:
17983 /* We can get here for a constant initializer of non-dependent type.
17984 FIXME stop folding in cp_parser_initializer_clause. */
17986 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17987 complain);
17988 RETURN (r);
17991 case TRANSACTION_EXPR:
17992 RETURN (tsubst_expr(t, args, complain, in_decl,
17993 integral_constant_expression_p));
17995 case PAREN_EXPR:
17996 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17998 case VEC_PERM_EXPR:
18000 tree op0 = RECUR (TREE_OPERAND (t, 0));
18001 tree op1 = RECUR (TREE_OPERAND (t, 1));
18002 tree op2 = RECUR (TREE_OPERAND (t, 2));
18003 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18004 complain));
18007 case REQUIRES_EXPR:
18008 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18010 default:
18011 /* Handle Objective-C++ constructs, if appropriate. */
18013 tree subst
18014 = objcp_tsubst_copy_and_build (t, args, complain,
18015 in_decl, /*function_p=*/false);
18016 if (subst)
18017 RETURN (subst);
18019 RETURN (tsubst_copy (t, args, complain, in_decl));
18022 #undef RECUR
18023 #undef RETURN
18024 out:
18025 input_location = loc;
18026 return retval;
18029 /* Verify that the instantiated ARGS are valid. For type arguments,
18030 make sure that the type's linkage is ok. For non-type arguments,
18031 make sure they are constants if they are integral or enumerations.
18032 Emit an error under control of COMPLAIN, and return TRUE on error. */
18034 static bool
18035 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18037 if (dependent_template_arg_p (t))
18038 return false;
18039 if (ARGUMENT_PACK_P (t))
18041 tree vec = ARGUMENT_PACK_ARGS (t);
18042 int len = TREE_VEC_LENGTH (vec);
18043 bool result = false;
18044 int i;
18046 for (i = 0; i < len; ++i)
18047 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18048 result = true;
18049 return result;
18051 else if (TYPE_P (t))
18053 /* [basic.link]: A name with no linkage (notably, the name
18054 of a class or enumeration declared in a local scope)
18055 shall not be used to declare an entity with linkage.
18056 This implies that names with no linkage cannot be used as
18057 template arguments
18059 DR 757 relaxes this restriction for C++0x. */
18060 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18061 : no_linkage_check (t, /*relaxed_p=*/false));
18063 if (nt)
18065 /* DR 488 makes use of a type with no linkage cause
18066 type deduction to fail. */
18067 if (complain & tf_error)
18069 if (TYPE_UNNAMED_P (nt))
18070 error ("%qT is/uses unnamed type", t);
18071 else
18072 error ("template argument for %qD uses local type %qT",
18073 tmpl, t);
18075 return true;
18077 /* In order to avoid all sorts of complications, we do not
18078 allow variably-modified types as template arguments. */
18079 else if (variably_modified_type_p (t, NULL_TREE))
18081 if (complain & tf_error)
18082 error ("%qT is a variably modified type", t);
18083 return true;
18086 /* Class template and alias template arguments should be OK. */
18087 else if (DECL_TYPE_TEMPLATE_P (t))
18089 /* A non-type argument of integral or enumerated type must be a
18090 constant. */
18091 else if (TREE_TYPE (t)
18092 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18093 && !REFERENCE_REF_P (t)
18094 && !TREE_CONSTANT (t))
18096 if (complain & tf_error)
18097 error ("integral expression %qE is not constant", t);
18098 return true;
18100 return false;
18103 static bool
18104 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18106 int ix, len = DECL_NTPARMS (tmpl);
18107 bool result = false;
18109 for (ix = 0; ix != len; ix++)
18111 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18112 result = true;
18114 if (result && (complain & tf_error))
18115 error (" trying to instantiate %qD", tmpl);
18116 return result;
18119 /* We're out of SFINAE context now, so generate diagnostics for the access
18120 errors we saw earlier when instantiating D from TMPL and ARGS. */
18122 static void
18123 recheck_decl_substitution (tree d, tree tmpl, tree args)
18125 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18126 tree type = TREE_TYPE (pattern);
18127 location_t loc = input_location;
18129 push_access_scope (d);
18130 push_deferring_access_checks (dk_no_deferred);
18131 input_location = DECL_SOURCE_LOCATION (pattern);
18132 tsubst (type, args, tf_warning_or_error, d);
18133 input_location = loc;
18134 pop_deferring_access_checks ();
18135 pop_access_scope (d);
18138 /* Instantiate the indicated variable, function, or alias template TMPL with
18139 the template arguments in TARG_PTR. */
18141 static tree
18142 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18144 tree targ_ptr = orig_args;
18145 tree fndecl;
18146 tree gen_tmpl;
18147 tree spec;
18148 bool access_ok = true;
18150 if (tmpl == error_mark_node)
18151 return error_mark_node;
18153 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18155 /* If this function is a clone, handle it specially. */
18156 if (DECL_CLONED_FUNCTION_P (tmpl))
18158 tree spec;
18159 tree clone;
18161 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18162 DECL_CLONED_FUNCTION. */
18163 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18164 targ_ptr, complain);
18165 if (spec == error_mark_node)
18166 return error_mark_node;
18168 /* Look for the clone. */
18169 FOR_EACH_CLONE (clone, spec)
18170 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18171 return clone;
18172 /* We should always have found the clone by now. */
18173 gcc_unreachable ();
18174 return NULL_TREE;
18177 if (targ_ptr == error_mark_node)
18178 return error_mark_node;
18180 /* Check to see if we already have this specialization. */
18181 gen_tmpl = most_general_template (tmpl);
18182 if (TMPL_ARGS_DEPTH (targ_ptr)
18183 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18184 /* targ_ptr only has the innermost template args, so add the outer ones
18185 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18186 the case of a non-dependent call within a template definition). */
18187 targ_ptr = (add_outermost_template_args
18188 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18189 targ_ptr));
18191 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18192 but it doesn't seem to be on the hot path. */
18193 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18195 gcc_assert (tmpl == gen_tmpl
18196 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18197 == spec)
18198 || fndecl == NULL_TREE);
18200 if (spec != NULL_TREE)
18202 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18204 if (complain & tf_error)
18205 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18206 return error_mark_node;
18208 return spec;
18211 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18212 complain))
18213 return error_mark_node;
18215 /* We are building a FUNCTION_DECL, during which the access of its
18216 parameters and return types have to be checked. However this
18217 FUNCTION_DECL which is the desired context for access checking
18218 is not built yet. We solve this chicken-and-egg problem by
18219 deferring all checks until we have the FUNCTION_DECL. */
18220 push_deferring_access_checks (dk_deferred);
18222 /* Instantiation of the function happens in the context of the function
18223 template, not the context of the overload resolution we're doing. */
18224 push_to_top_level ();
18225 /* If there are dependent arguments, e.g. because we're doing partial
18226 ordering, make sure processing_template_decl stays set. */
18227 if (uses_template_parms (targ_ptr))
18228 ++processing_template_decl;
18229 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18231 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18232 complain, gen_tmpl, true);
18233 push_nested_class (ctx);
18236 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18238 fndecl = NULL_TREE;
18239 if (VAR_P (pattern))
18241 /* We need to determine if we're using a partial or explicit
18242 specialization now, because the type of the variable could be
18243 different. */
18244 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18245 tree elt = most_specialized_partial_spec (tid, complain);
18246 if (elt == error_mark_node)
18247 pattern = error_mark_node;
18248 else if (elt)
18250 tree partial_tmpl = TREE_VALUE (elt);
18251 tree partial_args = TREE_PURPOSE (elt);
18252 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18253 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18257 /* Substitute template parameters to obtain the specialization. */
18258 if (fndecl == NULL_TREE)
18259 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18260 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18261 pop_nested_class ();
18262 pop_from_top_level ();
18264 if (fndecl == error_mark_node)
18266 pop_deferring_access_checks ();
18267 return error_mark_node;
18270 /* The DECL_TI_TEMPLATE should always be the immediate parent
18271 template, not the most general template. */
18272 DECL_TI_TEMPLATE (fndecl) = tmpl;
18273 DECL_TI_ARGS (fndecl) = targ_ptr;
18275 /* Now we know the specialization, compute access previously
18276 deferred. Do no access control for inheriting constructors,
18277 as we already checked access for the inherited constructor. */
18278 if (!(flag_new_inheriting_ctors
18279 && DECL_INHERITED_CTOR (fndecl)))
18281 push_access_scope (fndecl);
18282 if (!perform_deferred_access_checks (complain))
18283 access_ok = false;
18284 pop_access_scope (fndecl);
18286 pop_deferring_access_checks ();
18288 /* If we've just instantiated the main entry point for a function,
18289 instantiate all the alternate entry points as well. We do this
18290 by cloning the instantiation of the main entry point, not by
18291 instantiating the template clones. */
18292 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18293 clone_function_decl (fndecl, /*update_methods=*/false);
18295 if (!access_ok)
18297 if (!(complain & tf_error))
18299 /* Remember to reinstantiate when we're out of SFINAE so the user
18300 can see the errors. */
18301 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18303 return error_mark_node;
18305 return fndecl;
18308 /* Wrapper for instantiate_template_1. */
18310 tree
18311 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18313 tree ret;
18314 timevar_push (TV_TEMPLATE_INST);
18315 ret = instantiate_template_1 (tmpl, orig_args, complain);
18316 timevar_pop (TV_TEMPLATE_INST);
18317 return ret;
18320 /* Instantiate the alias template TMPL with ARGS. Also push a template
18321 instantiation level, which instantiate_template doesn't do because
18322 functions and variables have sufficient context established by the
18323 callers. */
18325 static tree
18326 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18328 struct pending_template *old_last_pend = last_pending_template;
18329 struct tinst_level *old_error_tinst = last_error_tinst_level;
18330 if (tmpl == error_mark_node || args == error_mark_node)
18331 return error_mark_node;
18332 tree tinst = build_tree_list (tmpl, args);
18333 if (!push_tinst_level (tinst))
18335 ggc_free (tinst);
18336 return error_mark_node;
18339 args =
18340 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18341 args, tmpl, complain,
18342 /*require_all_args=*/true,
18343 /*use_default_args=*/true);
18345 tree r = instantiate_template (tmpl, args, complain);
18346 pop_tinst_level ();
18347 /* We can't free this if a pending_template entry or last_error_tinst_level
18348 is pointing at it. */
18349 if (last_pending_template == old_last_pend
18350 && last_error_tinst_level == old_error_tinst)
18351 ggc_free (tinst);
18353 return r;
18356 /* PARM is a template parameter pack for FN. Returns true iff
18357 PARM is used in a deducible way in the argument list of FN. */
18359 static bool
18360 pack_deducible_p (tree parm, tree fn)
18362 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18363 for (; t; t = TREE_CHAIN (t))
18365 tree type = TREE_VALUE (t);
18366 tree packs;
18367 if (!PACK_EXPANSION_P (type))
18368 continue;
18369 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18370 packs; packs = TREE_CHAIN (packs))
18371 if (template_args_equal (TREE_VALUE (packs), parm))
18373 /* The template parameter pack is used in a function parameter
18374 pack. If this is the end of the parameter list, the
18375 template parameter pack is deducible. */
18376 if (TREE_CHAIN (t) == void_list_node)
18377 return true;
18378 else
18379 /* Otherwise, not. Well, it could be deduced from
18380 a non-pack parameter, but doing so would end up with
18381 a deduction mismatch, so don't bother. */
18382 return false;
18385 /* The template parameter pack isn't used in any function parameter
18386 packs, but it might be used deeper, e.g. tuple<Args...>. */
18387 return true;
18390 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18391 NARGS elements of the arguments that are being used when calling
18392 it. TARGS is a vector into which the deduced template arguments
18393 are placed.
18395 Returns either a FUNCTION_DECL for the matching specialization of FN or
18396 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18397 true, diagnostics will be printed to explain why it failed.
18399 If FN is a conversion operator, or we are trying to produce a specific
18400 specialization, RETURN_TYPE is the return type desired.
18402 The EXPLICIT_TARGS are explicit template arguments provided via a
18403 template-id.
18405 The parameter STRICT is one of:
18407 DEDUCE_CALL:
18408 We are deducing arguments for a function call, as in
18409 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18410 deducing arguments for a call to the result of a conversion
18411 function template, as in [over.call.object].
18413 DEDUCE_CONV:
18414 We are deducing arguments for a conversion function, as in
18415 [temp.deduct.conv].
18417 DEDUCE_EXACT:
18418 We are deducing arguments when doing an explicit instantiation
18419 as in [temp.explicit], when determining an explicit specialization
18420 as in [temp.expl.spec], or when taking the address of a function
18421 template, as in [temp.deduct.funcaddr]. */
18423 tree
18424 fn_type_unification (tree fn,
18425 tree explicit_targs,
18426 tree targs,
18427 const tree *args,
18428 unsigned int nargs,
18429 tree return_type,
18430 unification_kind_t strict,
18431 int flags,
18432 bool explain_p,
18433 bool decltype_p)
18435 tree parms;
18436 tree fntype;
18437 tree decl = NULL_TREE;
18438 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18439 bool ok;
18440 static int deduction_depth;
18441 struct pending_template *old_last_pend = last_pending_template;
18442 struct tinst_level *old_error_tinst = last_error_tinst_level;
18444 tree orig_fn = fn;
18445 if (flag_new_inheriting_ctors)
18446 fn = strip_inheriting_ctors (fn);
18448 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18449 tree tinst;
18450 tree r = error_mark_node;
18452 tree full_targs = targs;
18453 if (TMPL_ARGS_DEPTH (targs)
18454 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18455 full_targs = (add_outermost_template_args
18456 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18457 targs));
18459 if (decltype_p)
18460 complain |= tf_decltype;
18462 /* In C++0x, it's possible to have a function template whose type depends
18463 on itself recursively. This is most obvious with decltype, but can also
18464 occur with enumeration scope (c++/48969). So we need to catch infinite
18465 recursion and reject the substitution at deduction time; this function
18466 will return error_mark_node for any repeated substitution.
18468 This also catches excessive recursion such as when f<N> depends on
18469 f<N-1> across all integers, and returns error_mark_node for all the
18470 substitutions back up to the initial one.
18472 This is, of course, not reentrant. */
18473 if (excessive_deduction_depth)
18474 return error_mark_node;
18475 tinst = build_tree_list (fn, NULL_TREE);
18476 ++deduction_depth;
18478 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18480 fntype = TREE_TYPE (fn);
18481 if (explicit_targs)
18483 /* [temp.deduct]
18485 The specified template arguments must match the template
18486 parameters in kind (i.e., type, nontype, template), and there
18487 must not be more arguments than there are parameters;
18488 otherwise type deduction fails.
18490 Nontype arguments must match the types of the corresponding
18491 nontype template parameters, or must be convertible to the
18492 types of the corresponding nontype parameters as specified in
18493 _temp.arg.nontype_, otherwise type deduction fails.
18495 All references in the function type of the function template
18496 to the corresponding template parameters are replaced by the
18497 specified template argument values. If a substitution in a
18498 template parameter or in the function type of the function
18499 template results in an invalid type, type deduction fails. */
18500 int i, len = TREE_VEC_LENGTH (tparms);
18501 location_t loc = input_location;
18502 bool incomplete = false;
18504 if (explicit_targs == error_mark_node)
18505 goto fail;
18507 if (TMPL_ARGS_DEPTH (explicit_targs)
18508 < TMPL_ARGS_DEPTH (full_targs))
18509 explicit_targs = add_outermost_template_args (full_targs,
18510 explicit_targs);
18512 /* Adjust any explicit template arguments before entering the
18513 substitution context. */
18514 explicit_targs
18515 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18516 complain,
18517 /*require_all_args=*/false,
18518 /*use_default_args=*/false));
18519 if (explicit_targs == error_mark_node)
18520 goto fail;
18522 /* Substitute the explicit args into the function type. This is
18523 necessary so that, for instance, explicitly declared function
18524 arguments can match null pointed constants. If we were given
18525 an incomplete set of explicit args, we must not do semantic
18526 processing during substitution as we could create partial
18527 instantiations. */
18528 for (i = 0; i < len; i++)
18530 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18531 bool parameter_pack = false;
18532 tree targ = TREE_VEC_ELT (explicit_targs, i);
18534 /* Dig out the actual parm. */
18535 if (TREE_CODE (parm) == TYPE_DECL
18536 || TREE_CODE (parm) == TEMPLATE_DECL)
18538 parm = TREE_TYPE (parm);
18539 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18541 else if (TREE_CODE (parm) == PARM_DECL)
18543 parm = DECL_INITIAL (parm);
18544 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18547 if (!parameter_pack && targ == NULL_TREE)
18548 /* No explicit argument for this template parameter. */
18549 incomplete = true;
18551 if (parameter_pack && pack_deducible_p (parm, fn))
18553 /* Mark the argument pack as "incomplete". We could
18554 still deduce more arguments during unification.
18555 We remove this mark in type_unification_real. */
18556 if (targ)
18558 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18559 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18560 = ARGUMENT_PACK_ARGS (targ);
18563 /* We have some incomplete argument packs. */
18564 incomplete = true;
18568 TREE_VALUE (tinst) = explicit_targs;
18569 if (!push_tinst_level (tinst))
18571 excessive_deduction_depth = true;
18572 goto fail;
18574 processing_template_decl += incomplete;
18575 input_location = DECL_SOURCE_LOCATION (fn);
18576 /* Ignore any access checks; we'll see them again in
18577 instantiate_template and they might have the wrong
18578 access path at this point. */
18579 push_deferring_access_checks (dk_deferred);
18580 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18581 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18582 pop_deferring_access_checks ();
18583 input_location = loc;
18584 processing_template_decl -= incomplete;
18585 pop_tinst_level ();
18587 if (fntype == error_mark_node)
18588 goto fail;
18590 /* Place the explicitly specified arguments in TARGS. */
18591 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18592 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18593 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18596 /* Never do unification on the 'this' parameter. */
18597 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18599 if (return_type && strict == DEDUCE_CALL)
18601 /* We're deducing for a call to the result of a template conversion
18602 function. The parms we really want are in return_type. */
18603 if (POINTER_TYPE_P (return_type))
18604 return_type = TREE_TYPE (return_type);
18605 parms = TYPE_ARG_TYPES (return_type);
18607 else if (return_type)
18609 tree *new_args;
18611 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18612 new_args = XALLOCAVEC (tree, nargs + 1);
18613 new_args[0] = return_type;
18614 memcpy (new_args + 1, args, nargs * sizeof (tree));
18615 args = new_args;
18616 ++nargs;
18619 /* We allow incomplete unification without an error message here
18620 because the standard doesn't seem to explicitly prohibit it. Our
18621 callers must be ready to deal with unification failures in any
18622 event. */
18624 TREE_VALUE (tinst) = targs;
18625 /* If we aren't explaining yet, push tinst context so we can see where
18626 any errors (e.g. from class instantiations triggered by instantiation
18627 of default template arguments) come from. If we are explaining, this
18628 context is redundant. */
18629 if (!explain_p && !push_tinst_level (tinst))
18631 excessive_deduction_depth = true;
18632 goto fail;
18635 /* type_unification_real will pass back any access checks from default
18636 template argument substitution. */
18637 vec<deferred_access_check, va_gc> *checks;
18638 checks = NULL;
18640 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18641 full_targs, parms, args, nargs, /*subr=*/0,
18642 strict, flags, &checks, explain_p);
18643 if (!explain_p)
18644 pop_tinst_level ();
18645 if (!ok)
18646 goto fail;
18648 /* Now that we have bindings for all of the template arguments,
18649 ensure that the arguments deduced for the template template
18650 parameters have compatible template parameter lists. We cannot
18651 check this property before we have deduced all template
18652 arguments, because the template parameter types of a template
18653 template parameter might depend on prior template parameters
18654 deduced after the template template parameter. The following
18655 ill-formed example illustrates this issue:
18657 template<typename T, template<T> class C> void f(C<5>, T);
18659 template<int N> struct X {};
18661 void g() {
18662 f(X<5>(), 5l); // error: template argument deduction fails
18665 The template parameter list of 'C' depends on the template type
18666 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18667 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18668 time that we deduce 'C'. */
18669 if (!template_template_parm_bindings_ok_p
18670 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18672 unify_inconsistent_template_template_parameters (explain_p);
18673 goto fail;
18676 /* All is well so far. Now, check:
18678 [temp.deduct]
18680 When all template arguments have been deduced, all uses of
18681 template parameters in nondeduced contexts are replaced with
18682 the corresponding deduced argument values. If the
18683 substitution results in an invalid type, as described above,
18684 type deduction fails. */
18685 TREE_VALUE (tinst) = targs;
18686 if (!push_tinst_level (tinst))
18688 excessive_deduction_depth = true;
18689 goto fail;
18692 /* Also collect access checks from the instantiation. */
18693 reopen_deferring_access_checks (checks);
18695 decl = instantiate_template (fn, targs, complain);
18697 checks = get_deferred_access_checks ();
18698 pop_deferring_access_checks ();
18700 pop_tinst_level ();
18702 if (decl == error_mark_node)
18703 goto fail;
18705 /* Now perform any access checks encountered during substitution. */
18706 push_access_scope (decl);
18707 ok = perform_access_checks (checks, complain);
18708 pop_access_scope (decl);
18709 if (!ok)
18710 goto fail;
18712 /* If we're looking for an exact match, check that what we got
18713 is indeed an exact match. It might not be if some template
18714 parameters are used in non-deduced contexts. But don't check
18715 for an exact match if we have dependent template arguments;
18716 in that case we're doing partial ordering, and we already know
18717 that we have two candidates that will provide the actual type. */
18718 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18720 tree substed = TREE_TYPE (decl);
18721 unsigned int i;
18723 tree sarg
18724 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18725 if (return_type)
18726 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18727 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18728 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18730 unify_type_mismatch (explain_p, args[i],
18731 TREE_VALUE (sarg));
18732 goto fail;
18736 /* After doing deduction with the inherited constructor, actually return an
18737 instantiation of the inheriting constructor. */
18738 if (orig_fn != fn)
18739 decl = instantiate_template (orig_fn, targs, complain);
18741 r = decl;
18743 fail:
18744 --deduction_depth;
18745 if (excessive_deduction_depth)
18747 if (deduction_depth == 0)
18748 /* Reset once we're all the way out. */
18749 excessive_deduction_depth = false;
18752 /* We can't free this if a pending_template entry or last_error_tinst_level
18753 is pointing at it. */
18754 if (last_pending_template == old_last_pend
18755 && last_error_tinst_level == old_error_tinst)
18756 ggc_free (tinst);
18758 return r;
18761 /* Adjust types before performing type deduction, as described in
18762 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18763 sections are symmetric. PARM is the type of a function parameter
18764 or the return type of the conversion function. ARG is the type of
18765 the argument passed to the call, or the type of the value
18766 initialized with the result of the conversion function.
18767 ARG_EXPR is the original argument expression, which may be null. */
18769 static int
18770 maybe_adjust_types_for_deduction (unification_kind_t strict,
18771 tree* parm,
18772 tree* arg,
18773 tree arg_expr)
18775 int result = 0;
18777 switch (strict)
18779 case DEDUCE_CALL:
18780 break;
18782 case DEDUCE_CONV:
18783 /* Swap PARM and ARG throughout the remainder of this
18784 function; the handling is precisely symmetric since PARM
18785 will initialize ARG rather than vice versa. */
18786 std::swap (parm, arg);
18787 break;
18789 case DEDUCE_EXACT:
18790 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18791 too, but here handle it by stripping the reference from PARM
18792 rather than by adding it to ARG. */
18793 if (TREE_CODE (*parm) == REFERENCE_TYPE
18794 && TYPE_REF_IS_RVALUE (*parm)
18795 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18796 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18797 && TREE_CODE (*arg) == REFERENCE_TYPE
18798 && !TYPE_REF_IS_RVALUE (*arg))
18799 *parm = TREE_TYPE (*parm);
18800 /* Nothing else to do in this case. */
18801 return 0;
18803 default:
18804 gcc_unreachable ();
18807 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18809 /* [temp.deduct.call]
18811 If P is not a reference type:
18813 --If A is an array type, the pointer type produced by the
18814 array-to-pointer standard conversion (_conv.array_) is
18815 used in place of A for type deduction; otherwise,
18817 --If A is a function type, the pointer type produced by
18818 the function-to-pointer standard conversion
18819 (_conv.func_) is used in place of A for type deduction;
18820 otherwise,
18822 --If A is a cv-qualified type, the top level
18823 cv-qualifiers of A's type are ignored for type
18824 deduction. */
18825 if (TREE_CODE (*arg) == ARRAY_TYPE)
18826 *arg = build_pointer_type (TREE_TYPE (*arg));
18827 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18828 *arg = build_pointer_type (*arg);
18829 else
18830 *arg = TYPE_MAIN_VARIANT (*arg);
18833 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18834 reference to a cv-unqualified template parameter that does not represent a
18835 template parameter of a class template (during class template argument
18836 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18837 an lvalue, the type "lvalue reference to A" is used in place of A for type
18838 deduction. */
18839 if (TREE_CODE (*parm) == REFERENCE_TYPE
18840 && TYPE_REF_IS_RVALUE (*parm)
18841 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18842 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18843 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18844 && (arg_expr ? lvalue_p (arg_expr)
18845 /* try_one_overload doesn't provide an arg_expr, but
18846 functions are always lvalues. */
18847 : TREE_CODE (*arg) == FUNCTION_TYPE))
18848 *arg = build_reference_type (*arg);
18850 /* [temp.deduct.call]
18852 If P is a cv-qualified type, the top level cv-qualifiers
18853 of P's type are ignored for type deduction. If P is a
18854 reference type, the type referred to by P is used for
18855 type deduction. */
18856 *parm = TYPE_MAIN_VARIANT (*parm);
18857 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18859 *parm = TREE_TYPE (*parm);
18860 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18863 /* DR 322. For conversion deduction, remove a reference type on parm
18864 too (which has been swapped into ARG). */
18865 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18866 *arg = TREE_TYPE (*arg);
18868 return result;
18871 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18872 template which does contain any deducible template parameters; check if
18873 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18874 unify_one_argument. */
18876 static int
18877 check_non_deducible_conversion (tree parm, tree arg, int strict,
18878 int flags, bool explain_p)
18880 tree type;
18882 if (!TYPE_P (arg))
18883 type = TREE_TYPE (arg);
18884 else
18885 type = arg;
18887 if (same_type_p (parm, type))
18888 return unify_success (explain_p);
18890 if (strict == DEDUCE_CONV)
18892 if (can_convert_arg (type, parm, NULL_TREE, flags,
18893 explain_p ? tf_warning_or_error : tf_none))
18894 return unify_success (explain_p);
18896 else if (strict != DEDUCE_EXACT)
18898 if (can_convert_arg (parm, type,
18899 TYPE_P (arg) ? NULL_TREE : arg,
18900 flags, explain_p ? tf_warning_or_error : tf_none))
18901 return unify_success (explain_p);
18904 if (strict == DEDUCE_EXACT)
18905 return unify_type_mismatch (explain_p, parm, arg);
18906 else
18907 return unify_arg_conversion (explain_p, parm, type, arg);
18910 static bool uses_deducible_template_parms (tree type);
18912 /* Returns true iff the expression EXPR is one from which a template
18913 argument can be deduced. In other words, if it's an undecorated
18914 use of a template non-type parameter. */
18916 static bool
18917 deducible_expression (tree expr)
18919 /* Strip implicit conversions. */
18920 while (CONVERT_EXPR_P (expr))
18921 expr = TREE_OPERAND (expr, 0);
18922 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18925 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18926 deducible way; that is, if it has a max value of <PARM> - 1. */
18928 static bool
18929 deducible_array_bound (tree domain)
18931 if (domain == NULL_TREE)
18932 return false;
18934 tree max = TYPE_MAX_VALUE (domain);
18935 if (TREE_CODE (max) != MINUS_EXPR)
18936 return false;
18938 return deducible_expression (TREE_OPERAND (max, 0));
18941 /* Returns true iff the template arguments ARGS use a template parameter
18942 in a deducible way. */
18944 static bool
18945 deducible_template_args (tree args)
18947 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18949 bool deducible;
18950 tree elt = TREE_VEC_ELT (args, i);
18951 if (ARGUMENT_PACK_P (elt))
18952 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18953 else
18955 if (PACK_EXPANSION_P (elt))
18956 elt = PACK_EXPANSION_PATTERN (elt);
18957 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18958 deducible = true;
18959 else if (TYPE_P (elt))
18960 deducible = uses_deducible_template_parms (elt);
18961 else
18962 deducible = deducible_expression (elt);
18964 if (deducible)
18965 return true;
18967 return false;
18970 /* Returns true iff TYPE contains any deducible references to template
18971 parameters, as per 14.8.2.5. */
18973 static bool
18974 uses_deducible_template_parms (tree type)
18976 if (PACK_EXPANSION_P (type))
18977 type = PACK_EXPANSION_PATTERN (type);
18979 /* T
18980 cv-list T
18981 TT<T>
18982 TT<i>
18983 TT<> */
18984 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18985 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18986 return true;
18988 /* T*
18990 T&& */
18991 if (POINTER_TYPE_P (type))
18992 return uses_deducible_template_parms (TREE_TYPE (type));
18994 /* T[integer-constant ]
18995 type [i] */
18996 if (TREE_CODE (type) == ARRAY_TYPE)
18997 return (uses_deducible_template_parms (TREE_TYPE (type))
18998 || deducible_array_bound (TYPE_DOMAIN (type)));
19000 /* T type ::*
19001 type T::*
19002 T T::*
19003 T (type ::*)()
19004 type (T::*)()
19005 type (type ::*)(T)
19006 type (T::*)(T)
19007 T (type ::*)(T)
19008 T (T::*)()
19009 T (T::*)(T) */
19010 if (TYPE_PTRMEM_P (type))
19011 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19012 || (uses_deducible_template_parms
19013 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19015 /* template-name <T> (where template-name refers to a class template)
19016 template-name <i> (where template-name refers to a class template) */
19017 if (CLASS_TYPE_P (type)
19018 && CLASSTYPE_TEMPLATE_INFO (type)
19019 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19020 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19021 (CLASSTYPE_TI_ARGS (type)));
19023 /* type (T)
19025 T(T) */
19026 if (TREE_CODE (type) == FUNCTION_TYPE
19027 || TREE_CODE (type) == METHOD_TYPE)
19029 if (uses_deducible_template_parms (TREE_TYPE (type)))
19030 return true;
19031 tree parm = TYPE_ARG_TYPES (type);
19032 if (TREE_CODE (type) == METHOD_TYPE)
19033 parm = TREE_CHAIN (parm);
19034 for (; parm; parm = TREE_CHAIN (parm))
19035 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19036 return true;
19039 return false;
19042 /* Subroutine of type_unification_real and unify_pack_expansion to
19043 handle unification of a single P/A pair. Parameters are as
19044 for those functions. */
19046 static int
19047 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19048 int subr, unification_kind_t strict,
19049 bool explain_p)
19051 tree arg_expr = NULL_TREE;
19052 int arg_strict;
19054 if (arg == error_mark_node || parm == error_mark_node)
19055 return unify_invalid (explain_p);
19056 if (arg == unknown_type_node)
19057 /* We can't deduce anything from this, but we might get all the
19058 template args from other function args. */
19059 return unify_success (explain_p);
19061 /* Implicit conversions (Clause 4) will be performed on a function
19062 argument to convert it to the type of the corresponding function
19063 parameter if the parameter type contains no template-parameters that
19064 participate in template argument deduction. */
19065 if (strict != DEDUCE_EXACT
19066 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19067 /* For function parameters with no deducible template parameters,
19068 just return. We'll check non-dependent conversions later. */
19069 return unify_success (explain_p);
19071 switch (strict)
19073 case DEDUCE_CALL:
19074 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19075 | UNIFY_ALLOW_MORE_CV_QUAL
19076 | UNIFY_ALLOW_DERIVED);
19077 break;
19079 case DEDUCE_CONV:
19080 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19081 break;
19083 case DEDUCE_EXACT:
19084 arg_strict = UNIFY_ALLOW_NONE;
19085 break;
19087 default:
19088 gcc_unreachable ();
19091 /* We only do these transformations if this is the top-level
19092 parameter_type_list in a call or declaration matching; in other
19093 situations (nested function declarators, template argument lists) we
19094 won't be comparing a type to an expression, and we don't do any type
19095 adjustments. */
19096 if (!subr)
19098 if (!TYPE_P (arg))
19100 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19101 if (type_unknown_p (arg))
19103 /* [temp.deduct.type] A template-argument can be
19104 deduced from a pointer to function or pointer
19105 to member function argument if the set of
19106 overloaded functions does not contain function
19107 templates and at most one of a set of
19108 overloaded functions provides a unique
19109 match. */
19111 if (resolve_overloaded_unification
19112 (tparms, targs, parm, arg, strict,
19113 arg_strict, explain_p))
19114 return unify_success (explain_p);
19115 return unify_overload_resolution_failure (explain_p, arg);
19118 arg_expr = arg;
19119 arg = unlowered_expr_type (arg);
19120 if (arg == error_mark_node)
19121 return unify_invalid (explain_p);
19124 arg_strict |=
19125 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19127 else
19128 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19129 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19130 return unify_template_argument_mismatch (explain_p, parm, arg);
19132 /* For deduction from an init-list we need the actual list. */
19133 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19134 arg = arg_expr;
19135 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19138 /* for_each_template_parm callback that always returns 0. */
19140 static int
19141 zero_r (tree, void *)
19143 return 0;
19146 /* for_each_template_parm any_fn callback to handle deduction of a template
19147 type argument from the type of an array bound. */
19149 static int
19150 array_deduction_r (tree t, void *data)
19152 tree_pair_p d = (tree_pair_p)data;
19153 tree &tparms = d->purpose;
19154 tree &targs = d->value;
19156 if (TREE_CODE (t) == ARRAY_TYPE)
19157 if (tree dom = TYPE_DOMAIN (t))
19158 if (tree max = TYPE_MAX_VALUE (dom))
19160 if (TREE_CODE (max) == MINUS_EXPR)
19161 max = TREE_OPERAND (max, 0);
19162 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19163 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19164 UNIFY_ALLOW_NONE, /*explain*/false);
19167 /* Keep walking. */
19168 return 0;
19171 /* Try to deduce any not-yet-deduced template type arguments from the type of
19172 an array bound. This is handled separately from unify because 14.8.2.5 says
19173 "The type of a type parameter is only deduced from an array bound if it is
19174 not otherwise deduced." */
19176 static void
19177 try_array_deduction (tree tparms, tree targs, tree parm)
19179 tree_pair_s data = { tparms, targs };
19180 hash_set<tree> visited;
19181 for_each_template_parm (parm, zero_r, &data, &visited,
19182 /*nondeduced*/false, array_deduction_r);
19185 /* Most parms like fn_type_unification.
19187 If SUBR is 1, we're being called recursively (to unify the
19188 arguments of a function or method parameter of a function
19189 template).
19191 CHECKS is a pointer to a vector of access checks encountered while
19192 substituting default template arguments. */
19194 static int
19195 type_unification_real (tree tparms,
19196 tree full_targs,
19197 tree xparms,
19198 const tree *xargs,
19199 unsigned int xnargs,
19200 int subr,
19201 unification_kind_t strict,
19202 int flags,
19203 vec<deferred_access_check, va_gc> **checks,
19204 bool explain_p)
19206 tree parm, arg;
19207 int i;
19208 int ntparms = TREE_VEC_LENGTH (tparms);
19209 int saw_undeduced = 0;
19210 tree parms;
19211 const tree *args;
19212 unsigned int nargs;
19213 unsigned int ia;
19215 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19216 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19217 gcc_assert (ntparms > 0);
19219 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19221 /* Reset the number of non-defaulted template arguments contained
19222 in TARGS. */
19223 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19225 again:
19226 parms = xparms;
19227 args = xargs;
19228 nargs = xnargs;
19230 ia = 0;
19231 while (parms && parms != void_list_node
19232 && ia < nargs)
19234 parm = TREE_VALUE (parms);
19236 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19237 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19238 /* For a function parameter pack that occurs at the end of the
19239 parameter-declaration-list, the type A of each remaining
19240 argument of the call is compared with the type P of the
19241 declarator-id of the function parameter pack. */
19242 break;
19244 parms = TREE_CHAIN (parms);
19246 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19247 /* For a function parameter pack that does not occur at the
19248 end of the parameter-declaration-list, the type of the
19249 parameter pack is a non-deduced context. */
19250 continue;
19252 arg = args[ia];
19253 ++ia;
19255 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19256 explain_p))
19257 return 1;
19260 if (parms
19261 && parms != void_list_node
19262 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19264 /* Unify the remaining arguments with the pack expansion type. */
19265 tree argvec;
19266 tree parmvec = make_tree_vec (1);
19268 /* Allocate a TREE_VEC and copy in all of the arguments */
19269 argvec = make_tree_vec (nargs - ia);
19270 for (i = 0; ia < nargs; ++ia, ++i)
19271 TREE_VEC_ELT (argvec, i) = args[ia];
19273 /* Copy the parameter into parmvec. */
19274 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19275 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19276 /*subr=*/subr, explain_p))
19277 return 1;
19279 /* Advance to the end of the list of parameters. */
19280 parms = TREE_CHAIN (parms);
19283 /* Fail if we've reached the end of the parm list, and more args
19284 are present, and the parm list isn't variadic. */
19285 if (ia < nargs && parms == void_list_node)
19286 return unify_too_many_arguments (explain_p, nargs, ia);
19287 /* Fail if parms are left and they don't have default values and
19288 they aren't all deduced as empty packs (c++/57397). This is
19289 consistent with sufficient_parms_p. */
19290 if (parms && parms != void_list_node
19291 && TREE_PURPOSE (parms) == NULL_TREE)
19293 unsigned int count = nargs;
19294 tree p = parms;
19295 bool type_pack_p;
19298 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19299 if (!type_pack_p)
19300 count++;
19301 p = TREE_CHAIN (p);
19303 while (p && p != void_list_node);
19304 if (count != nargs)
19305 return unify_too_few_arguments (explain_p, ia, count,
19306 type_pack_p);
19309 if (!subr)
19311 tsubst_flags_t complain = (explain_p
19312 ? tf_warning_or_error
19313 : tf_none);
19314 bool tried_array_deduction = (cxx_dialect < cxx1z);
19316 for (i = 0; i < ntparms; i++)
19318 tree targ = TREE_VEC_ELT (targs, i);
19319 tree tparm = TREE_VEC_ELT (tparms, i);
19321 /* Clear the "incomplete" flags on all argument packs now so that
19322 substituting them into later default arguments works. */
19323 if (targ && ARGUMENT_PACK_P (targ))
19325 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19326 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19329 if (targ || tparm == error_mark_node)
19330 continue;
19331 tparm = TREE_VALUE (tparm);
19333 if (TREE_CODE (tparm) == TYPE_DECL
19334 && !tried_array_deduction)
19336 try_array_deduction (tparms, targs, xparms);
19337 tried_array_deduction = true;
19338 if (TREE_VEC_ELT (targs, i))
19339 continue;
19342 /* If this is an undeduced nontype parameter that depends on
19343 a type parameter, try another pass; its type may have been
19344 deduced from a later argument than the one from which
19345 this parameter can be deduced. */
19346 if (TREE_CODE (tparm) == PARM_DECL
19347 && uses_template_parms (TREE_TYPE (tparm))
19348 && saw_undeduced < 2)
19350 saw_undeduced = 1;
19351 continue;
19354 /* Core issue #226 (C++0x) [temp.deduct]:
19356 If a template argument has not been deduced, its
19357 default template argument, if any, is used.
19359 When we are in C++98 mode, TREE_PURPOSE will either
19360 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19361 to explicitly check cxx_dialect here. */
19362 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19363 /* OK, there is a default argument. Wait until after the
19364 conversion check to do substitution. */
19365 continue;
19367 /* If the type parameter is a parameter pack, then it will
19368 be deduced to an empty parameter pack. */
19369 if (template_parameter_pack_p (tparm))
19371 tree arg;
19373 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19375 arg = make_node (NONTYPE_ARGUMENT_PACK);
19376 TREE_CONSTANT (arg) = 1;
19378 else
19379 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19381 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19383 TREE_VEC_ELT (targs, i) = arg;
19384 continue;
19387 return unify_parameter_deduction_failure (explain_p, tparm);
19390 /* DR 1391: All parameters have args, now check non-dependent parms for
19391 convertibility. */
19392 if (saw_undeduced < 2)
19393 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19394 parms && parms != void_list_node && ia < nargs; )
19396 parm = TREE_VALUE (parms);
19398 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19399 && (!TREE_CHAIN (parms)
19400 || TREE_CHAIN (parms) == void_list_node))
19401 /* For a function parameter pack that occurs at the end of the
19402 parameter-declaration-list, the type A of each remaining
19403 argument of the call is compared with the type P of the
19404 declarator-id of the function parameter pack. */
19405 break;
19407 parms = TREE_CHAIN (parms);
19409 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19410 /* For a function parameter pack that does not occur at the
19411 end of the parameter-declaration-list, the type of the
19412 parameter pack is a non-deduced context. */
19413 continue;
19415 arg = args[ia];
19416 ++ia;
19418 if (uses_template_parms (parm))
19419 continue;
19420 if (check_non_deducible_conversion (parm, arg, strict, flags,
19421 explain_p))
19422 return 1;
19425 /* Now substitute into the default template arguments. */
19426 for (i = 0; i < ntparms; i++)
19428 tree targ = TREE_VEC_ELT (targs, i);
19429 tree tparm = TREE_VEC_ELT (tparms, i);
19431 if (targ || tparm == error_mark_node)
19432 continue;
19433 tree parm = TREE_VALUE (tparm);
19435 if (TREE_CODE (parm) == PARM_DECL
19436 && uses_template_parms (TREE_TYPE (parm))
19437 && saw_undeduced < 2)
19438 continue;
19440 tree arg = TREE_PURPOSE (tparm);
19441 reopen_deferring_access_checks (*checks);
19442 location_t save_loc = input_location;
19443 if (DECL_P (parm))
19444 input_location = DECL_SOURCE_LOCATION (parm);
19445 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19446 if (!uses_template_parms (arg))
19447 arg = convert_template_argument (parm, arg, full_targs, complain,
19448 i, NULL_TREE);
19449 else if (saw_undeduced < 2)
19450 arg = NULL_TREE;
19451 else
19452 arg = error_mark_node;
19453 input_location = save_loc;
19454 *checks = get_deferred_access_checks ();
19455 pop_deferring_access_checks ();
19456 if (arg == error_mark_node)
19457 return 1;
19458 else if (arg)
19460 TREE_VEC_ELT (targs, i) = arg;
19461 /* The position of the first default template argument,
19462 is also the number of non-defaulted arguments in TARGS.
19463 Record that. */
19464 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19469 if (saw_undeduced++ == 1)
19470 goto again;
19473 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19474 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19476 return unify_success (explain_p);
19479 /* Subroutine of type_unification_real. Args are like the variables
19480 at the call site. ARG is an overloaded function (or template-id);
19481 we try deducing template args from each of the overloads, and if
19482 only one succeeds, we go with that. Modifies TARGS and returns
19483 true on success. */
19485 static bool
19486 resolve_overloaded_unification (tree tparms,
19487 tree targs,
19488 tree parm,
19489 tree arg,
19490 unification_kind_t strict,
19491 int sub_strict,
19492 bool explain_p)
19494 tree tempargs = copy_node (targs);
19495 int good = 0;
19496 tree goodfn = NULL_TREE;
19497 bool addr_p;
19499 if (TREE_CODE (arg) == ADDR_EXPR)
19501 arg = TREE_OPERAND (arg, 0);
19502 addr_p = true;
19504 else
19505 addr_p = false;
19507 if (TREE_CODE (arg) == COMPONENT_REF)
19508 /* Handle `&x' where `x' is some static or non-static member
19509 function name. */
19510 arg = TREE_OPERAND (arg, 1);
19512 if (TREE_CODE (arg) == OFFSET_REF)
19513 arg = TREE_OPERAND (arg, 1);
19515 /* Strip baselink information. */
19516 if (BASELINK_P (arg))
19517 arg = BASELINK_FUNCTIONS (arg);
19519 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19521 /* If we got some explicit template args, we need to plug them into
19522 the affected templates before we try to unify, in case the
19523 explicit args will completely resolve the templates in question. */
19525 int ok = 0;
19526 tree expl_subargs = TREE_OPERAND (arg, 1);
19527 arg = TREE_OPERAND (arg, 0);
19529 for (lkp_iterator iter (arg); iter; ++iter)
19531 tree fn = *iter;
19532 tree subargs, elem;
19534 if (TREE_CODE (fn) != TEMPLATE_DECL)
19535 continue;
19537 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19538 expl_subargs, NULL_TREE, tf_none,
19539 /*require_all_args=*/true,
19540 /*use_default_args=*/true);
19541 if (subargs != error_mark_node
19542 && !any_dependent_template_arguments_p (subargs))
19544 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19545 if (try_one_overload (tparms, targs, tempargs, parm,
19546 elem, strict, sub_strict, addr_p, explain_p)
19547 && (!goodfn || !same_type_p (goodfn, elem)))
19549 goodfn = elem;
19550 ++good;
19553 else if (subargs)
19554 ++ok;
19556 /* If no templates (or more than one) are fully resolved by the
19557 explicit arguments, this template-id is a non-deduced context; it
19558 could still be OK if we deduce all template arguments for the
19559 enclosing call through other arguments. */
19560 if (good != 1)
19561 good = ok;
19563 else if (TREE_CODE (arg) != OVERLOAD
19564 && TREE_CODE (arg) != FUNCTION_DECL)
19565 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19566 -- but the deduction does not succeed because the expression is
19567 not just the function on its own. */
19568 return false;
19569 else
19570 for (lkp_iterator iter (arg); iter; ++iter)
19572 tree fn = *iter;
19573 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19574 strict, sub_strict, addr_p, explain_p)
19575 && (!goodfn || !decls_match (goodfn, fn)))
19577 goodfn = fn;
19578 ++good;
19582 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19583 to function or pointer to member function argument if the set of
19584 overloaded functions does not contain function templates and at most
19585 one of a set of overloaded functions provides a unique match.
19587 So if we found multiple possibilities, we return success but don't
19588 deduce anything. */
19590 if (good == 1)
19592 int i = TREE_VEC_LENGTH (targs);
19593 for (; i--; )
19594 if (TREE_VEC_ELT (tempargs, i))
19596 tree old = TREE_VEC_ELT (targs, i);
19597 tree new_ = TREE_VEC_ELT (tempargs, i);
19598 if (new_ && old && ARGUMENT_PACK_P (old)
19599 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19600 /* Don't forget explicit template arguments in a pack. */
19601 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19602 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19603 TREE_VEC_ELT (targs, i) = new_;
19606 if (good)
19607 return true;
19609 return false;
19612 /* Core DR 115: In contexts where deduction is done and fails, or in
19613 contexts where deduction is not done, if a template argument list is
19614 specified and it, along with any default template arguments, identifies
19615 a single function template specialization, then the template-id is an
19616 lvalue for the function template specialization. */
19618 tree
19619 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19621 tree expr, offset, baselink;
19622 bool addr;
19624 if (!type_unknown_p (orig_expr))
19625 return orig_expr;
19627 expr = orig_expr;
19628 addr = false;
19629 offset = NULL_TREE;
19630 baselink = NULL_TREE;
19632 if (TREE_CODE (expr) == ADDR_EXPR)
19634 expr = TREE_OPERAND (expr, 0);
19635 addr = true;
19637 if (TREE_CODE (expr) == OFFSET_REF)
19639 offset = expr;
19640 expr = TREE_OPERAND (expr, 1);
19642 if (BASELINK_P (expr))
19644 baselink = expr;
19645 expr = BASELINK_FUNCTIONS (expr);
19648 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19650 int good = 0;
19651 tree goodfn = NULL_TREE;
19653 /* If we got some explicit template args, we need to plug them into
19654 the affected templates before we try to unify, in case the
19655 explicit args will completely resolve the templates in question. */
19657 tree expl_subargs = TREE_OPERAND (expr, 1);
19658 tree arg = TREE_OPERAND (expr, 0);
19659 tree badfn = NULL_TREE;
19660 tree badargs = NULL_TREE;
19662 for (lkp_iterator iter (arg); iter; ++iter)
19664 tree fn = *iter;
19665 tree subargs, elem;
19667 if (TREE_CODE (fn) != TEMPLATE_DECL)
19668 continue;
19670 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19671 expl_subargs, NULL_TREE, tf_none,
19672 /*require_all_args=*/true,
19673 /*use_default_args=*/true);
19674 if (subargs != error_mark_node
19675 && !any_dependent_template_arguments_p (subargs))
19677 elem = instantiate_template (fn, subargs, tf_none);
19678 if (elem == error_mark_node)
19680 badfn = fn;
19681 badargs = subargs;
19683 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19685 goodfn = elem;
19686 ++good;
19690 if (good == 1)
19692 mark_used (goodfn);
19693 expr = goodfn;
19694 if (baselink)
19695 expr = build_baselink (BASELINK_BINFO (baselink),
19696 BASELINK_ACCESS_BINFO (baselink),
19697 expr, BASELINK_OPTYPE (baselink));
19698 if (offset)
19700 tree base
19701 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19702 expr = build_offset_ref (base, expr, addr, complain);
19704 if (addr)
19705 expr = cp_build_addr_expr (expr, complain);
19706 return expr;
19708 else if (good == 0 && badargs && (complain & tf_error))
19709 /* There were no good options and at least one bad one, so let the
19710 user know what the problem is. */
19711 instantiate_template (badfn, badargs, complain);
19713 return orig_expr;
19716 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19717 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19718 different overloads deduce different arguments for a given parm.
19719 ADDR_P is true if the expression for which deduction is being
19720 performed was of the form "& fn" rather than simply "fn".
19722 Returns 1 on success. */
19724 static int
19725 try_one_overload (tree tparms,
19726 tree orig_targs,
19727 tree targs,
19728 tree parm,
19729 tree arg,
19730 unification_kind_t strict,
19731 int sub_strict,
19732 bool addr_p,
19733 bool explain_p)
19735 int nargs;
19736 tree tempargs;
19737 int i;
19739 if (arg == error_mark_node)
19740 return 0;
19742 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19743 to function or pointer to member function argument if the set of
19744 overloaded functions does not contain function templates and at most
19745 one of a set of overloaded functions provides a unique match.
19747 So if this is a template, just return success. */
19749 if (uses_template_parms (arg))
19750 return 1;
19752 if (TREE_CODE (arg) == METHOD_TYPE)
19753 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19754 else if (addr_p)
19755 arg = build_pointer_type (arg);
19757 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19759 /* We don't copy orig_targs for this because if we have already deduced
19760 some template args from previous args, unify would complain when we
19761 try to deduce a template parameter for the same argument, even though
19762 there isn't really a conflict. */
19763 nargs = TREE_VEC_LENGTH (targs);
19764 tempargs = make_tree_vec (nargs);
19766 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19767 return 0;
19769 /* First make sure we didn't deduce anything that conflicts with
19770 explicitly specified args. */
19771 for (i = nargs; i--; )
19773 tree elt = TREE_VEC_ELT (tempargs, i);
19774 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19776 if (!elt)
19777 /*NOP*/;
19778 else if (uses_template_parms (elt))
19779 /* Since we're unifying against ourselves, we will fill in
19780 template args used in the function parm list with our own
19781 template parms. Discard them. */
19782 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19783 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19785 /* Check that the argument at each index of the deduced argument pack
19786 is equivalent to the corresponding explicitly specified argument.
19787 We may have deduced more arguments than were explicitly specified,
19788 and that's OK. */
19790 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19791 that's wrong if we deduce the same argument pack from multiple
19792 function arguments: it's only incomplete the first time. */
19794 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19795 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19797 if (TREE_VEC_LENGTH (deduced_pack)
19798 < TREE_VEC_LENGTH (explicit_pack))
19799 return 0;
19801 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19802 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19803 TREE_VEC_ELT (deduced_pack, j)))
19804 return 0;
19806 else if (oldelt && !template_args_equal (oldelt, elt))
19807 return 0;
19810 for (i = nargs; i--; )
19812 tree elt = TREE_VEC_ELT (tempargs, i);
19814 if (elt)
19815 TREE_VEC_ELT (targs, i) = elt;
19818 return 1;
19821 /* PARM is a template class (perhaps with unbound template
19822 parameters). ARG is a fully instantiated type. If ARG can be
19823 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19824 TARGS are as for unify. */
19826 static tree
19827 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19828 bool explain_p)
19830 tree copy_of_targs;
19832 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19833 return NULL_TREE;
19834 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19835 /* Matches anything. */;
19836 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19837 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19838 return NULL_TREE;
19840 /* We need to make a new template argument vector for the call to
19841 unify. If we used TARGS, we'd clutter it up with the result of
19842 the attempted unification, even if this class didn't work out.
19843 We also don't want to commit ourselves to all the unifications
19844 we've already done, since unification is supposed to be done on
19845 an argument-by-argument basis. In other words, consider the
19846 following pathological case:
19848 template <int I, int J, int K>
19849 struct S {};
19851 template <int I, int J>
19852 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19854 template <int I, int J, int K>
19855 void f(S<I, J, K>, S<I, I, I>);
19857 void g() {
19858 S<0, 0, 0> s0;
19859 S<0, 1, 2> s2;
19861 f(s0, s2);
19864 Now, by the time we consider the unification involving `s2', we
19865 already know that we must have `f<0, 0, 0>'. But, even though
19866 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19867 because there are two ways to unify base classes of S<0, 1, 2>
19868 with S<I, I, I>. If we kept the already deduced knowledge, we
19869 would reject the possibility I=1. */
19870 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19872 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19874 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19875 return NULL_TREE;
19876 return arg;
19879 /* If unification failed, we're done. */
19880 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19881 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19882 return NULL_TREE;
19884 return arg;
19887 /* Given a template type PARM and a class type ARG, find the unique
19888 base type in ARG that is an instance of PARM. We do not examine
19889 ARG itself; only its base-classes. If there is not exactly one
19890 appropriate base class, return NULL_TREE. PARM may be the type of
19891 a partial specialization, as well as a plain template type. Used
19892 by unify. */
19894 static enum template_base_result
19895 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19896 bool explain_p, tree *result)
19898 tree rval = NULL_TREE;
19899 tree binfo;
19901 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19903 binfo = TYPE_BINFO (complete_type (arg));
19904 if (!binfo)
19906 /* The type could not be completed. */
19907 *result = NULL_TREE;
19908 return tbr_incomplete_type;
19911 /* Walk in inheritance graph order. The search order is not
19912 important, and this avoids multiple walks of virtual bases. */
19913 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19915 tree r = try_class_unification (tparms, targs, parm,
19916 BINFO_TYPE (binfo), explain_p);
19918 if (r)
19920 /* If there is more than one satisfactory baseclass, then:
19922 [temp.deduct.call]
19924 If they yield more than one possible deduced A, the type
19925 deduction fails.
19927 applies. */
19928 if (rval && !same_type_p (r, rval))
19930 *result = NULL_TREE;
19931 return tbr_ambiguous_baseclass;
19934 rval = r;
19938 *result = rval;
19939 return tbr_success;
19942 /* Returns the level of DECL, which declares a template parameter. */
19944 static int
19945 template_decl_level (tree decl)
19947 switch (TREE_CODE (decl))
19949 case TYPE_DECL:
19950 case TEMPLATE_DECL:
19951 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19953 case PARM_DECL:
19954 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19956 default:
19957 gcc_unreachable ();
19959 return 0;
19962 /* Decide whether ARG can be unified with PARM, considering only the
19963 cv-qualifiers of each type, given STRICT as documented for unify.
19964 Returns nonzero iff the unification is OK on that basis. */
19966 static int
19967 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19969 int arg_quals = cp_type_quals (arg);
19970 int parm_quals = cp_type_quals (parm);
19972 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19973 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19975 /* Although a CVR qualifier is ignored when being applied to a
19976 substituted template parameter ([8.3.2]/1 for example), that
19977 does not allow us to unify "const T" with "int&" because both
19978 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19979 It is ok when we're allowing additional CV qualifiers
19980 at the outer level [14.8.2.1]/3,1st bullet. */
19981 if ((TREE_CODE (arg) == REFERENCE_TYPE
19982 || TREE_CODE (arg) == FUNCTION_TYPE
19983 || TREE_CODE (arg) == METHOD_TYPE)
19984 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19985 return 0;
19987 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19988 && (parm_quals & TYPE_QUAL_RESTRICT))
19989 return 0;
19992 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19993 && (arg_quals & parm_quals) != parm_quals)
19994 return 0;
19996 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19997 && (parm_quals & arg_quals) != arg_quals)
19998 return 0;
20000 return 1;
20003 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20004 void
20005 template_parm_level_and_index (tree parm, int* level, int* index)
20007 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20008 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20009 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20011 *index = TEMPLATE_TYPE_IDX (parm);
20012 *level = TEMPLATE_TYPE_LEVEL (parm);
20014 else
20016 *index = TEMPLATE_PARM_IDX (parm);
20017 *level = TEMPLATE_PARM_LEVEL (parm);
20021 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20022 do { \
20023 if (unify (TP, TA, P, A, S, EP)) \
20024 return 1; \
20025 } while (0)
20027 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20028 expansion at the end of PACKED_PARMS. Returns 0 if the type
20029 deduction succeeds, 1 otherwise. STRICT is the same as in
20030 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20031 function call argument list. We'll need to adjust the arguments to make them
20032 types. SUBR tells us if this is from a recursive call to
20033 type_unification_real, or for comparing two template argument
20034 lists. */
20036 static int
20037 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20038 tree packed_args, unification_kind_t strict,
20039 bool subr, bool explain_p)
20041 tree parm
20042 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20043 tree pattern = PACK_EXPANSION_PATTERN (parm);
20044 tree pack, packs = NULL_TREE;
20045 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20047 /* Add in any args remembered from an earlier partial instantiation. */
20048 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20050 packed_args = expand_template_argument_pack (packed_args);
20052 int len = TREE_VEC_LENGTH (packed_args);
20054 /* Determine the parameter packs we will be deducing from the
20055 pattern, and record their current deductions. */
20056 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20057 pack; pack = TREE_CHAIN (pack))
20059 tree parm_pack = TREE_VALUE (pack);
20060 int idx, level;
20062 /* Determine the index and level of this parameter pack. */
20063 template_parm_level_and_index (parm_pack, &level, &idx);
20065 /* Keep track of the parameter packs and their corresponding
20066 argument packs. */
20067 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20068 TREE_TYPE (packs) = make_tree_vec (len - start);
20071 /* Loop through all of the arguments that have not yet been
20072 unified and unify each with the pattern. */
20073 for (i = start; i < len; i++)
20075 tree parm;
20076 bool any_explicit = false;
20077 tree arg = TREE_VEC_ELT (packed_args, i);
20079 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20080 or the element of its argument pack at the current index if
20081 this argument was explicitly specified. */
20082 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20084 int idx, level;
20085 tree arg, pargs;
20086 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20088 arg = NULL_TREE;
20089 if (TREE_VALUE (pack)
20090 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20091 && (i - start < TREE_VEC_LENGTH (pargs)))
20093 any_explicit = true;
20094 arg = TREE_VEC_ELT (pargs, i - start);
20096 TMPL_ARG (targs, level, idx) = arg;
20099 /* If we had explicit template arguments, substitute them into the
20100 pattern before deduction. */
20101 if (any_explicit)
20103 /* Some arguments might still be unspecified or dependent. */
20104 bool dependent;
20105 ++processing_template_decl;
20106 dependent = any_dependent_template_arguments_p (targs);
20107 if (!dependent)
20108 --processing_template_decl;
20109 parm = tsubst (pattern, targs,
20110 explain_p ? tf_warning_or_error : tf_none,
20111 NULL_TREE);
20112 if (dependent)
20113 --processing_template_decl;
20114 if (parm == error_mark_node)
20115 return 1;
20117 else
20118 parm = pattern;
20120 /* Unify the pattern with the current argument. */
20121 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20122 explain_p))
20123 return 1;
20125 /* For each parameter pack, collect the deduced value. */
20126 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20128 int idx, level;
20129 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20131 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20132 TMPL_ARG (targs, level, idx);
20136 /* Verify that the results of unification with the parameter packs
20137 produce results consistent with what we've seen before, and make
20138 the deduced argument packs available. */
20139 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20141 tree old_pack = TREE_VALUE (pack);
20142 tree new_args = TREE_TYPE (pack);
20143 int i, len = TREE_VEC_LENGTH (new_args);
20144 int idx, level;
20145 bool nondeduced_p = false;
20147 /* By default keep the original deduced argument pack.
20148 If necessary, more specific code is going to update the
20149 resulting deduced argument later down in this function. */
20150 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20151 TMPL_ARG (targs, level, idx) = old_pack;
20153 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20154 actually deduce anything. */
20155 for (i = 0; i < len && !nondeduced_p; ++i)
20156 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20157 nondeduced_p = true;
20158 if (nondeduced_p)
20159 continue;
20161 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20163 /* If we had fewer function args than explicit template args,
20164 just use the explicits. */
20165 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20166 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20167 if (len < explicit_len)
20168 new_args = explicit_args;
20171 if (!old_pack)
20173 tree result;
20174 /* Build the deduced *_ARGUMENT_PACK. */
20175 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20177 result = make_node (NONTYPE_ARGUMENT_PACK);
20178 TREE_CONSTANT (result) = 1;
20180 else
20181 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20183 SET_ARGUMENT_PACK_ARGS (result, new_args);
20185 /* Note the deduced argument packs for this parameter
20186 pack. */
20187 TMPL_ARG (targs, level, idx) = result;
20189 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20190 && (ARGUMENT_PACK_ARGS (old_pack)
20191 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20193 /* We only had the explicitly-provided arguments before, but
20194 now we have a complete set of arguments. */
20195 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20197 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20198 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20199 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20201 else
20203 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20204 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20206 if (!comp_template_args (old_args, new_args,
20207 &bad_old_arg, &bad_new_arg))
20208 /* Inconsistent unification of this parameter pack. */
20209 return unify_parameter_pack_inconsistent (explain_p,
20210 bad_old_arg,
20211 bad_new_arg);
20215 return unify_success (explain_p);
20218 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20219 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20220 parameters and return value are as for unify. */
20222 static int
20223 unify_array_domain (tree tparms, tree targs,
20224 tree parm_dom, tree arg_dom,
20225 bool explain_p)
20227 tree parm_max;
20228 tree arg_max;
20229 bool parm_cst;
20230 bool arg_cst;
20232 /* Our representation of array types uses "N - 1" as the
20233 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20234 not an integer constant. We cannot unify arbitrarily
20235 complex expressions, so we eliminate the MINUS_EXPRs
20236 here. */
20237 parm_max = TYPE_MAX_VALUE (parm_dom);
20238 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20239 if (!parm_cst)
20241 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20242 parm_max = TREE_OPERAND (parm_max, 0);
20244 arg_max = TYPE_MAX_VALUE (arg_dom);
20245 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20246 if (!arg_cst)
20248 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20249 trying to unify the type of a variable with the type
20250 of a template parameter. For example:
20252 template <unsigned int N>
20253 void f (char (&) [N]);
20254 int g();
20255 void h(int i) {
20256 char a[g(i)];
20257 f(a);
20260 Here, the type of the ARG will be "int [g(i)]", and
20261 may be a SAVE_EXPR, etc. */
20262 if (TREE_CODE (arg_max) != MINUS_EXPR)
20263 return unify_vla_arg (explain_p, arg_dom);
20264 arg_max = TREE_OPERAND (arg_max, 0);
20267 /* If only one of the bounds used a MINUS_EXPR, compensate
20268 by adding one to the other bound. */
20269 if (parm_cst && !arg_cst)
20270 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20271 integer_type_node,
20272 parm_max,
20273 integer_one_node);
20274 else if (arg_cst && !parm_cst)
20275 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20276 integer_type_node,
20277 arg_max,
20278 integer_one_node);
20280 return unify (tparms, targs, parm_max, arg_max,
20281 UNIFY_ALLOW_INTEGER, explain_p);
20284 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20286 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20288 static pa_kind_t
20289 pa_kind (tree t)
20291 if (PACK_EXPANSION_P (t))
20292 t = PACK_EXPANSION_PATTERN (t);
20293 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20294 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20295 || DECL_TYPE_TEMPLATE_P (t))
20296 return pa_tmpl;
20297 else if (TYPE_P (t))
20298 return pa_type;
20299 else
20300 return pa_expr;
20303 /* Deduce the value of template parameters. TPARMS is the (innermost)
20304 set of template parameters to a template. TARGS is the bindings
20305 for those template parameters, as determined thus far; TARGS may
20306 include template arguments for outer levels of template parameters
20307 as well. PARM is a parameter to a template function, or a
20308 subcomponent of that parameter; ARG is the corresponding argument.
20309 This function attempts to match PARM with ARG in a manner
20310 consistent with the existing assignments in TARGS. If more values
20311 are deduced, then TARGS is updated.
20313 Returns 0 if the type deduction succeeds, 1 otherwise. The
20314 parameter STRICT is a bitwise or of the following flags:
20316 UNIFY_ALLOW_NONE:
20317 Require an exact match between PARM and ARG.
20318 UNIFY_ALLOW_MORE_CV_QUAL:
20319 Allow the deduced ARG to be more cv-qualified (by qualification
20320 conversion) than ARG.
20321 UNIFY_ALLOW_LESS_CV_QUAL:
20322 Allow the deduced ARG to be less cv-qualified than ARG.
20323 UNIFY_ALLOW_DERIVED:
20324 Allow the deduced ARG to be a template base class of ARG,
20325 or a pointer to a template base class of the type pointed to by
20326 ARG.
20327 UNIFY_ALLOW_INTEGER:
20328 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20329 case for more information.
20330 UNIFY_ALLOW_OUTER_LEVEL:
20331 This is the outermost level of a deduction. Used to determine validity
20332 of qualification conversions. A valid qualification conversion must
20333 have const qualified pointers leading up to the inner type which
20334 requires additional CV quals, except at the outer level, where const
20335 is not required [conv.qual]. It would be normal to set this flag in
20336 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20337 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20338 This is the outermost level of a deduction, and PARM can be more CV
20339 qualified at this point.
20340 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20341 This is the outermost level of a deduction, and PARM can be less CV
20342 qualified at this point. */
20344 static int
20345 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20346 bool explain_p)
20348 int idx;
20349 tree targ;
20350 tree tparm;
20351 int strict_in = strict;
20352 tsubst_flags_t complain = (explain_p
20353 ? tf_warning_or_error
20354 : tf_none);
20356 /* I don't think this will do the right thing with respect to types.
20357 But the only case I've seen it in so far has been array bounds, where
20358 signedness is the only information lost, and I think that will be
20359 okay. */
20360 while (CONVERT_EXPR_P (parm))
20361 parm = TREE_OPERAND (parm, 0);
20363 if (arg == error_mark_node)
20364 return unify_invalid (explain_p);
20365 if (arg == unknown_type_node
20366 || arg == init_list_type_node)
20367 /* We can't deduce anything from this, but we might get all the
20368 template args from other function args. */
20369 return unify_success (explain_p);
20371 if (parm == any_targ_node || arg == any_targ_node)
20372 return unify_success (explain_p);
20374 /* If PARM uses template parameters, then we can't bail out here,
20375 even if ARG == PARM, since we won't record unifications for the
20376 template parameters. We might need them if we're trying to
20377 figure out which of two things is more specialized. */
20378 if (arg == parm && !uses_template_parms (parm))
20379 return unify_success (explain_p);
20381 /* Handle init lists early, so the rest of the function can assume
20382 we're dealing with a type. */
20383 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20385 tree elt, elttype;
20386 unsigned i;
20387 tree orig_parm = parm;
20389 /* Replace T with std::initializer_list<T> for deduction. */
20390 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20391 && flag_deduce_init_list)
20392 parm = listify (parm);
20394 if (!is_std_init_list (parm)
20395 && TREE_CODE (parm) != ARRAY_TYPE)
20396 /* We can only deduce from an initializer list argument if the
20397 parameter is std::initializer_list or an array; otherwise this
20398 is a non-deduced context. */
20399 return unify_success (explain_p);
20401 if (TREE_CODE (parm) == ARRAY_TYPE)
20402 elttype = TREE_TYPE (parm);
20403 else
20405 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20406 /* Deduction is defined in terms of a single type, so just punt
20407 on the (bizarre) std::initializer_list<T...>. */
20408 if (PACK_EXPANSION_P (elttype))
20409 return unify_success (explain_p);
20412 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20414 int elt_strict = strict;
20416 if (elt == error_mark_node)
20417 return unify_invalid (explain_p);
20419 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20421 tree type = TREE_TYPE (elt);
20422 if (type == error_mark_node)
20423 return unify_invalid (explain_p);
20424 /* It should only be possible to get here for a call. */
20425 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20426 elt_strict |= maybe_adjust_types_for_deduction
20427 (DEDUCE_CALL, &elttype, &type, elt);
20428 elt = type;
20431 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20432 explain_p);
20435 if (TREE_CODE (parm) == ARRAY_TYPE
20436 && deducible_array_bound (TYPE_DOMAIN (parm)))
20438 /* Also deduce from the length of the initializer list. */
20439 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20440 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20441 if (idx == error_mark_node)
20442 return unify_invalid (explain_p);
20443 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20444 idx, explain_p);
20447 /* If the std::initializer_list<T> deduction worked, replace the
20448 deduced A with std::initializer_list<A>. */
20449 if (orig_parm != parm)
20451 idx = TEMPLATE_TYPE_IDX (orig_parm);
20452 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20453 targ = listify (targ);
20454 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20456 return unify_success (explain_p);
20459 /* If parm and arg aren't the same kind of thing (template, type, or
20460 expression), fail early. */
20461 if (pa_kind (parm) != pa_kind (arg))
20462 return unify_invalid (explain_p);
20464 /* Immediately reject some pairs that won't unify because of
20465 cv-qualification mismatches. */
20466 if (TREE_CODE (arg) == TREE_CODE (parm)
20467 && TYPE_P (arg)
20468 /* It is the elements of the array which hold the cv quals of an array
20469 type, and the elements might be template type parms. We'll check
20470 when we recurse. */
20471 && TREE_CODE (arg) != ARRAY_TYPE
20472 /* We check the cv-qualifiers when unifying with template type
20473 parameters below. We want to allow ARG `const T' to unify with
20474 PARM `T' for example, when computing which of two templates
20475 is more specialized, for example. */
20476 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20477 && !check_cv_quals_for_unify (strict_in, arg, parm))
20478 return unify_cv_qual_mismatch (explain_p, parm, arg);
20480 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20481 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20482 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20483 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20484 strict &= ~UNIFY_ALLOW_DERIVED;
20485 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20486 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20488 switch (TREE_CODE (parm))
20490 case TYPENAME_TYPE:
20491 case SCOPE_REF:
20492 case UNBOUND_CLASS_TEMPLATE:
20493 /* In a type which contains a nested-name-specifier, template
20494 argument values cannot be deduced for template parameters used
20495 within the nested-name-specifier. */
20496 return unify_success (explain_p);
20498 case TEMPLATE_TYPE_PARM:
20499 case TEMPLATE_TEMPLATE_PARM:
20500 case BOUND_TEMPLATE_TEMPLATE_PARM:
20501 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20502 if (error_operand_p (tparm))
20503 return unify_invalid (explain_p);
20505 if (TEMPLATE_TYPE_LEVEL (parm)
20506 != template_decl_level (tparm))
20507 /* The PARM is not one we're trying to unify. Just check
20508 to see if it matches ARG. */
20510 if (TREE_CODE (arg) == TREE_CODE (parm)
20511 && (is_auto (parm) ? is_auto (arg)
20512 : same_type_p (parm, arg)))
20513 return unify_success (explain_p);
20514 else
20515 return unify_type_mismatch (explain_p, parm, arg);
20517 idx = TEMPLATE_TYPE_IDX (parm);
20518 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20519 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20520 if (error_operand_p (tparm))
20521 return unify_invalid (explain_p);
20523 /* Check for mixed types and values. */
20524 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20525 && TREE_CODE (tparm) != TYPE_DECL)
20526 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20527 && TREE_CODE (tparm) != TEMPLATE_DECL))
20528 gcc_unreachable ();
20530 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20532 if ((strict_in & UNIFY_ALLOW_DERIVED)
20533 && CLASS_TYPE_P (arg))
20535 /* First try to match ARG directly. */
20536 tree t = try_class_unification (tparms, targs, parm, arg,
20537 explain_p);
20538 if (!t)
20540 /* Otherwise, look for a suitable base of ARG, as below. */
20541 enum template_base_result r;
20542 r = get_template_base (tparms, targs, parm, arg,
20543 explain_p, &t);
20544 if (!t)
20545 return unify_no_common_base (explain_p, r, parm, arg);
20546 arg = t;
20549 /* ARG must be constructed from a template class or a template
20550 template parameter. */
20551 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20552 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20553 return unify_template_deduction_failure (explain_p, parm, arg);
20555 /* Deduce arguments T, i from TT<T> or TT<i>. */
20556 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20557 return 1;
20559 arg = TYPE_TI_TEMPLATE (arg);
20561 /* Fall through to deduce template name. */
20564 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20565 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20567 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20569 /* Simple cases: Value already set, does match or doesn't. */
20570 if (targ != NULL_TREE && template_args_equal (targ, arg))
20571 return unify_success (explain_p);
20572 else if (targ)
20573 return unify_inconsistency (explain_p, parm, targ, arg);
20575 else
20577 /* If PARM is `const T' and ARG is only `int', we don't have
20578 a match unless we are allowing additional qualification.
20579 If ARG is `const int' and PARM is just `T' that's OK;
20580 that binds `const int' to `T'. */
20581 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20582 arg, parm))
20583 return unify_cv_qual_mismatch (explain_p, parm, arg);
20585 /* Consider the case where ARG is `const volatile int' and
20586 PARM is `const T'. Then, T should be `volatile int'. */
20587 arg = cp_build_qualified_type_real
20588 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20589 if (arg == error_mark_node)
20590 return unify_invalid (explain_p);
20592 /* Simple cases: Value already set, does match or doesn't. */
20593 if (targ != NULL_TREE && same_type_p (targ, arg))
20594 return unify_success (explain_p);
20595 else if (targ)
20596 return unify_inconsistency (explain_p, parm, targ, arg);
20598 /* Make sure that ARG is not a variable-sized array. (Note
20599 that were talking about variable-sized arrays (like
20600 `int[n]'), rather than arrays of unknown size (like
20601 `int[]').) We'll get very confused by such a type since
20602 the bound of the array is not constant, and therefore
20603 not mangleable. Besides, such types are not allowed in
20604 ISO C++, so we can do as we please here. We do allow
20605 them for 'auto' deduction, since that isn't ABI-exposed. */
20606 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20607 return unify_vla_arg (explain_p, arg);
20609 /* Strip typedefs as in convert_template_argument. */
20610 arg = canonicalize_type_argument (arg, tf_none);
20613 /* If ARG is a parameter pack or an expansion, we cannot unify
20614 against it unless PARM is also a parameter pack. */
20615 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20616 && !template_parameter_pack_p (parm))
20617 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20619 /* If the argument deduction results is a METHOD_TYPE,
20620 then there is a problem.
20621 METHOD_TYPE doesn't map to any real C++ type the result of
20622 the deduction can not be of that type. */
20623 if (TREE_CODE (arg) == METHOD_TYPE)
20624 return unify_method_type_error (explain_p, arg);
20626 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20627 return unify_success (explain_p);
20629 case TEMPLATE_PARM_INDEX:
20630 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20631 if (error_operand_p (tparm))
20632 return unify_invalid (explain_p);
20634 if (TEMPLATE_PARM_LEVEL (parm)
20635 != template_decl_level (tparm))
20637 /* The PARM is not one we're trying to unify. Just check
20638 to see if it matches ARG. */
20639 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20640 && cp_tree_equal (parm, arg));
20641 if (result)
20642 unify_expression_unequal (explain_p, parm, arg);
20643 return result;
20646 idx = TEMPLATE_PARM_IDX (parm);
20647 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20649 if (targ)
20651 int x = !cp_tree_equal (targ, arg);
20652 if (x)
20653 unify_inconsistency (explain_p, parm, targ, arg);
20654 return x;
20657 /* [temp.deduct.type] If, in the declaration of a function template
20658 with a non-type template-parameter, the non-type
20659 template-parameter is used in an expression in the function
20660 parameter-list and, if the corresponding template-argument is
20661 deduced, the template-argument type shall match the type of the
20662 template-parameter exactly, except that a template-argument
20663 deduced from an array bound may be of any integral type.
20664 The non-type parameter might use already deduced type parameters. */
20665 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20666 if (tree a = type_uses_auto (tparm))
20668 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20669 if (tparm == error_mark_node)
20670 return 1;
20673 if (!TREE_TYPE (arg))
20674 /* Template-parameter dependent expression. Just accept it for now.
20675 It will later be processed in convert_template_argument. */
20677 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20678 non_reference (tparm)))
20679 /* OK */;
20680 else if ((strict & UNIFY_ALLOW_INTEGER)
20681 && CP_INTEGRAL_TYPE_P (tparm))
20682 /* Convert the ARG to the type of PARM; the deduced non-type
20683 template argument must exactly match the types of the
20684 corresponding parameter. */
20685 arg = fold (build_nop (tparm, arg));
20686 else if (uses_template_parms (tparm))
20688 /* We haven't deduced the type of this parameter yet. */
20689 if (cxx_dialect >= cxx1z
20690 /* We deduce from array bounds in try_array_deduction. */
20691 && !(strict & UNIFY_ALLOW_INTEGER))
20693 /* Deduce it from the non-type argument. */
20694 tree atype = TREE_TYPE (arg);
20695 RECUR_AND_CHECK_FAILURE (tparms, targs,
20696 tparm, atype,
20697 UNIFY_ALLOW_NONE, explain_p);
20699 else
20700 /* Try again later. */
20701 return unify_success (explain_p);
20703 else
20704 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20706 /* If ARG is a parameter pack or an expansion, we cannot unify
20707 against it unless PARM is also a parameter pack. */
20708 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20709 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20710 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20713 bool removed_attr = false;
20714 arg = strip_typedefs_expr (arg, &removed_attr);
20716 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20717 return unify_success (explain_p);
20719 case PTRMEM_CST:
20721 /* A pointer-to-member constant can be unified only with
20722 another constant. */
20723 if (TREE_CODE (arg) != PTRMEM_CST)
20724 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20726 /* Just unify the class member. It would be useless (and possibly
20727 wrong, depending on the strict flags) to unify also
20728 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20729 arg refer to the same variable, even if through different
20730 classes. For instance:
20732 struct A { int x; };
20733 struct B : A { };
20735 Unification of &A::x and &B::x must succeed. */
20736 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20737 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20740 case POINTER_TYPE:
20742 if (!TYPE_PTR_P (arg))
20743 return unify_type_mismatch (explain_p, parm, arg);
20745 /* [temp.deduct.call]
20747 A can be another pointer or pointer to member type that can
20748 be converted to the deduced A via a qualification
20749 conversion (_conv.qual_).
20751 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20752 This will allow for additional cv-qualification of the
20753 pointed-to types if appropriate. */
20755 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20756 /* The derived-to-base conversion only persists through one
20757 level of pointers. */
20758 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20760 return unify (tparms, targs, TREE_TYPE (parm),
20761 TREE_TYPE (arg), strict, explain_p);
20764 case REFERENCE_TYPE:
20765 if (TREE_CODE (arg) != REFERENCE_TYPE)
20766 return unify_type_mismatch (explain_p, parm, arg);
20767 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20768 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20770 case ARRAY_TYPE:
20771 if (TREE_CODE (arg) != ARRAY_TYPE)
20772 return unify_type_mismatch (explain_p, parm, arg);
20773 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20774 != (TYPE_DOMAIN (arg) == NULL_TREE))
20775 return unify_type_mismatch (explain_p, parm, arg);
20776 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20777 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20778 if (TYPE_DOMAIN (parm) != NULL_TREE)
20779 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20780 TYPE_DOMAIN (arg), explain_p);
20781 return unify_success (explain_p);
20783 case REAL_TYPE:
20784 case COMPLEX_TYPE:
20785 case VECTOR_TYPE:
20786 case INTEGER_TYPE:
20787 case BOOLEAN_TYPE:
20788 case ENUMERAL_TYPE:
20789 case VOID_TYPE:
20790 case NULLPTR_TYPE:
20791 if (TREE_CODE (arg) != TREE_CODE (parm))
20792 return unify_type_mismatch (explain_p, parm, arg);
20794 /* We have already checked cv-qualification at the top of the
20795 function. */
20796 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20797 return unify_type_mismatch (explain_p, parm, arg);
20799 /* As far as unification is concerned, this wins. Later checks
20800 will invalidate it if necessary. */
20801 return unify_success (explain_p);
20803 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20804 /* Type INTEGER_CST can come from ordinary constant template args. */
20805 case INTEGER_CST:
20806 while (CONVERT_EXPR_P (arg))
20807 arg = TREE_OPERAND (arg, 0);
20809 if (TREE_CODE (arg) != INTEGER_CST)
20810 return unify_template_argument_mismatch (explain_p, parm, arg);
20811 return (tree_int_cst_equal (parm, arg)
20812 ? unify_success (explain_p)
20813 : unify_template_argument_mismatch (explain_p, parm, arg));
20815 case TREE_VEC:
20817 int i, len, argslen;
20818 int parm_variadic_p = 0;
20820 if (TREE_CODE (arg) != TREE_VEC)
20821 return unify_template_argument_mismatch (explain_p, parm, arg);
20823 len = TREE_VEC_LENGTH (parm);
20824 argslen = TREE_VEC_LENGTH (arg);
20826 /* Check for pack expansions in the parameters. */
20827 for (i = 0; i < len; ++i)
20829 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20831 if (i == len - 1)
20832 /* We can unify against something with a trailing
20833 parameter pack. */
20834 parm_variadic_p = 1;
20835 else
20836 /* [temp.deduct.type]/9: If the template argument list of
20837 P contains a pack expansion that is not the last
20838 template argument, the entire template argument list
20839 is a non-deduced context. */
20840 return unify_success (explain_p);
20844 /* If we don't have enough arguments to satisfy the parameters
20845 (not counting the pack expression at the end), or we have
20846 too many arguments for a parameter list that doesn't end in
20847 a pack expression, we can't unify. */
20848 if (parm_variadic_p
20849 ? argslen < len - parm_variadic_p
20850 : argslen != len)
20851 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20853 /* Unify all of the parameters that precede the (optional)
20854 pack expression. */
20855 for (i = 0; i < len - parm_variadic_p; ++i)
20857 RECUR_AND_CHECK_FAILURE (tparms, targs,
20858 TREE_VEC_ELT (parm, i),
20859 TREE_VEC_ELT (arg, i),
20860 UNIFY_ALLOW_NONE, explain_p);
20862 if (parm_variadic_p)
20863 return unify_pack_expansion (tparms, targs, parm, arg,
20864 DEDUCE_EXACT,
20865 /*subr=*/true, explain_p);
20866 return unify_success (explain_p);
20869 case RECORD_TYPE:
20870 case UNION_TYPE:
20871 if (TREE_CODE (arg) != TREE_CODE (parm))
20872 return unify_type_mismatch (explain_p, parm, arg);
20874 if (TYPE_PTRMEMFUNC_P (parm))
20876 if (!TYPE_PTRMEMFUNC_P (arg))
20877 return unify_type_mismatch (explain_p, parm, arg);
20879 return unify (tparms, targs,
20880 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20881 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20882 strict, explain_p);
20884 else if (TYPE_PTRMEMFUNC_P (arg))
20885 return unify_type_mismatch (explain_p, parm, arg);
20887 if (CLASSTYPE_TEMPLATE_INFO (parm))
20889 tree t = NULL_TREE;
20891 if (strict_in & UNIFY_ALLOW_DERIVED)
20893 /* First, we try to unify the PARM and ARG directly. */
20894 t = try_class_unification (tparms, targs,
20895 parm, arg, explain_p);
20897 if (!t)
20899 /* Fallback to the special case allowed in
20900 [temp.deduct.call]:
20902 If P is a class, and P has the form
20903 template-id, then A can be a derived class of
20904 the deduced A. Likewise, if P is a pointer to
20905 a class of the form template-id, A can be a
20906 pointer to a derived class pointed to by the
20907 deduced A. */
20908 enum template_base_result r;
20909 r = get_template_base (tparms, targs, parm, arg,
20910 explain_p, &t);
20912 if (!t)
20914 /* Don't give the derived diagnostic if we're
20915 already dealing with the same template. */
20916 bool same_template
20917 = (CLASSTYPE_TEMPLATE_INFO (arg)
20918 && (CLASSTYPE_TI_TEMPLATE (parm)
20919 == CLASSTYPE_TI_TEMPLATE (arg)));
20920 return unify_no_common_base (explain_p && !same_template,
20921 r, parm, arg);
20925 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20926 && (CLASSTYPE_TI_TEMPLATE (parm)
20927 == CLASSTYPE_TI_TEMPLATE (arg)))
20928 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20929 Then, we should unify `int' and `U'. */
20930 t = arg;
20931 else
20932 /* There's no chance of unification succeeding. */
20933 return unify_type_mismatch (explain_p, parm, arg);
20935 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20936 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20938 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20939 return unify_type_mismatch (explain_p, parm, arg);
20940 return unify_success (explain_p);
20942 case METHOD_TYPE:
20943 case FUNCTION_TYPE:
20945 unsigned int nargs;
20946 tree *args;
20947 tree a;
20948 unsigned int i;
20950 if (TREE_CODE (arg) != TREE_CODE (parm))
20951 return unify_type_mismatch (explain_p, parm, arg);
20953 /* CV qualifications for methods can never be deduced, they must
20954 match exactly. We need to check them explicitly here,
20955 because type_unification_real treats them as any other
20956 cv-qualified parameter. */
20957 if (TREE_CODE (parm) == METHOD_TYPE
20958 && (!check_cv_quals_for_unify
20959 (UNIFY_ALLOW_NONE,
20960 class_of_this_parm (arg),
20961 class_of_this_parm (parm))))
20962 return unify_cv_qual_mismatch (explain_p, parm, arg);
20963 if (TREE_CODE (arg) == FUNCTION_TYPE
20964 && type_memfn_quals (parm) != type_memfn_quals (arg))
20965 return unify_cv_qual_mismatch (explain_p, parm, arg);
20966 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20967 return unify_type_mismatch (explain_p, parm, arg);
20969 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20970 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20972 nargs = list_length (TYPE_ARG_TYPES (arg));
20973 args = XALLOCAVEC (tree, nargs);
20974 for (a = TYPE_ARG_TYPES (arg), i = 0;
20975 a != NULL_TREE && a != void_list_node;
20976 a = TREE_CHAIN (a), ++i)
20977 args[i] = TREE_VALUE (a);
20978 nargs = i;
20980 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20981 args, nargs, 1, DEDUCE_EXACT,
20982 LOOKUP_NORMAL, NULL, explain_p))
20983 return 1;
20985 if (flag_noexcept_type)
20987 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20988 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20989 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20990 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20991 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20992 && uses_template_parms (TREE_PURPOSE (pspec)))
20993 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20994 TREE_PURPOSE (aspec),
20995 UNIFY_ALLOW_NONE, explain_p);
20996 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20997 return unify_type_mismatch (explain_p, parm, arg);
21000 return 0;
21003 case OFFSET_TYPE:
21004 /* Unify a pointer to member with a pointer to member function, which
21005 deduces the type of the member as a function type. */
21006 if (TYPE_PTRMEMFUNC_P (arg))
21008 /* Check top-level cv qualifiers */
21009 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21010 return unify_cv_qual_mismatch (explain_p, parm, arg);
21012 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21013 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21014 UNIFY_ALLOW_NONE, explain_p);
21016 /* Determine the type of the function we are unifying against. */
21017 tree fntype = static_fn_type (arg);
21019 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21022 if (TREE_CODE (arg) != OFFSET_TYPE)
21023 return unify_type_mismatch (explain_p, parm, arg);
21024 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21025 TYPE_OFFSET_BASETYPE (arg),
21026 UNIFY_ALLOW_NONE, explain_p);
21027 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21028 strict, explain_p);
21030 case CONST_DECL:
21031 if (DECL_TEMPLATE_PARM_P (parm))
21032 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21033 if (arg != scalar_constant_value (parm))
21034 return unify_template_argument_mismatch (explain_p, parm, arg);
21035 return unify_success (explain_p);
21037 case FIELD_DECL:
21038 case TEMPLATE_DECL:
21039 /* Matched cases are handled by the ARG == PARM test above. */
21040 return unify_template_argument_mismatch (explain_p, parm, arg);
21042 case VAR_DECL:
21043 /* We might get a variable as a non-type template argument in parm if the
21044 corresponding parameter is type-dependent. Make any necessary
21045 adjustments based on whether arg is a reference. */
21046 if (CONSTANT_CLASS_P (arg))
21047 parm = fold_non_dependent_expr (parm);
21048 else if (REFERENCE_REF_P (arg))
21050 tree sub = TREE_OPERAND (arg, 0);
21051 STRIP_NOPS (sub);
21052 if (TREE_CODE (sub) == ADDR_EXPR)
21053 arg = TREE_OPERAND (sub, 0);
21055 /* Now use the normal expression code to check whether they match. */
21056 goto expr;
21058 case TYPE_ARGUMENT_PACK:
21059 case NONTYPE_ARGUMENT_PACK:
21060 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21061 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21063 case TYPEOF_TYPE:
21064 case DECLTYPE_TYPE:
21065 case UNDERLYING_TYPE:
21066 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21067 or UNDERLYING_TYPE nodes. */
21068 return unify_success (explain_p);
21070 case ERROR_MARK:
21071 /* Unification fails if we hit an error node. */
21072 return unify_invalid (explain_p);
21074 case INDIRECT_REF:
21075 if (REFERENCE_REF_P (parm))
21077 bool pexp = PACK_EXPANSION_P (arg);
21078 if (pexp)
21079 arg = PACK_EXPANSION_PATTERN (arg);
21080 if (REFERENCE_REF_P (arg))
21081 arg = TREE_OPERAND (arg, 0);
21082 if (pexp)
21083 arg = make_pack_expansion (arg);
21084 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21085 strict, explain_p);
21087 /* FALLTHRU */
21089 default:
21090 /* An unresolved overload is a nondeduced context. */
21091 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21092 return unify_success (explain_p);
21093 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21094 expr:
21095 /* We must be looking at an expression. This can happen with
21096 something like:
21098 template <int I>
21099 void foo(S<I>, S<I + 2>);
21101 This is a "nondeduced context":
21103 [deduct.type]
21105 The nondeduced contexts are:
21107 --A type that is a template-id in which one or more of
21108 the template-arguments is an expression that references
21109 a template-parameter.
21111 In these cases, we assume deduction succeeded, but don't
21112 actually infer any unifications. */
21114 if (!uses_template_parms (parm)
21115 && !template_args_equal (parm, arg))
21116 return unify_expression_unequal (explain_p, parm, arg);
21117 else
21118 return unify_success (explain_p);
21121 #undef RECUR_AND_CHECK_FAILURE
21123 /* Note that DECL can be defined in this translation unit, if
21124 required. */
21126 static void
21127 mark_definable (tree decl)
21129 tree clone;
21130 DECL_NOT_REALLY_EXTERN (decl) = 1;
21131 FOR_EACH_CLONE (clone, decl)
21132 DECL_NOT_REALLY_EXTERN (clone) = 1;
21135 /* Called if RESULT is explicitly instantiated, or is a member of an
21136 explicitly instantiated class. */
21138 void
21139 mark_decl_instantiated (tree result, int extern_p)
21141 SET_DECL_EXPLICIT_INSTANTIATION (result);
21143 /* If this entity has already been written out, it's too late to
21144 make any modifications. */
21145 if (TREE_ASM_WRITTEN (result))
21146 return;
21148 /* For anonymous namespace we don't need to do anything. */
21149 if (decl_anon_ns_mem_p (result))
21151 gcc_assert (!TREE_PUBLIC (result));
21152 return;
21155 if (TREE_CODE (result) != FUNCTION_DECL)
21156 /* The TREE_PUBLIC flag for function declarations will have been
21157 set correctly by tsubst. */
21158 TREE_PUBLIC (result) = 1;
21160 /* This might have been set by an earlier implicit instantiation. */
21161 DECL_COMDAT (result) = 0;
21163 if (extern_p)
21164 DECL_NOT_REALLY_EXTERN (result) = 0;
21165 else
21167 mark_definable (result);
21168 mark_needed (result);
21169 /* Always make artificials weak. */
21170 if (DECL_ARTIFICIAL (result) && flag_weak)
21171 comdat_linkage (result);
21172 /* For WIN32 we also want to put explicit instantiations in
21173 linkonce sections. */
21174 else if (TREE_PUBLIC (result))
21175 maybe_make_one_only (result);
21178 /* If EXTERN_P, then this function will not be emitted -- unless
21179 followed by an explicit instantiation, at which point its linkage
21180 will be adjusted. If !EXTERN_P, then this function will be
21181 emitted here. In neither circumstance do we want
21182 import_export_decl to adjust the linkage. */
21183 DECL_INTERFACE_KNOWN (result) = 1;
21186 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21187 important template arguments. If any are missing, we check whether
21188 they're important by using error_mark_node for substituting into any
21189 args that were used for partial ordering (the ones between ARGS and END)
21190 and seeing if it bubbles up. */
21192 static bool
21193 check_undeduced_parms (tree targs, tree args, tree end)
21195 bool found = false;
21196 int i;
21197 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21198 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21200 found = true;
21201 TREE_VEC_ELT (targs, i) = error_mark_node;
21203 if (found)
21205 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21206 if (substed == error_mark_node)
21207 return true;
21209 return false;
21212 /* Given two function templates PAT1 and PAT2, return:
21214 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21215 -1 if PAT2 is more specialized than PAT1.
21216 0 if neither is more specialized.
21218 LEN indicates the number of parameters we should consider
21219 (defaulted parameters should not be considered).
21221 The 1998 std underspecified function template partial ordering, and
21222 DR214 addresses the issue. We take pairs of arguments, one from
21223 each of the templates, and deduce them against each other. One of
21224 the templates will be more specialized if all the *other*
21225 template's arguments deduce against its arguments and at least one
21226 of its arguments *does* *not* deduce against the other template's
21227 corresponding argument. Deduction is done as for class templates.
21228 The arguments used in deduction have reference and top level cv
21229 qualifiers removed. Iff both arguments were originally reference
21230 types *and* deduction succeeds in both directions, an lvalue reference
21231 wins against an rvalue reference and otherwise the template
21232 with the more cv-qualified argument wins for that pairing (if
21233 neither is more cv-qualified, they both are equal). Unlike regular
21234 deduction, after all the arguments have been deduced in this way,
21235 we do *not* verify the deduced template argument values can be
21236 substituted into non-deduced contexts.
21238 The logic can be a bit confusing here, because we look at deduce1 and
21239 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21240 can find template arguments for pat1 to make arg1 look like arg2, that
21241 means that arg2 is at least as specialized as arg1. */
21244 more_specialized_fn (tree pat1, tree pat2, int len)
21246 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21247 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21248 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21249 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21250 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21251 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21252 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21253 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21254 tree origs1, origs2;
21255 bool lose1 = false;
21256 bool lose2 = false;
21258 /* Remove the this parameter from non-static member functions. If
21259 one is a non-static member function and the other is not a static
21260 member function, remove the first parameter from that function
21261 also. This situation occurs for operator functions where we
21262 locate both a member function (with this pointer) and non-member
21263 operator (with explicit first operand). */
21264 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21266 len--; /* LEN is the number of significant arguments for DECL1 */
21267 args1 = TREE_CHAIN (args1);
21268 if (!DECL_STATIC_FUNCTION_P (decl2))
21269 args2 = TREE_CHAIN (args2);
21271 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21273 args2 = TREE_CHAIN (args2);
21274 if (!DECL_STATIC_FUNCTION_P (decl1))
21276 len--;
21277 args1 = TREE_CHAIN (args1);
21281 /* If only one is a conversion operator, they are unordered. */
21282 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21283 return 0;
21285 /* Consider the return type for a conversion function */
21286 if (DECL_CONV_FN_P (decl1))
21288 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21289 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21290 len++;
21293 processing_template_decl++;
21295 origs1 = args1;
21296 origs2 = args2;
21298 while (len--
21299 /* Stop when an ellipsis is seen. */
21300 && args1 != NULL_TREE && args2 != NULL_TREE)
21302 tree arg1 = TREE_VALUE (args1);
21303 tree arg2 = TREE_VALUE (args2);
21304 int deduce1, deduce2;
21305 int quals1 = -1;
21306 int quals2 = -1;
21307 int ref1 = 0;
21308 int ref2 = 0;
21310 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21311 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21313 /* When both arguments are pack expansions, we need only
21314 unify the patterns themselves. */
21315 arg1 = PACK_EXPANSION_PATTERN (arg1);
21316 arg2 = PACK_EXPANSION_PATTERN (arg2);
21318 /* This is the last comparison we need to do. */
21319 len = 0;
21322 /* DR 1847: If a particular P contains no template-parameters that
21323 participate in template argument deduction, that P is not used to
21324 determine the ordering. */
21325 if (!uses_deducible_template_parms (arg1)
21326 && !uses_deducible_template_parms (arg2))
21327 goto next;
21329 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21331 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21332 arg1 = TREE_TYPE (arg1);
21333 quals1 = cp_type_quals (arg1);
21336 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21338 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21339 arg2 = TREE_TYPE (arg2);
21340 quals2 = cp_type_quals (arg2);
21343 arg1 = TYPE_MAIN_VARIANT (arg1);
21344 arg2 = TYPE_MAIN_VARIANT (arg2);
21346 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21348 int i, len2 = remaining_arguments (args2);
21349 tree parmvec = make_tree_vec (1);
21350 tree argvec = make_tree_vec (len2);
21351 tree ta = args2;
21353 /* Setup the parameter vector, which contains only ARG1. */
21354 TREE_VEC_ELT (parmvec, 0) = arg1;
21356 /* Setup the argument vector, which contains the remaining
21357 arguments. */
21358 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21359 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21361 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21362 argvec, DEDUCE_EXACT,
21363 /*subr=*/true, /*explain_p=*/false)
21364 == 0);
21366 /* We cannot deduce in the other direction, because ARG1 is
21367 a pack expansion but ARG2 is not. */
21368 deduce2 = 0;
21370 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21372 int i, len1 = remaining_arguments (args1);
21373 tree parmvec = make_tree_vec (1);
21374 tree argvec = make_tree_vec (len1);
21375 tree ta = args1;
21377 /* Setup the parameter vector, which contains only ARG1. */
21378 TREE_VEC_ELT (parmvec, 0) = arg2;
21380 /* Setup the argument vector, which contains the remaining
21381 arguments. */
21382 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21383 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21385 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21386 argvec, DEDUCE_EXACT,
21387 /*subr=*/true, /*explain_p=*/false)
21388 == 0);
21390 /* We cannot deduce in the other direction, because ARG2 is
21391 a pack expansion but ARG1 is not.*/
21392 deduce1 = 0;
21395 else
21397 /* The normal case, where neither argument is a pack
21398 expansion. */
21399 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21400 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21401 == 0);
21402 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21403 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21404 == 0);
21407 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21408 arg2, then arg2 is not as specialized as arg1. */
21409 if (!deduce1)
21410 lose2 = true;
21411 if (!deduce2)
21412 lose1 = true;
21414 /* "If, for a given type, deduction succeeds in both directions
21415 (i.e., the types are identical after the transformations above)
21416 and both P and A were reference types (before being replaced with
21417 the type referred to above):
21418 - if the type from the argument template was an lvalue reference and
21419 the type from the parameter template was not, the argument type is
21420 considered to be more specialized than the other; otherwise,
21421 - if the type from the argument template is more cv-qualified
21422 than the type from the parameter template (as described above),
21423 the argument type is considered to be more specialized than the other;
21424 otherwise,
21425 - neither type is more specialized than the other." */
21427 if (deduce1 && deduce2)
21429 if (ref1 && ref2 && ref1 != ref2)
21431 if (ref1 > ref2)
21432 lose1 = true;
21433 else
21434 lose2 = true;
21436 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21438 if ((quals1 & quals2) == quals2)
21439 lose2 = true;
21440 if ((quals1 & quals2) == quals1)
21441 lose1 = true;
21445 if (lose1 && lose2)
21446 /* We've failed to deduce something in either direction.
21447 These must be unordered. */
21448 break;
21450 next:
21452 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21453 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21454 /* We have already processed all of the arguments in our
21455 handing of the pack expansion type. */
21456 len = 0;
21458 args1 = TREE_CHAIN (args1);
21459 args2 = TREE_CHAIN (args2);
21462 /* "In most cases, all template parameters must have values in order for
21463 deduction to succeed, but for partial ordering purposes a template
21464 parameter may remain without a value provided it is not used in the
21465 types being used for partial ordering."
21467 Thus, if we are missing any of the targs1 we need to substitute into
21468 origs1, then pat2 is not as specialized as pat1. This can happen when
21469 there is a nondeduced context. */
21470 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21471 lose2 = true;
21472 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21473 lose1 = true;
21475 processing_template_decl--;
21477 /* If both deductions succeed, the partial ordering selects the more
21478 constrained template. */
21479 if (!lose1 && !lose2)
21481 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21482 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21483 lose1 = !subsumes_constraints (c1, c2);
21484 lose2 = !subsumes_constraints (c2, c1);
21487 /* All things being equal, if the next argument is a pack expansion
21488 for one function but not for the other, prefer the
21489 non-variadic function. FIXME this is bogus; see c++/41958. */
21490 if (lose1 == lose2
21491 && args1 && TREE_VALUE (args1)
21492 && args2 && TREE_VALUE (args2))
21494 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21495 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21498 if (lose1 == lose2)
21499 return 0;
21500 else if (!lose1)
21501 return 1;
21502 else
21503 return -1;
21506 /* Determine which of two partial specializations of TMPL is more
21507 specialized.
21509 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21510 to the first partial specialization. The TREE_PURPOSE is the
21511 innermost set of template parameters for the partial
21512 specialization. PAT2 is similar, but for the second template.
21514 Return 1 if the first partial specialization is more specialized;
21515 -1 if the second is more specialized; 0 if neither is more
21516 specialized.
21518 See [temp.class.order] for information about determining which of
21519 two templates is more specialized. */
21521 static int
21522 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21524 tree targs;
21525 int winner = 0;
21526 bool any_deductions = false;
21528 tree tmpl1 = TREE_VALUE (pat1);
21529 tree tmpl2 = TREE_VALUE (pat2);
21530 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21531 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21533 /* Just like what happens for functions, if we are ordering between
21534 different template specializations, we may encounter dependent
21535 types in the arguments, and we need our dependency check functions
21536 to behave correctly. */
21537 ++processing_template_decl;
21538 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21539 if (targs)
21541 --winner;
21542 any_deductions = true;
21545 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21546 if (targs)
21548 ++winner;
21549 any_deductions = true;
21551 --processing_template_decl;
21553 /* If both deductions succeed, the partial ordering selects the more
21554 constrained template. */
21555 if (!winner && any_deductions)
21556 return more_constrained (tmpl1, tmpl2);
21558 /* In the case of a tie where at least one of the templates
21559 has a parameter pack at the end, the template with the most
21560 non-packed parameters wins. */
21561 if (winner == 0
21562 && any_deductions
21563 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21564 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21566 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21567 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21568 int len1 = TREE_VEC_LENGTH (args1);
21569 int len2 = TREE_VEC_LENGTH (args2);
21571 /* We don't count the pack expansion at the end. */
21572 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21573 --len1;
21574 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21575 --len2;
21577 if (len1 > len2)
21578 return 1;
21579 else if (len1 < len2)
21580 return -1;
21583 return winner;
21586 /* Return the template arguments that will produce the function signature
21587 DECL from the function template FN, with the explicit template
21588 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21589 also match. Return NULL_TREE if no satisfactory arguments could be
21590 found. */
21592 static tree
21593 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21595 int ntparms = DECL_NTPARMS (fn);
21596 tree targs = make_tree_vec (ntparms);
21597 tree decl_type = TREE_TYPE (decl);
21598 tree decl_arg_types;
21599 tree *args;
21600 unsigned int nargs, ix;
21601 tree arg;
21603 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21605 /* Never do unification on the 'this' parameter. */
21606 decl_arg_types = skip_artificial_parms_for (decl,
21607 TYPE_ARG_TYPES (decl_type));
21609 nargs = list_length (decl_arg_types);
21610 args = XALLOCAVEC (tree, nargs);
21611 for (arg = decl_arg_types, ix = 0;
21612 arg != NULL_TREE && arg != void_list_node;
21613 arg = TREE_CHAIN (arg), ++ix)
21614 args[ix] = TREE_VALUE (arg);
21616 if (fn_type_unification (fn, explicit_args, targs,
21617 args, ix,
21618 (check_rettype || DECL_CONV_FN_P (fn)
21619 ? TREE_TYPE (decl_type) : NULL_TREE),
21620 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21621 /*decltype*/false)
21622 == error_mark_node)
21623 return NULL_TREE;
21625 return targs;
21628 /* Return the innermost template arguments that, when applied to a partial
21629 specialization SPEC_TMPL of TMPL, yield the ARGS.
21631 For example, suppose we have:
21633 template <class T, class U> struct S {};
21634 template <class T> struct S<T*, int> {};
21636 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21637 partial specialization and the ARGS will be {double*, int}. The resulting
21638 vector will be {double}, indicating that `T' is bound to `double'. */
21640 static tree
21641 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21643 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21644 tree spec_args
21645 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21646 int i, ntparms = TREE_VEC_LENGTH (tparms);
21647 tree deduced_args;
21648 tree innermost_deduced_args;
21650 innermost_deduced_args = make_tree_vec (ntparms);
21651 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21653 deduced_args = copy_node (args);
21654 SET_TMPL_ARGS_LEVEL (deduced_args,
21655 TMPL_ARGS_DEPTH (deduced_args),
21656 innermost_deduced_args);
21658 else
21659 deduced_args = innermost_deduced_args;
21661 bool tried_array_deduction = (cxx_dialect < cxx1z);
21662 again:
21663 if (unify (tparms, deduced_args,
21664 INNERMOST_TEMPLATE_ARGS (spec_args),
21665 INNERMOST_TEMPLATE_ARGS (args),
21666 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21667 return NULL_TREE;
21669 for (i = 0; i < ntparms; ++i)
21670 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21672 if (!tried_array_deduction)
21674 try_array_deduction (tparms, innermost_deduced_args,
21675 INNERMOST_TEMPLATE_ARGS (spec_args));
21676 tried_array_deduction = true;
21677 if (TREE_VEC_ELT (innermost_deduced_args, i))
21678 goto again;
21680 return NULL_TREE;
21683 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21684 if (!push_tinst_level (tinst))
21686 excessive_deduction_depth = true;
21687 return NULL_TREE;
21690 /* Verify that nondeduced template arguments agree with the type
21691 obtained from argument deduction.
21693 For example:
21695 struct A { typedef int X; };
21696 template <class T, class U> struct C {};
21697 template <class T> struct C<T, typename T::X> {};
21699 Then with the instantiation `C<A, int>', we can deduce that
21700 `T' is `A' but unify () does not check whether `typename T::X'
21701 is `int'. */
21702 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21704 if (spec_args != error_mark_node)
21705 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21706 INNERMOST_TEMPLATE_ARGS (spec_args),
21707 tmpl, tf_none, false, false);
21709 pop_tinst_level ();
21711 if (spec_args == error_mark_node
21712 /* We only need to check the innermost arguments; the other
21713 arguments will always agree. */
21714 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21715 INNERMOST_TEMPLATE_ARGS (args)))
21716 return NULL_TREE;
21718 /* Now that we have bindings for all of the template arguments,
21719 ensure that the arguments deduced for the template template
21720 parameters have compatible template parameter lists. See the use
21721 of template_template_parm_bindings_ok_p in fn_type_unification
21722 for more information. */
21723 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21724 return NULL_TREE;
21726 return deduced_args;
21729 // Compare two function templates T1 and T2 by deducing bindings
21730 // from one against the other. If both deductions succeed, compare
21731 // constraints to see which is more constrained.
21732 static int
21733 more_specialized_inst (tree t1, tree t2)
21735 int fate = 0;
21736 int count = 0;
21738 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21740 --fate;
21741 ++count;
21744 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21746 ++fate;
21747 ++count;
21750 // If both deductions succeed, then one may be more constrained.
21751 if (count == 2 && fate == 0)
21752 fate = more_constrained (t1, t2);
21754 return fate;
21757 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21758 Return the TREE_LIST node with the most specialized template, if
21759 any. If there is no most specialized template, the error_mark_node
21760 is returned.
21762 Note that this function does not look at, or modify, the
21763 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21764 returned is one of the elements of INSTANTIATIONS, callers may
21765 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21766 and retrieve it from the value returned. */
21768 tree
21769 most_specialized_instantiation (tree templates)
21771 tree fn, champ;
21773 ++processing_template_decl;
21775 champ = templates;
21776 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21778 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21779 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21780 if (fate == -1)
21781 champ = fn;
21782 else if (!fate)
21784 /* Equally specialized, move to next function. If there
21785 is no next function, nothing's most specialized. */
21786 fn = TREE_CHAIN (fn);
21787 champ = fn;
21788 if (!fn)
21789 break;
21793 if (champ)
21794 /* Now verify that champ is better than everything earlier in the
21795 instantiation list. */
21796 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21797 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21799 champ = NULL_TREE;
21800 break;
21804 processing_template_decl--;
21806 if (!champ)
21807 return error_mark_node;
21809 return champ;
21812 /* If DECL is a specialization of some template, return the most
21813 general such template. Otherwise, returns NULL_TREE.
21815 For example, given:
21817 template <class T> struct S { template <class U> void f(U); };
21819 if TMPL is `template <class U> void S<int>::f(U)' this will return
21820 the full template. This function will not trace past partial
21821 specializations, however. For example, given in addition:
21823 template <class T> struct S<T*> { template <class U> void f(U); };
21825 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21826 `template <class T> template <class U> S<T*>::f(U)'. */
21828 tree
21829 most_general_template (tree decl)
21831 if (TREE_CODE (decl) != TEMPLATE_DECL)
21833 if (tree tinfo = get_template_info (decl))
21834 decl = TI_TEMPLATE (tinfo);
21835 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21836 template friend, or a FIELD_DECL for a capture pack. */
21837 if (TREE_CODE (decl) != TEMPLATE_DECL)
21838 return NULL_TREE;
21841 /* Look for more and more general templates. */
21842 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21844 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21845 (See cp-tree.h for details.) */
21846 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21847 break;
21849 if (CLASS_TYPE_P (TREE_TYPE (decl))
21850 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21851 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21852 break;
21854 /* Stop if we run into an explicitly specialized class template. */
21855 if (!DECL_NAMESPACE_SCOPE_P (decl)
21856 && DECL_CONTEXT (decl)
21857 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21858 break;
21860 decl = DECL_TI_TEMPLATE (decl);
21863 return decl;
21866 /* Return the most specialized of the template partial specializations
21867 which can produce TARGET, a specialization of some class or variable
21868 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21869 a TEMPLATE_DECL node corresponding to the partial specialization, while
21870 the TREE_PURPOSE is the set of template arguments that must be
21871 substituted into the template pattern in order to generate TARGET.
21873 If the choice of partial specialization is ambiguous, a diagnostic
21874 is issued, and the error_mark_node is returned. If there are no
21875 partial specializations matching TARGET, then NULL_TREE is
21876 returned, indicating that the primary template should be used. */
21878 static tree
21879 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21881 tree list = NULL_TREE;
21882 tree t;
21883 tree champ;
21884 int fate;
21885 bool ambiguous_p;
21886 tree outer_args = NULL_TREE;
21887 tree tmpl, args;
21889 if (TYPE_P (target))
21891 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21892 tmpl = TI_TEMPLATE (tinfo);
21893 args = TI_ARGS (tinfo);
21895 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21897 tmpl = TREE_OPERAND (target, 0);
21898 args = TREE_OPERAND (target, 1);
21900 else if (VAR_P (target))
21902 tree tinfo = DECL_TEMPLATE_INFO (target);
21903 tmpl = TI_TEMPLATE (tinfo);
21904 args = TI_ARGS (tinfo);
21906 else
21907 gcc_unreachable ();
21909 tree main_tmpl = most_general_template (tmpl);
21911 /* For determining which partial specialization to use, only the
21912 innermost args are interesting. */
21913 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21915 outer_args = strip_innermost_template_args (args, 1);
21916 args = INNERMOST_TEMPLATE_ARGS (args);
21919 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21921 tree spec_args;
21922 tree spec_tmpl = TREE_VALUE (t);
21924 if (outer_args)
21926 /* Substitute in the template args from the enclosing class. */
21927 ++processing_template_decl;
21928 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21929 --processing_template_decl;
21932 if (spec_tmpl == error_mark_node)
21933 return error_mark_node;
21935 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21936 if (spec_args)
21938 if (outer_args)
21939 spec_args = add_to_template_args (outer_args, spec_args);
21941 /* Keep the candidate only if the constraints are satisfied,
21942 or if we're not compiling with concepts. */
21943 if (!flag_concepts
21944 || constraints_satisfied_p (spec_tmpl, spec_args))
21946 list = tree_cons (spec_args, TREE_VALUE (t), list);
21947 TREE_TYPE (list) = TREE_TYPE (t);
21952 if (! list)
21953 return NULL_TREE;
21955 ambiguous_p = false;
21956 t = list;
21957 champ = t;
21958 t = TREE_CHAIN (t);
21959 for (; t; t = TREE_CHAIN (t))
21961 fate = more_specialized_partial_spec (tmpl, champ, t);
21962 if (fate == 1)
21964 else
21966 if (fate == 0)
21968 t = TREE_CHAIN (t);
21969 if (! t)
21971 ambiguous_p = true;
21972 break;
21975 champ = t;
21979 if (!ambiguous_p)
21980 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21982 fate = more_specialized_partial_spec (tmpl, champ, t);
21983 if (fate != 1)
21985 ambiguous_p = true;
21986 break;
21990 if (ambiguous_p)
21992 const char *str;
21993 char *spaces = NULL;
21994 if (!(complain & tf_error))
21995 return error_mark_node;
21996 if (TYPE_P (target))
21997 error ("ambiguous template instantiation for %q#T", target);
21998 else
21999 error ("ambiguous template instantiation for %q#D", target);
22000 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22001 for (t = list; t; t = TREE_CHAIN (t))
22003 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22004 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22005 "%s %#qS", spaces ? spaces : str, subst);
22006 spaces = spaces ? spaces : get_spaces (str);
22008 free (spaces);
22009 return error_mark_node;
22012 return champ;
22015 /* Explicitly instantiate DECL. */
22017 void
22018 do_decl_instantiation (tree decl, tree storage)
22020 tree result = NULL_TREE;
22021 int extern_p = 0;
22023 if (!decl || decl == error_mark_node)
22024 /* An error occurred, for which grokdeclarator has already issued
22025 an appropriate message. */
22026 return;
22027 else if (! DECL_LANG_SPECIFIC (decl))
22029 error ("explicit instantiation of non-template %q#D", decl);
22030 return;
22033 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22034 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22036 if (VAR_P (decl) && !var_templ)
22038 /* There is an asymmetry here in the way VAR_DECLs and
22039 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22040 the latter, the DECL we get back will be marked as a
22041 template instantiation, and the appropriate
22042 DECL_TEMPLATE_INFO will be set up. This does not happen for
22043 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22044 should handle VAR_DECLs as it currently handles
22045 FUNCTION_DECLs. */
22046 if (!DECL_CLASS_SCOPE_P (decl))
22048 error ("%qD is not a static data member of a class template", decl);
22049 return;
22051 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22052 if (!result || !VAR_P (result))
22054 error ("no matching template for %qD found", decl);
22055 return;
22057 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22059 error ("type %qT for explicit instantiation %qD does not match "
22060 "declared type %qT", TREE_TYPE (result), decl,
22061 TREE_TYPE (decl));
22062 return;
22065 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22067 error ("explicit instantiation of %q#D", decl);
22068 return;
22070 else
22071 result = decl;
22073 /* Check for various error cases. Note that if the explicit
22074 instantiation is valid the RESULT will currently be marked as an
22075 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22076 until we get here. */
22078 if (DECL_TEMPLATE_SPECIALIZATION (result))
22080 /* DR 259 [temp.spec].
22082 Both an explicit instantiation and a declaration of an explicit
22083 specialization shall not appear in a program unless the explicit
22084 instantiation follows a declaration of the explicit specialization.
22086 For a given set of template parameters, if an explicit
22087 instantiation of a template appears after a declaration of an
22088 explicit specialization for that template, the explicit
22089 instantiation has no effect. */
22090 return;
22092 else if (DECL_EXPLICIT_INSTANTIATION (result))
22094 /* [temp.spec]
22096 No program shall explicitly instantiate any template more
22097 than once.
22099 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22100 the first instantiation was `extern' and the second is not,
22101 and EXTERN_P for the opposite case. */
22102 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22103 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22104 /* If an "extern" explicit instantiation follows an ordinary
22105 explicit instantiation, the template is instantiated. */
22106 if (extern_p)
22107 return;
22109 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22111 error ("no matching template for %qD found", result);
22112 return;
22114 else if (!DECL_TEMPLATE_INFO (result))
22116 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22117 return;
22120 if (storage == NULL_TREE)
22122 else if (storage == ridpointers[(int) RID_EXTERN])
22124 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22125 pedwarn (input_location, OPT_Wpedantic,
22126 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22127 "instantiations");
22128 extern_p = 1;
22130 else
22131 error ("storage class %qD applied to template instantiation", storage);
22133 check_explicit_instantiation_namespace (result);
22134 mark_decl_instantiated (result, extern_p);
22135 if (! extern_p)
22136 instantiate_decl (result, /*defer_ok=*/true,
22137 /*expl_inst_class_mem_p=*/false);
22140 static void
22141 mark_class_instantiated (tree t, int extern_p)
22143 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22144 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22145 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22146 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22147 if (! extern_p)
22149 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22150 rest_of_type_compilation (t, 1);
22154 /* Called from do_type_instantiation through binding_table_foreach to
22155 do recursive instantiation for the type bound in ENTRY. */
22156 static void
22157 bt_instantiate_type_proc (binding_entry entry, void *data)
22159 tree storage = *(tree *) data;
22161 if (MAYBE_CLASS_TYPE_P (entry->type)
22162 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22163 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22166 /* Perform an explicit instantiation of template class T. STORAGE, if
22167 non-null, is the RID for extern, inline or static. COMPLAIN is
22168 nonzero if this is called from the parser, zero if called recursively,
22169 since the standard is unclear (as detailed below). */
22171 void
22172 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22174 int extern_p = 0;
22175 int nomem_p = 0;
22176 int static_p = 0;
22177 int previous_instantiation_extern_p = 0;
22179 if (TREE_CODE (t) == TYPE_DECL)
22180 t = TREE_TYPE (t);
22182 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22184 tree tmpl =
22185 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22186 if (tmpl)
22187 error ("explicit instantiation of non-class template %qD", tmpl);
22188 else
22189 error ("explicit instantiation of non-template type %qT", t);
22190 return;
22193 complete_type (t);
22195 if (!COMPLETE_TYPE_P (t))
22197 if (complain & tf_error)
22198 error ("explicit instantiation of %q#T before definition of template",
22200 return;
22203 if (storage != NULL_TREE)
22205 if (!in_system_header_at (input_location))
22207 if (storage == ridpointers[(int) RID_EXTERN])
22209 if (cxx_dialect == cxx98)
22210 pedwarn (input_location, OPT_Wpedantic,
22211 "ISO C++ 1998 forbids the use of %<extern%> on "
22212 "explicit instantiations");
22214 else
22215 pedwarn (input_location, OPT_Wpedantic,
22216 "ISO C++ forbids the use of %qE"
22217 " on explicit instantiations", storage);
22220 if (storage == ridpointers[(int) RID_INLINE])
22221 nomem_p = 1;
22222 else if (storage == ridpointers[(int) RID_EXTERN])
22223 extern_p = 1;
22224 else if (storage == ridpointers[(int) RID_STATIC])
22225 static_p = 1;
22226 else
22228 error ("storage class %qD applied to template instantiation",
22229 storage);
22230 extern_p = 0;
22234 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22236 /* DR 259 [temp.spec].
22238 Both an explicit instantiation and a declaration of an explicit
22239 specialization shall not appear in a program unless the explicit
22240 instantiation follows a declaration of the explicit specialization.
22242 For a given set of template parameters, if an explicit
22243 instantiation of a template appears after a declaration of an
22244 explicit specialization for that template, the explicit
22245 instantiation has no effect. */
22246 return;
22248 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22250 /* [temp.spec]
22252 No program shall explicitly instantiate any template more
22253 than once.
22255 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22256 instantiation was `extern'. If EXTERN_P then the second is.
22257 These cases are OK. */
22258 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22260 if (!previous_instantiation_extern_p && !extern_p
22261 && (complain & tf_error))
22262 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22264 /* If we've already instantiated the template, just return now. */
22265 if (!CLASSTYPE_INTERFACE_ONLY (t))
22266 return;
22269 check_explicit_instantiation_namespace (TYPE_NAME (t));
22270 mark_class_instantiated (t, extern_p);
22272 if (nomem_p)
22273 return;
22275 /* In contrast to implicit instantiation, where only the
22276 declarations, and not the definitions, of members are
22277 instantiated, we have here:
22279 [temp.explicit]
22281 The explicit instantiation of a class template specialization
22282 implies the instantiation of all of its members not
22283 previously explicitly specialized in the translation unit
22284 containing the explicit instantiation.
22286 Of course, we can't instantiate member template classes, since we
22287 don't have any arguments for them. Note that the standard is
22288 unclear on whether the instantiation of the members are
22289 *explicit* instantiations or not. However, the most natural
22290 interpretation is that it should be an explicit
22291 instantiation. */
22292 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22293 if ((VAR_P (fld)
22294 || (TREE_CODE (fld) == FUNCTION_DECL
22295 && !static_p
22296 && user_provided_p (fld)))
22297 && DECL_TEMPLATE_INSTANTIATION (fld))
22299 mark_decl_instantiated (fld, extern_p);
22300 if (! extern_p)
22301 instantiate_decl (fld, /*defer_ok=*/true,
22302 /*expl_inst_class_mem_p=*/true);
22305 if (CLASSTYPE_NESTED_UTDS (t))
22306 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22307 bt_instantiate_type_proc, &storage);
22310 /* Given a function DECL, which is a specialization of TMPL, modify
22311 DECL to be a re-instantiation of TMPL with the same template
22312 arguments. TMPL should be the template into which tsubst'ing
22313 should occur for DECL, not the most general template.
22315 One reason for doing this is a scenario like this:
22317 template <class T>
22318 void f(const T&, int i);
22320 void g() { f(3, 7); }
22322 template <class T>
22323 void f(const T& t, const int i) { }
22325 Note that when the template is first instantiated, with
22326 instantiate_template, the resulting DECL will have no name for the
22327 first parameter, and the wrong type for the second. So, when we go
22328 to instantiate the DECL, we regenerate it. */
22330 static void
22331 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22333 /* The arguments used to instantiate DECL, from the most general
22334 template. */
22335 tree code_pattern;
22337 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22339 /* Make sure that we can see identifiers, and compute access
22340 correctly. */
22341 push_access_scope (decl);
22343 if (TREE_CODE (decl) == FUNCTION_DECL)
22345 tree decl_parm;
22346 tree pattern_parm;
22347 tree specs;
22348 int args_depth;
22349 int parms_depth;
22351 args_depth = TMPL_ARGS_DEPTH (args);
22352 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22353 if (args_depth > parms_depth)
22354 args = get_innermost_template_args (args, parms_depth);
22356 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22357 args, tf_error, NULL_TREE,
22358 /*defer_ok*/false);
22359 if (specs && specs != error_mark_node)
22360 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22361 specs);
22363 /* Merge parameter declarations. */
22364 decl_parm = skip_artificial_parms_for (decl,
22365 DECL_ARGUMENTS (decl));
22366 pattern_parm
22367 = skip_artificial_parms_for (code_pattern,
22368 DECL_ARGUMENTS (code_pattern));
22369 while (decl_parm && !DECL_PACK_P (pattern_parm))
22371 tree parm_type;
22372 tree attributes;
22374 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22375 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22376 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22377 NULL_TREE);
22378 parm_type = type_decays_to (parm_type);
22379 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22380 TREE_TYPE (decl_parm) = parm_type;
22381 attributes = DECL_ATTRIBUTES (pattern_parm);
22382 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22384 DECL_ATTRIBUTES (decl_parm) = attributes;
22385 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22387 decl_parm = DECL_CHAIN (decl_parm);
22388 pattern_parm = DECL_CHAIN (pattern_parm);
22390 /* Merge any parameters that match with the function parameter
22391 pack. */
22392 if (pattern_parm && DECL_PACK_P (pattern_parm))
22394 int i, len;
22395 tree expanded_types;
22396 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22397 the parameters in this function parameter pack. */
22398 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22399 args, tf_error, NULL_TREE);
22400 len = TREE_VEC_LENGTH (expanded_types);
22401 for (i = 0; i < len; i++)
22403 tree parm_type;
22404 tree attributes;
22406 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22407 /* Rename the parameter to include the index. */
22408 DECL_NAME (decl_parm) =
22409 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22410 parm_type = TREE_VEC_ELT (expanded_types, i);
22411 parm_type = type_decays_to (parm_type);
22412 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22413 TREE_TYPE (decl_parm) = parm_type;
22414 attributes = DECL_ATTRIBUTES (pattern_parm);
22415 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22417 DECL_ATTRIBUTES (decl_parm) = attributes;
22418 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22420 decl_parm = DECL_CHAIN (decl_parm);
22423 /* Merge additional specifiers from the CODE_PATTERN. */
22424 if (DECL_DECLARED_INLINE_P (code_pattern)
22425 && !DECL_DECLARED_INLINE_P (decl))
22426 DECL_DECLARED_INLINE_P (decl) = 1;
22428 else if (VAR_P (decl))
22430 DECL_INITIAL (decl) =
22431 tsubst_expr (DECL_INITIAL (code_pattern), args,
22432 tf_error, DECL_TI_TEMPLATE (decl),
22433 /*integral_constant_expression_p=*/false);
22434 if (VAR_HAD_UNKNOWN_BOUND (decl))
22435 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22436 tf_error, DECL_TI_TEMPLATE (decl));
22438 else
22439 gcc_unreachable ();
22441 pop_access_scope (decl);
22444 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22445 substituted to get DECL. */
22447 tree
22448 template_for_substitution (tree decl)
22450 tree tmpl = DECL_TI_TEMPLATE (decl);
22452 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22453 for the instantiation. This is not always the most general
22454 template. Consider, for example:
22456 template <class T>
22457 struct S { template <class U> void f();
22458 template <> void f<int>(); };
22460 and an instantiation of S<double>::f<int>. We want TD to be the
22461 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22462 while (/* An instantiation cannot have a definition, so we need a
22463 more general template. */
22464 DECL_TEMPLATE_INSTANTIATION (tmpl)
22465 /* We must also deal with friend templates. Given:
22467 template <class T> struct S {
22468 template <class U> friend void f() {};
22471 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22472 so far as the language is concerned, but that's still
22473 where we get the pattern for the instantiation from. On
22474 other hand, if the definition comes outside the class, say:
22476 template <class T> struct S {
22477 template <class U> friend void f();
22479 template <class U> friend void f() {}
22481 we don't need to look any further. That's what the check for
22482 DECL_INITIAL is for. */
22483 || (TREE_CODE (decl) == FUNCTION_DECL
22484 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22485 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22487 /* The present template, TD, should not be a definition. If it
22488 were a definition, we should be using it! Note that we
22489 cannot restructure the loop to just keep going until we find
22490 a template with a definition, since that might go too far if
22491 a specialization was declared, but not defined. */
22493 /* Fetch the more general template. */
22494 tmpl = DECL_TI_TEMPLATE (tmpl);
22497 return tmpl;
22500 /* Returns true if we need to instantiate this template instance even if we
22501 know we aren't going to emit it. */
22503 bool
22504 always_instantiate_p (tree decl)
22506 /* We always instantiate inline functions so that we can inline them. An
22507 explicit instantiation declaration prohibits implicit instantiation of
22508 non-inline functions. With high levels of optimization, we would
22509 normally inline non-inline functions -- but we're not allowed to do
22510 that for "extern template" functions. Therefore, we check
22511 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22512 return ((TREE_CODE (decl) == FUNCTION_DECL
22513 && (DECL_DECLARED_INLINE_P (decl)
22514 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22515 /* And we need to instantiate static data members so that
22516 their initializers are available in integral constant
22517 expressions. */
22518 || (VAR_P (decl)
22519 && decl_maybe_constant_var_p (decl)));
22522 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22523 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22524 error, true otherwise. */
22526 bool
22527 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22529 tree fntype, spec, noex, clone;
22531 /* Don't instantiate a noexcept-specification from template context. */
22532 if (processing_template_decl)
22533 return true;
22535 if (DECL_CLONED_FUNCTION_P (fn))
22536 fn = DECL_CLONED_FUNCTION (fn);
22537 fntype = TREE_TYPE (fn);
22538 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22540 if (!spec || !TREE_PURPOSE (spec))
22541 return true;
22543 noex = TREE_PURPOSE (spec);
22545 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22547 static hash_set<tree>* fns = new hash_set<tree>;
22548 bool added = false;
22549 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22550 spec = get_defaulted_eh_spec (fn, complain);
22551 else if (!(added = !fns->add (fn)))
22553 /* If hash_set::add returns true, the element was already there. */
22554 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22555 DECL_SOURCE_LOCATION (fn));
22556 error_at (loc,
22557 "exception specification of %qD depends on itself",
22558 fn);
22559 spec = noexcept_false_spec;
22561 else if (push_tinst_level (fn))
22563 push_access_scope (fn);
22564 push_deferring_access_checks (dk_no_deferred);
22565 input_location = DECL_SOURCE_LOCATION (fn);
22566 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22567 DEFERRED_NOEXCEPT_ARGS (noex),
22568 tf_warning_or_error, fn,
22569 /*function_p=*/false,
22570 /*integral_constant_expression_p=*/true);
22571 pop_deferring_access_checks ();
22572 pop_access_scope (fn);
22573 pop_tinst_level ();
22574 spec = build_noexcept_spec (noex, tf_warning_or_error);
22575 if (spec == error_mark_node)
22576 spec = noexcept_false_spec;
22578 else
22579 spec = noexcept_false_spec;
22581 if (added)
22582 fns->remove (fn);
22584 if (spec == error_mark_node)
22585 return false;
22587 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22590 FOR_EACH_CLONE (clone, fn)
22592 if (TREE_TYPE (clone) == fntype)
22593 TREE_TYPE (clone) = TREE_TYPE (fn);
22594 else
22595 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22598 return true;
22601 /* Produce the definition of D, a _DECL generated from a template. If
22602 DEFER_OK is true, then we don't have to actually do the
22603 instantiation now; we just have to do it sometime. Normally it is
22604 an error if this is an explicit instantiation but D is undefined.
22605 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22606 instantiated class template. */
22608 tree
22609 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22611 tree tmpl = DECL_TI_TEMPLATE (d);
22612 tree gen_args;
22613 tree args;
22614 tree td;
22615 tree code_pattern;
22616 tree spec;
22617 tree gen_tmpl;
22618 bool pattern_defined;
22619 location_t saved_loc = input_location;
22620 int saved_unevaluated_operand = cp_unevaluated_operand;
22621 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22622 bool external_p;
22623 bool deleted_p;
22625 /* This function should only be used to instantiate templates for
22626 functions and static member variables. */
22627 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22629 /* A concept is never instantiated. */
22630 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22632 /* Variables are never deferred; if instantiation is required, they
22633 are instantiated right away. That allows for better code in the
22634 case that an expression refers to the value of the variable --
22635 if the variable has a constant value the referring expression can
22636 take advantage of that fact. */
22637 if (VAR_P (d))
22638 defer_ok = false;
22640 /* Don't instantiate cloned functions. Instead, instantiate the
22641 functions they cloned. */
22642 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22643 d = DECL_CLONED_FUNCTION (d);
22645 if (DECL_TEMPLATE_INSTANTIATED (d)
22646 || (TREE_CODE (d) == FUNCTION_DECL
22647 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22648 || DECL_TEMPLATE_SPECIALIZATION (d))
22649 /* D has already been instantiated or explicitly specialized, so
22650 there's nothing for us to do here.
22652 It might seem reasonable to check whether or not D is an explicit
22653 instantiation, and, if so, stop here. But when an explicit
22654 instantiation is deferred until the end of the compilation,
22655 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22656 the instantiation. */
22657 return d;
22659 /* Check to see whether we know that this template will be
22660 instantiated in some other file, as with "extern template"
22661 extension. */
22662 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22664 /* In general, we do not instantiate such templates. */
22665 if (external_p && !always_instantiate_p (d))
22666 return d;
22668 gen_tmpl = most_general_template (tmpl);
22669 gen_args = DECL_TI_ARGS (d);
22671 if (tmpl != gen_tmpl)
22672 /* We should already have the extra args. */
22673 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22674 == TMPL_ARGS_DEPTH (gen_args));
22675 /* And what's in the hash table should match D. */
22676 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22677 || spec == NULL_TREE);
22679 /* This needs to happen before any tsubsting. */
22680 if (! push_tinst_level (d))
22681 return d;
22683 timevar_push (TV_TEMPLATE_INST);
22685 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22686 for the instantiation. */
22687 td = template_for_substitution (d);
22688 args = gen_args;
22690 if (VAR_P (d))
22692 /* Look up an explicit specialization, if any. */
22693 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22694 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22695 if (elt && elt != error_mark_node)
22697 td = TREE_VALUE (elt);
22698 args = TREE_PURPOSE (elt);
22702 code_pattern = DECL_TEMPLATE_RESULT (td);
22704 /* We should never be trying to instantiate a member of a class
22705 template or partial specialization. */
22706 gcc_assert (d != code_pattern);
22708 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22709 || DECL_TEMPLATE_SPECIALIZATION (td))
22710 /* In the case of a friend template whose definition is provided
22711 outside the class, we may have too many arguments. Drop the
22712 ones we don't need. The same is true for specializations. */
22713 args = get_innermost_template_args
22714 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22716 if (TREE_CODE (d) == FUNCTION_DECL)
22718 deleted_p = DECL_DELETED_FN (code_pattern);
22719 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22720 && DECL_INITIAL (code_pattern) != error_mark_node)
22721 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22722 || deleted_p);
22724 else
22726 deleted_p = false;
22727 if (DECL_CLASS_SCOPE_P (code_pattern))
22728 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22729 || DECL_INLINE_VAR_P (code_pattern));
22730 else
22731 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22734 /* We may be in the middle of deferred access check. Disable it now. */
22735 push_deferring_access_checks (dk_no_deferred);
22737 /* Unless an explicit instantiation directive has already determined
22738 the linkage of D, remember that a definition is available for
22739 this entity. */
22740 if (pattern_defined
22741 && !DECL_INTERFACE_KNOWN (d)
22742 && !DECL_NOT_REALLY_EXTERN (d))
22743 mark_definable (d);
22745 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22746 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22747 input_location = DECL_SOURCE_LOCATION (d);
22749 /* If D is a member of an explicitly instantiated class template,
22750 and no definition is available, treat it like an implicit
22751 instantiation. */
22752 if (!pattern_defined && expl_inst_class_mem_p
22753 && DECL_EXPLICIT_INSTANTIATION (d))
22755 /* Leave linkage flags alone on instantiations with anonymous
22756 visibility. */
22757 if (TREE_PUBLIC (d))
22759 DECL_NOT_REALLY_EXTERN (d) = 0;
22760 DECL_INTERFACE_KNOWN (d) = 0;
22762 SET_DECL_IMPLICIT_INSTANTIATION (d);
22765 /* Defer all other templates, unless we have been explicitly
22766 forbidden from doing so. */
22767 if (/* If there is no definition, we cannot instantiate the
22768 template. */
22769 ! pattern_defined
22770 /* If it's OK to postpone instantiation, do so. */
22771 || defer_ok
22772 /* If this is a static data member that will be defined
22773 elsewhere, we don't want to instantiate the entire data
22774 member, but we do want to instantiate the initializer so that
22775 we can substitute that elsewhere. */
22776 || (external_p && VAR_P (d))
22777 /* Handle here a deleted function too, avoid generating
22778 its body (c++/61080). */
22779 || deleted_p)
22781 /* The definition of the static data member is now required so
22782 we must substitute the initializer. */
22783 if (VAR_P (d)
22784 && !DECL_INITIAL (d)
22785 && DECL_INITIAL (code_pattern))
22787 tree ns;
22788 tree init;
22789 bool const_init = false;
22790 bool enter_context = DECL_CLASS_SCOPE_P (d);
22792 ns = decl_namespace_context (d);
22793 push_nested_namespace (ns);
22794 if (enter_context)
22795 push_nested_class (DECL_CONTEXT (d));
22796 init = tsubst_expr (DECL_INITIAL (code_pattern),
22797 args,
22798 tf_warning_or_error, NULL_TREE,
22799 /*integral_constant_expression_p=*/false);
22800 /* If instantiating the initializer involved instantiating this
22801 again, don't call cp_finish_decl twice. */
22802 if (!DECL_INITIAL (d))
22804 /* Make sure the initializer is still constant, in case of
22805 circular dependency (template/instantiate6.C). */
22806 const_init
22807 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22808 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22809 /*asmspec_tree=*/NULL_TREE,
22810 LOOKUP_ONLYCONVERTING);
22812 if (enter_context)
22813 pop_nested_class ();
22814 pop_nested_namespace (ns);
22817 /* We restore the source position here because it's used by
22818 add_pending_template. */
22819 input_location = saved_loc;
22821 if (at_eof && !pattern_defined
22822 && DECL_EXPLICIT_INSTANTIATION (d)
22823 && DECL_NOT_REALLY_EXTERN (d))
22824 /* [temp.explicit]
22826 The definition of a non-exported function template, a
22827 non-exported member function template, or a non-exported
22828 member function or static data member of a class template
22829 shall be present in every translation unit in which it is
22830 explicitly instantiated. */
22831 permerror (input_location, "explicit instantiation of %qD "
22832 "but no definition available", d);
22834 /* If we're in unevaluated context, we just wanted to get the
22835 constant value; this isn't an odr use, so don't queue
22836 a full instantiation. */
22837 if (cp_unevaluated_operand != 0)
22838 goto out;
22839 /* ??? Historically, we have instantiated inline functions, even
22840 when marked as "extern template". */
22841 if (!(external_p && VAR_P (d)))
22842 add_pending_template (d);
22843 goto out;
22845 /* Tell the repository that D is available in this translation unit
22846 -- and see if it is supposed to be instantiated here. */
22847 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22849 /* In a PCH file, despite the fact that the repository hasn't
22850 requested instantiation in the PCH it is still possible that
22851 an instantiation will be required in a file that includes the
22852 PCH. */
22853 if (pch_file)
22854 add_pending_template (d);
22855 /* Instantiate inline functions so that the inliner can do its
22856 job, even though we'll not be emitting a copy of this
22857 function. */
22858 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22859 goto out;
22862 bool push_to_top, nested;
22863 tree fn_context;
22864 fn_context = decl_function_context (d);
22865 nested = current_function_decl != NULL_TREE;
22866 push_to_top = !(nested && fn_context == current_function_decl);
22868 vec<tree> omp_privatization_save;
22869 if (nested)
22870 save_omp_privatization_clauses (omp_privatization_save);
22872 if (push_to_top)
22873 push_to_top_level ();
22874 else
22876 push_function_context ();
22877 cp_unevaluated_operand = 0;
22878 c_inhibit_evaluation_warnings = 0;
22881 /* Mark D as instantiated so that recursive calls to
22882 instantiate_decl do not try to instantiate it again. */
22883 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22885 /* Regenerate the declaration in case the template has been modified
22886 by a subsequent redeclaration. */
22887 regenerate_decl_from_template (d, td, args);
22889 /* We already set the file and line above. Reset them now in case
22890 they changed as a result of calling regenerate_decl_from_template. */
22891 input_location = DECL_SOURCE_LOCATION (d);
22893 if (VAR_P (d))
22895 tree init;
22896 bool const_init = false;
22898 /* Clear out DECL_RTL; whatever was there before may not be right
22899 since we've reset the type of the declaration. */
22900 SET_DECL_RTL (d, NULL);
22901 DECL_IN_AGGR_P (d) = 0;
22903 /* The initializer is placed in DECL_INITIAL by
22904 regenerate_decl_from_template so we don't need to
22905 push/pop_access_scope again here. Pull it out so that
22906 cp_finish_decl can process it. */
22907 init = DECL_INITIAL (d);
22908 DECL_INITIAL (d) = NULL_TREE;
22909 DECL_INITIALIZED_P (d) = 0;
22911 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22912 initializer. That function will defer actual emission until
22913 we have a chance to determine linkage. */
22914 DECL_EXTERNAL (d) = 0;
22916 /* Enter the scope of D so that access-checking works correctly. */
22917 bool enter_context = DECL_CLASS_SCOPE_P (d);
22918 if (enter_context)
22919 push_nested_class (DECL_CONTEXT (d));
22921 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22922 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22924 if (enter_context)
22925 pop_nested_class ();
22927 if (variable_template_p (gen_tmpl))
22928 note_variable_template_instantiation (d);
22930 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22931 synthesize_method (d);
22932 else if (TREE_CODE (d) == FUNCTION_DECL)
22934 hash_map<tree, tree> *saved_local_specializations;
22935 tree tmpl_parm;
22936 tree spec_parm;
22937 tree block = NULL_TREE;
22938 tree lambda_ctx = NULL_TREE;
22940 /* Save away the current list, in case we are instantiating one
22941 template from within the body of another. */
22942 saved_local_specializations = local_specializations;
22944 /* Set up the list of local specializations. */
22945 local_specializations = new hash_map<tree, tree>;
22947 /* Set up context. */
22948 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22949 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22950 block = push_stmt_list ();
22951 else
22953 if (push_to_top && LAMBDA_FUNCTION_P (d))
22955 /* When instantiating a lambda's templated function
22956 operator, we need to push the non-lambda class scope
22957 of the lambda itself so that the nested function
22958 stack is sufficiently correct to deal with this
22959 capture. */
22960 lambda_ctx = DECL_CONTEXT (d);
22962 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22963 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22964 if (lambda_ctx)
22965 push_nested_class (lambda_ctx);
22967 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22970 /* Some typedefs referenced from within the template code need to be
22971 access checked at template instantiation time, i.e now. These
22972 types were added to the template at parsing time. Let's get those
22973 and perform the access checks then. */
22974 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22975 args);
22977 /* Create substitution entries for the parameters. */
22978 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22979 spec_parm = DECL_ARGUMENTS (d);
22980 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22982 register_local_specialization (spec_parm, tmpl_parm);
22983 spec_parm = skip_artificial_parms_for (d, spec_parm);
22984 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22986 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22988 if (!DECL_PACK_P (tmpl_parm))
22990 register_local_specialization (spec_parm, tmpl_parm);
22991 spec_parm = DECL_CHAIN (spec_parm);
22993 else
22995 /* Register the (value) argument pack as a specialization of
22996 TMPL_PARM, then move on. */
22997 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22998 register_local_specialization (argpack, tmpl_parm);
23001 gcc_assert (!spec_parm);
23003 /* Substitute into the body of the function. */
23004 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23005 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23006 tf_warning_or_error, tmpl);
23007 else
23009 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23010 tf_warning_or_error, tmpl,
23011 /*integral_constant_expression_p=*/false);
23013 /* Set the current input_location to the end of the function
23014 so that finish_function knows where we are. */
23015 input_location
23016 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23018 /* Remember if we saw an infinite loop in the template. */
23019 current_function_infinite_loop
23020 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23023 /* We don't need the local specializations any more. */
23024 delete local_specializations;
23025 local_specializations = saved_local_specializations;
23027 /* Finish the function. */
23028 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23029 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23030 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23031 else
23033 d = finish_function (0);
23034 expand_or_defer_fn (d);
23036 if (lambda_ctx)
23037 pop_nested_class ();
23039 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23040 cp_check_omp_declare_reduction (d);
23043 /* We're not deferring instantiation any more. */
23044 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23046 if (push_to_top)
23047 pop_from_top_level ();
23048 else
23049 pop_function_context ();
23051 if (nested)
23052 restore_omp_privatization_clauses (omp_privatization_save);
23054 out:
23055 pop_deferring_access_checks ();
23056 timevar_pop (TV_TEMPLATE_INST);
23057 pop_tinst_level ();
23058 input_location = saved_loc;
23059 cp_unevaluated_operand = saved_unevaluated_operand;
23060 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23062 return d;
23065 /* Run through the list of templates that we wish we could
23066 instantiate, and instantiate any we can. RETRIES is the
23067 number of times we retry pending template instantiation. */
23069 void
23070 instantiate_pending_templates (int retries)
23072 int reconsider;
23073 location_t saved_loc = input_location;
23075 /* Instantiating templates may trigger vtable generation. This in turn
23076 may require further template instantiations. We place a limit here
23077 to avoid infinite loop. */
23078 if (pending_templates && retries >= max_tinst_depth)
23080 tree decl = pending_templates->tinst->decl;
23082 fatal_error (input_location,
23083 "template instantiation depth exceeds maximum of %d"
23084 " instantiating %q+D, possibly from virtual table generation"
23085 " (use -ftemplate-depth= to increase the maximum)",
23086 max_tinst_depth, decl);
23087 if (TREE_CODE (decl) == FUNCTION_DECL)
23088 /* Pretend that we defined it. */
23089 DECL_INITIAL (decl) = error_mark_node;
23090 return;
23095 struct pending_template **t = &pending_templates;
23096 struct pending_template *last = NULL;
23097 reconsider = 0;
23098 while (*t)
23100 tree instantiation = reopen_tinst_level ((*t)->tinst);
23101 bool complete = false;
23103 if (TYPE_P (instantiation))
23105 if (!COMPLETE_TYPE_P (instantiation))
23107 instantiate_class_template (instantiation);
23108 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23109 for (tree fld = TYPE_FIELDS (instantiation);
23110 fld; fld = TREE_CHAIN (fld))
23111 if ((VAR_P (fld)
23112 || (TREE_CODE (fld) == FUNCTION_DECL
23113 && !DECL_ARTIFICIAL (fld)))
23114 && DECL_TEMPLATE_INSTANTIATION (fld))
23115 instantiate_decl (fld,
23116 /*defer_ok=*/false,
23117 /*expl_inst_class_mem_p=*/false);
23119 if (COMPLETE_TYPE_P (instantiation))
23120 reconsider = 1;
23123 complete = COMPLETE_TYPE_P (instantiation);
23125 else
23127 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23128 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23130 instantiation
23131 = instantiate_decl (instantiation,
23132 /*defer_ok=*/false,
23133 /*expl_inst_class_mem_p=*/false);
23134 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23135 reconsider = 1;
23138 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23139 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23142 if (complete)
23143 /* If INSTANTIATION has been instantiated, then we don't
23144 need to consider it again in the future. */
23145 *t = (*t)->next;
23146 else
23148 last = *t;
23149 t = &(*t)->next;
23151 tinst_depth = 0;
23152 current_tinst_level = NULL;
23154 last_pending_template = last;
23156 while (reconsider);
23158 input_location = saved_loc;
23161 /* Substitute ARGVEC into T, which is a list of initializers for
23162 either base class or a non-static data member. The TREE_PURPOSEs
23163 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23164 instantiate_decl. */
23166 static tree
23167 tsubst_initializer_list (tree t, tree argvec)
23169 tree inits = NULL_TREE;
23171 for (; t; t = TREE_CHAIN (t))
23173 tree decl;
23174 tree init;
23175 tree expanded_bases = NULL_TREE;
23176 tree expanded_arguments = NULL_TREE;
23177 int i, len = 1;
23179 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23181 tree expr;
23182 tree arg;
23184 /* Expand the base class expansion type into separate base
23185 classes. */
23186 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23187 tf_warning_or_error,
23188 NULL_TREE);
23189 if (expanded_bases == error_mark_node)
23190 continue;
23192 /* We'll be building separate TREE_LISTs of arguments for
23193 each base. */
23194 len = TREE_VEC_LENGTH (expanded_bases);
23195 expanded_arguments = make_tree_vec (len);
23196 for (i = 0; i < len; i++)
23197 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23199 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23200 expand each argument in the TREE_VALUE of t. */
23201 expr = make_node (EXPR_PACK_EXPANSION);
23202 PACK_EXPANSION_LOCAL_P (expr) = true;
23203 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23204 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23206 if (TREE_VALUE (t) == void_type_node)
23207 /* VOID_TYPE_NODE is used to indicate
23208 value-initialization. */
23210 for (i = 0; i < len; i++)
23211 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23213 else
23215 /* Substitute parameter packs into each argument in the
23216 TREE_LIST. */
23217 in_base_initializer = 1;
23218 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23220 tree expanded_exprs;
23222 /* Expand the argument. */
23223 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23224 expanded_exprs
23225 = tsubst_pack_expansion (expr, argvec,
23226 tf_warning_or_error,
23227 NULL_TREE);
23228 if (expanded_exprs == error_mark_node)
23229 continue;
23231 /* Prepend each of the expanded expressions to the
23232 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23233 for (i = 0; i < len; i++)
23235 TREE_VEC_ELT (expanded_arguments, i) =
23236 tree_cons (NULL_TREE,
23237 TREE_VEC_ELT (expanded_exprs, i),
23238 TREE_VEC_ELT (expanded_arguments, i));
23241 in_base_initializer = 0;
23243 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23244 since we built them backwards. */
23245 for (i = 0; i < len; i++)
23247 TREE_VEC_ELT (expanded_arguments, i) =
23248 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23253 for (i = 0; i < len; ++i)
23255 if (expanded_bases)
23257 decl = TREE_VEC_ELT (expanded_bases, i);
23258 decl = expand_member_init (decl);
23259 init = TREE_VEC_ELT (expanded_arguments, i);
23261 else
23263 tree tmp;
23264 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23265 tf_warning_or_error, NULL_TREE);
23267 decl = expand_member_init (decl);
23268 if (decl && !DECL_P (decl))
23269 in_base_initializer = 1;
23271 init = TREE_VALUE (t);
23272 tmp = init;
23273 if (init != void_type_node)
23274 init = tsubst_expr (init, argvec,
23275 tf_warning_or_error, NULL_TREE,
23276 /*integral_constant_expression_p=*/false);
23277 if (init == NULL_TREE && tmp != NULL_TREE)
23278 /* If we had an initializer but it instantiated to nothing,
23279 value-initialize the object. This will only occur when
23280 the initializer was a pack expansion where the parameter
23281 packs used in that expansion were of length zero. */
23282 init = void_type_node;
23283 in_base_initializer = 0;
23286 if (decl)
23288 init = build_tree_list (decl, init);
23289 TREE_CHAIN (init) = inits;
23290 inits = init;
23294 return inits;
23297 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23299 static void
23300 set_current_access_from_decl (tree decl)
23302 if (TREE_PRIVATE (decl))
23303 current_access_specifier = access_private_node;
23304 else if (TREE_PROTECTED (decl))
23305 current_access_specifier = access_protected_node;
23306 else
23307 current_access_specifier = access_public_node;
23310 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23311 is the instantiation (which should have been created with
23312 start_enum) and ARGS are the template arguments to use. */
23314 static void
23315 tsubst_enum (tree tag, tree newtag, tree args)
23317 tree e;
23319 if (SCOPED_ENUM_P (newtag))
23320 begin_scope (sk_scoped_enum, newtag);
23322 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23324 tree value;
23325 tree decl;
23327 decl = TREE_VALUE (e);
23328 /* Note that in a template enum, the TREE_VALUE is the
23329 CONST_DECL, not the corresponding INTEGER_CST. */
23330 value = tsubst_expr (DECL_INITIAL (decl),
23331 args, tf_warning_or_error, NULL_TREE,
23332 /*integral_constant_expression_p=*/true);
23334 /* Give this enumeration constant the correct access. */
23335 set_current_access_from_decl (decl);
23337 /* Actually build the enumerator itself. Here we're assuming that
23338 enumerators can't have dependent attributes. */
23339 build_enumerator (DECL_NAME (decl), value, newtag,
23340 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23343 if (SCOPED_ENUM_P (newtag))
23344 finish_scope ();
23346 finish_enum_value_list (newtag);
23347 finish_enum (newtag);
23349 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23350 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23353 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23354 its type -- but without substituting the innermost set of template
23355 arguments. So, innermost set of template parameters will appear in
23356 the type. */
23358 tree
23359 get_mostly_instantiated_function_type (tree decl)
23361 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23362 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23365 /* Return truthvalue if we're processing a template different from
23366 the last one involved in diagnostics. */
23367 bool
23368 problematic_instantiation_changed (void)
23370 return current_tinst_level != last_error_tinst_level;
23373 /* Remember current template involved in diagnostics. */
23374 void
23375 record_last_problematic_instantiation (void)
23377 last_error_tinst_level = current_tinst_level;
23380 struct tinst_level *
23381 current_instantiation (void)
23383 return current_tinst_level;
23386 /* Return TRUE if current_function_decl is being instantiated, false
23387 otherwise. */
23389 bool
23390 instantiating_current_function_p (void)
23392 return (current_instantiation ()
23393 && current_instantiation ()->decl == current_function_decl);
23396 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23397 type. Return zero for ok, nonzero for disallowed. Issue error and
23398 warning messages under control of COMPLAIN. */
23400 static int
23401 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23403 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23404 return 0;
23405 else if (POINTER_TYPE_P (type))
23406 return 0;
23407 else if (TYPE_PTRMEM_P (type))
23408 return 0;
23409 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23410 return 0;
23411 else if (TREE_CODE (type) == TYPENAME_TYPE)
23412 return 0;
23413 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23414 return 0;
23415 else if (TREE_CODE (type) == NULLPTR_TYPE)
23416 return 0;
23417 /* A bound template template parm could later be instantiated to have a valid
23418 nontype parm type via an alias template. */
23419 else if (cxx_dialect >= cxx11
23420 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23421 return 0;
23423 if (complain & tf_error)
23425 if (type == error_mark_node)
23426 inform (input_location, "invalid template non-type parameter");
23427 else
23428 error ("%q#T is not a valid type for a template non-type parameter",
23429 type);
23431 return 1;
23434 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23435 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23437 static bool
23438 dependent_type_p_r (tree type)
23440 tree scope;
23442 /* [temp.dep.type]
23444 A type is dependent if it is:
23446 -- a template parameter. Template template parameters are types
23447 for us (since TYPE_P holds true for them) so we handle
23448 them here. */
23449 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23450 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23451 return true;
23452 /* -- a qualified-id with a nested-name-specifier which contains a
23453 class-name that names a dependent type or whose unqualified-id
23454 names a dependent type. */
23455 if (TREE_CODE (type) == TYPENAME_TYPE)
23456 return true;
23458 /* An alias template specialization can be dependent even if the
23459 resulting type is not. */
23460 if (dependent_alias_template_spec_p (type))
23461 return true;
23463 /* -- a cv-qualified type where the cv-unqualified type is
23464 dependent.
23465 No code is necessary for this bullet; the code below handles
23466 cv-qualified types, and we don't want to strip aliases with
23467 TYPE_MAIN_VARIANT because of DR 1558. */
23468 /* -- a compound type constructed from any dependent type. */
23469 if (TYPE_PTRMEM_P (type))
23470 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23471 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23472 (type)));
23473 else if (TYPE_PTR_P (type)
23474 || TREE_CODE (type) == REFERENCE_TYPE)
23475 return dependent_type_p (TREE_TYPE (type));
23476 else if (TREE_CODE (type) == FUNCTION_TYPE
23477 || TREE_CODE (type) == METHOD_TYPE)
23479 tree arg_type;
23481 if (dependent_type_p (TREE_TYPE (type)))
23482 return true;
23483 for (arg_type = TYPE_ARG_TYPES (type);
23484 arg_type;
23485 arg_type = TREE_CHAIN (arg_type))
23486 if (dependent_type_p (TREE_VALUE (arg_type)))
23487 return true;
23488 if (cxx_dialect >= cxx1z)
23490 /* A value-dependent noexcept-specifier makes the type dependent. */
23491 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23492 if (spec && TREE_PURPOSE (spec)
23493 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23494 return true;
23496 return false;
23498 /* -- an array type constructed from any dependent type or whose
23499 size is specified by a constant expression that is
23500 value-dependent.
23502 We checked for type- and value-dependence of the bounds in
23503 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23504 if (TREE_CODE (type) == ARRAY_TYPE)
23506 if (TYPE_DOMAIN (type)
23507 && dependent_type_p (TYPE_DOMAIN (type)))
23508 return true;
23509 return dependent_type_p (TREE_TYPE (type));
23512 /* -- a template-id in which either the template name is a template
23513 parameter ... */
23514 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23515 return true;
23516 /* ... or any of the template arguments is a dependent type or
23517 an expression that is type-dependent or value-dependent. */
23518 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23519 && (any_dependent_template_arguments_p
23520 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23521 return true;
23523 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23524 dependent; if the argument of the `typeof' expression is not
23525 type-dependent, then it should already been have resolved. */
23526 if (TREE_CODE (type) == TYPEOF_TYPE
23527 || TREE_CODE (type) == DECLTYPE_TYPE
23528 || TREE_CODE (type) == UNDERLYING_TYPE)
23529 return true;
23531 /* A template argument pack is dependent if any of its packed
23532 arguments are. */
23533 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23535 tree args = ARGUMENT_PACK_ARGS (type);
23536 int i, len = TREE_VEC_LENGTH (args);
23537 for (i = 0; i < len; ++i)
23538 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23539 return true;
23542 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23543 be template parameters. */
23544 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23545 return true;
23547 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23548 return true;
23550 /* The standard does not specifically mention types that are local
23551 to template functions or local classes, but they should be
23552 considered dependent too. For example:
23554 template <int I> void f() {
23555 enum E { a = I };
23556 S<sizeof (E)> s;
23559 The size of `E' cannot be known until the value of `I' has been
23560 determined. Therefore, `E' must be considered dependent. */
23561 scope = TYPE_CONTEXT (type);
23562 if (scope && TYPE_P (scope))
23563 return dependent_type_p (scope);
23564 /* Don't use type_dependent_expression_p here, as it can lead
23565 to infinite recursion trying to determine whether a lambda
23566 nested in a lambda is dependent (c++/47687). */
23567 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23568 && DECL_LANG_SPECIFIC (scope)
23569 && DECL_TEMPLATE_INFO (scope)
23570 && (any_dependent_template_arguments_p
23571 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23572 return true;
23574 /* Other types are non-dependent. */
23575 return false;
23578 /* Returns TRUE if TYPE is dependent, in the sense of
23579 [temp.dep.type]. Note that a NULL type is considered dependent. */
23581 bool
23582 dependent_type_p (tree type)
23584 /* If there are no template parameters in scope, then there can't be
23585 any dependent types. */
23586 if (!processing_template_decl)
23588 /* If we are not processing a template, then nobody should be
23589 providing us with a dependent type. */
23590 gcc_assert (type);
23591 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23592 return false;
23595 /* If the type is NULL, we have not computed a type for the entity
23596 in question; in that case, the type is dependent. */
23597 if (!type)
23598 return true;
23600 /* Erroneous types can be considered non-dependent. */
23601 if (type == error_mark_node)
23602 return false;
23604 /* Getting here with global_type_node means we improperly called this
23605 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23606 gcc_checking_assert (type != global_type_node);
23608 /* If we have not already computed the appropriate value for TYPE,
23609 do so now. */
23610 if (!TYPE_DEPENDENT_P_VALID (type))
23612 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23613 TYPE_DEPENDENT_P_VALID (type) = 1;
23616 return TYPE_DEPENDENT_P (type);
23619 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23620 lookup. In other words, a dependent type that is not the current
23621 instantiation. */
23623 bool
23624 dependent_scope_p (tree scope)
23626 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23627 && !currently_open_class (scope));
23630 /* T is a SCOPE_REF; return whether we need to consider it
23631 instantiation-dependent so that we can check access at instantiation
23632 time even though we know which member it resolves to. */
23634 static bool
23635 instantiation_dependent_scope_ref_p (tree t)
23637 if (DECL_P (TREE_OPERAND (t, 1))
23638 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23639 && accessible_in_template_p (TREE_OPERAND (t, 0),
23640 TREE_OPERAND (t, 1)))
23641 return false;
23642 else
23643 return true;
23646 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23647 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23648 expression. */
23650 /* Note that this predicate is not appropriate for general expressions;
23651 only constant expressions (that satisfy potential_constant_expression)
23652 can be tested for value dependence. */
23654 bool
23655 value_dependent_expression_p (tree expression)
23657 if (!processing_template_decl || expression == NULL_TREE)
23658 return false;
23660 /* A name declared with a dependent type. */
23661 if (DECL_P (expression) && type_dependent_expression_p (expression))
23662 return true;
23664 switch (TREE_CODE (expression))
23666 case BASELINK:
23667 /* A dependent member function of the current instantiation. */
23668 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23670 case FUNCTION_DECL:
23671 /* A dependent member function of the current instantiation. */
23672 if (DECL_CLASS_SCOPE_P (expression)
23673 && dependent_type_p (DECL_CONTEXT (expression)))
23674 return true;
23675 break;
23677 case IDENTIFIER_NODE:
23678 /* A name that has not been looked up -- must be dependent. */
23679 return true;
23681 case TEMPLATE_PARM_INDEX:
23682 /* A non-type template parm. */
23683 return true;
23685 case CONST_DECL:
23686 /* A non-type template parm. */
23687 if (DECL_TEMPLATE_PARM_P (expression))
23688 return true;
23689 return value_dependent_expression_p (DECL_INITIAL (expression));
23691 case VAR_DECL:
23692 /* A constant with literal type and is initialized
23693 with an expression that is value-dependent.
23695 Note that a non-dependent parenthesized initializer will have
23696 already been replaced with its constant value, so if we see
23697 a TREE_LIST it must be dependent. */
23698 if (DECL_INITIAL (expression)
23699 && decl_constant_var_p (expression)
23700 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23701 /* cp_finish_decl doesn't fold reference initializers. */
23702 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23703 || type_dependent_expression_p (DECL_INITIAL (expression))
23704 || value_dependent_expression_p (DECL_INITIAL (expression))))
23705 return true;
23706 if (DECL_HAS_VALUE_EXPR_P (expression))
23708 tree value_expr = DECL_VALUE_EXPR (expression);
23709 if (type_dependent_expression_p (value_expr))
23710 return true;
23712 return false;
23714 case DYNAMIC_CAST_EXPR:
23715 case STATIC_CAST_EXPR:
23716 case CONST_CAST_EXPR:
23717 case REINTERPRET_CAST_EXPR:
23718 case CAST_EXPR:
23719 /* These expressions are value-dependent if the type to which
23720 the cast occurs is dependent or the expression being casted
23721 is value-dependent. */
23723 tree type = TREE_TYPE (expression);
23725 if (dependent_type_p (type))
23726 return true;
23728 /* A functional cast has a list of operands. */
23729 expression = TREE_OPERAND (expression, 0);
23730 if (!expression)
23732 /* If there are no operands, it must be an expression such
23733 as "int()". This should not happen for aggregate types
23734 because it would form non-constant expressions. */
23735 gcc_assert (cxx_dialect >= cxx11
23736 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23738 return false;
23741 if (TREE_CODE (expression) == TREE_LIST)
23742 return any_value_dependent_elements_p (expression);
23744 return value_dependent_expression_p (expression);
23747 case SIZEOF_EXPR:
23748 if (SIZEOF_EXPR_TYPE_P (expression))
23749 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23750 /* FALLTHRU */
23751 case ALIGNOF_EXPR:
23752 case TYPEID_EXPR:
23753 /* A `sizeof' expression is value-dependent if the operand is
23754 type-dependent or is a pack expansion. */
23755 expression = TREE_OPERAND (expression, 0);
23756 if (PACK_EXPANSION_P (expression))
23757 return true;
23758 else if (TYPE_P (expression))
23759 return dependent_type_p (expression);
23760 return instantiation_dependent_uneval_expression_p (expression);
23762 case AT_ENCODE_EXPR:
23763 /* An 'encode' expression is value-dependent if the operand is
23764 type-dependent. */
23765 expression = TREE_OPERAND (expression, 0);
23766 return dependent_type_p (expression);
23768 case NOEXCEPT_EXPR:
23769 expression = TREE_OPERAND (expression, 0);
23770 return instantiation_dependent_uneval_expression_p (expression);
23772 case SCOPE_REF:
23773 /* All instantiation-dependent expressions should also be considered
23774 value-dependent. */
23775 return instantiation_dependent_scope_ref_p (expression);
23777 case COMPONENT_REF:
23778 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23779 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23781 case NONTYPE_ARGUMENT_PACK:
23782 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23783 is value-dependent. */
23785 tree values = ARGUMENT_PACK_ARGS (expression);
23786 int i, len = TREE_VEC_LENGTH (values);
23788 for (i = 0; i < len; ++i)
23789 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23790 return true;
23792 return false;
23795 case TRAIT_EXPR:
23797 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23798 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23799 || (type2 ? dependent_type_p (type2) : false));
23802 case MODOP_EXPR:
23803 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23804 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23806 case ARRAY_REF:
23807 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23808 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23810 case ADDR_EXPR:
23812 tree op = TREE_OPERAND (expression, 0);
23813 return (value_dependent_expression_p (op)
23814 || has_value_dependent_address (op));
23817 case REQUIRES_EXPR:
23818 /* Treat all requires-expressions as value-dependent so
23819 we don't try to fold them. */
23820 return true;
23822 case TYPE_REQ:
23823 return dependent_type_p (TREE_OPERAND (expression, 0));
23825 case CALL_EXPR:
23827 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23828 return true;
23829 tree fn = get_callee_fndecl (expression);
23830 int i, nargs;
23831 nargs = call_expr_nargs (expression);
23832 for (i = 0; i < nargs; ++i)
23834 tree op = CALL_EXPR_ARG (expression, i);
23835 /* In a call to a constexpr member function, look through the
23836 implicit ADDR_EXPR on the object argument so that it doesn't
23837 cause the call to be considered value-dependent. We also
23838 look through it in potential_constant_expression. */
23839 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23840 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23841 && TREE_CODE (op) == ADDR_EXPR)
23842 op = TREE_OPERAND (op, 0);
23843 if (value_dependent_expression_p (op))
23844 return true;
23846 return false;
23849 case TEMPLATE_ID_EXPR:
23850 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23851 type-dependent. */
23852 return type_dependent_expression_p (expression)
23853 || variable_concept_p (TREE_OPERAND (expression, 0));
23855 case CONSTRUCTOR:
23857 unsigned ix;
23858 tree val;
23859 if (dependent_type_p (TREE_TYPE (expression)))
23860 return true;
23861 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23862 if (value_dependent_expression_p (val))
23863 return true;
23864 return false;
23867 case STMT_EXPR:
23868 /* Treat a GNU statement expression as dependent to avoid crashing
23869 under instantiate_non_dependent_expr; it can't be constant. */
23870 return true;
23872 default:
23873 /* A constant expression is value-dependent if any subexpression is
23874 value-dependent. */
23875 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23877 case tcc_reference:
23878 case tcc_unary:
23879 case tcc_comparison:
23880 case tcc_binary:
23881 case tcc_expression:
23882 case tcc_vl_exp:
23884 int i, len = cp_tree_operand_length (expression);
23886 for (i = 0; i < len; i++)
23888 tree t = TREE_OPERAND (expression, i);
23890 /* In some cases, some of the operands may be missing.
23891 (For example, in the case of PREDECREMENT_EXPR, the
23892 amount to increment by may be missing.) That doesn't
23893 make the expression dependent. */
23894 if (t && value_dependent_expression_p (t))
23895 return true;
23898 break;
23899 default:
23900 break;
23902 break;
23905 /* The expression is not value-dependent. */
23906 return false;
23909 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23910 [temp.dep.expr]. Note that an expression with no type is
23911 considered dependent. Other parts of the compiler arrange for an
23912 expression with type-dependent subexpressions to have no type, so
23913 this function doesn't have to be fully recursive. */
23915 bool
23916 type_dependent_expression_p (tree expression)
23918 if (!processing_template_decl)
23919 return false;
23921 if (expression == NULL_TREE || expression == error_mark_node)
23922 return false;
23924 /* An unresolved name is always dependent. */
23925 if (identifier_p (expression)
23926 || TREE_CODE (expression) == USING_DECL
23927 || TREE_CODE (expression) == WILDCARD_DECL)
23928 return true;
23930 /* A fold expression is type-dependent. */
23931 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23932 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23933 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23934 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23935 return true;
23937 /* Some expression forms are never type-dependent. */
23938 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23939 || TREE_CODE (expression) == SIZEOF_EXPR
23940 || TREE_CODE (expression) == ALIGNOF_EXPR
23941 || TREE_CODE (expression) == AT_ENCODE_EXPR
23942 || TREE_CODE (expression) == NOEXCEPT_EXPR
23943 || TREE_CODE (expression) == TRAIT_EXPR
23944 || TREE_CODE (expression) == TYPEID_EXPR
23945 || TREE_CODE (expression) == DELETE_EXPR
23946 || TREE_CODE (expression) == VEC_DELETE_EXPR
23947 || TREE_CODE (expression) == THROW_EXPR
23948 || TREE_CODE (expression) == REQUIRES_EXPR)
23949 return false;
23951 /* The types of these expressions depends only on the type to which
23952 the cast occurs. */
23953 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23954 || TREE_CODE (expression) == STATIC_CAST_EXPR
23955 || TREE_CODE (expression) == CONST_CAST_EXPR
23956 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23957 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23958 || TREE_CODE (expression) == CAST_EXPR)
23959 return dependent_type_p (TREE_TYPE (expression));
23961 /* The types of these expressions depends only on the type created
23962 by the expression. */
23963 if (TREE_CODE (expression) == NEW_EXPR
23964 || TREE_CODE (expression) == VEC_NEW_EXPR)
23966 /* For NEW_EXPR tree nodes created inside a template, either
23967 the object type itself or a TREE_LIST may appear as the
23968 operand 1. */
23969 tree type = TREE_OPERAND (expression, 1);
23970 if (TREE_CODE (type) == TREE_LIST)
23971 /* This is an array type. We need to check array dimensions
23972 as well. */
23973 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23974 || value_dependent_expression_p
23975 (TREE_OPERAND (TREE_VALUE (type), 1));
23976 else
23977 return dependent_type_p (type);
23980 if (TREE_CODE (expression) == SCOPE_REF)
23982 tree scope = TREE_OPERAND (expression, 0);
23983 tree name = TREE_OPERAND (expression, 1);
23985 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23986 contains an identifier associated by name lookup with one or more
23987 declarations declared with a dependent type, or...a
23988 nested-name-specifier or qualified-id that names a member of an
23989 unknown specialization. */
23990 return (type_dependent_expression_p (name)
23991 || dependent_scope_p (scope));
23994 if (TREE_CODE (expression) == TEMPLATE_DECL
23995 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23996 return uses_outer_template_parms (expression);
23998 if (TREE_CODE (expression) == STMT_EXPR)
23999 expression = stmt_expr_value_expr (expression);
24001 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24003 tree elt;
24004 unsigned i;
24006 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24008 if (type_dependent_expression_p (elt))
24009 return true;
24011 return false;
24014 /* A static data member of the current instantiation with incomplete
24015 array type is type-dependent, as the definition and specializations
24016 can have different bounds. */
24017 if (VAR_P (expression)
24018 && DECL_CLASS_SCOPE_P (expression)
24019 && dependent_type_p (DECL_CONTEXT (expression))
24020 && VAR_HAD_UNKNOWN_BOUND (expression))
24021 return true;
24023 /* An array of unknown bound depending on a variadic parameter, eg:
24025 template<typename... Args>
24026 void foo (Args... args)
24028 int arr[] = { args... };
24031 template<int... vals>
24032 void bar ()
24034 int arr[] = { vals... };
24037 If the array has no length and has an initializer, it must be that
24038 we couldn't determine its length in cp_complete_array_type because
24039 it is dependent. */
24040 if (VAR_P (expression)
24041 && TREE_TYPE (expression) != NULL_TREE
24042 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24043 && !TYPE_DOMAIN (TREE_TYPE (expression))
24044 && DECL_INITIAL (expression))
24045 return true;
24047 /* A function or variable template-id is type-dependent if it has any
24048 dependent template arguments. */
24049 if (VAR_OR_FUNCTION_DECL_P (expression)
24050 && DECL_LANG_SPECIFIC (expression)
24051 && DECL_TEMPLATE_INFO (expression))
24053 /* Consider the innermost template arguments, since those are the ones
24054 that come from the template-id; the template arguments for the
24055 enclosing class do not make it type-dependent unless they are used in
24056 the type of the decl. */
24057 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24058 && (any_dependent_template_arguments_p
24059 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24060 return true;
24062 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24063 type-dependent. Checking this is important for functions with auto
24064 return type, which looks like a dependent type. */
24065 if (TREE_CODE (expression) == FUNCTION_DECL
24066 && (!DECL_CLASS_SCOPE_P (expression)
24067 || !dependent_type_p (DECL_CONTEXT (expression)))
24068 && (!DECL_FRIEND_CONTEXT (expression)
24069 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24070 && !DECL_LOCAL_FUNCTION_P (expression))
24072 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24073 || undeduced_auto_decl (expression));
24074 return false;
24078 /* Always dependent, on the number of arguments if nothing else. */
24079 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24080 return true;
24082 if (TREE_TYPE (expression) == unknown_type_node)
24084 if (TREE_CODE (expression) == ADDR_EXPR)
24085 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24086 if (TREE_CODE (expression) == COMPONENT_REF
24087 || TREE_CODE (expression) == OFFSET_REF)
24089 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24090 return true;
24091 expression = TREE_OPERAND (expression, 1);
24092 if (identifier_p (expression))
24093 return false;
24095 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24096 if (TREE_CODE (expression) == SCOPE_REF)
24097 return false;
24099 if (BASELINK_P (expression))
24101 if (BASELINK_OPTYPE (expression)
24102 && dependent_type_p (BASELINK_OPTYPE (expression)))
24103 return true;
24104 expression = BASELINK_FUNCTIONS (expression);
24107 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24109 if (any_dependent_template_arguments_p
24110 (TREE_OPERAND (expression, 1)))
24111 return true;
24112 expression = TREE_OPERAND (expression, 0);
24113 if (identifier_p (expression))
24114 return true;
24117 gcc_assert (TREE_CODE (expression) == OVERLOAD
24118 || TREE_CODE (expression) == FUNCTION_DECL);
24120 for (lkp_iterator iter (expression); iter; ++iter)
24121 if (type_dependent_expression_p (*iter))
24122 return true;
24124 return false;
24127 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24129 /* Dependent type attributes might not have made it from the decl to
24130 the type yet. */
24131 if (DECL_P (expression)
24132 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24133 return true;
24135 return (dependent_type_p (TREE_TYPE (expression)));
24138 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24139 type-dependent if the expression refers to a member of the current
24140 instantiation and the type of the referenced member is dependent, or the
24141 class member access expression refers to a member of an unknown
24142 specialization.
24144 This function returns true if the OBJECT in such a class member access
24145 expression is of an unknown specialization. */
24147 bool
24148 type_dependent_object_expression_p (tree object)
24150 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24151 dependent. */
24152 if (TREE_CODE (object) == IDENTIFIER_NODE)
24153 return true;
24154 tree scope = TREE_TYPE (object);
24155 return (!scope || dependent_scope_p (scope));
24158 /* walk_tree callback function for instantiation_dependent_expression_p,
24159 below. Returns non-zero if a dependent subexpression is found. */
24161 static tree
24162 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24163 void * /*data*/)
24165 if (TYPE_P (*tp))
24167 /* We don't have to worry about decltype currently because decltype
24168 of an instantiation-dependent expr is a dependent type. This
24169 might change depending on the resolution of DR 1172. */
24170 *walk_subtrees = false;
24171 return NULL_TREE;
24173 enum tree_code code = TREE_CODE (*tp);
24174 switch (code)
24176 /* Don't treat an argument list as dependent just because it has no
24177 TREE_TYPE. */
24178 case TREE_LIST:
24179 case TREE_VEC:
24180 return NULL_TREE;
24182 case TEMPLATE_PARM_INDEX:
24183 return *tp;
24185 /* Handle expressions with type operands. */
24186 case SIZEOF_EXPR:
24187 case ALIGNOF_EXPR:
24188 case TYPEID_EXPR:
24189 case AT_ENCODE_EXPR:
24191 tree op = TREE_OPERAND (*tp, 0);
24192 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24193 op = TREE_TYPE (op);
24194 if (TYPE_P (op))
24196 if (dependent_type_p (op))
24197 return *tp;
24198 else
24200 *walk_subtrees = false;
24201 return NULL_TREE;
24204 break;
24207 case COMPONENT_REF:
24208 if (identifier_p (TREE_OPERAND (*tp, 1)))
24209 /* In a template, finish_class_member_access_expr creates a
24210 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24211 type-dependent, so that we can check access control at
24212 instantiation time (PR 42277). See also Core issue 1273. */
24213 return *tp;
24214 break;
24216 case SCOPE_REF:
24217 if (instantiation_dependent_scope_ref_p (*tp))
24218 return *tp;
24219 else
24220 break;
24222 /* Treat statement-expressions as dependent. */
24223 case BIND_EXPR:
24224 return *tp;
24226 /* Treat requires-expressions as dependent. */
24227 case REQUIRES_EXPR:
24228 return *tp;
24230 case CALL_EXPR:
24231 /* Treat calls to function concepts as dependent. */
24232 if (function_concept_check_p (*tp))
24233 return *tp;
24234 break;
24236 case TEMPLATE_ID_EXPR:
24237 /* And variable concepts. */
24238 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24239 return *tp;
24240 break;
24242 default:
24243 break;
24246 if (type_dependent_expression_p (*tp))
24247 return *tp;
24248 else
24249 return NULL_TREE;
24252 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24253 sense defined by the ABI:
24255 "An expression is instantiation-dependent if it is type-dependent
24256 or value-dependent, or it has a subexpression that is type-dependent
24257 or value-dependent."
24259 Except don't actually check value-dependence for unevaluated expressions,
24260 because in sizeof(i) we don't care about the value of i. Checking
24261 type-dependence will in turn check value-dependence of array bounds/template
24262 arguments as needed. */
24264 bool
24265 instantiation_dependent_uneval_expression_p (tree expression)
24267 tree result;
24269 if (!processing_template_decl)
24270 return false;
24272 if (expression == error_mark_node)
24273 return false;
24275 result = cp_walk_tree_without_duplicates (&expression,
24276 instantiation_dependent_r, NULL);
24277 return result != NULL_TREE;
24280 /* As above, but also check value-dependence of the expression as a whole. */
24282 bool
24283 instantiation_dependent_expression_p (tree expression)
24285 return (instantiation_dependent_uneval_expression_p (expression)
24286 || value_dependent_expression_p (expression));
24289 /* Like type_dependent_expression_p, but it also works while not processing
24290 a template definition, i.e. during substitution or mangling. */
24292 bool
24293 type_dependent_expression_p_push (tree expr)
24295 bool b;
24296 ++processing_template_decl;
24297 b = type_dependent_expression_p (expr);
24298 --processing_template_decl;
24299 return b;
24302 /* Returns TRUE if ARGS contains a type-dependent expression. */
24304 bool
24305 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24307 unsigned int i;
24308 tree arg;
24310 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24312 if (type_dependent_expression_p (arg))
24313 return true;
24315 return false;
24318 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24319 expressions) contains any type-dependent expressions. */
24321 bool
24322 any_type_dependent_elements_p (const_tree list)
24324 for (; list; list = TREE_CHAIN (list))
24325 if (type_dependent_expression_p (TREE_VALUE (list)))
24326 return true;
24328 return false;
24331 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24332 expressions) contains any value-dependent expressions. */
24334 bool
24335 any_value_dependent_elements_p (const_tree list)
24337 for (; list; list = TREE_CHAIN (list))
24338 if (value_dependent_expression_p (TREE_VALUE (list)))
24339 return true;
24341 return false;
24344 /* Returns TRUE if the ARG (a template argument) is dependent. */
24346 bool
24347 dependent_template_arg_p (tree arg)
24349 if (!processing_template_decl)
24350 return false;
24352 /* Assume a template argument that was wrongly written by the user
24353 is dependent. This is consistent with what
24354 any_dependent_template_arguments_p [that calls this function]
24355 does. */
24356 if (!arg || arg == error_mark_node)
24357 return true;
24359 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24360 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24362 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24363 return true;
24364 if (TREE_CODE (arg) == TEMPLATE_DECL)
24366 if (DECL_TEMPLATE_PARM_P (arg))
24367 return true;
24368 /* A member template of a dependent class is not necessarily
24369 type-dependent, but it is a dependent template argument because it
24370 will be a member of an unknown specialization to that template. */
24371 tree scope = CP_DECL_CONTEXT (arg);
24372 return TYPE_P (scope) && dependent_type_p (scope);
24374 else if (ARGUMENT_PACK_P (arg))
24376 tree args = ARGUMENT_PACK_ARGS (arg);
24377 int i, len = TREE_VEC_LENGTH (args);
24378 for (i = 0; i < len; ++i)
24380 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24381 return true;
24384 return false;
24386 else if (TYPE_P (arg))
24387 return dependent_type_p (arg);
24388 else
24389 return (type_dependent_expression_p (arg)
24390 || value_dependent_expression_p (arg));
24393 /* Returns true if ARGS (a collection of template arguments) contains
24394 any types that require structural equality testing. */
24396 bool
24397 any_template_arguments_need_structural_equality_p (tree args)
24399 int i;
24400 int j;
24402 if (!args)
24403 return false;
24404 if (args == error_mark_node)
24405 return true;
24407 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24409 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24410 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24412 tree arg = TREE_VEC_ELT (level, j);
24413 tree packed_args = NULL_TREE;
24414 int k, len = 1;
24416 if (ARGUMENT_PACK_P (arg))
24418 /* Look inside the argument pack. */
24419 packed_args = ARGUMENT_PACK_ARGS (arg);
24420 len = TREE_VEC_LENGTH (packed_args);
24423 for (k = 0; k < len; ++k)
24425 if (packed_args)
24426 arg = TREE_VEC_ELT (packed_args, k);
24428 if (error_operand_p (arg))
24429 return true;
24430 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24431 continue;
24432 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24433 return true;
24434 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24435 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24436 return true;
24441 return false;
24444 /* Returns true if ARGS (a collection of template arguments) contains
24445 any dependent arguments. */
24447 bool
24448 any_dependent_template_arguments_p (const_tree args)
24450 int i;
24451 int j;
24453 if (!args)
24454 return false;
24455 if (args == error_mark_node)
24456 return true;
24458 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24460 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24461 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24462 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24463 return true;
24466 return false;
24469 /* Returns TRUE if the template TMPL is type-dependent. */
24471 bool
24472 dependent_template_p (tree tmpl)
24474 if (TREE_CODE (tmpl) == OVERLOAD)
24476 for (lkp_iterator iter (tmpl); iter; ++iter)
24477 if (dependent_template_p (*iter))
24478 return true;
24479 return false;
24482 /* Template template parameters are dependent. */
24483 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24484 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24485 return true;
24486 /* So are names that have not been looked up. */
24487 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24488 return true;
24489 return false;
24492 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24494 bool
24495 dependent_template_id_p (tree tmpl, tree args)
24497 return (dependent_template_p (tmpl)
24498 || any_dependent_template_arguments_p (args));
24501 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24502 are dependent. */
24504 bool
24505 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24507 int i;
24509 if (!processing_template_decl)
24510 return false;
24512 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24514 tree decl = TREE_VEC_ELT (declv, i);
24515 tree init = TREE_VEC_ELT (initv, i);
24516 tree cond = TREE_VEC_ELT (condv, i);
24517 tree incr = TREE_VEC_ELT (incrv, i);
24519 if (type_dependent_expression_p (decl)
24520 || TREE_CODE (decl) == SCOPE_REF)
24521 return true;
24523 if (init && type_dependent_expression_p (init))
24524 return true;
24526 if (type_dependent_expression_p (cond))
24527 return true;
24529 if (COMPARISON_CLASS_P (cond)
24530 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24531 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24532 return true;
24534 if (TREE_CODE (incr) == MODOP_EXPR)
24536 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24537 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24538 return true;
24540 else if (type_dependent_expression_p (incr))
24541 return true;
24542 else if (TREE_CODE (incr) == MODIFY_EXPR)
24544 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24545 return true;
24546 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24548 tree t = TREE_OPERAND (incr, 1);
24549 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24550 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24551 return true;
24556 return false;
24559 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24560 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24561 no such TYPE can be found. Note that this function peers inside
24562 uninstantiated templates and therefore should be used only in
24563 extremely limited situations. ONLY_CURRENT_P restricts this
24564 peering to the currently open classes hierarchy (which is required
24565 when comparing types). */
24567 tree
24568 resolve_typename_type (tree type, bool only_current_p)
24570 tree scope;
24571 tree name;
24572 tree decl;
24573 int quals;
24574 tree pushed_scope;
24575 tree result;
24577 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24579 scope = TYPE_CONTEXT (type);
24580 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24581 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24582 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24583 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24584 identifier of the TYPENAME_TYPE anymore.
24585 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24586 TYPENAME_TYPE instead, we avoid messing up with a possible
24587 typedef variant case. */
24588 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24590 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24591 it first before we can figure out what NAME refers to. */
24592 if (TREE_CODE (scope) == TYPENAME_TYPE)
24594 if (TYPENAME_IS_RESOLVING_P (scope))
24595 /* Given a class template A with a dependent base with nested type C,
24596 typedef typename A::C::C C will land us here, as trying to resolve
24597 the initial A::C leads to the local C typedef, which leads back to
24598 A::C::C. So we break the recursion now. */
24599 return type;
24600 else
24601 scope = resolve_typename_type (scope, only_current_p);
24603 /* If we don't know what SCOPE refers to, then we cannot resolve the
24604 TYPENAME_TYPE. */
24605 if (!CLASS_TYPE_P (scope))
24606 return type;
24607 /* If this is a typedef, we don't want to look inside (c++/11987). */
24608 if (typedef_variant_p (type))
24609 return type;
24610 /* If SCOPE isn't the template itself, it will not have a valid
24611 TYPE_FIELDS list. */
24612 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24613 /* scope is either the template itself or a compatible instantiation
24614 like X<T>, so look up the name in the original template. */
24615 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24616 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24617 gcc_checking_assert (uses_template_parms (scope));
24618 /* If scope has no fields, it can't be a current instantiation. Check this
24619 before currently_open_class to avoid infinite recursion (71515). */
24620 if (!TYPE_FIELDS (scope))
24621 return type;
24622 /* If the SCOPE is not the current instantiation, there's no reason
24623 to look inside it. */
24624 if (only_current_p && !currently_open_class (scope))
24625 return type;
24626 /* Enter the SCOPE so that name lookup will be resolved as if we
24627 were in the class definition. In particular, SCOPE will no
24628 longer be considered a dependent type. */
24629 pushed_scope = push_scope (scope);
24630 /* Look up the declaration. */
24631 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24632 tf_warning_or_error);
24634 result = NULL_TREE;
24636 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24637 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24638 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24639 if (!decl)
24640 /*nop*/;
24641 else if (identifier_p (fullname)
24642 && TREE_CODE (decl) == TYPE_DECL)
24644 result = TREE_TYPE (decl);
24645 if (result == error_mark_node)
24646 result = NULL_TREE;
24648 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24649 && DECL_CLASS_TEMPLATE_P (decl))
24651 /* Obtain the template and the arguments. */
24652 tree tmpl = TREE_OPERAND (fullname, 0);
24653 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24655 /* We get here with a plain identifier because a previous tentative
24656 parse of the nested-name-specifier as part of a ptr-operator saw
24657 ::template X<A>. The use of ::template is necessary in a
24658 ptr-operator, but wrong in a declarator-id.
24660 [temp.names]: In a qualified-id of a declarator-id, the keyword
24661 template shall not appear at the top level. */
24662 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24663 "keyword %<template%> not allowed in declarator-id");
24664 tmpl = decl;
24666 tree args = TREE_OPERAND (fullname, 1);
24667 /* Instantiate the template. */
24668 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24669 /*entering_scope=*/true,
24670 tf_error | tf_user);
24671 if (result == error_mark_node)
24672 result = NULL_TREE;
24675 /* Leave the SCOPE. */
24676 if (pushed_scope)
24677 pop_scope (pushed_scope);
24679 /* If we failed to resolve it, return the original typename. */
24680 if (!result)
24681 return type;
24683 /* If lookup found a typename type, resolve that too. */
24684 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24686 /* Ill-formed programs can cause infinite recursion here, so we
24687 must catch that. */
24688 TYPENAME_IS_RESOLVING_P (result) = 1;
24689 result = resolve_typename_type (result, only_current_p);
24690 TYPENAME_IS_RESOLVING_P (result) = 0;
24693 /* Qualify the resulting type. */
24694 quals = cp_type_quals (type);
24695 if (quals)
24696 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24698 return result;
24701 /* EXPR is an expression which is not type-dependent. Return a proxy
24702 for EXPR that can be used to compute the types of larger
24703 expressions containing EXPR. */
24705 tree
24706 build_non_dependent_expr (tree expr)
24708 tree inner_expr;
24710 /* When checking, try to get a constant value for all non-dependent
24711 expressions in order to expose bugs in *_dependent_expression_p
24712 and constexpr. This can affect code generation, see PR70704, so
24713 only do this for -fchecking=2. */
24714 if (flag_checking > 1
24715 && cxx_dialect >= cxx11
24716 /* Don't do this during nsdmi parsing as it can lead to
24717 unexpected recursive instantiations. */
24718 && !parsing_nsdmi ()
24719 /* Don't do this during concept expansion either and for
24720 the same reason. */
24721 && !expanding_concept ())
24722 fold_non_dependent_expr (expr);
24724 /* Preserve OVERLOADs; the functions must be available to resolve
24725 types. */
24726 inner_expr = expr;
24727 if (TREE_CODE (inner_expr) == STMT_EXPR)
24728 inner_expr = stmt_expr_value_expr (inner_expr);
24729 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24730 inner_expr = TREE_OPERAND (inner_expr, 0);
24731 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24732 inner_expr = TREE_OPERAND (inner_expr, 1);
24733 if (is_overloaded_fn (inner_expr)
24734 || TREE_CODE (inner_expr) == OFFSET_REF)
24735 return expr;
24736 /* There is no need to return a proxy for a variable. */
24737 if (VAR_P (expr))
24738 return expr;
24739 /* Preserve string constants; conversions from string constants to
24740 "char *" are allowed, even though normally a "const char *"
24741 cannot be used to initialize a "char *". */
24742 if (TREE_CODE (expr) == STRING_CST)
24743 return expr;
24744 /* Preserve void and arithmetic constants, as an optimization -- there is no
24745 reason to create a new node. */
24746 if (TREE_CODE (expr) == VOID_CST
24747 || TREE_CODE (expr) == INTEGER_CST
24748 || TREE_CODE (expr) == REAL_CST)
24749 return expr;
24750 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24751 There is at least one place where we want to know that a
24752 particular expression is a throw-expression: when checking a ?:
24753 expression, there are special rules if the second or third
24754 argument is a throw-expression. */
24755 if (TREE_CODE (expr) == THROW_EXPR)
24756 return expr;
24758 /* Don't wrap an initializer list, we need to be able to look inside. */
24759 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24760 return expr;
24762 /* Don't wrap a dummy object, we need to be able to test for it. */
24763 if (is_dummy_object (expr))
24764 return expr;
24766 if (TREE_CODE (expr) == COND_EXPR)
24767 return build3 (COND_EXPR,
24768 TREE_TYPE (expr),
24769 TREE_OPERAND (expr, 0),
24770 (TREE_OPERAND (expr, 1)
24771 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24772 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24773 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24774 if (TREE_CODE (expr) == COMPOUND_EXPR
24775 && !COMPOUND_EXPR_OVERLOADED (expr))
24776 return build2 (COMPOUND_EXPR,
24777 TREE_TYPE (expr),
24778 TREE_OPERAND (expr, 0),
24779 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24781 /* If the type is unknown, it can't really be non-dependent */
24782 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24784 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24785 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24788 /* ARGS is a vector of expressions as arguments to a function call.
24789 Replace the arguments with equivalent non-dependent expressions.
24790 This modifies ARGS in place. */
24792 void
24793 make_args_non_dependent (vec<tree, va_gc> *args)
24795 unsigned int ix;
24796 tree arg;
24798 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24800 tree newarg = build_non_dependent_expr (arg);
24801 if (newarg != arg)
24802 (*args)[ix] = newarg;
24806 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24807 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24808 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24810 static tree
24811 make_auto_1 (tree name, bool set_canonical)
24813 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24814 TYPE_NAME (au) = build_decl (input_location,
24815 TYPE_DECL, name, au);
24816 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24817 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24818 (0, processing_template_decl + 1, processing_template_decl + 1,
24819 TYPE_NAME (au), NULL_TREE);
24820 if (set_canonical)
24821 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24822 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24823 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24825 return au;
24828 tree
24829 make_decltype_auto (void)
24831 return make_auto_1 (decltype_auto_identifier, true);
24834 tree
24835 make_auto (void)
24837 return make_auto_1 (auto_identifier, true);
24840 /* Return a C++17 deduction placeholder for class template TMPL. */
24842 tree
24843 make_template_placeholder (tree tmpl)
24845 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24846 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24847 return t;
24850 /* True iff T is a C++17 class template deduction placeholder. */
24852 bool
24853 template_placeholder_p (tree t)
24855 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
24858 /* Make a "constrained auto" type-specifier. This is an
24859 auto type with constraints that must be associated after
24860 deduction. The constraint is formed from the given
24861 CONC and its optional sequence of arguments, which are
24862 non-null if written as partial-concept-id. */
24864 tree
24865 make_constrained_auto (tree con, tree args)
24867 tree type = make_auto_1 (auto_identifier, false);
24869 /* Build the constraint. */
24870 tree tmpl = DECL_TI_TEMPLATE (con);
24871 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24872 expr = build_concept_check (expr, type, args);
24874 tree constr = normalize_expression (expr);
24875 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24877 /* Our canonical type depends on the constraint. */
24878 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24880 /* Attach the constraint to the type declaration. */
24881 tree decl = TYPE_NAME (type);
24882 return decl;
24885 /* Given type ARG, return std::initializer_list<ARG>. */
24887 static tree
24888 listify (tree arg)
24890 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24892 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24894 gcc_rich_location richloc (input_location);
24895 maybe_add_include_fixit (&richloc, "<initializer_list>");
24896 error_at_rich_loc (&richloc,
24897 "deducing from brace-enclosed initializer list"
24898 " requires #include <initializer_list>");
24900 return error_mark_node;
24902 tree argvec = make_tree_vec (1);
24903 TREE_VEC_ELT (argvec, 0) = arg;
24905 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24906 NULL_TREE, 0, tf_warning_or_error);
24909 /* Replace auto in TYPE with std::initializer_list<auto>. */
24911 static tree
24912 listify_autos (tree type, tree auto_node)
24914 tree init_auto = listify (auto_node);
24915 tree argvec = make_tree_vec (1);
24916 TREE_VEC_ELT (argvec, 0) = init_auto;
24917 if (processing_template_decl)
24918 argvec = add_to_template_args (current_template_args (), argvec);
24919 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24922 /* Hash traits for hashing possibly constrained 'auto'
24923 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24925 struct auto_hash : default_hash_traits<tree>
24927 static inline hashval_t hash (tree);
24928 static inline bool equal (tree, tree);
24931 /* Hash the 'auto' T. */
24933 inline hashval_t
24934 auto_hash::hash (tree t)
24936 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24937 /* Matching constrained-type-specifiers denote the same template
24938 parameter, so hash the constraint. */
24939 return hash_placeholder_constraint (c);
24940 else
24941 /* But unconstrained autos are all separate, so just hash the pointer. */
24942 return iterative_hash_object (t, 0);
24945 /* Compare two 'auto's. */
24947 inline bool
24948 auto_hash::equal (tree t1, tree t2)
24950 if (t1 == t2)
24951 return true;
24953 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24954 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24956 /* Two unconstrained autos are distinct. */
24957 if (!c1 || !c2)
24958 return false;
24960 return equivalent_placeholder_constraints (c1, c2);
24963 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24964 constrained) auto, add it to the vector. */
24966 static int
24967 extract_autos_r (tree t, void *data)
24969 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24970 if (is_auto (t))
24972 /* All the autos were built with index 0; fix that up now. */
24973 tree *p = hash.find_slot (t, INSERT);
24974 unsigned idx;
24975 if (*p)
24976 /* If this is a repeated constrained-type-specifier, use the index we
24977 chose before. */
24978 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24979 else
24981 /* Otherwise this is new, so use the current count. */
24982 *p = t;
24983 idx = hash.elements () - 1;
24985 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24988 /* Always keep walking. */
24989 return 0;
24992 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24993 says they can appear anywhere in the type. */
24995 static tree
24996 extract_autos (tree type)
24998 hash_set<tree> visited;
24999 hash_table<auto_hash> hash (2);
25001 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25003 tree tree_vec = make_tree_vec (hash.elements());
25004 for (hash_table<auto_hash>::iterator iter = hash.begin();
25005 iter != hash.end(); ++iter)
25007 tree elt = *iter;
25008 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25009 TREE_VEC_ELT (tree_vec, i)
25010 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25013 return tree_vec;
25016 /* The stem for deduction guide names. */
25017 const char *const dguide_base = "__dguide_";
25019 /* Return the name for a deduction guide for class template TMPL. */
25021 tree
25022 dguide_name (tree tmpl)
25024 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25025 tree tname = TYPE_IDENTIFIER (type);
25026 char *buf = (char *) alloca (1 + strlen (dguide_base)
25027 + IDENTIFIER_LENGTH (tname));
25028 memcpy (buf, dguide_base, strlen (dguide_base));
25029 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25030 IDENTIFIER_LENGTH (tname) + 1);
25031 tree dname = get_identifier (buf);
25032 TREE_TYPE (dname) = type;
25033 return dname;
25036 /* True if NAME is the name of a deduction guide. */
25038 bool
25039 dguide_name_p (tree name)
25041 return (TREE_TYPE (name)
25042 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25043 strlen (dguide_base)));
25046 /* True if FN is a deduction guide. */
25048 bool
25049 deduction_guide_p (const_tree fn)
25051 if (DECL_P (fn))
25052 if (tree name = DECL_NAME (fn))
25053 return dguide_name_p (name);
25054 return false;
25057 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25059 bool
25060 copy_guide_p (const_tree fn)
25062 gcc_assert (deduction_guide_p (fn));
25063 if (!DECL_ARTIFICIAL (fn))
25064 return false;
25065 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25066 return (TREE_CHAIN (parms) == void_list_node
25067 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25070 /* True if FN is a guide generated from a constructor template. */
25072 bool
25073 template_guide_p (const_tree fn)
25075 gcc_assert (deduction_guide_p (fn));
25076 if (!DECL_ARTIFICIAL (fn))
25077 return false;
25078 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25080 tree tmpl = DECL_TI_TEMPLATE (ctor);
25081 return PRIMARY_TEMPLATE_P (tmpl);
25083 return false;
25086 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25087 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25088 template parameter types. Note that the handling of template template
25089 parameters relies on current_template_parms being set appropriately for the
25090 new template. */
25092 static tree
25093 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25094 tree tsubst_args, tsubst_flags_t complain)
25096 tree oldidx = get_template_parm_index (olddecl);
25098 tree newtype;
25099 if (TREE_CODE (olddecl) == TYPE_DECL
25100 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25102 tree oldtype = TREE_TYPE (olddecl);
25103 newtype = cxx_make_type (TREE_CODE (oldtype));
25104 TYPE_MAIN_VARIANT (newtype) = newtype;
25105 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25106 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25107 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25109 else
25110 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25111 complain, NULL_TREE);
25113 tree newdecl
25114 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25115 DECL_NAME (olddecl), newtype);
25116 SET_DECL_TEMPLATE_PARM_P (newdecl);
25118 tree newidx;
25119 if (TREE_CODE (olddecl) == TYPE_DECL
25120 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25122 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25123 = build_template_parm_index (index, level, level,
25124 newdecl, newtype);
25125 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25126 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25127 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25128 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25130 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25132 DECL_TEMPLATE_RESULT (newdecl)
25133 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25134 DECL_NAME (olddecl), newtype);
25135 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25136 // First create a copy (ttargs) of tsubst_args with an
25137 // additional level for the template template parameter's own
25138 // template parameters (ttparms).
25139 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25140 (DECL_TEMPLATE_PARMS (olddecl)));
25141 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25142 tree ttargs = make_tree_vec (depth + 1);
25143 for (int i = 0; i < depth; ++i)
25144 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25145 TREE_VEC_ELT (ttargs, depth)
25146 = template_parms_level_to_args (ttparms);
25147 // Substitute ttargs into ttparms to fix references to
25148 // other template parameters.
25149 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25150 complain);
25151 // Now substitute again with args based on tparms, to reduce
25152 // the level of the ttparms.
25153 ttargs = current_template_args ();
25154 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25155 complain);
25156 // Finally, tack the adjusted parms onto tparms.
25157 ttparms = tree_cons (size_int (depth), ttparms,
25158 current_template_parms);
25159 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25162 else
25164 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25165 tree newconst
25166 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25167 TREE_CODE (oldconst),
25168 DECL_NAME (oldconst), newtype);
25169 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25170 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25171 SET_DECL_TEMPLATE_PARM_P (newconst);
25172 newidx = build_template_parm_index (index, level, level,
25173 newconst, newtype);
25174 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25175 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25176 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25179 return newdecl;
25182 /* Returns a C++17 class deduction guide template based on the constructor
25183 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25184 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25186 static tree
25187 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25189 tree type, tparms, targs, fparms, fargs, ci;
25190 bool memtmpl = false;
25191 bool explicit_p;
25192 location_t loc;
25194 if (TYPE_P (ctor))
25196 type = ctor;
25197 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25198 if (copy_p)
25200 type = TREE_TYPE (type);
25201 fparms = tree_cons (NULL_TREE, type, void_list_node);
25203 else
25204 fparms = void_list_node;
25206 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25207 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25208 targs = CLASSTYPE_TI_ARGS (type);
25209 ci = NULL_TREE;
25210 fargs = NULL_TREE;
25211 loc = DECL_SOURCE_LOCATION (ctmpl);
25212 explicit_p = false;
25214 else
25216 ++processing_template_decl;
25218 tree fn_tmpl
25219 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25220 : DECL_TI_TEMPLATE (ctor));
25221 if (outer_args)
25222 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25223 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25225 type = DECL_CONTEXT (ctor);
25227 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25228 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25229 fully specialized args for the enclosing class. Strip those off, as
25230 the deduction guide won't have those template parameters. */
25231 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25232 TMPL_PARMS_DEPTH (tparms));
25233 /* Discard the 'this' parameter. */
25234 fparms = FUNCTION_ARG_CHAIN (ctor);
25235 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25236 ci = get_constraints (ctor);
25237 loc = DECL_SOURCE_LOCATION (ctor);
25238 explicit_p = DECL_NONCONVERTING_P (ctor);
25240 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25242 memtmpl = true;
25244 /* For a member template constructor, we need to flatten the two
25245 template parameter lists into one, and then adjust the function
25246 signature accordingly. This gets...complicated. */
25247 tree save_parms = current_template_parms;
25249 /* For a member template we should have two levels of parms/args, one
25250 for the class and one for the constructor. We stripped
25251 specialized args for further enclosing classes above. */
25252 const int depth = 2;
25253 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25255 /* Template args for translating references to the two-level template
25256 parameters into references to the one-level template parameters we
25257 are creating. */
25258 tree tsubst_args = copy_node (targs);
25259 TMPL_ARGS_LEVEL (tsubst_args, depth)
25260 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25262 /* Template parms for the constructor template. */
25263 tree ftparms = TREE_VALUE (tparms);
25264 unsigned flen = TREE_VEC_LENGTH (ftparms);
25265 /* Template parms for the class template. */
25266 tparms = TREE_CHAIN (tparms);
25267 tree ctparms = TREE_VALUE (tparms);
25268 unsigned clen = TREE_VEC_LENGTH (ctparms);
25269 /* Template parms for the deduction guide start as a copy of the
25270 template parms for the class. We set current_template_parms for
25271 lookup_template_class_1. */
25272 current_template_parms = tparms = copy_node (tparms);
25273 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25274 for (unsigned i = 0; i < clen; ++i)
25275 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25277 /* Now we need to rewrite the constructor parms to append them to the
25278 class parms. */
25279 for (unsigned i = 0; i < flen; ++i)
25281 unsigned index = i + clen;
25282 unsigned level = 1;
25283 tree oldelt = TREE_VEC_ELT (ftparms, i);
25284 tree olddecl = TREE_VALUE (oldelt);
25285 tree newdecl = rewrite_template_parm (olddecl, index, level,
25286 tsubst_args, complain);
25287 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25288 tsubst_args, complain, ctor);
25289 tree list = build_tree_list (newdef, newdecl);
25290 TEMPLATE_PARM_CONSTRAINTS (list)
25291 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25292 tsubst_args, complain, ctor);
25293 TREE_VEC_ELT (new_vec, index) = list;
25294 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25297 /* Now we have a final set of template parms to substitute into the
25298 function signature. */
25299 targs = template_parms_to_args (tparms);
25300 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25301 complain, ctor);
25302 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25303 if (ci)
25304 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25306 current_template_parms = save_parms;
25308 --processing_template_decl;
25311 if (!memtmpl)
25313 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25314 tparms = copy_node (tparms);
25315 INNERMOST_TEMPLATE_PARMS (tparms)
25316 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25319 tree fntype = build_function_type (type, fparms);
25320 tree ded_fn = build_lang_decl_loc (loc,
25321 FUNCTION_DECL,
25322 dguide_name (type), fntype);
25323 DECL_ARGUMENTS (ded_fn) = fargs;
25324 DECL_ARTIFICIAL (ded_fn) = true;
25325 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25326 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25327 DECL_ARTIFICIAL (ded_tmpl) = true;
25328 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25329 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25330 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25331 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25332 if (DECL_P (ctor))
25333 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25334 if (ci)
25335 set_constraints (ded_tmpl, ci);
25337 return ded_tmpl;
25340 /* Deduce template arguments for the class template placeholder PTYPE for
25341 template TMPL based on the initializer INIT, and return the resulting
25342 type. */
25344 static tree
25345 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25346 tsubst_flags_t complain)
25348 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25350 /* We should have handled this in the caller. */
25351 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25352 return ptype;
25353 if (complain & tf_error)
25354 error ("non-class template %qT used without template arguments", tmpl);
25355 return error_mark_node;
25358 tree type = TREE_TYPE (tmpl);
25360 bool try_list_ctor = false;
25362 vec<tree,va_gc> *args;
25363 if (init == NULL_TREE
25364 || TREE_CODE (init) == TREE_LIST)
25365 args = make_tree_vector_from_list (init);
25366 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25368 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25369 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25371 /* As an exception, the first phase in 16.3.1.7 (considering the
25372 initializer list as a single argument) is omitted if the
25373 initializer list consists of a single expression of type cv U,
25374 where U is a specialization of C or a class derived from a
25375 specialization of C. */
25376 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25377 tree etype = TREE_TYPE (elt);
25379 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25380 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25381 int err = unify (tparms, targs, type, etype,
25382 UNIFY_ALLOW_DERIVED, /*explain*/false);
25383 if (err == 0)
25384 try_list_ctor = false;
25385 ggc_free (targs);
25387 if (try_list_ctor || is_std_init_list (type))
25388 args = make_tree_vector_single (init);
25389 else
25390 args = make_tree_vector_from_ctor (init);
25392 else
25393 args = make_tree_vector_single (init);
25395 tree dname = dguide_name (tmpl);
25396 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25397 /*type*/false, /*complain*/false,
25398 /*hidden*/false);
25399 bool elided = false;
25400 if (cands == error_mark_node)
25401 cands = NULL_TREE;
25403 /* Prune explicit deduction guides in copy-initialization context. */
25404 if (flags & LOOKUP_ONLYCONVERTING)
25406 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25407 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25408 elided = true;
25410 if (elided)
25412 /* Found a nonconverting guide, prune the candidates. */
25413 tree pruned = NULL_TREE;
25414 for (lkp_iterator iter (cands); iter; ++iter)
25415 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25416 pruned = lookup_add (*iter, pruned);
25418 cands = pruned;
25422 tree outer_args = NULL_TREE;
25423 if (DECL_CLASS_SCOPE_P (tmpl)
25424 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25426 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25427 type = TREE_TYPE (most_general_template (tmpl));
25430 bool saw_ctor = false;
25431 if (CLASSTYPE_METHOD_VEC (type))
25432 // FIXME cache artificial deduction guides
25433 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25434 iter; ++iter)
25436 tree guide = build_deduction_guide (*iter, outer_args, complain);
25437 if ((flags & LOOKUP_ONLYCONVERTING)
25438 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25439 elided = true;
25440 else
25441 cands = lookup_add (guide, cands);
25443 saw_ctor = true;
25446 tree call = error_mark_node;
25448 /* If this is list-initialization and the class has a list constructor, first
25449 try deducing from the list as a single argument, as [over.match.list]. */
25450 tree list_cands = NULL_TREE;
25451 if (try_list_ctor && cands)
25452 for (lkp_iterator iter (cands); iter; ++iter)
25454 tree dg = *iter;
25455 if (is_list_ctor (dg))
25456 list_cands = lookup_add (dg, list_cands);
25458 if (list_cands)
25460 ++cp_unevaluated_operand;
25461 call = build_new_function_call (list_cands, &args, tf_decltype);
25462 --cp_unevaluated_operand;
25464 if (call == error_mark_node)
25466 /* That didn't work, now try treating the list as a sequence of
25467 arguments. */
25468 release_tree_vector (args);
25469 args = make_tree_vector_from_ctor (init);
25473 /* Maybe generate an implicit deduction guide. */
25474 if (call == error_mark_node && args->length () < 2)
25476 tree gtype = NULL_TREE;
25478 if (args->length () == 1)
25479 /* Generate a copy guide. */
25480 gtype = build_reference_type (type);
25481 else if (!saw_ctor)
25482 /* Generate a default guide. */
25483 gtype = type;
25485 if (gtype)
25487 tree guide = build_deduction_guide (gtype, outer_args, complain);
25488 cands = lookup_add (guide, cands);
25492 if (elided && !cands)
25494 error ("cannot deduce template arguments for copy-initialization"
25495 " of %qT, as it has no non-explicit deduction guides or "
25496 "user-declared constructors", type);
25497 return error_mark_node;
25499 else if (!cands && call == error_mark_node)
25501 error ("cannot deduce template arguments of %qT, as it has no viable "
25502 "deduction guides", type);
25503 return error_mark_node;
25506 if (call == error_mark_node)
25508 ++cp_unevaluated_operand;
25509 call = build_new_function_call (cands, &args, tf_decltype);
25510 --cp_unevaluated_operand;
25513 if (call == error_mark_node && (complain & tf_warning_or_error))
25515 error ("class template argument deduction failed:");
25517 ++cp_unevaluated_operand;
25518 call = build_new_function_call (cands, &args, complain | tf_decltype);
25519 --cp_unevaluated_operand;
25521 if (elided)
25522 inform (input_location, "explicit deduction guides not considered "
25523 "for copy-initialization");
25526 release_tree_vector (args);
25528 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25531 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25532 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25534 tree
25535 do_auto_deduction (tree type, tree init, tree auto_node)
25537 return do_auto_deduction (type, init, auto_node,
25538 tf_warning_or_error,
25539 adc_unspecified);
25542 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25543 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25544 The CONTEXT determines the context in which auto deduction is performed
25545 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25546 OUTER_TARGS are used during template argument deduction
25547 (context == adc_unify) to properly substitute the result, and is ignored
25548 in other contexts.
25550 For partial-concept-ids, extra args may be appended to the list of deduced
25551 template arguments prior to determining constraint satisfaction. */
25553 tree
25554 do_auto_deduction (tree type, tree init, tree auto_node,
25555 tsubst_flags_t complain, auto_deduction_context context,
25556 tree outer_targs, int flags)
25558 tree targs;
25560 if (init == error_mark_node)
25561 return error_mark_node;
25563 if (init && type_dependent_expression_p (init)
25564 && context != adc_unify)
25565 /* Defining a subset of type-dependent expressions that we can deduce
25566 from ahead of time isn't worth the trouble. */
25567 return type;
25569 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25570 /* C++17 class template argument deduction. */
25571 return do_class_deduction (type, tmpl, init, flags, complain);
25573 if (TREE_TYPE (init) == NULL_TREE)
25574 /* Nothing we can do with this, even in deduction context. */
25575 return type;
25577 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25578 with either a new invented type template parameter U or, if the
25579 initializer is a braced-init-list (8.5.4), with
25580 std::initializer_list<U>. */
25581 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25583 if (!DIRECT_LIST_INIT_P (init))
25584 type = listify_autos (type, auto_node);
25585 else if (CONSTRUCTOR_NELTS (init) == 1)
25586 init = CONSTRUCTOR_ELT (init, 0)->value;
25587 else
25589 if (complain & tf_warning_or_error)
25591 if (permerror (input_location, "direct-list-initialization of "
25592 "%<auto%> requires exactly one element"))
25593 inform (input_location,
25594 "for deduction to %<std::initializer_list%>, use copy-"
25595 "list-initialization (i.e. add %<=%> before the %<{%>)");
25597 type = listify_autos (type, auto_node);
25601 if (type == error_mark_node)
25602 return error_mark_node;
25604 init = resolve_nondeduced_context (init, complain);
25606 if (context == adc_decomp_type
25607 && auto_node == type
25608 && init != error_mark_node
25609 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25610 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25611 and initializer has array type, deduce cv-qualified array type. */
25612 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25613 complain);
25614 else if (AUTO_IS_DECLTYPE (auto_node))
25616 bool id = (DECL_P (init)
25617 || ((TREE_CODE (init) == COMPONENT_REF
25618 || TREE_CODE (init) == SCOPE_REF)
25619 && !REF_PARENTHESIZED_P (init)));
25620 targs = make_tree_vec (1);
25621 TREE_VEC_ELT (targs, 0)
25622 = finish_decltype_type (init, id, tf_warning_or_error);
25623 if (type != auto_node)
25625 if (complain & tf_error)
25626 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25627 return error_mark_node;
25630 else
25632 tree parms = build_tree_list (NULL_TREE, type);
25633 tree tparms;
25635 if (flag_concepts)
25636 tparms = extract_autos (type);
25637 else
25639 tparms = make_tree_vec (1);
25640 TREE_VEC_ELT (tparms, 0)
25641 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25644 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25645 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25646 DEDUCE_CALL, LOOKUP_NORMAL,
25647 NULL, /*explain_p=*/false);
25648 if (val > 0)
25650 if (processing_template_decl)
25651 /* Try again at instantiation time. */
25652 return type;
25653 if (type && type != error_mark_node
25654 && (complain & tf_error))
25655 /* If type is error_mark_node a diagnostic must have been
25656 emitted by now. Also, having a mention to '<type error>'
25657 in the diagnostic is not really useful to the user. */
25659 if (cfun && auto_node == current_function_auto_return_pattern
25660 && LAMBDA_FUNCTION_P (current_function_decl))
25661 error ("unable to deduce lambda return type from %qE", init);
25662 else
25663 error ("unable to deduce %qT from %qE", type, init);
25664 type_unification_real (tparms, targs, parms, &init, 1, 0,
25665 DEDUCE_CALL, LOOKUP_NORMAL,
25666 NULL, /*explain_p=*/true);
25668 return error_mark_node;
25672 /* Check any placeholder constraints against the deduced type. */
25673 if (flag_concepts && !processing_template_decl)
25674 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25676 /* Use the deduced type to check the associated constraints. If we
25677 have a partial-concept-id, rebuild the argument list so that
25678 we check using the extra arguments. */
25679 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25680 tree cargs = CHECK_CONSTR_ARGS (constr);
25681 if (TREE_VEC_LENGTH (cargs) > 1)
25683 cargs = copy_node (cargs);
25684 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25686 else
25687 cargs = targs;
25688 if (!constraints_satisfied_p (constr, cargs))
25690 if (complain & tf_warning_or_error)
25692 switch (context)
25694 case adc_unspecified:
25695 case adc_unify:
25696 error("placeholder constraints not satisfied");
25697 break;
25698 case adc_variable_type:
25699 case adc_decomp_type:
25700 error ("deduced initializer does not satisfy "
25701 "placeholder constraints");
25702 break;
25703 case adc_return_type:
25704 error ("deduced return type does not satisfy "
25705 "placeholder constraints");
25706 break;
25707 case adc_requirement:
25708 error ("deduced expression type does not satisfy "
25709 "placeholder constraints");
25710 break;
25712 diagnose_constraints (input_location, constr, targs);
25714 return error_mark_node;
25718 if (processing_template_decl && context != adc_unify)
25719 outer_targs = current_template_args ();
25720 targs = add_to_template_args (outer_targs, targs);
25721 return tsubst (type, targs, complain, NULL_TREE);
25724 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25725 result. */
25727 tree
25728 splice_late_return_type (tree type, tree late_return_type)
25730 if (is_auto (type))
25732 if (late_return_type)
25733 return late_return_type;
25735 tree idx = get_template_parm_index (type);
25736 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25737 /* In an abbreviated function template we didn't know we were dealing
25738 with a function template when we saw the auto return type, so update
25739 it to have the correct level. */
25740 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25742 return type;
25745 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25746 'decltype(auto)' or a deduced class template. */
25748 bool
25749 is_auto (const_tree type)
25751 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25752 && (TYPE_IDENTIFIER (type) == auto_identifier
25753 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25754 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25755 return true;
25756 else
25757 return false;
25760 /* for_each_template_parm callback for type_uses_auto. */
25763 is_auto_r (tree tp, void */*data*/)
25765 return is_auto (tp);
25768 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25769 a use of `auto'. Returns NULL_TREE otherwise. */
25771 tree
25772 type_uses_auto (tree type)
25774 if (type == NULL_TREE)
25775 return NULL_TREE;
25776 else if (flag_concepts)
25778 /* The Concepts TS allows multiple autos in one type-specifier; just
25779 return the first one we find, do_auto_deduction will collect all of
25780 them. */
25781 if (uses_template_parms (type))
25782 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25783 /*visited*/NULL, /*nondeduced*/true);
25784 else
25785 return NULL_TREE;
25787 else
25788 return find_type_usage (type, is_auto);
25791 /* For a given template T, return the vector of typedefs referenced
25792 in T for which access check is needed at T instantiation time.
25793 T is either a FUNCTION_DECL or a RECORD_TYPE.
25794 Those typedefs were added to T by the function
25795 append_type_to_template_for_access_check. */
25797 vec<qualified_typedef_usage_t, va_gc> *
25798 get_types_needing_access_check (tree t)
25800 tree ti;
25801 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25803 if (!t || t == error_mark_node)
25804 return NULL;
25806 if (!(ti = get_template_info (t)))
25807 return NULL;
25809 if (CLASS_TYPE_P (t)
25810 || TREE_CODE (t) == FUNCTION_DECL)
25812 if (!TI_TEMPLATE (ti))
25813 return NULL;
25815 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25818 return result;
25821 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25822 tied to T. That list of typedefs will be access checked at
25823 T instantiation time.
25824 T is either a FUNCTION_DECL or a RECORD_TYPE.
25825 TYPE_DECL is a TYPE_DECL node representing a typedef.
25826 SCOPE is the scope through which TYPE_DECL is accessed.
25827 LOCATION is the location of the usage point of TYPE_DECL.
25829 This function is a subroutine of
25830 append_type_to_template_for_access_check. */
25832 static void
25833 append_type_to_template_for_access_check_1 (tree t,
25834 tree type_decl,
25835 tree scope,
25836 location_t location)
25838 qualified_typedef_usage_t typedef_usage;
25839 tree ti;
25841 if (!t || t == error_mark_node)
25842 return;
25844 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25845 || CLASS_TYPE_P (t))
25846 && type_decl
25847 && TREE_CODE (type_decl) == TYPE_DECL
25848 && scope);
25850 if (!(ti = get_template_info (t)))
25851 return;
25853 gcc_assert (TI_TEMPLATE (ti));
25855 typedef_usage.typedef_decl = type_decl;
25856 typedef_usage.context = scope;
25857 typedef_usage.locus = location;
25859 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25862 /* Append TYPE_DECL to the template TEMPL.
25863 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25864 At TEMPL instanciation time, TYPE_DECL will be checked to see
25865 if it can be accessed through SCOPE.
25866 LOCATION is the location of the usage point of TYPE_DECL.
25868 e.g. consider the following code snippet:
25870 class C
25872 typedef int myint;
25875 template<class U> struct S
25877 C::myint mi; // <-- usage point of the typedef C::myint
25880 S<char> s;
25882 At S<char> instantiation time, we need to check the access of C::myint
25883 In other words, we need to check the access of the myint typedef through
25884 the C scope. For that purpose, this function will add the myint typedef
25885 and the scope C through which its being accessed to a list of typedefs
25886 tied to the template S. That list will be walked at template instantiation
25887 time and access check performed on each typedefs it contains.
25888 Note that this particular code snippet should yield an error because
25889 myint is private to C. */
25891 void
25892 append_type_to_template_for_access_check (tree templ,
25893 tree type_decl,
25894 tree scope,
25895 location_t location)
25897 qualified_typedef_usage_t *iter;
25898 unsigned i;
25900 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25902 /* Make sure we don't append the type to the template twice. */
25903 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25904 if (iter->typedef_decl == type_decl && scope == iter->context)
25905 return;
25907 append_type_to_template_for_access_check_1 (templ, type_decl,
25908 scope, location);
25911 /* Convert the generic type parameters in PARM that match the types given in the
25912 range [START_IDX, END_IDX) from the current_template_parms into generic type
25913 packs. */
25915 tree
25916 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25918 tree current = current_template_parms;
25919 int depth = TMPL_PARMS_DEPTH (current);
25920 current = INNERMOST_TEMPLATE_PARMS (current);
25921 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25923 for (int i = 0; i < start_idx; ++i)
25924 TREE_VEC_ELT (replacement, i)
25925 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25927 for (int i = start_idx; i < end_idx; ++i)
25929 /* Create a distinct parameter pack type from the current parm and add it
25930 to the replacement args to tsubst below into the generic function
25931 parameter. */
25933 tree o = TREE_TYPE (TREE_VALUE
25934 (TREE_VEC_ELT (current, i)));
25935 tree t = copy_type (o);
25936 TEMPLATE_TYPE_PARM_INDEX (t)
25937 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25938 o, 0, 0, tf_none);
25939 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25940 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25941 TYPE_MAIN_VARIANT (t) = t;
25942 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25943 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25944 TREE_VEC_ELT (replacement, i) = t;
25945 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25948 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25949 TREE_VEC_ELT (replacement, i)
25950 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25952 /* If there are more levels then build up the replacement with the outer
25953 template parms. */
25954 if (depth > 1)
25955 replacement = add_to_template_args (template_parms_to_args
25956 (TREE_CHAIN (current_template_parms)),
25957 replacement);
25959 return tsubst (parm, replacement, tf_none, NULL_TREE);
25962 /* Entries in the decl_constraint hash table. */
25963 struct GTY((for_user)) constr_entry
25965 tree decl;
25966 tree ci;
25969 /* Hashing function and equality for constraint entries. */
25970 struct constr_hasher : ggc_ptr_hash<constr_entry>
25972 static hashval_t hash (constr_entry *e)
25974 return (hashval_t)DECL_UID (e->decl);
25977 static bool equal (constr_entry *e1, constr_entry *e2)
25979 return e1->decl == e2->decl;
25983 /* A mapping from declarations to constraint information. Note that
25984 both templates and their underlying declarations are mapped to the
25985 same constraint information.
25987 FIXME: This is defined in pt.c because garbage collection
25988 code is not being generated for constraint.cc. */
25990 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25992 /* Returns the template constraints of declaration T. If T is not
25993 constrained, return NULL_TREE. Note that T must be non-null. */
25995 tree
25996 get_constraints (tree t)
25998 if (!flag_concepts)
25999 return NULL_TREE;
26001 gcc_assert (DECL_P (t));
26002 if (TREE_CODE (t) == TEMPLATE_DECL)
26003 t = DECL_TEMPLATE_RESULT (t);
26004 constr_entry elt = { t, NULL_TREE };
26005 constr_entry* found = decl_constraints->find (&elt);
26006 if (found)
26007 return found->ci;
26008 else
26009 return NULL_TREE;
26012 /* Associate the given constraint information CI with the declaration
26013 T. If T is a template, then the constraints are associated with
26014 its underlying declaration. Don't build associations if CI is
26015 NULL_TREE. */
26017 void
26018 set_constraints (tree t, tree ci)
26020 if (!ci)
26021 return;
26022 gcc_assert (t && flag_concepts);
26023 if (TREE_CODE (t) == TEMPLATE_DECL)
26024 t = DECL_TEMPLATE_RESULT (t);
26025 gcc_assert (!get_constraints (t));
26026 constr_entry elt = {t, ci};
26027 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26028 constr_entry* entry = ggc_alloc<constr_entry> ();
26029 *entry = elt;
26030 *slot = entry;
26033 /* Remove the associated constraints of the declaration T. */
26035 void
26036 remove_constraints (tree t)
26038 gcc_assert (DECL_P (t));
26039 if (TREE_CODE (t) == TEMPLATE_DECL)
26040 t = DECL_TEMPLATE_RESULT (t);
26042 constr_entry elt = {t, NULL_TREE};
26043 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26044 if (slot)
26045 decl_constraints->clear_slot (slot);
26048 /* Memoized satisfaction results for declarations. This
26049 maps the pair (constraint_info, arguments) to the result computed
26050 by constraints_satisfied_p. */
26052 struct GTY((for_user)) constraint_sat_entry
26054 tree ci;
26055 tree args;
26056 tree result;
26059 /* Hashing function and equality for constraint entries. */
26061 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26063 static hashval_t hash (constraint_sat_entry *e)
26065 hashval_t val = iterative_hash_object(e->ci, 0);
26066 return iterative_hash_template_arg (e->args, val);
26069 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26071 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26075 /* Memoized satisfaction results for concept checks. */
26077 struct GTY((for_user)) concept_spec_entry
26079 tree tmpl;
26080 tree args;
26081 tree result;
26084 /* Hashing function and equality for constraint entries. */
26086 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26088 static hashval_t hash (concept_spec_entry *e)
26090 return hash_tmpl_and_args (e->tmpl, e->args);
26093 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26095 ++comparing_specializations;
26096 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26097 --comparing_specializations;
26098 return eq;
26102 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26103 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26105 /* Search for a memoized satisfaction result. Returns one of the
26106 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26108 tree
26109 lookup_constraint_satisfaction (tree ci, tree args)
26111 constraint_sat_entry elt = { ci, args, NULL_TREE };
26112 constraint_sat_entry* found = constraint_memos->find (&elt);
26113 if (found)
26114 return found->result;
26115 else
26116 return NULL_TREE;
26119 /* Memoize the result of a satisfication test. Returns the saved result. */
26121 tree
26122 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26124 constraint_sat_entry elt = {ci, args, result};
26125 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26126 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26127 *entry = elt;
26128 *slot = entry;
26129 return result;
26132 /* Search for a memoized satisfaction result for a concept. */
26134 tree
26135 lookup_concept_satisfaction (tree tmpl, tree args)
26137 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26138 concept_spec_entry* found = concept_memos->find (&elt);
26139 if (found)
26140 return found->result;
26141 else
26142 return NULL_TREE;
26145 /* Memoize the result of a concept check. Returns the saved result. */
26147 tree
26148 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26150 concept_spec_entry elt = {tmpl, args, result};
26151 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26152 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26153 *entry = elt;
26154 *slot = entry;
26155 return result;
26158 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26160 /* Returns a prior concept specialization. This returns the substituted
26161 and normalized constraints defined by the concept. */
26163 tree
26164 get_concept_expansion (tree tmpl, tree args)
26166 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26167 concept_spec_entry* found = concept_expansions->find (&elt);
26168 if (found)
26169 return found->result;
26170 else
26171 return NULL_TREE;
26174 /* Save a concept expansion for later. */
26176 tree
26177 save_concept_expansion (tree tmpl, tree args, tree def)
26179 concept_spec_entry elt = {tmpl, args, def};
26180 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26181 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26182 *entry = elt;
26183 *slot = entry;
26184 return def;
26187 static hashval_t
26188 hash_subsumption_args (tree t1, tree t2)
26190 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26191 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26192 int val = 0;
26193 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26194 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26195 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26196 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26197 return val;
26200 /* Compare the constraints of two subsumption entries. The LEFT1 and
26201 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26202 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26204 static bool
26205 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26207 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26208 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26209 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26210 CHECK_CONSTR_ARGS (right1)))
26211 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26212 CHECK_CONSTR_ARGS (right2));
26213 return false;
26216 /* Key/value pair for learning and memoizing subsumption results. This
26217 associates a pair of check constraints (including arguments) with
26218 a boolean value indicating the result. */
26220 struct GTY((for_user)) subsumption_entry
26222 tree t1;
26223 tree t2;
26224 bool result;
26227 /* Hashing function and equality for constraint entries. */
26229 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26231 static hashval_t hash (subsumption_entry *e)
26233 return hash_subsumption_args (e->t1, e->t2);
26236 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26238 ++comparing_specializations;
26239 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26240 --comparing_specializations;
26241 return eq;
26245 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26247 /* Search for a previously cached subsumption result. */
26249 bool*
26250 lookup_subsumption_result (tree t1, tree t2)
26252 subsumption_entry elt = { t1, t2, false };
26253 subsumption_entry* found = subsumption_table->find (&elt);
26254 if (found)
26255 return &found->result;
26256 else
26257 return 0;
26260 /* Save a subsumption result. */
26262 bool
26263 save_subsumption_result (tree t1, tree t2, bool result)
26265 subsumption_entry elt = {t1, t2, result};
26266 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26267 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26268 *entry = elt;
26269 *slot = entry;
26270 return result;
26273 /* Set up the hash table for constraint association. */
26275 void
26276 init_constraint_processing (void)
26278 if (!flag_concepts)
26279 return;
26281 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26282 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26283 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26284 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26285 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26288 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26289 0..N-1. */
26291 void
26292 declare_integer_pack (void)
26294 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26295 build_function_type_list (integer_type_node,
26296 integer_type_node,
26297 NULL_TREE),
26298 NULL_TREE, ECF_CONST);
26299 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26300 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26303 /* Set up the hash tables for template instantiations. */
26305 void
26306 init_template_processing (void)
26308 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26309 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26311 if (cxx_dialect >= cxx11)
26312 declare_integer_pack ();
26315 /* Print stats about the template hash tables for -fstats. */
26317 void
26318 print_template_statistics (void)
26320 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26321 "%f collisions\n", (long) decl_specializations->size (),
26322 (long) decl_specializations->elements (),
26323 decl_specializations->collisions ());
26324 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26325 "%f collisions\n", (long) type_specializations->size (),
26326 (long) type_specializations->elements (),
26327 type_specializations->collisions ());
26330 #include "gt-cp-pt.h"