2017-08-06 Andrew Pinski <apinski@cavium.com>
[official-gcc.git] / gcc / cp / pt.c
blobbd61438fc39d834a473d440f47dbf8415800487a
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 tree fns = NULL_TREE;
2898 if (DECL_CONV_FN_P (decl))
2899 /* For a type-conversion operator, we cannot do a
2900 name-based lookup. We might be looking for `operator
2901 int' which will be a specialization of `operator T'.
2902 Grab all the conversion operators, and then select from
2903 them. */
2904 fns = lookup_all_conversions (ctype);
2905 else
2906 fns = lookup_fnfields_slot_nolazy (ctype, name);
2908 if (fns == NULL_TREE)
2910 error ("no member function %qD declared in %qT", name, ctype);
2911 return error_mark_node;
2913 else
2914 TREE_OPERAND (declarator, 0) = fns;
2917 /* Figure out what exactly is being specialized at this point.
2918 Note that for an explicit instantiation, even one for a
2919 member function, we cannot tell a priori whether the
2920 instantiation is for a member template, or just a member
2921 function of a template class. Even if a member template is
2922 being instantiated, the member template arguments may be
2923 elided if they can be deduced from the rest of the
2924 declaration. */
2925 tmpl = determine_specialization (declarator, decl,
2926 &targs,
2927 member_specialization,
2928 template_count,
2929 tsk);
2931 if (!tmpl || tmpl == error_mark_node)
2932 /* We couldn't figure out what this declaration was
2933 specializing. */
2934 return error_mark_node;
2935 else
2937 if (TREE_CODE (decl) == FUNCTION_DECL
2938 && DECL_HIDDEN_FRIEND_P (tmpl))
2940 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2941 "friend declaration %qD is not visible to "
2942 "explicit specialization", tmpl))
2943 inform (DECL_SOURCE_LOCATION (tmpl),
2944 "friend declaration here");
2946 else if (!ctype && !is_friend
2947 && CP_DECL_CONTEXT (decl) == current_namespace)
2948 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2950 tree gen_tmpl = most_general_template (tmpl);
2952 if (explicit_instantiation)
2954 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2955 is done by do_decl_instantiation later. */
2957 int arg_depth = TMPL_ARGS_DEPTH (targs);
2958 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2960 if (arg_depth > parm_depth)
2962 /* If TMPL is not the most general template (for
2963 example, if TMPL is a friend template that is
2964 injected into namespace scope), then there will
2965 be too many levels of TARGS. Remove some of them
2966 here. */
2967 int i;
2968 tree new_targs;
2970 new_targs = make_tree_vec (parm_depth);
2971 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2972 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2973 = TREE_VEC_ELT (targs, i);
2974 targs = new_targs;
2977 return instantiate_template (tmpl, targs, tf_error);
2980 /* If we thought that the DECL was a member function, but it
2981 turns out to be specializing a static member function,
2982 make DECL a static member function as well. */
2983 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2984 && DECL_STATIC_FUNCTION_P (tmpl)
2985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2986 revert_static_member_fn (decl);
2988 /* If this is a specialization of a member template of a
2989 template class, we want to return the TEMPLATE_DECL, not
2990 the specialization of it. */
2991 if (tsk == tsk_template && !was_template_id)
2993 tree result = DECL_TEMPLATE_RESULT (tmpl);
2994 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2995 DECL_INITIAL (result) = NULL_TREE;
2996 if (have_def)
2998 tree parm;
2999 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3000 DECL_SOURCE_LOCATION (result)
3001 = DECL_SOURCE_LOCATION (decl);
3002 /* We want to use the argument list specified in the
3003 definition, not in the original declaration. */
3004 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3005 for (parm = DECL_ARGUMENTS (result); parm;
3006 parm = DECL_CHAIN (parm))
3007 DECL_CONTEXT (parm) = result;
3009 return register_specialization (tmpl, gen_tmpl, targs,
3010 is_friend, 0);
3013 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3014 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3016 if (was_template_id)
3017 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3019 /* Inherit default function arguments from the template
3020 DECL is specializing. */
3021 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3022 copy_default_args_to_explicit_spec (decl);
3024 /* This specialization has the same protection as the
3025 template it specializes. */
3026 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3027 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3029 /* 7.1.1-1 [dcl.stc]
3031 A storage-class-specifier shall not be specified in an
3032 explicit specialization...
3034 The parser rejects these, so unless action is taken here,
3035 explicit function specializations will always appear with
3036 global linkage.
3038 The action recommended by the C++ CWG in response to C++
3039 defect report 605 is to make the storage class and linkage
3040 of the explicit specialization match the templated function:
3042 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3044 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3046 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3047 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3049 /* A concept cannot be specialized. */
3050 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3052 error ("explicit specialization of function concept %qD",
3053 gen_tmpl);
3054 return error_mark_node;
3057 /* This specialization has the same linkage and visibility as
3058 the function template it specializes. */
3059 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3060 if (! TREE_PUBLIC (decl))
3062 DECL_INTERFACE_KNOWN (decl) = 1;
3063 DECL_NOT_REALLY_EXTERN (decl) = 1;
3065 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3066 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3068 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3069 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3073 /* If DECL is a friend declaration, declared using an
3074 unqualified name, the namespace associated with DECL may
3075 have been set incorrectly. For example, in:
3077 template <typename T> void f(T);
3078 namespace N {
3079 struct S { friend void f<int>(int); }
3082 we will have set the DECL_CONTEXT for the friend
3083 declaration to N, rather than to the global namespace. */
3084 if (DECL_NAMESPACE_SCOPE_P (decl))
3085 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3087 if (is_friend && !have_def)
3088 /* This is not really a declaration of a specialization.
3089 It's just the name of an instantiation. But, it's not
3090 a request for an instantiation, either. */
3091 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3092 else if (TREE_CODE (decl) == FUNCTION_DECL)
3093 /* A specialization is not necessarily COMDAT. */
3094 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3095 && DECL_DECLARED_INLINE_P (decl));
3096 else if (VAR_P (decl))
3097 DECL_COMDAT (decl) = false;
3099 /* If this is a full specialization, register it so that we can find
3100 it again. Partial specializations will be registered in
3101 process_partial_specialization. */
3102 if (!processing_template_decl)
3103 decl = register_specialization (decl, gen_tmpl, targs,
3104 is_friend, 0);
3106 /* A 'structor should already have clones. */
3107 gcc_assert (decl == error_mark_node
3108 || variable_template_p (tmpl)
3109 || !(DECL_CONSTRUCTOR_P (decl)
3110 || DECL_DESTRUCTOR_P (decl))
3111 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3115 return decl;
3118 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3119 parameters. These are represented in the same format used for
3120 DECL_TEMPLATE_PARMS. */
3123 comp_template_parms (const_tree parms1, const_tree parms2)
3125 const_tree p1;
3126 const_tree p2;
3128 if (parms1 == parms2)
3129 return 1;
3131 for (p1 = parms1, p2 = parms2;
3132 p1 != NULL_TREE && p2 != NULL_TREE;
3133 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3135 tree t1 = TREE_VALUE (p1);
3136 tree t2 = TREE_VALUE (p2);
3137 int i;
3139 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3140 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3142 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3143 return 0;
3145 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3147 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3148 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3150 /* If either of the template parameters are invalid, assume
3151 they match for the sake of error recovery. */
3152 if (error_operand_p (parm1) || error_operand_p (parm2))
3153 return 1;
3155 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3156 return 0;
3158 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3159 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3160 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3161 continue;
3162 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3163 return 0;
3167 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3168 /* One set of parameters has more parameters lists than the
3169 other. */
3170 return 0;
3172 return 1;
3175 /* Determine whether PARM is a parameter pack. */
3177 bool
3178 template_parameter_pack_p (const_tree parm)
3180 /* Determine if we have a non-type template parameter pack. */
3181 if (TREE_CODE (parm) == PARM_DECL)
3182 return (DECL_TEMPLATE_PARM_P (parm)
3183 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3184 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3185 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3187 /* If this is a list of template parameters, we could get a
3188 TYPE_DECL or a TEMPLATE_DECL. */
3189 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3190 parm = TREE_TYPE (parm);
3192 /* Otherwise it must be a type template parameter. */
3193 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3194 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3195 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3198 /* Determine if T is a function parameter pack. */
3200 bool
3201 function_parameter_pack_p (const_tree t)
3203 if (t && TREE_CODE (t) == PARM_DECL)
3204 return DECL_PACK_P (t);
3205 return false;
3208 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3209 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3211 tree
3212 get_function_template_decl (const_tree primary_func_tmpl_inst)
3214 if (! primary_func_tmpl_inst
3215 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3216 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3217 return NULL;
3219 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3222 /* Return true iff the function parameter PARAM_DECL was expanded
3223 from the function parameter pack PACK. */
3225 bool
3226 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3228 if (DECL_ARTIFICIAL (param_decl)
3229 || !function_parameter_pack_p (pack))
3230 return false;
3232 /* The parameter pack and its pack arguments have the same
3233 DECL_PARM_INDEX. */
3234 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3237 /* Determine whether ARGS describes a variadic template args list,
3238 i.e., one that is terminated by a template argument pack. */
3240 static bool
3241 template_args_variadic_p (tree args)
3243 int nargs;
3244 tree last_parm;
3246 if (args == NULL_TREE)
3247 return false;
3249 args = INNERMOST_TEMPLATE_ARGS (args);
3250 nargs = TREE_VEC_LENGTH (args);
3252 if (nargs == 0)
3253 return false;
3255 last_parm = TREE_VEC_ELT (args, nargs - 1);
3257 return ARGUMENT_PACK_P (last_parm);
3260 /* Generate a new name for the parameter pack name NAME (an
3261 IDENTIFIER_NODE) that incorporates its */
3263 static tree
3264 make_ith_pack_parameter_name (tree name, int i)
3266 /* Munge the name to include the parameter index. */
3267 #define NUMBUF_LEN 128
3268 char numbuf[NUMBUF_LEN];
3269 char* newname;
3270 int newname_len;
3272 if (name == NULL_TREE)
3273 return name;
3274 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3275 newname_len = IDENTIFIER_LENGTH (name)
3276 + strlen (numbuf) + 2;
3277 newname = (char*)alloca (newname_len);
3278 snprintf (newname, newname_len,
3279 "%s#%i", IDENTIFIER_POINTER (name), i);
3280 return get_identifier (newname);
3283 /* Return true if T is a primary function, class or alias template
3284 instantiation. */
3286 bool
3287 primary_template_instantiation_p (const_tree t)
3289 if (!t)
3290 return false;
3292 if (TREE_CODE (t) == FUNCTION_DECL)
3293 return DECL_LANG_SPECIFIC (t)
3294 && DECL_TEMPLATE_INSTANTIATION (t)
3295 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3296 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3297 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3299 else if (alias_template_specialization_p (t))
3300 return true;
3301 return false;
3304 /* Return true if PARM is a template template parameter. */
3306 bool
3307 template_template_parameter_p (const_tree parm)
3309 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3312 /* Return true iff PARM is a DECL representing a type template
3313 parameter. */
3315 bool
3316 template_type_parameter_p (const_tree parm)
3318 return (parm
3319 && (TREE_CODE (parm) == TYPE_DECL
3320 || TREE_CODE (parm) == TEMPLATE_DECL)
3321 && DECL_TEMPLATE_PARM_P (parm));
3324 /* Return the template parameters of T if T is a
3325 primary template instantiation, NULL otherwise. */
3327 tree
3328 get_primary_template_innermost_parameters (const_tree t)
3330 tree parms = NULL, template_info = NULL;
3332 if ((template_info = get_template_info (t))
3333 && primary_template_instantiation_p (t))
3334 parms = INNERMOST_TEMPLATE_PARMS
3335 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3337 return parms;
3340 /* Return the template parameters of the LEVELth level from the full list
3341 of template parameters PARMS. */
3343 tree
3344 get_template_parms_at_level (tree parms, int level)
3346 tree p;
3347 if (!parms
3348 || TREE_CODE (parms) != TREE_LIST
3349 || level > TMPL_PARMS_DEPTH (parms))
3350 return NULL_TREE;
3352 for (p = parms; p; p = TREE_CHAIN (p))
3353 if (TMPL_PARMS_DEPTH (p) == level)
3354 return p;
3356 return NULL_TREE;
3359 /* Returns the template arguments of T if T is a template instantiation,
3360 NULL otherwise. */
3362 tree
3363 get_template_innermost_arguments (const_tree t)
3365 tree args = NULL, template_info = NULL;
3367 if ((template_info = get_template_info (t))
3368 && TI_ARGS (template_info))
3369 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3371 return args;
3374 /* Return the argument pack elements of T if T is a template argument pack,
3375 NULL otherwise. */
3377 tree
3378 get_template_argument_pack_elems (const_tree t)
3380 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3381 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3382 return NULL;
3384 return ARGUMENT_PACK_ARGS (t);
3387 /* True iff FN is a function representing a built-in variadic parameter
3388 pack. */
3390 bool
3391 builtin_pack_fn_p (tree fn)
3393 if (!fn
3394 || TREE_CODE (fn) != FUNCTION_DECL
3395 || !DECL_IS_BUILTIN (fn))
3396 return false;
3398 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3399 return true;
3401 return false;
3404 /* True iff CALL is a call to a function representing a built-in variadic
3405 parameter pack. */
3407 static bool
3408 builtin_pack_call_p (tree call)
3410 if (TREE_CODE (call) != CALL_EXPR)
3411 return false;
3412 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3415 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3417 static tree
3418 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3419 tree in_decl)
3421 tree ohi = CALL_EXPR_ARG (call, 0);
3422 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3423 false/*fn*/, true/*int_cst*/);
3425 if (value_dependent_expression_p (hi))
3427 if (hi != ohi)
3429 call = copy_node (call);
3430 CALL_EXPR_ARG (call, 0) = hi;
3432 tree ex = make_pack_expansion (call);
3433 tree vec = make_tree_vec (1);
3434 TREE_VEC_ELT (vec, 0) = ex;
3435 return vec;
3437 else
3439 hi = cxx_constant_value (hi);
3440 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3442 /* Calculate the largest value of len that won't make the size of the vec
3443 overflow an int. The compiler will exceed resource limits long before
3444 this, but it seems a decent place to diagnose. */
3445 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3447 if (len < 0 || len > max)
3449 if ((complain & tf_error)
3450 && hi != error_mark_node)
3451 error ("argument to __integer_pack must be between 0 and %d", max);
3452 return error_mark_node;
3455 tree vec = make_tree_vec (len);
3457 for (int i = 0; i < len; ++i)
3458 TREE_VEC_ELT (vec, i) = size_int (i);
3460 return vec;
3464 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3465 CALL. */
3467 static tree
3468 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3469 tree in_decl)
3471 if (!builtin_pack_call_p (call))
3472 return NULL_TREE;
3474 tree fn = CALL_EXPR_FN (call);
3476 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3477 return expand_integer_pack (call, args, complain, in_decl);
3479 return NULL_TREE;
3482 /* Structure used to track the progress of find_parameter_packs_r. */
3483 struct find_parameter_pack_data
3485 /* TREE_LIST that will contain all of the parameter packs found by
3486 the traversal. */
3487 tree* parameter_packs;
3489 /* Set of AST nodes that have been visited by the traversal. */
3490 hash_set<tree> *visited;
3492 /* True iff we're making a type pack expansion. */
3493 bool type_pack_expansion_p;
3496 /* Identifies all of the argument packs that occur in a template
3497 argument and appends them to the TREE_LIST inside DATA, which is a
3498 find_parameter_pack_data structure. This is a subroutine of
3499 make_pack_expansion and uses_parameter_packs. */
3500 static tree
3501 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3503 tree t = *tp;
3504 struct find_parameter_pack_data* ppd =
3505 (struct find_parameter_pack_data*)data;
3506 bool parameter_pack_p = false;
3508 /* Handle type aliases/typedefs. */
3509 if (TYPE_ALIAS_P (t))
3511 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3512 cp_walk_tree (&TI_ARGS (tinfo),
3513 &find_parameter_packs_r,
3514 ppd, ppd->visited);
3515 *walk_subtrees = 0;
3516 return NULL_TREE;
3519 /* Identify whether this is a parameter pack or not. */
3520 switch (TREE_CODE (t))
3522 case TEMPLATE_PARM_INDEX:
3523 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3524 parameter_pack_p = true;
3525 break;
3527 case TEMPLATE_TYPE_PARM:
3528 t = TYPE_MAIN_VARIANT (t);
3529 /* FALLTHRU */
3530 case TEMPLATE_TEMPLATE_PARM:
3531 /* If the placeholder appears in the decl-specifier-seq of a function
3532 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3533 is a pack expansion, the invented template parameter is a template
3534 parameter pack. */
3535 if (ppd->type_pack_expansion_p && is_auto (t))
3536 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3537 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3538 parameter_pack_p = true;
3539 break;
3541 case FIELD_DECL:
3542 case PARM_DECL:
3543 if (DECL_PACK_P (t))
3545 /* We don't want to walk into the type of a PARM_DECL,
3546 because we don't want to see the type parameter pack. */
3547 *walk_subtrees = 0;
3548 parameter_pack_p = true;
3550 break;
3552 /* Look through a lambda capture proxy to the field pack. */
3553 case VAR_DECL:
3554 if (DECL_HAS_VALUE_EXPR_P (t))
3556 tree v = DECL_VALUE_EXPR (t);
3557 cp_walk_tree (&v,
3558 &find_parameter_packs_r,
3559 ppd, ppd->visited);
3560 *walk_subtrees = 0;
3562 else if (variable_template_specialization_p (t))
3564 cp_walk_tree (&DECL_TI_ARGS (t),
3565 find_parameter_packs_r,
3566 ppd, ppd->visited);
3567 *walk_subtrees = 0;
3569 break;
3571 case CALL_EXPR:
3572 if (builtin_pack_call_p (t))
3573 parameter_pack_p = true;
3574 break;
3576 case BASES:
3577 parameter_pack_p = true;
3578 break;
3579 default:
3580 /* Not a parameter pack. */
3581 break;
3584 if (parameter_pack_p)
3586 /* Add this parameter pack to the list. */
3587 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3590 if (TYPE_P (t))
3591 cp_walk_tree (&TYPE_CONTEXT (t),
3592 &find_parameter_packs_r, ppd, ppd->visited);
3594 /* This switch statement will return immediately if we don't find a
3595 parameter pack. */
3596 switch (TREE_CODE (t))
3598 case TEMPLATE_PARM_INDEX:
3599 return NULL_TREE;
3601 case BOUND_TEMPLATE_TEMPLATE_PARM:
3602 /* Check the template itself. */
3603 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3604 &find_parameter_packs_r, ppd, ppd->visited);
3605 /* Check the template arguments. */
3606 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3607 ppd->visited);
3608 *walk_subtrees = 0;
3609 return NULL_TREE;
3611 case TEMPLATE_TYPE_PARM:
3612 case TEMPLATE_TEMPLATE_PARM:
3613 return NULL_TREE;
3615 case PARM_DECL:
3616 return NULL_TREE;
3618 case RECORD_TYPE:
3619 if (TYPE_PTRMEMFUNC_P (t))
3620 return NULL_TREE;
3621 /* Fall through. */
3623 case UNION_TYPE:
3624 case ENUMERAL_TYPE:
3625 if (TYPE_TEMPLATE_INFO (t))
3626 cp_walk_tree (&TYPE_TI_ARGS (t),
3627 &find_parameter_packs_r, ppd, ppd->visited);
3629 *walk_subtrees = 0;
3630 return NULL_TREE;
3632 case TEMPLATE_DECL:
3633 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3634 return NULL_TREE;
3635 gcc_fallthrough();
3637 case CONSTRUCTOR:
3638 cp_walk_tree (&TREE_TYPE (t),
3639 &find_parameter_packs_r, ppd, ppd->visited);
3640 return NULL_TREE;
3642 case TYPENAME_TYPE:
3643 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3644 ppd, ppd->visited);
3645 *walk_subtrees = 0;
3646 return NULL_TREE;
3648 case TYPE_PACK_EXPANSION:
3649 case EXPR_PACK_EXPANSION:
3650 *walk_subtrees = 0;
3651 return NULL_TREE;
3653 case INTEGER_TYPE:
3654 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3655 ppd, ppd->visited);
3656 *walk_subtrees = 0;
3657 return NULL_TREE;
3659 case IDENTIFIER_NODE:
3660 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3661 ppd->visited);
3662 *walk_subtrees = 0;
3663 return NULL_TREE;
3665 case DECLTYPE_TYPE:
3667 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3668 type_pack_expansion_p to false so that any placeholders
3669 within the expression don't get marked as parameter packs. */
3670 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3671 ppd->type_pack_expansion_p = false;
3672 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3673 ppd, ppd->visited);
3674 ppd->type_pack_expansion_p = type_pack_expansion_p;
3675 *walk_subtrees = 0;
3676 return NULL_TREE;
3679 default:
3680 return NULL_TREE;
3683 return NULL_TREE;
3686 /* Determines if the expression or type T uses any parameter packs. */
3687 bool
3688 uses_parameter_packs (tree t)
3690 tree parameter_packs = NULL_TREE;
3691 struct find_parameter_pack_data ppd;
3692 ppd.parameter_packs = &parameter_packs;
3693 ppd.visited = new hash_set<tree>;
3694 ppd.type_pack_expansion_p = false;
3695 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3696 delete ppd.visited;
3697 return parameter_packs != NULL_TREE;
3700 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3701 representation a base-class initializer into a parameter pack
3702 expansion. If all goes well, the resulting node will be an
3703 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3704 respectively. */
3705 tree
3706 make_pack_expansion (tree arg)
3708 tree result;
3709 tree parameter_packs = NULL_TREE;
3710 bool for_types = false;
3711 struct find_parameter_pack_data ppd;
3713 if (!arg || arg == error_mark_node)
3714 return arg;
3716 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3718 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3719 class initializer. In this case, the TREE_PURPOSE will be a
3720 _TYPE node (representing the base class expansion we're
3721 initializing) and the TREE_VALUE will be a TREE_LIST
3722 containing the initialization arguments.
3724 The resulting expansion looks somewhat different from most
3725 expansions. Rather than returning just one _EXPANSION, we
3726 return a TREE_LIST whose TREE_PURPOSE is a
3727 TYPE_PACK_EXPANSION containing the bases that will be
3728 initialized. The TREE_VALUE will be identical to the
3729 original TREE_VALUE, which is a list of arguments that will
3730 be passed to each base. We do not introduce any new pack
3731 expansion nodes into the TREE_VALUE (although it is possible
3732 that some already exist), because the TREE_PURPOSE and
3733 TREE_VALUE all need to be expanded together with the same
3734 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3735 resulting TREE_PURPOSE will mention the parameter packs in
3736 both the bases and the arguments to the bases. */
3737 tree purpose;
3738 tree value;
3739 tree parameter_packs = NULL_TREE;
3741 /* Determine which parameter packs will be used by the base
3742 class expansion. */
3743 ppd.visited = new hash_set<tree>;
3744 ppd.parameter_packs = &parameter_packs;
3745 ppd.type_pack_expansion_p = true;
3746 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3747 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3748 &ppd, ppd.visited);
3750 if (parameter_packs == NULL_TREE)
3752 error ("base initializer expansion %qT contains no parameter packs", arg);
3753 delete ppd.visited;
3754 return error_mark_node;
3757 if (TREE_VALUE (arg) != void_type_node)
3759 /* Collect the sets of parameter packs used in each of the
3760 initialization arguments. */
3761 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3763 /* Determine which parameter packs will be expanded in this
3764 argument. */
3765 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3766 &ppd, ppd.visited);
3770 delete ppd.visited;
3772 /* Create the pack expansion type for the base type. */
3773 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3774 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3775 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3777 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3778 they will rarely be compared to anything. */
3779 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3781 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3784 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3785 for_types = true;
3787 /* Build the PACK_EXPANSION_* node. */
3788 result = for_types
3789 ? cxx_make_type (TYPE_PACK_EXPANSION)
3790 : make_node (EXPR_PACK_EXPANSION);
3791 SET_PACK_EXPANSION_PATTERN (result, arg);
3792 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3794 /* Propagate type and const-expression information. */
3795 TREE_TYPE (result) = TREE_TYPE (arg);
3796 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3797 /* Mark this read now, since the expansion might be length 0. */
3798 mark_exp_read (arg);
3800 else
3801 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3802 they will rarely be compared to anything. */
3803 SET_TYPE_STRUCTURAL_EQUALITY (result);
3805 /* Determine which parameter packs will be expanded. */
3806 ppd.parameter_packs = &parameter_packs;
3807 ppd.visited = new hash_set<tree>;
3808 ppd.type_pack_expansion_p = TYPE_P (arg);
3809 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3810 delete ppd.visited;
3812 /* Make sure we found some parameter packs. */
3813 if (parameter_packs == NULL_TREE)
3815 if (TYPE_P (arg))
3816 error ("expansion pattern %qT contains no argument packs", arg);
3817 else
3818 error ("expansion pattern %qE contains no argument packs", arg);
3819 return error_mark_node;
3821 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3823 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3825 return result;
3828 /* Checks T for any "bare" parameter packs, which have not yet been
3829 expanded, and issues an error if any are found. This operation can
3830 only be done on full expressions or types (e.g., an expression
3831 statement, "if" condition, etc.), because we could have expressions like:
3833 foo(f(g(h(args)))...)
3835 where "args" is a parameter pack. check_for_bare_parameter_packs
3836 should not be called for the subexpressions args, h(args),
3837 g(h(args)), or f(g(h(args))), because we would produce erroneous
3838 error messages.
3840 Returns TRUE and emits an error if there were bare parameter packs,
3841 returns FALSE otherwise. */
3842 bool
3843 check_for_bare_parameter_packs (tree t)
3845 tree parameter_packs = NULL_TREE;
3846 struct find_parameter_pack_data ppd;
3848 if (!processing_template_decl || !t || t == error_mark_node)
3849 return false;
3851 if (TREE_CODE (t) == TYPE_DECL)
3852 t = TREE_TYPE (t);
3854 ppd.parameter_packs = &parameter_packs;
3855 ppd.visited = new hash_set<tree>;
3856 ppd.type_pack_expansion_p = false;
3857 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3858 delete ppd.visited;
3860 if (parameter_packs)
3862 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3863 error_at (loc, "parameter packs not expanded with %<...%>:");
3864 while (parameter_packs)
3866 tree pack = TREE_VALUE (parameter_packs);
3867 tree name = NULL_TREE;
3869 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3870 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3871 name = TYPE_NAME (pack);
3872 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3873 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3874 else if (TREE_CODE (pack) == CALL_EXPR)
3875 name = DECL_NAME (CALL_EXPR_FN (pack));
3876 else
3877 name = DECL_NAME (pack);
3879 if (name)
3880 inform (loc, " %qD", name);
3881 else
3882 inform (loc, " <anonymous>");
3884 parameter_packs = TREE_CHAIN (parameter_packs);
3887 return true;
3890 return false;
3893 /* Expand any parameter packs that occur in the template arguments in
3894 ARGS. */
3895 tree
3896 expand_template_argument_pack (tree args)
3898 if (args == error_mark_node)
3899 return error_mark_node;
3901 tree result_args = NULL_TREE;
3902 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3903 int num_result_args = -1;
3904 int non_default_args_count = -1;
3906 /* First, determine if we need to expand anything, and the number of
3907 slots we'll need. */
3908 for (in_arg = 0; in_arg < nargs; ++in_arg)
3910 tree arg = TREE_VEC_ELT (args, in_arg);
3911 if (arg == NULL_TREE)
3912 return args;
3913 if (ARGUMENT_PACK_P (arg))
3915 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3916 if (num_result_args < 0)
3917 num_result_args = in_arg + num_packed;
3918 else
3919 num_result_args += num_packed;
3921 else
3923 if (num_result_args >= 0)
3924 num_result_args++;
3928 /* If no expansion is necessary, we're done. */
3929 if (num_result_args < 0)
3930 return args;
3932 /* Expand arguments. */
3933 result_args = make_tree_vec (num_result_args);
3934 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3935 non_default_args_count =
3936 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3937 for (in_arg = 0; in_arg < nargs; ++in_arg)
3939 tree arg = TREE_VEC_ELT (args, in_arg);
3940 if (ARGUMENT_PACK_P (arg))
3942 tree packed = ARGUMENT_PACK_ARGS (arg);
3943 int i, num_packed = TREE_VEC_LENGTH (packed);
3944 for (i = 0; i < num_packed; ++i, ++out_arg)
3945 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3946 if (non_default_args_count > 0)
3947 non_default_args_count += num_packed - 1;
3949 else
3951 TREE_VEC_ELT (result_args, out_arg) = arg;
3952 ++out_arg;
3955 if (non_default_args_count >= 0)
3956 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3957 return result_args;
3960 /* Checks if DECL shadows a template parameter.
3962 [temp.local]: A template-parameter shall not be redeclared within its
3963 scope (including nested scopes).
3965 Emits an error and returns TRUE if the DECL shadows a parameter,
3966 returns FALSE otherwise. */
3968 bool
3969 check_template_shadow (tree decl)
3971 tree olddecl;
3973 /* If we're not in a template, we can't possibly shadow a template
3974 parameter. */
3975 if (!current_template_parms)
3976 return true;
3978 /* Figure out what we're shadowing. */
3979 decl = OVL_FIRST (decl);
3980 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3982 /* If there's no previous binding for this name, we're not shadowing
3983 anything, let alone a template parameter. */
3984 if (!olddecl)
3985 return true;
3987 /* If we're not shadowing a template parameter, we're done. Note
3988 that OLDDECL might be an OVERLOAD (or perhaps even an
3989 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3990 node. */
3991 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3992 return true;
3994 /* We check for decl != olddecl to avoid bogus errors for using a
3995 name inside a class. We check TPFI to avoid duplicate errors for
3996 inline member templates. */
3997 if (decl == olddecl
3998 || (DECL_TEMPLATE_PARM_P (decl)
3999 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4000 return true;
4002 /* Don't complain about the injected class name, as we've already
4003 complained about the class itself. */
4004 if (DECL_SELF_REFERENCE_P (decl))
4005 return false;
4007 if (DECL_TEMPLATE_PARM_P (decl))
4008 error ("declaration of template parameter %q+D shadows "
4009 "template parameter", decl);
4010 else
4011 error ("declaration of %q+#D shadows template parameter", decl);
4012 inform (DECL_SOURCE_LOCATION (olddecl),
4013 "template parameter %qD declared here", olddecl);
4014 return false;
4017 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4018 ORIG_LEVEL, DECL, and TYPE. */
4020 static tree
4021 build_template_parm_index (int index,
4022 int level,
4023 int orig_level,
4024 tree decl,
4025 tree type)
4027 tree t = make_node (TEMPLATE_PARM_INDEX);
4028 TEMPLATE_PARM_IDX (t) = index;
4029 TEMPLATE_PARM_LEVEL (t) = level;
4030 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4031 TEMPLATE_PARM_DECL (t) = decl;
4032 TREE_TYPE (t) = type;
4033 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4034 TREE_READONLY (t) = TREE_READONLY (decl);
4036 return t;
4039 /* Find the canonical type parameter for the given template type
4040 parameter. Returns the canonical type parameter, which may be TYPE
4041 if no such parameter existed. */
4043 static tree
4044 canonical_type_parameter (tree type)
4046 tree list;
4047 int idx = TEMPLATE_TYPE_IDX (type);
4048 if (!canonical_template_parms)
4049 vec_alloc (canonical_template_parms, idx + 1);
4051 if (canonical_template_parms->length () <= (unsigned) idx)
4052 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4054 list = (*canonical_template_parms)[idx];
4055 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4056 list = TREE_CHAIN (list);
4058 if (list)
4059 return TREE_VALUE (list);
4060 else
4062 (*canonical_template_parms)[idx]
4063 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4064 return type;
4068 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4069 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4070 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4071 new one is created. */
4073 static tree
4074 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4075 tsubst_flags_t complain)
4077 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4078 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4079 != TEMPLATE_PARM_LEVEL (index) - levels)
4080 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4082 tree orig_decl = TEMPLATE_PARM_DECL (index);
4083 tree decl, t;
4085 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4086 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4087 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4088 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4089 DECL_ARTIFICIAL (decl) = 1;
4090 SET_DECL_TEMPLATE_PARM_P (decl);
4092 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4093 TEMPLATE_PARM_LEVEL (index) - levels,
4094 TEMPLATE_PARM_ORIG_LEVEL (index),
4095 decl, type);
4096 TEMPLATE_PARM_DESCENDANTS (index) = t;
4097 TEMPLATE_PARM_PARAMETER_PACK (t)
4098 = TEMPLATE_PARM_PARAMETER_PACK (index);
4100 /* Template template parameters need this. */
4101 if (TREE_CODE (decl) == TEMPLATE_DECL)
4103 DECL_TEMPLATE_RESULT (decl)
4104 = build_decl (DECL_SOURCE_LOCATION (decl),
4105 TYPE_DECL, DECL_NAME (decl), type);
4106 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4107 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4108 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4112 return TEMPLATE_PARM_DESCENDANTS (index);
4115 /* Process information from new template parameter PARM and append it
4116 to the LIST being built. This new parameter is a non-type
4117 parameter iff IS_NON_TYPE is true. This new parameter is a
4118 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4119 is in PARM_LOC. */
4121 tree
4122 process_template_parm (tree list, location_t parm_loc, tree parm,
4123 bool is_non_type, bool is_parameter_pack)
4125 tree decl = 0;
4126 int idx = 0;
4128 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4129 tree defval = TREE_PURPOSE (parm);
4130 tree constr = TREE_TYPE (parm);
4132 if (list)
4134 tree p = tree_last (list);
4136 if (p && TREE_VALUE (p) != error_mark_node)
4138 p = TREE_VALUE (p);
4139 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4140 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4141 else
4142 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4145 ++idx;
4148 if (is_non_type)
4150 parm = TREE_VALUE (parm);
4152 SET_DECL_TEMPLATE_PARM_P (parm);
4154 if (TREE_TYPE (parm) != error_mark_node)
4156 /* [temp.param]
4158 The top-level cv-qualifiers on the template-parameter are
4159 ignored when determining its type. */
4160 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4161 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4162 TREE_TYPE (parm) = error_mark_node;
4163 else if (uses_parameter_packs (TREE_TYPE (parm))
4164 && !is_parameter_pack
4165 /* If we're in a nested template parameter list, the template
4166 template parameter could be a parameter pack. */
4167 && processing_template_parmlist == 1)
4169 /* This template parameter is not a parameter pack, but it
4170 should be. Complain about "bare" parameter packs. */
4171 check_for_bare_parameter_packs (TREE_TYPE (parm));
4173 /* Recover by calling this a parameter pack. */
4174 is_parameter_pack = true;
4178 /* A template parameter is not modifiable. */
4179 TREE_CONSTANT (parm) = 1;
4180 TREE_READONLY (parm) = 1;
4181 decl = build_decl (parm_loc,
4182 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4183 TREE_CONSTANT (decl) = 1;
4184 TREE_READONLY (decl) = 1;
4185 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4186 = build_template_parm_index (idx, processing_template_decl,
4187 processing_template_decl,
4188 decl, TREE_TYPE (parm));
4190 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4191 = is_parameter_pack;
4193 else
4195 tree t;
4196 parm = TREE_VALUE (TREE_VALUE (parm));
4198 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4200 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4201 /* This is for distinguishing between real templates and template
4202 template parameters */
4203 TREE_TYPE (parm) = t;
4204 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4205 decl = parm;
4207 else
4209 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4210 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4211 decl = build_decl (parm_loc,
4212 TYPE_DECL, parm, t);
4215 TYPE_NAME (t) = decl;
4216 TYPE_STUB_DECL (t) = decl;
4217 parm = decl;
4218 TEMPLATE_TYPE_PARM_INDEX (t)
4219 = build_template_parm_index (idx, processing_template_decl,
4220 processing_template_decl,
4221 decl, TREE_TYPE (parm));
4222 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4223 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4225 DECL_ARTIFICIAL (decl) = 1;
4226 SET_DECL_TEMPLATE_PARM_P (decl);
4228 /* Build requirements for the type/template parameter.
4229 This must be done after SET_DECL_TEMPLATE_PARM_P or
4230 process_template_parm could fail. */
4231 tree reqs = finish_shorthand_constraint (parm, constr);
4233 pushdecl (decl);
4235 /* Build the parameter node linking the parameter declaration,
4236 its default argument (if any), and its constraints (if any). */
4237 parm = build_tree_list (defval, parm);
4238 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4240 return chainon (list, parm);
4243 /* The end of a template parameter list has been reached. Process the
4244 tree list into a parameter vector, converting each parameter into a more
4245 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4246 as PARM_DECLs. */
4248 tree
4249 end_template_parm_list (tree parms)
4251 int nparms;
4252 tree parm, next;
4253 tree saved_parmlist = make_tree_vec (list_length (parms));
4255 /* Pop the dummy parameter level and add the real one. */
4256 current_template_parms = TREE_CHAIN (current_template_parms);
4258 current_template_parms
4259 = tree_cons (size_int (processing_template_decl),
4260 saved_parmlist, current_template_parms);
4262 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4264 next = TREE_CHAIN (parm);
4265 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4266 TREE_CHAIN (parm) = NULL_TREE;
4269 --processing_template_parmlist;
4271 return saved_parmlist;
4274 // Explicitly indicate the end of the template parameter list. We assume
4275 // that the current template parameters have been constructed and/or
4276 // managed explicitly, as when creating new template template parameters
4277 // from a shorthand constraint.
4278 void
4279 end_template_parm_list ()
4281 --processing_template_parmlist;
4284 /* end_template_decl is called after a template declaration is seen. */
4286 void
4287 end_template_decl (void)
4289 reset_specialization ();
4291 if (! processing_template_decl)
4292 return;
4294 /* This matches the pushlevel in begin_template_parm_list. */
4295 finish_scope ();
4297 --processing_template_decl;
4298 current_template_parms = TREE_CHAIN (current_template_parms);
4301 /* Takes a TREE_LIST representing a template parameter and convert it
4302 into an argument suitable to be passed to the type substitution
4303 functions. Note that If the TREE_LIST contains an error_mark
4304 node, the returned argument is error_mark_node. */
4306 tree
4307 template_parm_to_arg (tree t)
4310 if (t == NULL_TREE
4311 || TREE_CODE (t) != TREE_LIST)
4312 return t;
4314 if (error_operand_p (TREE_VALUE (t)))
4315 return error_mark_node;
4317 t = TREE_VALUE (t);
4319 if (TREE_CODE (t) == TYPE_DECL
4320 || TREE_CODE (t) == TEMPLATE_DECL)
4322 t = TREE_TYPE (t);
4324 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4326 /* Turn this argument into a TYPE_ARGUMENT_PACK
4327 with a single element, which expands T. */
4328 tree vec = make_tree_vec (1);
4329 if (CHECKING_P)
4330 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4332 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4334 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4335 SET_ARGUMENT_PACK_ARGS (t, vec);
4338 else
4340 t = DECL_INITIAL (t);
4342 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4344 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4345 with a single element, which expands T. */
4346 tree vec = make_tree_vec (1);
4347 if (CHECKING_P)
4348 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4350 t = convert_from_reference (t);
4351 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4353 t = make_node (NONTYPE_ARGUMENT_PACK);
4354 SET_ARGUMENT_PACK_ARGS (t, vec);
4356 else
4357 t = convert_from_reference (t);
4359 return t;
4362 /* Given a single level of template parameters (a TREE_VEC), return it
4363 as a set of template arguments. */
4365 static tree
4366 template_parms_level_to_args (tree parms)
4368 tree a = copy_node (parms);
4369 TREE_TYPE (a) = NULL_TREE;
4370 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4371 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4373 if (CHECKING_P)
4374 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4376 return a;
4379 /* Given a set of template parameters, return them as a set of template
4380 arguments. The template parameters are represented as a TREE_VEC, in
4381 the form documented in cp-tree.h for template arguments. */
4383 static tree
4384 template_parms_to_args (tree parms)
4386 tree header;
4387 tree args = NULL_TREE;
4388 int length = TMPL_PARMS_DEPTH (parms);
4389 int l = length;
4391 /* If there is only one level of template parameters, we do not
4392 create a TREE_VEC of TREE_VECs. Instead, we return a single
4393 TREE_VEC containing the arguments. */
4394 if (length > 1)
4395 args = make_tree_vec (length);
4397 for (header = parms; header; header = TREE_CHAIN (header))
4399 tree a = template_parms_level_to_args (TREE_VALUE (header));
4401 if (length > 1)
4402 TREE_VEC_ELT (args, --l) = a;
4403 else
4404 args = a;
4407 return args;
4410 /* Within the declaration of a template, return the currently active
4411 template parameters as an argument TREE_VEC. */
4413 static tree
4414 current_template_args (void)
4416 return template_parms_to_args (current_template_parms);
4419 /* Update the declared TYPE by doing any lookups which were thought to be
4420 dependent, but are not now that we know the SCOPE of the declarator. */
4422 tree
4423 maybe_update_decl_type (tree orig_type, tree scope)
4425 tree type = orig_type;
4427 if (type == NULL_TREE)
4428 return type;
4430 if (TREE_CODE (orig_type) == TYPE_DECL)
4431 type = TREE_TYPE (type);
4433 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4434 && dependent_type_p (type)
4435 /* Don't bother building up the args in this case. */
4436 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4438 /* tsubst in the args corresponding to the template parameters,
4439 including auto if present. Most things will be unchanged, but
4440 make_typename_type and tsubst_qualified_id will resolve
4441 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4442 tree args = current_template_args ();
4443 tree auto_node = type_uses_auto (type);
4444 tree pushed;
4445 if (auto_node)
4447 tree auto_vec = make_tree_vec (1);
4448 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4449 args = add_to_template_args (args, auto_vec);
4451 pushed = push_scope (scope);
4452 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4453 if (pushed)
4454 pop_scope (scope);
4457 if (type == error_mark_node)
4458 return orig_type;
4460 if (TREE_CODE (orig_type) == TYPE_DECL)
4462 if (same_type_p (type, TREE_TYPE (orig_type)))
4463 type = orig_type;
4464 else
4465 type = TYPE_NAME (type);
4467 return type;
4470 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4471 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4472 the new template is a member template. */
4474 tree
4475 build_template_decl (tree decl, tree parms, bool member_template_p)
4477 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4478 DECL_TEMPLATE_PARMS (tmpl) = parms;
4479 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4480 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4481 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4483 return tmpl;
4486 struct template_parm_data
4488 /* The level of the template parameters we are currently
4489 processing. */
4490 int level;
4492 /* The index of the specialization argument we are currently
4493 processing. */
4494 int current_arg;
4496 /* An array whose size is the number of template parameters. The
4497 elements are nonzero if the parameter has been used in any one
4498 of the arguments processed so far. */
4499 int* parms;
4501 /* An array whose size is the number of template arguments. The
4502 elements are nonzero if the argument makes use of template
4503 parameters of this level. */
4504 int* arg_uses_template_parms;
4507 /* Subroutine of push_template_decl used to see if each template
4508 parameter in a partial specialization is used in the explicit
4509 argument list. If T is of the LEVEL given in DATA (which is
4510 treated as a template_parm_data*), then DATA->PARMS is marked
4511 appropriately. */
4513 static int
4514 mark_template_parm (tree t, void* data)
4516 int level;
4517 int idx;
4518 struct template_parm_data* tpd = (struct template_parm_data*) data;
4520 template_parm_level_and_index (t, &level, &idx);
4522 if (level == tpd->level)
4524 tpd->parms[idx] = 1;
4525 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4528 /* In C++17 the type of a non-type argument is a deduced context. */
4529 if (cxx_dialect >= cxx1z
4530 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4531 for_each_template_parm (TREE_TYPE (t),
4532 &mark_template_parm,
4533 data,
4534 NULL,
4535 /*include_nondeduced_p=*/false);
4537 /* Return zero so that for_each_template_parm will continue the
4538 traversal of the tree; we want to mark *every* template parm. */
4539 return 0;
4542 /* Process the partial specialization DECL. */
4544 static tree
4545 process_partial_specialization (tree decl)
4547 tree type = TREE_TYPE (decl);
4548 tree tinfo = get_template_info (decl);
4549 tree maintmpl = TI_TEMPLATE (tinfo);
4550 tree specargs = TI_ARGS (tinfo);
4551 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4552 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4553 tree inner_parms;
4554 tree inst;
4555 int nargs = TREE_VEC_LENGTH (inner_args);
4556 int ntparms;
4557 int i;
4558 bool did_error_intro = false;
4559 struct template_parm_data tpd;
4560 struct template_parm_data tpd2;
4562 gcc_assert (current_template_parms);
4564 /* A concept cannot be specialized. */
4565 if (flag_concepts && variable_concept_p (maintmpl))
4567 error ("specialization of variable concept %q#D", maintmpl);
4568 return error_mark_node;
4571 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4572 ntparms = TREE_VEC_LENGTH (inner_parms);
4574 /* We check that each of the template parameters given in the
4575 partial specialization is used in the argument list to the
4576 specialization. For example:
4578 template <class T> struct S;
4579 template <class T> struct S<T*>;
4581 The second declaration is OK because `T*' uses the template
4582 parameter T, whereas
4584 template <class T> struct S<int>;
4586 is no good. Even trickier is:
4588 template <class T>
4589 struct S1
4591 template <class U>
4592 struct S2;
4593 template <class U>
4594 struct S2<T>;
4597 The S2<T> declaration is actually invalid; it is a
4598 full-specialization. Of course,
4600 template <class U>
4601 struct S2<T (*)(U)>;
4603 or some such would have been OK. */
4604 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4605 tpd.parms = XALLOCAVEC (int, ntparms);
4606 memset (tpd.parms, 0, sizeof (int) * ntparms);
4608 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4609 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4610 for (i = 0; i < nargs; ++i)
4612 tpd.current_arg = i;
4613 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4614 &mark_template_parm,
4615 &tpd,
4616 NULL,
4617 /*include_nondeduced_p=*/false);
4619 for (i = 0; i < ntparms; ++i)
4620 if (tpd.parms[i] == 0)
4622 /* One of the template parms was not used in a deduced context in the
4623 specialization. */
4624 if (!did_error_intro)
4626 error ("template parameters not deducible in "
4627 "partial specialization:");
4628 did_error_intro = true;
4631 inform (input_location, " %qD",
4632 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4635 if (did_error_intro)
4636 return error_mark_node;
4638 /* [temp.class.spec]
4640 The argument list of the specialization shall not be identical to
4641 the implicit argument list of the primary template. */
4642 tree main_args
4643 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4644 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4645 && (!flag_concepts
4646 || !strictly_subsumes (current_template_constraints (),
4647 get_constraints (maintmpl))))
4649 if (!flag_concepts)
4650 error ("partial specialization %q+D does not specialize "
4651 "any template arguments", decl);
4652 else
4653 error ("partial specialization %q+D does not specialize any "
4654 "template arguments and is not more constrained than", decl);
4655 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4658 /* A partial specialization that replaces multiple parameters of the
4659 primary template with a pack expansion is less specialized for those
4660 parameters. */
4661 if (nargs < DECL_NTPARMS (maintmpl))
4663 error ("partial specialization is not more specialized than the "
4664 "primary template because it replaces multiple parameters "
4665 "with a pack expansion");
4666 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4667 /* Avoid crash in process_partial_specialization. */
4668 return decl;
4671 /* If we aren't in a dependent class, we can actually try deduction. */
4672 else if (tpd.level == 1
4673 /* FIXME we should be able to handle a partial specialization of a
4674 partial instantiation, but currently we can't (c++/41727). */
4675 && TMPL_ARGS_DEPTH (specargs) == 1
4676 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4678 if (permerror (input_location, "partial specialization %qD is not "
4679 "more specialized than", decl))
4680 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4681 maintmpl);
4684 /* [temp.class.spec]
4686 A partially specialized non-type argument expression shall not
4687 involve template parameters of the partial specialization except
4688 when the argument expression is a simple identifier.
4690 The type of a template parameter corresponding to a specialized
4691 non-type argument shall not be dependent on a parameter of the
4692 specialization.
4694 Also, we verify that pack expansions only occur at the
4695 end of the argument list. */
4696 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4697 tpd2.parms = 0;
4698 for (i = 0; i < nargs; ++i)
4700 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4701 tree arg = TREE_VEC_ELT (inner_args, i);
4702 tree packed_args = NULL_TREE;
4703 int j, len = 1;
4705 if (ARGUMENT_PACK_P (arg))
4707 /* Extract the arguments from the argument pack. We'll be
4708 iterating over these in the following loop. */
4709 packed_args = ARGUMENT_PACK_ARGS (arg);
4710 len = TREE_VEC_LENGTH (packed_args);
4713 for (j = 0; j < len; j++)
4715 if (packed_args)
4716 /* Get the Jth argument in the parameter pack. */
4717 arg = TREE_VEC_ELT (packed_args, j);
4719 if (PACK_EXPANSION_P (arg))
4721 /* Pack expansions must come at the end of the
4722 argument list. */
4723 if ((packed_args && j < len - 1)
4724 || (!packed_args && i < nargs - 1))
4726 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4727 error ("parameter pack argument %qE must be at the "
4728 "end of the template argument list", arg);
4729 else
4730 error ("parameter pack argument %qT must be at the "
4731 "end of the template argument list", arg);
4735 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4736 /* We only care about the pattern. */
4737 arg = PACK_EXPANSION_PATTERN (arg);
4739 if (/* These first two lines are the `non-type' bit. */
4740 !TYPE_P (arg)
4741 && TREE_CODE (arg) != TEMPLATE_DECL
4742 /* This next two lines are the `argument expression is not just a
4743 simple identifier' condition and also the `specialized
4744 non-type argument' bit. */
4745 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4746 && !(REFERENCE_REF_P (arg)
4747 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4749 if ((!packed_args && tpd.arg_uses_template_parms[i])
4750 || (packed_args && uses_template_parms (arg)))
4751 error ("template argument %qE involves template parameter(s)",
4752 arg);
4753 else
4755 /* Look at the corresponding template parameter,
4756 marking which template parameters its type depends
4757 upon. */
4758 tree type = TREE_TYPE (parm);
4760 if (!tpd2.parms)
4762 /* We haven't yet initialized TPD2. Do so now. */
4763 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4764 /* The number of parameters here is the number in the
4765 main template, which, as checked in the assertion
4766 above, is NARGS. */
4767 tpd2.parms = XALLOCAVEC (int, nargs);
4768 tpd2.level =
4769 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4772 /* Mark the template parameters. But this time, we're
4773 looking for the template parameters of the main
4774 template, not in the specialization. */
4775 tpd2.current_arg = i;
4776 tpd2.arg_uses_template_parms[i] = 0;
4777 memset (tpd2.parms, 0, sizeof (int) * nargs);
4778 for_each_template_parm (type,
4779 &mark_template_parm,
4780 &tpd2,
4781 NULL,
4782 /*include_nondeduced_p=*/false);
4784 if (tpd2.arg_uses_template_parms [i])
4786 /* The type depended on some template parameters.
4787 If they are fully specialized in the
4788 specialization, that's OK. */
4789 int j;
4790 int count = 0;
4791 for (j = 0; j < nargs; ++j)
4792 if (tpd2.parms[j] != 0
4793 && tpd.arg_uses_template_parms [j])
4794 ++count;
4795 if (count != 0)
4796 error_n (input_location, count,
4797 "type %qT of template argument %qE depends "
4798 "on a template parameter",
4799 "type %qT of template argument %qE depends "
4800 "on template parameters",
4801 type,
4802 arg);
4809 /* We should only get here once. */
4810 if (TREE_CODE (decl) == TYPE_DECL)
4811 gcc_assert (!COMPLETE_TYPE_P (type));
4813 // Build the template decl.
4814 tree tmpl = build_template_decl (decl, current_template_parms,
4815 DECL_MEMBER_TEMPLATE_P (maintmpl));
4816 TREE_TYPE (tmpl) = type;
4817 DECL_TEMPLATE_RESULT (tmpl) = decl;
4818 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4819 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4820 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4822 /* Give template template parms a DECL_CONTEXT of the template
4823 for which they are a parameter. */
4824 for (i = 0; i < ntparms; ++i)
4826 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4827 if (TREE_CODE (parm) == TEMPLATE_DECL)
4828 DECL_CONTEXT (parm) = tmpl;
4831 if (VAR_P (decl))
4832 /* We didn't register this in check_explicit_specialization so we could
4833 wait until the constraints were set. */
4834 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4835 else
4836 associate_classtype_constraints (type);
4838 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4839 = tree_cons (specargs, tmpl,
4840 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4841 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4843 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4844 inst = TREE_CHAIN (inst))
4846 tree instance = TREE_VALUE (inst);
4847 if (TYPE_P (instance)
4848 ? (COMPLETE_TYPE_P (instance)
4849 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4850 : DECL_TEMPLATE_INSTANTIATION (instance))
4852 tree spec = most_specialized_partial_spec (instance, tf_none);
4853 tree inst_decl = (DECL_P (instance)
4854 ? instance : TYPE_NAME (instance));
4855 if (!spec)
4856 /* OK */;
4857 else if (spec == error_mark_node)
4858 permerror (input_location,
4859 "declaration of %qD ambiguates earlier template "
4860 "instantiation for %qD", decl, inst_decl);
4861 else if (TREE_VALUE (spec) == tmpl)
4862 permerror (input_location,
4863 "partial specialization of %qD after instantiation "
4864 "of %qD", decl, inst_decl);
4868 return decl;
4871 /* PARM is a template parameter of some form; return the corresponding
4872 TEMPLATE_PARM_INDEX. */
4874 static tree
4875 get_template_parm_index (tree parm)
4877 if (TREE_CODE (parm) == PARM_DECL
4878 || TREE_CODE (parm) == CONST_DECL)
4879 parm = DECL_INITIAL (parm);
4880 else if (TREE_CODE (parm) == TYPE_DECL
4881 || TREE_CODE (parm) == TEMPLATE_DECL)
4882 parm = TREE_TYPE (parm);
4883 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4884 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4885 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4886 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4887 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4888 return parm;
4891 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4892 parameter packs used by the template parameter PARM. */
4894 static void
4895 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4897 /* A type parm can't refer to another parm. */
4898 if (TREE_CODE (parm) == TYPE_DECL)
4899 return;
4900 else if (TREE_CODE (parm) == PARM_DECL)
4902 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4903 ppd, ppd->visited);
4904 return;
4907 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4909 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4910 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4911 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4914 /* PARM is a template parameter pack. Return any parameter packs used in
4915 its type or the type of any of its template parameters. If there are
4916 any such packs, it will be instantiated into a fixed template parameter
4917 list by partial instantiation rather than be fully deduced. */
4919 tree
4920 fixed_parameter_pack_p (tree parm)
4922 /* This can only be true in a member template. */
4923 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4924 return NULL_TREE;
4925 /* This can only be true for a parameter pack. */
4926 if (!template_parameter_pack_p (parm))
4927 return NULL_TREE;
4928 /* A type parm can't refer to another parm. */
4929 if (TREE_CODE (parm) == TYPE_DECL)
4930 return NULL_TREE;
4932 tree parameter_packs = NULL_TREE;
4933 struct find_parameter_pack_data ppd;
4934 ppd.parameter_packs = &parameter_packs;
4935 ppd.visited = new hash_set<tree>;
4936 ppd.type_pack_expansion_p = false;
4938 fixed_parameter_pack_p_1 (parm, &ppd);
4940 delete ppd.visited;
4941 return parameter_packs;
4944 /* Check that a template declaration's use of default arguments and
4945 parameter packs is not invalid. Here, PARMS are the template
4946 parameters. IS_PRIMARY is true if DECL is the thing declared by
4947 a primary template. IS_PARTIAL is true if DECL is a partial
4948 specialization.
4950 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4951 declaration (but not a definition); 1 indicates a declaration, 2
4952 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4953 emitted for extraneous default arguments.
4955 Returns TRUE if there were no errors found, FALSE otherwise. */
4957 bool
4958 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4959 bool is_partial, int is_friend_decl)
4961 const char *msg;
4962 int last_level_to_check;
4963 tree parm_level;
4964 bool no_errors = true;
4966 /* [temp.param]
4968 A default template-argument shall not be specified in a
4969 function template declaration or a function template definition, nor
4970 in the template-parameter-list of the definition of a member of a
4971 class template. */
4973 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4974 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4975 /* You can't have a function template declaration in a local
4976 scope, nor you can you define a member of a class template in a
4977 local scope. */
4978 return true;
4980 if ((TREE_CODE (decl) == TYPE_DECL
4981 && TREE_TYPE (decl)
4982 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4983 || (TREE_CODE (decl) == FUNCTION_DECL
4984 && LAMBDA_FUNCTION_P (decl)))
4985 /* A lambda doesn't have an explicit declaration; don't complain
4986 about the parms of the enclosing class. */
4987 return true;
4989 if (current_class_type
4990 && !TYPE_BEING_DEFINED (current_class_type)
4991 && DECL_LANG_SPECIFIC (decl)
4992 && DECL_DECLARES_FUNCTION_P (decl)
4993 /* If this is either a friend defined in the scope of the class
4994 or a member function. */
4995 && (DECL_FUNCTION_MEMBER_P (decl)
4996 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4997 : DECL_FRIEND_CONTEXT (decl)
4998 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4999 : false)
5000 /* And, if it was a member function, it really was defined in
5001 the scope of the class. */
5002 && (!DECL_FUNCTION_MEMBER_P (decl)
5003 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5004 /* We already checked these parameters when the template was
5005 declared, so there's no need to do it again now. This function
5006 was defined in class scope, but we're processing its body now
5007 that the class is complete. */
5008 return true;
5010 /* Core issue 226 (C++0x only): the following only applies to class
5011 templates. */
5012 if (is_primary
5013 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5015 /* [temp.param]
5017 If a template-parameter has a default template-argument, all
5018 subsequent template-parameters shall have a default
5019 template-argument supplied. */
5020 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5022 tree inner_parms = TREE_VALUE (parm_level);
5023 int ntparms = TREE_VEC_LENGTH (inner_parms);
5024 int seen_def_arg_p = 0;
5025 int i;
5027 for (i = 0; i < ntparms; ++i)
5029 tree parm = TREE_VEC_ELT (inner_parms, i);
5031 if (parm == error_mark_node)
5032 continue;
5034 if (TREE_PURPOSE (parm))
5035 seen_def_arg_p = 1;
5036 else if (seen_def_arg_p
5037 && !template_parameter_pack_p (TREE_VALUE (parm)))
5039 error ("no default argument for %qD", TREE_VALUE (parm));
5040 /* For better subsequent error-recovery, we indicate that
5041 there should have been a default argument. */
5042 TREE_PURPOSE (parm) = error_mark_node;
5043 no_errors = false;
5045 else if (!is_partial
5046 && !is_friend_decl
5047 /* Don't complain about an enclosing partial
5048 specialization. */
5049 && parm_level == parms
5050 && TREE_CODE (decl) == TYPE_DECL
5051 && i < ntparms - 1
5052 && template_parameter_pack_p (TREE_VALUE (parm))
5053 /* A fixed parameter pack will be partially
5054 instantiated into a fixed length list. */
5055 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5057 /* A primary class template can only have one
5058 parameter pack, at the end of the template
5059 parameter list. */
5061 error ("parameter pack %q+D must be at the end of the"
5062 " template parameter list", TREE_VALUE (parm));
5064 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5065 = error_mark_node;
5066 no_errors = false;
5072 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5073 || is_partial
5074 || !is_primary
5075 || is_friend_decl)
5076 /* For an ordinary class template, default template arguments are
5077 allowed at the innermost level, e.g.:
5078 template <class T = int>
5079 struct S {};
5080 but, in a partial specialization, they're not allowed even
5081 there, as we have in [temp.class.spec]:
5083 The template parameter list of a specialization shall not
5084 contain default template argument values.
5086 So, for a partial specialization, or for a function template
5087 (in C++98/C++03), we look at all of them. */
5089 else
5090 /* But, for a primary class template that is not a partial
5091 specialization we look at all template parameters except the
5092 innermost ones. */
5093 parms = TREE_CHAIN (parms);
5095 /* Figure out what error message to issue. */
5096 if (is_friend_decl == 2)
5097 msg = G_("default template arguments may not be used in function template "
5098 "friend re-declaration");
5099 else if (is_friend_decl)
5100 msg = G_("default template arguments may not be used in function template "
5101 "friend declarations");
5102 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5103 msg = G_("default template arguments may not be used in function templates "
5104 "without -std=c++11 or -std=gnu++11");
5105 else if (is_partial)
5106 msg = G_("default template arguments may not be used in "
5107 "partial specializations");
5108 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5109 msg = G_("default argument for template parameter for class enclosing %qD");
5110 else
5111 /* Per [temp.param]/9, "A default template-argument shall not be
5112 specified in the template-parameter-lists of the definition of
5113 a member of a class template that appears outside of the member's
5114 class.", thus if we aren't handling a member of a class template
5115 there is no need to examine the parameters. */
5116 return true;
5118 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5119 /* If we're inside a class definition, there's no need to
5120 examine the parameters to the class itself. On the one
5121 hand, they will be checked when the class is defined, and,
5122 on the other, default arguments are valid in things like:
5123 template <class T = double>
5124 struct S { template <class U> void f(U); };
5125 Here the default argument for `S' has no bearing on the
5126 declaration of `f'. */
5127 last_level_to_check = template_class_depth (current_class_type) + 1;
5128 else
5129 /* Check everything. */
5130 last_level_to_check = 0;
5132 for (parm_level = parms;
5133 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5134 parm_level = TREE_CHAIN (parm_level))
5136 tree inner_parms = TREE_VALUE (parm_level);
5137 int i;
5138 int ntparms;
5140 ntparms = TREE_VEC_LENGTH (inner_parms);
5141 for (i = 0; i < ntparms; ++i)
5143 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5144 continue;
5146 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5148 if (msg)
5150 no_errors = false;
5151 if (is_friend_decl == 2)
5152 return no_errors;
5154 error (msg, decl);
5155 msg = 0;
5158 /* Clear out the default argument so that we are not
5159 confused later. */
5160 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5164 /* At this point, if we're still interested in issuing messages,
5165 they must apply to classes surrounding the object declared. */
5166 if (msg)
5167 msg = G_("default argument for template parameter for class "
5168 "enclosing %qD");
5171 return no_errors;
5174 /* Worker for push_template_decl_real, called via
5175 for_each_template_parm. DATA is really an int, indicating the
5176 level of the parameters we are interested in. If T is a template
5177 parameter of that level, return nonzero. */
5179 static int
5180 template_parm_this_level_p (tree t, void* data)
5182 int this_level = *(int *)data;
5183 int level;
5185 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5186 level = TEMPLATE_PARM_LEVEL (t);
5187 else
5188 level = TEMPLATE_TYPE_LEVEL (t);
5189 return level == this_level;
5192 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5193 DATA is really an int, indicating the innermost outer level of parameters.
5194 If T is a template parameter of that level or further out, return
5195 nonzero. */
5197 static int
5198 template_parm_outer_level (tree t, void *data)
5200 int this_level = *(int *)data;
5201 int level;
5203 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5204 level = TEMPLATE_PARM_LEVEL (t);
5205 else
5206 level = TEMPLATE_TYPE_LEVEL (t);
5207 return level <= this_level;
5210 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5211 parameters given by current_template_args, or reuses a
5212 previously existing one, if appropriate. Returns the DECL, or an
5213 equivalent one, if it is replaced via a call to duplicate_decls.
5215 If IS_FRIEND is true, DECL is a friend declaration. */
5217 tree
5218 push_template_decl_real (tree decl, bool is_friend)
5220 tree tmpl;
5221 tree args;
5222 tree info;
5223 tree ctx;
5224 bool is_primary;
5225 bool is_partial;
5226 int new_template_p = 0;
5227 /* True if the template is a member template, in the sense of
5228 [temp.mem]. */
5229 bool member_template_p = false;
5231 if (decl == error_mark_node || !current_template_parms)
5232 return error_mark_node;
5234 /* See if this is a partial specialization. */
5235 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5236 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5237 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5238 || (VAR_P (decl)
5239 && DECL_LANG_SPECIFIC (decl)
5240 && DECL_TEMPLATE_SPECIALIZATION (decl)
5241 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5243 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5244 is_friend = true;
5246 if (is_friend)
5247 /* For a friend, we want the context of the friend function, not
5248 the type of which it is a friend. */
5249 ctx = CP_DECL_CONTEXT (decl);
5250 else if (CP_DECL_CONTEXT (decl)
5251 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5252 /* In the case of a virtual function, we want the class in which
5253 it is defined. */
5254 ctx = CP_DECL_CONTEXT (decl);
5255 else
5256 /* Otherwise, if we're currently defining some class, the DECL
5257 is assumed to be a member of the class. */
5258 ctx = current_scope ();
5260 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5261 ctx = NULL_TREE;
5263 if (!DECL_CONTEXT (decl))
5264 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5266 /* See if this is a primary template. */
5267 if (is_friend && ctx
5268 && uses_template_parms_level (ctx, processing_template_decl))
5269 /* A friend template that specifies a class context, i.e.
5270 template <typename T> friend void A<T>::f();
5271 is not primary. */
5272 is_primary = false;
5273 else if (TREE_CODE (decl) == TYPE_DECL
5274 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5275 is_primary = false;
5276 else
5277 is_primary = template_parm_scope_p ();
5279 if (is_primary)
5281 warning (OPT_Wtemplates, "template %qD declared", decl);
5283 if (DECL_CLASS_SCOPE_P (decl))
5284 member_template_p = true;
5285 if (TREE_CODE (decl) == TYPE_DECL
5286 && anon_aggrname_p (DECL_NAME (decl)))
5288 error ("template class without a name");
5289 return error_mark_node;
5291 else if (TREE_CODE (decl) == FUNCTION_DECL)
5293 if (member_template_p)
5295 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5296 error ("member template %qD may not have virt-specifiers", decl);
5298 if (DECL_DESTRUCTOR_P (decl))
5300 /* [temp.mem]
5302 A destructor shall not be a member template. */
5303 error ("destructor %qD declared as member template", decl);
5304 return error_mark_node;
5306 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5307 && (!prototype_p (TREE_TYPE (decl))
5308 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5309 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5310 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5311 == void_list_node)))
5313 /* [basic.stc.dynamic.allocation]
5315 An allocation function can be a function
5316 template. ... Template allocation functions shall
5317 have two or more parameters. */
5318 error ("invalid template declaration of %qD", decl);
5319 return error_mark_node;
5322 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5323 && CLASS_TYPE_P (TREE_TYPE (decl)))
5325 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5326 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5327 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5329 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5330 if (TREE_CODE (t) == TYPE_DECL)
5331 t = TREE_TYPE (t);
5332 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5333 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5336 else if (TREE_CODE (decl) == TYPE_DECL
5337 && TYPE_DECL_ALIAS_P (decl))
5338 /* alias-declaration */
5339 gcc_assert (!DECL_ARTIFICIAL (decl));
5340 else if (VAR_P (decl))
5341 /* C++14 variable template. */;
5342 else
5344 error ("template declaration of %q#D", decl);
5345 return error_mark_node;
5349 /* Check to see that the rules regarding the use of default
5350 arguments are not being violated. */
5351 check_default_tmpl_args (decl, current_template_parms,
5352 is_primary, is_partial, /*is_friend_decl=*/0);
5354 /* Ensure that there are no parameter packs in the type of this
5355 declaration that have not been expanded. */
5356 if (TREE_CODE (decl) == FUNCTION_DECL)
5358 /* Check each of the arguments individually to see if there are
5359 any bare parameter packs. */
5360 tree type = TREE_TYPE (decl);
5361 tree arg = DECL_ARGUMENTS (decl);
5362 tree argtype = TYPE_ARG_TYPES (type);
5364 while (arg && argtype)
5366 if (!DECL_PACK_P (arg)
5367 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5369 /* This is a PARM_DECL that contains unexpanded parameter
5370 packs. We have already complained about this in the
5371 check_for_bare_parameter_packs call, so just replace
5372 these types with ERROR_MARK_NODE. */
5373 TREE_TYPE (arg) = error_mark_node;
5374 TREE_VALUE (argtype) = error_mark_node;
5377 arg = DECL_CHAIN (arg);
5378 argtype = TREE_CHAIN (argtype);
5381 /* Check for bare parameter packs in the return type and the
5382 exception specifiers. */
5383 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5384 /* Errors were already issued, set return type to int
5385 as the frontend doesn't expect error_mark_node as
5386 the return type. */
5387 TREE_TYPE (type) = integer_type_node;
5388 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5389 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5391 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5392 && TYPE_DECL_ALIAS_P (decl))
5393 ? DECL_ORIGINAL_TYPE (decl)
5394 : TREE_TYPE (decl)))
5396 TREE_TYPE (decl) = error_mark_node;
5397 return error_mark_node;
5400 if (is_partial)
5401 return process_partial_specialization (decl);
5403 args = current_template_args ();
5405 if (!ctx
5406 || TREE_CODE (ctx) == FUNCTION_DECL
5407 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5408 || (TREE_CODE (decl) == TYPE_DECL
5409 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5410 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5412 if (DECL_LANG_SPECIFIC (decl)
5413 && DECL_TEMPLATE_INFO (decl)
5414 && DECL_TI_TEMPLATE (decl))
5415 tmpl = DECL_TI_TEMPLATE (decl);
5416 /* If DECL is a TYPE_DECL for a class-template, then there won't
5417 be DECL_LANG_SPECIFIC. The information equivalent to
5418 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5419 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5420 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5421 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5423 /* Since a template declaration already existed for this
5424 class-type, we must be redeclaring it here. Make sure
5425 that the redeclaration is valid. */
5426 redeclare_class_template (TREE_TYPE (decl),
5427 current_template_parms,
5428 current_template_constraints ());
5429 /* We don't need to create a new TEMPLATE_DECL; just use the
5430 one we already had. */
5431 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5433 else
5435 tmpl = build_template_decl (decl, current_template_parms,
5436 member_template_p);
5437 new_template_p = 1;
5439 if (DECL_LANG_SPECIFIC (decl)
5440 && DECL_TEMPLATE_SPECIALIZATION (decl))
5442 /* A specialization of a member template of a template
5443 class. */
5444 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5445 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5446 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5450 else
5452 tree a, t, current, parms;
5453 int i;
5454 tree tinfo = get_template_info (decl);
5456 if (!tinfo)
5458 error ("template definition of non-template %q#D", decl);
5459 return error_mark_node;
5462 tmpl = TI_TEMPLATE (tinfo);
5464 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5465 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5466 && DECL_TEMPLATE_SPECIALIZATION (decl)
5467 && DECL_MEMBER_TEMPLATE_P (tmpl))
5469 tree new_tmpl;
5471 /* The declaration is a specialization of a member
5472 template, declared outside the class. Therefore, the
5473 innermost template arguments will be NULL, so we
5474 replace them with the arguments determined by the
5475 earlier call to check_explicit_specialization. */
5476 args = DECL_TI_ARGS (decl);
5478 new_tmpl
5479 = build_template_decl (decl, current_template_parms,
5480 member_template_p);
5481 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5482 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5483 DECL_TI_TEMPLATE (decl) = new_tmpl;
5484 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5485 DECL_TEMPLATE_INFO (new_tmpl)
5486 = build_template_info (tmpl, args);
5488 register_specialization (new_tmpl,
5489 most_general_template (tmpl),
5490 args,
5491 is_friend, 0);
5492 return decl;
5495 /* Make sure the template headers we got make sense. */
5497 parms = DECL_TEMPLATE_PARMS (tmpl);
5498 i = TMPL_PARMS_DEPTH (parms);
5499 if (TMPL_ARGS_DEPTH (args) != i)
5501 error ("expected %d levels of template parms for %q#D, got %d",
5502 i, decl, TMPL_ARGS_DEPTH (args));
5503 DECL_INTERFACE_KNOWN (decl) = 1;
5504 return error_mark_node;
5506 else
5507 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5509 a = TMPL_ARGS_LEVEL (args, i);
5510 t = INNERMOST_TEMPLATE_PARMS (parms);
5512 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5514 if (current == decl)
5515 error ("got %d template parameters for %q#D",
5516 TREE_VEC_LENGTH (a), decl);
5517 else
5518 error ("got %d template parameters for %q#T",
5519 TREE_VEC_LENGTH (a), current);
5520 error (" but %d required", TREE_VEC_LENGTH (t));
5521 /* Avoid crash in import_export_decl. */
5522 DECL_INTERFACE_KNOWN (decl) = 1;
5523 return error_mark_node;
5526 if (current == decl)
5527 current = ctx;
5528 else if (current == NULL_TREE)
5529 /* Can happen in erroneous input. */
5530 break;
5531 else
5532 current = get_containing_scope (current);
5535 /* Check that the parms are used in the appropriate qualifying scopes
5536 in the declarator. */
5537 if (!comp_template_args
5538 (TI_ARGS (tinfo),
5539 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5541 error ("\
5542 template arguments to %qD do not match original template %qD",
5543 decl, DECL_TEMPLATE_RESULT (tmpl));
5544 if (!uses_template_parms (TI_ARGS (tinfo)))
5545 inform (input_location, "use template<> for an explicit specialization");
5546 /* Avoid crash in import_export_decl. */
5547 DECL_INTERFACE_KNOWN (decl) = 1;
5548 return error_mark_node;
5552 DECL_TEMPLATE_RESULT (tmpl) = decl;
5553 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5555 /* Push template declarations for global functions and types. Note
5556 that we do not try to push a global template friend declared in a
5557 template class; such a thing may well depend on the template
5558 parameters of the class. */
5559 if (new_template_p && !ctx
5560 && !(is_friend && template_class_depth (current_class_type) > 0))
5562 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5563 if (tmpl == error_mark_node)
5564 return error_mark_node;
5566 /* Hide template friend classes that haven't been declared yet. */
5567 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5569 DECL_ANTICIPATED (tmpl) = 1;
5570 DECL_FRIEND_P (tmpl) = 1;
5574 if (is_primary)
5576 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5577 int i;
5579 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5580 if (DECL_CONV_FN_P (tmpl))
5582 int depth = TMPL_PARMS_DEPTH (parms);
5584 /* It is a conversion operator. See if the type converted to
5585 depends on innermost template operands. */
5587 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5588 depth))
5589 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5592 /* Give template template parms a DECL_CONTEXT of the template
5593 for which they are a parameter. */
5594 parms = INNERMOST_TEMPLATE_PARMS (parms);
5595 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5597 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5598 if (TREE_CODE (parm) == TEMPLATE_DECL)
5599 DECL_CONTEXT (parm) = tmpl;
5602 if (TREE_CODE (decl) == TYPE_DECL
5603 && TYPE_DECL_ALIAS_P (decl)
5604 && complex_alias_template_p (tmpl))
5605 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5608 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5609 back to its most general template. If TMPL is a specialization,
5610 ARGS may only have the innermost set of arguments. Add the missing
5611 argument levels if necessary. */
5612 if (DECL_TEMPLATE_INFO (tmpl))
5613 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5615 info = build_template_info (tmpl, args);
5617 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5618 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5619 else
5621 if (is_primary)
5622 retrofit_lang_decl (decl);
5623 if (DECL_LANG_SPECIFIC (decl))
5624 DECL_TEMPLATE_INFO (decl) = info;
5627 if (flag_implicit_templates
5628 && !is_friend
5629 && TREE_PUBLIC (decl)
5630 && VAR_OR_FUNCTION_DECL_P (decl))
5631 /* Set DECL_COMDAT on template instantiations; if we force
5632 them to be emitted by explicit instantiation or -frepo,
5633 mark_needed will tell cgraph to do the right thing. */
5634 DECL_COMDAT (decl) = true;
5636 return DECL_TEMPLATE_RESULT (tmpl);
5639 tree
5640 push_template_decl (tree decl)
5642 return push_template_decl_real (decl, false);
5645 /* FN is an inheriting constructor that inherits from the constructor
5646 template INHERITED; turn FN into a constructor template with a matching
5647 template header. */
5649 tree
5650 add_inherited_template_parms (tree fn, tree inherited)
5652 tree inner_parms
5653 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5654 inner_parms = copy_node (inner_parms);
5655 tree parms
5656 = tree_cons (size_int (processing_template_decl + 1),
5657 inner_parms, current_template_parms);
5658 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5659 tree args = template_parms_to_args (parms);
5660 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5661 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5662 DECL_TEMPLATE_RESULT (tmpl) = fn;
5663 DECL_ARTIFICIAL (tmpl) = true;
5664 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5665 return tmpl;
5668 /* Called when a class template TYPE is redeclared with the indicated
5669 template PARMS, e.g.:
5671 template <class T> struct S;
5672 template <class T> struct S {}; */
5674 bool
5675 redeclare_class_template (tree type, tree parms, tree cons)
5677 tree tmpl;
5678 tree tmpl_parms;
5679 int i;
5681 if (!TYPE_TEMPLATE_INFO (type))
5683 error ("%qT is not a template type", type);
5684 return false;
5687 tmpl = TYPE_TI_TEMPLATE (type);
5688 if (!PRIMARY_TEMPLATE_P (tmpl))
5689 /* The type is nested in some template class. Nothing to worry
5690 about here; there are no new template parameters for the nested
5691 type. */
5692 return true;
5694 if (!parms)
5696 error ("template specifiers not specified in declaration of %qD",
5697 tmpl);
5698 return false;
5701 parms = INNERMOST_TEMPLATE_PARMS (parms);
5702 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5704 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5706 error_n (input_location, TREE_VEC_LENGTH (parms),
5707 "redeclared with %d template parameter",
5708 "redeclared with %d template parameters",
5709 TREE_VEC_LENGTH (parms));
5710 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5711 "previous declaration %qD used %d template parameter",
5712 "previous declaration %qD used %d template parameters",
5713 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5714 return false;
5717 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5719 tree tmpl_parm;
5720 tree parm;
5721 tree tmpl_default;
5722 tree parm_default;
5724 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5725 || TREE_VEC_ELT (parms, i) == error_mark_node)
5726 continue;
5728 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5729 if (error_operand_p (tmpl_parm))
5730 return false;
5732 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5733 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5734 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5736 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5737 TEMPLATE_DECL. */
5738 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5739 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5740 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5741 || (TREE_CODE (tmpl_parm) != PARM_DECL
5742 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5743 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5744 || (TREE_CODE (tmpl_parm) == PARM_DECL
5745 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5746 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5748 error ("template parameter %q+#D", tmpl_parm);
5749 error ("redeclared here as %q#D", parm);
5750 return false;
5753 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5755 /* We have in [temp.param]:
5757 A template-parameter may not be given default arguments
5758 by two different declarations in the same scope. */
5759 error_at (input_location, "redefinition of default argument for %q#D", parm);
5760 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5761 "original definition appeared here");
5762 return false;
5765 if (parm_default != NULL_TREE)
5766 /* Update the previous template parameters (which are the ones
5767 that will really count) with the new default value. */
5768 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5769 else if (tmpl_default != NULL_TREE)
5770 /* Update the new parameters, too; they'll be used as the
5771 parameters for any members. */
5772 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5774 /* Give each template template parm in this redeclaration a
5775 DECL_CONTEXT of the template for which they are a parameter. */
5776 if (TREE_CODE (parm) == TEMPLATE_DECL)
5778 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5779 DECL_CONTEXT (parm) = tmpl;
5782 if (TREE_CODE (parm) == TYPE_DECL)
5783 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5786 // Cannot redeclare a class template with a different set of constraints.
5787 if (!equivalent_constraints (get_constraints (tmpl), cons))
5789 error_at (input_location, "redeclaration %q#D with different "
5790 "constraints", tmpl);
5791 inform (DECL_SOURCE_LOCATION (tmpl),
5792 "original declaration appeared here");
5795 return true;
5798 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5799 to be used when the caller has already checked
5800 (processing_template_decl
5801 && !instantiation_dependent_expression_p (expr)
5802 && potential_constant_expression (expr))
5803 and cleared processing_template_decl. */
5805 tree
5806 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5808 return tsubst_copy_and_build (expr,
5809 /*args=*/NULL_TREE,
5810 complain,
5811 /*in_decl=*/NULL_TREE,
5812 /*function_p=*/false,
5813 /*integral_constant_expression_p=*/true);
5816 /* Simplify EXPR if it is a non-dependent expression. Returns the
5817 (possibly simplified) expression. */
5819 tree
5820 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5822 if (expr == NULL_TREE)
5823 return NULL_TREE;
5825 /* If we're in a template, but EXPR isn't value dependent, simplify
5826 it. We're supposed to treat:
5828 template <typename T> void f(T[1 + 1]);
5829 template <typename T> void f(T[2]);
5831 as two declarations of the same function, for example. */
5832 if (processing_template_decl
5833 && potential_nondependent_constant_expression (expr))
5835 processing_template_decl_sentinel s;
5836 expr = instantiate_non_dependent_expr_internal (expr, complain);
5838 return expr;
5841 tree
5842 instantiate_non_dependent_expr (tree expr)
5844 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5847 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5848 an uninstantiated expression. */
5850 tree
5851 instantiate_non_dependent_or_null (tree expr)
5853 if (expr == NULL_TREE)
5854 return NULL_TREE;
5855 if (processing_template_decl)
5857 if (!potential_nondependent_constant_expression (expr))
5858 expr = NULL_TREE;
5859 else
5861 processing_template_decl_sentinel s;
5862 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5865 return expr;
5868 /* True iff T is a specialization of a variable template. */
5870 bool
5871 variable_template_specialization_p (tree t)
5873 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5874 return false;
5875 tree tmpl = DECL_TI_TEMPLATE (t);
5876 return variable_template_p (tmpl);
5879 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5880 template declaration, or a TYPE_DECL for an alias declaration. */
5882 bool
5883 alias_type_or_template_p (tree t)
5885 if (t == NULL_TREE)
5886 return false;
5887 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5888 || (TYPE_P (t)
5889 && TYPE_NAME (t)
5890 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5891 || DECL_ALIAS_TEMPLATE_P (t));
5894 /* Return TRUE iff T is a specialization of an alias template. */
5896 bool
5897 alias_template_specialization_p (const_tree t)
5899 /* It's an alias template specialization if it's an alias and its
5900 TYPE_NAME is a specialization of a primary template. */
5901 if (TYPE_ALIAS_P (t))
5902 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5903 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5905 return false;
5908 /* An alias template is complex from a SFINAE perspective if a template-id
5909 using that alias can be ill-formed when the expansion is not, as with
5910 the void_t template. We determine this by checking whether the
5911 expansion for the alias template uses all its template parameters. */
5913 struct uses_all_template_parms_data
5915 int level;
5916 bool *seen;
5919 static int
5920 uses_all_template_parms_r (tree t, void *data_)
5922 struct uses_all_template_parms_data &data
5923 = *(struct uses_all_template_parms_data*)data_;
5924 tree idx = get_template_parm_index (t);
5926 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5927 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5928 return 0;
5931 static bool
5932 complex_alias_template_p (const_tree tmpl)
5934 struct uses_all_template_parms_data data;
5935 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5936 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5937 data.level = TMPL_PARMS_DEPTH (parms);
5938 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5939 data.seen = XALLOCAVEC (bool, len);
5940 for (int i = 0; i < len; ++i)
5941 data.seen[i] = false;
5943 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5944 for (int i = 0; i < len; ++i)
5945 if (!data.seen[i])
5946 return true;
5947 return false;
5950 /* Return TRUE iff T is a specialization of a complex alias template with
5951 dependent template-arguments. */
5953 bool
5954 dependent_alias_template_spec_p (const_tree t)
5956 if (!alias_template_specialization_p (t))
5957 return false;
5959 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5960 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5961 return false;
5963 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5964 if (!any_dependent_template_arguments_p (args))
5965 return false;
5967 return true;
5970 /* Return the number of innermost template parameters in TMPL. */
5972 static int
5973 num_innermost_template_parms (tree tmpl)
5975 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5976 return TREE_VEC_LENGTH (parms);
5979 /* Return either TMPL or another template that it is equivalent to under DR
5980 1286: An alias that just changes the name of a template is equivalent to
5981 the other template. */
5983 static tree
5984 get_underlying_template (tree tmpl)
5986 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5987 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5989 /* Determine if the alias is equivalent to an underlying template. */
5990 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5991 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5992 if (!tinfo)
5993 break;
5995 tree underlying = TI_TEMPLATE (tinfo);
5996 if (!PRIMARY_TEMPLATE_P (underlying)
5997 || (num_innermost_template_parms (tmpl)
5998 != num_innermost_template_parms (underlying)))
5999 break;
6001 tree alias_args = INNERMOST_TEMPLATE_ARGS
6002 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6003 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6004 break;
6006 /* Alias is equivalent. Strip it and repeat. */
6007 tmpl = underlying;
6010 return tmpl;
6013 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6014 must be a reference-to-function or a pointer-to-function type, as specified
6015 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6016 and check that the resulting function has external linkage. */
6018 static tree
6019 convert_nontype_argument_function (tree type, tree expr,
6020 tsubst_flags_t complain)
6022 tree fns = expr;
6023 tree fn, fn_no_ptr;
6024 linkage_kind linkage;
6026 fn = instantiate_type (type, fns, tf_none);
6027 if (fn == error_mark_node)
6028 return error_mark_node;
6030 if (value_dependent_expression_p (fn))
6031 goto accept;
6033 fn_no_ptr = strip_fnptr_conv (fn);
6034 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6035 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6036 if (BASELINK_P (fn_no_ptr))
6037 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6039 /* [temp.arg.nontype]/1
6041 A template-argument for a non-type, non-template template-parameter
6042 shall be one of:
6043 [...]
6044 -- the address of an object or function with external [C++11: or
6045 internal] linkage. */
6047 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6049 if (complain & tf_error)
6051 error ("%qE is not a valid template argument for type %qT",
6052 expr, type);
6053 if (TYPE_PTR_P (type))
6054 inform (input_location, "it must be the address of a function "
6055 "with external linkage");
6056 else
6057 inform (input_location, "it must be the name of a function with "
6058 "external linkage");
6060 return NULL_TREE;
6063 linkage = decl_linkage (fn_no_ptr);
6064 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6066 if (complain & tf_error)
6068 if (cxx_dialect >= cxx11)
6069 error ("%qE is not a valid template argument for type %qT "
6070 "because %qD has no linkage",
6071 expr, type, fn_no_ptr);
6072 else
6073 error ("%qE is not a valid template argument for type %qT "
6074 "because %qD does not have external linkage",
6075 expr, type, fn_no_ptr);
6077 return NULL_TREE;
6080 accept:
6081 if (TREE_CODE (type) == REFERENCE_TYPE)
6082 fn = build_address (fn);
6083 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6084 fn = build_nop (type, fn);
6086 return fn;
6089 /* Subroutine of convert_nontype_argument.
6090 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6091 Emit an error otherwise. */
6093 static bool
6094 check_valid_ptrmem_cst_expr (tree type, tree expr,
6095 tsubst_flags_t complain)
6097 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6098 tree orig_expr = expr;
6099 STRIP_NOPS (expr);
6100 if (null_ptr_cst_p (expr))
6101 return true;
6102 if (TREE_CODE (expr) == PTRMEM_CST
6103 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6104 PTRMEM_CST_CLASS (expr)))
6105 return true;
6106 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6107 return true;
6108 if (processing_template_decl
6109 && TREE_CODE (expr) == ADDR_EXPR
6110 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6111 return true;
6112 if (complain & tf_error)
6114 error_at (loc, "%qE is not a valid template argument for type %qT",
6115 orig_expr, type);
6116 if (TREE_CODE (expr) != PTRMEM_CST)
6117 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6118 else
6119 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6121 return false;
6124 /* Returns TRUE iff the address of OP is value-dependent.
6126 14.6.2.4 [temp.dep.temp]:
6127 A non-integral non-type template-argument is dependent if its type is
6128 dependent or it has either of the following forms
6129 qualified-id
6130 & qualified-id
6131 and contains a nested-name-specifier which specifies a class-name that
6132 names a dependent type.
6134 We generalize this to just say that the address of a member of a
6135 dependent class is value-dependent; the above doesn't cover the
6136 address of a static data member named with an unqualified-id. */
6138 static bool
6139 has_value_dependent_address (tree op)
6141 /* We could use get_inner_reference here, but there's no need;
6142 this is only relevant for template non-type arguments, which
6143 can only be expressed as &id-expression. */
6144 if (DECL_P (op))
6146 tree ctx = CP_DECL_CONTEXT (op);
6147 if (TYPE_P (ctx) && dependent_type_p (ctx))
6148 return true;
6151 return false;
6154 /* The next set of functions are used for providing helpful explanatory
6155 diagnostics for failed overload resolution. Their messages should be
6156 indented by two spaces for consistency with the messages in
6157 call.c */
6159 static int
6160 unify_success (bool /*explain_p*/)
6162 return 0;
6165 /* Other failure functions should call this one, to provide a single function
6166 for setting a breakpoint on. */
6168 static int
6169 unify_invalid (bool /*explain_p*/)
6171 return 1;
6174 static int
6175 unify_parameter_deduction_failure (bool explain_p, tree parm)
6177 if (explain_p)
6178 inform (input_location,
6179 " couldn't deduce template parameter %qD", parm);
6180 return unify_invalid (explain_p);
6183 static int
6184 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6186 if (explain_p)
6187 inform (input_location,
6188 " types %qT and %qT have incompatible cv-qualifiers",
6189 parm, arg);
6190 return unify_invalid (explain_p);
6193 static int
6194 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6196 if (explain_p)
6197 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6198 return unify_invalid (explain_p);
6201 static int
6202 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6204 if (explain_p)
6205 inform (input_location,
6206 " template parameter %qD is not a parameter pack, but "
6207 "argument %qD is",
6208 parm, arg);
6209 return unify_invalid (explain_p);
6212 static int
6213 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6215 if (explain_p)
6216 inform (input_location,
6217 " template argument %qE does not match "
6218 "pointer-to-member constant %qE",
6219 arg, parm);
6220 return unify_invalid (explain_p);
6223 static int
6224 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6226 if (explain_p)
6227 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6228 return unify_invalid (explain_p);
6231 static int
6232 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6234 if (explain_p)
6235 inform (input_location,
6236 " inconsistent parameter pack deduction with %qT and %qT",
6237 old_arg, new_arg);
6238 return unify_invalid (explain_p);
6241 static int
6242 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6244 if (explain_p)
6246 if (TYPE_P (parm))
6247 inform (input_location,
6248 " deduced conflicting types for parameter %qT (%qT and %qT)",
6249 parm, first, second);
6250 else
6251 inform (input_location,
6252 " deduced conflicting values for non-type parameter "
6253 "%qE (%qE and %qE)", parm, first, second);
6255 return unify_invalid (explain_p);
6258 static int
6259 unify_vla_arg (bool explain_p, tree arg)
6261 if (explain_p)
6262 inform (input_location,
6263 " variable-sized array type %qT is not "
6264 "a valid template argument",
6265 arg);
6266 return unify_invalid (explain_p);
6269 static int
6270 unify_method_type_error (bool explain_p, tree arg)
6272 if (explain_p)
6273 inform (input_location,
6274 " member function type %qT is not a valid template argument",
6275 arg);
6276 return unify_invalid (explain_p);
6279 static int
6280 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6282 if (explain_p)
6284 if (least_p)
6285 inform_n (input_location, wanted,
6286 " candidate expects at least %d argument, %d provided",
6287 " candidate expects at least %d arguments, %d provided",
6288 wanted, have);
6289 else
6290 inform_n (input_location, wanted,
6291 " candidate expects %d argument, %d provided",
6292 " candidate expects %d arguments, %d provided",
6293 wanted, have);
6295 return unify_invalid (explain_p);
6298 static int
6299 unify_too_many_arguments (bool explain_p, int have, int wanted)
6301 return unify_arity (explain_p, have, wanted);
6304 static int
6305 unify_too_few_arguments (bool explain_p, int have, int wanted,
6306 bool least_p = false)
6308 return unify_arity (explain_p, have, wanted, least_p);
6311 static int
6312 unify_arg_conversion (bool explain_p, tree to_type,
6313 tree from_type, tree arg)
6315 if (explain_p)
6316 inform (EXPR_LOC_OR_LOC (arg, input_location),
6317 " cannot convert %qE (type %qT) to type %qT",
6318 arg, from_type, to_type);
6319 return unify_invalid (explain_p);
6322 static int
6323 unify_no_common_base (bool explain_p, enum template_base_result r,
6324 tree parm, tree arg)
6326 if (explain_p)
6327 switch (r)
6329 case tbr_ambiguous_baseclass:
6330 inform (input_location, " %qT is an ambiguous base class of %qT",
6331 parm, arg);
6332 break;
6333 default:
6334 inform (input_location, " %qT is not derived from %qT", arg, parm);
6335 break;
6337 return unify_invalid (explain_p);
6340 static int
6341 unify_inconsistent_template_template_parameters (bool explain_p)
6343 if (explain_p)
6344 inform (input_location,
6345 " template parameters of a template template argument are "
6346 "inconsistent with other deduced template arguments");
6347 return unify_invalid (explain_p);
6350 static int
6351 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6353 if (explain_p)
6354 inform (input_location,
6355 " can't deduce a template for %qT from non-template type %qT",
6356 parm, arg);
6357 return unify_invalid (explain_p);
6360 static int
6361 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6363 if (explain_p)
6364 inform (input_location,
6365 " template argument %qE does not match %qE", arg, parm);
6366 return unify_invalid (explain_p);
6369 static int
6370 unify_overload_resolution_failure (bool explain_p, tree arg)
6372 if (explain_p)
6373 inform (input_location,
6374 " could not resolve address from overloaded function %qE",
6375 arg);
6376 return unify_invalid (explain_p);
6379 /* Attempt to convert the non-type template parameter EXPR to the
6380 indicated TYPE. If the conversion is successful, return the
6381 converted value. If the conversion is unsuccessful, return
6382 NULL_TREE if we issued an error message, or error_mark_node if we
6383 did not. We issue error messages for out-and-out bad template
6384 parameters, but not simply because the conversion failed, since we
6385 might be just trying to do argument deduction. Both TYPE and EXPR
6386 must be non-dependent.
6388 The conversion follows the special rules described in
6389 [temp.arg.nontype], and it is much more strict than an implicit
6390 conversion.
6392 This function is called twice for each template argument (see
6393 lookup_template_class for a more accurate description of this
6394 problem). This means that we need to handle expressions which
6395 are not valid in a C++ source, but can be created from the
6396 first call (for instance, casts to perform conversions). These
6397 hacks can go away after we fix the double coercion problem. */
6399 static tree
6400 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6402 tree expr_type;
6403 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6404 tree orig_expr = expr;
6406 /* Detect immediately string literals as invalid non-type argument.
6407 This special-case is not needed for correctness (we would easily
6408 catch this later), but only to provide better diagnostic for this
6409 common user mistake. As suggested by DR 100, we do not mention
6410 linkage issues in the diagnostic as this is not the point. */
6411 /* FIXME we're making this OK. */
6412 if (TREE_CODE (expr) == STRING_CST)
6414 if (complain & tf_error)
6415 error ("%qE is not a valid template argument for type %qT "
6416 "because string literals can never be used in this context",
6417 expr, type);
6418 return NULL_TREE;
6421 /* Add the ADDR_EXPR now for the benefit of
6422 value_dependent_expression_p. */
6423 if (TYPE_PTROBV_P (type)
6424 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6426 expr = decay_conversion (expr, complain);
6427 if (expr == error_mark_node)
6428 return error_mark_node;
6431 /* If we are in a template, EXPR may be non-dependent, but still
6432 have a syntactic, rather than semantic, form. For example, EXPR
6433 might be a SCOPE_REF, rather than the VAR_DECL to which the
6434 SCOPE_REF refers. Preserving the qualifying scope is necessary
6435 so that access checking can be performed when the template is
6436 instantiated -- but here we need the resolved form so that we can
6437 convert the argument. */
6438 bool non_dep = false;
6439 if (TYPE_REF_OBJ_P (type)
6440 && has_value_dependent_address (expr))
6441 /* If we want the address and it's value-dependent, don't fold. */;
6442 else if (processing_template_decl
6443 && potential_nondependent_constant_expression (expr))
6444 non_dep = true;
6445 if (error_operand_p (expr))
6446 return error_mark_node;
6447 expr_type = TREE_TYPE (expr);
6448 if (TREE_CODE (type) == REFERENCE_TYPE)
6449 expr = mark_lvalue_use (expr);
6450 else
6451 expr = mark_rvalue_use (expr);
6453 /* If the argument is non-dependent, perform any conversions in
6454 non-dependent context as well. */
6455 processing_template_decl_sentinel s (non_dep);
6456 if (non_dep)
6457 expr = instantiate_non_dependent_expr_internal (expr, complain);
6459 if (value_dependent_expression_p (expr))
6460 expr = canonicalize_expr_argument (expr, complain);
6462 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6463 to a non-type argument of "nullptr". */
6464 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6465 expr = fold_simple (convert (type, expr));
6467 /* In C++11, integral or enumeration non-type template arguments can be
6468 arbitrary constant expressions. Pointer and pointer to
6469 member arguments can be general constant expressions that evaluate
6470 to a null value, but otherwise still need to be of a specific form. */
6471 if (cxx_dialect >= cxx11)
6473 if (TREE_CODE (expr) == PTRMEM_CST)
6474 /* A PTRMEM_CST is already constant, and a valid template
6475 argument for a parameter of pointer to member type, we just want
6476 to leave it in that form rather than lower it to a
6477 CONSTRUCTOR. */;
6478 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6479 || cxx_dialect >= cxx1z)
6481 /* C++17: A template-argument for a non-type template-parameter shall
6482 be a converted constant expression (8.20) of the type of the
6483 template-parameter. */
6484 expr = build_converted_constant_expr (type, expr, complain);
6485 if (expr == error_mark_node)
6486 return error_mark_node;
6487 expr = maybe_constant_value (expr);
6488 expr = convert_from_reference (expr);
6490 else if (TYPE_PTR_OR_PTRMEM_P (type))
6492 tree folded = maybe_constant_value (expr);
6493 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6494 : null_member_pointer_value_p (folded))
6495 expr = folded;
6499 /* HACK: Due to double coercion, we can get a
6500 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6501 which is the tree that we built on the first call (see
6502 below when coercing to reference to object or to reference to
6503 function). We just strip everything and get to the arg.
6504 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6505 for examples. */
6506 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6508 tree probe_type, probe = expr;
6509 if (REFERENCE_REF_P (probe))
6510 probe = TREE_OPERAND (probe, 0);
6511 probe_type = TREE_TYPE (probe);
6512 if (TREE_CODE (probe) == NOP_EXPR)
6514 /* ??? Maybe we could use convert_from_reference here, but we
6515 would need to relax its constraints because the NOP_EXPR
6516 could actually change the type to something more cv-qualified,
6517 and this is not folded by convert_from_reference. */
6518 tree addr = TREE_OPERAND (probe, 0);
6519 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6520 && TREE_CODE (addr) == ADDR_EXPR
6521 && TYPE_PTR_P (TREE_TYPE (addr))
6522 && (same_type_ignoring_top_level_qualifiers_p
6523 (TREE_TYPE (probe_type),
6524 TREE_TYPE (TREE_TYPE (addr)))))
6526 expr = TREE_OPERAND (addr, 0);
6527 expr_type = TREE_TYPE (probe_type);
6532 /* [temp.arg.nontype]/5, bullet 1
6534 For a non-type template-parameter of integral or enumeration type,
6535 integral promotions (_conv.prom_) and integral conversions
6536 (_conv.integral_) are applied. */
6537 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6539 if (cxx_dialect < cxx11)
6541 tree t = build_converted_constant_expr (type, expr, complain);
6542 t = maybe_constant_value (t);
6543 if (t != error_mark_node)
6544 expr = t;
6547 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6548 return error_mark_node;
6550 /* Notice that there are constant expressions like '4 % 0' which
6551 do not fold into integer constants. */
6552 if (TREE_CODE (expr) != INTEGER_CST
6553 && !value_dependent_expression_p (expr))
6555 if (complain & tf_error)
6557 int errs = errorcount, warns = warningcount + werrorcount;
6558 if (!require_potential_constant_expression (expr))
6559 expr = error_mark_node;
6560 else
6561 expr = cxx_constant_value (expr);
6562 if (errorcount > errs || warningcount + werrorcount > warns)
6563 inform (loc, "in template argument for type %qT ", type);
6564 if (expr == error_mark_node)
6565 return NULL_TREE;
6566 /* else cxx_constant_value complained but gave us
6567 a real constant, so go ahead. */
6568 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6570 else
6571 return NULL_TREE;
6574 /* Avoid typedef problems. */
6575 if (TREE_TYPE (expr) != type)
6576 expr = fold_convert (type, expr);
6578 /* [temp.arg.nontype]/5, bullet 2
6580 For a non-type template-parameter of type pointer to object,
6581 qualification conversions (_conv.qual_) and the array-to-pointer
6582 conversion (_conv.array_) are applied. */
6583 else if (TYPE_PTROBV_P (type))
6585 tree decayed = expr;
6587 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6588 decay_conversion or an explicit cast. If it's a problematic cast,
6589 we'll complain about it below. */
6590 if (TREE_CODE (expr) == NOP_EXPR)
6592 tree probe = expr;
6593 STRIP_NOPS (probe);
6594 if (TREE_CODE (probe) == ADDR_EXPR
6595 && TYPE_PTR_P (TREE_TYPE (probe)))
6597 expr = probe;
6598 expr_type = TREE_TYPE (expr);
6602 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6604 A template-argument for a non-type, non-template template-parameter
6605 shall be one of: [...]
6607 -- the name of a non-type template-parameter;
6608 -- the address of an object or function with external linkage, [...]
6609 expressed as "& id-expression" where the & is optional if the name
6610 refers to a function or array, or if the corresponding
6611 template-parameter is a reference.
6613 Here, we do not care about functions, as they are invalid anyway
6614 for a parameter of type pointer-to-object. */
6616 if (value_dependent_expression_p (expr))
6617 /* Non-type template parameters are OK. */
6619 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6620 /* Null pointer values are OK in C++11. */;
6621 else if (TREE_CODE (expr) != ADDR_EXPR)
6623 if (VAR_P (expr))
6625 if (complain & tf_error)
6626 error ("%qD is not a valid template argument "
6627 "because %qD is a variable, not the address of "
6628 "a variable", orig_expr, expr);
6629 return NULL_TREE;
6631 if (POINTER_TYPE_P (expr_type))
6633 if (complain & tf_error)
6634 error ("%qE is not a valid template argument for %qT "
6635 "because it is not the address of a variable",
6636 orig_expr, type);
6637 return NULL_TREE;
6639 /* Other values, like integer constants, might be valid
6640 non-type arguments of some other type. */
6641 return error_mark_node;
6643 else
6645 tree decl = TREE_OPERAND (expr, 0);
6647 if (!VAR_P (decl))
6649 if (complain & tf_error)
6650 error ("%qE is not a valid template argument of type %qT "
6651 "because %qE is not a variable", orig_expr, type, decl);
6652 return NULL_TREE;
6654 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6656 if (complain & tf_error)
6657 error ("%qE is not a valid template argument of type %qT "
6658 "because %qD does not have external linkage",
6659 orig_expr, type, decl);
6660 return NULL_TREE;
6662 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6663 && decl_linkage (decl) == lk_none)
6665 if (complain & tf_error)
6666 error ("%qE is not a valid template argument of type %qT "
6667 "because %qD has no linkage", orig_expr, type, decl);
6668 return NULL_TREE;
6670 /* C++17: For a non-type template-parameter of reference or pointer
6671 type, the value of the constant expression shall not refer to (or
6672 for a pointer type, shall not be the address of):
6673 * a subobject (4.5),
6674 * a temporary object (15.2),
6675 * a string literal (5.13.5),
6676 * the result of a typeid expression (8.2.8), or
6677 * a predefined __func__ variable (11.4.1). */
6678 else if (DECL_ARTIFICIAL (decl))
6680 if (complain & tf_error)
6681 error ("the address of %qD is not a valid template argument",
6682 decl);
6683 return NULL_TREE;
6685 else if (!same_type_ignoring_top_level_qualifiers_p
6686 (strip_array_types (TREE_TYPE (type)),
6687 strip_array_types (TREE_TYPE (decl))))
6689 if (complain & tf_error)
6690 error ("the address of the %qT subobject of %qD is not a "
6691 "valid template argument", TREE_TYPE (type), decl);
6692 return NULL_TREE;
6694 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6696 if (complain & tf_error)
6697 error ("the address of %qD is not a valid template argument "
6698 "because it does not have static storage duration",
6699 decl);
6700 return NULL_TREE;
6704 expr = decayed;
6706 expr = perform_qualification_conversions (type, expr);
6707 if (expr == error_mark_node)
6708 return error_mark_node;
6710 /* [temp.arg.nontype]/5, bullet 3
6712 For a non-type template-parameter of type reference to object, no
6713 conversions apply. The type referred to by the reference may be more
6714 cv-qualified than the (otherwise identical) type of the
6715 template-argument. The template-parameter is bound directly to the
6716 template-argument, which must be an lvalue. */
6717 else if (TYPE_REF_OBJ_P (type))
6719 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6720 expr_type))
6721 return error_mark_node;
6723 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6725 if (complain & tf_error)
6726 error ("%qE is not a valid template argument for type %qT "
6727 "because of conflicts in cv-qualification", expr, type);
6728 return NULL_TREE;
6731 if (!lvalue_p (expr))
6733 if (complain & tf_error)
6734 error ("%qE is not a valid template argument for type %qT "
6735 "because it is not an lvalue", expr, type);
6736 return NULL_TREE;
6739 /* [temp.arg.nontype]/1
6741 A template-argument for a non-type, non-template template-parameter
6742 shall be one of: [...]
6744 -- the address of an object or function with external linkage. */
6745 if (INDIRECT_REF_P (expr)
6746 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6748 expr = TREE_OPERAND (expr, 0);
6749 if (DECL_P (expr))
6751 if (complain & tf_error)
6752 error ("%q#D is not a valid template argument for type %qT "
6753 "because a reference variable does not have a constant "
6754 "address", expr, type);
6755 return NULL_TREE;
6759 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6760 && value_dependent_expression_p (expr))
6761 /* OK, dependent reference. We don't want to ask whether a DECL is
6762 itself value-dependent, since what we want here is its address. */;
6763 else
6765 if (!DECL_P (expr))
6767 if (complain & tf_error)
6768 error ("%qE is not a valid template argument for type %qT "
6769 "because it is not an object with linkage",
6770 expr, type);
6771 return NULL_TREE;
6774 /* DR 1155 allows internal linkage in C++11 and up. */
6775 linkage_kind linkage = decl_linkage (expr);
6776 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6778 if (complain & tf_error)
6779 error ("%qE is not a valid template argument for type %qT "
6780 "because object %qD does not have linkage",
6781 expr, type, expr);
6782 return NULL_TREE;
6785 expr = build_address (expr);
6788 if (!same_type_p (type, TREE_TYPE (expr)))
6789 expr = build_nop (type, expr);
6791 /* [temp.arg.nontype]/5, bullet 4
6793 For a non-type template-parameter of type pointer to function, only
6794 the function-to-pointer conversion (_conv.func_) is applied. If the
6795 template-argument represents a set of overloaded functions (or a
6796 pointer to such), the matching function is selected from the set
6797 (_over.over_). */
6798 else if (TYPE_PTRFN_P (type))
6800 /* If the argument is a template-id, we might not have enough
6801 context information to decay the pointer. */
6802 if (!type_unknown_p (expr_type))
6804 expr = decay_conversion (expr, complain);
6805 if (expr == error_mark_node)
6806 return error_mark_node;
6809 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6810 /* Null pointer values are OK in C++11. */
6811 return perform_qualification_conversions (type, expr);
6813 expr = convert_nontype_argument_function (type, expr, complain);
6814 if (!expr || expr == error_mark_node)
6815 return expr;
6817 /* [temp.arg.nontype]/5, bullet 5
6819 For a non-type template-parameter of type reference to function, no
6820 conversions apply. If the template-argument represents a set of
6821 overloaded functions, the matching function is selected from the set
6822 (_over.over_). */
6823 else if (TYPE_REFFN_P (type))
6825 if (TREE_CODE (expr) == ADDR_EXPR)
6827 if (complain & tf_error)
6829 error ("%qE is not a valid template argument for type %qT "
6830 "because it is a pointer", expr, type);
6831 inform (input_location, "try using %qE instead",
6832 TREE_OPERAND (expr, 0));
6834 return NULL_TREE;
6837 expr = convert_nontype_argument_function (type, expr, complain);
6838 if (!expr || expr == error_mark_node)
6839 return expr;
6841 /* [temp.arg.nontype]/5, bullet 6
6843 For a non-type template-parameter of type pointer to member function,
6844 no conversions apply. If the template-argument represents a set of
6845 overloaded member functions, the matching member function is selected
6846 from the set (_over.over_). */
6847 else if (TYPE_PTRMEMFUNC_P (type))
6849 expr = instantiate_type (type, expr, tf_none);
6850 if (expr == error_mark_node)
6851 return error_mark_node;
6853 /* [temp.arg.nontype] bullet 1 says the pointer to member
6854 expression must be a pointer-to-member constant. */
6855 if (!value_dependent_expression_p (expr)
6856 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6857 return NULL_TREE;
6859 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6860 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6861 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6862 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6864 /* [temp.arg.nontype]/5, bullet 7
6866 For a non-type template-parameter of type pointer to data member,
6867 qualification conversions (_conv.qual_) are applied. */
6868 else if (TYPE_PTRDATAMEM_P (type))
6870 /* [temp.arg.nontype] bullet 1 says the pointer to member
6871 expression must be a pointer-to-member constant. */
6872 if (!value_dependent_expression_p (expr)
6873 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6874 return NULL_TREE;
6876 expr = perform_qualification_conversions (type, expr);
6877 if (expr == error_mark_node)
6878 return expr;
6880 else if (NULLPTR_TYPE_P (type))
6882 if (expr != nullptr_node)
6884 if (complain & tf_error)
6885 error ("%qE is not a valid template argument for type %qT "
6886 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6887 return NULL_TREE;
6889 return expr;
6891 /* A template non-type parameter must be one of the above. */
6892 else
6893 gcc_unreachable ();
6895 /* Sanity check: did we actually convert the argument to the
6896 right type? */
6897 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6898 (type, TREE_TYPE (expr)));
6899 return convert_from_reference (expr);
6902 /* Subroutine of coerce_template_template_parms, which returns 1 if
6903 PARM_PARM and ARG_PARM match using the rule for the template
6904 parameters of template template parameters. Both PARM and ARG are
6905 template parameters; the rest of the arguments are the same as for
6906 coerce_template_template_parms.
6908 static int
6909 coerce_template_template_parm (tree parm,
6910 tree arg,
6911 tsubst_flags_t complain,
6912 tree in_decl,
6913 tree outer_args)
6915 if (arg == NULL_TREE || error_operand_p (arg)
6916 || parm == NULL_TREE || error_operand_p (parm))
6917 return 0;
6919 if (TREE_CODE (arg) != TREE_CODE (parm))
6920 return 0;
6922 switch (TREE_CODE (parm))
6924 case TEMPLATE_DECL:
6925 /* We encounter instantiations of templates like
6926 template <template <template <class> class> class TT>
6927 class C; */
6929 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6930 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6932 if (!coerce_template_template_parms
6933 (parmparm, argparm, complain, in_decl, outer_args))
6934 return 0;
6936 /* Fall through. */
6938 case TYPE_DECL:
6939 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6940 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6941 /* Argument is a parameter pack but parameter is not. */
6942 return 0;
6943 break;
6945 case PARM_DECL:
6946 /* The tsubst call is used to handle cases such as
6948 template <int> class C {};
6949 template <class T, template <T> class TT> class D {};
6950 D<int, C> d;
6952 i.e. the parameter list of TT depends on earlier parameters. */
6953 if (!uses_template_parms (TREE_TYPE (arg)))
6955 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6956 if (!uses_template_parms (t)
6957 && !same_type_p (t, TREE_TYPE (arg)))
6958 return 0;
6961 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6962 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6963 /* Argument is a parameter pack but parameter is not. */
6964 return 0;
6966 break;
6968 default:
6969 gcc_unreachable ();
6972 return 1;
6975 /* Coerce template argument list ARGLIST for use with template
6976 template-parameter TEMPL. */
6978 static tree
6979 coerce_template_args_for_ttp (tree templ, tree arglist,
6980 tsubst_flags_t complain)
6982 /* Consider an example where a template template parameter declared as
6984 template <class T, class U = std::allocator<T> > class TT
6986 The template parameter level of T and U are one level larger than
6987 of TT. To proper process the default argument of U, say when an
6988 instantiation `TT<int>' is seen, we need to build the full
6989 arguments containing {int} as the innermost level. Outer levels,
6990 available when not appearing as default template argument, can be
6991 obtained from the arguments of the enclosing template.
6993 Suppose that TT is later substituted with std::vector. The above
6994 instantiation is `TT<int, std::allocator<T> >' with TT at
6995 level 1, and T at level 2, while the template arguments at level 1
6996 becomes {std::vector} and the inner level 2 is {int}. */
6998 tree outer = DECL_CONTEXT (templ);
6999 if (outer)
7001 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7002 /* We want arguments for the partial specialization, not arguments for
7003 the primary template. */
7004 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7005 else
7006 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7008 else if (current_template_parms)
7010 /* This is an argument of the current template, so we haven't set
7011 DECL_CONTEXT yet. */
7012 tree relevant_template_parms;
7014 /* Parameter levels that are greater than the level of the given
7015 template template parm are irrelevant. */
7016 relevant_template_parms = current_template_parms;
7017 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7018 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7019 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7021 outer = template_parms_to_args (relevant_template_parms);
7024 if (outer)
7025 arglist = add_to_template_args (outer, arglist);
7027 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7028 return coerce_template_parms (parmlist, arglist, templ,
7029 complain,
7030 /*require_all_args=*/true,
7031 /*use_default_args=*/true);
7034 /* A cache of template template parameters with match-all default
7035 arguments. */
7036 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7037 static void
7038 store_defaulted_ttp (tree v, tree t)
7040 if (!defaulted_ttp_cache)
7041 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7042 defaulted_ttp_cache->put (v, t);
7044 static tree
7045 lookup_defaulted_ttp (tree v)
7047 if (defaulted_ttp_cache)
7048 if (tree *p = defaulted_ttp_cache->get (v))
7049 return *p;
7050 return NULL_TREE;
7053 /* T is a bound template template-parameter. Copy its arguments into default
7054 arguments of the template template-parameter's template parameters. */
7056 static tree
7057 add_defaults_to_ttp (tree otmpl)
7059 if (tree c = lookup_defaulted_ttp (otmpl))
7060 return c;
7062 tree ntmpl = copy_node (otmpl);
7064 tree ntype = copy_node (TREE_TYPE (otmpl));
7065 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7066 TYPE_MAIN_VARIANT (ntype) = ntype;
7067 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7068 TYPE_NAME (ntype) = ntmpl;
7069 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7071 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7072 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7073 TEMPLATE_PARM_DECL (idx) = ntmpl;
7074 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7076 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7077 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7078 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7079 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7080 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7082 tree o = TREE_VEC_ELT (vec, i);
7083 if (!template_parameter_pack_p (TREE_VALUE (o)))
7085 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7086 TREE_PURPOSE (n) = any_targ_node;
7090 store_defaulted_ttp (otmpl, ntmpl);
7091 return ntmpl;
7094 /* ARG is a bound potential template template-argument, and PARGS is a list
7095 of arguments for the corresponding template template-parameter. Adjust
7096 PARGS as appropriate for application to ARG's template, and if ARG is a
7097 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7098 arguments to the template template parameter. */
7100 static tree
7101 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7103 ++processing_template_decl;
7104 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7105 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7107 /* When comparing two template template-parameters in partial ordering,
7108 rewrite the one currently being used as an argument to have default
7109 arguments for all parameters. */
7110 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7111 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7112 if (pargs != error_mark_node)
7113 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7114 TYPE_TI_ARGS (arg));
7116 else
7118 tree aparms
7119 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7120 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7121 /*require_all*/true,
7122 /*use_default*/true);
7124 --processing_template_decl;
7125 return pargs;
7128 /* Subroutine of unify for the case when PARM is a
7129 BOUND_TEMPLATE_TEMPLATE_PARM. */
7131 static int
7132 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7133 bool explain_p)
7135 tree parmvec = TYPE_TI_ARGS (parm);
7136 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7138 /* The template template parm might be variadic and the argument
7139 not, so flatten both argument lists. */
7140 parmvec = expand_template_argument_pack (parmvec);
7141 argvec = expand_template_argument_pack (argvec);
7143 if (flag_new_ttp)
7145 /* In keeping with P0522R0, adjust P's template arguments
7146 to apply to A's template; then flatten it again. */
7147 tree nparmvec = parmvec;
7148 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7149 nparmvec = expand_template_argument_pack (nparmvec);
7151 if (unify (tparms, targs, nparmvec, argvec,
7152 UNIFY_ALLOW_NONE, explain_p))
7153 return 1;
7155 /* If the P0522 adjustment eliminated a pack expansion, deduce
7156 empty packs. */
7157 if (flag_new_ttp
7158 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7159 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7160 DEDUCE_EXACT, /*sub*/true, explain_p))
7161 return 1;
7163 else
7165 /* Deduce arguments T, i from TT<T> or TT<i>.
7166 We check each element of PARMVEC and ARGVEC individually
7167 rather than the whole TREE_VEC since they can have
7168 different number of elements, which is allowed under N2555. */
7170 int len = TREE_VEC_LENGTH (parmvec);
7172 /* Check if the parameters end in a pack, making them
7173 variadic. */
7174 int parm_variadic_p = 0;
7175 if (len > 0
7176 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7177 parm_variadic_p = 1;
7179 for (int i = 0; i < len - parm_variadic_p; ++i)
7180 /* If the template argument list of P contains a pack
7181 expansion that is not the last template argument, the
7182 entire template argument list is a non-deduced
7183 context. */
7184 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7185 return unify_success (explain_p);
7187 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7188 return unify_too_few_arguments (explain_p,
7189 TREE_VEC_LENGTH (argvec), len);
7191 for (int i = 0; i < len - parm_variadic_p; ++i)
7192 if (unify (tparms, targs,
7193 TREE_VEC_ELT (parmvec, i),
7194 TREE_VEC_ELT (argvec, i),
7195 UNIFY_ALLOW_NONE, explain_p))
7196 return 1;
7198 if (parm_variadic_p
7199 && unify_pack_expansion (tparms, targs,
7200 parmvec, argvec,
7201 DEDUCE_EXACT,
7202 /*subr=*/true, explain_p))
7203 return 1;
7206 return 0;
7209 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7210 template template parameters. Both PARM_PARMS and ARG_PARMS are
7211 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7212 or PARM_DECL.
7214 Consider the example:
7215 template <class T> class A;
7216 template<template <class U> class TT> class B;
7218 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7219 the parameters to A, and OUTER_ARGS contains A. */
7221 static int
7222 coerce_template_template_parms (tree parm_parms,
7223 tree arg_parms,
7224 tsubst_flags_t complain,
7225 tree in_decl,
7226 tree outer_args)
7228 int nparms, nargs, i;
7229 tree parm, arg;
7230 int variadic_p = 0;
7232 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7233 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7235 nparms = TREE_VEC_LENGTH (parm_parms);
7236 nargs = TREE_VEC_LENGTH (arg_parms);
7238 if (flag_new_ttp)
7240 /* P0522R0: A template template-parameter P is at least as specialized as
7241 a template template-argument A if, given the following rewrite to two
7242 function templates, the function template corresponding to P is at
7243 least as specialized as the function template corresponding to A
7244 according to the partial ordering rules for function templates
7245 ([temp.func.order]). Given an invented class template X with the
7246 template parameter list of A (including default arguments):
7248 * Each of the two function templates has the same template parameters,
7249 respectively, as P or A.
7251 * Each function template has a single function parameter whose type is
7252 a specialization of X with template arguments corresponding to the
7253 template parameters from the respective function template where, for
7254 each template parameter PP in the template parameter list of the
7255 function template, a corresponding template argument AA is formed. If
7256 PP declares a parameter pack, then AA is the pack expansion
7257 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7259 If the rewrite produces an invalid type, then P is not at least as
7260 specialized as A. */
7262 /* So coerce P's args to apply to A's parms, and then deduce between A's
7263 args and the converted args. If that succeeds, A is at least as
7264 specialized as P, so they match.*/
7265 tree pargs = template_parms_level_to_args (parm_parms);
7266 ++processing_template_decl;
7267 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7268 /*require_all*/true, /*use_default*/true);
7269 --processing_template_decl;
7270 if (pargs != error_mark_node)
7272 tree targs = make_tree_vec (nargs);
7273 tree aargs = template_parms_level_to_args (arg_parms);
7274 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7275 /*explain*/false))
7276 return 1;
7280 /* Determine whether we have a parameter pack at the end of the
7281 template template parameter's template parameter list. */
7282 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7284 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7286 if (error_operand_p (parm))
7287 return 0;
7289 switch (TREE_CODE (parm))
7291 case TEMPLATE_DECL:
7292 case TYPE_DECL:
7293 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7294 variadic_p = 1;
7295 break;
7297 case PARM_DECL:
7298 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7299 variadic_p = 1;
7300 break;
7302 default:
7303 gcc_unreachable ();
7307 if (nargs != nparms
7308 && !(variadic_p && nargs >= nparms - 1))
7309 return 0;
7311 /* Check all of the template parameters except the parameter pack at
7312 the end (if any). */
7313 for (i = 0; i < nparms - variadic_p; ++i)
7315 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7316 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7317 continue;
7319 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7320 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7322 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7323 outer_args))
7324 return 0;
7328 if (variadic_p)
7330 /* Check each of the template parameters in the template
7331 argument against the template parameter pack at the end of
7332 the template template parameter. */
7333 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7334 return 0;
7336 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7338 for (; i < nargs; ++i)
7340 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7341 continue;
7343 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7345 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7346 outer_args))
7347 return 0;
7351 return 1;
7354 /* Verifies that the deduced template arguments (in TARGS) for the
7355 template template parameters (in TPARMS) represent valid bindings,
7356 by comparing the template parameter list of each template argument
7357 to the template parameter list of its corresponding template
7358 template parameter, in accordance with DR150. This
7359 routine can only be called after all template arguments have been
7360 deduced. It will return TRUE if all of the template template
7361 parameter bindings are okay, FALSE otherwise. */
7362 bool
7363 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7365 int i, ntparms = TREE_VEC_LENGTH (tparms);
7366 bool ret = true;
7368 /* We're dealing with template parms in this process. */
7369 ++processing_template_decl;
7371 targs = INNERMOST_TEMPLATE_ARGS (targs);
7373 for (i = 0; i < ntparms; ++i)
7375 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7376 tree targ = TREE_VEC_ELT (targs, i);
7378 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7380 tree packed_args = NULL_TREE;
7381 int idx, len = 1;
7383 if (ARGUMENT_PACK_P (targ))
7385 /* Look inside the argument pack. */
7386 packed_args = ARGUMENT_PACK_ARGS (targ);
7387 len = TREE_VEC_LENGTH (packed_args);
7390 for (idx = 0; idx < len; ++idx)
7392 tree targ_parms = NULL_TREE;
7394 if (packed_args)
7395 /* Extract the next argument from the argument
7396 pack. */
7397 targ = TREE_VEC_ELT (packed_args, idx);
7399 if (PACK_EXPANSION_P (targ))
7400 /* Look at the pattern of the pack expansion. */
7401 targ = PACK_EXPANSION_PATTERN (targ);
7403 /* Extract the template parameters from the template
7404 argument. */
7405 if (TREE_CODE (targ) == TEMPLATE_DECL)
7406 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7407 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7408 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7410 /* Verify that we can coerce the template template
7411 parameters from the template argument to the template
7412 parameter. This requires an exact match. */
7413 if (targ_parms
7414 && !coerce_template_template_parms
7415 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7416 targ_parms,
7417 tf_none,
7418 tparm,
7419 targs))
7421 ret = false;
7422 goto out;
7428 out:
7430 --processing_template_decl;
7431 return ret;
7434 /* Since type attributes aren't mangled, we need to strip them from
7435 template type arguments. */
7437 static tree
7438 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7440 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7441 return arg;
7442 bool removed_attributes = false;
7443 tree canon = strip_typedefs (arg, &removed_attributes);
7444 if (removed_attributes
7445 && (complain & tf_warning))
7446 warning (OPT_Wignored_attributes,
7447 "ignoring attributes on template argument %qT", arg);
7448 return canon;
7451 /* And from inside dependent non-type arguments like sizeof(Type). */
7453 static tree
7454 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7456 if (!arg || arg == error_mark_node)
7457 return arg;
7458 bool removed_attributes = false;
7459 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7460 if (removed_attributes
7461 && (complain & tf_warning))
7462 warning (OPT_Wignored_attributes,
7463 "ignoring attributes in template argument %qE", arg);
7464 return canon;
7467 // A template declaration can be substituted for a constrained
7468 // template template parameter only when the argument is more
7469 // constrained than the parameter.
7470 static bool
7471 is_compatible_template_arg (tree parm, tree arg)
7473 tree parm_cons = get_constraints (parm);
7475 /* For now, allow constrained template template arguments
7476 and unconstrained template template parameters. */
7477 if (parm_cons == NULL_TREE)
7478 return true;
7480 tree arg_cons = get_constraints (arg);
7482 // If the template parameter is constrained, we need to rewrite its
7483 // constraints in terms of the ARG's template parameters. This ensures
7484 // that all of the template parameter types will have the same depth.
7486 // Note that this is only valid when coerce_template_template_parm is
7487 // true for the innermost template parameters of PARM and ARG. In other
7488 // words, because coercion is successful, this conversion will be valid.
7489 if (parm_cons)
7491 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7492 parm_cons = tsubst_constraint_info (parm_cons,
7493 INNERMOST_TEMPLATE_ARGS (args),
7494 tf_none, NULL_TREE);
7495 if (parm_cons == error_mark_node)
7496 return false;
7499 return subsumes (parm_cons, arg_cons);
7502 // Convert a placeholder argument into a binding to the original
7503 // parameter. The original parameter is saved as the TREE_TYPE of
7504 // ARG.
7505 static inline tree
7506 convert_wildcard_argument (tree parm, tree arg)
7508 TREE_TYPE (arg) = parm;
7509 return arg;
7512 /* Convert the indicated template ARG as necessary to match the
7513 indicated template PARM. Returns the converted ARG, or
7514 error_mark_node if the conversion was unsuccessful. Error and
7515 warning messages are issued under control of COMPLAIN. This
7516 conversion is for the Ith parameter in the parameter list. ARGS is
7517 the full set of template arguments deduced so far. */
7519 static tree
7520 convert_template_argument (tree parm,
7521 tree arg,
7522 tree args,
7523 tsubst_flags_t complain,
7524 int i,
7525 tree in_decl)
7527 tree orig_arg;
7528 tree val;
7529 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7531 if (parm == error_mark_node)
7532 return error_mark_node;
7534 /* Trivially convert placeholders. */
7535 if (TREE_CODE (arg) == WILDCARD_DECL)
7536 return convert_wildcard_argument (parm, arg);
7538 if (arg == any_targ_node)
7539 return arg;
7541 if (TREE_CODE (arg) == TREE_LIST
7542 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7544 /* The template argument was the name of some
7545 member function. That's usually
7546 invalid, but static members are OK. In any
7547 case, grab the underlying fields/functions
7548 and issue an error later if required. */
7549 orig_arg = TREE_VALUE (arg);
7550 TREE_TYPE (arg) = unknown_type_node;
7553 orig_arg = arg;
7555 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7556 requires_type = (TREE_CODE (parm) == TYPE_DECL
7557 || requires_tmpl_type);
7559 /* When determining whether an argument pack expansion is a template,
7560 look at the pattern. */
7561 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7562 arg = PACK_EXPANSION_PATTERN (arg);
7564 /* Deal with an injected-class-name used as a template template arg. */
7565 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7567 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7568 if (TREE_CODE (t) == TEMPLATE_DECL)
7570 if (cxx_dialect >= cxx11)
7571 /* OK under DR 1004. */;
7572 else if (complain & tf_warning_or_error)
7573 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7574 " used as template template argument", TYPE_NAME (arg));
7575 else if (flag_pedantic_errors)
7576 t = arg;
7578 arg = t;
7582 is_tmpl_type =
7583 ((TREE_CODE (arg) == TEMPLATE_DECL
7584 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7585 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7586 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7587 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7589 if (is_tmpl_type
7590 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7591 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7592 arg = TYPE_STUB_DECL (arg);
7594 is_type = TYPE_P (arg) || is_tmpl_type;
7596 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7597 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7599 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7601 if (complain & tf_error)
7602 error ("invalid use of destructor %qE as a type", orig_arg);
7603 return error_mark_node;
7606 permerror (input_location,
7607 "to refer to a type member of a template parameter, "
7608 "use %<typename %E%>", orig_arg);
7610 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7611 TREE_OPERAND (arg, 1),
7612 typename_type,
7613 complain);
7614 arg = orig_arg;
7615 is_type = 1;
7617 if (is_type != requires_type)
7619 if (in_decl)
7621 if (complain & tf_error)
7623 error ("type/value mismatch at argument %d in template "
7624 "parameter list for %qD",
7625 i + 1, in_decl);
7626 if (is_type)
7627 inform (input_location,
7628 " expected a constant of type %qT, got %qT",
7629 TREE_TYPE (parm),
7630 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7631 else if (requires_tmpl_type)
7632 inform (input_location,
7633 " expected a class template, got %qE", orig_arg);
7634 else
7635 inform (input_location,
7636 " expected a type, got %qE", orig_arg);
7639 return error_mark_node;
7641 if (is_tmpl_type ^ requires_tmpl_type)
7643 if (in_decl && (complain & tf_error))
7645 error ("type/value mismatch at argument %d in template "
7646 "parameter list for %qD",
7647 i + 1, in_decl);
7648 if (is_tmpl_type)
7649 inform (input_location,
7650 " expected a type, got %qT", DECL_NAME (arg));
7651 else
7652 inform (input_location,
7653 " expected a class template, got %qT", orig_arg);
7655 return error_mark_node;
7658 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7659 /* We already did the appropriate conversion when packing args. */
7660 val = orig_arg;
7661 else if (is_type)
7663 if (requires_tmpl_type)
7665 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7666 /* The number of argument required is not known yet.
7667 Just accept it for now. */
7668 val = orig_arg;
7669 else
7671 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7672 tree argparm;
7674 /* Strip alias templates that are equivalent to another
7675 template. */
7676 arg = get_underlying_template (arg);
7677 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7679 if (coerce_template_template_parms (parmparm, argparm,
7680 complain, in_decl,
7681 args))
7683 val = arg;
7685 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7686 TEMPLATE_DECL. */
7687 if (val != error_mark_node)
7689 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7690 val = TREE_TYPE (val);
7691 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7692 val = make_pack_expansion (val);
7695 else
7697 if (in_decl && (complain & tf_error))
7699 error ("type/value mismatch at argument %d in "
7700 "template parameter list for %qD",
7701 i + 1, in_decl);
7702 inform (input_location,
7703 " expected a template of type %qD, got %qT",
7704 parm, orig_arg);
7707 val = error_mark_node;
7710 // Check that the constraints are compatible before allowing the
7711 // substitution.
7712 if (val != error_mark_node)
7713 if (!is_compatible_template_arg (parm, arg))
7715 if (in_decl && (complain & tf_error))
7717 error ("constraint mismatch at argument %d in "
7718 "template parameter list for %qD",
7719 i + 1, in_decl);
7720 inform (input_location, " expected %qD but got %qD",
7721 parm, arg);
7723 val = error_mark_node;
7727 else
7728 val = orig_arg;
7729 /* We only form one instance of each template specialization.
7730 Therefore, if we use a non-canonical variant (i.e., a
7731 typedef), any future messages referring to the type will use
7732 the typedef, which is confusing if those future uses do not
7733 themselves also use the typedef. */
7734 if (TYPE_P (val))
7735 val = canonicalize_type_argument (val, complain);
7737 else
7739 tree t = TREE_TYPE (parm);
7741 if (tree a = type_uses_auto (t))
7743 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7744 if (t == error_mark_node)
7745 return error_mark_node;
7747 else
7748 t = tsubst (t, args, complain, in_decl);
7750 if (invalid_nontype_parm_type_p (t, complain))
7751 return error_mark_node;
7753 if (!type_dependent_expression_p (orig_arg)
7754 && !uses_template_parms (t))
7755 /* We used to call digest_init here. However, digest_init
7756 will report errors, which we don't want when complain
7757 is zero. More importantly, digest_init will try too
7758 hard to convert things: for example, `0' should not be
7759 converted to pointer type at this point according to
7760 the standard. Accepting this is not merely an
7761 extension, since deciding whether or not these
7762 conversions can occur is part of determining which
7763 function template to call, or whether a given explicit
7764 argument specification is valid. */
7765 val = convert_nontype_argument (t, orig_arg, complain);
7766 else
7767 val = canonicalize_expr_argument (orig_arg, complain);
7769 if (val == NULL_TREE)
7770 val = error_mark_node;
7771 else if (val == error_mark_node && (complain & tf_error))
7772 error ("could not convert template argument %qE from %qT to %qT",
7773 orig_arg, TREE_TYPE (orig_arg), t);
7775 if (INDIRECT_REF_P (val))
7777 /* Reject template arguments that are references to built-in
7778 functions with no library fallbacks. */
7779 const_tree inner = TREE_OPERAND (val, 0);
7780 const_tree innertype = TREE_TYPE (inner);
7781 if (innertype
7782 && TREE_CODE (innertype) == REFERENCE_TYPE
7783 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7784 && 0 < TREE_OPERAND_LENGTH (inner)
7785 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7786 return error_mark_node;
7789 if (TREE_CODE (val) == SCOPE_REF)
7791 /* Strip typedefs from the SCOPE_REF. */
7792 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7793 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7794 complain);
7795 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7796 QUALIFIED_NAME_IS_TEMPLATE (val));
7800 return val;
7803 /* Coerces the remaining template arguments in INNER_ARGS (from
7804 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7805 Returns the coerced argument pack. PARM_IDX is the position of this
7806 parameter in the template parameter list. ARGS is the original
7807 template argument list. */
7808 static tree
7809 coerce_template_parameter_pack (tree parms,
7810 int parm_idx,
7811 tree args,
7812 tree inner_args,
7813 int arg_idx,
7814 tree new_args,
7815 int* lost,
7816 tree in_decl,
7817 tsubst_flags_t complain)
7819 tree parm = TREE_VEC_ELT (parms, parm_idx);
7820 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7821 tree packed_args;
7822 tree argument_pack;
7823 tree packed_parms = NULL_TREE;
7825 if (arg_idx > nargs)
7826 arg_idx = nargs;
7828 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7830 /* When the template parameter is a non-type template parameter pack
7831 or template template parameter pack whose type or template
7832 parameters use parameter packs, we know exactly how many arguments
7833 we are looking for. Build a vector of the instantiated decls for
7834 these template parameters in PACKED_PARMS. */
7835 /* We can't use make_pack_expansion here because it would interpret a
7836 _DECL as a use rather than a declaration. */
7837 tree decl = TREE_VALUE (parm);
7838 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7839 SET_PACK_EXPANSION_PATTERN (exp, decl);
7840 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7841 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7843 TREE_VEC_LENGTH (args)--;
7844 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7845 TREE_VEC_LENGTH (args)++;
7847 if (packed_parms == error_mark_node)
7848 return error_mark_node;
7850 /* If we're doing a partial instantiation of a member template,
7851 verify that all of the types used for the non-type
7852 template parameter pack are, in fact, valid for non-type
7853 template parameters. */
7854 if (arg_idx < nargs
7855 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7857 int j, len = TREE_VEC_LENGTH (packed_parms);
7858 for (j = 0; j < len; ++j)
7860 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7861 if (invalid_nontype_parm_type_p (t, complain))
7862 return error_mark_node;
7864 /* We don't know how many args we have yet, just
7865 use the unconverted ones for now. */
7866 return NULL_TREE;
7869 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7871 /* Check if we have a placeholder pack, which indicates we're
7872 in the context of a introduction list. In that case we want
7873 to match this pack to the single placeholder. */
7874 else if (arg_idx < nargs
7875 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7876 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7878 nargs = arg_idx + 1;
7879 packed_args = make_tree_vec (1);
7881 else
7882 packed_args = make_tree_vec (nargs - arg_idx);
7884 /* Convert the remaining arguments, which will be a part of the
7885 parameter pack "parm". */
7886 int first_pack_arg = arg_idx;
7887 for (; arg_idx < nargs; ++arg_idx)
7889 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7890 tree actual_parm = TREE_VALUE (parm);
7891 int pack_idx = arg_idx - first_pack_arg;
7893 if (packed_parms)
7895 /* Once we've packed as many args as we have types, stop. */
7896 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7897 break;
7898 else if (PACK_EXPANSION_P (arg))
7899 /* We don't know how many args we have yet, just
7900 use the unconverted ones for now. */
7901 return NULL_TREE;
7902 else
7903 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7906 if (arg == error_mark_node)
7908 if (complain & tf_error)
7909 error ("template argument %d is invalid", arg_idx + 1);
7911 else
7912 arg = convert_template_argument (actual_parm,
7913 arg, new_args, complain, parm_idx,
7914 in_decl);
7915 if (arg == error_mark_node)
7916 (*lost)++;
7917 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7920 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7921 && TREE_VEC_LENGTH (packed_args) > 0)
7923 if (complain & tf_error)
7924 error ("wrong number of template arguments (%d, should be %d)",
7925 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7926 return error_mark_node;
7929 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7930 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7931 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7932 else
7934 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7935 TREE_CONSTANT (argument_pack) = 1;
7938 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7939 if (CHECKING_P)
7940 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7941 TREE_VEC_LENGTH (packed_args));
7942 return argument_pack;
7945 /* Returns the number of pack expansions in the template argument vector
7946 ARGS. */
7948 static int
7949 pack_expansion_args_count (tree args)
7951 int i;
7952 int count = 0;
7953 if (args)
7954 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7956 tree elt = TREE_VEC_ELT (args, i);
7957 if (elt && PACK_EXPANSION_P (elt))
7958 ++count;
7960 return count;
7963 /* Convert all template arguments to their appropriate types, and
7964 return a vector containing the innermost resulting template
7965 arguments. If any error occurs, return error_mark_node. Error and
7966 warning messages are issued under control of COMPLAIN.
7968 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7969 for arguments not specified in ARGS. Otherwise, if
7970 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7971 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7972 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7973 ARGS. */
7975 static tree
7976 coerce_template_parms (tree parms,
7977 tree args,
7978 tree in_decl,
7979 tsubst_flags_t complain,
7980 bool require_all_args,
7981 bool use_default_args)
7983 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7984 tree orig_inner_args;
7985 tree inner_args;
7986 tree new_args;
7987 tree new_inner_args;
7988 int saved_unevaluated_operand;
7989 int saved_inhibit_evaluation_warnings;
7991 /* When used as a boolean value, indicates whether this is a
7992 variadic template parameter list. Since it's an int, we can also
7993 subtract it from nparms to get the number of non-variadic
7994 parameters. */
7995 int variadic_p = 0;
7996 int variadic_args_p = 0;
7997 int post_variadic_parms = 0;
7999 /* Likewise for parameters with default arguments. */
8000 int default_p = 0;
8002 if (args == error_mark_node)
8003 return error_mark_node;
8005 nparms = TREE_VEC_LENGTH (parms);
8007 /* Determine if there are any parameter packs or default arguments. */
8008 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8010 tree parm = TREE_VEC_ELT (parms, parm_idx);
8011 if (variadic_p)
8012 ++post_variadic_parms;
8013 if (template_parameter_pack_p (TREE_VALUE (parm)))
8014 ++variadic_p;
8015 if (TREE_PURPOSE (parm))
8016 ++default_p;
8019 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8020 /* If there are no parameters that follow a parameter pack, we need to
8021 expand any argument packs so that we can deduce a parameter pack from
8022 some non-packed args followed by an argument pack, as in variadic85.C.
8023 If there are such parameters, we need to leave argument packs intact
8024 so the arguments are assigned properly. This can happen when dealing
8025 with a nested class inside a partial specialization of a class
8026 template, as in variadic92.C, or when deducing a template parameter pack
8027 from a sub-declarator, as in variadic114.C. */
8028 if (!post_variadic_parms)
8029 inner_args = expand_template_argument_pack (inner_args);
8031 /* Count any pack expansion args. */
8032 variadic_args_p = pack_expansion_args_count (inner_args);
8034 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8035 if ((nargs - variadic_args_p > nparms && !variadic_p)
8036 || (nargs < nparms - variadic_p
8037 && require_all_args
8038 && !variadic_args_p
8039 && (!use_default_args
8040 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8041 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8043 if (complain & tf_error)
8045 if (variadic_p || default_p)
8047 nparms -= variadic_p + default_p;
8048 error ("wrong number of template arguments "
8049 "(%d, should be at least %d)", nargs, nparms);
8051 else
8052 error ("wrong number of template arguments "
8053 "(%d, should be %d)", nargs, nparms);
8055 if (in_decl)
8056 inform (DECL_SOURCE_LOCATION (in_decl),
8057 "provided for %qD", in_decl);
8060 return error_mark_node;
8062 /* We can't pass a pack expansion to a non-pack parameter of an alias
8063 template (DR 1430). */
8064 else if (in_decl
8065 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8066 || concept_template_p (in_decl))
8067 && variadic_args_p
8068 && nargs - variadic_args_p < nparms - variadic_p)
8070 if (complain & tf_error)
8072 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8074 tree arg = TREE_VEC_ELT (inner_args, i);
8075 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8077 if (PACK_EXPANSION_P (arg)
8078 && !template_parameter_pack_p (parm))
8080 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8081 error_at (location_of (arg),
8082 "pack expansion argument for non-pack parameter "
8083 "%qD of alias template %qD", parm, in_decl);
8084 else
8085 error_at (location_of (arg),
8086 "pack expansion argument for non-pack parameter "
8087 "%qD of concept %qD", parm, in_decl);
8088 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8089 goto found;
8092 gcc_unreachable ();
8093 found:;
8095 return error_mark_node;
8098 /* We need to evaluate the template arguments, even though this
8099 template-id may be nested within a "sizeof". */
8100 saved_unevaluated_operand = cp_unevaluated_operand;
8101 cp_unevaluated_operand = 0;
8102 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8103 c_inhibit_evaluation_warnings = 0;
8104 new_inner_args = make_tree_vec (nparms);
8105 new_args = add_outermost_template_args (args, new_inner_args);
8106 int pack_adjust = 0;
8107 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8109 tree arg;
8110 tree parm;
8112 /* Get the Ith template parameter. */
8113 parm = TREE_VEC_ELT (parms, parm_idx);
8115 if (parm == error_mark_node)
8117 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8118 continue;
8121 /* Calculate the next argument. */
8122 if (arg_idx < nargs)
8123 arg = TREE_VEC_ELT (inner_args, arg_idx);
8124 else
8125 arg = NULL_TREE;
8127 if (template_parameter_pack_p (TREE_VALUE (parm))
8128 && !(arg && ARGUMENT_PACK_P (arg)))
8130 /* Some arguments will be placed in the
8131 template parameter pack PARM. */
8132 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8133 inner_args, arg_idx,
8134 new_args, &lost,
8135 in_decl, complain);
8137 if (arg == NULL_TREE)
8139 /* We don't know how many args we have yet, just use the
8140 unconverted (and still packed) ones for now. */
8141 new_inner_args = orig_inner_args;
8142 arg_idx = nargs;
8143 break;
8146 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8148 /* Store this argument. */
8149 if (arg == error_mark_node)
8151 lost++;
8152 /* We are done with all of the arguments. */
8153 arg_idx = nargs;
8155 else
8157 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8158 arg_idx += pack_adjust;
8161 continue;
8163 else if (arg)
8165 if (PACK_EXPANSION_P (arg))
8167 /* "If every valid specialization of a variadic template
8168 requires an empty template parameter pack, the template is
8169 ill-formed, no diagnostic required." So check that the
8170 pattern works with this parameter. */
8171 tree pattern = PACK_EXPANSION_PATTERN (arg);
8172 tree conv = convert_template_argument (TREE_VALUE (parm),
8173 pattern, new_args,
8174 complain, parm_idx,
8175 in_decl);
8176 if (conv == error_mark_node)
8178 if (complain & tf_error)
8179 inform (input_location, "so any instantiation with a "
8180 "non-empty parameter pack would be ill-formed");
8181 ++lost;
8183 else if (TYPE_P (conv) && !TYPE_P (pattern))
8184 /* Recover from missing typename. */
8185 TREE_VEC_ELT (inner_args, arg_idx)
8186 = make_pack_expansion (conv);
8188 /* We don't know how many args we have yet, just
8189 use the unconverted ones for now. */
8190 new_inner_args = inner_args;
8191 arg_idx = nargs;
8192 break;
8195 else if (require_all_args)
8197 /* There must be a default arg in this case. */
8198 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8199 complain, in_decl);
8200 /* The position of the first default template argument,
8201 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8202 Record that. */
8203 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8204 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8205 arg_idx - pack_adjust);
8207 else
8208 break;
8210 if (arg == error_mark_node)
8212 if (complain & tf_error)
8213 error ("template argument %d is invalid", arg_idx + 1);
8215 else if (!arg)
8216 /* This only occurs if there was an error in the template
8217 parameter list itself (which we would already have
8218 reported) that we are trying to recover from, e.g., a class
8219 template with a parameter list such as
8220 template<typename..., typename>. */
8221 ++lost;
8222 else
8223 arg = convert_template_argument (TREE_VALUE (parm),
8224 arg, new_args, complain,
8225 parm_idx, in_decl);
8227 if (arg == error_mark_node)
8228 lost++;
8229 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8231 cp_unevaluated_operand = saved_unevaluated_operand;
8232 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8234 if (variadic_p && arg_idx < nargs)
8236 if (complain & tf_error)
8238 error ("wrong number of template arguments "
8239 "(%d, should be %d)", nargs, arg_idx);
8240 if (in_decl)
8241 error ("provided for %q+D", in_decl);
8243 return error_mark_node;
8246 if (lost)
8247 return error_mark_node;
8249 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8250 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8251 TREE_VEC_LENGTH (new_inner_args));
8253 return new_inner_args;
8256 /* Convert all template arguments to their appropriate types, and
8257 return a vector containing the innermost resulting template
8258 arguments. If any error occurs, return error_mark_node. Error and
8259 warning messages are not issued.
8261 Note that no function argument deduction is performed, and default
8262 arguments are used to fill in unspecified arguments. */
8263 tree
8264 coerce_template_parms (tree parms, tree args, tree in_decl)
8266 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8269 /* Convert all template arguments to their appropriate type, and
8270 instantiate default arguments as needed. This returns a vector
8271 containing the innermost resulting template arguments, or
8272 error_mark_node if unsuccessful. */
8273 tree
8274 coerce_template_parms (tree parms, tree args, tree in_decl,
8275 tsubst_flags_t complain)
8277 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8280 /* Like coerce_template_parms. If PARMS represents all template
8281 parameters levels, this function returns a vector of vectors
8282 representing all the resulting argument levels. Note that in this
8283 case, only the innermost arguments are coerced because the
8284 outermost ones are supposed to have been coerced already.
8286 Otherwise, if PARMS represents only (the innermost) vector of
8287 parameters, this function returns a vector containing just the
8288 innermost resulting arguments. */
8290 static tree
8291 coerce_innermost_template_parms (tree parms,
8292 tree args,
8293 tree in_decl,
8294 tsubst_flags_t complain,
8295 bool require_all_args,
8296 bool use_default_args)
8298 int parms_depth = TMPL_PARMS_DEPTH (parms);
8299 int args_depth = TMPL_ARGS_DEPTH (args);
8300 tree coerced_args;
8302 if (parms_depth > 1)
8304 coerced_args = make_tree_vec (parms_depth);
8305 tree level;
8306 int cur_depth;
8308 for (level = parms, cur_depth = parms_depth;
8309 parms_depth > 0 && level != NULL_TREE;
8310 level = TREE_CHAIN (level), --cur_depth)
8312 tree l;
8313 if (cur_depth == args_depth)
8314 l = coerce_template_parms (TREE_VALUE (level),
8315 args, in_decl, complain,
8316 require_all_args,
8317 use_default_args);
8318 else
8319 l = TMPL_ARGS_LEVEL (args, cur_depth);
8321 if (l == error_mark_node)
8322 return error_mark_node;
8324 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8327 else
8328 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8329 args, in_decl, complain,
8330 require_all_args,
8331 use_default_args);
8332 return coerced_args;
8335 /* Returns 1 if template args OT and NT are equivalent. */
8338 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8340 if (nt == ot)
8341 return 1;
8342 if (nt == NULL_TREE || ot == NULL_TREE)
8343 return false;
8344 if (nt == any_targ_node || ot == any_targ_node)
8345 return true;
8347 if (TREE_CODE (nt) == TREE_VEC)
8348 /* For member templates */
8349 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8350 else if (PACK_EXPANSION_P (ot))
8351 return (PACK_EXPANSION_P (nt)
8352 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8353 PACK_EXPANSION_PATTERN (nt))
8354 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8355 PACK_EXPANSION_EXTRA_ARGS (nt)));
8356 else if (ARGUMENT_PACK_P (ot))
8358 int i, len;
8359 tree opack, npack;
8361 if (!ARGUMENT_PACK_P (nt))
8362 return 0;
8364 opack = ARGUMENT_PACK_ARGS (ot);
8365 npack = ARGUMENT_PACK_ARGS (nt);
8366 len = TREE_VEC_LENGTH (opack);
8367 if (TREE_VEC_LENGTH (npack) != len)
8368 return 0;
8369 for (i = 0; i < len; ++i)
8370 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8371 TREE_VEC_ELT (npack, i)))
8372 return 0;
8373 return 1;
8375 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8376 gcc_unreachable ();
8377 else if (TYPE_P (nt))
8379 if (!TYPE_P (ot))
8380 return false;
8381 /* Don't treat an alias template specialization with dependent
8382 arguments as equivalent to its underlying type when used as a
8383 template argument; we need them to be distinct so that we
8384 substitute into the specialization arguments at instantiation
8385 time. And aliases can't be equivalent without being ==, so
8386 we don't need to look any deeper.
8388 During partial ordering, however, we need to treat them normally so
8389 that we can order uses of the same alias with different
8390 cv-qualification (79960). */
8391 if (!partial_order
8392 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8393 return false;
8394 else
8395 return same_type_p (ot, nt);
8397 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8398 return 0;
8399 else
8401 /* Try to treat a template non-type argument that has been converted
8402 to the parameter type as equivalent to one that hasn't yet. */
8403 for (enum tree_code code1 = TREE_CODE (ot);
8404 CONVERT_EXPR_CODE_P (code1)
8405 || code1 == NON_LVALUE_EXPR;
8406 code1 = TREE_CODE (ot))
8407 ot = TREE_OPERAND (ot, 0);
8408 for (enum tree_code code2 = TREE_CODE (nt);
8409 CONVERT_EXPR_CODE_P (code2)
8410 || code2 == NON_LVALUE_EXPR;
8411 code2 = TREE_CODE (nt))
8412 nt = TREE_OPERAND (nt, 0);
8414 return cp_tree_equal (ot, nt);
8418 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8419 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8420 NEWARG_PTR with the offending arguments if they are non-NULL. */
8423 comp_template_args (tree oldargs, tree newargs,
8424 tree *oldarg_ptr, tree *newarg_ptr,
8425 bool partial_order)
8427 int i;
8429 if (oldargs == newargs)
8430 return 1;
8432 if (!oldargs || !newargs)
8433 return 0;
8435 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8436 return 0;
8438 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8440 tree nt = TREE_VEC_ELT (newargs, i);
8441 tree ot = TREE_VEC_ELT (oldargs, i);
8443 if (! template_args_equal (ot, nt, partial_order))
8445 if (oldarg_ptr != NULL)
8446 *oldarg_ptr = ot;
8447 if (newarg_ptr != NULL)
8448 *newarg_ptr = nt;
8449 return 0;
8452 return 1;
8455 inline bool
8456 comp_template_args_porder (tree oargs, tree nargs)
8458 return comp_template_args (oargs, nargs, NULL, NULL, true);
8461 static void
8462 add_pending_template (tree d)
8464 tree ti = (TYPE_P (d)
8465 ? CLASSTYPE_TEMPLATE_INFO (d)
8466 : DECL_TEMPLATE_INFO (d));
8467 struct pending_template *pt;
8468 int level;
8470 if (TI_PENDING_TEMPLATE_FLAG (ti))
8471 return;
8473 /* We are called both from instantiate_decl, where we've already had a
8474 tinst_level pushed, and instantiate_template, where we haven't.
8475 Compensate. */
8476 level = !current_tinst_level || current_tinst_level->decl != d;
8478 if (level)
8479 push_tinst_level (d);
8481 pt = ggc_alloc<pending_template> ();
8482 pt->next = NULL;
8483 pt->tinst = current_tinst_level;
8484 if (last_pending_template)
8485 last_pending_template->next = pt;
8486 else
8487 pending_templates = pt;
8489 last_pending_template = pt;
8491 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8493 if (level)
8494 pop_tinst_level ();
8498 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8499 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8500 documentation for TEMPLATE_ID_EXPR. */
8502 tree
8503 lookup_template_function (tree fns, tree arglist)
8505 tree type;
8507 if (fns == error_mark_node || arglist == error_mark_node)
8508 return error_mark_node;
8510 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8512 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8514 error ("%q#D is not a function template", fns);
8515 return error_mark_node;
8518 if (BASELINK_P (fns))
8520 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8521 unknown_type_node,
8522 BASELINK_FUNCTIONS (fns),
8523 arglist);
8524 return fns;
8527 type = TREE_TYPE (fns);
8528 if (TREE_CODE (fns) == OVERLOAD || !type)
8529 type = unknown_type_node;
8531 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8534 /* Within the scope of a template class S<T>, the name S gets bound
8535 (in build_self_reference) to a TYPE_DECL for the class, not a
8536 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8537 or one of its enclosing classes, and that type is a template,
8538 return the associated TEMPLATE_DECL. Otherwise, the original
8539 DECL is returned.
8541 Also handle the case when DECL is a TREE_LIST of ambiguous
8542 injected-class-names from different bases. */
8544 tree
8545 maybe_get_template_decl_from_type_decl (tree decl)
8547 if (decl == NULL_TREE)
8548 return decl;
8550 /* DR 176: A lookup that finds an injected-class-name (10.2
8551 [class.member.lookup]) can result in an ambiguity in certain cases
8552 (for example, if it is found in more than one base class). If all of
8553 the injected-class-names that are found refer to specializations of
8554 the same class template, and if the name is followed by a
8555 template-argument-list, the reference refers to the class template
8556 itself and not a specialization thereof, and is not ambiguous. */
8557 if (TREE_CODE (decl) == TREE_LIST)
8559 tree t, tmpl = NULL_TREE;
8560 for (t = decl; t; t = TREE_CHAIN (t))
8562 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8563 if (!tmpl)
8564 tmpl = elt;
8565 else if (tmpl != elt)
8566 break;
8568 if (tmpl && t == NULL_TREE)
8569 return tmpl;
8570 else
8571 return decl;
8574 return (decl != NULL_TREE
8575 && DECL_SELF_REFERENCE_P (decl)
8576 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8577 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8580 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8581 parameters, find the desired type.
8583 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8585 IN_DECL, if non-NULL, is the template declaration we are trying to
8586 instantiate.
8588 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8589 the class we are looking up.
8591 Issue error and warning messages under control of COMPLAIN.
8593 If the template class is really a local class in a template
8594 function, then the FUNCTION_CONTEXT is the function in which it is
8595 being instantiated.
8597 ??? Note that this function is currently called *twice* for each
8598 template-id: the first time from the parser, while creating the
8599 incomplete type (finish_template_type), and the second type during the
8600 real instantiation (instantiate_template_class). This is surely something
8601 that we want to avoid. It also causes some problems with argument
8602 coercion (see convert_nontype_argument for more information on this). */
8604 static tree
8605 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8606 int entering_scope, tsubst_flags_t complain)
8608 tree templ = NULL_TREE, parmlist;
8609 tree t;
8610 spec_entry **slot;
8611 spec_entry *entry;
8612 spec_entry elt;
8613 hashval_t hash;
8615 if (identifier_p (d1))
8617 tree value = innermost_non_namespace_value (d1);
8618 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8619 templ = value;
8620 else
8622 if (context)
8623 push_decl_namespace (context);
8624 templ = lookup_name (d1);
8625 templ = maybe_get_template_decl_from_type_decl (templ);
8626 if (context)
8627 pop_decl_namespace ();
8629 if (templ)
8630 context = DECL_CONTEXT (templ);
8632 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8634 tree type = TREE_TYPE (d1);
8636 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8637 an implicit typename for the second A. Deal with it. */
8638 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8639 type = TREE_TYPE (type);
8641 if (CLASSTYPE_TEMPLATE_INFO (type))
8643 templ = CLASSTYPE_TI_TEMPLATE (type);
8644 d1 = DECL_NAME (templ);
8647 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8648 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8650 templ = TYPE_TI_TEMPLATE (d1);
8651 d1 = DECL_NAME (templ);
8653 else if (DECL_TYPE_TEMPLATE_P (d1))
8655 templ = d1;
8656 d1 = DECL_NAME (templ);
8657 context = DECL_CONTEXT (templ);
8659 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8661 templ = d1;
8662 d1 = DECL_NAME (templ);
8665 /* Issue an error message if we didn't find a template. */
8666 if (! templ)
8668 if (complain & tf_error)
8669 error ("%qT is not a template", d1);
8670 return error_mark_node;
8673 if (TREE_CODE (templ) != TEMPLATE_DECL
8674 /* Make sure it's a user visible template, if it was named by
8675 the user. */
8676 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8677 && !PRIMARY_TEMPLATE_P (templ)))
8679 if (complain & tf_error)
8681 error ("non-template type %qT used as a template", d1);
8682 if (in_decl)
8683 error ("for template declaration %q+D", in_decl);
8685 return error_mark_node;
8688 complain &= ~tf_user;
8690 /* An alias that just changes the name of a template is equivalent to the
8691 other template, so if any of the arguments are pack expansions, strip
8692 the alias to avoid problems with a pack expansion passed to a non-pack
8693 alias template parameter (DR 1430). */
8694 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8695 templ = get_underlying_template (templ);
8697 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8699 tree parm;
8700 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8701 if (arglist2 == error_mark_node
8702 || (!uses_template_parms (arglist2)
8703 && check_instantiated_args (templ, arglist2, complain)))
8704 return error_mark_node;
8706 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8707 return parm;
8709 else
8711 tree template_type = TREE_TYPE (templ);
8712 tree gen_tmpl;
8713 tree type_decl;
8714 tree found = NULL_TREE;
8715 int arg_depth;
8716 int parm_depth;
8717 int is_dependent_type;
8718 int use_partial_inst_tmpl = false;
8720 if (template_type == error_mark_node)
8721 /* An error occurred while building the template TEMPL, and a
8722 diagnostic has most certainly been emitted for that
8723 already. Let's propagate that error. */
8724 return error_mark_node;
8726 gen_tmpl = most_general_template (templ);
8727 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8728 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8729 arg_depth = TMPL_ARGS_DEPTH (arglist);
8731 if (arg_depth == 1 && parm_depth > 1)
8733 /* We've been given an incomplete set of template arguments.
8734 For example, given:
8736 template <class T> struct S1 {
8737 template <class U> struct S2 {};
8738 template <class U> struct S2<U*> {};
8741 we will be called with an ARGLIST of `U*', but the
8742 TEMPLATE will be `template <class T> template
8743 <class U> struct S1<T>::S2'. We must fill in the missing
8744 arguments. */
8745 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8746 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8747 arg_depth = TMPL_ARGS_DEPTH (arglist);
8750 /* Now we should have enough arguments. */
8751 gcc_assert (parm_depth == arg_depth);
8753 /* From here on, we're only interested in the most general
8754 template. */
8756 /* Calculate the BOUND_ARGS. These will be the args that are
8757 actually tsubst'd into the definition to create the
8758 instantiation. */
8759 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8760 complain,
8761 /*require_all_args=*/true,
8762 /*use_default_args=*/true);
8764 if (arglist == error_mark_node)
8765 /* We were unable to bind the arguments. */
8766 return error_mark_node;
8768 /* In the scope of a template class, explicit references to the
8769 template class refer to the type of the template, not any
8770 instantiation of it. For example, in:
8772 template <class T> class C { void f(C<T>); }
8774 the `C<T>' is just the same as `C'. Outside of the
8775 class, however, such a reference is an instantiation. */
8776 if (entering_scope
8777 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8778 || currently_open_class (template_type))
8780 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8782 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8783 return template_type;
8786 /* If we already have this specialization, return it. */
8787 elt.tmpl = gen_tmpl;
8788 elt.args = arglist;
8789 elt.spec = NULL_TREE;
8790 hash = spec_hasher::hash (&elt);
8791 entry = type_specializations->find_with_hash (&elt, hash);
8793 if (entry)
8794 return entry->spec;
8796 /* If the the template's constraints are not satisfied,
8797 then we cannot form a valid type.
8799 Note that the check is deferred until after the hash
8800 lookup. This prevents redundant checks on previously
8801 instantiated specializations. */
8802 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8804 if (complain & tf_error)
8806 error ("template constraint failure");
8807 diagnose_constraints (input_location, gen_tmpl, arglist);
8809 return error_mark_node;
8812 is_dependent_type = uses_template_parms (arglist);
8814 /* If the deduced arguments are invalid, then the binding
8815 failed. */
8816 if (!is_dependent_type
8817 && check_instantiated_args (gen_tmpl,
8818 INNERMOST_TEMPLATE_ARGS (arglist),
8819 complain))
8820 return error_mark_node;
8822 if (!is_dependent_type
8823 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8824 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8825 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8827 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8828 DECL_NAME (gen_tmpl),
8829 /*tag_scope=*/ts_global);
8830 return found;
8833 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8834 complain, in_decl);
8835 if (context == error_mark_node)
8836 return error_mark_node;
8838 if (!context)
8839 context = global_namespace;
8841 /* Create the type. */
8842 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8844 /* The user referred to a specialization of an alias
8845 template represented by GEN_TMPL.
8847 [temp.alias]/2 says:
8849 When a template-id refers to the specialization of an
8850 alias template, it is equivalent to the associated
8851 type obtained by substitution of its
8852 template-arguments for the template-parameters in the
8853 type-id of the alias template. */
8855 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8856 /* Note that the call above (by indirectly calling
8857 register_specialization in tsubst_decl) registers the
8858 TYPE_DECL representing the specialization of the alias
8859 template. So next time someone substitutes ARGLIST for
8860 the template parms into the alias template (GEN_TMPL),
8861 she'll get that TYPE_DECL back. */
8863 if (t == error_mark_node)
8864 return t;
8866 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8868 if (!is_dependent_type)
8870 set_current_access_from_decl (TYPE_NAME (template_type));
8871 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8872 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8873 arglist, complain, in_decl),
8874 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8875 arglist, complain, in_decl),
8876 SCOPED_ENUM_P (template_type), NULL);
8878 if (t == error_mark_node)
8879 return t;
8881 else
8883 /* We don't want to call start_enum for this type, since
8884 the values for the enumeration constants may involve
8885 template parameters. And, no one should be interested
8886 in the enumeration constants for such a type. */
8887 t = cxx_make_type (ENUMERAL_TYPE);
8888 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8890 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8891 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8892 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8894 else if (CLASS_TYPE_P (template_type))
8896 t = make_class_type (TREE_CODE (template_type));
8897 CLASSTYPE_DECLARED_CLASS (t)
8898 = CLASSTYPE_DECLARED_CLASS (template_type);
8899 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8901 /* A local class. Make sure the decl gets registered properly. */
8902 if (context == current_function_decl)
8903 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8905 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8906 /* This instantiation is another name for the primary
8907 template type. Set the TYPE_CANONICAL field
8908 appropriately. */
8909 TYPE_CANONICAL (t) = template_type;
8910 else if (any_template_arguments_need_structural_equality_p (arglist))
8911 /* Some of the template arguments require structural
8912 equality testing, so this template class requires
8913 structural equality testing. */
8914 SET_TYPE_STRUCTURAL_EQUALITY (t);
8916 else
8917 gcc_unreachable ();
8919 /* If we called start_enum or pushtag above, this information
8920 will already be set up. */
8921 if (!TYPE_NAME (t))
8923 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8925 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8926 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8927 DECL_SOURCE_LOCATION (type_decl)
8928 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8930 else
8931 type_decl = TYPE_NAME (t);
8933 if (CLASS_TYPE_P (template_type))
8935 TREE_PRIVATE (type_decl)
8936 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8937 TREE_PROTECTED (type_decl)
8938 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8939 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8941 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8942 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8946 if (OVERLOAD_TYPE_P (t)
8947 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8949 static const char *tags[] = {"abi_tag", "may_alias"};
8951 for (unsigned ix = 0; ix != 2; ix++)
8953 tree attributes
8954 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8956 if (attributes)
8957 TYPE_ATTRIBUTES (t)
8958 = tree_cons (TREE_PURPOSE (attributes),
8959 TREE_VALUE (attributes),
8960 TYPE_ATTRIBUTES (t));
8964 /* Let's consider the explicit specialization of a member
8965 of a class template specialization that is implicitly instantiated,
8966 e.g.:
8967 template<class T>
8968 struct S
8970 template<class U> struct M {}; //#0
8973 template<>
8974 template<>
8975 struct S<int>::M<char> //#1
8977 int i;
8979 [temp.expl.spec]/4 says this is valid.
8981 In this case, when we write:
8982 S<int>::M<char> m;
8984 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8985 the one of #0.
8987 When we encounter #1, we want to store the partial instantiation
8988 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8990 For all cases other than this "explicit specialization of member of a
8991 class template", we just want to store the most general template into
8992 the CLASSTYPE_TI_TEMPLATE of M.
8994 This case of "explicit specialization of member of a class template"
8995 only happens when:
8996 1/ the enclosing class is an instantiation of, and therefore not
8997 the same as, the context of the most general template, and
8998 2/ we aren't looking at the partial instantiation itself, i.e.
8999 the innermost arguments are not the same as the innermost parms of
9000 the most general template.
9002 So it's only when 1/ and 2/ happens that we want to use the partial
9003 instantiation of the member template in lieu of its most general
9004 template. */
9006 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9007 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9008 /* the enclosing class must be an instantiation... */
9009 && CLASS_TYPE_P (context)
9010 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9012 TREE_VEC_LENGTH (arglist)--;
9013 ++processing_template_decl;
9014 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9015 tree partial_inst_args =
9016 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9017 arglist, complain, NULL_TREE);
9018 --processing_template_decl;
9019 TREE_VEC_LENGTH (arglist)++;
9020 if (partial_inst_args == error_mark_node)
9021 return error_mark_node;
9022 use_partial_inst_tmpl =
9023 /*...and we must not be looking at the partial instantiation
9024 itself. */
9025 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9026 partial_inst_args);
9029 if (!use_partial_inst_tmpl)
9030 /* This case is easy; there are no member templates involved. */
9031 found = gen_tmpl;
9032 else
9034 /* This is a full instantiation of a member template. Find
9035 the partial instantiation of which this is an instance. */
9037 /* Temporarily reduce by one the number of levels in the ARGLIST
9038 so as to avoid comparing the last set of arguments. */
9039 TREE_VEC_LENGTH (arglist)--;
9040 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9041 TREE_VEC_LENGTH (arglist)++;
9042 /* FOUND is either a proper class type, or an alias
9043 template specialization. In the later case, it's a
9044 TYPE_DECL, resulting from the substituting of arguments
9045 for parameters in the TYPE_DECL of the alias template
9046 done earlier. So be careful while getting the template
9047 of FOUND. */
9048 found = (TREE_CODE (found) == TEMPLATE_DECL
9049 ? found
9050 : (TREE_CODE (found) == TYPE_DECL
9051 ? DECL_TI_TEMPLATE (found)
9052 : CLASSTYPE_TI_TEMPLATE (found)));
9055 // Build template info for the new specialization.
9056 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9058 elt.spec = t;
9059 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9060 entry = ggc_alloc<spec_entry> ();
9061 *entry = elt;
9062 *slot = entry;
9064 /* Note this use of the partial instantiation so we can check it
9065 later in maybe_process_partial_specialization. */
9066 DECL_TEMPLATE_INSTANTIATIONS (found)
9067 = tree_cons (arglist, t,
9068 DECL_TEMPLATE_INSTANTIATIONS (found));
9070 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9071 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9072 /* Now that the type has been registered on the instantiations
9073 list, we set up the enumerators. Because the enumeration
9074 constants may involve the enumeration type itself, we make
9075 sure to register the type first, and then create the
9076 constants. That way, doing tsubst_expr for the enumeration
9077 constants won't result in recursive calls here; we'll find
9078 the instantiation and exit above. */
9079 tsubst_enum (template_type, t, arglist);
9081 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9082 /* If the type makes use of template parameters, the
9083 code that generates debugging information will crash. */
9084 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9086 /* Possibly limit visibility based on template args. */
9087 TREE_PUBLIC (type_decl) = 1;
9088 determine_visibility (type_decl);
9090 inherit_targ_abi_tags (t);
9092 return t;
9096 /* Wrapper for lookup_template_class_1. */
9098 tree
9099 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9100 int entering_scope, tsubst_flags_t complain)
9102 tree ret;
9103 timevar_push (TV_TEMPLATE_INST);
9104 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9105 entering_scope, complain);
9106 timevar_pop (TV_TEMPLATE_INST);
9107 return ret;
9110 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9112 tree
9113 lookup_template_variable (tree templ, tree arglist)
9115 /* The type of the expression is NULL_TREE since the template-id could refer
9116 to an explicit or partial specialization. */
9117 tree type = NULL_TREE;
9118 if (flag_concepts && variable_concept_p (templ))
9119 /* Except that concepts are always bool. */
9120 type = boolean_type_node;
9121 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9124 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9126 tree
9127 finish_template_variable (tree var, tsubst_flags_t complain)
9129 tree templ = TREE_OPERAND (var, 0);
9130 tree arglist = TREE_OPERAND (var, 1);
9132 /* We never want to return a VAR_DECL for a variable concept, since they
9133 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9134 bool concept_p = flag_concepts && variable_concept_p (templ);
9135 if (concept_p && processing_template_decl)
9136 return var;
9138 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9139 arglist = add_outermost_template_args (tmpl_args, arglist);
9141 templ = most_general_template (templ);
9142 tree parms = DECL_TEMPLATE_PARMS (templ);
9143 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9144 /*req_all*/true,
9145 /*use_default*/true);
9147 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9149 if (complain & tf_error)
9151 error ("use of invalid variable template %qE", var);
9152 diagnose_constraints (location_of (var), templ, arglist);
9154 return error_mark_node;
9157 /* If a template-id refers to a specialization of a variable
9158 concept, then the expression is true if and only if the
9159 concept's constraints are satisfied by the given template
9160 arguments.
9162 NOTE: This is an extension of Concepts Lite TS that
9163 allows constraints to be used in expressions. */
9164 if (concept_p)
9166 tree decl = DECL_TEMPLATE_RESULT (templ);
9167 return evaluate_variable_concept (decl, arglist);
9170 return instantiate_template (templ, arglist, complain);
9173 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9174 TARGS template args, and instantiate it if it's not dependent. */
9176 tree
9177 lookup_and_finish_template_variable (tree templ, tree targs,
9178 tsubst_flags_t complain)
9180 templ = lookup_template_variable (templ, targs);
9181 if (!any_dependent_template_arguments_p (targs))
9183 templ = finish_template_variable (templ, complain);
9184 mark_used (templ);
9187 return convert_from_reference (templ);
9191 struct pair_fn_data
9193 tree_fn_t fn;
9194 tree_fn_t any_fn;
9195 void *data;
9196 /* True when we should also visit template parameters that occur in
9197 non-deduced contexts. */
9198 bool include_nondeduced_p;
9199 hash_set<tree> *visited;
9202 /* Called from for_each_template_parm via walk_tree. */
9204 static tree
9205 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9207 tree t = *tp;
9208 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9209 tree_fn_t fn = pfd->fn;
9210 void *data = pfd->data;
9211 tree result = NULL_TREE;
9213 #define WALK_SUBTREE(NODE) \
9214 do \
9216 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9217 pfd->include_nondeduced_p, \
9218 pfd->any_fn); \
9219 if (result) goto out; \
9221 while (0)
9223 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9224 return t;
9226 if (TYPE_P (t)
9227 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9228 WALK_SUBTREE (TYPE_CONTEXT (t));
9230 switch (TREE_CODE (t))
9232 case RECORD_TYPE:
9233 if (TYPE_PTRMEMFUNC_P (t))
9234 break;
9235 /* Fall through. */
9237 case UNION_TYPE:
9238 case ENUMERAL_TYPE:
9239 if (!TYPE_TEMPLATE_INFO (t))
9240 *walk_subtrees = 0;
9241 else
9242 WALK_SUBTREE (TYPE_TI_ARGS (t));
9243 break;
9245 case INTEGER_TYPE:
9246 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9247 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9248 break;
9250 case METHOD_TYPE:
9251 /* Since we're not going to walk subtrees, we have to do this
9252 explicitly here. */
9253 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9254 /* Fall through. */
9256 case FUNCTION_TYPE:
9257 /* Check the return type. */
9258 WALK_SUBTREE (TREE_TYPE (t));
9260 /* Check the parameter types. Since default arguments are not
9261 instantiated until they are needed, the TYPE_ARG_TYPES may
9262 contain expressions that involve template parameters. But,
9263 no-one should be looking at them yet. And, once they're
9264 instantiated, they don't contain template parameters, so
9265 there's no point in looking at them then, either. */
9267 tree parm;
9269 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9270 WALK_SUBTREE (TREE_VALUE (parm));
9272 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9273 want walk_tree walking into them itself. */
9274 *walk_subtrees = 0;
9277 if (flag_noexcept_type)
9279 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9280 if (spec)
9281 WALK_SUBTREE (TREE_PURPOSE (spec));
9283 break;
9285 case TYPEOF_TYPE:
9286 case UNDERLYING_TYPE:
9287 if (pfd->include_nondeduced_p
9288 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9289 pfd->visited,
9290 pfd->include_nondeduced_p,
9291 pfd->any_fn))
9292 return error_mark_node;
9293 break;
9295 case FUNCTION_DECL:
9296 case VAR_DECL:
9297 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9298 WALK_SUBTREE (DECL_TI_ARGS (t));
9299 /* Fall through. */
9301 case PARM_DECL:
9302 case CONST_DECL:
9303 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9304 WALK_SUBTREE (DECL_INITIAL (t));
9305 if (DECL_CONTEXT (t)
9306 && pfd->include_nondeduced_p)
9307 WALK_SUBTREE (DECL_CONTEXT (t));
9308 break;
9310 case BOUND_TEMPLATE_TEMPLATE_PARM:
9311 /* Record template parameters such as `T' inside `TT<T>'. */
9312 WALK_SUBTREE (TYPE_TI_ARGS (t));
9313 /* Fall through. */
9315 case TEMPLATE_TEMPLATE_PARM:
9316 case TEMPLATE_TYPE_PARM:
9317 case TEMPLATE_PARM_INDEX:
9318 if (fn && (*fn)(t, data))
9319 return t;
9320 else if (!fn)
9321 return t;
9322 break;
9324 case TEMPLATE_DECL:
9325 /* A template template parameter is encountered. */
9326 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9327 WALK_SUBTREE (TREE_TYPE (t));
9329 /* Already substituted template template parameter */
9330 *walk_subtrees = 0;
9331 break;
9333 case TYPENAME_TYPE:
9334 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9335 partial instantiation. */
9336 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9337 break;
9339 case CONSTRUCTOR:
9340 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9341 && pfd->include_nondeduced_p)
9342 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9343 break;
9345 case INDIRECT_REF:
9346 case COMPONENT_REF:
9347 /* If there's no type, then this thing must be some expression
9348 involving template parameters. */
9349 if (!fn && !TREE_TYPE (t))
9350 return error_mark_node;
9351 break;
9353 case MODOP_EXPR:
9354 case CAST_EXPR:
9355 case IMPLICIT_CONV_EXPR:
9356 case REINTERPRET_CAST_EXPR:
9357 case CONST_CAST_EXPR:
9358 case STATIC_CAST_EXPR:
9359 case DYNAMIC_CAST_EXPR:
9360 case ARROW_EXPR:
9361 case DOTSTAR_EXPR:
9362 case TYPEID_EXPR:
9363 case PSEUDO_DTOR_EXPR:
9364 if (!fn)
9365 return error_mark_node;
9366 break;
9368 default:
9369 break;
9372 #undef WALK_SUBTREE
9374 /* We didn't find any template parameters we liked. */
9375 out:
9376 return result;
9379 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9380 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9381 call FN with the parameter and the DATA.
9382 If FN returns nonzero, the iteration is terminated, and
9383 for_each_template_parm returns 1. Otherwise, the iteration
9384 continues. If FN never returns a nonzero value, the value
9385 returned by for_each_template_parm is 0. If FN is NULL, it is
9386 considered to be the function which always returns 1.
9388 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9389 parameters that occur in non-deduced contexts. When false, only
9390 visits those template parameters that can be deduced. */
9392 static tree
9393 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9394 hash_set<tree> *visited,
9395 bool include_nondeduced_p,
9396 tree_fn_t any_fn)
9398 struct pair_fn_data pfd;
9399 tree result;
9401 /* Set up. */
9402 pfd.fn = fn;
9403 pfd.any_fn = any_fn;
9404 pfd.data = data;
9405 pfd.include_nondeduced_p = include_nondeduced_p;
9407 /* Walk the tree. (Conceptually, we would like to walk without
9408 duplicates, but for_each_template_parm_r recursively calls
9409 for_each_template_parm, so we would need to reorganize a fair
9410 bit to use walk_tree_without_duplicates, so we keep our own
9411 visited list.) */
9412 if (visited)
9413 pfd.visited = visited;
9414 else
9415 pfd.visited = new hash_set<tree>;
9416 result = cp_walk_tree (&t,
9417 for_each_template_parm_r,
9418 &pfd,
9419 pfd.visited);
9421 /* Clean up. */
9422 if (!visited)
9424 delete pfd.visited;
9425 pfd.visited = 0;
9428 return result;
9431 /* Returns true if T depends on any template parameter. */
9434 uses_template_parms (tree t)
9436 if (t == NULL_TREE)
9437 return false;
9439 bool dependent_p;
9440 int saved_processing_template_decl;
9442 saved_processing_template_decl = processing_template_decl;
9443 if (!saved_processing_template_decl)
9444 processing_template_decl = 1;
9445 if (TYPE_P (t))
9446 dependent_p = dependent_type_p (t);
9447 else if (TREE_CODE (t) == TREE_VEC)
9448 dependent_p = any_dependent_template_arguments_p (t);
9449 else if (TREE_CODE (t) == TREE_LIST)
9450 dependent_p = (uses_template_parms (TREE_VALUE (t))
9451 || uses_template_parms (TREE_CHAIN (t)));
9452 else if (TREE_CODE (t) == TYPE_DECL)
9453 dependent_p = dependent_type_p (TREE_TYPE (t));
9454 else if (DECL_P (t)
9455 || EXPR_P (t)
9456 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9457 || TREE_CODE (t) == OVERLOAD
9458 || BASELINK_P (t)
9459 || identifier_p (t)
9460 || TREE_CODE (t) == TRAIT_EXPR
9461 || TREE_CODE (t) == CONSTRUCTOR
9462 || CONSTANT_CLASS_P (t))
9463 dependent_p = (type_dependent_expression_p (t)
9464 || value_dependent_expression_p (t));
9465 else
9467 gcc_assert (t == error_mark_node);
9468 dependent_p = false;
9471 processing_template_decl = saved_processing_template_decl;
9473 return dependent_p;
9476 /* Returns true iff current_function_decl is an incompletely instantiated
9477 template. Useful instead of processing_template_decl because the latter
9478 is set to 0 during instantiate_non_dependent_expr. */
9480 bool
9481 in_template_function (void)
9483 tree fn = current_function_decl;
9484 bool ret;
9485 ++processing_template_decl;
9486 ret = (fn && DECL_LANG_SPECIFIC (fn)
9487 && DECL_TEMPLATE_INFO (fn)
9488 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9489 --processing_template_decl;
9490 return ret;
9493 /* Returns true if T depends on any template parameter with level LEVEL. */
9495 bool
9496 uses_template_parms_level (tree t, int level)
9498 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9499 /*include_nondeduced_p=*/true);
9502 /* Returns true if the signature of DECL depends on any template parameter from
9503 its enclosing class. */
9505 bool
9506 uses_outer_template_parms (tree decl)
9508 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9509 if (depth == 0)
9510 return false;
9511 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9512 &depth, NULL, /*include_nondeduced_p=*/true))
9513 return true;
9514 if (PRIMARY_TEMPLATE_P (decl)
9515 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9516 (DECL_TEMPLATE_PARMS (decl)),
9517 template_parm_outer_level,
9518 &depth, NULL, /*include_nondeduced_p=*/true))
9519 return true;
9520 tree ci = get_constraints (decl);
9521 if (ci)
9522 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9523 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9524 &depth, NULL, /*nondeduced*/true))
9525 return true;
9526 return false;
9529 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9530 ill-formed translation unit, i.e. a variable or function that isn't
9531 usable in a constant expression. */
9533 static inline bool
9534 neglectable_inst_p (tree d)
9536 return (DECL_P (d)
9537 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9538 : decl_maybe_constant_var_p (d)));
9541 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9542 neglectable and instantiated from within an erroneous instantiation. */
9544 static bool
9545 limit_bad_template_recursion (tree decl)
9547 struct tinst_level *lev = current_tinst_level;
9548 int errs = errorcount + sorrycount;
9549 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9550 return false;
9552 for (; lev; lev = lev->next)
9553 if (neglectable_inst_p (lev->decl))
9554 break;
9556 return (lev && errs > lev->errors);
9559 static int tinst_depth;
9560 extern int max_tinst_depth;
9561 int depth_reached;
9563 static GTY(()) struct tinst_level *last_error_tinst_level;
9565 /* We're starting to instantiate D; record the template instantiation context
9566 for diagnostics and to restore it later. */
9568 bool
9569 push_tinst_level (tree d)
9571 return push_tinst_level_loc (d, input_location);
9574 /* We're starting to instantiate D; record the template instantiation context
9575 at LOC for diagnostics and to restore it later. */
9577 bool
9578 push_tinst_level_loc (tree d, location_t loc)
9580 struct tinst_level *new_level;
9582 if (tinst_depth >= max_tinst_depth)
9584 /* Tell error.c not to try to instantiate any templates. */
9585 at_eof = 2;
9586 fatal_error (input_location,
9587 "template instantiation depth exceeds maximum of %d"
9588 " (use -ftemplate-depth= to increase the maximum)",
9589 max_tinst_depth);
9590 return false;
9593 /* If the current instantiation caused problems, don't let it instantiate
9594 anything else. Do allow deduction substitution and decls usable in
9595 constant expressions. */
9596 if (limit_bad_template_recursion (d))
9597 return false;
9599 /* When not -quiet, dump template instantiations other than functions, since
9600 announce_function will take care of those. */
9601 if (!quiet_flag
9602 && TREE_CODE (d) != TREE_LIST
9603 && TREE_CODE (d) != FUNCTION_DECL)
9604 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9606 new_level = ggc_alloc<tinst_level> ();
9607 new_level->decl = d;
9608 new_level->locus = loc;
9609 new_level->errors = errorcount+sorrycount;
9610 new_level->in_system_header_p = in_system_header_at (input_location);
9611 new_level->next = current_tinst_level;
9612 current_tinst_level = new_level;
9614 ++tinst_depth;
9615 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9616 depth_reached = tinst_depth;
9618 return true;
9621 /* We're done instantiating this template; return to the instantiation
9622 context. */
9624 void
9625 pop_tinst_level (void)
9627 /* Restore the filename and line number stashed away when we started
9628 this instantiation. */
9629 input_location = current_tinst_level->locus;
9630 current_tinst_level = current_tinst_level->next;
9631 --tinst_depth;
9634 /* We're instantiating a deferred template; restore the template
9635 instantiation context in which the instantiation was requested, which
9636 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9638 static tree
9639 reopen_tinst_level (struct tinst_level *level)
9641 struct tinst_level *t;
9643 tinst_depth = 0;
9644 for (t = level; t; t = t->next)
9645 ++tinst_depth;
9647 current_tinst_level = level;
9648 pop_tinst_level ();
9649 if (current_tinst_level)
9650 current_tinst_level->errors = errorcount+sorrycount;
9651 return level->decl;
9654 /* Returns the TINST_LEVEL which gives the original instantiation
9655 context. */
9657 struct tinst_level *
9658 outermost_tinst_level (void)
9660 struct tinst_level *level = current_tinst_level;
9661 if (level)
9662 while (level->next)
9663 level = level->next;
9664 return level;
9667 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9668 vector of template arguments, as for tsubst.
9670 Returns an appropriate tsubst'd friend declaration. */
9672 static tree
9673 tsubst_friend_function (tree decl, tree args)
9675 tree new_friend;
9677 if (TREE_CODE (decl) == FUNCTION_DECL
9678 && DECL_TEMPLATE_INSTANTIATION (decl)
9679 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9680 /* This was a friend declared with an explicit template
9681 argument list, e.g.:
9683 friend void f<>(T);
9685 to indicate that f was a template instantiation, not a new
9686 function declaration. Now, we have to figure out what
9687 instantiation of what template. */
9689 tree template_id, arglist, fns;
9690 tree new_args;
9691 tree tmpl;
9692 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9694 /* Friend functions are looked up in the containing namespace scope.
9695 We must enter that scope, to avoid finding member functions of the
9696 current class with same name. */
9697 push_nested_namespace (ns);
9698 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9699 tf_warning_or_error, NULL_TREE,
9700 /*integral_constant_expression_p=*/false);
9701 pop_nested_namespace (ns);
9702 arglist = tsubst (DECL_TI_ARGS (decl), args,
9703 tf_warning_or_error, NULL_TREE);
9704 template_id = lookup_template_function (fns, arglist);
9706 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9707 tmpl = determine_specialization (template_id, new_friend,
9708 &new_args,
9709 /*need_member_template=*/0,
9710 TREE_VEC_LENGTH (args),
9711 tsk_none);
9712 return instantiate_template (tmpl, new_args, tf_error);
9715 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9717 /* The NEW_FRIEND will look like an instantiation, to the
9718 compiler, but is not an instantiation from the point of view of
9719 the language. For example, we might have had:
9721 template <class T> struct S {
9722 template <class U> friend void f(T, U);
9725 Then, in S<int>, template <class U> void f(int, U) is not an
9726 instantiation of anything. */
9727 if (new_friend == error_mark_node)
9728 return error_mark_node;
9730 DECL_USE_TEMPLATE (new_friend) = 0;
9731 if (TREE_CODE (decl) == TEMPLATE_DECL)
9733 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9734 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9735 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9738 /* The mangled name for the NEW_FRIEND is incorrect. The function
9739 is not a template instantiation and should not be mangled like
9740 one. Therefore, we forget the mangling here; we'll recompute it
9741 later if we need it. */
9742 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9744 SET_DECL_RTL (new_friend, NULL);
9745 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9748 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9750 tree old_decl;
9751 tree new_friend_template_info;
9752 tree new_friend_result_template_info;
9753 tree ns;
9754 int new_friend_is_defn;
9756 /* We must save some information from NEW_FRIEND before calling
9757 duplicate decls since that function will free NEW_FRIEND if
9758 possible. */
9759 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9760 new_friend_is_defn =
9761 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9762 (template_for_substitution (new_friend)))
9763 != NULL_TREE);
9764 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9766 /* This declaration is a `primary' template. */
9767 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9769 new_friend_result_template_info
9770 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9772 else
9773 new_friend_result_template_info = NULL_TREE;
9775 /* Inside pushdecl_namespace_level, we will push into the
9776 current namespace. However, the friend function should go
9777 into the namespace of the template. */
9778 ns = decl_namespace_context (new_friend);
9779 push_nested_namespace (ns);
9780 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9781 pop_nested_namespace (ns);
9783 if (old_decl == error_mark_node)
9784 return error_mark_node;
9786 if (old_decl != new_friend)
9788 /* This new friend declaration matched an existing
9789 declaration. For example, given:
9791 template <class T> void f(T);
9792 template <class U> class C {
9793 template <class T> friend void f(T) {}
9796 the friend declaration actually provides the definition
9797 of `f', once C has been instantiated for some type. So,
9798 old_decl will be the out-of-class template declaration,
9799 while new_friend is the in-class definition.
9801 But, if `f' was called before this point, the
9802 instantiation of `f' will have DECL_TI_ARGS corresponding
9803 to `T' but not to `U', references to which might appear
9804 in the definition of `f'. Previously, the most general
9805 template for an instantiation of `f' was the out-of-class
9806 version; now it is the in-class version. Therefore, we
9807 run through all specialization of `f', adding to their
9808 DECL_TI_ARGS appropriately. In particular, they need a
9809 new set of outer arguments, corresponding to the
9810 arguments for this class instantiation.
9812 The same situation can arise with something like this:
9814 friend void f(int);
9815 template <class T> class C {
9816 friend void f(T) {}
9819 when `C<int>' is instantiated. Now, `f(int)' is defined
9820 in the class. */
9822 if (!new_friend_is_defn)
9823 /* On the other hand, if the in-class declaration does
9824 *not* provide a definition, then we don't want to alter
9825 existing definitions. We can just leave everything
9826 alone. */
9828 else
9830 tree new_template = TI_TEMPLATE (new_friend_template_info);
9831 tree new_args = TI_ARGS (new_friend_template_info);
9833 /* Overwrite whatever template info was there before, if
9834 any, with the new template information pertaining to
9835 the declaration. */
9836 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9838 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9840 /* We should have called reregister_specialization in
9841 duplicate_decls. */
9842 gcc_assert (retrieve_specialization (new_template,
9843 new_args, 0)
9844 == old_decl);
9846 /* Instantiate it if the global has already been used. */
9847 if (DECL_ODR_USED (old_decl))
9848 instantiate_decl (old_decl, /*defer_ok=*/true,
9849 /*expl_inst_class_mem_p=*/false);
9851 else
9853 tree t;
9855 /* Indicate that the old function template is a partial
9856 instantiation. */
9857 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9858 = new_friend_result_template_info;
9860 gcc_assert (new_template
9861 == most_general_template (new_template));
9862 gcc_assert (new_template != old_decl);
9864 /* Reassign any specializations already in the hash table
9865 to the new more general template, and add the
9866 additional template args. */
9867 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9868 t != NULL_TREE;
9869 t = TREE_CHAIN (t))
9871 tree spec = TREE_VALUE (t);
9872 spec_entry elt;
9874 elt.tmpl = old_decl;
9875 elt.args = DECL_TI_ARGS (spec);
9876 elt.spec = NULL_TREE;
9878 decl_specializations->remove_elt (&elt);
9880 DECL_TI_ARGS (spec)
9881 = add_outermost_template_args (new_args,
9882 DECL_TI_ARGS (spec));
9884 register_specialization
9885 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9888 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9892 /* The information from NEW_FRIEND has been merged into OLD_DECL
9893 by duplicate_decls. */
9894 new_friend = old_decl;
9897 else
9899 tree context = DECL_CONTEXT (new_friend);
9900 bool dependent_p;
9902 /* In the code
9903 template <class T> class C {
9904 template <class U> friend void C1<U>::f (); // case 1
9905 friend void C2<T>::f (); // case 2
9907 we only need to make sure CONTEXT is a complete type for
9908 case 2. To distinguish between the two cases, we note that
9909 CONTEXT of case 1 remains dependent type after tsubst while
9910 this isn't true for case 2. */
9911 ++processing_template_decl;
9912 dependent_p = dependent_type_p (context);
9913 --processing_template_decl;
9915 if (!dependent_p
9916 && !complete_type_or_else (context, NULL_TREE))
9917 return error_mark_node;
9919 if (COMPLETE_TYPE_P (context))
9921 tree fn = new_friend;
9922 /* do_friend adds the TEMPLATE_DECL for any member friend
9923 template even if it isn't a member template, i.e.
9924 template <class T> friend A<T>::f();
9925 Look through it in that case. */
9926 if (TREE_CODE (fn) == TEMPLATE_DECL
9927 && !PRIMARY_TEMPLATE_P (fn))
9928 fn = DECL_TEMPLATE_RESULT (fn);
9929 /* Check to see that the declaration is really present, and,
9930 possibly obtain an improved declaration. */
9931 fn = check_classfn (context, fn, NULL_TREE);
9933 if (fn)
9934 new_friend = fn;
9938 return new_friend;
9941 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9942 template arguments, as for tsubst.
9944 Returns an appropriate tsubst'd friend type or error_mark_node on
9945 failure. */
9947 static tree
9948 tsubst_friend_class (tree friend_tmpl, tree args)
9950 tree friend_type;
9951 tree tmpl;
9952 tree context;
9954 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9956 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9957 return TREE_TYPE (t);
9960 context = CP_DECL_CONTEXT (friend_tmpl);
9962 if (context != global_namespace)
9964 if (TREE_CODE (context) == NAMESPACE_DECL)
9965 push_nested_namespace (context);
9966 else
9967 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9970 /* Look for a class template declaration. We look for hidden names
9971 because two friend declarations of the same template are the
9972 same. For example, in:
9974 struct A {
9975 template <typename> friend class F;
9977 template <typename> struct B {
9978 template <typename> friend class F;
9981 both F templates are the same. */
9982 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9983 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9985 /* But, if we don't find one, it might be because we're in a
9986 situation like this:
9988 template <class T>
9989 struct S {
9990 template <class U>
9991 friend struct S;
9994 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9995 for `S<int>', not the TEMPLATE_DECL. */
9996 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9998 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9999 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10002 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10004 /* The friend template has already been declared. Just
10005 check to see that the declarations match, and install any new
10006 default parameters. We must tsubst the default parameters,
10007 of course. We only need the innermost template parameters
10008 because that is all that redeclare_class_template will look
10009 at. */
10010 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10011 > TMPL_ARGS_DEPTH (args))
10013 tree parms;
10014 location_t saved_input_location;
10015 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10016 args, tf_warning_or_error);
10018 saved_input_location = input_location;
10019 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10020 tree cons = get_constraints (tmpl);
10021 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10022 input_location = saved_input_location;
10026 friend_type = TREE_TYPE (tmpl);
10028 else
10030 /* The friend template has not already been declared. In this
10031 case, the instantiation of the template class will cause the
10032 injection of this template into the global scope. */
10033 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10034 if (tmpl == error_mark_node)
10035 return error_mark_node;
10037 /* The new TMPL is not an instantiation of anything, so we
10038 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10039 the new type because that is supposed to be the corresponding
10040 template decl, i.e., TMPL. */
10041 DECL_USE_TEMPLATE (tmpl) = 0;
10042 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10043 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10044 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10045 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10047 /* Inject this template into the global scope. */
10048 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10051 if (context != global_namespace)
10053 if (TREE_CODE (context) == NAMESPACE_DECL)
10054 pop_nested_namespace (context);
10055 else
10056 pop_nested_class ();
10059 return friend_type;
10062 /* Returns zero if TYPE cannot be completed later due to circularity.
10063 Otherwise returns one. */
10065 static int
10066 can_complete_type_without_circularity (tree type)
10068 if (type == NULL_TREE || type == error_mark_node)
10069 return 0;
10070 else if (COMPLETE_TYPE_P (type))
10071 return 1;
10072 else if (TREE_CODE (type) == ARRAY_TYPE)
10073 return can_complete_type_without_circularity (TREE_TYPE (type));
10074 else if (CLASS_TYPE_P (type)
10075 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10076 return 0;
10077 else
10078 return 1;
10081 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10082 tsubst_flags_t, tree);
10084 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10085 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10087 static tree
10088 tsubst_attribute (tree t, tree *decl_p, tree args,
10089 tsubst_flags_t complain, tree in_decl)
10091 gcc_assert (ATTR_IS_DEPENDENT (t));
10093 tree val = TREE_VALUE (t);
10094 if (val == NULL_TREE)
10095 /* Nothing to do. */;
10096 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10097 && is_attribute_p ("omp declare simd",
10098 get_attribute_name (t)))
10100 tree clauses = TREE_VALUE (val);
10101 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10102 complain, in_decl);
10103 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10104 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10105 tree parms = DECL_ARGUMENTS (*decl_p);
10106 clauses
10107 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10108 if (clauses)
10109 val = build_tree_list (NULL_TREE, clauses);
10110 else
10111 val = NULL_TREE;
10113 /* If the first attribute argument is an identifier, don't
10114 pass it through tsubst. Attributes like mode, format,
10115 cleanup and several target specific attributes expect it
10116 unmodified. */
10117 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10119 tree chain
10120 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10121 /*integral_constant_expression_p=*/false);
10122 if (chain != TREE_CHAIN (val))
10123 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10125 else if (PACK_EXPANSION_P (val))
10127 /* An attribute pack expansion. */
10128 tree purp = TREE_PURPOSE (t);
10129 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10130 if (pack == error_mark_node)
10131 return error_mark_node;
10132 int len = TREE_VEC_LENGTH (pack);
10133 tree list = NULL_TREE;
10134 tree *q = &list;
10135 for (int i = 0; i < len; ++i)
10137 tree elt = TREE_VEC_ELT (pack, i);
10138 *q = build_tree_list (purp, elt);
10139 q = &TREE_CHAIN (*q);
10141 return list;
10143 else
10144 val = tsubst_expr (val, args, complain, in_decl,
10145 /*integral_constant_expression_p=*/false);
10147 if (val != TREE_VALUE (t))
10148 return build_tree_list (TREE_PURPOSE (t), val);
10149 return t;
10152 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10153 unchanged or a new TREE_LIST chain. */
10155 static tree
10156 tsubst_attributes (tree attributes, tree args,
10157 tsubst_flags_t complain, tree in_decl)
10159 tree last_dep = NULL_TREE;
10161 for (tree t = attributes; t; t = TREE_CHAIN (t))
10162 if (ATTR_IS_DEPENDENT (t))
10164 last_dep = t;
10165 attributes = copy_list (attributes);
10166 break;
10169 if (last_dep)
10170 for (tree *p = &attributes; *p; )
10172 tree t = *p;
10173 if (ATTR_IS_DEPENDENT (t))
10175 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10176 if (subst != t)
10178 *p = subst;
10180 p = &TREE_CHAIN (*p);
10181 while (*p);
10182 *p = TREE_CHAIN (t);
10183 continue;
10186 p = &TREE_CHAIN (*p);
10189 return attributes;
10192 /* Apply any attributes which had to be deferred until instantiation
10193 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10194 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10196 static void
10197 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10198 tree args, tsubst_flags_t complain, tree in_decl)
10200 tree last_dep = NULL_TREE;
10201 tree t;
10202 tree *p;
10204 if (attributes == NULL_TREE)
10205 return;
10207 if (DECL_P (*decl_p))
10209 if (TREE_TYPE (*decl_p) == error_mark_node)
10210 return;
10211 p = &DECL_ATTRIBUTES (*decl_p);
10212 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10213 to our attributes parameter. */
10214 gcc_assert (*p == attributes);
10216 else
10218 p = &TYPE_ATTRIBUTES (*decl_p);
10219 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10220 lookup_template_class_1, and should be preserved. */
10221 gcc_assert (*p != attributes);
10222 while (*p)
10223 p = &TREE_CHAIN (*p);
10226 for (t = attributes; t; t = TREE_CHAIN (t))
10227 if (ATTR_IS_DEPENDENT (t))
10229 last_dep = t;
10230 attributes = copy_list (attributes);
10231 break;
10234 *p = attributes;
10235 if (last_dep)
10237 tree late_attrs = NULL_TREE;
10238 tree *q = &late_attrs;
10240 for (; *p; )
10242 t = *p;
10243 if (ATTR_IS_DEPENDENT (t))
10245 *p = TREE_CHAIN (t);
10246 TREE_CHAIN (t) = NULL_TREE;
10247 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10249 q = &TREE_CHAIN (*q);
10250 while (*q);
10252 else
10253 p = &TREE_CHAIN (t);
10256 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10260 /* Perform (or defer) access check for typedefs that were referenced
10261 from within the template TMPL code.
10262 This is a subroutine of instantiate_decl and instantiate_class_template.
10263 TMPL is the template to consider and TARGS is the list of arguments of
10264 that template. */
10266 static void
10267 perform_typedefs_access_check (tree tmpl, tree targs)
10269 location_t saved_location;
10270 unsigned i;
10271 qualified_typedef_usage_t *iter;
10273 if (!tmpl
10274 || (!CLASS_TYPE_P (tmpl)
10275 && TREE_CODE (tmpl) != FUNCTION_DECL))
10276 return;
10278 saved_location = input_location;
10279 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10281 tree type_decl = iter->typedef_decl;
10282 tree type_scope = iter->context;
10284 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10285 continue;
10287 if (uses_template_parms (type_decl))
10288 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10289 if (uses_template_parms (type_scope))
10290 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10292 /* Make access check error messages point to the location
10293 of the use of the typedef. */
10294 input_location = iter->locus;
10295 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10296 type_decl, type_decl,
10297 tf_warning_or_error);
10299 input_location = saved_location;
10302 static tree
10303 instantiate_class_template_1 (tree type)
10305 tree templ, args, pattern, t, member;
10306 tree typedecl;
10307 tree pbinfo;
10308 tree base_list;
10309 unsigned int saved_maximum_field_alignment;
10310 tree fn_context;
10312 if (type == error_mark_node)
10313 return error_mark_node;
10315 if (COMPLETE_OR_OPEN_TYPE_P (type)
10316 || uses_template_parms (type))
10317 return type;
10319 /* Figure out which template is being instantiated. */
10320 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10321 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10323 /* Determine what specialization of the original template to
10324 instantiate. */
10325 t = most_specialized_partial_spec (type, tf_warning_or_error);
10326 if (t == error_mark_node)
10328 TYPE_BEING_DEFINED (type) = 1;
10329 return error_mark_node;
10331 else if (t)
10333 /* This TYPE is actually an instantiation of a partial
10334 specialization. We replace the innermost set of ARGS with
10335 the arguments appropriate for substitution. For example,
10336 given:
10338 template <class T> struct S {};
10339 template <class T> struct S<T*> {};
10341 and supposing that we are instantiating S<int*>, ARGS will
10342 presently be {int*} -- but we need {int}. */
10343 pattern = TREE_TYPE (t);
10344 args = TREE_PURPOSE (t);
10346 else
10348 pattern = TREE_TYPE (templ);
10349 args = CLASSTYPE_TI_ARGS (type);
10352 /* If the template we're instantiating is incomplete, then clearly
10353 there's nothing we can do. */
10354 if (!COMPLETE_TYPE_P (pattern))
10355 return type;
10357 /* If we've recursively instantiated too many templates, stop. */
10358 if (! push_tinst_level (type))
10359 return type;
10361 /* Now we're really doing the instantiation. Mark the type as in
10362 the process of being defined. */
10363 TYPE_BEING_DEFINED (type) = 1;
10365 /* We may be in the middle of deferred access check. Disable
10366 it now. */
10367 push_deferring_access_checks (dk_no_deferred);
10369 int saved_unevaluated_operand = cp_unevaluated_operand;
10370 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10372 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10373 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10374 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10375 fn_context = error_mark_node;
10376 if (!fn_context)
10377 push_to_top_level ();
10378 else
10380 cp_unevaluated_operand = 0;
10381 c_inhibit_evaluation_warnings = 0;
10383 /* Use #pragma pack from the template context. */
10384 saved_maximum_field_alignment = maximum_field_alignment;
10385 maximum_field_alignment = TYPE_PRECISION (pattern);
10387 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10389 /* Set the input location to the most specialized template definition.
10390 This is needed if tsubsting causes an error. */
10391 typedecl = TYPE_MAIN_DECL (pattern);
10392 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10393 DECL_SOURCE_LOCATION (typedecl);
10395 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10396 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10397 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10398 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10399 if (ANON_AGGR_TYPE_P (pattern))
10400 SET_ANON_AGGR_TYPE_P (type);
10401 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10403 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10404 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10405 /* Adjust visibility for template arguments. */
10406 determine_visibility (TYPE_MAIN_DECL (type));
10408 if (CLASS_TYPE_P (type))
10409 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10411 pbinfo = TYPE_BINFO (pattern);
10413 /* We should never instantiate a nested class before its enclosing
10414 class; we need to look up the nested class by name before we can
10415 instantiate it, and that lookup should instantiate the enclosing
10416 class. */
10417 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10418 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10420 base_list = NULL_TREE;
10421 if (BINFO_N_BASE_BINFOS (pbinfo))
10423 tree pbase_binfo;
10424 tree pushed_scope;
10425 int i;
10427 /* We must enter the scope containing the type, as that is where
10428 the accessibility of types named in dependent bases are
10429 looked up from. */
10430 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10432 /* Substitute into each of the bases to determine the actual
10433 basetypes. */
10434 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10436 tree base;
10437 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10438 tree expanded_bases = NULL_TREE;
10439 int idx, len = 1;
10441 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10443 expanded_bases =
10444 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10445 args, tf_error, NULL_TREE);
10446 if (expanded_bases == error_mark_node)
10447 continue;
10449 len = TREE_VEC_LENGTH (expanded_bases);
10452 for (idx = 0; idx < len; idx++)
10454 if (expanded_bases)
10455 /* Extract the already-expanded base class. */
10456 base = TREE_VEC_ELT (expanded_bases, idx);
10457 else
10458 /* Substitute to figure out the base class. */
10459 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10460 NULL_TREE);
10462 if (base == error_mark_node)
10463 continue;
10465 base_list = tree_cons (access, base, base_list);
10466 if (BINFO_VIRTUAL_P (pbase_binfo))
10467 TREE_TYPE (base_list) = integer_type_node;
10471 /* The list is now in reverse order; correct that. */
10472 base_list = nreverse (base_list);
10474 if (pushed_scope)
10475 pop_scope (pushed_scope);
10477 /* Now call xref_basetypes to set up all the base-class
10478 information. */
10479 xref_basetypes (type, base_list);
10481 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10482 (int) ATTR_FLAG_TYPE_IN_PLACE,
10483 args, tf_error, NULL_TREE);
10484 fixup_attribute_variants (type);
10486 /* Now that our base classes are set up, enter the scope of the
10487 class, so that name lookups into base classes, etc. will work
10488 correctly. This is precisely analogous to what we do in
10489 begin_class_definition when defining an ordinary non-template
10490 class, except we also need to push the enclosing classes. */
10491 push_nested_class (type);
10493 /* Now members are processed in the order of declaration. */
10494 for (member = CLASSTYPE_DECL_LIST (pattern);
10495 member; member = TREE_CHAIN (member))
10497 tree t = TREE_VALUE (member);
10499 if (TREE_PURPOSE (member))
10501 if (TYPE_P (t))
10503 /* Build new CLASSTYPE_NESTED_UTDS. */
10505 tree newtag;
10506 bool class_template_p;
10508 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10509 && TYPE_LANG_SPECIFIC (t)
10510 && CLASSTYPE_IS_TEMPLATE (t));
10511 /* If the member is a class template, then -- even after
10512 substitution -- there may be dependent types in the
10513 template argument list for the class. We increment
10514 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10515 that function will assume that no types are dependent
10516 when outside of a template. */
10517 if (class_template_p)
10518 ++processing_template_decl;
10519 newtag = tsubst (t, args, tf_error, NULL_TREE);
10520 if (class_template_p)
10521 --processing_template_decl;
10522 if (newtag == error_mark_node)
10523 continue;
10525 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10527 tree name = TYPE_IDENTIFIER (t);
10529 if (class_template_p)
10530 /* Unfortunately, lookup_template_class sets
10531 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10532 instantiation (i.e., for the type of a member
10533 template class nested within a template class.)
10534 This behavior is required for
10535 maybe_process_partial_specialization to work
10536 correctly, but is not accurate in this case;
10537 the TAG is not an instantiation of anything.
10538 (The corresponding TEMPLATE_DECL is an
10539 instantiation, but the TYPE is not.) */
10540 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10542 /* Now, we call pushtag to put this NEWTAG into the scope of
10543 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10544 pushtag calling push_template_decl. We don't have to do
10545 this for enums because it will already have been done in
10546 tsubst_enum. */
10547 if (name)
10548 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10549 pushtag (name, newtag, /*tag_scope=*/ts_current);
10552 else if (DECL_DECLARES_FUNCTION_P (t))
10554 tree r;
10556 if (TREE_CODE (t) == TEMPLATE_DECL)
10557 ++processing_template_decl;
10558 r = tsubst (t, args, tf_error, NULL_TREE);
10559 if (TREE_CODE (t) == TEMPLATE_DECL)
10560 --processing_template_decl;
10561 set_current_access_from_decl (r);
10562 finish_member_declaration (r);
10563 /* Instantiate members marked with attribute used. */
10564 if (r != error_mark_node && DECL_PRESERVE_P (r))
10565 mark_used (r);
10566 if (TREE_CODE (r) == FUNCTION_DECL
10567 && DECL_OMP_DECLARE_REDUCTION_P (r))
10568 cp_check_omp_declare_reduction (r);
10570 else if (DECL_CLASS_TEMPLATE_P (t)
10571 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10572 /* A closure type for a lambda in a default argument for a
10573 member template. Ignore it; it will be instantiated with
10574 the default argument. */;
10575 else
10577 /* Build new TYPE_FIELDS. */
10578 if (TREE_CODE (t) == STATIC_ASSERT)
10580 tree condition;
10582 ++c_inhibit_evaluation_warnings;
10583 condition =
10584 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10585 tf_warning_or_error, NULL_TREE,
10586 /*integral_constant_expression_p=*/true);
10587 --c_inhibit_evaluation_warnings;
10589 finish_static_assert (condition,
10590 STATIC_ASSERT_MESSAGE (t),
10591 STATIC_ASSERT_SOURCE_LOCATION (t),
10592 /*member_p=*/true);
10594 else if (TREE_CODE (t) != CONST_DECL)
10596 tree r;
10597 tree vec = NULL_TREE;
10598 int len = 1;
10600 /* The file and line for this declaration, to
10601 assist in error message reporting. Since we
10602 called push_tinst_level above, we don't need to
10603 restore these. */
10604 input_location = DECL_SOURCE_LOCATION (t);
10606 if (TREE_CODE (t) == TEMPLATE_DECL)
10607 ++processing_template_decl;
10608 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10609 if (TREE_CODE (t) == TEMPLATE_DECL)
10610 --processing_template_decl;
10612 if (TREE_CODE (r) == TREE_VEC)
10614 /* A capture pack became multiple fields. */
10615 vec = r;
10616 len = TREE_VEC_LENGTH (vec);
10619 for (int i = 0; i < len; ++i)
10621 if (vec)
10622 r = TREE_VEC_ELT (vec, i);
10623 if (VAR_P (r))
10625 /* In [temp.inst]:
10627 [t]he initialization (and any associated
10628 side-effects) of a static data member does
10629 not occur unless the static data member is
10630 itself used in a way that requires the
10631 definition of the static data member to
10632 exist.
10634 Therefore, we do not substitute into the
10635 initialized for the static data member here. */
10636 finish_static_data_member_decl
10638 /*init=*/NULL_TREE,
10639 /*init_const_expr_p=*/false,
10640 /*asmspec_tree=*/NULL_TREE,
10641 /*flags=*/0);
10642 /* Instantiate members marked with attribute used. */
10643 if (r != error_mark_node && DECL_PRESERVE_P (r))
10644 mark_used (r);
10646 else if (TREE_CODE (r) == FIELD_DECL)
10648 /* Determine whether R has a valid type and can be
10649 completed later. If R is invalid, then its type
10650 is replaced by error_mark_node. */
10651 tree rtype = TREE_TYPE (r);
10652 if (can_complete_type_without_circularity (rtype))
10653 complete_type (rtype);
10655 if (!complete_or_array_type_p (rtype))
10657 /* If R's type couldn't be completed and
10658 it isn't a flexible array member (whose
10659 type is incomplete by definition) give
10660 an error. */
10661 cxx_incomplete_type_error (r, rtype);
10662 TREE_TYPE (r) = error_mark_node;
10666 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10667 such a thing will already have been added to the field
10668 list by tsubst_enum in finish_member_declaration in the
10669 CLASSTYPE_NESTED_UTDS case above. */
10670 if (!(TREE_CODE (r) == TYPE_DECL
10671 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10672 && DECL_ARTIFICIAL (r)))
10674 set_current_access_from_decl (r);
10675 finish_member_declaration (r);
10681 else
10683 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10684 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10686 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10688 tree friend_type = t;
10689 bool adjust_processing_template_decl = false;
10691 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10693 /* template <class T> friend class C; */
10694 friend_type = tsubst_friend_class (friend_type, args);
10695 adjust_processing_template_decl = true;
10697 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10699 /* template <class T> friend class C::D; */
10700 friend_type = tsubst (friend_type, args,
10701 tf_warning_or_error, NULL_TREE);
10702 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10703 friend_type = TREE_TYPE (friend_type);
10704 adjust_processing_template_decl = true;
10706 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10707 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10709 /* This could be either
10711 friend class T::C;
10713 when dependent_type_p is false or
10715 template <class U> friend class T::C;
10717 otherwise. */
10718 /* Bump processing_template_decl in case this is something like
10719 template <class T> friend struct A<T>::B. */
10720 ++processing_template_decl;
10721 friend_type = tsubst (friend_type, args,
10722 tf_warning_or_error, NULL_TREE);
10723 if (dependent_type_p (friend_type))
10724 adjust_processing_template_decl = true;
10725 --processing_template_decl;
10727 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10728 && TYPE_HIDDEN_P (friend_type))
10730 /* friend class C;
10732 where C hasn't been declared yet. Let's lookup name
10733 from namespace scope directly, bypassing any name that
10734 come from dependent base class. */
10735 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10737 /* The call to xref_tag_from_type does injection for friend
10738 classes. */
10739 push_nested_namespace (ns);
10740 friend_type =
10741 xref_tag_from_type (friend_type, NULL_TREE,
10742 /*tag_scope=*/ts_current);
10743 pop_nested_namespace (ns);
10745 else if (uses_template_parms (friend_type))
10746 /* friend class C<T>; */
10747 friend_type = tsubst (friend_type, args,
10748 tf_warning_or_error, NULL_TREE);
10749 /* Otherwise it's
10751 friend class C;
10753 where C is already declared or
10755 friend class C<int>;
10757 We don't have to do anything in these cases. */
10759 if (adjust_processing_template_decl)
10760 /* Trick make_friend_class into realizing that the friend
10761 we're adding is a template, not an ordinary class. It's
10762 important that we use make_friend_class since it will
10763 perform some error-checking and output cross-reference
10764 information. */
10765 ++processing_template_decl;
10767 if (friend_type != error_mark_node)
10768 make_friend_class (type, friend_type, /*complain=*/false);
10770 if (adjust_processing_template_decl)
10771 --processing_template_decl;
10773 else
10775 /* Build new DECL_FRIENDLIST. */
10776 tree r;
10778 /* The file and line for this declaration, to
10779 assist in error message reporting. Since we
10780 called push_tinst_level above, we don't need to
10781 restore these. */
10782 input_location = DECL_SOURCE_LOCATION (t);
10784 if (TREE_CODE (t) == TEMPLATE_DECL)
10786 ++processing_template_decl;
10787 push_deferring_access_checks (dk_no_check);
10790 r = tsubst_friend_function (t, args);
10791 add_friend (type, r, /*complain=*/false);
10792 if (TREE_CODE (t) == TEMPLATE_DECL)
10794 pop_deferring_access_checks ();
10795 --processing_template_decl;
10801 if (fn_context)
10803 /* Restore these before substituting into the lambda capture
10804 initializers. */
10805 cp_unevaluated_operand = saved_unevaluated_operand;
10806 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10809 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10811 tree decl = lambda_function (type);
10812 if (decl)
10814 if (cxx_dialect >= cxx1z)
10815 CLASSTYPE_LITERAL_P (type) = true;
10817 if (!DECL_TEMPLATE_INFO (decl)
10818 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10820 /* Set function_depth to avoid garbage collection. */
10821 ++function_depth;
10822 instantiate_decl (decl, /*defer_ok=*/false, false);
10823 --function_depth;
10826 /* We need to instantiate the capture list from the template
10827 after we've instantiated the closure members, but before we
10828 consider adding the conversion op. Also keep any captures
10829 that may have been added during instantiation of the op(). */
10830 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10831 tree tmpl_cap
10832 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10833 args, tf_warning_or_error, NULL_TREE,
10834 false, false);
10836 LAMBDA_EXPR_CAPTURE_LIST (expr)
10837 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10839 maybe_add_lambda_conv_op (type);
10841 else
10842 gcc_assert (errorcount);
10845 /* Set the file and line number information to whatever is given for
10846 the class itself. This puts error messages involving generated
10847 implicit functions at a predictable point, and the same point
10848 that would be used for non-template classes. */
10849 input_location = DECL_SOURCE_LOCATION (typedecl);
10851 unreverse_member_declarations (type);
10852 finish_struct_1 (type);
10853 TYPE_BEING_DEFINED (type) = 0;
10855 /* We don't instantiate default arguments for member functions. 14.7.1:
10857 The implicit instantiation of a class template specialization causes
10858 the implicit instantiation of the declarations, but not of the
10859 definitions or default arguments, of the class member functions,
10860 member classes, static data members and member templates.... */
10862 /* Some typedefs referenced from within the template code need to be access
10863 checked at template instantiation time, i.e now. These types were
10864 added to the template at parsing time. Let's get those and perform
10865 the access checks then. */
10866 perform_typedefs_access_check (pattern, args);
10867 perform_deferred_access_checks (tf_warning_or_error);
10868 pop_nested_class ();
10869 maximum_field_alignment = saved_maximum_field_alignment;
10870 if (!fn_context)
10871 pop_from_top_level ();
10872 pop_deferring_access_checks ();
10873 pop_tinst_level ();
10875 /* The vtable for a template class can be emitted in any translation
10876 unit in which the class is instantiated. When there is no key
10877 method, however, finish_struct_1 will already have added TYPE to
10878 the keyed_classes. */
10879 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10880 vec_safe_push (keyed_classes, type);
10882 return type;
10885 /* Wrapper for instantiate_class_template_1. */
10887 tree
10888 instantiate_class_template (tree type)
10890 tree ret;
10891 timevar_push (TV_TEMPLATE_INST);
10892 ret = instantiate_class_template_1 (type);
10893 timevar_pop (TV_TEMPLATE_INST);
10894 return ret;
10897 static tree
10898 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10900 tree r;
10902 if (!t)
10903 r = t;
10904 else if (TYPE_P (t))
10905 r = tsubst (t, args, complain, in_decl);
10906 else
10908 if (!(complain & tf_warning))
10909 ++c_inhibit_evaluation_warnings;
10910 r = tsubst_expr (t, args, complain, in_decl,
10911 /*integral_constant_expression_p=*/true);
10912 if (!(complain & tf_warning))
10913 --c_inhibit_evaluation_warnings;
10915 return r;
10918 /* Given a function parameter pack TMPL_PARM and some function parameters
10919 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10920 and set *SPEC_P to point at the next point in the list. */
10922 tree
10923 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10925 /* Collect all of the extra "packed" parameters into an
10926 argument pack. */
10927 tree parmvec;
10928 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10929 tree spec_parm = *spec_p;
10930 int i, len;
10932 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10933 if (tmpl_parm
10934 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10935 break;
10937 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10938 parmvec = make_tree_vec (len);
10939 spec_parm = *spec_p;
10940 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10941 TREE_VEC_ELT (parmvec, i) = spec_parm;
10943 /* Build the argument packs. */
10944 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10945 *spec_p = spec_parm;
10947 return argpack;
10950 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10951 NONTYPE_ARGUMENT_PACK. */
10953 static tree
10954 make_fnparm_pack (tree spec_parm)
10956 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10959 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10960 pack expansion with no extra args, 2 if it has extra args, or 0
10961 if it is not a pack expansion. */
10963 static int
10964 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10966 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10967 if (i >= TREE_VEC_LENGTH (vec))
10968 return 0;
10969 tree elt = TREE_VEC_ELT (vec, i);
10970 if (DECL_P (elt))
10971 /* A decl pack is itself an expansion. */
10972 elt = TREE_TYPE (elt);
10973 if (!PACK_EXPANSION_P (elt))
10974 return 0;
10975 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10976 return 2;
10977 return 1;
10981 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10983 static tree
10984 make_argument_pack_select (tree arg_pack, unsigned index)
10986 tree aps = make_node (ARGUMENT_PACK_SELECT);
10988 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10989 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10991 return aps;
10994 /* This is a subroutine of tsubst_pack_expansion.
10996 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10997 mechanism to store the (non complete list of) arguments of the
10998 substitution and return a non substituted pack expansion, in order
10999 to wait for when we have enough arguments to really perform the
11000 substitution. */
11002 static bool
11003 use_pack_expansion_extra_args_p (tree parm_packs,
11004 int arg_pack_len,
11005 bool has_empty_arg)
11007 /* If one pack has an expansion and another pack has a normal
11008 argument or if one pack has an empty argument and an another
11009 one hasn't then tsubst_pack_expansion cannot perform the
11010 substitution and need to fall back on the
11011 PACK_EXPANSION_EXTRA mechanism. */
11012 if (parm_packs == NULL_TREE)
11013 return false;
11014 else if (has_empty_arg)
11015 return true;
11017 bool has_expansion_arg = false;
11018 for (int i = 0 ; i < arg_pack_len; ++i)
11020 bool has_non_expansion_arg = false;
11021 for (tree parm_pack = parm_packs;
11022 parm_pack;
11023 parm_pack = TREE_CHAIN (parm_pack))
11025 tree arg = TREE_VALUE (parm_pack);
11027 int exp = argument_pack_element_is_expansion_p (arg, i);
11028 if (exp == 2)
11029 /* We can't substitute a pack expansion with extra args into
11030 our pattern. */
11031 return true;
11032 else if (exp)
11033 has_expansion_arg = true;
11034 else
11035 has_non_expansion_arg = true;
11038 if (has_expansion_arg && has_non_expansion_arg)
11039 return true;
11041 return false;
11044 /* [temp.variadic]/6 says that:
11046 The instantiation of a pack expansion [...]
11047 produces a list E1,E2, ..., En, where N is the number of elements
11048 in the pack expansion parameters.
11050 This subroutine of tsubst_pack_expansion produces one of these Ei.
11052 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11053 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11054 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11055 INDEX is the index 'i' of the element Ei to produce. ARGS,
11056 COMPLAIN, and IN_DECL are the same parameters as for the
11057 tsubst_pack_expansion function.
11059 The function returns the resulting Ei upon successful completion,
11060 or error_mark_node.
11062 Note that this function possibly modifies the ARGS parameter, so
11063 it's the responsibility of the caller to restore it. */
11065 static tree
11066 gen_elem_of_pack_expansion_instantiation (tree pattern,
11067 tree parm_packs,
11068 unsigned index,
11069 tree args /* This parm gets
11070 modified. */,
11071 tsubst_flags_t complain,
11072 tree in_decl)
11074 tree t;
11075 bool ith_elem_is_expansion = false;
11077 /* For each parameter pack, change the substitution of the parameter
11078 pack to the ith argument in its argument pack, then expand the
11079 pattern. */
11080 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11082 tree parm = TREE_PURPOSE (pack);
11083 tree arg_pack = TREE_VALUE (pack);
11084 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11086 ith_elem_is_expansion |=
11087 argument_pack_element_is_expansion_p (arg_pack, index);
11089 /* Select the Ith argument from the pack. */
11090 if (TREE_CODE (parm) == PARM_DECL
11091 || TREE_CODE (parm) == FIELD_DECL)
11093 if (index == 0)
11095 aps = make_argument_pack_select (arg_pack, index);
11096 if (!mark_used (parm, complain) && !(complain & tf_error))
11097 return error_mark_node;
11098 register_local_specialization (aps, parm);
11100 else
11101 aps = retrieve_local_specialization (parm);
11103 else
11105 int idx, level;
11106 template_parm_level_and_index (parm, &level, &idx);
11108 if (index == 0)
11110 aps = make_argument_pack_select (arg_pack, index);
11111 /* Update the corresponding argument. */
11112 TMPL_ARG (args, level, idx) = aps;
11114 else
11115 /* Re-use the ARGUMENT_PACK_SELECT. */
11116 aps = TMPL_ARG (args, level, idx);
11118 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11121 /* Substitute into the PATTERN with the (possibly altered)
11122 arguments. */
11123 if (pattern == in_decl)
11124 /* Expanding a fixed parameter pack from
11125 coerce_template_parameter_pack. */
11126 t = tsubst_decl (pattern, args, complain);
11127 else if (pattern == error_mark_node)
11128 t = error_mark_node;
11129 else if (constraint_p (pattern))
11131 if (processing_template_decl)
11132 t = tsubst_constraint (pattern, args, complain, in_decl);
11133 else
11134 t = (constraints_satisfied_p (pattern, args)
11135 ? boolean_true_node : boolean_false_node);
11137 else if (!TYPE_P (pattern))
11138 t = tsubst_expr (pattern, args, complain, in_decl,
11139 /*integral_constant_expression_p=*/false);
11140 else
11141 t = tsubst (pattern, args, complain, in_decl);
11143 /* If the Ith argument pack element is a pack expansion, then
11144 the Ith element resulting from the substituting is going to
11145 be a pack expansion as well. */
11146 if (ith_elem_is_expansion)
11147 t = make_pack_expansion (t);
11149 return t;
11152 /* When the unexpanded parameter pack in a fold expression expands to an empty
11153 sequence, the value of the expression is as follows; the program is
11154 ill-formed if the operator is not listed in this table.
11156 && true
11157 || false
11158 , void() */
11160 tree
11161 expand_empty_fold (tree t, tsubst_flags_t complain)
11163 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11164 if (!FOLD_EXPR_MODIFY_P (t))
11165 switch (code)
11167 case TRUTH_ANDIF_EXPR:
11168 return boolean_true_node;
11169 case TRUTH_ORIF_EXPR:
11170 return boolean_false_node;
11171 case COMPOUND_EXPR:
11172 return void_node;
11173 default:
11174 break;
11177 if (complain & tf_error)
11178 error_at (location_of (t),
11179 "fold of empty expansion over %O", code);
11180 return error_mark_node;
11183 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11184 form an expression that combines the two terms using the
11185 operator of T. */
11187 static tree
11188 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11190 tree op = FOLD_EXPR_OP (t);
11191 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11193 // Handle compound assignment operators.
11194 if (FOLD_EXPR_MODIFY_P (t))
11195 return build_x_modify_expr (input_location, left, code, right, complain);
11197 switch (code)
11199 case COMPOUND_EXPR:
11200 return build_x_compound_expr (input_location, left, right, complain);
11201 case DOTSTAR_EXPR:
11202 return build_m_component_ref (left, right, complain);
11203 default:
11204 return build_x_binary_op (input_location, code,
11205 left, TREE_CODE (left),
11206 right, TREE_CODE (right),
11207 /*overload=*/NULL,
11208 complain);
11212 /* Substitute ARGS into the pack of a fold expression T. */
11214 static inline tree
11215 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11217 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11220 /* Substitute ARGS into the pack of a fold expression T. */
11222 static inline tree
11223 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11225 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11228 /* Expand a PACK of arguments into a grouped as left fold.
11229 Given a pack containing elements A0, A1, ..., An and an
11230 operator @, this builds the expression:
11232 ((A0 @ A1) @ A2) ... @ An
11234 Note that PACK must not be empty.
11236 The operator is defined by the original fold expression T. */
11238 static tree
11239 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11241 tree left = TREE_VEC_ELT (pack, 0);
11242 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11244 tree right = TREE_VEC_ELT (pack, i);
11245 left = fold_expression (t, left, right, complain);
11247 return left;
11250 /* Substitute into a unary left fold expression. */
11252 static tree
11253 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11254 tree in_decl)
11256 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11257 if (pack == error_mark_node)
11258 return error_mark_node;
11259 if (PACK_EXPANSION_P (pack))
11261 tree r = copy_node (t);
11262 FOLD_EXPR_PACK (r) = pack;
11263 return r;
11265 if (TREE_VEC_LENGTH (pack) == 0)
11266 return expand_empty_fold (t, complain);
11267 else
11268 return expand_left_fold (t, pack, complain);
11271 /* Substitute into a binary left fold expression.
11273 Do ths by building a single (non-empty) vector of argumnts and
11274 building the expression from those elements. */
11276 static tree
11277 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11278 tree in_decl)
11280 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11281 if (pack == error_mark_node)
11282 return error_mark_node;
11283 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11284 if (init == error_mark_node)
11285 return error_mark_node;
11287 if (PACK_EXPANSION_P (pack))
11289 tree r = copy_node (t);
11290 FOLD_EXPR_PACK (r) = pack;
11291 FOLD_EXPR_INIT (r) = init;
11292 return r;
11295 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11296 TREE_VEC_ELT (vec, 0) = init;
11297 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11298 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11300 return expand_left_fold (t, vec, complain);
11303 /* Expand a PACK of arguments into a grouped as right fold.
11304 Given a pack containing elementns A0, A1, ..., and an
11305 operator @, this builds the expression:
11307 A0@ ... (An-2 @ (An-1 @ An))
11309 Note that PACK must not be empty.
11311 The operator is defined by the original fold expression T. */
11313 tree
11314 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11316 // Build the expression.
11317 int n = TREE_VEC_LENGTH (pack);
11318 tree right = TREE_VEC_ELT (pack, n - 1);
11319 for (--n; n != 0; --n)
11321 tree left = TREE_VEC_ELT (pack, n - 1);
11322 right = fold_expression (t, left, right, complain);
11324 return right;
11327 /* Substitute into a unary right fold expression. */
11329 static tree
11330 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11331 tree in_decl)
11333 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11334 if (pack == error_mark_node)
11335 return error_mark_node;
11336 if (PACK_EXPANSION_P (pack))
11338 tree r = copy_node (t);
11339 FOLD_EXPR_PACK (r) = pack;
11340 return r;
11342 if (TREE_VEC_LENGTH (pack) == 0)
11343 return expand_empty_fold (t, complain);
11344 else
11345 return expand_right_fold (t, pack, complain);
11348 /* Substitute into a binary right fold expression.
11350 Do ths by building a single (non-empty) vector of arguments and
11351 building the expression from those elements. */
11353 static tree
11354 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11355 tree in_decl)
11357 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11358 if (pack == error_mark_node)
11359 return error_mark_node;
11360 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11361 if (init == error_mark_node)
11362 return error_mark_node;
11364 if (PACK_EXPANSION_P (pack))
11366 tree r = copy_node (t);
11367 FOLD_EXPR_PACK (r) = pack;
11368 FOLD_EXPR_INIT (r) = init;
11369 return r;
11372 int n = TREE_VEC_LENGTH (pack);
11373 tree vec = make_tree_vec (n + 1);
11374 for (int i = 0; i < n; ++i)
11375 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11376 TREE_VEC_ELT (vec, n) = init;
11378 return expand_right_fold (t, vec, complain);
11382 /* Substitute ARGS into T, which is an pack expansion
11383 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11384 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11385 (if only a partial substitution could be performed) or
11386 ERROR_MARK_NODE if there was an error. */
11387 tree
11388 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11389 tree in_decl)
11391 tree pattern;
11392 tree pack, packs = NULL_TREE;
11393 bool unsubstituted_packs = false;
11394 int i, len = -1;
11395 tree result;
11396 hash_map<tree, tree> *saved_local_specializations = NULL;
11397 bool need_local_specializations = false;
11398 int levels;
11400 gcc_assert (PACK_EXPANSION_P (t));
11401 pattern = PACK_EXPANSION_PATTERN (t);
11403 /* Add in any args remembered from an earlier partial instantiation. */
11404 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11406 levels = TMPL_ARGS_DEPTH (args);
11408 /* Determine the argument packs that will instantiate the parameter
11409 packs used in the expansion expression. While we're at it,
11410 compute the number of arguments to be expanded and make sure it
11411 is consistent. */
11412 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11413 pack = TREE_CHAIN (pack))
11415 tree parm_pack = TREE_VALUE (pack);
11416 tree arg_pack = NULL_TREE;
11417 tree orig_arg = NULL_TREE;
11418 int level = 0;
11420 if (TREE_CODE (parm_pack) == BASES)
11422 gcc_assert (parm_pack == pattern);
11423 if (BASES_DIRECT (parm_pack))
11424 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11425 args, complain, in_decl, false));
11426 else
11427 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11428 args, complain, in_decl, false));
11430 else if (builtin_pack_call_p (parm_pack))
11432 /* ??? Support use in other patterns. */
11433 gcc_assert (parm_pack == pattern);
11434 return expand_builtin_pack_call (parm_pack, args,
11435 complain, in_decl);
11437 else if (TREE_CODE (parm_pack) == PARM_DECL)
11439 /* We know we have correct local_specializations if this
11440 expansion is at function scope, or if we're dealing with a
11441 local parameter in a requires expression; for the latter,
11442 tsubst_requires_expr set it up appropriately. */
11443 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11444 arg_pack = retrieve_local_specialization (parm_pack);
11445 else
11446 /* We can't rely on local_specializations for a parameter
11447 name used later in a function declaration (such as in a
11448 late-specified return type). Even if it exists, it might
11449 have the wrong value for a recursive call. */
11450 need_local_specializations = true;
11452 if (!arg_pack)
11454 /* This parameter pack was used in an unevaluated context. Just
11455 make a dummy decl, since it's only used for its type. */
11456 arg_pack = tsubst_decl (parm_pack, args, complain);
11457 if (arg_pack && DECL_PACK_P (arg_pack))
11458 /* Partial instantiation of the parm_pack, we can't build
11459 up an argument pack yet. */
11460 arg_pack = NULL_TREE;
11461 else
11462 arg_pack = make_fnparm_pack (arg_pack);
11465 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11466 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11467 else
11469 int idx;
11470 template_parm_level_and_index (parm_pack, &level, &idx);
11472 if (level <= levels)
11473 arg_pack = TMPL_ARG (args, level, idx);
11476 orig_arg = arg_pack;
11477 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11478 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11480 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11481 /* This can only happen if we forget to expand an argument
11482 pack somewhere else. Just return an error, silently. */
11484 result = make_tree_vec (1);
11485 TREE_VEC_ELT (result, 0) = error_mark_node;
11486 return result;
11489 if (arg_pack)
11491 int my_len =
11492 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11494 /* Don't bother trying to do a partial substitution with
11495 incomplete packs; we'll try again after deduction. */
11496 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11497 return t;
11499 if (len < 0)
11500 len = my_len;
11501 else if (len != my_len)
11503 if (!(complain & tf_error))
11504 /* Fail quietly. */;
11505 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11506 error ("mismatched argument pack lengths while expanding %qT",
11507 pattern);
11508 else
11509 error ("mismatched argument pack lengths while expanding %qE",
11510 pattern);
11511 return error_mark_node;
11514 /* Keep track of the parameter packs and their corresponding
11515 argument packs. */
11516 packs = tree_cons (parm_pack, arg_pack, packs);
11517 TREE_TYPE (packs) = orig_arg;
11519 else
11521 /* We can't substitute for this parameter pack. We use a flag as
11522 well as the missing_level counter because function parameter
11523 packs don't have a level. */
11524 gcc_assert (processing_template_decl);
11525 unsubstituted_packs = true;
11529 /* If the expansion is just T..., return the matching argument pack, unless
11530 we need to call convert_from_reference on all the elements. This is an
11531 important optimization; see c++/68422. */
11532 if (!unsubstituted_packs
11533 && TREE_PURPOSE (packs) == pattern)
11535 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11536 /* Types need no adjustment, nor does sizeof..., and if we still have
11537 some pack expansion args we won't do anything yet. */
11538 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11539 || PACK_EXPANSION_SIZEOF_P (t)
11540 || pack_expansion_args_count (args))
11541 return args;
11542 /* Also optimize expression pack expansions if we can tell that the
11543 elements won't have reference type. */
11544 tree type = TREE_TYPE (pattern);
11545 if (type && TREE_CODE (type) != REFERENCE_TYPE
11546 && !PACK_EXPANSION_P (type)
11547 && !WILDCARD_TYPE_P (type))
11548 return args;
11549 /* Otherwise use the normal path so we get convert_from_reference. */
11552 /* We cannot expand this expansion expression, because we don't have
11553 all of the argument packs we need. */
11554 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11556 /* We got some full packs, but we can't substitute them in until we
11557 have values for all the packs. So remember these until then. */
11559 t = make_pack_expansion (pattern);
11560 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11561 return t;
11563 else if (unsubstituted_packs)
11565 /* There were no real arguments, we're just replacing a parameter
11566 pack with another version of itself. Substitute into the
11567 pattern and return a PACK_EXPANSION_*. The caller will need to
11568 deal with that. */
11569 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11570 t = tsubst_expr (pattern, args, complain, in_decl,
11571 /*integral_constant_expression_p=*/false);
11572 else
11573 t = tsubst (pattern, args, complain, in_decl);
11574 t = make_pack_expansion (t);
11575 return t;
11578 gcc_assert (len >= 0);
11580 if (need_local_specializations)
11582 /* We're in a late-specified return type, so create our own local
11583 specializations map; the current map is either NULL or (in the
11584 case of recursive unification) might have bindings that we don't
11585 want to use or alter. */
11586 saved_local_specializations = local_specializations;
11587 local_specializations = new hash_map<tree, tree>;
11590 /* For each argument in each argument pack, substitute into the
11591 pattern. */
11592 result = make_tree_vec (len);
11593 tree elem_args = copy_template_args (args);
11594 for (i = 0; i < len; ++i)
11596 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11598 elem_args, complain,
11599 in_decl);
11600 TREE_VEC_ELT (result, i) = t;
11601 if (t == error_mark_node)
11603 result = error_mark_node;
11604 break;
11608 /* Update ARGS to restore the substitution from parameter packs to
11609 their argument packs. */
11610 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11612 tree parm = TREE_PURPOSE (pack);
11614 if (TREE_CODE (parm) == PARM_DECL
11615 || TREE_CODE (parm) == FIELD_DECL)
11616 register_local_specialization (TREE_TYPE (pack), parm);
11617 else
11619 int idx, level;
11621 if (TREE_VALUE (pack) == NULL_TREE)
11622 continue;
11624 template_parm_level_and_index (parm, &level, &idx);
11626 /* Update the corresponding argument. */
11627 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11628 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11629 TREE_TYPE (pack);
11630 else
11631 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11635 if (need_local_specializations)
11637 delete local_specializations;
11638 local_specializations = saved_local_specializations;
11641 /* If the dependent pack arguments were such that we end up with only a
11642 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11643 if (len == 1 && TREE_CODE (result) == TREE_VEC
11644 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11645 return TREE_VEC_ELT (result, 0);
11647 return result;
11650 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11651 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11652 parameter packs; all parms generated from a function parameter pack will
11653 have the same DECL_PARM_INDEX. */
11655 tree
11656 get_pattern_parm (tree parm, tree tmpl)
11658 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11659 tree patparm;
11661 if (DECL_ARTIFICIAL (parm))
11663 for (patparm = DECL_ARGUMENTS (pattern);
11664 patparm; patparm = DECL_CHAIN (patparm))
11665 if (DECL_ARTIFICIAL (patparm)
11666 && DECL_NAME (parm) == DECL_NAME (patparm))
11667 break;
11669 else
11671 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11672 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11673 gcc_assert (DECL_PARM_INDEX (patparm)
11674 == DECL_PARM_INDEX (parm));
11677 return patparm;
11680 /* Make an argument pack out of the TREE_VEC VEC. */
11682 static tree
11683 make_argument_pack (tree vec)
11685 tree pack;
11686 tree elt = TREE_VEC_ELT (vec, 0);
11687 if (TYPE_P (elt))
11688 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11689 else
11691 pack = make_node (NONTYPE_ARGUMENT_PACK);
11692 TREE_CONSTANT (pack) = 1;
11694 SET_ARGUMENT_PACK_ARGS (pack, vec);
11695 return pack;
11698 /* Return an exact copy of template args T that can be modified
11699 independently. */
11701 static tree
11702 copy_template_args (tree t)
11704 if (t == error_mark_node)
11705 return t;
11707 int len = TREE_VEC_LENGTH (t);
11708 tree new_vec = make_tree_vec (len);
11710 for (int i = 0; i < len; ++i)
11712 tree elt = TREE_VEC_ELT (t, i);
11713 if (elt && TREE_CODE (elt) == TREE_VEC)
11714 elt = copy_template_args (elt);
11715 TREE_VEC_ELT (new_vec, i) = elt;
11718 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11719 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11721 return new_vec;
11724 /* Substitute ARGS into the vector or list of template arguments T. */
11726 static tree
11727 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11729 tree orig_t = t;
11730 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11731 tree *elts;
11733 if (t == error_mark_node)
11734 return error_mark_node;
11736 len = TREE_VEC_LENGTH (t);
11737 elts = XALLOCAVEC (tree, len);
11739 for (i = 0; i < len; i++)
11741 tree orig_arg = TREE_VEC_ELT (t, i);
11742 tree new_arg;
11744 if (TREE_CODE (orig_arg) == TREE_VEC)
11745 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11746 else if (PACK_EXPANSION_P (orig_arg))
11748 /* Substitute into an expansion expression. */
11749 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11751 if (TREE_CODE (new_arg) == TREE_VEC)
11752 /* Add to the expanded length adjustment the number of
11753 expanded arguments. We subtract one from this
11754 measurement, because the argument pack expression
11755 itself is already counted as 1 in
11756 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11757 the argument pack is empty. */
11758 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11760 else if (ARGUMENT_PACK_P (orig_arg))
11762 /* Substitute into each of the arguments. */
11763 new_arg = TYPE_P (orig_arg)
11764 ? cxx_make_type (TREE_CODE (orig_arg))
11765 : make_node (TREE_CODE (orig_arg));
11767 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11768 args, complain, in_decl);
11769 if (pack_args == error_mark_node)
11770 new_arg = error_mark_node;
11771 else
11772 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11774 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11775 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11777 else
11778 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11780 if (new_arg == error_mark_node)
11781 return error_mark_node;
11783 elts[i] = new_arg;
11784 if (new_arg != orig_arg)
11785 need_new = 1;
11788 if (!need_new)
11789 return t;
11791 /* Make space for the expanded arguments coming from template
11792 argument packs. */
11793 t = make_tree_vec (len + expanded_len_adjust);
11794 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11795 arguments for a member template.
11796 In that case each TREE_VEC in ORIG_T represents a level of template
11797 arguments, and ORIG_T won't carry any non defaulted argument count.
11798 It will rather be the nested TREE_VECs that will carry one.
11799 In other words, ORIG_T carries a non defaulted argument count only
11800 if it doesn't contain any nested TREE_VEC. */
11801 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11803 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11804 count += expanded_len_adjust;
11805 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11807 for (i = 0, out = 0; i < len; i++)
11809 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11810 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11811 && TREE_CODE (elts[i]) == TREE_VEC)
11813 int idx;
11815 /* Now expand the template argument pack "in place". */
11816 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11817 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11819 else
11821 TREE_VEC_ELT (t, out) = elts[i];
11822 out++;
11826 return t;
11829 /* Substitute ARGS into one level PARMS of template parameters. */
11831 static tree
11832 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11834 if (parms == error_mark_node)
11835 return error_mark_node;
11837 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11839 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11841 tree tuple = TREE_VEC_ELT (parms, i);
11843 if (tuple == error_mark_node)
11844 continue;
11846 TREE_VEC_ELT (new_vec, i) =
11847 tsubst_template_parm (tuple, args, complain);
11850 return new_vec;
11853 /* Return the result of substituting ARGS into the template parameters
11854 given by PARMS. If there are m levels of ARGS and m + n levels of
11855 PARMS, then the result will contain n levels of PARMS. For
11856 example, if PARMS is `template <class T> template <class U>
11857 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11858 result will be `template <int*, double, class V>'. */
11860 static tree
11861 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11863 tree r = NULL_TREE;
11864 tree* new_parms;
11866 /* When substituting into a template, we must set
11867 PROCESSING_TEMPLATE_DECL as the template parameters may be
11868 dependent if they are based on one-another, and the dependency
11869 predicates are short-circuit outside of templates. */
11870 ++processing_template_decl;
11872 for (new_parms = &r;
11873 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11874 new_parms = &(TREE_CHAIN (*new_parms)),
11875 parms = TREE_CHAIN (parms))
11877 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11878 args, complain);
11879 *new_parms =
11880 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11881 - TMPL_ARGS_DEPTH (args)),
11882 new_vec, NULL_TREE);
11885 --processing_template_decl;
11887 return r;
11890 /* Return the result of substituting ARGS into one template parameter
11891 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11892 parameter and which TREE_PURPOSE is the default argument of the
11893 template parameter. */
11895 static tree
11896 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11898 tree default_value, parm_decl;
11900 if (args == NULL_TREE
11901 || t == NULL_TREE
11902 || t == error_mark_node)
11903 return t;
11905 gcc_assert (TREE_CODE (t) == TREE_LIST);
11907 default_value = TREE_PURPOSE (t);
11908 parm_decl = TREE_VALUE (t);
11910 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11911 if (TREE_CODE (parm_decl) == PARM_DECL
11912 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11913 parm_decl = error_mark_node;
11914 default_value = tsubst_template_arg (default_value, args,
11915 complain, NULL_TREE);
11917 return build_tree_list (default_value, parm_decl);
11920 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11921 type T. If T is not an aggregate or enumeration type, it is
11922 handled as if by tsubst. IN_DECL is as for tsubst. If
11923 ENTERING_SCOPE is nonzero, T is the context for a template which
11924 we are presently tsubst'ing. Return the substituted value. */
11926 static tree
11927 tsubst_aggr_type (tree t,
11928 tree args,
11929 tsubst_flags_t complain,
11930 tree in_decl,
11931 int entering_scope)
11933 if (t == NULL_TREE)
11934 return NULL_TREE;
11936 switch (TREE_CODE (t))
11938 case RECORD_TYPE:
11939 if (TYPE_PTRMEMFUNC_P (t))
11940 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11942 /* Fall through. */
11943 case ENUMERAL_TYPE:
11944 case UNION_TYPE:
11945 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11947 tree argvec;
11948 tree context;
11949 tree r;
11950 int saved_unevaluated_operand;
11951 int saved_inhibit_evaluation_warnings;
11953 /* In "sizeof(X<I>)" we need to evaluate "I". */
11954 saved_unevaluated_operand = cp_unevaluated_operand;
11955 cp_unevaluated_operand = 0;
11956 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11957 c_inhibit_evaluation_warnings = 0;
11959 /* First, determine the context for the type we are looking
11960 up. */
11961 context = TYPE_CONTEXT (t);
11962 if (context && TYPE_P (context))
11964 context = tsubst_aggr_type (context, args, complain,
11965 in_decl, /*entering_scope=*/1);
11966 /* If context is a nested class inside a class template,
11967 it may still need to be instantiated (c++/33959). */
11968 context = complete_type (context);
11971 /* Then, figure out what arguments are appropriate for the
11972 type we are trying to find. For example, given:
11974 template <class T> struct S;
11975 template <class T, class U> void f(T, U) { S<U> su; }
11977 and supposing that we are instantiating f<int, double>,
11978 then our ARGS will be {int, double}, but, when looking up
11979 S we only want {double}. */
11980 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11981 complain, in_decl);
11982 if (argvec == error_mark_node)
11983 r = error_mark_node;
11984 else
11986 r = lookup_template_class (t, argvec, in_decl, context,
11987 entering_scope, complain);
11988 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11991 cp_unevaluated_operand = saved_unevaluated_operand;
11992 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11994 return r;
11996 else
11997 /* This is not a template type, so there's nothing to do. */
11998 return t;
12000 default:
12001 return tsubst (t, args, complain, in_decl);
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, tree type, tree arg, tsubst_flags_t complain)
12011 tree saved_class_ptr = NULL_TREE;
12012 tree saved_class_ref = NULL_TREE;
12013 int errs = errorcount + sorrycount;
12015 /* This can happen in invalid code. */
12016 if (TREE_CODE (arg) == DEFAULT_ARG)
12017 return arg;
12019 /* This default argument came from a template. Instantiate the
12020 default argument here, not in tsubst. In the case of
12021 something like:
12023 template <class T>
12024 struct S {
12025 static T t();
12026 void f(T = t());
12029 we must be careful to do name lookup in the scope of S<T>,
12030 rather than in the current class. */
12031 push_access_scope (fn);
12032 /* The "this" pointer is not valid in a default argument. */
12033 if (cfun)
12035 saved_class_ptr = current_class_ptr;
12036 cp_function_chain->x_current_class_ptr = NULL_TREE;
12037 saved_class_ref = current_class_ref;
12038 cp_function_chain->x_current_class_ref = NULL_TREE;
12041 push_deferring_access_checks(dk_no_deferred);
12042 /* The default argument expression may cause implicitly defined
12043 member functions to be synthesized, which will result in garbage
12044 collection. We must treat this situation as if we were within
12045 the body of function so as to avoid collecting live data on the
12046 stack. */
12047 ++function_depth;
12048 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12049 complain, NULL_TREE,
12050 /*integral_constant_expression_p=*/false);
12051 --function_depth;
12052 pop_deferring_access_checks();
12054 /* Restore the "this" pointer. */
12055 if (cfun)
12057 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12058 cp_function_chain->x_current_class_ref = saved_class_ref;
12061 if (errorcount+sorrycount > errs
12062 && (complain & tf_warning_or_error))
12063 inform (input_location,
12064 " when instantiating default argument for call to %qD", fn);
12066 /* Make sure the default argument is reasonable. */
12067 arg = check_default_argument (type, arg, complain);
12069 pop_access_scope (fn);
12071 return arg;
12074 /* Substitute into all the default arguments for FN. */
12076 static void
12077 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12079 tree arg;
12080 tree tmpl_args;
12082 tmpl_args = DECL_TI_ARGS (fn);
12084 /* If this function is not yet instantiated, we certainly don't need
12085 its default arguments. */
12086 if (uses_template_parms (tmpl_args))
12087 return;
12088 /* Don't do this again for clones. */
12089 if (DECL_CLONED_FUNCTION_P (fn))
12090 return;
12092 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12093 arg;
12094 arg = TREE_CHAIN (arg))
12095 if (TREE_PURPOSE (arg))
12096 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12097 TREE_VALUE (arg),
12098 TREE_PURPOSE (arg),
12099 complain);
12102 /* Substitute the ARGS into the T, which is a _DECL. Return the
12103 result of the substitution. Issue error and warning messages under
12104 control of COMPLAIN. */
12106 static tree
12107 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12109 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12110 location_t saved_loc;
12111 tree r = NULL_TREE;
12112 tree in_decl = t;
12113 hashval_t hash = 0;
12115 /* Set the filename and linenumber to improve error-reporting. */
12116 saved_loc = input_location;
12117 input_location = DECL_SOURCE_LOCATION (t);
12119 switch (TREE_CODE (t))
12121 case TEMPLATE_DECL:
12123 /* We can get here when processing a member function template,
12124 member class template, or template template parameter. */
12125 tree decl = DECL_TEMPLATE_RESULT (t);
12126 tree spec;
12127 tree tmpl_args;
12128 tree full_args;
12130 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12132 /* Template template parameter is treated here. */
12133 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12134 if (new_type == error_mark_node)
12135 r = error_mark_node;
12136 /* If we get a real template back, return it. This can happen in
12137 the context of most_specialized_partial_spec. */
12138 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12139 r = new_type;
12140 else
12141 /* The new TEMPLATE_DECL was built in
12142 reduce_template_parm_level. */
12143 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12144 break;
12147 /* We might already have an instance of this template.
12148 The ARGS are for the surrounding class type, so the
12149 full args contain the tsubst'd args for the context,
12150 plus the innermost args from the template decl. */
12151 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12152 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12153 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12154 /* Because this is a template, the arguments will still be
12155 dependent, even after substitution. If
12156 PROCESSING_TEMPLATE_DECL is not set, the dependency
12157 predicates will short-circuit. */
12158 ++processing_template_decl;
12159 full_args = tsubst_template_args (tmpl_args, args,
12160 complain, in_decl);
12161 --processing_template_decl;
12162 if (full_args == error_mark_node)
12163 RETURN (error_mark_node);
12165 /* If this is a default template template argument,
12166 tsubst might not have changed anything. */
12167 if (full_args == tmpl_args)
12168 RETURN (t);
12170 hash = hash_tmpl_and_args (t, full_args);
12171 spec = retrieve_specialization (t, full_args, hash);
12172 if (spec != NULL_TREE)
12174 r = spec;
12175 break;
12178 /* Make a new template decl. It will be similar to the
12179 original, but will record the current template arguments.
12180 We also create a new function declaration, which is just
12181 like the old one, but points to this new template, rather
12182 than the old one. */
12183 r = copy_decl (t);
12184 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12185 DECL_CHAIN (r) = NULL_TREE;
12187 // Build new template info linking to the original template decl.
12188 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12190 if (TREE_CODE (decl) == TYPE_DECL
12191 && !TYPE_DECL_ALIAS_P (decl))
12193 tree new_type;
12194 ++processing_template_decl;
12195 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12196 --processing_template_decl;
12197 if (new_type == error_mark_node)
12198 RETURN (error_mark_node);
12200 TREE_TYPE (r) = new_type;
12201 /* For a partial specialization, we need to keep pointing to
12202 the primary template. */
12203 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12204 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12205 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12206 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12207 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12209 else
12211 tree new_decl;
12212 ++processing_template_decl;
12213 new_decl = tsubst (decl, args, complain, in_decl);
12214 --processing_template_decl;
12215 if (new_decl == error_mark_node)
12216 RETURN (error_mark_node);
12218 DECL_TEMPLATE_RESULT (r) = new_decl;
12219 DECL_TI_TEMPLATE (new_decl) = r;
12220 TREE_TYPE (r) = TREE_TYPE (new_decl);
12221 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12222 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12225 SET_DECL_IMPLICIT_INSTANTIATION (r);
12226 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12227 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12229 /* The template parameters for this new template are all the
12230 template parameters for the old template, except the
12231 outermost level of parameters. */
12232 DECL_TEMPLATE_PARMS (r)
12233 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12234 complain);
12236 if (PRIMARY_TEMPLATE_P (t))
12237 DECL_PRIMARY_TEMPLATE (r) = r;
12239 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12240 /* Record this non-type partial instantiation. */
12241 register_specialization (r, t,
12242 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12243 false, hash);
12245 break;
12247 case FUNCTION_DECL:
12249 tree gen_tmpl, argvec;
12251 /* Nobody should be tsubst'ing into non-template functions. */
12252 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12254 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12256 /* If T is not dependent, just return it. */
12257 if (!uses_template_parms (DECL_TI_ARGS (t)))
12258 RETURN (t);
12260 /* Calculate the most general template of which R is a
12261 specialization, and the complete set of arguments used to
12262 specialize R. */
12263 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12264 argvec = tsubst_template_args (DECL_TI_ARGS
12265 (DECL_TEMPLATE_RESULT
12266 (DECL_TI_TEMPLATE (t))),
12267 args, complain, in_decl);
12268 if (argvec == error_mark_node)
12269 RETURN (error_mark_node);
12271 /* Check to see if we already have this specialization. */
12272 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12273 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12275 r = spec;
12276 break;
12279 /* We can see more levels of arguments than parameters if
12280 there was a specialization of a member template, like
12281 this:
12283 template <class T> struct S { template <class U> void f(); }
12284 template <> template <class U> void S<int>::f(U);
12286 Here, we'll be substituting into the specialization,
12287 because that's where we can find the code we actually
12288 want to generate, but we'll have enough arguments for
12289 the most general template.
12291 We also deal with the peculiar case:
12293 template <class T> struct S {
12294 template <class U> friend void f();
12296 template <class U> void f() {}
12297 template S<int>;
12298 template void f<double>();
12300 Here, the ARGS for the instantiation of will be {int,
12301 double}. But, we only need as many ARGS as there are
12302 levels of template parameters in CODE_PATTERN. We are
12303 careful not to get fooled into reducing the ARGS in
12304 situations like:
12306 template <class T> struct S { template <class U> void f(U); }
12307 template <class T> template <> void S<T>::f(int) {}
12309 which we can spot because the pattern will be a
12310 specialization in this case. */
12311 int args_depth = TMPL_ARGS_DEPTH (args);
12312 int parms_depth =
12313 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12315 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12316 args = get_innermost_template_args (args, parms_depth);
12318 else
12320 /* This special case arises when we have something like this:
12322 template <class T> struct S {
12323 friend void f<int>(int, double);
12326 Here, the DECL_TI_TEMPLATE for the friend declaration
12327 will be an IDENTIFIER_NODE. We are being called from
12328 tsubst_friend_function, and we want only to create a
12329 new decl (R) with appropriate types so that we can call
12330 determine_specialization. */
12331 gen_tmpl = NULL_TREE;
12332 argvec = NULL_TREE;
12335 tree ctx = DECL_CONTEXT (t);
12336 bool member = ctx && TYPE_P (ctx);
12338 if (member)
12339 ctx = tsubst_aggr_type (ctx, args,
12340 complain, t, /*entering_scope=*/1);
12342 tree type = tsubst (TREE_TYPE (t), args,
12343 complain | tf_fndecl_type, in_decl);
12344 if (type == error_mark_node)
12345 RETURN (error_mark_node);
12347 /* If we hit excessive deduction depth, the type is bogus even if
12348 it isn't error_mark_node, so don't build a decl. */
12349 if (excessive_deduction_depth)
12350 RETURN (error_mark_node);
12352 /* We do NOT check for matching decls pushed separately at this
12353 point, as they may not represent instantiations of this
12354 template, and in any case are considered separate under the
12355 discrete model. */
12356 r = copy_decl (t);
12357 DECL_USE_TEMPLATE (r) = 0;
12358 TREE_TYPE (r) = type;
12359 /* Clear out the mangled name and RTL for the instantiation. */
12360 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12361 SET_DECL_RTL (r, NULL);
12362 /* Leave DECL_INITIAL set on deleted instantiations. */
12363 if (!DECL_DELETED_FN (r))
12364 DECL_INITIAL (r) = NULL_TREE;
12365 DECL_CONTEXT (r) = ctx;
12367 /* OpenMP UDRs have the only argument a reference to the declared
12368 type. We want to diagnose if the declared type is a reference,
12369 which is invalid, but as references to references are usually
12370 quietly merged, diagnose it here. */
12371 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12373 tree argtype
12374 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12375 argtype = tsubst (argtype, args, complain, in_decl);
12376 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12377 error_at (DECL_SOURCE_LOCATION (t),
12378 "reference type %qT in "
12379 "%<#pragma omp declare reduction%>", argtype);
12380 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12381 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12382 argtype);
12385 if (member && DECL_CONV_FN_P (r))
12386 /* Type-conversion operator. Reconstruct the name, in
12387 case it's the name of one of the template's parameters. */
12388 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12390 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12391 complain, t);
12392 DECL_RESULT (r) = NULL_TREE;
12394 TREE_STATIC (r) = 0;
12395 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12396 DECL_EXTERNAL (r) = 1;
12397 /* If this is an instantiation of a function with internal
12398 linkage, we already know what object file linkage will be
12399 assigned to the instantiation. */
12400 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12401 DECL_DEFER_OUTPUT (r) = 0;
12402 DECL_CHAIN (r) = NULL_TREE;
12403 DECL_PENDING_INLINE_INFO (r) = 0;
12404 DECL_PENDING_INLINE_P (r) = 0;
12405 DECL_SAVED_TREE (r) = NULL_TREE;
12406 DECL_STRUCT_FUNCTION (r) = NULL;
12407 TREE_USED (r) = 0;
12408 /* We'll re-clone as appropriate in instantiate_template. */
12409 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12411 /* If we aren't complaining now, return on error before we register
12412 the specialization so that we'll complain eventually. */
12413 if ((complain & tf_error) == 0
12414 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12415 && !grok_op_properties (r, /*complain=*/false))
12416 RETURN (error_mark_node);
12418 /* When instantiating a constrained member, substitute
12419 into the constraints to create a new constraint. */
12420 if (tree ci = get_constraints (t))
12421 if (member)
12423 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12424 set_constraints (r, ci);
12427 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12428 this in the special friend case mentioned above where
12429 GEN_TMPL is NULL. */
12430 if (gen_tmpl)
12432 DECL_TEMPLATE_INFO (r)
12433 = build_template_info (gen_tmpl, argvec);
12434 SET_DECL_IMPLICIT_INSTANTIATION (r);
12436 tree new_r
12437 = register_specialization (r, gen_tmpl, argvec, false, hash);
12438 if (new_r != r)
12439 /* We instantiated this while substituting into
12440 the type earlier (template/friend54.C). */
12441 RETURN (new_r);
12443 /* We're not supposed to instantiate default arguments
12444 until they are called, for a template. But, for a
12445 declaration like:
12447 template <class T> void f ()
12448 { extern void g(int i = T()); }
12450 we should do the substitution when the template is
12451 instantiated. We handle the member function case in
12452 instantiate_class_template since the default arguments
12453 might refer to other members of the class. */
12454 if (!member
12455 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12456 && !uses_template_parms (argvec))
12457 tsubst_default_arguments (r, complain);
12459 else
12460 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12462 /* Copy the list of befriending classes. */
12463 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12464 *friends;
12465 friends = &TREE_CHAIN (*friends))
12467 *friends = copy_node (*friends);
12468 TREE_VALUE (*friends)
12469 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12472 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12474 maybe_retrofit_in_chrg (r);
12475 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12476 RETURN (error_mark_node);
12477 /* If this is an instantiation of a member template, clone it.
12478 If it isn't, that'll be handled by
12479 clone_constructors_and_destructors. */
12480 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12481 clone_function_decl (r, /*update_methods=*/false);
12483 else if ((complain & tf_error) != 0
12484 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12485 && !grok_op_properties (r, /*complain=*/true))
12486 RETURN (error_mark_node);
12488 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12489 SET_DECL_FRIEND_CONTEXT (r,
12490 tsubst (DECL_FRIEND_CONTEXT (t),
12491 args, complain, in_decl));
12493 /* Possibly limit visibility based on template args. */
12494 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12495 if (DECL_VISIBILITY_SPECIFIED (t))
12497 DECL_VISIBILITY_SPECIFIED (r) = 0;
12498 DECL_ATTRIBUTES (r)
12499 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12501 determine_visibility (r);
12502 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12503 && !processing_template_decl)
12504 defaulted_late_check (r);
12506 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12507 args, complain, in_decl);
12509 break;
12511 case PARM_DECL:
12513 tree type = NULL_TREE;
12514 int i, len = 1;
12515 tree expanded_types = NULL_TREE;
12516 tree prev_r = NULL_TREE;
12517 tree first_r = NULL_TREE;
12519 if (DECL_PACK_P (t))
12521 /* If there is a local specialization that isn't a
12522 parameter pack, it means that we're doing a "simple"
12523 substitution from inside tsubst_pack_expansion. Just
12524 return the local specialization (which will be a single
12525 parm). */
12526 tree spec = retrieve_local_specialization (t);
12527 if (spec
12528 && TREE_CODE (spec) == PARM_DECL
12529 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12530 RETURN (spec);
12532 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12533 the parameters in this function parameter pack. */
12534 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12535 complain, in_decl);
12536 if (TREE_CODE (expanded_types) == TREE_VEC)
12538 len = TREE_VEC_LENGTH (expanded_types);
12540 /* Zero-length parameter packs are boring. Just substitute
12541 into the chain. */
12542 if (len == 0)
12543 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12544 TREE_CHAIN (t)));
12546 else
12548 /* All we did was update the type. Make a note of that. */
12549 type = expanded_types;
12550 expanded_types = NULL_TREE;
12554 /* Loop through all of the parameters we'll build. When T is
12555 a function parameter pack, LEN is the number of expanded
12556 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12557 r = NULL_TREE;
12558 for (i = 0; i < len; ++i)
12560 prev_r = r;
12561 r = copy_node (t);
12562 if (DECL_TEMPLATE_PARM_P (t))
12563 SET_DECL_TEMPLATE_PARM_P (r);
12565 if (expanded_types)
12566 /* We're on the Ith parameter of the function parameter
12567 pack. */
12569 /* Get the Ith type. */
12570 type = TREE_VEC_ELT (expanded_types, i);
12572 /* Rename the parameter to include the index. */
12573 DECL_NAME (r)
12574 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12576 else if (!type)
12577 /* We're dealing with a normal parameter. */
12578 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12580 type = type_decays_to (type);
12581 TREE_TYPE (r) = type;
12582 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12584 if (DECL_INITIAL (r))
12586 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12587 DECL_INITIAL (r) = TREE_TYPE (r);
12588 else
12589 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12590 complain, in_decl);
12593 DECL_CONTEXT (r) = NULL_TREE;
12595 if (!DECL_TEMPLATE_PARM_P (r))
12596 DECL_ARG_TYPE (r) = type_passed_as (type);
12598 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12599 args, complain, in_decl);
12601 /* Keep track of the first new parameter we
12602 generate. That's what will be returned to the
12603 caller. */
12604 if (!first_r)
12605 first_r = r;
12607 /* Build a proper chain of parameters when substituting
12608 into a function parameter pack. */
12609 if (prev_r)
12610 DECL_CHAIN (prev_r) = r;
12613 /* If cp_unevaluated_operand is set, we're just looking for a
12614 single dummy parameter, so don't keep going. */
12615 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12616 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12617 complain, DECL_CHAIN (t));
12619 /* FIRST_R contains the start of the chain we've built. */
12620 r = first_r;
12622 break;
12624 case FIELD_DECL:
12626 tree type = NULL_TREE;
12627 tree vec = NULL_TREE;
12628 tree expanded_types = NULL_TREE;
12629 int len = 1;
12631 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12633 /* This field is a lambda capture pack. Return a TREE_VEC of
12634 the expanded fields to instantiate_class_template_1 and
12635 store them in the specializations hash table as a
12636 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12637 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12638 complain, in_decl);
12639 if (TREE_CODE (expanded_types) == TREE_VEC)
12641 len = TREE_VEC_LENGTH (expanded_types);
12642 vec = make_tree_vec (len);
12644 else
12646 /* All we did was update the type. Make a note of that. */
12647 type = expanded_types;
12648 expanded_types = NULL_TREE;
12652 for (int i = 0; i < len; ++i)
12654 r = copy_decl (t);
12655 if (expanded_types)
12657 type = TREE_VEC_ELT (expanded_types, i);
12658 DECL_NAME (r)
12659 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12661 else if (!type)
12662 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12664 if (type == error_mark_node)
12665 RETURN (error_mark_node);
12666 TREE_TYPE (r) = type;
12667 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12669 if (DECL_C_BIT_FIELD (r))
12670 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12671 non-bit-fields DECL_INITIAL is a non-static data member
12672 initializer, which gets deferred instantiation. */
12673 DECL_INITIAL (r)
12674 = tsubst_expr (DECL_INITIAL (t), args,
12675 complain, in_decl,
12676 /*integral_constant_expression_p=*/true);
12677 else if (DECL_INITIAL (t))
12679 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12680 NSDMI in perform_member_init. Still set DECL_INITIAL
12681 so that we know there is one. */
12682 DECL_INITIAL (r) = void_node;
12683 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12684 retrofit_lang_decl (r);
12685 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12687 /* We don't have to set DECL_CONTEXT here; it is set by
12688 finish_member_declaration. */
12689 DECL_CHAIN (r) = NULL_TREE;
12691 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12692 args, complain, in_decl);
12694 if (vec)
12695 TREE_VEC_ELT (vec, i) = r;
12698 if (vec)
12700 r = vec;
12701 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12702 SET_ARGUMENT_PACK_ARGS (pack, vec);
12703 register_specialization (pack, t, args, false, 0);
12706 break;
12708 case USING_DECL:
12709 /* We reach here only for member using decls. We also need to check
12710 uses_template_parms because DECL_DEPENDENT_P is not set for a
12711 using-declaration that designates a member of the current
12712 instantiation (c++/53549). */
12713 if (DECL_DEPENDENT_P (t)
12714 || uses_template_parms (USING_DECL_SCOPE (t)))
12716 tree scope = USING_DECL_SCOPE (t);
12717 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12718 if (PACK_EXPANSION_P (scope))
12720 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12721 int len = TREE_VEC_LENGTH (vec);
12722 r = make_tree_vec (len);
12723 for (int i = 0; i < len; ++i)
12725 tree escope = TREE_VEC_ELT (vec, i);
12726 tree elt = do_class_using_decl (escope, name);
12727 if (!elt)
12729 r = error_mark_node;
12730 break;
12732 else
12734 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12735 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12737 TREE_VEC_ELT (r, i) = elt;
12740 else
12742 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12743 complain, in_decl);
12744 r = do_class_using_decl (inst_scope, name);
12745 if (!r)
12746 r = error_mark_node;
12747 else
12749 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12750 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12754 else
12756 r = copy_node (t);
12757 DECL_CHAIN (r) = NULL_TREE;
12759 break;
12761 case TYPE_DECL:
12762 case VAR_DECL:
12764 tree argvec = NULL_TREE;
12765 tree gen_tmpl = NULL_TREE;
12766 tree spec;
12767 tree tmpl = NULL_TREE;
12768 tree ctx;
12769 tree type = NULL_TREE;
12770 bool local_p;
12772 if (TREE_TYPE (t) == error_mark_node)
12773 RETURN (error_mark_node);
12775 if (TREE_CODE (t) == TYPE_DECL
12776 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12778 /* If this is the canonical decl, we don't have to
12779 mess with instantiations, and often we can't (for
12780 typename, template type parms and such). Note that
12781 TYPE_NAME is not correct for the above test if
12782 we've copied the type for a typedef. */
12783 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12784 if (type == error_mark_node)
12785 RETURN (error_mark_node);
12786 r = TYPE_NAME (type);
12787 break;
12790 /* Check to see if we already have the specialization we
12791 need. */
12792 spec = NULL_TREE;
12793 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12795 /* T is a static data member or namespace-scope entity.
12796 We have to substitute into namespace-scope variables
12797 (not just variable templates) because of cases like:
12799 template <class T> void f() { extern T t; }
12801 where the entity referenced is not known until
12802 instantiation time. */
12803 local_p = false;
12804 ctx = DECL_CONTEXT (t);
12805 if (DECL_CLASS_SCOPE_P (t))
12807 ctx = tsubst_aggr_type (ctx, args,
12808 complain,
12809 in_decl, /*entering_scope=*/1);
12810 /* If CTX is unchanged, then T is in fact the
12811 specialization we want. That situation occurs when
12812 referencing a static data member within in its own
12813 class. We can use pointer equality, rather than
12814 same_type_p, because DECL_CONTEXT is always
12815 canonical... */
12816 if (ctx == DECL_CONTEXT (t)
12817 /* ... unless T is a member template; in which
12818 case our caller can be willing to create a
12819 specialization of that template represented
12820 by T. */
12821 && !(DECL_TI_TEMPLATE (t)
12822 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12823 spec = t;
12826 if (!spec)
12828 tmpl = DECL_TI_TEMPLATE (t);
12829 gen_tmpl = most_general_template (tmpl);
12830 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12831 if (argvec != error_mark_node)
12832 argvec = (coerce_innermost_template_parms
12833 (DECL_TEMPLATE_PARMS (gen_tmpl),
12834 argvec, t, complain,
12835 /*all*/true, /*defarg*/true));
12836 if (argvec == error_mark_node)
12837 RETURN (error_mark_node);
12838 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12839 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12842 else
12844 /* A local variable. */
12845 local_p = true;
12846 /* Subsequent calls to pushdecl will fill this in. */
12847 ctx = NULL_TREE;
12848 /* Unless this is a reference to a static variable from an
12849 enclosing function, in which case we need to fill it in now. */
12850 if (TREE_STATIC (t))
12852 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12853 if (fn != current_function_decl)
12854 ctx = fn;
12856 spec = retrieve_local_specialization (t);
12858 /* If we already have the specialization we need, there is
12859 nothing more to do. */
12860 if (spec)
12862 r = spec;
12863 break;
12866 /* Create a new node for the specialization we need. */
12867 r = copy_decl (t);
12868 if (type == NULL_TREE)
12870 if (is_typedef_decl (t))
12871 type = DECL_ORIGINAL_TYPE (t);
12872 else
12873 type = TREE_TYPE (t);
12874 if (VAR_P (t)
12875 && VAR_HAD_UNKNOWN_BOUND (t)
12876 && type != error_mark_node)
12877 type = strip_array_domain (type);
12878 type = tsubst (type, args, complain, in_decl);
12880 if (VAR_P (r))
12882 /* Even if the original location is out of scope, the
12883 newly substituted one is not. */
12884 DECL_DEAD_FOR_LOCAL (r) = 0;
12885 DECL_INITIALIZED_P (r) = 0;
12886 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12887 if (type == error_mark_node)
12888 RETURN (error_mark_node);
12889 if (TREE_CODE (type) == FUNCTION_TYPE)
12891 /* It may seem that this case cannot occur, since:
12893 typedef void f();
12894 void g() { f x; }
12896 declares a function, not a variable. However:
12898 typedef void f();
12899 template <typename T> void g() { T t; }
12900 template void g<f>();
12902 is an attempt to declare a variable with function
12903 type. */
12904 error ("variable %qD has function type",
12905 /* R is not yet sufficiently initialized, so we
12906 just use its name. */
12907 DECL_NAME (r));
12908 RETURN (error_mark_node);
12910 type = complete_type (type);
12911 /* Wait until cp_finish_decl to set this again, to handle
12912 circular dependency (template/instantiate6.C). */
12913 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12914 type = check_var_type (DECL_NAME (r), type);
12916 if (DECL_HAS_VALUE_EXPR_P (t))
12918 tree ve = DECL_VALUE_EXPR (t);
12919 ve = tsubst_expr (ve, args, complain, in_decl,
12920 /*constant_expression_p=*/false);
12921 if (REFERENCE_REF_P (ve))
12923 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12924 ve = TREE_OPERAND (ve, 0);
12926 SET_DECL_VALUE_EXPR (r, ve);
12928 if (CP_DECL_THREAD_LOCAL_P (r)
12929 && !processing_template_decl)
12930 set_decl_tls_model (r, decl_default_tls_model (r));
12932 else if (DECL_SELF_REFERENCE_P (t))
12933 SET_DECL_SELF_REFERENCE_P (r);
12934 TREE_TYPE (r) = type;
12935 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12936 DECL_CONTEXT (r) = ctx;
12937 /* Clear out the mangled name and RTL for the instantiation. */
12938 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12939 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12940 SET_DECL_RTL (r, NULL);
12941 /* The initializer must not be expanded until it is required;
12942 see [temp.inst]. */
12943 DECL_INITIAL (r) = NULL_TREE;
12944 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12945 if (VAR_P (r))
12947 SET_DECL_MODE (r, VOIDmode);
12949 /* Possibly limit visibility based on template args. */
12950 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12951 if (DECL_VISIBILITY_SPECIFIED (t))
12953 DECL_VISIBILITY_SPECIFIED (r) = 0;
12954 DECL_ATTRIBUTES (r)
12955 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12957 determine_visibility (r);
12960 if (!local_p)
12962 /* A static data member declaration is always marked
12963 external when it is declared in-class, even if an
12964 initializer is present. We mimic the non-template
12965 processing here. */
12966 DECL_EXTERNAL (r) = 1;
12967 if (DECL_NAMESPACE_SCOPE_P (t))
12968 DECL_NOT_REALLY_EXTERN (r) = 1;
12970 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12971 SET_DECL_IMPLICIT_INSTANTIATION (r);
12972 register_specialization (r, gen_tmpl, argvec, false, hash);
12974 else
12976 if (DECL_LANG_SPECIFIC (r))
12977 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12978 if (!cp_unevaluated_operand)
12979 register_local_specialization (r, t);
12982 DECL_CHAIN (r) = NULL_TREE;
12984 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12985 /*flags=*/0,
12986 args, complain, in_decl);
12988 /* Preserve a typedef that names a type. */
12989 if (is_typedef_decl (r) && type != error_mark_node)
12991 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12992 set_underlying_type (r);
12993 if (TYPE_DECL_ALIAS_P (r))
12994 /* An alias template specialization can be dependent
12995 even if its underlying type is not. */
12996 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12999 layout_decl (r, 0);
13001 break;
13003 default:
13004 gcc_unreachable ();
13006 #undef RETURN
13008 out:
13009 /* Restore the file and line information. */
13010 input_location = saved_loc;
13012 return r;
13015 /* Substitute into the ARG_TYPES of a function type.
13016 If END is a TREE_CHAIN, leave it and any following types
13017 un-substituted. */
13019 static tree
13020 tsubst_arg_types (tree arg_types,
13021 tree args,
13022 tree end,
13023 tsubst_flags_t complain,
13024 tree in_decl)
13026 tree remaining_arg_types;
13027 tree type = NULL_TREE;
13028 int i = 1;
13029 tree expanded_args = NULL_TREE;
13030 tree default_arg;
13032 if (!arg_types || arg_types == void_list_node || arg_types == end)
13033 return arg_types;
13035 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13036 args, end, complain, in_decl);
13037 if (remaining_arg_types == error_mark_node)
13038 return error_mark_node;
13040 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13042 /* For a pack expansion, perform substitution on the
13043 entire expression. Later on, we'll handle the arguments
13044 one-by-one. */
13045 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13046 args, complain, in_decl);
13048 if (TREE_CODE (expanded_args) == TREE_VEC)
13049 /* So that we'll spin through the parameters, one by one. */
13050 i = TREE_VEC_LENGTH (expanded_args);
13051 else
13053 /* We only partially substituted into the parameter
13054 pack. Our type is TYPE_PACK_EXPANSION. */
13055 type = expanded_args;
13056 expanded_args = NULL_TREE;
13060 while (i > 0) {
13061 --i;
13063 if (expanded_args)
13064 type = TREE_VEC_ELT (expanded_args, i);
13065 else if (!type)
13066 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13068 if (type == error_mark_node)
13069 return error_mark_node;
13070 if (VOID_TYPE_P (type))
13072 if (complain & tf_error)
13074 error ("invalid parameter type %qT", type);
13075 if (in_decl)
13076 error ("in declaration %q+D", in_decl);
13078 return error_mark_node;
13080 /* DR 657. */
13081 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13082 return error_mark_node;
13084 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13085 top-level qualifiers as required. */
13086 type = cv_unqualified (type_decays_to (type));
13088 /* We do not substitute into default arguments here. The standard
13089 mandates that they be instantiated only when needed, which is
13090 done in build_over_call. */
13091 default_arg = TREE_PURPOSE (arg_types);
13093 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13095 /* We've instantiated a template before its default arguments
13096 have been parsed. This can happen for a nested template
13097 class, and is not an error unless we require the default
13098 argument in a call of this function. */
13099 remaining_arg_types =
13100 tree_cons (default_arg, type, remaining_arg_types);
13101 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13103 else
13104 remaining_arg_types =
13105 hash_tree_cons (default_arg, type, remaining_arg_types);
13108 return remaining_arg_types;
13111 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13112 *not* handle the exception-specification for FNTYPE, because the
13113 initial substitution of explicitly provided template parameters
13114 during argument deduction forbids substitution into the
13115 exception-specification:
13117 [temp.deduct]
13119 All references in the function type of the function template to the
13120 corresponding template parameters are replaced by the specified tem-
13121 plate argument values. If a substitution in a template parameter or
13122 in the function type of the function template results in an invalid
13123 type, type deduction fails. [Note: The equivalent substitution in
13124 exception specifications is done only when the function is instanti-
13125 ated, at which point a program is ill-formed if the substitution
13126 results in an invalid type.] */
13128 static tree
13129 tsubst_function_type (tree t,
13130 tree args,
13131 tsubst_flags_t complain,
13132 tree in_decl)
13134 tree return_type;
13135 tree arg_types = NULL_TREE;
13136 tree fntype;
13138 /* The TYPE_CONTEXT is not used for function/method types. */
13139 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13141 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13142 failure. */
13143 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13145 if (late_return_type_p)
13147 /* Substitute the argument types. */
13148 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13149 complain, in_decl);
13150 if (arg_types == error_mark_node)
13151 return error_mark_node;
13153 tree save_ccp = current_class_ptr;
13154 tree save_ccr = current_class_ref;
13155 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13156 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13157 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13158 if (do_inject)
13160 /* DR 1207: 'this' is in scope in the trailing return type. */
13161 inject_this_parameter (this_type, cp_type_quals (this_type));
13164 /* Substitute the return type. */
13165 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13167 if (do_inject)
13169 current_class_ptr = save_ccp;
13170 current_class_ref = save_ccr;
13173 else
13174 /* Substitute the return type. */
13175 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13177 if (return_type == error_mark_node)
13178 return error_mark_node;
13179 /* DR 486 clarifies that creation of a function type with an
13180 invalid return type is a deduction failure. */
13181 if (TREE_CODE (return_type) == ARRAY_TYPE
13182 || TREE_CODE (return_type) == FUNCTION_TYPE)
13184 if (complain & tf_error)
13186 if (TREE_CODE (return_type) == ARRAY_TYPE)
13187 error ("function returning an array");
13188 else
13189 error ("function returning a function");
13191 return error_mark_node;
13193 /* And DR 657. */
13194 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13195 return error_mark_node;
13197 if (!late_return_type_p)
13199 /* Substitute the argument types. */
13200 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13201 complain, in_decl);
13202 if (arg_types == error_mark_node)
13203 return error_mark_node;
13206 /* Construct a new type node and return it. */
13207 if (TREE_CODE (t) == FUNCTION_TYPE)
13209 fntype = build_function_type (return_type, arg_types);
13210 fntype = apply_memfn_quals (fntype,
13211 type_memfn_quals (t),
13212 type_memfn_rqual (t));
13214 else
13216 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13217 /* Don't pick up extra function qualifiers from the basetype. */
13218 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13219 if (! MAYBE_CLASS_TYPE_P (r))
13221 /* [temp.deduct]
13223 Type deduction may fail for any of the following
13224 reasons:
13226 -- Attempting to create "pointer to member of T" when T
13227 is not a class type. */
13228 if (complain & tf_error)
13229 error ("creating pointer to member function of non-class type %qT",
13231 return error_mark_node;
13234 fntype = build_method_type_directly (r, return_type,
13235 TREE_CHAIN (arg_types));
13236 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13238 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13240 if (late_return_type_p)
13241 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13243 return fntype;
13246 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13247 ARGS into that specification, and return the substituted
13248 specification. If there is no specification, return NULL_TREE. */
13250 static tree
13251 tsubst_exception_specification (tree fntype,
13252 tree args,
13253 tsubst_flags_t complain,
13254 tree in_decl,
13255 bool defer_ok)
13257 tree specs;
13258 tree new_specs;
13260 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13261 new_specs = NULL_TREE;
13262 if (specs && TREE_PURPOSE (specs))
13264 /* A noexcept-specifier. */
13265 tree expr = TREE_PURPOSE (specs);
13266 if (TREE_CODE (expr) == INTEGER_CST)
13267 new_specs = expr;
13268 else if (defer_ok)
13270 /* Defer instantiation of noexcept-specifiers to avoid
13271 excessive instantiations (c++/49107). */
13272 new_specs = make_node (DEFERRED_NOEXCEPT);
13273 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13275 /* We already partially instantiated this member template,
13276 so combine the new args with the old. */
13277 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13278 = DEFERRED_NOEXCEPT_PATTERN (expr);
13279 DEFERRED_NOEXCEPT_ARGS (new_specs)
13280 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13282 else
13284 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13285 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13288 else
13289 new_specs = tsubst_copy_and_build
13290 (expr, args, complain, in_decl, /*function_p=*/false,
13291 /*integral_constant_expression_p=*/true);
13292 new_specs = build_noexcept_spec (new_specs, complain);
13294 else if (specs)
13296 if (! TREE_VALUE (specs))
13297 new_specs = specs;
13298 else
13299 while (specs)
13301 tree spec;
13302 int i, len = 1;
13303 tree expanded_specs = NULL_TREE;
13305 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13307 /* Expand the pack expansion type. */
13308 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13309 args, complain,
13310 in_decl);
13312 if (expanded_specs == error_mark_node)
13313 return error_mark_node;
13314 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13315 len = TREE_VEC_LENGTH (expanded_specs);
13316 else
13318 /* We're substituting into a member template, so
13319 we got a TYPE_PACK_EXPANSION back. Add that
13320 expansion and move on. */
13321 gcc_assert (TREE_CODE (expanded_specs)
13322 == TYPE_PACK_EXPANSION);
13323 new_specs = add_exception_specifier (new_specs,
13324 expanded_specs,
13325 complain);
13326 specs = TREE_CHAIN (specs);
13327 continue;
13331 for (i = 0; i < len; ++i)
13333 if (expanded_specs)
13334 spec = TREE_VEC_ELT (expanded_specs, i);
13335 else
13336 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13337 if (spec == error_mark_node)
13338 return spec;
13339 new_specs = add_exception_specifier (new_specs, spec,
13340 complain);
13343 specs = TREE_CHAIN (specs);
13346 return new_specs;
13349 /* Take the tree structure T and replace template parameters used
13350 therein with the argument vector ARGS. IN_DECL is an associated
13351 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13352 Issue error and warning messages under control of COMPLAIN. Note
13353 that we must be relatively non-tolerant of extensions here, in
13354 order to preserve conformance; if we allow substitutions that
13355 should not be allowed, we may allow argument deductions that should
13356 not succeed, and therefore report ambiguous overload situations
13357 where there are none. In theory, we could allow the substitution,
13358 but indicate that it should have failed, and allow our caller to
13359 make sure that the right thing happens, but we don't try to do this
13360 yet.
13362 This function is used for dealing with types, decls and the like;
13363 for expressions, use tsubst_expr or tsubst_copy. */
13365 tree
13366 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13368 enum tree_code code;
13369 tree type, r = NULL_TREE;
13371 if (t == NULL_TREE || t == error_mark_node
13372 || t == integer_type_node
13373 || t == void_type_node
13374 || t == char_type_node
13375 || t == unknown_type_node
13376 || TREE_CODE (t) == NAMESPACE_DECL
13377 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13378 return t;
13380 if (DECL_P (t))
13381 return tsubst_decl (t, args, complain);
13383 if (args == NULL_TREE)
13384 return t;
13386 code = TREE_CODE (t);
13388 if (code == IDENTIFIER_NODE)
13389 type = IDENTIFIER_TYPE_VALUE (t);
13390 else
13391 type = TREE_TYPE (t);
13393 gcc_assert (type != unknown_type_node);
13395 /* Reuse typedefs. We need to do this to handle dependent attributes,
13396 such as attribute aligned. */
13397 if (TYPE_P (t)
13398 && typedef_variant_p (t))
13400 tree decl = TYPE_NAME (t);
13402 if (alias_template_specialization_p (t))
13404 /* DECL represents an alias template and we want to
13405 instantiate it. */
13406 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13407 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13408 r = instantiate_alias_template (tmpl, gen_args, complain);
13410 else if (DECL_CLASS_SCOPE_P (decl)
13411 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13412 && uses_template_parms (DECL_CONTEXT (decl)))
13414 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13415 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13416 r = retrieve_specialization (tmpl, gen_args, 0);
13418 else if (DECL_FUNCTION_SCOPE_P (decl)
13419 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13420 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13421 r = retrieve_local_specialization (decl);
13422 else
13423 /* The typedef is from a non-template context. */
13424 return t;
13426 if (r)
13428 r = TREE_TYPE (r);
13429 r = cp_build_qualified_type_real
13430 (r, cp_type_quals (t) | cp_type_quals (r),
13431 complain | tf_ignore_bad_quals);
13432 return r;
13434 else
13436 /* We don't have an instantiation yet, so drop the typedef. */
13437 int quals = cp_type_quals (t);
13438 t = DECL_ORIGINAL_TYPE (decl);
13439 t = cp_build_qualified_type_real (t, quals,
13440 complain | tf_ignore_bad_quals);
13444 bool fndecl_type = (complain & tf_fndecl_type);
13445 complain &= ~tf_fndecl_type;
13447 if (type
13448 && code != TYPENAME_TYPE
13449 && code != TEMPLATE_TYPE_PARM
13450 && code != TEMPLATE_PARM_INDEX
13451 && code != IDENTIFIER_NODE
13452 && code != FUNCTION_TYPE
13453 && code != METHOD_TYPE)
13454 type = tsubst (type, args, complain, in_decl);
13455 if (type == error_mark_node)
13456 return error_mark_node;
13458 switch (code)
13460 case RECORD_TYPE:
13461 case UNION_TYPE:
13462 case ENUMERAL_TYPE:
13463 return tsubst_aggr_type (t, args, complain, in_decl,
13464 /*entering_scope=*/0);
13466 case ERROR_MARK:
13467 case IDENTIFIER_NODE:
13468 case VOID_TYPE:
13469 case REAL_TYPE:
13470 case COMPLEX_TYPE:
13471 case VECTOR_TYPE:
13472 case BOOLEAN_TYPE:
13473 case NULLPTR_TYPE:
13474 case LANG_TYPE:
13475 return t;
13477 case INTEGER_TYPE:
13478 if (t == integer_type_node)
13479 return t;
13481 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13482 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13483 return t;
13486 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13488 max = tsubst_expr (omax, args, complain, in_decl,
13489 /*integral_constant_expression_p=*/false);
13491 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13492 needed. */
13493 if (TREE_CODE (max) == NOP_EXPR
13494 && TREE_SIDE_EFFECTS (omax)
13495 && !TREE_TYPE (max))
13496 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13498 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13499 with TREE_SIDE_EFFECTS that indicates this is not an integral
13500 constant expression. */
13501 if (processing_template_decl
13502 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13504 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13505 TREE_SIDE_EFFECTS (max) = 1;
13508 return compute_array_index_type (NULL_TREE, max, complain);
13511 case TEMPLATE_TYPE_PARM:
13512 case TEMPLATE_TEMPLATE_PARM:
13513 case BOUND_TEMPLATE_TEMPLATE_PARM:
13514 case TEMPLATE_PARM_INDEX:
13516 int idx;
13517 int level;
13518 int levels;
13519 tree arg = NULL_TREE;
13521 /* Early in template argument deduction substitution, we don't
13522 want to reduce the level of 'auto', or it will be confused
13523 with a normal template parm in subsequent deduction. */
13524 if (is_auto (t) && (complain & tf_partial))
13525 return t;
13527 r = NULL_TREE;
13529 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13530 template_parm_level_and_index (t, &level, &idx);
13532 levels = TMPL_ARGS_DEPTH (args);
13533 if (level <= levels
13534 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13536 arg = TMPL_ARG (args, level, idx);
13538 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13540 /* See through ARGUMENT_PACK_SELECT arguments. */
13541 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13542 /* If the selected argument is an expansion E, that most
13543 likely means we were called from
13544 gen_elem_of_pack_expansion_instantiation during the
13545 substituting of pack an argument pack (which Ith
13546 element is a pack expansion, where I is
13547 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13548 In this case, the Ith element resulting from this
13549 substituting is going to be a pack expansion, which
13550 pattern is the pattern of E. Let's return the
13551 pattern of E, and
13552 gen_elem_of_pack_expansion_instantiation will
13553 build the resulting pack expansion from it. */
13554 if (PACK_EXPANSION_P (arg))
13556 /* Make sure we aren't throwing away arg info. */
13557 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13558 arg = PACK_EXPANSION_PATTERN (arg);
13563 if (arg == error_mark_node)
13564 return error_mark_node;
13565 else if (arg != NULL_TREE)
13567 if (ARGUMENT_PACK_P (arg))
13568 /* If ARG is an argument pack, we don't actually want to
13569 perform a substitution here, because substitutions
13570 for argument packs are only done
13571 element-by-element. We can get to this point when
13572 substituting the type of a non-type template
13573 parameter pack, when that type actually contains
13574 template parameter packs from an outer template, e.g.,
13576 template<typename... Types> struct A {
13577 template<Types... Values> struct B { };
13578 }; */
13579 return t;
13581 if (code == TEMPLATE_TYPE_PARM)
13583 int quals;
13584 gcc_assert (TYPE_P (arg));
13586 quals = cp_type_quals (arg) | cp_type_quals (t);
13588 return cp_build_qualified_type_real
13589 (arg, quals, complain | tf_ignore_bad_quals);
13591 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13593 /* We are processing a type constructed from a
13594 template template parameter. */
13595 tree argvec = tsubst (TYPE_TI_ARGS (t),
13596 args, complain, in_decl);
13597 if (argvec == error_mark_node)
13598 return error_mark_node;
13600 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13601 || TREE_CODE (arg) == TEMPLATE_DECL
13602 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13604 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13605 /* Consider this code:
13607 template <template <class> class Template>
13608 struct Internal {
13609 template <class Arg> using Bind = Template<Arg>;
13612 template <template <class> class Template, class Arg>
13613 using Instantiate = Template<Arg>; //#0
13615 template <template <class> class Template,
13616 class Argument>
13617 using Bind =
13618 Instantiate<Internal<Template>::template Bind,
13619 Argument>; //#1
13621 When #1 is parsed, the
13622 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13623 parameter `Template' in #0 matches the
13624 UNBOUND_CLASS_TEMPLATE representing the argument
13625 `Internal<Template>::template Bind'; We then want
13626 to assemble the type `Bind<Argument>' that can't
13627 be fully created right now, because
13628 `Internal<Template>' not being complete, the Bind
13629 template cannot be looked up in that context. So
13630 we need to "store" `Bind<Argument>' for later
13631 when the context of Bind becomes complete. Let's
13632 store that in a TYPENAME_TYPE. */
13633 return make_typename_type (TYPE_CONTEXT (arg),
13634 build_nt (TEMPLATE_ID_EXPR,
13635 TYPE_IDENTIFIER (arg),
13636 argvec),
13637 typename_type,
13638 complain);
13640 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13641 are resolving nested-types in the signature of a
13642 member function templates. Otherwise ARG is a
13643 TEMPLATE_DECL and is the real template to be
13644 instantiated. */
13645 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13646 arg = TYPE_NAME (arg);
13648 r = lookup_template_class (arg,
13649 argvec, in_decl,
13650 DECL_CONTEXT (arg),
13651 /*entering_scope=*/0,
13652 complain);
13653 return cp_build_qualified_type_real
13654 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13656 else if (code == TEMPLATE_TEMPLATE_PARM)
13657 return arg;
13658 else
13659 /* TEMPLATE_PARM_INDEX. */
13660 return convert_from_reference (unshare_expr (arg));
13663 if (level == 1)
13664 /* This can happen during the attempted tsubst'ing in
13665 unify. This means that we don't yet have any information
13666 about the template parameter in question. */
13667 return t;
13669 /* If we get here, we must have been looking at a parm for a
13670 more deeply nested template. Make a new version of this
13671 template parameter, but with a lower level. */
13672 switch (code)
13674 case TEMPLATE_TYPE_PARM:
13675 case TEMPLATE_TEMPLATE_PARM:
13676 case BOUND_TEMPLATE_TEMPLATE_PARM:
13677 if (cp_type_quals (t))
13679 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13680 r = cp_build_qualified_type_real
13681 (r, cp_type_quals (t),
13682 complain | (code == TEMPLATE_TYPE_PARM
13683 ? tf_ignore_bad_quals : 0));
13685 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13686 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13687 && (r = (TEMPLATE_PARM_DESCENDANTS
13688 (TEMPLATE_TYPE_PARM_INDEX (t))))
13689 && (r = TREE_TYPE (r))
13690 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13691 /* Break infinite recursion when substituting the constraints
13692 of a constrained placeholder. */;
13693 else
13695 r = copy_type (t);
13696 TEMPLATE_TYPE_PARM_INDEX (r)
13697 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13698 r, levels, args, complain);
13699 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13700 TYPE_MAIN_VARIANT (r) = r;
13701 TYPE_POINTER_TO (r) = NULL_TREE;
13702 TYPE_REFERENCE_TO (r) = NULL_TREE;
13704 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13706 /* Propagate constraints on placeholders. */
13707 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13708 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13709 = tsubst_constraint (constr, args, complain, in_decl);
13710 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13712 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13713 pl = tsubst (pl, args, complain, in_decl);
13714 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13718 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13719 /* We have reduced the level of the template
13720 template parameter, but not the levels of its
13721 template parameters, so canonical_type_parameter
13722 will not be able to find the canonical template
13723 template parameter for this level. Thus, we
13724 require structural equality checking to compare
13725 TEMPLATE_TEMPLATE_PARMs. */
13726 SET_TYPE_STRUCTURAL_EQUALITY (r);
13727 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13728 SET_TYPE_STRUCTURAL_EQUALITY (r);
13729 else
13730 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13732 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13734 tree tinfo = TYPE_TEMPLATE_INFO (t);
13735 /* We might need to substitute into the types of non-type
13736 template parameters. */
13737 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13738 complain, in_decl);
13739 if (tmpl == error_mark_node)
13740 return error_mark_node;
13741 tree argvec = tsubst (TI_ARGS (tinfo), args,
13742 complain, in_decl);
13743 if (argvec == error_mark_node)
13744 return error_mark_node;
13746 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13747 = build_template_info (tmpl, argvec);
13750 break;
13752 case TEMPLATE_PARM_INDEX:
13753 /* OK, now substitute the type of the non-type parameter. We
13754 couldn't do it earlier because it might be an auto parameter,
13755 and we wouldn't need to if we had an argument. */
13756 type = tsubst (type, args, complain, in_decl);
13757 r = reduce_template_parm_level (t, type, levels, args, complain);
13758 break;
13760 default:
13761 gcc_unreachable ();
13764 return r;
13767 case TREE_LIST:
13769 tree purpose, value, chain;
13771 if (t == void_list_node)
13772 return t;
13774 purpose = TREE_PURPOSE (t);
13775 if (purpose)
13777 purpose = tsubst (purpose, args, complain, in_decl);
13778 if (purpose == error_mark_node)
13779 return error_mark_node;
13781 value = TREE_VALUE (t);
13782 if (value)
13784 value = tsubst (value, args, complain, in_decl);
13785 if (value == error_mark_node)
13786 return error_mark_node;
13788 chain = TREE_CHAIN (t);
13789 if (chain && chain != void_type_node)
13791 chain = tsubst (chain, args, complain, in_decl);
13792 if (chain == error_mark_node)
13793 return error_mark_node;
13795 if (purpose == TREE_PURPOSE (t)
13796 && value == TREE_VALUE (t)
13797 && chain == TREE_CHAIN (t))
13798 return t;
13799 return hash_tree_cons (purpose, value, chain);
13802 case TREE_BINFO:
13803 /* We should never be tsubsting a binfo. */
13804 gcc_unreachable ();
13806 case TREE_VEC:
13807 /* A vector of template arguments. */
13808 gcc_assert (!type);
13809 return tsubst_template_args (t, args, complain, in_decl);
13811 case POINTER_TYPE:
13812 case REFERENCE_TYPE:
13814 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13815 return t;
13817 /* [temp.deduct]
13819 Type deduction may fail for any of the following
13820 reasons:
13822 -- Attempting to create a pointer to reference type.
13823 -- Attempting to create a reference to a reference type or
13824 a reference to void.
13826 Core issue 106 says that creating a reference to a reference
13827 during instantiation is no longer a cause for failure. We
13828 only enforce this check in strict C++98 mode. */
13829 if ((TREE_CODE (type) == REFERENCE_TYPE
13830 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13831 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13833 static location_t last_loc;
13835 /* We keep track of the last time we issued this error
13836 message to avoid spewing a ton of messages during a
13837 single bad template instantiation. */
13838 if (complain & tf_error
13839 && last_loc != input_location)
13841 if (VOID_TYPE_P (type))
13842 error ("forming reference to void");
13843 else if (code == POINTER_TYPE)
13844 error ("forming pointer to reference type %qT", type);
13845 else
13846 error ("forming reference to reference type %qT", type);
13847 last_loc = input_location;
13850 return error_mark_node;
13852 else if (TREE_CODE (type) == FUNCTION_TYPE
13853 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13854 || type_memfn_rqual (type) != REF_QUAL_NONE))
13856 if (complain & tf_error)
13858 if (code == POINTER_TYPE)
13859 error ("forming pointer to qualified function type %qT",
13860 type);
13861 else
13862 error ("forming reference to qualified function type %qT",
13863 type);
13865 return error_mark_node;
13867 else if (code == POINTER_TYPE)
13869 r = build_pointer_type (type);
13870 if (TREE_CODE (type) == METHOD_TYPE)
13871 r = build_ptrmemfunc_type (r);
13873 else if (TREE_CODE (type) == REFERENCE_TYPE)
13874 /* In C++0x, during template argument substitution, when there is an
13875 attempt to create a reference to a reference type, reference
13876 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13878 "If a template-argument for a template-parameter T names a type
13879 that is a reference to a type A, an attempt to create the type
13880 'lvalue reference to cv T' creates the type 'lvalue reference to
13881 A,' while an attempt to create the type type rvalue reference to
13882 cv T' creates the type T"
13884 r = cp_build_reference_type
13885 (TREE_TYPE (type),
13886 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13887 else
13888 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13889 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13891 if (r != error_mark_node)
13892 /* Will this ever be needed for TYPE_..._TO values? */
13893 layout_type (r);
13895 return r;
13897 case OFFSET_TYPE:
13899 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13900 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13902 /* [temp.deduct]
13904 Type deduction may fail for any of the following
13905 reasons:
13907 -- Attempting to create "pointer to member of T" when T
13908 is not a class type. */
13909 if (complain & tf_error)
13910 error ("creating pointer to member of non-class type %qT", r);
13911 return error_mark_node;
13913 if (TREE_CODE (type) == REFERENCE_TYPE)
13915 if (complain & tf_error)
13916 error ("creating pointer to member reference type %qT", type);
13917 return error_mark_node;
13919 if (VOID_TYPE_P (type))
13921 if (complain & tf_error)
13922 error ("creating pointer to member of type void");
13923 return error_mark_node;
13925 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13926 if (TREE_CODE (type) == FUNCTION_TYPE)
13928 /* The type of the implicit object parameter gets its
13929 cv-qualifiers from the FUNCTION_TYPE. */
13930 tree memptr;
13931 tree method_type
13932 = build_memfn_type (type, r, type_memfn_quals (type),
13933 type_memfn_rqual (type));
13934 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13935 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13936 complain);
13938 else
13939 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13940 cp_type_quals (t),
13941 complain);
13943 case FUNCTION_TYPE:
13944 case METHOD_TYPE:
13946 tree fntype;
13947 tree specs;
13948 fntype = tsubst_function_type (t, args, complain, in_decl);
13949 if (fntype == error_mark_node)
13950 return error_mark_node;
13952 /* Substitute the exception specification. */
13953 specs = tsubst_exception_specification (t, args, complain, in_decl,
13954 /*defer_ok*/fndecl_type);
13955 if (specs == error_mark_node)
13956 return error_mark_node;
13957 if (specs)
13958 fntype = build_exception_variant (fntype, specs);
13959 return fntype;
13961 case ARRAY_TYPE:
13963 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13964 if (domain == error_mark_node)
13965 return error_mark_node;
13967 /* As an optimization, we avoid regenerating the array type if
13968 it will obviously be the same as T. */
13969 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13970 return t;
13972 /* These checks should match the ones in create_array_type_for_decl.
13974 [temp.deduct]
13976 The deduction may fail for any of the following reasons:
13978 -- Attempting to create an array with an element type that
13979 is void, a function type, or a reference type, or [DR337]
13980 an abstract class type. */
13981 if (VOID_TYPE_P (type)
13982 || TREE_CODE (type) == FUNCTION_TYPE
13983 || (TREE_CODE (type) == ARRAY_TYPE
13984 && TYPE_DOMAIN (type) == NULL_TREE)
13985 || TREE_CODE (type) == REFERENCE_TYPE)
13987 if (complain & tf_error)
13988 error ("creating array of %qT", type);
13989 return error_mark_node;
13992 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13993 return error_mark_node;
13995 r = build_cplus_array_type (type, domain);
13997 if (TYPE_USER_ALIGN (t))
13999 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14000 TYPE_USER_ALIGN (r) = 1;
14003 return r;
14006 case TYPENAME_TYPE:
14008 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14009 in_decl, /*entering_scope=*/1);
14010 if (ctx == error_mark_node)
14011 return error_mark_node;
14013 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14014 complain, in_decl);
14015 if (f == error_mark_node)
14016 return error_mark_node;
14018 if (!MAYBE_CLASS_TYPE_P (ctx))
14020 if (complain & tf_error)
14021 error ("%qT is not a class, struct, or union type", ctx);
14022 return error_mark_node;
14024 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14026 /* Normally, make_typename_type does not require that the CTX
14027 have complete type in order to allow things like:
14029 template <class T> struct S { typename S<T>::X Y; };
14031 But, such constructs have already been resolved by this
14032 point, so here CTX really should have complete type, unless
14033 it's a partial instantiation. */
14034 ctx = complete_type (ctx);
14035 if (!COMPLETE_TYPE_P (ctx))
14037 if (complain & tf_error)
14038 cxx_incomplete_type_error (NULL_TREE, ctx);
14039 return error_mark_node;
14043 f = make_typename_type (ctx, f, typename_type,
14044 complain | tf_keep_type_decl);
14045 if (f == error_mark_node)
14046 return f;
14047 if (TREE_CODE (f) == TYPE_DECL)
14049 complain |= tf_ignore_bad_quals;
14050 f = TREE_TYPE (f);
14053 if (TREE_CODE (f) != TYPENAME_TYPE)
14055 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14057 if (complain & tf_error)
14058 error ("%qT resolves to %qT, which is not an enumeration type",
14059 t, f);
14060 else
14061 return error_mark_node;
14063 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14065 if (complain & tf_error)
14066 error ("%qT resolves to %qT, which is is not a class type",
14067 t, f);
14068 else
14069 return error_mark_node;
14073 return cp_build_qualified_type_real
14074 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14077 case UNBOUND_CLASS_TEMPLATE:
14079 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14080 in_decl, /*entering_scope=*/1);
14081 tree name = TYPE_IDENTIFIER (t);
14082 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14084 if (ctx == error_mark_node || name == error_mark_node)
14085 return error_mark_node;
14087 if (parm_list)
14088 parm_list = tsubst_template_parms (parm_list, args, complain);
14089 return make_unbound_class_template (ctx, name, parm_list, complain);
14092 case TYPEOF_TYPE:
14094 tree type;
14096 ++cp_unevaluated_operand;
14097 ++c_inhibit_evaluation_warnings;
14099 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14100 complain, in_decl,
14101 /*integral_constant_expression_p=*/false);
14103 --cp_unevaluated_operand;
14104 --c_inhibit_evaluation_warnings;
14106 type = finish_typeof (type);
14107 return cp_build_qualified_type_real (type,
14108 cp_type_quals (t)
14109 | cp_type_quals (type),
14110 complain);
14113 case DECLTYPE_TYPE:
14115 tree type;
14117 ++cp_unevaluated_operand;
14118 ++c_inhibit_evaluation_warnings;
14120 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14121 complain|tf_decltype, in_decl,
14122 /*function_p*/false,
14123 /*integral_constant_expression*/false);
14125 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14127 if (type == NULL_TREE)
14129 if (complain & tf_error)
14130 error ("empty initializer in lambda init-capture");
14131 type = error_mark_node;
14133 else if (TREE_CODE (type) == TREE_LIST)
14134 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14137 --cp_unevaluated_operand;
14138 --c_inhibit_evaluation_warnings;
14140 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14141 type = lambda_capture_field_type (type,
14142 DECLTYPE_FOR_INIT_CAPTURE (t),
14143 DECLTYPE_FOR_REF_CAPTURE (t));
14144 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14145 type = lambda_proxy_type (type);
14146 else
14148 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14149 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14150 && EXPR_P (type))
14151 /* In a template ~id could be either a complement expression
14152 or an unqualified-id naming a destructor; if instantiating
14153 it produces an expression, it's not an id-expression or
14154 member access. */
14155 id = false;
14156 type = finish_decltype_type (type, id, complain);
14158 return cp_build_qualified_type_real (type,
14159 cp_type_quals (t)
14160 | cp_type_quals (type),
14161 complain | tf_ignore_bad_quals);
14164 case UNDERLYING_TYPE:
14166 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14167 complain, in_decl);
14168 return finish_underlying_type (type);
14171 case TYPE_ARGUMENT_PACK:
14172 case NONTYPE_ARGUMENT_PACK:
14174 tree r;
14176 if (code == NONTYPE_ARGUMENT_PACK)
14177 r = make_node (code);
14178 else
14179 r = cxx_make_type (code);
14181 tree pack_args = ARGUMENT_PACK_ARGS (t);
14182 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14183 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14185 return r;
14188 case VOID_CST:
14189 case INTEGER_CST:
14190 case REAL_CST:
14191 case STRING_CST:
14192 case PLUS_EXPR:
14193 case MINUS_EXPR:
14194 case NEGATE_EXPR:
14195 case NOP_EXPR:
14196 case INDIRECT_REF:
14197 case ADDR_EXPR:
14198 case CALL_EXPR:
14199 case ARRAY_REF:
14200 case SCOPE_REF:
14201 /* We should use one of the expression tsubsts for these codes. */
14202 gcc_unreachable ();
14204 default:
14205 sorry ("use of %qs in template", get_tree_code_name (code));
14206 return error_mark_node;
14210 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14211 expression on the left-hand side of the "." or "->" operator. A
14212 baselink indicates a function from a base class. Both the
14213 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14214 bases of the template class, rather than the instantiated class.
14215 In addition, lookups that were not ambiguous before may be
14216 ambiguous now. Therefore, we perform the lookup again. */
14218 static tree
14219 tsubst_baselink (tree baselink, tree object_type,
14220 tree args, tsubst_flags_t complain, tree in_decl)
14222 bool qualified = BASELINK_QUALIFIED_P (baselink);
14224 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14225 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14227 tree optype = BASELINK_OPTYPE (baselink);
14228 optype = tsubst (optype, args, complain, in_decl);
14230 tree template_args = NULL_TREE;
14231 bool template_id_p = false;
14232 tree fns = BASELINK_FUNCTIONS (baselink);
14233 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14235 template_id_p = true;
14236 template_args = TREE_OPERAND (fns, 1);
14237 fns = TREE_OPERAND (fns, 0);
14238 if (template_args)
14239 template_args = tsubst_template_args (template_args, args,
14240 complain, in_decl);
14243 tree name = OVL_NAME (fns);
14244 if (IDENTIFIER_CONV_OP_P (name))
14245 name = make_conv_op_name (optype);
14247 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14248 if (!baselink)
14250 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14251 error ("cannot call constructor %<%T::%D%> directly",
14252 qualifying_scope, name);
14253 return error_mark_node;
14256 /* If lookup found a single function, mark it as used at this point.
14257 (If it lookup found multiple functions the one selected later by
14258 overload resolution will be marked as used at that point.) */
14259 if (BASELINK_P (baselink))
14260 fns = BASELINK_FUNCTIONS (baselink);
14261 if (!template_id_p && !really_overloaded_fn (fns)
14262 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14263 return error_mark_node;
14265 if (BASELINK_P (baselink))
14267 /* Add back the template arguments, if present. */
14268 if (template_id_p)
14269 BASELINK_FUNCTIONS (baselink)
14270 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14271 BASELINK_FUNCTIONS (baselink), template_args);
14273 /* Update the conversion operator type. */
14274 BASELINK_OPTYPE (baselink) = optype;
14277 if (!object_type)
14278 object_type = current_class_type;
14280 if (qualified || name == complete_dtor_identifier)
14282 baselink = adjust_result_of_qualified_name_lookup (baselink,
14283 qualifying_scope,
14284 object_type);
14285 if (!qualified)
14286 /* We need to call adjust_result_of_qualified_name_lookup in case the
14287 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14288 so that we still get virtual function binding. */
14289 BASELINK_QUALIFIED_P (baselink) = false;
14292 return baselink;
14295 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14296 true if the qualified-id will be a postfix-expression in-and-of
14297 itself; false if more of the postfix-expression follows the
14298 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14299 of "&". */
14301 static tree
14302 tsubst_qualified_id (tree qualified_id, tree args,
14303 tsubst_flags_t complain, tree in_decl,
14304 bool done, bool address_p)
14306 tree expr;
14307 tree scope;
14308 tree name;
14309 bool is_template;
14310 tree template_args;
14311 location_t loc = UNKNOWN_LOCATION;
14313 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14315 /* Figure out what name to look up. */
14316 name = TREE_OPERAND (qualified_id, 1);
14317 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14319 is_template = true;
14320 loc = EXPR_LOCATION (name);
14321 template_args = TREE_OPERAND (name, 1);
14322 if (template_args)
14323 template_args = tsubst_template_args (template_args, args,
14324 complain, in_decl);
14325 if (template_args == error_mark_node)
14326 return error_mark_node;
14327 name = TREE_OPERAND (name, 0);
14329 else
14331 is_template = false;
14332 template_args = NULL_TREE;
14335 /* Substitute into the qualifying scope. When there are no ARGS, we
14336 are just trying to simplify a non-dependent expression. In that
14337 case the qualifying scope may be dependent, and, in any case,
14338 substituting will not help. */
14339 scope = TREE_OPERAND (qualified_id, 0);
14340 if (args)
14342 scope = tsubst (scope, args, complain, in_decl);
14343 expr = tsubst_copy (name, args, complain, in_decl);
14345 else
14346 expr = name;
14348 if (dependent_scope_p (scope))
14350 if (is_template)
14351 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14352 tree r = build_qualified_name (NULL_TREE, scope, expr,
14353 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14354 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14355 return r;
14358 if (!BASELINK_P (name) && !DECL_P (expr))
14360 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14362 /* A BIT_NOT_EXPR is used to represent a destructor. */
14363 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14365 error ("qualifying type %qT does not match destructor name ~%qT",
14366 scope, TREE_OPERAND (expr, 0));
14367 expr = error_mark_node;
14369 else
14370 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14371 /*is_type_p=*/0, false);
14373 else
14374 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14375 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14376 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14378 if (complain & tf_error)
14380 error ("dependent-name %qE is parsed as a non-type, but "
14381 "instantiation yields a type", qualified_id);
14382 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14384 return error_mark_node;
14388 if (DECL_P (expr))
14390 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14391 scope);
14392 /* Remember that there was a reference to this entity. */
14393 if (!mark_used (expr, complain) && !(complain & tf_error))
14394 return error_mark_node;
14397 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14399 if (complain & tf_error)
14400 qualified_name_lookup_error (scope,
14401 TREE_OPERAND (qualified_id, 1),
14402 expr, input_location);
14403 return error_mark_node;
14406 if (is_template)
14408 if (variable_template_p (expr))
14409 expr = lookup_and_finish_template_variable (expr, template_args,
14410 complain);
14411 else
14412 expr = lookup_template_function (expr, template_args);
14415 if (expr == error_mark_node && complain & tf_error)
14416 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14417 expr, input_location);
14418 else if (TYPE_P (scope))
14420 expr = (adjust_result_of_qualified_name_lookup
14421 (expr, scope, current_nonlambda_class_type ()));
14422 expr = (finish_qualified_id_expr
14423 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14424 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14425 /*template_arg_p=*/false, complain));
14428 /* Expressions do not generally have reference type. */
14429 if (TREE_CODE (expr) != SCOPE_REF
14430 /* However, if we're about to form a pointer-to-member, we just
14431 want the referenced member referenced. */
14432 && TREE_CODE (expr) != OFFSET_REF)
14433 expr = convert_from_reference (expr);
14435 if (REF_PARENTHESIZED_P (qualified_id))
14436 expr = force_paren_expr (expr);
14438 return expr;
14441 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14442 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14443 for tsubst. */
14445 static tree
14446 tsubst_init (tree init, tree decl, tree args,
14447 tsubst_flags_t complain, tree in_decl)
14449 if (!init)
14450 return NULL_TREE;
14452 init = tsubst_expr (init, args, complain, in_decl, false);
14454 if (!init && TREE_TYPE (decl) != error_mark_node)
14456 /* If we had an initializer but it
14457 instantiated to nothing,
14458 value-initialize the object. This will
14459 only occur when the initializer was a
14460 pack expansion where the parameter packs
14461 used in that expansion were of length
14462 zero. */
14463 init = build_value_init (TREE_TYPE (decl),
14464 complain);
14465 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14466 init = get_target_expr_sfinae (init, complain);
14467 if (TREE_CODE (init) == TARGET_EXPR)
14468 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14471 return init;
14474 /* Like tsubst, but deals with expressions. This function just replaces
14475 template parms; to finish processing the resultant expression, use
14476 tsubst_copy_and_build or tsubst_expr. */
14478 static tree
14479 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14481 enum tree_code code;
14482 tree r;
14484 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14485 return t;
14487 code = TREE_CODE (t);
14489 switch (code)
14491 case PARM_DECL:
14492 r = retrieve_local_specialization (t);
14494 if (r == NULL_TREE)
14496 /* We get here for a use of 'this' in an NSDMI as part of a
14497 constructor call or as part of an aggregate initialization. */
14498 if (DECL_NAME (t) == this_identifier
14499 && ((current_function_decl
14500 && DECL_CONSTRUCTOR_P (current_function_decl))
14501 || (current_class_ref
14502 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14503 return current_class_ptr;
14505 /* This can happen for a parameter name used later in a function
14506 declaration (such as in a late-specified return type). Just
14507 make a dummy decl, since it's only used for its type. */
14508 gcc_assert (cp_unevaluated_operand != 0);
14509 r = tsubst_decl (t, args, complain);
14510 /* Give it the template pattern as its context; its true context
14511 hasn't been instantiated yet and this is good enough for
14512 mangling. */
14513 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14516 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14517 r = ARGUMENT_PACK_SELECT_ARG (r);
14518 if (!mark_used (r, complain) && !(complain & tf_error))
14519 return error_mark_node;
14520 return r;
14522 case CONST_DECL:
14524 tree enum_type;
14525 tree v;
14527 if (DECL_TEMPLATE_PARM_P (t))
14528 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14529 /* There is no need to substitute into namespace-scope
14530 enumerators. */
14531 if (DECL_NAMESPACE_SCOPE_P (t))
14532 return t;
14533 /* If ARGS is NULL, then T is known to be non-dependent. */
14534 if (args == NULL_TREE)
14535 return scalar_constant_value (t);
14537 /* Unfortunately, we cannot just call lookup_name here.
14538 Consider:
14540 template <int I> int f() {
14541 enum E { a = I };
14542 struct S { void g() { E e = a; } };
14545 When we instantiate f<7>::S::g(), say, lookup_name is not
14546 clever enough to find f<7>::a. */
14547 enum_type
14548 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14549 /*entering_scope=*/0);
14551 for (v = TYPE_VALUES (enum_type);
14552 v != NULL_TREE;
14553 v = TREE_CHAIN (v))
14554 if (TREE_PURPOSE (v) == DECL_NAME (t))
14555 return TREE_VALUE (v);
14557 /* We didn't find the name. That should never happen; if
14558 name-lookup found it during preliminary parsing, we
14559 should find it again here during instantiation. */
14560 gcc_unreachable ();
14562 return t;
14564 case FIELD_DECL:
14565 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14567 /* Check for a local specialization set up by
14568 tsubst_pack_expansion. */
14569 if (tree r = retrieve_local_specialization (t))
14571 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14572 r = ARGUMENT_PACK_SELECT_ARG (r);
14573 return r;
14576 /* When retrieving a capture pack from a generic lambda, remove the
14577 lambda call op's own template argument list from ARGS. Only the
14578 template arguments active for the closure type should be used to
14579 retrieve the pack specialization. */
14580 if (LAMBDA_FUNCTION_P (current_function_decl)
14581 && (template_class_depth (DECL_CONTEXT (t))
14582 != TMPL_ARGS_DEPTH (args)))
14583 args = strip_innermost_template_args (args, 1);
14585 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14586 tsubst_decl put in the hash table. */
14587 return retrieve_specialization (t, args, 0);
14590 if (DECL_CONTEXT (t))
14592 tree ctx;
14594 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14595 /*entering_scope=*/1);
14596 if (ctx != DECL_CONTEXT (t))
14598 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14599 if (!r)
14601 if (complain & tf_error)
14602 error ("using invalid field %qD", t);
14603 return error_mark_node;
14605 return r;
14609 return t;
14611 case VAR_DECL:
14612 case FUNCTION_DECL:
14613 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14614 r = tsubst (t, args, complain, in_decl);
14615 else if (local_variable_p (t)
14616 && uses_template_parms (DECL_CONTEXT (t)))
14618 r = retrieve_local_specialization (t);
14619 if (r == NULL_TREE)
14621 /* First try name lookup to find the instantiation. */
14622 r = lookup_name (DECL_NAME (t));
14623 if (r && !is_capture_proxy (r))
14625 /* Make sure that the one we found is the one we want. */
14626 tree ctx = DECL_CONTEXT (t);
14627 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14628 ctx = tsubst (ctx, args, complain, in_decl);
14629 if (ctx != DECL_CONTEXT (r))
14630 r = NULL_TREE;
14633 if (r)
14634 /* OK */;
14635 else
14637 /* This can happen for a variable used in a
14638 late-specified return type of a local lambda, or for a
14639 local static or constant. Building a new VAR_DECL
14640 should be OK in all those cases. */
14641 r = tsubst_decl (t, args, complain);
14642 if (local_specializations)
14643 /* Avoid infinite recursion (79640). */
14644 register_local_specialization (r, t);
14645 if (decl_maybe_constant_var_p (r))
14647 /* We can't call cp_finish_decl, so handle the
14648 initializer by hand. */
14649 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14650 complain, in_decl);
14651 if (!processing_template_decl)
14652 init = maybe_constant_init (init);
14653 if (processing_template_decl
14654 ? potential_constant_expression (init)
14655 : reduced_constant_expression_p (init))
14656 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14657 = TREE_CONSTANT (r) = true;
14658 DECL_INITIAL (r) = init;
14660 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14661 || decl_constant_var_p (r)
14662 || errorcount || sorrycount);
14663 if (!processing_template_decl
14664 && !TREE_STATIC (r))
14665 r = process_outer_var_ref (r, complain);
14667 /* Remember this for subsequent uses. */
14668 if (local_specializations)
14669 register_local_specialization (r, t);
14672 else
14673 r = t;
14674 if (!mark_used (r, complain))
14675 return error_mark_node;
14676 return r;
14678 case NAMESPACE_DECL:
14679 return t;
14681 case OVERLOAD:
14682 /* An OVERLOAD will always be a non-dependent overload set; an
14683 overload set from function scope will just be represented with an
14684 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14685 gcc_assert (!uses_template_parms (t));
14686 /* We must have marked any lookups as persistent. */
14687 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14688 return t;
14690 case BASELINK:
14691 return tsubst_baselink (t, current_nonlambda_class_type (),
14692 args, complain, in_decl);
14694 case TEMPLATE_DECL:
14695 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14696 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14697 args, complain, in_decl);
14698 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14699 return tsubst (t, args, complain, in_decl);
14700 else if (DECL_CLASS_SCOPE_P (t)
14701 && uses_template_parms (DECL_CONTEXT (t)))
14703 /* Template template argument like the following example need
14704 special treatment:
14706 template <template <class> class TT> struct C {};
14707 template <class T> struct D {
14708 template <class U> struct E {};
14709 C<E> c; // #1
14711 D<int> d; // #2
14713 We are processing the template argument `E' in #1 for
14714 the template instantiation #2. Originally, `E' is a
14715 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14716 have to substitute this with one having context `D<int>'. */
14718 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14719 if (dependent_scope_p (context))
14721 /* When rewriting a constructor into a deduction guide, a
14722 non-dependent name can become dependent, so memtmpl<args>
14723 becomes context::template memtmpl<args>. */
14724 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14725 return build_qualified_name (type, context, DECL_NAME (t),
14726 /*template*/true);
14728 return lookup_field (context, DECL_NAME(t), 0, false);
14730 else
14731 /* Ordinary template template argument. */
14732 return t;
14734 case CAST_EXPR:
14735 case REINTERPRET_CAST_EXPR:
14736 case CONST_CAST_EXPR:
14737 case STATIC_CAST_EXPR:
14738 case DYNAMIC_CAST_EXPR:
14739 case IMPLICIT_CONV_EXPR:
14740 case CONVERT_EXPR:
14741 case NOP_EXPR:
14743 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14744 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14745 return build1 (code, type, op0);
14748 case SIZEOF_EXPR:
14749 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14750 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14752 tree expanded, op = TREE_OPERAND (t, 0);
14753 int len = 0;
14755 if (SIZEOF_EXPR_TYPE_P (t))
14756 op = TREE_TYPE (op);
14758 ++cp_unevaluated_operand;
14759 ++c_inhibit_evaluation_warnings;
14760 /* We only want to compute the number of arguments. */
14761 if (PACK_EXPANSION_P (op))
14762 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14763 else
14764 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14765 args, complain, in_decl);
14766 --cp_unevaluated_operand;
14767 --c_inhibit_evaluation_warnings;
14769 if (TREE_CODE (expanded) == TREE_VEC)
14771 len = TREE_VEC_LENGTH (expanded);
14772 /* Set TREE_USED for the benefit of -Wunused. */
14773 for (int i = 0; i < len; i++)
14774 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14775 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14778 if (expanded == error_mark_node)
14779 return error_mark_node;
14780 else if (PACK_EXPANSION_P (expanded)
14781 || (TREE_CODE (expanded) == TREE_VEC
14782 && pack_expansion_args_count (expanded)))
14785 if (PACK_EXPANSION_P (expanded))
14786 /* OK. */;
14787 else if (TREE_VEC_LENGTH (expanded) == 1)
14788 expanded = TREE_VEC_ELT (expanded, 0);
14789 else
14790 expanded = make_argument_pack (expanded);
14792 if (TYPE_P (expanded))
14793 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14794 complain & tf_error);
14795 else
14796 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14797 complain & tf_error);
14799 else
14800 return build_int_cst (size_type_node, len);
14802 if (SIZEOF_EXPR_TYPE_P (t))
14804 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14805 args, complain, in_decl);
14806 r = build1 (NOP_EXPR, r, error_mark_node);
14807 r = build1 (SIZEOF_EXPR,
14808 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14809 SIZEOF_EXPR_TYPE_P (r) = 1;
14810 return r;
14812 /* Fall through */
14814 case INDIRECT_REF:
14815 case NEGATE_EXPR:
14816 case TRUTH_NOT_EXPR:
14817 case BIT_NOT_EXPR:
14818 case ADDR_EXPR:
14819 case UNARY_PLUS_EXPR: /* Unary + */
14820 case ALIGNOF_EXPR:
14821 case AT_ENCODE_EXPR:
14822 case ARROW_EXPR:
14823 case THROW_EXPR:
14824 case TYPEID_EXPR:
14825 case REALPART_EXPR:
14826 case IMAGPART_EXPR:
14827 case PAREN_EXPR:
14829 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14830 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14831 return build1 (code, type, op0);
14834 case COMPONENT_REF:
14836 tree object;
14837 tree name;
14839 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14840 name = TREE_OPERAND (t, 1);
14841 if (TREE_CODE (name) == BIT_NOT_EXPR)
14843 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14844 complain, in_decl);
14845 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14847 else if (TREE_CODE (name) == SCOPE_REF
14848 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14850 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14851 complain, in_decl);
14852 name = TREE_OPERAND (name, 1);
14853 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14854 complain, in_decl);
14855 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14856 name = build_qualified_name (/*type=*/NULL_TREE,
14857 base, name,
14858 /*template_p=*/false);
14860 else if (BASELINK_P (name))
14861 name = tsubst_baselink (name,
14862 non_reference (TREE_TYPE (object)),
14863 args, complain,
14864 in_decl);
14865 else
14866 name = tsubst_copy (name, args, complain, in_decl);
14867 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14870 case PLUS_EXPR:
14871 case MINUS_EXPR:
14872 case MULT_EXPR:
14873 case TRUNC_DIV_EXPR:
14874 case CEIL_DIV_EXPR:
14875 case FLOOR_DIV_EXPR:
14876 case ROUND_DIV_EXPR:
14877 case EXACT_DIV_EXPR:
14878 case BIT_AND_EXPR:
14879 case BIT_IOR_EXPR:
14880 case BIT_XOR_EXPR:
14881 case TRUNC_MOD_EXPR:
14882 case FLOOR_MOD_EXPR:
14883 case TRUTH_ANDIF_EXPR:
14884 case TRUTH_ORIF_EXPR:
14885 case TRUTH_AND_EXPR:
14886 case TRUTH_OR_EXPR:
14887 case RSHIFT_EXPR:
14888 case LSHIFT_EXPR:
14889 case RROTATE_EXPR:
14890 case LROTATE_EXPR:
14891 case EQ_EXPR:
14892 case NE_EXPR:
14893 case MAX_EXPR:
14894 case MIN_EXPR:
14895 case LE_EXPR:
14896 case GE_EXPR:
14897 case LT_EXPR:
14898 case GT_EXPR:
14899 case COMPOUND_EXPR:
14900 case DOTSTAR_EXPR:
14901 case MEMBER_REF:
14902 case PREDECREMENT_EXPR:
14903 case PREINCREMENT_EXPR:
14904 case POSTDECREMENT_EXPR:
14905 case POSTINCREMENT_EXPR:
14907 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14908 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14909 return build_nt (code, op0, op1);
14912 case SCOPE_REF:
14914 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14915 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14916 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14917 QUALIFIED_NAME_IS_TEMPLATE (t));
14920 case ARRAY_REF:
14922 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14923 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14924 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14927 case CALL_EXPR:
14929 int n = VL_EXP_OPERAND_LENGTH (t);
14930 tree result = build_vl_exp (CALL_EXPR, n);
14931 int i;
14932 for (i = 0; i < n; i++)
14933 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14934 complain, in_decl);
14935 return result;
14938 case COND_EXPR:
14939 case MODOP_EXPR:
14940 case PSEUDO_DTOR_EXPR:
14941 case VEC_PERM_EXPR:
14943 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14944 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14945 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14946 r = build_nt (code, op0, op1, op2);
14947 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14948 return r;
14951 case NEW_EXPR:
14953 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14954 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14955 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14956 r = build_nt (code, op0, op1, op2);
14957 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14958 return r;
14961 case DELETE_EXPR:
14963 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14964 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14965 r = build_nt (code, op0, op1);
14966 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14967 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14968 return r;
14971 case TEMPLATE_ID_EXPR:
14973 /* Substituted template arguments */
14974 tree fn = TREE_OPERAND (t, 0);
14975 tree targs = TREE_OPERAND (t, 1);
14977 fn = tsubst_copy (fn, args, complain, in_decl);
14978 if (targs)
14979 targs = tsubst_template_args (targs, args, complain, in_decl);
14981 return lookup_template_function (fn, targs);
14984 case TREE_LIST:
14986 tree purpose, value, chain;
14988 if (t == void_list_node)
14989 return t;
14991 purpose = TREE_PURPOSE (t);
14992 if (purpose)
14993 purpose = tsubst_copy (purpose, args, complain, in_decl);
14994 value = TREE_VALUE (t);
14995 if (value)
14996 value = tsubst_copy (value, args, complain, in_decl);
14997 chain = TREE_CHAIN (t);
14998 if (chain && chain != void_type_node)
14999 chain = tsubst_copy (chain, args, complain, in_decl);
15000 if (purpose == TREE_PURPOSE (t)
15001 && value == TREE_VALUE (t)
15002 && chain == TREE_CHAIN (t))
15003 return t;
15004 return tree_cons (purpose, value, chain);
15007 case RECORD_TYPE:
15008 case UNION_TYPE:
15009 case ENUMERAL_TYPE:
15010 case INTEGER_TYPE:
15011 case TEMPLATE_TYPE_PARM:
15012 case TEMPLATE_TEMPLATE_PARM:
15013 case BOUND_TEMPLATE_TEMPLATE_PARM:
15014 case TEMPLATE_PARM_INDEX:
15015 case POINTER_TYPE:
15016 case REFERENCE_TYPE:
15017 case OFFSET_TYPE:
15018 case FUNCTION_TYPE:
15019 case METHOD_TYPE:
15020 case ARRAY_TYPE:
15021 case TYPENAME_TYPE:
15022 case UNBOUND_CLASS_TEMPLATE:
15023 case TYPEOF_TYPE:
15024 case DECLTYPE_TYPE:
15025 case TYPE_DECL:
15026 return tsubst (t, args, complain, in_decl);
15028 case USING_DECL:
15029 t = DECL_NAME (t);
15030 /* Fall through. */
15031 case IDENTIFIER_NODE:
15032 if (IDENTIFIER_CONV_OP_P (t))
15034 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15035 return make_conv_op_name (new_type);
15037 else
15038 return t;
15040 case CONSTRUCTOR:
15041 /* This is handled by tsubst_copy_and_build. */
15042 gcc_unreachable ();
15044 case VA_ARG_EXPR:
15046 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15047 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15048 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15051 case CLEANUP_POINT_EXPR:
15052 /* We shouldn't have built any of these during initial template
15053 generation. Instead, they should be built during instantiation
15054 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15055 gcc_unreachable ();
15057 case OFFSET_REF:
15059 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15060 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15061 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15062 r = build2 (code, type, op0, op1);
15063 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15064 if (!mark_used (TREE_OPERAND (r, 1), complain)
15065 && !(complain & tf_error))
15066 return error_mark_node;
15067 return r;
15070 case EXPR_PACK_EXPANSION:
15071 error ("invalid use of pack expansion expression");
15072 return error_mark_node;
15074 case NONTYPE_ARGUMENT_PACK:
15075 error ("use %<...%> to expand argument pack");
15076 return error_mark_node;
15078 case VOID_CST:
15079 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15080 return t;
15082 case INTEGER_CST:
15083 case REAL_CST:
15084 case STRING_CST:
15085 case COMPLEX_CST:
15087 /* Instantiate any typedefs in the type. */
15088 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15089 r = fold_convert (type, t);
15090 gcc_assert (TREE_CODE (r) == code);
15091 return r;
15094 case PTRMEM_CST:
15095 /* These can sometimes show up in a partial instantiation, but never
15096 involve template parms. */
15097 gcc_assert (!uses_template_parms (t));
15098 return t;
15100 case UNARY_LEFT_FOLD_EXPR:
15101 return tsubst_unary_left_fold (t, args, complain, in_decl);
15102 case UNARY_RIGHT_FOLD_EXPR:
15103 return tsubst_unary_right_fold (t, args, complain, in_decl);
15104 case BINARY_LEFT_FOLD_EXPR:
15105 return tsubst_binary_left_fold (t, args, complain, in_decl);
15106 case BINARY_RIGHT_FOLD_EXPR:
15107 return tsubst_binary_right_fold (t, args, complain, in_decl);
15108 case PREDICT_EXPR:
15109 return t;
15111 default:
15112 /* We shouldn't get here, but keep going if !flag_checking. */
15113 if (flag_checking)
15114 gcc_unreachable ();
15115 return t;
15119 /* Helper function for tsubst_omp_clauses, used for instantiation of
15120 OMP_CLAUSE_DECL of clauses. */
15122 static tree
15123 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15124 tree in_decl)
15126 if (decl == NULL_TREE)
15127 return NULL_TREE;
15129 /* Handle an OpenMP array section represented as a TREE_LIST (or
15130 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15131 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15132 TREE_LIST. We can handle it exactly the same as an array section
15133 (purpose, value, and a chain), even though the nomenclature
15134 (low_bound, length, etc) is different. */
15135 if (TREE_CODE (decl) == TREE_LIST)
15137 tree low_bound
15138 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15139 /*integral_constant_expression_p=*/false);
15140 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15141 /*integral_constant_expression_p=*/false);
15142 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15143 in_decl);
15144 if (TREE_PURPOSE (decl) == low_bound
15145 && TREE_VALUE (decl) == length
15146 && TREE_CHAIN (decl) == chain)
15147 return decl;
15148 tree ret = tree_cons (low_bound, length, chain);
15149 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15150 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15151 return ret;
15153 tree ret = tsubst_expr (decl, args, complain, in_decl,
15154 /*integral_constant_expression_p=*/false);
15155 /* Undo convert_from_reference tsubst_expr could have called. */
15156 if (decl
15157 && REFERENCE_REF_P (ret)
15158 && !REFERENCE_REF_P (decl))
15159 ret = TREE_OPERAND (ret, 0);
15160 return ret;
15163 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15165 static tree
15166 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15167 tree args, tsubst_flags_t complain, tree in_decl)
15169 tree new_clauses = NULL_TREE, nc, oc;
15170 tree linear_no_step = NULL_TREE;
15172 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15174 nc = copy_node (oc);
15175 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15176 new_clauses = nc;
15178 switch (OMP_CLAUSE_CODE (nc))
15180 case OMP_CLAUSE_LASTPRIVATE:
15181 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15183 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15184 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15185 in_decl, /*integral_constant_expression_p=*/false);
15186 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15187 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15189 /* FALLTHRU */
15190 case OMP_CLAUSE_PRIVATE:
15191 case OMP_CLAUSE_SHARED:
15192 case OMP_CLAUSE_FIRSTPRIVATE:
15193 case OMP_CLAUSE_COPYIN:
15194 case OMP_CLAUSE_COPYPRIVATE:
15195 case OMP_CLAUSE_UNIFORM:
15196 case OMP_CLAUSE_DEPEND:
15197 case OMP_CLAUSE_FROM:
15198 case OMP_CLAUSE_TO:
15199 case OMP_CLAUSE_MAP:
15200 case OMP_CLAUSE_USE_DEVICE_PTR:
15201 case OMP_CLAUSE_IS_DEVICE_PTR:
15202 OMP_CLAUSE_DECL (nc)
15203 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15204 in_decl);
15205 break;
15206 case OMP_CLAUSE_TILE:
15207 case OMP_CLAUSE_IF:
15208 case OMP_CLAUSE_NUM_THREADS:
15209 case OMP_CLAUSE_SCHEDULE:
15210 case OMP_CLAUSE_COLLAPSE:
15211 case OMP_CLAUSE_FINAL:
15212 case OMP_CLAUSE_DEVICE:
15213 case OMP_CLAUSE_DIST_SCHEDULE:
15214 case OMP_CLAUSE_NUM_TEAMS:
15215 case OMP_CLAUSE_THREAD_LIMIT:
15216 case OMP_CLAUSE_SAFELEN:
15217 case OMP_CLAUSE_SIMDLEN:
15218 case OMP_CLAUSE_NUM_TASKS:
15219 case OMP_CLAUSE_GRAINSIZE:
15220 case OMP_CLAUSE_PRIORITY:
15221 case OMP_CLAUSE_ORDERED:
15222 case OMP_CLAUSE_HINT:
15223 case OMP_CLAUSE_NUM_GANGS:
15224 case OMP_CLAUSE_NUM_WORKERS:
15225 case OMP_CLAUSE_VECTOR_LENGTH:
15226 case OMP_CLAUSE_WORKER:
15227 case OMP_CLAUSE_VECTOR:
15228 case OMP_CLAUSE_ASYNC:
15229 case OMP_CLAUSE_WAIT:
15230 OMP_CLAUSE_OPERAND (nc, 0)
15231 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15232 in_decl, /*integral_constant_expression_p=*/false);
15233 break;
15234 case OMP_CLAUSE_REDUCTION:
15235 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15237 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15238 if (TREE_CODE (placeholder) == SCOPE_REF)
15240 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15241 complain, in_decl);
15242 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15243 = build_qualified_name (NULL_TREE, scope,
15244 TREE_OPERAND (placeholder, 1),
15245 false);
15247 else
15248 gcc_assert (identifier_p (placeholder));
15250 OMP_CLAUSE_DECL (nc)
15251 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15252 in_decl);
15253 break;
15254 case OMP_CLAUSE_GANG:
15255 case OMP_CLAUSE_ALIGNED:
15256 OMP_CLAUSE_DECL (nc)
15257 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15258 in_decl);
15259 OMP_CLAUSE_OPERAND (nc, 1)
15260 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15261 in_decl, /*integral_constant_expression_p=*/false);
15262 break;
15263 case OMP_CLAUSE_LINEAR:
15264 OMP_CLAUSE_DECL (nc)
15265 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15266 in_decl);
15267 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15269 gcc_assert (!linear_no_step);
15270 linear_no_step = nc;
15272 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15273 OMP_CLAUSE_LINEAR_STEP (nc)
15274 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15275 complain, in_decl);
15276 else
15277 OMP_CLAUSE_LINEAR_STEP (nc)
15278 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15279 in_decl,
15280 /*integral_constant_expression_p=*/false);
15281 break;
15282 case OMP_CLAUSE_NOWAIT:
15283 case OMP_CLAUSE_DEFAULT:
15284 case OMP_CLAUSE_UNTIED:
15285 case OMP_CLAUSE_MERGEABLE:
15286 case OMP_CLAUSE_INBRANCH:
15287 case OMP_CLAUSE_NOTINBRANCH:
15288 case OMP_CLAUSE_PROC_BIND:
15289 case OMP_CLAUSE_FOR:
15290 case OMP_CLAUSE_PARALLEL:
15291 case OMP_CLAUSE_SECTIONS:
15292 case OMP_CLAUSE_TASKGROUP:
15293 case OMP_CLAUSE_NOGROUP:
15294 case OMP_CLAUSE_THREADS:
15295 case OMP_CLAUSE_SIMD:
15296 case OMP_CLAUSE_DEFAULTMAP:
15297 case OMP_CLAUSE_INDEPENDENT:
15298 case OMP_CLAUSE_AUTO:
15299 case OMP_CLAUSE_SEQ:
15300 break;
15301 default:
15302 gcc_unreachable ();
15304 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15305 switch (OMP_CLAUSE_CODE (nc))
15307 case OMP_CLAUSE_SHARED:
15308 case OMP_CLAUSE_PRIVATE:
15309 case OMP_CLAUSE_FIRSTPRIVATE:
15310 case OMP_CLAUSE_LASTPRIVATE:
15311 case OMP_CLAUSE_COPYPRIVATE:
15312 case OMP_CLAUSE_LINEAR:
15313 case OMP_CLAUSE_REDUCTION:
15314 case OMP_CLAUSE_USE_DEVICE_PTR:
15315 case OMP_CLAUSE_IS_DEVICE_PTR:
15316 /* tsubst_expr on SCOPE_REF results in returning
15317 finish_non_static_data_member result. Undo that here. */
15318 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15319 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15320 == IDENTIFIER_NODE))
15322 tree t = OMP_CLAUSE_DECL (nc);
15323 tree v = t;
15324 while (v)
15325 switch (TREE_CODE (v))
15327 case COMPONENT_REF:
15328 case MEM_REF:
15329 case INDIRECT_REF:
15330 CASE_CONVERT:
15331 case POINTER_PLUS_EXPR:
15332 v = TREE_OPERAND (v, 0);
15333 continue;
15334 case PARM_DECL:
15335 if (DECL_CONTEXT (v) == current_function_decl
15336 && DECL_ARTIFICIAL (v)
15337 && DECL_NAME (v) == this_identifier)
15338 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15339 /* FALLTHRU */
15340 default:
15341 v = NULL_TREE;
15342 break;
15345 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15346 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15347 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15348 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15349 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15351 tree decl = OMP_CLAUSE_DECL (nc);
15352 if (VAR_P (decl))
15354 retrofit_lang_decl (decl);
15355 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15358 break;
15359 default:
15360 break;
15364 new_clauses = nreverse (new_clauses);
15365 if (ort != C_ORT_OMP_DECLARE_SIMD)
15367 new_clauses = finish_omp_clauses (new_clauses, ort);
15368 if (linear_no_step)
15369 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15370 if (nc == linear_no_step)
15372 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15373 break;
15376 return new_clauses;
15379 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15381 static tree
15382 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15383 tree in_decl)
15385 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15387 tree purpose, value, chain;
15389 if (t == NULL)
15390 return t;
15392 if (TREE_CODE (t) != TREE_LIST)
15393 return tsubst_copy_and_build (t, args, complain, in_decl,
15394 /*function_p=*/false,
15395 /*integral_constant_expression_p=*/false);
15397 if (t == void_list_node)
15398 return t;
15400 purpose = TREE_PURPOSE (t);
15401 if (purpose)
15402 purpose = RECUR (purpose);
15403 value = TREE_VALUE (t);
15404 if (value)
15406 if (TREE_CODE (value) != LABEL_DECL)
15407 value = RECUR (value);
15408 else
15410 value = lookup_label (DECL_NAME (value));
15411 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15412 TREE_USED (value) = 1;
15415 chain = TREE_CHAIN (t);
15416 if (chain && chain != void_type_node)
15417 chain = RECUR (chain);
15418 return tree_cons (purpose, value, chain);
15419 #undef RECUR
15422 /* Used to temporarily communicate the list of #pragma omp parallel
15423 clauses to #pragma omp for instantiation if they are combined
15424 together. */
15426 static tree *omp_parallel_combined_clauses;
15428 /* Substitute one OMP_FOR iterator. */
15430 static void
15431 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15432 tree initv, tree condv, tree incrv, tree *clauses,
15433 tree args, tsubst_flags_t complain, tree in_decl,
15434 bool integral_constant_expression_p)
15436 #define RECUR(NODE) \
15437 tsubst_expr ((NODE), args, complain, in_decl, \
15438 integral_constant_expression_p)
15439 tree decl, init, cond, incr;
15441 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15442 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15444 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15446 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15447 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15450 decl = TREE_OPERAND (init, 0);
15451 init = TREE_OPERAND (init, 1);
15452 tree decl_expr = NULL_TREE;
15453 if (init && TREE_CODE (init) == DECL_EXPR)
15455 /* We need to jump through some hoops to handle declarations in the
15456 init-statement, since we might need to handle auto deduction,
15457 but we need to keep control of initialization. */
15458 decl_expr = init;
15459 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15460 decl = tsubst_decl (decl, args, complain);
15462 else
15464 if (TREE_CODE (decl) == SCOPE_REF)
15466 decl = RECUR (decl);
15467 if (TREE_CODE (decl) == COMPONENT_REF)
15469 tree v = decl;
15470 while (v)
15471 switch (TREE_CODE (v))
15473 case COMPONENT_REF:
15474 case MEM_REF:
15475 case INDIRECT_REF:
15476 CASE_CONVERT:
15477 case POINTER_PLUS_EXPR:
15478 v = TREE_OPERAND (v, 0);
15479 continue;
15480 case PARM_DECL:
15481 if (DECL_CONTEXT (v) == current_function_decl
15482 && DECL_ARTIFICIAL (v)
15483 && DECL_NAME (v) == this_identifier)
15485 decl = TREE_OPERAND (decl, 1);
15486 decl = omp_privatize_field (decl, false);
15488 /* FALLTHRU */
15489 default:
15490 v = NULL_TREE;
15491 break;
15495 else
15496 decl = RECUR (decl);
15498 init = RECUR (init);
15500 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15501 if (auto_node && init)
15502 TREE_TYPE (decl)
15503 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15505 gcc_assert (!type_dependent_expression_p (decl));
15507 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15509 if (decl_expr)
15511 /* Declare the variable, but don't let that initialize it. */
15512 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15513 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15514 RECUR (decl_expr);
15515 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15518 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15519 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15520 if (TREE_CODE (incr) == MODIFY_EXPR)
15522 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15523 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15524 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15525 NOP_EXPR, rhs, complain);
15527 else
15528 incr = RECUR (incr);
15529 TREE_VEC_ELT (declv, i) = decl;
15530 TREE_VEC_ELT (initv, i) = init;
15531 TREE_VEC_ELT (condv, i) = cond;
15532 TREE_VEC_ELT (incrv, i) = incr;
15533 return;
15536 if (decl_expr)
15538 /* Declare and initialize the variable. */
15539 RECUR (decl_expr);
15540 init = NULL_TREE;
15542 else if (init)
15544 tree *pc;
15545 int j;
15546 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15548 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15550 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15551 && OMP_CLAUSE_DECL (*pc) == decl)
15552 break;
15553 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15554 && OMP_CLAUSE_DECL (*pc) == decl)
15556 if (j)
15557 break;
15558 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15559 tree c = *pc;
15560 *pc = OMP_CLAUSE_CHAIN (c);
15561 OMP_CLAUSE_CHAIN (c) = *clauses;
15562 *clauses = c;
15564 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15565 && OMP_CLAUSE_DECL (*pc) == decl)
15567 error ("iteration variable %qD should not be firstprivate",
15568 decl);
15569 *pc = OMP_CLAUSE_CHAIN (*pc);
15571 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15572 && OMP_CLAUSE_DECL (*pc) == decl)
15574 error ("iteration variable %qD should not be reduction",
15575 decl);
15576 *pc = OMP_CLAUSE_CHAIN (*pc);
15578 else
15579 pc = &OMP_CLAUSE_CHAIN (*pc);
15581 if (*pc)
15582 break;
15584 if (*pc == NULL_TREE)
15586 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15587 OMP_CLAUSE_DECL (c) = decl;
15588 c = finish_omp_clauses (c, C_ORT_OMP);
15589 if (c)
15591 OMP_CLAUSE_CHAIN (c) = *clauses;
15592 *clauses = c;
15596 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15597 if (COMPARISON_CLASS_P (cond))
15599 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15600 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15601 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15603 else
15604 cond = RECUR (cond);
15605 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15606 switch (TREE_CODE (incr))
15608 case PREINCREMENT_EXPR:
15609 case PREDECREMENT_EXPR:
15610 case POSTINCREMENT_EXPR:
15611 case POSTDECREMENT_EXPR:
15612 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15613 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15614 break;
15615 case MODIFY_EXPR:
15616 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15617 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15619 tree rhs = TREE_OPERAND (incr, 1);
15620 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15621 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15622 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15623 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15624 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15625 rhs0, rhs1));
15627 else
15628 incr = RECUR (incr);
15629 break;
15630 case MODOP_EXPR:
15631 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15632 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15634 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15635 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15636 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15637 TREE_TYPE (decl), lhs,
15638 RECUR (TREE_OPERAND (incr, 2))));
15640 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15641 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15642 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15644 tree rhs = TREE_OPERAND (incr, 2);
15645 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15646 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15647 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15648 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15649 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15650 rhs0, rhs1));
15652 else
15653 incr = RECUR (incr);
15654 break;
15655 default:
15656 incr = RECUR (incr);
15657 break;
15660 TREE_VEC_ELT (declv, i) = decl;
15661 TREE_VEC_ELT (initv, i) = init;
15662 TREE_VEC_ELT (condv, i) = cond;
15663 TREE_VEC_ELT (incrv, i) = incr;
15664 #undef RECUR
15667 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15668 of OMP_TARGET's body. */
15670 static tree
15671 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15673 *walk_subtrees = 0;
15674 switch (TREE_CODE (*tp))
15676 case OMP_TEAMS:
15677 return *tp;
15678 case BIND_EXPR:
15679 case STATEMENT_LIST:
15680 *walk_subtrees = 1;
15681 break;
15682 default:
15683 break;
15685 return NULL_TREE;
15688 /* Helper function for tsubst_expr. For decomposition declaration
15689 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15690 also the corresponding decls representing the identifiers
15691 of the decomposition declaration. Return DECL if successful
15692 or error_mark_node otherwise, set *FIRST to the first decl
15693 in the list chained through DECL_CHAIN and *CNT to the number
15694 of such decls. */
15696 static tree
15697 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15698 tsubst_flags_t complain, tree in_decl, tree *first,
15699 unsigned int *cnt)
15701 tree decl2, decl3, prev = decl;
15702 *cnt = 0;
15703 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15704 for (decl2 = DECL_CHAIN (pattern_decl);
15705 decl2
15706 && VAR_P (decl2)
15707 && DECL_DECOMPOSITION_P (decl2)
15708 && DECL_NAME (decl2);
15709 decl2 = DECL_CHAIN (decl2))
15711 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15713 gcc_assert (errorcount);
15714 return error_mark_node;
15716 (*cnt)++;
15717 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15718 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15719 tree v = DECL_VALUE_EXPR (decl2);
15720 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15721 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15722 decl3 = tsubst (decl2, args, complain, in_decl);
15723 SET_DECL_VALUE_EXPR (decl2, v);
15724 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15725 if (VAR_P (decl3))
15726 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15727 maybe_push_decl (decl3);
15728 if (error_operand_p (decl3))
15729 decl = error_mark_node;
15730 else if (decl != error_mark_node
15731 && DECL_CHAIN (decl3) != prev)
15733 gcc_assert (errorcount);
15734 decl = error_mark_node;
15736 else
15737 prev = decl3;
15739 *first = prev;
15740 return decl;
15743 /* Like tsubst_copy for expressions, etc. but also does semantic
15744 processing. */
15746 tree
15747 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15748 bool integral_constant_expression_p)
15750 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15751 #define RECUR(NODE) \
15752 tsubst_expr ((NODE), args, complain, in_decl, \
15753 integral_constant_expression_p)
15755 tree stmt, tmp;
15756 tree r;
15757 location_t loc;
15759 if (t == NULL_TREE || t == error_mark_node)
15760 return t;
15762 loc = input_location;
15763 if (EXPR_HAS_LOCATION (t))
15764 input_location = EXPR_LOCATION (t);
15765 if (STATEMENT_CODE_P (TREE_CODE (t)))
15766 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15768 switch (TREE_CODE (t))
15770 case STATEMENT_LIST:
15772 tree_stmt_iterator i;
15773 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15774 RECUR (tsi_stmt (i));
15775 break;
15778 case CTOR_INITIALIZER:
15779 finish_mem_initializers (tsubst_initializer_list
15780 (TREE_OPERAND (t, 0), args));
15781 break;
15783 case RETURN_EXPR:
15784 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15785 break;
15787 case EXPR_STMT:
15788 tmp = RECUR (EXPR_STMT_EXPR (t));
15789 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15790 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15791 else
15792 finish_expr_stmt (tmp);
15793 break;
15795 case USING_STMT:
15796 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15797 /*attribs=*/NULL_TREE);
15798 break;
15800 case DECL_EXPR:
15802 tree decl, pattern_decl;
15803 tree init;
15805 pattern_decl = decl = DECL_EXPR_DECL (t);
15806 if (TREE_CODE (decl) == LABEL_DECL)
15807 finish_label_decl (DECL_NAME (decl));
15808 else if (TREE_CODE (decl) == USING_DECL)
15810 tree scope = USING_DECL_SCOPE (decl);
15811 tree name = DECL_NAME (decl);
15813 scope = tsubst (scope, args, complain, in_decl);
15814 decl = lookup_qualified_name (scope, name,
15815 /*is_type_p=*/false,
15816 /*complain=*/false);
15817 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15818 qualified_name_lookup_error (scope, name, decl, input_location);
15819 else
15820 finish_local_using_decl (decl, scope, name);
15822 else if (DECL_PACK_P (decl))
15824 /* Don't build up decls for a variadic capture proxy, we'll
15825 instantiate the elements directly as needed. */
15826 break;
15828 else
15830 init = DECL_INITIAL (decl);
15831 decl = tsubst (decl, args, complain, in_decl);
15832 if (decl != error_mark_node)
15834 /* By marking the declaration as instantiated, we avoid
15835 trying to instantiate it. Since instantiate_decl can't
15836 handle local variables, and since we've already done
15837 all that needs to be done, that's the right thing to
15838 do. */
15839 if (VAR_P (decl))
15840 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15841 if (VAR_P (decl)
15842 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15843 /* Anonymous aggregates are a special case. */
15844 finish_anon_union (decl);
15845 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15847 DECL_CONTEXT (decl) = current_function_decl;
15848 if (DECL_NAME (decl) == this_identifier)
15850 tree lam = DECL_CONTEXT (current_function_decl);
15851 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15852 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15854 insert_capture_proxy (decl);
15856 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15857 /* We already did a pushtag. */;
15858 else if (TREE_CODE (decl) == FUNCTION_DECL
15859 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15860 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15862 DECL_CONTEXT (decl) = NULL_TREE;
15863 pushdecl (decl);
15864 DECL_CONTEXT (decl) = current_function_decl;
15865 cp_check_omp_declare_reduction (decl);
15867 else
15869 int const_init = false;
15870 maybe_push_decl (decl);
15871 if (VAR_P (decl)
15872 && DECL_PRETTY_FUNCTION_P (decl))
15874 /* For __PRETTY_FUNCTION__ we have to adjust the
15875 initializer. */
15876 const char *const name
15877 = cxx_printable_name (current_function_decl, 2);
15878 init = cp_fname_init (name, &TREE_TYPE (decl));
15880 else
15881 init = tsubst_init (init, decl, args, complain, in_decl);
15883 if (VAR_P (decl))
15884 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15885 (pattern_decl));
15886 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15887 if (VAR_P (decl)
15888 && DECL_DECOMPOSITION_P (decl)
15889 && TREE_TYPE (pattern_decl) != error_mark_node)
15891 unsigned int cnt;
15892 tree first;
15893 decl = tsubst_decomp_names (decl, pattern_decl, args,
15894 complain, in_decl, &first,
15895 &cnt);
15896 if (decl != error_mark_node)
15897 cp_finish_decomp (decl, first, cnt);
15903 break;
15906 case FOR_STMT:
15907 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15908 RECUR (FOR_INIT_STMT (t));
15909 finish_init_stmt (stmt);
15910 tmp = RECUR (FOR_COND (t));
15911 finish_for_cond (tmp, stmt, false);
15912 tmp = RECUR (FOR_EXPR (t));
15913 finish_for_expr (tmp, stmt);
15914 RECUR (FOR_BODY (t));
15915 finish_for_stmt (stmt);
15916 break;
15918 case RANGE_FOR_STMT:
15920 tree decl, expr;
15921 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15922 decl = RANGE_FOR_DECL (t);
15923 decl = tsubst (decl, args, complain, in_decl);
15924 maybe_push_decl (decl);
15925 expr = RECUR (RANGE_FOR_EXPR (t));
15926 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15928 unsigned int cnt;
15929 tree first;
15930 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15931 complain, in_decl, &first, &cnt);
15932 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15933 RANGE_FOR_IVDEP (t));
15935 else
15936 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15937 RANGE_FOR_IVDEP (t));
15938 RECUR (RANGE_FOR_BODY (t));
15939 finish_for_stmt (stmt);
15941 break;
15943 case WHILE_STMT:
15944 stmt = begin_while_stmt ();
15945 tmp = RECUR (WHILE_COND (t));
15946 finish_while_stmt_cond (tmp, stmt, false);
15947 RECUR (WHILE_BODY (t));
15948 finish_while_stmt (stmt);
15949 break;
15951 case DO_STMT:
15952 stmt = begin_do_stmt ();
15953 RECUR (DO_BODY (t));
15954 finish_do_body (stmt);
15955 tmp = RECUR (DO_COND (t));
15956 finish_do_stmt (tmp, stmt, false);
15957 break;
15959 case IF_STMT:
15960 stmt = begin_if_stmt ();
15961 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15962 tmp = RECUR (IF_COND (t));
15963 tmp = finish_if_stmt_cond (tmp, stmt);
15964 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15965 /* Don't instantiate the THEN_CLAUSE. */;
15966 else
15968 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15969 if (inhibit)
15970 ++c_inhibit_evaluation_warnings;
15971 RECUR (THEN_CLAUSE (t));
15972 if (inhibit)
15973 --c_inhibit_evaluation_warnings;
15975 finish_then_clause (stmt);
15977 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15978 /* Don't instantiate the ELSE_CLAUSE. */;
15979 else if (ELSE_CLAUSE (t))
15981 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15982 begin_else_clause (stmt);
15983 if (inhibit)
15984 ++c_inhibit_evaluation_warnings;
15985 RECUR (ELSE_CLAUSE (t));
15986 if (inhibit)
15987 --c_inhibit_evaluation_warnings;
15988 finish_else_clause (stmt);
15991 finish_if_stmt (stmt);
15992 break;
15994 case BIND_EXPR:
15995 if (BIND_EXPR_BODY_BLOCK (t))
15996 stmt = begin_function_body ();
15997 else
15998 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15999 ? BCS_TRY_BLOCK : 0);
16001 RECUR (BIND_EXPR_BODY (t));
16003 if (BIND_EXPR_BODY_BLOCK (t))
16004 finish_function_body (stmt);
16005 else
16006 finish_compound_stmt (stmt);
16007 break;
16009 case BREAK_STMT:
16010 finish_break_stmt ();
16011 break;
16013 case CONTINUE_STMT:
16014 finish_continue_stmt ();
16015 break;
16017 case SWITCH_STMT:
16018 stmt = begin_switch_stmt ();
16019 tmp = RECUR (SWITCH_STMT_COND (t));
16020 finish_switch_cond (tmp, stmt);
16021 RECUR (SWITCH_STMT_BODY (t));
16022 finish_switch_stmt (stmt);
16023 break;
16025 case CASE_LABEL_EXPR:
16027 tree low = RECUR (CASE_LOW (t));
16028 tree high = RECUR (CASE_HIGH (t));
16029 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16030 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16031 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16032 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16034 break;
16036 case LABEL_EXPR:
16038 tree decl = LABEL_EXPR_LABEL (t);
16039 tree label;
16041 label = finish_label_stmt (DECL_NAME (decl));
16042 if (TREE_CODE (label) == LABEL_DECL)
16043 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16044 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16045 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16047 break;
16049 case GOTO_EXPR:
16050 tmp = GOTO_DESTINATION (t);
16051 if (TREE_CODE (tmp) != LABEL_DECL)
16052 /* Computed goto's must be tsubst'd into. On the other hand,
16053 non-computed gotos must not be; the identifier in question
16054 will have no binding. */
16055 tmp = RECUR (tmp);
16056 else
16057 tmp = DECL_NAME (tmp);
16058 finish_goto_stmt (tmp);
16059 break;
16061 case ASM_EXPR:
16063 tree string = RECUR (ASM_STRING (t));
16064 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16065 complain, in_decl);
16066 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16067 complain, in_decl);
16068 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16069 complain, in_decl);
16070 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16071 complain, in_decl);
16072 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16073 clobbers, labels);
16074 tree asm_expr = tmp;
16075 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16076 asm_expr = TREE_OPERAND (asm_expr, 0);
16077 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16079 break;
16081 case TRY_BLOCK:
16082 if (CLEANUP_P (t))
16084 stmt = begin_try_block ();
16085 RECUR (TRY_STMTS (t));
16086 finish_cleanup_try_block (stmt);
16087 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16089 else
16091 tree compound_stmt = NULL_TREE;
16093 if (FN_TRY_BLOCK_P (t))
16094 stmt = begin_function_try_block (&compound_stmt);
16095 else
16096 stmt = begin_try_block ();
16098 RECUR (TRY_STMTS (t));
16100 if (FN_TRY_BLOCK_P (t))
16101 finish_function_try_block (stmt);
16102 else
16103 finish_try_block (stmt);
16105 RECUR (TRY_HANDLERS (t));
16106 if (FN_TRY_BLOCK_P (t))
16107 finish_function_handler_sequence (stmt, compound_stmt);
16108 else
16109 finish_handler_sequence (stmt);
16111 break;
16113 case HANDLER:
16115 tree decl = HANDLER_PARMS (t);
16117 if (decl)
16119 decl = tsubst (decl, args, complain, in_decl);
16120 /* Prevent instantiate_decl from trying to instantiate
16121 this variable. We've already done all that needs to be
16122 done. */
16123 if (decl != error_mark_node)
16124 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16126 stmt = begin_handler ();
16127 finish_handler_parms (decl, stmt);
16128 RECUR (HANDLER_BODY (t));
16129 finish_handler (stmt);
16131 break;
16133 case TAG_DEFN:
16134 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16135 if (CLASS_TYPE_P (tmp))
16137 /* Local classes are not independent templates; they are
16138 instantiated along with their containing function. And this
16139 way we don't have to deal with pushing out of one local class
16140 to instantiate a member of another local class. */
16141 /* Closures are handled by the LAMBDA_EXPR. */
16142 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16143 complete_type (tmp);
16144 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16145 if ((VAR_P (fld)
16146 || (TREE_CODE (fld) == FUNCTION_DECL
16147 && !DECL_ARTIFICIAL (fld)))
16148 && DECL_TEMPLATE_INSTANTIATION (fld))
16149 instantiate_decl (fld, /*defer_ok=*/false,
16150 /*expl_inst_class=*/false);
16152 break;
16154 case STATIC_ASSERT:
16156 tree condition;
16158 ++c_inhibit_evaluation_warnings;
16159 condition =
16160 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16161 args,
16162 complain, in_decl,
16163 /*integral_constant_expression_p=*/true);
16164 --c_inhibit_evaluation_warnings;
16166 finish_static_assert (condition,
16167 STATIC_ASSERT_MESSAGE (t),
16168 STATIC_ASSERT_SOURCE_LOCATION (t),
16169 /*member_p=*/false);
16171 break;
16173 case OACC_KERNELS:
16174 case OACC_PARALLEL:
16175 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16176 in_decl);
16177 stmt = begin_omp_parallel ();
16178 RECUR (OMP_BODY (t));
16179 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16180 break;
16182 case OMP_PARALLEL:
16183 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16184 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16185 complain, in_decl);
16186 if (OMP_PARALLEL_COMBINED (t))
16187 omp_parallel_combined_clauses = &tmp;
16188 stmt = begin_omp_parallel ();
16189 RECUR (OMP_PARALLEL_BODY (t));
16190 gcc_assert (omp_parallel_combined_clauses == NULL);
16191 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16192 = OMP_PARALLEL_COMBINED (t);
16193 pop_omp_privatization_clauses (r);
16194 break;
16196 case OMP_TASK:
16197 r = push_omp_privatization_clauses (false);
16198 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16199 complain, in_decl);
16200 stmt = begin_omp_task ();
16201 RECUR (OMP_TASK_BODY (t));
16202 finish_omp_task (tmp, stmt);
16203 pop_omp_privatization_clauses (r);
16204 break;
16206 case OMP_FOR:
16207 case OMP_SIMD:
16208 case CILK_SIMD:
16209 case CILK_FOR:
16210 case OMP_DISTRIBUTE:
16211 case OMP_TASKLOOP:
16212 case OACC_LOOP:
16214 tree clauses, body, pre_body;
16215 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16216 tree orig_declv = NULL_TREE;
16217 tree incrv = NULL_TREE;
16218 enum c_omp_region_type ort = C_ORT_OMP;
16219 int i;
16221 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16222 ort = C_ORT_CILK;
16223 else if (TREE_CODE (t) == OACC_LOOP)
16224 ort = C_ORT_ACC;
16226 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16227 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16228 in_decl);
16229 if (OMP_FOR_INIT (t) != NULL_TREE)
16231 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16232 if (OMP_FOR_ORIG_DECLS (t))
16233 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16234 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16235 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16236 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16239 stmt = begin_omp_structured_block ();
16241 pre_body = push_stmt_list ();
16242 RECUR (OMP_FOR_PRE_BODY (t));
16243 pre_body = pop_stmt_list (pre_body);
16245 if (OMP_FOR_INIT (t) != NULL_TREE)
16246 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16247 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16248 incrv, &clauses, args, complain, in_decl,
16249 integral_constant_expression_p);
16250 omp_parallel_combined_clauses = NULL;
16252 body = push_stmt_list ();
16253 RECUR (OMP_FOR_BODY (t));
16254 body = pop_stmt_list (body);
16256 if (OMP_FOR_INIT (t) != NULL_TREE)
16257 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16258 orig_declv, initv, condv, incrv, body, pre_body,
16259 NULL, clauses);
16260 else
16262 t = make_node (TREE_CODE (t));
16263 TREE_TYPE (t) = void_type_node;
16264 OMP_FOR_BODY (t) = body;
16265 OMP_FOR_PRE_BODY (t) = pre_body;
16266 OMP_FOR_CLAUSES (t) = clauses;
16267 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16268 add_stmt (t);
16271 add_stmt (finish_omp_structured_block (stmt));
16272 pop_omp_privatization_clauses (r);
16274 break;
16276 case OMP_SECTIONS:
16277 omp_parallel_combined_clauses = NULL;
16278 /* FALLTHRU */
16279 case OMP_SINGLE:
16280 case OMP_TEAMS:
16281 case OMP_CRITICAL:
16282 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16283 && OMP_TEAMS_COMBINED (t));
16284 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16285 in_decl);
16286 stmt = push_stmt_list ();
16287 RECUR (OMP_BODY (t));
16288 stmt = pop_stmt_list (stmt);
16290 t = copy_node (t);
16291 OMP_BODY (t) = stmt;
16292 OMP_CLAUSES (t) = tmp;
16293 add_stmt (t);
16294 pop_omp_privatization_clauses (r);
16295 break;
16297 case OACC_DATA:
16298 case OMP_TARGET_DATA:
16299 case OMP_TARGET:
16300 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16301 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16302 in_decl);
16303 keep_next_level (true);
16304 stmt = begin_omp_structured_block ();
16306 RECUR (OMP_BODY (t));
16307 stmt = finish_omp_structured_block (stmt);
16309 t = copy_node (t);
16310 OMP_BODY (t) = stmt;
16311 OMP_CLAUSES (t) = tmp;
16312 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16314 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16315 if (teams)
16317 /* For combined target teams, ensure the num_teams and
16318 thread_limit clause expressions are evaluated on the host,
16319 before entering the target construct. */
16320 tree c;
16321 for (c = OMP_TEAMS_CLAUSES (teams);
16322 c; c = OMP_CLAUSE_CHAIN (c))
16323 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16325 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16327 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16328 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16329 if (expr == error_mark_node)
16330 continue;
16331 tmp = TARGET_EXPR_SLOT (expr);
16332 add_stmt (expr);
16333 OMP_CLAUSE_OPERAND (c, 0) = expr;
16334 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16335 OMP_CLAUSE_FIRSTPRIVATE);
16336 OMP_CLAUSE_DECL (tc) = tmp;
16337 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16338 OMP_TARGET_CLAUSES (t) = tc;
16342 add_stmt (t);
16343 break;
16345 case OACC_DECLARE:
16346 t = copy_node (t);
16347 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16348 complain, in_decl);
16349 OACC_DECLARE_CLAUSES (t) = tmp;
16350 add_stmt (t);
16351 break;
16353 case OMP_TARGET_UPDATE:
16354 case OMP_TARGET_ENTER_DATA:
16355 case OMP_TARGET_EXIT_DATA:
16356 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16357 complain, in_decl);
16358 t = copy_node (t);
16359 OMP_STANDALONE_CLAUSES (t) = tmp;
16360 add_stmt (t);
16361 break;
16363 case OACC_ENTER_DATA:
16364 case OACC_EXIT_DATA:
16365 case OACC_UPDATE:
16366 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16367 complain, in_decl);
16368 t = copy_node (t);
16369 OMP_STANDALONE_CLAUSES (t) = tmp;
16370 add_stmt (t);
16371 break;
16373 case OMP_ORDERED:
16374 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16375 complain, in_decl);
16376 stmt = push_stmt_list ();
16377 RECUR (OMP_BODY (t));
16378 stmt = pop_stmt_list (stmt);
16380 t = copy_node (t);
16381 OMP_BODY (t) = stmt;
16382 OMP_ORDERED_CLAUSES (t) = tmp;
16383 add_stmt (t);
16384 break;
16386 case OMP_SECTION:
16387 case OMP_MASTER:
16388 case OMP_TASKGROUP:
16389 stmt = push_stmt_list ();
16390 RECUR (OMP_BODY (t));
16391 stmt = pop_stmt_list (stmt);
16393 t = copy_node (t);
16394 OMP_BODY (t) = stmt;
16395 add_stmt (t);
16396 break;
16398 case OMP_ATOMIC:
16399 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16400 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16402 tree op1 = TREE_OPERAND (t, 1);
16403 tree rhs1 = NULL_TREE;
16404 tree lhs, rhs;
16405 if (TREE_CODE (op1) == COMPOUND_EXPR)
16407 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16408 op1 = TREE_OPERAND (op1, 1);
16410 lhs = RECUR (TREE_OPERAND (op1, 0));
16411 rhs = RECUR (TREE_OPERAND (op1, 1));
16412 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16413 NULL_TREE, NULL_TREE, rhs1,
16414 OMP_ATOMIC_SEQ_CST (t));
16416 else
16418 tree op1 = TREE_OPERAND (t, 1);
16419 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16420 tree rhs1 = NULL_TREE;
16421 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16422 enum tree_code opcode = NOP_EXPR;
16423 if (code == OMP_ATOMIC_READ)
16425 v = RECUR (TREE_OPERAND (op1, 0));
16426 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16428 else if (code == OMP_ATOMIC_CAPTURE_OLD
16429 || code == OMP_ATOMIC_CAPTURE_NEW)
16431 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16432 v = RECUR (TREE_OPERAND (op1, 0));
16433 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16434 if (TREE_CODE (op11) == COMPOUND_EXPR)
16436 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16437 op11 = TREE_OPERAND (op11, 1);
16439 lhs = RECUR (TREE_OPERAND (op11, 0));
16440 rhs = RECUR (TREE_OPERAND (op11, 1));
16441 opcode = TREE_CODE (op11);
16442 if (opcode == MODIFY_EXPR)
16443 opcode = NOP_EXPR;
16445 else
16447 code = OMP_ATOMIC;
16448 lhs = RECUR (TREE_OPERAND (op1, 0));
16449 rhs = RECUR (TREE_OPERAND (op1, 1));
16451 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16452 OMP_ATOMIC_SEQ_CST (t));
16454 break;
16456 case TRANSACTION_EXPR:
16458 int flags = 0;
16459 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16460 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16462 if (TRANSACTION_EXPR_IS_STMT (t))
16464 tree body = TRANSACTION_EXPR_BODY (t);
16465 tree noex = NULL_TREE;
16466 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16468 noex = MUST_NOT_THROW_COND (body);
16469 if (noex == NULL_TREE)
16470 noex = boolean_true_node;
16471 body = TREE_OPERAND (body, 0);
16473 stmt = begin_transaction_stmt (input_location, NULL, flags);
16474 RECUR (body);
16475 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16477 else
16479 stmt = build_transaction_expr (EXPR_LOCATION (t),
16480 RECUR (TRANSACTION_EXPR_BODY (t)),
16481 flags, NULL_TREE);
16482 RETURN (stmt);
16485 break;
16487 case MUST_NOT_THROW_EXPR:
16489 tree op0 = RECUR (TREE_OPERAND (t, 0));
16490 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16491 RETURN (build_must_not_throw_expr (op0, cond));
16494 case EXPR_PACK_EXPANSION:
16495 error ("invalid use of pack expansion expression");
16496 RETURN (error_mark_node);
16498 case NONTYPE_ARGUMENT_PACK:
16499 error ("use %<...%> to expand argument pack");
16500 RETURN (error_mark_node);
16502 case CILK_SPAWN_STMT:
16503 cfun->calls_cilk_spawn = 1;
16504 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16506 case CILK_SYNC_STMT:
16507 RETURN (build_cilk_sync ());
16509 case COMPOUND_EXPR:
16510 tmp = RECUR (TREE_OPERAND (t, 0));
16511 if (tmp == NULL_TREE)
16512 /* If the first operand was a statement, we're done with it. */
16513 RETURN (RECUR (TREE_OPERAND (t, 1)));
16514 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16515 RECUR (TREE_OPERAND (t, 1)),
16516 complain));
16518 case ANNOTATE_EXPR:
16519 tmp = RECUR (TREE_OPERAND (t, 0));
16520 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16521 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16523 default:
16524 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16526 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16527 /*function_p=*/false,
16528 integral_constant_expression_p));
16531 RETURN (NULL_TREE);
16532 out:
16533 input_location = loc;
16534 return r;
16535 #undef RECUR
16536 #undef RETURN
16539 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16540 function. For description of the body see comment above
16541 cp_parser_omp_declare_reduction_exprs. */
16543 static void
16544 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16546 if (t == NULL_TREE || t == error_mark_node)
16547 return;
16549 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16551 tree_stmt_iterator tsi;
16552 int i;
16553 tree stmts[7];
16554 memset (stmts, 0, sizeof stmts);
16555 for (i = 0, tsi = tsi_start (t);
16556 i < 7 && !tsi_end_p (tsi);
16557 i++, tsi_next (&tsi))
16558 stmts[i] = tsi_stmt (tsi);
16559 gcc_assert (tsi_end_p (tsi));
16561 if (i >= 3)
16563 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16564 && TREE_CODE (stmts[1]) == DECL_EXPR);
16565 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16566 args, complain, in_decl);
16567 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16568 args, complain, in_decl);
16569 DECL_CONTEXT (omp_out) = current_function_decl;
16570 DECL_CONTEXT (omp_in) = current_function_decl;
16571 keep_next_level (true);
16572 tree block = begin_omp_structured_block ();
16573 tsubst_expr (stmts[2], args, complain, in_decl, false);
16574 block = finish_omp_structured_block (block);
16575 block = maybe_cleanup_point_expr_void (block);
16576 add_decl_expr (omp_out);
16577 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16578 TREE_NO_WARNING (omp_out) = 1;
16579 add_decl_expr (omp_in);
16580 finish_expr_stmt (block);
16582 if (i >= 6)
16584 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16585 && TREE_CODE (stmts[4]) == DECL_EXPR);
16586 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16587 args, complain, in_decl);
16588 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16589 args, complain, in_decl);
16590 DECL_CONTEXT (omp_priv) = current_function_decl;
16591 DECL_CONTEXT (omp_orig) = current_function_decl;
16592 keep_next_level (true);
16593 tree block = begin_omp_structured_block ();
16594 tsubst_expr (stmts[5], args, complain, in_decl, false);
16595 block = finish_omp_structured_block (block);
16596 block = maybe_cleanup_point_expr_void (block);
16597 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16598 add_decl_expr (omp_priv);
16599 add_decl_expr (omp_orig);
16600 finish_expr_stmt (block);
16601 if (i == 7)
16602 add_decl_expr (omp_orig);
16606 /* T is a postfix-expression that is not being used in a function
16607 call. Return the substituted version of T. */
16609 static tree
16610 tsubst_non_call_postfix_expression (tree t, tree args,
16611 tsubst_flags_t complain,
16612 tree in_decl)
16614 if (TREE_CODE (t) == SCOPE_REF)
16615 t = tsubst_qualified_id (t, args, complain, in_decl,
16616 /*done=*/false, /*address_p=*/false);
16617 else
16618 t = tsubst_copy_and_build (t, args, complain, in_decl,
16619 /*function_p=*/false,
16620 /*integral_constant_expression_p=*/false);
16622 return t;
16625 /* Like tsubst but deals with expressions and performs semantic
16626 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16628 tree
16629 tsubst_copy_and_build (tree t,
16630 tree args,
16631 tsubst_flags_t complain,
16632 tree in_decl,
16633 bool function_p,
16634 bool integral_constant_expression_p)
16636 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16637 #define RECUR(NODE) \
16638 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16639 /*function_p=*/false, \
16640 integral_constant_expression_p)
16642 tree retval, op1;
16643 location_t loc;
16645 if (t == NULL_TREE || t == error_mark_node)
16646 return t;
16648 loc = input_location;
16649 if (EXPR_HAS_LOCATION (t))
16650 input_location = EXPR_LOCATION (t);
16652 /* N3276 decltype magic only applies to calls at the top level or on the
16653 right side of a comma. */
16654 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16655 complain &= ~tf_decltype;
16657 switch (TREE_CODE (t))
16659 case USING_DECL:
16660 t = DECL_NAME (t);
16661 /* Fall through. */
16662 case IDENTIFIER_NODE:
16664 tree decl;
16665 cp_id_kind idk;
16666 bool non_integral_constant_expression_p;
16667 const char *error_msg;
16669 if (IDENTIFIER_CONV_OP_P (t))
16671 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16672 t = make_conv_op_name (new_type);
16675 /* Look up the name. */
16676 decl = lookup_name (t);
16678 /* By convention, expressions use ERROR_MARK_NODE to indicate
16679 failure, not NULL_TREE. */
16680 if (decl == NULL_TREE)
16681 decl = error_mark_node;
16683 decl = finish_id_expression (t, decl, NULL_TREE,
16684 &idk,
16685 integral_constant_expression_p,
16686 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16687 &non_integral_constant_expression_p,
16688 /*template_p=*/false,
16689 /*done=*/true,
16690 /*address_p=*/false,
16691 /*template_arg_p=*/false,
16692 &error_msg,
16693 input_location);
16694 if (error_msg)
16695 error (error_msg);
16696 if (!function_p && identifier_p (decl))
16698 if (complain & tf_error)
16699 unqualified_name_lookup_error (decl);
16700 decl = error_mark_node;
16702 RETURN (decl);
16705 case TEMPLATE_ID_EXPR:
16707 tree object;
16708 tree templ = RECUR (TREE_OPERAND (t, 0));
16709 tree targs = TREE_OPERAND (t, 1);
16711 if (targs)
16712 targs = tsubst_template_args (targs, args, complain, in_decl);
16713 if (targs == error_mark_node)
16714 return error_mark_node;
16716 if (TREE_CODE (templ) == SCOPE_REF)
16718 tree name = TREE_OPERAND (templ, 1);
16719 tree tid = lookup_template_function (name, targs);
16720 TREE_OPERAND (templ, 1) = tid;
16721 return templ;
16724 if (variable_template_p (templ))
16725 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16727 if (TREE_CODE (templ) == COMPONENT_REF)
16729 object = TREE_OPERAND (templ, 0);
16730 templ = TREE_OPERAND (templ, 1);
16732 else
16733 object = NULL_TREE;
16734 templ = lookup_template_function (templ, targs);
16736 if (object)
16737 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16738 object, templ, NULL_TREE));
16739 else
16740 RETURN (baselink_for_fns (templ));
16743 case INDIRECT_REF:
16745 tree r = RECUR (TREE_OPERAND (t, 0));
16747 if (REFERENCE_REF_P (t))
16749 /* A type conversion to reference type will be enclosed in
16750 such an indirect ref, but the substitution of the cast
16751 will have also added such an indirect ref. */
16752 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16753 r = convert_from_reference (r);
16755 else
16756 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16757 complain|decltype_flag);
16759 if (TREE_CODE (r) == INDIRECT_REF)
16760 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16762 RETURN (r);
16765 case NOP_EXPR:
16767 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16768 tree op0 = RECUR (TREE_OPERAND (t, 0));
16769 RETURN (build_nop (type, op0));
16772 case IMPLICIT_CONV_EXPR:
16774 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16775 tree expr = RECUR (TREE_OPERAND (t, 0));
16776 int flags = LOOKUP_IMPLICIT;
16777 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16778 flags = LOOKUP_NORMAL;
16779 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16780 flags));
16783 case CONVERT_EXPR:
16785 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16786 tree op0 = RECUR (TREE_OPERAND (t, 0));
16787 RETURN (build1 (CONVERT_EXPR, type, op0));
16790 case CAST_EXPR:
16791 case REINTERPRET_CAST_EXPR:
16792 case CONST_CAST_EXPR:
16793 case DYNAMIC_CAST_EXPR:
16794 case STATIC_CAST_EXPR:
16796 tree type;
16797 tree op, r = NULL_TREE;
16799 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16800 if (integral_constant_expression_p
16801 && !cast_valid_in_integral_constant_expression_p (type))
16803 if (complain & tf_error)
16804 error ("a cast to a type other than an integral or "
16805 "enumeration type cannot appear in a constant-expression");
16806 RETURN (error_mark_node);
16809 op = RECUR (TREE_OPERAND (t, 0));
16811 warning_sentinel s(warn_useless_cast);
16812 switch (TREE_CODE (t))
16814 case CAST_EXPR:
16815 r = build_functional_cast (type, op, complain);
16816 break;
16817 case REINTERPRET_CAST_EXPR:
16818 r = build_reinterpret_cast (type, op, complain);
16819 break;
16820 case CONST_CAST_EXPR:
16821 r = build_const_cast (type, op, complain);
16822 break;
16823 case DYNAMIC_CAST_EXPR:
16824 r = build_dynamic_cast (type, op, complain);
16825 break;
16826 case STATIC_CAST_EXPR:
16827 r = build_static_cast (type, op, complain);
16828 break;
16829 default:
16830 gcc_unreachable ();
16833 RETURN (r);
16836 case POSTDECREMENT_EXPR:
16837 case POSTINCREMENT_EXPR:
16838 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16839 args, complain, in_decl);
16840 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16841 complain|decltype_flag));
16843 case PREDECREMENT_EXPR:
16844 case PREINCREMENT_EXPR:
16845 case NEGATE_EXPR:
16846 case BIT_NOT_EXPR:
16847 case ABS_EXPR:
16848 case TRUTH_NOT_EXPR:
16849 case UNARY_PLUS_EXPR: /* Unary + */
16850 case REALPART_EXPR:
16851 case IMAGPART_EXPR:
16852 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16853 RECUR (TREE_OPERAND (t, 0)),
16854 complain|decltype_flag));
16856 case FIX_TRUNC_EXPR:
16857 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16858 false, complain));
16860 case ADDR_EXPR:
16861 op1 = TREE_OPERAND (t, 0);
16862 if (TREE_CODE (op1) == LABEL_DECL)
16863 RETURN (finish_label_address_expr (DECL_NAME (op1),
16864 EXPR_LOCATION (op1)));
16865 if (TREE_CODE (op1) == SCOPE_REF)
16866 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16867 /*done=*/true, /*address_p=*/true);
16868 else
16869 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16870 in_decl);
16871 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16872 complain|decltype_flag));
16874 case PLUS_EXPR:
16875 case MINUS_EXPR:
16876 case MULT_EXPR:
16877 case TRUNC_DIV_EXPR:
16878 case CEIL_DIV_EXPR:
16879 case FLOOR_DIV_EXPR:
16880 case ROUND_DIV_EXPR:
16881 case EXACT_DIV_EXPR:
16882 case BIT_AND_EXPR:
16883 case BIT_IOR_EXPR:
16884 case BIT_XOR_EXPR:
16885 case TRUNC_MOD_EXPR:
16886 case FLOOR_MOD_EXPR:
16887 case TRUTH_ANDIF_EXPR:
16888 case TRUTH_ORIF_EXPR:
16889 case TRUTH_AND_EXPR:
16890 case TRUTH_OR_EXPR:
16891 case RSHIFT_EXPR:
16892 case LSHIFT_EXPR:
16893 case RROTATE_EXPR:
16894 case LROTATE_EXPR:
16895 case EQ_EXPR:
16896 case NE_EXPR:
16897 case MAX_EXPR:
16898 case MIN_EXPR:
16899 case LE_EXPR:
16900 case GE_EXPR:
16901 case LT_EXPR:
16902 case GT_EXPR:
16903 case MEMBER_REF:
16904 case DOTSTAR_EXPR:
16906 warning_sentinel s1(warn_type_limits);
16907 warning_sentinel s2(warn_div_by_zero);
16908 warning_sentinel s3(warn_logical_op);
16909 warning_sentinel s4(warn_tautological_compare);
16910 tree op0 = RECUR (TREE_OPERAND (t, 0));
16911 tree op1 = RECUR (TREE_OPERAND (t, 1));
16912 tree r = build_x_binary_op
16913 (input_location, TREE_CODE (t),
16914 op0,
16915 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16916 ? ERROR_MARK
16917 : TREE_CODE (TREE_OPERAND (t, 0))),
16918 op1,
16919 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16920 ? ERROR_MARK
16921 : TREE_CODE (TREE_OPERAND (t, 1))),
16922 /*overload=*/NULL,
16923 complain|decltype_flag);
16924 if (EXPR_P (r) && TREE_NO_WARNING (t))
16925 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16927 RETURN (r);
16930 case POINTER_PLUS_EXPR:
16932 tree op0 = RECUR (TREE_OPERAND (t, 0));
16933 tree op1 = RECUR (TREE_OPERAND (t, 1));
16934 return fold_build_pointer_plus (op0, op1);
16937 case SCOPE_REF:
16938 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16939 /*address_p=*/false));
16940 case ARRAY_REF:
16941 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16942 args, complain, in_decl);
16943 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16944 RECUR (TREE_OPERAND (t, 1)),
16945 complain|decltype_flag));
16947 case ARRAY_NOTATION_REF:
16949 tree start_index, length, stride;
16950 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16951 args, complain, in_decl);
16952 start_index = RECUR (ARRAY_NOTATION_START (t));
16953 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16954 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16955 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16956 length, stride, TREE_TYPE (op1)));
16958 case SIZEOF_EXPR:
16959 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16960 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16961 RETURN (tsubst_copy (t, args, complain, in_decl));
16962 /* Fall through */
16964 case ALIGNOF_EXPR:
16966 tree r;
16968 op1 = TREE_OPERAND (t, 0);
16969 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16970 op1 = TREE_TYPE (op1);
16971 if (!args)
16973 /* When there are no ARGS, we are trying to evaluate a
16974 non-dependent expression from the parser. Trying to do
16975 the substitutions may not work. */
16976 if (!TYPE_P (op1))
16977 op1 = TREE_TYPE (op1);
16979 else
16981 ++cp_unevaluated_operand;
16982 ++c_inhibit_evaluation_warnings;
16983 if (TYPE_P (op1))
16984 op1 = tsubst (op1, args, complain, in_decl);
16985 else
16986 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16987 /*function_p=*/false,
16988 /*integral_constant_expression_p=*/
16989 false);
16990 --cp_unevaluated_operand;
16991 --c_inhibit_evaluation_warnings;
16993 if (TYPE_P (op1))
16994 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16995 complain & tf_error);
16996 else
16997 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16998 complain & tf_error);
16999 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17001 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17003 if (!processing_template_decl && TYPE_P (op1))
17005 r = build_min (SIZEOF_EXPR, size_type_node,
17006 build1 (NOP_EXPR, op1, error_mark_node));
17007 SIZEOF_EXPR_TYPE_P (r) = 1;
17009 else
17010 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17011 TREE_SIDE_EFFECTS (r) = 0;
17012 TREE_READONLY (r) = 1;
17014 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17016 RETURN (r);
17019 case AT_ENCODE_EXPR:
17021 op1 = TREE_OPERAND (t, 0);
17022 ++cp_unevaluated_operand;
17023 ++c_inhibit_evaluation_warnings;
17024 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17025 /*function_p=*/false,
17026 /*integral_constant_expression_p=*/false);
17027 --cp_unevaluated_operand;
17028 --c_inhibit_evaluation_warnings;
17029 RETURN (objc_build_encode_expr (op1));
17032 case NOEXCEPT_EXPR:
17033 op1 = TREE_OPERAND (t, 0);
17034 ++cp_unevaluated_operand;
17035 ++c_inhibit_evaluation_warnings;
17036 ++cp_noexcept_operand;
17037 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17038 /*function_p=*/false,
17039 /*integral_constant_expression_p=*/false);
17040 --cp_unevaluated_operand;
17041 --c_inhibit_evaluation_warnings;
17042 --cp_noexcept_operand;
17043 RETURN (finish_noexcept_expr (op1, complain));
17045 case MODOP_EXPR:
17047 warning_sentinel s(warn_div_by_zero);
17048 tree lhs = RECUR (TREE_OPERAND (t, 0));
17049 tree rhs = RECUR (TREE_OPERAND (t, 2));
17050 tree r = build_x_modify_expr
17051 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17052 complain|decltype_flag);
17053 /* TREE_NO_WARNING must be set if either the expression was
17054 parenthesized or it uses an operator such as >>= rather
17055 than plain assignment. In the former case, it was already
17056 set and must be copied. In the latter case,
17057 build_x_modify_expr sets it and it must not be reset
17058 here. */
17059 if (TREE_NO_WARNING (t))
17060 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17062 RETURN (r);
17065 case ARROW_EXPR:
17066 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17067 args, complain, in_decl);
17068 /* Remember that there was a reference to this entity. */
17069 if (DECL_P (op1)
17070 && !mark_used (op1, complain) && !(complain & tf_error))
17071 RETURN (error_mark_node);
17072 RETURN (build_x_arrow (input_location, op1, complain));
17074 case NEW_EXPR:
17076 tree placement = RECUR (TREE_OPERAND (t, 0));
17077 tree init = RECUR (TREE_OPERAND (t, 3));
17078 vec<tree, va_gc> *placement_vec;
17079 vec<tree, va_gc> *init_vec;
17080 tree ret;
17082 if (placement == NULL_TREE)
17083 placement_vec = NULL;
17084 else
17086 placement_vec = make_tree_vector ();
17087 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17088 vec_safe_push (placement_vec, TREE_VALUE (placement));
17091 /* If there was an initializer in the original tree, but it
17092 instantiated to an empty list, then we should pass a
17093 non-NULL empty vector to tell build_new that it was an
17094 empty initializer() rather than no initializer. This can
17095 only happen when the initializer is a pack expansion whose
17096 parameter packs are of length zero. */
17097 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17098 init_vec = NULL;
17099 else
17101 init_vec = make_tree_vector ();
17102 if (init == void_node)
17103 gcc_assert (init_vec != NULL);
17104 else
17106 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17107 vec_safe_push (init_vec, TREE_VALUE (init));
17111 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17112 tree op2 = RECUR (TREE_OPERAND (t, 2));
17113 ret = build_new (&placement_vec, op1, op2, &init_vec,
17114 NEW_EXPR_USE_GLOBAL (t),
17115 complain);
17117 if (placement_vec != NULL)
17118 release_tree_vector (placement_vec);
17119 if (init_vec != NULL)
17120 release_tree_vector (init_vec);
17122 RETURN (ret);
17125 case DELETE_EXPR:
17127 tree op0 = RECUR (TREE_OPERAND (t, 0));
17128 tree op1 = RECUR (TREE_OPERAND (t, 1));
17129 RETURN (delete_sanity (op0, op1,
17130 DELETE_EXPR_USE_VEC (t),
17131 DELETE_EXPR_USE_GLOBAL (t),
17132 complain));
17135 case COMPOUND_EXPR:
17137 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17138 complain & ~tf_decltype, in_decl,
17139 /*function_p=*/false,
17140 integral_constant_expression_p);
17141 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17142 op0,
17143 RECUR (TREE_OPERAND (t, 1)),
17144 complain|decltype_flag));
17147 case CALL_EXPR:
17149 tree function;
17150 vec<tree, va_gc> *call_args;
17151 unsigned int nargs, i;
17152 bool qualified_p;
17153 bool koenig_p;
17154 tree ret;
17156 function = CALL_EXPR_FN (t);
17157 /* Internal function with no arguments. */
17158 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17159 RETURN (t);
17161 /* When we parsed the expression, we determined whether or
17162 not Koenig lookup should be performed. */
17163 koenig_p = KOENIG_LOOKUP_P (t);
17164 if (function == NULL_TREE)
17166 koenig_p = false;
17167 qualified_p = false;
17169 else if (TREE_CODE (function) == SCOPE_REF)
17171 qualified_p = true;
17172 function = tsubst_qualified_id (function, args, complain, in_decl,
17173 /*done=*/false,
17174 /*address_p=*/false);
17176 else if (koenig_p && identifier_p (function))
17178 /* Do nothing; calling tsubst_copy_and_build on an identifier
17179 would incorrectly perform unqualified lookup again.
17181 Note that we can also have an IDENTIFIER_NODE if the earlier
17182 unqualified lookup found a member function; in that case
17183 koenig_p will be false and we do want to do the lookup
17184 again to find the instantiated member function.
17186 FIXME but doing that causes c++/15272, so we need to stop
17187 using IDENTIFIER_NODE in that situation. */
17188 qualified_p = false;
17190 else
17192 if (TREE_CODE (function) == COMPONENT_REF)
17194 tree op = TREE_OPERAND (function, 1);
17196 qualified_p = (TREE_CODE (op) == SCOPE_REF
17197 || (BASELINK_P (op)
17198 && BASELINK_QUALIFIED_P (op)));
17200 else
17201 qualified_p = false;
17203 if (TREE_CODE (function) == ADDR_EXPR
17204 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17205 /* Avoid error about taking the address of a constructor. */
17206 function = TREE_OPERAND (function, 0);
17208 function = tsubst_copy_and_build (function, args, complain,
17209 in_decl,
17210 !qualified_p,
17211 integral_constant_expression_p);
17213 if (BASELINK_P (function))
17214 qualified_p = true;
17217 nargs = call_expr_nargs (t);
17218 call_args = make_tree_vector ();
17219 for (i = 0; i < nargs; ++i)
17221 tree arg = CALL_EXPR_ARG (t, i);
17223 if (!PACK_EXPANSION_P (arg))
17224 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17225 else
17227 /* Expand the pack expansion and push each entry onto
17228 CALL_ARGS. */
17229 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17230 if (TREE_CODE (arg) == TREE_VEC)
17232 unsigned int len, j;
17234 len = TREE_VEC_LENGTH (arg);
17235 for (j = 0; j < len; ++j)
17237 tree value = TREE_VEC_ELT (arg, j);
17238 if (value != NULL_TREE)
17239 value = convert_from_reference (value);
17240 vec_safe_push (call_args, value);
17243 else
17245 /* A partial substitution. Add one entry. */
17246 vec_safe_push (call_args, arg);
17251 /* We do not perform argument-dependent lookup if normal
17252 lookup finds a non-function, in accordance with the
17253 expected resolution of DR 218. */
17254 if (koenig_p
17255 && ((is_overloaded_fn (function)
17256 /* If lookup found a member function, the Koenig lookup is
17257 not appropriate, even if an unqualified-name was used
17258 to denote the function. */
17259 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17260 || identifier_p (function))
17261 /* Only do this when substitution turns a dependent call
17262 into a non-dependent call. */
17263 && type_dependent_expression_p_push (t)
17264 && !any_type_dependent_arguments_p (call_args))
17265 function = perform_koenig_lookup (function, call_args, tf_none);
17267 if (function != NULL_TREE
17268 && identifier_p (function)
17269 && !any_type_dependent_arguments_p (call_args))
17271 if (koenig_p && (complain & tf_warning_or_error))
17273 /* For backwards compatibility and good diagnostics, try
17274 the unqualified lookup again if we aren't in SFINAE
17275 context. */
17276 tree unq = (tsubst_copy_and_build
17277 (function, args, complain, in_decl, true,
17278 integral_constant_expression_p));
17279 if (unq == error_mark_node)
17281 release_tree_vector (call_args);
17282 RETURN (error_mark_node);
17285 if (unq != function)
17287 /* In a lambda fn, we have to be careful to not
17288 introduce new this captures. Legacy code can't
17289 be using lambdas anyway, so it's ok to be
17290 stricter. */
17291 bool in_lambda = (current_class_type
17292 && LAMBDA_TYPE_P (current_class_type));
17293 char const *const msg
17294 = G_("%qD was not declared in this scope, "
17295 "and no declarations were found by "
17296 "argument-dependent lookup at the point "
17297 "of instantiation");
17299 bool diag = true;
17300 if (in_lambda)
17301 error_at (EXPR_LOC_OR_LOC (t, input_location),
17302 msg, function);
17303 else
17304 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17305 msg, function);
17306 if (diag)
17308 tree fn = unq;
17310 if (INDIRECT_REF_P (fn))
17311 fn = TREE_OPERAND (fn, 0);
17312 if (is_overloaded_fn (fn))
17313 fn = get_first_fn (fn);
17315 if (!DECL_P (fn))
17316 /* Can't say anything more. */;
17317 else if (DECL_CLASS_SCOPE_P (fn))
17319 location_t loc = EXPR_LOC_OR_LOC (t,
17320 input_location);
17321 inform (loc,
17322 "declarations in dependent base %qT are "
17323 "not found by unqualified lookup",
17324 DECL_CLASS_CONTEXT (fn));
17325 if (current_class_ptr)
17326 inform (loc,
17327 "use %<this->%D%> instead", function);
17328 else
17329 inform (loc,
17330 "use %<%T::%D%> instead",
17331 current_class_name, function);
17333 else
17334 inform (DECL_SOURCE_LOCATION (fn),
17335 "%qD declared here, later in the "
17336 "translation unit", fn);
17337 if (in_lambda)
17339 release_tree_vector (call_args);
17340 RETURN (error_mark_node);
17344 function = unq;
17347 if (identifier_p (function))
17349 if (complain & tf_error)
17350 unqualified_name_lookup_error (function);
17351 release_tree_vector (call_args);
17352 RETURN (error_mark_node);
17356 /* Remember that there was a reference to this entity. */
17357 if (function != NULL_TREE
17358 && DECL_P (function)
17359 && !mark_used (function, complain) && !(complain & tf_error))
17361 release_tree_vector (call_args);
17362 RETURN (error_mark_node);
17365 /* Put back tf_decltype for the actual call. */
17366 complain |= decltype_flag;
17368 if (function == NULL_TREE)
17369 switch (CALL_EXPR_IFN (t))
17371 case IFN_LAUNDER:
17372 gcc_assert (nargs == 1);
17373 if (vec_safe_length (call_args) != 1)
17375 error_at (EXPR_LOC_OR_LOC (t, input_location),
17376 "wrong number of arguments to "
17377 "%<__builtin_launder%>");
17378 ret = error_mark_node;
17380 else
17381 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17382 input_location),
17383 (*call_args)[0], complain);
17384 break;
17386 default:
17387 /* Unsupported internal function with arguments. */
17388 gcc_unreachable ();
17390 else if (TREE_CODE (function) == OFFSET_REF)
17391 ret = build_offset_ref_call_from_tree (function, &call_args,
17392 complain);
17393 else if (TREE_CODE (function) == COMPONENT_REF)
17395 tree instance = TREE_OPERAND (function, 0);
17396 tree fn = TREE_OPERAND (function, 1);
17398 if (processing_template_decl
17399 && (type_dependent_expression_p (instance)
17400 || (!BASELINK_P (fn)
17401 && TREE_CODE (fn) != FIELD_DECL)
17402 || type_dependent_expression_p (fn)
17403 || any_type_dependent_arguments_p (call_args)))
17404 ret = build_min_nt_call_vec (function, call_args);
17405 else if (!BASELINK_P (fn))
17406 ret = finish_call_expr (function, &call_args,
17407 /*disallow_virtual=*/false,
17408 /*koenig_p=*/false,
17409 complain);
17410 else
17411 ret = (build_new_method_call
17412 (instance, fn,
17413 &call_args, NULL_TREE,
17414 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17415 /*fn_p=*/NULL,
17416 complain));
17418 else
17419 ret = finish_call_expr (function, &call_args,
17420 /*disallow_virtual=*/qualified_p,
17421 koenig_p,
17422 complain);
17424 release_tree_vector (call_args);
17426 if (ret != error_mark_node)
17428 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17429 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17430 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17431 bool thk = CALL_FROM_THUNK_P (t);
17432 if (op || ord || rev || thk)
17434 function = extract_call_expr (ret);
17435 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17436 CALL_EXPR_ORDERED_ARGS (function) = ord;
17437 CALL_EXPR_REVERSE_ARGS (function) = rev;
17438 if (thk)
17440 CALL_FROM_THUNK_P (function) = true;
17441 /* The thunk location is not interesting. */
17442 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17447 RETURN (ret);
17450 case COND_EXPR:
17452 tree cond = RECUR (TREE_OPERAND (t, 0));
17453 tree folded_cond = fold_non_dependent_expr (cond);
17454 tree exp1, exp2;
17456 if (TREE_CODE (folded_cond) == INTEGER_CST)
17458 if (integer_zerop (folded_cond))
17460 ++c_inhibit_evaluation_warnings;
17461 exp1 = RECUR (TREE_OPERAND (t, 1));
17462 --c_inhibit_evaluation_warnings;
17463 exp2 = RECUR (TREE_OPERAND (t, 2));
17465 else
17467 exp1 = RECUR (TREE_OPERAND (t, 1));
17468 ++c_inhibit_evaluation_warnings;
17469 exp2 = RECUR (TREE_OPERAND (t, 2));
17470 --c_inhibit_evaluation_warnings;
17472 cond = folded_cond;
17474 else
17476 exp1 = RECUR (TREE_OPERAND (t, 1));
17477 exp2 = RECUR (TREE_OPERAND (t, 2));
17480 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17481 cond, exp1, exp2, complain));
17484 case PSEUDO_DTOR_EXPR:
17486 tree op0 = RECUR (TREE_OPERAND (t, 0));
17487 tree op1 = RECUR (TREE_OPERAND (t, 1));
17488 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17489 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17490 input_location));
17493 case TREE_LIST:
17495 tree purpose, value, chain;
17497 if (t == void_list_node)
17498 RETURN (t);
17500 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17501 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17503 /* We have pack expansions, so expand those and
17504 create a new list out of it. */
17505 tree purposevec = NULL_TREE;
17506 tree valuevec = NULL_TREE;
17507 tree chain;
17508 int i, len = -1;
17510 /* Expand the argument expressions. */
17511 if (TREE_PURPOSE (t))
17512 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17513 complain, in_decl);
17514 if (TREE_VALUE (t))
17515 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17516 complain, in_decl);
17518 /* Build the rest of the list. */
17519 chain = TREE_CHAIN (t);
17520 if (chain && chain != void_type_node)
17521 chain = RECUR (chain);
17523 /* Determine the number of arguments. */
17524 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17526 len = TREE_VEC_LENGTH (purposevec);
17527 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17529 else if (TREE_CODE (valuevec) == TREE_VEC)
17530 len = TREE_VEC_LENGTH (valuevec);
17531 else
17533 /* Since we only performed a partial substitution into
17534 the argument pack, we only RETURN (a single list
17535 node. */
17536 if (purposevec == TREE_PURPOSE (t)
17537 && valuevec == TREE_VALUE (t)
17538 && chain == TREE_CHAIN (t))
17539 RETURN (t);
17541 RETURN (tree_cons (purposevec, valuevec, chain));
17544 /* Convert the argument vectors into a TREE_LIST */
17545 i = len;
17546 while (i > 0)
17548 /* Grab the Ith values. */
17549 i--;
17550 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17551 : NULL_TREE;
17552 value
17553 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17554 : NULL_TREE;
17556 /* Build the list (backwards). */
17557 chain = tree_cons (purpose, value, chain);
17560 RETURN (chain);
17563 purpose = TREE_PURPOSE (t);
17564 if (purpose)
17565 purpose = RECUR (purpose);
17566 value = TREE_VALUE (t);
17567 if (value)
17568 value = RECUR (value);
17569 chain = TREE_CHAIN (t);
17570 if (chain && chain != void_type_node)
17571 chain = RECUR (chain);
17572 if (purpose == TREE_PURPOSE (t)
17573 && value == TREE_VALUE (t)
17574 && chain == TREE_CHAIN (t))
17575 RETURN (t);
17576 RETURN (tree_cons (purpose, value, chain));
17579 case COMPONENT_REF:
17581 tree object;
17582 tree object_type;
17583 tree member;
17584 tree r;
17586 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17587 args, complain, in_decl);
17588 /* Remember that there was a reference to this entity. */
17589 if (DECL_P (object)
17590 && !mark_used (object, complain) && !(complain & tf_error))
17591 RETURN (error_mark_node);
17592 object_type = TREE_TYPE (object);
17594 member = TREE_OPERAND (t, 1);
17595 if (BASELINK_P (member))
17596 member = tsubst_baselink (member,
17597 non_reference (TREE_TYPE (object)),
17598 args, complain, in_decl);
17599 else
17600 member = tsubst_copy (member, args, complain, in_decl);
17601 if (member == error_mark_node)
17602 RETURN (error_mark_node);
17604 if (TREE_CODE (member) == FIELD_DECL)
17606 r = finish_non_static_data_member (member, object, NULL_TREE);
17607 if (TREE_CODE (r) == COMPONENT_REF)
17608 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17609 RETURN (r);
17611 else if (type_dependent_expression_p (object))
17612 /* We can't do much here. */;
17613 else if (!CLASS_TYPE_P (object_type))
17615 if (scalarish_type_p (object_type))
17617 tree s = NULL_TREE;
17618 tree dtor = member;
17620 if (TREE_CODE (dtor) == SCOPE_REF)
17622 s = TREE_OPERAND (dtor, 0);
17623 dtor = TREE_OPERAND (dtor, 1);
17625 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17627 dtor = TREE_OPERAND (dtor, 0);
17628 if (TYPE_P (dtor))
17629 RETURN (finish_pseudo_destructor_expr
17630 (object, s, dtor, input_location));
17634 else if (TREE_CODE (member) == SCOPE_REF
17635 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17637 /* Lookup the template functions now that we know what the
17638 scope is. */
17639 tree scope = TREE_OPERAND (member, 0);
17640 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17641 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17642 member = lookup_qualified_name (scope, tmpl,
17643 /*is_type_p=*/false,
17644 /*complain=*/false);
17645 if (BASELINK_P (member))
17647 BASELINK_FUNCTIONS (member)
17648 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17649 args);
17650 member = (adjust_result_of_qualified_name_lookup
17651 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17652 object_type));
17654 else
17656 qualified_name_lookup_error (scope, tmpl, member,
17657 input_location);
17658 RETURN (error_mark_node);
17661 else if (TREE_CODE (member) == SCOPE_REF
17662 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17663 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17665 if (complain & tf_error)
17667 if (TYPE_P (TREE_OPERAND (member, 0)))
17668 error ("%qT is not a class or namespace",
17669 TREE_OPERAND (member, 0));
17670 else
17671 error ("%qD is not a class or namespace",
17672 TREE_OPERAND (member, 0));
17674 RETURN (error_mark_node);
17677 r = finish_class_member_access_expr (object, member,
17678 /*template_p=*/false,
17679 complain);
17680 if (TREE_CODE (r) == COMPONENT_REF)
17681 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17682 RETURN (r);
17685 case THROW_EXPR:
17686 RETURN (build_throw
17687 (RECUR (TREE_OPERAND (t, 0))));
17689 case CONSTRUCTOR:
17691 vec<constructor_elt, va_gc> *n;
17692 constructor_elt *ce;
17693 unsigned HOST_WIDE_INT idx;
17694 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17695 bool process_index_p;
17696 int newlen;
17697 bool need_copy_p = false;
17698 tree r;
17700 if (type == error_mark_node)
17701 RETURN (error_mark_node);
17703 /* digest_init will do the wrong thing if we let it. */
17704 if (type && TYPE_PTRMEMFUNC_P (type))
17705 RETURN (t);
17707 /* We do not want to process the index of aggregate
17708 initializers as they are identifier nodes which will be
17709 looked up by digest_init. */
17710 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17712 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17713 newlen = vec_safe_length (n);
17714 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17716 if (ce->index && process_index_p
17717 /* An identifier index is looked up in the type
17718 being initialized, not the current scope. */
17719 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17720 ce->index = RECUR (ce->index);
17722 if (PACK_EXPANSION_P (ce->value))
17724 /* Substitute into the pack expansion. */
17725 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17726 in_decl);
17728 if (ce->value == error_mark_node
17729 || PACK_EXPANSION_P (ce->value))
17731 else if (TREE_VEC_LENGTH (ce->value) == 1)
17732 /* Just move the argument into place. */
17733 ce->value = TREE_VEC_ELT (ce->value, 0);
17734 else
17736 /* Update the length of the final CONSTRUCTOR
17737 arguments vector, and note that we will need to
17738 copy.*/
17739 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17740 need_copy_p = true;
17743 else
17744 ce->value = RECUR (ce->value);
17747 if (need_copy_p)
17749 vec<constructor_elt, va_gc> *old_n = n;
17751 vec_alloc (n, newlen);
17752 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17754 if (TREE_CODE (ce->value) == TREE_VEC)
17756 int i, len = TREE_VEC_LENGTH (ce->value);
17757 for (i = 0; i < len; ++i)
17758 CONSTRUCTOR_APPEND_ELT (n, 0,
17759 TREE_VEC_ELT (ce->value, i));
17761 else
17762 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17766 r = build_constructor (init_list_type_node, n);
17767 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17769 if (TREE_HAS_CONSTRUCTOR (t))
17771 fcl_t cl = fcl_functional;
17772 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17773 cl = fcl_c99;
17774 RETURN (finish_compound_literal (type, r, complain, cl));
17777 TREE_TYPE (r) = type;
17778 RETURN (r);
17781 case TYPEID_EXPR:
17783 tree operand_0 = TREE_OPERAND (t, 0);
17784 if (TYPE_P (operand_0))
17786 operand_0 = tsubst (operand_0, args, complain, in_decl);
17787 RETURN (get_typeid (operand_0, complain));
17789 else
17791 operand_0 = RECUR (operand_0);
17792 RETURN (build_typeid (operand_0, complain));
17796 case VAR_DECL:
17797 if (!args)
17798 RETURN (t);
17799 else if (DECL_PACK_P (t))
17801 /* We don't build decls for an instantiation of a
17802 variadic capture proxy, we instantiate the elements
17803 when needed. */
17804 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17805 return RECUR (DECL_VALUE_EXPR (t));
17807 /* Fall through */
17809 case PARM_DECL:
17811 tree r = tsubst_copy (t, args, complain, in_decl);
17812 /* ??? We're doing a subset of finish_id_expression here. */
17813 if (VAR_P (r)
17814 && !processing_template_decl
17815 && !cp_unevaluated_operand
17816 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17817 && CP_DECL_THREAD_LOCAL_P (r))
17819 if (tree wrap = get_tls_wrapper_fn (r))
17820 /* Replace an evaluated use of the thread_local variable with
17821 a call to its wrapper. */
17822 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17824 else if (outer_automatic_var_p (r))
17826 r = process_outer_var_ref (r, complain);
17827 if (is_capture_proxy (r))
17828 register_local_specialization (r, t);
17831 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17832 /* If the original type was a reference, we'll be wrapped in
17833 the appropriate INDIRECT_REF. */
17834 r = convert_from_reference (r);
17835 RETURN (r);
17838 case VA_ARG_EXPR:
17840 tree op0 = RECUR (TREE_OPERAND (t, 0));
17841 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17842 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17845 case OFFSETOF_EXPR:
17847 tree object_ptr
17848 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17849 in_decl, /*function_p=*/false,
17850 /*integral_constant_expression_p=*/false);
17851 RETURN (finish_offsetof (object_ptr,
17852 RECUR (TREE_OPERAND (t, 0)),
17853 EXPR_LOCATION (t)));
17856 case ADDRESSOF_EXPR:
17857 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17858 RECUR (TREE_OPERAND (t, 0)), complain));
17860 case TRAIT_EXPR:
17862 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17863 complain, in_decl);
17865 tree type2 = TRAIT_EXPR_TYPE2 (t);
17866 if (type2 && TREE_CODE (type2) == TREE_LIST)
17867 type2 = RECUR (type2);
17868 else if (type2)
17869 type2 = tsubst (type2, args, complain, in_decl);
17871 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17874 case STMT_EXPR:
17876 tree old_stmt_expr = cur_stmt_expr;
17877 tree stmt_expr = begin_stmt_expr ();
17879 cur_stmt_expr = stmt_expr;
17880 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17881 integral_constant_expression_p);
17882 stmt_expr = finish_stmt_expr (stmt_expr, false);
17883 cur_stmt_expr = old_stmt_expr;
17885 /* If the resulting list of expression statement is empty,
17886 fold it further into void_node. */
17887 if (empty_expr_stmt_p (stmt_expr))
17888 stmt_expr = void_node;
17890 RETURN (stmt_expr);
17893 case LAMBDA_EXPR:
17895 tree r = build_lambda_expr ();
17897 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17898 LAMBDA_EXPR_CLOSURE (r) = type;
17899 CLASSTYPE_LAMBDA_EXPR (type) = r;
17901 LAMBDA_EXPR_LOCATION (r)
17902 = LAMBDA_EXPR_LOCATION (t);
17903 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17904 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17905 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17906 LAMBDA_EXPR_DISCRIMINATOR (r)
17907 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17908 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17909 if (!scope)
17910 /* No substitution needed. */;
17911 else if (VAR_OR_FUNCTION_DECL_P (scope))
17912 /* For a function or variable scope, we want to use tsubst so that we
17913 don't complain about referring to an auto before deduction. */
17914 scope = tsubst (scope, args, complain, in_decl);
17915 else if (TREE_CODE (scope) == PARM_DECL)
17917 /* Look up the parameter we want directly, as tsubst_copy
17918 doesn't do what we need. */
17919 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17920 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17921 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17922 parm = DECL_CHAIN (parm);
17923 scope = parm;
17924 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17925 if (DECL_CONTEXT (scope) == NULL_TREE)
17926 DECL_CONTEXT (scope) = fn;
17928 else if (TREE_CODE (scope) == FIELD_DECL)
17929 /* For a field, use tsubst_copy so that we look up the existing field
17930 rather than build a new one. */
17931 scope = RECUR (scope);
17932 else
17933 gcc_unreachable ();
17934 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17936 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17937 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17939 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17940 determine_visibility (TYPE_NAME (type));
17941 /* Now that we know visibility, instantiate the type so we have a
17942 declaration of the op() for later calls to lambda_function. */
17943 complete_type (type);
17945 if (tree fn = lambda_function (type))
17946 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17948 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17950 insert_pending_capture_proxies ();
17952 RETURN (build_lambda_object (r));
17955 case TARGET_EXPR:
17956 /* We can get here for a constant initializer of non-dependent type.
17957 FIXME stop folding in cp_parser_initializer_clause. */
17959 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17960 complain);
17961 RETURN (r);
17964 case TRANSACTION_EXPR:
17965 RETURN (tsubst_expr(t, args, complain, in_decl,
17966 integral_constant_expression_p));
17968 case PAREN_EXPR:
17969 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17971 case VEC_PERM_EXPR:
17973 tree op0 = RECUR (TREE_OPERAND (t, 0));
17974 tree op1 = RECUR (TREE_OPERAND (t, 1));
17975 tree op2 = RECUR (TREE_OPERAND (t, 2));
17976 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17977 complain));
17980 case REQUIRES_EXPR:
17981 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17983 default:
17984 /* Handle Objective-C++ constructs, if appropriate. */
17986 tree subst
17987 = objcp_tsubst_copy_and_build (t, args, complain,
17988 in_decl, /*function_p=*/false);
17989 if (subst)
17990 RETURN (subst);
17992 RETURN (tsubst_copy (t, args, complain, in_decl));
17995 #undef RECUR
17996 #undef RETURN
17997 out:
17998 input_location = loc;
17999 return retval;
18002 /* Verify that the instantiated ARGS are valid. For type arguments,
18003 make sure that the type's linkage is ok. For non-type arguments,
18004 make sure they are constants if they are integral or enumerations.
18005 Emit an error under control of COMPLAIN, and return TRUE on error. */
18007 static bool
18008 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18010 if (dependent_template_arg_p (t))
18011 return false;
18012 if (ARGUMENT_PACK_P (t))
18014 tree vec = ARGUMENT_PACK_ARGS (t);
18015 int len = TREE_VEC_LENGTH (vec);
18016 bool result = false;
18017 int i;
18019 for (i = 0; i < len; ++i)
18020 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18021 result = true;
18022 return result;
18024 else if (TYPE_P (t))
18026 /* [basic.link]: A name with no linkage (notably, the name
18027 of a class or enumeration declared in a local scope)
18028 shall not be used to declare an entity with linkage.
18029 This implies that names with no linkage cannot be used as
18030 template arguments
18032 DR 757 relaxes this restriction for C++0x. */
18033 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18034 : no_linkage_check (t, /*relaxed_p=*/false));
18036 if (nt)
18038 /* DR 488 makes use of a type with no linkage cause
18039 type deduction to fail. */
18040 if (complain & tf_error)
18042 if (TYPE_UNNAMED_P (nt))
18043 error ("%qT is/uses unnamed type", t);
18044 else
18045 error ("template argument for %qD uses local type %qT",
18046 tmpl, t);
18048 return true;
18050 /* In order to avoid all sorts of complications, we do not
18051 allow variably-modified types as template arguments. */
18052 else if (variably_modified_type_p (t, NULL_TREE))
18054 if (complain & tf_error)
18055 error ("%qT is a variably modified type", t);
18056 return true;
18059 /* Class template and alias template arguments should be OK. */
18060 else if (DECL_TYPE_TEMPLATE_P (t))
18062 /* A non-type argument of integral or enumerated type must be a
18063 constant. */
18064 else if (TREE_TYPE (t)
18065 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18066 && !REFERENCE_REF_P (t)
18067 && !TREE_CONSTANT (t))
18069 if (complain & tf_error)
18070 error ("integral expression %qE is not constant", t);
18071 return true;
18073 return false;
18076 static bool
18077 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18079 int ix, len = DECL_NTPARMS (tmpl);
18080 bool result = false;
18082 for (ix = 0; ix != len; ix++)
18084 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18085 result = true;
18087 if (result && (complain & tf_error))
18088 error (" trying to instantiate %qD", tmpl);
18089 return result;
18092 /* We're out of SFINAE context now, so generate diagnostics for the access
18093 errors we saw earlier when instantiating D from TMPL and ARGS. */
18095 static void
18096 recheck_decl_substitution (tree d, tree tmpl, tree args)
18098 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18099 tree type = TREE_TYPE (pattern);
18100 location_t loc = input_location;
18102 push_access_scope (d);
18103 push_deferring_access_checks (dk_no_deferred);
18104 input_location = DECL_SOURCE_LOCATION (pattern);
18105 tsubst (type, args, tf_warning_or_error, d);
18106 input_location = loc;
18107 pop_deferring_access_checks ();
18108 pop_access_scope (d);
18111 /* Instantiate the indicated variable, function, or alias template TMPL with
18112 the template arguments in TARG_PTR. */
18114 static tree
18115 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18117 tree targ_ptr = orig_args;
18118 tree fndecl;
18119 tree gen_tmpl;
18120 tree spec;
18121 bool access_ok = true;
18123 if (tmpl == error_mark_node)
18124 return error_mark_node;
18126 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18128 /* If this function is a clone, handle it specially. */
18129 if (DECL_CLONED_FUNCTION_P (tmpl))
18131 tree spec;
18132 tree clone;
18134 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18135 DECL_CLONED_FUNCTION. */
18136 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18137 targ_ptr, complain);
18138 if (spec == error_mark_node)
18139 return error_mark_node;
18141 /* Look for the clone. */
18142 FOR_EACH_CLONE (clone, spec)
18143 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18144 return clone;
18145 /* We should always have found the clone by now. */
18146 gcc_unreachable ();
18147 return NULL_TREE;
18150 if (targ_ptr == error_mark_node)
18151 return error_mark_node;
18153 /* Check to see if we already have this specialization. */
18154 gen_tmpl = most_general_template (tmpl);
18155 if (TMPL_ARGS_DEPTH (targ_ptr)
18156 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18157 /* targ_ptr only has the innermost template args, so add the outer ones
18158 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18159 the case of a non-dependent call within a template definition). */
18160 targ_ptr = (add_outermost_template_args
18161 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18162 targ_ptr));
18164 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18165 but it doesn't seem to be on the hot path. */
18166 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18168 gcc_assert (tmpl == gen_tmpl
18169 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18170 == spec)
18171 || fndecl == NULL_TREE);
18173 if (spec != NULL_TREE)
18175 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18177 if (complain & tf_error)
18178 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18179 return error_mark_node;
18181 return spec;
18184 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18185 complain))
18186 return error_mark_node;
18188 /* We are building a FUNCTION_DECL, during which the access of its
18189 parameters and return types have to be checked. However this
18190 FUNCTION_DECL which is the desired context for access checking
18191 is not built yet. We solve this chicken-and-egg problem by
18192 deferring all checks until we have the FUNCTION_DECL. */
18193 push_deferring_access_checks (dk_deferred);
18195 /* Instantiation of the function happens in the context of the function
18196 template, not the context of the overload resolution we're doing. */
18197 push_to_top_level ();
18198 /* If there are dependent arguments, e.g. because we're doing partial
18199 ordering, make sure processing_template_decl stays set. */
18200 if (uses_template_parms (targ_ptr))
18201 ++processing_template_decl;
18202 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18204 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18205 complain, gen_tmpl, true);
18206 push_nested_class (ctx);
18209 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18211 fndecl = NULL_TREE;
18212 if (VAR_P (pattern))
18214 /* We need to determine if we're using a partial or explicit
18215 specialization now, because the type of the variable could be
18216 different. */
18217 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18218 tree elt = most_specialized_partial_spec (tid, complain);
18219 if (elt == error_mark_node)
18220 pattern = error_mark_node;
18221 else if (elt)
18223 tree partial_tmpl = TREE_VALUE (elt);
18224 tree partial_args = TREE_PURPOSE (elt);
18225 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18226 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18230 /* Substitute template parameters to obtain the specialization. */
18231 if (fndecl == NULL_TREE)
18232 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18233 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18234 pop_nested_class ();
18235 pop_from_top_level ();
18237 if (fndecl == error_mark_node)
18239 pop_deferring_access_checks ();
18240 return error_mark_node;
18243 /* The DECL_TI_TEMPLATE should always be the immediate parent
18244 template, not the most general template. */
18245 DECL_TI_TEMPLATE (fndecl) = tmpl;
18246 DECL_TI_ARGS (fndecl) = targ_ptr;
18248 /* Now we know the specialization, compute access previously
18249 deferred. Do no access control for inheriting constructors,
18250 as we already checked access for the inherited constructor. */
18251 if (!(flag_new_inheriting_ctors
18252 && DECL_INHERITED_CTOR (fndecl)))
18254 push_access_scope (fndecl);
18255 if (!perform_deferred_access_checks (complain))
18256 access_ok = false;
18257 pop_access_scope (fndecl);
18259 pop_deferring_access_checks ();
18261 /* If we've just instantiated the main entry point for a function,
18262 instantiate all the alternate entry points as well. We do this
18263 by cloning the instantiation of the main entry point, not by
18264 instantiating the template clones. */
18265 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18266 clone_function_decl (fndecl, /*update_methods=*/false);
18268 if (!access_ok)
18270 if (!(complain & tf_error))
18272 /* Remember to reinstantiate when we're out of SFINAE so the user
18273 can see the errors. */
18274 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18276 return error_mark_node;
18278 return fndecl;
18281 /* Wrapper for instantiate_template_1. */
18283 tree
18284 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18286 tree ret;
18287 timevar_push (TV_TEMPLATE_INST);
18288 ret = instantiate_template_1 (tmpl, orig_args, complain);
18289 timevar_pop (TV_TEMPLATE_INST);
18290 return ret;
18293 /* Instantiate the alias template TMPL with ARGS. Also push a template
18294 instantiation level, which instantiate_template doesn't do because
18295 functions and variables have sufficient context established by the
18296 callers. */
18298 static tree
18299 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18301 struct pending_template *old_last_pend = last_pending_template;
18302 struct tinst_level *old_error_tinst = last_error_tinst_level;
18303 if (tmpl == error_mark_node || args == error_mark_node)
18304 return error_mark_node;
18305 tree tinst = build_tree_list (tmpl, args);
18306 if (!push_tinst_level (tinst))
18308 ggc_free (tinst);
18309 return error_mark_node;
18312 args =
18313 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18314 args, tmpl, complain,
18315 /*require_all_args=*/true,
18316 /*use_default_args=*/true);
18318 tree r = instantiate_template (tmpl, args, complain);
18319 pop_tinst_level ();
18320 /* We can't free this if a pending_template entry or last_error_tinst_level
18321 is pointing at it. */
18322 if (last_pending_template == old_last_pend
18323 && last_error_tinst_level == old_error_tinst)
18324 ggc_free (tinst);
18326 return r;
18329 /* PARM is a template parameter pack for FN. Returns true iff
18330 PARM is used in a deducible way in the argument list of FN. */
18332 static bool
18333 pack_deducible_p (tree parm, tree fn)
18335 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18336 for (; t; t = TREE_CHAIN (t))
18338 tree type = TREE_VALUE (t);
18339 tree packs;
18340 if (!PACK_EXPANSION_P (type))
18341 continue;
18342 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18343 packs; packs = TREE_CHAIN (packs))
18344 if (template_args_equal (TREE_VALUE (packs), parm))
18346 /* The template parameter pack is used in a function parameter
18347 pack. If this is the end of the parameter list, the
18348 template parameter pack is deducible. */
18349 if (TREE_CHAIN (t) == void_list_node)
18350 return true;
18351 else
18352 /* Otherwise, not. Well, it could be deduced from
18353 a non-pack parameter, but doing so would end up with
18354 a deduction mismatch, so don't bother. */
18355 return false;
18358 /* The template parameter pack isn't used in any function parameter
18359 packs, but it might be used deeper, e.g. tuple<Args...>. */
18360 return true;
18363 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18364 NARGS elements of the arguments that are being used when calling
18365 it. TARGS is a vector into which the deduced template arguments
18366 are placed.
18368 Returns either a FUNCTION_DECL for the matching specialization of FN or
18369 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18370 true, diagnostics will be printed to explain why it failed.
18372 If FN is a conversion operator, or we are trying to produce a specific
18373 specialization, RETURN_TYPE is the return type desired.
18375 The EXPLICIT_TARGS are explicit template arguments provided via a
18376 template-id.
18378 The parameter STRICT is one of:
18380 DEDUCE_CALL:
18381 We are deducing arguments for a function call, as in
18382 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18383 deducing arguments for a call to the result of a conversion
18384 function template, as in [over.call.object].
18386 DEDUCE_CONV:
18387 We are deducing arguments for a conversion function, as in
18388 [temp.deduct.conv].
18390 DEDUCE_EXACT:
18391 We are deducing arguments when doing an explicit instantiation
18392 as in [temp.explicit], when determining an explicit specialization
18393 as in [temp.expl.spec], or when taking the address of a function
18394 template, as in [temp.deduct.funcaddr]. */
18396 tree
18397 fn_type_unification (tree fn,
18398 tree explicit_targs,
18399 tree targs,
18400 const tree *args,
18401 unsigned int nargs,
18402 tree return_type,
18403 unification_kind_t strict,
18404 int flags,
18405 bool explain_p,
18406 bool decltype_p)
18408 tree parms;
18409 tree fntype;
18410 tree decl = NULL_TREE;
18411 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18412 bool ok;
18413 static int deduction_depth;
18414 struct pending_template *old_last_pend = last_pending_template;
18415 struct tinst_level *old_error_tinst = last_error_tinst_level;
18417 tree orig_fn = fn;
18418 if (flag_new_inheriting_ctors)
18419 fn = strip_inheriting_ctors (fn);
18421 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18422 tree tinst;
18423 tree r = error_mark_node;
18425 tree full_targs = targs;
18426 if (TMPL_ARGS_DEPTH (targs)
18427 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18428 full_targs = (add_outermost_template_args
18429 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18430 targs));
18432 if (decltype_p)
18433 complain |= tf_decltype;
18435 /* In C++0x, it's possible to have a function template whose type depends
18436 on itself recursively. This is most obvious with decltype, but can also
18437 occur with enumeration scope (c++/48969). So we need to catch infinite
18438 recursion and reject the substitution at deduction time; this function
18439 will return error_mark_node for any repeated substitution.
18441 This also catches excessive recursion such as when f<N> depends on
18442 f<N-1> across all integers, and returns error_mark_node for all the
18443 substitutions back up to the initial one.
18445 This is, of course, not reentrant. */
18446 if (excessive_deduction_depth)
18447 return error_mark_node;
18448 tinst = build_tree_list (fn, NULL_TREE);
18449 ++deduction_depth;
18451 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18453 fntype = TREE_TYPE (fn);
18454 if (explicit_targs)
18456 /* [temp.deduct]
18458 The specified template arguments must match the template
18459 parameters in kind (i.e., type, nontype, template), and there
18460 must not be more arguments than there are parameters;
18461 otherwise type deduction fails.
18463 Nontype arguments must match the types of the corresponding
18464 nontype template parameters, or must be convertible to the
18465 types of the corresponding nontype parameters as specified in
18466 _temp.arg.nontype_, otherwise type deduction fails.
18468 All references in the function type of the function template
18469 to the corresponding template parameters are replaced by the
18470 specified template argument values. If a substitution in a
18471 template parameter or in the function type of the function
18472 template results in an invalid type, type deduction fails. */
18473 int i, len = TREE_VEC_LENGTH (tparms);
18474 location_t loc = input_location;
18475 bool incomplete = false;
18477 if (explicit_targs == error_mark_node)
18478 goto fail;
18480 if (TMPL_ARGS_DEPTH (explicit_targs)
18481 < TMPL_ARGS_DEPTH (full_targs))
18482 explicit_targs = add_outermost_template_args (full_targs,
18483 explicit_targs);
18485 /* Adjust any explicit template arguments before entering the
18486 substitution context. */
18487 explicit_targs
18488 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18489 complain,
18490 /*require_all_args=*/false,
18491 /*use_default_args=*/false));
18492 if (explicit_targs == error_mark_node)
18493 goto fail;
18495 /* Substitute the explicit args into the function type. This is
18496 necessary so that, for instance, explicitly declared function
18497 arguments can match null pointed constants. If we were given
18498 an incomplete set of explicit args, we must not do semantic
18499 processing during substitution as we could create partial
18500 instantiations. */
18501 for (i = 0; i < len; i++)
18503 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18504 bool parameter_pack = false;
18505 tree targ = TREE_VEC_ELT (explicit_targs, i);
18507 /* Dig out the actual parm. */
18508 if (TREE_CODE (parm) == TYPE_DECL
18509 || TREE_CODE (parm) == TEMPLATE_DECL)
18511 parm = TREE_TYPE (parm);
18512 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18514 else if (TREE_CODE (parm) == PARM_DECL)
18516 parm = DECL_INITIAL (parm);
18517 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18520 if (!parameter_pack && targ == NULL_TREE)
18521 /* No explicit argument for this template parameter. */
18522 incomplete = true;
18524 if (parameter_pack && pack_deducible_p (parm, fn))
18526 /* Mark the argument pack as "incomplete". We could
18527 still deduce more arguments during unification.
18528 We remove this mark in type_unification_real. */
18529 if (targ)
18531 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18532 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18533 = ARGUMENT_PACK_ARGS (targ);
18536 /* We have some incomplete argument packs. */
18537 incomplete = true;
18541 TREE_VALUE (tinst) = explicit_targs;
18542 if (!push_tinst_level (tinst))
18544 excessive_deduction_depth = true;
18545 goto fail;
18547 processing_template_decl += incomplete;
18548 input_location = DECL_SOURCE_LOCATION (fn);
18549 /* Ignore any access checks; we'll see them again in
18550 instantiate_template and they might have the wrong
18551 access path at this point. */
18552 push_deferring_access_checks (dk_deferred);
18553 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18554 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18555 pop_deferring_access_checks ();
18556 input_location = loc;
18557 processing_template_decl -= incomplete;
18558 pop_tinst_level ();
18560 if (fntype == error_mark_node)
18561 goto fail;
18563 /* Place the explicitly specified arguments in TARGS. */
18564 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18565 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18566 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18569 /* Never do unification on the 'this' parameter. */
18570 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18572 if (return_type && strict == DEDUCE_CALL)
18574 /* We're deducing for a call to the result of a template conversion
18575 function. The parms we really want are in return_type. */
18576 if (POINTER_TYPE_P (return_type))
18577 return_type = TREE_TYPE (return_type);
18578 parms = TYPE_ARG_TYPES (return_type);
18580 else if (return_type)
18582 tree *new_args;
18584 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18585 new_args = XALLOCAVEC (tree, nargs + 1);
18586 new_args[0] = return_type;
18587 memcpy (new_args + 1, args, nargs * sizeof (tree));
18588 args = new_args;
18589 ++nargs;
18592 /* We allow incomplete unification without an error message here
18593 because the standard doesn't seem to explicitly prohibit it. Our
18594 callers must be ready to deal with unification failures in any
18595 event. */
18597 TREE_VALUE (tinst) = targs;
18598 /* If we aren't explaining yet, push tinst context so we can see where
18599 any errors (e.g. from class instantiations triggered by instantiation
18600 of default template arguments) come from. If we are explaining, this
18601 context is redundant. */
18602 if (!explain_p && !push_tinst_level (tinst))
18604 excessive_deduction_depth = true;
18605 goto fail;
18608 /* type_unification_real will pass back any access checks from default
18609 template argument substitution. */
18610 vec<deferred_access_check, va_gc> *checks;
18611 checks = NULL;
18613 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18614 full_targs, parms, args, nargs, /*subr=*/0,
18615 strict, flags, &checks, explain_p);
18616 if (!explain_p)
18617 pop_tinst_level ();
18618 if (!ok)
18619 goto fail;
18621 /* Now that we have bindings for all of the template arguments,
18622 ensure that the arguments deduced for the template template
18623 parameters have compatible template parameter lists. We cannot
18624 check this property before we have deduced all template
18625 arguments, because the template parameter types of a template
18626 template parameter might depend on prior template parameters
18627 deduced after the template template parameter. The following
18628 ill-formed example illustrates this issue:
18630 template<typename T, template<T> class C> void f(C<5>, T);
18632 template<int N> struct X {};
18634 void g() {
18635 f(X<5>(), 5l); // error: template argument deduction fails
18638 The template parameter list of 'C' depends on the template type
18639 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18640 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18641 time that we deduce 'C'. */
18642 if (!template_template_parm_bindings_ok_p
18643 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18645 unify_inconsistent_template_template_parameters (explain_p);
18646 goto fail;
18649 /* All is well so far. Now, check:
18651 [temp.deduct]
18653 When all template arguments have been deduced, all uses of
18654 template parameters in nondeduced contexts are replaced with
18655 the corresponding deduced argument values. If the
18656 substitution results in an invalid type, as described above,
18657 type deduction fails. */
18658 TREE_VALUE (tinst) = targs;
18659 if (!push_tinst_level (tinst))
18661 excessive_deduction_depth = true;
18662 goto fail;
18665 /* Also collect access checks from the instantiation. */
18666 reopen_deferring_access_checks (checks);
18668 decl = instantiate_template (fn, targs, complain);
18670 checks = get_deferred_access_checks ();
18671 pop_deferring_access_checks ();
18673 pop_tinst_level ();
18675 if (decl == error_mark_node)
18676 goto fail;
18678 /* Now perform any access checks encountered during substitution. */
18679 push_access_scope (decl);
18680 ok = perform_access_checks (checks, complain);
18681 pop_access_scope (decl);
18682 if (!ok)
18683 goto fail;
18685 /* If we're looking for an exact match, check that what we got
18686 is indeed an exact match. It might not be if some template
18687 parameters are used in non-deduced contexts. But don't check
18688 for an exact match if we have dependent template arguments;
18689 in that case we're doing partial ordering, and we already know
18690 that we have two candidates that will provide the actual type. */
18691 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18693 tree substed = TREE_TYPE (decl);
18694 unsigned int i;
18696 tree sarg
18697 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18698 if (return_type)
18699 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18700 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18701 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18703 unify_type_mismatch (explain_p, args[i],
18704 TREE_VALUE (sarg));
18705 goto fail;
18709 /* After doing deduction with the inherited constructor, actually return an
18710 instantiation of the inheriting constructor. */
18711 if (orig_fn != fn)
18712 decl = instantiate_template (orig_fn, targs, complain);
18714 r = decl;
18716 fail:
18717 --deduction_depth;
18718 if (excessive_deduction_depth)
18720 if (deduction_depth == 0)
18721 /* Reset once we're all the way out. */
18722 excessive_deduction_depth = false;
18725 /* We can't free this if a pending_template entry or last_error_tinst_level
18726 is pointing at it. */
18727 if (last_pending_template == old_last_pend
18728 && last_error_tinst_level == old_error_tinst)
18729 ggc_free (tinst);
18731 return r;
18734 /* Adjust types before performing type deduction, as described in
18735 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18736 sections are symmetric. PARM is the type of a function parameter
18737 or the return type of the conversion function. ARG is the type of
18738 the argument passed to the call, or the type of the value
18739 initialized with the result of the conversion function.
18740 ARG_EXPR is the original argument expression, which may be null. */
18742 static int
18743 maybe_adjust_types_for_deduction (unification_kind_t strict,
18744 tree* parm,
18745 tree* arg,
18746 tree arg_expr)
18748 int result = 0;
18750 switch (strict)
18752 case DEDUCE_CALL:
18753 break;
18755 case DEDUCE_CONV:
18756 /* Swap PARM and ARG throughout the remainder of this
18757 function; the handling is precisely symmetric since PARM
18758 will initialize ARG rather than vice versa. */
18759 std::swap (parm, arg);
18760 break;
18762 case DEDUCE_EXACT:
18763 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18764 too, but here handle it by stripping the reference from PARM
18765 rather than by adding it to ARG. */
18766 if (TREE_CODE (*parm) == REFERENCE_TYPE
18767 && TYPE_REF_IS_RVALUE (*parm)
18768 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18769 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18770 && TREE_CODE (*arg) == REFERENCE_TYPE
18771 && !TYPE_REF_IS_RVALUE (*arg))
18772 *parm = TREE_TYPE (*parm);
18773 /* Nothing else to do in this case. */
18774 return 0;
18776 default:
18777 gcc_unreachable ();
18780 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18782 /* [temp.deduct.call]
18784 If P is not a reference type:
18786 --If A is an array type, the pointer type produced by the
18787 array-to-pointer standard conversion (_conv.array_) is
18788 used in place of A for type deduction; otherwise,
18790 --If A is a function type, the pointer type produced by
18791 the function-to-pointer standard conversion
18792 (_conv.func_) is used in place of A for type deduction;
18793 otherwise,
18795 --If A is a cv-qualified type, the top level
18796 cv-qualifiers of A's type are ignored for type
18797 deduction. */
18798 if (TREE_CODE (*arg) == ARRAY_TYPE)
18799 *arg = build_pointer_type (TREE_TYPE (*arg));
18800 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18801 *arg = build_pointer_type (*arg);
18802 else
18803 *arg = TYPE_MAIN_VARIANT (*arg);
18806 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18807 reference to a cv-unqualified template parameter that does not represent a
18808 template parameter of a class template (during class template argument
18809 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18810 an lvalue, the type "lvalue reference to A" is used in place of A for type
18811 deduction. */
18812 if (TREE_CODE (*parm) == REFERENCE_TYPE
18813 && TYPE_REF_IS_RVALUE (*parm)
18814 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18815 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18816 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18817 && (arg_expr ? lvalue_p (arg_expr)
18818 /* try_one_overload doesn't provide an arg_expr, but
18819 functions are always lvalues. */
18820 : TREE_CODE (*arg) == FUNCTION_TYPE))
18821 *arg = build_reference_type (*arg);
18823 /* [temp.deduct.call]
18825 If P is a cv-qualified type, the top level cv-qualifiers
18826 of P's type are ignored for type deduction. If P is a
18827 reference type, the type referred to by P is used for
18828 type deduction. */
18829 *parm = TYPE_MAIN_VARIANT (*parm);
18830 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18832 *parm = TREE_TYPE (*parm);
18833 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18836 /* DR 322. For conversion deduction, remove a reference type on parm
18837 too (which has been swapped into ARG). */
18838 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18839 *arg = TREE_TYPE (*arg);
18841 return result;
18844 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18845 template which does contain any deducible template parameters; check if
18846 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18847 unify_one_argument. */
18849 static int
18850 check_non_deducible_conversion (tree parm, tree arg, int strict,
18851 int flags, bool explain_p)
18853 tree type;
18855 if (!TYPE_P (arg))
18856 type = TREE_TYPE (arg);
18857 else
18858 type = arg;
18860 if (same_type_p (parm, type))
18861 return unify_success (explain_p);
18863 if (strict == DEDUCE_CONV)
18865 if (can_convert_arg (type, parm, NULL_TREE, flags,
18866 explain_p ? tf_warning_or_error : tf_none))
18867 return unify_success (explain_p);
18869 else if (strict != DEDUCE_EXACT)
18871 if (can_convert_arg (parm, type,
18872 TYPE_P (arg) ? NULL_TREE : arg,
18873 flags, explain_p ? tf_warning_or_error : tf_none))
18874 return unify_success (explain_p);
18877 if (strict == DEDUCE_EXACT)
18878 return unify_type_mismatch (explain_p, parm, arg);
18879 else
18880 return unify_arg_conversion (explain_p, parm, type, arg);
18883 static bool uses_deducible_template_parms (tree type);
18885 /* Returns true iff the expression EXPR is one from which a template
18886 argument can be deduced. In other words, if it's an undecorated
18887 use of a template non-type parameter. */
18889 static bool
18890 deducible_expression (tree expr)
18892 /* Strip implicit conversions. */
18893 while (CONVERT_EXPR_P (expr))
18894 expr = TREE_OPERAND (expr, 0);
18895 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18898 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18899 deducible way; that is, if it has a max value of <PARM> - 1. */
18901 static bool
18902 deducible_array_bound (tree domain)
18904 if (domain == NULL_TREE)
18905 return false;
18907 tree max = TYPE_MAX_VALUE (domain);
18908 if (TREE_CODE (max) != MINUS_EXPR)
18909 return false;
18911 return deducible_expression (TREE_OPERAND (max, 0));
18914 /* Returns true iff the template arguments ARGS use a template parameter
18915 in a deducible way. */
18917 static bool
18918 deducible_template_args (tree args)
18920 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18922 bool deducible;
18923 tree elt = TREE_VEC_ELT (args, i);
18924 if (ARGUMENT_PACK_P (elt))
18925 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18926 else
18928 if (PACK_EXPANSION_P (elt))
18929 elt = PACK_EXPANSION_PATTERN (elt);
18930 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18931 deducible = true;
18932 else if (TYPE_P (elt))
18933 deducible = uses_deducible_template_parms (elt);
18934 else
18935 deducible = deducible_expression (elt);
18937 if (deducible)
18938 return true;
18940 return false;
18943 /* Returns true iff TYPE contains any deducible references to template
18944 parameters, as per 14.8.2.5. */
18946 static bool
18947 uses_deducible_template_parms (tree type)
18949 if (PACK_EXPANSION_P (type))
18950 type = PACK_EXPANSION_PATTERN (type);
18952 /* T
18953 cv-list T
18954 TT<T>
18955 TT<i>
18956 TT<> */
18957 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18958 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18959 return true;
18961 /* T*
18963 T&& */
18964 if (POINTER_TYPE_P (type))
18965 return uses_deducible_template_parms (TREE_TYPE (type));
18967 /* T[integer-constant ]
18968 type [i] */
18969 if (TREE_CODE (type) == ARRAY_TYPE)
18970 return (uses_deducible_template_parms (TREE_TYPE (type))
18971 || deducible_array_bound (TYPE_DOMAIN (type)));
18973 /* T type ::*
18974 type T::*
18975 T T::*
18976 T (type ::*)()
18977 type (T::*)()
18978 type (type ::*)(T)
18979 type (T::*)(T)
18980 T (type ::*)(T)
18981 T (T::*)()
18982 T (T::*)(T) */
18983 if (TYPE_PTRMEM_P (type))
18984 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18985 || (uses_deducible_template_parms
18986 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18988 /* template-name <T> (where template-name refers to a class template)
18989 template-name <i> (where template-name refers to a class template) */
18990 if (CLASS_TYPE_P (type)
18991 && CLASSTYPE_TEMPLATE_INFO (type)
18992 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18993 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18994 (CLASSTYPE_TI_ARGS (type)));
18996 /* type (T)
18998 T(T) */
18999 if (TREE_CODE (type) == FUNCTION_TYPE
19000 || TREE_CODE (type) == METHOD_TYPE)
19002 if (uses_deducible_template_parms (TREE_TYPE (type)))
19003 return true;
19004 tree parm = TYPE_ARG_TYPES (type);
19005 if (TREE_CODE (type) == METHOD_TYPE)
19006 parm = TREE_CHAIN (parm);
19007 for (; parm; parm = TREE_CHAIN (parm))
19008 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19009 return true;
19012 return false;
19015 /* Subroutine of type_unification_real and unify_pack_expansion to
19016 handle unification of a single P/A pair. Parameters are as
19017 for those functions. */
19019 static int
19020 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19021 int subr, unification_kind_t strict,
19022 bool explain_p)
19024 tree arg_expr = NULL_TREE;
19025 int arg_strict;
19027 if (arg == error_mark_node || parm == error_mark_node)
19028 return unify_invalid (explain_p);
19029 if (arg == unknown_type_node)
19030 /* We can't deduce anything from this, but we might get all the
19031 template args from other function args. */
19032 return unify_success (explain_p);
19034 /* Implicit conversions (Clause 4) will be performed on a function
19035 argument to convert it to the type of the corresponding function
19036 parameter if the parameter type contains no template-parameters that
19037 participate in template argument deduction. */
19038 if (strict != DEDUCE_EXACT
19039 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19040 /* For function parameters with no deducible template parameters,
19041 just return. We'll check non-dependent conversions later. */
19042 return unify_success (explain_p);
19044 switch (strict)
19046 case DEDUCE_CALL:
19047 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19048 | UNIFY_ALLOW_MORE_CV_QUAL
19049 | UNIFY_ALLOW_DERIVED);
19050 break;
19052 case DEDUCE_CONV:
19053 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19054 break;
19056 case DEDUCE_EXACT:
19057 arg_strict = UNIFY_ALLOW_NONE;
19058 break;
19060 default:
19061 gcc_unreachable ();
19064 /* We only do these transformations if this is the top-level
19065 parameter_type_list in a call or declaration matching; in other
19066 situations (nested function declarators, template argument lists) we
19067 won't be comparing a type to an expression, and we don't do any type
19068 adjustments. */
19069 if (!subr)
19071 if (!TYPE_P (arg))
19073 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19074 if (type_unknown_p (arg))
19076 /* [temp.deduct.type] A template-argument can be
19077 deduced from a pointer to function or pointer
19078 to member function argument if the set of
19079 overloaded functions does not contain function
19080 templates and at most one of a set of
19081 overloaded functions provides a unique
19082 match. */
19084 if (resolve_overloaded_unification
19085 (tparms, targs, parm, arg, strict,
19086 arg_strict, explain_p))
19087 return unify_success (explain_p);
19088 return unify_overload_resolution_failure (explain_p, arg);
19091 arg_expr = arg;
19092 arg = unlowered_expr_type (arg);
19093 if (arg == error_mark_node)
19094 return unify_invalid (explain_p);
19097 arg_strict |=
19098 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19100 else
19101 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19102 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19103 return unify_template_argument_mismatch (explain_p, parm, arg);
19105 /* For deduction from an init-list we need the actual list. */
19106 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19107 arg = arg_expr;
19108 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19111 /* for_each_template_parm callback that always returns 0. */
19113 static int
19114 zero_r (tree, void *)
19116 return 0;
19119 /* for_each_template_parm any_fn callback to handle deduction of a template
19120 type argument from the type of an array bound. */
19122 static int
19123 array_deduction_r (tree t, void *data)
19125 tree_pair_p d = (tree_pair_p)data;
19126 tree &tparms = d->purpose;
19127 tree &targs = d->value;
19129 if (TREE_CODE (t) == ARRAY_TYPE)
19130 if (tree dom = TYPE_DOMAIN (t))
19131 if (tree max = TYPE_MAX_VALUE (dom))
19133 if (TREE_CODE (max) == MINUS_EXPR)
19134 max = TREE_OPERAND (max, 0);
19135 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19136 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19137 UNIFY_ALLOW_NONE, /*explain*/false);
19140 /* Keep walking. */
19141 return 0;
19144 /* Try to deduce any not-yet-deduced template type arguments from the type of
19145 an array bound. This is handled separately from unify because 14.8.2.5 says
19146 "The type of a type parameter is only deduced from an array bound if it is
19147 not otherwise deduced." */
19149 static void
19150 try_array_deduction (tree tparms, tree targs, tree parm)
19152 tree_pair_s data = { tparms, targs };
19153 hash_set<tree> visited;
19154 for_each_template_parm (parm, zero_r, &data, &visited,
19155 /*nondeduced*/false, array_deduction_r);
19158 /* Most parms like fn_type_unification.
19160 If SUBR is 1, we're being called recursively (to unify the
19161 arguments of a function or method parameter of a function
19162 template).
19164 CHECKS is a pointer to a vector of access checks encountered while
19165 substituting default template arguments. */
19167 static int
19168 type_unification_real (tree tparms,
19169 tree full_targs,
19170 tree xparms,
19171 const tree *xargs,
19172 unsigned int xnargs,
19173 int subr,
19174 unification_kind_t strict,
19175 int flags,
19176 vec<deferred_access_check, va_gc> **checks,
19177 bool explain_p)
19179 tree parm, arg;
19180 int i;
19181 int ntparms = TREE_VEC_LENGTH (tparms);
19182 int saw_undeduced = 0;
19183 tree parms;
19184 const tree *args;
19185 unsigned int nargs;
19186 unsigned int ia;
19188 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19189 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19190 gcc_assert (ntparms > 0);
19192 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19194 /* Reset the number of non-defaulted template arguments contained
19195 in TARGS. */
19196 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19198 again:
19199 parms = xparms;
19200 args = xargs;
19201 nargs = xnargs;
19203 ia = 0;
19204 while (parms && parms != void_list_node
19205 && ia < nargs)
19207 parm = TREE_VALUE (parms);
19209 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19210 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19211 /* For a function parameter pack that occurs at the end of the
19212 parameter-declaration-list, the type A of each remaining
19213 argument of the call is compared with the type P of the
19214 declarator-id of the function parameter pack. */
19215 break;
19217 parms = TREE_CHAIN (parms);
19219 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19220 /* For a function parameter pack that does not occur at the
19221 end of the parameter-declaration-list, the type of the
19222 parameter pack is a non-deduced context. */
19223 continue;
19225 arg = args[ia];
19226 ++ia;
19228 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19229 explain_p))
19230 return 1;
19233 if (parms
19234 && parms != void_list_node
19235 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19237 /* Unify the remaining arguments with the pack expansion type. */
19238 tree argvec;
19239 tree parmvec = make_tree_vec (1);
19241 /* Allocate a TREE_VEC and copy in all of the arguments */
19242 argvec = make_tree_vec (nargs - ia);
19243 for (i = 0; ia < nargs; ++ia, ++i)
19244 TREE_VEC_ELT (argvec, i) = args[ia];
19246 /* Copy the parameter into parmvec. */
19247 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19248 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19249 /*subr=*/subr, explain_p))
19250 return 1;
19252 /* Advance to the end of the list of parameters. */
19253 parms = TREE_CHAIN (parms);
19256 /* Fail if we've reached the end of the parm list, and more args
19257 are present, and the parm list isn't variadic. */
19258 if (ia < nargs && parms == void_list_node)
19259 return unify_too_many_arguments (explain_p, nargs, ia);
19260 /* Fail if parms are left and they don't have default values and
19261 they aren't all deduced as empty packs (c++/57397). This is
19262 consistent with sufficient_parms_p. */
19263 if (parms && parms != void_list_node
19264 && TREE_PURPOSE (parms) == NULL_TREE)
19266 unsigned int count = nargs;
19267 tree p = parms;
19268 bool type_pack_p;
19271 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19272 if (!type_pack_p)
19273 count++;
19274 p = TREE_CHAIN (p);
19276 while (p && p != void_list_node);
19277 if (count != nargs)
19278 return unify_too_few_arguments (explain_p, ia, count,
19279 type_pack_p);
19282 if (!subr)
19284 tsubst_flags_t complain = (explain_p
19285 ? tf_warning_or_error
19286 : tf_none);
19287 bool tried_array_deduction = (cxx_dialect < cxx1z);
19289 for (i = 0; i < ntparms; i++)
19291 tree targ = TREE_VEC_ELT (targs, i);
19292 tree tparm = TREE_VEC_ELT (tparms, i);
19294 /* Clear the "incomplete" flags on all argument packs now so that
19295 substituting them into later default arguments works. */
19296 if (targ && ARGUMENT_PACK_P (targ))
19298 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19299 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19302 if (targ || tparm == error_mark_node)
19303 continue;
19304 tparm = TREE_VALUE (tparm);
19306 if (TREE_CODE (tparm) == TYPE_DECL
19307 && !tried_array_deduction)
19309 try_array_deduction (tparms, targs, xparms);
19310 tried_array_deduction = true;
19311 if (TREE_VEC_ELT (targs, i))
19312 continue;
19315 /* If this is an undeduced nontype parameter that depends on
19316 a type parameter, try another pass; its type may have been
19317 deduced from a later argument than the one from which
19318 this parameter can be deduced. */
19319 if (TREE_CODE (tparm) == PARM_DECL
19320 && uses_template_parms (TREE_TYPE (tparm))
19321 && saw_undeduced < 2)
19323 saw_undeduced = 1;
19324 continue;
19327 /* Core issue #226 (C++0x) [temp.deduct]:
19329 If a template argument has not been deduced, its
19330 default template argument, if any, is used.
19332 When we are in C++98 mode, TREE_PURPOSE will either
19333 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19334 to explicitly check cxx_dialect here. */
19335 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19336 /* OK, there is a default argument. Wait until after the
19337 conversion check to do substitution. */
19338 continue;
19340 /* If the type parameter is a parameter pack, then it will
19341 be deduced to an empty parameter pack. */
19342 if (template_parameter_pack_p (tparm))
19344 tree arg;
19346 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19348 arg = make_node (NONTYPE_ARGUMENT_PACK);
19349 TREE_CONSTANT (arg) = 1;
19351 else
19352 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19354 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19356 TREE_VEC_ELT (targs, i) = arg;
19357 continue;
19360 return unify_parameter_deduction_failure (explain_p, tparm);
19363 /* DR 1391: All parameters have args, now check non-dependent parms for
19364 convertibility. */
19365 if (saw_undeduced < 2)
19366 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19367 parms && parms != void_list_node && ia < nargs; )
19369 parm = TREE_VALUE (parms);
19371 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19372 && (!TREE_CHAIN (parms)
19373 || TREE_CHAIN (parms) == void_list_node))
19374 /* For a function parameter pack that occurs at the end of the
19375 parameter-declaration-list, the type A of each remaining
19376 argument of the call is compared with the type P of the
19377 declarator-id of the function parameter pack. */
19378 break;
19380 parms = TREE_CHAIN (parms);
19382 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19383 /* For a function parameter pack that does not occur at the
19384 end of the parameter-declaration-list, the type of the
19385 parameter pack is a non-deduced context. */
19386 continue;
19388 arg = args[ia];
19389 ++ia;
19391 if (uses_template_parms (parm))
19392 continue;
19393 if (check_non_deducible_conversion (parm, arg, strict, flags,
19394 explain_p))
19395 return 1;
19398 /* Now substitute into the default template arguments. */
19399 for (i = 0; i < ntparms; i++)
19401 tree targ = TREE_VEC_ELT (targs, i);
19402 tree tparm = TREE_VEC_ELT (tparms, i);
19404 if (targ || tparm == error_mark_node)
19405 continue;
19406 tree parm = TREE_VALUE (tparm);
19408 if (TREE_CODE (parm) == PARM_DECL
19409 && uses_template_parms (TREE_TYPE (parm))
19410 && saw_undeduced < 2)
19411 continue;
19413 tree arg = TREE_PURPOSE (tparm);
19414 reopen_deferring_access_checks (*checks);
19415 location_t save_loc = input_location;
19416 if (DECL_P (parm))
19417 input_location = DECL_SOURCE_LOCATION (parm);
19418 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19419 if (!uses_template_parms (arg))
19420 arg = convert_template_argument (parm, arg, full_targs, complain,
19421 i, NULL_TREE);
19422 else if (saw_undeduced < 2)
19423 arg = NULL_TREE;
19424 else
19425 arg = error_mark_node;
19426 input_location = save_loc;
19427 *checks = get_deferred_access_checks ();
19428 pop_deferring_access_checks ();
19429 if (arg == error_mark_node)
19430 return 1;
19431 else if (arg)
19433 TREE_VEC_ELT (targs, i) = arg;
19434 /* The position of the first default template argument,
19435 is also the number of non-defaulted arguments in TARGS.
19436 Record that. */
19437 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19438 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19442 if (saw_undeduced++ == 1)
19443 goto again;
19446 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19447 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19449 return unify_success (explain_p);
19452 /* Subroutine of type_unification_real. Args are like the variables
19453 at the call site. ARG is an overloaded function (or template-id);
19454 we try deducing template args from each of the overloads, and if
19455 only one succeeds, we go with that. Modifies TARGS and returns
19456 true on success. */
19458 static bool
19459 resolve_overloaded_unification (tree tparms,
19460 tree targs,
19461 tree parm,
19462 tree arg,
19463 unification_kind_t strict,
19464 int sub_strict,
19465 bool explain_p)
19467 tree tempargs = copy_node (targs);
19468 int good = 0;
19469 tree goodfn = NULL_TREE;
19470 bool addr_p;
19472 if (TREE_CODE (arg) == ADDR_EXPR)
19474 arg = TREE_OPERAND (arg, 0);
19475 addr_p = true;
19477 else
19478 addr_p = false;
19480 if (TREE_CODE (arg) == COMPONENT_REF)
19481 /* Handle `&x' where `x' is some static or non-static member
19482 function name. */
19483 arg = TREE_OPERAND (arg, 1);
19485 if (TREE_CODE (arg) == OFFSET_REF)
19486 arg = TREE_OPERAND (arg, 1);
19488 /* Strip baselink information. */
19489 if (BASELINK_P (arg))
19490 arg = BASELINK_FUNCTIONS (arg);
19492 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19494 /* If we got some explicit template args, we need to plug them into
19495 the affected templates before we try to unify, in case the
19496 explicit args will completely resolve the templates in question. */
19498 int ok = 0;
19499 tree expl_subargs = TREE_OPERAND (arg, 1);
19500 arg = TREE_OPERAND (arg, 0);
19502 for (lkp_iterator iter (arg); iter; ++iter)
19504 tree fn = *iter;
19505 tree subargs, elem;
19507 if (TREE_CODE (fn) != TEMPLATE_DECL)
19508 continue;
19510 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19511 expl_subargs, NULL_TREE, tf_none,
19512 /*require_all_args=*/true,
19513 /*use_default_args=*/true);
19514 if (subargs != error_mark_node
19515 && !any_dependent_template_arguments_p (subargs))
19517 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19518 if (try_one_overload (tparms, targs, tempargs, parm,
19519 elem, strict, sub_strict, addr_p, explain_p)
19520 && (!goodfn || !same_type_p (goodfn, elem)))
19522 goodfn = elem;
19523 ++good;
19526 else if (subargs)
19527 ++ok;
19529 /* If no templates (or more than one) are fully resolved by the
19530 explicit arguments, this template-id is a non-deduced context; it
19531 could still be OK if we deduce all template arguments for the
19532 enclosing call through other arguments. */
19533 if (good != 1)
19534 good = ok;
19536 else if (TREE_CODE (arg) != OVERLOAD
19537 && TREE_CODE (arg) != FUNCTION_DECL)
19538 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19539 -- but the deduction does not succeed because the expression is
19540 not just the function on its own. */
19541 return false;
19542 else
19543 for (lkp_iterator iter (arg); iter; ++iter)
19545 tree fn = *iter;
19546 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19547 strict, sub_strict, addr_p, explain_p)
19548 && (!goodfn || !decls_match (goodfn, fn)))
19550 goodfn = fn;
19551 ++good;
19555 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19556 to function or pointer to member function argument if the set of
19557 overloaded functions does not contain function templates and at most
19558 one of a set of overloaded functions provides a unique match.
19560 So if we found multiple possibilities, we return success but don't
19561 deduce anything. */
19563 if (good == 1)
19565 int i = TREE_VEC_LENGTH (targs);
19566 for (; i--; )
19567 if (TREE_VEC_ELT (tempargs, i))
19569 tree old = TREE_VEC_ELT (targs, i);
19570 tree new_ = TREE_VEC_ELT (tempargs, i);
19571 if (new_ && old && ARGUMENT_PACK_P (old)
19572 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19573 /* Don't forget explicit template arguments in a pack. */
19574 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19575 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19576 TREE_VEC_ELT (targs, i) = new_;
19579 if (good)
19580 return true;
19582 return false;
19585 /* Core DR 115: In contexts where deduction is done and fails, or in
19586 contexts where deduction is not done, if a template argument list is
19587 specified and it, along with any default template arguments, identifies
19588 a single function template specialization, then the template-id is an
19589 lvalue for the function template specialization. */
19591 tree
19592 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19594 tree expr, offset, baselink;
19595 bool addr;
19597 if (!type_unknown_p (orig_expr))
19598 return orig_expr;
19600 expr = orig_expr;
19601 addr = false;
19602 offset = NULL_TREE;
19603 baselink = NULL_TREE;
19605 if (TREE_CODE (expr) == ADDR_EXPR)
19607 expr = TREE_OPERAND (expr, 0);
19608 addr = true;
19610 if (TREE_CODE (expr) == OFFSET_REF)
19612 offset = expr;
19613 expr = TREE_OPERAND (expr, 1);
19615 if (BASELINK_P (expr))
19617 baselink = expr;
19618 expr = BASELINK_FUNCTIONS (expr);
19621 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19623 int good = 0;
19624 tree goodfn = NULL_TREE;
19626 /* If we got some explicit template args, we need to plug them into
19627 the affected templates before we try to unify, in case the
19628 explicit args will completely resolve the templates in question. */
19630 tree expl_subargs = TREE_OPERAND (expr, 1);
19631 tree arg = TREE_OPERAND (expr, 0);
19632 tree badfn = NULL_TREE;
19633 tree badargs = NULL_TREE;
19635 for (lkp_iterator iter (arg); iter; ++iter)
19637 tree fn = *iter;
19638 tree subargs, elem;
19640 if (TREE_CODE (fn) != TEMPLATE_DECL)
19641 continue;
19643 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19644 expl_subargs, NULL_TREE, tf_none,
19645 /*require_all_args=*/true,
19646 /*use_default_args=*/true);
19647 if (subargs != error_mark_node
19648 && !any_dependent_template_arguments_p (subargs))
19650 elem = instantiate_template (fn, subargs, tf_none);
19651 if (elem == error_mark_node)
19653 badfn = fn;
19654 badargs = subargs;
19656 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19658 goodfn = elem;
19659 ++good;
19663 if (good == 1)
19665 mark_used (goodfn);
19666 expr = goodfn;
19667 if (baselink)
19668 expr = build_baselink (BASELINK_BINFO (baselink),
19669 BASELINK_ACCESS_BINFO (baselink),
19670 expr, BASELINK_OPTYPE (baselink));
19671 if (offset)
19673 tree base
19674 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19675 expr = build_offset_ref (base, expr, addr, complain);
19677 if (addr)
19678 expr = cp_build_addr_expr (expr, complain);
19679 return expr;
19681 else if (good == 0 && badargs && (complain & tf_error))
19682 /* There were no good options and at least one bad one, so let the
19683 user know what the problem is. */
19684 instantiate_template (badfn, badargs, complain);
19686 return orig_expr;
19689 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19690 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19691 different overloads deduce different arguments for a given parm.
19692 ADDR_P is true if the expression for which deduction is being
19693 performed was of the form "& fn" rather than simply "fn".
19695 Returns 1 on success. */
19697 static int
19698 try_one_overload (tree tparms,
19699 tree orig_targs,
19700 tree targs,
19701 tree parm,
19702 tree arg,
19703 unification_kind_t strict,
19704 int sub_strict,
19705 bool addr_p,
19706 bool explain_p)
19708 int nargs;
19709 tree tempargs;
19710 int i;
19712 if (arg == error_mark_node)
19713 return 0;
19715 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19716 to function or pointer to member function argument if the set of
19717 overloaded functions does not contain function templates and at most
19718 one of a set of overloaded functions provides a unique match.
19720 So if this is a template, just return success. */
19722 if (uses_template_parms (arg))
19723 return 1;
19725 if (TREE_CODE (arg) == METHOD_TYPE)
19726 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19727 else if (addr_p)
19728 arg = build_pointer_type (arg);
19730 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19732 /* We don't copy orig_targs for this because if we have already deduced
19733 some template args from previous args, unify would complain when we
19734 try to deduce a template parameter for the same argument, even though
19735 there isn't really a conflict. */
19736 nargs = TREE_VEC_LENGTH (targs);
19737 tempargs = make_tree_vec (nargs);
19739 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19740 return 0;
19742 /* First make sure we didn't deduce anything that conflicts with
19743 explicitly specified args. */
19744 for (i = nargs; i--; )
19746 tree elt = TREE_VEC_ELT (tempargs, i);
19747 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19749 if (!elt)
19750 /*NOP*/;
19751 else if (uses_template_parms (elt))
19752 /* Since we're unifying against ourselves, we will fill in
19753 template args used in the function parm list with our own
19754 template parms. Discard them. */
19755 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19756 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19758 /* Check that the argument at each index of the deduced argument pack
19759 is equivalent to the corresponding explicitly specified argument.
19760 We may have deduced more arguments than were explicitly specified,
19761 and that's OK. */
19763 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19764 that's wrong if we deduce the same argument pack from multiple
19765 function arguments: it's only incomplete the first time. */
19767 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19768 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19770 if (TREE_VEC_LENGTH (deduced_pack)
19771 < TREE_VEC_LENGTH (explicit_pack))
19772 return 0;
19774 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19775 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19776 TREE_VEC_ELT (deduced_pack, j)))
19777 return 0;
19779 else if (oldelt && !template_args_equal (oldelt, elt))
19780 return 0;
19783 for (i = nargs; i--; )
19785 tree elt = TREE_VEC_ELT (tempargs, i);
19787 if (elt)
19788 TREE_VEC_ELT (targs, i) = elt;
19791 return 1;
19794 /* PARM is a template class (perhaps with unbound template
19795 parameters). ARG is a fully instantiated type. If ARG can be
19796 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19797 TARGS are as for unify. */
19799 static tree
19800 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19801 bool explain_p)
19803 tree copy_of_targs;
19805 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19806 return NULL_TREE;
19807 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19808 /* Matches anything. */;
19809 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19810 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19811 return NULL_TREE;
19813 /* We need to make a new template argument vector for the call to
19814 unify. If we used TARGS, we'd clutter it up with the result of
19815 the attempted unification, even if this class didn't work out.
19816 We also don't want to commit ourselves to all the unifications
19817 we've already done, since unification is supposed to be done on
19818 an argument-by-argument basis. In other words, consider the
19819 following pathological case:
19821 template <int I, int J, int K>
19822 struct S {};
19824 template <int I, int J>
19825 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19827 template <int I, int J, int K>
19828 void f(S<I, J, K>, S<I, I, I>);
19830 void g() {
19831 S<0, 0, 0> s0;
19832 S<0, 1, 2> s2;
19834 f(s0, s2);
19837 Now, by the time we consider the unification involving `s2', we
19838 already know that we must have `f<0, 0, 0>'. But, even though
19839 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19840 because there are two ways to unify base classes of S<0, 1, 2>
19841 with S<I, I, I>. If we kept the already deduced knowledge, we
19842 would reject the possibility I=1. */
19843 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19845 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19847 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19848 return NULL_TREE;
19849 return arg;
19852 /* If unification failed, we're done. */
19853 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19854 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19855 return NULL_TREE;
19857 return arg;
19860 /* Given a template type PARM and a class type ARG, find the unique
19861 base type in ARG that is an instance of PARM. We do not examine
19862 ARG itself; only its base-classes. If there is not exactly one
19863 appropriate base class, return NULL_TREE. PARM may be the type of
19864 a partial specialization, as well as a plain template type. Used
19865 by unify. */
19867 static enum template_base_result
19868 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19869 bool explain_p, tree *result)
19871 tree rval = NULL_TREE;
19872 tree binfo;
19874 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19876 binfo = TYPE_BINFO (complete_type (arg));
19877 if (!binfo)
19879 /* The type could not be completed. */
19880 *result = NULL_TREE;
19881 return tbr_incomplete_type;
19884 /* Walk in inheritance graph order. The search order is not
19885 important, and this avoids multiple walks of virtual bases. */
19886 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19888 tree r = try_class_unification (tparms, targs, parm,
19889 BINFO_TYPE (binfo), explain_p);
19891 if (r)
19893 /* If there is more than one satisfactory baseclass, then:
19895 [temp.deduct.call]
19897 If they yield more than one possible deduced A, the type
19898 deduction fails.
19900 applies. */
19901 if (rval && !same_type_p (r, rval))
19903 *result = NULL_TREE;
19904 return tbr_ambiguous_baseclass;
19907 rval = r;
19911 *result = rval;
19912 return tbr_success;
19915 /* Returns the level of DECL, which declares a template parameter. */
19917 static int
19918 template_decl_level (tree decl)
19920 switch (TREE_CODE (decl))
19922 case TYPE_DECL:
19923 case TEMPLATE_DECL:
19924 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19926 case PARM_DECL:
19927 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19929 default:
19930 gcc_unreachable ();
19932 return 0;
19935 /* Decide whether ARG can be unified with PARM, considering only the
19936 cv-qualifiers of each type, given STRICT as documented for unify.
19937 Returns nonzero iff the unification is OK on that basis. */
19939 static int
19940 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19942 int arg_quals = cp_type_quals (arg);
19943 int parm_quals = cp_type_quals (parm);
19945 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19946 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19948 /* Although a CVR qualifier is ignored when being applied to a
19949 substituted template parameter ([8.3.2]/1 for example), that
19950 does not allow us to unify "const T" with "int&" because both
19951 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19952 It is ok when we're allowing additional CV qualifiers
19953 at the outer level [14.8.2.1]/3,1st bullet. */
19954 if ((TREE_CODE (arg) == REFERENCE_TYPE
19955 || TREE_CODE (arg) == FUNCTION_TYPE
19956 || TREE_CODE (arg) == METHOD_TYPE)
19957 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19958 return 0;
19960 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19961 && (parm_quals & TYPE_QUAL_RESTRICT))
19962 return 0;
19965 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19966 && (arg_quals & parm_quals) != parm_quals)
19967 return 0;
19969 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19970 && (parm_quals & arg_quals) != arg_quals)
19971 return 0;
19973 return 1;
19976 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19977 void
19978 template_parm_level_and_index (tree parm, int* level, int* index)
19980 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19981 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19982 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19984 *index = TEMPLATE_TYPE_IDX (parm);
19985 *level = TEMPLATE_TYPE_LEVEL (parm);
19987 else
19989 *index = TEMPLATE_PARM_IDX (parm);
19990 *level = TEMPLATE_PARM_LEVEL (parm);
19994 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19995 do { \
19996 if (unify (TP, TA, P, A, S, EP)) \
19997 return 1; \
19998 } while (0)
20000 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20001 expansion at the end of PACKED_PARMS. Returns 0 if the type
20002 deduction succeeds, 1 otherwise. STRICT is the same as in
20003 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20004 function call argument list. We'll need to adjust the arguments to make them
20005 types. SUBR tells us if this is from a recursive call to
20006 type_unification_real, or for comparing two template argument
20007 lists. */
20009 static int
20010 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20011 tree packed_args, unification_kind_t strict,
20012 bool subr, bool explain_p)
20014 tree parm
20015 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20016 tree pattern = PACK_EXPANSION_PATTERN (parm);
20017 tree pack, packs = NULL_TREE;
20018 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20020 /* Add in any args remembered from an earlier partial instantiation. */
20021 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20023 packed_args = expand_template_argument_pack (packed_args);
20025 int len = TREE_VEC_LENGTH (packed_args);
20027 /* Determine the parameter packs we will be deducing from the
20028 pattern, and record their current deductions. */
20029 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20030 pack; pack = TREE_CHAIN (pack))
20032 tree parm_pack = TREE_VALUE (pack);
20033 int idx, level;
20035 /* Determine the index and level of this parameter pack. */
20036 template_parm_level_and_index (parm_pack, &level, &idx);
20038 /* Keep track of the parameter packs and their corresponding
20039 argument packs. */
20040 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20041 TREE_TYPE (packs) = make_tree_vec (len - start);
20044 /* Loop through all of the arguments that have not yet been
20045 unified and unify each with the pattern. */
20046 for (i = start; i < len; i++)
20048 tree parm;
20049 bool any_explicit = false;
20050 tree arg = TREE_VEC_ELT (packed_args, i);
20052 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20053 or the element of its argument pack at the current index if
20054 this argument was explicitly specified. */
20055 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20057 int idx, level;
20058 tree arg, pargs;
20059 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20061 arg = NULL_TREE;
20062 if (TREE_VALUE (pack)
20063 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20064 && (i - start < TREE_VEC_LENGTH (pargs)))
20066 any_explicit = true;
20067 arg = TREE_VEC_ELT (pargs, i - start);
20069 TMPL_ARG (targs, level, idx) = arg;
20072 /* If we had explicit template arguments, substitute them into the
20073 pattern before deduction. */
20074 if (any_explicit)
20076 /* Some arguments might still be unspecified or dependent. */
20077 bool dependent;
20078 ++processing_template_decl;
20079 dependent = any_dependent_template_arguments_p (targs);
20080 if (!dependent)
20081 --processing_template_decl;
20082 parm = tsubst (pattern, targs,
20083 explain_p ? tf_warning_or_error : tf_none,
20084 NULL_TREE);
20085 if (dependent)
20086 --processing_template_decl;
20087 if (parm == error_mark_node)
20088 return 1;
20090 else
20091 parm = pattern;
20093 /* Unify the pattern with the current argument. */
20094 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20095 explain_p))
20096 return 1;
20098 /* For each parameter pack, collect the deduced value. */
20099 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20101 int idx, level;
20102 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20104 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20105 TMPL_ARG (targs, level, idx);
20109 /* Verify that the results of unification with the parameter packs
20110 produce results consistent with what we've seen before, and make
20111 the deduced argument packs available. */
20112 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20114 tree old_pack = TREE_VALUE (pack);
20115 tree new_args = TREE_TYPE (pack);
20116 int i, len = TREE_VEC_LENGTH (new_args);
20117 int idx, level;
20118 bool nondeduced_p = false;
20120 /* By default keep the original deduced argument pack.
20121 If necessary, more specific code is going to update the
20122 resulting deduced argument later down in this function. */
20123 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20124 TMPL_ARG (targs, level, idx) = old_pack;
20126 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20127 actually deduce anything. */
20128 for (i = 0; i < len && !nondeduced_p; ++i)
20129 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20130 nondeduced_p = true;
20131 if (nondeduced_p)
20132 continue;
20134 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20136 /* If we had fewer function args than explicit template args,
20137 just use the explicits. */
20138 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20139 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20140 if (len < explicit_len)
20141 new_args = explicit_args;
20144 if (!old_pack)
20146 tree result;
20147 /* Build the deduced *_ARGUMENT_PACK. */
20148 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20150 result = make_node (NONTYPE_ARGUMENT_PACK);
20151 TREE_CONSTANT (result) = 1;
20153 else
20154 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20156 SET_ARGUMENT_PACK_ARGS (result, new_args);
20158 /* Note the deduced argument packs for this parameter
20159 pack. */
20160 TMPL_ARG (targs, level, idx) = result;
20162 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20163 && (ARGUMENT_PACK_ARGS (old_pack)
20164 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20166 /* We only had the explicitly-provided arguments before, but
20167 now we have a complete set of arguments. */
20168 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20170 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20171 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20172 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20174 else
20176 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20177 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20179 if (!comp_template_args (old_args, new_args,
20180 &bad_old_arg, &bad_new_arg))
20181 /* Inconsistent unification of this parameter pack. */
20182 return unify_parameter_pack_inconsistent (explain_p,
20183 bad_old_arg,
20184 bad_new_arg);
20188 return unify_success (explain_p);
20191 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20192 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20193 parameters and return value are as for unify. */
20195 static int
20196 unify_array_domain (tree tparms, tree targs,
20197 tree parm_dom, tree arg_dom,
20198 bool explain_p)
20200 tree parm_max;
20201 tree arg_max;
20202 bool parm_cst;
20203 bool arg_cst;
20205 /* Our representation of array types uses "N - 1" as the
20206 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20207 not an integer constant. We cannot unify arbitrarily
20208 complex expressions, so we eliminate the MINUS_EXPRs
20209 here. */
20210 parm_max = TYPE_MAX_VALUE (parm_dom);
20211 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20212 if (!parm_cst)
20214 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20215 parm_max = TREE_OPERAND (parm_max, 0);
20217 arg_max = TYPE_MAX_VALUE (arg_dom);
20218 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20219 if (!arg_cst)
20221 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20222 trying to unify the type of a variable with the type
20223 of a template parameter. For example:
20225 template <unsigned int N>
20226 void f (char (&) [N]);
20227 int g();
20228 void h(int i) {
20229 char a[g(i)];
20230 f(a);
20233 Here, the type of the ARG will be "int [g(i)]", and
20234 may be a SAVE_EXPR, etc. */
20235 if (TREE_CODE (arg_max) != MINUS_EXPR)
20236 return unify_vla_arg (explain_p, arg_dom);
20237 arg_max = TREE_OPERAND (arg_max, 0);
20240 /* If only one of the bounds used a MINUS_EXPR, compensate
20241 by adding one to the other bound. */
20242 if (parm_cst && !arg_cst)
20243 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20244 integer_type_node,
20245 parm_max,
20246 integer_one_node);
20247 else if (arg_cst && !parm_cst)
20248 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20249 integer_type_node,
20250 arg_max,
20251 integer_one_node);
20253 return unify (tparms, targs, parm_max, arg_max,
20254 UNIFY_ALLOW_INTEGER, explain_p);
20257 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20259 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20261 static pa_kind_t
20262 pa_kind (tree t)
20264 if (PACK_EXPANSION_P (t))
20265 t = PACK_EXPANSION_PATTERN (t);
20266 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20267 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20268 || DECL_TYPE_TEMPLATE_P (t))
20269 return pa_tmpl;
20270 else if (TYPE_P (t))
20271 return pa_type;
20272 else
20273 return pa_expr;
20276 /* Deduce the value of template parameters. TPARMS is the (innermost)
20277 set of template parameters to a template. TARGS is the bindings
20278 for those template parameters, as determined thus far; TARGS may
20279 include template arguments for outer levels of template parameters
20280 as well. PARM is a parameter to a template function, or a
20281 subcomponent of that parameter; ARG is the corresponding argument.
20282 This function attempts to match PARM with ARG in a manner
20283 consistent with the existing assignments in TARGS. If more values
20284 are deduced, then TARGS is updated.
20286 Returns 0 if the type deduction succeeds, 1 otherwise. The
20287 parameter STRICT is a bitwise or of the following flags:
20289 UNIFY_ALLOW_NONE:
20290 Require an exact match between PARM and ARG.
20291 UNIFY_ALLOW_MORE_CV_QUAL:
20292 Allow the deduced ARG to be more cv-qualified (by qualification
20293 conversion) than ARG.
20294 UNIFY_ALLOW_LESS_CV_QUAL:
20295 Allow the deduced ARG to be less cv-qualified than ARG.
20296 UNIFY_ALLOW_DERIVED:
20297 Allow the deduced ARG to be a template base class of ARG,
20298 or a pointer to a template base class of the type pointed to by
20299 ARG.
20300 UNIFY_ALLOW_INTEGER:
20301 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20302 case for more information.
20303 UNIFY_ALLOW_OUTER_LEVEL:
20304 This is the outermost level of a deduction. Used to determine validity
20305 of qualification conversions. A valid qualification conversion must
20306 have const qualified pointers leading up to the inner type which
20307 requires additional CV quals, except at the outer level, where const
20308 is not required [conv.qual]. It would be normal to set this flag in
20309 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20310 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20311 This is the outermost level of a deduction, and PARM can be more CV
20312 qualified at this point.
20313 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20314 This is the outermost level of a deduction, and PARM can be less CV
20315 qualified at this point. */
20317 static int
20318 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20319 bool explain_p)
20321 int idx;
20322 tree targ;
20323 tree tparm;
20324 int strict_in = strict;
20325 tsubst_flags_t complain = (explain_p
20326 ? tf_warning_or_error
20327 : tf_none);
20329 /* I don't think this will do the right thing with respect to types.
20330 But the only case I've seen it in so far has been array bounds, where
20331 signedness is the only information lost, and I think that will be
20332 okay. */
20333 while (CONVERT_EXPR_P (parm))
20334 parm = TREE_OPERAND (parm, 0);
20336 if (arg == error_mark_node)
20337 return unify_invalid (explain_p);
20338 if (arg == unknown_type_node
20339 || arg == init_list_type_node)
20340 /* We can't deduce anything from this, but we might get all the
20341 template args from other function args. */
20342 return unify_success (explain_p);
20344 if (parm == any_targ_node || arg == any_targ_node)
20345 return unify_success (explain_p);
20347 /* If PARM uses template parameters, then we can't bail out here,
20348 even if ARG == PARM, since we won't record unifications for the
20349 template parameters. We might need them if we're trying to
20350 figure out which of two things is more specialized. */
20351 if (arg == parm && !uses_template_parms (parm))
20352 return unify_success (explain_p);
20354 /* Handle init lists early, so the rest of the function can assume
20355 we're dealing with a type. */
20356 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20358 tree elt, elttype;
20359 unsigned i;
20360 tree orig_parm = parm;
20362 /* Replace T with std::initializer_list<T> for deduction. */
20363 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20364 && flag_deduce_init_list)
20365 parm = listify (parm);
20367 if (!is_std_init_list (parm)
20368 && TREE_CODE (parm) != ARRAY_TYPE)
20369 /* We can only deduce from an initializer list argument if the
20370 parameter is std::initializer_list or an array; otherwise this
20371 is a non-deduced context. */
20372 return unify_success (explain_p);
20374 if (TREE_CODE (parm) == ARRAY_TYPE)
20375 elttype = TREE_TYPE (parm);
20376 else
20378 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20379 /* Deduction is defined in terms of a single type, so just punt
20380 on the (bizarre) std::initializer_list<T...>. */
20381 if (PACK_EXPANSION_P (elttype))
20382 return unify_success (explain_p);
20385 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20387 int elt_strict = strict;
20389 if (elt == error_mark_node)
20390 return unify_invalid (explain_p);
20392 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20394 tree type = TREE_TYPE (elt);
20395 if (type == error_mark_node)
20396 return unify_invalid (explain_p);
20397 /* It should only be possible to get here for a call. */
20398 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20399 elt_strict |= maybe_adjust_types_for_deduction
20400 (DEDUCE_CALL, &elttype, &type, elt);
20401 elt = type;
20404 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20405 explain_p);
20408 if (TREE_CODE (parm) == ARRAY_TYPE
20409 && deducible_array_bound (TYPE_DOMAIN (parm)))
20411 /* Also deduce from the length of the initializer list. */
20412 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20413 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20414 if (idx == error_mark_node)
20415 return unify_invalid (explain_p);
20416 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20417 idx, explain_p);
20420 /* If the std::initializer_list<T> deduction worked, replace the
20421 deduced A with std::initializer_list<A>. */
20422 if (orig_parm != parm)
20424 idx = TEMPLATE_TYPE_IDX (orig_parm);
20425 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20426 targ = listify (targ);
20427 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20429 return unify_success (explain_p);
20432 /* If parm and arg aren't the same kind of thing (template, type, or
20433 expression), fail early. */
20434 if (pa_kind (parm) != pa_kind (arg))
20435 return unify_invalid (explain_p);
20437 /* Immediately reject some pairs that won't unify because of
20438 cv-qualification mismatches. */
20439 if (TREE_CODE (arg) == TREE_CODE (parm)
20440 && TYPE_P (arg)
20441 /* It is the elements of the array which hold the cv quals of an array
20442 type, and the elements might be template type parms. We'll check
20443 when we recurse. */
20444 && TREE_CODE (arg) != ARRAY_TYPE
20445 /* We check the cv-qualifiers when unifying with template type
20446 parameters below. We want to allow ARG `const T' to unify with
20447 PARM `T' for example, when computing which of two templates
20448 is more specialized, for example. */
20449 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20450 && !check_cv_quals_for_unify (strict_in, arg, parm))
20451 return unify_cv_qual_mismatch (explain_p, parm, arg);
20453 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20454 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20455 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20456 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20457 strict &= ~UNIFY_ALLOW_DERIVED;
20458 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20459 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20461 switch (TREE_CODE (parm))
20463 case TYPENAME_TYPE:
20464 case SCOPE_REF:
20465 case UNBOUND_CLASS_TEMPLATE:
20466 /* In a type which contains a nested-name-specifier, template
20467 argument values cannot be deduced for template parameters used
20468 within the nested-name-specifier. */
20469 return unify_success (explain_p);
20471 case TEMPLATE_TYPE_PARM:
20472 case TEMPLATE_TEMPLATE_PARM:
20473 case BOUND_TEMPLATE_TEMPLATE_PARM:
20474 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20475 if (error_operand_p (tparm))
20476 return unify_invalid (explain_p);
20478 if (TEMPLATE_TYPE_LEVEL (parm)
20479 != template_decl_level (tparm))
20480 /* The PARM is not one we're trying to unify. Just check
20481 to see if it matches ARG. */
20483 if (TREE_CODE (arg) == TREE_CODE (parm)
20484 && (is_auto (parm) ? is_auto (arg)
20485 : same_type_p (parm, arg)))
20486 return unify_success (explain_p);
20487 else
20488 return unify_type_mismatch (explain_p, parm, arg);
20490 idx = TEMPLATE_TYPE_IDX (parm);
20491 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20492 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20493 if (error_operand_p (tparm))
20494 return unify_invalid (explain_p);
20496 /* Check for mixed types and values. */
20497 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20498 && TREE_CODE (tparm) != TYPE_DECL)
20499 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20500 && TREE_CODE (tparm) != TEMPLATE_DECL))
20501 gcc_unreachable ();
20503 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20505 if ((strict_in & UNIFY_ALLOW_DERIVED)
20506 && CLASS_TYPE_P (arg))
20508 /* First try to match ARG directly. */
20509 tree t = try_class_unification (tparms, targs, parm, arg,
20510 explain_p);
20511 if (!t)
20513 /* Otherwise, look for a suitable base of ARG, as below. */
20514 enum template_base_result r;
20515 r = get_template_base (tparms, targs, parm, arg,
20516 explain_p, &t);
20517 if (!t)
20518 return unify_no_common_base (explain_p, r, parm, arg);
20519 arg = t;
20522 /* ARG must be constructed from a template class or a template
20523 template parameter. */
20524 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20525 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20526 return unify_template_deduction_failure (explain_p, parm, arg);
20528 /* Deduce arguments T, i from TT<T> or TT<i>. */
20529 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20530 return 1;
20532 arg = TYPE_TI_TEMPLATE (arg);
20534 /* Fall through to deduce template name. */
20537 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20538 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20540 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20542 /* Simple cases: Value already set, does match or doesn't. */
20543 if (targ != NULL_TREE && template_args_equal (targ, arg))
20544 return unify_success (explain_p);
20545 else if (targ)
20546 return unify_inconsistency (explain_p, parm, targ, arg);
20548 else
20550 /* If PARM is `const T' and ARG is only `int', we don't have
20551 a match unless we are allowing additional qualification.
20552 If ARG is `const int' and PARM is just `T' that's OK;
20553 that binds `const int' to `T'. */
20554 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20555 arg, parm))
20556 return unify_cv_qual_mismatch (explain_p, parm, arg);
20558 /* Consider the case where ARG is `const volatile int' and
20559 PARM is `const T'. Then, T should be `volatile int'. */
20560 arg = cp_build_qualified_type_real
20561 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20562 if (arg == error_mark_node)
20563 return unify_invalid (explain_p);
20565 /* Simple cases: Value already set, does match or doesn't. */
20566 if (targ != NULL_TREE && same_type_p (targ, arg))
20567 return unify_success (explain_p);
20568 else if (targ)
20569 return unify_inconsistency (explain_p, parm, targ, arg);
20571 /* Make sure that ARG is not a variable-sized array. (Note
20572 that were talking about variable-sized arrays (like
20573 `int[n]'), rather than arrays of unknown size (like
20574 `int[]').) We'll get very confused by such a type since
20575 the bound of the array is not constant, and therefore
20576 not mangleable. Besides, such types are not allowed in
20577 ISO C++, so we can do as we please here. We do allow
20578 them for 'auto' deduction, since that isn't ABI-exposed. */
20579 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20580 return unify_vla_arg (explain_p, arg);
20582 /* Strip typedefs as in convert_template_argument. */
20583 arg = canonicalize_type_argument (arg, tf_none);
20586 /* If ARG is a parameter pack or an expansion, we cannot unify
20587 against it unless PARM is also a parameter pack. */
20588 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20589 && !template_parameter_pack_p (parm))
20590 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20592 /* If the argument deduction results is a METHOD_TYPE,
20593 then there is a problem.
20594 METHOD_TYPE doesn't map to any real C++ type the result of
20595 the deduction can not be of that type. */
20596 if (TREE_CODE (arg) == METHOD_TYPE)
20597 return unify_method_type_error (explain_p, arg);
20599 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20600 return unify_success (explain_p);
20602 case TEMPLATE_PARM_INDEX:
20603 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20604 if (error_operand_p (tparm))
20605 return unify_invalid (explain_p);
20607 if (TEMPLATE_PARM_LEVEL (parm)
20608 != template_decl_level (tparm))
20610 /* The PARM is not one we're trying to unify. Just check
20611 to see if it matches ARG. */
20612 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20613 && cp_tree_equal (parm, arg));
20614 if (result)
20615 unify_expression_unequal (explain_p, parm, arg);
20616 return result;
20619 idx = TEMPLATE_PARM_IDX (parm);
20620 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20622 if (targ)
20624 int x = !cp_tree_equal (targ, arg);
20625 if (x)
20626 unify_inconsistency (explain_p, parm, targ, arg);
20627 return x;
20630 /* [temp.deduct.type] If, in the declaration of a function template
20631 with a non-type template-parameter, the non-type
20632 template-parameter is used in an expression in the function
20633 parameter-list and, if the corresponding template-argument is
20634 deduced, the template-argument type shall match the type of the
20635 template-parameter exactly, except that a template-argument
20636 deduced from an array bound may be of any integral type.
20637 The non-type parameter might use already deduced type parameters. */
20638 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20639 if (tree a = type_uses_auto (tparm))
20641 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20642 if (tparm == error_mark_node)
20643 return 1;
20646 if (!TREE_TYPE (arg))
20647 /* Template-parameter dependent expression. Just accept it for now.
20648 It will later be processed in convert_template_argument. */
20650 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20651 non_reference (tparm)))
20652 /* OK */;
20653 else if ((strict & UNIFY_ALLOW_INTEGER)
20654 && CP_INTEGRAL_TYPE_P (tparm))
20655 /* Convert the ARG to the type of PARM; the deduced non-type
20656 template argument must exactly match the types of the
20657 corresponding parameter. */
20658 arg = fold (build_nop (tparm, arg));
20659 else if (uses_template_parms (tparm))
20661 /* We haven't deduced the type of this parameter yet. */
20662 if (cxx_dialect >= cxx1z
20663 /* We deduce from array bounds in try_array_deduction. */
20664 && !(strict & UNIFY_ALLOW_INTEGER))
20666 /* Deduce it from the non-type argument. */
20667 tree atype = TREE_TYPE (arg);
20668 RECUR_AND_CHECK_FAILURE (tparms, targs,
20669 tparm, atype,
20670 UNIFY_ALLOW_NONE, explain_p);
20672 else
20673 /* Try again later. */
20674 return unify_success (explain_p);
20676 else
20677 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20679 /* If ARG is a parameter pack or an expansion, we cannot unify
20680 against it unless PARM is also a parameter pack. */
20681 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20682 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20683 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20686 bool removed_attr = false;
20687 arg = strip_typedefs_expr (arg, &removed_attr);
20689 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20690 return unify_success (explain_p);
20692 case PTRMEM_CST:
20694 /* A pointer-to-member constant can be unified only with
20695 another constant. */
20696 if (TREE_CODE (arg) != PTRMEM_CST)
20697 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20699 /* Just unify the class member. It would be useless (and possibly
20700 wrong, depending on the strict flags) to unify also
20701 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20702 arg refer to the same variable, even if through different
20703 classes. For instance:
20705 struct A { int x; };
20706 struct B : A { };
20708 Unification of &A::x and &B::x must succeed. */
20709 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20710 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20713 case POINTER_TYPE:
20715 if (!TYPE_PTR_P (arg))
20716 return unify_type_mismatch (explain_p, parm, arg);
20718 /* [temp.deduct.call]
20720 A can be another pointer or pointer to member type that can
20721 be converted to the deduced A via a qualification
20722 conversion (_conv.qual_).
20724 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20725 This will allow for additional cv-qualification of the
20726 pointed-to types if appropriate. */
20728 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20729 /* The derived-to-base conversion only persists through one
20730 level of pointers. */
20731 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20733 return unify (tparms, targs, TREE_TYPE (parm),
20734 TREE_TYPE (arg), strict, explain_p);
20737 case REFERENCE_TYPE:
20738 if (TREE_CODE (arg) != REFERENCE_TYPE)
20739 return unify_type_mismatch (explain_p, parm, arg);
20740 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20741 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20743 case ARRAY_TYPE:
20744 if (TREE_CODE (arg) != ARRAY_TYPE)
20745 return unify_type_mismatch (explain_p, parm, arg);
20746 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20747 != (TYPE_DOMAIN (arg) == NULL_TREE))
20748 return unify_type_mismatch (explain_p, parm, arg);
20749 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20750 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20751 if (TYPE_DOMAIN (parm) != NULL_TREE)
20752 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20753 TYPE_DOMAIN (arg), explain_p);
20754 return unify_success (explain_p);
20756 case REAL_TYPE:
20757 case COMPLEX_TYPE:
20758 case VECTOR_TYPE:
20759 case INTEGER_TYPE:
20760 case BOOLEAN_TYPE:
20761 case ENUMERAL_TYPE:
20762 case VOID_TYPE:
20763 case NULLPTR_TYPE:
20764 if (TREE_CODE (arg) != TREE_CODE (parm))
20765 return unify_type_mismatch (explain_p, parm, arg);
20767 /* We have already checked cv-qualification at the top of the
20768 function. */
20769 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20770 return unify_type_mismatch (explain_p, parm, arg);
20772 /* As far as unification is concerned, this wins. Later checks
20773 will invalidate it if necessary. */
20774 return unify_success (explain_p);
20776 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20777 /* Type INTEGER_CST can come from ordinary constant template args. */
20778 case INTEGER_CST:
20779 while (CONVERT_EXPR_P (arg))
20780 arg = TREE_OPERAND (arg, 0);
20782 if (TREE_CODE (arg) != INTEGER_CST)
20783 return unify_template_argument_mismatch (explain_p, parm, arg);
20784 return (tree_int_cst_equal (parm, arg)
20785 ? unify_success (explain_p)
20786 : unify_template_argument_mismatch (explain_p, parm, arg));
20788 case TREE_VEC:
20790 int i, len, argslen;
20791 int parm_variadic_p = 0;
20793 if (TREE_CODE (arg) != TREE_VEC)
20794 return unify_template_argument_mismatch (explain_p, parm, arg);
20796 len = TREE_VEC_LENGTH (parm);
20797 argslen = TREE_VEC_LENGTH (arg);
20799 /* Check for pack expansions in the parameters. */
20800 for (i = 0; i < len; ++i)
20802 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20804 if (i == len - 1)
20805 /* We can unify against something with a trailing
20806 parameter pack. */
20807 parm_variadic_p = 1;
20808 else
20809 /* [temp.deduct.type]/9: If the template argument list of
20810 P contains a pack expansion that is not the last
20811 template argument, the entire template argument list
20812 is a non-deduced context. */
20813 return unify_success (explain_p);
20817 /* If we don't have enough arguments to satisfy the parameters
20818 (not counting the pack expression at the end), or we have
20819 too many arguments for a parameter list that doesn't end in
20820 a pack expression, we can't unify. */
20821 if (parm_variadic_p
20822 ? argslen < len - parm_variadic_p
20823 : argslen != len)
20824 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20826 /* Unify all of the parameters that precede the (optional)
20827 pack expression. */
20828 for (i = 0; i < len - parm_variadic_p; ++i)
20830 RECUR_AND_CHECK_FAILURE (tparms, targs,
20831 TREE_VEC_ELT (parm, i),
20832 TREE_VEC_ELT (arg, i),
20833 UNIFY_ALLOW_NONE, explain_p);
20835 if (parm_variadic_p)
20836 return unify_pack_expansion (tparms, targs, parm, arg,
20837 DEDUCE_EXACT,
20838 /*subr=*/true, explain_p);
20839 return unify_success (explain_p);
20842 case RECORD_TYPE:
20843 case UNION_TYPE:
20844 if (TREE_CODE (arg) != TREE_CODE (parm))
20845 return unify_type_mismatch (explain_p, parm, arg);
20847 if (TYPE_PTRMEMFUNC_P (parm))
20849 if (!TYPE_PTRMEMFUNC_P (arg))
20850 return unify_type_mismatch (explain_p, parm, arg);
20852 return unify (tparms, targs,
20853 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20854 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20855 strict, explain_p);
20857 else if (TYPE_PTRMEMFUNC_P (arg))
20858 return unify_type_mismatch (explain_p, parm, arg);
20860 if (CLASSTYPE_TEMPLATE_INFO (parm))
20862 tree t = NULL_TREE;
20864 if (strict_in & UNIFY_ALLOW_DERIVED)
20866 /* First, we try to unify the PARM and ARG directly. */
20867 t = try_class_unification (tparms, targs,
20868 parm, arg, explain_p);
20870 if (!t)
20872 /* Fallback to the special case allowed in
20873 [temp.deduct.call]:
20875 If P is a class, and P has the form
20876 template-id, then A can be a derived class of
20877 the deduced A. Likewise, if P is a pointer to
20878 a class of the form template-id, A can be a
20879 pointer to a derived class pointed to by the
20880 deduced A. */
20881 enum template_base_result r;
20882 r = get_template_base (tparms, targs, parm, arg,
20883 explain_p, &t);
20885 if (!t)
20887 /* Don't give the derived diagnostic if we're
20888 already dealing with the same template. */
20889 bool same_template
20890 = (CLASSTYPE_TEMPLATE_INFO (arg)
20891 && (CLASSTYPE_TI_TEMPLATE (parm)
20892 == CLASSTYPE_TI_TEMPLATE (arg)));
20893 return unify_no_common_base (explain_p && !same_template,
20894 r, parm, arg);
20898 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20899 && (CLASSTYPE_TI_TEMPLATE (parm)
20900 == CLASSTYPE_TI_TEMPLATE (arg)))
20901 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20902 Then, we should unify `int' and `U'. */
20903 t = arg;
20904 else
20905 /* There's no chance of unification succeeding. */
20906 return unify_type_mismatch (explain_p, parm, arg);
20908 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20909 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20911 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20912 return unify_type_mismatch (explain_p, parm, arg);
20913 return unify_success (explain_p);
20915 case METHOD_TYPE:
20916 case FUNCTION_TYPE:
20918 unsigned int nargs;
20919 tree *args;
20920 tree a;
20921 unsigned int i;
20923 if (TREE_CODE (arg) != TREE_CODE (parm))
20924 return unify_type_mismatch (explain_p, parm, arg);
20926 /* CV qualifications for methods can never be deduced, they must
20927 match exactly. We need to check them explicitly here,
20928 because type_unification_real treats them as any other
20929 cv-qualified parameter. */
20930 if (TREE_CODE (parm) == METHOD_TYPE
20931 && (!check_cv_quals_for_unify
20932 (UNIFY_ALLOW_NONE,
20933 class_of_this_parm (arg),
20934 class_of_this_parm (parm))))
20935 return unify_cv_qual_mismatch (explain_p, parm, arg);
20936 if (TREE_CODE (arg) == FUNCTION_TYPE
20937 && type_memfn_quals (parm) != type_memfn_quals (arg))
20938 return unify_cv_qual_mismatch (explain_p, parm, arg);
20939 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20940 return unify_type_mismatch (explain_p, parm, arg);
20942 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20943 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20945 nargs = list_length (TYPE_ARG_TYPES (arg));
20946 args = XALLOCAVEC (tree, nargs);
20947 for (a = TYPE_ARG_TYPES (arg), i = 0;
20948 a != NULL_TREE && a != void_list_node;
20949 a = TREE_CHAIN (a), ++i)
20950 args[i] = TREE_VALUE (a);
20951 nargs = i;
20953 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20954 args, nargs, 1, DEDUCE_EXACT,
20955 LOOKUP_NORMAL, NULL, explain_p))
20956 return 1;
20958 if (flag_noexcept_type)
20960 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20961 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20962 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20963 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20964 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20965 && uses_template_parms (TREE_PURPOSE (pspec)))
20966 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20967 TREE_PURPOSE (aspec),
20968 UNIFY_ALLOW_NONE, explain_p);
20969 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20970 return unify_type_mismatch (explain_p, parm, arg);
20973 return 0;
20976 case OFFSET_TYPE:
20977 /* Unify a pointer to member with a pointer to member function, which
20978 deduces the type of the member as a function type. */
20979 if (TYPE_PTRMEMFUNC_P (arg))
20981 /* Check top-level cv qualifiers */
20982 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20983 return unify_cv_qual_mismatch (explain_p, parm, arg);
20985 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20986 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20987 UNIFY_ALLOW_NONE, explain_p);
20989 /* Determine the type of the function we are unifying against. */
20990 tree fntype = static_fn_type (arg);
20992 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20995 if (TREE_CODE (arg) != OFFSET_TYPE)
20996 return unify_type_mismatch (explain_p, parm, arg);
20997 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20998 TYPE_OFFSET_BASETYPE (arg),
20999 UNIFY_ALLOW_NONE, explain_p);
21000 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21001 strict, explain_p);
21003 case CONST_DECL:
21004 if (DECL_TEMPLATE_PARM_P (parm))
21005 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21006 if (arg != scalar_constant_value (parm))
21007 return unify_template_argument_mismatch (explain_p, parm, arg);
21008 return unify_success (explain_p);
21010 case FIELD_DECL:
21011 case TEMPLATE_DECL:
21012 /* Matched cases are handled by the ARG == PARM test above. */
21013 return unify_template_argument_mismatch (explain_p, parm, arg);
21015 case VAR_DECL:
21016 /* We might get a variable as a non-type template argument in parm if the
21017 corresponding parameter is type-dependent. Make any necessary
21018 adjustments based on whether arg is a reference. */
21019 if (CONSTANT_CLASS_P (arg))
21020 parm = fold_non_dependent_expr (parm);
21021 else if (REFERENCE_REF_P (arg))
21023 tree sub = TREE_OPERAND (arg, 0);
21024 STRIP_NOPS (sub);
21025 if (TREE_CODE (sub) == ADDR_EXPR)
21026 arg = TREE_OPERAND (sub, 0);
21028 /* Now use the normal expression code to check whether they match. */
21029 goto expr;
21031 case TYPE_ARGUMENT_PACK:
21032 case NONTYPE_ARGUMENT_PACK:
21033 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21034 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21036 case TYPEOF_TYPE:
21037 case DECLTYPE_TYPE:
21038 case UNDERLYING_TYPE:
21039 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21040 or UNDERLYING_TYPE nodes. */
21041 return unify_success (explain_p);
21043 case ERROR_MARK:
21044 /* Unification fails if we hit an error node. */
21045 return unify_invalid (explain_p);
21047 case INDIRECT_REF:
21048 if (REFERENCE_REF_P (parm))
21050 bool pexp = PACK_EXPANSION_P (arg);
21051 if (pexp)
21052 arg = PACK_EXPANSION_PATTERN (arg);
21053 if (REFERENCE_REF_P (arg))
21054 arg = TREE_OPERAND (arg, 0);
21055 if (pexp)
21056 arg = make_pack_expansion (arg);
21057 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21058 strict, explain_p);
21060 /* FALLTHRU */
21062 default:
21063 /* An unresolved overload is a nondeduced context. */
21064 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21065 return unify_success (explain_p);
21066 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21067 expr:
21068 /* We must be looking at an expression. This can happen with
21069 something like:
21071 template <int I>
21072 void foo(S<I>, S<I + 2>);
21074 This is a "nondeduced context":
21076 [deduct.type]
21078 The nondeduced contexts are:
21080 --A type that is a template-id in which one or more of
21081 the template-arguments is an expression that references
21082 a template-parameter.
21084 In these cases, we assume deduction succeeded, but don't
21085 actually infer any unifications. */
21087 if (!uses_template_parms (parm)
21088 && !template_args_equal (parm, arg))
21089 return unify_expression_unequal (explain_p, parm, arg);
21090 else
21091 return unify_success (explain_p);
21094 #undef RECUR_AND_CHECK_FAILURE
21096 /* Note that DECL can be defined in this translation unit, if
21097 required. */
21099 static void
21100 mark_definable (tree decl)
21102 tree clone;
21103 DECL_NOT_REALLY_EXTERN (decl) = 1;
21104 FOR_EACH_CLONE (clone, decl)
21105 DECL_NOT_REALLY_EXTERN (clone) = 1;
21108 /* Called if RESULT is explicitly instantiated, or is a member of an
21109 explicitly instantiated class. */
21111 void
21112 mark_decl_instantiated (tree result, int extern_p)
21114 SET_DECL_EXPLICIT_INSTANTIATION (result);
21116 /* If this entity has already been written out, it's too late to
21117 make any modifications. */
21118 if (TREE_ASM_WRITTEN (result))
21119 return;
21121 /* For anonymous namespace we don't need to do anything. */
21122 if (decl_anon_ns_mem_p (result))
21124 gcc_assert (!TREE_PUBLIC (result));
21125 return;
21128 if (TREE_CODE (result) != FUNCTION_DECL)
21129 /* The TREE_PUBLIC flag for function declarations will have been
21130 set correctly by tsubst. */
21131 TREE_PUBLIC (result) = 1;
21133 /* This might have been set by an earlier implicit instantiation. */
21134 DECL_COMDAT (result) = 0;
21136 if (extern_p)
21137 DECL_NOT_REALLY_EXTERN (result) = 0;
21138 else
21140 mark_definable (result);
21141 mark_needed (result);
21142 /* Always make artificials weak. */
21143 if (DECL_ARTIFICIAL (result) && flag_weak)
21144 comdat_linkage (result);
21145 /* For WIN32 we also want to put explicit instantiations in
21146 linkonce sections. */
21147 else if (TREE_PUBLIC (result))
21148 maybe_make_one_only (result);
21151 /* If EXTERN_P, then this function will not be emitted -- unless
21152 followed by an explicit instantiation, at which point its linkage
21153 will be adjusted. If !EXTERN_P, then this function will be
21154 emitted here. In neither circumstance do we want
21155 import_export_decl to adjust the linkage. */
21156 DECL_INTERFACE_KNOWN (result) = 1;
21159 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21160 important template arguments. If any are missing, we check whether
21161 they're important by using error_mark_node for substituting into any
21162 args that were used for partial ordering (the ones between ARGS and END)
21163 and seeing if it bubbles up. */
21165 static bool
21166 check_undeduced_parms (tree targs, tree args, tree end)
21168 bool found = false;
21169 int i;
21170 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21171 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21173 found = true;
21174 TREE_VEC_ELT (targs, i) = error_mark_node;
21176 if (found)
21178 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21179 if (substed == error_mark_node)
21180 return true;
21182 return false;
21185 /* Given two function templates PAT1 and PAT2, return:
21187 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21188 -1 if PAT2 is more specialized than PAT1.
21189 0 if neither is more specialized.
21191 LEN indicates the number of parameters we should consider
21192 (defaulted parameters should not be considered).
21194 The 1998 std underspecified function template partial ordering, and
21195 DR214 addresses the issue. We take pairs of arguments, one from
21196 each of the templates, and deduce them against each other. One of
21197 the templates will be more specialized if all the *other*
21198 template's arguments deduce against its arguments and at least one
21199 of its arguments *does* *not* deduce against the other template's
21200 corresponding argument. Deduction is done as for class templates.
21201 The arguments used in deduction have reference and top level cv
21202 qualifiers removed. Iff both arguments were originally reference
21203 types *and* deduction succeeds in both directions, an lvalue reference
21204 wins against an rvalue reference and otherwise the template
21205 with the more cv-qualified argument wins for that pairing (if
21206 neither is more cv-qualified, they both are equal). Unlike regular
21207 deduction, after all the arguments have been deduced in this way,
21208 we do *not* verify the deduced template argument values can be
21209 substituted into non-deduced contexts.
21211 The logic can be a bit confusing here, because we look at deduce1 and
21212 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21213 can find template arguments for pat1 to make arg1 look like arg2, that
21214 means that arg2 is at least as specialized as arg1. */
21217 more_specialized_fn (tree pat1, tree pat2, int len)
21219 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21220 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21221 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21222 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21223 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21224 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21225 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21226 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21227 tree origs1, origs2;
21228 bool lose1 = false;
21229 bool lose2 = false;
21231 /* Remove the this parameter from non-static member functions. If
21232 one is a non-static member function and the other is not a static
21233 member function, remove the first parameter from that function
21234 also. This situation occurs for operator functions where we
21235 locate both a member function (with this pointer) and non-member
21236 operator (with explicit first operand). */
21237 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21239 len--; /* LEN is the number of significant arguments for DECL1 */
21240 args1 = TREE_CHAIN (args1);
21241 if (!DECL_STATIC_FUNCTION_P (decl2))
21242 args2 = TREE_CHAIN (args2);
21244 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21246 args2 = TREE_CHAIN (args2);
21247 if (!DECL_STATIC_FUNCTION_P (decl1))
21249 len--;
21250 args1 = TREE_CHAIN (args1);
21254 /* If only one is a conversion operator, they are unordered. */
21255 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21256 return 0;
21258 /* Consider the return type for a conversion function */
21259 if (DECL_CONV_FN_P (decl1))
21261 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21262 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21263 len++;
21266 processing_template_decl++;
21268 origs1 = args1;
21269 origs2 = args2;
21271 while (len--
21272 /* Stop when an ellipsis is seen. */
21273 && args1 != NULL_TREE && args2 != NULL_TREE)
21275 tree arg1 = TREE_VALUE (args1);
21276 tree arg2 = TREE_VALUE (args2);
21277 int deduce1, deduce2;
21278 int quals1 = -1;
21279 int quals2 = -1;
21280 int ref1 = 0;
21281 int ref2 = 0;
21283 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21284 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21286 /* When both arguments are pack expansions, we need only
21287 unify the patterns themselves. */
21288 arg1 = PACK_EXPANSION_PATTERN (arg1);
21289 arg2 = PACK_EXPANSION_PATTERN (arg2);
21291 /* This is the last comparison we need to do. */
21292 len = 0;
21295 /* DR 1847: If a particular P contains no template-parameters that
21296 participate in template argument deduction, that P is not used to
21297 determine the ordering. */
21298 if (!uses_deducible_template_parms (arg1)
21299 && !uses_deducible_template_parms (arg2))
21300 goto next;
21302 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21304 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21305 arg1 = TREE_TYPE (arg1);
21306 quals1 = cp_type_quals (arg1);
21309 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21311 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21312 arg2 = TREE_TYPE (arg2);
21313 quals2 = cp_type_quals (arg2);
21316 arg1 = TYPE_MAIN_VARIANT (arg1);
21317 arg2 = TYPE_MAIN_VARIANT (arg2);
21319 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21321 int i, len2 = remaining_arguments (args2);
21322 tree parmvec = make_tree_vec (1);
21323 tree argvec = make_tree_vec (len2);
21324 tree ta = args2;
21326 /* Setup the parameter vector, which contains only ARG1. */
21327 TREE_VEC_ELT (parmvec, 0) = arg1;
21329 /* Setup the argument vector, which contains the remaining
21330 arguments. */
21331 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21332 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21334 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21335 argvec, DEDUCE_EXACT,
21336 /*subr=*/true, /*explain_p=*/false)
21337 == 0);
21339 /* We cannot deduce in the other direction, because ARG1 is
21340 a pack expansion but ARG2 is not. */
21341 deduce2 = 0;
21343 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21345 int i, len1 = remaining_arguments (args1);
21346 tree parmvec = make_tree_vec (1);
21347 tree argvec = make_tree_vec (len1);
21348 tree ta = args1;
21350 /* Setup the parameter vector, which contains only ARG1. */
21351 TREE_VEC_ELT (parmvec, 0) = arg2;
21353 /* Setup the argument vector, which contains the remaining
21354 arguments. */
21355 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21356 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21358 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21359 argvec, DEDUCE_EXACT,
21360 /*subr=*/true, /*explain_p=*/false)
21361 == 0);
21363 /* We cannot deduce in the other direction, because ARG2 is
21364 a pack expansion but ARG1 is not.*/
21365 deduce1 = 0;
21368 else
21370 /* The normal case, where neither argument is a pack
21371 expansion. */
21372 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21373 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21374 == 0);
21375 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21376 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21377 == 0);
21380 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21381 arg2, then arg2 is not as specialized as arg1. */
21382 if (!deduce1)
21383 lose2 = true;
21384 if (!deduce2)
21385 lose1 = true;
21387 /* "If, for a given type, deduction succeeds in both directions
21388 (i.e., the types are identical after the transformations above)
21389 and both P and A were reference types (before being replaced with
21390 the type referred to above):
21391 - if the type from the argument template was an lvalue reference and
21392 the type from the parameter template was not, the argument type is
21393 considered to be more specialized than the other; otherwise,
21394 - if the type from the argument template is more cv-qualified
21395 than the type from the parameter template (as described above),
21396 the argument type is considered to be more specialized than the other;
21397 otherwise,
21398 - neither type is more specialized than the other." */
21400 if (deduce1 && deduce2)
21402 if (ref1 && ref2 && ref1 != ref2)
21404 if (ref1 > ref2)
21405 lose1 = true;
21406 else
21407 lose2 = true;
21409 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21411 if ((quals1 & quals2) == quals2)
21412 lose2 = true;
21413 if ((quals1 & quals2) == quals1)
21414 lose1 = true;
21418 if (lose1 && lose2)
21419 /* We've failed to deduce something in either direction.
21420 These must be unordered. */
21421 break;
21423 next:
21425 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21426 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21427 /* We have already processed all of the arguments in our
21428 handing of the pack expansion type. */
21429 len = 0;
21431 args1 = TREE_CHAIN (args1);
21432 args2 = TREE_CHAIN (args2);
21435 /* "In most cases, all template parameters must have values in order for
21436 deduction to succeed, but for partial ordering purposes a template
21437 parameter may remain without a value provided it is not used in the
21438 types being used for partial ordering."
21440 Thus, if we are missing any of the targs1 we need to substitute into
21441 origs1, then pat2 is not as specialized as pat1. This can happen when
21442 there is a nondeduced context. */
21443 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21444 lose2 = true;
21445 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21446 lose1 = true;
21448 processing_template_decl--;
21450 /* If both deductions succeed, the partial ordering selects the more
21451 constrained template. */
21452 if (!lose1 && !lose2)
21454 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21455 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21456 lose1 = !subsumes_constraints (c1, c2);
21457 lose2 = !subsumes_constraints (c2, c1);
21460 /* All things being equal, if the next argument is a pack expansion
21461 for one function but not for the other, prefer the
21462 non-variadic function. FIXME this is bogus; see c++/41958. */
21463 if (lose1 == lose2
21464 && args1 && TREE_VALUE (args1)
21465 && args2 && TREE_VALUE (args2))
21467 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21468 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21471 if (lose1 == lose2)
21472 return 0;
21473 else if (!lose1)
21474 return 1;
21475 else
21476 return -1;
21479 /* Determine which of two partial specializations of TMPL is more
21480 specialized.
21482 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21483 to the first partial specialization. The TREE_PURPOSE is the
21484 innermost set of template parameters for the partial
21485 specialization. PAT2 is similar, but for the second template.
21487 Return 1 if the first partial specialization is more specialized;
21488 -1 if the second is more specialized; 0 if neither is more
21489 specialized.
21491 See [temp.class.order] for information about determining which of
21492 two templates is more specialized. */
21494 static int
21495 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21497 tree targs;
21498 int winner = 0;
21499 bool any_deductions = false;
21501 tree tmpl1 = TREE_VALUE (pat1);
21502 tree tmpl2 = TREE_VALUE (pat2);
21503 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21504 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21506 /* Just like what happens for functions, if we are ordering between
21507 different template specializations, we may encounter dependent
21508 types in the arguments, and we need our dependency check functions
21509 to behave correctly. */
21510 ++processing_template_decl;
21511 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21512 if (targs)
21514 --winner;
21515 any_deductions = true;
21518 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21519 if (targs)
21521 ++winner;
21522 any_deductions = true;
21524 --processing_template_decl;
21526 /* If both deductions succeed, the partial ordering selects the more
21527 constrained template. */
21528 if (!winner && any_deductions)
21529 return more_constrained (tmpl1, tmpl2);
21531 /* In the case of a tie where at least one of the templates
21532 has a parameter pack at the end, the template with the most
21533 non-packed parameters wins. */
21534 if (winner == 0
21535 && any_deductions
21536 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21537 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21539 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21540 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21541 int len1 = TREE_VEC_LENGTH (args1);
21542 int len2 = TREE_VEC_LENGTH (args2);
21544 /* We don't count the pack expansion at the end. */
21545 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21546 --len1;
21547 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21548 --len2;
21550 if (len1 > len2)
21551 return 1;
21552 else if (len1 < len2)
21553 return -1;
21556 return winner;
21559 /* Return the template arguments that will produce the function signature
21560 DECL from the function template FN, with the explicit template
21561 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21562 also match. Return NULL_TREE if no satisfactory arguments could be
21563 found. */
21565 static tree
21566 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21568 int ntparms = DECL_NTPARMS (fn);
21569 tree targs = make_tree_vec (ntparms);
21570 tree decl_type = TREE_TYPE (decl);
21571 tree decl_arg_types;
21572 tree *args;
21573 unsigned int nargs, ix;
21574 tree arg;
21576 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21578 /* Never do unification on the 'this' parameter. */
21579 decl_arg_types = skip_artificial_parms_for (decl,
21580 TYPE_ARG_TYPES (decl_type));
21582 nargs = list_length (decl_arg_types);
21583 args = XALLOCAVEC (tree, nargs);
21584 for (arg = decl_arg_types, ix = 0;
21585 arg != NULL_TREE && arg != void_list_node;
21586 arg = TREE_CHAIN (arg), ++ix)
21587 args[ix] = TREE_VALUE (arg);
21589 if (fn_type_unification (fn, explicit_args, targs,
21590 args, ix,
21591 (check_rettype || DECL_CONV_FN_P (fn)
21592 ? TREE_TYPE (decl_type) : NULL_TREE),
21593 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21594 /*decltype*/false)
21595 == error_mark_node)
21596 return NULL_TREE;
21598 return targs;
21601 /* Return the innermost template arguments that, when applied to a partial
21602 specialization SPEC_TMPL of TMPL, yield the ARGS.
21604 For example, suppose we have:
21606 template <class T, class U> struct S {};
21607 template <class T> struct S<T*, int> {};
21609 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21610 partial specialization and the ARGS will be {double*, int}. The resulting
21611 vector will be {double}, indicating that `T' is bound to `double'. */
21613 static tree
21614 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21616 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21617 tree spec_args
21618 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21619 int i, ntparms = TREE_VEC_LENGTH (tparms);
21620 tree deduced_args;
21621 tree innermost_deduced_args;
21623 innermost_deduced_args = make_tree_vec (ntparms);
21624 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21626 deduced_args = copy_node (args);
21627 SET_TMPL_ARGS_LEVEL (deduced_args,
21628 TMPL_ARGS_DEPTH (deduced_args),
21629 innermost_deduced_args);
21631 else
21632 deduced_args = innermost_deduced_args;
21634 bool tried_array_deduction = (cxx_dialect < cxx1z);
21635 again:
21636 if (unify (tparms, deduced_args,
21637 INNERMOST_TEMPLATE_ARGS (spec_args),
21638 INNERMOST_TEMPLATE_ARGS (args),
21639 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21640 return NULL_TREE;
21642 for (i = 0; i < ntparms; ++i)
21643 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21645 if (!tried_array_deduction)
21647 try_array_deduction (tparms, innermost_deduced_args,
21648 INNERMOST_TEMPLATE_ARGS (spec_args));
21649 tried_array_deduction = true;
21650 if (TREE_VEC_ELT (innermost_deduced_args, i))
21651 goto again;
21653 return NULL_TREE;
21656 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21657 if (!push_tinst_level (tinst))
21659 excessive_deduction_depth = true;
21660 return NULL_TREE;
21663 /* Verify that nondeduced template arguments agree with the type
21664 obtained from argument deduction.
21666 For example:
21668 struct A { typedef int X; };
21669 template <class T, class U> struct C {};
21670 template <class T> struct C<T, typename T::X> {};
21672 Then with the instantiation `C<A, int>', we can deduce that
21673 `T' is `A' but unify () does not check whether `typename T::X'
21674 is `int'. */
21675 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21677 if (spec_args != error_mark_node)
21678 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21679 INNERMOST_TEMPLATE_ARGS (spec_args),
21680 tmpl, tf_none, false, false);
21682 pop_tinst_level ();
21684 if (spec_args == error_mark_node
21685 /* We only need to check the innermost arguments; the other
21686 arguments will always agree. */
21687 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21688 INNERMOST_TEMPLATE_ARGS (args)))
21689 return NULL_TREE;
21691 /* Now that we have bindings for all of the template arguments,
21692 ensure that the arguments deduced for the template template
21693 parameters have compatible template parameter lists. See the use
21694 of template_template_parm_bindings_ok_p in fn_type_unification
21695 for more information. */
21696 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21697 return NULL_TREE;
21699 return deduced_args;
21702 // Compare two function templates T1 and T2 by deducing bindings
21703 // from one against the other. If both deductions succeed, compare
21704 // constraints to see which is more constrained.
21705 static int
21706 more_specialized_inst (tree t1, tree t2)
21708 int fate = 0;
21709 int count = 0;
21711 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21713 --fate;
21714 ++count;
21717 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21719 ++fate;
21720 ++count;
21723 // If both deductions succeed, then one may be more constrained.
21724 if (count == 2 && fate == 0)
21725 fate = more_constrained (t1, t2);
21727 return fate;
21730 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21731 Return the TREE_LIST node with the most specialized template, if
21732 any. If there is no most specialized template, the error_mark_node
21733 is returned.
21735 Note that this function does not look at, or modify, the
21736 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21737 returned is one of the elements of INSTANTIATIONS, callers may
21738 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21739 and retrieve it from the value returned. */
21741 tree
21742 most_specialized_instantiation (tree templates)
21744 tree fn, champ;
21746 ++processing_template_decl;
21748 champ = templates;
21749 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21751 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21752 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21753 if (fate == -1)
21754 champ = fn;
21755 else if (!fate)
21757 /* Equally specialized, move to next function. If there
21758 is no next function, nothing's most specialized. */
21759 fn = TREE_CHAIN (fn);
21760 champ = fn;
21761 if (!fn)
21762 break;
21766 if (champ)
21767 /* Now verify that champ is better than everything earlier in the
21768 instantiation list. */
21769 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21770 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21772 champ = NULL_TREE;
21773 break;
21777 processing_template_decl--;
21779 if (!champ)
21780 return error_mark_node;
21782 return champ;
21785 /* If DECL is a specialization of some template, return the most
21786 general such template. Otherwise, returns NULL_TREE.
21788 For example, given:
21790 template <class T> struct S { template <class U> void f(U); };
21792 if TMPL is `template <class U> void S<int>::f(U)' this will return
21793 the full template. This function will not trace past partial
21794 specializations, however. For example, given in addition:
21796 template <class T> struct S<T*> { template <class U> void f(U); };
21798 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21799 `template <class T> template <class U> S<T*>::f(U)'. */
21801 tree
21802 most_general_template (tree decl)
21804 if (TREE_CODE (decl) != TEMPLATE_DECL)
21806 if (tree tinfo = get_template_info (decl))
21807 decl = TI_TEMPLATE (tinfo);
21808 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21809 template friend, or a FIELD_DECL for a capture pack. */
21810 if (TREE_CODE (decl) != TEMPLATE_DECL)
21811 return NULL_TREE;
21814 /* Look for more and more general templates. */
21815 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21817 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21818 (See cp-tree.h for details.) */
21819 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21820 break;
21822 if (CLASS_TYPE_P (TREE_TYPE (decl))
21823 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21824 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21825 break;
21827 /* Stop if we run into an explicitly specialized class template. */
21828 if (!DECL_NAMESPACE_SCOPE_P (decl)
21829 && DECL_CONTEXT (decl)
21830 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21831 break;
21833 decl = DECL_TI_TEMPLATE (decl);
21836 return decl;
21839 /* Return the most specialized of the template partial specializations
21840 which can produce TARGET, a specialization of some class or variable
21841 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21842 a TEMPLATE_DECL node corresponding to the partial specialization, while
21843 the TREE_PURPOSE is the set of template arguments that must be
21844 substituted into the template pattern in order to generate TARGET.
21846 If the choice of partial specialization is ambiguous, a diagnostic
21847 is issued, and the error_mark_node is returned. If there are no
21848 partial specializations matching TARGET, then NULL_TREE is
21849 returned, indicating that the primary template should be used. */
21851 static tree
21852 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21854 tree list = NULL_TREE;
21855 tree t;
21856 tree champ;
21857 int fate;
21858 bool ambiguous_p;
21859 tree outer_args = NULL_TREE;
21860 tree tmpl, args;
21862 if (TYPE_P (target))
21864 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21865 tmpl = TI_TEMPLATE (tinfo);
21866 args = TI_ARGS (tinfo);
21868 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21870 tmpl = TREE_OPERAND (target, 0);
21871 args = TREE_OPERAND (target, 1);
21873 else if (VAR_P (target))
21875 tree tinfo = DECL_TEMPLATE_INFO (target);
21876 tmpl = TI_TEMPLATE (tinfo);
21877 args = TI_ARGS (tinfo);
21879 else
21880 gcc_unreachable ();
21882 tree main_tmpl = most_general_template (tmpl);
21884 /* For determining which partial specialization to use, only the
21885 innermost args are interesting. */
21886 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21888 outer_args = strip_innermost_template_args (args, 1);
21889 args = INNERMOST_TEMPLATE_ARGS (args);
21892 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21894 tree spec_args;
21895 tree spec_tmpl = TREE_VALUE (t);
21897 if (outer_args)
21899 /* Substitute in the template args from the enclosing class. */
21900 ++processing_template_decl;
21901 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21902 --processing_template_decl;
21905 if (spec_tmpl == error_mark_node)
21906 return error_mark_node;
21908 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21909 if (spec_args)
21911 if (outer_args)
21912 spec_args = add_to_template_args (outer_args, spec_args);
21914 /* Keep the candidate only if the constraints are satisfied,
21915 or if we're not compiling with concepts. */
21916 if (!flag_concepts
21917 || constraints_satisfied_p (spec_tmpl, spec_args))
21919 list = tree_cons (spec_args, TREE_VALUE (t), list);
21920 TREE_TYPE (list) = TREE_TYPE (t);
21925 if (! list)
21926 return NULL_TREE;
21928 ambiguous_p = false;
21929 t = list;
21930 champ = t;
21931 t = TREE_CHAIN (t);
21932 for (; t; t = TREE_CHAIN (t))
21934 fate = more_specialized_partial_spec (tmpl, champ, t);
21935 if (fate == 1)
21937 else
21939 if (fate == 0)
21941 t = TREE_CHAIN (t);
21942 if (! t)
21944 ambiguous_p = true;
21945 break;
21948 champ = t;
21952 if (!ambiguous_p)
21953 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21955 fate = more_specialized_partial_spec (tmpl, champ, t);
21956 if (fate != 1)
21958 ambiguous_p = true;
21959 break;
21963 if (ambiguous_p)
21965 const char *str;
21966 char *spaces = NULL;
21967 if (!(complain & tf_error))
21968 return error_mark_node;
21969 if (TYPE_P (target))
21970 error ("ambiguous template instantiation for %q#T", target);
21971 else
21972 error ("ambiguous template instantiation for %q#D", target);
21973 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21974 for (t = list; t; t = TREE_CHAIN (t))
21976 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21977 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21978 "%s %#qS", spaces ? spaces : str, subst);
21979 spaces = spaces ? spaces : get_spaces (str);
21981 free (spaces);
21982 return error_mark_node;
21985 return champ;
21988 /* Explicitly instantiate DECL. */
21990 void
21991 do_decl_instantiation (tree decl, tree storage)
21993 tree result = NULL_TREE;
21994 int extern_p = 0;
21996 if (!decl || decl == error_mark_node)
21997 /* An error occurred, for which grokdeclarator has already issued
21998 an appropriate message. */
21999 return;
22000 else if (! DECL_LANG_SPECIFIC (decl))
22002 error ("explicit instantiation of non-template %q#D", decl);
22003 return;
22006 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22007 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22009 if (VAR_P (decl) && !var_templ)
22011 /* There is an asymmetry here in the way VAR_DECLs and
22012 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22013 the latter, the DECL we get back will be marked as a
22014 template instantiation, and the appropriate
22015 DECL_TEMPLATE_INFO will be set up. This does not happen for
22016 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22017 should handle VAR_DECLs as it currently handles
22018 FUNCTION_DECLs. */
22019 if (!DECL_CLASS_SCOPE_P (decl))
22021 error ("%qD is not a static data member of a class template", decl);
22022 return;
22024 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22025 if (!result || !VAR_P (result))
22027 error ("no matching template for %qD found", decl);
22028 return;
22030 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22032 error ("type %qT for explicit instantiation %qD does not match "
22033 "declared type %qT", TREE_TYPE (result), decl,
22034 TREE_TYPE (decl));
22035 return;
22038 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22040 error ("explicit instantiation of %q#D", decl);
22041 return;
22043 else
22044 result = decl;
22046 /* Check for various error cases. Note that if the explicit
22047 instantiation is valid the RESULT will currently be marked as an
22048 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22049 until we get here. */
22051 if (DECL_TEMPLATE_SPECIALIZATION (result))
22053 /* DR 259 [temp.spec].
22055 Both an explicit instantiation and a declaration of an explicit
22056 specialization shall not appear in a program unless the explicit
22057 instantiation follows a declaration of the explicit specialization.
22059 For a given set of template parameters, if an explicit
22060 instantiation of a template appears after a declaration of an
22061 explicit specialization for that template, the explicit
22062 instantiation has no effect. */
22063 return;
22065 else if (DECL_EXPLICIT_INSTANTIATION (result))
22067 /* [temp.spec]
22069 No program shall explicitly instantiate any template more
22070 than once.
22072 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22073 the first instantiation was `extern' and the second is not,
22074 and EXTERN_P for the opposite case. */
22075 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22076 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22077 /* If an "extern" explicit instantiation follows an ordinary
22078 explicit instantiation, the template is instantiated. */
22079 if (extern_p)
22080 return;
22082 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22084 error ("no matching template for %qD found", result);
22085 return;
22087 else if (!DECL_TEMPLATE_INFO (result))
22089 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22090 return;
22093 if (storage == NULL_TREE)
22095 else if (storage == ridpointers[(int) RID_EXTERN])
22097 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22098 pedwarn (input_location, OPT_Wpedantic,
22099 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22100 "instantiations");
22101 extern_p = 1;
22103 else
22104 error ("storage class %qD applied to template instantiation", storage);
22106 check_explicit_instantiation_namespace (result);
22107 mark_decl_instantiated (result, extern_p);
22108 if (! extern_p)
22109 instantiate_decl (result, /*defer_ok=*/true,
22110 /*expl_inst_class_mem_p=*/false);
22113 static void
22114 mark_class_instantiated (tree t, int extern_p)
22116 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22117 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22118 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22119 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22120 if (! extern_p)
22122 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22123 rest_of_type_compilation (t, 1);
22127 /* Called from do_type_instantiation through binding_table_foreach to
22128 do recursive instantiation for the type bound in ENTRY. */
22129 static void
22130 bt_instantiate_type_proc (binding_entry entry, void *data)
22132 tree storage = *(tree *) data;
22134 if (MAYBE_CLASS_TYPE_P (entry->type)
22135 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22136 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22139 /* Perform an explicit instantiation of template class T. STORAGE, if
22140 non-null, is the RID for extern, inline or static. COMPLAIN is
22141 nonzero if this is called from the parser, zero if called recursively,
22142 since the standard is unclear (as detailed below). */
22144 void
22145 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22147 int extern_p = 0;
22148 int nomem_p = 0;
22149 int static_p = 0;
22150 int previous_instantiation_extern_p = 0;
22152 if (TREE_CODE (t) == TYPE_DECL)
22153 t = TREE_TYPE (t);
22155 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22157 tree tmpl =
22158 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22159 if (tmpl)
22160 error ("explicit instantiation of non-class template %qD", tmpl);
22161 else
22162 error ("explicit instantiation of non-template type %qT", t);
22163 return;
22166 complete_type (t);
22168 if (!COMPLETE_TYPE_P (t))
22170 if (complain & tf_error)
22171 error ("explicit instantiation of %q#T before definition of template",
22173 return;
22176 if (storage != NULL_TREE)
22178 if (!in_system_header_at (input_location))
22180 if (storage == ridpointers[(int) RID_EXTERN])
22182 if (cxx_dialect == cxx98)
22183 pedwarn (input_location, OPT_Wpedantic,
22184 "ISO C++ 1998 forbids the use of %<extern%> on "
22185 "explicit instantiations");
22187 else
22188 pedwarn (input_location, OPT_Wpedantic,
22189 "ISO C++ forbids the use of %qE"
22190 " on explicit instantiations", storage);
22193 if (storage == ridpointers[(int) RID_INLINE])
22194 nomem_p = 1;
22195 else if (storage == ridpointers[(int) RID_EXTERN])
22196 extern_p = 1;
22197 else if (storage == ridpointers[(int) RID_STATIC])
22198 static_p = 1;
22199 else
22201 error ("storage class %qD applied to template instantiation",
22202 storage);
22203 extern_p = 0;
22207 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22209 /* DR 259 [temp.spec].
22211 Both an explicit instantiation and a declaration of an explicit
22212 specialization shall not appear in a program unless the explicit
22213 instantiation follows a declaration of the explicit specialization.
22215 For a given set of template parameters, if an explicit
22216 instantiation of a template appears after a declaration of an
22217 explicit specialization for that template, the explicit
22218 instantiation has no effect. */
22219 return;
22221 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22223 /* [temp.spec]
22225 No program shall explicitly instantiate any template more
22226 than once.
22228 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22229 instantiation was `extern'. If EXTERN_P then the second is.
22230 These cases are OK. */
22231 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22233 if (!previous_instantiation_extern_p && !extern_p
22234 && (complain & tf_error))
22235 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22237 /* If we've already instantiated the template, just return now. */
22238 if (!CLASSTYPE_INTERFACE_ONLY (t))
22239 return;
22242 check_explicit_instantiation_namespace (TYPE_NAME (t));
22243 mark_class_instantiated (t, extern_p);
22245 if (nomem_p)
22246 return;
22248 /* In contrast to implicit instantiation, where only the
22249 declarations, and not the definitions, of members are
22250 instantiated, we have here:
22252 [temp.explicit]
22254 The explicit instantiation of a class template specialization
22255 implies the instantiation of all of its members not
22256 previously explicitly specialized in the translation unit
22257 containing the explicit instantiation.
22259 Of course, we can't instantiate member template classes, since we
22260 don't have any arguments for them. Note that the standard is
22261 unclear on whether the instantiation of the members are
22262 *explicit* instantiations or not. However, the most natural
22263 interpretation is that it should be an explicit
22264 instantiation. */
22265 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22266 if ((VAR_P (fld)
22267 || (TREE_CODE (fld) == FUNCTION_DECL
22268 && !static_p
22269 && user_provided_p (fld)))
22270 && DECL_TEMPLATE_INSTANTIATION (fld))
22272 mark_decl_instantiated (fld, extern_p);
22273 if (! extern_p)
22274 instantiate_decl (fld, /*defer_ok=*/true,
22275 /*expl_inst_class_mem_p=*/true);
22278 if (CLASSTYPE_NESTED_UTDS (t))
22279 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22280 bt_instantiate_type_proc, &storage);
22283 /* Given a function DECL, which is a specialization of TMPL, modify
22284 DECL to be a re-instantiation of TMPL with the same template
22285 arguments. TMPL should be the template into which tsubst'ing
22286 should occur for DECL, not the most general template.
22288 One reason for doing this is a scenario like this:
22290 template <class T>
22291 void f(const T&, int i);
22293 void g() { f(3, 7); }
22295 template <class T>
22296 void f(const T& t, const int i) { }
22298 Note that when the template is first instantiated, with
22299 instantiate_template, the resulting DECL will have no name for the
22300 first parameter, and the wrong type for the second. So, when we go
22301 to instantiate the DECL, we regenerate it. */
22303 static void
22304 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22306 /* The arguments used to instantiate DECL, from the most general
22307 template. */
22308 tree code_pattern;
22310 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22312 /* Make sure that we can see identifiers, and compute access
22313 correctly. */
22314 push_access_scope (decl);
22316 if (TREE_CODE (decl) == FUNCTION_DECL)
22318 tree decl_parm;
22319 tree pattern_parm;
22320 tree specs;
22321 int args_depth;
22322 int parms_depth;
22324 args_depth = TMPL_ARGS_DEPTH (args);
22325 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22326 if (args_depth > parms_depth)
22327 args = get_innermost_template_args (args, parms_depth);
22329 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22330 args, tf_error, NULL_TREE,
22331 /*defer_ok*/false);
22332 if (specs && specs != error_mark_node)
22333 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22334 specs);
22336 /* Merge parameter declarations. */
22337 decl_parm = skip_artificial_parms_for (decl,
22338 DECL_ARGUMENTS (decl));
22339 pattern_parm
22340 = skip_artificial_parms_for (code_pattern,
22341 DECL_ARGUMENTS (code_pattern));
22342 while (decl_parm && !DECL_PACK_P (pattern_parm))
22344 tree parm_type;
22345 tree attributes;
22347 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22348 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22349 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22350 NULL_TREE);
22351 parm_type = type_decays_to (parm_type);
22352 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22353 TREE_TYPE (decl_parm) = parm_type;
22354 attributes = DECL_ATTRIBUTES (pattern_parm);
22355 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22357 DECL_ATTRIBUTES (decl_parm) = attributes;
22358 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22360 decl_parm = DECL_CHAIN (decl_parm);
22361 pattern_parm = DECL_CHAIN (pattern_parm);
22363 /* Merge any parameters that match with the function parameter
22364 pack. */
22365 if (pattern_parm && DECL_PACK_P (pattern_parm))
22367 int i, len;
22368 tree expanded_types;
22369 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22370 the parameters in this function parameter pack. */
22371 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22372 args, tf_error, NULL_TREE);
22373 len = TREE_VEC_LENGTH (expanded_types);
22374 for (i = 0; i < len; i++)
22376 tree parm_type;
22377 tree attributes;
22379 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22380 /* Rename the parameter to include the index. */
22381 DECL_NAME (decl_parm) =
22382 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22383 parm_type = TREE_VEC_ELT (expanded_types, i);
22384 parm_type = type_decays_to (parm_type);
22385 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22386 TREE_TYPE (decl_parm) = parm_type;
22387 attributes = DECL_ATTRIBUTES (pattern_parm);
22388 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22390 DECL_ATTRIBUTES (decl_parm) = attributes;
22391 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22393 decl_parm = DECL_CHAIN (decl_parm);
22396 /* Merge additional specifiers from the CODE_PATTERN. */
22397 if (DECL_DECLARED_INLINE_P (code_pattern)
22398 && !DECL_DECLARED_INLINE_P (decl))
22399 DECL_DECLARED_INLINE_P (decl) = 1;
22401 else if (VAR_P (decl))
22403 DECL_INITIAL (decl) =
22404 tsubst_expr (DECL_INITIAL (code_pattern), args,
22405 tf_error, DECL_TI_TEMPLATE (decl),
22406 /*integral_constant_expression_p=*/false);
22407 if (VAR_HAD_UNKNOWN_BOUND (decl))
22408 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22409 tf_error, DECL_TI_TEMPLATE (decl));
22411 else
22412 gcc_unreachable ();
22414 pop_access_scope (decl);
22417 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22418 substituted to get DECL. */
22420 tree
22421 template_for_substitution (tree decl)
22423 tree tmpl = DECL_TI_TEMPLATE (decl);
22425 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22426 for the instantiation. This is not always the most general
22427 template. Consider, for example:
22429 template <class T>
22430 struct S { template <class U> void f();
22431 template <> void f<int>(); };
22433 and an instantiation of S<double>::f<int>. We want TD to be the
22434 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22435 while (/* An instantiation cannot have a definition, so we need a
22436 more general template. */
22437 DECL_TEMPLATE_INSTANTIATION (tmpl)
22438 /* We must also deal with friend templates. Given:
22440 template <class T> struct S {
22441 template <class U> friend void f() {};
22444 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22445 so far as the language is concerned, but that's still
22446 where we get the pattern for the instantiation from. On
22447 other hand, if the definition comes outside the class, say:
22449 template <class T> struct S {
22450 template <class U> friend void f();
22452 template <class U> friend void f() {}
22454 we don't need to look any further. That's what the check for
22455 DECL_INITIAL is for. */
22456 || (TREE_CODE (decl) == FUNCTION_DECL
22457 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22458 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22460 /* The present template, TD, should not be a definition. If it
22461 were a definition, we should be using it! Note that we
22462 cannot restructure the loop to just keep going until we find
22463 a template with a definition, since that might go too far if
22464 a specialization was declared, but not defined. */
22466 /* Fetch the more general template. */
22467 tmpl = DECL_TI_TEMPLATE (tmpl);
22470 return tmpl;
22473 /* Returns true if we need to instantiate this template instance even if we
22474 know we aren't going to emit it. */
22476 bool
22477 always_instantiate_p (tree decl)
22479 /* We always instantiate inline functions so that we can inline them. An
22480 explicit instantiation declaration prohibits implicit instantiation of
22481 non-inline functions. With high levels of optimization, we would
22482 normally inline non-inline functions -- but we're not allowed to do
22483 that for "extern template" functions. Therefore, we check
22484 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22485 return ((TREE_CODE (decl) == FUNCTION_DECL
22486 && (DECL_DECLARED_INLINE_P (decl)
22487 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22488 /* And we need to instantiate static data members so that
22489 their initializers are available in integral constant
22490 expressions. */
22491 || (VAR_P (decl)
22492 && decl_maybe_constant_var_p (decl)));
22495 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22496 instantiate it now, modifying TREE_TYPE (fn). */
22498 void
22499 maybe_instantiate_noexcept (tree fn)
22501 tree fntype, spec, noex, clone;
22503 /* Don't instantiate a noexcept-specification from template context. */
22504 if (processing_template_decl)
22505 return;
22507 if (DECL_CLONED_FUNCTION_P (fn))
22508 fn = DECL_CLONED_FUNCTION (fn);
22509 fntype = TREE_TYPE (fn);
22510 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22512 if (!spec || !TREE_PURPOSE (spec))
22513 return;
22515 noex = TREE_PURPOSE (spec);
22517 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22519 static hash_set<tree>* fns = new hash_set<tree>;
22520 bool added = false;
22521 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22522 spec = get_defaulted_eh_spec (fn);
22523 else if (!(added = !fns->add (fn)))
22525 /* If hash_set::add returns true, the element was already there. */
22526 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22527 DECL_SOURCE_LOCATION (fn));
22528 error_at (loc,
22529 "exception specification of %qD depends on itself",
22530 fn);
22531 spec = noexcept_false_spec;
22533 else if (push_tinst_level (fn))
22535 push_access_scope (fn);
22536 push_deferring_access_checks (dk_no_deferred);
22537 input_location = DECL_SOURCE_LOCATION (fn);
22538 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22539 DEFERRED_NOEXCEPT_ARGS (noex),
22540 tf_warning_or_error, fn,
22541 /*function_p=*/false,
22542 /*integral_constant_expression_p=*/true);
22543 pop_deferring_access_checks ();
22544 pop_access_scope (fn);
22545 pop_tinst_level ();
22546 spec = build_noexcept_spec (noex, tf_warning_or_error);
22547 if (spec == error_mark_node)
22548 spec = noexcept_false_spec;
22550 else
22551 spec = noexcept_false_spec;
22553 if (added)
22554 fns->remove (fn);
22556 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22559 FOR_EACH_CLONE (clone, fn)
22561 if (TREE_TYPE (clone) == fntype)
22562 TREE_TYPE (clone) = TREE_TYPE (fn);
22563 else
22564 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22568 /* Produce the definition of D, a _DECL generated from a template. If
22569 DEFER_OK is true, then we don't have to actually do the
22570 instantiation now; we just have to do it sometime. Normally it is
22571 an error if this is an explicit instantiation but D is undefined.
22572 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22573 instantiated class template. */
22575 tree
22576 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22578 tree tmpl = DECL_TI_TEMPLATE (d);
22579 tree gen_args;
22580 tree args;
22581 tree td;
22582 tree code_pattern;
22583 tree spec;
22584 tree gen_tmpl;
22585 bool pattern_defined;
22586 location_t saved_loc = input_location;
22587 int saved_unevaluated_operand = cp_unevaluated_operand;
22588 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22589 bool external_p;
22590 bool deleted_p;
22592 /* This function should only be used to instantiate templates for
22593 functions and static member variables. */
22594 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22596 /* A concept is never instantiated. */
22597 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22599 /* Variables are never deferred; if instantiation is required, they
22600 are instantiated right away. That allows for better code in the
22601 case that an expression refers to the value of the variable --
22602 if the variable has a constant value the referring expression can
22603 take advantage of that fact. */
22604 if (VAR_P (d))
22605 defer_ok = false;
22607 /* Don't instantiate cloned functions. Instead, instantiate the
22608 functions they cloned. */
22609 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22610 d = DECL_CLONED_FUNCTION (d);
22612 if (DECL_TEMPLATE_INSTANTIATED (d)
22613 || (TREE_CODE (d) == FUNCTION_DECL
22614 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22615 || DECL_TEMPLATE_SPECIALIZATION (d))
22616 /* D has already been instantiated or explicitly specialized, so
22617 there's nothing for us to do here.
22619 It might seem reasonable to check whether or not D is an explicit
22620 instantiation, and, if so, stop here. But when an explicit
22621 instantiation is deferred until the end of the compilation,
22622 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22623 the instantiation. */
22624 return d;
22626 /* Check to see whether we know that this template will be
22627 instantiated in some other file, as with "extern template"
22628 extension. */
22629 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22631 /* In general, we do not instantiate such templates. */
22632 if (external_p && !always_instantiate_p (d))
22633 return d;
22635 gen_tmpl = most_general_template (tmpl);
22636 gen_args = DECL_TI_ARGS (d);
22638 if (tmpl != gen_tmpl)
22639 /* We should already have the extra args. */
22640 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22641 == TMPL_ARGS_DEPTH (gen_args));
22642 /* And what's in the hash table should match D. */
22643 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22644 || spec == NULL_TREE);
22646 /* This needs to happen before any tsubsting. */
22647 if (! push_tinst_level (d))
22648 return d;
22650 timevar_push (TV_TEMPLATE_INST);
22652 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22653 for the instantiation. */
22654 td = template_for_substitution (d);
22655 args = gen_args;
22657 if (VAR_P (d))
22659 /* Look up an explicit specialization, if any. */
22660 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22661 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22662 if (elt && elt != error_mark_node)
22664 td = TREE_VALUE (elt);
22665 args = TREE_PURPOSE (elt);
22669 code_pattern = DECL_TEMPLATE_RESULT (td);
22671 /* We should never be trying to instantiate a member of a class
22672 template or partial specialization. */
22673 gcc_assert (d != code_pattern);
22675 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22676 || DECL_TEMPLATE_SPECIALIZATION (td))
22677 /* In the case of a friend template whose definition is provided
22678 outside the class, we may have too many arguments. Drop the
22679 ones we don't need. The same is true for specializations. */
22680 args = get_innermost_template_args
22681 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22683 if (TREE_CODE (d) == FUNCTION_DECL)
22685 deleted_p = DECL_DELETED_FN (code_pattern);
22686 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22687 && DECL_INITIAL (code_pattern) != error_mark_node)
22688 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22689 || deleted_p);
22691 else
22693 deleted_p = false;
22694 if (DECL_CLASS_SCOPE_P (code_pattern))
22695 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22696 || DECL_INLINE_VAR_P (code_pattern));
22697 else
22698 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22701 /* We may be in the middle of deferred access check. Disable it now. */
22702 push_deferring_access_checks (dk_no_deferred);
22704 /* Unless an explicit instantiation directive has already determined
22705 the linkage of D, remember that a definition is available for
22706 this entity. */
22707 if (pattern_defined
22708 && !DECL_INTERFACE_KNOWN (d)
22709 && !DECL_NOT_REALLY_EXTERN (d))
22710 mark_definable (d);
22712 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22713 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22714 input_location = DECL_SOURCE_LOCATION (d);
22716 /* If D is a member of an explicitly instantiated class template,
22717 and no definition is available, treat it like an implicit
22718 instantiation. */
22719 if (!pattern_defined && expl_inst_class_mem_p
22720 && DECL_EXPLICIT_INSTANTIATION (d))
22722 /* Leave linkage flags alone on instantiations with anonymous
22723 visibility. */
22724 if (TREE_PUBLIC (d))
22726 DECL_NOT_REALLY_EXTERN (d) = 0;
22727 DECL_INTERFACE_KNOWN (d) = 0;
22729 SET_DECL_IMPLICIT_INSTANTIATION (d);
22732 /* Defer all other templates, unless we have been explicitly
22733 forbidden from doing so. */
22734 if (/* If there is no definition, we cannot instantiate the
22735 template. */
22736 ! pattern_defined
22737 /* If it's OK to postpone instantiation, do so. */
22738 || defer_ok
22739 /* If this is a static data member that will be defined
22740 elsewhere, we don't want to instantiate the entire data
22741 member, but we do want to instantiate the initializer so that
22742 we can substitute that elsewhere. */
22743 || (external_p && VAR_P (d))
22744 /* Handle here a deleted function too, avoid generating
22745 its body (c++/61080). */
22746 || deleted_p)
22748 /* The definition of the static data member is now required so
22749 we must substitute the initializer. */
22750 if (VAR_P (d)
22751 && !DECL_INITIAL (d)
22752 && DECL_INITIAL (code_pattern))
22754 tree ns;
22755 tree init;
22756 bool const_init = false;
22757 bool enter_context = DECL_CLASS_SCOPE_P (d);
22759 ns = decl_namespace_context (d);
22760 push_nested_namespace (ns);
22761 if (enter_context)
22762 push_nested_class (DECL_CONTEXT (d));
22763 init = tsubst_expr (DECL_INITIAL (code_pattern),
22764 args,
22765 tf_warning_or_error, NULL_TREE,
22766 /*integral_constant_expression_p=*/false);
22767 /* If instantiating the initializer involved instantiating this
22768 again, don't call cp_finish_decl twice. */
22769 if (!DECL_INITIAL (d))
22771 /* Make sure the initializer is still constant, in case of
22772 circular dependency (template/instantiate6.C). */
22773 const_init
22774 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22775 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22776 /*asmspec_tree=*/NULL_TREE,
22777 LOOKUP_ONLYCONVERTING);
22779 if (enter_context)
22780 pop_nested_class ();
22781 pop_nested_namespace (ns);
22784 /* We restore the source position here because it's used by
22785 add_pending_template. */
22786 input_location = saved_loc;
22788 if (at_eof && !pattern_defined
22789 && DECL_EXPLICIT_INSTANTIATION (d)
22790 && DECL_NOT_REALLY_EXTERN (d))
22791 /* [temp.explicit]
22793 The definition of a non-exported function template, a
22794 non-exported member function template, or a non-exported
22795 member function or static data member of a class template
22796 shall be present in every translation unit in which it is
22797 explicitly instantiated. */
22798 permerror (input_location, "explicit instantiation of %qD "
22799 "but no definition available", d);
22801 /* If we're in unevaluated context, we just wanted to get the
22802 constant value; this isn't an odr use, so don't queue
22803 a full instantiation. */
22804 if (cp_unevaluated_operand != 0)
22805 goto out;
22806 /* ??? Historically, we have instantiated inline functions, even
22807 when marked as "extern template". */
22808 if (!(external_p && VAR_P (d)))
22809 add_pending_template (d);
22810 goto out;
22812 /* Tell the repository that D is available in this translation unit
22813 -- and see if it is supposed to be instantiated here. */
22814 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22816 /* In a PCH file, despite the fact that the repository hasn't
22817 requested instantiation in the PCH it is still possible that
22818 an instantiation will be required in a file that includes the
22819 PCH. */
22820 if (pch_file)
22821 add_pending_template (d);
22822 /* Instantiate inline functions so that the inliner can do its
22823 job, even though we'll not be emitting a copy of this
22824 function. */
22825 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22826 goto out;
22829 bool push_to_top, nested;
22830 tree fn_context;
22831 fn_context = decl_function_context (d);
22832 nested = current_function_decl != NULL_TREE;
22833 push_to_top = !(nested && fn_context == current_function_decl);
22835 vec<tree> omp_privatization_save;
22836 if (nested)
22837 save_omp_privatization_clauses (omp_privatization_save);
22839 if (push_to_top)
22840 push_to_top_level ();
22841 else
22843 push_function_context ();
22844 cp_unevaluated_operand = 0;
22845 c_inhibit_evaluation_warnings = 0;
22848 /* Mark D as instantiated so that recursive calls to
22849 instantiate_decl do not try to instantiate it again. */
22850 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22852 /* Regenerate the declaration in case the template has been modified
22853 by a subsequent redeclaration. */
22854 regenerate_decl_from_template (d, td, args);
22856 /* We already set the file and line above. Reset them now in case
22857 they changed as a result of calling regenerate_decl_from_template. */
22858 input_location = DECL_SOURCE_LOCATION (d);
22860 if (VAR_P (d))
22862 tree init;
22863 bool const_init = false;
22865 /* Clear out DECL_RTL; whatever was there before may not be right
22866 since we've reset the type of the declaration. */
22867 SET_DECL_RTL (d, NULL);
22868 DECL_IN_AGGR_P (d) = 0;
22870 /* The initializer is placed in DECL_INITIAL by
22871 regenerate_decl_from_template so we don't need to
22872 push/pop_access_scope again here. Pull it out so that
22873 cp_finish_decl can process it. */
22874 init = DECL_INITIAL (d);
22875 DECL_INITIAL (d) = NULL_TREE;
22876 DECL_INITIALIZED_P (d) = 0;
22878 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22879 initializer. That function will defer actual emission until
22880 we have a chance to determine linkage. */
22881 DECL_EXTERNAL (d) = 0;
22883 /* Enter the scope of D so that access-checking works correctly. */
22884 bool enter_context = DECL_CLASS_SCOPE_P (d);
22885 if (enter_context)
22886 push_nested_class (DECL_CONTEXT (d));
22888 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22889 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22891 if (enter_context)
22892 pop_nested_class ();
22894 if (variable_template_p (gen_tmpl))
22895 note_variable_template_instantiation (d);
22897 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22898 synthesize_method (d);
22899 else if (TREE_CODE (d) == FUNCTION_DECL)
22901 hash_map<tree, tree> *saved_local_specializations;
22902 tree tmpl_parm;
22903 tree spec_parm;
22904 tree block = NULL_TREE;
22905 tree lambda_ctx = NULL_TREE;
22907 /* Save away the current list, in case we are instantiating one
22908 template from within the body of another. */
22909 saved_local_specializations = local_specializations;
22911 /* Set up the list of local specializations. */
22912 local_specializations = new hash_map<tree, tree>;
22914 /* Set up context. */
22915 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22916 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22917 block = push_stmt_list ();
22918 else
22920 if (push_to_top && LAMBDA_FUNCTION_P (d))
22922 /* When instantiating a lambda's templated function
22923 operator, we need to push the non-lambda class scope
22924 of the lambda itself so that the nested function
22925 stack is sufficiently correct to deal with this
22926 capture. */
22927 lambda_ctx = DECL_CONTEXT (d);
22929 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22930 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22931 if (lambda_ctx)
22932 push_nested_class (lambda_ctx);
22934 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22937 /* Some typedefs referenced from within the template code need to be
22938 access checked at template instantiation time, i.e now. These
22939 types were added to the template at parsing time. Let's get those
22940 and perform the access checks then. */
22941 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22942 args);
22944 /* Create substitution entries for the parameters. */
22945 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22946 spec_parm = DECL_ARGUMENTS (d);
22947 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22949 register_local_specialization (spec_parm, tmpl_parm);
22950 spec_parm = skip_artificial_parms_for (d, spec_parm);
22951 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22953 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22955 if (!DECL_PACK_P (tmpl_parm))
22957 register_local_specialization (spec_parm, tmpl_parm);
22958 spec_parm = DECL_CHAIN (spec_parm);
22960 else
22962 /* Register the (value) argument pack as a specialization of
22963 TMPL_PARM, then move on. */
22964 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22965 register_local_specialization (argpack, tmpl_parm);
22968 gcc_assert (!spec_parm);
22970 /* Substitute into the body of the function. */
22971 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22972 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22973 tf_warning_or_error, tmpl);
22974 else
22976 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22977 tf_warning_or_error, tmpl,
22978 /*integral_constant_expression_p=*/false);
22980 /* Set the current input_location to the end of the function
22981 so that finish_function knows where we are. */
22982 input_location
22983 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22985 /* Remember if we saw an infinite loop in the template. */
22986 current_function_infinite_loop
22987 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22990 /* We don't need the local specializations any more. */
22991 delete local_specializations;
22992 local_specializations = saved_local_specializations;
22994 /* Finish the function. */
22995 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22996 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22997 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22998 else
23000 d = finish_function (0);
23001 expand_or_defer_fn (d);
23003 if (lambda_ctx)
23004 pop_nested_class ();
23006 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23007 cp_check_omp_declare_reduction (d);
23010 /* We're not deferring instantiation any more. */
23011 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23013 if (push_to_top)
23014 pop_from_top_level ();
23015 else
23016 pop_function_context ();
23018 if (nested)
23019 restore_omp_privatization_clauses (omp_privatization_save);
23021 out:
23022 pop_deferring_access_checks ();
23023 timevar_pop (TV_TEMPLATE_INST);
23024 pop_tinst_level ();
23025 input_location = saved_loc;
23026 cp_unevaluated_operand = saved_unevaluated_operand;
23027 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23029 return d;
23032 /* Run through the list of templates that we wish we could
23033 instantiate, and instantiate any we can. RETRIES is the
23034 number of times we retry pending template instantiation. */
23036 void
23037 instantiate_pending_templates (int retries)
23039 int reconsider;
23040 location_t saved_loc = input_location;
23042 /* Instantiating templates may trigger vtable generation. This in turn
23043 may require further template instantiations. We place a limit here
23044 to avoid infinite loop. */
23045 if (pending_templates && retries >= max_tinst_depth)
23047 tree decl = pending_templates->tinst->decl;
23049 fatal_error (input_location,
23050 "template instantiation depth exceeds maximum of %d"
23051 " instantiating %q+D, possibly from virtual table generation"
23052 " (use -ftemplate-depth= to increase the maximum)",
23053 max_tinst_depth, decl);
23054 if (TREE_CODE (decl) == FUNCTION_DECL)
23055 /* Pretend that we defined it. */
23056 DECL_INITIAL (decl) = error_mark_node;
23057 return;
23062 struct pending_template **t = &pending_templates;
23063 struct pending_template *last = NULL;
23064 reconsider = 0;
23065 while (*t)
23067 tree instantiation = reopen_tinst_level ((*t)->tinst);
23068 bool complete = false;
23070 if (TYPE_P (instantiation))
23072 if (!COMPLETE_TYPE_P (instantiation))
23074 instantiate_class_template (instantiation);
23075 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23076 for (tree fld = TYPE_FIELDS (instantiation);
23077 fld; fld = TREE_CHAIN (fld))
23078 if ((VAR_P (fld)
23079 || (TREE_CODE (fld) == FUNCTION_DECL
23080 && !DECL_ARTIFICIAL (fld)))
23081 && DECL_TEMPLATE_INSTANTIATION (fld))
23082 instantiate_decl (fld,
23083 /*defer_ok=*/false,
23084 /*expl_inst_class_mem_p=*/false);
23086 if (COMPLETE_TYPE_P (instantiation))
23087 reconsider = 1;
23090 complete = COMPLETE_TYPE_P (instantiation);
23092 else
23094 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23095 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23097 instantiation
23098 = instantiate_decl (instantiation,
23099 /*defer_ok=*/false,
23100 /*expl_inst_class_mem_p=*/false);
23101 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23102 reconsider = 1;
23105 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23106 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23109 if (complete)
23110 /* If INSTANTIATION has been instantiated, then we don't
23111 need to consider it again in the future. */
23112 *t = (*t)->next;
23113 else
23115 last = *t;
23116 t = &(*t)->next;
23118 tinst_depth = 0;
23119 current_tinst_level = NULL;
23121 last_pending_template = last;
23123 while (reconsider);
23125 input_location = saved_loc;
23128 /* Substitute ARGVEC into T, which is a list of initializers for
23129 either base class or a non-static data member. The TREE_PURPOSEs
23130 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23131 instantiate_decl. */
23133 static tree
23134 tsubst_initializer_list (tree t, tree argvec)
23136 tree inits = NULL_TREE;
23138 for (; t; t = TREE_CHAIN (t))
23140 tree decl;
23141 tree init;
23142 tree expanded_bases = NULL_TREE;
23143 tree expanded_arguments = NULL_TREE;
23144 int i, len = 1;
23146 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23148 tree expr;
23149 tree arg;
23151 /* Expand the base class expansion type into separate base
23152 classes. */
23153 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23154 tf_warning_or_error,
23155 NULL_TREE);
23156 if (expanded_bases == error_mark_node)
23157 continue;
23159 /* We'll be building separate TREE_LISTs of arguments for
23160 each base. */
23161 len = TREE_VEC_LENGTH (expanded_bases);
23162 expanded_arguments = make_tree_vec (len);
23163 for (i = 0; i < len; i++)
23164 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23166 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23167 expand each argument in the TREE_VALUE of t. */
23168 expr = make_node (EXPR_PACK_EXPANSION);
23169 PACK_EXPANSION_LOCAL_P (expr) = true;
23170 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23171 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23173 if (TREE_VALUE (t) == void_type_node)
23174 /* VOID_TYPE_NODE is used to indicate
23175 value-initialization. */
23177 for (i = 0; i < len; i++)
23178 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23180 else
23182 /* Substitute parameter packs into each argument in the
23183 TREE_LIST. */
23184 in_base_initializer = 1;
23185 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23187 tree expanded_exprs;
23189 /* Expand the argument. */
23190 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23191 expanded_exprs
23192 = tsubst_pack_expansion (expr, argvec,
23193 tf_warning_or_error,
23194 NULL_TREE);
23195 if (expanded_exprs == error_mark_node)
23196 continue;
23198 /* Prepend each of the expanded expressions to the
23199 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23200 for (i = 0; i < len; i++)
23202 TREE_VEC_ELT (expanded_arguments, i) =
23203 tree_cons (NULL_TREE,
23204 TREE_VEC_ELT (expanded_exprs, i),
23205 TREE_VEC_ELT (expanded_arguments, i));
23208 in_base_initializer = 0;
23210 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23211 since we built them backwards. */
23212 for (i = 0; i < len; i++)
23214 TREE_VEC_ELT (expanded_arguments, i) =
23215 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23220 for (i = 0; i < len; ++i)
23222 if (expanded_bases)
23224 decl = TREE_VEC_ELT (expanded_bases, i);
23225 decl = expand_member_init (decl);
23226 init = TREE_VEC_ELT (expanded_arguments, i);
23228 else
23230 tree tmp;
23231 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23232 tf_warning_or_error, NULL_TREE);
23234 decl = expand_member_init (decl);
23235 if (decl && !DECL_P (decl))
23236 in_base_initializer = 1;
23238 init = TREE_VALUE (t);
23239 tmp = init;
23240 if (init != void_type_node)
23241 init = tsubst_expr (init, argvec,
23242 tf_warning_or_error, NULL_TREE,
23243 /*integral_constant_expression_p=*/false);
23244 if (init == NULL_TREE && tmp != NULL_TREE)
23245 /* If we had an initializer but it instantiated to nothing,
23246 value-initialize the object. This will only occur when
23247 the initializer was a pack expansion where the parameter
23248 packs used in that expansion were of length zero. */
23249 init = void_type_node;
23250 in_base_initializer = 0;
23253 if (decl)
23255 init = build_tree_list (decl, init);
23256 TREE_CHAIN (init) = inits;
23257 inits = init;
23261 return inits;
23264 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23266 static void
23267 set_current_access_from_decl (tree decl)
23269 if (TREE_PRIVATE (decl))
23270 current_access_specifier = access_private_node;
23271 else if (TREE_PROTECTED (decl))
23272 current_access_specifier = access_protected_node;
23273 else
23274 current_access_specifier = access_public_node;
23277 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23278 is the instantiation (which should have been created with
23279 start_enum) and ARGS are the template arguments to use. */
23281 static void
23282 tsubst_enum (tree tag, tree newtag, tree args)
23284 tree e;
23286 if (SCOPED_ENUM_P (newtag))
23287 begin_scope (sk_scoped_enum, newtag);
23289 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23291 tree value;
23292 tree decl;
23294 decl = TREE_VALUE (e);
23295 /* Note that in a template enum, the TREE_VALUE is the
23296 CONST_DECL, not the corresponding INTEGER_CST. */
23297 value = tsubst_expr (DECL_INITIAL (decl),
23298 args, tf_warning_or_error, NULL_TREE,
23299 /*integral_constant_expression_p=*/true);
23301 /* Give this enumeration constant the correct access. */
23302 set_current_access_from_decl (decl);
23304 /* Actually build the enumerator itself. Here we're assuming that
23305 enumerators can't have dependent attributes. */
23306 build_enumerator (DECL_NAME (decl), value, newtag,
23307 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23310 if (SCOPED_ENUM_P (newtag))
23311 finish_scope ();
23313 finish_enum_value_list (newtag);
23314 finish_enum (newtag);
23316 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23317 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23320 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23321 its type -- but without substituting the innermost set of template
23322 arguments. So, innermost set of template parameters will appear in
23323 the type. */
23325 tree
23326 get_mostly_instantiated_function_type (tree decl)
23328 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23329 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23332 /* Return truthvalue if we're processing a template different from
23333 the last one involved in diagnostics. */
23334 bool
23335 problematic_instantiation_changed (void)
23337 return current_tinst_level != last_error_tinst_level;
23340 /* Remember current template involved in diagnostics. */
23341 void
23342 record_last_problematic_instantiation (void)
23344 last_error_tinst_level = current_tinst_level;
23347 struct tinst_level *
23348 current_instantiation (void)
23350 return current_tinst_level;
23353 /* Return TRUE if current_function_decl is being instantiated, false
23354 otherwise. */
23356 bool
23357 instantiating_current_function_p (void)
23359 return (current_instantiation ()
23360 && current_instantiation ()->decl == current_function_decl);
23363 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23364 type. Return zero for ok, nonzero for disallowed. Issue error and
23365 warning messages under control of COMPLAIN. */
23367 static int
23368 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23370 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23371 return 0;
23372 else if (POINTER_TYPE_P (type))
23373 return 0;
23374 else if (TYPE_PTRMEM_P (type))
23375 return 0;
23376 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23377 return 0;
23378 else if (TREE_CODE (type) == TYPENAME_TYPE)
23379 return 0;
23380 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23381 return 0;
23382 else if (TREE_CODE (type) == NULLPTR_TYPE)
23383 return 0;
23384 /* A bound template template parm could later be instantiated to have a valid
23385 nontype parm type via an alias template. */
23386 else if (cxx_dialect >= cxx11
23387 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23388 return 0;
23390 if (complain & tf_error)
23392 if (type == error_mark_node)
23393 inform (input_location, "invalid template non-type parameter");
23394 else
23395 error ("%q#T is not a valid type for a template non-type parameter",
23396 type);
23398 return 1;
23401 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23402 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23404 static bool
23405 dependent_type_p_r (tree type)
23407 tree scope;
23409 /* [temp.dep.type]
23411 A type is dependent if it is:
23413 -- a template parameter. Template template parameters are types
23414 for us (since TYPE_P holds true for them) so we handle
23415 them here. */
23416 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23417 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23418 return true;
23419 /* -- a qualified-id with a nested-name-specifier which contains a
23420 class-name that names a dependent type or whose unqualified-id
23421 names a dependent type. */
23422 if (TREE_CODE (type) == TYPENAME_TYPE)
23423 return true;
23425 /* An alias template specialization can be dependent even if the
23426 resulting type is not. */
23427 if (dependent_alias_template_spec_p (type))
23428 return true;
23430 /* -- a cv-qualified type where the cv-unqualified type is
23431 dependent.
23432 No code is necessary for this bullet; the code below handles
23433 cv-qualified types, and we don't want to strip aliases with
23434 TYPE_MAIN_VARIANT because of DR 1558. */
23435 /* -- a compound type constructed from any dependent type. */
23436 if (TYPE_PTRMEM_P (type))
23437 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23438 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23439 (type)));
23440 else if (TYPE_PTR_P (type)
23441 || TREE_CODE (type) == REFERENCE_TYPE)
23442 return dependent_type_p (TREE_TYPE (type));
23443 else if (TREE_CODE (type) == FUNCTION_TYPE
23444 || TREE_CODE (type) == METHOD_TYPE)
23446 tree arg_type;
23448 if (dependent_type_p (TREE_TYPE (type)))
23449 return true;
23450 for (arg_type = TYPE_ARG_TYPES (type);
23451 arg_type;
23452 arg_type = TREE_CHAIN (arg_type))
23453 if (dependent_type_p (TREE_VALUE (arg_type)))
23454 return true;
23455 if (cxx_dialect >= cxx1z)
23457 /* A value-dependent noexcept-specifier makes the type dependent. */
23458 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23459 if (spec && TREE_PURPOSE (spec)
23460 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23461 return true;
23463 return false;
23465 /* -- an array type constructed from any dependent type or whose
23466 size is specified by a constant expression that is
23467 value-dependent.
23469 We checked for type- and value-dependence of the bounds in
23470 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23471 if (TREE_CODE (type) == ARRAY_TYPE)
23473 if (TYPE_DOMAIN (type)
23474 && dependent_type_p (TYPE_DOMAIN (type)))
23475 return true;
23476 return dependent_type_p (TREE_TYPE (type));
23479 /* -- a template-id in which either the template name is a template
23480 parameter ... */
23481 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23482 return true;
23483 /* ... or any of the template arguments is a dependent type or
23484 an expression that is type-dependent or value-dependent. */
23485 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23486 && (any_dependent_template_arguments_p
23487 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23488 return true;
23490 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23491 dependent; if the argument of the `typeof' expression is not
23492 type-dependent, then it should already been have resolved. */
23493 if (TREE_CODE (type) == TYPEOF_TYPE
23494 || TREE_CODE (type) == DECLTYPE_TYPE
23495 || TREE_CODE (type) == UNDERLYING_TYPE)
23496 return true;
23498 /* A template argument pack is dependent if any of its packed
23499 arguments are. */
23500 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23502 tree args = ARGUMENT_PACK_ARGS (type);
23503 int i, len = TREE_VEC_LENGTH (args);
23504 for (i = 0; i < len; ++i)
23505 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23506 return true;
23509 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23510 be template parameters. */
23511 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23512 return true;
23514 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23515 return true;
23517 /* The standard does not specifically mention types that are local
23518 to template functions or local classes, but they should be
23519 considered dependent too. For example:
23521 template <int I> void f() {
23522 enum E { a = I };
23523 S<sizeof (E)> s;
23526 The size of `E' cannot be known until the value of `I' has been
23527 determined. Therefore, `E' must be considered dependent. */
23528 scope = TYPE_CONTEXT (type);
23529 if (scope && TYPE_P (scope))
23530 return dependent_type_p (scope);
23531 /* Don't use type_dependent_expression_p here, as it can lead
23532 to infinite recursion trying to determine whether a lambda
23533 nested in a lambda is dependent (c++/47687). */
23534 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23535 && DECL_LANG_SPECIFIC (scope)
23536 && DECL_TEMPLATE_INFO (scope)
23537 && (any_dependent_template_arguments_p
23538 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23539 return true;
23541 /* Other types are non-dependent. */
23542 return false;
23545 /* Returns TRUE if TYPE is dependent, in the sense of
23546 [temp.dep.type]. Note that a NULL type is considered dependent. */
23548 bool
23549 dependent_type_p (tree type)
23551 /* If there are no template parameters in scope, then there can't be
23552 any dependent types. */
23553 if (!processing_template_decl)
23555 /* If we are not processing a template, then nobody should be
23556 providing us with a dependent type. */
23557 gcc_assert (type);
23558 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23559 return false;
23562 /* If the type is NULL, we have not computed a type for the entity
23563 in question; in that case, the type is dependent. */
23564 if (!type)
23565 return true;
23567 /* Erroneous types can be considered non-dependent. */
23568 if (type == error_mark_node)
23569 return false;
23571 /* Getting here with global_type_node means we improperly called this
23572 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23573 gcc_checking_assert (type != global_type_node);
23575 /* If we have not already computed the appropriate value for TYPE,
23576 do so now. */
23577 if (!TYPE_DEPENDENT_P_VALID (type))
23579 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23580 TYPE_DEPENDENT_P_VALID (type) = 1;
23583 return TYPE_DEPENDENT_P (type);
23586 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23587 lookup. In other words, a dependent type that is not the current
23588 instantiation. */
23590 bool
23591 dependent_scope_p (tree scope)
23593 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23594 && !currently_open_class (scope));
23597 /* T is a SCOPE_REF; return whether we need to consider it
23598 instantiation-dependent so that we can check access at instantiation
23599 time even though we know which member it resolves to. */
23601 static bool
23602 instantiation_dependent_scope_ref_p (tree t)
23604 if (DECL_P (TREE_OPERAND (t, 1))
23605 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23606 && accessible_in_template_p (TREE_OPERAND (t, 0),
23607 TREE_OPERAND (t, 1)))
23608 return false;
23609 else
23610 return true;
23613 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23614 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23615 expression. */
23617 /* Note that this predicate is not appropriate for general expressions;
23618 only constant expressions (that satisfy potential_constant_expression)
23619 can be tested for value dependence. */
23621 bool
23622 value_dependent_expression_p (tree expression)
23624 if (!processing_template_decl || expression == NULL_TREE)
23625 return false;
23627 /* A name declared with a dependent type. */
23628 if (DECL_P (expression) && type_dependent_expression_p (expression))
23629 return true;
23631 switch (TREE_CODE (expression))
23633 case BASELINK:
23634 /* A dependent member function of the current instantiation. */
23635 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23637 case FUNCTION_DECL:
23638 /* A dependent member function of the current instantiation. */
23639 if (DECL_CLASS_SCOPE_P (expression)
23640 && dependent_type_p (DECL_CONTEXT (expression)))
23641 return true;
23642 break;
23644 case IDENTIFIER_NODE:
23645 /* A name that has not been looked up -- must be dependent. */
23646 return true;
23648 case TEMPLATE_PARM_INDEX:
23649 /* A non-type template parm. */
23650 return true;
23652 case CONST_DECL:
23653 /* A non-type template parm. */
23654 if (DECL_TEMPLATE_PARM_P (expression))
23655 return true;
23656 return value_dependent_expression_p (DECL_INITIAL (expression));
23658 case VAR_DECL:
23659 /* A constant with literal type and is initialized
23660 with an expression that is value-dependent.
23662 Note that a non-dependent parenthesized initializer will have
23663 already been replaced with its constant value, so if we see
23664 a TREE_LIST it must be dependent. */
23665 if (DECL_INITIAL (expression)
23666 && decl_constant_var_p (expression)
23667 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23668 /* cp_finish_decl doesn't fold reference initializers. */
23669 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23670 || type_dependent_expression_p (DECL_INITIAL (expression))
23671 || value_dependent_expression_p (DECL_INITIAL (expression))))
23672 return true;
23673 if (DECL_HAS_VALUE_EXPR_P (expression))
23675 tree value_expr = DECL_VALUE_EXPR (expression);
23676 if (type_dependent_expression_p (value_expr))
23677 return true;
23679 return false;
23681 case DYNAMIC_CAST_EXPR:
23682 case STATIC_CAST_EXPR:
23683 case CONST_CAST_EXPR:
23684 case REINTERPRET_CAST_EXPR:
23685 case CAST_EXPR:
23686 /* These expressions are value-dependent if the type to which
23687 the cast occurs is dependent or the expression being casted
23688 is value-dependent. */
23690 tree type = TREE_TYPE (expression);
23692 if (dependent_type_p (type))
23693 return true;
23695 /* A functional cast has a list of operands. */
23696 expression = TREE_OPERAND (expression, 0);
23697 if (!expression)
23699 /* If there are no operands, it must be an expression such
23700 as "int()". This should not happen for aggregate types
23701 because it would form non-constant expressions. */
23702 gcc_assert (cxx_dialect >= cxx11
23703 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23705 return false;
23708 if (TREE_CODE (expression) == TREE_LIST)
23709 return any_value_dependent_elements_p (expression);
23711 return value_dependent_expression_p (expression);
23714 case SIZEOF_EXPR:
23715 if (SIZEOF_EXPR_TYPE_P (expression))
23716 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23717 /* FALLTHRU */
23718 case ALIGNOF_EXPR:
23719 case TYPEID_EXPR:
23720 /* A `sizeof' expression is value-dependent if the operand is
23721 type-dependent or is a pack expansion. */
23722 expression = TREE_OPERAND (expression, 0);
23723 if (PACK_EXPANSION_P (expression))
23724 return true;
23725 else if (TYPE_P (expression))
23726 return dependent_type_p (expression);
23727 return instantiation_dependent_uneval_expression_p (expression);
23729 case AT_ENCODE_EXPR:
23730 /* An 'encode' expression is value-dependent if the operand is
23731 type-dependent. */
23732 expression = TREE_OPERAND (expression, 0);
23733 return dependent_type_p (expression);
23735 case NOEXCEPT_EXPR:
23736 expression = TREE_OPERAND (expression, 0);
23737 return instantiation_dependent_uneval_expression_p (expression);
23739 case SCOPE_REF:
23740 /* All instantiation-dependent expressions should also be considered
23741 value-dependent. */
23742 return instantiation_dependent_scope_ref_p (expression);
23744 case COMPONENT_REF:
23745 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23746 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23748 case NONTYPE_ARGUMENT_PACK:
23749 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23750 is value-dependent. */
23752 tree values = ARGUMENT_PACK_ARGS (expression);
23753 int i, len = TREE_VEC_LENGTH (values);
23755 for (i = 0; i < len; ++i)
23756 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23757 return true;
23759 return false;
23762 case TRAIT_EXPR:
23764 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23765 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23766 || (type2 ? dependent_type_p (type2) : false));
23769 case MODOP_EXPR:
23770 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23771 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23773 case ARRAY_REF:
23774 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23775 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23777 case ADDR_EXPR:
23779 tree op = TREE_OPERAND (expression, 0);
23780 return (value_dependent_expression_p (op)
23781 || has_value_dependent_address (op));
23784 case REQUIRES_EXPR:
23785 /* Treat all requires-expressions as value-dependent so
23786 we don't try to fold them. */
23787 return true;
23789 case TYPE_REQ:
23790 return dependent_type_p (TREE_OPERAND (expression, 0));
23792 case CALL_EXPR:
23794 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23795 return true;
23796 tree fn = get_callee_fndecl (expression);
23797 int i, nargs;
23798 nargs = call_expr_nargs (expression);
23799 for (i = 0; i < nargs; ++i)
23801 tree op = CALL_EXPR_ARG (expression, i);
23802 /* In a call to a constexpr member function, look through the
23803 implicit ADDR_EXPR on the object argument so that it doesn't
23804 cause the call to be considered value-dependent. We also
23805 look through it in potential_constant_expression. */
23806 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23807 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23808 && TREE_CODE (op) == ADDR_EXPR)
23809 op = TREE_OPERAND (op, 0);
23810 if (value_dependent_expression_p (op))
23811 return true;
23813 return false;
23816 case TEMPLATE_ID_EXPR:
23817 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23818 type-dependent. */
23819 return type_dependent_expression_p (expression)
23820 || variable_concept_p (TREE_OPERAND (expression, 0));
23822 case CONSTRUCTOR:
23824 unsigned ix;
23825 tree val;
23826 if (dependent_type_p (TREE_TYPE (expression)))
23827 return true;
23828 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23829 if (value_dependent_expression_p (val))
23830 return true;
23831 return false;
23834 case STMT_EXPR:
23835 /* Treat a GNU statement expression as dependent to avoid crashing
23836 under instantiate_non_dependent_expr; it can't be constant. */
23837 return true;
23839 default:
23840 /* A constant expression is value-dependent if any subexpression is
23841 value-dependent. */
23842 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23844 case tcc_reference:
23845 case tcc_unary:
23846 case tcc_comparison:
23847 case tcc_binary:
23848 case tcc_expression:
23849 case tcc_vl_exp:
23851 int i, len = cp_tree_operand_length (expression);
23853 for (i = 0; i < len; i++)
23855 tree t = TREE_OPERAND (expression, i);
23857 /* In some cases, some of the operands may be missing.
23858 (For example, in the case of PREDECREMENT_EXPR, the
23859 amount to increment by may be missing.) That doesn't
23860 make the expression dependent. */
23861 if (t && value_dependent_expression_p (t))
23862 return true;
23865 break;
23866 default:
23867 break;
23869 break;
23872 /* The expression is not value-dependent. */
23873 return false;
23876 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23877 [temp.dep.expr]. Note that an expression with no type is
23878 considered dependent. Other parts of the compiler arrange for an
23879 expression with type-dependent subexpressions to have no type, so
23880 this function doesn't have to be fully recursive. */
23882 bool
23883 type_dependent_expression_p (tree expression)
23885 if (!processing_template_decl)
23886 return false;
23888 if (expression == NULL_TREE || expression == error_mark_node)
23889 return false;
23891 /* An unresolved name is always dependent. */
23892 if (identifier_p (expression)
23893 || TREE_CODE (expression) == USING_DECL
23894 || TREE_CODE (expression) == WILDCARD_DECL)
23895 return true;
23897 /* A fold expression is type-dependent. */
23898 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23899 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23900 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23901 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23902 return true;
23904 /* Some expression forms are never type-dependent. */
23905 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23906 || TREE_CODE (expression) == SIZEOF_EXPR
23907 || TREE_CODE (expression) == ALIGNOF_EXPR
23908 || TREE_CODE (expression) == AT_ENCODE_EXPR
23909 || TREE_CODE (expression) == NOEXCEPT_EXPR
23910 || TREE_CODE (expression) == TRAIT_EXPR
23911 || TREE_CODE (expression) == TYPEID_EXPR
23912 || TREE_CODE (expression) == DELETE_EXPR
23913 || TREE_CODE (expression) == VEC_DELETE_EXPR
23914 || TREE_CODE (expression) == THROW_EXPR
23915 || TREE_CODE (expression) == REQUIRES_EXPR)
23916 return false;
23918 /* The types of these expressions depends only on the type to which
23919 the cast occurs. */
23920 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23921 || TREE_CODE (expression) == STATIC_CAST_EXPR
23922 || TREE_CODE (expression) == CONST_CAST_EXPR
23923 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23924 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23925 || TREE_CODE (expression) == CAST_EXPR)
23926 return dependent_type_p (TREE_TYPE (expression));
23928 /* The types of these expressions depends only on the type created
23929 by the expression. */
23930 if (TREE_CODE (expression) == NEW_EXPR
23931 || TREE_CODE (expression) == VEC_NEW_EXPR)
23933 /* For NEW_EXPR tree nodes created inside a template, either
23934 the object type itself or a TREE_LIST may appear as the
23935 operand 1. */
23936 tree type = TREE_OPERAND (expression, 1);
23937 if (TREE_CODE (type) == TREE_LIST)
23938 /* This is an array type. We need to check array dimensions
23939 as well. */
23940 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23941 || value_dependent_expression_p
23942 (TREE_OPERAND (TREE_VALUE (type), 1));
23943 else
23944 return dependent_type_p (type);
23947 if (TREE_CODE (expression) == SCOPE_REF)
23949 tree scope = TREE_OPERAND (expression, 0);
23950 tree name = TREE_OPERAND (expression, 1);
23952 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23953 contains an identifier associated by name lookup with one or more
23954 declarations declared with a dependent type, or...a
23955 nested-name-specifier or qualified-id that names a member of an
23956 unknown specialization. */
23957 return (type_dependent_expression_p (name)
23958 || dependent_scope_p (scope));
23961 if (TREE_CODE (expression) == TEMPLATE_DECL
23962 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23963 return uses_outer_template_parms (expression);
23965 if (TREE_CODE (expression) == STMT_EXPR)
23966 expression = stmt_expr_value_expr (expression);
23968 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23970 tree elt;
23971 unsigned i;
23973 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23975 if (type_dependent_expression_p (elt))
23976 return true;
23978 return false;
23981 /* A static data member of the current instantiation with incomplete
23982 array type is type-dependent, as the definition and specializations
23983 can have different bounds. */
23984 if (VAR_P (expression)
23985 && DECL_CLASS_SCOPE_P (expression)
23986 && dependent_type_p (DECL_CONTEXT (expression))
23987 && VAR_HAD_UNKNOWN_BOUND (expression))
23988 return true;
23990 /* An array of unknown bound depending on a variadic parameter, eg:
23992 template<typename... Args>
23993 void foo (Args... args)
23995 int arr[] = { args... };
23998 template<int... vals>
23999 void bar ()
24001 int arr[] = { vals... };
24004 If the array has no length and has an initializer, it must be that
24005 we couldn't determine its length in cp_complete_array_type because
24006 it is dependent. */
24007 if (VAR_P (expression)
24008 && TREE_TYPE (expression) != NULL_TREE
24009 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24010 && !TYPE_DOMAIN (TREE_TYPE (expression))
24011 && DECL_INITIAL (expression))
24012 return true;
24014 /* A function or variable template-id is type-dependent if it has any
24015 dependent template arguments. */
24016 if (VAR_OR_FUNCTION_DECL_P (expression)
24017 && DECL_LANG_SPECIFIC (expression)
24018 && DECL_TEMPLATE_INFO (expression))
24020 /* Consider the innermost template arguments, since those are the ones
24021 that come from the template-id; the template arguments for the
24022 enclosing class do not make it type-dependent unless they are used in
24023 the type of the decl. */
24024 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24025 && (any_dependent_template_arguments_p
24026 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24027 return true;
24029 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24030 type-dependent. Checking this is important for functions with auto
24031 return type, which looks like a dependent type. */
24032 if (TREE_CODE (expression) == FUNCTION_DECL
24033 && (!DECL_CLASS_SCOPE_P (expression)
24034 || !dependent_type_p (DECL_CONTEXT (expression)))
24035 && (!DECL_FRIEND_CONTEXT (expression)
24036 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24037 && !DECL_LOCAL_FUNCTION_P (expression))
24039 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24040 || undeduced_auto_decl (expression));
24041 return false;
24045 /* Always dependent, on the number of arguments if nothing else. */
24046 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24047 return true;
24049 if (TREE_TYPE (expression) == unknown_type_node)
24051 if (TREE_CODE (expression) == ADDR_EXPR)
24052 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24053 if (TREE_CODE (expression) == COMPONENT_REF
24054 || TREE_CODE (expression) == OFFSET_REF)
24056 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24057 return true;
24058 expression = TREE_OPERAND (expression, 1);
24059 if (identifier_p (expression))
24060 return false;
24062 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24063 if (TREE_CODE (expression) == SCOPE_REF)
24064 return false;
24066 if (BASELINK_P (expression))
24068 if (BASELINK_OPTYPE (expression)
24069 && dependent_type_p (BASELINK_OPTYPE (expression)))
24070 return true;
24071 expression = BASELINK_FUNCTIONS (expression);
24074 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24076 if (any_dependent_template_arguments_p
24077 (TREE_OPERAND (expression, 1)))
24078 return true;
24079 expression = TREE_OPERAND (expression, 0);
24080 if (identifier_p (expression))
24081 return true;
24084 gcc_assert (TREE_CODE (expression) == OVERLOAD
24085 || TREE_CODE (expression) == FUNCTION_DECL);
24087 for (lkp_iterator iter (expression); iter; ++iter)
24088 if (type_dependent_expression_p (*iter))
24089 return true;
24091 return false;
24094 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24096 /* Dependent type attributes might not have made it from the decl to
24097 the type yet. */
24098 if (DECL_P (expression)
24099 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24100 return true;
24102 return (dependent_type_p (TREE_TYPE (expression)));
24105 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24106 type-dependent if the expression refers to a member of the current
24107 instantiation and the type of the referenced member is dependent, or the
24108 class member access expression refers to a member of an unknown
24109 specialization.
24111 This function returns true if the OBJECT in such a class member access
24112 expression is of an unknown specialization. */
24114 bool
24115 type_dependent_object_expression_p (tree object)
24117 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24118 dependent. */
24119 if (TREE_CODE (object) == IDENTIFIER_NODE)
24120 return true;
24121 tree scope = TREE_TYPE (object);
24122 return (!scope || dependent_scope_p (scope));
24125 /* walk_tree callback function for instantiation_dependent_expression_p,
24126 below. Returns non-zero if a dependent subexpression is found. */
24128 static tree
24129 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24130 void * /*data*/)
24132 if (TYPE_P (*tp))
24134 /* We don't have to worry about decltype currently because decltype
24135 of an instantiation-dependent expr is a dependent type. This
24136 might change depending on the resolution of DR 1172. */
24137 *walk_subtrees = false;
24138 return NULL_TREE;
24140 enum tree_code code = TREE_CODE (*tp);
24141 switch (code)
24143 /* Don't treat an argument list as dependent just because it has no
24144 TREE_TYPE. */
24145 case TREE_LIST:
24146 case TREE_VEC:
24147 return NULL_TREE;
24149 case TEMPLATE_PARM_INDEX:
24150 return *tp;
24152 /* Handle expressions with type operands. */
24153 case SIZEOF_EXPR:
24154 case ALIGNOF_EXPR:
24155 case TYPEID_EXPR:
24156 case AT_ENCODE_EXPR:
24158 tree op = TREE_OPERAND (*tp, 0);
24159 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24160 op = TREE_TYPE (op);
24161 if (TYPE_P (op))
24163 if (dependent_type_p (op))
24164 return *tp;
24165 else
24167 *walk_subtrees = false;
24168 return NULL_TREE;
24171 break;
24174 case COMPONENT_REF:
24175 if (identifier_p (TREE_OPERAND (*tp, 1)))
24176 /* In a template, finish_class_member_access_expr creates a
24177 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24178 type-dependent, so that we can check access control at
24179 instantiation time (PR 42277). See also Core issue 1273. */
24180 return *tp;
24181 break;
24183 case SCOPE_REF:
24184 if (instantiation_dependent_scope_ref_p (*tp))
24185 return *tp;
24186 else
24187 break;
24189 /* Treat statement-expressions as dependent. */
24190 case BIND_EXPR:
24191 return *tp;
24193 /* Treat requires-expressions as dependent. */
24194 case REQUIRES_EXPR:
24195 return *tp;
24197 case CALL_EXPR:
24198 /* Treat calls to function concepts as dependent. */
24199 if (function_concept_check_p (*tp))
24200 return *tp;
24201 break;
24203 case TEMPLATE_ID_EXPR:
24204 /* And variable concepts. */
24205 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24206 return *tp;
24207 break;
24209 default:
24210 break;
24213 if (type_dependent_expression_p (*tp))
24214 return *tp;
24215 else
24216 return NULL_TREE;
24219 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24220 sense defined by the ABI:
24222 "An expression is instantiation-dependent if it is type-dependent
24223 or value-dependent, or it has a subexpression that is type-dependent
24224 or value-dependent."
24226 Except don't actually check value-dependence for unevaluated expressions,
24227 because in sizeof(i) we don't care about the value of i. Checking
24228 type-dependence will in turn check value-dependence of array bounds/template
24229 arguments as needed. */
24231 bool
24232 instantiation_dependent_uneval_expression_p (tree expression)
24234 tree result;
24236 if (!processing_template_decl)
24237 return false;
24239 if (expression == error_mark_node)
24240 return false;
24242 result = cp_walk_tree_without_duplicates (&expression,
24243 instantiation_dependent_r, NULL);
24244 return result != NULL_TREE;
24247 /* As above, but also check value-dependence of the expression as a whole. */
24249 bool
24250 instantiation_dependent_expression_p (tree expression)
24252 return (instantiation_dependent_uneval_expression_p (expression)
24253 || value_dependent_expression_p (expression));
24256 /* Like type_dependent_expression_p, but it also works while not processing
24257 a template definition, i.e. during substitution or mangling. */
24259 bool
24260 type_dependent_expression_p_push (tree expr)
24262 bool b;
24263 ++processing_template_decl;
24264 b = type_dependent_expression_p (expr);
24265 --processing_template_decl;
24266 return b;
24269 /* Returns TRUE if ARGS contains a type-dependent expression. */
24271 bool
24272 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24274 unsigned int i;
24275 tree arg;
24277 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24279 if (type_dependent_expression_p (arg))
24280 return true;
24282 return false;
24285 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24286 expressions) contains any type-dependent expressions. */
24288 bool
24289 any_type_dependent_elements_p (const_tree list)
24291 for (; list; list = TREE_CHAIN (list))
24292 if (type_dependent_expression_p (TREE_VALUE (list)))
24293 return true;
24295 return false;
24298 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24299 expressions) contains any value-dependent expressions. */
24301 bool
24302 any_value_dependent_elements_p (const_tree list)
24304 for (; list; list = TREE_CHAIN (list))
24305 if (value_dependent_expression_p (TREE_VALUE (list)))
24306 return true;
24308 return false;
24311 /* Returns TRUE if the ARG (a template argument) is dependent. */
24313 bool
24314 dependent_template_arg_p (tree arg)
24316 if (!processing_template_decl)
24317 return false;
24319 /* Assume a template argument that was wrongly written by the user
24320 is dependent. This is consistent with what
24321 any_dependent_template_arguments_p [that calls this function]
24322 does. */
24323 if (!arg || arg == error_mark_node)
24324 return true;
24326 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24327 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24329 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24330 return true;
24331 if (TREE_CODE (arg) == TEMPLATE_DECL)
24333 if (DECL_TEMPLATE_PARM_P (arg))
24334 return true;
24335 /* A member template of a dependent class is not necessarily
24336 type-dependent, but it is a dependent template argument because it
24337 will be a member of an unknown specialization to that template. */
24338 tree scope = CP_DECL_CONTEXT (arg);
24339 return TYPE_P (scope) && dependent_type_p (scope);
24341 else if (ARGUMENT_PACK_P (arg))
24343 tree args = ARGUMENT_PACK_ARGS (arg);
24344 int i, len = TREE_VEC_LENGTH (args);
24345 for (i = 0; i < len; ++i)
24347 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24348 return true;
24351 return false;
24353 else if (TYPE_P (arg))
24354 return dependent_type_p (arg);
24355 else
24356 return (type_dependent_expression_p (arg)
24357 || value_dependent_expression_p (arg));
24360 /* Returns true if ARGS (a collection of template arguments) contains
24361 any types that require structural equality testing. */
24363 bool
24364 any_template_arguments_need_structural_equality_p (tree args)
24366 int i;
24367 int j;
24369 if (!args)
24370 return false;
24371 if (args == error_mark_node)
24372 return true;
24374 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24376 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24377 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24379 tree arg = TREE_VEC_ELT (level, j);
24380 tree packed_args = NULL_TREE;
24381 int k, len = 1;
24383 if (ARGUMENT_PACK_P (arg))
24385 /* Look inside the argument pack. */
24386 packed_args = ARGUMENT_PACK_ARGS (arg);
24387 len = TREE_VEC_LENGTH (packed_args);
24390 for (k = 0; k < len; ++k)
24392 if (packed_args)
24393 arg = TREE_VEC_ELT (packed_args, k);
24395 if (error_operand_p (arg))
24396 return true;
24397 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24398 continue;
24399 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24400 return true;
24401 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24402 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24403 return true;
24408 return false;
24411 /* Returns true if ARGS (a collection of template arguments) contains
24412 any dependent arguments. */
24414 bool
24415 any_dependent_template_arguments_p (const_tree args)
24417 int i;
24418 int j;
24420 if (!args)
24421 return false;
24422 if (args == error_mark_node)
24423 return true;
24425 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24427 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24428 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24429 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24430 return true;
24433 return false;
24436 /* Returns TRUE if the template TMPL is type-dependent. */
24438 bool
24439 dependent_template_p (tree tmpl)
24441 if (TREE_CODE (tmpl) == OVERLOAD)
24443 for (lkp_iterator iter (tmpl); iter; ++iter)
24444 if (dependent_template_p (*iter))
24445 return true;
24446 return false;
24449 /* Template template parameters are dependent. */
24450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24451 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24452 return true;
24453 /* So are names that have not been looked up. */
24454 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24455 return true;
24456 return false;
24459 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24461 bool
24462 dependent_template_id_p (tree tmpl, tree args)
24464 return (dependent_template_p (tmpl)
24465 || any_dependent_template_arguments_p (args));
24468 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24469 are dependent. */
24471 bool
24472 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24474 int i;
24476 if (!processing_template_decl)
24477 return false;
24479 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24481 tree decl = TREE_VEC_ELT (declv, i);
24482 tree init = TREE_VEC_ELT (initv, i);
24483 tree cond = TREE_VEC_ELT (condv, i);
24484 tree incr = TREE_VEC_ELT (incrv, i);
24486 if (type_dependent_expression_p (decl)
24487 || TREE_CODE (decl) == SCOPE_REF)
24488 return true;
24490 if (init && type_dependent_expression_p (init))
24491 return true;
24493 if (type_dependent_expression_p (cond))
24494 return true;
24496 if (COMPARISON_CLASS_P (cond)
24497 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24498 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24499 return true;
24501 if (TREE_CODE (incr) == MODOP_EXPR)
24503 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24504 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24505 return true;
24507 else if (type_dependent_expression_p (incr))
24508 return true;
24509 else if (TREE_CODE (incr) == MODIFY_EXPR)
24511 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24512 return true;
24513 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24515 tree t = TREE_OPERAND (incr, 1);
24516 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24517 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24518 return true;
24523 return false;
24526 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24527 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24528 no such TYPE can be found. Note that this function peers inside
24529 uninstantiated templates and therefore should be used only in
24530 extremely limited situations. ONLY_CURRENT_P restricts this
24531 peering to the currently open classes hierarchy (which is required
24532 when comparing types). */
24534 tree
24535 resolve_typename_type (tree type, bool only_current_p)
24537 tree scope;
24538 tree name;
24539 tree decl;
24540 int quals;
24541 tree pushed_scope;
24542 tree result;
24544 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24546 scope = TYPE_CONTEXT (type);
24547 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24548 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24549 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24550 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24551 identifier of the TYPENAME_TYPE anymore.
24552 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24553 TYPENAME_TYPE instead, we avoid messing up with a possible
24554 typedef variant case. */
24555 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24557 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24558 it first before we can figure out what NAME refers to. */
24559 if (TREE_CODE (scope) == TYPENAME_TYPE)
24561 if (TYPENAME_IS_RESOLVING_P (scope))
24562 /* Given a class template A with a dependent base with nested type C,
24563 typedef typename A::C::C C will land us here, as trying to resolve
24564 the initial A::C leads to the local C typedef, which leads back to
24565 A::C::C. So we break the recursion now. */
24566 return type;
24567 else
24568 scope = resolve_typename_type (scope, only_current_p);
24570 /* If we don't know what SCOPE refers to, then we cannot resolve the
24571 TYPENAME_TYPE. */
24572 if (!CLASS_TYPE_P (scope))
24573 return type;
24574 /* If this is a typedef, we don't want to look inside (c++/11987). */
24575 if (typedef_variant_p (type))
24576 return type;
24577 /* If SCOPE isn't the template itself, it will not have a valid
24578 TYPE_FIELDS list. */
24579 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24580 /* scope is either the template itself or a compatible instantiation
24581 like X<T>, so look up the name in the original template. */
24582 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24583 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24584 gcc_checking_assert (uses_template_parms (scope));
24585 /* If scope has no fields, it can't be a current instantiation. Check this
24586 before currently_open_class to avoid infinite recursion (71515). */
24587 if (!TYPE_FIELDS (scope))
24588 return type;
24589 /* If the SCOPE is not the current instantiation, there's no reason
24590 to look inside it. */
24591 if (only_current_p && !currently_open_class (scope))
24592 return type;
24593 /* Enter the SCOPE so that name lookup will be resolved as if we
24594 were in the class definition. In particular, SCOPE will no
24595 longer be considered a dependent type. */
24596 pushed_scope = push_scope (scope);
24597 /* Look up the declaration. */
24598 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24599 tf_warning_or_error);
24601 result = NULL_TREE;
24603 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24604 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24605 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24606 if (!decl)
24607 /*nop*/;
24608 else if (identifier_p (fullname)
24609 && TREE_CODE (decl) == TYPE_DECL)
24611 result = TREE_TYPE (decl);
24612 if (result == error_mark_node)
24613 result = NULL_TREE;
24615 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24616 && DECL_CLASS_TEMPLATE_P (decl))
24618 /* Obtain the template and the arguments. */
24619 tree tmpl = TREE_OPERAND (fullname, 0);
24620 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24622 /* We get here with a plain identifier because a previous tentative
24623 parse of the nested-name-specifier as part of a ptr-operator saw
24624 ::template X<A>. The use of ::template is necessary in a
24625 ptr-operator, but wrong in a declarator-id.
24627 [temp.names]: In a qualified-id of a declarator-id, the keyword
24628 template shall not appear at the top level. */
24629 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24630 "keyword %<template%> not allowed in declarator-id");
24631 tmpl = decl;
24633 tree args = TREE_OPERAND (fullname, 1);
24634 /* Instantiate the template. */
24635 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24636 /*entering_scope=*/true,
24637 tf_error | tf_user);
24638 if (result == error_mark_node)
24639 result = NULL_TREE;
24642 /* Leave the SCOPE. */
24643 if (pushed_scope)
24644 pop_scope (pushed_scope);
24646 /* If we failed to resolve it, return the original typename. */
24647 if (!result)
24648 return type;
24650 /* If lookup found a typename type, resolve that too. */
24651 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24653 /* Ill-formed programs can cause infinite recursion here, so we
24654 must catch that. */
24655 TYPENAME_IS_RESOLVING_P (result) = 1;
24656 result = resolve_typename_type (result, only_current_p);
24657 TYPENAME_IS_RESOLVING_P (result) = 0;
24660 /* Qualify the resulting type. */
24661 quals = cp_type_quals (type);
24662 if (quals)
24663 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24665 return result;
24668 /* EXPR is an expression which is not type-dependent. Return a proxy
24669 for EXPR that can be used to compute the types of larger
24670 expressions containing EXPR. */
24672 tree
24673 build_non_dependent_expr (tree expr)
24675 tree inner_expr;
24677 /* When checking, try to get a constant value for all non-dependent
24678 expressions in order to expose bugs in *_dependent_expression_p
24679 and constexpr. This can affect code generation, see PR70704, so
24680 only do this for -fchecking=2. */
24681 if (flag_checking > 1
24682 && cxx_dialect >= cxx11
24683 /* Don't do this during nsdmi parsing as it can lead to
24684 unexpected recursive instantiations. */
24685 && !parsing_nsdmi ()
24686 /* Don't do this during concept expansion either and for
24687 the same reason. */
24688 && !expanding_concept ())
24689 fold_non_dependent_expr (expr);
24691 /* Preserve OVERLOADs; the functions must be available to resolve
24692 types. */
24693 inner_expr = expr;
24694 if (TREE_CODE (inner_expr) == STMT_EXPR)
24695 inner_expr = stmt_expr_value_expr (inner_expr);
24696 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24697 inner_expr = TREE_OPERAND (inner_expr, 0);
24698 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24699 inner_expr = TREE_OPERAND (inner_expr, 1);
24700 if (is_overloaded_fn (inner_expr)
24701 || TREE_CODE (inner_expr) == OFFSET_REF)
24702 return expr;
24703 /* There is no need to return a proxy for a variable. */
24704 if (VAR_P (expr))
24705 return expr;
24706 /* Preserve string constants; conversions from string constants to
24707 "char *" are allowed, even though normally a "const char *"
24708 cannot be used to initialize a "char *". */
24709 if (TREE_CODE (expr) == STRING_CST)
24710 return expr;
24711 /* Preserve void and arithmetic constants, as an optimization -- there is no
24712 reason to create a new node. */
24713 if (TREE_CODE (expr) == VOID_CST
24714 || TREE_CODE (expr) == INTEGER_CST
24715 || TREE_CODE (expr) == REAL_CST)
24716 return expr;
24717 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24718 There is at least one place where we want to know that a
24719 particular expression is a throw-expression: when checking a ?:
24720 expression, there are special rules if the second or third
24721 argument is a throw-expression. */
24722 if (TREE_CODE (expr) == THROW_EXPR)
24723 return expr;
24725 /* Don't wrap an initializer list, we need to be able to look inside. */
24726 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24727 return expr;
24729 /* Don't wrap a dummy object, we need to be able to test for it. */
24730 if (is_dummy_object (expr))
24731 return expr;
24733 if (TREE_CODE (expr) == COND_EXPR)
24734 return build3 (COND_EXPR,
24735 TREE_TYPE (expr),
24736 TREE_OPERAND (expr, 0),
24737 (TREE_OPERAND (expr, 1)
24738 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24739 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24740 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24741 if (TREE_CODE (expr) == COMPOUND_EXPR
24742 && !COMPOUND_EXPR_OVERLOADED (expr))
24743 return build2 (COMPOUND_EXPR,
24744 TREE_TYPE (expr),
24745 TREE_OPERAND (expr, 0),
24746 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24748 /* If the type is unknown, it can't really be non-dependent */
24749 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24751 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24752 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24755 /* ARGS is a vector of expressions as arguments to a function call.
24756 Replace the arguments with equivalent non-dependent expressions.
24757 This modifies ARGS in place. */
24759 void
24760 make_args_non_dependent (vec<tree, va_gc> *args)
24762 unsigned int ix;
24763 tree arg;
24765 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24767 tree newarg = build_non_dependent_expr (arg);
24768 if (newarg != arg)
24769 (*args)[ix] = newarg;
24773 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24774 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24775 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24777 static tree
24778 make_auto_1 (tree name, bool set_canonical)
24780 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24781 TYPE_NAME (au) = build_decl (input_location,
24782 TYPE_DECL, name, au);
24783 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24784 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24785 (0, processing_template_decl + 1, processing_template_decl + 1,
24786 TYPE_NAME (au), NULL_TREE);
24787 if (set_canonical)
24788 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24789 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24790 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24792 return au;
24795 tree
24796 make_decltype_auto (void)
24798 return make_auto_1 (decltype_auto_identifier, true);
24801 tree
24802 make_auto (void)
24804 return make_auto_1 (auto_identifier, true);
24807 /* Return a C++17 deduction placeholder for class template TMPL. */
24809 tree
24810 make_template_placeholder (tree tmpl)
24812 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24813 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24814 return t;
24817 /* True iff T is a C++17 class template deduction placeholder. */
24819 bool
24820 template_placeholder_p (tree t)
24822 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
24825 /* Make a "constrained auto" type-specifier. This is an
24826 auto type with constraints that must be associated after
24827 deduction. The constraint is formed from the given
24828 CONC and its optional sequence of arguments, which are
24829 non-null if written as partial-concept-id. */
24831 tree
24832 make_constrained_auto (tree con, tree args)
24834 tree type = make_auto_1 (auto_identifier, false);
24836 /* Build the constraint. */
24837 tree tmpl = DECL_TI_TEMPLATE (con);
24838 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24839 expr = build_concept_check (expr, type, args);
24841 tree constr = normalize_expression (expr);
24842 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24844 /* Our canonical type depends on the constraint. */
24845 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24847 /* Attach the constraint to the type declaration. */
24848 tree decl = TYPE_NAME (type);
24849 return decl;
24852 /* Given type ARG, return std::initializer_list<ARG>. */
24854 static tree
24855 listify (tree arg)
24857 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24859 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24861 gcc_rich_location richloc (input_location);
24862 maybe_add_include_fixit (&richloc, "<initializer_list>");
24863 error_at_rich_loc (&richloc,
24864 "deducing from brace-enclosed initializer list"
24865 " requires #include <initializer_list>");
24867 return error_mark_node;
24869 tree argvec = make_tree_vec (1);
24870 TREE_VEC_ELT (argvec, 0) = arg;
24872 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24873 NULL_TREE, 0, tf_warning_or_error);
24876 /* Replace auto in TYPE with std::initializer_list<auto>. */
24878 static tree
24879 listify_autos (tree type, tree auto_node)
24881 tree init_auto = listify (auto_node);
24882 tree argvec = make_tree_vec (1);
24883 TREE_VEC_ELT (argvec, 0) = init_auto;
24884 if (processing_template_decl)
24885 argvec = add_to_template_args (current_template_args (), argvec);
24886 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24889 /* Hash traits for hashing possibly constrained 'auto'
24890 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24892 struct auto_hash : default_hash_traits<tree>
24894 static inline hashval_t hash (tree);
24895 static inline bool equal (tree, tree);
24898 /* Hash the 'auto' T. */
24900 inline hashval_t
24901 auto_hash::hash (tree t)
24903 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24904 /* Matching constrained-type-specifiers denote the same template
24905 parameter, so hash the constraint. */
24906 return hash_placeholder_constraint (c);
24907 else
24908 /* But unconstrained autos are all separate, so just hash the pointer. */
24909 return iterative_hash_object (t, 0);
24912 /* Compare two 'auto's. */
24914 inline bool
24915 auto_hash::equal (tree t1, tree t2)
24917 if (t1 == t2)
24918 return true;
24920 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24921 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24923 /* Two unconstrained autos are distinct. */
24924 if (!c1 || !c2)
24925 return false;
24927 return equivalent_placeholder_constraints (c1, c2);
24930 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24931 constrained) auto, add it to the vector. */
24933 static int
24934 extract_autos_r (tree t, void *data)
24936 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24937 if (is_auto (t))
24939 /* All the autos were built with index 0; fix that up now. */
24940 tree *p = hash.find_slot (t, INSERT);
24941 unsigned idx;
24942 if (*p)
24943 /* If this is a repeated constrained-type-specifier, use the index we
24944 chose before. */
24945 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24946 else
24948 /* Otherwise this is new, so use the current count. */
24949 *p = t;
24950 idx = hash.elements () - 1;
24952 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24955 /* Always keep walking. */
24956 return 0;
24959 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24960 says they can appear anywhere in the type. */
24962 static tree
24963 extract_autos (tree type)
24965 hash_set<tree> visited;
24966 hash_table<auto_hash> hash (2);
24968 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24970 tree tree_vec = make_tree_vec (hash.elements());
24971 for (hash_table<auto_hash>::iterator iter = hash.begin();
24972 iter != hash.end(); ++iter)
24974 tree elt = *iter;
24975 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24976 TREE_VEC_ELT (tree_vec, i)
24977 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24980 return tree_vec;
24983 /* The stem for deduction guide names. */
24984 const char *const dguide_base = "__dguide_";
24986 /* Return the name for a deduction guide for class template TMPL. */
24988 tree
24989 dguide_name (tree tmpl)
24991 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24992 tree tname = TYPE_IDENTIFIER (type);
24993 char *buf = (char *) alloca (1 + strlen (dguide_base)
24994 + IDENTIFIER_LENGTH (tname));
24995 memcpy (buf, dguide_base, strlen (dguide_base));
24996 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24997 IDENTIFIER_LENGTH (tname) + 1);
24998 tree dname = get_identifier (buf);
24999 TREE_TYPE (dname) = type;
25000 return dname;
25003 /* True if NAME is the name of a deduction guide. */
25005 bool
25006 dguide_name_p (tree name)
25008 return (TREE_TYPE (name)
25009 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25010 strlen (dguide_base)));
25013 /* True if FN is a deduction guide. */
25015 bool
25016 deduction_guide_p (const_tree fn)
25018 if (DECL_P (fn))
25019 if (tree name = DECL_NAME (fn))
25020 return dguide_name_p (name);
25021 return false;
25024 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25026 bool
25027 copy_guide_p (const_tree fn)
25029 gcc_assert (deduction_guide_p (fn));
25030 if (!DECL_ARTIFICIAL (fn))
25031 return false;
25032 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25033 return (TREE_CHAIN (parms) == void_list_node
25034 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25037 /* True if FN is a guide generated from a constructor template. */
25039 bool
25040 template_guide_p (const_tree fn)
25042 gcc_assert (deduction_guide_p (fn));
25043 if (!DECL_ARTIFICIAL (fn))
25044 return false;
25045 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25047 tree tmpl = DECL_TI_TEMPLATE (ctor);
25048 return PRIMARY_TEMPLATE_P (tmpl);
25050 return false;
25053 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25054 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25055 template parameter types. Note that the handling of template template
25056 parameters relies on current_template_parms being set appropriately for the
25057 new template. */
25059 static tree
25060 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25061 tree tsubst_args, tsubst_flags_t complain)
25063 tree oldidx = get_template_parm_index (olddecl);
25065 tree newtype;
25066 if (TREE_CODE (olddecl) == TYPE_DECL
25067 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25069 tree oldtype = TREE_TYPE (olddecl);
25070 newtype = cxx_make_type (TREE_CODE (oldtype));
25071 TYPE_MAIN_VARIANT (newtype) = newtype;
25072 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25073 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25074 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25076 else
25077 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25078 complain, NULL_TREE);
25080 tree newdecl
25081 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25082 DECL_NAME (olddecl), newtype);
25083 SET_DECL_TEMPLATE_PARM_P (newdecl);
25085 tree newidx;
25086 if (TREE_CODE (olddecl) == TYPE_DECL
25087 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25089 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25090 = build_template_parm_index (index, level, level,
25091 newdecl, newtype);
25092 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25093 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25094 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25095 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25097 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25099 DECL_TEMPLATE_RESULT (newdecl)
25100 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25101 DECL_NAME (olddecl), newtype);
25102 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25103 // First create a copy (ttargs) of tsubst_args with an
25104 // additional level for the template template parameter's own
25105 // template parameters (ttparms).
25106 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25107 (DECL_TEMPLATE_PARMS (olddecl)));
25108 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25109 tree ttargs = make_tree_vec (depth + 1);
25110 for (int i = 0; i < depth; ++i)
25111 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25112 TREE_VEC_ELT (ttargs, depth)
25113 = template_parms_level_to_args (ttparms);
25114 // Substitute ttargs into ttparms to fix references to
25115 // other template parameters.
25116 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25117 complain);
25118 // Now substitute again with args based on tparms, to reduce
25119 // the level of the ttparms.
25120 ttargs = current_template_args ();
25121 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25122 complain);
25123 // Finally, tack the adjusted parms onto tparms.
25124 ttparms = tree_cons (size_int (depth), ttparms,
25125 current_template_parms);
25126 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25129 else
25131 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25132 tree newconst
25133 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25134 TREE_CODE (oldconst),
25135 DECL_NAME (oldconst), newtype);
25136 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25137 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25138 SET_DECL_TEMPLATE_PARM_P (newconst);
25139 newidx = build_template_parm_index (index, level, level,
25140 newconst, newtype);
25141 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25142 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25143 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25146 return newdecl;
25149 /* Returns a C++17 class deduction guide template based on the constructor
25150 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25151 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25153 static tree
25154 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25156 tree type, tparms, targs, fparms, fargs, ci;
25157 bool memtmpl = false;
25158 bool explicit_p;
25159 location_t loc;
25161 if (TYPE_P (ctor))
25163 type = ctor;
25164 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25165 if (copy_p)
25167 type = TREE_TYPE (type);
25168 fparms = tree_cons (NULL_TREE, type, void_list_node);
25170 else
25171 fparms = void_list_node;
25173 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25174 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25175 targs = CLASSTYPE_TI_ARGS (type);
25176 ci = NULL_TREE;
25177 fargs = NULL_TREE;
25178 loc = DECL_SOURCE_LOCATION (ctmpl);
25179 explicit_p = false;
25181 else
25183 ++processing_template_decl;
25185 tree fn_tmpl
25186 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25187 : DECL_TI_TEMPLATE (ctor));
25188 if (outer_args)
25189 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25190 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25192 type = DECL_CONTEXT (ctor);
25194 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25195 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25196 fully specialized args for the enclosing class. Strip those off, as
25197 the deduction guide won't have those template parameters. */
25198 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25199 TMPL_PARMS_DEPTH (tparms));
25200 /* Discard the 'this' parameter. */
25201 fparms = FUNCTION_ARG_CHAIN (ctor);
25202 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25203 ci = get_constraints (ctor);
25204 loc = DECL_SOURCE_LOCATION (ctor);
25205 explicit_p = DECL_NONCONVERTING_P (ctor);
25207 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25209 memtmpl = true;
25211 /* For a member template constructor, we need to flatten the two
25212 template parameter lists into one, and then adjust the function
25213 signature accordingly. This gets...complicated. */
25214 tree save_parms = current_template_parms;
25216 /* For a member template we should have two levels of parms/args, one
25217 for the class and one for the constructor. We stripped
25218 specialized args for further enclosing classes above. */
25219 const int depth = 2;
25220 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25222 /* Template args for translating references to the two-level template
25223 parameters into references to the one-level template parameters we
25224 are creating. */
25225 tree tsubst_args = copy_node (targs);
25226 TMPL_ARGS_LEVEL (tsubst_args, depth)
25227 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25229 /* Template parms for the constructor template. */
25230 tree ftparms = TREE_VALUE (tparms);
25231 unsigned flen = TREE_VEC_LENGTH (ftparms);
25232 /* Template parms for the class template. */
25233 tparms = TREE_CHAIN (tparms);
25234 tree ctparms = TREE_VALUE (tparms);
25235 unsigned clen = TREE_VEC_LENGTH (ctparms);
25236 /* Template parms for the deduction guide start as a copy of the
25237 template parms for the class. We set current_template_parms for
25238 lookup_template_class_1. */
25239 current_template_parms = tparms = copy_node (tparms);
25240 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25241 for (unsigned i = 0; i < clen; ++i)
25242 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25244 /* Now we need to rewrite the constructor parms to append them to the
25245 class parms. */
25246 for (unsigned i = 0; i < flen; ++i)
25248 unsigned index = i + clen;
25249 unsigned level = 1;
25250 tree oldelt = TREE_VEC_ELT (ftparms, i);
25251 tree olddecl = TREE_VALUE (oldelt);
25252 tree newdecl = rewrite_template_parm (olddecl, index, level,
25253 tsubst_args, complain);
25254 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25255 tsubst_args, complain, ctor);
25256 tree list = build_tree_list (newdef, newdecl);
25257 TEMPLATE_PARM_CONSTRAINTS (list)
25258 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25259 tsubst_args, complain, ctor);
25260 TREE_VEC_ELT (new_vec, index) = list;
25261 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25264 /* Now we have a final set of template parms to substitute into the
25265 function signature. */
25266 targs = template_parms_to_args (tparms);
25267 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25268 complain, ctor);
25269 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25270 if (ci)
25271 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25273 current_template_parms = save_parms;
25275 --processing_template_decl;
25278 if (!memtmpl)
25280 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25281 tparms = copy_node (tparms);
25282 INNERMOST_TEMPLATE_PARMS (tparms)
25283 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25286 tree fntype = build_function_type (type, fparms);
25287 tree ded_fn = build_lang_decl_loc (loc,
25288 FUNCTION_DECL,
25289 dguide_name (type), fntype);
25290 DECL_ARGUMENTS (ded_fn) = fargs;
25291 DECL_ARTIFICIAL (ded_fn) = true;
25292 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25293 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25294 DECL_ARTIFICIAL (ded_tmpl) = true;
25295 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25296 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25297 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25298 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25299 if (DECL_P (ctor))
25300 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25301 if (ci)
25302 set_constraints (ded_tmpl, ci);
25304 return ded_tmpl;
25307 /* Deduce template arguments for the class template placeholder PTYPE for
25308 template TMPL based on the initializer INIT, and return the resulting
25309 type. */
25311 static tree
25312 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25313 tsubst_flags_t complain)
25315 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25317 /* We should have handled this in the caller. */
25318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25319 return ptype;
25320 if (complain & tf_error)
25321 error ("non-class template %qT used without template arguments", tmpl);
25322 return error_mark_node;
25325 tree type = TREE_TYPE (tmpl);
25327 bool try_list_ctor = false;
25329 vec<tree,va_gc> *args;
25330 if (init == NULL_TREE
25331 || TREE_CODE (init) == TREE_LIST)
25332 args = make_tree_vector_from_list (init);
25333 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25335 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25336 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25338 /* As an exception, the first phase in 16.3.1.7 (considering the
25339 initializer list as a single argument) is omitted if the
25340 initializer list consists of a single expression of type cv U,
25341 where U is a specialization of C or a class derived from a
25342 specialization of C. */
25343 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25344 tree etype = TREE_TYPE (elt);
25346 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25347 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25348 int err = unify (tparms, targs, type, etype,
25349 UNIFY_ALLOW_DERIVED, /*explain*/false);
25350 if (err == 0)
25351 try_list_ctor = false;
25352 ggc_free (targs);
25354 if (try_list_ctor || is_std_init_list (type))
25355 args = make_tree_vector_single (init);
25356 else
25357 args = make_tree_vector_from_ctor (init);
25359 else
25360 args = make_tree_vector_single (init);
25362 tree dname = dguide_name (tmpl);
25363 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25364 /*type*/false, /*complain*/false,
25365 /*hidden*/false);
25366 bool elided = false;
25367 if (cands == error_mark_node)
25368 cands = NULL_TREE;
25370 /* Prune explicit deduction guides in copy-initialization context. */
25371 if (flags & LOOKUP_ONLYCONVERTING)
25373 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25374 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25375 elided = true;
25377 if (elided)
25379 /* Found a nonconverting guide, prune the candidates. */
25380 tree pruned = NULL_TREE;
25381 for (lkp_iterator iter (cands); iter; ++iter)
25382 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25383 pruned = lookup_add (*iter, pruned);
25385 cands = pruned;
25389 tree outer_args = NULL_TREE;
25390 if (DECL_CLASS_SCOPE_P (tmpl)
25391 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25393 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25394 type = TREE_TYPE (most_general_template (tmpl));
25397 bool saw_ctor = false;
25398 if (CLASSTYPE_METHOD_VEC (type))
25399 // FIXME cache artificial deduction guides
25400 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25401 iter; ++iter)
25403 tree guide = build_deduction_guide (*iter, outer_args, complain);
25404 if ((flags & LOOKUP_ONLYCONVERTING)
25405 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25406 elided = true;
25407 else
25408 cands = lookup_add (guide, cands);
25410 saw_ctor = true;
25413 tree call = error_mark_node;
25415 /* If this is list-initialization and the class has a list constructor, first
25416 try deducing from the list as a single argument, as [over.match.list]. */
25417 tree list_cands = NULL_TREE;
25418 if (try_list_ctor && cands)
25419 for (lkp_iterator iter (cands); iter; ++iter)
25421 tree dg = *iter;
25422 if (is_list_ctor (dg))
25423 list_cands = lookup_add (dg, list_cands);
25425 if (list_cands)
25427 ++cp_unevaluated_operand;
25428 call = build_new_function_call (list_cands, &args, tf_decltype);
25429 --cp_unevaluated_operand;
25431 if (call == error_mark_node)
25433 /* That didn't work, now try treating the list as a sequence of
25434 arguments. */
25435 release_tree_vector (args);
25436 args = make_tree_vector_from_ctor (init);
25440 /* Maybe generate an implicit deduction guide. */
25441 if (call == error_mark_node && args->length () < 2)
25443 tree gtype = NULL_TREE;
25445 if (args->length () == 1)
25446 /* Generate a copy guide. */
25447 gtype = build_reference_type (type);
25448 else if (!saw_ctor)
25449 /* Generate a default guide. */
25450 gtype = type;
25452 if (gtype)
25454 tree guide = build_deduction_guide (gtype, outer_args, complain);
25455 cands = lookup_add (guide, cands);
25459 if (elided && !cands)
25461 error ("cannot deduce template arguments for copy-initialization"
25462 " of %qT, as it has no non-explicit deduction guides or "
25463 "user-declared constructors", type);
25464 return error_mark_node;
25466 else if (!cands && call == error_mark_node)
25468 error ("cannot deduce template arguments of %qT, as it has no viable "
25469 "deduction guides", type);
25470 return error_mark_node;
25473 if (call == error_mark_node)
25475 ++cp_unevaluated_operand;
25476 call = build_new_function_call (cands, &args, tf_decltype);
25477 --cp_unevaluated_operand;
25480 if (call == error_mark_node && (complain & tf_warning_or_error))
25482 error ("class template argument deduction failed:");
25484 ++cp_unevaluated_operand;
25485 call = build_new_function_call (cands, &args, complain | tf_decltype);
25486 --cp_unevaluated_operand;
25488 if (elided)
25489 inform (input_location, "explicit deduction guides not considered "
25490 "for copy-initialization");
25493 release_tree_vector (args);
25495 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25498 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25499 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25501 tree
25502 do_auto_deduction (tree type, tree init, tree auto_node)
25504 return do_auto_deduction (type, init, auto_node,
25505 tf_warning_or_error,
25506 adc_unspecified);
25509 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25510 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25511 The CONTEXT determines the context in which auto deduction is performed
25512 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25513 OUTER_TARGS are used during template argument deduction
25514 (context == adc_unify) to properly substitute the result, and is ignored
25515 in other contexts.
25517 For partial-concept-ids, extra args may be appended to the list of deduced
25518 template arguments prior to determining constraint satisfaction. */
25520 tree
25521 do_auto_deduction (tree type, tree init, tree auto_node,
25522 tsubst_flags_t complain, auto_deduction_context context,
25523 tree outer_targs, int flags)
25525 tree targs;
25527 if (init == error_mark_node)
25528 return error_mark_node;
25530 if (init && type_dependent_expression_p (init)
25531 && context != adc_unify)
25532 /* Defining a subset of type-dependent expressions that we can deduce
25533 from ahead of time isn't worth the trouble. */
25534 return type;
25536 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25537 /* C++17 class template argument deduction. */
25538 return do_class_deduction (type, tmpl, init, flags, complain);
25540 if (TREE_TYPE (init) == NULL_TREE)
25541 /* Nothing we can do with this, even in deduction context. */
25542 return type;
25544 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25545 with either a new invented type template parameter U or, if the
25546 initializer is a braced-init-list (8.5.4), with
25547 std::initializer_list<U>. */
25548 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25550 if (!DIRECT_LIST_INIT_P (init))
25551 type = listify_autos (type, auto_node);
25552 else if (CONSTRUCTOR_NELTS (init) == 1)
25553 init = CONSTRUCTOR_ELT (init, 0)->value;
25554 else
25556 if (complain & tf_warning_or_error)
25558 if (permerror (input_location, "direct-list-initialization of "
25559 "%<auto%> requires exactly one element"))
25560 inform (input_location,
25561 "for deduction to %<std::initializer_list%>, use copy-"
25562 "list-initialization (i.e. add %<=%> before the %<{%>)");
25564 type = listify_autos (type, auto_node);
25568 if (type == error_mark_node)
25569 return error_mark_node;
25571 init = resolve_nondeduced_context (init, complain);
25573 if (context == adc_decomp_type
25574 && auto_node == type
25575 && init != error_mark_node
25576 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25577 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25578 and initializer has array type, deduce cv-qualified array type. */
25579 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25580 complain);
25581 else if (AUTO_IS_DECLTYPE (auto_node))
25583 bool id = (DECL_P (init)
25584 || ((TREE_CODE (init) == COMPONENT_REF
25585 || TREE_CODE (init) == SCOPE_REF)
25586 && !REF_PARENTHESIZED_P (init)));
25587 targs = make_tree_vec (1);
25588 TREE_VEC_ELT (targs, 0)
25589 = finish_decltype_type (init, id, tf_warning_or_error);
25590 if (type != auto_node)
25592 if (complain & tf_error)
25593 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25594 return error_mark_node;
25597 else
25599 tree parms = build_tree_list (NULL_TREE, type);
25600 tree tparms;
25602 if (flag_concepts)
25603 tparms = extract_autos (type);
25604 else
25606 tparms = make_tree_vec (1);
25607 TREE_VEC_ELT (tparms, 0)
25608 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25611 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25612 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25613 DEDUCE_CALL, LOOKUP_NORMAL,
25614 NULL, /*explain_p=*/false);
25615 if (val > 0)
25617 if (processing_template_decl)
25618 /* Try again at instantiation time. */
25619 return type;
25620 if (type && type != error_mark_node
25621 && (complain & tf_error))
25622 /* If type is error_mark_node a diagnostic must have been
25623 emitted by now. Also, having a mention to '<type error>'
25624 in the diagnostic is not really useful to the user. */
25626 if (cfun && auto_node == current_function_auto_return_pattern
25627 && LAMBDA_FUNCTION_P (current_function_decl))
25628 error ("unable to deduce lambda return type from %qE", init);
25629 else
25630 error ("unable to deduce %qT from %qE", type, init);
25631 type_unification_real (tparms, targs, parms, &init, 1, 0,
25632 DEDUCE_CALL, LOOKUP_NORMAL,
25633 NULL, /*explain_p=*/true);
25635 return error_mark_node;
25639 /* Check any placeholder constraints against the deduced type. */
25640 if (flag_concepts && !processing_template_decl)
25641 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25643 /* Use the deduced type to check the associated constraints. If we
25644 have a partial-concept-id, rebuild the argument list so that
25645 we check using the extra arguments. */
25646 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25647 tree cargs = CHECK_CONSTR_ARGS (constr);
25648 if (TREE_VEC_LENGTH (cargs) > 1)
25650 cargs = copy_node (cargs);
25651 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25653 else
25654 cargs = targs;
25655 if (!constraints_satisfied_p (constr, cargs))
25657 if (complain & tf_warning_or_error)
25659 switch (context)
25661 case adc_unspecified:
25662 case adc_unify:
25663 error("placeholder constraints not satisfied");
25664 break;
25665 case adc_variable_type:
25666 case adc_decomp_type:
25667 error ("deduced initializer does not satisfy "
25668 "placeholder constraints");
25669 break;
25670 case adc_return_type:
25671 error ("deduced return type does not satisfy "
25672 "placeholder constraints");
25673 break;
25674 case adc_requirement:
25675 error ("deduced expression type does not satisfy "
25676 "placeholder constraints");
25677 break;
25679 diagnose_constraints (input_location, constr, targs);
25681 return error_mark_node;
25685 if (processing_template_decl && context != adc_unify)
25686 outer_targs = current_template_args ();
25687 targs = add_to_template_args (outer_targs, targs);
25688 return tsubst (type, targs, complain, NULL_TREE);
25691 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25692 result. */
25694 tree
25695 splice_late_return_type (tree type, tree late_return_type)
25697 if (is_auto (type))
25699 if (late_return_type)
25700 return late_return_type;
25702 tree idx = get_template_parm_index (type);
25703 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25704 /* In an abbreviated function template we didn't know we were dealing
25705 with a function template when we saw the auto return type, so update
25706 it to have the correct level. */
25707 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25709 return type;
25712 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25713 'decltype(auto)' or a deduced class template. */
25715 bool
25716 is_auto (const_tree type)
25718 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25719 && (TYPE_IDENTIFIER (type) == auto_identifier
25720 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25721 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25722 return true;
25723 else
25724 return false;
25727 /* for_each_template_parm callback for type_uses_auto. */
25730 is_auto_r (tree tp, void */*data*/)
25732 return is_auto (tp);
25735 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25736 a use of `auto'. Returns NULL_TREE otherwise. */
25738 tree
25739 type_uses_auto (tree type)
25741 if (type == NULL_TREE)
25742 return NULL_TREE;
25743 else if (flag_concepts)
25745 /* The Concepts TS allows multiple autos in one type-specifier; just
25746 return the first one we find, do_auto_deduction will collect all of
25747 them. */
25748 if (uses_template_parms (type))
25749 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25750 /*visited*/NULL, /*nondeduced*/true);
25751 else
25752 return NULL_TREE;
25754 else
25755 return find_type_usage (type, is_auto);
25758 /* For a given template T, return the vector of typedefs referenced
25759 in T for which access check is needed at T instantiation time.
25760 T is either a FUNCTION_DECL or a RECORD_TYPE.
25761 Those typedefs were added to T by the function
25762 append_type_to_template_for_access_check. */
25764 vec<qualified_typedef_usage_t, va_gc> *
25765 get_types_needing_access_check (tree t)
25767 tree ti;
25768 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25770 if (!t || t == error_mark_node)
25771 return NULL;
25773 if (!(ti = get_template_info (t)))
25774 return NULL;
25776 if (CLASS_TYPE_P (t)
25777 || TREE_CODE (t) == FUNCTION_DECL)
25779 if (!TI_TEMPLATE (ti))
25780 return NULL;
25782 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25785 return result;
25788 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25789 tied to T. That list of typedefs will be access checked at
25790 T instantiation time.
25791 T is either a FUNCTION_DECL or a RECORD_TYPE.
25792 TYPE_DECL is a TYPE_DECL node representing a typedef.
25793 SCOPE is the scope through which TYPE_DECL is accessed.
25794 LOCATION is the location of the usage point of TYPE_DECL.
25796 This function is a subroutine of
25797 append_type_to_template_for_access_check. */
25799 static void
25800 append_type_to_template_for_access_check_1 (tree t,
25801 tree type_decl,
25802 tree scope,
25803 location_t location)
25805 qualified_typedef_usage_t typedef_usage;
25806 tree ti;
25808 if (!t || t == error_mark_node)
25809 return;
25811 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25812 || CLASS_TYPE_P (t))
25813 && type_decl
25814 && TREE_CODE (type_decl) == TYPE_DECL
25815 && scope);
25817 if (!(ti = get_template_info (t)))
25818 return;
25820 gcc_assert (TI_TEMPLATE (ti));
25822 typedef_usage.typedef_decl = type_decl;
25823 typedef_usage.context = scope;
25824 typedef_usage.locus = location;
25826 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25829 /* Append TYPE_DECL to the template TEMPL.
25830 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25831 At TEMPL instanciation time, TYPE_DECL will be checked to see
25832 if it can be accessed through SCOPE.
25833 LOCATION is the location of the usage point of TYPE_DECL.
25835 e.g. consider the following code snippet:
25837 class C
25839 typedef int myint;
25842 template<class U> struct S
25844 C::myint mi; // <-- usage point of the typedef C::myint
25847 S<char> s;
25849 At S<char> instantiation time, we need to check the access of C::myint
25850 In other words, we need to check the access of the myint typedef through
25851 the C scope. For that purpose, this function will add the myint typedef
25852 and the scope C through which its being accessed to a list of typedefs
25853 tied to the template S. That list will be walked at template instantiation
25854 time and access check performed on each typedefs it contains.
25855 Note that this particular code snippet should yield an error because
25856 myint is private to C. */
25858 void
25859 append_type_to_template_for_access_check (tree templ,
25860 tree type_decl,
25861 tree scope,
25862 location_t location)
25864 qualified_typedef_usage_t *iter;
25865 unsigned i;
25867 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25869 /* Make sure we don't append the type to the template twice. */
25870 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25871 if (iter->typedef_decl == type_decl && scope == iter->context)
25872 return;
25874 append_type_to_template_for_access_check_1 (templ, type_decl,
25875 scope, location);
25878 /* Convert the generic type parameters in PARM that match the types given in the
25879 range [START_IDX, END_IDX) from the current_template_parms into generic type
25880 packs. */
25882 tree
25883 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25885 tree current = current_template_parms;
25886 int depth = TMPL_PARMS_DEPTH (current);
25887 current = INNERMOST_TEMPLATE_PARMS (current);
25888 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25890 for (int i = 0; i < start_idx; ++i)
25891 TREE_VEC_ELT (replacement, i)
25892 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25894 for (int i = start_idx; i < end_idx; ++i)
25896 /* Create a distinct parameter pack type from the current parm and add it
25897 to the replacement args to tsubst below into the generic function
25898 parameter. */
25900 tree o = TREE_TYPE (TREE_VALUE
25901 (TREE_VEC_ELT (current, i)));
25902 tree t = copy_type (o);
25903 TEMPLATE_TYPE_PARM_INDEX (t)
25904 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25905 o, 0, 0, tf_none);
25906 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25907 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25908 TYPE_MAIN_VARIANT (t) = t;
25909 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25910 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25911 TREE_VEC_ELT (replacement, i) = t;
25912 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25915 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25916 TREE_VEC_ELT (replacement, i)
25917 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25919 /* If there are more levels then build up the replacement with the outer
25920 template parms. */
25921 if (depth > 1)
25922 replacement = add_to_template_args (template_parms_to_args
25923 (TREE_CHAIN (current_template_parms)),
25924 replacement);
25926 return tsubst (parm, replacement, tf_none, NULL_TREE);
25929 /* Entries in the decl_constraint hash table. */
25930 struct GTY((for_user)) constr_entry
25932 tree decl;
25933 tree ci;
25936 /* Hashing function and equality for constraint entries. */
25937 struct constr_hasher : ggc_ptr_hash<constr_entry>
25939 static hashval_t hash (constr_entry *e)
25941 return (hashval_t)DECL_UID (e->decl);
25944 static bool equal (constr_entry *e1, constr_entry *e2)
25946 return e1->decl == e2->decl;
25950 /* A mapping from declarations to constraint information. Note that
25951 both templates and their underlying declarations are mapped to the
25952 same constraint information.
25954 FIXME: This is defined in pt.c because garbage collection
25955 code is not being generated for constraint.cc. */
25957 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25959 /* Returns the template constraints of declaration T. If T is not
25960 constrained, return NULL_TREE. Note that T must be non-null. */
25962 tree
25963 get_constraints (tree t)
25965 if (!flag_concepts)
25966 return NULL_TREE;
25968 gcc_assert (DECL_P (t));
25969 if (TREE_CODE (t) == TEMPLATE_DECL)
25970 t = DECL_TEMPLATE_RESULT (t);
25971 constr_entry elt = { t, NULL_TREE };
25972 constr_entry* found = decl_constraints->find (&elt);
25973 if (found)
25974 return found->ci;
25975 else
25976 return NULL_TREE;
25979 /* Associate the given constraint information CI with the declaration
25980 T. If T is a template, then the constraints are associated with
25981 its underlying declaration. Don't build associations if CI is
25982 NULL_TREE. */
25984 void
25985 set_constraints (tree t, tree ci)
25987 if (!ci)
25988 return;
25989 gcc_assert (t && flag_concepts);
25990 if (TREE_CODE (t) == TEMPLATE_DECL)
25991 t = DECL_TEMPLATE_RESULT (t);
25992 gcc_assert (!get_constraints (t));
25993 constr_entry elt = {t, ci};
25994 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25995 constr_entry* entry = ggc_alloc<constr_entry> ();
25996 *entry = elt;
25997 *slot = entry;
26000 /* Remove the associated constraints of the declaration T. */
26002 void
26003 remove_constraints (tree t)
26005 gcc_assert (DECL_P (t));
26006 if (TREE_CODE (t) == TEMPLATE_DECL)
26007 t = DECL_TEMPLATE_RESULT (t);
26009 constr_entry elt = {t, NULL_TREE};
26010 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26011 if (slot)
26012 decl_constraints->clear_slot (slot);
26015 /* Memoized satisfaction results for declarations. This
26016 maps the pair (constraint_info, arguments) to the result computed
26017 by constraints_satisfied_p. */
26019 struct GTY((for_user)) constraint_sat_entry
26021 tree ci;
26022 tree args;
26023 tree result;
26026 /* Hashing function and equality for constraint entries. */
26028 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26030 static hashval_t hash (constraint_sat_entry *e)
26032 hashval_t val = iterative_hash_object(e->ci, 0);
26033 return iterative_hash_template_arg (e->args, val);
26036 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26038 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26042 /* Memoized satisfaction results for concept checks. */
26044 struct GTY((for_user)) concept_spec_entry
26046 tree tmpl;
26047 tree args;
26048 tree result;
26051 /* Hashing function and equality for constraint entries. */
26053 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26055 static hashval_t hash (concept_spec_entry *e)
26057 return hash_tmpl_and_args (e->tmpl, e->args);
26060 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26062 ++comparing_specializations;
26063 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26064 --comparing_specializations;
26065 return eq;
26069 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26070 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26072 /* Search for a memoized satisfaction result. Returns one of the
26073 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26075 tree
26076 lookup_constraint_satisfaction (tree ci, tree args)
26078 constraint_sat_entry elt = { ci, args, NULL_TREE };
26079 constraint_sat_entry* found = constraint_memos->find (&elt);
26080 if (found)
26081 return found->result;
26082 else
26083 return NULL_TREE;
26086 /* Memoize the result of a satisfication test. Returns the saved result. */
26088 tree
26089 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26091 constraint_sat_entry elt = {ci, args, result};
26092 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26093 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26094 *entry = elt;
26095 *slot = entry;
26096 return result;
26099 /* Search for a memoized satisfaction result for a concept. */
26101 tree
26102 lookup_concept_satisfaction (tree tmpl, tree args)
26104 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26105 concept_spec_entry* found = concept_memos->find (&elt);
26106 if (found)
26107 return found->result;
26108 else
26109 return NULL_TREE;
26112 /* Memoize the result of a concept check. Returns the saved result. */
26114 tree
26115 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26117 concept_spec_entry elt = {tmpl, args, result};
26118 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26119 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26120 *entry = elt;
26121 *slot = entry;
26122 return result;
26125 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26127 /* Returns a prior concept specialization. This returns the substituted
26128 and normalized constraints defined by the concept. */
26130 tree
26131 get_concept_expansion (tree tmpl, tree args)
26133 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26134 concept_spec_entry* found = concept_expansions->find (&elt);
26135 if (found)
26136 return found->result;
26137 else
26138 return NULL_TREE;
26141 /* Save a concept expansion for later. */
26143 tree
26144 save_concept_expansion (tree tmpl, tree args, tree def)
26146 concept_spec_entry elt = {tmpl, args, def};
26147 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26148 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26149 *entry = elt;
26150 *slot = entry;
26151 return def;
26154 static hashval_t
26155 hash_subsumption_args (tree t1, tree t2)
26157 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26158 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26159 int val = 0;
26160 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26161 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26162 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26163 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26164 return val;
26167 /* Compare the constraints of two subsumption entries. The LEFT1 and
26168 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26169 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26171 static bool
26172 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26174 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26175 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26176 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26177 CHECK_CONSTR_ARGS (right1)))
26178 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26179 CHECK_CONSTR_ARGS (right2));
26180 return false;
26183 /* Key/value pair for learning and memoizing subsumption results. This
26184 associates a pair of check constraints (including arguments) with
26185 a boolean value indicating the result. */
26187 struct GTY((for_user)) subsumption_entry
26189 tree t1;
26190 tree t2;
26191 bool result;
26194 /* Hashing function and equality for constraint entries. */
26196 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26198 static hashval_t hash (subsumption_entry *e)
26200 return hash_subsumption_args (e->t1, e->t2);
26203 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26205 ++comparing_specializations;
26206 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26207 --comparing_specializations;
26208 return eq;
26212 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26214 /* Search for a previously cached subsumption result. */
26216 bool*
26217 lookup_subsumption_result (tree t1, tree t2)
26219 subsumption_entry elt = { t1, t2, false };
26220 subsumption_entry* found = subsumption_table->find (&elt);
26221 if (found)
26222 return &found->result;
26223 else
26224 return 0;
26227 /* Save a subsumption result. */
26229 bool
26230 save_subsumption_result (tree t1, tree t2, bool result)
26232 subsumption_entry elt = {t1, t2, result};
26233 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26234 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26235 *entry = elt;
26236 *slot = entry;
26237 return result;
26240 /* Set up the hash table for constraint association. */
26242 void
26243 init_constraint_processing (void)
26245 if (!flag_concepts)
26246 return;
26248 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26249 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26250 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26251 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26252 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26255 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26256 0..N-1. */
26258 void
26259 declare_integer_pack (void)
26261 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26262 build_function_type_list (integer_type_node,
26263 integer_type_node,
26264 NULL_TREE),
26265 NULL_TREE, ECF_CONST);
26266 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26267 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26270 /* Set up the hash tables for template instantiations. */
26272 void
26273 init_template_processing (void)
26275 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26276 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26278 if (cxx_dialect >= cxx11)
26279 declare_integer_pack ();
26282 /* Print stats about the template hash tables for -fstats. */
26284 void
26285 print_template_statistics (void)
26287 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26288 "%f collisions\n", (long) decl_specializations->size (),
26289 (long) decl_specializations->elements (),
26290 decl_specializations->collisions ());
26291 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26292 "%f collisions\n", (long) type_specializations->size (),
26293 (long) type_specializations->elements (),
26294 type_specializations->collisions ());
26297 #include "gt-cp-pt.h"