PR c++/81899
[official-gcc.git] / gcc / cp / pt.c
blob8d816c732447553d2fb062c7795169d2d081ed6f
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 (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
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 (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10728 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10729 && TYPE_HIDDEN_P (friend_type))
10731 /* friend class C;
10733 where C hasn't been declared yet. Let's lookup name
10734 from namespace scope directly, bypassing any name that
10735 come from dependent base class. */
10736 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10738 /* The call to xref_tag_from_type does injection for friend
10739 classes. */
10740 push_nested_namespace (ns);
10741 friend_type =
10742 xref_tag_from_type (friend_type, NULL_TREE,
10743 /*tag_scope=*/ts_current);
10744 pop_nested_namespace (ns);
10746 else if (uses_template_parms (friend_type))
10747 /* friend class C<T>; */
10748 friend_type = tsubst (friend_type, args,
10749 tf_warning_or_error, NULL_TREE);
10750 /* Otherwise it's
10752 friend class C;
10754 where C is already declared or
10756 friend class C<int>;
10758 We don't have to do anything in these cases. */
10760 if (adjust_processing_template_decl)
10761 /* Trick make_friend_class into realizing that the friend
10762 we're adding is a template, not an ordinary class. It's
10763 important that we use make_friend_class since it will
10764 perform some error-checking and output cross-reference
10765 information. */
10766 ++processing_template_decl;
10768 if (friend_type != error_mark_node)
10769 make_friend_class (type, friend_type, /*complain=*/false);
10771 if (adjust_processing_template_decl)
10772 --processing_template_decl;
10774 else
10776 /* Build new DECL_FRIENDLIST. */
10777 tree r;
10779 /* The file and line for this declaration, to
10780 assist in error message reporting. Since we
10781 called push_tinst_level above, we don't need to
10782 restore these. */
10783 input_location = DECL_SOURCE_LOCATION (t);
10785 if (TREE_CODE (t) == TEMPLATE_DECL)
10787 ++processing_template_decl;
10788 push_deferring_access_checks (dk_no_check);
10791 r = tsubst_friend_function (t, args);
10792 add_friend (type, r, /*complain=*/false);
10793 if (TREE_CODE (t) == TEMPLATE_DECL)
10795 pop_deferring_access_checks ();
10796 --processing_template_decl;
10802 if (fn_context)
10804 /* Restore these before substituting into the lambda capture
10805 initializers. */
10806 cp_unevaluated_operand = saved_unevaluated_operand;
10807 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10810 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10812 tree decl = lambda_function (type);
10813 if (decl)
10815 if (cxx_dialect >= cxx1z)
10816 CLASSTYPE_LITERAL_P (type) = true;
10818 if (!DECL_TEMPLATE_INFO (decl)
10819 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10821 /* Set function_depth to avoid garbage collection. */
10822 ++function_depth;
10823 instantiate_decl (decl, /*defer_ok=*/false, false);
10824 --function_depth;
10827 /* We need to instantiate the capture list from the template
10828 after we've instantiated the closure members, but before we
10829 consider adding the conversion op. Also keep any captures
10830 that may have been added during instantiation of the op(). */
10831 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10832 tree tmpl_cap
10833 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10834 args, tf_warning_or_error, NULL_TREE,
10835 false, false);
10837 LAMBDA_EXPR_CAPTURE_LIST (expr)
10838 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10840 maybe_add_lambda_conv_op (type);
10842 else
10843 gcc_assert (errorcount);
10846 /* Set the file and line number information to whatever is given for
10847 the class itself. This puts error messages involving generated
10848 implicit functions at a predictable point, and the same point
10849 that would be used for non-template classes. */
10850 input_location = DECL_SOURCE_LOCATION (typedecl);
10852 unreverse_member_declarations (type);
10853 finish_struct_1 (type);
10854 TYPE_BEING_DEFINED (type) = 0;
10856 /* We don't instantiate default arguments for member functions. 14.7.1:
10858 The implicit instantiation of a class template specialization causes
10859 the implicit instantiation of the declarations, but not of the
10860 definitions or default arguments, of the class member functions,
10861 member classes, static data members and member templates.... */
10863 /* Some typedefs referenced from within the template code need to be access
10864 checked at template instantiation time, i.e now. These types were
10865 added to the template at parsing time. Let's get those and perform
10866 the access checks then. */
10867 perform_typedefs_access_check (pattern, args);
10868 perform_deferred_access_checks (tf_warning_or_error);
10869 pop_nested_class ();
10870 maximum_field_alignment = saved_maximum_field_alignment;
10871 if (!fn_context)
10872 pop_from_top_level ();
10873 pop_deferring_access_checks ();
10874 pop_tinst_level ();
10876 /* The vtable for a template class can be emitted in any translation
10877 unit in which the class is instantiated. When there is no key
10878 method, however, finish_struct_1 will already have added TYPE to
10879 the keyed_classes. */
10880 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10881 vec_safe_push (keyed_classes, type);
10883 return type;
10886 /* Wrapper for instantiate_class_template_1. */
10888 tree
10889 instantiate_class_template (tree type)
10891 tree ret;
10892 timevar_push (TV_TEMPLATE_INST);
10893 ret = instantiate_class_template_1 (type);
10894 timevar_pop (TV_TEMPLATE_INST);
10895 return ret;
10898 static tree
10899 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10901 tree r;
10903 if (!t)
10904 r = t;
10905 else if (TYPE_P (t))
10906 r = tsubst (t, args, complain, in_decl);
10907 else
10909 if (!(complain & tf_warning))
10910 ++c_inhibit_evaluation_warnings;
10911 r = tsubst_expr (t, args, complain, in_decl,
10912 /*integral_constant_expression_p=*/true);
10913 if (!(complain & tf_warning))
10914 --c_inhibit_evaluation_warnings;
10916 return r;
10919 /* Given a function parameter pack TMPL_PARM and some function parameters
10920 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10921 and set *SPEC_P to point at the next point in the list. */
10923 tree
10924 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10926 /* Collect all of the extra "packed" parameters into an
10927 argument pack. */
10928 tree parmvec;
10929 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10930 tree spec_parm = *spec_p;
10931 int i, len;
10933 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10934 if (tmpl_parm
10935 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10936 break;
10938 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10939 parmvec = make_tree_vec (len);
10940 spec_parm = *spec_p;
10941 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10942 TREE_VEC_ELT (parmvec, i) = spec_parm;
10944 /* Build the argument packs. */
10945 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10946 *spec_p = spec_parm;
10948 return argpack;
10951 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10952 NONTYPE_ARGUMENT_PACK. */
10954 static tree
10955 make_fnparm_pack (tree spec_parm)
10957 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10960 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10961 pack expansion with no extra args, 2 if it has extra args, or 0
10962 if it is not a pack expansion. */
10964 static int
10965 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10967 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10968 if (i >= TREE_VEC_LENGTH (vec))
10969 return 0;
10970 tree elt = TREE_VEC_ELT (vec, i);
10971 if (DECL_P (elt))
10972 /* A decl pack is itself an expansion. */
10973 elt = TREE_TYPE (elt);
10974 if (!PACK_EXPANSION_P (elt))
10975 return 0;
10976 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10977 return 2;
10978 return 1;
10982 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10984 static tree
10985 make_argument_pack_select (tree arg_pack, unsigned index)
10987 tree aps = make_node (ARGUMENT_PACK_SELECT);
10989 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10990 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10992 return aps;
10995 /* This is a subroutine of tsubst_pack_expansion.
10997 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10998 mechanism to store the (non complete list of) arguments of the
10999 substitution and return a non substituted pack expansion, in order
11000 to wait for when we have enough arguments to really perform the
11001 substitution. */
11003 static bool
11004 use_pack_expansion_extra_args_p (tree parm_packs,
11005 int arg_pack_len,
11006 bool has_empty_arg)
11008 /* If one pack has an expansion and another pack has a normal
11009 argument or if one pack has an empty argument and an another
11010 one hasn't then tsubst_pack_expansion cannot perform the
11011 substitution and need to fall back on the
11012 PACK_EXPANSION_EXTRA mechanism. */
11013 if (parm_packs == NULL_TREE)
11014 return false;
11015 else if (has_empty_arg)
11016 return true;
11018 bool has_expansion_arg = false;
11019 for (int i = 0 ; i < arg_pack_len; ++i)
11021 bool has_non_expansion_arg = false;
11022 for (tree parm_pack = parm_packs;
11023 parm_pack;
11024 parm_pack = TREE_CHAIN (parm_pack))
11026 tree arg = TREE_VALUE (parm_pack);
11028 int exp = argument_pack_element_is_expansion_p (arg, i);
11029 if (exp == 2)
11030 /* We can't substitute a pack expansion with extra args into
11031 our pattern. */
11032 return true;
11033 else if (exp)
11034 has_expansion_arg = true;
11035 else
11036 has_non_expansion_arg = true;
11039 if (has_expansion_arg && has_non_expansion_arg)
11040 return true;
11042 return false;
11045 /* [temp.variadic]/6 says that:
11047 The instantiation of a pack expansion [...]
11048 produces a list E1,E2, ..., En, where N is the number of elements
11049 in the pack expansion parameters.
11051 This subroutine of tsubst_pack_expansion produces one of these Ei.
11053 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11054 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11055 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11056 INDEX is the index 'i' of the element Ei to produce. ARGS,
11057 COMPLAIN, and IN_DECL are the same parameters as for the
11058 tsubst_pack_expansion function.
11060 The function returns the resulting Ei upon successful completion,
11061 or error_mark_node.
11063 Note that this function possibly modifies the ARGS parameter, so
11064 it's the responsibility of the caller to restore it. */
11066 static tree
11067 gen_elem_of_pack_expansion_instantiation (tree pattern,
11068 tree parm_packs,
11069 unsigned index,
11070 tree args /* This parm gets
11071 modified. */,
11072 tsubst_flags_t complain,
11073 tree in_decl)
11075 tree t;
11076 bool ith_elem_is_expansion = false;
11078 /* For each parameter pack, change the substitution of the parameter
11079 pack to the ith argument in its argument pack, then expand the
11080 pattern. */
11081 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11083 tree parm = TREE_PURPOSE (pack);
11084 tree arg_pack = TREE_VALUE (pack);
11085 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11087 ith_elem_is_expansion |=
11088 argument_pack_element_is_expansion_p (arg_pack, index);
11090 /* Select the Ith argument from the pack. */
11091 if (TREE_CODE (parm) == PARM_DECL
11092 || TREE_CODE (parm) == FIELD_DECL)
11094 if (index == 0)
11096 aps = make_argument_pack_select (arg_pack, index);
11097 if (!mark_used (parm, complain) && !(complain & tf_error))
11098 return error_mark_node;
11099 register_local_specialization (aps, parm);
11101 else
11102 aps = retrieve_local_specialization (parm);
11104 else
11106 int idx, level;
11107 template_parm_level_and_index (parm, &level, &idx);
11109 if (index == 0)
11111 aps = make_argument_pack_select (arg_pack, index);
11112 /* Update the corresponding argument. */
11113 TMPL_ARG (args, level, idx) = aps;
11115 else
11116 /* Re-use the ARGUMENT_PACK_SELECT. */
11117 aps = TMPL_ARG (args, level, idx);
11119 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11122 /* Substitute into the PATTERN with the (possibly altered)
11123 arguments. */
11124 if (pattern == in_decl)
11125 /* Expanding a fixed parameter pack from
11126 coerce_template_parameter_pack. */
11127 t = tsubst_decl (pattern, args, complain);
11128 else if (pattern == error_mark_node)
11129 t = error_mark_node;
11130 else if (constraint_p (pattern))
11132 if (processing_template_decl)
11133 t = tsubst_constraint (pattern, args, complain, in_decl);
11134 else
11135 t = (constraints_satisfied_p (pattern, args)
11136 ? boolean_true_node : boolean_false_node);
11138 else if (!TYPE_P (pattern))
11139 t = tsubst_expr (pattern, args, complain, in_decl,
11140 /*integral_constant_expression_p=*/false);
11141 else
11142 t = tsubst (pattern, args, complain, in_decl);
11144 /* If the Ith argument pack element is a pack expansion, then
11145 the Ith element resulting from the substituting is going to
11146 be a pack expansion as well. */
11147 if (ith_elem_is_expansion)
11148 t = make_pack_expansion (t);
11150 return t;
11153 /* When the unexpanded parameter pack in a fold expression expands to an empty
11154 sequence, the value of the expression is as follows; the program is
11155 ill-formed if the operator is not listed in this table.
11157 && true
11158 || false
11159 , void() */
11161 tree
11162 expand_empty_fold (tree t, tsubst_flags_t complain)
11164 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11165 if (!FOLD_EXPR_MODIFY_P (t))
11166 switch (code)
11168 case TRUTH_ANDIF_EXPR:
11169 return boolean_true_node;
11170 case TRUTH_ORIF_EXPR:
11171 return boolean_false_node;
11172 case COMPOUND_EXPR:
11173 return void_node;
11174 default:
11175 break;
11178 if (complain & tf_error)
11179 error_at (location_of (t),
11180 "fold of empty expansion over %O", code);
11181 return error_mark_node;
11184 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11185 form an expression that combines the two terms using the
11186 operator of T. */
11188 static tree
11189 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11191 tree op = FOLD_EXPR_OP (t);
11192 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11194 // Handle compound assignment operators.
11195 if (FOLD_EXPR_MODIFY_P (t))
11196 return build_x_modify_expr (input_location, left, code, right, complain);
11198 switch (code)
11200 case COMPOUND_EXPR:
11201 return build_x_compound_expr (input_location, left, right, complain);
11202 case DOTSTAR_EXPR:
11203 return build_m_component_ref (left, right, complain);
11204 default:
11205 return build_x_binary_op (input_location, code,
11206 left, TREE_CODE (left),
11207 right, TREE_CODE (right),
11208 /*overload=*/NULL,
11209 complain);
11213 /* Substitute ARGS into the pack of a fold expression T. */
11215 static inline tree
11216 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11218 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11221 /* Substitute ARGS into the pack of a fold expression T. */
11223 static inline tree
11224 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11226 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11229 /* Expand a PACK of arguments into a grouped as left fold.
11230 Given a pack containing elements A0, A1, ..., An and an
11231 operator @, this builds the expression:
11233 ((A0 @ A1) @ A2) ... @ An
11235 Note that PACK must not be empty.
11237 The operator is defined by the original fold expression T. */
11239 static tree
11240 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11242 tree left = TREE_VEC_ELT (pack, 0);
11243 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11245 tree right = TREE_VEC_ELT (pack, i);
11246 left = fold_expression (t, left, right, complain);
11248 return left;
11251 /* Substitute into a unary left fold expression. */
11253 static tree
11254 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11255 tree in_decl)
11257 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11258 if (pack == error_mark_node)
11259 return error_mark_node;
11260 if (PACK_EXPANSION_P (pack))
11262 tree r = copy_node (t);
11263 FOLD_EXPR_PACK (r) = pack;
11264 return r;
11266 if (TREE_VEC_LENGTH (pack) == 0)
11267 return expand_empty_fold (t, complain);
11268 else
11269 return expand_left_fold (t, pack, complain);
11272 /* Substitute into a binary left fold expression.
11274 Do ths by building a single (non-empty) vector of argumnts and
11275 building the expression from those elements. */
11277 static tree
11278 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11279 tree in_decl)
11281 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11282 if (pack == error_mark_node)
11283 return error_mark_node;
11284 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11285 if (init == error_mark_node)
11286 return error_mark_node;
11288 if (PACK_EXPANSION_P (pack))
11290 tree r = copy_node (t);
11291 FOLD_EXPR_PACK (r) = pack;
11292 FOLD_EXPR_INIT (r) = init;
11293 return r;
11296 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11297 TREE_VEC_ELT (vec, 0) = init;
11298 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11299 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11301 return expand_left_fold (t, vec, complain);
11304 /* Expand a PACK of arguments into a grouped as right fold.
11305 Given a pack containing elementns A0, A1, ..., and an
11306 operator @, this builds the expression:
11308 A0@ ... (An-2 @ (An-1 @ An))
11310 Note that PACK must not be empty.
11312 The operator is defined by the original fold expression T. */
11314 tree
11315 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11317 // Build the expression.
11318 int n = TREE_VEC_LENGTH (pack);
11319 tree right = TREE_VEC_ELT (pack, n - 1);
11320 for (--n; n != 0; --n)
11322 tree left = TREE_VEC_ELT (pack, n - 1);
11323 right = fold_expression (t, left, right, complain);
11325 return right;
11328 /* Substitute into a unary right fold expression. */
11330 static tree
11331 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11332 tree in_decl)
11334 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11335 if (pack == error_mark_node)
11336 return error_mark_node;
11337 if (PACK_EXPANSION_P (pack))
11339 tree r = copy_node (t);
11340 FOLD_EXPR_PACK (r) = pack;
11341 return r;
11343 if (TREE_VEC_LENGTH (pack) == 0)
11344 return expand_empty_fold (t, complain);
11345 else
11346 return expand_right_fold (t, pack, complain);
11349 /* Substitute into a binary right fold expression.
11351 Do ths by building a single (non-empty) vector of arguments and
11352 building the expression from those elements. */
11354 static tree
11355 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11356 tree in_decl)
11358 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11359 if (pack == error_mark_node)
11360 return error_mark_node;
11361 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11362 if (init == error_mark_node)
11363 return error_mark_node;
11365 if (PACK_EXPANSION_P (pack))
11367 tree r = copy_node (t);
11368 FOLD_EXPR_PACK (r) = pack;
11369 FOLD_EXPR_INIT (r) = init;
11370 return r;
11373 int n = TREE_VEC_LENGTH (pack);
11374 tree vec = make_tree_vec (n + 1);
11375 for (int i = 0; i < n; ++i)
11376 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11377 TREE_VEC_ELT (vec, n) = init;
11379 return expand_right_fold (t, vec, complain);
11383 /* Substitute ARGS into T, which is an pack expansion
11384 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11385 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11386 (if only a partial substitution could be performed) or
11387 ERROR_MARK_NODE if there was an error. */
11388 tree
11389 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11390 tree in_decl)
11392 tree pattern;
11393 tree pack, packs = NULL_TREE;
11394 bool unsubstituted_packs = false;
11395 int i, len = -1;
11396 tree result;
11397 hash_map<tree, tree> *saved_local_specializations = NULL;
11398 bool need_local_specializations = false;
11399 int levels;
11401 gcc_assert (PACK_EXPANSION_P (t));
11402 pattern = PACK_EXPANSION_PATTERN (t);
11404 /* Add in any args remembered from an earlier partial instantiation. */
11405 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11407 levels = TMPL_ARGS_DEPTH (args);
11409 /* Determine the argument packs that will instantiate the parameter
11410 packs used in the expansion expression. While we're at it,
11411 compute the number of arguments to be expanded and make sure it
11412 is consistent. */
11413 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11414 pack = TREE_CHAIN (pack))
11416 tree parm_pack = TREE_VALUE (pack);
11417 tree arg_pack = NULL_TREE;
11418 tree orig_arg = NULL_TREE;
11419 int level = 0;
11421 if (TREE_CODE (parm_pack) == BASES)
11423 gcc_assert (parm_pack == pattern);
11424 if (BASES_DIRECT (parm_pack))
11425 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11426 args, complain, in_decl, false));
11427 else
11428 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11429 args, complain, in_decl, false));
11431 else if (builtin_pack_call_p (parm_pack))
11433 /* ??? Support use in other patterns. */
11434 gcc_assert (parm_pack == pattern);
11435 return expand_builtin_pack_call (parm_pack, args,
11436 complain, in_decl);
11438 else if (TREE_CODE (parm_pack) == PARM_DECL)
11440 /* We know we have correct local_specializations if this
11441 expansion is at function scope, or if we're dealing with a
11442 local parameter in a requires expression; for the latter,
11443 tsubst_requires_expr set it up appropriately. */
11444 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11445 arg_pack = retrieve_local_specialization (parm_pack);
11446 else
11447 /* We can't rely on local_specializations for a parameter
11448 name used later in a function declaration (such as in a
11449 late-specified return type). Even if it exists, it might
11450 have the wrong value for a recursive call. */
11451 need_local_specializations = true;
11453 if (!arg_pack)
11455 /* This parameter pack was used in an unevaluated context. Just
11456 make a dummy decl, since it's only used for its type. */
11457 arg_pack = tsubst_decl (parm_pack, args, complain);
11458 if (arg_pack && DECL_PACK_P (arg_pack))
11459 /* Partial instantiation of the parm_pack, we can't build
11460 up an argument pack yet. */
11461 arg_pack = NULL_TREE;
11462 else
11463 arg_pack = make_fnparm_pack (arg_pack);
11466 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11467 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11468 else
11470 int idx;
11471 template_parm_level_and_index (parm_pack, &level, &idx);
11473 if (level <= levels)
11474 arg_pack = TMPL_ARG (args, level, idx);
11477 orig_arg = arg_pack;
11478 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11479 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11481 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11482 /* This can only happen if we forget to expand an argument
11483 pack somewhere else. Just return an error, silently. */
11485 result = make_tree_vec (1);
11486 TREE_VEC_ELT (result, 0) = error_mark_node;
11487 return result;
11490 if (arg_pack)
11492 int my_len =
11493 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11495 /* Don't bother trying to do a partial substitution with
11496 incomplete packs; we'll try again after deduction. */
11497 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11498 return t;
11500 if (len < 0)
11501 len = my_len;
11502 else if (len != my_len)
11504 if (!(complain & tf_error))
11505 /* Fail quietly. */;
11506 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11507 error ("mismatched argument pack lengths while expanding %qT",
11508 pattern);
11509 else
11510 error ("mismatched argument pack lengths while expanding %qE",
11511 pattern);
11512 return error_mark_node;
11515 /* Keep track of the parameter packs and their corresponding
11516 argument packs. */
11517 packs = tree_cons (parm_pack, arg_pack, packs);
11518 TREE_TYPE (packs) = orig_arg;
11520 else
11522 /* We can't substitute for this parameter pack. We use a flag as
11523 well as the missing_level counter because function parameter
11524 packs don't have a level. */
11525 gcc_assert (processing_template_decl);
11526 unsubstituted_packs = true;
11530 /* If the expansion is just T..., return the matching argument pack, unless
11531 we need to call convert_from_reference on all the elements. This is an
11532 important optimization; see c++/68422. */
11533 if (!unsubstituted_packs
11534 && TREE_PURPOSE (packs) == pattern)
11536 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11537 /* Types need no adjustment, nor does sizeof..., and if we still have
11538 some pack expansion args we won't do anything yet. */
11539 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11540 || PACK_EXPANSION_SIZEOF_P (t)
11541 || pack_expansion_args_count (args))
11542 return args;
11543 /* Also optimize expression pack expansions if we can tell that the
11544 elements won't have reference type. */
11545 tree type = TREE_TYPE (pattern);
11546 if (type && TREE_CODE (type) != REFERENCE_TYPE
11547 && !PACK_EXPANSION_P (type)
11548 && !WILDCARD_TYPE_P (type))
11549 return args;
11550 /* Otherwise use the normal path so we get convert_from_reference. */
11553 /* We cannot expand this expansion expression, because we don't have
11554 all of the argument packs we need. */
11555 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11557 /* We got some full packs, but we can't substitute them in until we
11558 have values for all the packs. So remember these until then. */
11560 t = make_pack_expansion (pattern);
11561 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11562 return t;
11564 else if (unsubstituted_packs)
11566 /* There were no real arguments, we're just replacing a parameter
11567 pack with another version of itself. Substitute into the
11568 pattern and return a PACK_EXPANSION_*. The caller will need to
11569 deal with that. */
11570 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11571 t = tsubst_expr (pattern, args, complain, in_decl,
11572 /*integral_constant_expression_p=*/false);
11573 else
11574 t = tsubst (pattern, args, complain, in_decl);
11575 t = make_pack_expansion (t);
11576 return t;
11579 gcc_assert (len >= 0);
11581 if (need_local_specializations)
11583 /* We're in a late-specified return type, so create our own local
11584 specializations map; the current map is either NULL or (in the
11585 case of recursive unification) might have bindings that we don't
11586 want to use or alter. */
11587 saved_local_specializations = local_specializations;
11588 local_specializations = new hash_map<tree, tree>;
11591 /* For each argument in each argument pack, substitute into the
11592 pattern. */
11593 result = make_tree_vec (len);
11594 tree elem_args = copy_template_args (args);
11595 for (i = 0; i < len; ++i)
11597 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11599 elem_args, complain,
11600 in_decl);
11601 TREE_VEC_ELT (result, i) = t;
11602 if (t == error_mark_node)
11604 result = error_mark_node;
11605 break;
11609 /* Update ARGS to restore the substitution from parameter packs to
11610 their argument packs. */
11611 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11613 tree parm = TREE_PURPOSE (pack);
11615 if (TREE_CODE (parm) == PARM_DECL
11616 || TREE_CODE (parm) == FIELD_DECL)
11617 register_local_specialization (TREE_TYPE (pack), parm);
11618 else
11620 int idx, level;
11622 if (TREE_VALUE (pack) == NULL_TREE)
11623 continue;
11625 template_parm_level_and_index (parm, &level, &idx);
11627 /* Update the corresponding argument. */
11628 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11629 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11630 TREE_TYPE (pack);
11631 else
11632 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11636 if (need_local_specializations)
11638 delete local_specializations;
11639 local_specializations = saved_local_specializations;
11642 /* If the dependent pack arguments were such that we end up with only a
11643 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11644 if (len == 1 && TREE_CODE (result) == TREE_VEC
11645 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11646 return TREE_VEC_ELT (result, 0);
11648 return result;
11651 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11652 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11653 parameter packs; all parms generated from a function parameter pack will
11654 have the same DECL_PARM_INDEX. */
11656 tree
11657 get_pattern_parm (tree parm, tree tmpl)
11659 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11660 tree patparm;
11662 if (DECL_ARTIFICIAL (parm))
11664 for (patparm = DECL_ARGUMENTS (pattern);
11665 patparm; patparm = DECL_CHAIN (patparm))
11666 if (DECL_ARTIFICIAL (patparm)
11667 && DECL_NAME (parm) == DECL_NAME (patparm))
11668 break;
11670 else
11672 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11673 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11674 gcc_assert (DECL_PARM_INDEX (patparm)
11675 == DECL_PARM_INDEX (parm));
11678 return patparm;
11681 /* Make an argument pack out of the TREE_VEC VEC. */
11683 static tree
11684 make_argument_pack (tree vec)
11686 tree pack;
11687 tree elt = TREE_VEC_ELT (vec, 0);
11688 if (TYPE_P (elt))
11689 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11690 else
11692 pack = make_node (NONTYPE_ARGUMENT_PACK);
11693 TREE_CONSTANT (pack) = 1;
11695 SET_ARGUMENT_PACK_ARGS (pack, vec);
11696 return pack;
11699 /* Return an exact copy of template args T that can be modified
11700 independently. */
11702 static tree
11703 copy_template_args (tree t)
11705 if (t == error_mark_node)
11706 return t;
11708 int len = TREE_VEC_LENGTH (t);
11709 tree new_vec = make_tree_vec (len);
11711 for (int i = 0; i < len; ++i)
11713 tree elt = TREE_VEC_ELT (t, i);
11714 if (elt && TREE_CODE (elt) == TREE_VEC)
11715 elt = copy_template_args (elt);
11716 TREE_VEC_ELT (new_vec, i) = elt;
11719 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11720 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11722 return new_vec;
11725 /* Substitute ARGS into the vector or list of template arguments T. */
11727 static tree
11728 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11730 tree orig_t = t;
11731 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11732 tree *elts;
11734 if (t == error_mark_node)
11735 return error_mark_node;
11737 len = TREE_VEC_LENGTH (t);
11738 elts = XALLOCAVEC (tree, len);
11740 for (i = 0; i < len; i++)
11742 tree orig_arg = TREE_VEC_ELT (t, i);
11743 tree new_arg;
11745 if (TREE_CODE (orig_arg) == TREE_VEC)
11746 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11747 else if (PACK_EXPANSION_P (orig_arg))
11749 /* Substitute into an expansion expression. */
11750 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11752 if (TREE_CODE (new_arg) == TREE_VEC)
11753 /* Add to the expanded length adjustment the number of
11754 expanded arguments. We subtract one from this
11755 measurement, because the argument pack expression
11756 itself is already counted as 1 in
11757 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11758 the argument pack is empty. */
11759 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11761 else if (ARGUMENT_PACK_P (orig_arg))
11763 /* Substitute into each of the arguments. */
11764 new_arg = TYPE_P (orig_arg)
11765 ? cxx_make_type (TREE_CODE (orig_arg))
11766 : make_node (TREE_CODE (orig_arg));
11768 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11769 args, complain, in_decl);
11770 if (pack_args == error_mark_node)
11771 new_arg = error_mark_node;
11772 else
11773 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11775 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11776 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11778 else
11779 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11781 if (new_arg == error_mark_node)
11782 return error_mark_node;
11784 elts[i] = new_arg;
11785 if (new_arg != orig_arg)
11786 need_new = 1;
11789 if (!need_new)
11790 return t;
11792 /* Make space for the expanded arguments coming from template
11793 argument packs. */
11794 t = make_tree_vec (len + expanded_len_adjust);
11795 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11796 arguments for a member template.
11797 In that case each TREE_VEC in ORIG_T represents a level of template
11798 arguments, and ORIG_T won't carry any non defaulted argument count.
11799 It will rather be the nested TREE_VECs that will carry one.
11800 In other words, ORIG_T carries a non defaulted argument count only
11801 if it doesn't contain any nested TREE_VEC. */
11802 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11804 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11805 count += expanded_len_adjust;
11806 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11808 for (i = 0, out = 0; i < len; i++)
11810 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11811 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11812 && TREE_CODE (elts[i]) == TREE_VEC)
11814 int idx;
11816 /* Now expand the template argument pack "in place". */
11817 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11818 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11820 else
11822 TREE_VEC_ELT (t, out) = elts[i];
11823 out++;
11827 return t;
11830 /* Substitute ARGS into one level PARMS of template parameters. */
11832 static tree
11833 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11835 if (parms == error_mark_node)
11836 return error_mark_node;
11838 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11840 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11842 tree tuple = TREE_VEC_ELT (parms, i);
11844 if (tuple == error_mark_node)
11845 continue;
11847 TREE_VEC_ELT (new_vec, i) =
11848 tsubst_template_parm (tuple, args, complain);
11851 return new_vec;
11854 /* Return the result of substituting ARGS into the template parameters
11855 given by PARMS. If there are m levels of ARGS and m + n levels of
11856 PARMS, then the result will contain n levels of PARMS. For
11857 example, if PARMS is `template <class T> template <class U>
11858 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11859 result will be `template <int*, double, class V>'. */
11861 static tree
11862 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11864 tree r = NULL_TREE;
11865 tree* new_parms;
11867 /* When substituting into a template, we must set
11868 PROCESSING_TEMPLATE_DECL as the template parameters may be
11869 dependent if they are based on one-another, and the dependency
11870 predicates are short-circuit outside of templates. */
11871 ++processing_template_decl;
11873 for (new_parms = &r;
11874 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11875 new_parms = &(TREE_CHAIN (*new_parms)),
11876 parms = TREE_CHAIN (parms))
11878 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11879 args, complain);
11880 *new_parms =
11881 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11882 - TMPL_ARGS_DEPTH (args)),
11883 new_vec, NULL_TREE);
11886 --processing_template_decl;
11888 return r;
11891 /* Return the result of substituting ARGS into one template parameter
11892 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11893 parameter and which TREE_PURPOSE is the default argument of the
11894 template parameter. */
11896 static tree
11897 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11899 tree default_value, parm_decl;
11901 if (args == NULL_TREE
11902 || t == NULL_TREE
11903 || t == error_mark_node)
11904 return t;
11906 gcc_assert (TREE_CODE (t) == TREE_LIST);
11908 default_value = TREE_PURPOSE (t);
11909 parm_decl = TREE_VALUE (t);
11911 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11912 if (TREE_CODE (parm_decl) == PARM_DECL
11913 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11914 parm_decl = error_mark_node;
11915 default_value = tsubst_template_arg (default_value, args,
11916 complain, NULL_TREE);
11918 return build_tree_list (default_value, parm_decl);
11921 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11922 type T. If T is not an aggregate or enumeration type, it is
11923 handled as if by tsubst. IN_DECL is as for tsubst. If
11924 ENTERING_SCOPE is nonzero, T is the context for a template which
11925 we are presently tsubst'ing. Return the substituted value. */
11927 static tree
11928 tsubst_aggr_type (tree t,
11929 tree args,
11930 tsubst_flags_t complain,
11931 tree in_decl,
11932 int entering_scope)
11934 if (t == NULL_TREE)
11935 return NULL_TREE;
11937 switch (TREE_CODE (t))
11939 case RECORD_TYPE:
11940 if (TYPE_PTRMEMFUNC_P (t))
11941 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11943 /* Fall through. */
11944 case ENUMERAL_TYPE:
11945 case UNION_TYPE:
11946 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11948 tree argvec;
11949 tree context;
11950 tree r;
11951 int saved_unevaluated_operand;
11952 int saved_inhibit_evaluation_warnings;
11954 /* In "sizeof(X<I>)" we need to evaluate "I". */
11955 saved_unevaluated_operand = cp_unevaluated_operand;
11956 cp_unevaluated_operand = 0;
11957 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11958 c_inhibit_evaluation_warnings = 0;
11960 /* First, determine the context for the type we are looking
11961 up. */
11962 context = TYPE_CONTEXT (t);
11963 if (context && TYPE_P (context))
11965 context = tsubst_aggr_type (context, args, complain,
11966 in_decl, /*entering_scope=*/1);
11967 /* If context is a nested class inside a class template,
11968 it may still need to be instantiated (c++/33959). */
11969 context = complete_type (context);
11972 /* Then, figure out what arguments are appropriate for the
11973 type we are trying to find. For example, given:
11975 template <class T> struct S;
11976 template <class T, class U> void f(T, U) { S<U> su; }
11978 and supposing that we are instantiating f<int, double>,
11979 then our ARGS will be {int, double}, but, when looking up
11980 S we only want {double}. */
11981 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11982 complain, in_decl);
11983 if (argvec == error_mark_node)
11984 r = error_mark_node;
11985 else
11987 r = lookup_template_class (t, argvec, in_decl, context,
11988 entering_scope, complain);
11989 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11992 cp_unevaluated_operand = saved_unevaluated_operand;
11993 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11995 return r;
11997 else
11998 /* This is not a template type, so there's nothing to do. */
11999 return t;
12001 default:
12002 return tsubst (t, args, complain, in_decl);
12006 /* Substitute into the default argument ARG (a default argument for
12007 FN), which has the indicated TYPE. */
12009 tree
12010 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
12012 tree saved_class_ptr = NULL_TREE;
12013 tree saved_class_ref = NULL_TREE;
12014 int errs = errorcount + sorrycount;
12016 /* This can happen in invalid code. */
12017 if (TREE_CODE (arg) == DEFAULT_ARG)
12018 return arg;
12020 /* This default argument came from a template. Instantiate the
12021 default argument here, not in tsubst. In the case of
12022 something like:
12024 template <class T>
12025 struct S {
12026 static T t();
12027 void f(T = t());
12030 we must be careful to do name lookup in the scope of S<T>,
12031 rather than in the current class. */
12032 push_access_scope (fn);
12033 /* The "this" pointer is not valid in a default argument. */
12034 if (cfun)
12036 saved_class_ptr = current_class_ptr;
12037 cp_function_chain->x_current_class_ptr = NULL_TREE;
12038 saved_class_ref = current_class_ref;
12039 cp_function_chain->x_current_class_ref = NULL_TREE;
12042 push_deferring_access_checks(dk_no_deferred);
12043 /* The default argument expression may cause implicitly defined
12044 member functions to be synthesized, which will result in garbage
12045 collection. We must treat this situation as if we were within
12046 the body of function so as to avoid collecting live data on the
12047 stack. */
12048 ++function_depth;
12049 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12050 complain, NULL_TREE,
12051 /*integral_constant_expression_p=*/false);
12052 --function_depth;
12053 pop_deferring_access_checks();
12055 /* Restore the "this" pointer. */
12056 if (cfun)
12058 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12059 cp_function_chain->x_current_class_ref = saved_class_ref;
12062 if (errorcount+sorrycount > errs
12063 && (complain & tf_warning_or_error))
12064 inform (input_location,
12065 " when instantiating default argument for call to %qD", fn);
12067 /* Make sure the default argument is reasonable. */
12068 arg = check_default_argument (type, arg, complain);
12070 pop_access_scope (fn);
12072 return arg;
12075 /* Substitute into all the default arguments for FN. */
12077 static void
12078 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12080 tree arg;
12081 tree tmpl_args;
12083 tmpl_args = DECL_TI_ARGS (fn);
12085 /* If this function is not yet instantiated, we certainly don't need
12086 its default arguments. */
12087 if (uses_template_parms (tmpl_args))
12088 return;
12089 /* Don't do this again for clones. */
12090 if (DECL_CLONED_FUNCTION_P (fn))
12091 return;
12093 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12094 arg;
12095 arg = TREE_CHAIN (arg))
12096 if (TREE_PURPOSE (arg))
12097 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12098 TREE_VALUE (arg),
12099 TREE_PURPOSE (arg),
12100 complain);
12103 /* Substitute the ARGS into the T, which is a _DECL. Return the
12104 result of the substitution. Issue error and warning messages under
12105 control of COMPLAIN. */
12107 static tree
12108 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12110 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12111 location_t saved_loc;
12112 tree r = NULL_TREE;
12113 tree in_decl = t;
12114 hashval_t hash = 0;
12116 /* Set the filename and linenumber to improve error-reporting. */
12117 saved_loc = input_location;
12118 input_location = DECL_SOURCE_LOCATION (t);
12120 switch (TREE_CODE (t))
12122 case TEMPLATE_DECL:
12124 /* We can get here when processing a member function template,
12125 member class template, or template template parameter. */
12126 tree decl = DECL_TEMPLATE_RESULT (t);
12127 tree spec;
12128 tree tmpl_args;
12129 tree full_args;
12131 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12133 /* Template template parameter is treated here. */
12134 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12135 if (new_type == error_mark_node)
12136 r = error_mark_node;
12137 /* If we get a real template back, return it. This can happen in
12138 the context of most_specialized_partial_spec. */
12139 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12140 r = new_type;
12141 else
12142 /* The new TEMPLATE_DECL was built in
12143 reduce_template_parm_level. */
12144 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12145 break;
12148 /* We might already have an instance of this template.
12149 The ARGS are for the surrounding class type, so the
12150 full args contain the tsubst'd args for the context,
12151 plus the innermost args from the template decl. */
12152 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12153 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12154 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12155 /* Because this is a template, the arguments will still be
12156 dependent, even after substitution. If
12157 PROCESSING_TEMPLATE_DECL is not set, the dependency
12158 predicates will short-circuit. */
12159 ++processing_template_decl;
12160 full_args = tsubst_template_args (tmpl_args, args,
12161 complain, in_decl);
12162 --processing_template_decl;
12163 if (full_args == error_mark_node)
12164 RETURN (error_mark_node);
12166 /* If this is a default template template argument,
12167 tsubst might not have changed anything. */
12168 if (full_args == tmpl_args)
12169 RETURN (t);
12171 hash = hash_tmpl_and_args (t, full_args);
12172 spec = retrieve_specialization (t, full_args, hash);
12173 if (spec != NULL_TREE)
12175 r = spec;
12176 break;
12179 /* Make a new template decl. It will be similar to the
12180 original, but will record the current template arguments.
12181 We also create a new function declaration, which is just
12182 like the old one, but points to this new template, rather
12183 than the old one. */
12184 r = copy_decl (t);
12185 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12186 DECL_CHAIN (r) = NULL_TREE;
12188 // Build new template info linking to the original template decl.
12189 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12191 if (TREE_CODE (decl) == TYPE_DECL
12192 && !TYPE_DECL_ALIAS_P (decl))
12194 tree new_type;
12195 ++processing_template_decl;
12196 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12197 --processing_template_decl;
12198 if (new_type == error_mark_node)
12199 RETURN (error_mark_node);
12201 TREE_TYPE (r) = new_type;
12202 /* For a partial specialization, we need to keep pointing to
12203 the primary template. */
12204 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12205 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12206 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12207 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12208 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12210 else
12212 tree new_decl;
12213 ++processing_template_decl;
12214 new_decl = tsubst (decl, args, complain, in_decl);
12215 --processing_template_decl;
12216 if (new_decl == error_mark_node)
12217 RETURN (error_mark_node);
12219 DECL_TEMPLATE_RESULT (r) = new_decl;
12220 DECL_TI_TEMPLATE (new_decl) = r;
12221 TREE_TYPE (r) = TREE_TYPE (new_decl);
12222 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12223 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12226 SET_DECL_IMPLICIT_INSTANTIATION (r);
12227 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12228 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12230 /* The template parameters for this new template are all the
12231 template parameters for the old template, except the
12232 outermost level of parameters. */
12233 DECL_TEMPLATE_PARMS (r)
12234 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12235 complain);
12237 if (PRIMARY_TEMPLATE_P (t))
12238 DECL_PRIMARY_TEMPLATE (r) = r;
12240 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12241 /* Record this non-type partial instantiation. */
12242 register_specialization (r, t,
12243 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12244 false, hash);
12246 break;
12248 case FUNCTION_DECL:
12250 tree gen_tmpl, argvec;
12252 /* Nobody should be tsubst'ing into non-template functions. */
12253 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12255 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12257 /* If T is not dependent, just return it. */
12258 if (!uses_template_parms (DECL_TI_ARGS (t)))
12259 RETURN (t);
12261 /* Calculate the most general template of which R is a
12262 specialization, and the complete set of arguments used to
12263 specialize R. */
12264 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12265 argvec = tsubst_template_args (DECL_TI_ARGS
12266 (DECL_TEMPLATE_RESULT
12267 (DECL_TI_TEMPLATE (t))),
12268 args, complain, in_decl);
12269 if (argvec == error_mark_node)
12270 RETURN (error_mark_node);
12272 /* Check to see if we already have this specialization. */
12273 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12274 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12276 r = spec;
12277 break;
12280 /* We can see more levels of arguments than parameters if
12281 there was a specialization of a member template, like
12282 this:
12284 template <class T> struct S { template <class U> void f(); }
12285 template <> template <class U> void S<int>::f(U);
12287 Here, we'll be substituting into the specialization,
12288 because that's where we can find the code we actually
12289 want to generate, but we'll have enough arguments for
12290 the most general template.
12292 We also deal with the peculiar case:
12294 template <class T> struct S {
12295 template <class U> friend void f();
12297 template <class U> void f() {}
12298 template S<int>;
12299 template void f<double>();
12301 Here, the ARGS for the instantiation of will be {int,
12302 double}. But, we only need as many ARGS as there are
12303 levels of template parameters in CODE_PATTERN. We are
12304 careful not to get fooled into reducing the ARGS in
12305 situations like:
12307 template <class T> struct S { template <class U> void f(U); }
12308 template <class T> template <> void S<T>::f(int) {}
12310 which we can spot because the pattern will be a
12311 specialization in this case. */
12312 int args_depth = TMPL_ARGS_DEPTH (args);
12313 int parms_depth =
12314 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12316 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12317 args = get_innermost_template_args (args, parms_depth);
12319 else
12321 /* This special case arises when we have something like this:
12323 template <class T> struct S {
12324 friend void f<int>(int, double);
12327 Here, the DECL_TI_TEMPLATE for the friend declaration
12328 will be an IDENTIFIER_NODE. We are being called from
12329 tsubst_friend_function, and we want only to create a
12330 new decl (R) with appropriate types so that we can call
12331 determine_specialization. */
12332 gen_tmpl = NULL_TREE;
12333 argvec = NULL_TREE;
12336 tree ctx = DECL_CONTEXT (t);
12337 bool member = ctx && TYPE_P (ctx);
12339 if (member)
12340 ctx = tsubst_aggr_type (ctx, args,
12341 complain, t, /*entering_scope=*/1);
12343 tree type = tsubst (TREE_TYPE (t), args,
12344 complain | tf_fndecl_type, in_decl);
12345 if (type == error_mark_node)
12346 RETURN (error_mark_node);
12348 /* If we hit excessive deduction depth, the type is bogus even if
12349 it isn't error_mark_node, so don't build a decl. */
12350 if (excessive_deduction_depth)
12351 RETURN (error_mark_node);
12353 /* We do NOT check for matching decls pushed separately at this
12354 point, as they may not represent instantiations of this
12355 template, and in any case are considered separate under the
12356 discrete model. */
12357 r = copy_decl (t);
12358 DECL_USE_TEMPLATE (r) = 0;
12359 TREE_TYPE (r) = type;
12360 /* Clear out the mangled name and RTL for the instantiation. */
12361 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12362 SET_DECL_RTL (r, NULL);
12363 /* Leave DECL_INITIAL set on deleted instantiations. */
12364 if (!DECL_DELETED_FN (r))
12365 DECL_INITIAL (r) = NULL_TREE;
12366 DECL_CONTEXT (r) = ctx;
12368 /* OpenMP UDRs have the only argument a reference to the declared
12369 type. We want to diagnose if the declared type is a reference,
12370 which is invalid, but as references to references are usually
12371 quietly merged, diagnose it here. */
12372 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12374 tree argtype
12375 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12376 argtype = tsubst (argtype, args, complain, in_decl);
12377 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12378 error_at (DECL_SOURCE_LOCATION (t),
12379 "reference type %qT in "
12380 "%<#pragma omp declare reduction%>", argtype);
12381 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12382 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12383 argtype);
12386 if (member && DECL_CONV_FN_P (r))
12387 /* Type-conversion operator. Reconstruct the name, in
12388 case it's the name of one of the template's parameters. */
12389 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12391 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12392 complain, t);
12393 DECL_RESULT (r) = NULL_TREE;
12395 TREE_STATIC (r) = 0;
12396 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12397 DECL_EXTERNAL (r) = 1;
12398 /* If this is an instantiation of a function with internal
12399 linkage, we already know what object file linkage will be
12400 assigned to the instantiation. */
12401 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12402 DECL_DEFER_OUTPUT (r) = 0;
12403 DECL_CHAIN (r) = NULL_TREE;
12404 DECL_PENDING_INLINE_INFO (r) = 0;
12405 DECL_PENDING_INLINE_P (r) = 0;
12406 DECL_SAVED_TREE (r) = NULL_TREE;
12407 DECL_STRUCT_FUNCTION (r) = NULL;
12408 TREE_USED (r) = 0;
12409 /* We'll re-clone as appropriate in instantiate_template. */
12410 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12412 /* If we aren't complaining now, return on error before we register
12413 the specialization so that we'll complain eventually. */
12414 if ((complain & tf_error) == 0
12415 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12416 && !grok_op_properties (r, /*complain=*/false))
12417 RETURN (error_mark_node);
12419 /* When instantiating a constrained member, substitute
12420 into the constraints to create a new constraint. */
12421 if (tree ci = get_constraints (t))
12422 if (member)
12424 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12425 set_constraints (r, ci);
12428 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12429 this in the special friend case mentioned above where
12430 GEN_TMPL is NULL. */
12431 if (gen_tmpl)
12433 DECL_TEMPLATE_INFO (r)
12434 = build_template_info (gen_tmpl, argvec);
12435 SET_DECL_IMPLICIT_INSTANTIATION (r);
12437 tree new_r
12438 = register_specialization (r, gen_tmpl, argvec, false, hash);
12439 if (new_r != r)
12440 /* We instantiated this while substituting into
12441 the type earlier (template/friend54.C). */
12442 RETURN (new_r);
12444 /* We're not supposed to instantiate default arguments
12445 until they are called, for a template. But, for a
12446 declaration like:
12448 template <class T> void f ()
12449 { extern void g(int i = T()); }
12451 we should do the substitution when the template is
12452 instantiated. We handle the member function case in
12453 instantiate_class_template since the default arguments
12454 might refer to other members of the class. */
12455 if (!member
12456 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12457 && !uses_template_parms (argvec))
12458 tsubst_default_arguments (r, complain);
12460 else
12461 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12463 /* Copy the list of befriending classes. */
12464 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12465 *friends;
12466 friends = &TREE_CHAIN (*friends))
12468 *friends = copy_node (*friends);
12469 TREE_VALUE (*friends)
12470 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12473 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12475 maybe_retrofit_in_chrg (r);
12476 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12477 RETURN (error_mark_node);
12478 /* If this is an instantiation of a member template, clone it.
12479 If it isn't, that'll be handled by
12480 clone_constructors_and_destructors. */
12481 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12482 clone_function_decl (r, /*update_methods=*/false);
12484 else if ((complain & tf_error) != 0
12485 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12486 && !grok_op_properties (r, /*complain=*/true))
12487 RETURN (error_mark_node);
12489 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12490 SET_DECL_FRIEND_CONTEXT (r,
12491 tsubst (DECL_FRIEND_CONTEXT (t),
12492 args, complain, in_decl));
12494 /* Possibly limit visibility based on template args. */
12495 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12496 if (DECL_VISIBILITY_SPECIFIED (t))
12498 DECL_VISIBILITY_SPECIFIED (r) = 0;
12499 DECL_ATTRIBUTES (r)
12500 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12502 determine_visibility (r);
12503 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12504 && !processing_template_decl)
12505 defaulted_late_check (r);
12507 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12508 args, complain, in_decl);
12510 break;
12512 case PARM_DECL:
12514 tree type = NULL_TREE;
12515 int i, len = 1;
12516 tree expanded_types = NULL_TREE;
12517 tree prev_r = NULL_TREE;
12518 tree first_r = NULL_TREE;
12520 if (DECL_PACK_P (t))
12522 /* If there is a local specialization that isn't a
12523 parameter pack, it means that we're doing a "simple"
12524 substitution from inside tsubst_pack_expansion. Just
12525 return the local specialization (which will be a single
12526 parm). */
12527 tree spec = retrieve_local_specialization (t);
12528 if (spec
12529 && TREE_CODE (spec) == PARM_DECL
12530 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12531 RETURN (spec);
12533 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12534 the parameters in this function parameter pack. */
12535 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12536 complain, in_decl);
12537 if (TREE_CODE (expanded_types) == TREE_VEC)
12539 len = TREE_VEC_LENGTH (expanded_types);
12541 /* Zero-length parameter packs are boring. Just substitute
12542 into the chain. */
12543 if (len == 0)
12544 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12545 TREE_CHAIN (t)));
12547 else
12549 /* All we did was update the type. Make a note of that. */
12550 type = expanded_types;
12551 expanded_types = NULL_TREE;
12555 /* Loop through all of the parameters we'll build. When T is
12556 a function parameter pack, LEN is the number of expanded
12557 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12558 r = NULL_TREE;
12559 for (i = 0; i < len; ++i)
12561 prev_r = r;
12562 r = copy_node (t);
12563 if (DECL_TEMPLATE_PARM_P (t))
12564 SET_DECL_TEMPLATE_PARM_P (r);
12566 if (expanded_types)
12567 /* We're on the Ith parameter of the function parameter
12568 pack. */
12570 /* Get the Ith type. */
12571 type = TREE_VEC_ELT (expanded_types, i);
12573 /* Rename the parameter to include the index. */
12574 DECL_NAME (r)
12575 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12577 else if (!type)
12578 /* We're dealing with a normal parameter. */
12579 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12581 type = type_decays_to (type);
12582 TREE_TYPE (r) = type;
12583 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12585 if (DECL_INITIAL (r))
12587 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12588 DECL_INITIAL (r) = TREE_TYPE (r);
12589 else
12590 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12591 complain, in_decl);
12594 DECL_CONTEXT (r) = NULL_TREE;
12596 if (!DECL_TEMPLATE_PARM_P (r))
12597 DECL_ARG_TYPE (r) = type_passed_as (type);
12599 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12600 args, complain, in_decl);
12602 /* Keep track of the first new parameter we
12603 generate. That's what will be returned to the
12604 caller. */
12605 if (!first_r)
12606 first_r = r;
12608 /* Build a proper chain of parameters when substituting
12609 into a function parameter pack. */
12610 if (prev_r)
12611 DECL_CHAIN (prev_r) = r;
12614 /* If cp_unevaluated_operand is set, we're just looking for a
12615 single dummy parameter, so don't keep going. */
12616 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12617 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12618 complain, DECL_CHAIN (t));
12620 /* FIRST_R contains the start of the chain we've built. */
12621 r = first_r;
12623 break;
12625 case FIELD_DECL:
12627 tree type = NULL_TREE;
12628 tree vec = NULL_TREE;
12629 tree expanded_types = NULL_TREE;
12630 int len = 1;
12632 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12634 /* This field is a lambda capture pack. Return a TREE_VEC of
12635 the expanded fields to instantiate_class_template_1 and
12636 store them in the specializations hash table as a
12637 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12638 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12639 complain, in_decl);
12640 if (TREE_CODE (expanded_types) == TREE_VEC)
12642 len = TREE_VEC_LENGTH (expanded_types);
12643 vec = make_tree_vec (len);
12645 else
12647 /* All we did was update the type. Make a note of that. */
12648 type = expanded_types;
12649 expanded_types = NULL_TREE;
12653 for (int i = 0; i < len; ++i)
12655 r = copy_decl (t);
12656 if (expanded_types)
12658 type = TREE_VEC_ELT (expanded_types, i);
12659 DECL_NAME (r)
12660 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12662 else if (!type)
12663 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12665 if (type == error_mark_node)
12666 RETURN (error_mark_node);
12667 TREE_TYPE (r) = type;
12668 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12670 if (DECL_C_BIT_FIELD (r))
12671 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12672 non-bit-fields DECL_INITIAL is a non-static data member
12673 initializer, which gets deferred instantiation. */
12674 DECL_INITIAL (r)
12675 = tsubst_expr (DECL_INITIAL (t), args,
12676 complain, in_decl,
12677 /*integral_constant_expression_p=*/true);
12678 else if (DECL_INITIAL (t))
12680 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12681 NSDMI in perform_member_init. Still set DECL_INITIAL
12682 so that we know there is one. */
12683 DECL_INITIAL (r) = void_node;
12684 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12685 retrofit_lang_decl (r);
12686 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12688 /* We don't have to set DECL_CONTEXT here; it is set by
12689 finish_member_declaration. */
12690 DECL_CHAIN (r) = NULL_TREE;
12692 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12693 args, complain, in_decl);
12695 if (vec)
12696 TREE_VEC_ELT (vec, i) = r;
12699 if (vec)
12701 r = vec;
12702 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12703 SET_ARGUMENT_PACK_ARGS (pack, vec);
12704 register_specialization (pack, t, args, false, 0);
12707 break;
12709 case USING_DECL:
12710 /* We reach here only for member using decls. We also need to check
12711 uses_template_parms because DECL_DEPENDENT_P is not set for a
12712 using-declaration that designates a member of the current
12713 instantiation (c++/53549). */
12714 if (DECL_DEPENDENT_P (t)
12715 || uses_template_parms (USING_DECL_SCOPE (t)))
12717 tree scope = USING_DECL_SCOPE (t);
12718 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12719 if (PACK_EXPANSION_P (scope))
12721 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12722 int len = TREE_VEC_LENGTH (vec);
12723 r = make_tree_vec (len);
12724 for (int i = 0; i < len; ++i)
12726 tree escope = TREE_VEC_ELT (vec, i);
12727 tree elt = do_class_using_decl (escope, name);
12728 if (!elt)
12730 r = error_mark_node;
12731 break;
12733 else
12735 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12736 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12738 TREE_VEC_ELT (r, i) = elt;
12741 else
12743 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12744 complain, in_decl);
12745 r = do_class_using_decl (inst_scope, name);
12746 if (!r)
12747 r = error_mark_node;
12748 else
12750 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12751 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12755 else
12757 r = copy_node (t);
12758 DECL_CHAIN (r) = NULL_TREE;
12760 break;
12762 case TYPE_DECL:
12763 case VAR_DECL:
12765 tree argvec = NULL_TREE;
12766 tree gen_tmpl = NULL_TREE;
12767 tree spec;
12768 tree tmpl = NULL_TREE;
12769 tree ctx;
12770 tree type = NULL_TREE;
12771 bool local_p;
12773 if (TREE_TYPE (t) == error_mark_node)
12774 RETURN (error_mark_node);
12776 if (TREE_CODE (t) == TYPE_DECL
12777 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12779 /* If this is the canonical decl, we don't have to
12780 mess with instantiations, and often we can't (for
12781 typename, template type parms and such). Note that
12782 TYPE_NAME is not correct for the above test if
12783 we've copied the type for a typedef. */
12784 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12785 if (type == error_mark_node)
12786 RETURN (error_mark_node);
12787 r = TYPE_NAME (type);
12788 break;
12791 /* Check to see if we already have the specialization we
12792 need. */
12793 spec = NULL_TREE;
12794 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12796 /* T is a static data member or namespace-scope entity.
12797 We have to substitute into namespace-scope variables
12798 (not just variable templates) because of cases like:
12800 template <class T> void f() { extern T t; }
12802 where the entity referenced is not known until
12803 instantiation time. */
12804 local_p = false;
12805 ctx = DECL_CONTEXT (t);
12806 if (DECL_CLASS_SCOPE_P (t))
12808 ctx = tsubst_aggr_type (ctx, args,
12809 complain,
12810 in_decl, /*entering_scope=*/1);
12811 /* If CTX is unchanged, then T is in fact the
12812 specialization we want. That situation occurs when
12813 referencing a static data member within in its own
12814 class. We can use pointer equality, rather than
12815 same_type_p, because DECL_CONTEXT is always
12816 canonical... */
12817 if (ctx == DECL_CONTEXT (t)
12818 /* ... unless T is a member template; in which
12819 case our caller can be willing to create a
12820 specialization of that template represented
12821 by T. */
12822 && !(DECL_TI_TEMPLATE (t)
12823 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12824 spec = t;
12827 if (!spec)
12829 tmpl = DECL_TI_TEMPLATE (t);
12830 gen_tmpl = most_general_template (tmpl);
12831 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12832 if (argvec != error_mark_node)
12833 argvec = (coerce_innermost_template_parms
12834 (DECL_TEMPLATE_PARMS (gen_tmpl),
12835 argvec, t, complain,
12836 /*all*/true, /*defarg*/true));
12837 if (argvec == error_mark_node)
12838 RETURN (error_mark_node);
12839 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12840 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12843 else
12845 /* A local variable. */
12846 local_p = true;
12847 /* Subsequent calls to pushdecl will fill this in. */
12848 ctx = NULL_TREE;
12849 /* Unless this is a reference to a static variable from an
12850 enclosing function, in which case we need to fill it in now. */
12851 if (TREE_STATIC (t))
12853 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12854 if (fn != current_function_decl)
12855 ctx = fn;
12857 spec = retrieve_local_specialization (t);
12859 /* If we already have the specialization we need, there is
12860 nothing more to do. */
12861 if (spec)
12863 r = spec;
12864 break;
12867 /* Create a new node for the specialization we need. */
12868 r = copy_decl (t);
12869 if (type == NULL_TREE)
12871 if (is_typedef_decl (t))
12872 type = DECL_ORIGINAL_TYPE (t);
12873 else
12874 type = TREE_TYPE (t);
12875 if (VAR_P (t)
12876 && VAR_HAD_UNKNOWN_BOUND (t)
12877 && type != error_mark_node)
12878 type = strip_array_domain (type);
12879 tree auto_node = type_uses_auto (type);
12880 int len = TREE_VEC_LENGTH (args);
12881 if (auto_node)
12882 /* Mask off any template args past the variable's context so we
12883 don't replace the auto with an unrelated argument. */
12884 TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
12885 type = tsubst (type, args, complain, in_decl);
12886 if (auto_node)
12887 TREE_VEC_LENGTH (args) = len;
12889 if (VAR_P (r))
12891 /* Even if the original location is out of scope, the
12892 newly substituted one is not. */
12893 DECL_DEAD_FOR_LOCAL (r) = 0;
12894 DECL_INITIALIZED_P (r) = 0;
12895 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12896 if (type == error_mark_node)
12897 RETURN (error_mark_node);
12898 if (TREE_CODE (type) == FUNCTION_TYPE)
12900 /* It may seem that this case cannot occur, since:
12902 typedef void f();
12903 void g() { f x; }
12905 declares a function, not a variable. However:
12907 typedef void f();
12908 template <typename T> void g() { T t; }
12909 template void g<f>();
12911 is an attempt to declare a variable with function
12912 type. */
12913 error ("variable %qD has function type",
12914 /* R is not yet sufficiently initialized, so we
12915 just use its name. */
12916 DECL_NAME (r));
12917 RETURN (error_mark_node);
12919 type = complete_type (type);
12920 /* Wait until cp_finish_decl to set this again, to handle
12921 circular dependency (template/instantiate6.C). */
12922 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12923 type = check_var_type (DECL_NAME (r), type);
12925 if (DECL_HAS_VALUE_EXPR_P (t))
12927 tree ve = DECL_VALUE_EXPR (t);
12928 ve = tsubst_expr (ve, args, complain, in_decl,
12929 /*constant_expression_p=*/false);
12930 if (REFERENCE_REF_P (ve))
12932 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12933 ve = TREE_OPERAND (ve, 0);
12935 SET_DECL_VALUE_EXPR (r, ve);
12937 if (CP_DECL_THREAD_LOCAL_P (r)
12938 && !processing_template_decl)
12939 set_decl_tls_model (r, decl_default_tls_model (r));
12941 else if (DECL_SELF_REFERENCE_P (t))
12942 SET_DECL_SELF_REFERENCE_P (r);
12943 TREE_TYPE (r) = type;
12944 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12945 DECL_CONTEXT (r) = ctx;
12946 /* Clear out the mangled name and RTL for the instantiation. */
12947 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12948 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12949 SET_DECL_RTL (r, NULL);
12950 /* The initializer must not be expanded until it is required;
12951 see [temp.inst]. */
12952 DECL_INITIAL (r) = NULL_TREE;
12953 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12954 if (VAR_P (r))
12956 SET_DECL_MODE (r, VOIDmode);
12958 /* Possibly limit visibility based on template args. */
12959 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12960 if (DECL_VISIBILITY_SPECIFIED (t))
12962 DECL_VISIBILITY_SPECIFIED (r) = 0;
12963 DECL_ATTRIBUTES (r)
12964 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12966 determine_visibility (r);
12969 if (!local_p)
12971 /* A static data member declaration is always marked
12972 external when it is declared in-class, even if an
12973 initializer is present. We mimic the non-template
12974 processing here. */
12975 DECL_EXTERNAL (r) = 1;
12976 if (DECL_NAMESPACE_SCOPE_P (t))
12977 DECL_NOT_REALLY_EXTERN (r) = 1;
12979 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12980 SET_DECL_IMPLICIT_INSTANTIATION (r);
12981 register_specialization (r, gen_tmpl, argvec, false, hash);
12983 else
12985 if (DECL_LANG_SPECIFIC (r))
12986 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12987 if (!cp_unevaluated_operand)
12988 register_local_specialization (r, t);
12991 DECL_CHAIN (r) = NULL_TREE;
12993 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12994 /*flags=*/0,
12995 args, complain, in_decl);
12997 /* Preserve a typedef that names a type. */
12998 if (is_typedef_decl (r) && type != error_mark_node)
13000 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13001 set_underlying_type (r);
13002 if (TYPE_DECL_ALIAS_P (r))
13003 /* An alias template specialization can be dependent
13004 even if its underlying type is not. */
13005 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13008 layout_decl (r, 0);
13010 break;
13012 default:
13013 gcc_unreachable ();
13015 #undef RETURN
13017 out:
13018 /* Restore the file and line information. */
13019 input_location = saved_loc;
13021 return r;
13024 /* Substitute into the ARG_TYPES of a function type.
13025 If END is a TREE_CHAIN, leave it and any following types
13026 un-substituted. */
13028 static tree
13029 tsubst_arg_types (tree arg_types,
13030 tree args,
13031 tree end,
13032 tsubst_flags_t complain,
13033 tree in_decl)
13035 tree remaining_arg_types;
13036 tree type = NULL_TREE;
13037 int i = 1;
13038 tree expanded_args = NULL_TREE;
13039 tree default_arg;
13041 if (!arg_types || arg_types == void_list_node || arg_types == end)
13042 return arg_types;
13044 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13045 args, end, complain, in_decl);
13046 if (remaining_arg_types == error_mark_node)
13047 return error_mark_node;
13049 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13051 /* For a pack expansion, perform substitution on the
13052 entire expression. Later on, we'll handle the arguments
13053 one-by-one. */
13054 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13055 args, complain, in_decl);
13057 if (TREE_CODE (expanded_args) == TREE_VEC)
13058 /* So that we'll spin through the parameters, one by one. */
13059 i = TREE_VEC_LENGTH (expanded_args);
13060 else
13062 /* We only partially substituted into the parameter
13063 pack. Our type is TYPE_PACK_EXPANSION. */
13064 type = expanded_args;
13065 expanded_args = NULL_TREE;
13069 while (i > 0) {
13070 --i;
13072 if (expanded_args)
13073 type = TREE_VEC_ELT (expanded_args, i);
13074 else if (!type)
13075 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13077 if (type == error_mark_node)
13078 return error_mark_node;
13079 if (VOID_TYPE_P (type))
13081 if (complain & tf_error)
13083 error ("invalid parameter type %qT", type);
13084 if (in_decl)
13085 error ("in declaration %q+D", in_decl);
13087 return error_mark_node;
13089 /* DR 657. */
13090 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13091 return error_mark_node;
13093 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13094 top-level qualifiers as required. */
13095 type = cv_unqualified (type_decays_to (type));
13097 /* We do not substitute into default arguments here. The standard
13098 mandates that they be instantiated only when needed, which is
13099 done in build_over_call. */
13100 default_arg = TREE_PURPOSE (arg_types);
13102 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13104 /* We've instantiated a template before its default arguments
13105 have been parsed. This can happen for a nested template
13106 class, and is not an error unless we require the default
13107 argument in a call of this function. */
13108 remaining_arg_types =
13109 tree_cons (default_arg, type, remaining_arg_types);
13110 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13112 else
13113 remaining_arg_types =
13114 hash_tree_cons (default_arg, type, remaining_arg_types);
13117 return remaining_arg_types;
13120 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13121 *not* handle the exception-specification for FNTYPE, because the
13122 initial substitution of explicitly provided template parameters
13123 during argument deduction forbids substitution into the
13124 exception-specification:
13126 [temp.deduct]
13128 All references in the function type of the function template to the
13129 corresponding template parameters are replaced by the specified tem-
13130 plate argument values. If a substitution in a template parameter or
13131 in the function type of the function template results in an invalid
13132 type, type deduction fails. [Note: The equivalent substitution in
13133 exception specifications is done only when the function is instanti-
13134 ated, at which point a program is ill-formed if the substitution
13135 results in an invalid type.] */
13137 static tree
13138 tsubst_function_type (tree t,
13139 tree args,
13140 tsubst_flags_t complain,
13141 tree in_decl)
13143 tree return_type;
13144 tree arg_types = NULL_TREE;
13145 tree fntype;
13147 /* The TYPE_CONTEXT is not used for function/method types. */
13148 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13150 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13151 failure. */
13152 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13154 if (late_return_type_p)
13156 /* Substitute the argument types. */
13157 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13158 complain, in_decl);
13159 if (arg_types == error_mark_node)
13160 return error_mark_node;
13162 tree save_ccp = current_class_ptr;
13163 tree save_ccr = current_class_ref;
13164 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13165 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13166 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13167 if (do_inject)
13169 /* DR 1207: 'this' is in scope in the trailing return type. */
13170 inject_this_parameter (this_type, cp_type_quals (this_type));
13173 /* Substitute the return type. */
13174 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13176 if (do_inject)
13178 current_class_ptr = save_ccp;
13179 current_class_ref = save_ccr;
13182 else
13183 /* Substitute the return type. */
13184 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13186 if (return_type == error_mark_node)
13187 return error_mark_node;
13188 /* DR 486 clarifies that creation of a function type with an
13189 invalid return type is a deduction failure. */
13190 if (TREE_CODE (return_type) == ARRAY_TYPE
13191 || TREE_CODE (return_type) == FUNCTION_TYPE)
13193 if (complain & tf_error)
13195 if (TREE_CODE (return_type) == ARRAY_TYPE)
13196 error ("function returning an array");
13197 else
13198 error ("function returning a function");
13200 return error_mark_node;
13202 /* And DR 657. */
13203 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13204 return error_mark_node;
13206 if (!late_return_type_p)
13208 /* Substitute the argument types. */
13209 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13210 complain, in_decl);
13211 if (arg_types == error_mark_node)
13212 return error_mark_node;
13215 /* Construct a new type node and return it. */
13216 if (TREE_CODE (t) == FUNCTION_TYPE)
13218 fntype = build_function_type (return_type, arg_types);
13219 fntype = apply_memfn_quals (fntype,
13220 type_memfn_quals (t),
13221 type_memfn_rqual (t));
13223 else
13225 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13226 /* Don't pick up extra function qualifiers from the basetype. */
13227 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13228 if (! MAYBE_CLASS_TYPE_P (r))
13230 /* [temp.deduct]
13232 Type deduction may fail for any of the following
13233 reasons:
13235 -- Attempting to create "pointer to member of T" when T
13236 is not a class type. */
13237 if (complain & tf_error)
13238 error ("creating pointer to member function of non-class type %qT",
13240 return error_mark_node;
13243 fntype = build_method_type_directly (r, return_type,
13244 TREE_CHAIN (arg_types));
13245 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13247 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13249 if (late_return_type_p)
13250 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13252 return fntype;
13255 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13256 ARGS into that specification, and return the substituted
13257 specification. If there is no specification, return NULL_TREE. */
13259 static tree
13260 tsubst_exception_specification (tree fntype,
13261 tree args,
13262 tsubst_flags_t complain,
13263 tree in_decl,
13264 bool defer_ok)
13266 tree specs;
13267 tree new_specs;
13269 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13270 new_specs = NULL_TREE;
13271 if (specs && TREE_PURPOSE (specs))
13273 /* A noexcept-specifier. */
13274 tree expr = TREE_PURPOSE (specs);
13275 if (TREE_CODE (expr) == INTEGER_CST)
13276 new_specs = expr;
13277 else if (defer_ok)
13279 /* Defer instantiation of noexcept-specifiers to avoid
13280 excessive instantiations (c++/49107). */
13281 new_specs = make_node (DEFERRED_NOEXCEPT);
13282 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13284 /* We already partially instantiated this member template,
13285 so combine the new args with the old. */
13286 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13287 = DEFERRED_NOEXCEPT_PATTERN (expr);
13288 DEFERRED_NOEXCEPT_ARGS (new_specs)
13289 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13291 else
13293 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13294 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13297 else
13298 new_specs = tsubst_copy_and_build
13299 (expr, args, complain, in_decl, /*function_p=*/false,
13300 /*integral_constant_expression_p=*/true);
13301 new_specs = build_noexcept_spec (new_specs, complain);
13303 else if (specs)
13305 if (! TREE_VALUE (specs))
13306 new_specs = specs;
13307 else
13308 while (specs)
13310 tree spec;
13311 int i, len = 1;
13312 tree expanded_specs = NULL_TREE;
13314 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13316 /* Expand the pack expansion type. */
13317 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13318 args, complain,
13319 in_decl);
13321 if (expanded_specs == error_mark_node)
13322 return error_mark_node;
13323 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13324 len = TREE_VEC_LENGTH (expanded_specs);
13325 else
13327 /* We're substituting into a member template, so
13328 we got a TYPE_PACK_EXPANSION back. Add that
13329 expansion and move on. */
13330 gcc_assert (TREE_CODE (expanded_specs)
13331 == TYPE_PACK_EXPANSION);
13332 new_specs = add_exception_specifier (new_specs,
13333 expanded_specs,
13334 complain);
13335 specs = TREE_CHAIN (specs);
13336 continue;
13340 for (i = 0; i < len; ++i)
13342 if (expanded_specs)
13343 spec = TREE_VEC_ELT (expanded_specs, i);
13344 else
13345 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13346 if (spec == error_mark_node)
13347 return spec;
13348 new_specs = add_exception_specifier (new_specs, spec,
13349 complain);
13352 specs = TREE_CHAIN (specs);
13355 return new_specs;
13358 /* Take the tree structure T and replace template parameters used
13359 therein with the argument vector ARGS. IN_DECL is an associated
13360 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13361 Issue error and warning messages under control of COMPLAIN. Note
13362 that we must be relatively non-tolerant of extensions here, in
13363 order to preserve conformance; if we allow substitutions that
13364 should not be allowed, we may allow argument deductions that should
13365 not succeed, and therefore report ambiguous overload situations
13366 where there are none. In theory, we could allow the substitution,
13367 but indicate that it should have failed, and allow our caller to
13368 make sure that the right thing happens, but we don't try to do this
13369 yet.
13371 This function is used for dealing with types, decls and the like;
13372 for expressions, use tsubst_expr or tsubst_copy. */
13374 tree
13375 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13377 enum tree_code code;
13378 tree type, r = NULL_TREE;
13380 if (t == NULL_TREE || t == error_mark_node
13381 || t == integer_type_node
13382 || t == void_type_node
13383 || t == char_type_node
13384 || t == unknown_type_node
13385 || TREE_CODE (t) == NAMESPACE_DECL
13386 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13387 return t;
13389 if (DECL_P (t))
13390 return tsubst_decl (t, args, complain);
13392 if (args == NULL_TREE)
13393 return t;
13395 code = TREE_CODE (t);
13397 if (code == IDENTIFIER_NODE)
13398 type = IDENTIFIER_TYPE_VALUE (t);
13399 else
13400 type = TREE_TYPE (t);
13402 gcc_assert (type != unknown_type_node);
13404 /* Reuse typedefs. We need to do this to handle dependent attributes,
13405 such as attribute aligned. */
13406 if (TYPE_P (t)
13407 && typedef_variant_p (t))
13409 tree decl = TYPE_NAME (t);
13411 if (alias_template_specialization_p (t))
13413 /* DECL represents an alias template and we want to
13414 instantiate it. */
13415 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13416 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13417 r = instantiate_alias_template (tmpl, gen_args, complain);
13419 else if (DECL_CLASS_SCOPE_P (decl)
13420 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13421 && uses_template_parms (DECL_CONTEXT (decl)))
13423 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13424 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13425 r = retrieve_specialization (tmpl, gen_args, 0);
13427 else if (DECL_FUNCTION_SCOPE_P (decl)
13428 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13429 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13430 r = retrieve_local_specialization (decl);
13431 else
13432 /* The typedef is from a non-template context. */
13433 return t;
13435 if (r)
13437 r = TREE_TYPE (r);
13438 r = cp_build_qualified_type_real
13439 (r, cp_type_quals (t) | cp_type_quals (r),
13440 complain | tf_ignore_bad_quals);
13441 return r;
13443 else
13445 /* We don't have an instantiation yet, so drop the typedef. */
13446 int quals = cp_type_quals (t);
13447 t = DECL_ORIGINAL_TYPE (decl);
13448 t = cp_build_qualified_type_real (t, quals,
13449 complain | tf_ignore_bad_quals);
13453 bool fndecl_type = (complain & tf_fndecl_type);
13454 complain &= ~tf_fndecl_type;
13456 if (type
13457 && code != TYPENAME_TYPE
13458 && code != TEMPLATE_TYPE_PARM
13459 && code != TEMPLATE_PARM_INDEX
13460 && code != IDENTIFIER_NODE
13461 && code != FUNCTION_TYPE
13462 && code != METHOD_TYPE)
13463 type = tsubst (type, args, complain, in_decl);
13464 if (type == error_mark_node)
13465 return error_mark_node;
13467 switch (code)
13469 case RECORD_TYPE:
13470 case UNION_TYPE:
13471 case ENUMERAL_TYPE:
13472 return tsubst_aggr_type (t, args, complain, in_decl,
13473 /*entering_scope=*/0);
13475 case ERROR_MARK:
13476 case IDENTIFIER_NODE:
13477 case VOID_TYPE:
13478 case REAL_TYPE:
13479 case COMPLEX_TYPE:
13480 case VECTOR_TYPE:
13481 case BOOLEAN_TYPE:
13482 case NULLPTR_TYPE:
13483 case LANG_TYPE:
13484 return t;
13486 case INTEGER_TYPE:
13487 if (t == integer_type_node)
13488 return t;
13490 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13491 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13492 return t;
13495 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13497 max = tsubst_expr (omax, args, complain, in_decl,
13498 /*integral_constant_expression_p=*/false);
13500 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13501 needed. */
13502 if (TREE_CODE (max) == NOP_EXPR
13503 && TREE_SIDE_EFFECTS (omax)
13504 && !TREE_TYPE (max))
13505 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13507 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13508 with TREE_SIDE_EFFECTS that indicates this is not an integral
13509 constant expression. */
13510 if (processing_template_decl
13511 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13513 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13514 TREE_SIDE_EFFECTS (max) = 1;
13517 return compute_array_index_type (NULL_TREE, max, complain);
13520 case TEMPLATE_TYPE_PARM:
13521 case TEMPLATE_TEMPLATE_PARM:
13522 case BOUND_TEMPLATE_TEMPLATE_PARM:
13523 case TEMPLATE_PARM_INDEX:
13525 int idx;
13526 int level;
13527 int levels;
13528 tree arg = NULL_TREE;
13530 /* Early in template argument deduction substitution, we don't
13531 want to reduce the level of 'auto', or it will be confused
13532 with a normal template parm in subsequent deduction. */
13533 if (is_auto (t) && (complain & tf_partial))
13534 return t;
13536 r = NULL_TREE;
13538 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13539 template_parm_level_and_index (t, &level, &idx);
13541 levels = TMPL_ARGS_DEPTH (args);
13542 if (level <= levels
13543 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13545 arg = TMPL_ARG (args, level, idx);
13547 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13549 /* See through ARGUMENT_PACK_SELECT arguments. */
13550 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13551 /* If the selected argument is an expansion E, that most
13552 likely means we were called from
13553 gen_elem_of_pack_expansion_instantiation during the
13554 substituting of pack an argument pack (which Ith
13555 element is a pack expansion, where I is
13556 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13557 In this case, the Ith element resulting from this
13558 substituting is going to be a pack expansion, which
13559 pattern is the pattern of E. Let's return the
13560 pattern of E, and
13561 gen_elem_of_pack_expansion_instantiation will
13562 build the resulting pack expansion from it. */
13563 if (PACK_EXPANSION_P (arg))
13565 /* Make sure we aren't throwing away arg info. */
13566 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13567 arg = PACK_EXPANSION_PATTERN (arg);
13572 if (arg == error_mark_node)
13573 return error_mark_node;
13574 else if (arg != NULL_TREE)
13576 if (ARGUMENT_PACK_P (arg))
13577 /* If ARG is an argument pack, we don't actually want to
13578 perform a substitution here, because substitutions
13579 for argument packs are only done
13580 element-by-element. We can get to this point when
13581 substituting the type of a non-type template
13582 parameter pack, when that type actually contains
13583 template parameter packs from an outer template, e.g.,
13585 template<typename... Types> struct A {
13586 template<Types... Values> struct B { };
13587 }; */
13588 return t;
13590 if (code == TEMPLATE_TYPE_PARM)
13592 int quals;
13593 gcc_assert (TYPE_P (arg));
13595 quals = cp_type_quals (arg) | cp_type_quals (t);
13597 return cp_build_qualified_type_real
13598 (arg, quals, complain | tf_ignore_bad_quals);
13600 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13602 /* We are processing a type constructed from a
13603 template template parameter. */
13604 tree argvec = tsubst (TYPE_TI_ARGS (t),
13605 args, complain, in_decl);
13606 if (argvec == error_mark_node)
13607 return error_mark_node;
13609 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13610 || TREE_CODE (arg) == TEMPLATE_DECL
13611 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13613 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13614 /* Consider this code:
13616 template <template <class> class Template>
13617 struct Internal {
13618 template <class Arg> using Bind = Template<Arg>;
13621 template <template <class> class Template, class Arg>
13622 using Instantiate = Template<Arg>; //#0
13624 template <template <class> class Template,
13625 class Argument>
13626 using Bind =
13627 Instantiate<Internal<Template>::template Bind,
13628 Argument>; //#1
13630 When #1 is parsed, the
13631 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13632 parameter `Template' in #0 matches the
13633 UNBOUND_CLASS_TEMPLATE representing the argument
13634 `Internal<Template>::template Bind'; We then want
13635 to assemble the type `Bind<Argument>' that can't
13636 be fully created right now, because
13637 `Internal<Template>' not being complete, the Bind
13638 template cannot be looked up in that context. So
13639 we need to "store" `Bind<Argument>' for later
13640 when the context of Bind becomes complete. Let's
13641 store that in a TYPENAME_TYPE. */
13642 return make_typename_type (TYPE_CONTEXT (arg),
13643 build_nt (TEMPLATE_ID_EXPR,
13644 TYPE_IDENTIFIER (arg),
13645 argvec),
13646 typename_type,
13647 complain);
13649 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13650 are resolving nested-types in the signature of a
13651 member function templates. Otherwise ARG is a
13652 TEMPLATE_DECL and is the real template to be
13653 instantiated. */
13654 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13655 arg = TYPE_NAME (arg);
13657 r = lookup_template_class (arg,
13658 argvec, in_decl,
13659 DECL_CONTEXT (arg),
13660 /*entering_scope=*/0,
13661 complain);
13662 return cp_build_qualified_type_real
13663 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13665 else if (code == TEMPLATE_TEMPLATE_PARM)
13666 return arg;
13667 else
13668 /* TEMPLATE_PARM_INDEX. */
13669 return convert_from_reference (unshare_expr (arg));
13672 if (level == 1)
13673 /* This can happen during the attempted tsubst'ing in
13674 unify. This means that we don't yet have any information
13675 about the template parameter in question. */
13676 return t;
13678 /* If we get here, we must have been looking at a parm for a
13679 more deeply nested template. Make a new version of this
13680 template parameter, but with a lower level. */
13681 switch (code)
13683 case TEMPLATE_TYPE_PARM:
13684 case TEMPLATE_TEMPLATE_PARM:
13685 case BOUND_TEMPLATE_TEMPLATE_PARM:
13686 if (cp_type_quals (t))
13688 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13689 r = cp_build_qualified_type_real
13690 (r, cp_type_quals (t),
13691 complain | (code == TEMPLATE_TYPE_PARM
13692 ? tf_ignore_bad_quals : 0));
13694 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13695 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13696 && (r = (TEMPLATE_PARM_DESCENDANTS
13697 (TEMPLATE_TYPE_PARM_INDEX (t))))
13698 && (r = TREE_TYPE (r))
13699 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13700 /* Break infinite recursion when substituting the constraints
13701 of a constrained placeholder. */;
13702 else
13704 r = copy_type (t);
13705 TEMPLATE_TYPE_PARM_INDEX (r)
13706 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13707 r, levels, args, complain);
13708 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13709 TYPE_MAIN_VARIANT (r) = r;
13710 TYPE_POINTER_TO (r) = NULL_TREE;
13711 TYPE_REFERENCE_TO (r) = NULL_TREE;
13713 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13715 /* Propagate constraints on placeholders. */
13716 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13717 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13718 = tsubst_constraint (constr, args, complain, in_decl);
13719 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13721 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13722 pl = tsubst (pl, args, complain, in_decl);
13723 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13727 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13728 /* We have reduced the level of the template
13729 template parameter, but not the levels of its
13730 template parameters, so canonical_type_parameter
13731 will not be able to find the canonical template
13732 template parameter for this level. Thus, we
13733 require structural equality checking to compare
13734 TEMPLATE_TEMPLATE_PARMs. */
13735 SET_TYPE_STRUCTURAL_EQUALITY (r);
13736 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13737 SET_TYPE_STRUCTURAL_EQUALITY (r);
13738 else
13739 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13741 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13743 tree tinfo = TYPE_TEMPLATE_INFO (t);
13744 /* We might need to substitute into the types of non-type
13745 template parameters. */
13746 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13747 complain, in_decl);
13748 if (tmpl == error_mark_node)
13749 return error_mark_node;
13750 tree argvec = tsubst (TI_ARGS (tinfo), args,
13751 complain, in_decl);
13752 if (argvec == error_mark_node)
13753 return error_mark_node;
13755 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13756 = build_template_info (tmpl, argvec);
13759 break;
13761 case TEMPLATE_PARM_INDEX:
13762 /* OK, now substitute the type of the non-type parameter. We
13763 couldn't do it earlier because it might be an auto parameter,
13764 and we wouldn't need to if we had an argument. */
13765 type = tsubst (type, args, complain, in_decl);
13766 r = reduce_template_parm_level (t, type, levels, args, complain);
13767 break;
13769 default:
13770 gcc_unreachable ();
13773 return r;
13776 case TREE_LIST:
13778 tree purpose, value, chain;
13780 if (t == void_list_node)
13781 return t;
13783 purpose = TREE_PURPOSE (t);
13784 if (purpose)
13786 purpose = tsubst (purpose, args, complain, in_decl);
13787 if (purpose == error_mark_node)
13788 return error_mark_node;
13790 value = TREE_VALUE (t);
13791 if (value)
13793 value = tsubst (value, args, complain, in_decl);
13794 if (value == error_mark_node)
13795 return error_mark_node;
13797 chain = TREE_CHAIN (t);
13798 if (chain && chain != void_type_node)
13800 chain = tsubst (chain, args, complain, in_decl);
13801 if (chain == error_mark_node)
13802 return error_mark_node;
13804 if (purpose == TREE_PURPOSE (t)
13805 && value == TREE_VALUE (t)
13806 && chain == TREE_CHAIN (t))
13807 return t;
13808 return hash_tree_cons (purpose, value, chain);
13811 case TREE_BINFO:
13812 /* We should never be tsubsting a binfo. */
13813 gcc_unreachable ();
13815 case TREE_VEC:
13816 /* A vector of template arguments. */
13817 gcc_assert (!type);
13818 return tsubst_template_args (t, args, complain, in_decl);
13820 case POINTER_TYPE:
13821 case REFERENCE_TYPE:
13823 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13824 return t;
13826 /* [temp.deduct]
13828 Type deduction may fail for any of the following
13829 reasons:
13831 -- Attempting to create a pointer to reference type.
13832 -- Attempting to create a reference to a reference type or
13833 a reference to void.
13835 Core issue 106 says that creating a reference to a reference
13836 during instantiation is no longer a cause for failure. We
13837 only enforce this check in strict C++98 mode. */
13838 if ((TREE_CODE (type) == REFERENCE_TYPE
13839 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13840 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13842 static location_t last_loc;
13844 /* We keep track of the last time we issued this error
13845 message to avoid spewing a ton of messages during a
13846 single bad template instantiation. */
13847 if (complain & tf_error
13848 && last_loc != input_location)
13850 if (VOID_TYPE_P (type))
13851 error ("forming reference to void");
13852 else if (code == POINTER_TYPE)
13853 error ("forming pointer to reference type %qT", type);
13854 else
13855 error ("forming reference to reference type %qT", type);
13856 last_loc = input_location;
13859 return error_mark_node;
13861 else if (TREE_CODE (type) == FUNCTION_TYPE
13862 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13863 || type_memfn_rqual (type) != REF_QUAL_NONE))
13865 if (complain & tf_error)
13867 if (code == POINTER_TYPE)
13868 error ("forming pointer to qualified function type %qT",
13869 type);
13870 else
13871 error ("forming reference to qualified function type %qT",
13872 type);
13874 return error_mark_node;
13876 else if (code == POINTER_TYPE)
13878 r = build_pointer_type (type);
13879 if (TREE_CODE (type) == METHOD_TYPE)
13880 r = build_ptrmemfunc_type (r);
13882 else if (TREE_CODE (type) == REFERENCE_TYPE)
13883 /* In C++0x, during template argument substitution, when there is an
13884 attempt to create a reference to a reference type, reference
13885 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13887 "If a template-argument for a template-parameter T names a type
13888 that is a reference to a type A, an attempt to create the type
13889 'lvalue reference to cv T' creates the type 'lvalue reference to
13890 A,' while an attempt to create the type type rvalue reference to
13891 cv T' creates the type T"
13893 r = cp_build_reference_type
13894 (TREE_TYPE (type),
13895 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13896 else
13897 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13898 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13900 if (r != error_mark_node)
13901 /* Will this ever be needed for TYPE_..._TO values? */
13902 layout_type (r);
13904 return r;
13906 case OFFSET_TYPE:
13908 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13909 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13911 /* [temp.deduct]
13913 Type deduction may fail for any of the following
13914 reasons:
13916 -- Attempting to create "pointer to member of T" when T
13917 is not a class type. */
13918 if (complain & tf_error)
13919 error ("creating pointer to member of non-class type %qT", r);
13920 return error_mark_node;
13922 if (TREE_CODE (type) == REFERENCE_TYPE)
13924 if (complain & tf_error)
13925 error ("creating pointer to member reference type %qT", type);
13926 return error_mark_node;
13928 if (VOID_TYPE_P (type))
13930 if (complain & tf_error)
13931 error ("creating pointer to member of type void");
13932 return error_mark_node;
13934 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13935 if (TREE_CODE (type) == FUNCTION_TYPE)
13937 /* The type of the implicit object parameter gets its
13938 cv-qualifiers from the FUNCTION_TYPE. */
13939 tree memptr;
13940 tree method_type
13941 = build_memfn_type (type, r, type_memfn_quals (type),
13942 type_memfn_rqual (type));
13943 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13944 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13945 complain);
13947 else
13948 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13949 cp_type_quals (t),
13950 complain);
13952 case FUNCTION_TYPE:
13953 case METHOD_TYPE:
13955 tree fntype;
13956 tree specs;
13957 fntype = tsubst_function_type (t, args, complain, in_decl);
13958 if (fntype == error_mark_node)
13959 return error_mark_node;
13961 /* Substitute the exception specification. */
13962 specs = tsubst_exception_specification (t, args, complain, in_decl,
13963 /*defer_ok*/fndecl_type);
13964 if (specs == error_mark_node)
13965 return error_mark_node;
13966 if (specs)
13967 fntype = build_exception_variant (fntype, specs);
13968 return fntype;
13970 case ARRAY_TYPE:
13972 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13973 if (domain == error_mark_node)
13974 return error_mark_node;
13976 /* As an optimization, we avoid regenerating the array type if
13977 it will obviously be the same as T. */
13978 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13979 return t;
13981 /* These checks should match the ones in create_array_type_for_decl.
13983 [temp.deduct]
13985 The deduction may fail for any of the following reasons:
13987 -- Attempting to create an array with an element type that
13988 is void, a function type, or a reference type, or [DR337]
13989 an abstract class type. */
13990 if (VOID_TYPE_P (type)
13991 || TREE_CODE (type) == FUNCTION_TYPE
13992 || (TREE_CODE (type) == ARRAY_TYPE
13993 && TYPE_DOMAIN (type) == NULL_TREE)
13994 || TREE_CODE (type) == REFERENCE_TYPE)
13996 if (complain & tf_error)
13997 error ("creating array of %qT", type);
13998 return error_mark_node;
14001 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14002 return error_mark_node;
14004 r = build_cplus_array_type (type, domain);
14006 if (TYPE_USER_ALIGN (t))
14008 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14009 TYPE_USER_ALIGN (r) = 1;
14012 return r;
14015 case TYPENAME_TYPE:
14017 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14018 in_decl, /*entering_scope=*/1);
14019 if (ctx == error_mark_node)
14020 return error_mark_node;
14022 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14023 complain, in_decl);
14024 if (f == error_mark_node)
14025 return error_mark_node;
14027 if (!MAYBE_CLASS_TYPE_P (ctx))
14029 if (complain & tf_error)
14030 error ("%qT is not a class, struct, or union type", ctx);
14031 return error_mark_node;
14033 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14035 /* Normally, make_typename_type does not require that the CTX
14036 have complete type in order to allow things like:
14038 template <class T> struct S { typename S<T>::X Y; };
14040 But, such constructs have already been resolved by this
14041 point, so here CTX really should have complete type, unless
14042 it's a partial instantiation. */
14043 ctx = complete_type (ctx);
14044 if (!COMPLETE_TYPE_P (ctx))
14046 if (complain & tf_error)
14047 cxx_incomplete_type_error (NULL_TREE, ctx);
14048 return error_mark_node;
14052 f = make_typename_type (ctx, f, typename_type,
14053 complain | tf_keep_type_decl);
14054 if (f == error_mark_node)
14055 return f;
14056 if (TREE_CODE (f) == TYPE_DECL)
14058 complain |= tf_ignore_bad_quals;
14059 f = TREE_TYPE (f);
14062 if (TREE_CODE (f) != TYPENAME_TYPE)
14064 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14066 if (complain & tf_error)
14067 error ("%qT resolves to %qT, which is not an enumeration type",
14068 t, f);
14069 else
14070 return error_mark_node;
14072 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14074 if (complain & tf_error)
14075 error ("%qT resolves to %qT, which is is not a class type",
14076 t, f);
14077 else
14078 return error_mark_node;
14082 return cp_build_qualified_type_real
14083 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14086 case UNBOUND_CLASS_TEMPLATE:
14088 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14089 in_decl, /*entering_scope=*/1);
14090 tree name = TYPE_IDENTIFIER (t);
14091 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14093 if (ctx == error_mark_node || name == error_mark_node)
14094 return error_mark_node;
14096 if (parm_list)
14097 parm_list = tsubst_template_parms (parm_list, args, complain);
14098 return make_unbound_class_template (ctx, name, parm_list, complain);
14101 case TYPEOF_TYPE:
14103 tree type;
14105 ++cp_unevaluated_operand;
14106 ++c_inhibit_evaluation_warnings;
14108 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14109 complain, in_decl,
14110 /*integral_constant_expression_p=*/false);
14112 --cp_unevaluated_operand;
14113 --c_inhibit_evaluation_warnings;
14115 type = finish_typeof (type);
14116 return cp_build_qualified_type_real (type,
14117 cp_type_quals (t)
14118 | cp_type_quals (type),
14119 complain);
14122 case DECLTYPE_TYPE:
14124 tree type;
14126 ++cp_unevaluated_operand;
14127 ++c_inhibit_evaluation_warnings;
14129 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14130 complain|tf_decltype, in_decl,
14131 /*function_p*/false,
14132 /*integral_constant_expression*/false);
14134 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14136 if (type == NULL_TREE)
14138 if (complain & tf_error)
14139 error ("empty initializer in lambda init-capture");
14140 type = error_mark_node;
14142 else if (TREE_CODE (type) == TREE_LIST)
14143 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14146 --cp_unevaluated_operand;
14147 --c_inhibit_evaluation_warnings;
14149 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14150 type = lambda_capture_field_type (type,
14151 DECLTYPE_FOR_INIT_CAPTURE (t),
14152 DECLTYPE_FOR_REF_CAPTURE (t));
14153 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14154 type = lambda_proxy_type (type);
14155 else
14157 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14158 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14159 && EXPR_P (type))
14160 /* In a template ~id could be either a complement expression
14161 or an unqualified-id naming a destructor; if instantiating
14162 it produces an expression, it's not an id-expression or
14163 member access. */
14164 id = false;
14165 type = finish_decltype_type (type, id, complain);
14167 return cp_build_qualified_type_real (type,
14168 cp_type_quals (t)
14169 | cp_type_quals (type),
14170 complain | tf_ignore_bad_quals);
14173 case UNDERLYING_TYPE:
14175 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14176 complain, in_decl);
14177 return finish_underlying_type (type);
14180 case TYPE_ARGUMENT_PACK:
14181 case NONTYPE_ARGUMENT_PACK:
14183 tree r;
14185 if (code == NONTYPE_ARGUMENT_PACK)
14186 r = make_node (code);
14187 else
14188 r = cxx_make_type (code);
14190 tree pack_args = ARGUMENT_PACK_ARGS (t);
14191 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14192 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14194 return r;
14197 case VOID_CST:
14198 case INTEGER_CST:
14199 case REAL_CST:
14200 case STRING_CST:
14201 case PLUS_EXPR:
14202 case MINUS_EXPR:
14203 case NEGATE_EXPR:
14204 case NOP_EXPR:
14205 case INDIRECT_REF:
14206 case ADDR_EXPR:
14207 case CALL_EXPR:
14208 case ARRAY_REF:
14209 case SCOPE_REF:
14210 /* We should use one of the expression tsubsts for these codes. */
14211 gcc_unreachable ();
14213 default:
14214 sorry ("use of %qs in template", get_tree_code_name (code));
14215 return error_mark_node;
14219 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14220 expression on the left-hand side of the "." or "->" operator. A
14221 baselink indicates a function from a base class. Both the
14222 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14223 bases of the template class, rather than the instantiated class.
14224 In addition, lookups that were not ambiguous before may be
14225 ambiguous now. Therefore, we perform the lookup again. */
14227 static tree
14228 tsubst_baselink (tree baselink, tree object_type,
14229 tree args, tsubst_flags_t complain, tree in_decl)
14231 bool qualified = BASELINK_QUALIFIED_P (baselink);
14233 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14234 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14236 tree optype = BASELINK_OPTYPE (baselink);
14237 optype = tsubst (optype, args, complain, in_decl);
14239 tree template_args = NULL_TREE;
14240 bool template_id_p = false;
14241 tree fns = BASELINK_FUNCTIONS (baselink);
14242 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14244 template_id_p = true;
14245 template_args = TREE_OPERAND (fns, 1);
14246 fns = TREE_OPERAND (fns, 0);
14247 if (template_args)
14248 template_args = tsubst_template_args (template_args, args,
14249 complain, in_decl);
14252 tree name = OVL_NAME (fns);
14253 if (IDENTIFIER_CONV_OP_P (name))
14254 name = make_conv_op_name (optype);
14256 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14257 if (!baselink)
14259 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14260 error ("cannot call constructor %<%T::%D%> directly",
14261 qualifying_scope, name);
14262 return error_mark_node;
14265 /* If lookup found a single function, mark it as used at this point.
14266 (If it lookup found multiple functions the one selected later by
14267 overload resolution will be marked as used at that point.) */
14268 if (BASELINK_P (baselink))
14269 fns = BASELINK_FUNCTIONS (baselink);
14270 if (!template_id_p && !really_overloaded_fn (fns)
14271 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14272 return error_mark_node;
14274 if (BASELINK_P (baselink))
14276 /* Add back the template arguments, if present. */
14277 if (template_id_p)
14278 BASELINK_FUNCTIONS (baselink)
14279 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14280 BASELINK_FUNCTIONS (baselink), template_args);
14282 /* Update the conversion operator type. */
14283 BASELINK_OPTYPE (baselink) = optype;
14286 if (!object_type)
14287 object_type = current_class_type;
14289 if (qualified || name == complete_dtor_identifier)
14291 baselink = adjust_result_of_qualified_name_lookup (baselink,
14292 qualifying_scope,
14293 object_type);
14294 if (!qualified)
14295 /* We need to call adjust_result_of_qualified_name_lookup in case the
14296 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14297 so that we still get virtual function binding. */
14298 BASELINK_QUALIFIED_P (baselink) = false;
14301 return baselink;
14304 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14305 true if the qualified-id will be a postfix-expression in-and-of
14306 itself; false if more of the postfix-expression follows the
14307 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14308 of "&". */
14310 static tree
14311 tsubst_qualified_id (tree qualified_id, tree args,
14312 tsubst_flags_t complain, tree in_decl,
14313 bool done, bool address_p)
14315 tree expr;
14316 tree scope;
14317 tree name;
14318 bool is_template;
14319 tree template_args;
14320 location_t loc = UNKNOWN_LOCATION;
14322 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14324 /* Figure out what name to look up. */
14325 name = TREE_OPERAND (qualified_id, 1);
14326 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14328 is_template = true;
14329 loc = EXPR_LOCATION (name);
14330 template_args = TREE_OPERAND (name, 1);
14331 if (template_args)
14332 template_args = tsubst_template_args (template_args, args,
14333 complain, in_decl);
14334 if (template_args == error_mark_node)
14335 return error_mark_node;
14336 name = TREE_OPERAND (name, 0);
14338 else
14340 is_template = false;
14341 template_args = NULL_TREE;
14344 /* Substitute into the qualifying scope. When there are no ARGS, we
14345 are just trying to simplify a non-dependent expression. In that
14346 case the qualifying scope may be dependent, and, in any case,
14347 substituting will not help. */
14348 scope = TREE_OPERAND (qualified_id, 0);
14349 if (args)
14351 scope = tsubst (scope, args, complain, in_decl);
14352 expr = tsubst_copy (name, args, complain, in_decl);
14354 else
14355 expr = name;
14357 if (dependent_scope_p (scope))
14359 if (is_template)
14360 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14361 tree r = build_qualified_name (NULL_TREE, scope, expr,
14362 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14363 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14364 return r;
14367 if (!BASELINK_P (name) && !DECL_P (expr))
14369 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14371 /* A BIT_NOT_EXPR is used to represent a destructor. */
14372 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14374 error ("qualifying type %qT does not match destructor name ~%qT",
14375 scope, TREE_OPERAND (expr, 0));
14376 expr = error_mark_node;
14378 else
14379 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14380 /*is_type_p=*/0, false);
14382 else
14383 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14384 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14385 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14387 if (complain & tf_error)
14389 error ("dependent-name %qE is parsed as a non-type, but "
14390 "instantiation yields a type", qualified_id);
14391 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14393 return error_mark_node;
14397 if (DECL_P (expr))
14399 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14400 scope);
14401 /* Remember that there was a reference to this entity. */
14402 if (!mark_used (expr, complain) && !(complain & tf_error))
14403 return error_mark_node;
14406 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14408 if (complain & tf_error)
14409 qualified_name_lookup_error (scope,
14410 TREE_OPERAND (qualified_id, 1),
14411 expr, input_location);
14412 return error_mark_node;
14415 if (is_template)
14417 if (variable_template_p (expr))
14418 expr = lookup_and_finish_template_variable (expr, template_args,
14419 complain);
14420 else
14421 expr = lookup_template_function (expr, template_args);
14424 if (expr == error_mark_node && complain & tf_error)
14425 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14426 expr, input_location);
14427 else if (TYPE_P (scope))
14429 expr = (adjust_result_of_qualified_name_lookup
14430 (expr, scope, current_nonlambda_class_type ()));
14431 expr = (finish_qualified_id_expr
14432 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14433 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14434 /*template_arg_p=*/false, complain));
14437 /* Expressions do not generally have reference type. */
14438 if (TREE_CODE (expr) != SCOPE_REF
14439 /* However, if we're about to form a pointer-to-member, we just
14440 want the referenced member referenced. */
14441 && TREE_CODE (expr) != OFFSET_REF)
14442 expr = convert_from_reference (expr);
14444 if (REF_PARENTHESIZED_P (qualified_id))
14445 expr = force_paren_expr (expr);
14447 return expr;
14450 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14451 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14452 for tsubst. */
14454 static tree
14455 tsubst_init (tree init, tree decl, tree args,
14456 tsubst_flags_t complain, tree in_decl)
14458 if (!init)
14459 return NULL_TREE;
14461 init = tsubst_expr (init, args, complain, in_decl, false);
14463 if (!init && TREE_TYPE (decl) != error_mark_node)
14465 /* If we had an initializer but it
14466 instantiated to nothing,
14467 value-initialize the object. This will
14468 only occur when the initializer was a
14469 pack expansion where the parameter packs
14470 used in that expansion were of length
14471 zero. */
14472 init = build_value_init (TREE_TYPE (decl),
14473 complain);
14474 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14475 init = get_target_expr_sfinae (init, complain);
14476 if (TREE_CODE (init) == TARGET_EXPR)
14477 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14480 return init;
14483 /* Like tsubst, but deals with expressions. This function just replaces
14484 template parms; to finish processing the resultant expression, use
14485 tsubst_copy_and_build or tsubst_expr. */
14487 static tree
14488 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14490 enum tree_code code;
14491 tree r;
14493 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14494 return t;
14496 code = TREE_CODE (t);
14498 switch (code)
14500 case PARM_DECL:
14501 r = retrieve_local_specialization (t);
14503 if (r == NULL_TREE)
14505 /* We get here for a use of 'this' in an NSDMI as part of a
14506 constructor call or as part of an aggregate initialization. */
14507 if (DECL_NAME (t) == this_identifier
14508 && ((current_function_decl
14509 && DECL_CONSTRUCTOR_P (current_function_decl))
14510 || (current_class_ref
14511 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14512 return current_class_ptr;
14514 /* This can happen for a parameter name used later in a function
14515 declaration (such as in a late-specified return type). Just
14516 make a dummy decl, since it's only used for its type. */
14517 gcc_assert (cp_unevaluated_operand != 0);
14518 r = tsubst_decl (t, args, complain);
14519 /* Give it the template pattern as its context; its true context
14520 hasn't been instantiated yet and this is good enough for
14521 mangling. */
14522 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14525 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14526 r = ARGUMENT_PACK_SELECT_ARG (r);
14527 if (!mark_used (r, complain) && !(complain & tf_error))
14528 return error_mark_node;
14529 return r;
14531 case CONST_DECL:
14533 tree enum_type;
14534 tree v;
14536 if (DECL_TEMPLATE_PARM_P (t))
14537 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14538 /* There is no need to substitute into namespace-scope
14539 enumerators. */
14540 if (DECL_NAMESPACE_SCOPE_P (t))
14541 return t;
14542 /* If ARGS is NULL, then T is known to be non-dependent. */
14543 if (args == NULL_TREE)
14544 return scalar_constant_value (t);
14546 /* Unfortunately, we cannot just call lookup_name here.
14547 Consider:
14549 template <int I> int f() {
14550 enum E { a = I };
14551 struct S { void g() { E e = a; } };
14554 When we instantiate f<7>::S::g(), say, lookup_name is not
14555 clever enough to find f<7>::a. */
14556 enum_type
14557 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14558 /*entering_scope=*/0);
14560 for (v = TYPE_VALUES (enum_type);
14561 v != NULL_TREE;
14562 v = TREE_CHAIN (v))
14563 if (TREE_PURPOSE (v) == DECL_NAME (t))
14564 return TREE_VALUE (v);
14566 /* We didn't find the name. That should never happen; if
14567 name-lookup found it during preliminary parsing, we
14568 should find it again here during instantiation. */
14569 gcc_unreachable ();
14571 return t;
14573 case FIELD_DECL:
14574 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14576 /* Check for a local specialization set up by
14577 tsubst_pack_expansion. */
14578 if (tree r = retrieve_local_specialization (t))
14580 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14581 r = ARGUMENT_PACK_SELECT_ARG (r);
14582 return r;
14585 /* When retrieving a capture pack from a generic lambda, remove the
14586 lambda call op's own template argument list from ARGS. Only the
14587 template arguments active for the closure type should be used to
14588 retrieve the pack specialization. */
14589 if (LAMBDA_FUNCTION_P (current_function_decl)
14590 && (template_class_depth (DECL_CONTEXT (t))
14591 != TMPL_ARGS_DEPTH (args)))
14592 args = strip_innermost_template_args (args, 1);
14594 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14595 tsubst_decl put in the hash table. */
14596 return retrieve_specialization (t, args, 0);
14599 if (DECL_CONTEXT (t))
14601 tree ctx;
14603 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14604 /*entering_scope=*/1);
14605 if (ctx != DECL_CONTEXT (t))
14607 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14608 if (!r)
14610 if (complain & tf_error)
14611 error ("using invalid field %qD", t);
14612 return error_mark_node;
14614 return r;
14618 return t;
14620 case VAR_DECL:
14621 case FUNCTION_DECL:
14622 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14623 r = tsubst (t, args, complain, in_decl);
14624 else if (local_variable_p (t)
14625 && uses_template_parms (DECL_CONTEXT (t)))
14627 r = retrieve_local_specialization (t);
14628 if (r == NULL_TREE)
14630 /* First try name lookup to find the instantiation. */
14631 r = lookup_name (DECL_NAME (t));
14632 if (r && !is_capture_proxy (r))
14634 /* Make sure that the one we found is the one we want. */
14635 tree ctx = DECL_CONTEXT (t);
14636 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14637 ctx = tsubst (ctx, args, complain, in_decl);
14638 if (ctx != DECL_CONTEXT (r))
14639 r = NULL_TREE;
14642 if (r)
14643 /* OK */;
14644 else
14646 /* This can happen for a variable used in a
14647 late-specified return type of a local lambda, or for a
14648 local static or constant. Building a new VAR_DECL
14649 should be OK in all those cases. */
14650 r = tsubst_decl (t, args, complain);
14651 if (local_specializations)
14652 /* Avoid infinite recursion (79640). */
14653 register_local_specialization (r, t);
14654 if (decl_maybe_constant_var_p (r))
14656 /* We can't call cp_finish_decl, so handle the
14657 initializer by hand. */
14658 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14659 complain, in_decl);
14660 if (!processing_template_decl)
14661 init = maybe_constant_init (init);
14662 if (processing_template_decl
14663 ? potential_constant_expression (init)
14664 : reduced_constant_expression_p (init))
14665 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14666 = TREE_CONSTANT (r) = true;
14667 DECL_INITIAL (r) = init;
14668 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14669 TREE_TYPE (r)
14670 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14671 complain, adc_variable_type);
14673 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14674 || decl_constant_var_p (r)
14675 || errorcount || sorrycount);
14676 if (!processing_template_decl
14677 && !TREE_STATIC (r))
14678 r = process_outer_var_ref (r, complain);
14680 /* Remember this for subsequent uses. */
14681 if (local_specializations)
14682 register_local_specialization (r, t);
14685 else
14686 r = t;
14687 if (!mark_used (r, complain))
14688 return error_mark_node;
14689 return r;
14691 case NAMESPACE_DECL:
14692 return t;
14694 case OVERLOAD:
14695 /* An OVERLOAD will always be a non-dependent overload set; an
14696 overload set from function scope will just be represented with an
14697 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14698 gcc_assert (!uses_template_parms (t));
14699 /* We must have marked any lookups as persistent. */
14700 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14701 return t;
14703 case BASELINK:
14704 return tsubst_baselink (t, current_nonlambda_class_type (),
14705 args, complain, in_decl);
14707 case TEMPLATE_DECL:
14708 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14709 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14710 args, complain, in_decl);
14711 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14712 return tsubst (t, args, complain, in_decl);
14713 else if (DECL_CLASS_SCOPE_P (t)
14714 && uses_template_parms (DECL_CONTEXT (t)))
14716 /* Template template argument like the following example need
14717 special treatment:
14719 template <template <class> class TT> struct C {};
14720 template <class T> struct D {
14721 template <class U> struct E {};
14722 C<E> c; // #1
14724 D<int> d; // #2
14726 We are processing the template argument `E' in #1 for
14727 the template instantiation #2. Originally, `E' is a
14728 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14729 have to substitute this with one having context `D<int>'. */
14731 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14732 if (dependent_scope_p (context))
14734 /* When rewriting a constructor into a deduction guide, a
14735 non-dependent name can become dependent, so memtmpl<args>
14736 becomes context::template memtmpl<args>. */
14737 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14738 return build_qualified_name (type, context, DECL_NAME (t),
14739 /*template*/true);
14741 return lookup_field (context, DECL_NAME(t), 0, false);
14743 else
14744 /* Ordinary template template argument. */
14745 return t;
14747 case CAST_EXPR:
14748 case REINTERPRET_CAST_EXPR:
14749 case CONST_CAST_EXPR:
14750 case STATIC_CAST_EXPR:
14751 case DYNAMIC_CAST_EXPR:
14752 case IMPLICIT_CONV_EXPR:
14753 case CONVERT_EXPR:
14754 case NOP_EXPR:
14756 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14757 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14758 return build1 (code, type, op0);
14761 case SIZEOF_EXPR:
14762 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14763 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14765 tree expanded, op = TREE_OPERAND (t, 0);
14766 int len = 0;
14768 if (SIZEOF_EXPR_TYPE_P (t))
14769 op = TREE_TYPE (op);
14771 ++cp_unevaluated_operand;
14772 ++c_inhibit_evaluation_warnings;
14773 /* We only want to compute the number of arguments. */
14774 if (PACK_EXPANSION_P (op))
14775 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14776 else
14777 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14778 args, complain, in_decl);
14779 --cp_unevaluated_operand;
14780 --c_inhibit_evaluation_warnings;
14782 if (TREE_CODE (expanded) == TREE_VEC)
14784 len = TREE_VEC_LENGTH (expanded);
14785 /* Set TREE_USED for the benefit of -Wunused. */
14786 for (int i = 0; i < len; i++)
14787 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14788 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14791 if (expanded == error_mark_node)
14792 return error_mark_node;
14793 else if (PACK_EXPANSION_P (expanded)
14794 || (TREE_CODE (expanded) == TREE_VEC
14795 && pack_expansion_args_count (expanded)))
14798 if (PACK_EXPANSION_P (expanded))
14799 /* OK. */;
14800 else if (TREE_VEC_LENGTH (expanded) == 1)
14801 expanded = TREE_VEC_ELT (expanded, 0);
14802 else
14803 expanded = make_argument_pack (expanded);
14805 if (TYPE_P (expanded))
14806 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14807 complain & tf_error);
14808 else
14809 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14810 complain & tf_error);
14812 else
14813 return build_int_cst (size_type_node, len);
14815 if (SIZEOF_EXPR_TYPE_P (t))
14817 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14818 args, complain, in_decl);
14819 r = build1 (NOP_EXPR, r, error_mark_node);
14820 r = build1 (SIZEOF_EXPR,
14821 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14822 SIZEOF_EXPR_TYPE_P (r) = 1;
14823 return r;
14825 /* Fall through */
14827 case INDIRECT_REF:
14828 case NEGATE_EXPR:
14829 case TRUTH_NOT_EXPR:
14830 case BIT_NOT_EXPR:
14831 case ADDR_EXPR:
14832 case UNARY_PLUS_EXPR: /* Unary + */
14833 case ALIGNOF_EXPR:
14834 case AT_ENCODE_EXPR:
14835 case ARROW_EXPR:
14836 case THROW_EXPR:
14837 case TYPEID_EXPR:
14838 case REALPART_EXPR:
14839 case IMAGPART_EXPR:
14840 case PAREN_EXPR:
14842 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14843 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14844 return build1 (code, type, op0);
14847 case COMPONENT_REF:
14849 tree object;
14850 tree name;
14852 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14853 name = TREE_OPERAND (t, 1);
14854 if (TREE_CODE (name) == BIT_NOT_EXPR)
14856 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14857 complain, in_decl);
14858 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14860 else if (TREE_CODE (name) == SCOPE_REF
14861 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14863 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14864 complain, in_decl);
14865 name = TREE_OPERAND (name, 1);
14866 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14867 complain, in_decl);
14868 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14869 name = build_qualified_name (/*type=*/NULL_TREE,
14870 base, name,
14871 /*template_p=*/false);
14873 else if (BASELINK_P (name))
14874 name = tsubst_baselink (name,
14875 non_reference (TREE_TYPE (object)),
14876 args, complain,
14877 in_decl);
14878 else
14879 name = tsubst_copy (name, args, complain, in_decl);
14880 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14883 case PLUS_EXPR:
14884 case MINUS_EXPR:
14885 case MULT_EXPR:
14886 case TRUNC_DIV_EXPR:
14887 case CEIL_DIV_EXPR:
14888 case FLOOR_DIV_EXPR:
14889 case ROUND_DIV_EXPR:
14890 case EXACT_DIV_EXPR:
14891 case BIT_AND_EXPR:
14892 case BIT_IOR_EXPR:
14893 case BIT_XOR_EXPR:
14894 case TRUNC_MOD_EXPR:
14895 case FLOOR_MOD_EXPR:
14896 case TRUTH_ANDIF_EXPR:
14897 case TRUTH_ORIF_EXPR:
14898 case TRUTH_AND_EXPR:
14899 case TRUTH_OR_EXPR:
14900 case RSHIFT_EXPR:
14901 case LSHIFT_EXPR:
14902 case RROTATE_EXPR:
14903 case LROTATE_EXPR:
14904 case EQ_EXPR:
14905 case NE_EXPR:
14906 case MAX_EXPR:
14907 case MIN_EXPR:
14908 case LE_EXPR:
14909 case GE_EXPR:
14910 case LT_EXPR:
14911 case GT_EXPR:
14912 case COMPOUND_EXPR:
14913 case DOTSTAR_EXPR:
14914 case MEMBER_REF:
14915 case PREDECREMENT_EXPR:
14916 case PREINCREMENT_EXPR:
14917 case POSTDECREMENT_EXPR:
14918 case POSTINCREMENT_EXPR:
14920 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14921 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14922 return build_nt (code, op0, op1);
14925 case SCOPE_REF:
14927 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14928 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14929 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14930 QUALIFIED_NAME_IS_TEMPLATE (t));
14933 case ARRAY_REF:
14935 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14936 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14937 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14940 case CALL_EXPR:
14942 int n = VL_EXP_OPERAND_LENGTH (t);
14943 tree result = build_vl_exp (CALL_EXPR, n);
14944 int i;
14945 for (i = 0; i < n; i++)
14946 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14947 complain, in_decl);
14948 return result;
14951 case COND_EXPR:
14952 case MODOP_EXPR:
14953 case PSEUDO_DTOR_EXPR:
14954 case VEC_PERM_EXPR:
14956 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14957 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14958 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14959 r = build_nt (code, op0, op1, op2);
14960 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14961 return r;
14964 case NEW_EXPR:
14966 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14967 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14968 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14969 r = build_nt (code, op0, op1, op2);
14970 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14971 return r;
14974 case DELETE_EXPR:
14976 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14977 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14978 r = build_nt (code, op0, op1);
14979 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14980 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14981 return r;
14984 case TEMPLATE_ID_EXPR:
14986 /* Substituted template arguments */
14987 tree fn = TREE_OPERAND (t, 0);
14988 tree targs = TREE_OPERAND (t, 1);
14990 fn = tsubst_copy (fn, args, complain, in_decl);
14991 if (targs)
14992 targs = tsubst_template_args (targs, args, complain, in_decl);
14994 return lookup_template_function (fn, targs);
14997 case TREE_LIST:
14999 tree purpose, value, chain;
15001 if (t == void_list_node)
15002 return t;
15004 purpose = TREE_PURPOSE (t);
15005 if (purpose)
15006 purpose = tsubst_copy (purpose, args, complain, in_decl);
15007 value = TREE_VALUE (t);
15008 if (value)
15009 value = tsubst_copy (value, args, complain, in_decl);
15010 chain = TREE_CHAIN (t);
15011 if (chain && chain != void_type_node)
15012 chain = tsubst_copy (chain, args, complain, in_decl);
15013 if (purpose == TREE_PURPOSE (t)
15014 && value == TREE_VALUE (t)
15015 && chain == TREE_CHAIN (t))
15016 return t;
15017 return tree_cons (purpose, value, chain);
15020 case RECORD_TYPE:
15021 case UNION_TYPE:
15022 case ENUMERAL_TYPE:
15023 case INTEGER_TYPE:
15024 case TEMPLATE_TYPE_PARM:
15025 case TEMPLATE_TEMPLATE_PARM:
15026 case BOUND_TEMPLATE_TEMPLATE_PARM:
15027 case TEMPLATE_PARM_INDEX:
15028 case POINTER_TYPE:
15029 case REFERENCE_TYPE:
15030 case OFFSET_TYPE:
15031 case FUNCTION_TYPE:
15032 case METHOD_TYPE:
15033 case ARRAY_TYPE:
15034 case TYPENAME_TYPE:
15035 case UNBOUND_CLASS_TEMPLATE:
15036 case TYPEOF_TYPE:
15037 case DECLTYPE_TYPE:
15038 case TYPE_DECL:
15039 return tsubst (t, args, complain, in_decl);
15041 case USING_DECL:
15042 t = DECL_NAME (t);
15043 /* Fall through. */
15044 case IDENTIFIER_NODE:
15045 if (IDENTIFIER_CONV_OP_P (t))
15047 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15048 return make_conv_op_name (new_type);
15050 else
15051 return t;
15053 case CONSTRUCTOR:
15054 /* This is handled by tsubst_copy_and_build. */
15055 gcc_unreachable ();
15057 case VA_ARG_EXPR:
15059 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15060 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15061 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15064 case CLEANUP_POINT_EXPR:
15065 /* We shouldn't have built any of these during initial template
15066 generation. Instead, they should be built during instantiation
15067 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15068 gcc_unreachable ();
15070 case OFFSET_REF:
15072 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15073 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15074 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15075 r = build2 (code, type, op0, op1);
15076 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15077 if (!mark_used (TREE_OPERAND (r, 1), complain)
15078 && !(complain & tf_error))
15079 return error_mark_node;
15080 return r;
15083 case EXPR_PACK_EXPANSION:
15084 error ("invalid use of pack expansion expression");
15085 return error_mark_node;
15087 case NONTYPE_ARGUMENT_PACK:
15088 error ("use %<...%> to expand argument pack");
15089 return error_mark_node;
15091 case VOID_CST:
15092 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15093 return t;
15095 case INTEGER_CST:
15096 case REAL_CST:
15097 case STRING_CST:
15098 case COMPLEX_CST:
15100 /* Instantiate any typedefs in the type. */
15101 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15102 r = fold_convert (type, t);
15103 gcc_assert (TREE_CODE (r) == code);
15104 return r;
15107 case PTRMEM_CST:
15108 /* These can sometimes show up in a partial instantiation, but never
15109 involve template parms. */
15110 gcc_assert (!uses_template_parms (t));
15111 return t;
15113 case UNARY_LEFT_FOLD_EXPR:
15114 return tsubst_unary_left_fold (t, args, complain, in_decl);
15115 case UNARY_RIGHT_FOLD_EXPR:
15116 return tsubst_unary_right_fold (t, args, complain, in_decl);
15117 case BINARY_LEFT_FOLD_EXPR:
15118 return tsubst_binary_left_fold (t, args, complain, in_decl);
15119 case BINARY_RIGHT_FOLD_EXPR:
15120 return tsubst_binary_right_fold (t, args, complain, in_decl);
15121 case PREDICT_EXPR:
15122 return t;
15124 default:
15125 /* We shouldn't get here, but keep going if !flag_checking. */
15126 if (flag_checking)
15127 gcc_unreachable ();
15128 return t;
15132 /* Helper function for tsubst_omp_clauses, used for instantiation of
15133 OMP_CLAUSE_DECL of clauses. */
15135 static tree
15136 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15137 tree in_decl)
15139 if (decl == NULL_TREE)
15140 return NULL_TREE;
15142 /* Handle an OpenMP array section represented as a TREE_LIST (or
15143 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15144 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15145 TREE_LIST. We can handle it exactly the same as an array section
15146 (purpose, value, and a chain), even though the nomenclature
15147 (low_bound, length, etc) is different. */
15148 if (TREE_CODE (decl) == TREE_LIST)
15150 tree low_bound
15151 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15152 /*integral_constant_expression_p=*/false);
15153 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15154 /*integral_constant_expression_p=*/false);
15155 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15156 in_decl);
15157 if (TREE_PURPOSE (decl) == low_bound
15158 && TREE_VALUE (decl) == length
15159 && TREE_CHAIN (decl) == chain)
15160 return decl;
15161 tree ret = tree_cons (low_bound, length, chain);
15162 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15163 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15164 return ret;
15166 tree ret = tsubst_expr (decl, args, complain, in_decl,
15167 /*integral_constant_expression_p=*/false);
15168 /* Undo convert_from_reference tsubst_expr could have called. */
15169 if (decl
15170 && REFERENCE_REF_P (ret)
15171 && !REFERENCE_REF_P (decl))
15172 ret = TREE_OPERAND (ret, 0);
15173 return ret;
15176 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15178 static tree
15179 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15180 tree args, tsubst_flags_t complain, tree in_decl)
15182 tree new_clauses = NULL_TREE, nc, oc;
15183 tree linear_no_step = NULL_TREE;
15185 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15187 nc = copy_node (oc);
15188 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15189 new_clauses = nc;
15191 switch (OMP_CLAUSE_CODE (nc))
15193 case OMP_CLAUSE_LASTPRIVATE:
15194 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15196 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15197 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15198 in_decl, /*integral_constant_expression_p=*/false);
15199 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15200 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15202 /* FALLTHRU */
15203 case OMP_CLAUSE_PRIVATE:
15204 case OMP_CLAUSE_SHARED:
15205 case OMP_CLAUSE_FIRSTPRIVATE:
15206 case OMP_CLAUSE_COPYIN:
15207 case OMP_CLAUSE_COPYPRIVATE:
15208 case OMP_CLAUSE_UNIFORM:
15209 case OMP_CLAUSE_DEPEND:
15210 case OMP_CLAUSE_FROM:
15211 case OMP_CLAUSE_TO:
15212 case OMP_CLAUSE_MAP:
15213 case OMP_CLAUSE_USE_DEVICE_PTR:
15214 case OMP_CLAUSE_IS_DEVICE_PTR:
15215 OMP_CLAUSE_DECL (nc)
15216 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15217 in_decl);
15218 break;
15219 case OMP_CLAUSE_TILE:
15220 case OMP_CLAUSE_IF:
15221 case OMP_CLAUSE_NUM_THREADS:
15222 case OMP_CLAUSE_SCHEDULE:
15223 case OMP_CLAUSE_COLLAPSE:
15224 case OMP_CLAUSE_FINAL:
15225 case OMP_CLAUSE_DEVICE:
15226 case OMP_CLAUSE_DIST_SCHEDULE:
15227 case OMP_CLAUSE_NUM_TEAMS:
15228 case OMP_CLAUSE_THREAD_LIMIT:
15229 case OMP_CLAUSE_SAFELEN:
15230 case OMP_CLAUSE_SIMDLEN:
15231 case OMP_CLAUSE_NUM_TASKS:
15232 case OMP_CLAUSE_GRAINSIZE:
15233 case OMP_CLAUSE_PRIORITY:
15234 case OMP_CLAUSE_ORDERED:
15235 case OMP_CLAUSE_HINT:
15236 case OMP_CLAUSE_NUM_GANGS:
15237 case OMP_CLAUSE_NUM_WORKERS:
15238 case OMP_CLAUSE_VECTOR_LENGTH:
15239 case OMP_CLAUSE_WORKER:
15240 case OMP_CLAUSE_VECTOR:
15241 case OMP_CLAUSE_ASYNC:
15242 case OMP_CLAUSE_WAIT:
15243 OMP_CLAUSE_OPERAND (nc, 0)
15244 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15245 in_decl, /*integral_constant_expression_p=*/false);
15246 break;
15247 case OMP_CLAUSE_REDUCTION:
15248 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15250 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15251 if (TREE_CODE (placeholder) == SCOPE_REF)
15253 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15254 complain, in_decl);
15255 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15256 = build_qualified_name (NULL_TREE, scope,
15257 TREE_OPERAND (placeholder, 1),
15258 false);
15260 else
15261 gcc_assert (identifier_p (placeholder));
15263 OMP_CLAUSE_DECL (nc)
15264 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15265 in_decl);
15266 break;
15267 case OMP_CLAUSE_GANG:
15268 case OMP_CLAUSE_ALIGNED:
15269 OMP_CLAUSE_DECL (nc)
15270 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15271 in_decl);
15272 OMP_CLAUSE_OPERAND (nc, 1)
15273 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15274 in_decl, /*integral_constant_expression_p=*/false);
15275 break;
15276 case OMP_CLAUSE_LINEAR:
15277 OMP_CLAUSE_DECL (nc)
15278 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15279 in_decl);
15280 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15282 gcc_assert (!linear_no_step);
15283 linear_no_step = nc;
15285 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15286 OMP_CLAUSE_LINEAR_STEP (nc)
15287 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15288 complain, in_decl);
15289 else
15290 OMP_CLAUSE_LINEAR_STEP (nc)
15291 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15292 in_decl,
15293 /*integral_constant_expression_p=*/false);
15294 break;
15295 case OMP_CLAUSE_NOWAIT:
15296 case OMP_CLAUSE_DEFAULT:
15297 case OMP_CLAUSE_UNTIED:
15298 case OMP_CLAUSE_MERGEABLE:
15299 case OMP_CLAUSE_INBRANCH:
15300 case OMP_CLAUSE_NOTINBRANCH:
15301 case OMP_CLAUSE_PROC_BIND:
15302 case OMP_CLAUSE_FOR:
15303 case OMP_CLAUSE_PARALLEL:
15304 case OMP_CLAUSE_SECTIONS:
15305 case OMP_CLAUSE_TASKGROUP:
15306 case OMP_CLAUSE_NOGROUP:
15307 case OMP_CLAUSE_THREADS:
15308 case OMP_CLAUSE_SIMD:
15309 case OMP_CLAUSE_DEFAULTMAP:
15310 case OMP_CLAUSE_INDEPENDENT:
15311 case OMP_CLAUSE_AUTO:
15312 case OMP_CLAUSE_SEQ:
15313 break;
15314 default:
15315 gcc_unreachable ();
15317 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15318 switch (OMP_CLAUSE_CODE (nc))
15320 case OMP_CLAUSE_SHARED:
15321 case OMP_CLAUSE_PRIVATE:
15322 case OMP_CLAUSE_FIRSTPRIVATE:
15323 case OMP_CLAUSE_LASTPRIVATE:
15324 case OMP_CLAUSE_COPYPRIVATE:
15325 case OMP_CLAUSE_LINEAR:
15326 case OMP_CLAUSE_REDUCTION:
15327 case OMP_CLAUSE_USE_DEVICE_PTR:
15328 case OMP_CLAUSE_IS_DEVICE_PTR:
15329 /* tsubst_expr on SCOPE_REF results in returning
15330 finish_non_static_data_member result. Undo that here. */
15331 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15332 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15333 == IDENTIFIER_NODE))
15335 tree t = OMP_CLAUSE_DECL (nc);
15336 tree v = t;
15337 while (v)
15338 switch (TREE_CODE (v))
15340 case COMPONENT_REF:
15341 case MEM_REF:
15342 case INDIRECT_REF:
15343 CASE_CONVERT:
15344 case POINTER_PLUS_EXPR:
15345 v = TREE_OPERAND (v, 0);
15346 continue;
15347 case PARM_DECL:
15348 if (DECL_CONTEXT (v) == current_function_decl
15349 && DECL_ARTIFICIAL (v)
15350 && DECL_NAME (v) == this_identifier)
15351 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15352 /* FALLTHRU */
15353 default:
15354 v = NULL_TREE;
15355 break;
15358 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15359 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15360 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15361 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15362 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15364 tree decl = OMP_CLAUSE_DECL (nc);
15365 if (VAR_P (decl))
15367 retrofit_lang_decl (decl);
15368 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15371 break;
15372 default:
15373 break;
15377 new_clauses = nreverse (new_clauses);
15378 if (ort != C_ORT_OMP_DECLARE_SIMD)
15380 new_clauses = finish_omp_clauses (new_clauses, ort);
15381 if (linear_no_step)
15382 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15383 if (nc == linear_no_step)
15385 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15386 break;
15389 return new_clauses;
15392 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15394 static tree
15395 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15396 tree in_decl)
15398 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15400 tree purpose, value, chain;
15402 if (t == NULL)
15403 return t;
15405 if (TREE_CODE (t) != TREE_LIST)
15406 return tsubst_copy_and_build (t, args, complain, in_decl,
15407 /*function_p=*/false,
15408 /*integral_constant_expression_p=*/false);
15410 if (t == void_list_node)
15411 return t;
15413 purpose = TREE_PURPOSE (t);
15414 if (purpose)
15415 purpose = RECUR (purpose);
15416 value = TREE_VALUE (t);
15417 if (value)
15419 if (TREE_CODE (value) != LABEL_DECL)
15420 value = RECUR (value);
15421 else
15423 value = lookup_label (DECL_NAME (value));
15424 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15425 TREE_USED (value) = 1;
15428 chain = TREE_CHAIN (t);
15429 if (chain && chain != void_type_node)
15430 chain = RECUR (chain);
15431 return tree_cons (purpose, value, chain);
15432 #undef RECUR
15435 /* Used to temporarily communicate the list of #pragma omp parallel
15436 clauses to #pragma omp for instantiation if they are combined
15437 together. */
15439 static tree *omp_parallel_combined_clauses;
15441 /* Substitute one OMP_FOR iterator. */
15443 static void
15444 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15445 tree initv, tree condv, tree incrv, tree *clauses,
15446 tree args, tsubst_flags_t complain, tree in_decl,
15447 bool integral_constant_expression_p)
15449 #define RECUR(NODE) \
15450 tsubst_expr ((NODE), args, complain, in_decl, \
15451 integral_constant_expression_p)
15452 tree decl, init, cond, incr;
15454 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15455 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15457 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15459 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15460 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15463 decl = TREE_OPERAND (init, 0);
15464 init = TREE_OPERAND (init, 1);
15465 tree decl_expr = NULL_TREE;
15466 if (init && TREE_CODE (init) == DECL_EXPR)
15468 /* We need to jump through some hoops to handle declarations in the
15469 init-statement, since we might need to handle auto deduction,
15470 but we need to keep control of initialization. */
15471 decl_expr = init;
15472 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15473 decl = tsubst_decl (decl, args, complain);
15475 else
15477 if (TREE_CODE (decl) == SCOPE_REF)
15479 decl = RECUR (decl);
15480 if (TREE_CODE (decl) == COMPONENT_REF)
15482 tree v = decl;
15483 while (v)
15484 switch (TREE_CODE (v))
15486 case COMPONENT_REF:
15487 case MEM_REF:
15488 case INDIRECT_REF:
15489 CASE_CONVERT:
15490 case POINTER_PLUS_EXPR:
15491 v = TREE_OPERAND (v, 0);
15492 continue;
15493 case PARM_DECL:
15494 if (DECL_CONTEXT (v) == current_function_decl
15495 && DECL_ARTIFICIAL (v)
15496 && DECL_NAME (v) == this_identifier)
15498 decl = TREE_OPERAND (decl, 1);
15499 decl = omp_privatize_field (decl, false);
15501 /* FALLTHRU */
15502 default:
15503 v = NULL_TREE;
15504 break;
15508 else
15509 decl = RECUR (decl);
15511 init = RECUR (init);
15513 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15514 if (auto_node && init)
15515 TREE_TYPE (decl)
15516 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15518 gcc_assert (!type_dependent_expression_p (decl));
15520 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15522 if (decl_expr)
15524 /* Declare the variable, but don't let that initialize it. */
15525 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15526 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15527 RECUR (decl_expr);
15528 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15531 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15532 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15533 if (TREE_CODE (incr) == MODIFY_EXPR)
15535 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15536 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15537 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15538 NOP_EXPR, rhs, complain);
15540 else
15541 incr = RECUR (incr);
15542 TREE_VEC_ELT (declv, i) = decl;
15543 TREE_VEC_ELT (initv, i) = init;
15544 TREE_VEC_ELT (condv, i) = cond;
15545 TREE_VEC_ELT (incrv, i) = incr;
15546 return;
15549 if (decl_expr)
15551 /* Declare and initialize the variable. */
15552 RECUR (decl_expr);
15553 init = NULL_TREE;
15555 else if (init)
15557 tree *pc;
15558 int j;
15559 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15561 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15563 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15564 && OMP_CLAUSE_DECL (*pc) == decl)
15565 break;
15566 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15567 && OMP_CLAUSE_DECL (*pc) == decl)
15569 if (j)
15570 break;
15571 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15572 tree c = *pc;
15573 *pc = OMP_CLAUSE_CHAIN (c);
15574 OMP_CLAUSE_CHAIN (c) = *clauses;
15575 *clauses = c;
15577 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15578 && OMP_CLAUSE_DECL (*pc) == decl)
15580 error ("iteration variable %qD should not be firstprivate",
15581 decl);
15582 *pc = OMP_CLAUSE_CHAIN (*pc);
15584 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15585 && OMP_CLAUSE_DECL (*pc) == decl)
15587 error ("iteration variable %qD should not be reduction",
15588 decl);
15589 *pc = OMP_CLAUSE_CHAIN (*pc);
15591 else
15592 pc = &OMP_CLAUSE_CHAIN (*pc);
15594 if (*pc)
15595 break;
15597 if (*pc == NULL_TREE)
15599 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15600 OMP_CLAUSE_DECL (c) = decl;
15601 c = finish_omp_clauses (c, C_ORT_OMP);
15602 if (c)
15604 OMP_CLAUSE_CHAIN (c) = *clauses;
15605 *clauses = c;
15609 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15610 if (COMPARISON_CLASS_P (cond))
15612 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15613 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15614 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15616 else
15617 cond = RECUR (cond);
15618 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15619 switch (TREE_CODE (incr))
15621 case PREINCREMENT_EXPR:
15622 case PREDECREMENT_EXPR:
15623 case POSTINCREMENT_EXPR:
15624 case POSTDECREMENT_EXPR:
15625 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15626 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15627 break;
15628 case MODIFY_EXPR:
15629 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15630 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15632 tree rhs = TREE_OPERAND (incr, 1);
15633 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15634 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15635 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15636 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15637 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15638 rhs0, rhs1));
15640 else
15641 incr = RECUR (incr);
15642 break;
15643 case MODOP_EXPR:
15644 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15645 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15647 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15648 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15649 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15650 TREE_TYPE (decl), lhs,
15651 RECUR (TREE_OPERAND (incr, 2))));
15653 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15654 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15655 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15657 tree rhs = TREE_OPERAND (incr, 2);
15658 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15659 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15660 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15661 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15662 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15663 rhs0, rhs1));
15665 else
15666 incr = RECUR (incr);
15667 break;
15668 default:
15669 incr = RECUR (incr);
15670 break;
15673 TREE_VEC_ELT (declv, i) = decl;
15674 TREE_VEC_ELT (initv, i) = init;
15675 TREE_VEC_ELT (condv, i) = cond;
15676 TREE_VEC_ELT (incrv, i) = incr;
15677 #undef RECUR
15680 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15681 of OMP_TARGET's body. */
15683 static tree
15684 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15686 *walk_subtrees = 0;
15687 switch (TREE_CODE (*tp))
15689 case OMP_TEAMS:
15690 return *tp;
15691 case BIND_EXPR:
15692 case STATEMENT_LIST:
15693 *walk_subtrees = 1;
15694 break;
15695 default:
15696 break;
15698 return NULL_TREE;
15701 /* Helper function for tsubst_expr. For decomposition declaration
15702 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15703 also the corresponding decls representing the identifiers
15704 of the decomposition declaration. Return DECL if successful
15705 or error_mark_node otherwise, set *FIRST to the first decl
15706 in the list chained through DECL_CHAIN and *CNT to the number
15707 of such decls. */
15709 static tree
15710 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15711 tsubst_flags_t complain, tree in_decl, tree *first,
15712 unsigned int *cnt)
15714 tree decl2, decl3, prev = decl;
15715 *cnt = 0;
15716 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15717 for (decl2 = DECL_CHAIN (pattern_decl);
15718 decl2
15719 && VAR_P (decl2)
15720 && DECL_DECOMPOSITION_P (decl2)
15721 && DECL_NAME (decl2);
15722 decl2 = DECL_CHAIN (decl2))
15724 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15726 gcc_assert (errorcount);
15727 return error_mark_node;
15729 (*cnt)++;
15730 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15731 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15732 tree v = DECL_VALUE_EXPR (decl2);
15733 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15734 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15735 decl3 = tsubst (decl2, args, complain, in_decl);
15736 SET_DECL_VALUE_EXPR (decl2, v);
15737 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15738 if (VAR_P (decl3))
15739 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15740 maybe_push_decl (decl3);
15741 if (error_operand_p (decl3))
15742 decl = error_mark_node;
15743 else if (decl != error_mark_node
15744 && DECL_CHAIN (decl3) != prev)
15746 gcc_assert (errorcount);
15747 decl = error_mark_node;
15749 else
15750 prev = decl3;
15752 *first = prev;
15753 return decl;
15756 /* Like tsubst_copy for expressions, etc. but also does semantic
15757 processing. */
15759 tree
15760 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15761 bool integral_constant_expression_p)
15763 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15764 #define RECUR(NODE) \
15765 tsubst_expr ((NODE), args, complain, in_decl, \
15766 integral_constant_expression_p)
15768 tree stmt, tmp;
15769 tree r;
15770 location_t loc;
15772 if (t == NULL_TREE || t == error_mark_node)
15773 return t;
15775 loc = input_location;
15776 if (EXPR_HAS_LOCATION (t))
15777 input_location = EXPR_LOCATION (t);
15778 if (STATEMENT_CODE_P (TREE_CODE (t)))
15779 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15781 switch (TREE_CODE (t))
15783 case STATEMENT_LIST:
15785 tree_stmt_iterator i;
15786 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15787 RECUR (tsi_stmt (i));
15788 break;
15791 case CTOR_INITIALIZER:
15792 finish_mem_initializers (tsubst_initializer_list
15793 (TREE_OPERAND (t, 0), args));
15794 break;
15796 case RETURN_EXPR:
15797 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15798 break;
15800 case EXPR_STMT:
15801 tmp = RECUR (EXPR_STMT_EXPR (t));
15802 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15803 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15804 else
15805 finish_expr_stmt (tmp);
15806 break;
15808 case USING_STMT:
15809 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15810 /*attribs=*/NULL_TREE);
15811 break;
15813 case DECL_EXPR:
15815 tree decl, pattern_decl;
15816 tree init;
15818 pattern_decl = decl = DECL_EXPR_DECL (t);
15819 if (TREE_CODE (decl) == LABEL_DECL)
15820 finish_label_decl (DECL_NAME (decl));
15821 else if (TREE_CODE (decl) == USING_DECL)
15823 tree scope = USING_DECL_SCOPE (decl);
15824 tree name = DECL_NAME (decl);
15826 scope = tsubst (scope, args, complain, in_decl);
15827 decl = lookup_qualified_name (scope, name,
15828 /*is_type_p=*/false,
15829 /*complain=*/false);
15830 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15831 qualified_name_lookup_error (scope, name, decl, input_location);
15832 else
15833 finish_local_using_decl (decl, scope, name);
15835 else if (DECL_PACK_P (decl))
15837 /* Don't build up decls for a variadic capture proxy, we'll
15838 instantiate the elements directly as needed. */
15839 break;
15841 else
15843 init = DECL_INITIAL (decl);
15844 decl = tsubst (decl, args, complain, in_decl);
15845 if (decl != error_mark_node)
15847 /* By marking the declaration as instantiated, we avoid
15848 trying to instantiate it. Since instantiate_decl can't
15849 handle local variables, and since we've already done
15850 all that needs to be done, that's the right thing to
15851 do. */
15852 if (VAR_P (decl))
15853 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15854 if (VAR_P (decl)
15855 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15856 /* Anonymous aggregates are a special case. */
15857 finish_anon_union (decl);
15858 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15860 DECL_CONTEXT (decl) = current_function_decl;
15861 if (DECL_NAME (decl) == this_identifier)
15863 tree lam = DECL_CONTEXT (current_function_decl);
15864 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15865 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15867 insert_capture_proxy (decl);
15869 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15870 /* We already did a pushtag. */;
15871 else if (TREE_CODE (decl) == FUNCTION_DECL
15872 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15873 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15875 DECL_CONTEXT (decl) = NULL_TREE;
15876 pushdecl (decl);
15877 DECL_CONTEXT (decl) = current_function_decl;
15878 cp_check_omp_declare_reduction (decl);
15880 else
15882 int const_init = false;
15883 maybe_push_decl (decl);
15884 if (VAR_P (decl)
15885 && DECL_PRETTY_FUNCTION_P (decl))
15887 /* For __PRETTY_FUNCTION__ we have to adjust the
15888 initializer. */
15889 const char *const name
15890 = cxx_printable_name (current_function_decl, 2);
15891 init = cp_fname_init (name, &TREE_TYPE (decl));
15893 else
15894 init = tsubst_init (init, decl, args, complain, in_decl);
15896 if (VAR_P (decl))
15897 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15898 (pattern_decl));
15899 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15900 if (VAR_P (decl)
15901 && DECL_DECOMPOSITION_P (decl)
15902 && TREE_TYPE (pattern_decl) != error_mark_node)
15904 unsigned int cnt;
15905 tree first;
15906 decl = tsubst_decomp_names (decl, pattern_decl, args,
15907 complain, in_decl, &first,
15908 &cnt);
15909 if (decl != error_mark_node)
15910 cp_finish_decomp (decl, first, cnt);
15916 break;
15919 case FOR_STMT:
15920 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15921 RECUR (FOR_INIT_STMT (t));
15922 finish_init_stmt (stmt);
15923 tmp = RECUR (FOR_COND (t));
15924 finish_for_cond (tmp, stmt, false);
15925 tmp = RECUR (FOR_EXPR (t));
15926 finish_for_expr (tmp, stmt);
15927 RECUR (FOR_BODY (t));
15928 finish_for_stmt (stmt);
15929 break;
15931 case RANGE_FOR_STMT:
15933 tree decl, expr;
15934 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15935 decl = RANGE_FOR_DECL (t);
15936 decl = tsubst (decl, args, complain, in_decl);
15937 maybe_push_decl (decl);
15938 expr = RECUR (RANGE_FOR_EXPR (t));
15939 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15941 unsigned int cnt;
15942 tree first;
15943 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15944 complain, in_decl, &first, &cnt);
15945 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15946 RANGE_FOR_IVDEP (t));
15948 else
15949 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15950 RANGE_FOR_IVDEP (t));
15951 RECUR (RANGE_FOR_BODY (t));
15952 finish_for_stmt (stmt);
15954 break;
15956 case WHILE_STMT:
15957 stmt = begin_while_stmt ();
15958 tmp = RECUR (WHILE_COND (t));
15959 finish_while_stmt_cond (tmp, stmt, false);
15960 RECUR (WHILE_BODY (t));
15961 finish_while_stmt (stmt);
15962 break;
15964 case DO_STMT:
15965 stmt = begin_do_stmt ();
15966 RECUR (DO_BODY (t));
15967 finish_do_body (stmt);
15968 tmp = RECUR (DO_COND (t));
15969 finish_do_stmt (tmp, stmt, false);
15970 break;
15972 case IF_STMT:
15973 stmt = begin_if_stmt ();
15974 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15975 tmp = RECUR (IF_COND (t));
15976 tmp = finish_if_stmt_cond (tmp, stmt);
15977 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15978 /* Don't instantiate the THEN_CLAUSE. */;
15979 else
15981 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15982 if (inhibit)
15983 ++c_inhibit_evaluation_warnings;
15984 RECUR (THEN_CLAUSE (t));
15985 if (inhibit)
15986 --c_inhibit_evaluation_warnings;
15988 finish_then_clause (stmt);
15990 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15991 /* Don't instantiate the ELSE_CLAUSE. */;
15992 else if (ELSE_CLAUSE (t))
15994 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15995 begin_else_clause (stmt);
15996 if (inhibit)
15997 ++c_inhibit_evaluation_warnings;
15998 RECUR (ELSE_CLAUSE (t));
15999 if (inhibit)
16000 --c_inhibit_evaluation_warnings;
16001 finish_else_clause (stmt);
16004 finish_if_stmt (stmt);
16005 break;
16007 case BIND_EXPR:
16008 if (BIND_EXPR_BODY_BLOCK (t))
16009 stmt = begin_function_body ();
16010 else
16011 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16012 ? BCS_TRY_BLOCK : 0);
16014 RECUR (BIND_EXPR_BODY (t));
16016 if (BIND_EXPR_BODY_BLOCK (t))
16017 finish_function_body (stmt);
16018 else
16019 finish_compound_stmt (stmt);
16020 break;
16022 case BREAK_STMT:
16023 finish_break_stmt ();
16024 break;
16026 case CONTINUE_STMT:
16027 finish_continue_stmt ();
16028 break;
16030 case SWITCH_STMT:
16031 stmt = begin_switch_stmt ();
16032 tmp = RECUR (SWITCH_STMT_COND (t));
16033 finish_switch_cond (tmp, stmt);
16034 RECUR (SWITCH_STMT_BODY (t));
16035 finish_switch_stmt (stmt);
16036 break;
16038 case CASE_LABEL_EXPR:
16040 tree low = RECUR (CASE_LOW (t));
16041 tree high = RECUR (CASE_HIGH (t));
16042 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16043 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16044 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16045 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16047 break;
16049 case LABEL_EXPR:
16051 tree decl = LABEL_EXPR_LABEL (t);
16052 tree label;
16054 label = finish_label_stmt (DECL_NAME (decl));
16055 if (TREE_CODE (label) == LABEL_DECL)
16056 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16057 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16058 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16060 break;
16062 case GOTO_EXPR:
16063 tmp = GOTO_DESTINATION (t);
16064 if (TREE_CODE (tmp) != LABEL_DECL)
16065 /* Computed goto's must be tsubst'd into. On the other hand,
16066 non-computed gotos must not be; the identifier in question
16067 will have no binding. */
16068 tmp = RECUR (tmp);
16069 else
16070 tmp = DECL_NAME (tmp);
16071 finish_goto_stmt (tmp);
16072 break;
16074 case ASM_EXPR:
16076 tree string = RECUR (ASM_STRING (t));
16077 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16078 complain, in_decl);
16079 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16080 complain, in_decl);
16081 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16082 complain, in_decl);
16083 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16084 complain, in_decl);
16085 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16086 clobbers, labels);
16087 tree asm_expr = tmp;
16088 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16089 asm_expr = TREE_OPERAND (asm_expr, 0);
16090 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16092 break;
16094 case TRY_BLOCK:
16095 if (CLEANUP_P (t))
16097 stmt = begin_try_block ();
16098 RECUR (TRY_STMTS (t));
16099 finish_cleanup_try_block (stmt);
16100 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16102 else
16104 tree compound_stmt = NULL_TREE;
16106 if (FN_TRY_BLOCK_P (t))
16107 stmt = begin_function_try_block (&compound_stmt);
16108 else
16109 stmt = begin_try_block ();
16111 RECUR (TRY_STMTS (t));
16113 if (FN_TRY_BLOCK_P (t))
16114 finish_function_try_block (stmt);
16115 else
16116 finish_try_block (stmt);
16118 RECUR (TRY_HANDLERS (t));
16119 if (FN_TRY_BLOCK_P (t))
16120 finish_function_handler_sequence (stmt, compound_stmt);
16121 else
16122 finish_handler_sequence (stmt);
16124 break;
16126 case HANDLER:
16128 tree decl = HANDLER_PARMS (t);
16130 if (decl)
16132 decl = tsubst (decl, args, complain, in_decl);
16133 /* Prevent instantiate_decl from trying to instantiate
16134 this variable. We've already done all that needs to be
16135 done. */
16136 if (decl != error_mark_node)
16137 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16139 stmt = begin_handler ();
16140 finish_handler_parms (decl, stmt);
16141 RECUR (HANDLER_BODY (t));
16142 finish_handler (stmt);
16144 break;
16146 case TAG_DEFN:
16147 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16148 if (CLASS_TYPE_P (tmp))
16150 /* Local classes are not independent templates; they are
16151 instantiated along with their containing function. And this
16152 way we don't have to deal with pushing out of one local class
16153 to instantiate a member of another local class. */
16154 /* Closures are handled by the LAMBDA_EXPR. */
16155 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16156 complete_type (tmp);
16157 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16158 if ((VAR_P (fld)
16159 || (TREE_CODE (fld) == FUNCTION_DECL
16160 && !DECL_ARTIFICIAL (fld)))
16161 && DECL_TEMPLATE_INSTANTIATION (fld))
16162 instantiate_decl (fld, /*defer_ok=*/false,
16163 /*expl_inst_class=*/false);
16165 break;
16167 case STATIC_ASSERT:
16169 tree condition;
16171 ++c_inhibit_evaluation_warnings;
16172 condition =
16173 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16174 args,
16175 complain, in_decl,
16176 /*integral_constant_expression_p=*/true);
16177 --c_inhibit_evaluation_warnings;
16179 finish_static_assert (condition,
16180 STATIC_ASSERT_MESSAGE (t),
16181 STATIC_ASSERT_SOURCE_LOCATION (t),
16182 /*member_p=*/false);
16184 break;
16186 case OACC_KERNELS:
16187 case OACC_PARALLEL:
16188 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16189 in_decl);
16190 stmt = begin_omp_parallel ();
16191 RECUR (OMP_BODY (t));
16192 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16193 break;
16195 case OMP_PARALLEL:
16196 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16197 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16198 complain, in_decl);
16199 if (OMP_PARALLEL_COMBINED (t))
16200 omp_parallel_combined_clauses = &tmp;
16201 stmt = begin_omp_parallel ();
16202 RECUR (OMP_PARALLEL_BODY (t));
16203 gcc_assert (omp_parallel_combined_clauses == NULL);
16204 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16205 = OMP_PARALLEL_COMBINED (t);
16206 pop_omp_privatization_clauses (r);
16207 break;
16209 case OMP_TASK:
16210 r = push_omp_privatization_clauses (false);
16211 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16212 complain, in_decl);
16213 stmt = begin_omp_task ();
16214 RECUR (OMP_TASK_BODY (t));
16215 finish_omp_task (tmp, stmt);
16216 pop_omp_privatization_clauses (r);
16217 break;
16219 case OMP_FOR:
16220 case OMP_SIMD:
16221 case CILK_SIMD:
16222 case CILK_FOR:
16223 case OMP_DISTRIBUTE:
16224 case OMP_TASKLOOP:
16225 case OACC_LOOP:
16227 tree clauses, body, pre_body;
16228 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16229 tree orig_declv = NULL_TREE;
16230 tree incrv = NULL_TREE;
16231 enum c_omp_region_type ort = C_ORT_OMP;
16232 int i;
16234 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16235 ort = C_ORT_CILK;
16236 else if (TREE_CODE (t) == OACC_LOOP)
16237 ort = C_ORT_ACC;
16239 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16240 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16241 in_decl);
16242 if (OMP_FOR_INIT (t) != NULL_TREE)
16244 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16245 if (OMP_FOR_ORIG_DECLS (t))
16246 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16247 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16248 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16249 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16252 stmt = begin_omp_structured_block ();
16254 pre_body = push_stmt_list ();
16255 RECUR (OMP_FOR_PRE_BODY (t));
16256 pre_body = pop_stmt_list (pre_body);
16258 if (OMP_FOR_INIT (t) != NULL_TREE)
16259 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16260 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16261 incrv, &clauses, args, complain, in_decl,
16262 integral_constant_expression_p);
16263 omp_parallel_combined_clauses = NULL;
16265 body = push_stmt_list ();
16266 RECUR (OMP_FOR_BODY (t));
16267 body = pop_stmt_list (body);
16269 if (OMP_FOR_INIT (t) != NULL_TREE)
16270 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16271 orig_declv, initv, condv, incrv, body, pre_body,
16272 NULL, clauses);
16273 else
16275 t = make_node (TREE_CODE (t));
16276 TREE_TYPE (t) = void_type_node;
16277 OMP_FOR_BODY (t) = body;
16278 OMP_FOR_PRE_BODY (t) = pre_body;
16279 OMP_FOR_CLAUSES (t) = clauses;
16280 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16281 add_stmt (t);
16284 add_stmt (finish_omp_structured_block (stmt));
16285 pop_omp_privatization_clauses (r);
16287 break;
16289 case OMP_SECTIONS:
16290 omp_parallel_combined_clauses = NULL;
16291 /* FALLTHRU */
16292 case OMP_SINGLE:
16293 case OMP_TEAMS:
16294 case OMP_CRITICAL:
16295 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16296 && OMP_TEAMS_COMBINED (t));
16297 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16298 in_decl);
16299 stmt = push_stmt_list ();
16300 RECUR (OMP_BODY (t));
16301 stmt = pop_stmt_list (stmt);
16303 t = copy_node (t);
16304 OMP_BODY (t) = stmt;
16305 OMP_CLAUSES (t) = tmp;
16306 add_stmt (t);
16307 pop_omp_privatization_clauses (r);
16308 break;
16310 case OACC_DATA:
16311 case OMP_TARGET_DATA:
16312 case OMP_TARGET:
16313 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16314 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16315 in_decl);
16316 keep_next_level (true);
16317 stmt = begin_omp_structured_block ();
16319 RECUR (OMP_BODY (t));
16320 stmt = finish_omp_structured_block (stmt);
16322 t = copy_node (t);
16323 OMP_BODY (t) = stmt;
16324 OMP_CLAUSES (t) = tmp;
16325 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16327 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16328 if (teams)
16330 /* For combined target teams, ensure the num_teams and
16331 thread_limit clause expressions are evaluated on the host,
16332 before entering the target construct. */
16333 tree c;
16334 for (c = OMP_TEAMS_CLAUSES (teams);
16335 c; c = OMP_CLAUSE_CHAIN (c))
16336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16338 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16340 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16341 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16342 if (expr == error_mark_node)
16343 continue;
16344 tmp = TARGET_EXPR_SLOT (expr);
16345 add_stmt (expr);
16346 OMP_CLAUSE_OPERAND (c, 0) = expr;
16347 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16348 OMP_CLAUSE_FIRSTPRIVATE);
16349 OMP_CLAUSE_DECL (tc) = tmp;
16350 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16351 OMP_TARGET_CLAUSES (t) = tc;
16355 add_stmt (t);
16356 break;
16358 case OACC_DECLARE:
16359 t = copy_node (t);
16360 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16361 complain, in_decl);
16362 OACC_DECLARE_CLAUSES (t) = tmp;
16363 add_stmt (t);
16364 break;
16366 case OMP_TARGET_UPDATE:
16367 case OMP_TARGET_ENTER_DATA:
16368 case OMP_TARGET_EXIT_DATA:
16369 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16370 complain, in_decl);
16371 t = copy_node (t);
16372 OMP_STANDALONE_CLAUSES (t) = tmp;
16373 add_stmt (t);
16374 break;
16376 case OACC_ENTER_DATA:
16377 case OACC_EXIT_DATA:
16378 case OACC_UPDATE:
16379 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16380 complain, in_decl);
16381 t = copy_node (t);
16382 OMP_STANDALONE_CLAUSES (t) = tmp;
16383 add_stmt (t);
16384 break;
16386 case OMP_ORDERED:
16387 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16388 complain, in_decl);
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 OMP_ORDERED_CLAUSES (t) = tmp;
16396 add_stmt (t);
16397 break;
16399 case OMP_SECTION:
16400 case OMP_MASTER:
16401 case OMP_TASKGROUP:
16402 stmt = push_stmt_list ();
16403 RECUR (OMP_BODY (t));
16404 stmt = pop_stmt_list (stmt);
16406 t = copy_node (t);
16407 OMP_BODY (t) = stmt;
16408 add_stmt (t);
16409 break;
16411 case OMP_ATOMIC:
16412 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16413 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16415 tree op1 = TREE_OPERAND (t, 1);
16416 tree rhs1 = NULL_TREE;
16417 tree lhs, rhs;
16418 if (TREE_CODE (op1) == COMPOUND_EXPR)
16420 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16421 op1 = TREE_OPERAND (op1, 1);
16423 lhs = RECUR (TREE_OPERAND (op1, 0));
16424 rhs = RECUR (TREE_OPERAND (op1, 1));
16425 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16426 NULL_TREE, NULL_TREE, rhs1,
16427 OMP_ATOMIC_SEQ_CST (t));
16429 else
16431 tree op1 = TREE_OPERAND (t, 1);
16432 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16433 tree rhs1 = NULL_TREE;
16434 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16435 enum tree_code opcode = NOP_EXPR;
16436 if (code == OMP_ATOMIC_READ)
16438 v = RECUR (TREE_OPERAND (op1, 0));
16439 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16441 else if (code == OMP_ATOMIC_CAPTURE_OLD
16442 || code == OMP_ATOMIC_CAPTURE_NEW)
16444 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16445 v = RECUR (TREE_OPERAND (op1, 0));
16446 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16447 if (TREE_CODE (op11) == COMPOUND_EXPR)
16449 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16450 op11 = TREE_OPERAND (op11, 1);
16452 lhs = RECUR (TREE_OPERAND (op11, 0));
16453 rhs = RECUR (TREE_OPERAND (op11, 1));
16454 opcode = TREE_CODE (op11);
16455 if (opcode == MODIFY_EXPR)
16456 opcode = NOP_EXPR;
16458 else
16460 code = OMP_ATOMIC;
16461 lhs = RECUR (TREE_OPERAND (op1, 0));
16462 rhs = RECUR (TREE_OPERAND (op1, 1));
16464 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16465 OMP_ATOMIC_SEQ_CST (t));
16467 break;
16469 case TRANSACTION_EXPR:
16471 int flags = 0;
16472 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16473 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16475 if (TRANSACTION_EXPR_IS_STMT (t))
16477 tree body = TRANSACTION_EXPR_BODY (t);
16478 tree noex = NULL_TREE;
16479 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16481 noex = MUST_NOT_THROW_COND (body);
16482 if (noex == NULL_TREE)
16483 noex = boolean_true_node;
16484 body = TREE_OPERAND (body, 0);
16486 stmt = begin_transaction_stmt (input_location, NULL, flags);
16487 RECUR (body);
16488 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16490 else
16492 stmt = build_transaction_expr (EXPR_LOCATION (t),
16493 RECUR (TRANSACTION_EXPR_BODY (t)),
16494 flags, NULL_TREE);
16495 RETURN (stmt);
16498 break;
16500 case MUST_NOT_THROW_EXPR:
16502 tree op0 = RECUR (TREE_OPERAND (t, 0));
16503 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16504 RETURN (build_must_not_throw_expr (op0, cond));
16507 case EXPR_PACK_EXPANSION:
16508 error ("invalid use of pack expansion expression");
16509 RETURN (error_mark_node);
16511 case NONTYPE_ARGUMENT_PACK:
16512 error ("use %<...%> to expand argument pack");
16513 RETURN (error_mark_node);
16515 case CILK_SPAWN_STMT:
16516 cfun->calls_cilk_spawn = 1;
16517 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16519 case CILK_SYNC_STMT:
16520 RETURN (build_cilk_sync ());
16522 case COMPOUND_EXPR:
16523 tmp = RECUR (TREE_OPERAND (t, 0));
16524 if (tmp == NULL_TREE)
16525 /* If the first operand was a statement, we're done with it. */
16526 RETURN (RECUR (TREE_OPERAND (t, 1)));
16527 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16528 RECUR (TREE_OPERAND (t, 1)),
16529 complain));
16531 case ANNOTATE_EXPR:
16532 tmp = RECUR (TREE_OPERAND (t, 0));
16533 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16534 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16536 default:
16537 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16539 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16540 /*function_p=*/false,
16541 integral_constant_expression_p));
16544 RETURN (NULL_TREE);
16545 out:
16546 input_location = loc;
16547 return r;
16548 #undef RECUR
16549 #undef RETURN
16552 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16553 function. For description of the body see comment above
16554 cp_parser_omp_declare_reduction_exprs. */
16556 static void
16557 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16559 if (t == NULL_TREE || t == error_mark_node)
16560 return;
16562 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16564 tree_stmt_iterator tsi;
16565 int i;
16566 tree stmts[7];
16567 memset (stmts, 0, sizeof stmts);
16568 for (i = 0, tsi = tsi_start (t);
16569 i < 7 && !tsi_end_p (tsi);
16570 i++, tsi_next (&tsi))
16571 stmts[i] = tsi_stmt (tsi);
16572 gcc_assert (tsi_end_p (tsi));
16574 if (i >= 3)
16576 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16577 && TREE_CODE (stmts[1]) == DECL_EXPR);
16578 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16579 args, complain, in_decl);
16580 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16581 args, complain, in_decl);
16582 DECL_CONTEXT (omp_out) = current_function_decl;
16583 DECL_CONTEXT (omp_in) = current_function_decl;
16584 keep_next_level (true);
16585 tree block = begin_omp_structured_block ();
16586 tsubst_expr (stmts[2], args, complain, in_decl, false);
16587 block = finish_omp_structured_block (block);
16588 block = maybe_cleanup_point_expr_void (block);
16589 add_decl_expr (omp_out);
16590 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16591 TREE_NO_WARNING (omp_out) = 1;
16592 add_decl_expr (omp_in);
16593 finish_expr_stmt (block);
16595 if (i >= 6)
16597 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16598 && TREE_CODE (stmts[4]) == DECL_EXPR);
16599 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16600 args, complain, in_decl);
16601 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16602 args, complain, in_decl);
16603 DECL_CONTEXT (omp_priv) = current_function_decl;
16604 DECL_CONTEXT (omp_orig) = current_function_decl;
16605 keep_next_level (true);
16606 tree block = begin_omp_structured_block ();
16607 tsubst_expr (stmts[5], args, complain, in_decl, false);
16608 block = finish_omp_structured_block (block);
16609 block = maybe_cleanup_point_expr_void (block);
16610 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16611 add_decl_expr (omp_priv);
16612 add_decl_expr (omp_orig);
16613 finish_expr_stmt (block);
16614 if (i == 7)
16615 add_decl_expr (omp_orig);
16619 /* T is a postfix-expression that is not being used in a function
16620 call. Return the substituted version of T. */
16622 static tree
16623 tsubst_non_call_postfix_expression (tree t, tree args,
16624 tsubst_flags_t complain,
16625 tree in_decl)
16627 if (TREE_CODE (t) == SCOPE_REF)
16628 t = tsubst_qualified_id (t, args, complain, in_decl,
16629 /*done=*/false, /*address_p=*/false);
16630 else
16631 t = tsubst_copy_and_build (t, args, complain, in_decl,
16632 /*function_p=*/false,
16633 /*integral_constant_expression_p=*/false);
16635 return t;
16638 /* Like tsubst but deals with expressions and performs semantic
16639 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16641 tree
16642 tsubst_copy_and_build (tree t,
16643 tree args,
16644 tsubst_flags_t complain,
16645 tree in_decl,
16646 bool function_p,
16647 bool integral_constant_expression_p)
16649 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16650 #define RECUR(NODE) \
16651 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16652 /*function_p=*/false, \
16653 integral_constant_expression_p)
16655 tree retval, op1;
16656 location_t loc;
16658 if (t == NULL_TREE || t == error_mark_node)
16659 return t;
16661 loc = input_location;
16662 if (EXPR_HAS_LOCATION (t))
16663 input_location = EXPR_LOCATION (t);
16665 /* N3276 decltype magic only applies to calls at the top level or on the
16666 right side of a comma. */
16667 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16668 complain &= ~tf_decltype;
16670 switch (TREE_CODE (t))
16672 case USING_DECL:
16673 t = DECL_NAME (t);
16674 /* Fall through. */
16675 case IDENTIFIER_NODE:
16677 tree decl;
16678 cp_id_kind idk;
16679 bool non_integral_constant_expression_p;
16680 const char *error_msg;
16682 if (IDENTIFIER_CONV_OP_P (t))
16684 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16685 t = make_conv_op_name (new_type);
16688 /* Look up the name. */
16689 decl = lookup_name (t);
16691 /* By convention, expressions use ERROR_MARK_NODE to indicate
16692 failure, not NULL_TREE. */
16693 if (decl == NULL_TREE)
16694 decl = error_mark_node;
16696 decl = finish_id_expression (t, decl, NULL_TREE,
16697 &idk,
16698 integral_constant_expression_p,
16699 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16700 &non_integral_constant_expression_p,
16701 /*template_p=*/false,
16702 /*done=*/true,
16703 /*address_p=*/false,
16704 /*template_arg_p=*/false,
16705 &error_msg,
16706 input_location);
16707 if (error_msg)
16708 error (error_msg);
16709 if (!function_p && identifier_p (decl))
16711 if (complain & tf_error)
16712 unqualified_name_lookup_error (decl);
16713 decl = error_mark_node;
16715 RETURN (decl);
16718 case TEMPLATE_ID_EXPR:
16720 tree object;
16721 tree templ = RECUR (TREE_OPERAND (t, 0));
16722 tree targs = TREE_OPERAND (t, 1);
16724 if (targs)
16725 targs = tsubst_template_args (targs, args, complain, in_decl);
16726 if (targs == error_mark_node)
16727 return error_mark_node;
16729 if (TREE_CODE (templ) == SCOPE_REF)
16731 tree name = TREE_OPERAND (templ, 1);
16732 tree tid = lookup_template_function (name, targs);
16733 TREE_OPERAND (templ, 1) = tid;
16734 return templ;
16737 if (variable_template_p (templ))
16738 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16740 if (TREE_CODE (templ) == COMPONENT_REF)
16742 object = TREE_OPERAND (templ, 0);
16743 templ = TREE_OPERAND (templ, 1);
16745 else
16746 object = NULL_TREE;
16747 templ = lookup_template_function (templ, targs);
16749 if (object)
16750 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16751 object, templ, NULL_TREE));
16752 else
16753 RETURN (baselink_for_fns (templ));
16756 case INDIRECT_REF:
16758 tree r = RECUR (TREE_OPERAND (t, 0));
16760 if (REFERENCE_REF_P (t))
16762 /* A type conversion to reference type will be enclosed in
16763 such an indirect ref, but the substitution of the cast
16764 will have also added such an indirect ref. */
16765 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16766 r = convert_from_reference (r);
16768 else
16769 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16770 complain|decltype_flag);
16772 if (TREE_CODE (r) == INDIRECT_REF)
16773 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16775 RETURN (r);
16778 case NOP_EXPR:
16780 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16781 tree op0 = RECUR (TREE_OPERAND (t, 0));
16782 RETURN (build_nop (type, op0));
16785 case IMPLICIT_CONV_EXPR:
16787 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16788 tree expr = RECUR (TREE_OPERAND (t, 0));
16789 int flags = LOOKUP_IMPLICIT;
16790 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16791 flags = LOOKUP_NORMAL;
16792 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16793 flags));
16796 case CONVERT_EXPR:
16798 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16799 tree op0 = RECUR (TREE_OPERAND (t, 0));
16800 RETURN (build1 (CONVERT_EXPR, type, op0));
16803 case CAST_EXPR:
16804 case REINTERPRET_CAST_EXPR:
16805 case CONST_CAST_EXPR:
16806 case DYNAMIC_CAST_EXPR:
16807 case STATIC_CAST_EXPR:
16809 tree type;
16810 tree op, r = NULL_TREE;
16812 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16813 if (integral_constant_expression_p
16814 && !cast_valid_in_integral_constant_expression_p (type))
16816 if (complain & tf_error)
16817 error ("a cast to a type other than an integral or "
16818 "enumeration type cannot appear in a constant-expression");
16819 RETURN (error_mark_node);
16822 op = RECUR (TREE_OPERAND (t, 0));
16824 warning_sentinel s(warn_useless_cast);
16825 switch (TREE_CODE (t))
16827 case CAST_EXPR:
16828 r = build_functional_cast (type, op, complain);
16829 break;
16830 case REINTERPRET_CAST_EXPR:
16831 r = build_reinterpret_cast (type, op, complain);
16832 break;
16833 case CONST_CAST_EXPR:
16834 r = build_const_cast (type, op, complain);
16835 break;
16836 case DYNAMIC_CAST_EXPR:
16837 r = build_dynamic_cast (type, op, complain);
16838 break;
16839 case STATIC_CAST_EXPR:
16840 r = build_static_cast (type, op, complain);
16841 break;
16842 default:
16843 gcc_unreachable ();
16846 RETURN (r);
16849 case POSTDECREMENT_EXPR:
16850 case POSTINCREMENT_EXPR:
16851 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16852 args, complain, in_decl);
16853 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16854 complain|decltype_flag));
16856 case PREDECREMENT_EXPR:
16857 case PREINCREMENT_EXPR:
16858 case NEGATE_EXPR:
16859 case BIT_NOT_EXPR:
16860 case ABS_EXPR:
16861 case TRUTH_NOT_EXPR:
16862 case UNARY_PLUS_EXPR: /* Unary + */
16863 case REALPART_EXPR:
16864 case IMAGPART_EXPR:
16865 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16866 RECUR (TREE_OPERAND (t, 0)),
16867 complain|decltype_flag));
16869 case FIX_TRUNC_EXPR:
16870 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16871 false, complain));
16873 case ADDR_EXPR:
16874 op1 = TREE_OPERAND (t, 0);
16875 if (TREE_CODE (op1) == LABEL_DECL)
16876 RETURN (finish_label_address_expr (DECL_NAME (op1),
16877 EXPR_LOCATION (op1)));
16878 if (TREE_CODE (op1) == SCOPE_REF)
16879 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16880 /*done=*/true, /*address_p=*/true);
16881 else
16882 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16883 in_decl);
16884 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16885 complain|decltype_flag));
16887 case PLUS_EXPR:
16888 case MINUS_EXPR:
16889 case MULT_EXPR:
16890 case TRUNC_DIV_EXPR:
16891 case CEIL_DIV_EXPR:
16892 case FLOOR_DIV_EXPR:
16893 case ROUND_DIV_EXPR:
16894 case EXACT_DIV_EXPR:
16895 case BIT_AND_EXPR:
16896 case BIT_IOR_EXPR:
16897 case BIT_XOR_EXPR:
16898 case TRUNC_MOD_EXPR:
16899 case FLOOR_MOD_EXPR:
16900 case TRUTH_ANDIF_EXPR:
16901 case TRUTH_ORIF_EXPR:
16902 case TRUTH_AND_EXPR:
16903 case TRUTH_OR_EXPR:
16904 case RSHIFT_EXPR:
16905 case LSHIFT_EXPR:
16906 case RROTATE_EXPR:
16907 case LROTATE_EXPR:
16908 case EQ_EXPR:
16909 case NE_EXPR:
16910 case MAX_EXPR:
16911 case MIN_EXPR:
16912 case LE_EXPR:
16913 case GE_EXPR:
16914 case LT_EXPR:
16915 case GT_EXPR:
16916 case MEMBER_REF:
16917 case DOTSTAR_EXPR:
16919 warning_sentinel s1(warn_type_limits);
16920 warning_sentinel s2(warn_div_by_zero);
16921 warning_sentinel s3(warn_logical_op);
16922 warning_sentinel s4(warn_tautological_compare);
16923 tree op0 = RECUR (TREE_OPERAND (t, 0));
16924 tree op1 = RECUR (TREE_OPERAND (t, 1));
16925 tree r = build_x_binary_op
16926 (input_location, TREE_CODE (t),
16927 op0,
16928 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16929 ? ERROR_MARK
16930 : TREE_CODE (TREE_OPERAND (t, 0))),
16931 op1,
16932 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16933 ? ERROR_MARK
16934 : TREE_CODE (TREE_OPERAND (t, 1))),
16935 /*overload=*/NULL,
16936 complain|decltype_flag);
16937 if (EXPR_P (r) && TREE_NO_WARNING (t))
16938 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16940 RETURN (r);
16943 case POINTER_PLUS_EXPR:
16945 tree op0 = RECUR (TREE_OPERAND (t, 0));
16946 tree op1 = RECUR (TREE_OPERAND (t, 1));
16947 return fold_build_pointer_plus (op0, op1);
16950 case SCOPE_REF:
16951 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16952 /*address_p=*/false));
16953 case ARRAY_REF:
16954 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16955 args, complain, in_decl);
16956 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16957 RECUR (TREE_OPERAND (t, 1)),
16958 complain|decltype_flag));
16960 case ARRAY_NOTATION_REF:
16962 tree start_index, length, stride;
16963 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16964 args, complain, in_decl);
16965 start_index = RECUR (ARRAY_NOTATION_START (t));
16966 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16967 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16968 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16969 length, stride, TREE_TYPE (op1)));
16971 case SIZEOF_EXPR:
16972 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16973 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16974 RETURN (tsubst_copy (t, args, complain, in_decl));
16975 /* Fall through */
16977 case ALIGNOF_EXPR:
16979 tree r;
16981 op1 = TREE_OPERAND (t, 0);
16982 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16983 op1 = TREE_TYPE (op1);
16984 if (!args)
16986 /* When there are no ARGS, we are trying to evaluate a
16987 non-dependent expression from the parser. Trying to do
16988 the substitutions may not work. */
16989 if (!TYPE_P (op1))
16990 op1 = TREE_TYPE (op1);
16992 else
16994 ++cp_unevaluated_operand;
16995 ++c_inhibit_evaluation_warnings;
16996 if (TYPE_P (op1))
16997 op1 = tsubst (op1, args, complain, in_decl);
16998 else
16999 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17000 /*function_p=*/false,
17001 /*integral_constant_expression_p=*/
17002 false);
17003 --cp_unevaluated_operand;
17004 --c_inhibit_evaluation_warnings;
17006 if (TYPE_P (op1))
17007 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17008 complain & tf_error);
17009 else
17010 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17011 complain & tf_error);
17012 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17014 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17016 if (!processing_template_decl && TYPE_P (op1))
17018 r = build_min (SIZEOF_EXPR, size_type_node,
17019 build1 (NOP_EXPR, op1, error_mark_node));
17020 SIZEOF_EXPR_TYPE_P (r) = 1;
17022 else
17023 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17024 TREE_SIDE_EFFECTS (r) = 0;
17025 TREE_READONLY (r) = 1;
17027 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17029 RETURN (r);
17032 case AT_ENCODE_EXPR:
17034 op1 = TREE_OPERAND (t, 0);
17035 ++cp_unevaluated_operand;
17036 ++c_inhibit_evaluation_warnings;
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 RETURN (objc_build_encode_expr (op1));
17045 case NOEXCEPT_EXPR:
17046 op1 = TREE_OPERAND (t, 0);
17047 ++cp_unevaluated_operand;
17048 ++c_inhibit_evaluation_warnings;
17049 ++cp_noexcept_operand;
17050 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17051 /*function_p=*/false,
17052 /*integral_constant_expression_p=*/false);
17053 --cp_unevaluated_operand;
17054 --c_inhibit_evaluation_warnings;
17055 --cp_noexcept_operand;
17056 RETURN (finish_noexcept_expr (op1, complain));
17058 case MODOP_EXPR:
17060 warning_sentinel s(warn_div_by_zero);
17061 tree lhs = RECUR (TREE_OPERAND (t, 0));
17062 tree rhs = RECUR (TREE_OPERAND (t, 2));
17063 tree r = build_x_modify_expr
17064 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17065 complain|decltype_flag);
17066 /* TREE_NO_WARNING must be set if either the expression was
17067 parenthesized or it uses an operator such as >>= rather
17068 than plain assignment. In the former case, it was already
17069 set and must be copied. In the latter case,
17070 build_x_modify_expr sets it and it must not be reset
17071 here. */
17072 if (TREE_NO_WARNING (t))
17073 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17075 RETURN (r);
17078 case ARROW_EXPR:
17079 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17080 args, complain, in_decl);
17081 /* Remember that there was a reference to this entity. */
17082 if (DECL_P (op1)
17083 && !mark_used (op1, complain) && !(complain & tf_error))
17084 RETURN (error_mark_node);
17085 RETURN (build_x_arrow (input_location, op1, complain));
17087 case NEW_EXPR:
17089 tree placement = RECUR (TREE_OPERAND (t, 0));
17090 tree init = RECUR (TREE_OPERAND (t, 3));
17091 vec<tree, va_gc> *placement_vec;
17092 vec<tree, va_gc> *init_vec;
17093 tree ret;
17095 if (placement == NULL_TREE)
17096 placement_vec = NULL;
17097 else
17099 placement_vec = make_tree_vector ();
17100 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17101 vec_safe_push (placement_vec, TREE_VALUE (placement));
17104 /* If there was an initializer in the original tree, but it
17105 instantiated to an empty list, then we should pass a
17106 non-NULL empty vector to tell build_new that it was an
17107 empty initializer() rather than no initializer. This can
17108 only happen when the initializer is a pack expansion whose
17109 parameter packs are of length zero. */
17110 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17111 init_vec = NULL;
17112 else
17114 init_vec = make_tree_vector ();
17115 if (init == void_node)
17116 gcc_assert (init_vec != NULL);
17117 else
17119 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17120 vec_safe_push (init_vec, TREE_VALUE (init));
17124 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17125 tree op2 = RECUR (TREE_OPERAND (t, 2));
17126 ret = build_new (&placement_vec, op1, op2, &init_vec,
17127 NEW_EXPR_USE_GLOBAL (t),
17128 complain);
17130 if (placement_vec != NULL)
17131 release_tree_vector (placement_vec);
17132 if (init_vec != NULL)
17133 release_tree_vector (init_vec);
17135 RETURN (ret);
17138 case DELETE_EXPR:
17140 tree op0 = RECUR (TREE_OPERAND (t, 0));
17141 tree op1 = RECUR (TREE_OPERAND (t, 1));
17142 RETURN (delete_sanity (op0, op1,
17143 DELETE_EXPR_USE_VEC (t),
17144 DELETE_EXPR_USE_GLOBAL (t),
17145 complain));
17148 case COMPOUND_EXPR:
17150 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17151 complain & ~tf_decltype, in_decl,
17152 /*function_p=*/false,
17153 integral_constant_expression_p);
17154 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17155 op0,
17156 RECUR (TREE_OPERAND (t, 1)),
17157 complain|decltype_flag));
17160 case CALL_EXPR:
17162 tree function;
17163 vec<tree, va_gc> *call_args;
17164 unsigned int nargs, i;
17165 bool qualified_p;
17166 bool koenig_p;
17167 tree ret;
17169 function = CALL_EXPR_FN (t);
17170 /* Internal function with no arguments. */
17171 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17172 RETURN (t);
17174 /* When we parsed the expression, we determined whether or
17175 not Koenig lookup should be performed. */
17176 koenig_p = KOENIG_LOOKUP_P (t);
17177 if (function == NULL_TREE)
17179 koenig_p = false;
17180 qualified_p = false;
17182 else if (TREE_CODE (function) == SCOPE_REF)
17184 qualified_p = true;
17185 function = tsubst_qualified_id (function, args, complain, in_decl,
17186 /*done=*/false,
17187 /*address_p=*/false);
17189 else if (koenig_p && identifier_p (function))
17191 /* Do nothing; calling tsubst_copy_and_build on an identifier
17192 would incorrectly perform unqualified lookup again.
17194 Note that we can also have an IDENTIFIER_NODE if the earlier
17195 unqualified lookup found a member function; in that case
17196 koenig_p will be false and we do want to do the lookup
17197 again to find the instantiated member function.
17199 FIXME but doing that causes c++/15272, so we need to stop
17200 using IDENTIFIER_NODE in that situation. */
17201 qualified_p = false;
17203 else
17205 if (TREE_CODE (function) == COMPONENT_REF)
17207 tree op = TREE_OPERAND (function, 1);
17209 qualified_p = (TREE_CODE (op) == SCOPE_REF
17210 || (BASELINK_P (op)
17211 && BASELINK_QUALIFIED_P (op)));
17213 else
17214 qualified_p = false;
17216 if (TREE_CODE (function) == ADDR_EXPR
17217 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17218 /* Avoid error about taking the address of a constructor. */
17219 function = TREE_OPERAND (function, 0);
17221 function = tsubst_copy_and_build (function, args, complain,
17222 in_decl,
17223 !qualified_p,
17224 integral_constant_expression_p);
17226 if (BASELINK_P (function))
17227 qualified_p = true;
17230 nargs = call_expr_nargs (t);
17231 call_args = make_tree_vector ();
17232 for (i = 0; i < nargs; ++i)
17234 tree arg = CALL_EXPR_ARG (t, i);
17236 if (!PACK_EXPANSION_P (arg))
17237 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17238 else
17240 /* Expand the pack expansion and push each entry onto
17241 CALL_ARGS. */
17242 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17243 if (TREE_CODE (arg) == TREE_VEC)
17245 unsigned int len, j;
17247 len = TREE_VEC_LENGTH (arg);
17248 for (j = 0; j < len; ++j)
17250 tree value = TREE_VEC_ELT (arg, j);
17251 if (value != NULL_TREE)
17252 value = convert_from_reference (value);
17253 vec_safe_push (call_args, value);
17256 else
17258 /* A partial substitution. Add one entry. */
17259 vec_safe_push (call_args, arg);
17264 /* We do not perform argument-dependent lookup if normal
17265 lookup finds a non-function, in accordance with the
17266 expected resolution of DR 218. */
17267 if (koenig_p
17268 && ((is_overloaded_fn (function)
17269 /* If lookup found a member function, the Koenig lookup is
17270 not appropriate, even if an unqualified-name was used
17271 to denote the function. */
17272 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17273 || identifier_p (function))
17274 /* Only do this when substitution turns a dependent call
17275 into a non-dependent call. */
17276 && type_dependent_expression_p_push (t)
17277 && !any_type_dependent_arguments_p (call_args))
17278 function = perform_koenig_lookup (function, call_args, tf_none);
17280 if (function != NULL_TREE
17281 && identifier_p (function)
17282 && !any_type_dependent_arguments_p (call_args))
17284 if (koenig_p && (complain & tf_warning_or_error))
17286 /* For backwards compatibility and good diagnostics, try
17287 the unqualified lookup again if we aren't in SFINAE
17288 context. */
17289 tree unq = (tsubst_copy_and_build
17290 (function, args, complain, in_decl, true,
17291 integral_constant_expression_p));
17292 if (unq == error_mark_node)
17294 release_tree_vector (call_args);
17295 RETURN (error_mark_node);
17298 if (unq != function)
17300 /* In a lambda fn, we have to be careful to not
17301 introduce new this captures. Legacy code can't
17302 be using lambdas anyway, so it's ok to be
17303 stricter. */
17304 bool in_lambda = (current_class_type
17305 && LAMBDA_TYPE_P (current_class_type));
17306 char const *const msg
17307 = G_("%qD was not declared in this scope, "
17308 "and no declarations were found by "
17309 "argument-dependent lookup at the point "
17310 "of instantiation");
17312 bool diag = true;
17313 if (in_lambda)
17314 error_at (EXPR_LOC_OR_LOC (t, input_location),
17315 msg, function);
17316 else
17317 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17318 msg, function);
17319 if (diag)
17321 tree fn = unq;
17323 if (INDIRECT_REF_P (fn))
17324 fn = TREE_OPERAND (fn, 0);
17325 if (is_overloaded_fn (fn))
17326 fn = get_first_fn (fn);
17328 if (!DECL_P (fn))
17329 /* Can't say anything more. */;
17330 else if (DECL_CLASS_SCOPE_P (fn))
17332 location_t loc = EXPR_LOC_OR_LOC (t,
17333 input_location);
17334 inform (loc,
17335 "declarations in dependent base %qT are "
17336 "not found by unqualified lookup",
17337 DECL_CLASS_CONTEXT (fn));
17338 if (current_class_ptr)
17339 inform (loc,
17340 "use %<this->%D%> instead", function);
17341 else
17342 inform (loc,
17343 "use %<%T::%D%> instead",
17344 current_class_name, function);
17346 else
17347 inform (DECL_SOURCE_LOCATION (fn),
17348 "%qD declared here, later in the "
17349 "translation unit", fn);
17350 if (in_lambda)
17352 release_tree_vector (call_args);
17353 RETURN (error_mark_node);
17357 function = unq;
17360 if (identifier_p (function))
17362 if (complain & tf_error)
17363 unqualified_name_lookup_error (function);
17364 release_tree_vector (call_args);
17365 RETURN (error_mark_node);
17369 /* Remember that there was a reference to this entity. */
17370 if (function != NULL_TREE
17371 && DECL_P (function)
17372 && !mark_used (function, complain) && !(complain & tf_error))
17374 release_tree_vector (call_args);
17375 RETURN (error_mark_node);
17378 /* Put back tf_decltype for the actual call. */
17379 complain |= decltype_flag;
17381 if (function == NULL_TREE)
17382 switch (CALL_EXPR_IFN (t))
17384 case IFN_LAUNDER:
17385 gcc_assert (nargs == 1);
17386 if (vec_safe_length (call_args) != 1)
17388 error_at (EXPR_LOC_OR_LOC (t, input_location),
17389 "wrong number of arguments to "
17390 "%<__builtin_launder%>");
17391 ret = error_mark_node;
17393 else
17394 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17395 input_location),
17396 (*call_args)[0], complain);
17397 break;
17399 default:
17400 /* Unsupported internal function with arguments. */
17401 gcc_unreachable ();
17403 else if (TREE_CODE (function) == OFFSET_REF)
17404 ret = build_offset_ref_call_from_tree (function, &call_args,
17405 complain);
17406 else if (TREE_CODE (function) == COMPONENT_REF)
17408 tree instance = TREE_OPERAND (function, 0);
17409 tree fn = TREE_OPERAND (function, 1);
17411 if (processing_template_decl
17412 && (type_dependent_expression_p (instance)
17413 || (!BASELINK_P (fn)
17414 && TREE_CODE (fn) != FIELD_DECL)
17415 || type_dependent_expression_p (fn)
17416 || any_type_dependent_arguments_p (call_args)))
17417 ret = build_min_nt_call_vec (function, call_args);
17418 else if (!BASELINK_P (fn))
17419 ret = finish_call_expr (function, &call_args,
17420 /*disallow_virtual=*/false,
17421 /*koenig_p=*/false,
17422 complain);
17423 else
17424 ret = (build_new_method_call
17425 (instance, fn,
17426 &call_args, NULL_TREE,
17427 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17428 /*fn_p=*/NULL,
17429 complain));
17431 else
17432 ret = finish_call_expr (function, &call_args,
17433 /*disallow_virtual=*/qualified_p,
17434 koenig_p,
17435 complain);
17437 release_tree_vector (call_args);
17439 if (ret != error_mark_node)
17441 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17442 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17443 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17444 bool thk = CALL_FROM_THUNK_P (t);
17445 if (op || ord || rev || thk)
17447 function = extract_call_expr (ret);
17448 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17449 CALL_EXPR_ORDERED_ARGS (function) = ord;
17450 CALL_EXPR_REVERSE_ARGS (function) = rev;
17451 if (thk)
17453 CALL_FROM_THUNK_P (function) = true;
17454 /* The thunk location is not interesting. */
17455 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17460 RETURN (ret);
17463 case COND_EXPR:
17465 tree cond = RECUR (TREE_OPERAND (t, 0));
17466 tree folded_cond = fold_non_dependent_expr (cond);
17467 tree exp1, exp2;
17469 if (TREE_CODE (folded_cond) == INTEGER_CST)
17471 if (integer_zerop (folded_cond))
17473 ++c_inhibit_evaluation_warnings;
17474 exp1 = RECUR (TREE_OPERAND (t, 1));
17475 --c_inhibit_evaluation_warnings;
17476 exp2 = RECUR (TREE_OPERAND (t, 2));
17478 else
17480 exp1 = RECUR (TREE_OPERAND (t, 1));
17481 ++c_inhibit_evaluation_warnings;
17482 exp2 = RECUR (TREE_OPERAND (t, 2));
17483 --c_inhibit_evaluation_warnings;
17485 cond = folded_cond;
17487 else
17489 exp1 = RECUR (TREE_OPERAND (t, 1));
17490 exp2 = RECUR (TREE_OPERAND (t, 2));
17493 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17494 cond, exp1, exp2, complain));
17497 case PSEUDO_DTOR_EXPR:
17499 tree op0 = RECUR (TREE_OPERAND (t, 0));
17500 tree op1 = RECUR (TREE_OPERAND (t, 1));
17501 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17502 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17503 input_location));
17506 case TREE_LIST:
17508 tree purpose, value, chain;
17510 if (t == void_list_node)
17511 RETURN (t);
17513 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17514 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17516 /* We have pack expansions, so expand those and
17517 create a new list out of it. */
17518 tree purposevec = NULL_TREE;
17519 tree valuevec = NULL_TREE;
17520 tree chain;
17521 int i, len = -1;
17523 /* Expand the argument expressions. */
17524 if (TREE_PURPOSE (t))
17525 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17526 complain, in_decl);
17527 if (TREE_VALUE (t))
17528 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17529 complain, in_decl);
17531 /* Build the rest of the list. */
17532 chain = TREE_CHAIN (t);
17533 if (chain && chain != void_type_node)
17534 chain = RECUR (chain);
17536 /* Determine the number of arguments. */
17537 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17539 len = TREE_VEC_LENGTH (purposevec);
17540 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17542 else if (TREE_CODE (valuevec) == TREE_VEC)
17543 len = TREE_VEC_LENGTH (valuevec);
17544 else
17546 /* Since we only performed a partial substitution into
17547 the argument pack, we only RETURN (a single list
17548 node. */
17549 if (purposevec == TREE_PURPOSE (t)
17550 && valuevec == TREE_VALUE (t)
17551 && chain == TREE_CHAIN (t))
17552 RETURN (t);
17554 RETURN (tree_cons (purposevec, valuevec, chain));
17557 /* Convert the argument vectors into a TREE_LIST */
17558 i = len;
17559 while (i > 0)
17561 /* Grab the Ith values. */
17562 i--;
17563 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17564 : NULL_TREE;
17565 value
17566 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17567 : NULL_TREE;
17569 /* Build the list (backwards). */
17570 chain = tree_cons (purpose, value, chain);
17573 RETURN (chain);
17576 purpose = TREE_PURPOSE (t);
17577 if (purpose)
17578 purpose = RECUR (purpose);
17579 value = TREE_VALUE (t);
17580 if (value)
17581 value = RECUR (value);
17582 chain = TREE_CHAIN (t);
17583 if (chain && chain != void_type_node)
17584 chain = RECUR (chain);
17585 if (purpose == TREE_PURPOSE (t)
17586 && value == TREE_VALUE (t)
17587 && chain == TREE_CHAIN (t))
17588 RETURN (t);
17589 RETURN (tree_cons (purpose, value, chain));
17592 case COMPONENT_REF:
17594 tree object;
17595 tree object_type;
17596 tree member;
17597 tree r;
17599 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17600 args, complain, in_decl);
17601 /* Remember that there was a reference to this entity. */
17602 if (DECL_P (object)
17603 && !mark_used (object, complain) && !(complain & tf_error))
17604 RETURN (error_mark_node);
17605 object_type = TREE_TYPE (object);
17607 member = TREE_OPERAND (t, 1);
17608 if (BASELINK_P (member))
17609 member = tsubst_baselink (member,
17610 non_reference (TREE_TYPE (object)),
17611 args, complain, in_decl);
17612 else
17613 member = tsubst_copy (member, args, complain, in_decl);
17614 if (member == error_mark_node)
17615 RETURN (error_mark_node);
17617 if (TREE_CODE (member) == FIELD_DECL)
17619 r = finish_non_static_data_member (member, object, NULL_TREE);
17620 if (TREE_CODE (r) == COMPONENT_REF)
17621 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17622 RETURN (r);
17624 else if (type_dependent_expression_p (object))
17625 /* We can't do much here. */;
17626 else if (!CLASS_TYPE_P (object_type))
17628 if (scalarish_type_p (object_type))
17630 tree s = NULL_TREE;
17631 tree dtor = member;
17633 if (TREE_CODE (dtor) == SCOPE_REF)
17635 s = TREE_OPERAND (dtor, 0);
17636 dtor = TREE_OPERAND (dtor, 1);
17638 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17640 dtor = TREE_OPERAND (dtor, 0);
17641 if (TYPE_P (dtor))
17642 RETURN (finish_pseudo_destructor_expr
17643 (object, s, dtor, input_location));
17647 else if (TREE_CODE (member) == SCOPE_REF
17648 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17650 /* Lookup the template functions now that we know what the
17651 scope is. */
17652 tree scope = TREE_OPERAND (member, 0);
17653 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17654 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17655 member = lookup_qualified_name (scope, tmpl,
17656 /*is_type_p=*/false,
17657 /*complain=*/false);
17658 if (BASELINK_P (member))
17660 BASELINK_FUNCTIONS (member)
17661 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17662 args);
17663 member = (adjust_result_of_qualified_name_lookup
17664 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17665 object_type));
17667 else
17669 qualified_name_lookup_error (scope, tmpl, member,
17670 input_location);
17671 RETURN (error_mark_node);
17674 else if (TREE_CODE (member) == SCOPE_REF
17675 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17676 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17678 if (complain & tf_error)
17680 if (TYPE_P (TREE_OPERAND (member, 0)))
17681 error ("%qT is not a class or namespace",
17682 TREE_OPERAND (member, 0));
17683 else
17684 error ("%qD is not a class or namespace",
17685 TREE_OPERAND (member, 0));
17687 RETURN (error_mark_node);
17690 r = finish_class_member_access_expr (object, member,
17691 /*template_p=*/false,
17692 complain);
17693 if (TREE_CODE (r) == COMPONENT_REF)
17694 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17695 RETURN (r);
17698 case THROW_EXPR:
17699 RETURN (build_throw
17700 (RECUR (TREE_OPERAND (t, 0))));
17702 case CONSTRUCTOR:
17704 vec<constructor_elt, va_gc> *n;
17705 constructor_elt *ce;
17706 unsigned HOST_WIDE_INT idx;
17707 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17708 bool process_index_p;
17709 int newlen;
17710 bool need_copy_p = false;
17711 tree r;
17713 if (type == error_mark_node)
17714 RETURN (error_mark_node);
17716 /* digest_init will do the wrong thing if we let it. */
17717 if (type && TYPE_PTRMEMFUNC_P (type))
17718 RETURN (t);
17720 /* We do not want to process the index of aggregate
17721 initializers as they are identifier nodes which will be
17722 looked up by digest_init. */
17723 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17725 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17726 newlen = vec_safe_length (n);
17727 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17729 if (ce->index && process_index_p
17730 /* An identifier index is looked up in the type
17731 being initialized, not the current scope. */
17732 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17733 ce->index = RECUR (ce->index);
17735 if (PACK_EXPANSION_P (ce->value))
17737 /* Substitute into the pack expansion. */
17738 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17739 in_decl);
17741 if (ce->value == error_mark_node
17742 || PACK_EXPANSION_P (ce->value))
17744 else if (TREE_VEC_LENGTH (ce->value) == 1)
17745 /* Just move the argument into place. */
17746 ce->value = TREE_VEC_ELT (ce->value, 0);
17747 else
17749 /* Update the length of the final CONSTRUCTOR
17750 arguments vector, and note that we will need to
17751 copy.*/
17752 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17753 need_copy_p = true;
17756 else
17757 ce->value = RECUR (ce->value);
17760 if (need_copy_p)
17762 vec<constructor_elt, va_gc> *old_n = n;
17764 vec_alloc (n, newlen);
17765 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17767 if (TREE_CODE (ce->value) == TREE_VEC)
17769 int i, len = TREE_VEC_LENGTH (ce->value);
17770 for (i = 0; i < len; ++i)
17771 CONSTRUCTOR_APPEND_ELT (n, 0,
17772 TREE_VEC_ELT (ce->value, i));
17774 else
17775 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17779 r = build_constructor (init_list_type_node, n);
17780 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17782 if (TREE_HAS_CONSTRUCTOR (t))
17784 fcl_t cl = fcl_functional;
17785 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17786 cl = fcl_c99;
17787 RETURN (finish_compound_literal (type, r, complain, cl));
17790 TREE_TYPE (r) = type;
17791 RETURN (r);
17794 case TYPEID_EXPR:
17796 tree operand_0 = TREE_OPERAND (t, 0);
17797 if (TYPE_P (operand_0))
17799 operand_0 = tsubst (operand_0, args, complain, in_decl);
17800 RETURN (get_typeid (operand_0, complain));
17802 else
17804 operand_0 = RECUR (operand_0);
17805 RETURN (build_typeid (operand_0, complain));
17809 case VAR_DECL:
17810 if (!args)
17811 RETURN (t);
17812 else if (DECL_PACK_P (t))
17814 /* We don't build decls for an instantiation of a
17815 variadic capture proxy, we instantiate the elements
17816 when needed. */
17817 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17818 return RECUR (DECL_VALUE_EXPR (t));
17820 /* Fall through */
17822 case PARM_DECL:
17824 tree r = tsubst_copy (t, args, complain, in_decl);
17825 /* ??? We're doing a subset of finish_id_expression here. */
17826 if (VAR_P (r)
17827 && !processing_template_decl
17828 && !cp_unevaluated_operand
17829 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17830 && CP_DECL_THREAD_LOCAL_P (r))
17832 if (tree wrap = get_tls_wrapper_fn (r))
17833 /* Replace an evaluated use of the thread_local variable with
17834 a call to its wrapper. */
17835 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17837 else if (outer_automatic_var_p (r))
17839 r = process_outer_var_ref (r, complain);
17840 if (is_capture_proxy (r))
17841 register_local_specialization (r, t);
17844 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17845 /* If the original type was a reference, we'll be wrapped in
17846 the appropriate INDIRECT_REF. */
17847 r = convert_from_reference (r);
17848 RETURN (r);
17851 case VA_ARG_EXPR:
17853 tree op0 = RECUR (TREE_OPERAND (t, 0));
17854 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17855 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17858 case OFFSETOF_EXPR:
17860 tree object_ptr
17861 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17862 in_decl, /*function_p=*/false,
17863 /*integral_constant_expression_p=*/false);
17864 RETURN (finish_offsetof (object_ptr,
17865 RECUR (TREE_OPERAND (t, 0)),
17866 EXPR_LOCATION (t)));
17869 case ADDRESSOF_EXPR:
17870 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17871 RECUR (TREE_OPERAND (t, 0)), complain));
17873 case TRAIT_EXPR:
17875 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17876 complain, in_decl);
17878 tree type2 = TRAIT_EXPR_TYPE2 (t);
17879 if (type2 && TREE_CODE (type2) == TREE_LIST)
17880 type2 = RECUR (type2);
17881 else if (type2)
17882 type2 = tsubst (type2, args, complain, in_decl);
17884 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17887 case STMT_EXPR:
17889 tree old_stmt_expr = cur_stmt_expr;
17890 tree stmt_expr = begin_stmt_expr ();
17892 cur_stmt_expr = stmt_expr;
17893 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17894 integral_constant_expression_p);
17895 stmt_expr = finish_stmt_expr (stmt_expr, false);
17896 cur_stmt_expr = old_stmt_expr;
17898 /* If the resulting list of expression statement is empty,
17899 fold it further into void_node. */
17900 if (empty_expr_stmt_p (stmt_expr))
17901 stmt_expr = void_node;
17903 RETURN (stmt_expr);
17906 case LAMBDA_EXPR:
17908 tree r = build_lambda_expr ();
17910 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17911 LAMBDA_EXPR_CLOSURE (r) = type;
17912 CLASSTYPE_LAMBDA_EXPR (type) = r;
17914 LAMBDA_EXPR_LOCATION (r)
17915 = LAMBDA_EXPR_LOCATION (t);
17916 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17917 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17918 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17919 LAMBDA_EXPR_DISCRIMINATOR (r)
17920 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17921 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17922 if (!scope)
17923 /* No substitution needed. */;
17924 else if (VAR_OR_FUNCTION_DECL_P (scope))
17925 /* For a function or variable scope, we want to use tsubst so that we
17926 don't complain about referring to an auto before deduction. */
17927 scope = tsubst (scope, args, complain, in_decl);
17928 else if (TREE_CODE (scope) == PARM_DECL)
17930 /* Look up the parameter we want directly, as tsubst_copy
17931 doesn't do what we need. */
17932 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17933 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17934 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17935 parm = DECL_CHAIN (parm);
17936 scope = parm;
17937 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17938 if (DECL_CONTEXT (scope) == NULL_TREE)
17939 DECL_CONTEXT (scope) = fn;
17941 else if (TREE_CODE (scope) == FIELD_DECL)
17942 /* For a field, use tsubst_copy so that we look up the existing field
17943 rather than build a new one. */
17944 scope = RECUR (scope);
17945 else
17946 gcc_unreachable ();
17947 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17949 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17950 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17952 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17953 determine_visibility (TYPE_NAME (type));
17954 /* Now that we know visibility, instantiate the type so we have a
17955 declaration of the op() for later calls to lambda_function. */
17956 complete_type (type);
17958 if (tree fn = lambda_function (type))
17959 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17961 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17963 insert_pending_capture_proxies ();
17965 RETURN (build_lambda_object (r));
17968 case TARGET_EXPR:
17969 /* We can get here for a constant initializer of non-dependent type.
17970 FIXME stop folding in cp_parser_initializer_clause. */
17972 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17973 complain);
17974 RETURN (r);
17977 case TRANSACTION_EXPR:
17978 RETURN (tsubst_expr(t, args, complain, in_decl,
17979 integral_constant_expression_p));
17981 case PAREN_EXPR:
17982 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17984 case VEC_PERM_EXPR:
17986 tree op0 = RECUR (TREE_OPERAND (t, 0));
17987 tree op1 = RECUR (TREE_OPERAND (t, 1));
17988 tree op2 = RECUR (TREE_OPERAND (t, 2));
17989 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17990 complain));
17993 case REQUIRES_EXPR:
17994 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17996 default:
17997 /* Handle Objective-C++ constructs, if appropriate. */
17999 tree subst
18000 = objcp_tsubst_copy_and_build (t, args, complain,
18001 in_decl, /*function_p=*/false);
18002 if (subst)
18003 RETURN (subst);
18005 RETURN (tsubst_copy (t, args, complain, in_decl));
18008 #undef RECUR
18009 #undef RETURN
18010 out:
18011 input_location = loc;
18012 return retval;
18015 /* Verify that the instantiated ARGS are valid. For type arguments,
18016 make sure that the type's linkage is ok. For non-type arguments,
18017 make sure they are constants if they are integral or enumerations.
18018 Emit an error under control of COMPLAIN, and return TRUE on error. */
18020 static bool
18021 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18023 if (dependent_template_arg_p (t))
18024 return false;
18025 if (ARGUMENT_PACK_P (t))
18027 tree vec = ARGUMENT_PACK_ARGS (t);
18028 int len = TREE_VEC_LENGTH (vec);
18029 bool result = false;
18030 int i;
18032 for (i = 0; i < len; ++i)
18033 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18034 result = true;
18035 return result;
18037 else if (TYPE_P (t))
18039 /* [basic.link]: A name with no linkage (notably, the name
18040 of a class or enumeration declared in a local scope)
18041 shall not be used to declare an entity with linkage.
18042 This implies that names with no linkage cannot be used as
18043 template arguments
18045 DR 757 relaxes this restriction for C++0x. */
18046 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18047 : no_linkage_check (t, /*relaxed_p=*/false));
18049 if (nt)
18051 /* DR 488 makes use of a type with no linkage cause
18052 type deduction to fail. */
18053 if (complain & tf_error)
18055 if (TYPE_UNNAMED_P (nt))
18056 error ("%qT is/uses unnamed type", t);
18057 else
18058 error ("template argument for %qD uses local type %qT",
18059 tmpl, t);
18061 return true;
18063 /* In order to avoid all sorts of complications, we do not
18064 allow variably-modified types as template arguments. */
18065 else if (variably_modified_type_p (t, NULL_TREE))
18067 if (complain & tf_error)
18068 error ("%qT is a variably modified type", t);
18069 return true;
18072 /* Class template and alias template arguments should be OK. */
18073 else if (DECL_TYPE_TEMPLATE_P (t))
18075 /* A non-type argument of integral or enumerated type must be a
18076 constant. */
18077 else if (TREE_TYPE (t)
18078 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18079 && !REFERENCE_REF_P (t)
18080 && !TREE_CONSTANT (t))
18082 if (complain & tf_error)
18083 error ("integral expression %qE is not constant", t);
18084 return true;
18086 return false;
18089 static bool
18090 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18092 int ix, len = DECL_NTPARMS (tmpl);
18093 bool result = false;
18095 for (ix = 0; ix != len; ix++)
18097 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18098 result = true;
18100 if (result && (complain & tf_error))
18101 error (" trying to instantiate %qD", tmpl);
18102 return result;
18105 /* We're out of SFINAE context now, so generate diagnostics for the access
18106 errors we saw earlier when instantiating D from TMPL and ARGS. */
18108 static void
18109 recheck_decl_substitution (tree d, tree tmpl, tree args)
18111 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18112 tree type = TREE_TYPE (pattern);
18113 location_t loc = input_location;
18115 push_access_scope (d);
18116 push_deferring_access_checks (dk_no_deferred);
18117 input_location = DECL_SOURCE_LOCATION (pattern);
18118 tsubst (type, args, tf_warning_or_error, d);
18119 input_location = loc;
18120 pop_deferring_access_checks ();
18121 pop_access_scope (d);
18124 /* Instantiate the indicated variable, function, or alias template TMPL with
18125 the template arguments in TARG_PTR. */
18127 static tree
18128 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18130 tree targ_ptr = orig_args;
18131 tree fndecl;
18132 tree gen_tmpl;
18133 tree spec;
18134 bool access_ok = true;
18136 if (tmpl == error_mark_node)
18137 return error_mark_node;
18139 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18141 /* If this function is a clone, handle it specially. */
18142 if (DECL_CLONED_FUNCTION_P (tmpl))
18144 tree spec;
18145 tree clone;
18147 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18148 DECL_CLONED_FUNCTION. */
18149 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18150 targ_ptr, complain);
18151 if (spec == error_mark_node)
18152 return error_mark_node;
18154 /* Look for the clone. */
18155 FOR_EACH_CLONE (clone, spec)
18156 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18157 return clone;
18158 /* We should always have found the clone by now. */
18159 gcc_unreachable ();
18160 return NULL_TREE;
18163 if (targ_ptr == error_mark_node)
18164 return error_mark_node;
18166 /* Check to see if we already have this specialization. */
18167 gen_tmpl = most_general_template (tmpl);
18168 if (TMPL_ARGS_DEPTH (targ_ptr)
18169 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18170 /* targ_ptr only has the innermost template args, so add the outer ones
18171 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18172 the case of a non-dependent call within a template definition). */
18173 targ_ptr = (add_outermost_template_args
18174 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18175 targ_ptr));
18177 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18178 but it doesn't seem to be on the hot path. */
18179 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18181 gcc_assert (tmpl == gen_tmpl
18182 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18183 == spec)
18184 || fndecl == NULL_TREE);
18186 if (spec != NULL_TREE)
18188 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18190 if (complain & tf_error)
18191 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18192 return error_mark_node;
18194 return spec;
18197 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18198 complain))
18199 return error_mark_node;
18201 /* We are building a FUNCTION_DECL, during which the access of its
18202 parameters and return types have to be checked. However this
18203 FUNCTION_DECL which is the desired context for access checking
18204 is not built yet. We solve this chicken-and-egg problem by
18205 deferring all checks until we have the FUNCTION_DECL. */
18206 push_deferring_access_checks (dk_deferred);
18208 /* Instantiation of the function happens in the context of the function
18209 template, not the context of the overload resolution we're doing. */
18210 push_to_top_level ();
18211 /* If there are dependent arguments, e.g. because we're doing partial
18212 ordering, make sure processing_template_decl stays set. */
18213 if (uses_template_parms (targ_ptr))
18214 ++processing_template_decl;
18215 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18217 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18218 complain, gen_tmpl, true);
18219 push_nested_class (ctx);
18222 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18224 fndecl = NULL_TREE;
18225 if (VAR_P (pattern))
18227 /* We need to determine if we're using a partial or explicit
18228 specialization now, because the type of the variable could be
18229 different. */
18230 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18231 tree elt = most_specialized_partial_spec (tid, complain);
18232 if (elt == error_mark_node)
18233 pattern = error_mark_node;
18234 else if (elt)
18236 tree partial_tmpl = TREE_VALUE (elt);
18237 tree partial_args = TREE_PURPOSE (elt);
18238 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18239 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18243 /* Substitute template parameters to obtain the specialization. */
18244 if (fndecl == NULL_TREE)
18245 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18246 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18247 pop_nested_class ();
18248 pop_from_top_level ();
18250 if (fndecl == error_mark_node)
18252 pop_deferring_access_checks ();
18253 return error_mark_node;
18256 /* The DECL_TI_TEMPLATE should always be the immediate parent
18257 template, not the most general template. */
18258 DECL_TI_TEMPLATE (fndecl) = tmpl;
18259 DECL_TI_ARGS (fndecl) = targ_ptr;
18261 /* Now we know the specialization, compute access previously
18262 deferred. Do no access control for inheriting constructors,
18263 as we already checked access for the inherited constructor. */
18264 if (!(flag_new_inheriting_ctors
18265 && DECL_INHERITED_CTOR (fndecl)))
18267 push_access_scope (fndecl);
18268 if (!perform_deferred_access_checks (complain))
18269 access_ok = false;
18270 pop_access_scope (fndecl);
18272 pop_deferring_access_checks ();
18274 /* If we've just instantiated the main entry point for a function,
18275 instantiate all the alternate entry points as well. We do this
18276 by cloning the instantiation of the main entry point, not by
18277 instantiating the template clones. */
18278 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18279 clone_function_decl (fndecl, /*update_methods=*/false);
18281 if (!access_ok)
18283 if (!(complain & tf_error))
18285 /* Remember to reinstantiate when we're out of SFINAE so the user
18286 can see the errors. */
18287 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18289 return error_mark_node;
18291 return fndecl;
18294 /* Wrapper for instantiate_template_1. */
18296 tree
18297 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18299 tree ret;
18300 timevar_push (TV_TEMPLATE_INST);
18301 ret = instantiate_template_1 (tmpl, orig_args, complain);
18302 timevar_pop (TV_TEMPLATE_INST);
18303 return ret;
18306 /* Instantiate the alias template TMPL with ARGS. Also push a template
18307 instantiation level, which instantiate_template doesn't do because
18308 functions and variables have sufficient context established by the
18309 callers. */
18311 static tree
18312 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18314 struct pending_template *old_last_pend = last_pending_template;
18315 struct tinst_level *old_error_tinst = last_error_tinst_level;
18316 if (tmpl == error_mark_node || args == error_mark_node)
18317 return error_mark_node;
18318 tree tinst = build_tree_list (tmpl, args);
18319 if (!push_tinst_level (tinst))
18321 ggc_free (tinst);
18322 return error_mark_node;
18325 args =
18326 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18327 args, tmpl, complain,
18328 /*require_all_args=*/true,
18329 /*use_default_args=*/true);
18331 tree r = instantiate_template (tmpl, args, complain);
18332 pop_tinst_level ();
18333 /* We can't free this if a pending_template entry or last_error_tinst_level
18334 is pointing at it. */
18335 if (last_pending_template == old_last_pend
18336 && last_error_tinst_level == old_error_tinst)
18337 ggc_free (tinst);
18339 return r;
18342 /* PARM is a template parameter pack for FN. Returns true iff
18343 PARM is used in a deducible way in the argument list of FN. */
18345 static bool
18346 pack_deducible_p (tree parm, tree fn)
18348 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18349 for (; t; t = TREE_CHAIN (t))
18351 tree type = TREE_VALUE (t);
18352 tree packs;
18353 if (!PACK_EXPANSION_P (type))
18354 continue;
18355 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18356 packs; packs = TREE_CHAIN (packs))
18357 if (template_args_equal (TREE_VALUE (packs), parm))
18359 /* The template parameter pack is used in a function parameter
18360 pack. If this is the end of the parameter list, the
18361 template parameter pack is deducible. */
18362 if (TREE_CHAIN (t) == void_list_node)
18363 return true;
18364 else
18365 /* Otherwise, not. Well, it could be deduced from
18366 a non-pack parameter, but doing so would end up with
18367 a deduction mismatch, so don't bother. */
18368 return false;
18371 /* The template parameter pack isn't used in any function parameter
18372 packs, but it might be used deeper, e.g. tuple<Args...>. */
18373 return true;
18376 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18377 NARGS elements of the arguments that are being used when calling
18378 it. TARGS is a vector into which the deduced template arguments
18379 are placed.
18381 Returns either a FUNCTION_DECL for the matching specialization of FN or
18382 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18383 true, diagnostics will be printed to explain why it failed.
18385 If FN is a conversion operator, or we are trying to produce a specific
18386 specialization, RETURN_TYPE is the return type desired.
18388 The EXPLICIT_TARGS are explicit template arguments provided via a
18389 template-id.
18391 The parameter STRICT is one of:
18393 DEDUCE_CALL:
18394 We are deducing arguments for a function call, as in
18395 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18396 deducing arguments for a call to the result of a conversion
18397 function template, as in [over.call.object].
18399 DEDUCE_CONV:
18400 We are deducing arguments for a conversion function, as in
18401 [temp.deduct.conv].
18403 DEDUCE_EXACT:
18404 We are deducing arguments when doing an explicit instantiation
18405 as in [temp.explicit], when determining an explicit specialization
18406 as in [temp.expl.spec], or when taking the address of a function
18407 template, as in [temp.deduct.funcaddr]. */
18409 tree
18410 fn_type_unification (tree fn,
18411 tree explicit_targs,
18412 tree targs,
18413 const tree *args,
18414 unsigned int nargs,
18415 tree return_type,
18416 unification_kind_t strict,
18417 int flags,
18418 bool explain_p,
18419 bool decltype_p)
18421 tree parms;
18422 tree fntype;
18423 tree decl = NULL_TREE;
18424 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18425 bool ok;
18426 static int deduction_depth;
18427 struct pending_template *old_last_pend = last_pending_template;
18428 struct tinst_level *old_error_tinst = last_error_tinst_level;
18430 tree orig_fn = fn;
18431 if (flag_new_inheriting_ctors)
18432 fn = strip_inheriting_ctors (fn);
18434 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18435 tree tinst;
18436 tree r = error_mark_node;
18438 tree full_targs = targs;
18439 if (TMPL_ARGS_DEPTH (targs)
18440 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18441 full_targs = (add_outermost_template_args
18442 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18443 targs));
18445 if (decltype_p)
18446 complain |= tf_decltype;
18448 /* In C++0x, it's possible to have a function template whose type depends
18449 on itself recursively. This is most obvious with decltype, but can also
18450 occur with enumeration scope (c++/48969). So we need to catch infinite
18451 recursion and reject the substitution at deduction time; this function
18452 will return error_mark_node for any repeated substitution.
18454 This also catches excessive recursion such as when f<N> depends on
18455 f<N-1> across all integers, and returns error_mark_node for all the
18456 substitutions back up to the initial one.
18458 This is, of course, not reentrant. */
18459 if (excessive_deduction_depth)
18460 return error_mark_node;
18461 tinst = build_tree_list (fn, NULL_TREE);
18462 ++deduction_depth;
18464 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18466 fntype = TREE_TYPE (fn);
18467 if (explicit_targs)
18469 /* [temp.deduct]
18471 The specified template arguments must match the template
18472 parameters in kind (i.e., type, nontype, template), and there
18473 must not be more arguments than there are parameters;
18474 otherwise type deduction fails.
18476 Nontype arguments must match the types of the corresponding
18477 nontype template parameters, or must be convertible to the
18478 types of the corresponding nontype parameters as specified in
18479 _temp.arg.nontype_, otherwise type deduction fails.
18481 All references in the function type of the function template
18482 to the corresponding template parameters are replaced by the
18483 specified template argument values. If a substitution in a
18484 template parameter or in the function type of the function
18485 template results in an invalid type, type deduction fails. */
18486 int i, len = TREE_VEC_LENGTH (tparms);
18487 location_t loc = input_location;
18488 bool incomplete = false;
18490 if (explicit_targs == error_mark_node)
18491 goto fail;
18493 if (TMPL_ARGS_DEPTH (explicit_targs)
18494 < TMPL_ARGS_DEPTH (full_targs))
18495 explicit_targs = add_outermost_template_args (full_targs,
18496 explicit_targs);
18498 /* Adjust any explicit template arguments before entering the
18499 substitution context. */
18500 explicit_targs
18501 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18502 complain,
18503 /*require_all_args=*/false,
18504 /*use_default_args=*/false));
18505 if (explicit_targs == error_mark_node)
18506 goto fail;
18508 /* Substitute the explicit args into the function type. This is
18509 necessary so that, for instance, explicitly declared function
18510 arguments can match null pointed constants. If we were given
18511 an incomplete set of explicit args, we must not do semantic
18512 processing during substitution as we could create partial
18513 instantiations. */
18514 for (i = 0; i < len; i++)
18516 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18517 bool parameter_pack = false;
18518 tree targ = TREE_VEC_ELT (explicit_targs, i);
18520 /* Dig out the actual parm. */
18521 if (TREE_CODE (parm) == TYPE_DECL
18522 || TREE_CODE (parm) == TEMPLATE_DECL)
18524 parm = TREE_TYPE (parm);
18525 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18527 else if (TREE_CODE (parm) == PARM_DECL)
18529 parm = DECL_INITIAL (parm);
18530 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18533 if (!parameter_pack && targ == NULL_TREE)
18534 /* No explicit argument for this template parameter. */
18535 incomplete = true;
18537 if (parameter_pack && pack_deducible_p (parm, fn))
18539 /* Mark the argument pack as "incomplete". We could
18540 still deduce more arguments during unification.
18541 We remove this mark in type_unification_real. */
18542 if (targ)
18544 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18545 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18546 = ARGUMENT_PACK_ARGS (targ);
18549 /* We have some incomplete argument packs. */
18550 incomplete = true;
18554 TREE_VALUE (tinst) = explicit_targs;
18555 if (!push_tinst_level (tinst))
18557 excessive_deduction_depth = true;
18558 goto fail;
18560 processing_template_decl += incomplete;
18561 input_location = DECL_SOURCE_LOCATION (fn);
18562 /* Ignore any access checks; we'll see them again in
18563 instantiate_template and they might have the wrong
18564 access path at this point. */
18565 push_deferring_access_checks (dk_deferred);
18566 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18567 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18568 pop_deferring_access_checks ();
18569 input_location = loc;
18570 processing_template_decl -= incomplete;
18571 pop_tinst_level ();
18573 if (fntype == error_mark_node)
18574 goto fail;
18576 /* Place the explicitly specified arguments in TARGS. */
18577 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18578 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18579 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18582 /* Never do unification on the 'this' parameter. */
18583 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18585 if (return_type && strict == DEDUCE_CALL)
18587 /* We're deducing for a call to the result of a template conversion
18588 function. The parms we really want are in return_type. */
18589 if (POINTER_TYPE_P (return_type))
18590 return_type = TREE_TYPE (return_type);
18591 parms = TYPE_ARG_TYPES (return_type);
18593 else if (return_type)
18595 tree *new_args;
18597 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18598 new_args = XALLOCAVEC (tree, nargs + 1);
18599 new_args[0] = return_type;
18600 memcpy (new_args + 1, args, nargs * sizeof (tree));
18601 args = new_args;
18602 ++nargs;
18605 /* We allow incomplete unification without an error message here
18606 because the standard doesn't seem to explicitly prohibit it. Our
18607 callers must be ready to deal with unification failures in any
18608 event. */
18610 TREE_VALUE (tinst) = targs;
18611 /* If we aren't explaining yet, push tinst context so we can see where
18612 any errors (e.g. from class instantiations triggered by instantiation
18613 of default template arguments) come from. If we are explaining, this
18614 context is redundant. */
18615 if (!explain_p && !push_tinst_level (tinst))
18617 excessive_deduction_depth = true;
18618 goto fail;
18621 /* type_unification_real will pass back any access checks from default
18622 template argument substitution. */
18623 vec<deferred_access_check, va_gc> *checks;
18624 checks = NULL;
18626 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18627 full_targs, parms, args, nargs, /*subr=*/0,
18628 strict, flags, &checks, explain_p);
18629 if (!explain_p)
18630 pop_tinst_level ();
18631 if (!ok)
18632 goto fail;
18634 /* Now that we have bindings for all of the template arguments,
18635 ensure that the arguments deduced for the template template
18636 parameters have compatible template parameter lists. We cannot
18637 check this property before we have deduced all template
18638 arguments, because the template parameter types of a template
18639 template parameter might depend on prior template parameters
18640 deduced after the template template parameter. The following
18641 ill-formed example illustrates this issue:
18643 template<typename T, template<T> class C> void f(C<5>, T);
18645 template<int N> struct X {};
18647 void g() {
18648 f(X<5>(), 5l); // error: template argument deduction fails
18651 The template parameter list of 'C' depends on the template type
18652 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18653 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18654 time that we deduce 'C'. */
18655 if (!template_template_parm_bindings_ok_p
18656 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18658 unify_inconsistent_template_template_parameters (explain_p);
18659 goto fail;
18662 /* All is well so far. Now, check:
18664 [temp.deduct]
18666 When all template arguments have been deduced, all uses of
18667 template parameters in nondeduced contexts are replaced with
18668 the corresponding deduced argument values. If the
18669 substitution results in an invalid type, as described above,
18670 type deduction fails. */
18671 TREE_VALUE (tinst) = targs;
18672 if (!push_tinst_level (tinst))
18674 excessive_deduction_depth = true;
18675 goto fail;
18678 /* Also collect access checks from the instantiation. */
18679 reopen_deferring_access_checks (checks);
18681 decl = instantiate_template (fn, targs, complain);
18683 checks = get_deferred_access_checks ();
18684 pop_deferring_access_checks ();
18686 pop_tinst_level ();
18688 if (decl == error_mark_node)
18689 goto fail;
18691 /* Now perform any access checks encountered during substitution. */
18692 push_access_scope (decl);
18693 ok = perform_access_checks (checks, complain);
18694 pop_access_scope (decl);
18695 if (!ok)
18696 goto fail;
18698 /* If we're looking for an exact match, check that what we got
18699 is indeed an exact match. It might not be if some template
18700 parameters are used in non-deduced contexts. But don't check
18701 for an exact match if we have dependent template arguments;
18702 in that case we're doing partial ordering, and we already know
18703 that we have two candidates that will provide the actual type. */
18704 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18706 tree substed = TREE_TYPE (decl);
18707 unsigned int i;
18709 tree sarg
18710 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18711 if (return_type)
18712 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18713 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18714 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18716 unify_type_mismatch (explain_p, args[i],
18717 TREE_VALUE (sarg));
18718 goto fail;
18722 /* After doing deduction with the inherited constructor, actually return an
18723 instantiation of the inheriting constructor. */
18724 if (orig_fn != fn)
18725 decl = instantiate_template (orig_fn, targs, complain);
18727 r = decl;
18729 fail:
18730 --deduction_depth;
18731 if (excessive_deduction_depth)
18733 if (deduction_depth == 0)
18734 /* Reset once we're all the way out. */
18735 excessive_deduction_depth = false;
18738 /* We can't free this if a pending_template entry or last_error_tinst_level
18739 is pointing at it. */
18740 if (last_pending_template == old_last_pend
18741 && last_error_tinst_level == old_error_tinst)
18742 ggc_free (tinst);
18744 return r;
18747 /* Adjust types before performing type deduction, as described in
18748 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18749 sections are symmetric. PARM is the type of a function parameter
18750 or the return type of the conversion function. ARG is the type of
18751 the argument passed to the call, or the type of the value
18752 initialized with the result of the conversion function.
18753 ARG_EXPR is the original argument expression, which may be null. */
18755 static int
18756 maybe_adjust_types_for_deduction (unification_kind_t strict,
18757 tree* parm,
18758 tree* arg,
18759 tree arg_expr)
18761 int result = 0;
18763 switch (strict)
18765 case DEDUCE_CALL:
18766 break;
18768 case DEDUCE_CONV:
18769 /* Swap PARM and ARG throughout the remainder of this
18770 function; the handling is precisely symmetric since PARM
18771 will initialize ARG rather than vice versa. */
18772 std::swap (parm, arg);
18773 break;
18775 case DEDUCE_EXACT:
18776 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18777 too, but here handle it by stripping the reference from PARM
18778 rather than by adding it to ARG. */
18779 if (TREE_CODE (*parm) == REFERENCE_TYPE
18780 && TYPE_REF_IS_RVALUE (*parm)
18781 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18782 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18783 && TREE_CODE (*arg) == REFERENCE_TYPE
18784 && !TYPE_REF_IS_RVALUE (*arg))
18785 *parm = TREE_TYPE (*parm);
18786 /* Nothing else to do in this case. */
18787 return 0;
18789 default:
18790 gcc_unreachable ();
18793 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18795 /* [temp.deduct.call]
18797 If P is not a reference type:
18799 --If A is an array type, the pointer type produced by the
18800 array-to-pointer standard conversion (_conv.array_) is
18801 used in place of A for type deduction; otherwise,
18803 --If A is a function type, the pointer type produced by
18804 the function-to-pointer standard conversion
18805 (_conv.func_) is used in place of A for type deduction;
18806 otherwise,
18808 --If A is a cv-qualified type, the top level
18809 cv-qualifiers of A's type are ignored for type
18810 deduction. */
18811 if (TREE_CODE (*arg) == ARRAY_TYPE)
18812 *arg = build_pointer_type (TREE_TYPE (*arg));
18813 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18814 *arg = build_pointer_type (*arg);
18815 else
18816 *arg = TYPE_MAIN_VARIANT (*arg);
18819 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18820 reference to a cv-unqualified template parameter that does not represent a
18821 template parameter of a class template (during class template argument
18822 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18823 an lvalue, the type "lvalue reference to A" is used in place of A for type
18824 deduction. */
18825 if (TREE_CODE (*parm) == REFERENCE_TYPE
18826 && TYPE_REF_IS_RVALUE (*parm)
18827 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18828 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18829 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18830 && (arg_expr ? lvalue_p (arg_expr)
18831 /* try_one_overload doesn't provide an arg_expr, but
18832 functions are always lvalues. */
18833 : TREE_CODE (*arg) == FUNCTION_TYPE))
18834 *arg = build_reference_type (*arg);
18836 /* [temp.deduct.call]
18838 If P is a cv-qualified type, the top level cv-qualifiers
18839 of P's type are ignored for type deduction. If P is a
18840 reference type, the type referred to by P is used for
18841 type deduction. */
18842 *parm = TYPE_MAIN_VARIANT (*parm);
18843 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18845 *parm = TREE_TYPE (*parm);
18846 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18849 /* DR 322. For conversion deduction, remove a reference type on parm
18850 too (which has been swapped into ARG). */
18851 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18852 *arg = TREE_TYPE (*arg);
18854 return result;
18857 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18858 template which does contain any deducible template parameters; check if
18859 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18860 unify_one_argument. */
18862 static int
18863 check_non_deducible_conversion (tree parm, tree arg, int strict,
18864 int flags, bool explain_p)
18866 tree type;
18868 if (!TYPE_P (arg))
18869 type = TREE_TYPE (arg);
18870 else
18871 type = arg;
18873 if (same_type_p (parm, type))
18874 return unify_success (explain_p);
18876 if (strict == DEDUCE_CONV)
18878 if (can_convert_arg (type, parm, NULL_TREE, flags,
18879 explain_p ? tf_warning_or_error : tf_none))
18880 return unify_success (explain_p);
18882 else if (strict != DEDUCE_EXACT)
18884 if (can_convert_arg (parm, type,
18885 TYPE_P (arg) ? NULL_TREE : arg,
18886 flags, explain_p ? tf_warning_or_error : tf_none))
18887 return unify_success (explain_p);
18890 if (strict == DEDUCE_EXACT)
18891 return unify_type_mismatch (explain_p, parm, arg);
18892 else
18893 return unify_arg_conversion (explain_p, parm, type, arg);
18896 static bool uses_deducible_template_parms (tree type);
18898 /* Returns true iff the expression EXPR is one from which a template
18899 argument can be deduced. In other words, if it's an undecorated
18900 use of a template non-type parameter. */
18902 static bool
18903 deducible_expression (tree expr)
18905 /* Strip implicit conversions. */
18906 while (CONVERT_EXPR_P (expr))
18907 expr = TREE_OPERAND (expr, 0);
18908 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18911 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18912 deducible way; that is, if it has a max value of <PARM> - 1. */
18914 static bool
18915 deducible_array_bound (tree domain)
18917 if (domain == NULL_TREE)
18918 return false;
18920 tree max = TYPE_MAX_VALUE (domain);
18921 if (TREE_CODE (max) != MINUS_EXPR)
18922 return false;
18924 return deducible_expression (TREE_OPERAND (max, 0));
18927 /* Returns true iff the template arguments ARGS use a template parameter
18928 in a deducible way. */
18930 static bool
18931 deducible_template_args (tree args)
18933 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18935 bool deducible;
18936 tree elt = TREE_VEC_ELT (args, i);
18937 if (ARGUMENT_PACK_P (elt))
18938 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18939 else
18941 if (PACK_EXPANSION_P (elt))
18942 elt = PACK_EXPANSION_PATTERN (elt);
18943 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18944 deducible = true;
18945 else if (TYPE_P (elt))
18946 deducible = uses_deducible_template_parms (elt);
18947 else
18948 deducible = deducible_expression (elt);
18950 if (deducible)
18951 return true;
18953 return false;
18956 /* Returns true iff TYPE contains any deducible references to template
18957 parameters, as per 14.8.2.5. */
18959 static bool
18960 uses_deducible_template_parms (tree type)
18962 if (PACK_EXPANSION_P (type))
18963 type = PACK_EXPANSION_PATTERN (type);
18965 /* T
18966 cv-list T
18967 TT<T>
18968 TT<i>
18969 TT<> */
18970 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18971 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18972 return true;
18974 /* T*
18976 T&& */
18977 if (POINTER_TYPE_P (type))
18978 return uses_deducible_template_parms (TREE_TYPE (type));
18980 /* T[integer-constant ]
18981 type [i] */
18982 if (TREE_CODE (type) == ARRAY_TYPE)
18983 return (uses_deducible_template_parms (TREE_TYPE (type))
18984 || deducible_array_bound (TYPE_DOMAIN (type)));
18986 /* T type ::*
18987 type T::*
18988 T T::*
18989 T (type ::*)()
18990 type (T::*)()
18991 type (type ::*)(T)
18992 type (T::*)(T)
18993 T (type ::*)(T)
18994 T (T::*)()
18995 T (T::*)(T) */
18996 if (TYPE_PTRMEM_P (type))
18997 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18998 || (uses_deducible_template_parms
18999 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19001 /* template-name <T> (where template-name refers to a class template)
19002 template-name <i> (where template-name refers to a class template) */
19003 if (CLASS_TYPE_P (type)
19004 && CLASSTYPE_TEMPLATE_INFO (type)
19005 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19006 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19007 (CLASSTYPE_TI_ARGS (type)));
19009 /* type (T)
19011 T(T) */
19012 if (TREE_CODE (type) == FUNCTION_TYPE
19013 || TREE_CODE (type) == METHOD_TYPE)
19015 if (uses_deducible_template_parms (TREE_TYPE (type)))
19016 return true;
19017 tree parm = TYPE_ARG_TYPES (type);
19018 if (TREE_CODE (type) == METHOD_TYPE)
19019 parm = TREE_CHAIN (parm);
19020 for (; parm; parm = TREE_CHAIN (parm))
19021 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19022 return true;
19025 return false;
19028 /* Subroutine of type_unification_real and unify_pack_expansion to
19029 handle unification of a single P/A pair. Parameters are as
19030 for those functions. */
19032 static int
19033 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19034 int subr, unification_kind_t strict,
19035 bool explain_p)
19037 tree arg_expr = NULL_TREE;
19038 int arg_strict;
19040 if (arg == error_mark_node || parm == error_mark_node)
19041 return unify_invalid (explain_p);
19042 if (arg == unknown_type_node)
19043 /* We can't deduce anything from this, but we might get all the
19044 template args from other function args. */
19045 return unify_success (explain_p);
19047 /* Implicit conversions (Clause 4) will be performed on a function
19048 argument to convert it to the type of the corresponding function
19049 parameter if the parameter type contains no template-parameters that
19050 participate in template argument deduction. */
19051 if (strict != DEDUCE_EXACT
19052 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19053 /* For function parameters with no deducible template parameters,
19054 just return. We'll check non-dependent conversions later. */
19055 return unify_success (explain_p);
19057 switch (strict)
19059 case DEDUCE_CALL:
19060 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19061 | UNIFY_ALLOW_MORE_CV_QUAL
19062 | UNIFY_ALLOW_DERIVED);
19063 break;
19065 case DEDUCE_CONV:
19066 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19067 break;
19069 case DEDUCE_EXACT:
19070 arg_strict = UNIFY_ALLOW_NONE;
19071 break;
19073 default:
19074 gcc_unreachable ();
19077 /* We only do these transformations if this is the top-level
19078 parameter_type_list in a call or declaration matching; in other
19079 situations (nested function declarators, template argument lists) we
19080 won't be comparing a type to an expression, and we don't do any type
19081 adjustments. */
19082 if (!subr)
19084 if (!TYPE_P (arg))
19086 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19087 if (type_unknown_p (arg))
19089 /* [temp.deduct.type] A template-argument can be
19090 deduced from a pointer to function or pointer
19091 to member function argument if the set of
19092 overloaded functions does not contain function
19093 templates and at most one of a set of
19094 overloaded functions provides a unique
19095 match. */
19097 if (resolve_overloaded_unification
19098 (tparms, targs, parm, arg, strict,
19099 arg_strict, explain_p))
19100 return unify_success (explain_p);
19101 return unify_overload_resolution_failure (explain_p, arg);
19104 arg_expr = arg;
19105 arg = unlowered_expr_type (arg);
19106 if (arg == error_mark_node)
19107 return unify_invalid (explain_p);
19110 arg_strict |=
19111 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19113 else
19114 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19115 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19116 return unify_template_argument_mismatch (explain_p, parm, arg);
19118 /* For deduction from an init-list we need the actual list. */
19119 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19120 arg = arg_expr;
19121 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19124 /* for_each_template_parm callback that always returns 0. */
19126 static int
19127 zero_r (tree, void *)
19129 return 0;
19132 /* for_each_template_parm any_fn callback to handle deduction of a template
19133 type argument from the type of an array bound. */
19135 static int
19136 array_deduction_r (tree t, void *data)
19138 tree_pair_p d = (tree_pair_p)data;
19139 tree &tparms = d->purpose;
19140 tree &targs = d->value;
19142 if (TREE_CODE (t) == ARRAY_TYPE)
19143 if (tree dom = TYPE_DOMAIN (t))
19144 if (tree max = TYPE_MAX_VALUE (dom))
19146 if (TREE_CODE (max) == MINUS_EXPR)
19147 max = TREE_OPERAND (max, 0);
19148 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19149 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19150 UNIFY_ALLOW_NONE, /*explain*/false);
19153 /* Keep walking. */
19154 return 0;
19157 /* Try to deduce any not-yet-deduced template type arguments from the type of
19158 an array bound. This is handled separately from unify because 14.8.2.5 says
19159 "The type of a type parameter is only deduced from an array bound if it is
19160 not otherwise deduced." */
19162 static void
19163 try_array_deduction (tree tparms, tree targs, tree parm)
19165 tree_pair_s data = { tparms, targs };
19166 hash_set<tree> visited;
19167 for_each_template_parm (parm, zero_r, &data, &visited,
19168 /*nondeduced*/false, array_deduction_r);
19171 /* Most parms like fn_type_unification.
19173 If SUBR is 1, we're being called recursively (to unify the
19174 arguments of a function or method parameter of a function
19175 template).
19177 CHECKS is a pointer to a vector of access checks encountered while
19178 substituting default template arguments. */
19180 static int
19181 type_unification_real (tree tparms,
19182 tree full_targs,
19183 tree xparms,
19184 const tree *xargs,
19185 unsigned int xnargs,
19186 int subr,
19187 unification_kind_t strict,
19188 int flags,
19189 vec<deferred_access_check, va_gc> **checks,
19190 bool explain_p)
19192 tree parm, arg;
19193 int i;
19194 int ntparms = TREE_VEC_LENGTH (tparms);
19195 int saw_undeduced = 0;
19196 tree parms;
19197 const tree *args;
19198 unsigned int nargs;
19199 unsigned int ia;
19201 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19202 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19203 gcc_assert (ntparms > 0);
19205 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19207 /* Reset the number of non-defaulted template arguments contained
19208 in TARGS. */
19209 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19211 again:
19212 parms = xparms;
19213 args = xargs;
19214 nargs = xnargs;
19216 ia = 0;
19217 while (parms && parms != void_list_node
19218 && ia < nargs)
19220 parm = TREE_VALUE (parms);
19222 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19223 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19224 /* For a function parameter pack that occurs at the end of the
19225 parameter-declaration-list, the type A of each remaining
19226 argument of the call is compared with the type P of the
19227 declarator-id of the function parameter pack. */
19228 break;
19230 parms = TREE_CHAIN (parms);
19232 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19233 /* For a function parameter pack that does not occur at the
19234 end of the parameter-declaration-list, the type of the
19235 parameter pack is a non-deduced context. */
19236 continue;
19238 arg = args[ia];
19239 ++ia;
19241 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19242 explain_p))
19243 return 1;
19246 if (parms
19247 && parms != void_list_node
19248 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19250 /* Unify the remaining arguments with the pack expansion type. */
19251 tree argvec;
19252 tree parmvec = make_tree_vec (1);
19254 /* Allocate a TREE_VEC and copy in all of the arguments */
19255 argvec = make_tree_vec (nargs - ia);
19256 for (i = 0; ia < nargs; ++ia, ++i)
19257 TREE_VEC_ELT (argvec, i) = args[ia];
19259 /* Copy the parameter into parmvec. */
19260 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19261 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19262 /*subr=*/subr, explain_p))
19263 return 1;
19265 /* Advance to the end of the list of parameters. */
19266 parms = TREE_CHAIN (parms);
19269 /* Fail if we've reached the end of the parm list, and more args
19270 are present, and the parm list isn't variadic. */
19271 if (ia < nargs && parms == void_list_node)
19272 return unify_too_many_arguments (explain_p, nargs, ia);
19273 /* Fail if parms are left and they don't have default values and
19274 they aren't all deduced as empty packs (c++/57397). This is
19275 consistent with sufficient_parms_p. */
19276 if (parms && parms != void_list_node
19277 && TREE_PURPOSE (parms) == NULL_TREE)
19279 unsigned int count = nargs;
19280 tree p = parms;
19281 bool type_pack_p;
19284 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19285 if (!type_pack_p)
19286 count++;
19287 p = TREE_CHAIN (p);
19289 while (p && p != void_list_node);
19290 if (count != nargs)
19291 return unify_too_few_arguments (explain_p, ia, count,
19292 type_pack_p);
19295 if (!subr)
19297 tsubst_flags_t complain = (explain_p
19298 ? tf_warning_or_error
19299 : tf_none);
19300 bool tried_array_deduction = (cxx_dialect < cxx1z);
19302 for (i = 0; i < ntparms; i++)
19304 tree targ = TREE_VEC_ELT (targs, i);
19305 tree tparm = TREE_VEC_ELT (tparms, i);
19307 /* Clear the "incomplete" flags on all argument packs now so that
19308 substituting them into later default arguments works. */
19309 if (targ && ARGUMENT_PACK_P (targ))
19311 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19312 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19315 if (targ || tparm == error_mark_node)
19316 continue;
19317 tparm = TREE_VALUE (tparm);
19319 if (TREE_CODE (tparm) == TYPE_DECL
19320 && !tried_array_deduction)
19322 try_array_deduction (tparms, targs, xparms);
19323 tried_array_deduction = true;
19324 if (TREE_VEC_ELT (targs, i))
19325 continue;
19328 /* If this is an undeduced nontype parameter that depends on
19329 a type parameter, try another pass; its type may have been
19330 deduced from a later argument than the one from which
19331 this parameter can be deduced. */
19332 if (TREE_CODE (tparm) == PARM_DECL
19333 && uses_template_parms (TREE_TYPE (tparm))
19334 && saw_undeduced < 2)
19336 saw_undeduced = 1;
19337 continue;
19340 /* Core issue #226 (C++0x) [temp.deduct]:
19342 If a template argument has not been deduced, its
19343 default template argument, if any, is used.
19345 When we are in C++98 mode, TREE_PURPOSE will either
19346 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19347 to explicitly check cxx_dialect here. */
19348 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19349 /* OK, there is a default argument. Wait until after the
19350 conversion check to do substitution. */
19351 continue;
19353 /* If the type parameter is a parameter pack, then it will
19354 be deduced to an empty parameter pack. */
19355 if (template_parameter_pack_p (tparm))
19357 tree arg;
19359 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19361 arg = make_node (NONTYPE_ARGUMENT_PACK);
19362 TREE_CONSTANT (arg) = 1;
19364 else
19365 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19367 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19369 TREE_VEC_ELT (targs, i) = arg;
19370 continue;
19373 return unify_parameter_deduction_failure (explain_p, tparm);
19376 /* DR 1391: All parameters have args, now check non-dependent parms for
19377 convertibility. */
19378 if (saw_undeduced < 2)
19379 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19380 parms && parms != void_list_node && ia < nargs; )
19382 parm = TREE_VALUE (parms);
19384 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19385 && (!TREE_CHAIN (parms)
19386 || TREE_CHAIN (parms) == void_list_node))
19387 /* For a function parameter pack that occurs at the end of the
19388 parameter-declaration-list, the type A of each remaining
19389 argument of the call is compared with the type P of the
19390 declarator-id of the function parameter pack. */
19391 break;
19393 parms = TREE_CHAIN (parms);
19395 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19396 /* For a function parameter pack that does not occur at the
19397 end of the parameter-declaration-list, the type of the
19398 parameter pack is a non-deduced context. */
19399 continue;
19401 arg = args[ia];
19402 ++ia;
19404 if (uses_template_parms (parm))
19405 continue;
19406 if (check_non_deducible_conversion (parm, arg, strict, flags,
19407 explain_p))
19408 return 1;
19411 /* Now substitute into the default template arguments. */
19412 for (i = 0; i < ntparms; i++)
19414 tree targ = TREE_VEC_ELT (targs, i);
19415 tree tparm = TREE_VEC_ELT (tparms, i);
19417 if (targ || tparm == error_mark_node)
19418 continue;
19419 tree parm = TREE_VALUE (tparm);
19421 if (TREE_CODE (parm) == PARM_DECL
19422 && uses_template_parms (TREE_TYPE (parm))
19423 && saw_undeduced < 2)
19424 continue;
19426 tree arg = TREE_PURPOSE (tparm);
19427 reopen_deferring_access_checks (*checks);
19428 location_t save_loc = input_location;
19429 if (DECL_P (parm))
19430 input_location = DECL_SOURCE_LOCATION (parm);
19431 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19432 if (!uses_template_parms (arg))
19433 arg = convert_template_argument (parm, arg, full_targs, complain,
19434 i, NULL_TREE);
19435 else if (saw_undeduced < 2)
19436 arg = NULL_TREE;
19437 else
19438 arg = error_mark_node;
19439 input_location = save_loc;
19440 *checks = get_deferred_access_checks ();
19441 pop_deferring_access_checks ();
19442 if (arg == error_mark_node)
19443 return 1;
19444 else if (arg)
19446 TREE_VEC_ELT (targs, i) = arg;
19447 /* The position of the first default template argument,
19448 is also the number of non-defaulted arguments in TARGS.
19449 Record that. */
19450 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19451 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19455 if (saw_undeduced++ == 1)
19456 goto again;
19459 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19460 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19462 return unify_success (explain_p);
19465 /* Subroutine of type_unification_real. Args are like the variables
19466 at the call site. ARG is an overloaded function (or template-id);
19467 we try deducing template args from each of the overloads, and if
19468 only one succeeds, we go with that. Modifies TARGS and returns
19469 true on success. */
19471 static bool
19472 resolve_overloaded_unification (tree tparms,
19473 tree targs,
19474 tree parm,
19475 tree arg,
19476 unification_kind_t strict,
19477 int sub_strict,
19478 bool explain_p)
19480 tree tempargs = copy_node (targs);
19481 int good = 0;
19482 tree goodfn = NULL_TREE;
19483 bool addr_p;
19485 if (TREE_CODE (arg) == ADDR_EXPR)
19487 arg = TREE_OPERAND (arg, 0);
19488 addr_p = true;
19490 else
19491 addr_p = false;
19493 if (TREE_CODE (arg) == COMPONENT_REF)
19494 /* Handle `&x' where `x' is some static or non-static member
19495 function name. */
19496 arg = TREE_OPERAND (arg, 1);
19498 if (TREE_CODE (arg) == OFFSET_REF)
19499 arg = TREE_OPERAND (arg, 1);
19501 /* Strip baselink information. */
19502 if (BASELINK_P (arg))
19503 arg = BASELINK_FUNCTIONS (arg);
19505 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19507 /* If we got some explicit template args, we need to plug them into
19508 the affected templates before we try to unify, in case the
19509 explicit args will completely resolve the templates in question. */
19511 int ok = 0;
19512 tree expl_subargs = TREE_OPERAND (arg, 1);
19513 arg = TREE_OPERAND (arg, 0);
19515 for (lkp_iterator iter (arg); iter; ++iter)
19517 tree fn = *iter;
19518 tree subargs, elem;
19520 if (TREE_CODE (fn) != TEMPLATE_DECL)
19521 continue;
19523 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19524 expl_subargs, NULL_TREE, tf_none,
19525 /*require_all_args=*/true,
19526 /*use_default_args=*/true);
19527 if (subargs != error_mark_node
19528 && !any_dependent_template_arguments_p (subargs))
19530 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19531 if (try_one_overload (tparms, targs, tempargs, parm,
19532 elem, strict, sub_strict, addr_p, explain_p)
19533 && (!goodfn || !same_type_p (goodfn, elem)))
19535 goodfn = elem;
19536 ++good;
19539 else if (subargs)
19540 ++ok;
19542 /* If no templates (or more than one) are fully resolved by the
19543 explicit arguments, this template-id is a non-deduced context; it
19544 could still be OK if we deduce all template arguments for the
19545 enclosing call through other arguments. */
19546 if (good != 1)
19547 good = ok;
19549 else if (TREE_CODE (arg) != OVERLOAD
19550 && TREE_CODE (arg) != FUNCTION_DECL)
19551 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19552 -- but the deduction does not succeed because the expression is
19553 not just the function on its own. */
19554 return false;
19555 else
19556 for (lkp_iterator iter (arg); iter; ++iter)
19558 tree fn = *iter;
19559 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19560 strict, sub_strict, addr_p, explain_p)
19561 && (!goodfn || !decls_match (goodfn, fn)))
19563 goodfn = fn;
19564 ++good;
19568 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19569 to function or pointer to member function argument if the set of
19570 overloaded functions does not contain function templates and at most
19571 one of a set of overloaded functions provides a unique match.
19573 So if we found multiple possibilities, we return success but don't
19574 deduce anything. */
19576 if (good == 1)
19578 int i = TREE_VEC_LENGTH (targs);
19579 for (; i--; )
19580 if (TREE_VEC_ELT (tempargs, i))
19582 tree old = TREE_VEC_ELT (targs, i);
19583 tree new_ = TREE_VEC_ELT (tempargs, i);
19584 if (new_ && old && ARGUMENT_PACK_P (old)
19585 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19586 /* Don't forget explicit template arguments in a pack. */
19587 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19588 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19589 TREE_VEC_ELT (targs, i) = new_;
19592 if (good)
19593 return true;
19595 return false;
19598 /* Core DR 115: In contexts where deduction is done and fails, or in
19599 contexts where deduction is not done, if a template argument list is
19600 specified and it, along with any default template arguments, identifies
19601 a single function template specialization, then the template-id is an
19602 lvalue for the function template specialization. */
19604 tree
19605 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19607 tree expr, offset, baselink;
19608 bool addr;
19610 if (!type_unknown_p (orig_expr))
19611 return orig_expr;
19613 expr = orig_expr;
19614 addr = false;
19615 offset = NULL_TREE;
19616 baselink = NULL_TREE;
19618 if (TREE_CODE (expr) == ADDR_EXPR)
19620 expr = TREE_OPERAND (expr, 0);
19621 addr = true;
19623 if (TREE_CODE (expr) == OFFSET_REF)
19625 offset = expr;
19626 expr = TREE_OPERAND (expr, 1);
19628 if (BASELINK_P (expr))
19630 baselink = expr;
19631 expr = BASELINK_FUNCTIONS (expr);
19634 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19636 int good = 0;
19637 tree goodfn = NULL_TREE;
19639 /* If we got some explicit template args, we need to plug them into
19640 the affected templates before we try to unify, in case the
19641 explicit args will completely resolve the templates in question. */
19643 tree expl_subargs = TREE_OPERAND (expr, 1);
19644 tree arg = TREE_OPERAND (expr, 0);
19645 tree badfn = NULL_TREE;
19646 tree badargs = NULL_TREE;
19648 for (lkp_iterator iter (arg); iter; ++iter)
19650 tree fn = *iter;
19651 tree subargs, elem;
19653 if (TREE_CODE (fn) != TEMPLATE_DECL)
19654 continue;
19656 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19657 expl_subargs, NULL_TREE, tf_none,
19658 /*require_all_args=*/true,
19659 /*use_default_args=*/true);
19660 if (subargs != error_mark_node
19661 && !any_dependent_template_arguments_p (subargs))
19663 elem = instantiate_template (fn, subargs, tf_none);
19664 if (elem == error_mark_node)
19666 badfn = fn;
19667 badargs = subargs;
19669 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19671 goodfn = elem;
19672 ++good;
19676 if (good == 1)
19678 mark_used (goodfn);
19679 expr = goodfn;
19680 if (baselink)
19681 expr = build_baselink (BASELINK_BINFO (baselink),
19682 BASELINK_ACCESS_BINFO (baselink),
19683 expr, BASELINK_OPTYPE (baselink));
19684 if (offset)
19686 tree base
19687 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19688 expr = build_offset_ref (base, expr, addr, complain);
19690 if (addr)
19691 expr = cp_build_addr_expr (expr, complain);
19692 return expr;
19694 else if (good == 0 && badargs && (complain & tf_error))
19695 /* There were no good options and at least one bad one, so let the
19696 user know what the problem is. */
19697 instantiate_template (badfn, badargs, complain);
19699 return orig_expr;
19702 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19703 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19704 different overloads deduce different arguments for a given parm.
19705 ADDR_P is true if the expression for which deduction is being
19706 performed was of the form "& fn" rather than simply "fn".
19708 Returns 1 on success. */
19710 static int
19711 try_one_overload (tree tparms,
19712 tree orig_targs,
19713 tree targs,
19714 tree parm,
19715 tree arg,
19716 unification_kind_t strict,
19717 int sub_strict,
19718 bool addr_p,
19719 bool explain_p)
19721 int nargs;
19722 tree tempargs;
19723 int i;
19725 if (arg == error_mark_node)
19726 return 0;
19728 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19729 to function or pointer to member function argument if the set of
19730 overloaded functions does not contain function templates and at most
19731 one of a set of overloaded functions provides a unique match.
19733 So if this is a template, just return success. */
19735 if (uses_template_parms (arg))
19736 return 1;
19738 if (TREE_CODE (arg) == METHOD_TYPE)
19739 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19740 else if (addr_p)
19741 arg = build_pointer_type (arg);
19743 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19745 /* We don't copy orig_targs for this because if we have already deduced
19746 some template args from previous args, unify would complain when we
19747 try to deduce a template parameter for the same argument, even though
19748 there isn't really a conflict. */
19749 nargs = TREE_VEC_LENGTH (targs);
19750 tempargs = make_tree_vec (nargs);
19752 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19753 return 0;
19755 /* First make sure we didn't deduce anything that conflicts with
19756 explicitly specified args. */
19757 for (i = nargs; i--; )
19759 tree elt = TREE_VEC_ELT (tempargs, i);
19760 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19762 if (!elt)
19763 /*NOP*/;
19764 else if (uses_template_parms (elt))
19765 /* Since we're unifying against ourselves, we will fill in
19766 template args used in the function parm list with our own
19767 template parms. Discard them. */
19768 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19769 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19771 /* Check that the argument at each index of the deduced argument pack
19772 is equivalent to the corresponding explicitly specified argument.
19773 We may have deduced more arguments than were explicitly specified,
19774 and that's OK. */
19776 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19777 that's wrong if we deduce the same argument pack from multiple
19778 function arguments: it's only incomplete the first time. */
19780 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19781 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19783 if (TREE_VEC_LENGTH (deduced_pack)
19784 < TREE_VEC_LENGTH (explicit_pack))
19785 return 0;
19787 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19788 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19789 TREE_VEC_ELT (deduced_pack, j)))
19790 return 0;
19792 else if (oldelt && !template_args_equal (oldelt, elt))
19793 return 0;
19796 for (i = nargs; i--; )
19798 tree elt = TREE_VEC_ELT (tempargs, i);
19800 if (elt)
19801 TREE_VEC_ELT (targs, i) = elt;
19804 return 1;
19807 /* PARM is a template class (perhaps with unbound template
19808 parameters). ARG is a fully instantiated type. If ARG can be
19809 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19810 TARGS are as for unify. */
19812 static tree
19813 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19814 bool explain_p)
19816 tree copy_of_targs;
19818 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19819 return NULL_TREE;
19820 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19821 /* Matches anything. */;
19822 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19823 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19824 return NULL_TREE;
19826 /* We need to make a new template argument vector for the call to
19827 unify. If we used TARGS, we'd clutter it up with the result of
19828 the attempted unification, even if this class didn't work out.
19829 We also don't want to commit ourselves to all the unifications
19830 we've already done, since unification is supposed to be done on
19831 an argument-by-argument basis. In other words, consider the
19832 following pathological case:
19834 template <int I, int J, int K>
19835 struct S {};
19837 template <int I, int J>
19838 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19840 template <int I, int J, int K>
19841 void f(S<I, J, K>, S<I, I, I>);
19843 void g() {
19844 S<0, 0, 0> s0;
19845 S<0, 1, 2> s2;
19847 f(s0, s2);
19850 Now, by the time we consider the unification involving `s2', we
19851 already know that we must have `f<0, 0, 0>'. But, even though
19852 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19853 because there are two ways to unify base classes of S<0, 1, 2>
19854 with S<I, I, I>. If we kept the already deduced knowledge, we
19855 would reject the possibility I=1. */
19856 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19858 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19860 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19861 return NULL_TREE;
19862 return arg;
19865 /* If unification failed, we're done. */
19866 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19867 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19868 return NULL_TREE;
19870 return arg;
19873 /* Given a template type PARM and a class type ARG, find the unique
19874 base type in ARG that is an instance of PARM. We do not examine
19875 ARG itself; only its base-classes. If there is not exactly one
19876 appropriate base class, return NULL_TREE. PARM may be the type of
19877 a partial specialization, as well as a plain template type. Used
19878 by unify. */
19880 static enum template_base_result
19881 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19882 bool explain_p, tree *result)
19884 tree rval = NULL_TREE;
19885 tree binfo;
19887 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19889 binfo = TYPE_BINFO (complete_type (arg));
19890 if (!binfo)
19892 /* The type could not be completed. */
19893 *result = NULL_TREE;
19894 return tbr_incomplete_type;
19897 /* Walk in inheritance graph order. The search order is not
19898 important, and this avoids multiple walks of virtual bases. */
19899 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19901 tree r = try_class_unification (tparms, targs, parm,
19902 BINFO_TYPE (binfo), explain_p);
19904 if (r)
19906 /* If there is more than one satisfactory baseclass, then:
19908 [temp.deduct.call]
19910 If they yield more than one possible deduced A, the type
19911 deduction fails.
19913 applies. */
19914 if (rval && !same_type_p (r, rval))
19916 *result = NULL_TREE;
19917 return tbr_ambiguous_baseclass;
19920 rval = r;
19924 *result = rval;
19925 return tbr_success;
19928 /* Returns the level of DECL, which declares a template parameter. */
19930 static int
19931 template_decl_level (tree decl)
19933 switch (TREE_CODE (decl))
19935 case TYPE_DECL:
19936 case TEMPLATE_DECL:
19937 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19939 case PARM_DECL:
19940 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19942 default:
19943 gcc_unreachable ();
19945 return 0;
19948 /* Decide whether ARG can be unified with PARM, considering only the
19949 cv-qualifiers of each type, given STRICT as documented for unify.
19950 Returns nonzero iff the unification is OK on that basis. */
19952 static int
19953 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19955 int arg_quals = cp_type_quals (arg);
19956 int parm_quals = cp_type_quals (parm);
19958 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19959 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19961 /* Although a CVR qualifier is ignored when being applied to a
19962 substituted template parameter ([8.3.2]/1 for example), that
19963 does not allow us to unify "const T" with "int&" because both
19964 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19965 It is ok when we're allowing additional CV qualifiers
19966 at the outer level [14.8.2.1]/3,1st bullet. */
19967 if ((TREE_CODE (arg) == REFERENCE_TYPE
19968 || TREE_CODE (arg) == FUNCTION_TYPE
19969 || TREE_CODE (arg) == METHOD_TYPE)
19970 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19971 return 0;
19973 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19974 && (parm_quals & TYPE_QUAL_RESTRICT))
19975 return 0;
19978 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19979 && (arg_quals & parm_quals) != parm_quals)
19980 return 0;
19982 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19983 && (parm_quals & arg_quals) != arg_quals)
19984 return 0;
19986 return 1;
19989 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19990 void
19991 template_parm_level_and_index (tree parm, int* level, int* index)
19993 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19994 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19995 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19997 *index = TEMPLATE_TYPE_IDX (parm);
19998 *level = TEMPLATE_TYPE_LEVEL (parm);
20000 else
20002 *index = TEMPLATE_PARM_IDX (parm);
20003 *level = TEMPLATE_PARM_LEVEL (parm);
20007 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20008 do { \
20009 if (unify (TP, TA, P, A, S, EP)) \
20010 return 1; \
20011 } while (0)
20013 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20014 expansion at the end of PACKED_PARMS. Returns 0 if the type
20015 deduction succeeds, 1 otherwise. STRICT is the same as in
20016 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20017 function call argument list. We'll need to adjust the arguments to make them
20018 types. SUBR tells us if this is from a recursive call to
20019 type_unification_real, or for comparing two template argument
20020 lists. */
20022 static int
20023 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20024 tree packed_args, unification_kind_t strict,
20025 bool subr, bool explain_p)
20027 tree parm
20028 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20029 tree pattern = PACK_EXPANSION_PATTERN (parm);
20030 tree pack, packs = NULL_TREE;
20031 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20033 /* Add in any args remembered from an earlier partial instantiation. */
20034 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20036 packed_args = expand_template_argument_pack (packed_args);
20038 int len = TREE_VEC_LENGTH (packed_args);
20040 /* Determine the parameter packs we will be deducing from the
20041 pattern, and record their current deductions. */
20042 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20043 pack; pack = TREE_CHAIN (pack))
20045 tree parm_pack = TREE_VALUE (pack);
20046 int idx, level;
20048 /* Determine the index and level of this parameter pack. */
20049 template_parm_level_and_index (parm_pack, &level, &idx);
20051 /* Keep track of the parameter packs and their corresponding
20052 argument packs. */
20053 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20054 TREE_TYPE (packs) = make_tree_vec (len - start);
20057 /* Loop through all of the arguments that have not yet been
20058 unified and unify each with the pattern. */
20059 for (i = start; i < len; i++)
20061 tree parm;
20062 bool any_explicit = false;
20063 tree arg = TREE_VEC_ELT (packed_args, i);
20065 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20066 or the element of its argument pack at the current index if
20067 this argument was explicitly specified. */
20068 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20070 int idx, level;
20071 tree arg, pargs;
20072 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20074 arg = NULL_TREE;
20075 if (TREE_VALUE (pack)
20076 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20077 && (i - start < TREE_VEC_LENGTH (pargs)))
20079 any_explicit = true;
20080 arg = TREE_VEC_ELT (pargs, i - start);
20082 TMPL_ARG (targs, level, idx) = arg;
20085 /* If we had explicit template arguments, substitute them into the
20086 pattern before deduction. */
20087 if (any_explicit)
20089 /* Some arguments might still be unspecified or dependent. */
20090 bool dependent;
20091 ++processing_template_decl;
20092 dependent = any_dependent_template_arguments_p (targs);
20093 if (!dependent)
20094 --processing_template_decl;
20095 parm = tsubst (pattern, targs,
20096 explain_p ? tf_warning_or_error : tf_none,
20097 NULL_TREE);
20098 if (dependent)
20099 --processing_template_decl;
20100 if (parm == error_mark_node)
20101 return 1;
20103 else
20104 parm = pattern;
20106 /* Unify the pattern with the current argument. */
20107 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20108 explain_p))
20109 return 1;
20111 /* For each parameter pack, collect the deduced value. */
20112 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20114 int idx, level;
20115 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20117 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20118 TMPL_ARG (targs, level, idx);
20122 /* Verify that the results of unification with the parameter packs
20123 produce results consistent with what we've seen before, and make
20124 the deduced argument packs available. */
20125 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20127 tree old_pack = TREE_VALUE (pack);
20128 tree new_args = TREE_TYPE (pack);
20129 int i, len = TREE_VEC_LENGTH (new_args);
20130 int idx, level;
20131 bool nondeduced_p = false;
20133 /* By default keep the original deduced argument pack.
20134 If necessary, more specific code is going to update the
20135 resulting deduced argument later down in this function. */
20136 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20137 TMPL_ARG (targs, level, idx) = old_pack;
20139 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20140 actually deduce anything. */
20141 for (i = 0; i < len && !nondeduced_p; ++i)
20142 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20143 nondeduced_p = true;
20144 if (nondeduced_p)
20145 continue;
20147 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20149 /* If we had fewer function args than explicit template args,
20150 just use the explicits. */
20151 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20152 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20153 if (len < explicit_len)
20154 new_args = explicit_args;
20157 if (!old_pack)
20159 tree result;
20160 /* Build the deduced *_ARGUMENT_PACK. */
20161 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20163 result = make_node (NONTYPE_ARGUMENT_PACK);
20164 TREE_CONSTANT (result) = 1;
20166 else
20167 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20169 SET_ARGUMENT_PACK_ARGS (result, new_args);
20171 /* Note the deduced argument packs for this parameter
20172 pack. */
20173 TMPL_ARG (targs, level, idx) = result;
20175 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20176 && (ARGUMENT_PACK_ARGS (old_pack)
20177 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20179 /* We only had the explicitly-provided arguments before, but
20180 now we have a complete set of arguments. */
20181 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20183 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20184 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20185 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20187 else
20189 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20190 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20192 if (!comp_template_args (old_args, new_args,
20193 &bad_old_arg, &bad_new_arg))
20194 /* Inconsistent unification of this parameter pack. */
20195 return unify_parameter_pack_inconsistent (explain_p,
20196 bad_old_arg,
20197 bad_new_arg);
20201 return unify_success (explain_p);
20204 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20205 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20206 parameters and return value are as for unify. */
20208 static int
20209 unify_array_domain (tree tparms, tree targs,
20210 tree parm_dom, tree arg_dom,
20211 bool explain_p)
20213 tree parm_max;
20214 tree arg_max;
20215 bool parm_cst;
20216 bool arg_cst;
20218 /* Our representation of array types uses "N - 1" as the
20219 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20220 not an integer constant. We cannot unify arbitrarily
20221 complex expressions, so we eliminate the MINUS_EXPRs
20222 here. */
20223 parm_max = TYPE_MAX_VALUE (parm_dom);
20224 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20225 if (!parm_cst)
20227 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20228 parm_max = TREE_OPERAND (parm_max, 0);
20230 arg_max = TYPE_MAX_VALUE (arg_dom);
20231 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20232 if (!arg_cst)
20234 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20235 trying to unify the type of a variable with the type
20236 of a template parameter. For example:
20238 template <unsigned int N>
20239 void f (char (&) [N]);
20240 int g();
20241 void h(int i) {
20242 char a[g(i)];
20243 f(a);
20246 Here, the type of the ARG will be "int [g(i)]", and
20247 may be a SAVE_EXPR, etc. */
20248 if (TREE_CODE (arg_max) != MINUS_EXPR)
20249 return unify_vla_arg (explain_p, arg_dom);
20250 arg_max = TREE_OPERAND (arg_max, 0);
20253 /* If only one of the bounds used a MINUS_EXPR, compensate
20254 by adding one to the other bound. */
20255 if (parm_cst && !arg_cst)
20256 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20257 integer_type_node,
20258 parm_max,
20259 integer_one_node);
20260 else if (arg_cst && !parm_cst)
20261 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20262 integer_type_node,
20263 arg_max,
20264 integer_one_node);
20266 return unify (tparms, targs, parm_max, arg_max,
20267 UNIFY_ALLOW_INTEGER, explain_p);
20270 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20272 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20274 static pa_kind_t
20275 pa_kind (tree t)
20277 if (PACK_EXPANSION_P (t))
20278 t = PACK_EXPANSION_PATTERN (t);
20279 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20280 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20281 || DECL_TYPE_TEMPLATE_P (t))
20282 return pa_tmpl;
20283 else if (TYPE_P (t))
20284 return pa_type;
20285 else
20286 return pa_expr;
20289 /* Deduce the value of template parameters. TPARMS is the (innermost)
20290 set of template parameters to a template. TARGS is the bindings
20291 for those template parameters, as determined thus far; TARGS may
20292 include template arguments for outer levels of template parameters
20293 as well. PARM is a parameter to a template function, or a
20294 subcomponent of that parameter; ARG is the corresponding argument.
20295 This function attempts to match PARM with ARG in a manner
20296 consistent with the existing assignments in TARGS. If more values
20297 are deduced, then TARGS is updated.
20299 Returns 0 if the type deduction succeeds, 1 otherwise. The
20300 parameter STRICT is a bitwise or of the following flags:
20302 UNIFY_ALLOW_NONE:
20303 Require an exact match between PARM and ARG.
20304 UNIFY_ALLOW_MORE_CV_QUAL:
20305 Allow the deduced ARG to be more cv-qualified (by qualification
20306 conversion) than ARG.
20307 UNIFY_ALLOW_LESS_CV_QUAL:
20308 Allow the deduced ARG to be less cv-qualified than ARG.
20309 UNIFY_ALLOW_DERIVED:
20310 Allow the deduced ARG to be a template base class of ARG,
20311 or a pointer to a template base class of the type pointed to by
20312 ARG.
20313 UNIFY_ALLOW_INTEGER:
20314 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20315 case for more information.
20316 UNIFY_ALLOW_OUTER_LEVEL:
20317 This is the outermost level of a deduction. Used to determine validity
20318 of qualification conversions. A valid qualification conversion must
20319 have const qualified pointers leading up to the inner type which
20320 requires additional CV quals, except at the outer level, where const
20321 is not required [conv.qual]. It would be normal to set this flag in
20322 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20323 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20324 This is the outermost level of a deduction, and PARM can be more CV
20325 qualified at this point.
20326 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20327 This is the outermost level of a deduction, and PARM can be less CV
20328 qualified at this point. */
20330 static int
20331 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20332 bool explain_p)
20334 int idx;
20335 tree targ;
20336 tree tparm;
20337 int strict_in = strict;
20338 tsubst_flags_t complain = (explain_p
20339 ? tf_warning_or_error
20340 : tf_none);
20342 /* I don't think this will do the right thing with respect to types.
20343 But the only case I've seen it in so far has been array bounds, where
20344 signedness is the only information lost, and I think that will be
20345 okay. */
20346 while (CONVERT_EXPR_P (parm))
20347 parm = TREE_OPERAND (parm, 0);
20349 if (arg == error_mark_node)
20350 return unify_invalid (explain_p);
20351 if (arg == unknown_type_node
20352 || arg == init_list_type_node)
20353 /* We can't deduce anything from this, but we might get all the
20354 template args from other function args. */
20355 return unify_success (explain_p);
20357 if (parm == any_targ_node || arg == any_targ_node)
20358 return unify_success (explain_p);
20360 /* If PARM uses template parameters, then we can't bail out here,
20361 even if ARG == PARM, since we won't record unifications for the
20362 template parameters. We might need them if we're trying to
20363 figure out which of two things is more specialized. */
20364 if (arg == parm && !uses_template_parms (parm))
20365 return unify_success (explain_p);
20367 /* Handle init lists early, so the rest of the function can assume
20368 we're dealing with a type. */
20369 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20371 tree elt, elttype;
20372 unsigned i;
20373 tree orig_parm = parm;
20375 /* Replace T with std::initializer_list<T> for deduction. */
20376 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20377 && flag_deduce_init_list)
20378 parm = listify (parm);
20380 if (!is_std_init_list (parm)
20381 && TREE_CODE (parm) != ARRAY_TYPE)
20382 /* We can only deduce from an initializer list argument if the
20383 parameter is std::initializer_list or an array; otherwise this
20384 is a non-deduced context. */
20385 return unify_success (explain_p);
20387 if (TREE_CODE (parm) == ARRAY_TYPE)
20388 elttype = TREE_TYPE (parm);
20389 else
20391 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20392 /* Deduction is defined in terms of a single type, so just punt
20393 on the (bizarre) std::initializer_list<T...>. */
20394 if (PACK_EXPANSION_P (elttype))
20395 return unify_success (explain_p);
20398 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20400 int elt_strict = strict;
20402 if (elt == error_mark_node)
20403 return unify_invalid (explain_p);
20405 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20407 tree type = TREE_TYPE (elt);
20408 if (type == error_mark_node)
20409 return unify_invalid (explain_p);
20410 /* It should only be possible to get here for a call. */
20411 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20412 elt_strict |= maybe_adjust_types_for_deduction
20413 (DEDUCE_CALL, &elttype, &type, elt);
20414 elt = type;
20417 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20418 explain_p);
20421 if (TREE_CODE (parm) == ARRAY_TYPE
20422 && deducible_array_bound (TYPE_DOMAIN (parm)))
20424 /* Also deduce from the length of the initializer list. */
20425 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20426 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20427 if (idx == error_mark_node)
20428 return unify_invalid (explain_p);
20429 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20430 idx, explain_p);
20433 /* If the std::initializer_list<T> deduction worked, replace the
20434 deduced A with std::initializer_list<A>. */
20435 if (orig_parm != parm)
20437 idx = TEMPLATE_TYPE_IDX (orig_parm);
20438 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20439 targ = listify (targ);
20440 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20442 return unify_success (explain_p);
20445 /* If parm and arg aren't the same kind of thing (template, type, or
20446 expression), fail early. */
20447 if (pa_kind (parm) != pa_kind (arg))
20448 return unify_invalid (explain_p);
20450 /* Immediately reject some pairs that won't unify because of
20451 cv-qualification mismatches. */
20452 if (TREE_CODE (arg) == TREE_CODE (parm)
20453 && TYPE_P (arg)
20454 /* It is the elements of the array which hold the cv quals of an array
20455 type, and the elements might be template type parms. We'll check
20456 when we recurse. */
20457 && TREE_CODE (arg) != ARRAY_TYPE
20458 /* We check the cv-qualifiers when unifying with template type
20459 parameters below. We want to allow ARG `const T' to unify with
20460 PARM `T' for example, when computing which of two templates
20461 is more specialized, for example. */
20462 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20463 && !check_cv_quals_for_unify (strict_in, arg, parm))
20464 return unify_cv_qual_mismatch (explain_p, parm, arg);
20466 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20467 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20468 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20469 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20470 strict &= ~UNIFY_ALLOW_DERIVED;
20471 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20472 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20474 switch (TREE_CODE (parm))
20476 case TYPENAME_TYPE:
20477 case SCOPE_REF:
20478 case UNBOUND_CLASS_TEMPLATE:
20479 /* In a type which contains a nested-name-specifier, template
20480 argument values cannot be deduced for template parameters used
20481 within the nested-name-specifier. */
20482 return unify_success (explain_p);
20484 case TEMPLATE_TYPE_PARM:
20485 case TEMPLATE_TEMPLATE_PARM:
20486 case BOUND_TEMPLATE_TEMPLATE_PARM:
20487 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20488 if (error_operand_p (tparm))
20489 return unify_invalid (explain_p);
20491 if (TEMPLATE_TYPE_LEVEL (parm)
20492 != template_decl_level (tparm))
20493 /* The PARM is not one we're trying to unify. Just check
20494 to see if it matches ARG. */
20496 if (TREE_CODE (arg) == TREE_CODE (parm)
20497 && (is_auto (parm) ? is_auto (arg)
20498 : same_type_p (parm, arg)))
20499 return unify_success (explain_p);
20500 else
20501 return unify_type_mismatch (explain_p, parm, arg);
20503 idx = TEMPLATE_TYPE_IDX (parm);
20504 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20505 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20506 if (error_operand_p (tparm))
20507 return unify_invalid (explain_p);
20509 /* Check for mixed types and values. */
20510 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20511 && TREE_CODE (tparm) != TYPE_DECL)
20512 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20513 && TREE_CODE (tparm) != TEMPLATE_DECL))
20514 gcc_unreachable ();
20516 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20518 if ((strict_in & UNIFY_ALLOW_DERIVED)
20519 && CLASS_TYPE_P (arg))
20521 /* First try to match ARG directly. */
20522 tree t = try_class_unification (tparms, targs, parm, arg,
20523 explain_p);
20524 if (!t)
20526 /* Otherwise, look for a suitable base of ARG, as below. */
20527 enum template_base_result r;
20528 r = get_template_base (tparms, targs, parm, arg,
20529 explain_p, &t);
20530 if (!t)
20531 return unify_no_common_base (explain_p, r, parm, arg);
20532 arg = t;
20535 /* ARG must be constructed from a template class or a template
20536 template parameter. */
20537 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20538 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20539 return unify_template_deduction_failure (explain_p, parm, arg);
20541 /* Deduce arguments T, i from TT<T> or TT<i>. */
20542 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20543 return 1;
20545 arg = TYPE_TI_TEMPLATE (arg);
20547 /* Fall through to deduce template name. */
20550 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20551 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20553 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20555 /* Simple cases: Value already set, does match or doesn't. */
20556 if (targ != NULL_TREE && template_args_equal (targ, arg))
20557 return unify_success (explain_p);
20558 else if (targ)
20559 return unify_inconsistency (explain_p, parm, targ, arg);
20561 else
20563 /* If PARM is `const T' and ARG is only `int', we don't have
20564 a match unless we are allowing additional qualification.
20565 If ARG is `const int' and PARM is just `T' that's OK;
20566 that binds `const int' to `T'. */
20567 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20568 arg, parm))
20569 return unify_cv_qual_mismatch (explain_p, parm, arg);
20571 /* Consider the case where ARG is `const volatile int' and
20572 PARM is `const T'. Then, T should be `volatile int'. */
20573 arg = cp_build_qualified_type_real
20574 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20575 if (arg == error_mark_node)
20576 return unify_invalid (explain_p);
20578 /* Simple cases: Value already set, does match or doesn't. */
20579 if (targ != NULL_TREE && same_type_p (targ, arg))
20580 return unify_success (explain_p);
20581 else if (targ)
20582 return unify_inconsistency (explain_p, parm, targ, arg);
20584 /* Make sure that ARG is not a variable-sized array. (Note
20585 that were talking about variable-sized arrays (like
20586 `int[n]'), rather than arrays of unknown size (like
20587 `int[]').) We'll get very confused by such a type since
20588 the bound of the array is not constant, and therefore
20589 not mangleable. Besides, such types are not allowed in
20590 ISO C++, so we can do as we please here. We do allow
20591 them for 'auto' deduction, since that isn't ABI-exposed. */
20592 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20593 return unify_vla_arg (explain_p, arg);
20595 /* Strip typedefs as in convert_template_argument. */
20596 arg = canonicalize_type_argument (arg, tf_none);
20599 /* If ARG is a parameter pack or an expansion, we cannot unify
20600 against it unless PARM is also a parameter pack. */
20601 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20602 && !template_parameter_pack_p (parm))
20603 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20605 /* If the argument deduction results is a METHOD_TYPE,
20606 then there is a problem.
20607 METHOD_TYPE doesn't map to any real C++ type the result of
20608 the deduction can not be of that type. */
20609 if (TREE_CODE (arg) == METHOD_TYPE)
20610 return unify_method_type_error (explain_p, arg);
20612 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20613 return unify_success (explain_p);
20615 case TEMPLATE_PARM_INDEX:
20616 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20617 if (error_operand_p (tparm))
20618 return unify_invalid (explain_p);
20620 if (TEMPLATE_PARM_LEVEL (parm)
20621 != template_decl_level (tparm))
20623 /* The PARM is not one we're trying to unify. Just check
20624 to see if it matches ARG. */
20625 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20626 && cp_tree_equal (parm, arg));
20627 if (result)
20628 unify_expression_unequal (explain_p, parm, arg);
20629 return result;
20632 idx = TEMPLATE_PARM_IDX (parm);
20633 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20635 if (targ)
20637 int x = !cp_tree_equal (targ, arg);
20638 if (x)
20639 unify_inconsistency (explain_p, parm, targ, arg);
20640 return x;
20643 /* [temp.deduct.type] If, in the declaration of a function template
20644 with a non-type template-parameter, the non-type
20645 template-parameter is used in an expression in the function
20646 parameter-list and, if the corresponding template-argument is
20647 deduced, the template-argument type shall match the type of the
20648 template-parameter exactly, except that a template-argument
20649 deduced from an array bound may be of any integral type.
20650 The non-type parameter might use already deduced type parameters. */
20651 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20652 if (tree a = type_uses_auto (tparm))
20654 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20655 if (tparm == error_mark_node)
20656 return 1;
20659 if (!TREE_TYPE (arg))
20660 /* Template-parameter dependent expression. Just accept it for now.
20661 It will later be processed in convert_template_argument. */
20663 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20664 non_reference (tparm)))
20665 /* OK */;
20666 else if ((strict & UNIFY_ALLOW_INTEGER)
20667 && CP_INTEGRAL_TYPE_P (tparm))
20668 /* Convert the ARG to the type of PARM; the deduced non-type
20669 template argument must exactly match the types of the
20670 corresponding parameter. */
20671 arg = fold (build_nop (tparm, arg));
20672 else if (uses_template_parms (tparm))
20674 /* We haven't deduced the type of this parameter yet. */
20675 if (cxx_dialect >= cxx1z
20676 /* We deduce from array bounds in try_array_deduction. */
20677 && !(strict & UNIFY_ALLOW_INTEGER))
20679 /* Deduce it from the non-type argument. */
20680 tree atype = TREE_TYPE (arg);
20681 RECUR_AND_CHECK_FAILURE (tparms, targs,
20682 tparm, atype,
20683 UNIFY_ALLOW_NONE, explain_p);
20685 else
20686 /* Try again later. */
20687 return unify_success (explain_p);
20689 else
20690 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20692 /* If ARG is a parameter pack or an expansion, we cannot unify
20693 against it unless PARM is also a parameter pack. */
20694 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20695 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20696 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20699 bool removed_attr = false;
20700 arg = strip_typedefs_expr (arg, &removed_attr);
20702 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20703 return unify_success (explain_p);
20705 case PTRMEM_CST:
20707 /* A pointer-to-member constant can be unified only with
20708 another constant. */
20709 if (TREE_CODE (arg) != PTRMEM_CST)
20710 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20712 /* Just unify the class member. It would be useless (and possibly
20713 wrong, depending on the strict flags) to unify also
20714 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20715 arg refer to the same variable, even if through different
20716 classes. For instance:
20718 struct A { int x; };
20719 struct B : A { };
20721 Unification of &A::x and &B::x must succeed. */
20722 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20723 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20726 case POINTER_TYPE:
20728 if (!TYPE_PTR_P (arg))
20729 return unify_type_mismatch (explain_p, parm, arg);
20731 /* [temp.deduct.call]
20733 A can be another pointer or pointer to member type that can
20734 be converted to the deduced A via a qualification
20735 conversion (_conv.qual_).
20737 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20738 This will allow for additional cv-qualification of the
20739 pointed-to types if appropriate. */
20741 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20742 /* The derived-to-base conversion only persists through one
20743 level of pointers. */
20744 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20746 return unify (tparms, targs, TREE_TYPE (parm),
20747 TREE_TYPE (arg), strict, explain_p);
20750 case REFERENCE_TYPE:
20751 if (TREE_CODE (arg) != REFERENCE_TYPE)
20752 return unify_type_mismatch (explain_p, parm, arg);
20753 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20754 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20756 case ARRAY_TYPE:
20757 if (TREE_CODE (arg) != ARRAY_TYPE)
20758 return unify_type_mismatch (explain_p, parm, arg);
20759 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20760 != (TYPE_DOMAIN (arg) == NULL_TREE))
20761 return unify_type_mismatch (explain_p, parm, arg);
20762 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20763 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20764 if (TYPE_DOMAIN (parm) != NULL_TREE)
20765 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20766 TYPE_DOMAIN (arg), explain_p);
20767 return unify_success (explain_p);
20769 case REAL_TYPE:
20770 case COMPLEX_TYPE:
20771 case VECTOR_TYPE:
20772 case INTEGER_TYPE:
20773 case BOOLEAN_TYPE:
20774 case ENUMERAL_TYPE:
20775 case VOID_TYPE:
20776 case NULLPTR_TYPE:
20777 if (TREE_CODE (arg) != TREE_CODE (parm))
20778 return unify_type_mismatch (explain_p, parm, arg);
20780 /* We have already checked cv-qualification at the top of the
20781 function. */
20782 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20783 return unify_type_mismatch (explain_p, parm, arg);
20785 /* As far as unification is concerned, this wins. Later checks
20786 will invalidate it if necessary. */
20787 return unify_success (explain_p);
20789 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20790 /* Type INTEGER_CST can come from ordinary constant template args. */
20791 case INTEGER_CST:
20792 while (CONVERT_EXPR_P (arg))
20793 arg = TREE_OPERAND (arg, 0);
20795 if (TREE_CODE (arg) != INTEGER_CST)
20796 return unify_template_argument_mismatch (explain_p, parm, arg);
20797 return (tree_int_cst_equal (parm, arg)
20798 ? unify_success (explain_p)
20799 : unify_template_argument_mismatch (explain_p, parm, arg));
20801 case TREE_VEC:
20803 int i, len, argslen;
20804 int parm_variadic_p = 0;
20806 if (TREE_CODE (arg) != TREE_VEC)
20807 return unify_template_argument_mismatch (explain_p, parm, arg);
20809 len = TREE_VEC_LENGTH (parm);
20810 argslen = TREE_VEC_LENGTH (arg);
20812 /* Check for pack expansions in the parameters. */
20813 for (i = 0; i < len; ++i)
20815 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20817 if (i == len - 1)
20818 /* We can unify against something with a trailing
20819 parameter pack. */
20820 parm_variadic_p = 1;
20821 else
20822 /* [temp.deduct.type]/9: If the template argument list of
20823 P contains a pack expansion that is not the last
20824 template argument, the entire template argument list
20825 is a non-deduced context. */
20826 return unify_success (explain_p);
20830 /* If we don't have enough arguments to satisfy the parameters
20831 (not counting the pack expression at the end), or we have
20832 too many arguments for a parameter list that doesn't end in
20833 a pack expression, we can't unify. */
20834 if (parm_variadic_p
20835 ? argslen < len - parm_variadic_p
20836 : argslen != len)
20837 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20839 /* Unify all of the parameters that precede the (optional)
20840 pack expression. */
20841 for (i = 0; i < len - parm_variadic_p; ++i)
20843 RECUR_AND_CHECK_FAILURE (tparms, targs,
20844 TREE_VEC_ELT (parm, i),
20845 TREE_VEC_ELT (arg, i),
20846 UNIFY_ALLOW_NONE, explain_p);
20848 if (parm_variadic_p)
20849 return unify_pack_expansion (tparms, targs, parm, arg,
20850 DEDUCE_EXACT,
20851 /*subr=*/true, explain_p);
20852 return unify_success (explain_p);
20855 case RECORD_TYPE:
20856 case UNION_TYPE:
20857 if (TREE_CODE (arg) != TREE_CODE (parm))
20858 return unify_type_mismatch (explain_p, parm, arg);
20860 if (TYPE_PTRMEMFUNC_P (parm))
20862 if (!TYPE_PTRMEMFUNC_P (arg))
20863 return unify_type_mismatch (explain_p, parm, arg);
20865 return unify (tparms, targs,
20866 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20867 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20868 strict, explain_p);
20870 else if (TYPE_PTRMEMFUNC_P (arg))
20871 return unify_type_mismatch (explain_p, parm, arg);
20873 if (CLASSTYPE_TEMPLATE_INFO (parm))
20875 tree t = NULL_TREE;
20877 if (strict_in & UNIFY_ALLOW_DERIVED)
20879 /* First, we try to unify the PARM and ARG directly. */
20880 t = try_class_unification (tparms, targs,
20881 parm, arg, explain_p);
20883 if (!t)
20885 /* Fallback to the special case allowed in
20886 [temp.deduct.call]:
20888 If P is a class, and P has the form
20889 template-id, then A can be a derived class of
20890 the deduced A. Likewise, if P is a pointer to
20891 a class of the form template-id, A can be a
20892 pointer to a derived class pointed to by the
20893 deduced A. */
20894 enum template_base_result r;
20895 r = get_template_base (tparms, targs, parm, arg,
20896 explain_p, &t);
20898 if (!t)
20900 /* Don't give the derived diagnostic if we're
20901 already dealing with the same template. */
20902 bool same_template
20903 = (CLASSTYPE_TEMPLATE_INFO (arg)
20904 && (CLASSTYPE_TI_TEMPLATE (parm)
20905 == CLASSTYPE_TI_TEMPLATE (arg)));
20906 return unify_no_common_base (explain_p && !same_template,
20907 r, parm, arg);
20911 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20912 && (CLASSTYPE_TI_TEMPLATE (parm)
20913 == CLASSTYPE_TI_TEMPLATE (arg)))
20914 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20915 Then, we should unify `int' and `U'. */
20916 t = arg;
20917 else
20918 /* There's no chance of unification succeeding. */
20919 return unify_type_mismatch (explain_p, parm, arg);
20921 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20922 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20924 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20925 return unify_type_mismatch (explain_p, parm, arg);
20926 return unify_success (explain_p);
20928 case METHOD_TYPE:
20929 case FUNCTION_TYPE:
20931 unsigned int nargs;
20932 tree *args;
20933 tree a;
20934 unsigned int i;
20936 if (TREE_CODE (arg) != TREE_CODE (parm))
20937 return unify_type_mismatch (explain_p, parm, arg);
20939 /* CV qualifications for methods can never be deduced, they must
20940 match exactly. We need to check them explicitly here,
20941 because type_unification_real treats them as any other
20942 cv-qualified parameter. */
20943 if (TREE_CODE (parm) == METHOD_TYPE
20944 && (!check_cv_quals_for_unify
20945 (UNIFY_ALLOW_NONE,
20946 class_of_this_parm (arg),
20947 class_of_this_parm (parm))))
20948 return unify_cv_qual_mismatch (explain_p, parm, arg);
20949 if (TREE_CODE (arg) == FUNCTION_TYPE
20950 && type_memfn_quals (parm) != type_memfn_quals (arg))
20951 return unify_cv_qual_mismatch (explain_p, parm, arg);
20952 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20953 return unify_type_mismatch (explain_p, parm, arg);
20955 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20956 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20958 nargs = list_length (TYPE_ARG_TYPES (arg));
20959 args = XALLOCAVEC (tree, nargs);
20960 for (a = TYPE_ARG_TYPES (arg), i = 0;
20961 a != NULL_TREE && a != void_list_node;
20962 a = TREE_CHAIN (a), ++i)
20963 args[i] = TREE_VALUE (a);
20964 nargs = i;
20966 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20967 args, nargs, 1, DEDUCE_EXACT,
20968 LOOKUP_NORMAL, NULL, explain_p))
20969 return 1;
20971 if (flag_noexcept_type)
20973 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20974 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20975 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20976 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20977 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20978 && uses_template_parms (TREE_PURPOSE (pspec)))
20979 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20980 TREE_PURPOSE (aspec),
20981 UNIFY_ALLOW_NONE, explain_p);
20982 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20983 return unify_type_mismatch (explain_p, parm, arg);
20986 return 0;
20989 case OFFSET_TYPE:
20990 /* Unify a pointer to member with a pointer to member function, which
20991 deduces the type of the member as a function type. */
20992 if (TYPE_PTRMEMFUNC_P (arg))
20994 /* Check top-level cv qualifiers */
20995 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20996 return unify_cv_qual_mismatch (explain_p, parm, arg);
20998 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20999 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21000 UNIFY_ALLOW_NONE, explain_p);
21002 /* Determine the type of the function we are unifying against. */
21003 tree fntype = static_fn_type (arg);
21005 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21008 if (TREE_CODE (arg) != OFFSET_TYPE)
21009 return unify_type_mismatch (explain_p, parm, arg);
21010 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21011 TYPE_OFFSET_BASETYPE (arg),
21012 UNIFY_ALLOW_NONE, explain_p);
21013 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21014 strict, explain_p);
21016 case CONST_DECL:
21017 if (DECL_TEMPLATE_PARM_P (parm))
21018 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21019 if (arg != scalar_constant_value (parm))
21020 return unify_template_argument_mismatch (explain_p, parm, arg);
21021 return unify_success (explain_p);
21023 case FIELD_DECL:
21024 case TEMPLATE_DECL:
21025 /* Matched cases are handled by the ARG == PARM test above. */
21026 return unify_template_argument_mismatch (explain_p, parm, arg);
21028 case VAR_DECL:
21029 /* We might get a variable as a non-type template argument in parm if the
21030 corresponding parameter is type-dependent. Make any necessary
21031 adjustments based on whether arg is a reference. */
21032 if (CONSTANT_CLASS_P (arg))
21033 parm = fold_non_dependent_expr (parm);
21034 else if (REFERENCE_REF_P (arg))
21036 tree sub = TREE_OPERAND (arg, 0);
21037 STRIP_NOPS (sub);
21038 if (TREE_CODE (sub) == ADDR_EXPR)
21039 arg = TREE_OPERAND (sub, 0);
21041 /* Now use the normal expression code to check whether they match. */
21042 goto expr;
21044 case TYPE_ARGUMENT_PACK:
21045 case NONTYPE_ARGUMENT_PACK:
21046 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21047 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21049 case TYPEOF_TYPE:
21050 case DECLTYPE_TYPE:
21051 case UNDERLYING_TYPE:
21052 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21053 or UNDERLYING_TYPE nodes. */
21054 return unify_success (explain_p);
21056 case ERROR_MARK:
21057 /* Unification fails if we hit an error node. */
21058 return unify_invalid (explain_p);
21060 case INDIRECT_REF:
21061 if (REFERENCE_REF_P (parm))
21063 bool pexp = PACK_EXPANSION_P (arg);
21064 if (pexp)
21065 arg = PACK_EXPANSION_PATTERN (arg);
21066 if (REFERENCE_REF_P (arg))
21067 arg = TREE_OPERAND (arg, 0);
21068 if (pexp)
21069 arg = make_pack_expansion (arg);
21070 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21071 strict, explain_p);
21073 /* FALLTHRU */
21075 default:
21076 /* An unresolved overload is a nondeduced context. */
21077 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21078 return unify_success (explain_p);
21079 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21080 expr:
21081 /* We must be looking at an expression. This can happen with
21082 something like:
21084 template <int I>
21085 void foo(S<I>, S<I + 2>);
21087 This is a "nondeduced context":
21089 [deduct.type]
21091 The nondeduced contexts are:
21093 --A type that is a template-id in which one or more of
21094 the template-arguments is an expression that references
21095 a template-parameter.
21097 In these cases, we assume deduction succeeded, but don't
21098 actually infer any unifications. */
21100 if (!uses_template_parms (parm)
21101 && !template_args_equal (parm, arg))
21102 return unify_expression_unequal (explain_p, parm, arg);
21103 else
21104 return unify_success (explain_p);
21107 #undef RECUR_AND_CHECK_FAILURE
21109 /* Note that DECL can be defined in this translation unit, if
21110 required. */
21112 static void
21113 mark_definable (tree decl)
21115 tree clone;
21116 DECL_NOT_REALLY_EXTERN (decl) = 1;
21117 FOR_EACH_CLONE (clone, decl)
21118 DECL_NOT_REALLY_EXTERN (clone) = 1;
21121 /* Called if RESULT is explicitly instantiated, or is a member of an
21122 explicitly instantiated class. */
21124 void
21125 mark_decl_instantiated (tree result, int extern_p)
21127 SET_DECL_EXPLICIT_INSTANTIATION (result);
21129 /* If this entity has already been written out, it's too late to
21130 make any modifications. */
21131 if (TREE_ASM_WRITTEN (result))
21132 return;
21134 /* For anonymous namespace we don't need to do anything. */
21135 if (decl_anon_ns_mem_p (result))
21137 gcc_assert (!TREE_PUBLIC (result));
21138 return;
21141 if (TREE_CODE (result) != FUNCTION_DECL)
21142 /* The TREE_PUBLIC flag for function declarations will have been
21143 set correctly by tsubst. */
21144 TREE_PUBLIC (result) = 1;
21146 /* This might have been set by an earlier implicit instantiation. */
21147 DECL_COMDAT (result) = 0;
21149 if (extern_p)
21150 DECL_NOT_REALLY_EXTERN (result) = 0;
21151 else
21153 mark_definable (result);
21154 mark_needed (result);
21155 /* Always make artificials weak. */
21156 if (DECL_ARTIFICIAL (result) && flag_weak)
21157 comdat_linkage (result);
21158 /* For WIN32 we also want to put explicit instantiations in
21159 linkonce sections. */
21160 else if (TREE_PUBLIC (result))
21161 maybe_make_one_only (result);
21164 /* If EXTERN_P, then this function will not be emitted -- unless
21165 followed by an explicit instantiation, at which point its linkage
21166 will be adjusted. If !EXTERN_P, then this function will be
21167 emitted here. In neither circumstance do we want
21168 import_export_decl to adjust the linkage. */
21169 DECL_INTERFACE_KNOWN (result) = 1;
21172 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21173 important template arguments. If any are missing, we check whether
21174 they're important by using error_mark_node for substituting into any
21175 args that were used for partial ordering (the ones between ARGS and END)
21176 and seeing if it bubbles up. */
21178 static bool
21179 check_undeduced_parms (tree targs, tree args, tree end)
21181 bool found = false;
21182 int i;
21183 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21184 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21186 found = true;
21187 TREE_VEC_ELT (targs, i) = error_mark_node;
21189 if (found)
21191 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21192 if (substed == error_mark_node)
21193 return true;
21195 return false;
21198 /* Given two function templates PAT1 and PAT2, return:
21200 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21201 -1 if PAT2 is more specialized than PAT1.
21202 0 if neither is more specialized.
21204 LEN indicates the number of parameters we should consider
21205 (defaulted parameters should not be considered).
21207 The 1998 std underspecified function template partial ordering, and
21208 DR214 addresses the issue. We take pairs of arguments, one from
21209 each of the templates, and deduce them against each other. One of
21210 the templates will be more specialized if all the *other*
21211 template's arguments deduce against its arguments and at least one
21212 of its arguments *does* *not* deduce against the other template's
21213 corresponding argument. Deduction is done as for class templates.
21214 The arguments used in deduction have reference and top level cv
21215 qualifiers removed. Iff both arguments were originally reference
21216 types *and* deduction succeeds in both directions, an lvalue reference
21217 wins against an rvalue reference and otherwise the template
21218 with the more cv-qualified argument wins for that pairing (if
21219 neither is more cv-qualified, they both are equal). Unlike regular
21220 deduction, after all the arguments have been deduced in this way,
21221 we do *not* verify the deduced template argument values can be
21222 substituted into non-deduced contexts.
21224 The logic can be a bit confusing here, because we look at deduce1 and
21225 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21226 can find template arguments for pat1 to make arg1 look like arg2, that
21227 means that arg2 is at least as specialized as arg1. */
21230 more_specialized_fn (tree pat1, tree pat2, int len)
21232 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21233 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21234 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21235 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21236 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21237 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21238 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21239 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21240 tree origs1, origs2;
21241 bool lose1 = false;
21242 bool lose2 = false;
21244 /* Remove the this parameter from non-static member functions. If
21245 one is a non-static member function and the other is not a static
21246 member function, remove the first parameter from that function
21247 also. This situation occurs for operator functions where we
21248 locate both a member function (with this pointer) and non-member
21249 operator (with explicit first operand). */
21250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21252 len--; /* LEN is the number of significant arguments for DECL1 */
21253 args1 = TREE_CHAIN (args1);
21254 if (!DECL_STATIC_FUNCTION_P (decl2))
21255 args2 = TREE_CHAIN (args2);
21257 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21259 args2 = TREE_CHAIN (args2);
21260 if (!DECL_STATIC_FUNCTION_P (decl1))
21262 len--;
21263 args1 = TREE_CHAIN (args1);
21267 /* If only one is a conversion operator, they are unordered. */
21268 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21269 return 0;
21271 /* Consider the return type for a conversion function */
21272 if (DECL_CONV_FN_P (decl1))
21274 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21275 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21276 len++;
21279 processing_template_decl++;
21281 origs1 = args1;
21282 origs2 = args2;
21284 while (len--
21285 /* Stop when an ellipsis is seen. */
21286 && args1 != NULL_TREE && args2 != NULL_TREE)
21288 tree arg1 = TREE_VALUE (args1);
21289 tree arg2 = TREE_VALUE (args2);
21290 int deduce1, deduce2;
21291 int quals1 = -1;
21292 int quals2 = -1;
21293 int ref1 = 0;
21294 int ref2 = 0;
21296 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21297 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21299 /* When both arguments are pack expansions, we need only
21300 unify the patterns themselves. */
21301 arg1 = PACK_EXPANSION_PATTERN (arg1);
21302 arg2 = PACK_EXPANSION_PATTERN (arg2);
21304 /* This is the last comparison we need to do. */
21305 len = 0;
21308 /* DR 1847: If a particular P contains no template-parameters that
21309 participate in template argument deduction, that P is not used to
21310 determine the ordering. */
21311 if (!uses_deducible_template_parms (arg1)
21312 && !uses_deducible_template_parms (arg2))
21313 goto next;
21315 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21317 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21318 arg1 = TREE_TYPE (arg1);
21319 quals1 = cp_type_quals (arg1);
21322 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21324 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21325 arg2 = TREE_TYPE (arg2);
21326 quals2 = cp_type_quals (arg2);
21329 arg1 = TYPE_MAIN_VARIANT (arg1);
21330 arg2 = TYPE_MAIN_VARIANT (arg2);
21332 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21334 int i, len2 = remaining_arguments (args2);
21335 tree parmvec = make_tree_vec (1);
21336 tree argvec = make_tree_vec (len2);
21337 tree ta = args2;
21339 /* Setup the parameter vector, which contains only ARG1. */
21340 TREE_VEC_ELT (parmvec, 0) = arg1;
21342 /* Setup the argument vector, which contains the remaining
21343 arguments. */
21344 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21345 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21347 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21348 argvec, DEDUCE_EXACT,
21349 /*subr=*/true, /*explain_p=*/false)
21350 == 0);
21352 /* We cannot deduce in the other direction, because ARG1 is
21353 a pack expansion but ARG2 is not. */
21354 deduce2 = 0;
21356 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21358 int i, len1 = remaining_arguments (args1);
21359 tree parmvec = make_tree_vec (1);
21360 tree argvec = make_tree_vec (len1);
21361 tree ta = args1;
21363 /* Setup the parameter vector, which contains only ARG1. */
21364 TREE_VEC_ELT (parmvec, 0) = arg2;
21366 /* Setup the argument vector, which contains the remaining
21367 arguments. */
21368 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21369 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21371 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21372 argvec, DEDUCE_EXACT,
21373 /*subr=*/true, /*explain_p=*/false)
21374 == 0);
21376 /* We cannot deduce in the other direction, because ARG2 is
21377 a pack expansion but ARG1 is not.*/
21378 deduce1 = 0;
21381 else
21383 /* The normal case, where neither argument is a pack
21384 expansion. */
21385 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21386 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21387 == 0);
21388 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21389 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21390 == 0);
21393 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21394 arg2, then arg2 is not as specialized as arg1. */
21395 if (!deduce1)
21396 lose2 = true;
21397 if (!deduce2)
21398 lose1 = true;
21400 /* "If, for a given type, deduction succeeds in both directions
21401 (i.e., the types are identical after the transformations above)
21402 and both P and A were reference types (before being replaced with
21403 the type referred to above):
21404 - if the type from the argument template was an lvalue reference and
21405 the type from the parameter template was not, the argument type is
21406 considered to be more specialized than the other; otherwise,
21407 - if the type from the argument template is more cv-qualified
21408 than the type from the parameter template (as described above),
21409 the argument type is considered to be more specialized than the other;
21410 otherwise,
21411 - neither type is more specialized than the other." */
21413 if (deduce1 && deduce2)
21415 if (ref1 && ref2 && ref1 != ref2)
21417 if (ref1 > ref2)
21418 lose1 = true;
21419 else
21420 lose2 = true;
21422 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21424 if ((quals1 & quals2) == quals2)
21425 lose2 = true;
21426 if ((quals1 & quals2) == quals1)
21427 lose1 = true;
21431 if (lose1 && lose2)
21432 /* We've failed to deduce something in either direction.
21433 These must be unordered. */
21434 break;
21436 next:
21438 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21439 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21440 /* We have already processed all of the arguments in our
21441 handing of the pack expansion type. */
21442 len = 0;
21444 args1 = TREE_CHAIN (args1);
21445 args2 = TREE_CHAIN (args2);
21448 /* "In most cases, all template parameters must have values in order for
21449 deduction to succeed, but for partial ordering purposes a template
21450 parameter may remain without a value provided it is not used in the
21451 types being used for partial ordering."
21453 Thus, if we are missing any of the targs1 we need to substitute into
21454 origs1, then pat2 is not as specialized as pat1. This can happen when
21455 there is a nondeduced context. */
21456 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21457 lose2 = true;
21458 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21459 lose1 = true;
21461 processing_template_decl--;
21463 /* If both deductions succeed, the partial ordering selects the more
21464 constrained template. */
21465 if (!lose1 && !lose2)
21467 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21468 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21469 lose1 = !subsumes_constraints (c1, c2);
21470 lose2 = !subsumes_constraints (c2, c1);
21473 /* All things being equal, if the next argument is a pack expansion
21474 for one function but not for the other, prefer the
21475 non-variadic function. FIXME this is bogus; see c++/41958. */
21476 if (lose1 == lose2
21477 && args1 && TREE_VALUE (args1)
21478 && args2 && TREE_VALUE (args2))
21480 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21481 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21484 if (lose1 == lose2)
21485 return 0;
21486 else if (!lose1)
21487 return 1;
21488 else
21489 return -1;
21492 /* Determine which of two partial specializations of TMPL is more
21493 specialized.
21495 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21496 to the first partial specialization. The TREE_PURPOSE is the
21497 innermost set of template parameters for the partial
21498 specialization. PAT2 is similar, but for the second template.
21500 Return 1 if the first partial specialization is more specialized;
21501 -1 if the second is more specialized; 0 if neither is more
21502 specialized.
21504 See [temp.class.order] for information about determining which of
21505 two templates is more specialized. */
21507 static int
21508 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21510 tree targs;
21511 int winner = 0;
21512 bool any_deductions = false;
21514 tree tmpl1 = TREE_VALUE (pat1);
21515 tree tmpl2 = TREE_VALUE (pat2);
21516 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21517 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21519 /* Just like what happens for functions, if we are ordering between
21520 different template specializations, we may encounter dependent
21521 types in the arguments, and we need our dependency check functions
21522 to behave correctly. */
21523 ++processing_template_decl;
21524 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21525 if (targs)
21527 --winner;
21528 any_deductions = true;
21531 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21532 if (targs)
21534 ++winner;
21535 any_deductions = true;
21537 --processing_template_decl;
21539 /* If both deductions succeed, the partial ordering selects the more
21540 constrained template. */
21541 if (!winner && any_deductions)
21542 return more_constrained (tmpl1, tmpl2);
21544 /* In the case of a tie where at least one of the templates
21545 has a parameter pack at the end, the template with the most
21546 non-packed parameters wins. */
21547 if (winner == 0
21548 && any_deductions
21549 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21550 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21552 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21553 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21554 int len1 = TREE_VEC_LENGTH (args1);
21555 int len2 = TREE_VEC_LENGTH (args2);
21557 /* We don't count the pack expansion at the end. */
21558 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21559 --len1;
21560 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21561 --len2;
21563 if (len1 > len2)
21564 return 1;
21565 else if (len1 < len2)
21566 return -1;
21569 return winner;
21572 /* Return the template arguments that will produce the function signature
21573 DECL from the function template FN, with the explicit template
21574 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21575 also match. Return NULL_TREE if no satisfactory arguments could be
21576 found. */
21578 static tree
21579 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21581 int ntparms = DECL_NTPARMS (fn);
21582 tree targs = make_tree_vec (ntparms);
21583 tree decl_type = TREE_TYPE (decl);
21584 tree decl_arg_types;
21585 tree *args;
21586 unsigned int nargs, ix;
21587 tree arg;
21589 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21591 /* Never do unification on the 'this' parameter. */
21592 decl_arg_types = skip_artificial_parms_for (decl,
21593 TYPE_ARG_TYPES (decl_type));
21595 nargs = list_length (decl_arg_types);
21596 args = XALLOCAVEC (tree, nargs);
21597 for (arg = decl_arg_types, ix = 0;
21598 arg != NULL_TREE && arg != void_list_node;
21599 arg = TREE_CHAIN (arg), ++ix)
21600 args[ix] = TREE_VALUE (arg);
21602 if (fn_type_unification (fn, explicit_args, targs,
21603 args, ix,
21604 (check_rettype || DECL_CONV_FN_P (fn)
21605 ? TREE_TYPE (decl_type) : NULL_TREE),
21606 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21607 /*decltype*/false)
21608 == error_mark_node)
21609 return NULL_TREE;
21611 return targs;
21614 /* Return the innermost template arguments that, when applied to a partial
21615 specialization SPEC_TMPL of TMPL, yield the ARGS.
21617 For example, suppose we have:
21619 template <class T, class U> struct S {};
21620 template <class T> struct S<T*, int> {};
21622 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21623 partial specialization and the ARGS will be {double*, int}. The resulting
21624 vector will be {double}, indicating that `T' is bound to `double'. */
21626 static tree
21627 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21629 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21630 tree spec_args
21631 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21632 int i, ntparms = TREE_VEC_LENGTH (tparms);
21633 tree deduced_args;
21634 tree innermost_deduced_args;
21636 innermost_deduced_args = make_tree_vec (ntparms);
21637 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21639 deduced_args = copy_node (args);
21640 SET_TMPL_ARGS_LEVEL (deduced_args,
21641 TMPL_ARGS_DEPTH (deduced_args),
21642 innermost_deduced_args);
21644 else
21645 deduced_args = innermost_deduced_args;
21647 bool tried_array_deduction = (cxx_dialect < cxx1z);
21648 again:
21649 if (unify (tparms, deduced_args,
21650 INNERMOST_TEMPLATE_ARGS (spec_args),
21651 INNERMOST_TEMPLATE_ARGS (args),
21652 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21653 return NULL_TREE;
21655 for (i = 0; i < ntparms; ++i)
21656 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21658 if (!tried_array_deduction)
21660 try_array_deduction (tparms, innermost_deduced_args,
21661 INNERMOST_TEMPLATE_ARGS (spec_args));
21662 tried_array_deduction = true;
21663 if (TREE_VEC_ELT (innermost_deduced_args, i))
21664 goto again;
21666 return NULL_TREE;
21669 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21670 if (!push_tinst_level (tinst))
21672 excessive_deduction_depth = true;
21673 return NULL_TREE;
21676 /* Verify that nondeduced template arguments agree with the type
21677 obtained from argument deduction.
21679 For example:
21681 struct A { typedef int X; };
21682 template <class T, class U> struct C {};
21683 template <class T> struct C<T, typename T::X> {};
21685 Then with the instantiation `C<A, int>', we can deduce that
21686 `T' is `A' but unify () does not check whether `typename T::X'
21687 is `int'. */
21688 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21690 if (spec_args != error_mark_node)
21691 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21692 INNERMOST_TEMPLATE_ARGS (spec_args),
21693 tmpl, tf_none, false, false);
21695 pop_tinst_level ();
21697 if (spec_args == error_mark_node
21698 /* We only need to check the innermost arguments; the other
21699 arguments will always agree. */
21700 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21701 INNERMOST_TEMPLATE_ARGS (args)))
21702 return NULL_TREE;
21704 /* Now that we have bindings for all of the template arguments,
21705 ensure that the arguments deduced for the template template
21706 parameters have compatible template parameter lists. See the use
21707 of template_template_parm_bindings_ok_p in fn_type_unification
21708 for more information. */
21709 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21710 return NULL_TREE;
21712 return deduced_args;
21715 // Compare two function templates T1 and T2 by deducing bindings
21716 // from one against the other. If both deductions succeed, compare
21717 // constraints to see which is more constrained.
21718 static int
21719 more_specialized_inst (tree t1, tree t2)
21721 int fate = 0;
21722 int count = 0;
21724 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21726 --fate;
21727 ++count;
21730 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21732 ++fate;
21733 ++count;
21736 // If both deductions succeed, then one may be more constrained.
21737 if (count == 2 && fate == 0)
21738 fate = more_constrained (t1, t2);
21740 return fate;
21743 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21744 Return the TREE_LIST node with the most specialized template, if
21745 any. If there is no most specialized template, the error_mark_node
21746 is returned.
21748 Note that this function does not look at, or modify, the
21749 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21750 returned is one of the elements of INSTANTIATIONS, callers may
21751 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21752 and retrieve it from the value returned. */
21754 tree
21755 most_specialized_instantiation (tree templates)
21757 tree fn, champ;
21759 ++processing_template_decl;
21761 champ = templates;
21762 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21764 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21765 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21766 if (fate == -1)
21767 champ = fn;
21768 else if (!fate)
21770 /* Equally specialized, move to next function. If there
21771 is no next function, nothing's most specialized. */
21772 fn = TREE_CHAIN (fn);
21773 champ = fn;
21774 if (!fn)
21775 break;
21779 if (champ)
21780 /* Now verify that champ is better than everything earlier in the
21781 instantiation list. */
21782 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21783 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21785 champ = NULL_TREE;
21786 break;
21790 processing_template_decl--;
21792 if (!champ)
21793 return error_mark_node;
21795 return champ;
21798 /* If DECL is a specialization of some template, return the most
21799 general such template. Otherwise, returns NULL_TREE.
21801 For example, given:
21803 template <class T> struct S { template <class U> void f(U); };
21805 if TMPL is `template <class U> void S<int>::f(U)' this will return
21806 the full template. This function will not trace past partial
21807 specializations, however. For example, given in addition:
21809 template <class T> struct S<T*> { template <class U> void f(U); };
21811 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21812 `template <class T> template <class U> S<T*>::f(U)'. */
21814 tree
21815 most_general_template (tree decl)
21817 if (TREE_CODE (decl) != TEMPLATE_DECL)
21819 if (tree tinfo = get_template_info (decl))
21820 decl = TI_TEMPLATE (tinfo);
21821 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21822 template friend, or a FIELD_DECL for a capture pack. */
21823 if (TREE_CODE (decl) != TEMPLATE_DECL)
21824 return NULL_TREE;
21827 /* Look for more and more general templates. */
21828 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21830 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21831 (See cp-tree.h for details.) */
21832 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21833 break;
21835 if (CLASS_TYPE_P (TREE_TYPE (decl))
21836 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21837 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21838 break;
21840 /* Stop if we run into an explicitly specialized class template. */
21841 if (!DECL_NAMESPACE_SCOPE_P (decl)
21842 && DECL_CONTEXT (decl)
21843 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21844 break;
21846 decl = DECL_TI_TEMPLATE (decl);
21849 return decl;
21852 /* Return the most specialized of the template partial specializations
21853 which can produce TARGET, a specialization of some class or variable
21854 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21855 a TEMPLATE_DECL node corresponding to the partial specialization, while
21856 the TREE_PURPOSE is the set of template arguments that must be
21857 substituted into the template pattern in order to generate TARGET.
21859 If the choice of partial specialization is ambiguous, a diagnostic
21860 is issued, and the error_mark_node is returned. If there are no
21861 partial specializations matching TARGET, then NULL_TREE is
21862 returned, indicating that the primary template should be used. */
21864 static tree
21865 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21867 tree list = NULL_TREE;
21868 tree t;
21869 tree champ;
21870 int fate;
21871 bool ambiguous_p;
21872 tree outer_args = NULL_TREE;
21873 tree tmpl, args;
21875 if (TYPE_P (target))
21877 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21878 tmpl = TI_TEMPLATE (tinfo);
21879 args = TI_ARGS (tinfo);
21881 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21883 tmpl = TREE_OPERAND (target, 0);
21884 args = TREE_OPERAND (target, 1);
21886 else if (VAR_P (target))
21888 tree tinfo = DECL_TEMPLATE_INFO (target);
21889 tmpl = TI_TEMPLATE (tinfo);
21890 args = TI_ARGS (tinfo);
21892 else
21893 gcc_unreachable ();
21895 tree main_tmpl = most_general_template (tmpl);
21897 /* For determining which partial specialization to use, only the
21898 innermost args are interesting. */
21899 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21901 outer_args = strip_innermost_template_args (args, 1);
21902 args = INNERMOST_TEMPLATE_ARGS (args);
21905 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21907 tree spec_args;
21908 tree spec_tmpl = TREE_VALUE (t);
21910 if (outer_args)
21912 /* Substitute in the template args from the enclosing class. */
21913 ++processing_template_decl;
21914 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21915 --processing_template_decl;
21918 if (spec_tmpl == error_mark_node)
21919 return error_mark_node;
21921 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21922 if (spec_args)
21924 if (outer_args)
21925 spec_args = add_to_template_args (outer_args, spec_args);
21927 /* Keep the candidate only if the constraints are satisfied,
21928 or if we're not compiling with concepts. */
21929 if (!flag_concepts
21930 || constraints_satisfied_p (spec_tmpl, spec_args))
21932 list = tree_cons (spec_args, TREE_VALUE (t), list);
21933 TREE_TYPE (list) = TREE_TYPE (t);
21938 if (! list)
21939 return NULL_TREE;
21941 ambiguous_p = false;
21942 t = list;
21943 champ = t;
21944 t = TREE_CHAIN (t);
21945 for (; t; t = TREE_CHAIN (t))
21947 fate = more_specialized_partial_spec (tmpl, champ, t);
21948 if (fate == 1)
21950 else
21952 if (fate == 0)
21954 t = TREE_CHAIN (t);
21955 if (! t)
21957 ambiguous_p = true;
21958 break;
21961 champ = t;
21965 if (!ambiguous_p)
21966 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21968 fate = more_specialized_partial_spec (tmpl, champ, t);
21969 if (fate != 1)
21971 ambiguous_p = true;
21972 break;
21976 if (ambiguous_p)
21978 const char *str;
21979 char *spaces = NULL;
21980 if (!(complain & tf_error))
21981 return error_mark_node;
21982 if (TYPE_P (target))
21983 error ("ambiguous template instantiation for %q#T", target);
21984 else
21985 error ("ambiguous template instantiation for %q#D", target);
21986 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21987 for (t = list; t; t = TREE_CHAIN (t))
21989 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21990 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21991 "%s %#qS", spaces ? spaces : str, subst);
21992 spaces = spaces ? spaces : get_spaces (str);
21994 free (spaces);
21995 return error_mark_node;
21998 return champ;
22001 /* Explicitly instantiate DECL. */
22003 void
22004 do_decl_instantiation (tree decl, tree storage)
22006 tree result = NULL_TREE;
22007 int extern_p = 0;
22009 if (!decl || decl == error_mark_node)
22010 /* An error occurred, for which grokdeclarator has already issued
22011 an appropriate message. */
22012 return;
22013 else if (! DECL_LANG_SPECIFIC (decl))
22015 error ("explicit instantiation of non-template %q#D", decl);
22016 return;
22019 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22020 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22022 if (VAR_P (decl) && !var_templ)
22024 /* There is an asymmetry here in the way VAR_DECLs and
22025 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22026 the latter, the DECL we get back will be marked as a
22027 template instantiation, and the appropriate
22028 DECL_TEMPLATE_INFO will be set up. This does not happen for
22029 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22030 should handle VAR_DECLs as it currently handles
22031 FUNCTION_DECLs. */
22032 if (!DECL_CLASS_SCOPE_P (decl))
22034 error ("%qD is not a static data member of a class template", decl);
22035 return;
22037 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22038 if (!result || !VAR_P (result))
22040 error ("no matching template for %qD found", decl);
22041 return;
22043 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22045 error ("type %qT for explicit instantiation %qD does not match "
22046 "declared type %qT", TREE_TYPE (result), decl,
22047 TREE_TYPE (decl));
22048 return;
22051 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22053 error ("explicit instantiation of %q#D", decl);
22054 return;
22056 else
22057 result = decl;
22059 /* Check for various error cases. Note that if the explicit
22060 instantiation is valid the RESULT will currently be marked as an
22061 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22062 until we get here. */
22064 if (DECL_TEMPLATE_SPECIALIZATION (result))
22066 /* DR 259 [temp.spec].
22068 Both an explicit instantiation and a declaration of an explicit
22069 specialization shall not appear in a program unless the explicit
22070 instantiation follows a declaration of the explicit specialization.
22072 For a given set of template parameters, if an explicit
22073 instantiation of a template appears after a declaration of an
22074 explicit specialization for that template, the explicit
22075 instantiation has no effect. */
22076 return;
22078 else if (DECL_EXPLICIT_INSTANTIATION (result))
22080 /* [temp.spec]
22082 No program shall explicitly instantiate any template more
22083 than once.
22085 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22086 the first instantiation was `extern' and the second is not,
22087 and EXTERN_P for the opposite case. */
22088 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22089 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22090 /* If an "extern" explicit instantiation follows an ordinary
22091 explicit instantiation, the template is instantiated. */
22092 if (extern_p)
22093 return;
22095 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22097 error ("no matching template for %qD found", result);
22098 return;
22100 else if (!DECL_TEMPLATE_INFO (result))
22102 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22103 return;
22106 if (storage == NULL_TREE)
22108 else if (storage == ridpointers[(int) RID_EXTERN])
22110 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22111 pedwarn (input_location, OPT_Wpedantic,
22112 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22113 "instantiations");
22114 extern_p = 1;
22116 else
22117 error ("storage class %qD applied to template instantiation", storage);
22119 check_explicit_instantiation_namespace (result);
22120 mark_decl_instantiated (result, extern_p);
22121 if (! extern_p)
22122 instantiate_decl (result, /*defer_ok=*/true,
22123 /*expl_inst_class_mem_p=*/false);
22126 static void
22127 mark_class_instantiated (tree t, int extern_p)
22129 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22130 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22131 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22132 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22133 if (! extern_p)
22135 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22136 rest_of_type_compilation (t, 1);
22140 /* Called from do_type_instantiation through binding_table_foreach to
22141 do recursive instantiation for the type bound in ENTRY. */
22142 static void
22143 bt_instantiate_type_proc (binding_entry entry, void *data)
22145 tree storage = *(tree *) data;
22147 if (MAYBE_CLASS_TYPE_P (entry->type)
22148 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22149 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22152 /* Perform an explicit instantiation of template class T. STORAGE, if
22153 non-null, is the RID for extern, inline or static. COMPLAIN is
22154 nonzero if this is called from the parser, zero if called recursively,
22155 since the standard is unclear (as detailed below). */
22157 void
22158 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22160 int extern_p = 0;
22161 int nomem_p = 0;
22162 int static_p = 0;
22163 int previous_instantiation_extern_p = 0;
22165 if (TREE_CODE (t) == TYPE_DECL)
22166 t = TREE_TYPE (t);
22168 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22170 tree tmpl =
22171 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22172 if (tmpl)
22173 error ("explicit instantiation of non-class template %qD", tmpl);
22174 else
22175 error ("explicit instantiation of non-template type %qT", t);
22176 return;
22179 complete_type (t);
22181 if (!COMPLETE_TYPE_P (t))
22183 if (complain & tf_error)
22184 error ("explicit instantiation of %q#T before definition of template",
22186 return;
22189 if (storage != NULL_TREE)
22191 if (!in_system_header_at (input_location))
22193 if (storage == ridpointers[(int) RID_EXTERN])
22195 if (cxx_dialect == cxx98)
22196 pedwarn (input_location, OPT_Wpedantic,
22197 "ISO C++ 1998 forbids the use of %<extern%> on "
22198 "explicit instantiations");
22200 else
22201 pedwarn (input_location, OPT_Wpedantic,
22202 "ISO C++ forbids the use of %qE"
22203 " on explicit instantiations", storage);
22206 if (storage == ridpointers[(int) RID_INLINE])
22207 nomem_p = 1;
22208 else if (storage == ridpointers[(int) RID_EXTERN])
22209 extern_p = 1;
22210 else if (storage == ridpointers[(int) RID_STATIC])
22211 static_p = 1;
22212 else
22214 error ("storage class %qD applied to template instantiation",
22215 storage);
22216 extern_p = 0;
22220 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22222 /* DR 259 [temp.spec].
22224 Both an explicit instantiation and a declaration of an explicit
22225 specialization shall not appear in a program unless the explicit
22226 instantiation follows a declaration of the explicit specialization.
22228 For a given set of template parameters, if an explicit
22229 instantiation of a template appears after a declaration of an
22230 explicit specialization for that template, the explicit
22231 instantiation has no effect. */
22232 return;
22234 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22236 /* [temp.spec]
22238 No program shall explicitly instantiate any template more
22239 than once.
22241 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22242 instantiation was `extern'. If EXTERN_P then the second is.
22243 These cases are OK. */
22244 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22246 if (!previous_instantiation_extern_p && !extern_p
22247 && (complain & tf_error))
22248 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22250 /* If we've already instantiated the template, just return now. */
22251 if (!CLASSTYPE_INTERFACE_ONLY (t))
22252 return;
22255 check_explicit_instantiation_namespace (TYPE_NAME (t));
22256 mark_class_instantiated (t, extern_p);
22258 if (nomem_p)
22259 return;
22261 /* In contrast to implicit instantiation, where only the
22262 declarations, and not the definitions, of members are
22263 instantiated, we have here:
22265 [temp.explicit]
22267 The explicit instantiation of a class template specialization
22268 implies the instantiation of all of its members not
22269 previously explicitly specialized in the translation unit
22270 containing the explicit instantiation.
22272 Of course, we can't instantiate member template classes, since we
22273 don't have any arguments for them. Note that the standard is
22274 unclear on whether the instantiation of the members are
22275 *explicit* instantiations or not. However, the most natural
22276 interpretation is that it should be an explicit
22277 instantiation. */
22278 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22279 if ((VAR_P (fld)
22280 || (TREE_CODE (fld) == FUNCTION_DECL
22281 && !static_p
22282 && user_provided_p (fld)))
22283 && DECL_TEMPLATE_INSTANTIATION (fld))
22285 mark_decl_instantiated (fld, extern_p);
22286 if (! extern_p)
22287 instantiate_decl (fld, /*defer_ok=*/true,
22288 /*expl_inst_class_mem_p=*/true);
22291 if (CLASSTYPE_NESTED_UTDS (t))
22292 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22293 bt_instantiate_type_proc, &storage);
22296 /* Given a function DECL, which is a specialization of TMPL, modify
22297 DECL to be a re-instantiation of TMPL with the same template
22298 arguments. TMPL should be the template into which tsubst'ing
22299 should occur for DECL, not the most general template.
22301 One reason for doing this is a scenario like this:
22303 template <class T>
22304 void f(const T&, int i);
22306 void g() { f(3, 7); }
22308 template <class T>
22309 void f(const T& t, const int i) { }
22311 Note that when the template is first instantiated, with
22312 instantiate_template, the resulting DECL will have no name for the
22313 first parameter, and the wrong type for the second. So, when we go
22314 to instantiate the DECL, we regenerate it. */
22316 static void
22317 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22319 /* The arguments used to instantiate DECL, from the most general
22320 template. */
22321 tree code_pattern;
22323 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22325 /* Make sure that we can see identifiers, and compute access
22326 correctly. */
22327 push_access_scope (decl);
22329 if (TREE_CODE (decl) == FUNCTION_DECL)
22331 tree decl_parm;
22332 tree pattern_parm;
22333 tree specs;
22334 int args_depth;
22335 int parms_depth;
22337 args_depth = TMPL_ARGS_DEPTH (args);
22338 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22339 if (args_depth > parms_depth)
22340 args = get_innermost_template_args (args, parms_depth);
22342 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22343 args, tf_error, NULL_TREE,
22344 /*defer_ok*/false);
22345 if (specs && specs != error_mark_node)
22346 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22347 specs);
22349 /* Merge parameter declarations. */
22350 decl_parm = skip_artificial_parms_for (decl,
22351 DECL_ARGUMENTS (decl));
22352 pattern_parm
22353 = skip_artificial_parms_for (code_pattern,
22354 DECL_ARGUMENTS (code_pattern));
22355 while (decl_parm && !DECL_PACK_P (pattern_parm))
22357 tree parm_type;
22358 tree attributes;
22360 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22361 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22362 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22363 NULL_TREE);
22364 parm_type = type_decays_to (parm_type);
22365 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22366 TREE_TYPE (decl_parm) = parm_type;
22367 attributes = DECL_ATTRIBUTES (pattern_parm);
22368 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22370 DECL_ATTRIBUTES (decl_parm) = attributes;
22371 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22373 decl_parm = DECL_CHAIN (decl_parm);
22374 pattern_parm = DECL_CHAIN (pattern_parm);
22376 /* Merge any parameters that match with the function parameter
22377 pack. */
22378 if (pattern_parm && DECL_PACK_P (pattern_parm))
22380 int i, len;
22381 tree expanded_types;
22382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22383 the parameters in this function parameter pack. */
22384 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22385 args, tf_error, NULL_TREE);
22386 len = TREE_VEC_LENGTH (expanded_types);
22387 for (i = 0; i < len; i++)
22389 tree parm_type;
22390 tree attributes;
22392 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22393 /* Rename the parameter to include the index. */
22394 DECL_NAME (decl_parm) =
22395 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22396 parm_type = TREE_VEC_ELT (expanded_types, i);
22397 parm_type = type_decays_to (parm_type);
22398 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22399 TREE_TYPE (decl_parm) = parm_type;
22400 attributes = DECL_ATTRIBUTES (pattern_parm);
22401 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22403 DECL_ATTRIBUTES (decl_parm) = attributes;
22404 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22406 decl_parm = DECL_CHAIN (decl_parm);
22409 /* Merge additional specifiers from the CODE_PATTERN. */
22410 if (DECL_DECLARED_INLINE_P (code_pattern)
22411 && !DECL_DECLARED_INLINE_P (decl))
22412 DECL_DECLARED_INLINE_P (decl) = 1;
22414 else if (VAR_P (decl))
22416 DECL_INITIAL (decl) =
22417 tsubst_expr (DECL_INITIAL (code_pattern), args,
22418 tf_error, DECL_TI_TEMPLATE (decl),
22419 /*integral_constant_expression_p=*/false);
22420 if (VAR_HAD_UNKNOWN_BOUND (decl))
22421 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22422 tf_error, DECL_TI_TEMPLATE (decl));
22424 else
22425 gcc_unreachable ();
22427 pop_access_scope (decl);
22430 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22431 substituted to get DECL. */
22433 tree
22434 template_for_substitution (tree decl)
22436 tree tmpl = DECL_TI_TEMPLATE (decl);
22438 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22439 for the instantiation. This is not always the most general
22440 template. Consider, for example:
22442 template <class T>
22443 struct S { template <class U> void f();
22444 template <> void f<int>(); };
22446 and an instantiation of S<double>::f<int>. We want TD to be the
22447 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22448 while (/* An instantiation cannot have a definition, so we need a
22449 more general template. */
22450 DECL_TEMPLATE_INSTANTIATION (tmpl)
22451 /* We must also deal with friend templates. Given:
22453 template <class T> struct S {
22454 template <class U> friend void f() {};
22457 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22458 so far as the language is concerned, but that's still
22459 where we get the pattern for the instantiation from. On
22460 other hand, if the definition comes outside the class, say:
22462 template <class T> struct S {
22463 template <class U> friend void f();
22465 template <class U> friend void f() {}
22467 we don't need to look any further. That's what the check for
22468 DECL_INITIAL is for. */
22469 || (TREE_CODE (decl) == FUNCTION_DECL
22470 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22471 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22473 /* The present template, TD, should not be a definition. If it
22474 were a definition, we should be using it! Note that we
22475 cannot restructure the loop to just keep going until we find
22476 a template with a definition, since that might go too far if
22477 a specialization was declared, but not defined. */
22479 /* Fetch the more general template. */
22480 tmpl = DECL_TI_TEMPLATE (tmpl);
22483 return tmpl;
22486 /* Returns true if we need to instantiate this template instance even if we
22487 know we aren't going to emit it. */
22489 bool
22490 always_instantiate_p (tree decl)
22492 /* We always instantiate inline functions so that we can inline them. An
22493 explicit instantiation declaration prohibits implicit instantiation of
22494 non-inline functions. With high levels of optimization, we would
22495 normally inline non-inline functions -- but we're not allowed to do
22496 that for "extern template" functions. Therefore, we check
22497 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22498 return ((TREE_CODE (decl) == FUNCTION_DECL
22499 && (DECL_DECLARED_INLINE_P (decl)
22500 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22501 /* And we need to instantiate static data members so that
22502 their initializers are available in integral constant
22503 expressions. */
22504 || (VAR_P (decl)
22505 && decl_maybe_constant_var_p (decl)));
22508 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22509 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22510 error, true otherwise. */
22512 bool
22513 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22515 tree fntype, spec, noex, clone;
22517 /* Don't instantiate a noexcept-specification from template context. */
22518 if (processing_template_decl)
22519 return true;
22521 if (DECL_CLONED_FUNCTION_P (fn))
22522 fn = DECL_CLONED_FUNCTION (fn);
22523 fntype = TREE_TYPE (fn);
22524 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22526 if (!spec || !TREE_PURPOSE (spec))
22527 return true;
22529 noex = TREE_PURPOSE (spec);
22531 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22533 static hash_set<tree>* fns = new hash_set<tree>;
22534 bool added = false;
22535 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22536 spec = get_defaulted_eh_spec (fn, complain);
22537 else if (!(added = !fns->add (fn)))
22539 /* If hash_set::add returns true, the element was already there. */
22540 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22541 DECL_SOURCE_LOCATION (fn));
22542 error_at (loc,
22543 "exception specification of %qD depends on itself",
22544 fn);
22545 spec = noexcept_false_spec;
22547 else if (push_tinst_level (fn))
22549 push_access_scope (fn);
22550 push_deferring_access_checks (dk_no_deferred);
22551 input_location = DECL_SOURCE_LOCATION (fn);
22552 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22553 DEFERRED_NOEXCEPT_ARGS (noex),
22554 tf_warning_or_error, fn,
22555 /*function_p=*/false,
22556 /*integral_constant_expression_p=*/true);
22557 pop_deferring_access_checks ();
22558 pop_access_scope (fn);
22559 pop_tinst_level ();
22560 spec = build_noexcept_spec (noex, tf_warning_or_error);
22561 if (spec == error_mark_node)
22562 spec = noexcept_false_spec;
22564 else
22565 spec = noexcept_false_spec;
22567 if (added)
22568 fns->remove (fn);
22570 if (spec == error_mark_node)
22571 return false;
22573 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22576 FOR_EACH_CLONE (clone, fn)
22578 if (TREE_TYPE (clone) == fntype)
22579 TREE_TYPE (clone) = TREE_TYPE (fn);
22580 else
22581 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22584 return true;
22587 /* Produce the definition of D, a _DECL generated from a template. If
22588 DEFER_OK is true, then we don't have to actually do the
22589 instantiation now; we just have to do it sometime. Normally it is
22590 an error if this is an explicit instantiation but D is undefined.
22591 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22592 instantiated class template. */
22594 tree
22595 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22597 tree tmpl = DECL_TI_TEMPLATE (d);
22598 tree gen_args;
22599 tree args;
22600 tree td;
22601 tree code_pattern;
22602 tree spec;
22603 tree gen_tmpl;
22604 bool pattern_defined;
22605 location_t saved_loc = input_location;
22606 int saved_unevaluated_operand = cp_unevaluated_operand;
22607 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22608 bool external_p;
22609 bool deleted_p;
22611 /* This function should only be used to instantiate templates for
22612 functions and static member variables. */
22613 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22615 /* A concept is never instantiated. */
22616 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22618 /* Variables are never deferred; if instantiation is required, they
22619 are instantiated right away. That allows for better code in the
22620 case that an expression refers to the value of the variable --
22621 if the variable has a constant value the referring expression can
22622 take advantage of that fact. */
22623 if (VAR_P (d))
22624 defer_ok = false;
22626 /* Don't instantiate cloned functions. Instead, instantiate the
22627 functions they cloned. */
22628 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22629 d = DECL_CLONED_FUNCTION (d);
22631 if (DECL_TEMPLATE_INSTANTIATED (d)
22632 || (TREE_CODE (d) == FUNCTION_DECL
22633 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22634 || DECL_TEMPLATE_SPECIALIZATION (d))
22635 /* D has already been instantiated or explicitly specialized, so
22636 there's nothing for us to do here.
22638 It might seem reasonable to check whether or not D is an explicit
22639 instantiation, and, if so, stop here. But when an explicit
22640 instantiation is deferred until the end of the compilation,
22641 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22642 the instantiation. */
22643 return d;
22645 /* Check to see whether we know that this template will be
22646 instantiated in some other file, as with "extern template"
22647 extension. */
22648 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22650 /* In general, we do not instantiate such templates. */
22651 if (external_p && !always_instantiate_p (d))
22652 return d;
22654 gen_tmpl = most_general_template (tmpl);
22655 gen_args = DECL_TI_ARGS (d);
22657 if (tmpl != gen_tmpl)
22658 /* We should already have the extra args. */
22659 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22660 == TMPL_ARGS_DEPTH (gen_args));
22661 /* And what's in the hash table should match D. */
22662 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22663 || spec == NULL_TREE);
22665 /* This needs to happen before any tsubsting. */
22666 if (! push_tinst_level (d))
22667 return d;
22669 timevar_push (TV_TEMPLATE_INST);
22671 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22672 for the instantiation. */
22673 td = template_for_substitution (d);
22674 args = gen_args;
22676 if (VAR_P (d))
22678 /* Look up an explicit specialization, if any. */
22679 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22680 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22681 if (elt && elt != error_mark_node)
22683 td = TREE_VALUE (elt);
22684 args = TREE_PURPOSE (elt);
22688 code_pattern = DECL_TEMPLATE_RESULT (td);
22690 /* We should never be trying to instantiate a member of a class
22691 template or partial specialization. */
22692 gcc_assert (d != code_pattern);
22694 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22695 || DECL_TEMPLATE_SPECIALIZATION (td))
22696 /* In the case of a friend template whose definition is provided
22697 outside the class, we may have too many arguments. Drop the
22698 ones we don't need. The same is true for specializations. */
22699 args = get_innermost_template_args
22700 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22702 if (TREE_CODE (d) == FUNCTION_DECL)
22704 deleted_p = DECL_DELETED_FN (code_pattern);
22705 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22706 && DECL_INITIAL (code_pattern) != error_mark_node)
22707 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22708 || deleted_p);
22710 else
22712 deleted_p = false;
22713 if (DECL_CLASS_SCOPE_P (code_pattern))
22714 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22715 || DECL_INLINE_VAR_P (code_pattern));
22716 else
22717 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22720 /* We may be in the middle of deferred access check. Disable it now. */
22721 push_deferring_access_checks (dk_no_deferred);
22723 /* Unless an explicit instantiation directive has already determined
22724 the linkage of D, remember that a definition is available for
22725 this entity. */
22726 if (pattern_defined
22727 && !DECL_INTERFACE_KNOWN (d)
22728 && !DECL_NOT_REALLY_EXTERN (d))
22729 mark_definable (d);
22731 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22732 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22733 input_location = DECL_SOURCE_LOCATION (d);
22735 /* If D is a member of an explicitly instantiated class template,
22736 and no definition is available, treat it like an implicit
22737 instantiation. */
22738 if (!pattern_defined && expl_inst_class_mem_p
22739 && DECL_EXPLICIT_INSTANTIATION (d))
22741 /* Leave linkage flags alone on instantiations with anonymous
22742 visibility. */
22743 if (TREE_PUBLIC (d))
22745 DECL_NOT_REALLY_EXTERN (d) = 0;
22746 DECL_INTERFACE_KNOWN (d) = 0;
22748 SET_DECL_IMPLICIT_INSTANTIATION (d);
22751 /* Defer all other templates, unless we have been explicitly
22752 forbidden from doing so. */
22753 if (/* If there is no definition, we cannot instantiate the
22754 template. */
22755 ! pattern_defined
22756 /* If it's OK to postpone instantiation, do so. */
22757 || defer_ok
22758 /* If this is a static data member that will be defined
22759 elsewhere, we don't want to instantiate the entire data
22760 member, but we do want to instantiate the initializer so that
22761 we can substitute that elsewhere. */
22762 || (external_p && VAR_P (d))
22763 /* Handle here a deleted function too, avoid generating
22764 its body (c++/61080). */
22765 || deleted_p)
22767 /* The definition of the static data member is now required so
22768 we must substitute the initializer. */
22769 if (VAR_P (d)
22770 && !DECL_INITIAL (d)
22771 && DECL_INITIAL (code_pattern))
22773 tree ns;
22774 tree init;
22775 bool const_init = false;
22776 bool enter_context = DECL_CLASS_SCOPE_P (d);
22778 ns = decl_namespace_context (d);
22779 push_nested_namespace (ns);
22780 if (enter_context)
22781 push_nested_class (DECL_CONTEXT (d));
22782 init = tsubst_expr (DECL_INITIAL (code_pattern),
22783 args,
22784 tf_warning_or_error, NULL_TREE,
22785 /*integral_constant_expression_p=*/false);
22786 /* If instantiating the initializer involved instantiating this
22787 again, don't call cp_finish_decl twice. */
22788 if (!DECL_INITIAL (d))
22790 /* Make sure the initializer is still constant, in case of
22791 circular dependency (template/instantiate6.C). */
22792 const_init
22793 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22794 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22795 /*asmspec_tree=*/NULL_TREE,
22796 LOOKUP_ONLYCONVERTING);
22798 if (enter_context)
22799 pop_nested_class ();
22800 pop_nested_namespace (ns);
22803 /* We restore the source position here because it's used by
22804 add_pending_template. */
22805 input_location = saved_loc;
22807 if (at_eof && !pattern_defined
22808 && DECL_EXPLICIT_INSTANTIATION (d)
22809 && DECL_NOT_REALLY_EXTERN (d))
22810 /* [temp.explicit]
22812 The definition of a non-exported function template, a
22813 non-exported member function template, or a non-exported
22814 member function or static data member of a class template
22815 shall be present in every translation unit in which it is
22816 explicitly instantiated. */
22817 permerror (input_location, "explicit instantiation of %qD "
22818 "but no definition available", d);
22820 /* If we're in unevaluated context, we just wanted to get the
22821 constant value; this isn't an odr use, so don't queue
22822 a full instantiation. */
22823 if (cp_unevaluated_operand != 0)
22824 goto out;
22825 /* ??? Historically, we have instantiated inline functions, even
22826 when marked as "extern template". */
22827 if (!(external_p && VAR_P (d)))
22828 add_pending_template (d);
22829 goto out;
22831 /* Tell the repository that D is available in this translation unit
22832 -- and see if it is supposed to be instantiated here. */
22833 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22835 /* In a PCH file, despite the fact that the repository hasn't
22836 requested instantiation in the PCH it is still possible that
22837 an instantiation will be required in a file that includes the
22838 PCH. */
22839 if (pch_file)
22840 add_pending_template (d);
22841 /* Instantiate inline functions so that the inliner can do its
22842 job, even though we'll not be emitting a copy of this
22843 function. */
22844 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22845 goto out;
22848 bool push_to_top, nested;
22849 tree fn_context;
22850 fn_context = decl_function_context (d);
22851 nested = current_function_decl != NULL_TREE;
22852 push_to_top = !(nested && fn_context == current_function_decl);
22854 vec<tree> omp_privatization_save;
22855 if (nested)
22856 save_omp_privatization_clauses (omp_privatization_save);
22858 if (push_to_top)
22859 push_to_top_level ();
22860 else
22862 push_function_context ();
22863 cp_unevaluated_operand = 0;
22864 c_inhibit_evaluation_warnings = 0;
22867 /* Mark D as instantiated so that recursive calls to
22868 instantiate_decl do not try to instantiate it again. */
22869 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22871 /* Regenerate the declaration in case the template has been modified
22872 by a subsequent redeclaration. */
22873 regenerate_decl_from_template (d, td, args);
22875 /* We already set the file and line above. Reset them now in case
22876 they changed as a result of calling regenerate_decl_from_template. */
22877 input_location = DECL_SOURCE_LOCATION (d);
22879 if (VAR_P (d))
22881 tree init;
22882 bool const_init = false;
22884 /* Clear out DECL_RTL; whatever was there before may not be right
22885 since we've reset the type of the declaration. */
22886 SET_DECL_RTL (d, NULL);
22887 DECL_IN_AGGR_P (d) = 0;
22889 /* The initializer is placed in DECL_INITIAL by
22890 regenerate_decl_from_template so we don't need to
22891 push/pop_access_scope again here. Pull it out so that
22892 cp_finish_decl can process it. */
22893 init = DECL_INITIAL (d);
22894 DECL_INITIAL (d) = NULL_TREE;
22895 DECL_INITIALIZED_P (d) = 0;
22897 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22898 initializer. That function will defer actual emission until
22899 we have a chance to determine linkage. */
22900 DECL_EXTERNAL (d) = 0;
22902 /* Enter the scope of D so that access-checking works correctly. */
22903 bool enter_context = DECL_CLASS_SCOPE_P (d);
22904 if (enter_context)
22905 push_nested_class (DECL_CONTEXT (d));
22907 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22908 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22910 if (enter_context)
22911 pop_nested_class ();
22913 if (variable_template_p (gen_tmpl))
22914 note_variable_template_instantiation (d);
22916 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22917 synthesize_method (d);
22918 else if (TREE_CODE (d) == FUNCTION_DECL)
22920 hash_map<tree, tree> *saved_local_specializations;
22921 tree tmpl_parm;
22922 tree spec_parm;
22923 tree block = NULL_TREE;
22924 tree lambda_ctx = NULL_TREE;
22926 /* Save away the current list, in case we are instantiating one
22927 template from within the body of another. */
22928 saved_local_specializations = local_specializations;
22930 /* Set up the list of local specializations. */
22931 local_specializations = new hash_map<tree, tree>;
22933 /* Set up context. */
22934 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22935 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22936 block = push_stmt_list ();
22937 else
22939 if (push_to_top && LAMBDA_FUNCTION_P (d))
22941 /* When instantiating a lambda's templated function
22942 operator, we need to push the non-lambda class scope
22943 of the lambda itself so that the nested function
22944 stack is sufficiently correct to deal with this
22945 capture. */
22946 lambda_ctx = DECL_CONTEXT (d);
22948 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22949 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22950 if (lambda_ctx)
22951 push_nested_class (lambda_ctx);
22953 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22956 /* Some typedefs referenced from within the template code need to be
22957 access checked at template instantiation time, i.e now. These
22958 types were added to the template at parsing time. Let's get those
22959 and perform the access checks then. */
22960 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22961 args);
22963 /* Create substitution entries for the parameters. */
22964 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22965 spec_parm = DECL_ARGUMENTS (d);
22966 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22968 register_local_specialization (spec_parm, tmpl_parm);
22969 spec_parm = skip_artificial_parms_for (d, spec_parm);
22970 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22972 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22974 if (!DECL_PACK_P (tmpl_parm))
22976 register_local_specialization (spec_parm, tmpl_parm);
22977 spec_parm = DECL_CHAIN (spec_parm);
22979 else
22981 /* Register the (value) argument pack as a specialization of
22982 TMPL_PARM, then move on. */
22983 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22984 register_local_specialization (argpack, tmpl_parm);
22987 gcc_assert (!spec_parm);
22989 /* Substitute into the body of the function. */
22990 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22991 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22992 tf_warning_or_error, tmpl);
22993 else
22995 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22996 tf_warning_or_error, tmpl,
22997 /*integral_constant_expression_p=*/false);
22999 /* Set the current input_location to the end of the function
23000 so that finish_function knows where we are. */
23001 input_location
23002 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23004 /* Remember if we saw an infinite loop in the template. */
23005 current_function_infinite_loop
23006 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23009 /* We don't need the local specializations any more. */
23010 delete local_specializations;
23011 local_specializations = saved_local_specializations;
23013 /* Finish the function. */
23014 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23015 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23016 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23017 else
23019 d = finish_function (0);
23020 expand_or_defer_fn (d);
23022 if (lambda_ctx)
23023 pop_nested_class ();
23025 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23026 cp_check_omp_declare_reduction (d);
23029 /* We're not deferring instantiation any more. */
23030 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23032 if (push_to_top)
23033 pop_from_top_level ();
23034 else
23035 pop_function_context ();
23037 if (nested)
23038 restore_omp_privatization_clauses (omp_privatization_save);
23040 out:
23041 pop_deferring_access_checks ();
23042 timevar_pop (TV_TEMPLATE_INST);
23043 pop_tinst_level ();
23044 input_location = saved_loc;
23045 cp_unevaluated_operand = saved_unevaluated_operand;
23046 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23048 return d;
23051 /* Run through the list of templates that we wish we could
23052 instantiate, and instantiate any we can. RETRIES is the
23053 number of times we retry pending template instantiation. */
23055 void
23056 instantiate_pending_templates (int retries)
23058 int reconsider;
23059 location_t saved_loc = input_location;
23061 /* Instantiating templates may trigger vtable generation. This in turn
23062 may require further template instantiations. We place a limit here
23063 to avoid infinite loop. */
23064 if (pending_templates && retries >= max_tinst_depth)
23066 tree decl = pending_templates->tinst->decl;
23068 fatal_error (input_location,
23069 "template instantiation depth exceeds maximum of %d"
23070 " instantiating %q+D, possibly from virtual table generation"
23071 " (use -ftemplate-depth= to increase the maximum)",
23072 max_tinst_depth, decl);
23073 if (TREE_CODE (decl) == FUNCTION_DECL)
23074 /* Pretend that we defined it. */
23075 DECL_INITIAL (decl) = error_mark_node;
23076 return;
23081 struct pending_template **t = &pending_templates;
23082 struct pending_template *last = NULL;
23083 reconsider = 0;
23084 while (*t)
23086 tree instantiation = reopen_tinst_level ((*t)->tinst);
23087 bool complete = false;
23089 if (TYPE_P (instantiation))
23091 if (!COMPLETE_TYPE_P (instantiation))
23093 instantiate_class_template (instantiation);
23094 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23095 for (tree fld = TYPE_FIELDS (instantiation);
23096 fld; fld = TREE_CHAIN (fld))
23097 if ((VAR_P (fld)
23098 || (TREE_CODE (fld) == FUNCTION_DECL
23099 && !DECL_ARTIFICIAL (fld)))
23100 && DECL_TEMPLATE_INSTANTIATION (fld))
23101 instantiate_decl (fld,
23102 /*defer_ok=*/false,
23103 /*expl_inst_class_mem_p=*/false);
23105 if (COMPLETE_TYPE_P (instantiation))
23106 reconsider = 1;
23109 complete = COMPLETE_TYPE_P (instantiation);
23111 else
23113 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23114 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23116 instantiation
23117 = instantiate_decl (instantiation,
23118 /*defer_ok=*/false,
23119 /*expl_inst_class_mem_p=*/false);
23120 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23121 reconsider = 1;
23124 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23125 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23128 if (complete)
23129 /* If INSTANTIATION has been instantiated, then we don't
23130 need to consider it again in the future. */
23131 *t = (*t)->next;
23132 else
23134 last = *t;
23135 t = &(*t)->next;
23137 tinst_depth = 0;
23138 current_tinst_level = NULL;
23140 last_pending_template = last;
23142 while (reconsider);
23144 input_location = saved_loc;
23147 /* Substitute ARGVEC into T, which is a list of initializers for
23148 either base class or a non-static data member. The TREE_PURPOSEs
23149 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23150 instantiate_decl. */
23152 static tree
23153 tsubst_initializer_list (tree t, tree argvec)
23155 tree inits = NULL_TREE;
23157 for (; t; t = TREE_CHAIN (t))
23159 tree decl;
23160 tree init;
23161 tree expanded_bases = NULL_TREE;
23162 tree expanded_arguments = NULL_TREE;
23163 int i, len = 1;
23165 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23167 tree expr;
23168 tree arg;
23170 /* Expand the base class expansion type into separate base
23171 classes. */
23172 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23173 tf_warning_or_error,
23174 NULL_TREE);
23175 if (expanded_bases == error_mark_node)
23176 continue;
23178 /* We'll be building separate TREE_LISTs of arguments for
23179 each base. */
23180 len = TREE_VEC_LENGTH (expanded_bases);
23181 expanded_arguments = make_tree_vec (len);
23182 for (i = 0; i < len; i++)
23183 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23185 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23186 expand each argument in the TREE_VALUE of t. */
23187 expr = make_node (EXPR_PACK_EXPANSION);
23188 PACK_EXPANSION_LOCAL_P (expr) = true;
23189 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23190 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23192 if (TREE_VALUE (t) == void_type_node)
23193 /* VOID_TYPE_NODE is used to indicate
23194 value-initialization. */
23196 for (i = 0; i < len; i++)
23197 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23199 else
23201 /* Substitute parameter packs into each argument in the
23202 TREE_LIST. */
23203 in_base_initializer = 1;
23204 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23206 tree expanded_exprs;
23208 /* Expand the argument. */
23209 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23210 expanded_exprs
23211 = tsubst_pack_expansion (expr, argvec,
23212 tf_warning_or_error,
23213 NULL_TREE);
23214 if (expanded_exprs == error_mark_node)
23215 continue;
23217 /* Prepend each of the expanded expressions to the
23218 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23219 for (i = 0; i < len; i++)
23221 TREE_VEC_ELT (expanded_arguments, i) =
23222 tree_cons (NULL_TREE,
23223 TREE_VEC_ELT (expanded_exprs, i),
23224 TREE_VEC_ELT (expanded_arguments, i));
23227 in_base_initializer = 0;
23229 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23230 since we built them backwards. */
23231 for (i = 0; i < len; i++)
23233 TREE_VEC_ELT (expanded_arguments, i) =
23234 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23239 for (i = 0; i < len; ++i)
23241 if (expanded_bases)
23243 decl = TREE_VEC_ELT (expanded_bases, i);
23244 decl = expand_member_init (decl);
23245 init = TREE_VEC_ELT (expanded_arguments, i);
23247 else
23249 tree tmp;
23250 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23251 tf_warning_or_error, NULL_TREE);
23253 decl = expand_member_init (decl);
23254 if (decl && !DECL_P (decl))
23255 in_base_initializer = 1;
23257 init = TREE_VALUE (t);
23258 tmp = init;
23259 if (init != void_type_node)
23260 init = tsubst_expr (init, argvec,
23261 tf_warning_or_error, NULL_TREE,
23262 /*integral_constant_expression_p=*/false);
23263 if (init == NULL_TREE && tmp != NULL_TREE)
23264 /* If we had an initializer but it instantiated to nothing,
23265 value-initialize the object. This will only occur when
23266 the initializer was a pack expansion where the parameter
23267 packs used in that expansion were of length zero. */
23268 init = void_type_node;
23269 in_base_initializer = 0;
23272 if (decl)
23274 init = build_tree_list (decl, init);
23275 TREE_CHAIN (init) = inits;
23276 inits = init;
23280 return inits;
23283 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23285 static void
23286 set_current_access_from_decl (tree decl)
23288 if (TREE_PRIVATE (decl))
23289 current_access_specifier = access_private_node;
23290 else if (TREE_PROTECTED (decl))
23291 current_access_specifier = access_protected_node;
23292 else
23293 current_access_specifier = access_public_node;
23296 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23297 is the instantiation (which should have been created with
23298 start_enum) and ARGS are the template arguments to use. */
23300 static void
23301 tsubst_enum (tree tag, tree newtag, tree args)
23303 tree e;
23305 if (SCOPED_ENUM_P (newtag))
23306 begin_scope (sk_scoped_enum, newtag);
23308 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23310 tree value;
23311 tree decl;
23313 decl = TREE_VALUE (e);
23314 /* Note that in a template enum, the TREE_VALUE is the
23315 CONST_DECL, not the corresponding INTEGER_CST. */
23316 value = tsubst_expr (DECL_INITIAL (decl),
23317 args, tf_warning_or_error, NULL_TREE,
23318 /*integral_constant_expression_p=*/true);
23320 /* Give this enumeration constant the correct access. */
23321 set_current_access_from_decl (decl);
23323 /* Actually build the enumerator itself. Here we're assuming that
23324 enumerators can't have dependent attributes. */
23325 build_enumerator (DECL_NAME (decl), value, newtag,
23326 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23329 if (SCOPED_ENUM_P (newtag))
23330 finish_scope ();
23332 finish_enum_value_list (newtag);
23333 finish_enum (newtag);
23335 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23336 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23339 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23340 its type -- but without substituting the innermost set of template
23341 arguments. So, innermost set of template parameters will appear in
23342 the type. */
23344 tree
23345 get_mostly_instantiated_function_type (tree decl)
23347 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23348 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23351 /* Return truthvalue if we're processing a template different from
23352 the last one involved in diagnostics. */
23353 bool
23354 problematic_instantiation_changed (void)
23356 return current_tinst_level != last_error_tinst_level;
23359 /* Remember current template involved in diagnostics. */
23360 void
23361 record_last_problematic_instantiation (void)
23363 last_error_tinst_level = current_tinst_level;
23366 struct tinst_level *
23367 current_instantiation (void)
23369 return current_tinst_level;
23372 /* Return TRUE if current_function_decl is being instantiated, false
23373 otherwise. */
23375 bool
23376 instantiating_current_function_p (void)
23378 return (current_instantiation ()
23379 && current_instantiation ()->decl == current_function_decl);
23382 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23383 type. Return zero for ok, nonzero for disallowed. Issue error and
23384 warning messages under control of COMPLAIN. */
23386 static int
23387 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23389 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23390 return 0;
23391 else if (POINTER_TYPE_P (type))
23392 return 0;
23393 else if (TYPE_PTRMEM_P (type))
23394 return 0;
23395 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23396 return 0;
23397 else if (TREE_CODE (type) == TYPENAME_TYPE)
23398 return 0;
23399 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23400 return 0;
23401 else if (TREE_CODE (type) == NULLPTR_TYPE)
23402 return 0;
23403 /* A bound template template parm could later be instantiated to have a valid
23404 nontype parm type via an alias template. */
23405 else if (cxx_dialect >= cxx11
23406 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23407 return 0;
23409 if (complain & tf_error)
23411 if (type == error_mark_node)
23412 inform (input_location, "invalid template non-type parameter");
23413 else
23414 error ("%q#T is not a valid type for a template non-type parameter",
23415 type);
23417 return 1;
23420 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23421 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23423 static bool
23424 dependent_type_p_r (tree type)
23426 tree scope;
23428 /* [temp.dep.type]
23430 A type is dependent if it is:
23432 -- a template parameter. Template template parameters are types
23433 for us (since TYPE_P holds true for them) so we handle
23434 them here. */
23435 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23436 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23437 return true;
23438 /* -- a qualified-id with a nested-name-specifier which contains a
23439 class-name that names a dependent type or whose unqualified-id
23440 names a dependent type. */
23441 if (TREE_CODE (type) == TYPENAME_TYPE)
23442 return true;
23444 /* An alias template specialization can be dependent even if the
23445 resulting type is not. */
23446 if (dependent_alias_template_spec_p (type))
23447 return true;
23449 /* -- a cv-qualified type where the cv-unqualified type is
23450 dependent.
23451 No code is necessary for this bullet; the code below handles
23452 cv-qualified types, and we don't want to strip aliases with
23453 TYPE_MAIN_VARIANT because of DR 1558. */
23454 /* -- a compound type constructed from any dependent type. */
23455 if (TYPE_PTRMEM_P (type))
23456 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23457 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23458 (type)));
23459 else if (TYPE_PTR_P (type)
23460 || TREE_CODE (type) == REFERENCE_TYPE)
23461 return dependent_type_p (TREE_TYPE (type));
23462 else if (TREE_CODE (type) == FUNCTION_TYPE
23463 || TREE_CODE (type) == METHOD_TYPE)
23465 tree arg_type;
23467 if (dependent_type_p (TREE_TYPE (type)))
23468 return true;
23469 for (arg_type = TYPE_ARG_TYPES (type);
23470 arg_type;
23471 arg_type = TREE_CHAIN (arg_type))
23472 if (dependent_type_p (TREE_VALUE (arg_type)))
23473 return true;
23474 if (cxx_dialect >= cxx1z)
23476 /* A value-dependent noexcept-specifier makes the type dependent. */
23477 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23478 if (spec && TREE_PURPOSE (spec)
23479 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23480 return true;
23482 return false;
23484 /* -- an array type constructed from any dependent type or whose
23485 size is specified by a constant expression that is
23486 value-dependent.
23488 We checked for type- and value-dependence of the bounds in
23489 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23490 if (TREE_CODE (type) == ARRAY_TYPE)
23492 if (TYPE_DOMAIN (type)
23493 && dependent_type_p (TYPE_DOMAIN (type)))
23494 return true;
23495 return dependent_type_p (TREE_TYPE (type));
23498 /* -- a template-id in which either the template name is a template
23499 parameter ... */
23500 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23501 return true;
23502 /* ... or any of the template arguments is a dependent type or
23503 an expression that is type-dependent or value-dependent. */
23504 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23505 && (any_dependent_template_arguments_p
23506 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23507 return true;
23509 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23510 dependent; if the argument of the `typeof' expression is not
23511 type-dependent, then it should already been have resolved. */
23512 if (TREE_CODE (type) == TYPEOF_TYPE
23513 || TREE_CODE (type) == DECLTYPE_TYPE
23514 || TREE_CODE (type) == UNDERLYING_TYPE)
23515 return true;
23517 /* A template argument pack is dependent if any of its packed
23518 arguments are. */
23519 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23521 tree args = ARGUMENT_PACK_ARGS (type);
23522 int i, len = TREE_VEC_LENGTH (args);
23523 for (i = 0; i < len; ++i)
23524 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23525 return true;
23528 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23529 be template parameters. */
23530 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23531 return true;
23533 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23534 return true;
23536 /* The standard does not specifically mention types that are local
23537 to template functions or local classes, but they should be
23538 considered dependent too. For example:
23540 template <int I> void f() {
23541 enum E { a = I };
23542 S<sizeof (E)> s;
23545 The size of `E' cannot be known until the value of `I' has been
23546 determined. Therefore, `E' must be considered dependent. */
23547 scope = TYPE_CONTEXT (type);
23548 if (scope && TYPE_P (scope))
23549 return dependent_type_p (scope);
23550 /* Don't use type_dependent_expression_p here, as it can lead
23551 to infinite recursion trying to determine whether a lambda
23552 nested in a lambda is dependent (c++/47687). */
23553 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23554 && DECL_LANG_SPECIFIC (scope)
23555 && DECL_TEMPLATE_INFO (scope)
23556 && (any_dependent_template_arguments_p
23557 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23558 return true;
23560 /* Other types are non-dependent. */
23561 return false;
23564 /* Returns TRUE if TYPE is dependent, in the sense of
23565 [temp.dep.type]. Note that a NULL type is considered dependent. */
23567 bool
23568 dependent_type_p (tree type)
23570 /* If there are no template parameters in scope, then there can't be
23571 any dependent types. */
23572 if (!processing_template_decl)
23574 /* If we are not processing a template, then nobody should be
23575 providing us with a dependent type. */
23576 gcc_assert (type);
23577 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23578 return false;
23581 /* If the type is NULL, we have not computed a type for the entity
23582 in question; in that case, the type is dependent. */
23583 if (!type)
23584 return true;
23586 /* Erroneous types can be considered non-dependent. */
23587 if (type == error_mark_node)
23588 return false;
23590 /* Getting here with global_type_node means we improperly called this
23591 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23592 gcc_checking_assert (type != global_type_node);
23594 /* If we have not already computed the appropriate value for TYPE,
23595 do so now. */
23596 if (!TYPE_DEPENDENT_P_VALID (type))
23598 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23599 TYPE_DEPENDENT_P_VALID (type) = 1;
23602 return TYPE_DEPENDENT_P (type);
23605 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23606 lookup. In other words, a dependent type that is not the current
23607 instantiation. */
23609 bool
23610 dependent_scope_p (tree scope)
23612 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23613 && !currently_open_class (scope));
23616 /* T is a SCOPE_REF; return whether we need to consider it
23617 instantiation-dependent so that we can check access at instantiation
23618 time even though we know which member it resolves to. */
23620 static bool
23621 instantiation_dependent_scope_ref_p (tree t)
23623 if (DECL_P (TREE_OPERAND (t, 1))
23624 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23625 && accessible_in_template_p (TREE_OPERAND (t, 0),
23626 TREE_OPERAND (t, 1)))
23627 return false;
23628 else
23629 return true;
23632 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23633 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23634 expression. */
23636 /* Note that this predicate is not appropriate for general expressions;
23637 only constant expressions (that satisfy potential_constant_expression)
23638 can be tested for value dependence. */
23640 bool
23641 value_dependent_expression_p (tree expression)
23643 if (!processing_template_decl || expression == NULL_TREE)
23644 return false;
23646 /* A name declared with a dependent type. */
23647 if (DECL_P (expression) && type_dependent_expression_p (expression))
23648 return true;
23650 switch (TREE_CODE (expression))
23652 case BASELINK:
23653 /* A dependent member function of the current instantiation. */
23654 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23656 case FUNCTION_DECL:
23657 /* A dependent member function of the current instantiation. */
23658 if (DECL_CLASS_SCOPE_P (expression)
23659 && dependent_type_p (DECL_CONTEXT (expression)))
23660 return true;
23661 break;
23663 case IDENTIFIER_NODE:
23664 /* A name that has not been looked up -- must be dependent. */
23665 return true;
23667 case TEMPLATE_PARM_INDEX:
23668 /* A non-type template parm. */
23669 return true;
23671 case CONST_DECL:
23672 /* A non-type template parm. */
23673 if (DECL_TEMPLATE_PARM_P (expression))
23674 return true;
23675 return value_dependent_expression_p (DECL_INITIAL (expression));
23677 case VAR_DECL:
23678 /* A constant with literal type and is initialized
23679 with an expression that is value-dependent.
23681 Note that a non-dependent parenthesized initializer will have
23682 already been replaced with its constant value, so if we see
23683 a TREE_LIST it must be dependent. */
23684 if (DECL_INITIAL (expression)
23685 && decl_constant_var_p (expression)
23686 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23687 /* cp_finish_decl doesn't fold reference initializers. */
23688 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23689 || type_dependent_expression_p (DECL_INITIAL (expression))
23690 || value_dependent_expression_p (DECL_INITIAL (expression))))
23691 return true;
23692 if (DECL_HAS_VALUE_EXPR_P (expression))
23694 tree value_expr = DECL_VALUE_EXPR (expression);
23695 if (type_dependent_expression_p (value_expr))
23696 return true;
23698 return false;
23700 case DYNAMIC_CAST_EXPR:
23701 case STATIC_CAST_EXPR:
23702 case CONST_CAST_EXPR:
23703 case REINTERPRET_CAST_EXPR:
23704 case CAST_EXPR:
23705 /* These expressions are value-dependent if the type to which
23706 the cast occurs is dependent or the expression being casted
23707 is value-dependent. */
23709 tree type = TREE_TYPE (expression);
23711 if (dependent_type_p (type))
23712 return true;
23714 /* A functional cast has a list of operands. */
23715 expression = TREE_OPERAND (expression, 0);
23716 if (!expression)
23718 /* If there are no operands, it must be an expression such
23719 as "int()". This should not happen for aggregate types
23720 because it would form non-constant expressions. */
23721 gcc_assert (cxx_dialect >= cxx11
23722 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23724 return false;
23727 if (TREE_CODE (expression) == TREE_LIST)
23728 return any_value_dependent_elements_p (expression);
23730 return value_dependent_expression_p (expression);
23733 case SIZEOF_EXPR:
23734 if (SIZEOF_EXPR_TYPE_P (expression))
23735 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23736 /* FALLTHRU */
23737 case ALIGNOF_EXPR:
23738 case TYPEID_EXPR:
23739 /* A `sizeof' expression is value-dependent if the operand is
23740 type-dependent or is a pack expansion. */
23741 expression = TREE_OPERAND (expression, 0);
23742 if (PACK_EXPANSION_P (expression))
23743 return true;
23744 else if (TYPE_P (expression))
23745 return dependent_type_p (expression);
23746 return instantiation_dependent_uneval_expression_p (expression);
23748 case AT_ENCODE_EXPR:
23749 /* An 'encode' expression is value-dependent if the operand is
23750 type-dependent. */
23751 expression = TREE_OPERAND (expression, 0);
23752 return dependent_type_p (expression);
23754 case NOEXCEPT_EXPR:
23755 expression = TREE_OPERAND (expression, 0);
23756 return instantiation_dependent_uneval_expression_p (expression);
23758 case SCOPE_REF:
23759 /* All instantiation-dependent expressions should also be considered
23760 value-dependent. */
23761 return instantiation_dependent_scope_ref_p (expression);
23763 case COMPONENT_REF:
23764 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23765 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23767 case NONTYPE_ARGUMENT_PACK:
23768 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23769 is value-dependent. */
23771 tree values = ARGUMENT_PACK_ARGS (expression);
23772 int i, len = TREE_VEC_LENGTH (values);
23774 for (i = 0; i < len; ++i)
23775 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23776 return true;
23778 return false;
23781 case TRAIT_EXPR:
23783 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23784 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23785 || (type2 ? dependent_type_p (type2) : false));
23788 case MODOP_EXPR:
23789 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23790 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23792 case ARRAY_REF:
23793 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23794 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23796 case ADDR_EXPR:
23798 tree op = TREE_OPERAND (expression, 0);
23799 return (value_dependent_expression_p (op)
23800 || has_value_dependent_address (op));
23803 case REQUIRES_EXPR:
23804 /* Treat all requires-expressions as value-dependent so
23805 we don't try to fold them. */
23806 return true;
23808 case TYPE_REQ:
23809 return dependent_type_p (TREE_OPERAND (expression, 0));
23811 case CALL_EXPR:
23813 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23814 return true;
23815 tree fn = get_callee_fndecl (expression);
23816 int i, nargs;
23817 nargs = call_expr_nargs (expression);
23818 for (i = 0; i < nargs; ++i)
23820 tree op = CALL_EXPR_ARG (expression, i);
23821 /* In a call to a constexpr member function, look through the
23822 implicit ADDR_EXPR on the object argument so that it doesn't
23823 cause the call to be considered value-dependent. We also
23824 look through it in potential_constant_expression. */
23825 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23826 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23827 && TREE_CODE (op) == ADDR_EXPR)
23828 op = TREE_OPERAND (op, 0);
23829 if (value_dependent_expression_p (op))
23830 return true;
23832 return false;
23835 case TEMPLATE_ID_EXPR:
23836 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23837 type-dependent. */
23838 return type_dependent_expression_p (expression)
23839 || variable_concept_p (TREE_OPERAND (expression, 0));
23841 case CONSTRUCTOR:
23843 unsigned ix;
23844 tree val;
23845 if (dependent_type_p (TREE_TYPE (expression)))
23846 return true;
23847 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23848 if (value_dependent_expression_p (val))
23849 return true;
23850 return false;
23853 case STMT_EXPR:
23854 /* Treat a GNU statement expression as dependent to avoid crashing
23855 under instantiate_non_dependent_expr; it can't be constant. */
23856 return true;
23858 default:
23859 /* A constant expression is value-dependent if any subexpression is
23860 value-dependent. */
23861 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23863 case tcc_reference:
23864 case tcc_unary:
23865 case tcc_comparison:
23866 case tcc_binary:
23867 case tcc_expression:
23868 case tcc_vl_exp:
23870 int i, len = cp_tree_operand_length (expression);
23872 for (i = 0; i < len; i++)
23874 tree t = TREE_OPERAND (expression, i);
23876 /* In some cases, some of the operands may be missing.
23877 (For example, in the case of PREDECREMENT_EXPR, the
23878 amount to increment by may be missing.) That doesn't
23879 make the expression dependent. */
23880 if (t && value_dependent_expression_p (t))
23881 return true;
23884 break;
23885 default:
23886 break;
23888 break;
23891 /* The expression is not value-dependent. */
23892 return false;
23895 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23896 [temp.dep.expr]. Note that an expression with no type is
23897 considered dependent. Other parts of the compiler arrange for an
23898 expression with type-dependent subexpressions to have no type, so
23899 this function doesn't have to be fully recursive. */
23901 bool
23902 type_dependent_expression_p (tree expression)
23904 if (!processing_template_decl)
23905 return false;
23907 if (expression == NULL_TREE || expression == error_mark_node)
23908 return false;
23910 /* An unresolved name is always dependent. */
23911 if (identifier_p (expression)
23912 || TREE_CODE (expression) == USING_DECL
23913 || TREE_CODE (expression) == WILDCARD_DECL)
23914 return true;
23916 /* A fold expression is type-dependent. */
23917 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23918 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23919 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23920 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23921 return true;
23923 /* Some expression forms are never type-dependent. */
23924 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23925 || TREE_CODE (expression) == SIZEOF_EXPR
23926 || TREE_CODE (expression) == ALIGNOF_EXPR
23927 || TREE_CODE (expression) == AT_ENCODE_EXPR
23928 || TREE_CODE (expression) == NOEXCEPT_EXPR
23929 || TREE_CODE (expression) == TRAIT_EXPR
23930 || TREE_CODE (expression) == TYPEID_EXPR
23931 || TREE_CODE (expression) == DELETE_EXPR
23932 || TREE_CODE (expression) == VEC_DELETE_EXPR
23933 || TREE_CODE (expression) == THROW_EXPR
23934 || TREE_CODE (expression) == REQUIRES_EXPR)
23935 return false;
23937 /* The types of these expressions depends only on the type to which
23938 the cast occurs. */
23939 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23940 || TREE_CODE (expression) == STATIC_CAST_EXPR
23941 || TREE_CODE (expression) == CONST_CAST_EXPR
23942 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23943 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23944 || TREE_CODE (expression) == CAST_EXPR)
23945 return dependent_type_p (TREE_TYPE (expression));
23947 /* The types of these expressions depends only on the type created
23948 by the expression. */
23949 if (TREE_CODE (expression) == NEW_EXPR
23950 || TREE_CODE (expression) == VEC_NEW_EXPR)
23952 /* For NEW_EXPR tree nodes created inside a template, either
23953 the object type itself or a TREE_LIST may appear as the
23954 operand 1. */
23955 tree type = TREE_OPERAND (expression, 1);
23956 if (TREE_CODE (type) == TREE_LIST)
23957 /* This is an array type. We need to check array dimensions
23958 as well. */
23959 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23960 || value_dependent_expression_p
23961 (TREE_OPERAND (TREE_VALUE (type), 1));
23962 else
23963 return dependent_type_p (type);
23966 if (TREE_CODE (expression) == SCOPE_REF)
23968 tree scope = TREE_OPERAND (expression, 0);
23969 tree name = TREE_OPERAND (expression, 1);
23971 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23972 contains an identifier associated by name lookup with one or more
23973 declarations declared with a dependent type, or...a
23974 nested-name-specifier or qualified-id that names a member of an
23975 unknown specialization. */
23976 return (type_dependent_expression_p (name)
23977 || dependent_scope_p (scope));
23980 if (TREE_CODE (expression) == TEMPLATE_DECL
23981 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23982 return uses_outer_template_parms (expression);
23984 if (TREE_CODE (expression) == STMT_EXPR)
23985 expression = stmt_expr_value_expr (expression);
23987 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23989 tree elt;
23990 unsigned i;
23992 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23994 if (type_dependent_expression_p (elt))
23995 return true;
23997 return false;
24000 /* A static data member of the current instantiation with incomplete
24001 array type is type-dependent, as the definition and specializations
24002 can have different bounds. */
24003 if (VAR_P (expression)
24004 && DECL_CLASS_SCOPE_P (expression)
24005 && dependent_type_p (DECL_CONTEXT (expression))
24006 && VAR_HAD_UNKNOWN_BOUND (expression))
24007 return true;
24009 /* An array of unknown bound depending on a variadic parameter, eg:
24011 template<typename... Args>
24012 void foo (Args... args)
24014 int arr[] = { args... };
24017 template<int... vals>
24018 void bar ()
24020 int arr[] = { vals... };
24023 If the array has no length and has an initializer, it must be that
24024 we couldn't determine its length in cp_complete_array_type because
24025 it is dependent. */
24026 if (VAR_P (expression)
24027 && TREE_TYPE (expression) != NULL_TREE
24028 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24029 && !TYPE_DOMAIN (TREE_TYPE (expression))
24030 && DECL_INITIAL (expression))
24031 return true;
24033 /* A function or variable template-id is type-dependent if it has any
24034 dependent template arguments. */
24035 if (VAR_OR_FUNCTION_DECL_P (expression)
24036 && DECL_LANG_SPECIFIC (expression)
24037 && DECL_TEMPLATE_INFO (expression))
24039 /* Consider the innermost template arguments, since those are the ones
24040 that come from the template-id; the template arguments for the
24041 enclosing class do not make it type-dependent unless they are used in
24042 the type of the decl. */
24043 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24044 && (any_dependent_template_arguments_p
24045 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24046 return true;
24048 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24049 type-dependent. Checking this is important for functions with auto
24050 return type, which looks like a dependent type. */
24051 if (TREE_CODE (expression) == FUNCTION_DECL
24052 && (!DECL_CLASS_SCOPE_P (expression)
24053 || !dependent_type_p (DECL_CONTEXT (expression)))
24054 && (!DECL_FRIEND_CONTEXT (expression)
24055 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24056 && !DECL_LOCAL_FUNCTION_P (expression))
24058 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24059 || undeduced_auto_decl (expression));
24060 return false;
24064 /* Always dependent, on the number of arguments if nothing else. */
24065 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24066 return true;
24068 if (TREE_TYPE (expression) == unknown_type_node)
24070 if (TREE_CODE (expression) == ADDR_EXPR)
24071 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24072 if (TREE_CODE (expression) == COMPONENT_REF
24073 || TREE_CODE (expression) == OFFSET_REF)
24075 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24076 return true;
24077 expression = TREE_OPERAND (expression, 1);
24078 if (identifier_p (expression))
24079 return false;
24081 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24082 if (TREE_CODE (expression) == SCOPE_REF)
24083 return false;
24085 if (BASELINK_P (expression))
24087 if (BASELINK_OPTYPE (expression)
24088 && dependent_type_p (BASELINK_OPTYPE (expression)))
24089 return true;
24090 expression = BASELINK_FUNCTIONS (expression);
24093 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24095 if (any_dependent_template_arguments_p
24096 (TREE_OPERAND (expression, 1)))
24097 return true;
24098 expression = TREE_OPERAND (expression, 0);
24099 if (identifier_p (expression))
24100 return true;
24103 gcc_assert (TREE_CODE (expression) == OVERLOAD
24104 || TREE_CODE (expression) == FUNCTION_DECL);
24106 for (lkp_iterator iter (expression); iter; ++iter)
24107 if (type_dependent_expression_p (*iter))
24108 return true;
24110 return false;
24113 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24115 /* Dependent type attributes might not have made it from the decl to
24116 the type yet. */
24117 if (DECL_P (expression)
24118 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24119 return true;
24121 return (dependent_type_p (TREE_TYPE (expression)));
24124 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24125 type-dependent if the expression refers to a member of the current
24126 instantiation and the type of the referenced member is dependent, or the
24127 class member access expression refers to a member of an unknown
24128 specialization.
24130 This function returns true if the OBJECT in such a class member access
24131 expression is of an unknown specialization. */
24133 bool
24134 type_dependent_object_expression_p (tree object)
24136 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24137 dependent. */
24138 if (TREE_CODE (object) == IDENTIFIER_NODE)
24139 return true;
24140 tree scope = TREE_TYPE (object);
24141 return (!scope || dependent_scope_p (scope));
24144 /* walk_tree callback function for instantiation_dependent_expression_p,
24145 below. Returns non-zero if a dependent subexpression is found. */
24147 static tree
24148 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24149 void * /*data*/)
24151 if (TYPE_P (*tp))
24153 /* We don't have to worry about decltype currently because decltype
24154 of an instantiation-dependent expr is a dependent type. This
24155 might change depending on the resolution of DR 1172. */
24156 *walk_subtrees = false;
24157 return NULL_TREE;
24159 enum tree_code code = TREE_CODE (*tp);
24160 switch (code)
24162 /* Don't treat an argument list as dependent just because it has no
24163 TREE_TYPE. */
24164 case TREE_LIST:
24165 case TREE_VEC:
24166 return NULL_TREE;
24168 case TEMPLATE_PARM_INDEX:
24169 return *tp;
24171 /* Handle expressions with type operands. */
24172 case SIZEOF_EXPR:
24173 case ALIGNOF_EXPR:
24174 case TYPEID_EXPR:
24175 case AT_ENCODE_EXPR:
24177 tree op = TREE_OPERAND (*tp, 0);
24178 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24179 op = TREE_TYPE (op);
24180 if (TYPE_P (op))
24182 if (dependent_type_p (op))
24183 return *tp;
24184 else
24186 *walk_subtrees = false;
24187 return NULL_TREE;
24190 break;
24193 case COMPONENT_REF:
24194 if (identifier_p (TREE_OPERAND (*tp, 1)))
24195 /* In a template, finish_class_member_access_expr creates a
24196 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24197 type-dependent, so that we can check access control at
24198 instantiation time (PR 42277). See also Core issue 1273. */
24199 return *tp;
24200 break;
24202 case SCOPE_REF:
24203 if (instantiation_dependent_scope_ref_p (*tp))
24204 return *tp;
24205 else
24206 break;
24208 /* Treat statement-expressions as dependent. */
24209 case BIND_EXPR:
24210 return *tp;
24212 /* Treat requires-expressions as dependent. */
24213 case REQUIRES_EXPR:
24214 return *tp;
24216 case CALL_EXPR:
24217 /* Treat calls to function concepts as dependent. */
24218 if (function_concept_check_p (*tp))
24219 return *tp;
24220 break;
24222 case TEMPLATE_ID_EXPR:
24223 /* And variable concepts. */
24224 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24225 return *tp;
24226 break;
24228 default:
24229 break;
24232 if (type_dependent_expression_p (*tp))
24233 return *tp;
24234 else
24235 return NULL_TREE;
24238 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24239 sense defined by the ABI:
24241 "An expression is instantiation-dependent if it is type-dependent
24242 or value-dependent, or it has a subexpression that is type-dependent
24243 or value-dependent."
24245 Except don't actually check value-dependence for unevaluated expressions,
24246 because in sizeof(i) we don't care about the value of i. Checking
24247 type-dependence will in turn check value-dependence of array bounds/template
24248 arguments as needed. */
24250 bool
24251 instantiation_dependent_uneval_expression_p (tree expression)
24253 tree result;
24255 if (!processing_template_decl)
24256 return false;
24258 if (expression == error_mark_node)
24259 return false;
24261 result = cp_walk_tree_without_duplicates (&expression,
24262 instantiation_dependent_r, NULL);
24263 return result != NULL_TREE;
24266 /* As above, but also check value-dependence of the expression as a whole. */
24268 bool
24269 instantiation_dependent_expression_p (tree expression)
24271 return (instantiation_dependent_uneval_expression_p (expression)
24272 || value_dependent_expression_p (expression));
24275 /* Like type_dependent_expression_p, but it also works while not processing
24276 a template definition, i.e. during substitution or mangling. */
24278 bool
24279 type_dependent_expression_p_push (tree expr)
24281 bool b;
24282 ++processing_template_decl;
24283 b = type_dependent_expression_p (expr);
24284 --processing_template_decl;
24285 return b;
24288 /* Returns TRUE if ARGS contains a type-dependent expression. */
24290 bool
24291 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24293 unsigned int i;
24294 tree arg;
24296 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24298 if (type_dependent_expression_p (arg))
24299 return true;
24301 return false;
24304 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24305 expressions) contains any type-dependent expressions. */
24307 bool
24308 any_type_dependent_elements_p (const_tree list)
24310 for (; list; list = TREE_CHAIN (list))
24311 if (type_dependent_expression_p (TREE_VALUE (list)))
24312 return true;
24314 return false;
24317 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24318 expressions) contains any value-dependent expressions. */
24320 bool
24321 any_value_dependent_elements_p (const_tree list)
24323 for (; list; list = TREE_CHAIN (list))
24324 if (value_dependent_expression_p (TREE_VALUE (list)))
24325 return true;
24327 return false;
24330 /* Returns TRUE if the ARG (a template argument) is dependent. */
24332 bool
24333 dependent_template_arg_p (tree arg)
24335 if (!processing_template_decl)
24336 return false;
24338 /* Assume a template argument that was wrongly written by the user
24339 is dependent. This is consistent with what
24340 any_dependent_template_arguments_p [that calls this function]
24341 does. */
24342 if (!arg || arg == error_mark_node)
24343 return true;
24345 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24346 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24348 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24349 return true;
24350 if (TREE_CODE (arg) == TEMPLATE_DECL)
24352 if (DECL_TEMPLATE_PARM_P (arg))
24353 return true;
24354 /* A member template of a dependent class is not necessarily
24355 type-dependent, but it is a dependent template argument because it
24356 will be a member of an unknown specialization to that template. */
24357 tree scope = CP_DECL_CONTEXT (arg);
24358 return TYPE_P (scope) && dependent_type_p (scope);
24360 else if (ARGUMENT_PACK_P (arg))
24362 tree args = ARGUMENT_PACK_ARGS (arg);
24363 int i, len = TREE_VEC_LENGTH (args);
24364 for (i = 0; i < len; ++i)
24366 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24367 return true;
24370 return false;
24372 else if (TYPE_P (arg))
24373 return dependent_type_p (arg);
24374 else
24375 return (type_dependent_expression_p (arg)
24376 || value_dependent_expression_p (arg));
24379 /* Returns true if ARGS (a collection of template arguments) contains
24380 any types that require structural equality testing. */
24382 bool
24383 any_template_arguments_need_structural_equality_p (tree args)
24385 int i;
24386 int j;
24388 if (!args)
24389 return false;
24390 if (args == error_mark_node)
24391 return true;
24393 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24395 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24396 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24398 tree arg = TREE_VEC_ELT (level, j);
24399 tree packed_args = NULL_TREE;
24400 int k, len = 1;
24402 if (ARGUMENT_PACK_P (arg))
24404 /* Look inside the argument pack. */
24405 packed_args = ARGUMENT_PACK_ARGS (arg);
24406 len = TREE_VEC_LENGTH (packed_args);
24409 for (k = 0; k < len; ++k)
24411 if (packed_args)
24412 arg = TREE_VEC_ELT (packed_args, k);
24414 if (error_operand_p (arg))
24415 return true;
24416 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24417 continue;
24418 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24419 return true;
24420 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24421 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24422 return true;
24427 return false;
24430 /* Returns true if ARGS (a collection of template arguments) contains
24431 any dependent arguments. */
24433 bool
24434 any_dependent_template_arguments_p (const_tree args)
24436 int i;
24437 int j;
24439 if (!args)
24440 return false;
24441 if (args == error_mark_node)
24442 return true;
24444 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24446 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24447 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24448 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24449 return true;
24452 return false;
24455 /* Returns TRUE if the template TMPL is type-dependent. */
24457 bool
24458 dependent_template_p (tree tmpl)
24460 if (TREE_CODE (tmpl) == OVERLOAD)
24462 for (lkp_iterator iter (tmpl); iter; ++iter)
24463 if (dependent_template_p (*iter))
24464 return true;
24465 return false;
24468 /* Template template parameters are dependent. */
24469 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24470 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24471 return true;
24472 /* So are names that have not been looked up. */
24473 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24474 return true;
24475 return false;
24478 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24480 bool
24481 dependent_template_id_p (tree tmpl, tree args)
24483 return (dependent_template_p (tmpl)
24484 || any_dependent_template_arguments_p (args));
24487 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24488 are dependent. */
24490 bool
24491 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24493 int i;
24495 if (!processing_template_decl)
24496 return false;
24498 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24500 tree decl = TREE_VEC_ELT (declv, i);
24501 tree init = TREE_VEC_ELT (initv, i);
24502 tree cond = TREE_VEC_ELT (condv, i);
24503 tree incr = TREE_VEC_ELT (incrv, i);
24505 if (type_dependent_expression_p (decl)
24506 || TREE_CODE (decl) == SCOPE_REF)
24507 return true;
24509 if (init && type_dependent_expression_p (init))
24510 return true;
24512 if (type_dependent_expression_p (cond))
24513 return true;
24515 if (COMPARISON_CLASS_P (cond)
24516 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24517 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24518 return true;
24520 if (TREE_CODE (incr) == MODOP_EXPR)
24522 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24523 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24524 return true;
24526 else if (type_dependent_expression_p (incr))
24527 return true;
24528 else if (TREE_CODE (incr) == MODIFY_EXPR)
24530 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24531 return true;
24532 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24534 tree t = TREE_OPERAND (incr, 1);
24535 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24536 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24537 return true;
24542 return false;
24545 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24546 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24547 no such TYPE can be found. Note that this function peers inside
24548 uninstantiated templates and therefore should be used only in
24549 extremely limited situations. ONLY_CURRENT_P restricts this
24550 peering to the currently open classes hierarchy (which is required
24551 when comparing types). */
24553 tree
24554 resolve_typename_type (tree type, bool only_current_p)
24556 tree scope;
24557 tree name;
24558 tree decl;
24559 int quals;
24560 tree pushed_scope;
24561 tree result;
24563 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24565 scope = TYPE_CONTEXT (type);
24566 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24567 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24568 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24569 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24570 identifier of the TYPENAME_TYPE anymore.
24571 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24572 TYPENAME_TYPE instead, we avoid messing up with a possible
24573 typedef variant case. */
24574 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24576 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24577 it first before we can figure out what NAME refers to. */
24578 if (TREE_CODE (scope) == TYPENAME_TYPE)
24580 if (TYPENAME_IS_RESOLVING_P (scope))
24581 /* Given a class template A with a dependent base with nested type C,
24582 typedef typename A::C::C C will land us here, as trying to resolve
24583 the initial A::C leads to the local C typedef, which leads back to
24584 A::C::C. So we break the recursion now. */
24585 return type;
24586 else
24587 scope = resolve_typename_type (scope, only_current_p);
24589 /* If we don't know what SCOPE refers to, then we cannot resolve the
24590 TYPENAME_TYPE. */
24591 if (!CLASS_TYPE_P (scope))
24592 return type;
24593 /* If this is a typedef, we don't want to look inside (c++/11987). */
24594 if (typedef_variant_p (type))
24595 return type;
24596 /* If SCOPE isn't the template itself, it will not have a valid
24597 TYPE_FIELDS list. */
24598 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24599 /* scope is either the template itself or a compatible instantiation
24600 like X<T>, so look up the name in the original template. */
24601 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24602 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24603 gcc_checking_assert (uses_template_parms (scope));
24604 /* If scope has no fields, it can't be a current instantiation. Check this
24605 before currently_open_class to avoid infinite recursion (71515). */
24606 if (!TYPE_FIELDS (scope))
24607 return type;
24608 /* If the SCOPE is not the current instantiation, there's no reason
24609 to look inside it. */
24610 if (only_current_p && !currently_open_class (scope))
24611 return type;
24612 /* Enter the SCOPE so that name lookup will be resolved as if we
24613 were in the class definition. In particular, SCOPE will no
24614 longer be considered a dependent type. */
24615 pushed_scope = push_scope (scope);
24616 /* Look up the declaration. */
24617 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24618 tf_warning_or_error);
24620 result = NULL_TREE;
24622 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24623 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24624 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24625 if (!decl)
24626 /*nop*/;
24627 else if (identifier_p (fullname)
24628 && TREE_CODE (decl) == TYPE_DECL)
24630 result = TREE_TYPE (decl);
24631 if (result == error_mark_node)
24632 result = NULL_TREE;
24634 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24635 && DECL_CLASS_TEMPLATE_P (decl))
24637 /* Obtain the template and the arguments. */
24638 tree tmpl = TREE_OPERAND (fullname, 0);
24639 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24641 /* We get here with a plain identifier because a previous tentative
24642 parse of the nested-name-specifier as part of a ptr-operator saw
24643 ::template X<A>. The use of ::template is necessary in a
24644 ptr-operator, but wrong in a declarator-id.
24646 [temp.names]: In a qualified-id of a declarator-id, the keyword
24647 template shall not appear at the top level. */
24648 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24649 "keyword %<template%> not allowed in declarator-id");
24650 tmpl = decl;
24652 tree args = TREE_OPERAND (fullname, 1);
24653 /* Instantiate the template. */
24654 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24655 /*entering_scope=*/true,
24656 tf_error | tf_user);
24657 if (result == error_mark_node)
24658 result = NULL_TREE;
24661 /* Leave the SCOPE. */
24662 if (pushed_scope)
24663 pop_scope (pushed_scope);
24665 /* If we failed to resolve it, return the original typename. */
24666 if (!result)
24667 return type;
24669 /* If lookup found a typename type, resolve that too. */
24670 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24672 /* Ill-formed programs can cause infinite recursion here, so we
24673 must catch that. */
24674 TYPENAME_IS_RESOLVING_P (result) = 1;
24675 result = resolve_typename_type (result, only_current_p);
24676 TYPENAME_IS_RESOLVING_P (result) = 0;
24679 /* Qualify the resulting type. */
24680 quals = cp_type_quals (type);
24681 if (quals)
24682 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24684 return result;
24687 /* EXPR is an expression which is not type-dependent. Return a proxy
24688 for EXPR that can be used to compute the types of larger
24689 expressions containing EXPR. */
24691 tree
24692 build_non_dependent_expr (tree expr)
24694 tree inner_expr;
24696 /* When checking, try to get a constant value for all non-dependent
24697 expressions in order to expose bugs in *_dependent_expression_p
24698 and constexpr. This can affect code generation, see PR70704, so
24699 only do this for -fchecking=2. */
24700 if (flag_checking > 1
24701 && cxx_dialect >= cxx11
24702 /* Don't do this during nsdmi parsing as it can lead to
24703 unexpected recursive instantiations. */
24704 && !parsing_nsdmi ()
24705 /* Don't do this during concept expansion either and for
24706 the same reason. */
24707 && !expanding_concept ())
24708 fold_non_dependent_expr (expr);
24710 /* Preserve OVERLOADs; the functions must be available to resolve
24711 types. */
24712 inner_expr = expr;
24713 if (TREE_CODE (inner_expr) == STMT_EXPR)
24714 inner_expr = stmt_expr_value_expr (inner_expr);
24715 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24716 inner_expr = TREE_OPERAND (inner_expr, 0);
24717 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24718 inner_expr = TREE_OPERAND (inner_expr, 1);
24719 if (is_overloaded_fn (inner_expr)
24720 || TREE_CODE (inner_expr) == OFFSET_REF)
24721 return expr;
24722 /* There is no need to return a proxy for a variable. */
24723 if (VAR_P (expr))
24724 return expr;
24725 /* Preserve string constants; conversions from string constants to
24726 "char *" are allowed, even though normally a "const char *"
24727 cannot be used to initialize a "char *". */
24728 if (TREE_CODE (expr) == STRING_CST)
24729 return expr;
24730 /* Preserve void and arithmetic constants, as an optimization -- there is no
24731 reason to create a new node. */
24732 if (TREE_CODE (expr) == VOID_CST
24733 || TREE_CODE (expr) == INTEGER_CST
24734 || TREE_CODE (expr) == REAL_CST)
24735 return expr;
24736 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24737 There is at least one place where we want to know that a
24738 particular expression is a throw-expression: when checking a ?:
24739 expression, there are special rules if the second or third
24740 argument is a throw-expression. */
24741 if (TREE_CODE (expr) == THROW_EXPR)
24742 return expr;
24744 /* Don't wrap an initializer list, we need to be able to look inside. */
24745 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24746 return expr;
24748 /* Don't wrap a dummy object, we need to be able to test for it. */
24749 if (is_dummy_object (expr))
24750 return expr;
24752 if (TREE_CODE (expr) == COND_EXPR)
24753 return build3 (COND_EXPR,
24754 TREE_TYPE (expr),
24755 TREE_OPERAND (expr, 0),
24756 (TREE_OPERAND (expr, 1)
24757 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24758 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24759 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24760 if (TREE_CODE (expr) == COMPOUND_EXPR
24761 && !COMPOUND_EXPR_OVERLOADED (expr))
24762 return build2 (COMPOUND_EXPR,
24763 TREE_TYPE (expr),
24764 TREE_OPERAND (expr, 0),
24765 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24767 /* If the type is unknown, it can't really be non-dependent */
24768 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24770 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24771 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24774 /* ARGS is a vector of expressions as arguments to a function call.
24775 Replace the arguments with equivalent non-dependent expressions.
24776 This modifies ARGS in place. */
24778 void
24779 make_args_non_dependent (vec<tree, va_gc> *args)
24781 unsigned int ix;
24782 tree arg;
24784 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24786 tree newarg = build_non_dependent_expr (arg);
24787 if (newarg != arg)
24788 (*args)[ix] = newarg;
24792 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24793 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24794 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24796 static tree
24797 make_auto_1 (tree name, bool set_canonical)
24799 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24800 TYPE_NAME (au) = build_decl (input_location,
24801 TYPE_DECL, name, au);
24802 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24803 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24804 (0, processing_template_decl + 1, processing_template_decl + 1,
24805 TYPE_NAME (au), NULL_TREE);
24806 if (set_canonical)
24807 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24808 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24809 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24811 return au;
24814 tree
24815 make_decltype_auto (void)
24817 return make_auto_1 (decltype_auto_identifier, true);
24820 tree
24821 make_auto (void)
24823 return make_auto_1 (auto_identifier, true);
24826 /* Return a C++17 deduction placeholder for class template TMPL. */
24828 tree
24829 make_template_placeholder (tree tmpl)
24831 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24832 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24833 return t;
24836 /* True iff T is a C++17 class template deduction placeholder. */
24838 bool
24839 template_placeholder_p (tree t)
24841 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
24844 /* Make a "constrained auto" type-specifier. This is an
24845 auto type with constraints that must be associated after
24846 deduction. The constraint is formed from the given
24847 CONC and its optional sequence of arguments, which are
24848 non-null if written as partial-concept-id. */
24850 tree
24851 make_constrained_auto (tree con, tree args)
24853 tree type = make_auto_1 (auto_identifier, false);
24855 /* Build the constraint. */
24856 tree tmpl = DECL_TI_TEMPLATE (con);
24857 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24858 expr = build_concept_check (expr, type, args);
24860 tree constr = normalize_expression (expr);
24861 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24863 /* Our canonical type depends on the constraint. */
24864 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24866 /* Attach the constraint to the type declaration. */
24867 tree decl = TYPE_NAME (type);
24868 return decl;
24871 /* Given type ARG, return std::initializer_list<ARG>. */
24873 static tree
24874 listify (tree arg)
24876 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24878 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24880 gcc_rich_location richloc (input_location);
24881 maybe_add_include_fixit (&richloc, "<initializer_list>");
24882 error_at_rich_loc (&richloc,
24883 "deducing from brace-enclosed initializer list"
24884 " requires #include <initializer_list>");
24886 return error_mark_node;
24888 tree argvec = make_tree_vec (1);
24889 TREE_VEC_ELT (argvec, 0) = arg;
24891 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24892 NULL_TREE, 0, tf_warning_or_error);
24895 /* Replace auto in TYPE with std::initializer_list<auto>. */
24897 static tree
24898 listify_autos (tree type, tree auto_node)
24900 tree init_auto = listify (auto_node);
24901 tree argvec = make_tree_vec (1);
24902 TREE_VEC_ELT (argvec, 0) = init_auto;
24903 if (processing_template_decl)
24904 argvec = add_to_template_args (current_template_args (), argvec);
24905 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24908 /* Hash traits for hashing possibly constrained 'auto'
24909 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24911 struct auto_hash : default_hash_traits<tree>
24913 static inline hashval_t hash (tree);
24914 static inline bool equal (tree, tree);
24917 /* Hash the 'auto' T. */
24919 inline hashval_t
24920 auto_hash::hash (tree t)
24922 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24923 /* Matching constrained-type-specifiers denote the same template
24924 parameter, so hash the constraint. */
24925 return hash_placeholder_constraint (c);
24926 else
24927 /* But unconstrained autos are all separate, so just hash the pointer. */
24928 return iterative_hash_object (t, 0);
24931 /* Compare two 'auto's. */
24933 inline bool
24934 auto_hash::equal (tree t1, tree t2)
24936 if (t1 == t2)
24937 return true;
24939 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24940 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24942 /* Two unconstrained autos are distinct. */
24943 if (!c1 || !c2)
24944 return false;
24946 return equivalent_placeholder_constraints (c1, c2);
24949 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24950 constrained) auto, add it to the vector. */
24952 static int
24953 extract_autos_r (tree t, void *data)
24955 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24956 if (is_auto (t))
24958 /* All the autos were built with index 0; fix that up now. */
24959 tree *p = hash.find_slot (t, INSERT);
24960 unsigned idx;
24961 if (*p)
24962 /* If this is a repeated constrained-type-specifier, use the index we
24963 chose before. */
24964 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24965 else
24967 /* Otherwise this is new, so use the current count. */
24968 *p = t;
24969 idx = hash.elements () - 1;
24971 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24974 /* Always keep walking. */
24975 return 0;
24978 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24979 says they can appear anywhere in the type. */
24981 static tree
24982 extract_autos (tree type)
24984 hash_set<tree> visited;
24985 hash_table<auto_hash> hash (2);
24987 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24989 tree tree_vec = make_tree_vec (hash.elements());
24990 for (hash_table<auto_hash>::iterator iter = hash.begin();
24991 iter != hash.end(); ++iter)
24993 tree elt = *iter;
24994 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24995 TREE_VEC_ELT (tree_vec, i)
24996 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24999 return tree_vec;
25002 /* The stem for deduction guide names. */
25003 const char *const dguide_base = "__dguide_";
25005 /* Return the name for a deduction guide for class template TMPL. */
25007 tree
25008 dguide_name (tree tmpl)
25010 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25011 tree tname = TYPE_IDENTIFIER (type);
25012 char *buf = (char *) alloca (1 + strlen (dguide_base)
25013 + IDENTIFIER_LENGTH (tname));
25014 memcpy (buf, dguide_base, strlen (dguide_base));
25015 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25016 IDENTIFIER_LENGTH (tname) + 1);
25017 tree dname = get_identifier (buf);
25018 TREE_TYPE (dname) = type;
25019 return dname;
25022 /* True if NAME is the name of a deduction guide. */
25024 bool
25025 dguide_name_p (tree name)
25027 return (TREE_TYPE (name)
25028 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25029 strlen (dguide_base)));
25032 /* True if FN is a deduction guide. */
25034 bool
25035 deduction_guide_p (const_tree fn)
25037 if (DECL_P (fn))
25038 if (tree name = DECL_NAME (fn))
25039 return dguide_name_p (name);
25040 return false;
25043 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25045 bool
25046 copy_guide_p (const_tree fn)
25048 gcc_assert (deduction_guide_p (fn));
25049 if (!DECL_ARTIFICIAL (fn))
25050 return false;
25051 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25052 return (TREE_CHAIN (parms) == void_list_node
25053 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25056 /* True if FN is a guide generated from a constructor template. */
25058 bool
25059 template_guide_p (const_tree fn)
25061 gcc_assert (deduction_guide_p (fn));
25062 if (!DECL_ARTIFICIAL (fn))
25063 return false;
25064 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25066 tree tmpl = DECL_TI_TEMPLATE (ctor);
25067 return PRIMARY_TEMPLATE_P (tmpl);
25069 return false;
25072 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25073 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25074 template parameter types. Note that the handling of template template
25075 parameters relies on current_template_parms being set appropriately for the
25076 new template. */
25078 static tree
25079 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25080 tree tsubst_args, tsubst_flags_t complain)
25082 tree oldidx = get_template_parm_index (olddecl);
25084 tree newtype;
25085 if (TREE_CODE (olddecl) == TYPE_DECL
25086 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25088 tree oldtype = TREE_TYPE (olddecl);
25089 newtype = cxx_make_type (TREE_CODE (oldtype));
25090 TYPE_MAIN_VARIANT (newtype) = newtype;
25091 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25092 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25093 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25095 else
25096 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25097 complain, NULL_TREE);
25099 tree newdecl
25100 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25101 DECL_NAME (olddecl), newtype);
25102 SET_DECL_TEMPLATE_PARM_P (newdecl);
25104 tree newidx;
25105 if (TREE_CODE (olddecl) == TYPE_DECL
25106 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25108 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25109 = build_template_parm_index (index, level, level,
25110 newdecl, newtype);
25111 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25112 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25113 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25114 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25116 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25118 DECL_TEMPLATE_RESULT (newdecl)
25119 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25120 DECL_NAME (olddecl), newtype);
25121 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25122 // First create a copy (ttargs) of tsubst_args with an
25123 // additional level for the template template parameter's own
25124 // template parameters (ttparms).
25125 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25126 (DECL_TEMPLATE_PARMS (olddecl)));
25127 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25128 tree ttargs = make_tree_vec (depth + 1);
25129 for (int i = 0; i < depth; ++i)
25130 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25131 TREE_VEC_ELT (ttargs, depth)
25132 = template_parms_level_to_args (ttparms);
25133 // Substitute ttargs into ttparms to fix references to
25134 // other template parameters.
25135 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25136 complain);
25137 // Now substitute again with args based on tparms, to reduce
25138 // the level of the ttparms.
25139 ttargs = current_template_args ();
25140 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25141 complain);
25142 // Finally, tack the adjusted parms onto tparms.
25143 ttparms = tree_cons (size_int (depth), ttparms,
25144 current_template_parms);
25145 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25148 else
25150 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25151 tree newconst
25152 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25153 TREE_CODE (oldconst),
25154 DECL_NAME (oldconst), newtype);
25155 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25156 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25157 SET_DECL_TEMPLATE_PARM_P (newconst);
25158 newidx = build_template_parm_index (index, level, level,
25159 newconst, newtype);
25160 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25161 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25162 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25165 return newdecl;
25168 /* Returns a C++17 class deduction guide template based on the constructor
25169 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25170 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25172 static tree
25173 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25175 tree type, tparms, targs, fparms, fargs, ci;
25176 bool memtmpl = false;
25177 bool explicit_p;
25178 location_t loc;
25180 if (TYPE_P (ctor))
25182 type = ctor;
25183 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25184 if (copy_p)
25186 type = TREE_TYPE (type);
25187 fparms = tree_cons (NULL_TREE, type, void_list_node);
25189 else
25190 fparms = void_list_node;
25192 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25193 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25194 targs = CLASSTYPE_TI_ARGS (type);
25195 ci = NULL_TREE;
25196 fargs = NULL_TREE;
25197 loc = DECL_SOURCE_LOCATION (ctmpl);
25198 explicit_p = false;
25200 else
25202 ++processing_template_decl;
25204 tree fn_tmpl
25205 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25206 : DECL_TI_TEMPLATE (ctor));
25207 if (outer_args)
25208 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25209 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25211 type = DECL_CONTEXT (ctor);
25213 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25214 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25215 fully specialized args for the enclosing class. Strip those off, as
25216 the deduction guide won't have those template parameters. */
25217 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25218 TMPL_PARMS_DEPTH (tparms));
25219 /* Discard the 'this' parameter. */
25220 fparms = FUNCTION_ARG_CHAIN (ctor);
25221 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25222 ci = get_constraints (ctor);
25223 loc = DECL_SOURCE_LOCATION (ctor);
25224 explicit_p = DECL_NONCONVERTING_P (ctor);
25226 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25228 memtmpl = true;
25230 /* For a member template constructor, we need to flatten the two
25231 template parameter lists into one, and then adjust the function
25232 signature accordingly. This gets...complicated. */
25233 tree save_parms = current_template_parms;
25235 /* For a member template we should have two levels of parms/args, one
25236 for the class and one for the constructor. We stripped
25237 specialized args for further enclosing classes above. */
25238 const int depth = 2;
25239 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25241 /* Template args for translating references to the two-level template
25242 parameters into references to the one-level template parameters we
25243 are creating. */
25244 tree tsubst_args = copy_node (targs);
25245 TMPL_ARGS_LEVEL (tsubst_args, depth)
25246 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25248 /* Template parms for the constructor template. */
25249 tree ftparms = TREE_VALUE (tparms);
25250 unsigned flen = TREE_VEC_LENGTH (ftparms);
25251 /* Template parms for the class template. */
25252 tparms = TREE_CHAIN (tparms);
25253 tree ctparms = TREE_VALUE (tparms);
25254 unsigned clen = TREE_VEC_LENGTH (ctparms);
25255 /* Template parms for the deduction guide start as a copy of the
25256 template parms for the class. We set current_template_parms for
25257 lookup_template_class_1. */
25258 current_template_parms = tparms = copy_node (tparms);
25259 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25260 for (unsigned i = 0; i < clen; ++i)
25261 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25263 /* Now we need to rewrite the constructor parms to append them to the
25264 class parms. */
25265 for (unsigned i = 0; i < flen; ++i)
25267 unsigned index = i + clen;
25268 unsigned level = 1;
25269 tree oldelt = TREE_VEC_ELT (ftparms, i);
25270 tree olddecl = TREE_VALUE (oldelt);
25271 tree newdecl = rewrite_template_parm (olddecl, index, level,
25272 tsubst_args, complain);
25273 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25274 tsubst_args, complain, ctor);
25275 tree list = build_tree_list (newdef, newdecl);
25276 TEMPLATE_PARM_CONSTRAINTS (list)
25277 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25278 tsubst_args, complain, ctor);
25279 TREE_VEC_ELT (new_vec, index) = list;
25280 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25283 /* Now we have a final set of template parms to substitute into the
25284 function signature. */
25285 targs = template_parms_to_args (tparms);
25286 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25287 complain, ctor);
25288 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25289 if (ci)
25290 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25292 current_template_parms = save_parms;
25294 --processing_template_decl;
25297 if (!memtmpl)
25299 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25300 tparms = copy_node (tparms);
25301 INNERMOST_TEMPLATE_PARMS (tparms)
25302 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25305 tree fntype = build_function_type (type, fparms);
25306 tree ded_fn = build_lang_decl_loc (loc,
25307 FUNCTION_DECL,
25308 dguide_name (type), fntype);
25309 DECL_ARGUMENTS (ded_fn) = fargs;
25310 DECL_ARTIFICIAL (ded_fn) = true;
25311 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25312 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25313 DECL_ARTIFICIAL (ded_tmpl) = true;
25314 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25315 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25316 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25317 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25318 if (DECL_P (ctor))
25319 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25320 if (ci)
25321 set_constraints (ded_tmpl, ci);
25323 return ded_tmpl;
25326 /* Deduce template arguments for the class template placeholder PTYPE for
25327 template TMPL based on the initializer INIT, and return the resulting
25328 type. */
25330 static tree
25331 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25332 tsubst_flags_t complain)
25334 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25336 /* We should have handled this in the caller. */
25337 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25338 return ptype;
25339 if (complain & tf_error)
25340 error ("non-class template %qT used without template arguments", tmpl);
25341 return error_mark_node;
25344 tree type = TREE_TYPE (tmpl);
25346 bool try_list_ctor = false;
25348 vec<tree,va_gc> *args;
25349 if (init == NULL_TREE
25350 || TREE_CODE (init) == TREE_LIST)
25351 args = make_tree_vector_from_list (init);
25352 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25354 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25355 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25357 /* As an exception, the first phase in 16.3.1.7 (considering the
25358 initializer list as a single argument) is omitted if the
25359 initializer list consists of a single expression of type cv U,
25360 where U is a specialization of C or a class derived from a
25361 specialization of C. */
25362 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25363 tree etype = TREE_TYPE (elt);
25365 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25366 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25367 int err = unify (tparms, targs, type, etype,
25368 UNIFY_ALLOW_DERIVED, /*explain*/false);
25369 if (err == 0)
25370 try_list_ctor = false;
25371 ggc_free (targs);
25373 if (try_list_ctor || is_std_init_list (type))
25374 args = make_tree_vector_single (init);
25375 else
25376 args = make_tree_vector_from_ctor (init);
25378 else
25379 args = make_tree_vector_single (init);
25381 tree dname = dguide_name (tmpl);
25382 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25383 /*type*/false, /*complain*/false,
25384 /*hidden*/false);
25385 bool elided = false;
25386 if (cands == error_mark_node)
25387 cands = NULL_TREE;
25389 /* Prune explicit deduction guides in copy-initialization context. */
25390 if (flags & LOOKUP_ONLYCONVERTING)
25392 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25393 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25394 elided = true;
25396 if (elided)
25398 /* Found a nonconverting guide, prune the candidates. */
25399 tree pruned = NULL_TREE;
25400 for (lkp_iterator iter (cands); iter; ++iter)
25401 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25402 pruned = lookup_add (*iter, pruned);
25404 cands = pruned;
25408 tree outer_args = NULL_TREE;
25409 if (DECL_CLASS_SCOPE_P (tmpl)
25410 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25412 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25413 type = TREE_TYPE (most_general_template (tmpl));
25416 bool saw_ctor = false;
25417 if (CLASSTYPE_METHOD_VEC (type))
25418 // FIXME cache artificial deduction guides
25419 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25420 iter; ++iter)
25422 tree guide = build_deduction_guide (*iter, outer_args, complain);
25423 if ((flags & LOOKUP_ONLYCONVERTING)
25424 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25425 elided = true;
25426 else
25427 cands = lookup_add (guide, cands);
25429 saw_ctor = true;
25432 tree call = error_mark_node;
25434 /* If this is list-initialization and the class has a list constructor, first
25435 try deducing from the list as a single argument, as [over.match.list]. */
25436 tree list_cands = NULL_TREE;
25437 if (try_list_ctor && cands)
25438 for (lkp_iterator iter (cands); iter; ++iter)
25440 tree dg = *iter;
25441 if (is_list_ctor (dg))
25442 list_cands = lookup_add (dg, list_cands);
25444 if (list_cands)
25446 ++cp_unevaluated_operand;
25447 call = build_new_function_call (list_cands, &args, tf_decltype);
25448 --cp_unevaluated_operand;
25450 if (call == error_mark_node)
25452 /* That didn't work, now try treating the list as a sequence of
25453 arguments. */
25454 release_tree_vector (args);
25455 args = make_tree_vector_from_ctor (init);
25459 /* Maybe generate an implicit deduction guide. */
25460 if (call == error_mark_node && args->length () < 2)
25462 tree gtype = NULL_TREE;
25464 if (args->length () == 1)
25465 /* Generate a copy guide. */
25466 gtype = build_reference_type (type);
25467 else if (!saw_ctor)
25468 /* Generate a default guide. */
25469 gtype = type;
25471 if (gtype)
25473 tree guide = build_deduction_guide (gtype, outer_args, complain);
25474 cands = lookup_add (guide, cands);
25478 if (elided && !cands)
25480 error ("cannot deduce template arguments for copy-initialization"
25481 " of %qT, as it has no non-explicit deduction guides or "
25482 "user-declared constructors", type);
25483 return error_mark_node;
25485 else if (!cands && call == error_mark_node)
25487 error ("cannot deduce template arguments of %qT, as it has no viable "
25488 "deduction guides", type);
25489 return error_mark_node;
25492 if (call == error_mark_node)
25494 ++cp_unevaluated_operand;
25495 call = build_new_function_call (cands, &args, tf_decltype);
25496 --cp_unevaluated_operand;
25499 if (call == error_mark_node && (complain & tf_warning_or_error))
25501 error ("class template argument deduction failed:");
25503 ++cp_unevaluated_operand;
25504 call = build_new_function_call (cands, &args, complain | tf_decltype);
25505 --cp_unevaluated_operand;
25507 if (elided)
25508 inform (input_location, "explicit deduction guides not considered "
25509 "for copy-initialization");
25512 release_tree_vector (args);
25514 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25517 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25518 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25520 tree
25521 do_auto_deduction (tree type, tree init, tree auto_node)
25523 return do_auto_deduction (type, init, auto_node,
25524 tf_warning_or_error,
25525 adc_unspecified);
25528 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25529 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25530 The CONTEXT determines the context in which auto deduction is performed
25531 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25532 OUTER_TARGS are used during template argument deduction
25533 (context == adc_unify) to properly substitute the result, and is ignored
25534 in other contexts.
25536 For partial-concept-ids, extra args may be appended to the list of deduced
25537 template arguments prior to determining constraint satisfaction. */
25539 tree
25540 do_auto_deduction (tree type, tree init, tree auto_node,
25541 tsubst_flags_t complain, auto_deduction_context context,
25542 tree outer_targs, int flags)
25544 tree targs;
25546 if (init == error_mark_node)
25547 return error_mark_node;
25549 if (init && type_dependent_expression_p (init)
25550 && context != adc_unify)
25551 /* Defining a subset of type-dependent expressions that we can deduce
25552 from ahead of time isn't worth the trouble. */
25553 return type;
25555 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25556 /* C++17 class template argument deduction. */
25557 return do_class_deduction (type, tmpl, init, flags, complain);
25559 if (TREE_TYPE (init) == NULL_TREE)
25560 /* Nothing we can do with this, even in deduction context. */
25561 return type;
25563 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25564 with either a new invented type template parameter U or, if the
25565 initializer is a braced-init-list (8.5.4), with
25566 std::initializer_list<U>. */
25567 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25569 if (!DIRECT_LIST_INIT_P (init))
25570 type = listify_autos (type, auto_node);
25571 else if (CONSTRUCTOR_NELTS (init) == 1)
25572 init = CONSTRUCTOR_ELT (init, 0)->value;
25573 else
25575 if (complain & tf_warning_or_error)
25577 if (permerror (input_location, "direct-list-initialization of "
25578 "%<auto%> requires exactly one element"))
25579 inform (input_location,
25580 "for deduction to %<std::initializer_list%>, use copy-"
25581 "list-initialization (i.e. add %<=%> before the %<{%>)");
25583 type = listify_autos (type, auto_node);
25587 if (type == error_mark_node)
25588 return error_mark_node;
25590 init = resolve_nondeduced_context (init, complain);
25592 if (context == adc_decomp_type
25593 && auto_node == type
25594 && init != error_mark_node
25595 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25596 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25597 and initializer has array type, deduce cv-qualified array type. */
25598 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25599 complain);
25600 else if (AUTO_IS_DECLTYPE (auto_node))
25602 bool id = (DECL_P (init)
25603 || ((TREE_CODE (init) == COMPONENT_REF
25604 || TREE_CODE (init) == SCOPE_REF)
25605 && !REF_PARENTHESIZED_P (init)));
25606 targs = make_tree_vec (1);
25607 TREE_VEC_ELT (targs, 0)
25608 = finish_decltype_type (init, id, tf_warning_or_error);
25609 if (type != auto_node)
25611 if (complain & tf_error)
25612 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25613 return error_mark_node;
25616 else
25618 tree parms = build_tree_list (NULL_TREE, type);
25619 tree tparms;
25621 if (flag_concepts)
25622 tparms = extract_autos (type);
25623 else
25625 tparms = make_tree_vec (1);
25626 TREE_VEC_ELT (tparms, 0)
25627 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25630 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25631 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25632 DEDUCE_CALL, LOOKUP_NORMAL,
25633 NULL, /*explain_p=*/false);
25634 if (val > 0)
25636 if (processing_template_decl)
25637 /* Try again at instantiation time. */
25638 return type;
25639 if (type && type != error_mark_node
25640 && (complain & tf_error))
25641 /* If type is error_mark_node a diagnostic must have been
25642 emitted by now. Also, having a mention to '<type error>'
25643 in the diagnostic is not really useful to the user. */
25645 if (cfun && auto_node == current_function_auto_return_pattern
25646 && LAMBDA_FUNCTION_P (current_function_decl))
25647 error ("unable to deduce lambda return type from %qE", init);
25648 else
25649 error ("unable to deduce %qT from %qE", type, init);
25650 type_unification_real (tparms, targs, parms, &init, 1, 0,
25651 DEDUCE_CALL, LOOKUP_NORMAL,
25652 NULL, /*explain_p=*/true);
25654 return error_mark_node;
25658 /* Check any placeholder constraints against the deduced type. */
25659 if (flag_concepts && !processing_template_decl)
25660 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25662 /* Use the deduced type to check the associated constraints. If we
25663 have a partial-concept-id, rebuild the argument list so that
25664 we check using the extra arguments. */
25665 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25666 tree cargs = CHECK_CONSTR_ARGS (constr);
25667 if (TREE_VEC_LENGTH (cargs) > 1)
25669 cargs = copy_node (cargs);
25670 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25672 else
25673 cargs = targs;
25674 if (!constraints_satisfied_p (constr, cargs))
25676 if (complain & tf_warning_or_error)
25678 switch (context)
25680 case adc_unspecified:
25681 case adc_unify:
25682 error("placeholder constraints not satisfied");
25683 break;
25684 case adc_variable_type:
25685 case adc_decomp_type:
25686 error ("deduced initializer does not satisfy "
25687 "placeholder constraints");
25688 break;
25689 case adc_return_type:
25690 error ("deduced return type does not satisfy "
25691 "placeholder constraints");
25692 break;
25693 case adc_requirement:
25694 error ("deduced expression type does not satisfy "
25695 "placeholder constraints");
25696 break;
25698 diagnose_constraints (input_location, constr, targs);
25700 return error_mark_node;
25704 if (processing_template_decl && context != adc_unify)
25705 outer_targs = current_template_args ();
25706 targs = add_to_template_args (outer_targs, targs);
25707 return tsubst (type, targs, complain, NULL_TREE);
25710 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25711 result. */
25713 tree
25714 splice_late_return_type (tree type, tree late_return_type)
25716 if (is_auto (type))
25718 if (late_return_type)
25719 return late_return_type;
25721 tree idx = get_template_parm_index (type);
25722 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25723 /* In an abbreviated function template we didn't know we were dealing
25724 with a function template when we saw the auto return type, so update
25725 it to have the correct level. */
25726 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25728 return type;
25731 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25732 'decltype(auto)' or a deduced class template. */
25734 bool
25735 is_auto (const_tree type)
25737 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25738 && (TYPE_IDENTIFIER (type) == auto_identifier
25739 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25740 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25741 return true;
25742 else
25743 return false;
25746 /* for_each_template_parm callback for type_uses_auto. */
25749 is_auto_r (tree tp, void */*data*/)
25751 return is_auto (tp);
25754 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25755 a use of `auto'. Returns NULL_TREE otherwise. */
25757 tree
25758 type_uses_auto (tree type)
25760 if (type == NULL_TREE)
25761 return NULL_TREE;
25762 else if (flag_concepts)
25764 /* The Concepts TS allows multiple autos in one type-specifier; just
25765 return the first one we find, do_auto_deduction will collect all of
25766 them. */
25767 if (uses_template_parms (type))
25768 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25769 /*visited*/NULL, /*nondeduced*/true);
25770 else
25771 return NULL_TREE;
25773 else
25774 return find_type_usage (type, is_auto);
25777 /* For a given template T, return the vector of typedefs referenced
25778 in T for which access check is needed at T instantiation time.
25779 T is either a FUNCTION_DECL or a RECORD_TYPE.
25780 Those typedefs were added to T by the function
25781 append_type_to_template_for_access_check. */
25783 vec<qualified_typedef_usage_t, va_gc> *
25784 get_types_needing_access_check (tree t)
25786 tree ti;
25787 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25789 if (!t || t == error_mark_node)
25790 return NULL;
25792 if (!(ti = get_template_info (t)))
25793 return NULL;
25795 if (CLASS_TYPE_P (t)
25796 || TREE_CODE (t) == FUNCTION_DECL)
25798 if (!TI_TEMPLATE (ti))
25799 return NULL;
25801 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25804 return result;
25807 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25808 tied to T. That list of typedefs will be access checked at
25809 T instantiation time.
25810 T is either a FUNCTION_DECL or a RECORD_TYPE.
25811 TYPE_DECL is a TYPE_DECL node representing a typedef.
25812 SCOPE is the scope through which TYPE_DECL is accessed.
25813 LOCATION is the location of the usage point of TYPE_DECL.
25815 This function is a subroutine of
25816 append_type_to_template_for_access_check. */
25818 static void
25819 append_type_to_template_for_access_check_1 (tree t,
25820 tree type_decl,
25821 tree scope,
25822 location_t location)
25824 qualified_typedef_usage_t typedef_usage;
25825 tree ti;
25827 if (!t || t == error_mark_node)
25828 return;
25830 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25831 || CLASS_TYPE_P (t))
25832 && type_decl
25833 && TREE_CODE (type_decl) == TYPE_DECL
25834 && scope);
25836 if (!(ti = get_template_info (t)))
25837 return;
25839 gcc_assert (TI_TEMPLATE (ti));
25841 typedef_usage.typedef_decl = type_decl;
25842 typedef_usage.context = scope;
25843 typedef_usage.locus = location;
25845 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25848 /* Append TYPE_DECL to the template TEMPL.
25849 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25850 At TEMPL instanciation time, TYPE_DECL will be checked to see
25851 if it can be accessed through SCOPE.
25852 LOCATION is the location of the usage point of TYPE_DECL.
25854 e.g. consider the following code snippet:
25856 class C
25858 typedef int myint;
25861 template<class U> struct S
25863 C::myint mi; // <-- usage point of the typedef C::myint
25866 S<char> s;
25868 At S<char> instantiation time, we need to check the access of C::myint
25869 In other words, we need to check the access of the myint typedef through
25870 the C scope. For that purpose, this function will add the myint typedef
25871 and the scope C through which its being accessed to a list of typedefs
25872 tied to the template S. That list will be walked at template instantiation
25873 time and access check performed on each typedefs it contains.
25874 Note that this particular code snippet should yield an error because
25875 myint is private to C. */
25877 void
25878 append_type_to_template_for_access_check (tree templ,
25879 tree type_decl,
25880 tree scope,
25881 location_t location)
25883 qualified_typedef_usage_t *iter;
25884 unsigned i;
25886 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25888 /* Make sure we don't append the type to the template twice. */
25889 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25890 if (iter->typedef_decl == type_decl && scope == iter->context)
25891 return;
25893 append_type_to_template_for_access_check_1 (templ, type_decl,
25894 scope, location);
25897 /* Convert the generic type parameters in PARM that match the types given in the
25898 range [START_IDX, END_IDX) from the current_template_parms into generic type
25899 packs. */
25901 tree
25902 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25904 tree current = current_template_parms;
25905 int depth = TMPL_PARMS_DEPTH (current);
25906 current = INNERMOST_TEMPLATE_PARMS (current);
25907 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25909 for (int i = 0; i < start_idx; ++i)
25910 TREE_VEC_ELT (replacement, i)
25911 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25913 for (int i = start_idx; i < end_idx; ++i)
25915 /* Create a distinct parameter pack type from the current parm and add it
25916 to the replacement args to tsubst below into the generic function
25917 parameter. */
25919 tree o = TREE_TYPE (TREE_VALUE
25920 (TREE_VEC_ELT (current, i)));
25921 tree t = copy_type (o);
25922 TEMPLATE_TYPE_PARM_INDEX (t)
25923 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25924 o, 0, 0, tf_none);
25925 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25926 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25927 TYPE_MAIN_VARIANT (t) = t;
25928 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25929 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25930 TREE_VEC_ELT (replacement, i) = t;
25931 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25934 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25935 TREE_VEC_ELT (replacement, i)
25936 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25938 /* If there are more levels then build up the replacement with the outer
25939 template parms. */
25940 if (depth > 1)
25941 replacement = add_to_template_args (template_parms_to_args
25942 (TREE_CHAIN (current_template_parms)),
25943 replacement);
25945 return tsubst (parm, replacement, tf_none, NULL_TREE);
25948 /* Entries in the decl_constraint hash table. */
25949 struct GTY((for_user)) constr_entry
25951 tree decl;
25952 tree ci;
25955 /* Hashing function and equality for constraint entries. */
25956 struct constr_hasher : ggc_ptr_hash<constr_entry>
25958 static hashval_t hash (constr_entry *e)
25960 return (hashval_t)DECL_UID (e->decl);
25963 static bool equal (constr_entry *e1, constr_entry *e2)
25965 return e1->decl == e2->decl;
25969 /* A mapping from declarations to constraint information. Note that
25970 both templates and their underlying declarations are mapped to the
25971 same constraint information.
25973 FIXME: This is defined in pt.c because garbage collection
25974 code is not being generated for constraint.cc. */
25976 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25978 /* Returns the template constraints of declaration T. If T is not
25979 constrained, return NULL_TREE. Note that T must be non-null. */
25981 tree
25982 get_constraints (tree t)
25984 if (!flag_concepts)
25985 return NULL_TREE;
25987 gcc_assert (DECL_P (t));
25988 if (TREE_CODE (t) == TEMPLATE_DECL)
25989 t = DECL_TEMPLATE_RESULT (t);
25990 constr_entry elt = { t, NULL_TREE };
25991 constr_entry* found = decl_constraints->find (&elt);
25992 if (found)
25993 return found->ci;
25994 else
25995 return NULL_TREE;
25998 /* Associate the given constraint information CI with the declaration
25999 T. If T is a template, then the constraints are associated with
26000 its underlying declaration. Don't build associations if CI is
26001 NULL_TREE. */
26003 void
26004 set_constraints (tree t, tree ci)
26006 if (!ci)
26007 return;
26008 gcc_assert (t && flag_concepts);
26009 if (TREE_CODE (t) == TEMPLATE_DECL)
26010 t = DECL_TEMPLATE_RESULT (t);
26011 gcc_assert (!get_constraints (t));
26012 constr_entry elt = {t, ci};
26013 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26014 constr_entry* entry = ggc_alloc<constr_entry> ();
26015 *entry = elt;
26016 *slot = entry;
26019 /* Remove the associated constraints of the declaration T. */
26021 void
26022 remove_constraints (tree t)
26024 gcc_assert (DECL_P (t));
26025 if (TREE_CODE (t) == TEMPLATE_DECL)
26026 t = DECL_TEMPLATE_RESULT (t);
26028 constr_entry elt = {t, NULL_TREE};
26029 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26030 if (slot)
26031 decl_constraints->clear_slot (slot);
26034 /* Memoized satisfaction results for declarations. This
26035 maps the pair (constraint_info, arguments) to the result computed
26036 by constraints_satisfied_p. */
26038 struct GTY((for_user)) constraint_sat_entry
26040 tree ci;
26041 tree args;
26042 tree result;
26045 /* Hashing function and equality for constraint entries. */
26047 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26049 static hashval_t hash (constraint_sat_entry *e)
26051 hashval_t val = iterative_hash_object(e->ci, 0);
26052 return iterative_hash_template_arg (e->args, val);
26055 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26057 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26061 /* Memoized satisfaction results for concept checks. */
26063 struct GTY((for_user)) concept_spec_entry
26065 tree tmpl;
26066 tree args;
26067 tree result;
26070 /* Hashing function and equality for constraint entries. */
26072 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26074 static hashval_t hash (concept_spec_entry *e)
26076 return hash_tmpl_and_args (e->tmpl, e->args);
26079 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26081 ++comparing_specializations;
26082 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26083 --comparing_specializations;
26084 return eq;
26088 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26089 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26091 /* Search for a memoized satisfaction result. Returns one of the
26092 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26094 tree
26095 lookup_constraint_satisfaction (tree ci, tree args)
26097 constraint_sat_entry elt = { ci, args, NULL_TREE };
26098 constraint_sat_entry* found = constraint_memos->find (&elt);
26099 if (found)
26100 return found->result;
26101 else
26102 return NULL_TREE;
26105 /* Memoize the result of a satisfication test. Returns the saved result. */
26107 tree
26108 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26110 constraint_sat_entry elt = {ci, args, result};
26111 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26112 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26113 *entry = elt;
26114 *slot = entry;
26115 return result;
26118 /* Search for a memoized satisfaction result for a concept. */
26120 tree
26121 lookup_concept_satisfaction (tree tmpl, tree args)
26123 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26124 concept_spec_entry* found = concept_memos->find (&elt);
26125 if (found)
26126 return found->result;
26127 else
26128 return NULL_TREE;
26131 /* Memoize the result of a concept check. Returns the saved result. */
26133 tree
26134 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26136 concept_spec_entry elt = {tmpl, args, result};
26137 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26138 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26139 *entry = elt;
26140 *slot = entry;
26141 return result;
26144 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26146 /* Returns a prior concept specialization. This returns the substituted
26147 and normalized constraints defined by the concept. */
26149 tree
26150 get_concept_expansion (tree tmpl, tree args)
26152 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26153 concept_spec_entry* found = concept_expansions->find (&elt);
26154 if (found)
26155 return found->result;
26156 else
26157 return NULL_TREE;
26160 /* Save a concept expansion for later. */
26162 tree
26163 save_concept_expansion (tree tmpl, tree args, tree def)
26165 concept_spec_entry elt = {tmpl, args, def};
26166 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26167 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26168 *entry = elt;
26169 *slot = entry;
26170 return def;
26173 static hashval_t
26174 hash_subsumption_args (tree t1, tree t2)
26176 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26177 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26178 int val = 0;
26179 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26180 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26181 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26182 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26183 return val;
26186 /* Compare the constraints of two subsumption entries. The LEFT1 and
26187 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26188 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26190 static bool
26191 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26193 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26194 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26195 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26196 CHECK_CONSTR_ARGS (right1)))
26197 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26198 CHECK_CONSTR_ARGS (right2));
26199 return false;
26202 /* Key/value pair for learning and memoizing subsumption results. This
26203 associates a pair of check constraints (including arguments) with
26204 a boolean value indicating the result. */
26206 struct GTY((for_user)) subsumption_entry
26208 tree t1;
26209 tree t2;
26210 bool result;
26213 /* Hashing function and equality for constraint entries. */
26215 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26217 static hashval_t hash (subsumption_entry *e)
26219 return hash_subsumption_args (e->t1, e->t2);
26222 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26224 ++comparing_specializations;
26225 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26226 --comparing_specializations;
26227 return eq;
26231 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26233 /* Search for a previously cached subsumption result. */
26235 bool*
26236 lookup_subsumption_result (tree t1, tree t2)
26238 subsumption_entry elt = { t1, t2, false };
26239 subsumption_entry* found = subsumption_table->find (&elt);
26240 if (found)
26241 return &found->result;
26242 else
26243 return 0;
26246 /* Save a subsumption result. */
26248 bool
26249 save_subsumption_result (tree t1, tree t2, bool result)
26251 subsumption_entry elt = {t1, t2, result};
26252 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26253 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26254 *entry = elt;
26255 *slot = entry;
26256 return result;
26259 /* Set up the hash table for constraint association. */
26261 void
26262 init_constraint_processing (void)
26264 if (!flag_concepts)
26265 return;
26267 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26268 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26269 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26270 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26271 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26274 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26275 0..N-1. */
26277 void
26278 declare_integer_pack (void)
26280 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26281 build_function_type_list (integer_type_node,
26282 integer_type_node,
26283 NULL_TREE),
26284 NULL_TREE, ECF_CONST);
26285 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26286 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26289 /* Set up the hash tables for template instantiations. */
26291 void
26292 init_template_processing (void)
26294 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26295 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26297 if (cxx_dialect >= cxx11)
26298 declare_integer_pack ();
26301 /* Print stats about the template hash tables for -fstats. */
26303 void
26304 print_template_statistics (void)
26306 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26307 "%f collisions\n", (long) decl_specializations->size (),
26308 (long) decl_specializations->elements (),
26309 decl_specializations->collisions ());
26310 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26311 "%f collisions\n", (long) type_specializations->size (),
26312 (long) type_specializations->elements (),
26313 type_specializations->collisions ());
26316 #include "gt-cp-pt.h"